DC motor controlled using L239D motor driver IC with ATMEGA328


#include <avr/io.h>
#include <util/delay.h>

int main(void)
{

    //control pins
    DDRB|= 1<<PB0; //D1 on L293D 
    DDRB|= 1<<PB1; //D2 on L293D
  
    while(1)
    {

      //Rotate in clockwise direction
      PORTB&=~ (1<<PB0);
      PORTB|= 1<<PB1;
      _delay_ms(2000);

      
      //Rotate in anti-clockwise direction
      PORTB|=  1<<PB0;
      PORTB&=~ (1<<PB1);
      _delay_ms(2000);

    }
}


4×4 keypad interfacing using ATMEGA328p

#include <avr/io.h>
#include <util/delay.h>

#define KeypadDDR DDRD
#define KeypadPortControl PORTD
#define KeypadPinValue PIND

#define LedDDR DDRC
#define LedPortControl PORTC

void KeypadScan()
{
 
  _delay_ms(10);

  //Set Cols as INPUT and ROWS as OUTPUT
  KeypadDDR = 0b11100000;         _delay_ms(1);
  KeypadPortControl = 0b00011111; _delay_ms(1);

  //scan the keypad
  uint8_t keyPressCode = KeypadPinValue;

  if( keyPressCode == 0b00011011 ) //If C1 becomes LOW
  {
     //Set Cols as OUTPUT and ROWS as INPUT
     KeypadDDR = 0b00011100;          _delay_ms(1);
     KeypadPortControl = 0b11100011;  _delay_ms(1);

     //Read the input again
     keyPressCode = KeypadPinValue;
     
     if( keyPressCode == 0b11000011 ) LedPortControl = 0b00000001; //If R1 becomes LOW
     if( keyPressCode == 0b10100011 ) LedPortControl = 0b00000010; //If R2 becomes LOW
     if( keyPressCode == 0b01100011 ) LedPortControl = 0b00000100; //If R3 becomes LOW
   
  }

  if( keyPressCode == 0b00010111 ) //If C2 becomes LOW
  {
     //Set Cols as OUTPUT and ROWS as INPUT
     KeypadDDR = 0b00011100;          _delay_ms(1);
     KeypadPortControl = 0b11100011;  _delay_ms(1);

     //Read the input again
     keyPressCode = KeypadPinValue;
     
     if( keyPressCode == 0b11000011 ) LedPortControl = 0b00001000; //If R1 becomes LOW
     if( keyPressCode == 0b10100011 ) LedPortControl = 0b00010000; //If R2 becomes LOW
     if( keyPressCode == 0b01100011 ) LedPortControl = 0b00100000; //If R3 becomes LOW
   
  }
  
  _delay_ms(100);

  //turn off all lights
  LedPortControl = 0b00000000;
}

int main(void)
{
  //4x3 matrix keypad(Bit 1,2 are  unused)
   KeypadDDR         = 0b11100000;// Rows = 111 , Cols = 000
   KeypadPortControl = 0b00011111;// Rows = 000 , Cols = 111

   LedDDR         = 0b00111111;
   LedPortControl = 0b00000000; // quick led test OFF
   LedPortControl = 0b00111111; // 
   _delay_ms(100);
    while(1)
    {
      KeypadScan();
    }//while
  return 0;
}

Setting up Atmel Studio 7 for arduino boards

Steps as follows,

  1. Download Atmel Studio 7 .
  2. Go to Tools -> External Tools .
  3. Click Add and enter the following details,
    1. Title : Arduino_via_Bootloader (can be anything)
    2. Command : (path to your avrdude.exe)
      H:\Program Files (x86)\Arduino\hardware\tools\avr\bin\avrdude.exe  
    3. Arguments : (path to your avrdude.conf + atmel dir specifics)
      -C”H:\Program Files (x86)\Arduino\hardware\tools\avr\etc\avrdude.conf” -v -v -v -v -patmega328p -carduino -PCOM6 -b115200 -D -Uflash:w:”$(ProjectDir)Debug\$(TargetName).hex”:i

 

