1. LED Blinking: 

#include <avr/io.h>
#include <avr/delay.h>
void main()
{
int x;
DDRC=0xff;
PORTC=0xff;
while(1)
{
x=~PORTC;
PORTC=x;
_delay_ms(1000);
}}

 

 

2. LED Scrolling: 

#include <avr/io.h>
#include <avr/delay.h>
void main()
{
DDRA=0xff;
int i;
while(1)
{
for(i=1;i<9;i=i*2)
{
PORTA=i;
_delay_ms(1000);
}}}

 

 3. LED Blinking Using Switch:

#include <avr/io.h>
#include <avr/delay.h>
void main()
{
DDRA=0x00; //PORTA as output
DDRB=0xff; //PORTB as input
unsigned char i,x,y;
PORTB=0x00;
while(1)
{
i=PINA;
x=i&0b00000001;
if(x>0)
{
y=~PORTB;
PORTB=y;
_delay_ms(500);
x=0;
}}}

 

 4. Scrolling LED Using switch:

#include <avr/io.h>
#include <avr/delay.h>
void main()
{
DDRA=0xff; //PORTA as output
DDRB=0x00; //PORTB as input
unsigned char i,x,y=1;
PORTA=0x00;
while(1)
{
i=PINB;
x=i&0b00000001;
if(x>0)
{
PORTA=y;
y=y*2;
if(y==8)
{
y=1;
}
_delay_ms(500);
x=0;
}}}

 

  5. Seven Segment Display:

#include <avr/io.h>
#include <util/delay.h>
int match(uint8_t tanvir);
volatile int i,count=0,fd=0b10111111,sd=0b10111111,first,second,extra;
void main()
{
DDRB=0x00;        //PORTB as INPUT
DDRA=0xff;        //PORTA as output
DDRD=0xff;        //PORTD as output
for(;;)
{
count++;
_delay_ms(1000);
if(i==1)
{
     if(count>99)
    {    
    count=0;
    }
first=count%10;
fd = match(first);
second=count/10;
sd=match(second);
_delay_ms(200);        //for debouncing
}
PORTA=fd;
PORTD=0x01;        //First 7-segment Control Pin at PD0
_delay_ms(15);
PORTD=0x0;
PORTA=sd;
PORTD=0x2;        //Second 7-segment Control pin at PD1
_delay_ms(15);
PORTD=0x0;
}}
int match(uint8_t tanvir)
{
    if (tanvir==0)
        return 0b10111111;        // 0
    else if (tanvir==1)
        return 0b10000110;        // 1
    else if (tanvir==2)
        return 0b11011011;        // 2
     else if (tanvir==3)
        return 0b11001111;        // 3
     else if (tanvir==4)
        return 0b11100110;        // 4
     else if (tanvir==5)
        return 0b11101101;        // 5
     else if (tanvir==6)
        return 0b11111101;        // 6
     else if (tanvir==7)
        return 0b10000111;        // 7
     else if (tanvir==8)
        return 0b11111111;         // 8
     else
        return 0b11101111;        // 9
}

 
lcd_lib.c lcd_lib.c
Size : 8.46 Kb
Type : c
lcd_lib.h lcd_lib.h
Size : 4.407 Kb
Type : h

6. LCD:

#include <avr/io.h>
#include <util/delay.h>
#include "lcd_lib.h"
#include <avr/pgmspace.h>
#include <stdio.h>

/*LCD functions are -
1.     LCDsendChar()            -    writes character on LCD
2.     LCDsendCommand()        -    Sends command to LCD
3.     LCDinit(void)            -    Initializes LCD
4.    LCDclr(void)            -    Clears LCD
5.    LCDhome(void)            -    LCD cursor home
6.    LCDstring(*data, bytes)    -    Writes string to LCD
7.    LCDGotoXY(X,Y)            -    Cursor to X, Y position
8.    CopyStringtoLCD()        -    Copy string from flash memory to LCD
9.    LCDdefinechar()            -    Define Character symbol in CGRAM
10.    LCDshiftLeft(n)            -    Shifts character on LCD Left   
11.    LCDshiftRight(n)        -    Shifts character on LCD Right
12.    LCDcursorOn(void)        -    Cursor is visible
13.    LCDcursorOnBlink(void)    -    Cursor is on and blinking
14.    LCDcursorOFF(void)        -    Cursor is invisible
15.    LCDblank(void)            -    Blanks the LCD
16.    LCDvisible(void)        -    Shows LCD
17.    LCDcursorLeft(n)        -    Shifts the cursor left by n place
18.    LCDcursorRight(n)        -    Shifts the cursor right by n place
19.    LCDprogressBar()        -    progress bar on LCD


*/
void main(void)
{
int x;
LCDinit();
for(x=0;x<100;x++)
{
LCDsendChar('-');
LCDsendChar('-');
LCDsendChar('>');
_delay_ms(500);
LCDclr();
LCDGotoXY(x,0);
}}

 

