*WAP TO SEND VALUES 00-FF TO PORT B
#include<avr/io.h>
Int main(void)
{
Unsigned char z;
DDRB=0xff; //portb is o/p
for(z=0;z<=255;z++)
PORTB=z;
return 0;
}
*WAP TO SEND HEX VALUES FOR ASCII CHARACTERS OF
0,1,2,3,4,5,A,B,C,D TO PORT B
#include <avr/io.h>
Int main(void)
{
Unsigned char myList[]=”012345ABCD”;
Unsigned char z;;
DDRB=0xff;
for(z=0;z<10;z++)
PORTB=myList[z];
while(1);
return 0;
}
*WAP TO TOGGLE ALL BITS OF PORTB 200 TIMES
#include<avr/io.h>
Int main(void)
{
DDRB=0xff;
PORTB=0xaa;
Unsigned char z;
for(z=0;z<200;z++)
PORTB= ~PORTB;
while (1);
return 0;
}
*WAP TO TOGGLE ALL
BITS OF PORTB CONTINUOSLY WITH 100mS DELAY. ASSUME SYS IS ATmega32 WITH
XTAL=8MHZ
#include<avr/io.h>
Void delay100ms(void)
{
Unsigned int I;
For(i=0;i<42100;i++);
}
Int main(void)
{
DDRB=0xff;
While(1)
{
PORTA=0xaa;
Delay100ms();
PORTB=0x55;
Delay100ms();
}
Return 0;
}
*WAP TO TOGGLE ALL PINS OF PORTC CONTINUOSLY with 100ms
delay. Use predefined function.
#include<avr/io.h>
#define F_CPU 1600000UL
#include<util/delay.h>
Int main(void)
{
DDRC=0xff;
while(1)
{
PORTC=0xff;
_delay_ms(10);
PORTC=0x55;
_delay_ms(10);
_delay_us(1000);
}
return 0;
}
I/O PROGRAMMING
*WAP THAT SHOWS COUNT FROM 0 TO FFH ON LEDS AT PORTB
#include<avr/io.h>
Int main(void)
{
DDRB=0xff;
while(1)
{
PORTB=PORTB+1;
}
return 0;
}
*WAP TO GET A BYTE FROM PORTB AND SEND IT TO PORTC
#include<avr/io.h>
Int main(void)
{
Unsigned char temp;
DDRB=0x00;
//portb is i/p
DDRC=0xff;
//portc is o/p
while(1)
{
Temp=PINB;
PORTC=temp;
}
return 0;
}
*WAP TO TOGGLE ONLY BIT4 OF PORTB CONTINUOSLY WITHOUT
DISTURBING THE REST OF THE PINS OF PORTB.
#include<avr/io.h>
Int main(void)
{
DDRB=0xff;
while(1)
{
PORTB=PORTB|0b00010000; //pin4=1
PORTB=PORTB&0b11101111; //pin4=0
}
rerurn 0;
}
*A DOOR SENSOR IS CONNECTED TO BIT 1 OF PORTB AND AN LED
IS CONNECTED TO BIT7 OF PORTC.WAP TO MONITOR DOOR SENSOR & WHEN IT OPENS,
TURN ON THE LED.
#include<avr/io.h>
Int main(void)
{
DDRB=DDRB&0b11111101; //CHECK
DDRC=DDRC|0b10000000;
//SET
while(1)
{
if(PINB&0b00000010)
PORTC=PORTC|0b10000000;
else
PORTC=PORTC&0b01111111;
}
return 0;
}
BITWISE SHIFT OPERATION IN AVR
Eg: 0b00010000>>3 = 0b00000010(shift right)
1<<3=
0b00001000(shift left)
*WAP TO MONITOR BIT7 OF PORTB. IF IT IS 1,MAKE BIT4 OF
PORTB I/P ELSE CHANGE PIN4 OF PORTB TO O/P.
#include<avr/io.h>
int main(void)
{
DDRB=DDRB&~(1<<7);
while(1)
{
if(PINB &(1<<7))
DDRB=DDRB & ~(1<<4); //bit4 of b is i/p
else
DDRB=DDRB |(1<<4); //bit4 of b is o/p
}
return 0;
}
*A DOOR SENSOR IS CONNECTED TO PORTB PIN1, AND AN LED IS
CONNECTED TO PORTC PIN7. WAP TO MONITOR THE DOOR SENSOR AND WHEN IT OPENS, TURN
ON LED.
#include<avr/io.h>
#define LED 7
#define SENSOR 1
int main(void)
{
DDRB=DDRB &(1<<SENSOR); //sensor is i/p
DDRC=DDRC |(1<<LED); //led is o/p
while(1)
{
If(PINB & (1<<SENSOR)) //check sensor pin
PORTC=PORTC|(1<<LED); //set led pin
else
PORTC=PORTC & ~(1<<LED); //clear led pin
}
return 0;
}
0 comments:
Post a Comment