atmel_setup

16×2 LCD(1602A) interfacing with ATMEGA328p 4-bit mode

The code is not tested for output.
Blocks are shown instead of chars.

#include <avr/io.h>
#include <util/delay.h>

#define LCD_DATA_DDR DDRD
#define LCD_CTRL_DDR DDRB
#define LCD_DataBus PORTD  //Using PD4 to PD7
#define LCD_CTRL_PORT PORTB

//function prototypes
void LCD_Write(unsigned char cmd, unsigned char reg_select);
void LCD_WriteData(char data);
void LCD_WriteCmd(char cmd);
void LCD_Init();

void LCD_Write(unsigned char cmd, unsigned char reg_select)
{
  char  tempCmd;
  if(reg_select == 1)
    LCD_CTRL_PORT |= (1<<PB0);   //Select Data Register
  else
    LCD_CTRL_PORT &= ~(1<<PB0);   //Select Command Register

  //Mask data/cmd
  tempCmd = cmd;
  tempCmd &= 0xf0;// mask Lower 4 bits

  //Set data/cmd
  LCD_DataBus &= 0xf0;           //Set PORTD LSB to HIGH since we use PD4 to PD7
  LCD_DataBus |= tempCmd;        //Set bits in PORTD

  //Send data/cmd
  LCD_CTRL_PORT |= (1<<PB1); // Send a High-to-Low pulse at Enable pin
  _delay_ms(400);
  LCD_CTRL_PORT &= ~(1<<PB1); // Send a High-to-Low pulse at Enable pin

  //******Send next 4bits

  //mask data/cmd
  tempCmd = cmd << 4; //shift left 4 times so the LSB bit can be sent.
  tempCmd &= 0xf0; //mask Lower 4 bits

  //Set data/cmd in bus
  LCD_DataBus &= 0xf0;
  LCD_DataBus |= tempCmd;

  //Send data/cmd
  LCD_CTRL_PORT |= (1<<PB1); // Send a High-to-Low pulse at Enable pin
  _delay_ms(400);
  LCD_CTRL_PORT &= ~(1<<PB1); // Send a High-to-Low pulse at Enable pin

  return ;
}

//Send data by setting RS=1.
void LCD_WriteData(char data)
{
  LCD_Write(data, 1);
}

//Send cmd by setting RS=0.
void LCD_WriteCmd(char cmd)
{
  LCD_Write(cmd, 0);
}

void LCD_Init()
{
    LCD_WriteCmd(0x02); // to initialize LCD in 4-bit mode.
    _delay_ms(1);
    LCD_WriteCmd(0x28); //to initialize LCD in 2 lines, 5X7 dots and 4bit mode.
    _delay_ms(1);
    LCD_WriteCmd(0x01); // clear LCD
    _delay_ms(1);
    LCD_WriteCmd(0x0E); // cursor ON
    _delay_ms(1);
    LCD_WriteCmd(0x80); // —8 go to first line and –0 is for 0th position
    _delay_ms(1);
    return;
}

int main(void)
{
    LCD_DATA_DDR |= 0xf0;  //set the LCD DDRD to output
    LCD_CTRL_DDR |= 0b00000011; //For RS, E

    LCD_Init();
    _delay_ms(30);
    LCD_WriteData('a');
    LCD_WriteData('2');
    while(1)
    {

    }//while
}

LED blink looping in C

#include <avr/io.h>
#include <util/delay.h>

int main(void)
{
  DDRD = 0b11111111; //led test
  PORTD = 0b00000000;
  unsigned char pinNo = 0;
  while(1)
  {
    while(pinNo<8)
    {
       PORTD = 0x00;
       PORTD |= 1<<pinNo;
       if(pinNo>=7)
          pinNo=0;
       else
          pinNo++;

      _delay_ms(500);

    }
  }
}

