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

Slides:



Advertisements
Similar presentations
INPUT-OUTPUT ORGANIZATION
Advertisements

Dr. Rabie A. Ramadan Al-Azhar University Lecture 3
Programmable Interval Timer
Programmable Keyboard/ Display Interface: 8279
Setha Pan-ngum. History of CAN [1] It was created in mid-1980s for automotive applications by Robert Bosch. Design goal was to make automobiles more reliable,
Microprocessor and Microcontroller
University College Cork IRELAND Hardware Concepts An understanding of computer hardware is a vital prerequisite for the study of operating systems.
Dave Mills CANbus: A brief introduction Incorporating: The Fujitsu status Dave Mills Queen Mary, University of London.
7-1 Digital Serial Input/Output Two basic approaches  Synchronous shared common clock signal all devices synchronised with the shared clock signal data.
Lecture 9 Timer Operations and Programming. 2  Introduction  Summary of timers  Timer programming sequence  Summary of timer SFRs  Timer 0: 8-bit.
INPUT-OUTPUT ORGANIZATION
SERIAL BUS COMMUNICATION PROTOCOLS
CS-334: Computer Architecture
The University of New Hampshire InterOperability Laboratory Serial ATA (SATA) Protocol Chapter 10 – Transport Layer.
Department of Electrical Engineering, National Taiwan Ocean University NuMicro Learning Board Introduction 1/29/2015 Richard Kuo Assistant.
© 2008, Renesas Technology America, Inc. All Rights Reserved The RCAN-ET peripheral and the CAN API SH2 & SH2A MCUs V 1.2 Mar 2010.
Lecture 14 Controller Area Network (CAN) NCHUEE 720A Lab Prof. Jichiang Tsai.
2/26/2015 Richard Kuo Assistant Professor
Universal Asynchronous RX/TX
Department of Electrical Engineering, National Taiwan Ocean University NuMicro MCU Learning Board SDK Installation 3/7/2013 Richard.
A Few Words From Dilbert
MICROPROCESSOR INPUT/OUTPUT
Lecture Set 9 MCS-51 Serial Port.
Department of Electrical Engineering, National Taiwan Ocean University Pulse Width Modulation 4/25/2013 Richard Kuo Assistant Professor.
Department of Electrical Engineering, National Taiwan Ocean University Motor Control 5/2/2013 Richard Kuo Assistant Professor.
Universal Synchronous/Asynchronous Receiver/Transmitter (USART)
Department of Electrical Engineering, National Taiwan Ocean University C Programming 3/14/2013 Richard Kuo Assistant Professor.
Unit 4 Design and Synthesis of Datapath Controllers
Department of Electrical Engineering, National Taiwan Ocean University Analog-to-Digital Converter (ADC) 4/18/2013 Richard Kuo Assistant.
CHAPTER 3 TOP LEVEL VIEW OF COMPUTER FUNCTION AND INTERCONNECTION
1-1 Embedded Network Interface (ENI) API Concepts Shared RAM vs. FIFO modes ENI API’s.
Department of Electrical Engineering, National Taiwan Ocean University LCD Display 3/28/2013 Richard Kuo Assistant Professor.
Universal Asynchronous Receiver/Transmitter (UART)
UART: Universal Asynchronous RX/TX
Top Level View of Computer Function and Interconnection.
DEVICES AND COMMUNICATION BUSES FOR DEVICES NETWORK
Cortex-M0 MCU Interface and Driver Design - Course Overview
Department of Electrical Engineering, National Taiwan Ocean University Basic Electronics 3/7/2013 Richard Kuo Assistant Professor.
Department of Electrical Engineering, National Taiwan Ocean University ARM Cortex-M0 Introduction 3/1/2012 Richard Kuo Assistant Professor.
Department of Electrical Engineering, National Taiwan Ocean University Universal Serial Bus & SD card 5/30/2013 Richard Kuo Assistant.
NS Training Hardware. Serial Controller - UART.
8279 KEYBOARD AND DISPLAY INTERFACING
Department of Electrical Engineering, National Taiwan Ocean University Timer/WatchDog /RTC 4/11/2013 Richard Kuo Assistant Professor.
© 2008, Renesas Technology America, Inc., All Rights Reserved 1 Course Introduction Purpose This module provides an overview of sophisticated peripheral.
EEE440 Computer Architecture
ATtiny23131 A SEMINAR ON AVR MICROCONTROLLER ATtiny2313.
FPGA firmware of DC5 FEE. Outline List of issue Data loss issue Command error issue (DCM to FEM) Command lost issue (PC with USB connection to GANDALF)
Serial Peripheral Interface
Interrupts  An interrupt is any service request that causes the CPU to stop its current execution stream and to execute an instruction stream that services.
Serial Communications Interface Module Slide #1 of 19 MC68HC908GP20 Training PURPOSE -To explain how to configure and use the Serial Communications Interface.
© 2009, Renesas Technology America, Inc., All Rights Reserved 1 Course Introduction  Purpose:  This course provides an overview of the serial communication.
8279 KEYBOARD AND DISPLAY INTERFACING
General Purpose Input/Output
Interrupt driven I/O Computer Organization and Assembly Language: Module 12.
Lecture 4 General-Purpose Input/Output NCHUEE 720A Lab Prof. Jichiang Tsai.
© 2008, Renesas Technology America, Inc., All Rights Reserved 1 Course Introduction Purpose  This course provides an introduction to the peripheral functions.
An Introduction to CAN CAN Basics 2 Renesas Interactive
1 Copyright © 2014 Tata Consultancy Services Limited Controller Area Network (CAN) By Renukacharya A. Thakare.
Chapter 3 System Buses.  Hardwired systems are inflexible  General purpose hardware can do different tasks, given correct control signals  Instead.
Department of Electrical Engineering, National Taiwan Ocean University I2C Bus 5/16/2013 Richard Kuo Assistant Professor.
KEYBOARD/DISPLAY CONTROLLER - INTEL Features of 8279 The important features of 8279 are, Simultaneous keyboard and display operations. Scanned keyboard.
Controller Area Network
1. PIC ADC  PIC18F877 has 8 analog input channels i.e. port A pins(RA0 to RA5) and port E pins(RE1 and RE2). These pins are used as Analog input pins.
Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective.
Voice Controlled Robot by Cell Phone with Android App
Serial mode of data transfer
Timer and Interrupts.
Controller Area Networks (CAN)
Presentation transcript:

