// LCD module connections
sbit LCD_RS at RC4_bit;
sbit LCD_EN at RC5_bit;
sbit LCD_D4 at RC0_bit;
sbit LCD_D5 at RC1_bit;
sbit LCD_D6 at RC2_bit;
sbit LCD_D7 at RC3_bit;
sbit LCD_RS_Direction at TRISC4_bit;
sbit LCD_EN_Direction at TRISC5_bit;
sbit LCD_D4_Direction at TRISC0_bit;
sbit LCD_D5_Direction at TRISC1_bit;
sbit LCD_D6_Direction at TRISC2_bit;
sbit LCD_D7_Direction at TRISC3_bit;
// End LCD module connections
void main()
{
/*-----------prog variable----------------------------*/
unsigned char temperature; // temperature variable
unsigned char voltage; // voltage variable
unsigned char Data[8]; // data variable
unsigned short init_flag, send_flag, dt, len, read_flag; // canbus
char SJW, BRP, Phase_Seg1, Phase_Seg2, Prop_Seg, txt[4]; // canbus
unsigned long id, mask; // id variable canbus
/*-----------port definition------------------------*/
TRISC = 0x00; // PORTC are outputs (LCD)
TRISB = 0x08; // RB2 is output, RB3 is input
/*----------------lcd------------------------------*/
Lcd_Init(); // init LCD
Lcd_Cmd(_LCD_CLEAR); // Clear LCD
Lcd_Cmd(_LCD_CURSOR_OFF); // no flashing cursor
Lcd_Out(1,1,"CANBUS TEST"); // Display TEST on LCD
Delay_ms(3000); // Wait for 3 seconds
Lcd_Cmd(_LCD_CLEAR); // Clear LCD
/*----------------CAN BUS Parameters----------------------*/
SJW = 1;
BRP = 1;
Phase_Seg1 = 6;
Phase_Seg2 = 7;
Prop_Seg = 6;
init_flag = _CAN_CONFIG_SAMPLE_THRICE &
_CAN_CONFIG_PHSEG2_PRG_ON &
_CAN_CONFIG_STD_MSG &
_CAN_CONFIG_DBL_BUFFER_ON &
_CAN_CONFIG_VALID_XTD_MSG &
_CAN_CONFIG_LINE_FILTER_OFF;
send_flag = _CAN_TX_PRIORITY_0 &
_CAN_TX_XTD_FRAME &
_CAN_TX_NO_RTR_FRAME;
read_flag = 0;
CANInitialize(SJW, BRP, Phase_Seg1, Phase_Seg2, Prop_Seg, init_flag);
CANSetOperationMode(_CAN_MODE_CONFIG, 0xFF);
mask = -1;
CANSetMask(_CAN_MASK_B1, mask, _CAN_CONFIG_XTD_MSG);
CANSetMask(_CAN_MASK_B2, mask, _CAN_CONFIG_XTD_MSG);
CANSetFilter(_CAN_FILTER_B2_F3,3,_CAN_CONFIG_XTD_MSG);
CANSetOperationMode(_CAN_MODE_NORMAL, 0xFF);
/*-----------------END CAN BUS Parameters----------------------------*/
/*----------------------loop-----------------------------------*/
for(;;) // loop Endless loop
{
//Lcd_Cmd(_LCD_CLEAR); // Clear LCD
Lcd_Cmd(_LCD_CURSOR_OFF); //
/*--------request Node:TEMP-COLLECTOR & display data----------------*/
Data[0] = 'T'; // temperature Data to be sent
id = 500; // Identifier address temperature collector
CANWrite(id, Data, 1, send_flag); // send 'T' ---canbus send data----
//Delay_ms(500);
// fin send data TEMP + debut get temperature data
dt = 0;
while(!dt)
dt = CANRead(&id, Data, &len, &read_flag); // ---read canbus---
if((id == 3) && (Data[1] == 'A')) // address #3 is node display address
// data 'A' received temp data from temp collector from address 500
{
temperature = Data[0];
Lcd_Out(1,1,"Temperature=");
ByteToStr(temperature,txt); // Convert to string
Lcd_Out(1,13,txt); // Output to LCD
ByteToStr(temperature,txt); // Convert to string
}
/*---------------fin get & display data TEMP------------------------*/
Delay_ms(500);
/*--------request Node:VOLT-COLLECTOR & display data----------------*/
Data[0] = 'T'; // voltage Data to be sent
id = 501; // Identifier address voltage collector
CANWrite(id, Data, 1, send_flag); // send 'T' ---canbus send data----
//Delay_ms(500);
// fin send data VOLT + debut get voltage data
dt = 0;
while(!dt)
dt = CANRead(&id, Data, &len, &read_flag); // ---read canbus---
if((id == 3) && (Data[1] == 'B')) // address #3 is node display address
// data 'B' received volt data from volt collector from address 501
{
voltage = Data[0];
Lcd_Out(2,1,"Voltage =");
ByteToStr(voltage,txt); // Convert to string
Lcd_Out(2,13,txt); // Output to LCD
ByteToStr(voltage,txt); // Convert to string
}
/*----------------fin get & display data VOLT-------------------------*/
//Delay_ms(2000); // Wait 2 second
}
}