7. 7*5 Dot Matrix Showing E: 

#include <avr/io.h>
#include <util/delay.h>
#include <avr/pgmspace.h>
const char dotmatrix[5] PROGMEM=
{
0b01111111,
0b01001001,
0b01001001,
0b01001001,
0b01000001
};
void main(void)
{
DDRA=0xff;    //PORTA as OUTPUT
DDRD=0xff;    //PORTA as OUTPUT for controlling transistor
PORTD=0x00;
PORTA=0x00;
int i;
char b;
while(1)
{
for(i=0;i<5;i++)
{
b=pgm_read_byte(&(dotmatrix[i]));
PORTA=b;
switch(i)
{
case 0:
PORTD=0x01;
break;
case 1:
PORTD=0x02;
break;
case 2:
PORTD=0x04;
break;
case 3:
PORTD=0x08;
break;
case 4:
PORTD=0x10;
break;
default:
break;
}
_delay_ms(5);
}}}

 

8. A Microcontroller Based Clock: 

#include <avr/io.h>
#include <util/delay.h>
#include "lcd_lib.h"
#include <stdio.h>
void main(void)
{
//double tanvir=4.134;
unsigned char bappi[6];
uint8_t second=0,minute=0,hour=12,pm_am=0;
unsigned char time[8];
LCDinit();
LCDclr();
start:
_delay_ms(1000);
second++;
if(second==60)
{
second=0;
minute++;
if(minute==60)
{
minute=0;
hour++;
if(hour==12)
{
pm_am++;
//Date
if(pm_am==2)
{
pm_am=0;
//More Code for date are inter listed here
}
}
if(hour==13)
{
hour=1;
}}}
// LCD representation here
//sprintf(bappi,"%f",tanvir);
sprintf(time,"%2d:%2d:%2d",hour,minute,second);
LCDGotoXY(0,0);
LCDstring(time,8);
LCDGotoXY(8,0);
if(pm_am==0)
{
LCDsendChar('A');
LCDsendChar('M');
}
if(pm_am==1)
{
LCDsendChar('P');
LCDsendChar('M');
}
//
//LCDstring(bappi,5);
goto start;
}

 

9. ADC- Microcontroller based Thermometer: 

#include <avr/io.h>
#include <util/delay.h>
#include <stdio.h>
#include <avr/interrupt.h>
#include "lcd_lib.h"
float voltage_bin;
char voltage[7];
/*****************************************************************************
  ADC module initialization
*****************************************************************************/
void adc_init(void)
{
//select reference voltage
//AVCC with external capacitor at AREF pin
//ADMUX|=(0<<REFS1)|(1<<REFS0);
ADMUX=0b01000000;
ADCSRA=0b10001000;
//set prescaller and enable ADC
//ADCSRA|=(1<<ADEN)|(1<<ADIE);//enable ADC with dummy conversion
//set sleep mode for ADC noise reduction conversion
//set_sleep_mode(SLEEP_MODE_ADC);
}
/*****************************************************************************
 ADC single conversion routine
******************************************************************************/
void adc_start_conversion(uint8_t channel)
{
//remember current ADC channel;
//ch=channel;
//set ADC channel
ADMUX=(ADMUX&0xF0)|channel;
//Start conversionio with Interupt after conversion
//enable global interrupts
sei();
ADCSRA =(ADCSRA| ((1<<ADSC)|(1<<ADIE)));
//ADCSRA=
}
/*****************************************************************************
delay 1s
*****************************************************************************/
void delay1s(void)
{
    uint8_t i;
    for(i=0;i<100;i++)
    {
        _delay_ms(10);
    }
}
/*****************************************************************************
init AVR
*****************************************************************************/
void init(void)
{
    //init LCD
    LCDinit();//init LCD bit, dual line, cursor right
    LCDclr();//clears LCD
    LCDGotoXY(0, 0);//Cursor Home
    //Init ADC
    adc_init();
}
/*****************************************************************************
ADC conversion complete service routine
*****************************************************************************/
ISR(ADC_vect)
{
    uint16_t adc_value;
    adc_value = ADCL; 
    /*shift from low level to high level ADC, from 8bit to 10bit*/
    adc_value += (ADCH<<8);
    voltage_bin=adc_value;
    voltage_bin=((voltage_bin*5)/1024);   
    sprintf(voltage,"%1.4f",voltage_bin);
    LCDGotoXY(0,0);
    LCDstring(voltage,6);
}
/*****************************************************************************
run analog digital converter, timer.
*****************************************************************************/
int main(void)
{
    init();
    while(1)//loop demos
    {
    //read LDR
    adc_start_conversion(0);
    _delay_ms(30);
    //continue infinitely
    }
    return 0;
}

 

 10. MC based Function Generation: 

