Home
Introduction to the KEIL IDE uVision
Contents
1. 0 configure ADC interrupt control register ADCIC 0x0055 ADCIE 1 ILVL 5 GLVL 1 allow all interrupts IEN 1 start conversion ADST 1 while 1 only plot result if something s changed if myResult oldResult new value gt print result printf Converted value x n myResult oldResult myResult Comments The program starts by initialising the serial interface we re using printf to display the result of our conversion before initialising two global variables myResult and oldResult Notice that at least the variable myResult needs to be global as we are accessing it from within the main program as well as the ADC interrupt service routine The program continues by configuring the ADC ADCON ADCIC before the interrupts are allowed to happen IEN 1 Following the starting of the conversion ADST 1 we enter an infinite loop in which new results are displayed on a terminal connected to the serial interface Compile this program and load it into the simulator debugger Open the peripheral windows for ADC as well as the interrupt control window Step through the code and observe how the interrupt levels and group levels are programmed as expected Figure LMP1 20 20 Microcontroller Programming LMP1 Introduction to Controller programming E E x ele Edt view project Debug Peripherals Tools SVCS Window Help onve
2. 24 Microcontroller Programming LMP1 Introduction to Controller programming JSopy 20 MHz Timer Input Selection T2I T3U T4I Prescaler factor 8 146e 32 jes 128 256 512 1024 Input 1 25 625 312 5 156 25 78 125 39 06 19 53 Frequency ate MHz kHz KHz kHz kHz kHz kHz Table LMP1 1 Timer T3 Pre scale factors Example With a pre scale factor of 512 the effective timer frequency is 20 MHz 512 39 06 kHz This corresponds to a per step increment of At 1 39 06 10 25 6 us Counting up all 16 bit 0x0000 OxFFFF yields a maximum period of 25 6 us x OxFFFF 1 68 s To program a period of 1 second we would have to initialise the timer register with 1 1 68 x OxFFFF 0x9860 and count downwards Figure LMP1 24 illustrates this example OxFFFF 0x9860 0x0000 Os 1s 1 68 s Figure LMP1 24 Programming a 1 second timer period T3 To configure timer T3 for this sort of operation we would have to initialise the T3 control register T3CON with a pre scale factor of 512 Figure LMP1 25 shows the internal structure of the timer T3 control register As indicated by table LMP1 1 the timer input selection bits T3l need to be set to 110 T3CON Timer 3 Control Register SFR FF42y A1y Reset value 0000 11 10 T3 T3 Figure LMP1 25 Timer T3 control register 25 Microcontroller Programming LMP1 Introduction to pController programming The function of the remaining bits can be found from the User Manua
3. Al sea Currently used 1236 Bytes 15 fi22e 1 ies me fi RAW I Mstart GOSEE ASGENBO rer M Mike P Acro slack E F Kell E8 myrr 5 ampo BRANGSO iPM Figure LMP1 17 Displaying the converted value error Click on the red stop button circle in Figure LMP1 17 This interrupts the currently executing machine code The simulator displays the disassembly of the code the Instruction Program Counter IPC currently points to here address 0x1032A see Figure LMP1 18 16 Microcontroller Programming LMP1 Introduction to Controller programming zix Bie Edt view project Debug Peripherals Tools SVCS Window Help jasua tejo c E Faaal Jaula lalmmoera en on Tto geere A Analog Digital Converter Analog Digital Converter Mode Single Ch Conversion z ADCON 0 0000 00010328 ADCTC Tcpu 4 ADDAT 0x01FF ADSTC Tbc 8 ADDAT2 foxo000 0001032E 7EB6 BCLR SOTIR 00010330 F6F8B0FE MOV DPP3 0x3EB0 R8 I ADST ADBSY J ADWR ADCH 0x0000 00010334 F048 MOV R4 R8 00010336 CB00 RET pRa Channel Injection 5 void main void ADCIR J ADEIR M ADCRQ ADCIN 6 Analog Input Channels T i d t Ri lt 8 a teers ee ata 0 2 5000 1 j0 0000 200000 3 0 0000 Bi configure ADC for single conversion on channi 4 o o000 5 ooo 6 Joo000 7 foooo0 10 ADCON 0 11 8 0 0000 9 0 0000 10
4. j0 0000 11 0 0000 7 start conversion 00010338 E6D00000 MOV ADCON 0x0000 00000 o 0000 o 2 0000 j ADST 1 14 15 wait for end of conversion 0001033C 7FDO BSET ADST 0088H T3INT 008CH TANT 0090H TSINT 0094H TEINT 0098H CRINT 009CH while ADCIR 0 17 18 d fateh racnlt From ANNAT 7 x Restricted Version with 8192 Byte Code Size Limit CC19INT OOCCH xl xx Currently used 1236 Bytes 15 Selected Interrupt r Req Ena vfo ev fo For Help press F1 Rw I pal Hser GSSEONMAGE MBO Krer Muk Pprcob Ese C Fre ime OAGROREMNGSO 1577M Figure LMP1 18 Disassembly of the latest machine code instruction Try to continue stepping through the code step over The controller appears to be blocked by the highlighted line 0001032A Q9AB6FE70 JNB SOTIR 0x01032A This line of assembler code indicates that a flag SOTIR is tested and a jump to 0x1032A i e back to this very line is performed when SOTIR is zero From the Peripherals menu open the serial interface ASCO Observe that the SO Transmission Interrupt Request flag is indeed cleared Figure LMP1 19 No serial communication appears to be happening Does this explain to you why the printf statement isn t working It should If not have a look at section 3 again Stop debugging red d button and fix your program Re compile everything and test your modified program The follo
5. TOODRMNGOSHBA 1205 Figure LMP1 4 Choosing a microcontroller Per default the IDE creates a project called Target 1 with an empty folder called Source Group 1 You can modify these names in the same way you would modify a filename in explorer Select the target by clicking on Target 1 Click again to enter the filename editing mode Change the target name to Simulation Repeat this procedure to change the source folder name from Source Group 1 to Source Select File New to create a new text file A notepad style editor window should appear in the work area of the IDE Save this file as myHelloWorld c notice the file extension c Add the newly created file to the project by right hand clicking on the group folder Source and selecting Add Files to Group Source Figure LMP1 5 Microcontroller Programming LMP1 Introduction to Controller programming i myProject pVision2 jl bie Eet view project Debug Peripherals Tools SVCS Window Help asung ve 2c EF4sazka Jajaja mmen Remove Group Source and it s Files ee i Add Files to current Project Group ET f Rw I I Asan FOSTRRMAGE MIO ee Mure Pharo eaedd Q rve i CAEPODEANGDSO vin Figure LMP1 5 Adding a file to the project This concludes the creation of a new project We are now ready to write a simple C program for the C167CR microcontroller
6. exactly half of the full scale value 5 V the result will be Ox3FF 2 Ox1FF top bit clear remaining 9 bits set Continue to step through the code until you get to the printf line When executing the printf statement the simulator appears to hang Why Figure LMP1 17 Bie Exit view Project Debug Peripherals Tools Svcs Window Help jeasaa s eleci enuranwl aalala HORAE noosa Analog Digital Converter Mode Single Ch Conversion m ADCON 040000 ADCTC Tcpu 4 m ADDAT Ox01FF ADSTC Tbc 8 ADDAT2 0x0000 P ADST f ADBSY J ADWR ADCH 0x0000 IRQ Channel Injection M ADCR ADEIR M aDCRQ M ADCIN Analog Input Chimes o 25000 1 fo ooo0 2 Jo ooo0 3 fo oooo 4 foo000 5 fooooo 6 fo oooo 7 0 0000 8 foo000 3 fooooo 10 fo oo00 11 0 0000 12 fo o000 13 fo o000 14 fo oo00 15 0 0000 B F Keil _work myADC myADC c include lt stdio h gt standard I jer file regl67 h gt r j void main void unsigned int myResult 0088H T3INT 008CH TANT 0090H TSINT 0094H TEINT 0098H CRINT 009CH ADCINT O040H ADEINT O044H SOTINT O048H SORINT O04CH SOEINT 0080H SSCTINT 0084H SSCRINT 0088H SSCEINT O0BCH CCIGINT OOCOH CC17INT OOC4H CCISINT OOC8H CCISINT OOCCH Selected Interapt a a aaa F Req Ena ILVL f0 GLYL 0 ooooooooo0oo0oo0o4000000 ooooooooooo0o00oo00000 ooooooooooo0o0o0oo00000 oooooooo0o0o000000000
7. opps fox0000 rrr rrr rr Pins 0000 rrr rr r rrr rrr paes Currently used 1240 Bytes 15 TT Rw I I Hen FOSS RMASE MIO R re Muk Epa Else Orve E MOAGFOREMN DO us Figure LMP1 11 Simulating debugging the hello world project You may have noticed that as you step through your program the grey bar to the left of the source code has changed to green This code coverage feature is very useful when debugging complex programs with nested loops and conditional sub sections A green bar indicates that the corresponding line has been executed at least once Now step over the printf line Observe that the output of the printf command has been mapped to the serial port Figure LMP1 12 The next line to be executed is an endless while loop while 1 This is a frequently encountered construction which effectively halts the microcontroller Note that an embedded program starts following the power up reset from whereon it runs forever well at least until the power is cut Without this blocking instruction a microcontroller would continue to execute any arbitrary rubbish it finds in the memory locations following the program which probably will make it hang fairly quickly Alternative constructions with the same effect are while 1 and for 11 Microcontroller Programming LMP1 Introduction to Controller programming Bie Exit view project Debug P
8. wo o Q 2 z 2 5 2 x ec000000000 eccce0000000 ee09000050000 eoce000000000 xxx Restricted Version with 8192 Byte Code Size Limit xxx Currently used 1236 Bytes 15 CCISINT OOCCH x Selected Interrupt Req Ena tv fo Givi fo f Eoc J RAW p Bsar O SSERNMASEDIO H rr Muke Pharo Eseo Grivel imer N AGFODEMNASO 10m Figure LMP1 16 ADC peripheral control window 15 Microcontroller Programming LMP1 Introduction to Controller programming Step over the initialisation and the starting of the ADC first two lines of code Observe that the ADC has been configured for single conversions on channel 0 and that the conversion has been started ADST 1 ADBSY 1 Every A D conversion takes a finite time around 8 us The simulator is sufficiently realistic to take these delays into account When continuing to step through the code you should notice that code execution is briefly held by while ADCIR 0 wait for end of conversion Keep an eye on the 3 column of the interrupt control window Interrupt requests as well as on the busy flag and the start stop flag of the ADC ADBSY and ADST respectively As soon as the conversion is complete the interrupt request flag ADCIR is set and both busy and start stop flag are reset ADBSY ADST 0 The result of the conversion can be found in ADDAT from where it can be fetched and displayed As 2 5 V is
9. 0084H 00B8H OOBCH OOCOH O0C4H OOC8H E OOCCH xxx Currently used 1310 Bytes 15 Selected Interrupt Req Men ive afi ooooooooo0o000000000 oooooooooooto0oo0o0000 ooooooooooomnooooo0oo oooooooooo0oo0oto0o00000 Eac TT Rw I As FO SESRMAGE MIO P Muc Paro Ese Devei e EMEI ARANGA sos Figure LMP1 21 ADC in continuous conversion mode Notice that you could have chosen to re scale the converted result to a range from 0 to 5 V using myResult_scaled float myResult Ox3FF 5 where myResult_scaled is a variable of type float 6 Timer All digital signal processing and control applications require tasks to be run in real time The term real time refers to the requirement of synchrony between external real world events and events triggered by the microcontroller e g the acquisition of an analogue value through the ADC or the output of a controller response through a digital I O line or a D A converter 22 Microcontroller Programming LMP1 Introduction to Controller programming A microcontroller can be programmed to run at a particular sample rate using timers and the associated interrupt service routines In this section you will learn how to generate a pulse train with a fixed programmable period Chapter 10 of the User Manual introduces the general purpose timer units GPT1 GPT2 of the C167CR Most microcontrollers have similar timing units Figure LMP1 22 show
10. 3 10 DATA HIGH LINE IDLE SOTIC 0x80 SET TRANSMIT INTERRUPT FLAG TX COMPLETE SORIC 0x00 CLEAR RECEIVE INTERRUPT FLAG NOTHING REC SOBG 0x40 SET BAUDRATE TO 9600 BAUD SOCON 0x8011 SET SERIAL MODE To learn about the meaning of each of these special function registers you can consult the user manual of the C167CR The Keil IDE facilitates access to a number of user guides and datasheets Select the Books tab at the bottom of the project browser Figure LMP1 7 This tab provides convenient access to a variety of online manuals for both the microcontroller e g C167CR as well as the development environment uVision 2 From the Device Data Books section select the User Manual Figure LMP 1 8 Should this link be inactive please open the user manual from myUni at MP Course Material Tutorials C167CR User Manual Chapter 11 of this user manual outlines the functionality of the asynchronous serial communication interface ASCO Make use of this document whenever you need to program a peripheral unit with which you are still unfamiliar Microcontroller Programming LMP1 Introduction to Controller programming i myProject pVision2 F Keil _work myHelloWorld myHelloWorld c laj xj El Eie gat view project Debug Peripherals Tools SVCS Window Help laix jesng ve 24 ew dalalala lmm gnn include lt stdio h gt uVision2 include lt regl67 h gt uvision2 Re
11. 7 Er Source Files include lt reg167 h gt special function register 80C167 E Hello Ej Config Files 2 Start167 266 B E Documentation k Abstract txt i ution starts here i ialize the serial interface use tor 16 KEIL Seal SOFTWARE endif printf Hell while 1 is IDE for Microcontroller This program is protected by U S and international copyright laws For Help press F1 22c 1 Fal I Figure LMP1 2 The Keil IDE Vision 2 start up screen The main window of the IDE is split into 3 areas On the left hand side you can see the possibly empty project browser This is an Explorer type reference window through which all components of a project can be accessed e g the source files library files to be included and the online documentation Books The window at the bottom of the screen will present you with useful information about the compilation process Build error messages etc Finally the large central area of the screen is where you are going to develop and debug your programs The IDE automatically opens the latest project here the sample program hello world Close this project by pulling down the Project menu and selecting Close Project Microcontroller Programming LMP1 Introduction to Controller programming Create an empty project by choosing Project New Project Change the path to your workspace on the departmental server Save the empt
12. Microcontroller Programming LMP1 Introduction to Controller programming Laboratory LPM1 Example based introduction to pController programming Objectives To learn about typical elements common to most modern microcontrollers To introduce the Keil development environment for Infineon microcontrollers as a tool for a simulation based microcontroller programming To create a simple project and learn how to debug it in simulation mode To explore the C167CR information data base and sample programs Introduction The Infineon C166 is a family of 16 bit microcontrollers which has become a popular platform in the automotive industry Originally designed in the early 90s by Siemens Semiconductors now Infineon Technologies www infineon com the C166 family has quickly grown and currently includes the following members C161 C163 C164 C165 and C167 The various members of this family differ in the amount of on chip RAM and or ROM as well as in the number of peripheral elements including the Digital Input Output DIO channels the Analog to Digital Converters ADC the Pulse Width Modulation PWM units the General Purpose Timers GPT event counters capture and compare CAPCOM and communication interfaces for synchronous serial communication SSC asynchronous serial communication ASC Controller Area Network CAN bus based communication etc Many of these peripheral elements are fairly standard to most modern microcontroll
13. OxFFFF reduces the maximum period to a smaller value Depending on the direction of the timer counting upwards or downwards two scenarios are possible Figure LMP1 30 Once a suitable period has been programmed and the timer has been configured for single shot operation or reload operation execution can be started timer run Interrupts can be configured to be triggered by timer overflows and or underflows or upon reaching the threshold value reload value With an appropriate interrupt service routine a timer can be used to toggle output pins in a regular timely manner frequency generation pulse width modulation etc Other uses of timers include the periodic computation of system equations digital control filters and the periodic servicing of ADCs and DACs 29 Microcontroller Programming LMP1 Introduction to uController programming OxFFFF upwards 0x9860 0x0000 Os 1s 1 68 s Duration 1 68s 1s 0 68s OxFFFF downwards 0x9860 z 0x0000 Os Duration 4s 1 68 s 1s Figure LMP1 30 Upward and downward directed timer operation 7 Where to go from here This initial laboratory has been designed to get you up to speed with a number of fundamental concepts of microcontroller programming The advantage of the Keil developing environment with its powerful simulator will hopefully make it a little easier for you to develop a grasp of the most important elements of a microcontroller Subsequent laboratories will focus on the Mo
14. and test it using the pVision simulator 2 A simple Hello world program Most introductions to high level programming languages begin with a simple program displaying the message Hello world on the standard output usually the screen of the monitor With microcontrollers however things are slightly more complicated In contrast to a personal computer PC a microcontroller does not have a dedicated standard output The habitual C commands printf and scanf can therefore not be used in the same way as on a PC A microcontroller program can display information by changing the logic levels high low of its digital I O pins Connecting a logic probe LED oscilloscope etc these levels can be made visible to an external observer Such a single bit display is usually limited to true false statements such as program has reached line X or A D conversion complete Slightly more flexibility arises from combining n digital I O lines to an n bit binary coded word The value of this data word can then be displayed using n LEDs a 7 segment display or an LCD display Most frequently however the microcontroller is connected to a host computer through a serial or parallel cable Using serial communication and a terminal program the microcontroller can be provided with a standard output channel similar to that of a PC e g the currently active window on the screen The output of C commands such as printf and pu
15. ctively Details can be found in the User Manual p 10 17 f Figure LMP1 27 shows the internal structure of T2CON Figure LMP1 28 illustrates the automatic reload mechanism T2CON Timer 2 Control Register SFR FF40 4 A0y Reset value 0000 15 14 13 12 11 10 9 T2 T2 Figure LMP1 27 Timer T2 control register T2CON 26 Microcontroller Programming LMP1 Introduction to Controller programming Source Edge Select Reload Register Tx Interrupt TxIN t Request interrupt Request Up Down A T3OUT Note Line only affected by over underflows of T3 but NOT by software modifications of T3OTL Figure LMP1 28 Block diagram of the auxiliary timer T2 T4 in reload mode Programming a timer is pretty similar to programming an A D converter A number of special function registers have to be configured an interrupt priority level might have to be selected etc In summary the following aspects need to be looked at The timer unit first needs to be configured for timer mode with a particular pre scale factor T3CON An initial timer value will have to be placed in the timer register T3 To activate the automatic reloading mechanism the auxiliary timer register T2 or T4 will have to be loaded with the same value the corresponding timer control register T2CON T4CON needs to be configured for reload operation If the timer is meant to trigger interrupts an interrupt priority level needs to be set in T3IC structu
16. d c Before we can compile and execute our program we have to tell the compiler where to put the code on the C167CR microcontroller RAM ROM starting address etc The specification of these details is necessary as the memory map of the C167CR might vary from one project to another Select menu Project Options for target Simulation and enter the following information Figure LMP1 9 ROM 0x0 Length 0x20000 RAM 0x80000 Length 0x10000 This instructs compiler and linker to produce executable code in the ROM address space from 0 to 0x20000 with all variables stored in RAM beginning at 0x80000 These values are fairly uncritical as we are not going to use real hardware in this exercise simulation only virtual target hardware Device Target Output Listing C166 EC A166 L166 Locate L166 Misc Debus Infineon C167CR LM Clock MHz 20 0 Memory Model Small near functions and data v I Use On chip CAN XRAM 0xE000 OxE7FF Operating System None H Data rear 6 m Near Memory 16 KB RAM 16 KB ROM r Btemal Memory Start Size 1 Rom o 20000 4 RAM v M 2 RAM x x80000 amp lt 10000 5 RAM Emme 3 Ram gt 6 Ram gt eee Figure LMP 1 9 Target options for the hello world program Accept these settings OK and click on build target see small circle in Figure LMP1 10 Compiler and linker will be called and the status wind
17. entiometer connected to channel 0 We will therefore set up the controller to perform a fixed channel single conversion Three special function registers control the operation of the ADC ADCON ADC control register ADDAT ADC data register converted value can be found here ADCIC ADC interrupt control register Figure LMP1 13 shows an overview of the internal structure of the ADC unit Figure LMP1 13 Structure of the ADC on the C167CR ANO Result Reg ADDAT Result Reg ADDAT2 10 Bit Converter Vv a AREF AGND AN15 MCB04389 The ADC works by comparing the input voltage to a fixed reference voltage between Vacnp 0 V and Varer 5 V Voltages below and up to 0 V produce an output value of 0 voltages beyond 5 V produce the full scale value Ox3FF all 10 bits set to 1 To configure the ADC for fixed channel single conversion operation the ADCON register needs to be loaded with an appropriate value cf User Manual p 17 3 Figure LMP1 14 shows the internal structure of the ADC control register ADCON 13 Microcontroller Programming LMP1 Introduction to Controller programming ADCON ADC Control Register SFR FFA0 D0 Reset value 00004 eam rw w mw wh w Figure LMP1 14 Structure of the ADCON register The individual bits of ADCON determine the mode of operation ADM the channel to be converted from ADCH and they allow a conversion to be started or stopped ADST The bu
18. er Programming LMP1 Introduction to Controller programming Now try to change the operational mode from single conversion to continuous conversion You can do this by selecting Single Channel Continuous Conversion from the list box of the ADC control window Enter a new voltage for channel 0 e g 2 V and start the ADC check box next to ADST Notice that the value of ADCON has changed to 0x0190 Continue to step through the code You should observe that interrupt is triggered very frequently now In fact we almost immediately re enter the ISR as soon as we get out Try a few voltages to ensure that your program is working correctly Figure LMP1 21 BEE Eie Edt view project Debug Peripherals Tools Svcs Window Help jeasa s elec nurnewl HalulalamEnenm E lE o t Tro gt ilea raoe AZ Analog Digital Converter ey p Analog Digital Converter Mode Single Ch Cont Conv Y ADCON 0 0190 ADCTC Tcpu 4 ADDAT ox03FF ADSTC Tbc 8 z ADDAT2 Ox0000 IV ADST V ADBSY J ADWR ADCH 0x0000 IRQ Channel Injection I ADOIRA I ADEIR M ADCRQ M ADCIN m Analog Input Channels aa a a fs 0000 1 fooo00 2 fooo00 3 fo oo00 4 ooo 5 fooooo 6 fo oooo 7 fo o000 a foo000 s foo000 10 fo o000 11 0 0000 12 0 0000 13 0 0000 14 0 0000 15 0 0000 0088H 008CH 0090H 0094H 0098H 009CH 00A0H 00A4H 0048H OOACH OOBOH
19. eral control windows will allow you to observe what is going on inside the microcontroller while your program is running Step over the first line of your program Single stepping can be achieved using the Step Into or Step Over button indicated by a small circle in Figure LMP1 11 Bit 10 in the data direction register of port 3 DP3 should now be set thereby defining P3 10 as output cf the window labelled Parallel Port 3 Observe how each line you execute sets or resets bits within the corresponding special function registers Step through your program until you reach the printf line 10 Microcontroller Programming LMP1 Introduction to Controller programming PT myPropect WW OOOO OOO eix le Eeit wew project Debug Peripherals Tools Svcs Window Help or 4 wT 08 fe Ow B F Keil _work myHelloWorld myHelloWorld c include lt stdio h gt f 1 include lt regl 7 h gt Mode 8 Bit Synchronous Baudrate 2500000 M SOR soc foomo S0STP I SOREN socon 0x0000 r sop SOTBUF 00000 sopRS SoRBUF ox0000 M SOODD p Error Detection I SOOEN I SOFEN M SOPEN F sooe f SOFE T SOPE r lhterugs I sotiR SORIR M SOEIR I SOTBIR endless loop to stop program from running into void TETE MOAISTE C P310 I DP310 M DP311 Parallel Port 3 xj Port 3 Pa foo rrr Ere ere DP3 fo0000 r rrr rrr
20. eripherals Tools Svcs Window Help jascha semeloeleernnanwl aaneen AETR RRA AEL aaa include lt stdio h gt include lt regl 7 h gt Mode 8 Bit Baudrate pas WM SOR soc foooao amp S0STP IZ SOREN socon oem F soe SOTBUF 0x0004 I soBRs SoRBUF ox0000 M SOODD p Error Detection I SOOEN I SOFEN I SOPEN J sooe f SOFE SOPE r Interupts I sotiR f SORIR M SOEIR M SOTBIR r Ports i P310 M DP310 M DP311 Parallel Port 3 3 E o Frrr iere rrr Er ello World bPa oa e Opps foxoo0 Pee cee Pins 0040 rr er cee paes Currently used 1240 Bytes 15 I ES Sipe Hsor FSSESRMAGE MIO Sre wrx paro Eae Orve Emer EAMA MANGAO sew Figure LMP 1 12 Mapping of the standard output printf to the serial port To stop the debug mode and return to the programming environment select Debug gt Start Stop Debug Session or click on the red d button in the toolbar Note The remapping of printf and putc to the serial interface is not the only way a microcontroller can be given access to a clear text output medium monitor screen printer Another common approach is to supply customized print commands which send characters strings or numbers to the serial interface The structure of these commands is very similar on all microcontrollers Frequently a suitable collection of commands can be fou
21. ers The diagram shown in Figure LMP1 1 outlines the structure of the Infineon C167CR microcontroller C167CR C167CR Figure LMP1 1 The C167CR microcontroller Notice how RAM ROM and all peripheral units are connected to the computational C166 core via a common address and data bus Von Neumann architecture The 1 Microcontroller Programming LMP1 Introduction to Controller programming external bus controller is a gateway to the optional external address and data bus providing the microcontroller with access to externally connected peripherals e g external memory etc The computational core comprises the central processing unit CPU an interrupt controller as well as a special Peripheral Event Controller PEC not used in this exercise A number of programming environments are available for this microcontroller family In this exercise we will use Keil wVision an Integrated Development Environment IDE which includes a powerful simulator of C166 based microcontrollers A code size limited evaluation version of the Keil development tool chain can be obtained free of charge from www keil com 1 Creating a new project Launch the IDE by clicking on the Start menu entry Keil uVision 2 You should be presented with the following screen Figure LMP1 2 Bie Exit view project Debug Peripherals Tools Svcs Window Help asA i eB 2cl s ee 9 6 QiDR sane include lt stdio h gt standard I O h file
22. familiar with e g a new programming language a piece of equipment you ve never before come across etc The good news You won t be sacked if you don t mange to do it all within 6 weeks 31
23. features of the underlying computer hardware processor memory etc Once T3 is zero we regain control Single step through the code until T3 is decremented to OxFFFF underflow This should take approximately 60 odd steps use F10 Observe that the output toggle flag T3OTL has been set This flag is also displayed on T3OUT P3 3 On a real microcontroller we would be able to measure a logical high level 5 V on the associated pin Also note that the timer register T3 has been reloaded with the value from T2 0x9860 and that the timer T3 interrupt request flag T3IR has been set This could be used to divert execution control from the main program to an interrupt service routine T3INT You can click on RUN again to repeat this experiment Stop the debugger simulator when you get bored Conclusion It should be noted that other microcontrollers might implement their timer counter control in a slightly different way However the fundamental concept is always the same A pre scale factor is chosen to slow down the otherwise too fast running clock signal for a 16 bit timer counter this selects a maximum timer period of Tmax pre scale factor fosc x OxFFFF Example Tmax 512 20 10 x OxFFFF 512 20 10 x 65535 1 678 seconds A fine adjustment within this maximum period can be achieved with the initial value reload value of the timer Initialising the timer and or the reload register with a smaller value than
24. ges0 T TAN xxx Restricted Version with 8192 Byte Code Size Limit Pi foxoo0n a ENEENEEN T2CON Joxo027 M TAR xxx Currently used 324 Bytes 3 ne I TTT Rw Msur F SSE SRMASESIO fSiec Mm psc Epe Drk Hece AGFODEMNGSO 7 29C 1 Figure LMP1 29 Simulating a periodic timer It quickly becomes apparent that single stepping is not very efficient when trying to understand timers It takes around 60 70 clicks of the mouse to decrease the timer register by one don t do this use F10 instead please To by pass this problem we can define a conditional breakpoint From the Debug menu select Breakpoints You should be presented with a window allowing you to define and edit complex breakpoints 28 Microcontroller Programming LMP1 Introduction to uController programming Click inside the text box Expressions and enter the following condition T3 Click on Define and Close the window This instructs the simulator to halt execution when timer register T3 reaches 0 From the View menu select Periodic Window Update then start execution by clicking on the RUN button see small circle in Figure LMP1 29 Observe how the timer register T3 is decremented until the value 0 is reached On a real C167CR microcontroller running at 20 MHz this would take exactly 1 second The execution speed of the simulation on the other hand depends on the
25. he CPU of the controller contains a general interrupt enable flag IEN This master switch can be used to quickly enable disable all configured interrupts In our case this only affects the ADC interrupt as we haven t allowed any other interrupts yet Make the following modifications to the previous program or load the corresponding electronic copy from myUni MP Course Material gt Tutorials C167CR myADCint c 19 Microcontroller Programming LMP1 Introduction to wController programming include lt stdio h gt standard I O header file printf include lt regl67 h gt special function registers of the C167 global variables unsigned int myResult oldResult end of conversion ISR void ADC_finished void interrupt 0x28 conversion complete gt fetch result from ADDAT myResult ADDAT main program void main void initialize serial interface 9600 bps 8 N 1 DP3 0x0400 PORT 3 10 DIRECTION CONTROL OUTPUT TXD DP3 amp 0x0800 PORT 3 11 DIRECTION CONTROL INPUT RXD P3 0x0400 PORT 3 10 DATA HIGH LINE IDLE SOTIC 0x80 SET TRANSMIT INTERRUPT FLAG TX COMPLETE SORIC 0x00 CLEAR RECEIVE INTERRUPT FLAG NOTHING REC SOBG 0x40 SET BAUDRATE TO 9600 BAUD SOCON 0x8011 SET SERIAL MODE initialize variables myResult 0 oldResult 0 configure ADC for single conversion on channel 0 ADCON
26. he main program to an interrupt service routine ISR An ISR is a user supplied piece of code which is executed when the associated interrupt is triggered Three things will have to be added to our program to make it work with interrupts 1 An interrupt service routine ISR will have to be supplied 2 The ADC interrupt control register ADCIC needs to be configured to define the priority level of the interrupt to be triggered 3 All interrupts will have to be allowed to happen processor flag 18 Microcontroller Programming LMP1 Introduction to Controller programming The C167CR implements a hierarchy of 16 main interrupt levels ILVL Each of these levels is sub divided into 4 sub levels the so called group levels GLVL Altogether this gives a programmer the freedom of choosing up to 64 different priority levels higher order tasks can interrupt lower order tasks In our case however we only have a single interrupts source namely the ADC We can thus set the interrupt level and or group level to any value say ILVL 5 GLVL 1 Figure LMP1 15 p 14 shows the ADC interrupt control register ADCIC To configure the ADC for the above values we need to set ILVL to 5 0101 and GLVL to 1 01 Furthermore the ADC Interrupt Enable ADCIE needs to be set In summary ADCIC 0000 0000 0101 0101 0x0055 tht ADCIE GLVL ILVL The second modification of our program is the inclusion of an interrupt service routine fo
27. ing two lines DP3 0x0400 PORT 3 10 DIRECTION CONTROL OUTPUT TXD DP3 amp 0x0800 PORT 3 11 DIRECTION CONTROL INPUT RXD 6 Microcontroller Programming LMP1 Introduction to uController programming DP3 is the 16 bit data direction register of port 3 Setting a bit gt 1 in DP3 configures the corresponding signal pin as output re setting a bit 0 ensures that the corresponding pin is switched onto the input driver The above lines set bit 10 of DP3 while bit 11 is reset to zero Recall that DP3 0x0400 is short for DP3 DP3 0x0400 binary OR and that the operator stands for binary OR The hexadecimal value 0x0400 can be interpreted as binary 0000 0100 0000 0000 i e bit 10 is set whereas all other bits are zero This value is called a positive mask for bit 10 A negative mask can be produced using the negation operator 0x0800 0000 1000 0000 0000 1111 0111 1111 1111 OxF7FF To reset a bit of a special function register we AND it with a negative mask This is what is done above DP3 amp 0x0800 is short for DP3 DP3 amp OxF7FF binary AND The other five lines of the initialisation of SO pull the transmission line P3 10 high set and reset the transmission interrupt flag and the reception interrupt flag respectively and configure the interface for a line speed of 9600 bps 8 data bits no parity checking and 1 stop bit P3 0x0400 PORT
28. l p 10 3 T3M denotes the mode of operation timer counter gated counter etc T3R starts and stops the timer T3UD up down indicates whether the timer is incrementing or decrementing etc Interesting effects can be achieved in conjunction with the alternate output function pin T30UT internally connected to port P3 3 Enabling the alternate output function T30E 1 allows the state of the output toggle flag T3OTL to be displayed on P3 3 This port pin will have to be set up as output DP3 3 1 and initialised with a logical high level P3 3 1 Flag T3OTL is toggled every time the timer overflows when counting up or underflows when counting down Programming a periodic timer with a period of say 1 second would therefore produce an output pulse train on P3 3 with a period of 2 seconds 1 second ON 1 second OFF Figure LMP1 26 illustrates the use of the alternate output function pin T3OUT interrupt fopu Request TxOUT TxEUD TxUDE MCBO2028 T3EUD P3 4 T30UT P3 3 x 3 Figure LMP1 26 Alternate output function of timer T3 It should be noted that timer T3 can be made periodic using an automatic reload mechanism The auxiliary timer registers T2 and T4 can be configured as reload registers with every T3 overflow underflow triggering an automatic transfer of the contents of T2 or T4 into timer register T3 This reload operation is activated through the respective timer control registers T2CON or T4CON respe
29. lease Not uVision2 User s Guide Effi Tools User s Guide bq Release Notes void main void uVision2 Getting Star standard I O header file printf special function registers of the C167 tate e ee beatae main program ae a aa te C166 User s Guide initialize the serial interface C166 Library Function DP3 0x0400 PORT 3 10 DIRECTION CONTROL OUTPUT TXD EC166 Embedded C DP3 s 0x08007 PORT 3 11 DIRECTION CONTROL INPUT RXD A166 amp Utilities User s P3 0x0400 PORT 3 10 DATA HIGH LINE IDLE uVision2 Debug Comn SOTIC 0x80 SET TRANSMIT INTERRUPT FLAG TX COMPLETE Eh ffi Device Data Books SORIC 0x00 CLEAR RECEIVE INTERRUPT FLAG NOTHING REC Product Overview SOBG 0x40 SET BAUDRATE TO 9600 BAUD Data Sheet SOCON 0x8011 SET SERIAL MODE display hello world printf Hello World n Instruction Set Manu endless loop to stop program from running into void wWhile 1 For Help press F1 Bsr QGOSSERDNMAGEMBO H5xe Mur paced B E Fei E mver E Figure LMP1 7 Access to the reference documentation amp Acrobat Reader M167 pdf lal xj PE Ble Edit Document Tools View Window Help 18 x jSASS4 48 Bl gt oiesi or eo FA A C 1 Introduction L 6 Clock Generation 7 Parallel Ports 8 Dedicated Pins 2 Architectural Overview 3 Memory O
30. les to zero i e nothing will be displayed unless a conversion has taken place When the ongoing conversion is complete after a few clicks the interrupt request flag ADCIR is set and the end of conversion interrupt is triggered The main program is interrupted and code execution resumes inside the interrupt service routine ADC_finished Notice that upon entry to the ISR the interrupt controller automatically resets the interrupt request flag ADCIR This is a particularity of the C167CR Other microcontroller might require the programmer to ensure that the interrupt request flag is cleared The ISR loads the global variable myResult with the conversion result found in ADDAT before returning to the main program This in turn breaks the blockage of the main program myResult oldResult which resumes operation by printing the result of the conversion Continue to step through the program You should observe that following the first successful conversion the program doesn t seem to do a great deal it seems that we re stuck on the above while line even if we change the voltage of channel O to another value e g 1 V The reason of course is that we have configured the ADC for single conversions Change the voltage to 1 V and check the box next to ADST This has the effect of re starting the ADC After a few clicks of the mouse the conversion is complete and the interrupt is triggered again converted value cc 21 Microcontroll
31. nd somewhere on the Internet be it in source code format or as a library of compiled objects In this case a programmer simply has to add the downloaded files Source code or library to their project and include the corresponding header file at the top of any module that calls upon these functions An example is shown below include lt stdio h gt standard I O header file printf include lt reg1l67 h gt special function registers of the C167 include mySerial h serial interface functions void main void initialise serial interface UART_Init 9600 makes use of a function from the library display hello world UART_SendString Hello World n makes use of a function from the library endless loop to stop program from running into nowhere land while 1 12 Microcontroller Programming LMP1 Introduction to uController programming 4 A D conversions In this section we are going to write a program that reads the on chip A D converter and displays the acquired value on a terminal connected to the serial interface SO Chapter 17 in the User Manual provides some background information about the ADC of the C167CR There appear to be several modes of operation including fixed channel single conversion fixed channel continuous conversion and a number of multi channel scanning modes In this exercise we are simply interested in the acquisition of an analogue voltage from a pot
32. nd the ADC interrupt ADCINT cf Figure LMP1 16 1 myProject wVision2 aa Eie x ele Edit view project Debug Peripherals Tools SVCS Window Help asugi vej 2ac Er4sazka Hale Slqinm sene E S MO 0 gt gaarne A xi pAnalog Digital Converter amp F Keil _work myADC myADC c A Single Ch Conversion gt include lt regl67 h gt special function registers of the C167 Modes Single Ch Conversion E ADEDN 0 0000 ADCTC Tcpu 4 x ADDAT 0x0000 void main void ADSTC Tbe 8 z ADDAT2 0x0000 unsigned int myResult J ADST f ADBSY J ADWR ADCH 00000 5 P m RQ _ Channel Injection fs f re ADC for singl onversii on channel 0 Bie i aes CS iiig P ADCIR f ADEIR r ADCRQ ADCIN 7 Analog Input Channels start conversion ADST 1 0 25000 i foo000 2 foo000 3 fo ooco ro 4 0 0000 5 0 0000 6 j0 0000 7 0 0000 wait for end of conversion while ADCIR 0 8 0 0000 9 0 0000 10 j0 0000 11 0 0000 fetch result from ADDAT 12 0 0000 13 0 0000 14 0 0000 15 0 0000 myResult ADDAT conversion finished gt print result printf Converted value x n myResult Int Source Vector Req Ena IVL Givi a T2INT 0088H 0 0 0 0 endless loop to stop program from running into nowhere land while 1 a E 2 B T ooooo 0 0 0 0 0 0 ofr POD OOO
33. nterrupt Control C 12 The High speed Synchronous Serial Interface OTN L 13 The Watchdog Timer WDT E a a o C 14 The Bootstrap Loader 15 The Capture Compare Units L 16 The Pulse Width Modulation Module 7 17 The Analog Digital Converter FxD0PS 10 18 The On chip CAN Interface L 19 System Reset _ 20 Power Management 21 System Programming 1 22 The Register Set Bako Eror Interrupt Control Register 23 Instruction Set Summary pia L 24 Device Specification 25 Keyword Index OOPS Por 3 Open Drain Control Regiater P3 Port 3 Data Register DPS 3 Direction Control Register SOCON ASCO Control Register S056 _ ASCO Baud Rate Generi Reload Reg ASCO Receive Buffer Register read SOTBUF ASCO Transmit Buffer Register SOIC ASCO Traramit Interrupt Contol Register SOTBIC ASCO Transmit Butler Interupt Cti Reg oniy ASCO Receive Interrupt Control Figure 11 1 SFRs and Port Pins Associated with ASCO a ET M 4 2510464 bm 817x1157 O H W i s GOSERNMAZEmIO Belac du Qo Da gul gu EAA ae LE TE 38M Figure LMP1 8 The Infineon C167CR User Manual Microcontroller Programming LMP1 Introduction to Controller programming Switch back to the uVision IDE and enter the above program A digital copy can be found on myUni at MP Course Material Tutorials C167CR myHelloWorld c Copy and paste this program into the previously created empty program file and save it as myHelloWorl
34. nversion This is done by setting bit 7 in ADCON The Keil compiler provides direct access to the ADST using the single bit addressing mode of the C167CR microcontroller To start a conversion a programmer simply sets ADST to 1 ADST 1 this starts a conversion Keil Alternatively ADCON could have been ORed with the following positive mask ADCON 0x0080 alternative way of starting the ADC 14 Microcontroller Programming LMP1 Introduction to Controller programming Enter the following program or copy n paste it from myUni MP Course Material Tutorials C167CR myADC c include lt stdio h gt standard I O header file printf include lt regl67 h gt special function registers of the C167 void main void unsigned int myResult configure ADC for single conversion on channel 0 ADCON 0 start conversion ADST 1 wait for end of conversion while ADCIR 0 fetch result from ADDAT myResult ADDAT conversion finished gt print result printf Converted value x n myResult endless loop to stop program from running into nowhere land while 1 Compile the code and launch the debugger simulator From the Peripherals menu open the ADC control window as well as the interrupt control window In the ADC control window set the input voltage of channel 0 to 2 5 V In the interrupt control window scroll down to fi
35. ow should display O errors and 0 warnings Microcontroller Programming LMP1 Introduction to Controller programming myProject pVision2 F Keil _work myHelloWorld mytelloWorld c 18 x IEl Eie Exit view Project Debug Peripherals Tools Svcs Window Help 1a xj ion seelocl seen el Jausseran include lt stdio h gt include lt regl67 h gt aiBuild target Simulation 4 compiling myHelloWorld c linking myProject 0 Error s 0 Warning s N Build A command A Finan Fies J IL gt Build target 22c 1 mapes Rw sur O 3 ER AGEMBO P re Mmix Ppacod Jsdo C F kei i mver DEAM FI ARANGO 114M Figure LMP1 10 Building the hello world project 3 Simulating and debugging the hello world program In this section we are going to use the simulator debugger of the uVision IDE The simulator can be launched from the Debug menu by clicking on Start Stop Debug Session Alternatively you can click on the red d button in the toolbar The simulator should start with the cursor on the first line to be executed yellow arrow Figure LMP1 11 The CPU registers are displayed instead of the project browser window From the Peripherals menu open the Serial Interface ASCO as well as I O Port 3 From the View menu open the Serial Window 1 You may have to click on Window Cascade to see all open windows Figure LMP1 11 The periph
36. program from running into cyberspace while 1 Upon compilation launch the simulator debugger and display the peripheral control windows for timer T3 auxiliary timer T2 as well as for the parallel port P3 Step through the code carefully observing how the timer registers and flags are affected by the commands Figure LMP1 29 myProject pVision2 _ 18 xj bie Edit view project Debug Peripherals Tools SVCS Window Help j sea seeloci eneneul Hale Slqinm senre a b 8 o gt HE AAY BOE DP B F Keil _work myT3int myT3 c T3CON 0x0286 program port 3 pin 3 as output initialise with 1 pores ee DP3 0x0008 pin 3 P3 0x0008 pin 3 Timer 7 load initial timer value T3 Input Prescaler 512 z T3 0x9260 this represent a duration of 1 second pre scale factor 512 eat load auxilary timer T2 with the same value reload value F T3UDE J T3EUD V T3UD T2 0x9860 Direction Down i uxilary ti for reload mode any transition of T30TL r Output 7 11 0x0027 7 T2CON 0x002 i ET mode F 1301 M 130E M T30uT Vv start timer Status Run M T3R T3R 1 3 09860 i T3N keep program from running into cyberspace T3CON 0x02C6 F T3R while 1 x p Timer Counter 2 m Mode Reload hs either Transition at T3OTL F pDiecion rao FT Me erence Port 3 px foome r rre eres pee ds DPS ox0008 ee er Status Stop F Tar opps foxo000 ee eee T2 fox
37. r the ADC interrupt The online User Manual tells you that the ADC triggers an interrupt with a reference number 0x28 Chapter 5 Table 5 1 This interrupt reference number denotes an entry within the interrupt vector table It appears that the ADC interrupt is the 0x28 40 entry in this table The interrupt vector table is a list of 4 byte starting addresses for all interrupt service routines know to the microcontroller The compiler needs to be instructed to place the starting address of our end of conversion ISR at vector number 0x28 This is done using the special attribute interrupt A sub routine can be defined as ISR using the following construct void myISR void interrupt lt vector_number gt i ae In our case we would define the ISR ADC_finished as follows void ADC_finished void interrupt 0x28 ADC finished gt fetch result from ADDAT myResult ADDAT Notice that the address of an interrupt vector can always be found by multiplying the interrupt vector number by 4 This is because there are 4 bytes per interrupt vector For the end of conversion interrupt the vector address is 4 0x28 0x00A0 This value is displayed in the interrupt control window of the simulator debugger Finally in addition to supplying an interrupt service routine ADC_finished and configuring the ADC interrupt control register ADCIC we also have to instruct the microcontroller to allow all interrupts The status register within t
38. rally identical to ADCIC Furthermore an interrupt service routine needs to be provided for T3INT vector 0x23 and all interrupts need to be enabled in general IEN 1 If the alternate output function T3OUT is to be used we also need to program port P3 3 as output and initialise it with a logic high level The following program generates a regular square wave signal with a period of 2 seconds and a duty cycle of 50 1 second ON 1 second OFF Enter this program or simply copy it from myUni MP Course Material gt Tutorials gt C167CR myT3 c include lt regl67 h gt special function registers of the C167 main program void main void configure T3 for timer mode pre scale factor 512 period 1 second enable alternate output function T30UT load toggle bit T30TL with 0 T3CON 0000 0010 1000 0110 0x0286 T3CON 0x0286 program port 3 pin 3 as output initialise with 1 27 Microcontroller Programming LMP1 Introduction to Controller programming DP3 0x0008 pin 3 P3 0x0008 pin 3 load initial timer value T3 T3 0x9860 this represent a duration of 1 second pre scale factor 512 load auxilary timer T2 with the same value reload value T2 0x9860 configure auxilary timer for reload mode any transition of T30TL T2CON 0000 0000 0010 0111 0x0027 T2CON 0x0027 reload mode start timer T3R 1 keep
39. rflow interrupt which can be used to restart the timer 23 Microcontroller Programming LMP1 Introduction to uController programming The principal operation of a timer is best explained using the following diagram which has been drawn for a downward counting 16 bit timer range 0x0000 OxFFFF cf Figure LMP1 23 OxFFFF Initial value 0x0000 0 duration Tni Toa Figure LMP1 23 Operation of a general purpose timer With every cycle of the CPU clock signal the timer register is decreased or increased by 1 Counting through the entire range from 0x0000 to OxFFFF would therefore lead to a maximum duration of EL T A max CPU With a clock frequency of fepu 20 MHz this corresponds to T yay 20 MHz 36 OxFFFF 3 277 ms 0 The minimum time that could be programmed at 20 MHz would require an initial value of 1 counting downwards The corresponding T min is Tin 20 MHz aa 0x0001 5 0 10 s 50 ns Tmin iS also called the resolution of the timer As these durations might be too small for many applications the timer unit can be configured to pre scale the incoming CPU clock signal This 2 1 frequency divider allows the clock signal to be slowed down thereby increasing the maximum accessible period Tmax However it should be noted that this is at the expense of a decreased resolution increased Tmin The following table Table LMP1 1 lists the available pre scale factors for timer T3
40. rganization 4 The Central Processing Unit CPU 5 Interrupt and Trap Functions L 9 The External Bus Interface C 10 The General Purpose Timer Units if C167CR Derivatives The Asynchronous Synchronous Serial Interface 11 The Asynchronous Synchronous Serial Interface The Asynchronous Synchronous Serial Interface ASCO provides serial communication between the C167CR and other microcontrollers microprocessors or external peripherals The ASCO supports full duplex asynchronous communication up to 781 KBaud 1 03 MBaud and half duplex synchronous communication up to 3 1 4 1 MBaud 25 33MHz CPU clock In synchronous mode data are transmitted or received synchronous to a shift clock which is generated by the C167CR In asynchronous mode 8 or 9 bit data transfer parity generation and the number of stop bits can be selected Parity framing and overrun error detection is provided to increase the reliability of data transfers Transmission and reception of data is double buffered For multiprocessor communication a mechanism to distinguish address from data bytes is included Testing is supported by a loop back option A 13 bit baud rate generator provides the ASCO with a separate serial clock signal 11 1 Asynchronous Operation 11 2 Synchronous Operation 11 3 Hardware Error Detection Capabilities 11 4 ASCO Baud Rate Generation 11 5 ASCO Interrupt Control Ports amp Direction Control Data Registers Control Registers I
41. s a block diagram of the general purpose timer unit GPT1 This unit comprises 3 independent timers the core timer T3 as well as two auxiliary timers T2 and T4 In this exercise we will program the core timer T3 Interrupt f gn 4 T2 GPT1 Timer T2 Request Mode TOINF Control amp Reload Interrupt aay Request Toggle FF T3 T3IN ee Mode i GPT1 Timer T3 a T3OUT Control T3EUD Other Timers T4INT T4 if moce Inte t j n Control 2PT1 Ti frup fi ay GPT1 Timer T4 Teaia p U D MCTO2141 T4EUDT Figure LMP1 22 General purpose timer unit GPT 1 A timer is essentially a 16 bit register whose value can be incremented or decremented by internal or external events External events are commonly rising and or falling edges on an associated digital input line When used with external events the general purpose timer is said to be operating in gated counter mode as it is simply counting events Internal events are rising falling edges of the CPU clock signal and overflow events of other timer registers cascaded timers As the CPU clock signal is a very regular timing pattern counting its edges is equivalent to measuring durations or periods of time timer mode The maximum period of a timer depends on the clock frequency as well as the programmed initial timer value The latter denotes a threshold from which the timer counts up towards OxFFFF or down towards 0x0000 Reaching these limits triggers a timer overflow unde
42. sy flag ADBSY can be used to determine if a conversion is currently ongoing ADBSY 1 or has been completed ADBSY 0 Alternative to polling the ADBSY flag a programmer can choose to trigger an interrupt once the conversion has been completed This can be done by assigning one of the 64 available interrupt levels to the ADC Special function register ADCIC is used to set this level Figure LMP 1 15 ADCIC ADC Conversion Intr Ctrl Reg SFR FF98 CC Reset value 00 15 14 13 12 11 10 9 8 F 6 5 4 3 2 1 0 ADC ADC gt 7 7 TW TW TW Figure LMP1 15 Structure of the ADCIC register A third way of noticing the end of a conversion is to poll the ADC interrupt request flag ADCIR This flag is set upon completion of a conversion it thus works inversely to the ADBSY flag In our first ADC program we shall poll the ADCIR flag to detect the end of a conversion The triggering of the end of conversion interrupt will remain disabled ADCIE 0 To configure the ADC for single conversions on channel 0 we have to set the 2 bits of ADM to 00 fixed channel single conversion mode and the 4 bits of ADCH to 0000 channel 0 All other bits can remain at their default value 0 Altogether ADCON will have to be loaded with 0x0000 coincidentally this is the reset value strictly speaking we wouldn t have to assign a value to ADCON to configure the ADC for single conversion on channel 0 The next step is to start the co
43. tc is re directed to the serial interface thereby allowing a developer to display information on the screen of the host computer Many software development 5 Microcontroller Programming LMP1 Introduction to Controller programming environments for microcontrollers ship with customized ANSI C libraries which readily implement this re direction of the standard input output channel to the serial interface All a programmer needs to do is to configure the serial interface of the microcontroller for communication at the desired line speed e g 57600 bits s The following short program demonstrates how this can be done on the C167CR include lt stdio h gt standard I O header file printf include lt regl67 h gt special function registers of the C167 RRR KKK RK KR KK KKK main program p RRR KKK KKK RK KKK void main void initialize the serial interface sf DP3 0x0400 PORT 3 10 DIRECTION CONTROL OUTPUT TXD DP3 amp 0x0800 PORT 3 11 DIRECTION CONTROL INPUT RXD P3 0x0400 PORT 3 10 DATA HIGH LINE IDLE SOTIC 0x80 SET TRANSMIT INTERRUPT FLAG TX COMPLETE SORIC 0x00 CLEAR RECEIVE INTERRUPT FLAG NOTHING REC SOBG 0x40 SET BAUDRATE TO 9600 BAUD SOCON 0x8011 SET SERIAL MODE display hello world printf Hello World n endless loop to stop program from running into void while 1 The program includes
44. torola MC9S12DP256B a similar 16 bit microcontroller of which the School has recently purchased a number of development boards The 9S12 will be programmed with the Metrowerks CodeWarrior integrated development environment It is hoped that this introduction will allow you to make a swift transition from the Keil environment to Metrowerks The reason why we developed this lab based on a different microcontroller is two fold Firstly the Keil simulation environment is very intuitive if you disagree wait until you ve seen others Secondly you are expected to develop conceptual skills which are independent of a particular hardware All microcontrollers are essentially the same It is recommended you spend some time playing with this environment trying to implement say a pulse width modulated signal PWM Consult the online help system and or have a look for sample programs on the Internet The earlier you get your head 30 Microcontroller Programming LMP1 Introduction to uController programming around the introduced concepts the more likely it is you will benefit from the remaining laboratory sessions Take this series of laboratories as a preview of what to expect in the work environment of a mechatronics engineer You will almost certainly have to be able to find relevant information from data sheets or some manufacturer s instruction manuals and you ll have to be able to learn about new techniques you may not yet be
45. two header files stdio h and reg167 h The former provides a function prototype for printf while the latter contains the definition of all special function registers SFR of the microcontroller Special function registers are used to control peripheral units such as the digital I O drivers the A D converter the serial interface SO etc When written to the individual bits of an SFR often configure the mode of operation of such a peripheral unit Reading the contents of an SFR can provide information about a unit s current state Example Setting bit 7 in the control register of the A D converter ADC triggers a conversion cycle Upon completion the ADC sets bit 6 of the control register and possibly triggers an interrupt Polling bit 6 of the ADC control register can thus be used to determine whether the A D conversion is still ongoing or has finished The main program begins by initialising the asynchronous serial communication interface SO Communication with the host is done using a simple null modem This type of connection consists of a three wire cable with lines for receive RXD transmit TXD and ground GND Figure LMP1 6 demonstrates this type of connection TXD TXD Figure LMP1 6 Null modem On the C167CR the serial interface is connected to port 3 pin 10 TXD and pin 11 RXD We therefore have to ensure that P3 10 is configured as output whereas P3 11 needs to be set up as input This is achieved by the follow
46. wing output should appear on the serial terminal window Converted value 1ff 17 Microcontroller Programming LMP1 Introduction to Controller programming EE la x Bie Edt view project Debug Peripherals Tools SVCS Window Help Psa SOB 2eSHseenn a S Qo oer i Analog Digital Converter Mode Single Ch Conversion v ADCON ox0000 een ooo 00010328 7EB7 BCLR SORIR ADCTC Tcpu 4 z ADDAT 0x01FF ADSTC The 8 ADDAT2 fox0000 0001032E 7EB6 BCLR SOTIR Tbc 8 y 00010330 F6F8B0FE MOV DPP3 0x3EB0 R8 I ADST f ADBSY J ADWR ADCH 00000 00010334 F048 MOV R4 R8 00010336 CB00 RET IRQ Channel Injection 5 void main void V ADCIR ADEIR I ADCRQ ADCIN 6 r Analog Input Channels as d int R lt 8 aa a aN 0 2 5000 1 j0 0000 200000 3 0 0000 Bis configure ADC for single conversion on channi 4 o o000 5 0 0000 6 foo000 7 oo000 10 ADCON 0 Il 8 0 0000 9 0 0000 10 0 0000 11 0 0000 7 start conversion 00010338 E6D00000 MOV ADCON 0x0000 12 oo000 13 fooo00 14 fooo00 15 fo oooo ADST 1 14 E interop system 15 wait for end of conver 00001033C 7FDO BSET ADST Mode EAEE wT 16 hile ADCIR 0 17 white tt Baudrate 2500000 SOR T3INT 008CH TANT 0090H TSINT 0094H TEINT 0098H CRINT 009CH 18 fateh reenlt From anna sosa ooo SOsTP I SOREN socon 00000 soe soreur 0
47. x0000 soens SORBUF 0 0000 M s0000 Error Detection Ir SOOEN I SOFEN M SOPEN D so0E f SOFE _ SOPE Cr soma soria M soem ETTER CCISINT COCCH E r Pots Selected Inter P310 I DP310 M DP311 r Req Ena WM 0 ewf A sea Currently used 1236 Bytes 15 F Sl SS ee Hsart G 8 SER ASEDIO Kren mmk Ap acrob Blackb C F keil ie myPr BicoAGFO BANGO 206m Figure LMP1 19 Confirming that flag SOTIR is cleared 5 A D conversions with interrupts In this section we are going to modify our ADC program to incorporate interrupts The objective is to understand how an interrupt can be triggered at the end of each conversion The converted value is to be displayed on a terminal connected to the serial interface Polling the ADCIR flag or the ADBSY flag for the end of conversion seems pretty wasteful as the controller is simply waiting for the ADC unit to complete its job In most situation this wasted time could and should be used to do other things such as perform a calculation service a network connection or control external units via the digital I O lines This is what interrupts are for Configured to trigger an end of conversion interrupt the ADC can run in the background while the main program gets on with other things Once a conversion is complete the interrupt controller takes over and switches context from t
48. xi cO seelocle nenreel Fala l Slqinhi sana e HOMPEo gt Be ABY BOE ert m Analog Digital Converter Mode Single Ch Conversion gt ADCON 0 0000 ADCTC Tcpu 4 ADDAT 0x0000 ADSTC Tbc 8 ADDAT2 fox0000 ADST ADBSY J ADWR ADCH 0 0000 IRQ Channel Injection ADcIR I ADEIR M ADCRO ADCIN Analog Input Channels o foao 1 foao 2fooo00 3 fo ooo0 4 fo o000 5 fooooo 6 fo oooo 7 0 0000 8 fo o000 3 fooooo 10 fo o000 11 0 0000 12 fooo00 13 fooo00 14 foo000 1s fooooo x Int Source Vector Reg Ena iv civ _ 0 0 T2INT 0088H O 0 T3INT O08CH 0 T4INT 0090H 0 0 0 0094H 0 O0ACH 0 SOEINT OOBOH 0 SSCTINT 0084H 0 SSCRINT 0088H 0 SSCEINT OOBCH 0 CCIGINT OOCOH 0 CCIVINT OOC4H O CCISINT OOCBH 0 0 ooooooooodbidoocooo ooooooocooopoMopooo ooooooocooodolopoocoo zifea Restricted Version with 8192 Byte Code Size Limit CC19INT OOCCH xl xx Currently used 1310 Bytes 15 Selected Interrupt Ir Req MEn vfs Gf aici ia aaa fi Rw Asan G E SER ASEDIO Prr Mmk Ppacod E sedd amp F xe imr OAGFODEMNGSO sem Figure LMP 1 20 End of conversion interrupts Set the value of channel 0 to 2 5 V and continue to step through the code Execution is briefly blocked at the line while myResult oldResult Notice that we ve initialised both of these variab
49. y project using myProject as filename Figure LMP1 3 bie Edit view project Debug Peripherals Tools SVCS Window Help Sitka eel ele TAE Create New Project 2 xi Save in E myHeloWord 7 e ck em myProject Uv2 Filename rryProject Uv2 Save as type Project Files uv2 z Cancel A x SS SSS 2 90 SIDER Agemso ne Bak Ppacr Eseo Eiweiss CAG FODEMNASO 0M Figure LMP1 3 Creating a new project You should be presented with a menu from which you can select the type of microcontroller for this project Choose Infineon then select the C767CR LM LM stands for less memory this is the ROM less version of the C167 A brief summary of the available on chip peripherals is displayed Figure LMP 1 4 Microcontroller Programming LMP1 Introduction to Controller programming laj x bie Eet view project Debug Peripherals Tools SVCS Window Help Jacsda seelocleesennel Jaelan a Taret 1 z Jx Select Device for Target Target 1 Description 16 Bit Microcontroller with 111 1 0 Lines Power Down Watchdog Clock Compare Unit 32 channels PWM Output 4 channels chip RAM 2KB 2KB XRAM C166 all Variants Ga C167CR 16FM C167CR 16RM C167CR 4RM Ga cie7cs4RM C167CS LM C167 LM a c167S 4RM C167SR LM C501 wo or l OC R Asaj O O SERNMAFEDIO Mess Gcipo Auna Roae Mmr Mu
Download Pdf Manuals
Related Search
Related Contents
Aiwa CX-NHMT75 CD Player User Manual Exhibitor's Manual and Service Order Forms Manual de Usuario - Bancos V.2 Bryant 580J*08--14D User's Manual HP Officejet 6000 User's Manual Dale Tiffany TT13092 Instructions / Assembly MANUAL de INSTRUCCIONES Weider WESY9625 User's Manual Copyright © All rights reserved.
Failed to retrieve file