Presentation is loading. Please wait.

Presentation is loading. Please wait.

Atmega328p Introduction for Digital and PWM Output Brion L Fuller II Robotics Club.

Similar presentations


Presentation on theme: "Atmega328p Introduction for Digital and PWM Output Brion L Fuller II Robotics Club."— Presentation transcript:

1 Atmega328p Introduction for Digital and PWM Output Brion L Fuller II Robotics Club

2 ARDUINO NANO

3 This is how to connect an LED to a pin on the micro controller. This one we used D6 according to the silkscreen. Which is PortD Pin 6 when programming.

4 The led can be wired in two different ways. Use the Pin as groundUse the Pin as 5v

5 Start up AVR Studios Create a new project (C++) 00001 // this is the header file that tells the compiler what pins and ports, etc. 00002 // are available on this chip. 00003 #include 00004 00005 // define what pins the LEDs are connected to. 00006 // in reality, PD6 is really just '6' 00007 #define LED PD6 00008 00009 // Some macros that make the code more readable 00010 #define output_low(port,pin) port &= ~(1<<pin) 00011 #define output_high(port,pin) port |= (1<<pin) 00012 #define set_input(portdir,pin) portdir &= ~(1<<pin) 00013 #define set_output(portdir,pin) portdir |= (1<<pin) 00014 00015 // this is just a program that 'kills time' in a calibrated method 00016 void delay_ms(uint8_t ms) { 00017 uint16_t delay_count = F_CPU / 17500; 00018 volatile uint16_t i; 00019 00020 while (ms != 0) { 00021 for (i=0; i != delay_count; i++); 00022 ms--; 00023 } 00024 } 00025 00026 int main(void) { 00027 // initialize the direction of PORTD #6 to be an output 00028 set_output(DDRD, LED);set_output(DDRD, LED); 00029 00030 while (1) { 00031 // turn on the LED for 200ms 00032 output_high(PORTD, LED);output_high(PORTD, LED); 00033 delay_ms(200);delay_ms(200); 00034 // now turn off the LED for another 200ms 00035 output_low(PORTD, LED);output_low(PORTD, LED); 00036 delay_ms(200);delay_ms(200); 00037 // now start over 00038 } 00039 }

6 Setup This part of the code is called a header file that has instructions on how to convert parts of codes to the microcontroller we are using. The file is usually found in C:\WinAVR\avr\include\avr. 00001 // this is the header file that tells the compiler what pins and ports, etc. 00002 // are available on this chip. 00003 #include Definitions help translate code to make programs easier to read and troubleshot for people. 00005 // define what pin the LED is connected to. 00006 // in reality, PD6 is really just '6' 00007 #define LED PD6 00009 // Some macros that make the code more readable 00010 #define output_low(port,pin) port &= ~(1<<pin) 00011 #define output_high(port,pin) port |= (1<<pin) Port &= ~(1<<pin) sets the pin to ground or zero Port |= (1<<pin) sets the pin to high or one 00012 #define set_input(portdir,pin) portdir &= ~(1<<pin) 00013 #define set_output(portdir,pin) portdir |= (1<<pin) This part is important. The microcontroller needs to know what pins are going to be receiving information and what pins will be giving information.

7 Important Delay 00015 // this is just a program that 'kills time' in a calibrated method 00016 void delay_ms(uint16_t ms) { 00017 uint16_t delay_count = F_CPU / 17500; 00018 volatile uint16_t i; 00019 00020 while (ms != 0) { 00021 for (i=0; i != delay_count; i++); 00022 ms--; 00023 } 00024 } Our processor runs at 16Mhz which can be found on a datasheet. That is 16,000,000 (1/s) which is 62.5ns. This is a very important number to remember for any processor because this defines the time it takes for a line of code to finish. This defined function makes delays easier by giving a format of delay(N ms) based on the CPU frequency.

8 Important Delay 00015 // this is just a program that 'kills time' in a calibrated method 00016 void delay_ms(uint8_t ms) { 00017 uint16_t delay_count = F_CPU / 17500; 00018 volatile uint16_t i; 00019 00020 while (ms != 0) { 00021 for (i=0; i != delay_count; i++); 00022 ms--; 00023 } 00024 } Our processor runs at 16Mhz which can be found on a datasheet. That is 16,000,000 (1/s) which is 62.5ns. This is a very important number to remember for any processor because this defines the time it takes for a line of code to finish. This defined function makes delays easier by giving a format of delay(N ms) based on the CPU frequency. (Only up to 255ms) 00015 // this delay is for seconds 00016 void delay_s(uint8_t s) { 00017 uint16_t delay_count = F_CPU / 17500; 00018 volatile uint16_t i; 00019 00020 00021 for (s; s != 0; s--){ 00022 delay_ms(250); 00023 delay_ms(250); 00024 delay_ms(250); 00025 delay_ms(250); 00026 } 00027 }

