Today i show you how to make LED Blink on AVR microcontroller so first you have to need know about AVR Microcontroller First Program | Turn On an LED
I hope you know already on previous tutorial what the <avr/io.h> does, so I am looking froward,and won't waste your time with that one, but today there is a new #include statement. That is delay.h will provide us a some of convenient methods for us to use. the delay.h will provide us with a way to create delays in our program.
We need a way to ONLY affect one bit, the pin 0 bit. so you apply "OR", logic
01001011 |
10001101
equals
11001111
DDRB = DDRB | 0b00000001;
This will takes it's former self and "OR" it to a mask.
Explain 1 << PINB0 is the act of creating the mask. The "1" represents what will be inserted into the mask, the << is a left shift operator. and PINB0 is a number of positions that the "1" will shift left. In essence, PINB0 is just equal to 0. So, you start with a 0b00000000, and you add a "1" to make 0b00000001 and then you shift it left 0 positions. So you are left with 0b00000001, the same number from above.
PORTB |= 1 << PINB0;
_delay_ms(100);
PORTB &= ~(1 << PINB0);
_delay_ms(100);
#include <util/delay.h>
int main(void)
{
DDRB |= 1 << PINB0;
while (1)
{
PORTB ^= 1 << PINB0;
_delay_ms(100);
}
}
Proteus simulation view
Proteus simulation video
I hope you know already on previous tutorial what the <avr/io.h> does, so I am looking froward,and won't waste your time with that one, but today there is a new #include statement. That is delay.h will provide us a some of convenient methods for us to use. the delay.h will provide us with a way to create delays in our program.
#include <util/delay.h>
This is what we had before. This is not a very good way of minipulating the pins because we just changed all of the pins from 1 to 7 as input, but "what if" we had a larger program that used those pins for other things, like for instance, pin 2 all pies brake pressure control for the anti-lock braking system.
DDRB = 0b00000001;
"OR", logic
so you have to know or logic one the binary number for the example 01001011 |
10001101
equals
11001111
so you can write on
you can write on the C++ this way
DDRB |= 0b00000001;
This method is vary complicate so you can write this way on C++ code
DDRB |= 1 << PINB0
if it was PINB4? The statement would be: 1 << PINB4. The "1" would be left shifted 4 times resulting in: 0b00010000.
_delay_ms(100);
PORTB &= ~(1 << PINB0);
_delay_ms(100);
PORTB &= ~(1 << PINB0);
here we apply AND logic
What is and logics
Example AND binary number operation (the "&" is used in C programming for AND):
01001011 &
10001101
equals
00001001
10001101
equals
00001001
So complete code is
#include <avr/io.h>
#include <util/delay.h>
int main(void)
{
DDRB |= 1 << PINB0;
while (1)
{
PORTB = 1 << PINB0;
_delay_ms(500);
PORTB = 0 << PINB0;
_delay_ms(500);
}
}
One this program you cane write use XOR logic
#include <avr/io.h>#include <util/delay.h>
int main(void)
{
DDRB |= 1 << PINB0;
while (1)
{
PORTB ^= 1 << PINB0;
_delay_ms(100);
}
}
No comments