Reading input pin in avr using Embedded C

#include <avr/io.h>
#include <util/delay.h>

int main(void)
{
  DDRD |=  0<<PD2;//PD2 is set to Input
  PORTD |= 1<<PD2;//pull-up activated.So, PD2 is high by default

  DDRB |= 1<<PB5 ;
  PORTB |= 0<<PB5 ;
  char portDVal = 0;
  while(1)
  {

      _delay_ms(100);
      portDVal = PIND;

      if( portDVal & (1<<PD2) )//if PD2 is HIGH(value read is 1)
      {
        PORTB &= ~(1<<PB5);
        _delay_ms(50);
      }
      else//if PD2 is LOW(value read is 0)
      {
        PORTB |= (1<<PB5);
        _delay_ms(50);
      }
  }
}

Inmoov bot : Eye movement and laugh in arduino sketch using Induino R5


#include <Servo.h>
 
int eyeLRServopin = 4;
int servoMouthPin = 7;
int eyeTDServopin = 8;

Servo servoEyeLR,servoMouth,servoEyeTD;  
 
int servoAngle = 0;   // servo position in degrees
int laughCount=0;
int eyesPosition;
void setup()
{
  servoEyeLR.attach(eyeLRServopin);
  servoMouth.attach(servoMouthPin);
  servoEyeTD.attach(eyeTDServopin);
  
  pinMode(12,OUTPUT);
  pinMode(13,OUTPUT);

  resetPosition();
  delay(2000);          
 
}
 
 
void loop()
{
//control the servo's direction and the position of the motor
 
   digitalWrite(12, HIGH);//led
   digitalWrite(13, LOW);//led

   servoEyeLR.write(0);  
   delay(2000);          
   servoEyeLR.write(180);     
   delay(2000);          

   servoEyeLR.write(90);//reset eyeLR
    
   servoEyeTD.write(0);  
   delay(2000);          
   servoEyeTD.write(180);     
   delay(2000);          

   servoEyeTD.write(90);//reset eyeTD
//
//    for(eyesPosition=0;eyesPosition<=180;eyesPosition++) // { // servoEyeLR.write(eyesPosition); // delay(500); // servoEyeTD.write(eyesPosition); // delay(500); // } // // for(eyesPosition=180;eyesPosition>=0;eyesPosition--)
//    {
//      servoEyeLR.write(eyesPosition);
//      delay(500);
//      servoEyeTD.write(eyesPosition);
//      delay(500);
//    }

   resetPosition();

    for(laughCount=0;laughCount<10;laughCount++)
    {
     servoMouth.write(0);  
     delay(100);          
  
     servoMouth.write(180);     
     delay(100);          
    }

   //reset
  resetPosition();
  delay(2000);          

}

void resetPosition()
{
    servoEyeLR.write(90);  
    servoMouth.write(90); 
    servoEyeTD.write(90);
}

Fast PWM based Muli-color RGB dimming using ATMEGA328 : Induino R5 in C

/*
 * main.c
 */
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>

double dutyCycle = 0;
int colorSet = 0;

