Lab Six - Lab Six - PWM (Combined Timer, USART, and ADC)...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
Lab Six - PWM (Combined Timer, USART, and ADC) Wallace Kerrigan, 1541317 wallace.kerrigan@uconn.edu 24 October 2011
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Part One Figure 1. Flow Chart of Microcontroller Buzzer with Varied PWM Frequencies Code #define F_CPU 4000000UL #include <avr/io.h> #include <avr/interrupt.h> #include <util/delay.h> #include <stdbool.h> // Boolean header bool btn0_hit = false; // Define each button as "false" to be used later. bool btn1_hit = false; bool btn2_hit = false; bool btn3_hit = false; void InitPWM(void) // Initiate the PWM
Background image of page 2
{ TCCR1A = 0b01000011; // Control Register A for Fast PWM TCCR1B = 0b00011001; // Control Register B for Fast PWM OCR1A = 0x00; // Set the output register for the buzzer } int main( void ) { InitPWM(); // Initiate the PWM DDRB = 0xF0; // Enable the buttons as the input PORTB = 0xF0; // Enable the pullup resistors. DDRD = 0x20; // Enable pin number 5 on PORTD as the output, which is the buzzer OCR1A = 0x00; uint8_t buttonValue = 0; for(;;) { buttonValue = ~(PINB & 0x0F); // Define "buttonValue" for when a switch is pressed // Each "if" statement will check if each button is "false" = not pressed. If so, buzzer will make a noise at set frequency. // It will then set it to "true" = pressed. // Another "if" statement that will check if it's "true", then set OCR1A to 0 and turn the buzzer off. // then an "else" statement to do nothing. if(btn0_hit == false && btn1_hit == false && btn2_hit == false && btn3_hit == false) { OCR1A = 125; btn0_hit = true; } else if(btn0_hit == true) { OCR1A = 0; btn0_hit = false; } else { // Do nothing } } if(btn0_hit == false && btn1_hit == false && btn2_hit == false && btn3_hit == false) { OCR1A = 250; btn1_hit = true; } else if(btn1_hit == true) {
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
OCR1A = 0; btn1_hit = false; } else { // Do nothing } } if(btn0_hit == false && btn1_hit == false && btn2_hit == false && btn3_hit == false) { OCR1A = 500; btn2_hit = true; } else if(btn2_hit == true) { OCR1A = 0; btn2_hit = false; } else { // Do nothing } } if(btn0_hit == false && btn1_hit == false && btn2_hit == false && btn3_hit == false) { OCR1A = 1000; btn3_hit = true; } else if(btn3_hit == true) { OCR1A = 0; btn3_hit = false; } else { // Do nothing } } } } Explanation Four buttons were defined as Boolean expressions to be used later in the program. The PWM was set up next. The Timer1 Control Registers A and B were used to set up the PWM. The values of these registers were determined by the data sheet. The binary value “0b01000011” was used to initialize the fast PWM and toggle the OCnA and OCRnA. The same technique was used for the Control Register B. OCR1A was also implemented to set the output register for the buzzer. The main function was initialized next. DDRB and PORTB were set both as 0xF0 to
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/21/2011 for the course ECE 3411 taught by Professor Park during the Fall '11 term at UConn.

Page1 / 12

Lab Six - Lab Six - PWM (Combined Timer, USART, and ADC)...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online