9 Main Program 00028 //This is the start of the program 00029 int main(void){ This lets the processor know when to start executing a program. 00030 //initialize the direction of PortD #6 to output 00031 set_output(DDRD, LED); This is the setup phase for the program. Most of the time global variables and default direction lines should be place near to the main function. 00030 //repeat program forever because 1 is always true 00031 while (1) { 00032 // turn on the LED for 200ms 00033 output_high(PORTD, LED);output_high(PORTD, LED); 00034 delay_ms(200);delay_ms(200); 00035 // now turn off the LED for another 200ms 00036 output_low(PORTD, LED);output_low(PORTD, LED); 00037 delay_ms(200);delay_ms(200); 00038 // now start over 00039 } This is our program finally. This is a simple blink LED for 200ms at a time.

10 Exercises 1.Make the LED blink twice as fast 2.Make the LED blink twice as slow 3.Change the line that sets the pin direction so that its an input, what happens? (Turn off the light and observe. This effect will be explained later...) 4.Make the LED blink REALLY fast (like, only 5ms delay) What happens? Turn off the light and wave the board around. 5.Make the LED blink out morse code for SOS (3 short blinks, a pause, 3 long blinks, a pause, 3 short blinks, a long pause...)

11 But what about dimming the LED? Is it too bright not bright enough? I don’t want to replace the resistor everytime I would like to change the intensity. Well that’s where PWM comes in. GREAT NOW THE LED IS ON OR OFF

12 Back to the pin mapping. We have modified some software to PWM in the last exercise but in all honestly that is a very processor hungry activity and what if you want to do other things while PWM. That is where some of these random functions in the ( ) are put to use. These all have special function that are connected to the pins and can perform specialized tasks to free up processor time. For PWM, we will look at the pins that have (OC_ _) this tells us that there are build in hardware timers at these pins.

13

14 #define pwm6(pwm) OCR0A = pwm Here is the setup that goes near the top of the main function 00027 // Motor Initialization routine -- this function must be called 00028 // before you use any of the above functions 00029 { 00030 //Configure for inverted PWM output on motor and clear on 255 00031 TCCR0A = b11110011; 00032 TCCR0B = b00000010; 00033 00034 //set the pwm to 0% 00035 pwm6(0); 00036 } Now we can change the simple output to the LED from on or off to dimming 00037 while (1) { 00038 // turn on the LED for at 50% duty cycle 00039 pwm6(127); 00040 delay_ms(200);delay_ms(200); 00041 // now turn off the LED to 100% 00042 pwm6(255); 00043 delay_ms(200);delay_ms(200); 00044 //now turn off the LED 00045 pwm6(0); 00046 delay_ms(200);delay_ms(200); 00045 // now start over 00045 } 00046 }

15 00001 // this is the header file that tells the compiler what pins and ports, etc. 00002 // are available on this chip. 00003 #include 00004 00005 // define what pins the LEDs are connected to. 00006 // in reality, PD6 is really just '6' 00007 #define LED 6 00008 00009 // Some macros that make the code more readable 00010 #define output_low(port,pin) port &= ~(1<<pin) 00011 #define output_high(port,pin) port |= (1<<pin) 00012 #define set_input(portdir,pin) portdir &= ~(1<<pin) 00013 #define set_output(portdir,pin) portdir |= (1<<pin) 00014 #define pwm6(pwm) OCR0A = pwm 00015 // this is just a program that 'kills time' in a calibrated method 00016 void delay_ms(uint8_t ms) { 00017 uint16_t delay_count = F_CPU / 17500; 00018 volatile uint16_t i; 00019 00020 while (ms != 0) { 00021 for (i=0; i != delay_count; i++); 00022 ms--; 00023 } 00024 } 00025 00026 // this delay is for seconds 00027 void delay_s(uint8_t s) { 00028 uint16_t delay_count = F_CPU / 17500; 00029 volatile uint16_t i; 00030 00031 00032 for (s; s != 0; s--){ 00033 delay_ms(250); 00034 delay_ms(250); 00035 delay_ms(250); 00036 delay_ms(250); 00037 } 00038 } 00026 int main(void) { 00027 // initialize the direction of PORTD #6 to be an output 00028 set_output(DDRD, LED);set_output(DDRD, LED); 00029 00030 //Configure for inverted PWM output on motor and clear on 255 00031 TCCR0A = b11110011; 00032 TCCR0B = b00000010; 00033 00034 //set the pwm to 0% 00035 pwm6(0); 00036 00037 while (1) { 00038 // turn on the LED for at 50% duty cycle 00039 pwm6(127); 00040 delay_ms(200);delay_ms(200); 00041 // now turn off the LED to 100% 00042 pwm6(255); 00043 delay_ms(200);delay_ms(200); 00044 //now turn off the LED 00045 pwm6(0); 00046 delay_ms(200);delay_ms(200); 00045 // now start over 00045 } 00046 }

16 Exercises 1.Make the LED brighten and dim 2.Change the Pin that the LED is on 3.Replace the LED with a motor


Download ppt "Atmega328p Introduction for Digital and PWM Output Brion L Fuller II Robotics Club."

Similar presentations


Ads by Google