int main(void)
{
    DDRB|= 1<<PB4; //testing

    //Timer setting (Fast PWM, non-inverting)
    TCCR0A = (1 << WGM00) | (1 << WGM01) | (1 << COM0A1) | (1 << COM0B1);
    TCCR0B = (1 << CS00);//CLOCK SELECT : set prescaler to 0

    TCCR2A = (1 << WGM20) | (1 << WGM21) | (1 << COM2A1) | (1 << COM2B1);
    TCCR2B = (1 << CS20);//CLOCK SELECT : set prescaler to 0

    DDRD|= 1<<PD6;//Red (OCOA)
    DDRD|= 1<<PD3;//Green (OC2B)
    DDRD|= 1<<PD5;//Blue (OCOB)

    while(1)
    {
      //testing
        PORTB|=1<<PB4;         _delay_ms(30);         dutyCycle+=1;         if(dutyCycle > 100)
        {
            dutyCycle=0;

            //increment colorSet
            colorSet++;
            if(colorSet>6)
            {
              //reset to start color
              colorSet=0;
            }

            //testing
            if( PORTB & (1<<PB4) ) // if PB4 set
                PORTB&=~1<<PB4; //unset it      

            _delay_ms(1000);

        }

        if(colorSet==0)
          setLEDValues(255,0,0); //RED
        else
        if(colorSet==1)
          setLEDValues(0,255,0); //GREEN
        else
        if(colorSet==2)
          setLEDValues(0,0,255); //BLUE
        else
        if(colorSet==3)
          setLEDValues(255,255,255); //WHITE
        else
        if(colorSet==4)
          setLEDValues(0,255,255); //CYAN
        else
        if(colorSet==5)
          setLEDValues(255,0,255); //PINK
        else
        if(colorSet==6)
          setLEDValues(255,255,0); //YELLOW

    }
}

void setLEDValues(int r,int g,int b)
{
    OCR0A = (dutyCycle/100)*r;  //RED led
    OCR2B = (dutyCycle/100)*g;  //GREEN led
    OCR0B = (dutyCycle/100)*b;  //BLUE led
}

/*
 * TCCR0A, TCCR0B - Timer/Counter Control Registers
 * OCOA  , OCOB   - Output Compare Pins
 * OCR0A , OCR0B  - Output Compare Registers
 * CS01           - Clock Select bit
 * COM0A1, COM0B1 - Compare Output Mode bits
 *
 * /
 */

Timer/Counter Registers in ATMEGA328

 

The following are the timer registers present int atmega328 microcontroller.
  TCNT0 – Timer/Counter Register (8-bit)
  OCR0A – Output Compare Register A
  OCR0B – Output Compare Register B
  TCCR0A/B – Timer/Counter Control Registers
  TIMSK0 – Timer/Counter Interrupt Mask Register
     TOV interrupt
     Compare A&B interrupts
  TIFR0 – Timer/Counter Interrupt Flag Register
     TOV interrupt
     Compare A&B interrupts




timer_atmega328

PWM on RGB led using ATMEGA328 : Induino – R5

/*
 * main.c
 */
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>

double dutyCycle = 0;

int main(void)
{
    DDRB|= 1<<PB4; //testing

    //Timer setting (Fast PWM, non-inverting)
    TCCR0A = (1 << WGM00) | (1 << WGM01) | (1 << COM0A1) | (1 << COM0B1);
    TCCR0B = (1 << CS00);//CLOCK SELECT : set prescaler to 0

    TCCR2A = (1 << WGM20) | (1 << WGM21) | (1 << COM2A1) | (1 << COM2B1);
    TCCR2B = (1 << CS20);//CLOCK SELECT : set prescaler to 0
    
    DDRD|= 1<<PD6;//Red (OCOA)
    DDRD|= 1<<PD3;//Green (OC2B)
    DDRD|= 1<<PD5;//Blue (OCOB)
    
    while(1)
    {
      //testing
        PORTB|=1<<PB4;
        _delay_ms(30);


        //PWM - RED
  
        dutyCycle+=1;
  
        if(dutyCycle > 100)
        {
          dutyCycle=0;
            
          if( PORTB & (1<<PB4) ) // if PB4 set
              PORTB&=~1<<PB4; //unset it      
              
          _delay_ms(500);
  
        }
        OCR0A = (dutyCycle/100)*255;  //RED led
        OCR0B = (dutyCycle/100)*255;  //BLUE led
        OCR2B = (dutyCycle/100)*255;  //GREEN led
    }
}

/*
 * TCCR0A, TCCR0B - Timer/Counter Control Registers
 * OCOA  , OCOB   - Output Compare Pins
 * OCR0A , OCR0B  - Output Compare Registers
 * CS01           - Clock Select bit
 * COM0A1, COM0B1 - Compare Output Mode bits
 *
 */