Department of Electrical Engineering, National Taiwan Ocean University Controller Area Network bus 5/30/2013 Richard Kuo Assistant Professor

Department of Electrical Engineering, National Taiwan Ocean University 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

Department of Electrical Engineering, National Taiwan Ocean University 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

Department of Electrical Engineering, National Taiwan Ocean University CAN Network Structure

Department of Electrical Engineering, National Taiwan Ocean University ISO/OSI Reference Model

Department of Electrical Engineering, National Taiwan Ocean University ISO/OSI Reference Model

Department of Electrical Engineering, National Taiwan Ocean University Application layers ► CANopen ► CANaerospace ► ISObus/ISO ► DeviceNet ► NMEA ► MilCAN ► SafetyBUS ► CAN Kingdom

Department of Electrical Engineering, National Taiwan Ocean University 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

Department of Electrical Engineering, National Taiwan Ocean University CAN bus access & arbitration CSMA/CD and AMP

Department of Electrical Engineering, National Taiwan Ocean University Non-Destructive Bitwise Arbitration

Department of Electrical Engineering, National Taiwan Ocean University 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

Department of Electrical Engineering, National Taiwan Ocean University CAN Bus Synchronization

Department of Electrical Engineering, National Taiwan Ocean University CAN Bit Construction

Department of Electrical Engineering, National Taiwan Ocean University Relation between Baud Rate and Bus Length

