Mechatronic Systems ME 430
Popular in Course
Popular in Mechanical Engineering
This 5 page Class Notes was uploaded by Halie Berge on Monday October 19, 2015. The Class Notes belongs to ME 430 at Rose-Hulman Institute of Technology taught by David Fisher in Fall. Since its upload, it has received 10 views. For similar materials see /class/225094/me-430-rose-hulman-institute-of-technology in Mechanical Engineering at Rose-Hulman Institute of Technology.
Reviews for Mechatronic Systems
Report this Material
What is Karma?
Karma is the currency of StudySoup.
You can buy or earn more Karma at anytime and redeem it for class notes, study guides, flashcards, and more!
Date Created: 10/19/15
ME430 Mechatronic Systems Using the Timer Peripheral Timers are a PIC peripheral function that can run in the background without interfering with the rest of the program Other peripherals we ll use include the PWM peripheral and ADC peripheral On the 18F4520 there are 4 timers that you can use which run via hardware in the background These timers are numbered TimerO Timerl Timer2 Timer3 Timers 0 1 and 3 are 16 bit timers that store a int value that counts from 0 to 65535 You can setup the timer using the timerh library functions In this example code I setup the timer 0 zero Setup the timer with a 14 prescaler with 16 bits resolution Therefore the timerO freq is clock frequency 4 4 OpenTimer0 TIMERilNTioFF amp T0716BIT amp ToisOURCEilNT amp T033474 There is only one line of code there and the rest are comments note for big commands you can go to the next line without any problems In much the same way that the instruction cycle frequency runs slower than the clock frequency the timer frequency can run slower than the instruction cycle frequency The instruction cycle is always 14th of the clock frequency but the timer can be setup to different values Table 1 Timer Prescaler Options Library con g Statement Divides the instruction cycle frequency by how much ToiPsilil 11 prescale T07P87172 12 prescale ToiPsilitl 14 prescale T07P87178 18 prescale T07P871716 116 prescale T07P871732 132 prescale T07P571764 164 prescale ToiPsililZS 1128 prescale T07P5717256 1256 prescale So for the example code earlier we used T035374 So that made Timer 0 run at 14th the speed of the instruction cycle The timer frequency slow down can be set to many di erent values This can be handy for keeping the instruction cycle frequency very fast but still timing things that are slow a subtle but key difference The library functions do an excellent job of making the timers very easy to use Go look at the C18 library section 29 If you look at the example code below you can see some of the commands that might get used with a timer This is not a complete program but it shows you some of the timer commands 292 Example of Use include ltp18F4520hgt include lttimershgt void main void T int result ADCONl OXOF A configure timerO OpenTimer0 TIMERilNTiOFF amp ToisOURCEilNT amp T07P571732 while 1 l whilePORTAbitsRA4 Wait for RA4 to get pushed result ReadTimer0 read timer could send the result to the LCD WriteTimer0 0 restart timer l CloseTimer0 Close modules For an example with a complete program you can download Timers Democ off the course website That code is also shown on the next few pages FileName timer democ Processor PIC18F4520 Compiler MPLAB C18 v306 gtxlt This le displays the timer 0 on the PICDEM 2 LCD screen gtxlt As a bonus it increments the PORTB LEDs using an interrupt when the timer over ows gtxlt Author Date Comment WWWWWWWWWWNWWM David Fisher10307 Processor Header Files include ltp18f4520hgt include quotPICDEM 2 LCD modulehquot include ltstdiohgt include lttimershgt De ne Constants Here de ne delayTime 100 Local Function Prototypes void lowiisrvoid void highiisrvoid Configuration Bits For details on PIC18F con guration bit settings see PIC18 Con guration Settings in MPLABIDE Help pragma con g OSC EC EXtemal 4MHz crystal necessary for LCD pragma con g WDT OFF pragma con g LVP OFF pragma con g BOREN OFF gtxltgtxlt Declare Interrupt Vector sections gtk pragma code highivector0X08 void interruptiatihighivectorvoid iasm goto highiisr iendasm pragma code lowivector0Xl 8 void interruptiatilowivectorvoid iasm goto lowiisr iendasm Global Variables char tempString5 char linel quotTimer 0 readingquot unsigned int result Function void mainvoid pragma code void main void ADCONl OXOF TRISB OXFO PORTB 0X00 LCDInit DisplayClr LCDLine l textidisplayline l A con gure timer0 OpenTimer0 TIMERilNTioN amp T0716BIT amp T07SOURCE71NT amp T07PS717128 Clock Frequency 4 MHZ Instruction cycle Frequency l MHZ Timer 0 Frequency l MHZ 128 78125 HZ Timer 0 Period 128 uSeconds Period in microseconds always equals prescaler when clock is 4 MHZ Over ow every 128 uSecond 2Al6 ticks 84 seconds This code is only necessary for the interrupt only INTCON2bitsTMROIP 0 Make Timer Interrupt High Priority INTCONbitsTMROIE l Enable the Timer zero interrupt INTCONbitsGIEH l Enable High priority interrupt while 1 result ReadTimer0 read timer sprintftempStringquot u ticksquotresult LCDLine2 textidisplaytempString CloseTimer0 close modules Function void highiisrvoid Input Occurs on a timer over ow Output Modify the PORTB LEDs Overview The interrupt counts over ows using LEDs pragma interrupt highiisr declare function as high priority isr void highiisrvoid INTCONbitsTMROIF 0 PORTB Function void lowiisrvoid Input Output Overview pragma interruptlow lowiisr declare function as low priority isr void lowiisrvoid
Are you sure you want to buy this material for
You're already Subscribed!
Looks like you've already subscribed to StudySoup, you won't need to purchase another subscription to get this material. To access this material simply click 'View Full Document'