Controller Area Network bus Department of Electrical Engineering
Controller Area Network bus Department of Electrical Engineering, National Taiwan Ocean University 5/30/2013 Richard Kuo Assistant Professor www. ee. ntou. edu. tw
Outline Department of Electrical Engineering, National Taiwan Ocean University ► CAN Bus Introduction – 14. Nu. Micro_CAN. ppt ► Exercise: CAN Basic Transmission (Smpl_CAN) www. ee. ntou. edu. tw – TX for standard ID & RX for LCD display message Exercise : CAN networking with Scan. Key (Smpl_CAN_Keypad) ► Exercise : CAN networking with ADC (Smpl_CAN_ADC 7) ► Exercise : CAN networking with Timer 0 (Smpl_CAN_Timer 0) ► Exercise : CAN master/slave to control GPIOs ► ► To use CAN to network and control an external control board (with MOSFETs/Relys) for driving DC Motor /Step Motor /Actuator
Controller Area Network (CAN) Department of Electrical Engineering, National Taiwan Ocean University www. ee. ntou. edu. tw ► The CAN Bus is an automotive bus developed by Robert Bosch, which has quickly gained acceptance into the automotive and aerospace industries. ► CAN is a serial bus protocol to connect individual systems and sensors as an alternative to conventional multi-wire looms. It allows automotive components to communicate on a single or dual-wire networked data bus up to 1 Mbps
CAN Network Structure Department of Electrical Engineering, National Taiwan Ocean University www. ee. ntou. edu. tw
ISO/OSI Reference Model Department of Electrical Engineering, National Taiwan Ocean University www. ee. ntou. edu. tw
ISO/OSI Reference Model Department of Electrical Engineering, National Taiwan Ocean University www. ee. ntou. edu. tw
Application layers Department of Electrical Engineering, National Taiwan Ocean University ► ► ► ► CANopen http: //www. can-cia. de/ CANaerospace http: //www. canaerospace. net/ ISObus/ISO 11783 http: //www. isobus. net/ Device. Net http: //www. odva. org/ NMEA 2000 http: //www. nmea. org/ Mil. CAN http: //www. milcan. org/ Safety. BUS http: //www. safetybus. de/ CAN Kingdom http: //www. kvaser. se/ www. ee. ntou. edu. tw
CAN basic specification Department of Electrical Engineering, National Taiwan Ocean University ► Transfer www. ee. ntou. edu. tw rate: 1 Mbps max. ► Max. message length: 8 bytes ► Carrier Sense Multiple Access/Bitwise Arbitration (CSMA/BA) ► Fault tolerance: 15 bits CRC + ACK ► Media: twisted pair
CAN bus access & arbitration Department of Electrical Engineering, National Taiwan Ocean University CSMA/CD and AMP www. ee. ntou. edu. tw
Non-Destructive Bitwise Arbitration Department of Electrical Engineering, National Taiwan Ocean University www. ee. ntou. edu. tw
CAN Bit Coding & Bit Staffing Department of Electrical Engineering, National Taiwan Ocean University www. ee. ntou. edu. tw Bit Coding : NRZ(Non-Return-To-Zero code) does not ensure enough edges for synchronization ► Stuff Bits are inserted after 5 consecutive bits of the same level ► Stuff bits have the inverse level of the previous bit ► No deterministic encoding, frame length depends on transmitted data ►
CAN Bus Synchronization Department of Electrical Engineering, National Taiwan Ocean University www. ee. ntou. edu. tw
CAN Bit Construction Department of Electrical Engineering, National Taiwan Ocean University www. ee. ntou. edu. tw
Relation between Baud Rate and Bus Length Department of Electrical Engineering, National Taiwan Ocean University www. ee. ntou. edu. tw
Frame 格式 Department of Electrical Engineering, National Taiwan Ocean University www. ee. ntou. edu. tw
Data Rate vs. Bus Length Department of Electrical Engineering, National Taiwan Ocean University www. ee. ntou. edu. tw Data Rate Length 1 Mbit/s 40 m 500 kbit/s 100 m 250 kbit/s 200 m 125 kbit/s 500 m 50 kbit/s 1 km 10 kbit/s 6 km
Error detection and error confinement Department of Electrical Engineering, National Taiwan Ocean University ► Frame level – Cyclic Redundancy Checks (CRC) – Frame check – ACK error check ► Bit level – Bit monitor – Bit stuffing www. ee. ntou. edu. tw
CAN-H GND waveform Department of Electrical Engineering, National Taiwan Ocean University www. ee. ntou. edu. tw
CAN-L GND waveform Department of Electrical Engineering, National Taiwan Ocean University www. ee. ntou. edu. tw
Nu. Micro built-in Bosch CAN 2. 0 b controller Department of Electrical Engineering, National Taiwan Ocean University ► ► ► ► ► www. ee. ntou. edu. tw Support CAN protocol version 2. 0 part A and B Bit rate up to 1 M bit/s 32 Message Objects Error Message Object has its own identifier mask Programmable FIFO mode (concatenation of Message object) Maskable interrupt Disable Automatic Re-transmission mode for Time Triggered CAN application Programmable loop-back mode for self-test operation 16 -bit module interface to AMBA APB bus Support wake-up function
CAN controller block diagram Department of Electrical Engineering, National Taiwan Ocean University www. ee. ntou. edu. tw
CAN controller block function Department of Electrical Engineering, National Taiwan Ocean University ► www. ee. ntou. edu. tw CAN_core – CAN protocol controller and TX/RX Shift Registers for parallel/serial conversion of messages ► Message RAM – Store Message Objects and Identifier Masks ► Registers – All registers used to control and to configure the CAN controller ► Message Handler – State Machine that controls the data transfer between the TX/RXShift Register of the CAN core and the Message RAM, and interrupts generation ► Module Interface – Interface of AMBA APB 16 bit bus
Functional Description Department of Electrical Engineering, National Taiwan Ocean University ► www. ee. ntou. edu. tw Software Initialization – Setting the Init by software or hardware reset, or by Bus_Off state – While Init bit is set, all message transfer to and from the CAN bus are stopped the status of CAN_TX output pin is recessive (High). The Error Management Logic (EML) counters are unchanged. Setting Init bit did not change any configuration register. – To initialize the CAN controller, software has to set up the Bit Timing Register and each Message Object. If a Message Object is not required, the corresponding Msg. Val bit should be cleared. – Reset the Init bit (by software only) finishes the software initialization
Functional Description Department of Electrical Engineering, National Taiwan Ocean University ► www. ee. ntou. edu. tw CAN Message Transfer – Once CAN controller is initialized and Init bit is reset to zero, CAN core synchronizes itself to CAN bus, and starts the message transfer – Received messages are stored in their appropriate Message Objects if they pass the Message Handler’s acceptance filtering. – The whole message including all arbitation bits, DLC, and eight data bytes – If the Identifier Mask is used, the arbitration bits which are masked to “don’t care” may be overwritten in the Message Object – Software can read or write each message any time through the Interface Registers and the Message Handler guarantees data consistency in case of concurrent accesses.
Managing Message Objects Department of Electrical Engineering, National Taiwan Ocean University ► ► ► ► www. ee. ntou. edu. tw The configuration of the Message Objects in the Message RAM will not be affected by chip reset (except CAN control registers : Msg. Val, New. Dat, Int. Pnd & Tx. Rqst) All Message Objects must be initialized by the application software or they must be “not valid” (Msg. Val=0) and the bit timing must be configured before the application software clears the Init bit. The configuration of a Message Object is done by programming Mask, Arbitration, Control and Data fields of one of the two interface registers to the desired value. By writing to the corresponding IFn Command Request Register, the IFn Message Buffer Registers are loaded into the addressd Message Object in the Message RAM While Init bit is cleared, CAN protocol controller’s state machine and Message Handler state machine control the internal data flow of CAN controller Received messages that pass the acceptance filtering are stored into the Message RAM messages with pending transmission request are loaded into the CAN_Core Shift Register and are transmitted through the CAN bus The application software reads received messages and update messages to be transmitted through the IFn Interface Registers. Depending on the configuration, application software is interrupted on certain CAN message and CAN error events
Message Handler State Machine Department of Electrical Engineering, National Taiwan Ocean University www. ee. ntou. edu. tw The Message Handler controls the data transfer CAN_Core TX/RX Register, Message RAM, and IFn Registers ► The Message Handler FSM controls the following functions ► – – – – Data Transfer from IFn Registers to the Message RAM Data Transfer from Message RAM to the IFn Registers Data Transfer from Shift Register to the Message RAM Data Transfer from Message RAM to Shift Register Data Transfer from Shift Register to the Acceptance Filtering unit Scanning of Message RAM for a matching Message Object Handling of Tx. Rqst flags Handling of interrupts
Data Transfer between IFn Register and Message Department of Electrical Engineering, National Taiwan Ocean University www. ee. ntou. edu. tw
CAN Register Map for each bit function Department of Electrical Engineering, National Taiwan Ocean University www. ee. ntou. edu. tw
CAN Register Map for each bit function Department of Electrical Engineering, National Taiwan Ocean University www. ee. ntou. edu. tw
CAN connection of Learning Board Department of Electrical Engineering, National Taiwan Ocean University ► Step 1. plug in CAN connector to learning board www. ee. ntou. edu. tw
CAN connection of Learning Board Department of Electrical Engineering, National Taiwan Ocean University ► Step 2. connecting DB 9 female-to-female cable www. ee. ntou. edu. tw
CAN Connnector circuit Department of Electrical Engineering, National Taiwan Ocean University www. ee. ntou. edu. tw
CAN transceiver circuit on Nu-LB-NUC 140 Department of Electrical Engineering, National Taiwan Ocean University www. ee. ntou. edu. tw
Smpl_CAN Department of Electrical Engineering, National Taiwan Ocean University www. ee. ntou. edu. tw Both boards running the same program with CAN connected RX received and display message (an incrementing number) TX keep sending standard ID 0 x 777 and data =“NUC n” n is incrementing up to 32
Smpl_CAN Department of Electrical Engineering, National Taiwan Ocean University // // Smpl_CAN : transmit ID 700~61 F, receive & show Msg. ID // TX: generate message with ID=0 x 777 // RX: set Mask Filter to receive 0 x 7 XX and show Msg Data // For 1 Mbps, R 21=120 ohm only one on the CAN bus // other learning board should remove R 21 // #include <stdio. h> #include "NUC 1 xx. h" #include "DriverDrv. GPIO. h" #include "DriverDrv. UART. h" #include "DriverDrv. CAN. h" #include "DriverDrv. SYS. h" #include "DriverDrv. TIMER. h" #include "NUC 1 xx-LB_002LCD_Driver. h" char TEXT 0[16] = "CAN : char TEXT 1[16] = "ID : char TEXT 2[16] = "Data: char TEXT 3[16] = " "; "; www. ee. ntou. edu. tw Transmitting Type = Standard ID Id = 0 x 777 DLC = 5 (message byte count) Receiving Show. Message display Id & Data
Smpl_CAN Department of Electrical Engineering, National Taiwan Ocean University www. ee. ntou. edu. tw // Interrupt Service Routine / Callback function void CAN_Show. Msg(STR_CANMSG_T* Msg); STR_CANMSG_T rr. Msg; if(u 32 IIDR==31+1) { printf("Msg-31 INT and Callback n"); Drv. CAN_Read. Msg. Obj(31, TRUE, &rr. Msg); CAN_Show. Msg(&rr. Msg); Interrupt Service void CAN_Callback. Fn(uint 32_t u 32 IIDR) { if(u 32 IIDR==1) { printf("Msg-0 INT and Callbackn"); Drv. CAN_Read. Msg. Obj(0, TRUE, &rr. Msg); CAN_Show. Msg(&rr. Msg); } if(u 32 IIDR==5+1) { printf("Msg-5 INT and Callback n"); Drv. CAN_Read. Msg. Obj(5, TRUE, &rr. Msg); CAN_Show. Msg(&rr. Msg); } } } Print message to LCD void CAN_Show. Msg(STR_CANMSG_T* Msg) { uint 8_t i; sprintf(TEXT 1+6, "%x", Msg->Id); print_lcd(1, TEXT 1); for(i=0; i<Msg->DLC; i++) sprintf(TEXT 2+6+i, "%c", Msg->Data[i]); print_lcd(2, TEXT 2); } For RX to display message
Smpl_CAN Set. Mask. Filter() Department of Electrical Engineering, National Taiwan Ocean University // RX set Mask Filter void Set. Mask. Filter() { STR_CANMASK_T Mask. Msg; RX set mask filter www. ee. ntou. edu. tw Drv. CAN_Set. Rx. Msg. Obj(MSG(0), CAN_STD_ID, 0 x 7 FF , TRUE); printf("Waiting Messagen"); Drv. SYS_Delay(100000); //Drv. CAN_Uninstall. Callback(CALLBACK_MSG); //Drv. CAN_Disable. Int(CAN_CON_IE); Drv. CAN_Enable. Int(CAN_CON_IE); Drv. CAN_Install. Callback(CALLBACK_MSG, (CAN_CALLBACK)CAN_Callback. Fn); } /* Set b'0' means don't care*/ /* Set Messge Object No. 0 mask ID */ Mask. Msg. u 8 Xtd = 1; Mask. Msg. u 8 Dir = 1; Mask. Msg. u 8 Id. Type = 0; // 1: EXT or 0: STD Mask. Msg. u 32 Id = 0 x 700; Drv. CAN_Set. Msg. Obj. Mask(MSG(0), &Mask. Msg ); For RX to set mask
Smpl_CAN Test. Mask. Filter() Department of Electrical Engineering, National Taiwan Ocean University // TX send 11 -bit ID void Test. Mask. Filter() { int 32_t i 32 id=0; STR_CANMSG_T t. Msg; www. ee. ntou. edu. tw if(Drv. CAN_Set. Tx. Msg. Obj(MSG(1), &t. Msg) < 0) // Call Drv. CAN_Set. Tx. Msg. Obj() only Configure Msg { printf("Set Tx Msg Object failedn"); return; } Drv. CAN_Set. Tx. Rqst(MSG(1)); i 32 id++; if(i 32 id>=0 x 20) break; // incrementing to 32, then break Drv. SYS_Delay(100000); TX send standard ID while(1) { /* Standard 11 -bits ID */ t. Msg. Frame. Type= DATA_FRAME; t. Msg. Id. Type = CAN_STD_ID; t. Msg. Id = 0 x 777; t. Msg. DLC = 5; t. Msg. Data[0] = 'N'; t. Msg. Data[1] = 'U'; t. Msg. Data[2] = 'C'; t. Msg. Data[3] = ' '; t. Msg. Data[4] = 0 x 30+i 32 id; } printf("Transfer Donen"); } For TX, sending standard ID = 0 x 777, and transmit incrementing data
Smpl_CAN main() Department of Electrical Engineering, National Taiwan Ocean University www. ee. ntou. edu. tw int main (void) { int 32_t CAN_speed = 1000; // Kbps int 32_t i 32 Err =0; STR_UART_T param; UNLOCKREG(); Drv. SYS_Set. Osc. Ctrl(E_SYS_OSC 22 M, 1); Drv. SYS_Delay(20000); /* Delay for Xtal stable */ Initial_panel(); clr_all_panel(); // CAN Transceiver setting Drv. GPIO_Open(E_GPB, 12, E_IO_OUTPUT); Drv. GPIO_Clr. Bit(E_GPB, 12); /* Select CAN Multi-Function */ Drv. GPIO_Init. Function(E_FUNC_CAN 0); Drv. CAN_Init(); i 32 Err = Drv. CAN_Open(CAN_speed); // set CAN speed if(i 32 Err<0) printf("Set CAN bit rate is failn"); sprintf(TEXT 0+5, "%d Kbps", CAN_speed); print_lcd(0, TEXT 0); while(!SYSCLK->CLKSTATUS. OSC 22 M_STB); Drv. SYS_Select. HCLKSource(0); RX set mask /* Init GPIO and configure UART 0 */ Drv. GPIO_Init. Function(E_FUNC_UART 0); param. u 32 Baud. Rate = 115200; param. u 8 c. Data. Bits = DRVUART_DATABITS_8; param. u 8 c. Stop. Bits = DRVUART_STOPBITS_1; param. u 8 c. Parity = DRVUART_PARITY_NONE; param. u 8 c. Rx. Trigger. Level = DRVUART_FIFO_1 BYTES; param. u 8 Time. Out = 0; Drv. UART_Open(UART_PORT 0, ¶m); Set. Mask. Filter(); // set receiving message sprintf(TEXT 3, "Set. Mask. Filter"); print_lcd(3, TEXT 3); TX keep sending while(1) { Test. Mask. Filter(); // set sending message sprintf(TEXT 3, "Test. Mask. Filter"); print_lcd(3, TEXT 3); } }
Smpl_CAN_Keypad Department of Electrical Engineering, National Taiwan Ocean University Received ID=0 x 701 Data = Key 1 www. ee. ntou. edu. tw press key=1
Smpl_CAN_Keypad Department of Electrical Engineering, National Taiwan Ocean University www. ee. ntou. edu. tw // TX send 11 -bit ID void Test. Mask. Filter(uint 8_t number) { //int 32_t i 32 id=0; STR_CANMSG_T t. Msg; //while(1) //{ if(Drv. CAN_Set. Tx. Msg. Obj(MSG(1), &t. Msg) < 0) // Call Drv. CAN_Set. Tx. Msg. Obj() only Configure Msg { printf("Set Tx Msg Object failedn"); return; } Drv. CAN_Set. Tx. Rqst(MSG(1)); //i 32 id++; //if(i 32 id>=0 x 20) break; // incrementing to 32, then break //Drv. SYS_Delay(100000); Remove incrementing //} number & adding Delay printf("Transfer Donen"); remove while loop to keep scan keypad in main /* Standard 11 -bits ID */ t. Msg. Frame. Type= DATA_FRAME; t. Msg. Id. Type = CAN_STD_ID; t. Msg. Id = 0 x 700+number; t. Msg. DLC = 5; t. Msg. Data[0] = ‘K'; TX send t. Msg. Data[1] = ‘E'; ID + number t. Msg. Data[2] = ‘Y'; Data = Number t. Msg. Data[3] = ' '; t. Msg. Data[4] = 0 x 30+number; } For TX, sending standard ID = 0 x 70 n, and transmit data “NUC n”
Smpl_CAN_Keypad Department of Electrical Engineering, National Taiwan Ocean University int main (void) { int 32_t CAN_speed = 1000; // Kbps int 32_t i 32 Err =0; uint 8_t keyin; STR_UART_T param; UNLOCKREG(); Drv. SYS_Set. Osc. Ctrl(E_SYS_OSC 22 M, 1); Drv. SYS_Delay(20000); /* Delay for Xtal stable */ while(!SYSCLK->CLKSTATUS. OSC 22 M_STB); Drv. SYS_Select. HCLKSource(0); /* Init GPIO and configure UART 0 */ Drv. GPIO_Init. Function(E_FUNC_UART 0); param. u 32 Baud. Rate = 115200; param. u 8 c. Data. Bits = DRVUART_DATABITS_8; param. u 8 c. Stop. Bits = DRVUART_STOPBITS_1; param. u 8 c. Parity = DRVUART_PARITY_NONE; param. u 8 c. Rx. Trigger. Level = DRVUART_FIFO_1 BYTES; param. u 8 Time. Out = 0; Drv. UART_Open(UART_PORT 0, ¶m); www. ee. ntou. edu. tw Initial_panel(); Init keypad clr_all_panel(); Open. Key. Pad(); // Initialize Keypad // CAN Transceiver setting Drv. GPIO_Open(E_GPB, 12, E_IO_OUTPUT); Drv. GPIO_Clr. Bit(E_GPB, 12); /* Select CAN Multi-Function */ Drv. GPIO_Init. Function(E_FUNC_CAN 0); Drv. CAN_Init(); i 32 Err = Drv. CAN_Open(CAN_speed); // set CAN speed if(i 32 Err<0) printf("Set CAN bit rate is failn"); sprintf(TEXT 0+5, "%d Kbps", CAN_speed); print_lcd(0, TEXT 0); RX set mask Set. Mask. Filter(); // set receiving message sprintf(TEXT 3, "Set. Mask. Filter"); print_lcd(3, TEXT 3); TX scankey & send while(1) { keyin = Scankey(); // scan keypad to input a number Test. Mask. Filter(keyin); // set sending message sprintf(TEXT 3, "Test. Mask. Filter"); print_lcd(3, TEXT 3); }
Smpl_CAN_ADC 7 Department of Electrical Engineering, National Taiwan Ocean University www. ee. ntou. edu. tw TX read out VR 1 value & transmit to RX, RX received & display ADC value
Smpl_CAN_ADC 7 Department of Electrical Engineering, National Taiwan Ocean University www. ee. ntou. edu. tw if(u 32 IIDR==31+1) void CAN_Show. Msg(STR_CANMSG_T* Msg); { STR_CANMSG_T rr. Msg; // Interrupt Service Routine/Callback Function void CAN_Callback. Fn(uint 32_t u 32 IIDR) { if(u 32 IIDR==1) { printf("Msg-0 INT and Callbackn"); Drv. CAN_Read. Msg. Obj(0, TRUE, &rr. Msg); CAN_Show. Msg(&rr. Msg); } if(u 32 IIDR==5+1) { printf("Msg-5 INT and Callback n"); Drv. CAN_Read. Msg. Obj(5, TRUE, &rr. Msg); CAN_Show. Msg(&rr. Msg); } printf("Msg-31 INT and Callback n"); Drv. CAN_Read. Msg. Obj(31, TRUE, &rr. Msg); CAN_Show. Msg(&rr. Msg); } } // Display Message on LCD void CAN_Show. Msg(STR_CANMSG_T* Msg) { uint 8_t i; sprintf(TEXT 1+6, "%x", Msg->Id); print_lcd(1, TEXT 1); for(i=0; i<Msg->DLC; i++) sprintf(TEXT 2+6+i, "%c", Msg->Data[i]); print_lcd(2, TEXT 2); }
Smpl_CAN_ADC 7 Department of Electrical Engineering, National Taiwan Ocean University // TX send 11 -bit ID void Test. Mask. Filter(int 32_t number) { STR_CANMSG_T t. Msg; t. Msg. Frame. Type= DATA_FRAME; t. Msg. Id. Type = CAN_STD_ID; // send 11 -bit ID t. Msg. Id = 0 x 777; // ID = 0 x 777 t. Msg. DLC = 4; // data length = 4 bytes // Convert ADC value to 4 digits ASCII code t. Msg. Data[0] = 0 x 30 + number/1000; number = number - number/1000 *1000; t. Msg. Data[1] = 0 x 30 + number /100; number = number - number/100 *100; t. Msg. Data[2] = 0 x 30 + number /10; number = number - number/10 * 10; t. Msg. Data[3] = 0 x 30 + number; if(Drv. CAN_Set. Tx. Msg. Obj(MSG(1), &t. Msg) < 0) // Confiugre Msg RAM { printf("Set Tx Msg Object failedn"); return; } Drv. CAN_Set. Tx. Rqst(MSG(1)); Drv. SYS_Delay(100000); //printf("Transfer Donen"); } www. ee. ntou. edu. tw // RX set Mask Filter void Set. Mask. Filter() { STR_CANMASK_T Mask. Msg; Drv. CAN_Enable. Int(CAN_CON_IE); Drv. CAN_Install. Callback(CALLBACK_MSG, (CAN_CALLBACK)CAN_Callback. Fn); /* Set b'0' means don't care*/ /* Set Messge Object No. 0 mask ID */ Mask. Msg. u 8 Xtd = 1; Mask. Msg. u 8 Dir = 1; Mask. Msg. u 8 Id. Type = 0; /* 1: EXT or 0: STD */ Mask. Msg. u 32 Id = 0 x 700; Drv. CAN_Set. Msg. Obj. Mask(MSG(0), &Mask. Msg); Drv. CAN_Set. Rx. Msg. Obj(MSG(0), CAN_STD_ID, 0 x 7 FF , TRUE); printf("If there is a message-ID 0 x 700~0 x 70 F, n ONLY 0 x 707/0 x 70 F can pass acceptance filter. n"); printf("Waiting Messagen"); Drv. SYS_Delay(100000); //Drv. CAN_Uninstall. Callback(CALLBACK_MSG); // Drv. CAN_Disable. Int(CAN_CON_IE); }
Smpl_CAN_ADC 7 Department of Electrical Engineering, National Taiwan Ocean University www. ee. ntou. edu. tw void Init. ADC(void) { /* Step 1. GPIO initial */ GPIOA->OFFD|=0 x 00800000; //Disable digital input path SYS->GPAMFP. ADC 7_SS 21_AD 6=1; //Set ADC function /* Step 3. Select Operation mode */ ADC->ADCR. DIFFEN = 0; //single end input ADC->ADCR. ADMD = 0; //single mode /* Step 2. Enable and Select ADC clock source, and then enable ADC module */ SYSCLK->CLKSEL 1. ADC_S = 2; //Select 22 Mhz for ADC SYSCLK->CLKDIV. ADC_N = 1; //ADC clock source = 22 Mhz/2 =11 Mhz; SYSCLK->APBCLK. ADC_EN = 1; //Enable clock source ADC->ADCR. ADEN = 1; //Enable ADC module /* Step 5. Enable ADC interrupt */ ADC->ADSR. ADF =1; //clear the A/D interrupt flags for safe ADC->ADCR. ADIE = 1; NVIC_Enable. IRQ(ADC_IRQn); /* Step 4. Select ADC channel */ ADC->ADCHER. CHEN = 0 x 80; // /* Step 6. Enable WDT module */ ADC->ADCR. ADST=1; }
Smpl_CAN_ADC 7 Department of Electrical Engineering, National Taiwan Ocean University int main (void) { int 32_t CAN_speed = 1000; // Kbps int 32_t i 32 Err =0; int 32_t adc_value; STR_UART_T param; UNLOCKREG(); Drv. SYS_Set. Osc. Ctrl(E_SYS_XTL 12 M, 1); Drv. SYS_Set. Osc. Ctrl(E_SYS_OSC 22 M, 1); Drv. SYS_Delay(20000); while(!SYSCLK->CLKSTATUS. XTL 12 M_STB); while(!SYSCLK->CLKSTATUS. OSC 22 M_STB); Drv. SYS_Select. HCLKSource(0); Drv. GPIO_Init. Function(E_FUNC_UART 0); param. u 32 Baud. Rate = 115200; param. u 8 c. Data. Bits = DRVUART_DATABITS_8; param. u 8 c. Stop. Bits = DRVUART_STOPBITS_1; param. u 8 c. Parity = DRVUART_PARITY_NONE; param. u 8 c. Rx. Trigger. Level = DRVUART_FIFO_1 BYTES; param. u 8 Time. Out = 0; www. ee. ntou. edu. tw Drv. UART_Open(UART_PORT 0, ¶m); Initial_panel(); clr_all_panel(); Init. ADC(); // Drv. GPIO_Open(E_GPD, 14, E_IO_OUTPUT); // LCD backlight // Drv. GPIO_Clr. Bit(E_GPD, 14); Drv. GPIO_Open(E_GPB, 12, E_IO_OUTPUT); // CAN Transceiver setting Drv. GPIO_Clr. Bit(E_GPB, 12); /* Select CAN Multi-Function */ Drv. GPIO_Init. Function(E_FUNC_CAN 0); Drv. CAN_Init(); i 32 Err = Drv. CAN_Open(CAN_speed); // set CAN speed if(i 32 Err<0) printf("Set CAN bit rate is failn"); sprintf(TEXT 0+5, "%d Kbps", CAN_speed); print_lcd(0, TEXT 0);
Smpl_CAN_ADC 7 Department of Electrical Engineering, National Taiwan Ocean University Set. Mask. Filter(); // set receiving message sprintf(TEXT 3, "Set. Mask. Filter"); print_lcd(3, TEXT 3); while(1) { while(ADC->ADSR. ADF==0); // ADC Flag, wait till 1 (A/DC conversion done) ADC->ADSR. ADF=1; // write 1 to ADF is to clear the flag // } adc_value=ADC->ADDR[7]. RSLT; // input 12 -bit ADC value Test. Mask. Filter(adc_value); // set sending message sprintf(TEXT 3, "ADC value: %d", adc_value); print_lcd(3, TEXT 3); ADC->ADCR. ADST=1; // activate next ADC sample } Drv. CAN_Close(); www. ee. ntou. edu. tw
Smpl_CAN_Timer Department of Electrical Engineering, National Taiwan Ocean University www. ee. ntou. edu. tw Both boards TX is triggered by Timer 0, RX received & display Timer 0 counter value
Smpl_CAN_Timer 0 Department of Electrical Engineering, National Taiwan Ocean University www. ee. ntou. edu. tw void Init. TIMER 0(void) { /* Step 1. Enable and Select Timer clock source */ SYSCLK->CLKSEL 1. TMR 0_S = 0; //Select 12 Mhz for Timer 0 clock source SYSCLK->APBCLK. TMR 0_EN =1; //Enable Timer 0 clock source /* Step 2. Select Operation mode */ TIMER 0 ->TCSR. MODE=1; //Select periodic mode for operation mode /* Step 3. Select Time out period = (Period of timer clock input) * (8 -bit Prescale + 1) * (24 -bit TCMP)*/ TIMER 0 ->TCSR. PRESCALE=255; // Set Prescale [0~255] TIMER 0 ->TCMPR = 46875; // Set TCMPR [0~16777215] // (1/12000000)*(255+1)*(2765)= 125. 01 usec or 7999. 42 Hz // (1/12000000)*(255+1)*46875 = 1 sec or 1 Hz /* Step 4. Enable interrupt */ TIMER 0 ->TCSR. IE = 1; TIMER 0 ->TISR. TIF = 1; NVIC_Enable. IRQ(TMR 0_IRQn); //Write 1 to clear for safty //Enable Timer 0 Interrupt
Smpl_CAN_Timer 0 Department of Electrical Engineering, National Taiwan Ocean University /* Step 5. Enable Timer module */ TIMER 0 ->TCSR. CRST = 1; TIMER 0 ->TCSR. CEN = 1; // } TIMER 0 ->TCSR. TDR_EN=1; www. ee. ntou. edu. tw //Reset up counter //Enable Timer 0 // Enable TDR function void TMR 0_IRQHandler(void) // Timer 0 interrupt subroutine { Timer 0 Counter+=1; // increment Timer 0 Counter TIMER 0 ->TISR. TIF =1; Test. Mask. Filter(Timer 0 Counter); // send counter value to CAN bus }
Smpl_CAN_Timer 0 Department of Electrical Engineering, National Taiwan Ocean University www. ee. ntou. edu. tw // TX send 11 -bit message void Test. Mask. Filter(uint 16_t number) { STR_CANMSG_T t. Msg; // Configure Msg RAM if(Drv. CAN_Set. Tx. Msg. Obj(MSG(1), &t. Msg) < 0) { printf("Set Tx Msg Object failedn"); return; } Drv. CAN_Set. Tx. Rqst(MSG(1)); t. Msg. Frame. Type= DATA_FRAME; t. Msg. Id. Type = CAN_STD_ID; // standard 11 -bit ID t. Msg. Id = 0 x 777; // ID = 0 x 777 t. Msg. DLC = 5; // data length = 5 bytes // converting counter value to 5 -digit number ASCII code t. Msg. Data[0] = 0 x 30 + number /10000; number = number - number/10000 *10000; t. Msg. Data[1] = 0 x 30 + number /1000; number = number - number/1000 *1000; t. Msg. Data[2] = 0 x 30 + number /100; number = number - number/100 *100; t. Msg. Data[3] = 0 x 30 + number /10; number = number - number/10 *10; t. Msg. Data[4] = 0 x 30 + number; Drv. SYS_Delay(100000); }
Smpl_CAN_Timer 0 Department of Electrical Engineering, National Taiwan Ocean University int main (void) { int 32_t CAN_speed = 1000; // Kbps int 32_t i 32 Err =0; STR_UART_T param; UNLOCKREG(); Drv. SYS_Set. Osc. Ctrl(E_SYS_XTL 12 M, 1); Drv. SYS_Set. Osc. Ctrl(E_SYS_OSC 22 M, 1); Drv. SYS_Delay(20000); while(!SYSCLK->CLKSTATUS. XTL 12 M_STB); while(!SYSCLK->CLKSTATUS. OSC 22 M_STB); Drv. SYS_Select. HCLKSource(0); /* Init GPIO and configure UART 0 */ Drv. GPIO_Init. Function(E_FUNC_UART 0); param. u 32 Baud. Rate = 115200; param. u 8 c. Data. Bits = DRVUART_DATABITS_8; param. u 8 c. Stop. Bits = DRVUART_STOPBITS_1; param. u 8 c. Parity = DRVUART_PARITY_NONE; param. u 8 c. Rx. Trigger. Level = DRVUART_FIFO_1 BYTES; param. u 8 Time. Out = 0; www. ee. ntou. edu. tw Drv. UART_Open(UART_PORT 0, ¶m); Initial_panel(); clr_all_panel(); Init. TIMER 0(); // LCD Backlight // Drv. GPIO_Open(E_GPD, 14, E_IO_OUTPUT); // Drv. GPIO_Clr. Bit(E_GPD, 14); // CAN Transceiver setting Drv. GPIO_Open(E_GPB, 12, E_IO_OUTPUT); Drv. GPIO_Clr. Bit(E_GPB, 12); /* Select CAN Multi-Function */ Drv. GPIO_Init. Function(E_FUNC_CAN 0); Drv. CAN_Init(); i 32 Err = Drv. CAN_Open(CAN_speed); // set CAN speed if(i 32 Err<0) printf("Set CAN bit rate is failn"); sprintf(TEXT 0+5, "%d Kbps", CAN_speed); print_lcd(0, TEXT 0);
Smpl_CAN_Timer 0 Department of Electrical Engineering, National Taiwan Ocean University Set. Mask. Filter(); // set receiving message sprintf(TEXT 3, "Set. Mask. Filter"); print_lcd(3, TEXT 3); // } while(1) { //Test. Mask. Filter(); // set sending message //sprintf(TEXT 3, "Test. Mask. Filter"); //print_lcd(3, TEXT 3); } Drv. CAN_Close(); www. ee. ntou. edu. tw
General Disclaimer Department of Electrical Engineering, National Taiwan Ocean University www. ee. ntou. edu. tw The Lecture is strictly used for educational purpose. MAKES NO GUARANTEE OF VALIDITY ► The lecture cannot guarantee the validity of the information found here. The lecture may recently have been changed, vandalized or altered by someone whose opinion does not correspond with the state of knowledge in the relevant fields. Note that most other encyclopedias and reference works also have similar disclaimers. No formal peer review ► The lecture is not uniformly peer reviewed; while readers may correct errors or engage in casual peer review, they have no legal duty to do so and thus all information read here is without any implied warranty of fitness for any purpose or use whatsoever. Even articles that have been vetted by informal peer review or featured article processes may later have been edited inappropriately, just before you view them. No contract; limited license ► ► Please make sure that you understand that the information provided here is being provided freely, and that no kind of agreement or contract is created between you and the owners or users of this site, the owners of the servers upon which it is housed, the individual Wikipedia contributors, any project administrators, sysops or anyone else who is in any way connected with this project or sister projects subject to your claims against them directly. You are being granted a limited license to copy anything from this site; it does not create or imply any contractual or extracontractual liability on the part of Wikipedia or any of its agents, members, organizers or other users. There is no agreement or understanding between you and the content provider regarding your use or modification of this information beyond the Creative Commons Attribution-Sharealike 3. 0 Unported License (CCBY-SA) and the GNU Free Documentation License (GFDL);
General Disclaimer Department of Electrical Engineering, National Taiwan Ocean University www. ee. ntou. edu. tw Trademarks ► Any of the trademarks, service marks, collective marks, design rights or similar rights that are mentioned, used or cited in the lectures are the property of their respective owners. Their use here does not imply that you may use them for any purpose other than for the same or a similar informational use as contemplated by the original authors under the CC-BY-SA and GFDL licensing schemes. Unless otherwise stated , we are neither endorsed by nor affiliated with any of the holders of any such rights and as such we cannot grant any rights to use any otherwise protected materials. Your use of any such or similar incorporeal property is at your own risk. Personality rights ► The lecture may portray an identifiable person who is alive or deceased recently. The use of images of living or recently deceased individuals is, in some jurisdictions, restricted by laws pertaining to personality rights, independent from their copyright status. Before using these types of content, please ensure that you have the right to use it under the laws which apply in the circumstances of your intended use. You are solely responsible for ensuring that you do not infringe someone else's personality rights.
- Slides: 56