Home

ECED3204 – Lab #4

image

Contents

1. int main void LED as output DDRD 1 lt lt 7 Pull up on ICP PORTD 1 lt lt 6 ECED 3204 Microprocessors Lab 4 Timers Defaults TCCRIA 0 Enable noise cancel look for falling edge TCCRIB Clock divided by 1024 TCCRIB Input capture interrupt Enable TIMSK1 Enable interrupts sei uintl6_t timediff while 1 if current_edge 2 Check for normal no wrap around if starting_cnt lt ending_cnt A B timediff ending_cnt starting_cnt else wrap around Oxffff A B done without requiring signed math timediff starting_cnt ending_cnt timediff Oxffff timediff if timediff gt 10000 amp amp timediff lt 30000 LED_ON _delay_ms 5000 LED_OFF current_edge 0 ISR TIMER1_CAPT_vect if current_edge 0 Save timestamp starting cnt Switch to rising edge TCCRIB current_edge 1 else if current_edge 1 Save timestamp ending_cnt ICR1 Switch to falling edge TCCRIB amp current_edge 2 10 ECED 3204 Microprocessors Lab 4 Timers TIFR1 1 lt lt ICF1 3 Using the register settings for TIMER1 from the ATMega644A datasheet fill in the blanks above The objective of this code is to measure the length of time the push button signal is low as when the button is pressed t
2. a button is pressed using the input capture Required Materials e Setup from Part 1 e Push Button wired into PORTD 6 should have been done in Part 1 Background The timer system in the ATMega644P can be used for a variety of purposes This section will use the Input Capture functionality which is used to measure the length of a pulse You can use this for many features such as 1 Measuring the width of a pulse 2 Measuring the duty cycle of a signal 3 Measuring the frequency of a signal We will measure the length of time a push button is pressed If the button is pressed for a length of time inside our allowed range an LED will light up If you press the button for too short or too long of a time the LED will not light up NOTE This lab has a video overview at http www youtube com watch v OEyghfrqMgo amp hd 1 It may be useful to see the system operating to understand the push button operation Procedure 1 Ensure you have the same setup as previous parts including the button connect to PORTD 6 2 Start a new project or re use the project from the previous parts and load the following template include lt avr io h gt include lt avr interrupt h gt include lt stdint h gt include lt util delay h gt define LED_ON PORTD 1 lt lt 7 define LED_OFF PORTD amp 1 lt lt 7 volatile unsigned char current_edge 0 volatile uint16_t starting_cnt volatile uintl6_t ending_cnt
3. 00 2 14745600 256 57600 times per second That is too fast Finding Table 13 9 you can see some prescaler settings Table 13 9 Clock Select Bit Description Continued clk o 64 From prescaler clk o 256 From prescaler Using a prescaler of 64 would mean the timer now overflows 900 times per second This tells us the setting for two bits in the TCCROB register as these CS00 CS01 CS02 bits are present in this register ECED 3204 Microprocessors Lab 4 Timers 7 6 5 3 2 1 0 FOC FOC TT woes tS0_ C01 cs00_ Tocron We will use the default all 0 settings for TCCROA 7 6 5 3 2 1 0 coment comoa como comuso wawor waMoo J TCCROA See the datasheet for a description of those bits This means our setup area of the code looks like this Timer configuration TCCROA 0 TCCROB 1 lt lt CSO0O 1 lt lt CS01 4 Next you need to fill in the SPECIFY NAME_HERE section of the code which is the interrupt vector This routine will get called every time the interrupt occurs You can find a list of all the names at http www nongnu org avr libc user manual group _avr__interrupts html you must be VERY CAREFUL to ensure you use a name that exists on the ATmega644P device In this case look for the TIMERO_OVF_vect name as shown in the following aavanaNY_U vrv vue Mev vraw nv wv rmn CuuY vyu w Rt reves i aa eves AAA VILITI A Uy iy maa ay Lw AT90S12
4. 00 AT90S2333 AT90S4414 AT90S4433 AT90S4434 AT90S8515 AT90S8535 AT9OPWM216 AT9OPWM2B AT90PWM316 AT90PWM3B AT9OPWM3 AT9OPWM2 AT9OPWM1 AT90CAN128 AT90CAN32 AT9OCAN64 ATmegal03 ATmegal28 ATmegal284P ATmegal6 ATmegal61 ATmegal62 ATmegal63 ATmegal65 ATmega165P ATmegal68P ATmegal69 ATmegal69P ATmega32 ATmega323 ATmega325 ATmega3250 ATmega3250P ATmega328P ATmega329 TIMERO_OVF_vect ISIG_OVERFLOWO0 Timer Counter0 Overflow ATmega3290 ATmega3290P ATmega32HVB ATmega48P ATmega64 ATmega645 ATmega6450 ATmega649 ATmega6490 ATmega8 ATmega8515 ATmega8535 ATmega88P ATmegal68 ATmega48 ATmega88 ATmega640 ATmegal280 ATmegal281 ATmega2560 ATmega2561 ATmega324P ATmegal64P ATmega644 ATmegal6HVA ATtiny11 ATtiny12 ATtiny15 ATtiny2313 ATtiny28 ATtiny48 ATtiny261 ATtiny461 ATtiny861 AT90USB162 AT90USB82 AT90USB1287 AT90USB1286 AT90USB647 AT90USB646 5 Finally you need to enable the overflow interrupt which is done by setting bit 1 of the TIMSKO register T 6 5 3 2 1 0 TO EO OCIEDA T TOIEO J TIMSKO R R R R R R W R W RW 0 0 0 0 0 0 0 0 6 At this point your code should look similar to this int main void DDRD 1 lt lt 7 Timer configuration TCCROA 0 TCCROB 1 lt lt S00 I lt lt CS01 gt Interrupt mask enables ECED 3204 Microprocessors Lab 4 Timers TIMSKO 1 lt lt TOIEO Enable global i
5. ECED 3204 Microprocessors Lab 4 Timers ECED3204 Lab 4 STUDENT NAME s STUDENT NUMBER s BOO Pre Lab Information It is recommended that you read this entire lab ahead of time Doing so will save you considerable time during the lab as you will be required to write some simple C code during this lab Overall Objective This lab has several main objectives e Learn about using timers for event timing e Learn about Pulse Width Modulation PWM e Learn about Input Capture mode to measure pulse width NOTE This lab has a video overview at http www youtube com watch v OEyqhfrqMgo amp hd 1 ECED 3204 Microprocessors Lab 4 Timers Part 1 Timing Events Objective e Familiarize yourself with using a timer and interrupt Required Materials e Microprocessor Module with Programmer e Breadboard e USB Cable e Power Supply e Computer with Atmel Studio 6 2 and Programmer Utility installed e Push Button Background The timer system in the ATMega644P can be used for a variety of purposes In this first part of the lab we will use the overflow interrupt of the timer The timer system has an 8 bit or 16 bit register depending on the timer being used which is incremented on a rate you can choose See Chapter 11 of the course textbook for more information TOVn Int Req DATA BUS Edge Detector From Prescaler Control Logic Procedure 1 Build a similar circuit from Part 1 o
6. f Lab 3 except connect the switch to PORTD 6 For reference the schematic is shown below we will only use the LED in Part 1 and Part 2 but will use the switch in Part 3 ECED 3204 Microprocessors Lab 4 Timers 2 T m ur J UJI if U gogog A Ulm IODO PAPN W J hO D Le J e jt o0 T an 71D Y D i I D ALHOUSIE MICRO BOARD Which might look as follows sree eer secen es2e9e0 2 Start a new C C project see Lab 1 for details copy the following template into it include lt avr io h gt ECED 3204 Microprocessors Lab 4 Timers 3 include lt avr interrupt h gt volatile unsigned int tick define LED _ON PORTD 1 lt lt 7 define LED_OFF PORTD amp 1 lt lt 7 int main woud DDRD 1 lt lt 7 Timer configuration TCCROA TCCROB Interrupt mask enables TIMSKO Enable global interrupts sei while 1 ISR SPECIFY_NAME_HERE Code here r Before being able to compile this code we need to finish a few things The first is to determine the setup of TCCROA and TCCROB We want the timer to overflow somewhere between 100 5000 times per second The clock source for the AVR is a 14745600Hz crystal we learned this in Lab 1 If you used this to directly drive an 8 bit counter the counter would overflow at the rate of 147456
7. he input capture line goes low 11 ECED 3204 Microprocessors Lab 4 Timers Lab Questions 1 In Part 1 we uses an interrupt which is called periodically What is the fastest number of times we could call that interrupt using the timer overflow interrupt i e using the lowest possible clock prescaller giving you the fastest clock operation Would you foresee any issues calling the interrupt that quickly 2 What was the frequency of the PWM signal 3 How would you use the input capture feature to measure the distance to an object if you have a Time of Flight sensor which generates a pulse indicating how long it took a sound echo to reflect off an object 4 In Part 3 the button must be pressed for between 10 000 and 30 000 counts of the input capture register Based on the Timer1 prescaler settings and the system operating frequency what duration does that correspond to 12
8. ld configure timer counter 2 as the following o PWM Phase Correct WGM2 0 WGM1 1 WGMO 0 o Clock divider OClock 8 o OC2A pin operating in non inverting mode The following shows the basic operating instructions include lt avr io h gt int main void DDRD 1 lt lt 7 Set clock divider to be 8 TCCR2B Set waveform generation mode TCCR2A Set output on OC2A pin TCCR2A Set PWM to half way 50 duty cycle OCR2A 127 ECED 3204 Microprocessors Lab 4 Timers while 1l 3 The LED should be partially illuminated now You can also check with an oscilloscope you are getting an appropriate signal 4 Set the OCR2A register to various values in the range 0 255 and observe the effect on the output signal Use an oscilloscope to measure how the duty cycle changes and measure the frequency as well of the PWM output 5 Add some code to slowly increase the value and see what happens For example here is a complete code listing include lt avr io h gt include lt util delay h gt int main void DDRD 1 lt lt 7 Set clock divider to be 8 TCCR2B 1 lt lt CS21 Set waveform generation mode TCCR2A 1 lt lt WGM20 Set output on OC2A pin TCCR2A 1 lt lt COM2A1 OCR2A 0 while 1 OCR2A _delay_ms 50 ECED 3204 Microprocessors Lab 4 Timers Part 3 Input Capture Objective e Measure the time
9. nterrupts sei while 1 ISR TIMERO_OVF_vect Code here r 7 Finally program the interrupt service routine ISR to toggle the LED An example implementation is as follows using the tick variable to keep track of how many runs through the interrupt routine have occurred ISR TIMERO_OVF_vect tick LE Eek 1 LED_ON else if tick 400 LED_OFF else if tick gt 900 tick 0 8 Adjust the delay to blink 5 seconds on 5 seconds off ECED 3204 Microprocessors Lab 4 Timers Part 2 PWM Output Objective e Generate a PWM signal with a variable duty cycle Required Materials e Setup from Part 1 Background The timer system in the ATMega644P can be used for a variety of purposes In this first part of the lab we will use the overflow interrupt of the timer The timer system has an 8 bit or 16 bit register depending on the timer being used which is incremented on a rate you can choose See Chapter 11 of the course textbook for more information Procedure 1 Build the same circuit from Part 1 of this lab or if you just completed Part 1 leave your circuit as is 2 Using Timer 2 generate a Pulse Width Modulation PWM signal of around 2 10 kHz Output this on in PD7 which is the OC2A pin You will need the following references in the datasheet o Section 15 11 1 TCCR2A Register o Section 15 11 2 TCCR2B Register You shou

Download Pdf Manuals

image

Related Search

Related Contents

User Instruction & Installation Manual M9 & M10 Searchlight  KWC COMO S.10.R4.02 User's Manual  Model ACC34AC Signal Conditioner/Converter (AC)  HP F4135 Setup Guide  User Manual 2008 - Mobile Electronics  Grandstream Networks HandyTone 701 ATA  Deutschsprachige Anleitung als PDF - Uni  Le savoir-être ! Un référentiel professionnel d`excellence  Samsung SGH-E530 Bruksanvisning    

Copyright © All rights reserved.
Failed to retrieve file