Department of Electrical Engineering, National Taiwan Ocean University Frame 格式

Department of Electrical Engineering, National Taiwan Ocean University 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

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

Department of Electrical Engineering, National Taiwan Ocean University CAN-H  GND waveform

Department of Electrical Engineering, National Taiwan Ocean University CAN-L  GND waveform

Department of Electrical Engineering, National Taiwan Ocean University 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

Department of Electrical Engineering, National Taiwan Ocean University CAN controller block diagram

Department of Electrical Engineering, National Taiwan Ocean University 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

Department of Electrical Engineering, National Taiwan Ocean University 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

Department of Electrical Engineering, National Taiwan Ocean University 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.

Department of Electrical Engineering, National Taiwan Ocean University 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

Department of Electrical Engineering, National Taiwan Ocean University 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

Department of Electrical Engineering, National Taiwan Ocean University Data Transfer between IFn Register and Message

Department of Electrical Engineering, National Taiwan Ocean University CAN Register Map for each bit function

Department of Electrical Engineering, National Taiwan Ocean University CAN Register Map for each bit function

Department of Electrical Engineering, National Taiwan Ocean University CAN connection of Learning Board ► Step 1. plug in CAN connector to learning board

Department of Electrical Engineering, National Taiwan Ocean University CAN connection of Learning Board ► Step2. connecting DB9 female-to-female cable

Department of Electrical Engineering, National Taiwan Ocean University CAN Connnector circuit

Department of Electrical Engineering, National Taiwan Ocean University CAN transceiver circuit on Nu-LB-NUC140

Department of Electrical Engineering, National Taiwan Ocean University LIN circuit on Nu-LB-NUC140

Department of Electrical Engineering, National Taiwan Ocean University 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

Department of Electrical Engineering, National Taiwan Ocean University 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

Department of Electrical Engineering, National Taiwan Ocean University 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

Department of Electrical Engineering, National Taiwan Ocean University 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

Department of Electrical Engineering, National Taiwan Ocean University 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

Department of Electrical Engineering, National Taiwan Ocean University 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 = ; 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

Department of Electrical Engineering, National Taiwan Ocean University Smpl_CAN_Keypad press key=1 Received ID=0x701 Data = Key 1

Department of Electrical Engineering, National Taiwan Ocean University 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

Department of Electrical Engineering, National Taiwan Ocean University 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 = ; 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

Department of Electrical Engineering, National Taiwan Ocean University Smpl_CAN_ADC7 TX read out VR1 value & transmit to RX, RX received & display ADC value

Department of Electrical Engineering, National Taiwan Ocean University 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); }

Department of Electrical Engineering, National Taiwan Ocean University 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); }

Department of Electrical Engineering, National Taiwan Ocean University Smpl_CAN_ADC7 void InitADC(void) { /* Step 1. GPIO initial */ GPIOA->OFFD|=0x ; //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; }

Department of Electrical Engineering, National Taiwan Ocean University 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 = ; 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);

Department of Electrical Engineering, National Taiwan Ocean University 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(); }

Department of Electrical Engineering, National Taiwan Ocean University Smpl_CAN_Timer Both boards TX is triggered by Timer0, RX received & display Timer0counter value

Department of Electrical Engineering, National Taiwan Ocean University 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~ ] // (1/ )*(255+1)*(2765)= usec or Hz // (1/ )*(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

Department of Electrical Engineering, National Taiwan Ocean University 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 }

Department of Electrical Engineering, National Taiwan Ocean University 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); }

Department of Electrical Engineering, National Taiwan Ocean University 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 = ; 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);

Department of Electrical Engineering, National Taiwan Ocean University 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(); }

Department of Electrical Engineering, National Taiwan Ocean University 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

Department of Electrical Engineering, National Taiwan Ocean University 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