Pages

Thursday, 19 March 2015

Programming With AVR Microcontroller: Chapter 3

INTERFACE
  1. LED BLINKING
Here we are blinking the led making it on and off for a certain duration using AT Mega 16



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 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:


 # 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);
     }
     }
 







 







Wednesday, 18 March 2015

Programming With AVR Microcontroller: Chapter 2

BURNING THE CODE        
  1. AVR osp-2
  1. click on avr osp
  2. The default avr osp 2 will look like this

3.First you need to configure your avr osp to check its connected to the same com port or not    and default Baud rate 


4.you can manually select the device which you are using or else u can even auto detect it  



5.In device select which device you are using as here I am using ATmega 16 I have selected that .Even you can Auto Detect your device that option will automatically Detect your Device which you are using




6.after pressing auto detect you will see this means that your microcontroller has been detected and it read to flash program on to it



7.To flash the program on the Microcontroller You Browse the program and Then click on program after click on the program it will flash the program.



8.Browse your hex file where you have  store



9.After clicking on program it will erase the previous content of the chip and will flash the program on to the chip 



10.After  the completion of the flash it will compare the flash data if its equal than it means that your flash is successful and it will show “Equal”


2.SINA PROG 2.1

  • If You are using ISP Atmega Programmer to Burn the code In Your Microcontroller than this Programmer will surely come in handy
  • Its simple to use and You can easily use the Microcontroller For External Crystal Frequency
 
  • If You Want to Change Internal Frequency Or want To use External Crystal Frequency than You can change the Fuse.
 

  • Here You Can Set Internal Frequency To
  1. 1 MHz
  2. 2 MHz
  3. 4 MHz
  4. 8 MHz
  • If You want to use External Frequency than select Ext.Crys.
(If Ext Crys Doesn’t work properly than Try changing the BC valve as shown in Below Pic)


  • Usually For Atmega 16 it comes out to be BC 32 KHz and For Atmega 32 BC 16 KHz but try with Other Valve if this Doesn’t Works.

Monday, 16 March 2015

Programming With AVR Microcontroller: Chapter-1

OVERVIEW:
ATmega16 is an 8-bit high performance microcontroller of Atmel’s Mega AVR family with low power consumption. Atmega16 is based on enhanced RISC architecture with 131 powerful instructions. Most of the instructions execute in one machine cycle. Atmega16 can work on a maximum frequency of 16MHz.

PORTS:
There are 32 I/O (Input/Output) pins grouped as A, B, C & D with 8 pins in each group. This group is called as PORT.
PA0 - PA7 (PORTA)
PB0 - PB7 (PORTB)
PC0 - PC7 (PORTC)
PD0 - PD7 (PORTD)


These are additional function that pin can perform other than I/O. Some of them are.
  • ADC (ADC0 - ADC7 on PORTA)
  • UART (Rx,Tx on PORTD)
  • TIMERS (OC0 - OC2)
  • SPI (MISO, MOSI, SCK on PORTB)
  • External Interrupts (INT0 - INT2)


PIN DESCRIPTION:
  • VCC: Digital supply voltage. (+5V)
  • GND: Ground. (0 V) Note there are 2 ground Pins.

  • Port A (PA7 - PA0)

Port A serves as the analog inputs to the A/D Converter. Port A also serves as an 8-bit bi-directional I/O port, if the A/D Converter is not used. When pins PA0 to PA7 are used as inputs and are externally pulled low, they will source current if the internal pull-up resistors are activated. The Port A pins are tri-stated when a reset condition becomes active, even if the clock is not running.
 



  • Port B (PB7 - PB0)
Port B is an 8-bit bi-directional I/O port with internal pull-up resistors (selected for each bit). Port B also serves the functions of various special features of the ATmega16 as listed on page 58 of datasheet.
  • Port C (PC7 - PC0)
Port C is an 8-bit bi-directional I/O port with internal pull-up resistors (selected for each bit). Port C also serves the functions of the JTAG interface and other special features of the ATmega16 as listed on page 61 of datasheet. If the JTAG interface is enabled, the pull-up resistors on pins PC5 (TDI), PC3 (TMS) and PC2 (TCK) will be activated even if a reset occurs.
  • Port D (PD7 - PD0)
Port D is an 8-bit bi-directional I/O port with internal pull-up resistors (selected for each bit). Port D also serves the functions of various special features of the ATmega16 as listed on page 63 of datasheet.
  • RESET: Reset Input. A low level on this pin for longer than the minimum pulse length will generate a reset, even if the clock is not running.
  • XTAL1: External oscillator pin 1
  • XTAL2: External oscillator pin 2
  • AVCC: AVCC is the supply voltage pin for Port A and the A/D Converter. It should be externally connected to VCC, even if the ADC is not used. If the ADC is used, it should be connected to VCC through a low-pass filter.
  • AREF: AREF is the analog reference pin for the A/D Converter.
 WRITING THE CODE
  

  1.   AVR STUDIO 
Setup:
  1. open AVR studio 4
  2. click new project
  3. Select AVR GCC as we would be doing program in c and enter the project name.

4.Add your project name. Here I have given my project name as serial and it will automatically create your initial file name in .c format


 5.click on next 

6.Select AVR simulator in debug platform and in the Device select the Device your using as here I am using ATmega 16 i have selected that.(for Atmega 32 select Atmega 32).







 7.click on finish
8.This is the default window you will see after you click on finish and project name called serial will get open  
9.Now click on this to create a new file


10.Type the program and save it .To compile the files first you need to add files in your source for doing that right click on the source file and then 



click on add existing source file
11.Now inside that source file their will be your file saved here the file name I have used is urted
12.Now to build the file go to build and click on build
13.If your program has no errors  it will be successfully build and it will show the build window like this


TO BURN  THE  PROGRAM   IN  YOUR  MICROCONTROLLER  YOU  NEED  A BURNER  FOR BURNING  WE  USE AVR OPS-2