Cogitum
Joined: 22 Mar 2012 Posts: 70 Location: France (Paris)
|
CAN BUS problem to read 2 id ? |
Posted: Thu Jul 19, 2012 8:19 am |
|
|
HI
This program no RUN correctly !!!
I need to read 2 x ID (0x38D & 0x38D) but unfortunately, my problem is to keep always the same ID for specific treatment. However, ID aren't changing alternatively.
From time to time the id change and the results calculation aren't WRONG ?????
Source data aren't stable !!
What's wrong in this program ???
Code: |
#include <18F2580.h> //
#DEVICE ADC=10
#fuses HS, NOPROTECT, PUT, BROWNOUT, NOWDT, NOLVP //H4 ou HS
#use delay(clock=40MHZ,CRYSTAL=10MHZ)
// #use delay(clock=20000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,stream=COM_18F6722)
#include <can-18xxx8.c>
#include <flex_lcd420.c>
#ZERO_RAM // RAZ
#define odo 0x38D // id
#define conso 0x488 // id
int32 T,T2;
int16 msec;
float distance,speed;// to see id = 0x38D
float conso_moyenne,conso_cumulee_mm3,conso_trajet;
short TopSeconde;
short TopSeconde2;
//short Top500ms;
short Top100ms;
int Cpt_Seconde;
// Variables UART link
short rs232_18F6722;
char buffer_rs232_18F6722[20];
int Compteur_rs232_18F6722;
struct rx_stat rxstat;
int32 rx_odo,rx_conso,id_nbr;// id
int in_odo[8]={0,0,0,0,0,0,0,0}; // odometer + speed
int in_conso[8]={0,0,0,0,0,0,0,0}; // Conso
int rx_len=8; // taille buffer de reception
//unsigned int32 rx_id;// id
//int8 in_data [8]={0,0,0,0,0,0,0,0}; // RPM
int32 C1,V0,V1;
//============================================================================//
//==================== iD Filter ============================//
//============================================================================//
/******* 11 Bits Filter *******/
void id(void)
{
can_set_mode(CAN_OP_CONFIG);
can_set_id(RX0MASK, 0x7FF, 0); //set mask 0 Force à 11 Bits
can_set_id(RX0FILTER0, id_nbr,0); //set filter 0 of mask 0
can_set_id(RX0FILTER1, id_nbr,0); //set filter 1 of mask 0
can_set_id(RX1MASK, 0x7FF, 0); //set mask 1
can_set_id(RX1FILTER2, id_nbr,0); //set filter 0 of mask 1
can_set_id(RX1FILTER3, id_nbr,0); //set filter 1 of mask 1
can_set_id(RX1FILTER4, id_nbr,0); //set filter 2 of mask 1
can_set_id(RX1FILTER5, id_nbr,0); //set filter 3 of mask 1
can_set_mode(CAN_OP_NORMAL);
}
//********* TIMER1 ***********/
#int_timer1
void TIMER1_isr(void)
{
if (msec++ >=38)//8 = 104 ms 38 =0.497 second
{
// T2++;
// output_toggle(pin_B4);
Top100ms=true;
msec=0;
}
//++++++++++++ CLOCK = 40MHZ +++++++++++//
if (cpt_seconde++ >= 38)// 156 = 2 second ,78 = 1.022 second
{
// T2++;
TopSeconde=true;
TopSeconde2=true;
Cpt_seconde=0;
}
}
//============================================================================//
//============================== MAIN Program ===============================//
//============================================================================//
void main()
{
setup_timer_1(T1_INTERNAL|T1_DIV_BY_2); // "by_1"=> 1,022 sec by 8==> 104 ms
enable_interrupts(INT_TIMER1); // by 2 pour 40MHZ
// enable_interrupts(INT_RDA);
enable_interrupts(GLOBAL);
can_init(); // initialisation du CAN
// code pour vitesse bus à 500K (2µs) Xtal 40MHZ (200ns)
can_set_mode(CAN_OP_CONFIG);
BRGCON1.brp=1; //
BRGCON1.sjw=1;
BRGCON2.prseg=2; //
BRGCON2.seg1ph=7; //
BRGCON2.sam=FALSE;
BRGCON2.seg2phts=FALSE;
BRGCON3.seg2ph=7; //2
BRGCON3.wakfil=TRUE;
lcd_init();
T=0;
T2=0;
// speed=0;
conso_cumulee_mm3=0;
conso_moyenne=0;
C1=0;
while(true)
{
//====================================================================//
//=========================== CAN DATA ==============================//
//====================================================================//
if(Top100ms)//(TopSeconde2)
{
T2++;
// output_toggle(Pin_B4);
//================== Frame Odometer 0x38D ================
id_nbr=odo; // id=909
id();
if ( can_kbhit() ) //...data in buffer ?
{
rx_odo=odo;
if (can_getd(rx_odo,in_odo,rx_len,rxstat)) //...get data from buffer !
delay_ms(300);
{
if (rx_odo == odo)
{
//============= SpeedVehicule Km/h ==========
V0=in_odo[0];
V1=in_odo[1];
speed = (V0*256+V1)*0.01; // speed en KM/h
distance=distance+speed*0.000277;// distance m/s
in_odo[0]=0;
in_odo[1]=0;
}
}
}
Top100ms=False;//Topseconde2=False;
}
delay_ms(250);
if (TopSeconde)
{
T++;
// output_toggle(Pin_B4);
//================ Frame Fuel 0x488 ===================
id_nbr=conso; // id = 1160
id();
if ( can_kbhit() ) //...data in buffer ?
{
rx_conso=conso;
if (can_getd(rx_conso,in_conso,rx_len,rxstat)) //...get data in buffer !
{
if (rx_conso == conso)
{
C1=in_conso[1]; // distance en m/s ; 1 mm3 = 0.000001 L
conso_cumulee_mm3 = conso_cumulee_mm3+(C1*80);//en mm3 si 20400 passe à 0
Conso_moyenne = ((Conso_cumulee_mm3/1000000)/(distance*1000))*100000;//L/100km
Conso_trajet = conso_cumulee_mm3*0.000001;// display en litre
in_conso[1]=0;
}
}
}
TopSeconde=False;
}
//======================= Display on LCD ==============================//
lcd_init();
lcd_gotoxy(1,1);
printf(lcd_putc,"Trajet=%3.1f",distance); // Kms cumulated
lcd_gotoxy(1,2);
printf(lcd_putc,"Km/h=%3.0f",speed); // Speed Vehicule
// printf(lcd_putc,"Km/h=%3.0f D=%3.1f",speed,D2);
lcd_gotoxy(-3,3);
printf(lcd_putc,"L/100=%3.1f",conso_moyenne); // L/100
lcd_gotoxy(-3,4);
printf(lcd_putc,"L/Trajet=%2.1f",conso_trajet); // L
}
}
|
Thanks in advance |
|