CAN Bus Drivers for Atmel C51 Products.pdf

(84 KB) Pobierz
Untitled Document
CAN Bus Drivers for Atmel C51 Products
This application note describes the CAN Driver functions Version 1.0.0 for the CAN
microcontroller T89C51CC01.
The purpose of these functions is to help customer start a CAN application by provide
him some basic functions.
The package contain following files:
• can_lib.c
• can_lib.h
• compiler.h
The KEIL compiler was used for compilation.
CAN
Microcontrollers
Application Note
Rev. 4229B–CAN–01/03
1
231804518.006.png 231804518.007.png
 
231804518.008.png
 
231804518.001.png
Interface Description
Types definition
Typedef declared in compiler.h file.
Uchar : unsigned char
Uint16 : unsigned short
Uint32 : unsigned long
Int16 : signed short
Int32 : signed long
Typedef declared in can_lib.h file.
can_id_t: union
Uint32 ext :
"ext" field allows access to 29-bit identifier (extended mode).
– Uint16 std:
"std" field allows access to 11-bit identifier (standard mode).
– Uchar tab[4]:
"tab" allow access by byte to identifier.
st_can_msg_t: struct
can_id_t id:
identifier of the CAN message, 11 bits in standard mode and 29 bits in
extended mode.
– Uchar ctrl:
– b7: RTR -> Data Frame = 0 or Remote Frame = 1 (access by mask
MSK_CTRL_RTR)
– b4: IDE -> Standard = 0 or Extended = 1(access by mask MSK_CTRL_IDE)
– b3:0 DLC -> number of data used dlc_t (access by mask MSK_CTRL_DLC)
Uchar * pt_data:
pointer on Uchar table of data (size max = 8).
• channel_t: enum
CHANNEL_0 .. CHANNEL_14, NO_CHANNEL
• dlc_t: enum
CONF_DLC_0 .. CONF_DLC_8
2
CAN Application Note
4229B–CAN–01/03
231804518.002.png
CAN Application Note
Functional Description
CanSetBRP
This function is used to initialize the prescaler of CAN controller with parameter
"prescaler".
1. Prototype:
– void CanSetBRP (Uchar prescaler)
2. Input:
– prescaler: Value of field BRP (Baud Rate Prescaler) in CANBT1 register
3. Output:
– void
CanSetSJW
This function is used to initialize the re_Synchronization Jump Width of CAN controller
with parameter "sjw".
1. Prototype:
– void CanSetSJW (Uchar sjw)
2. Input:
– sjw: Value of field SJW (re-Synchronization Jump Width) in CANBT2 register
3. Output:
– void
CanSetPRS
This function is used to initialize the programming time segment of CAN controller with
parameter "prs"
1. Prototype:
– void CanSetPRS (Uchar prs)
2. Input:
– prs: Value of field PRS (Programming time Segment) in CANBT2 register
3. Output:
– void
CanSetPHS2
– This function is used to initialize the phase segment 2 of CAN controller with
parameter "phs2".
1. Prototype:
– void CanSetPHS2 (Uchar phs2);
2. Input:
– phs2: Value of field PHS2 (Phase Segment 2) in CANBT3 register.
3. Output:
– void
CanSetPHS1
This function is used to initialize the phase segment 1 of CAN controller with parameter
"phs1".
1. Prototype:
– void CanSetPHS1 (Uchar phs1)
2. Input:
– phs1: Value of field PHS1 (Phase Segment 1) in CANBT3 register.
3. Output:
– void
3
4229B–CAN–01/03
231804518.003.png
ConfChannel_Rx
This function is used to configure a channel in reception mode.
Before calling this function the corresponding channel must be selected.
Make sure the channel is free because no verification is performed by the function.
To increase that the performance global variables are used.
1. Prototype:
– void ConfChannel_Rx (void)
2. Input:
– void
3. Output:
– void
4. Global Variables:
– can_id_t can_rx_filt: Value of the filter for identifier
– can_it_t can_rx_msk: Value of the mask for identifier
– Uchar conf_rx:
– b7: mask ide field (CONF_MSK_IDE) or not (CONF_NOMSK_IDE)
– b5: if mask on rtr field accept only RTR frame (CONF_RTR) or data frame
(CONF_NORTR)
– b4: if mask on ide field accept only standard identifier (CONF_NOIDE) or
extended (CONF_IDE)
– b2: mask rtr field (CONF_MSK_RTR) or not (CONF_NOMSK_RTR)
– b0: configure channel in reception (CONF_NOBUFFER) or in buffer
reception (CONF_BUFFER)
5. C Example:
– To configure channel 3 in reception only for standard frame data accepting
only identifier start by 0x35X.
– can_rx_filt.std = 0x35F;
– can_rx_msk.std = 0x350;
– conf_rx =
CONF_NOIDE|CONF_MSK_IDE|CONF_NOBUFFER|CONF_NORTR;
– CAN_SET_CHANNEL(CHANNEL_3);
– ConfChannel_Rx();
SendCanMsg
This function is used to send a message on the CAN bus.
Before calling this function the corresponding channel must be selected.
Make sure that the channel is free because no verification is performed by the function.
To increase the performance global variables are used.
1. Prototype:
– void SendCanMsg (void)
2. Input:
– void
3. Output:
– void
4. Global Variables:
– can_id_t can_tx_id: Value of the filter for identifier
– Uchar * pt_candata_tx: Table with data to transmit
4
CAN Application Note
4229B–CAN–01/03
231804518.004.png
CAN Application Note
– Uchar conf_tx:
– b5: remote frame (CONF_RTR) or data frame (CONF_NORTR)
– b4: standard identifier (CONF_NOIDE) or extended identifier (CONF_IDE)
– b3-0: number of data bytes to transmit (used dlc_t, CONF_DLC_0...
CONF_DLC_8)
5. C Example:
– To send standard frame with id = 0x318, and 8 data bytes.
– Uchar data[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77};
– st_can_msg_t can_msg_tx = {0x318, CONF_NOIDE | CONF_NORTR |
CONF_DLC_8, data[]};
– conf_tx = can_msg_tx.ctrl;
– CAN_SET_CHANNEL(CHANNEL_3);
– SendCanMsg ();
ReadCanMsg
This function is used to read a message received on "num_channel" and store it in the
structure pt_st_can_msg.
Before calling this function the corresponding channel must be selected.
To increase the performance global variables are used.
1. Prototype:
– void ReadCanMsg (Uchar next_conf)
2. Input:
– Uchar next_conf: This field select the configuration of the corresponding
channel after reading message.
– channel must be disable = CHANNEL_DISABLE
– channel enable in reception with same configuration =
CHANNEL_RX_ENABLE
– channel must be enable with same configuration but in buffer mode =
CHANNEL_RXB_ENABLE.
3. Output:
– void
4. Global Variables:
– can_msg_t * pt_st_can_rx: application structure for received message.
5. C Example:
– For read message received on channel 5, and reconfigure the channel in
reception.
– Uchar data[8];
– st_can_msg_t can_msg_rx = {0x00, 0x00, data[0]};
– pt_st_can_rx = &can_msg_rx;
– CAN_SET_CHANNEL(CHANNEL_5);
– ReadCanMsg (CHANNEL_RX_ENABLE);
Fct_can_it
This function is used to manage interrupt.
This function must be called in application interrupt routine.
5
4229B–CAN–01/03
231804518.005.png
Zgłoś jeśli naruszono regulamin