Hardware Reference
In-Depth Information
if (CAN1RDLR 55 0)
/* data length 0, do nothing */
return;
cmd2lcd(0x80);
/* set LCD cursor to first row */
puts2lcd(t1Msg);
/* output “Temperature is” on LCD */
cmd2lcd(0xC4);
/* set LCD cursor to second row */
ptr 5 (char *)&CAN1RDSR0;
/* ptr points to the first data byte */
for (i 5 0; i < CAN1RDLR; i 11 )
putc2lcd(*ptr 11 );
/* output temperature value on the LCD second row */
}
else if (tmp 55 1) {
/* filter 1 hit */
if(CAN1RDLR 55 0)
/* data length 0, do nothing */
return;
cmd2lcd(0x80);
/* set LCD cursor to first row */
puts2lcd(v1Msg);
/* output “Voltage is” on the first row of LCD */
cmd2lcd(0xC4);
/* set LCD cursor to second row */
ptr 5 (char *)&CAN1RDSR0;
/* ptr points to the first data byte */
for(i 5 0; i < CAN1RDLR; i 11 )
putc2lcd(*ptr 11 );
/* output voltage value on the second row of LCD */
}
else asm(“nop”);
/* other hit, do nothing */
}
This service routine can be modified to handle the remote transmission request (when the
RTR bit of the received message is set to 1). The message to be transmitted in response to this
request should be stored in a buffer and kept up to date. The modification to this service routine
is straightforward, and hence is left as an exercise.
13.13.3 Putting It All Together
The program pieces needed for CAN data communications are already in place. This sec-
tion uses one example to combine them.
Example 13.8
Write a C program to be run in a CAN environment using the same timing parameters as
computed in Example 13.1. Each CAN node measures the voltage (in the range from 0 to 5 V)
and sends it out from the CAN bus and also receives the voltage message sent over the CAN
bus by other nodes. Configure the CAN1 module to receive messages having an extended iden-
tifier starting with V1. The transmission and reception are to proceed as follows:
The program measures the voltage connected at the AN7 pin every 200 ms and sends
out the value with identifier V1. The voltage is represented in the format x.y V. After
sending out a message, the program outputs the following message on the first row of
the LCD:
Sent: x.y V
Message reception is interrupt-driven. Whenever a new message is accepted, the
program outputs the following message on the second row of the LCD:
Received x.y V
 
Search WWH ::




Custom Search