#include <avr/io.h>
#include <util/delay.h>
#include <avr/pgmspace.h>
const uint8_t  sinewave[256] PROGMEM= //sine 256 values
{
0x80,0x83,0x86,0x89,0x8c,0x8f,0x92,0x95,0x98,0x9c,0x9f,0xa2,0xa5,0xa8,0xab,0xae,
0xb0,0xb3,0xb6,0xb9,0xbc,0xbf,0xc1,0xc4,0xc7,0xc9,0xcc,0xce,0xd1,0xd3,0xd5,0xd8,
0xda,0xdc,0xde,0xe0,0xe2,0xe4,0xe6,0xe8,0xea,0xec,0xed,0xef,0xf0,0xf2,0xf3,0xf5,
0xf6,0xf7,0xf8,0xf9,0xfa,0xfb,0xfc,0xfc,0xfd,0xfe,0xfe,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0xfe,0xfd,0xfc,0xfc,0xfb,0xfa,0xf9,0xf8,0xf7,
0xf6,0xf5,0xf3,0xf2,0xf0,0xef,0xed,0xec,0xea,0xe8,0xe6,0xe4,0xe2,0xe0,0xde,0xdc,
0xda,0xd8,0xd5,0xd3,0xd1,0xce,0xcc,0xc9,0xc7,0xc4,0xc1,0xbf,0xbc,0xb9,0xb6,0xb3,
0xb0,0xae,0xab,0xa8,0xa5,0xa2,0x9f,0x9c,0x98,0x95,0x92,0x8f,0x8c,0x89,0x86,0x83,
0x80,0x7c,0x79,0x76,0x73,0x70,0x6d,0x6a,0x67,0x63,0x60,0x5d,0x5a,0x57,0x54,0x51,
0x4f,0x4c,0x49,0x46,0x43,0x40,0x3e,0x3b,0x38,0x36,0x33,0x31,0x2e,0x2c,0x2a,0x27,
0x25,0x23,0x21,0x1f,0x1d,0x1b,0x19,0x17,0x15,0x13,0x12,0x10,0x0f,0x0d,0x0c,0x0a,
0x09,0x08,0x07,0x06,0x05,0x04,0x03,0x03,0x02,0x01,0x01,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x02,0x03,0x03,0x04,0x05,0x06,0x07,0x08,
0x09,0x0a,0x0c,0x0d,0x0f,0x10,0x12,0x13,0x15,0x17,0x19,0x1b,0x1d,0x1f,0x21,0x23,
0x25,0x27,0x2a,0x2c,0x2e,0x31,0x33,0x36,0x38,0x3b,0x3e,0x40,0x43,0x46,0x49,0x4c,
0x4f,0x51,0x54,0x57,0x5a,0x5d,0x60,0x63,0x67,0x6a,0x6d,0x70,0x73,0x76,0x79,0x7c
};
void main(void)
{
int i,x;
DDRA=0xff;
for(;;)
{
for(i=0;i<=255;i++)
{
x=pgm_read_byte(&(sinewave[i]));
PORTA=x;
_delay_ms(5);
}}}

 

 11. MC Communication using TWI:

MASTER:

