//LED Interfacing Program for different pattern generation
//Pattern Example 1 BCD counting on the port
#include <avr/io.h>
int main(void)
{
DDRB=0xff;
unsigned char i;
//while(1)
{
for (i=1;i<=255;i++)
{
PORTB=i;
}
}
1. We can use while(1) or not it is optional in this program.
2. If while is not used still the after the 255 or FF value it is automatically switch to zero and start counting again.
//LED Interfacing Program for different pattern generation
//Pattern Example 2. Toggle the all LED which is connected to port B
#include <avr/io.h>
int main(void)
{
DDRB=0xff;
while(1)
{
PORTB=0x55;
PORTB=~PORTB;
}
}
This program is run only on the simulator because microcontroller switching speed is very fast. So to see the effect at the port of microcontroller used LED and operate it with the delay.
There are three way to generate time delay in the microcontroller
1. Using simple delay function or loop
2. Using predefine function
3. Using internal timer of AVR
//LED Interfacing Program for different pattern generation
//Pattern Example 3. Toggle the all LED which is connected to port B using delay function.
#include <avr/io.h>
void delay(unsigned int a)
{
unsigned int i;
for(i=1;i<=a;i++);
}
int main(void)
{
DDRB=0xff;
while(1)
{
PORTB=0x55;
delay(1000);
PORTB=0xaa;
delay(1000);
}
}
Try it on the simulator by changing the value of delay.
Using pre-define function
//LED Interfacing Program for different pattern generation
//Pattern Example 5. Toggle the all LED which is connected to port B using delay of library and use 1 second delay.
#include <avr/io.h>
#include <util/delay.h>
int main(void)
{
DDRB=0xff;
while(1)
{
PORTB=0x55;
_delay_ms(1000);
PORTB=0xaa;
_delay_ms(1000);
}
}
Note:- The other function of delay generation are also used which is depend according to the different simulator like _delay_ms(),_delay_us(), delay_us(), delay_ms(), delay_loop1(), delay_loop2() etc.
It is very important to note that the predefine delay function is not necessary to give exact value because different complier used different way to execute so the some error may be generate. That’s why it better to used internal timer of microcontroller.
#include <avr/io.h>
#include <util/delay.h>
int main(void)
{
unsigned char temp;
DDRB=0xff;
DDRD=0X00;
while(1)
{
temp=PIND;
PORTB=temp;
}
}
Note: – if we provide the input high then we get high output or other hand if we apply low input then we get low output.
An analog sensor is connected to a PORTD of pin and supposes its give the value in integer form. Now write a program that if it is read less than 500 then send it to the PORTB 0xaa, for the turn on the cooling system else send to the PORTC 0x55, for the normal operations.
#include <avr/io.h>
#include <util/delay.h>
int main(void)
{
unsigned char temp;
DDRD=0X00;
DDRB=0xff;
DDRC=0xff;
while(1)
{
if (PIND<500)
PORTB=0xaa;
else
PORTC=0x55;
}
}
A sensor is connected to the first pin of PORTD. Write a program when door is opened led is turn ON which is connected to the first pin of PORTB
#include <avr/io.h>
#include <util/delay.h>
int main(void)
{
DDRD=DDRD & 0b1111110; //as an input port
DDRB=DDRB|0b00000001; //as an output port
while(1)
{
if (PIND&0b00000001)
PORTB=PORTB|0b0000001;
else
PORTB=PORTB & 0b00000000;
}
}