Presentation is loading. Please wait.

Presentation is loading. Please wait.

Www.ee.ntou.edu.tw Department of Electrical Engineering, National Taiwan Ocean University Controller Area Network bus 5/30/2013 Richard Kuo Assistant Professor.

Similar presentations


Presentation on theme: "Www.ee.ntou.edu.tw Department of Electrical Engineering, National Taiwan Ocean University Controller Area Network bus 5/30/2013 Richard Kuo Assistant Professor."— Presentation transcript:

1 www.ee.ntou.edu.tw Department of Electrical Engineering, National Taiwan Ocean University Controller Area Network bus 5/30/2013 Richard Kuo Assistant Professor

2 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Outline ► CAN Bus Introduction –14.NuMicro_CAN.ppt ► Exercise: CAN Basic Transmission (Smpl_CAN) –TX for standard ID & RX for LCD display message ► Exercise : CAN networking with ScanKey (Smpl_CAN_Keypad) ► Exercise : CAN networking with ADC (Smpl_CAN_ADC7) ► Exercise : CAN networking with Timer0 (Smpl_CAN_Timer0) ► 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

3 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Controller Area Network (CAN) ► 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 1Mbps

4 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw CAN Network Structure

5 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw ISO/OSI Reference Model

6 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw ISO/OSI Reference Model

7 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Application layers ► CANopen http://www.can-cia.de/http://www.can-cia.de/ ► CANaerospace http://www.canaerospace.net/http://www.canaerospace.net/ ► ISObus/ISO 11783 http://www.isobus.net/http://www.isobus.net/ ► DeviceNet http://www.odva.org/http://www.odva.org/ ► NMEA 2000 http://www.nmea.org/http://www.nmea.org/ ► MilCAN http://www.milcan.org/http://www.milcan.org/ ► SafetyBUS http://www.safetybus.de/http://www.safetybus.de/ ► CAN Kingdom http://www.kvaser.se/http://www.kvaser.se/

8 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw CAN basic specification ► Transfer 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

9 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw CAN bus access & arbitration CSMA/CD and AMP

10 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Non-Destructive Bitwise Arbitration

11 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw CAN Bit Coding & Bit Staffing ► 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

12 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw CAN Bus Synchronization

13 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw CAN Bit Construction

14 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Relation between Baud Rate and Bus Length

15 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Frame 格式

16 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Data Rate vs. Bus Length Data RateLength 1 Mbit/s40 m 500 kbit/s100 m 250 kbit/s200 m 125 kbit/s500 m 50 kbit/s1 km 10 kbit/s6 km

17 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Error detection and error confinement ► Frame level –Cyclic Redundancy Checks (CRC) –Frame check –ACK error check ► Bit level –Bit monitor –Bit stuffing

18 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw CAN-H  GND waveform

19 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw CAN-L  GND waveform

20 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw NuMicro built-in Bosch CAN2.0b controller ► Support CAN protocol version 2.0 part A and B ► Bit rate up to 1M 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

21 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw CAN controller block diagram

22 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw CAN controller block function ► 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

23 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Functional Description ► Software Initialization –Setting the Init bit 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 MsgVal bit should be cleared. –Reset the Init bit (by software only) finishes the software initialization

24 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Functional Description ► 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.