#include <avr/io.h>
#include <util/delay.h>
#include <util/twi.h>
void twi_init(void)
{
TWBR=0x01;
TWSR=(TWSR|((0<<TWPS1)|(0<<TWPS0)));        //SCL frequency ~ 41KHz
}
void twi_send_start(void)
{
TWCR=(1<<TWINT)|(1<<TWSTA)|(1<<TWEN);  //twi send start signal to slave
}
void twi_send_data(void)
{
TWCR=(1<<TWINT)|(1<<TWEN);
}
void twi_send_stop(void)
{
TWCR=(1<<TWINT)|(1<<TWEN)|(1<<TWSTO);  //send Stop signal to Slave
}
void error(void)
{
}
void main(void)
{
_delay_ms(1000);
twi_init();
twi_send_start();
while(!(TWCR&(1<<TWINT)));        //Wait util transmission complete.
if(TWSR&0xf8 != TW_START)
    error();
TWDR=0b00010100;                    //Address - 0001110 and 0- MT
twi_send_data();
while(!(TWCR&(1<<TWINT)));        //Wait util transmission complete.
if(TWSR&0xf8 !=TW_MT_SLA_ACK)    //check error
    error();
TWDR=0b11110000;        //data to be send
twi_send_data();
while(!(TWCR&(1<<TWINT)));        //Wait util transmission complete.
if(TWSR&0xf8 !=TW_MT_DATA_ACK)        //check error
    error();
_delay_ms(500);
/* Second DATA        */    
TWDR=0b11011011;        //data to be send
twi_send_data();
while(!(TWCR&(1<<TWINT)));        //Wait util transmission complete.
if(TWSR&0xf8 !=TW_MT_DATA_ACK)    //check error
    error();    
_delay_ms(500);    
/*Third Data        */
TWDR=0b10101010;        //data to be send
twi_send_data();
while(!(TWCR&(1<<TWINT)));        //Wait util transmission complete.
if(TWSR&0xf8 !=TW_MT_DATA_ACK)
    error();
twi_send_stop();
// Second SLave
_delay_ms(500);
twi_init();
twi_send_start();
while(!(TWCR&(1<<TWINT)));        //Wait util transmission complete.
if(TWSR&0xf8 != TW_START)
    error();
TWDR=0b00011100;                    //Address - 0001110 and 0- MT
twi_send_data();
while(!(TWCR&(1<<TWINT)));        //Wait util transmission complete.
if(TWSR&0xf8 !=TW_MT_SLA_ACK)
    error();
TWDR=0b11000011;        //data to be send
twi_send_data();
while(!(TWCR&(1<<TWINT)));        //Wait util transmission complete.
if(TWSR&0xf8 !=TW_MT_DATA_ACK)
    error();
_delay_ms(500);
/* Second DATA        */    
TWDR=0b11101110;        //data to be send
twi_send_data();
while(!(TWCR&(1<<TWINT)));        //Wait util transmission complete.
if(TWSR&0xf8 !=TW_MT_DATA_ACK)
    error();    
_delay_ms(500);    
/*Third Data        */
TWDR=0b10000001;        //data to be send
twi_send_data();
while(!(TWCR&(1<<TWINT)));        //Wait util transmission complete.
if(TWSR&0xf8 !=TW_MT_DATA_ACK)
    error();
twi_send_stop();
_delay_ms(500);
twi_init();
twi_send_start();
while(!(TWCR&(1<<TWINT)));        //Wait util transmission complete.
if(TWSR&0xf8 != TW_START)
    error();
TWDR=0b00010100;                    //Address - 0001110 and 0- MT
twi_send_data();
while(!(TWCR&(1<<TWINT)));        //Wait util transmission complete.
if(TWSR&0xf8 !=TW_MT_SLA_ACK)    //check error
    error(); 
TWDR=0b11110000;        //data to be send
twi_send_data();
while(!(TWCR&(1<<TWINT)));        //Wait util transmission complete.
if(TWSR&0xf8 !=TW_MT_DATA_ACK)        //check error
    error();
_delay_ms(500);
/* Second DATA        */    
TWDR=0b11011011;        //data to be send
twi_send_data();
while(!(TWCR&(1<<TWINT)));        //Wait util transmission complete.
if(TWSR&0xf8 !=TW_MT_DATA_ACK)    //check error
    error();    
_delay_ms(500);    
/*Third Data        */
TWDR=0b10101010;        //data to be send
twi_send_data();
while(!(TWCR&(1<<TWINT)));        //Wait util transmission complete.
if(TWSR&0xf8 !=TW_MT_DATA_ACK)
    error();
twi_send_stop();
}


SLAVE 1:

// TWI Receiver Slave 1
#include <avr/io.h>
#include <util/delay.h>
#include <util/twi.h>
void twi_init(void)
{
TWBR=0x01;
TWSR=(TWSR|((0<<TWPS1)|(0<<TWPS0)));        //SCL frequency ~ 41KHz while cpu frequency 1 mhz
TWAR=0b00010100;        //set the slave address
}
void main(void)
{
DDRA=0xff;
uint8_t x;
twi_init();
while(1)
{
TWCR=(1<<TWINT)|(1<<TWEA)|(0<<TWSTA)|(0<<TWSTO)|(1<<TWEN);
while(!(TWCR&(1<<TWINT)));        //Wait util transmission complete.
x=TWDR;
PORTA=x;
}}


SLAVE 2:

// TWI Receiver Slave 2
#include <avr/io.h>
#include <util/delay.h>
#include <util/twi.h>
void twi_init(void)
{
TWBR=0x01;
TWSR=(TWSR|((0<<TWPS1)|(0<<TWPS0)));        //SCL frequency ~ 41KHz
TWAR=0b00011100;        //set the slave address
}
void main(void)
{
DDRA=0xff;
uint8_t x;
twi_init();
while(1)
{
TWCR=(1<<TWINT)|(1<<TWEA)|(0<<TWSTA)|(0<<TWSTO)|(1<<TWEN);
while(!(TWCR&(1<<TWINT)));        //Wait util transmission complete.
x=TWDR;
PORTA=x;
}}

 
 
Make a Free Website with Yola.