Microprocessor Fundamentals Topic 13 Square Waves, Frequencies, & Duty Cycles Objectives • Gain a better understanding of the ATmega128 and assembly language programming • Developing output waveforms (square waves) – Specific frequency – Specific duty cycle – Multiple square waves 1/4/2010 2 Task • Suppose: – We want to output a square wave • With a specific frequency • With a specific duty cycle – What would we have to do? – How would you write the program? 1/4/2010 3 Task • Suppose: – We want to output a square wave • With a specific frequency 10 kHz • With a specific duty cycle 50% duty cycle – What would we have to do? – How would you write the program? We’ll consider applications soon 1/4/2010 4 Square Wave tH T f = tL 1 T tH tH DutyCycle = * 100% = * 100% tH + tL T 1/4/2010 5 Square Wave tH T tL 1 1 T= = = 100µs f 10k tH = 1/4/2010 DutyCycle* T 100% 50% * 100µs = = 50µs 100% 6 The Plan 1. Initialize ports 2. Set the output bit 1. Delay for 50µs 3. Clear the output bit 1. Delay for 50µs 4. Go back to step 2 1/4/2010 7 Square Wave tH T tL 1 1 T= = = 100µs f 10k tH = DutyCycle* T 100% 50% * 100µs = = 50µs 100% Exercise: What would be the time high and time low if you needed a 5kHz, 25% duty cycle square wave? 1/4/2010 8 Square Wave tH T tL 1 1 T= = = 200µs f 5k tH = DutyCycle* T 100% 25% * 200µs = = 50µs 100% t L = T − t H = 150µs 1/4/2010 9 Task • Suppose: – We want to output 2 square waves simultaneously • Each with a specific frequency • Each with a specific duty cycle – What would we have to do? – How would you write the program? 1/4/2010 10 Square Wave f = 1kHz f = 2kHz This is what we need. How would you approach it? 1/4/2010 11 Square Wave f = 1kHz f = 2kHz 250µs 250µs 250µs 250µs I would divide the waveforms into segments at transitions in the waveforms. In this example it works out very well. 1/4/2010 12 Square Wave f = 1kHz f = 2kHz Waveform 1 on bit0 Waveform 2 on bit1 250µs 250µs 250µs 250µs 1. Output 1 on b0 and 1 on b1 for 250 µs 2. Output 1 on b0 and 0 on b1 for 250 µs 3. Output 0 on b0 and 1 on b1 for 250 µs 4. Output 0 on b0 and 0 on b1 for 250 µs 5. Go back to step 1 1/4/2010 13 Task • Suppose: – We want to output 2 square waves simultaneously • Each with the same frequency • Each with the same duty cycle • 90° out of phase – What would we have to do? – How would you write the program? 1/4/2010 14 Square Wave W1 W2 This is what we need. How would you approach it? 1/4/2010 15 Square Wave W1 W2 50µs 50µs 50µs 50µs I would take the same approach as before and divide the waveforms into segments at the transitions. If both waveforms were 5kHz then we would need 50μs 1/4/2010 16 Task • Suppose: – We want to output 3 square waves simultaneously • Each with different frequencies • Each with different duty cycles • All out of phase with the others – What would we have to do? – How would you write the program? 1/4/2010 17 Task • Suppose: – We want to output 3 square waves simultaneously • Each with different frequencies • Each with different duty cycles • All out of phase with the others – What would we have to do? – How would you write the program? Now it’s getting a little complicated! At this point we may want to consider using multiple timers and interrupts. 1/4/2010 18 Summary • In this topic we: – Gained a better understanding of the ATmega128 and assembly language programming – Developed output waveforms (square waves) • Specific frequency • Specific duty cycle • Multiple square waves 1/4/2010 19