can信号车速读取模拟
// PRACTICE5.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include"stdio.h"
#include "Windows.h"
#include "stdlib.h"
#include "time.h"
//
#include"stdio.h"
#include "Windows.h"
#include "stdlib.h"
#include "time.h"
#define PRACTICE5_SHOW_CONFIG (0U)
#define PRACTICE5_CAN_ID (0xAAU)
#define PRACTICE5_CAN_DIC (8U)
#define PRACTICE5_CAN_DATALENGTH (13U)
#define PRACTICE5_CAN_STARTBIT (16U)
#define PRACTICE5_CAN_MAX (0x1fffU)
#define PRACTICE5_DATA_MAX (0x1900U)
#define PRACTICE5_DATA_RANGE (0x190U)
#define PRACTICE5_TIME_100MS (5U)
#define PRACTICE5_VEHICLE_SPEEDMAX (360)
#define PRACTICE5_LAST_BIT (1U)
#define PRACTICE5_SPEED_ACCURACY (5625U)
#define PRACTICE5_CALCULATE_EXPAND (100000U)
#define PRACTICE5_TIME_20 (20U)
#define PRACTICE5_ZERO (0U)
#define PRACTICE5_MILLEAGE_HOUR (1U)
#define PRACTICE5_MILLEAGE_MAX (999999U)
#define PRACTICE5_CONTRL_50 (50U)
#define PRACTICE5_CAN_ID (0xAAU)
#define PRACTICE5_CAN_DIC (8U)
#define PRACTICE5_CAN_DATALENGTH (13U)
#define PRACTICE5_CAN_STARTBIT (16U)
#define PRACTICE5_CAN_MAX (0x1fffU)
#define PRACTICE5_DATA_MAX (0x1900U)
#define PRACTICE5_DATA_RANGE (0x190U)
#define PRACTICE5_TIME_100MS (5U)
#define PRACTICE5_VEHICLE_SPEEDMAX (360)
#define PRACTICE5_LAST_BIT (1U)
#define PRACTICE5_SPEED_ACCURACY (5625U)
#define PRACTICE5_CALCULATE_EXPAND (100000U)
#define PRACTICE5_TIME_20 (20U)
#define PRACTICE5_ZERO (0U)
#define PRACTICE5_MILLEAGE_HOUR (1U)
#define PRACTICE5_MILLEAGE_MAX (999999U)
#define PRACTICE5_CONTRL_50 (50U)
typedef unsigned char U1;
typedef unsigned short U2;
typedef unsigned long U4;
typedef unsigned long long U8;
typedef signed char S1;
typedef signed short S2;
typedef signed long S4;
typedef unsigned short U2;
typedef unsigned long U4;
typedef unsigned long long U8;
typedef signed char S1;
typedef signed short S2;
typedef signed long S4;
typedef struct {
U2 id;
U1 dlc;
U1 start_bit;
U1 can_length;
U1 data[8];
}ST_PRACTICE5_CANCONFIG;
static ST_PRACTICE5_CANCONFIG st_practice_canconfig;
U2 id;
U1 dlc;
U1 start_bit;
U1 can_length;
U1 data[8];
}ST_PRACTICE5_CANCONFIG;
static ST_PRACTICE5_CANCONFIG st_practice_canconfig;
static void vd_practice5_can_generate(U4 U4_a_data);
static void vd_practice5_can_config(void);
static U4 u4_practice5_can_getdata(void);
static void vd_practice5_can_config(void);
static U4 u4_practice5_can_getdata(void);
int main()
{
U1 u1_t_counter;
U2 u2_t_speed;
U4 u4_t_speed_expand;
U4 u4_t_hour_counter;
U4 u4_t_first_speed;
U4 u4_t_update_speeddata;
U4 u4_t_get_speeddata;
U8 u8_t_mileage_expand;
U4 u4_t_mileage_int;
U4 u4_t_mileage_decimal;
S2 s2_t_speed_change;
{
U1 u1_t_counter;
U2 u2_t_speed;
U4 u4_t_speed_expand;
U4 u4_t_hour_counter;
U4 u4_t_first_speed;
U4 u4_t_update_speeddata;
U4 u4_t_get_speeddata;
U8 u8_t_mileage_expand;
U4 u4_t_mileage_int;
U4 u4_t_mileage_decimal;
S2 s2_t_speed_change;
u1_t_counter = PRACTICE5_ZERO;
u8_t_mileage_expand = PRACTICE5_ZERO;
vd_practice5_can_config();
srand(time(NULL));
u4_t_first_speed = rand() % PRACTICE5_DATA_MAX;
u4_t_update_speeddata = u4_t_first_speed;
vd_practice5_can_generate(u4_t_update_speeddata);
#if PRACTICE5_SHOW_CAN
printf("$%d $%d ")
for (u1_t_counter = 0;u1_t_counter < PRACTICE5_CAN_DIC;u1_t_counter++)
{
printf("$%x ", st_practice_canconfig.data[u1_t_counter]);
}
#else
while (1)
{
if (u1_t_counter % PRACTICE5_TIME_100MS== PRACTICE5_ZERO)
{
s2_t_speed_change = rand() % (PRACTICE5_DATA_RANGE);
s2_t_speed_change = ((s2_t_speed_change & PRACTICE5_LAST_BIT) == PRACTICE5_ZERO) ? s2_t_speed_change : (-s2_t_speed_change);
if (u4_t_update_speeddata + s2_t_speed_change > PRACTICE5_DATA_MAX)
{
u4_t_update_speeddata = PRACTICE5_DATA_MAX;
}
else if (u4_t_update_speeddata + s2_t_speed_change < PRACTICE5_ZERO)
{
u4_t_update_speeddata = PRACTICE5_ZERO;
}
else
{
u4_t_update_speeddata += s2_t_speed_change;
}
vd_practice5_can_generate(u4_t_update_speeddata);
}
Sleep(20);
u1_t_counter++;
u4_t_get_speeddata=u4_practice5_can_getdata();
if (u4_t_get_speeddata > PRACTICE5_DATA_MAX)
{
u4_t_get_speeddata = PRACTICE5_DATA_MAX;
}
u4_t_speed_expand = u4_t_get_speeddata * PRACTICE5_SPEED_ACCURACY;
u8_t_mileage_expand += (U8)u4_t_speed_expand * PRACTICE5_MILLEAGE_HOUR;
if (u8_t_mileage_expand / PRACTICE5_CALCULATE_EXPAND > PRACTICE5_MILLEAGE_MAX)
{
u8_t_mileage_expand = PRACTICE5_MILLEAGE_MAX;
}
if (u1_t_counter >= PRACTICE5_CONTRL_50)
{
u1_t_counter = PRACTICE5_ZERO;
printf(" \nSPEED:%d.%d , MILLEAGE :%ld.%ld ", u4_t_speed_expand / PRACTICE5_CALCULATE_EXPAND, u4_t_speed_expand % PRACTICE5_CALCULATE_EXPAND,
u8_t_mileage_expand/ PRACTICE5_CALCULATE_EXPAND, u8_t_mileage_expand % PRACTICE5_CALCULATE_EXPAND);
}
}
#endif
system("pause");
return PRACTICE5_ZERO;
}
u8_t_mileage_expand = PRACTICE5_ZERO;
vd_practice5_can_config();
srand(time(NULL));
u4_t_first_speed = rand() % PRACTICE5_DATA_MAX;
u4_t_update_speeddata = u4_t_first_speed;
vd_practice5_can_generate(u4_t_update_speeddata);
#if PRACTICE5_SHOW_CAN
printf("$%d $%d ")
for (u1_t_counter = 0;u1_t_counter < PRACTICE5_CAN_DIC;u1_t_counter++)
{
printf("$%x ", st_practice_canconfig.data[u1_t_counter]);
}
#else
while (1)
{
if (u1_t_counter % PRACTICE5_TIME_100MS== PRACTICE5_ZERO)
{
s2_t_speed_change = rand() % (PRACTICE5_DATA_RANGE);
s2_t_speed_change = ((s2_t_speed_change & PRACTICE5_LAST_BIT) == PRACTICE5_ZERO) ? s2_t_speed_change : (-s2_t_speed_change);
if (u4_t_update_speeddata + s2_t_speed_change > PRACTICE5_DATA_MAX)
{
u4_t_update_speeddata = PRACTICE5_DATA_MAX;
}
else if (u4_t_update_speeddata + s2_t_speed_change < PRACTICE5_ZERO)
{
u4_t_update_speeddata = PRACTICE5_ZERO;
}
else
{
u4_t_update_speeddata += s2_t_speed_change;
}
vd_practice5_can_generate(u4_t_update_speeddata);
}
Sleep(20);
u1_t_counter++;
u4_t_get_speeddata=u4_practice5_can_getdata();
if (u4_t_get_speeddata > PRACTICE5_DATA_MAX)
{
u4_t_get_speeddata = PRACTICE5_DATA_MAX;
}
u4_t_speed_expand = u4_t_get_speeddata * PRACTICE5_SPEED_ACCURACY;
u8_t_mileage_expand += (U8)u4_t_speed_expand * PRACTICE5_MILLEAGE_HOUR;
if (u8_t_mileage_expand / PRACTICE5_CALCULATE_EXPAND > PRACTICE5_MILLEAGE_MAX)
{
u8_t_mileage_expand = PRACTICE5_MILLEAGE_MAX;
}
if (u1_t_counter >= PRACTICE5_CONTRL_50)
{
u1_t_counter = PRACTICE5_ZERO;
printf(" \nSPEED:%d.%d , MILLEAGE :%ld.%ld ", u4_t_speed_expand / PRACTICE5_CALCULATE_EXPAND, u4_t_speed_expand % PRACTICE5_CALCULATE_EXPAND,
u8_t_mileage_expand/ PRACTICE5_CALCULATE_EXPAND, u8_t_mileage_expand % PRACTICE5_CALCULATE_EXPAND);
}
}
#endif
system("pause");
return PRACTICE5_ZERO;
}
static void vd_practice5_can_config(void)
{
U1 u1_t_counter;
st_practice_canconfig.id = PRACTICE5_CAN_ID;
st_practice_canconfig.can_length = PRACTICE5_CAN_DATALENGTH;
st_practice_canconfig.start_bit = PRACTICE5_CAN_STARTBIT;
st_practice_canconfig.dlc = PRACTICE5_CAN_DIC;
for (u1_t_counter = PRACTICE5_ZERO; u1_t_counter < PRACTICE5_CAN_DIC ;u1_t_counter++)
{
st_practice_canconfig.data[u1_t_counter] = PRACTICE5_ZERO;
}
}
{
U1 u1_t_counter;
st_practice_canconfig.id = PRACTICE5_CAN_ID;
st_practice_canconfig.can_length = PRACTICE5_CAN_DATALENGTH;
st_practice_canconfig.start_bit = PRACTICE5_CAN_STARTBIT;
st_practice_canconfig.dlc = PRACTICE5_CAN_DIC;
for (u1_t_counter = PRACTICE5_ZERO; u1_t_counter < PRACTICE5_CAN_DIC ;u1_t_counter++)
{
st_practice_canconfig.data[u1_t_counter] = PRACTICE5_ZERO;
}
}
static void vd_practice5_can_generate(U4 U4_a_data )
{
U1 can_start_byte;
U1 can_start_bit;
U1 can_stop_byte;
U1 can_stop_bit;
U1 can_offset_bit;
U1 u1_t_counter;
{
U1 can_start_byte;
U1 can_start_bit;
U1 can_stop_byte;
U1 can_stop_bit;
U1 can_offset_bit;
U1 u1_t_counter;
can_start_byte = st_practice_canconfig.start_bit / 8;
can_start_bit = st_practice_canconfig.start_bit % 8;
can_stop_byte = can_start_byte - (can_start_bit + st_practice_canconfig.can_length) / 8;
can_stop_bit = (can_start_bit + st_practice_canconfig.can_length - 1) % 8;
can_offset_bit = (st_practice_canconfig.can_length - can_stop_bit - 1);
if (U4_a_data > PRACTICE5_CAN_MAX)
U4_a_data = PRACTICE5_CAN_MAX;
for ( u1_t_counter = PRACTICE5_ZERO;u1_t_counter < 8;u1_t_counter++)
{
st_practice_canconfig.data[u1_t_counter] = PRACTICE5_ZERO;
if ((u1_t_counter >= can_stop_byte) && (u1_t_counter <= can_start_byte))
{
if (u1_t_counter == can_start_byte)
{
st_practice_canconfig.data[u1_t_counter] |= U4_a_data << can_start_bit;
}
else if (u1_t_counter == can_stop_byte)
{
st_practice_canconfig.data[u1_t_counter] |= U4_a_data >> can_offset_bit;
}
else
{
can_offset_bit = can_offset_bit - 8;
st_practice_canconfig.data[u1_t_counter] |= U4_a_data >> can_offset_bit;
}
}
}
}
can_start_bit = st_practice_canconfig.start_bit % 8;
can_stop_byte = can_start_byte - (can_start_bit + st_practice_canconfig.can_length) / 8;
can_stop_bit = (can_start_bit + st_practice_canconfig.can_length - 1) % 8;
can_offset_bit = (st_practice_canconfig.can_length - can_stop_bit - 1);
if (U4_a_data > PRACTICE5_CAN_MAX)
U4_a_data = PRACTICE5_CAN_MAX;
for ( u1_t_counter = PRACTICE5_ZERO;u1_t_counter < 8;u1_t_counter++)
{
st_practice_canconfig.data[u1_t_counter] = PRACTICE5_ZERO;
if ((u1_t_counter >= can_stop_byte) && (u1_t_counter <= can_start_byte))
{
if (u1_t_counter == can_start_byte)
{
st_practice_canconfig.data[u1_t_counter] |= U4_a_data << can_start_bit;
}
else if (u1_t_counter == can_stop_byte)
{
st_practice_canconfig.data[u1_t_counter] |= U4_a_data >> can_offset_bit;
}
else
{
can_offset_bit = can_offset_bit - 8;
st_practice_canconfig.data[u1_t_counter] |= U4_a_data >> can_offset_bit;
}
}
}
}
static U4 u4_practice5_can_getdata(void)
{
U4 u4_t_candata;
U1 can_start_byte;
U1 can_start_bit;
U1 can_stop_byte;
U1 can_stop_bit;
U1 can_offset_bit;
U1 u1_t_counter;
{
U4 u4_t_candata;
U1 can_start_byte;
U1 can_start_bit;
U1 can_stop_byte;
U1 can_stop_bit;
U1 can_offset_bit;
U1 u1_t_counter;
u4_t_candata = PRACTICE5_ZERO;
can_start_byte = st_practice_canconfig.start_bit / 8;
can_start_bit = st_practice_canconfig.start_bit % 8;
can_stop_byte = can_start_byte - (can_start_bit + st_practice_canconfig.can_length) / 8;
can_stop_bit = (can_start_bit + st_practice_canconfig.can_length - 1) % 8;
can_offset_bit = (st_practice_canconfig.can_length - can_stop_bit - 1);
for ( u1_t_counter = PRACTICE5_ZERO;u1_t_counter < 8;u1_t_counter++)
{
if ((u1_t_counter >= can_stop_byte) && (u1_t_counter <= can_start_byte))
{
if (u1_t_counter == can_start_byte)
{
u4_t_candata |= (st_practice_canconfig.data[u1_t_counter] >> can_start_bit);
}
else if (u1_t_counter == can_stop_byte)
{
u4_t_candata |= st_practice_canconfig.data[u1_t_counter] << can_offset_bit;
}
else
{
can_offset_bit = can_offset_bit - 8;
u4_t_candata |= st_practice_canconfig.data[u1_t_counter] << can_offset_bit;
}
}
}
u4_t_candata = u4_t_candata & PRACTICE5_CAN_MAX;
return(u4_t_candata);
}
can_start_byte = st_practice_canconfig.start_bit / 8;
can_start_bit = st_practice_canconfig.start_bit % 8;
can_stop_byte = can_start_byte - (can_start_bit + st_practice_canconfig.can_length) / 8;
can_stop_bit = (can_start_bit + st_practice_canconfig.can_length - 1) % 8;
can_offset_bit = (st_practice_canconfig.can_length - can_stop_bit - 1);
for ( u1_t_counter = PRACTICE5_ZERO;u1_t_counter < 8;u1_t_counter++)
{
if ((u1_t_counter >= can_stop_byte) && (u1_t_counter <= can_start_byte))
{
if (u1_t_counter == can_start_byte)
{
u4_t_candata |= (st_practice_canconfig.data[u1_t_counter] >> can_start_bit);
}
else if (u1_t_counter == can_stop_byte)
{
u4_t_candata |= st_practice_canconfig.data[u1_t_counter] << can_offset_bit;
}
else
{
can_offset_bit = can_offset_bit - 8;
u4_t_candata |= st_practice_canconfig.data[u1_t_counter] << can_offset_bit;
}
}
}
u4_t_candata = u4_t_candata & PRACTICE5_CAN_MAX;
return(u4_t_candata);
}

浙公网安备 33010602011771号