INTERFACE
Here we are blinking the led making it on and off for a certain duration using AT Mega 16 - LED BLINKING
Circuit diagram:
Program:
# define F_CPU
1000000UL
#define FOSC 16000000L //here we define the clock
frequency
#include <avr/io.h>
#include <util/delay.h>
int main(void)
{
DDRB = 0xFF; //Makes PORTC as
Output
While (1) //infinite loop
{
PORTB = 0xFF; //Turns ON All LEDs
_delay_ms(1000); //1 second delay
PORTB= 0x00; //Turns OFF All LEDs
_delay_ms(1000); //1 second delay
}
}
2.LCD :
CIRCUIT DIAGRAM:
Program:
# define F_CPU 1000000UL
#include
<avr/io.h>
#include
<util/delay.h>
#include
<string.h>
//#define
LCD_PORT PORTB
#define
RS PC0 //initialize register select as PC0 pin
#define
EN PC1 //initialize enable pin as PC1
void
CMD_WRT(unsigned char val)
{
PORTB=val;
PORTC = PORTC &
(~(1<<RS));
_delay_ms(1); //
here
we provide a delay of 1 sec
ORTC = PORTC |
((1<<EN)); //make enable pin
high
_delay_ms(1);
PORTC = PORTC & (~(1<<EN)); //make enable pin low
}
void
DATA_WRT(unsigned char ch)
{
PORTB
= ch;
PORTC
= PORTC | ((1<<RS));//make register select pin high
_delay_ms(1);
PORTC = PORTC | ((1<<EN)); //make enable pin
high
_delay_ms(1);
PORTC = PORTC & (~(1<<EN)); //make enable pin low
}
void
LCD_WRT( char *string)
{
while(*string)
DATA_WRT(*string++);//will write the
strings
}
int
main(void)
{
//setting the display
of the lcd
unsigned char CMD[]={0x38,0x01,0x0f,0x06,0x80},TEMP1,i;
DDRB=0XFF; //make PORTB as output
DDRC =
0xFF;//(1<<RS)|(1<<EN); //make PORTC as
output
_delay_ms(10); //provide the delay
of 10ms
for(i=0;i<5;i++)
{
TEMP1=CMD[i]; //it will place the
command in cmd array
CMD_WRT(TEMP1); //it will write all the cmd that is in the cmd array
}
while(1)
{
CMD_WRT(0X01); //clear display
CMD_WRT(0X80); // blink the cursor in 1st row
LCD_WRT(" --RDL--");//display RDL in lcd
CMD_WRT(0XC0); //to use 2nd row of lcd
LCD_WRT(" LCD_DISPLAY"); //display LCD_DISPLAY
in lcd
_delay_ms(1000); //delay of 1sec
}
return 0;
}
3.PULSE WIDTH MODULATION:
Program:
Program:
//
program
to change brightness of an LED
//
demonstration
of PWM
#include
<avr/io.h>
#include
<util/delay.h>
//
initialize PWM
void
pwm_init()
{
// initialize timer0 in PWM mode
TCCR0 |=
(1<<WGM00)|(1<<COM01)|(1<<WGM01)|(1<<CS00);
// make sure to make OC0 pin (pin PB3 for atmega32) as output pin
DDRB |= (1<<PB3);
}
void
main()
{
uint8_t
brightness;
// initialize timer0 in PWM mode
pwm_init();
// run forever
while(1)
{
// increasing brightness
for (brightness = 0; brightness <
255; ++brightness)
{
// set the brightness as duty cycle
OCR0 = brightness;
// delay so as to make the user
"see" the change in brightness
_delay_ms(10);
}
// decreasing brightness
for (brightness = 255; brightness >
0; --brightness)
{
// set the brightness as duty cycle
OCR0 = brightness;
// delay so as to make the user
"see" the change in brightness
_delay_ms(10);
}
// repeat this forever
}
}
4.ADC :
Program:
Block diagram:
Program:
# define F_CPU 1000000UL
#include
<avr/io.h>
#include
<util/delay.h>
#include
<string.h>
//#include
<iom16.h>
//#define
LCD_PORT PORTB
#define
RS PC0 // connect register
select pin to PC0
#define
EN PC1 // connect enable pin to
PC1
#define LTHRES 500 //setting the threshold valve
#define RTHRES 500
#include
<stdlib.h>
void
CMD_WRT(unsigned char val)
{
PORTB=val; // initializing PORTB as input and passing valve onto it
PORTC
= PORTC & (~(1<<RS));//make RS pin low
_delay_ms(1);
PORTC
= PORTC | ((1<<EN));// make EN pin high
_delay_ms(1);
PORTC
= PORTC & (~(1<<EN));// make EN pin low
}
void
DATA_WRT(unsigned char ch)
{
PORTB
= ch; //initializing
PORTB as input and passing CMD onto it
PORTC
= PORTC | ((1<<RS)); // make RS pin high
_delay_ms(1);
PORTC
= PORTC | ((1<<EN)); //make EN pin high
_delay_ms(1);
PORTC
= PORTC & (~(1<<EN));// make EN pin low
}
void
LCD_WRT( char *string)
{
while(*string)
DATA_WRT(*string++);
}
// initialize adc
void adc_init()
{
// AREF = AVcc
ADMUX = (1<<REFS0); //initialize admux
// ADC Enable and
prescaler of 128
// 16000000/128 =
125000
ADCSRA =
(1<<ADEN)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0);
} //ADEN means ADC enabled
// read adc value
int adc_read(char ch)
{
// select the
corresponding channel 0~7
// ANDing with '7'
will always keep the value
// of 'ch' between 0
and 7
ch &= 0b00000111; // AND operation with 7
ADMUX = (ADMUX & 0xF8)|ch; // clears the bottom 3 bits before ORing
// start single
conversion
// write '1' to ADSC
ADCSRA |= (1<<ADSC);
// wait for conversion
to complete
// ADSC becomes '0'
again
// till then, run loop
continuously
while(ADCSRA & (1<<ADSC));
return
(ADC);
}
int
main(void)
{
uint16_t adc_result0;//, adc_result1;
char int_buffer[10]; //creating array of 10
unsigned
char CMD[]={0x38,0x01,0x0f,0x06,0x80},TEMP1,i;
DDRB=0XFF;//set port b as
output
DDRC
= 0xFF;//(1<<RS)|(1<<EN);
_delay_ms(10);
for(i=0;i<5;i++)
{
TEMP1=CMD[i]; //for each one cycle each command will be placed in that cmd array
CMD_WRT(TEMP1);
}
adc_init();
while(1)
{
//adc_result0 = adc_read(0); // read adc value at PA0
adc_result0 = adc_read(1);
itoa(adc_result0, int_buffer,10);
CMD_WRT(0X01); //clear display
CMD_WRT(0X80); // cursor on first
line
LCD_WRT(" --RDL--"); //display RDL
CMD_WRT(0XC0); //cursor on next line
LCD_WRT(int_buffer);
_delay_ms(1000);
//TODO:: Please write your
application code
}
return
0;
}
5.KEYPAD :
Block diagram:
Program:
#include<avr/io.h>
#include
<avr/keypad.h> // to initialize the keypad
#define
F_CPU 1000000UL
#include
<util/delay.h> //header to use delay
function
#define KEYPAD_PORT
PORTC // connecting keypad
to port c
#define KEYPAD_PIN
PINC //initializing pins
for keypad
#define
LCD_DATA_PORT PORTB
#define
LCD_CONT_PORT PORTD
#define
LCD_RS PD0
#define
LCD_RW PD1
#define
LCD_EN PD2
#include
<lcd.h> //header to
initialize LCD commands
void
main(void)
{
DDRB=0xFF; //make PORTB as output
DDRD=0X07; //make PORTD pin 0, 1, 2 as output
DDRB=0X0F;
PORTC=0xFF; //make PORTC as output
unsigned
char keypad_valve;
lcd_init();
while(1)
{
lcd_command_write(0x08);
//display
off cursor off
lcd_string_write("PRESS
ANY KEY");
lcd_command_write(0xc0);//2nd line display
keypad_valve=read_keypad();
if(keypad_valve!=0xFF)
{
lcd_number_write(keypad_valve,10);//if key is pressed
corresponding valve
will be displayed
lcd_data_write(' ');
}
else
;
_delay_ms(300);
}
}