25 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Managing Message Objects ► The configuration of the Message Objects in the Message RAM will not be affected by chip reset (except CAN control registers : MsgVal, NewDat, IntPnd & TxRqst) ► All Message Objects must be initialized by the application software or they must be “not valid” (MsgVal=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

26 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Message Handler State Machine ► 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 TxRqst flags –Handling of interrupts

27 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Data Transfer between IFn Register and Message

28 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw CAN Register Map for each bit function

29 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw CAN Register Map for each bit function

30 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw CAN connection of Learning Board ► Step 1. plug in CAN connector to learning board

31 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw CAN connection of Learning Board ► Step2. connecting DB9 female-to-female cable

32 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw CAN Connnector circuit

33 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw CAN transceiver circuit on Nu-LB-NUC140

34 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw LIN circuit on Nu-LB-NUC140

35 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Smpl_CAN Both boards running the same program with CAN connected RX received and display message (an incrementing number) TX keep sending standard ID 0x777 and data =“NUC n” n is incrementing up to 32

36 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Smpl_CAN // // Smpl_CAN : transmit ID 700~61F, receive & show MsgID // TX: generate message with ID=0x777 // RX: set Mask Filter to receive 0x7XX and show Msg Data // For 1Mbps, R21=120ohm only one on the CAN bus // other learning board should remove R21 // #include #include "NUC1xx.h" #include "Driver\DrvGPIO.h" #include "Driver\DrvUART.h" #include "Driver\DrvCAN.h" #include "Driver\DrvSYS.h" #include "Driver\DrvTIMER.h" #include "NUC1xx-LB_002\LCD_Driver.h" char TEXT0[16] = "CAN : "; char TEXT1[16] = "ID : "; char TEXT2[16] = "Data: "; char TEXT3[16] = " "; Transmitting Type = Standard ID Id = 0x777 DLC = 5 (message byte count) Receiving ShowMessage display Id & Data

37 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Smpl_CAN if(u32IIDR==31+1) { printf("Msg-31 INT and Callback \n"); DrvCAN_ReadMsgObj(31,TRUE,&rrMsg); CAN_ShowMsg(&rrMsg); } void CAN_ShowMsg(STR_CANMSG_T* Msg) { uint8_t i; sprintf(TEXT1+6,"%x", Msg->Id); print_lcd(1,TEXT1); for(i=0;i DLC;i++) sprintf(TEXT2+6+i,"%c", Msg->Data[i]); print_lcd(2,TEXT2); } // Interrupt Service Routine / Callback function void CAN_ShowMsg(STR_CANMSG_T* Msg); STR_CANMSG_T rrMsg; void CAN_CallbackFn(uint32_t u32IIDR) { if(u32IIDR==1) { printf("Msg-0 INT and Callback\n"); DrvCAN_ReadMsgObj(0,TRUE,&rrMsg); CAN_ShowMsg(&rrMsg); } if(u32IIDR==5+1) { printf("Msg-5 INT and Callback \n"); DrvCAN_ReadMsgObj(5,TRUE,&rrMsg); CAN_ShowMsg(&rrMsg); } For RX to display message Print message to LCD Interrupt Service

38 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Smpl_CAN SetMaskFilter() DrvCAN_SetRxMsgObj(MSG(0), CAN_STD_ID, 0x7FF, TRUE); printf("Waiting Message\n"); DrvSYS_Delay(100000); //DrvCAN_UninstallCallback(CALLBACK_MSG); //DrvCAN_DisableInt(CAN_CON_IE); } // RX set Mask Filter void SetMaskFilter() { STR_CANMASK_T MaskMsg; DrvCAN_EnableInt(CAN_CON_IE); DrvCAN_InstallCallback(CALLBACK_MSG, (CAN_CALLBACK)CAN_CallbackFn); /* Set b'0' means don't care*/ /* Set Messge Object No.0 mask ID */ MaskMsg.u8Xtd = 1; MaskMsg.u8Dir = 1; MaskMsg.u8IdType = 0; // 1:EXT or 0:STD MaskMsg.u32Id = 0x700; DrvCAN_SetMsgObjMask(MSG(0),&MaskMsg ); For RX to set mask RX set mask filter

39 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Smpl_CAN TestMaskFilter() if(DrvCAN_SetTxMsgObj(MSG(1),&tMsg) < 0) // Call DrvCAN_SetTxMsgObj() only Configure Msg { printf("Set Tx Msg Object failed\n"); return; } DrvCAN_SetTxRqst(MSG(1)); i32id++; if(i32id>=0x20) break; // incrementing to 32, then break DrvSYS_Delay(100000); } printf("Transfer Done\n"); } // TX send 11-bit ID void TestMaskFilter() { int32_t i32id=0; STR_CANMSG_T tMsg; while(1) { /* Standard 11-bits ID */ tMsg.FrameType= DATA_FRAME; tMsg.IdType = CAN_STD_ID; tMsg.Id = 0x777; tMsg.DLC = 5; tMsg.Data[0] = 'N'; tMsg.Data[1] = 'U'; tMsg.Data[2] = 'C'; tMsg.Data[3] = ' '; tMsg.Data[4] = 0x30+i32id; For TX, sending standard ID = 0x777, and transmit incrementing data TX send standard ID

40 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Smpl_CAN main() Initial_panel(); clr_all_panel(); // CAN Transceiver setting DrvGPIO_Open(E_GPB,12,E_IO_OUTPUT); DrvGPIO_ClrBit(E_GPB,12); /* Select CAN Multi-Function */ DrvGPIO_InitFunction(E_FUNC_CAN0); DrvCAN_Init(); i32Err = DrvCAN_Open(CAN_speed);// set CAN speed if(i32Err<0) printf("Set CAN bit rate is fail\n"); sprintf(TEXT0+5,"%d Kbps", CAN_speed); print_lcd(0,TEXT0); SetMaskFilter(); // set receiving message sprintf(TEXT3,"SetMaskFilter"); print_lcd(3,TEXT3); while(1) { TestMaskFilter(); // set sending message sprintf(TEXT3,"TestMaskFilter"); print_lcd(3,TEXT3); } int main (void) { int32_t CAN_speed = 1000; // Kbps int32_t i32Err =0; STR_UART_T param; UNLOCKREG(); DrvSYS_SetOscCtrl(E_SYS_OSC22M,1); DrvSYS_Delay(20000);/* Delay for Xtal stable */ while(!SYSCLK->CLKSTATUS.OSC22M_STB); DrvSYS_SelectHCLKSource(0); /* Init GPIO and configure UART0 */ DrvGPIO_InitFunction(E_FUNC_UART0); param.u32BaudRate = 115200; param.u8cDataBits = DRVUART_DATABITS_8; param.u8cStopBits = DRVUART_STOPBITS_1; param.u8cParity = DRVUART_PARITY_NONE; param.u8cRxTriggerLevel = DRVUART_FIFO_1BYTES; param.u8TimeOut = 0; DrvUART_Open(UART_PORT0, &param); RX set mask TX keep sending

41 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Smpl_CAN_Keypad press key=1 Received ID=0x701 Data = Key 1

42 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Smpl_CAN_Keypad if(DrvCAN_SetTxMsgObj(MSG(1),&tMsg) < 0) // Call DrvCAN_SetTxMsgObj() only Configure Msg { printf("Set Tx Msg Object failed\n"); return; } DrvCAN_SetTxRqst(MSG(1)); //i32id++; //if(i32id>=0x20) break; // incrementing to 32, then break //DrvSYS_Delay(100000); //} printf("Transfer Done\n"); } // TX send 11-bit ID void TestMaskFilter(uint8_t number) { //int32_t i32id=0; STR_CANMSG_T tMsg; //while(1) //{ /* Standard 11-bits ID */ tMsg.FrameType= DATA_FRAME; tMsg.IdType = CAN_STD_ID; tMsg.Id = 0x700+number; tMsg.DLC = 5; tMsg.Data[0] = ‘K'; tMsg.Data[1] = ‘E'; tMsg.Data[2] = ‘Y'; tMsg.Data[3] = ' '; tMsg.Data[4] = 0x30+number; For TX, sending standard ID = 0x70n, and transmit data “NUC n” TX send ID + number Data = Number remove while loop to keep scan keypad in main Remove incrementing number & adding Delay

43 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Smpl_CAN_Keypad Initial_panel(); clr_all_panel(); OpenKeyPad(); // Initialize Keypad // CAN Transceiver setting DrvGPIO_Open(E_GPB,12,E_IO_OUTPUT); DrvGPIO_ClrBit(E_GPB,12); /* Select CAN Multi-Function */ DrvGPIO_InitFunction(E_FUNC_CAN0); DrvCAN_Init(); i32Err = DrvCAN_Open(CAN_speed);// set CAN speed if(i32Err<0) printf("Set CAN bit rate is fail\n"); sprintf(TEXT0+5,"%d Kbps", CAN_speed); print_lcd(0,TEXT0); SetMaskFilter(); // set receiving message sprintf(TEXT3,"SetMaskFilter"); print_lcd(3,TEXT3); while(1) { keyin = Scankey(); // scan keypad to input a number TestMaskFilter(keyin);// set sending message sprintf(TEXT3,"TestMaskFilter"); print_lcd(3,TEXT3); } int main (void) { int32_t CAN_speed = 1000; // Kbps int32_t i32Err =0; uint8_t keyin; STR_UART_T param; UNLOCKREG(); DrvSYS_SetOscCtrl(E_SYS_OSC22M,1); DrvSYS_Delay(20000);/* Delay for Xtal stable */ while(!SYSCLK->CLKSTATUS.OSC22M_STB); DrvSYS_SelectHCLKSource(0); /* Init GPIO and configure UART0 */ DrvGPIO_InitFunction(E_FUNC_UART0); param.u32BaudRate = 115200; param.u8cDataBits = DRVUART_DATABITS_8; param.u8cStopBits = DRVUART_STOPBITS_1; param.u8cParity = DRVUART_PARITY_NONE; param.u8cRxTriggerLevel = DRVUART_FIFO_1BYTES; param.u8TimeOut = 0; DrvUART_Open(UART_PORT0, &param); RX set mask TX scankey & send Init keypad

44 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Smpl_CAN_ADC7 TX read out VR1 value & transmit to RX, RX received & display ADC value

45 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Smpl_CAN_ADC7 void CAN_ShowMsg(STR_CANMSG_T* Msg); STR_CANMSG_T rrMsg; // Interrupt Service Routine/Callback Function void CAN_CallbackFn(uint32_t u32IIDR) { if(u32IIDR==1) { printf("Msg-0 INT and Callback\n"); DrvCAN_ReadMsgObj(0,TRUE,&rrMsg); CAN_ShowMsg(&rrMsg); } if(u32IIDR==5+1) { printf("Msg-5 INT and Callback \n"); DrvCAN_ReadMsgObj(5,TRUE,&rrMsg); CAN_ShowMsg(&rrMsg); } if(u32IIDR==31+1) { printf("Msg-31 INT and Callback \n"); DrvCAN_ReadMsgObj(31,TRUE,&rrMsg); CAN_ShowMsg(&rrMsg); } // Display Message on LCD void CAN_ShowMsg(STR_CANMSG_T* Msg) { uint8_t i; sprintf(TEXT1+6,"%x", Msg->Id); print_lcd(1,TEXT1); for(i=0;i DLC;i++) sprintf(TEXT2+6+i,"%c", Msg->Data[i]); print_lcd(2,TEXT2); }

46 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Smpl_CAN_ADC7 // TX send 11-bit ID void TestMaskFilter(int32_t number) { STR_CANMSG_T tMsg; tMsg.FrameType= DATA_FRAME; tMsg.IdType = CAN_STD_ID; // send 11-bit ID tMsg.Id = 0x777; // ID = 0x777 tMsg.DLC = 4; // data length = 4 bytes // Convert ADC value to 4 digits ASCII code tMsg.Data[0] = 0x30 + number/1000; number = number - number/1000 *1000; tMsg.Data[1] = 0x30 + number /100; number = number - number/100 *100; tMsg.Data[2] = 0x30 + number /10; number = number - number/10 * 10; tMsg.Data[3] = 0x30 + number; if(DrvCAN_SetTxMsgObj(MSG(1),&tMsg) < 0) // Confiugre Msg RAM { printf("Set Tx Msg Object failed\n"); return; } DrvCAN_SetTxRqst(MSG(1)); DrvSYS_Delay(100000); //printf("Transfer Done\n"); } // RX set Mask Filter void SetMaskFilter() { STR_CANMASK_T MaskMsg; DrvCAN_EnableInt(CAN_CON_IE); DrvCAN_InstallCallback(CALLBACK_MSG, (CAN_CALLBACK)CAN_CallbackFn); /* Set b'0' means don't care*/ /* Set Messge Object No.0 mask ID */ MaskMsg.u8Xtd = 1; MaskMsg.u8Dir = 1; MaskMsg.u8IdType = 0; /* 1:EXT or 0:STD */ MaskMsg.u32Id = 0x700; DrvCAN_SetMsgObjMask(MSG(0),&MaskMsg); DrvCAN_SetRxMsgObj(MSG(0), CAN_STD_ID, 0x7FF, TRUE); printf("If there is a message-ID 0x700~0x70F,\n ONLY 0x707/0x70F can pass acceptance filter.\n"); printf("Waiting Message\n"); DrvSYS_Delay(100000); //DrvCAN_UninstallCallback(CALLBACK_MSG); // DrvCAN_DisableInt(CAN_CON_IE); }

47 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Smpl_CAN_ADC7 void InitADC(void) { /* Step 1. GPIO initial */ GPIOA->OFFD|=0x00800000; //Disable digital input path SYS->GPAMFP.ADC7_SS21_AD6=1; //Set ADC function /* Step 2. Enable and Select ADC clock source, and then enable ADC module */ SYSCLK->CLKSEL1.ADC_S = 2; //Select 22Mhz for ADC SYSCLK->CLKDIV.ADC_N = 1; //ADC clock source = 22Mhz/2 =11Mhz; SYSCLK->APBCLK.ADC_EN = 1; //Enable clock source ADC->ADCR.ADEN = 1; //Enable ADC module /* Step 3. Select Operation mode */ ADC->ADCR.DIFFEN = 0; //single end input ADC->ADCR.ADMD = 0; //single mode /* Step 4. Select ADC channel */ ADC->ADCHER.CHEN = 0x80; /* Step 5. Enable ADC interrupt */ ADC->ADSR.ADF =1; //clear the A/D interrupt flags for safe ADC->ADCR.ADIE = 1; //NVIC_EnableIRQ(ADC_IRQn); /* Step 6. Enable WDT module */ ADC->ADCR.ADST=1; }

48 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Smpl_CAN_ADC7 int main (void) { int32_t CAN_speed = 1000; // Kbps int32_t i32Err =0; int32_t adc_value; STR_UART_T param; UNLOCKREG(); DrvSYS_SetOscCtrl(E_SYS_XTL12M,1); DrvSYS_SetOscCtrl(E_SYS_OSC22M,1); DrvSYS_Delay(20000); while(!SYSCLK->CLKSTATUS.XTL12M_STB); while(!SYSCLK->CLKSTATUS.OSC22M_STB); DrvSYS_SelectHCLKSource(0); DrvGPIO_InitFunction(E_FUNC_UART0); param.u32BaudRate = 115200; param.u8cDataBits = DRVUART_DATABITS_8; param.u8cStopBits = DRVUART_STOPBITS_1; param.u8cParity = DRVUART_PARITY_NONE; param.u8cRxTriggerLevel = DRVUART_FIFO_1BYTES; param.u8TimeOut = 0; DrvUART_Open(UART_PORT0, &param); Initial_panel(); clr_all_panel(); InitADC(); // DrvGPIO_Open(E_GPD,14,E_IO_OUTPUT); // LCD backlight // DrvGPIO_ClrBit(E_GPD,14); DrvGPIO_Open(E_GPB,12,E_IO_OUTPUT); // CAN Transceiver setting DrvGPIO_ClrBit(E_GPB,12); /* Select CAN Multi-Function */ DrvGPIO_InitFunction(E_FUNC_CAN0); DrvCAN_Init(); i32Err = DrvCAN_Open(CAN_speed); // set CAN speed if(i32Err<0) printf("Set CAN bit rate is fail\n"); sprintf(TEXT0+5,"%d Kbps", CAN_speed); print_lcd(0,TEXT0);

49 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Smpl_CAN_ADC7 SetMaskFilter(); // set receiving message sprintf(TEXT3,"SetMaskFilter"); print_lcd(3,TEXT3); 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 TestMaskFilter(adc_value);// set sending message sprintf(TEXT3,"ADC value:%d", adc_value); print_lcd(3,TEXT3); ADC->ADCR.ADST=1; // activate next ADC sample } //DrvCAN_Close(); }

50 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Smpl_CAN_Timer Both boards TX is triggered by Timer0, RX received & display Timer0counter value

51 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Smpl_CAN_Timer0 void InitTIMER0(void) { /* Step 1. Enable and Select Timer clock source */ SYSCLK->CLKSEL1.TMR0_S = 0;//Select 12Mhz for Timer0 clock source SYSCLK->APBCLK.TMR0_EN =1;//Enable Timer0 clock source /* Step 2. Select Operation mode */ TIMER0->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)*/ TIMER0->TCSR.PRESCALE=255;// Set Prescale [0~255] TIMER0->TCMPR = 46875;// Set TCMPR [0~16777215] // (1/12000000)*(255+1)*(2765)= 125.01usec or 7999.42Hz // (1/12000000)*(255+1)*46875 = 1 sec or 1Hz /* Step 4. Enable interrupt */ TIMER0->TCSR.IE = 1; TIMER0->TISR.TIF = 1;//Write 1 to clear for safty NVIC_EnableIRQ(TMR0_IRQn);//Enable Timer0 Interrupt

52 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Smpl_CAN_Timer0 /* Step 5. Enable Timer module */ TIMER0->TCSR.CRST = 1;//Reset up counter TIMER0->TCSR.CEN = 1;//Enable Timer0 // TIMER0->TCSR.TDR_EN=1;// Enable TDR function } void TMR0_IRQHandler(void) // Timer0 interrupt subroutine { Timer0Counter+=1; // increment Timer0Counter TIMER0->TISR.TIF =1; TestMaskFilter(Timer0Counter); // send counter value to CAN bus }

53 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Smpl_CAN_Timer0 // TX send 11-bit message void TestMaskFilter(uint16_t number) { STR_CANMSG_T tMsg; tMsg.FrameType= DATA_FRAME; tMsg.IdType = CAN_STD_ID; // standard 11-bit ID tMsg.Id = 0x777;// ID = 0x777 tMsg.DLC = 5;// data length = 5 bytes // converting counter value to 5-digit number ASCII code tMsg.Data[0] = 0x30 + number /10000; number = number - number/10000 *10000; tMsg.Data[1] = 0x30 + number /1000; number = number - number/1000 *1000; tMsg.Data[2] = 0x30 + number /100; number = number - number/100 *100; tMsg.Data[3] = 0x30 + number /10; number = number - number/10 *10; tMsg.Data[4] = 0x30 + number; // Configure Msg RAM if(DrvCAN_SetTxMsgObj(MSG(1),&tMsg) < 0) { printf("Set Tx Msg Object failed\n"); return; } DrvCAN_SetTxRqst(MSG(1)); DrvSYS_Delay(100000); }

54 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Smpl_CAN_Timer0 int main (void) { int32_t CAN_speed = 1000; // Kbps int32_t i32Err =0; STR_UART_T param; UNLOCKREG(); DrvSYS_SetOscCtrl(E_SYS_XTL12M,1); DrvSYS_SetOscCtrl(E_SYS_OSC22M,1); DrvSYS_Delay(20000); while(!SYSCLK->CLKSTATUS.XTL12M_STB); while(!SYSCLK->CLKSTATUS.OSC22M_STB); DrvSYS_SelectHCLKSource(0); /* Init GPIO and configure UART0 */ DrvGPIO_InitFunction(E_FUNC_UART0); param.u32BaudRate = 115200; param.u8cDataBits = DRVUART_DATABITS_8; param.u8cStopBits = DRVUART_STOPBITS_1; param.u8cParity = DRVUART_PARITY_NONE; param.u8cRxTriggerLevel = DRVUART_FIFO_1BYTES; param.u8TimeOut = 0; DrvUART_Open(UART_PORT0, &param); Initial_panel(); clr_all_panel(); InitTIMER0(); // LCD Backlight // DrvGPIO_Open(E_GPD,14,E_IO_OUTPUT); // DrvGPIO_ClrBit(E_GPD,14); // CAN Transceiver setting DrvGPIO_Open(E_GPB,12,E_IO_OUTPUT); DrvGPIO_ClrBit(E_GPB,12); /* Select CAN Multi-Function */ DrvGPIO_InitFunction(E_FUNC_CAN0); DrvCAN_Init(); i32Err = DrvCAN_Open(CAN_speed); // set CAN speed if(i32Err<0) printf("Set CAN bit rate is fail\n"); sprintf(TEXT0+5,"%d Kbps", CAN_speed); print_lcd(0,TEXT0);

55 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Smpl_CAN_Timer0 SetMaskFilter(); // set receiving message sprintf(TEXT3,"SetMaskFilter"); print_lcd(3,TEXT3); while(1) { //TestMaskFilter(); // set sending message //sprintf(TEXT3,"TestMaskFilter"); //print_lcd(3,TEXT3); } //DrvCAN_Close(); }

56 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw General Disclaimer 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.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.peer reviewfeatured article 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 (CC-BY-SA) and the GNU Free Documentation License (GFDL);Creative Commons Attribution-Sharealike 3.0 Unported License GNU Free Documentation License

57 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw General Disclaimer 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.personality rights


Download ppt "Www.ee.ntou.edu.tw Department of Electrical Engineering, National Taiwan Ocean University Controller Area Network bus 5/30/2013 Richard Kuo Assistant Professor."

Similar presentations


Ads by Google