Showing posts with label Robotics. Show all posts
Showing posts with label Robotics. Show all posts

Thursday, 30 July 2015

What is Clock Stretching of I2C protocol? Why it is needed?

Clock Stretching:
Clock stretching is a phenomenon where the I2C slave pulls the SCL line low on the 9th clock of every I2C data transfer. The clock is pulled low when the CPU is processing the I2C interrupt to evaluate either the address or process a data received from Master or to prepare the next data when Master is reading from the slave.

Why needed?
When the master is reading from the slave, its the slave that places the data on the SDA line, but its the master that controls the clock. If the slave is not ready to send the data, the microprocessor on the slave device will need to go to an interrupt routine, save its working registers, find out what address the master wants to read from, get the data and place it in its transmission register. The master is blissfully sending out clock pulses on the SCL line that the slave cannot respond to. That time clock stretching is needed. In this time, the slave is allowed to hold the SCL line low. The I2C slave holds down the SCL line and the master should wait until it goes high which indicates that the slave is ready to send data.


Wednesday, 24 June 2015

Seven Segment Display

/*

 * seven_segment.c

 *

 *  Created on: May 25, 2015

 *      Author: Tanzila Islam

 *  atmega328p driving a seven segment common cathode display. seven segment A-G is connected to portd 0-6 in order

 *    G F E D C B A

 *   0 ->  0 1 1 1 1 1 1

 *   1 -> 0 0 0 0 1 1 0

 *   2 -> 1 0 1 1 0 1 1

 *   3 -> 1 0 0 1 1 1 1

 *   4 -> 1 1 0 0 1 1 0

 *   5 -> 1 1 0 1 1 0 1

 *   6 -> 1 1 1 1 1 0 1

 * 7 -> 0 0 0 0 1 1 1

 * 8 -> 1 1 1 1 1 1 1

 * 9 -> 1 1 0 1 1 1 1

 * A -> 1 1 1 0 1 1 1

 * b -> 1 1 1 1 1 0 0

 * C -> 0 1 1 1 0 0 1

 *   d -> 1 0 1 1 1 1 0

 *   E -> 1 1 1 1 0 0 1

 *   F -> 1 1 1 0 0 0 1

 *

 *      _A

 *  F|_|B

 *  E|_|C

 *      D

 */


#include <avr/io.h>

#include <avr/delay.h>


#define F_CPU 16000000UL


const uint8_t segment_look_up[] ={

  0b00111111,//0

  0b00000110,//1

  0b01011011,//2

  0b01001111,//3

  0b01100110,//4

  0b01101101,//5

  0b01111101,//6

  0b00000111,//7

  0b01111111,//8

  0b01101111,//9

  0b01110111,//A

  0b01111100,//b

  0b00111001,//C

  0b01011110,//d

  0b01111001,//E

  0b01110001 //F

};


int main()

{

 DDRD = 0b01111111;

 //Following will have same effect

 //DDRD |= (1<<DDD6)|(1<<DDD5)|(1<<DDD4)|(1<<DDD3)|(1<<DDD2)|(1<<DDD1)|(1<<DDD0);

 //DDRD |= _BV(DDD6)|_BV(DDD5)|_BV(DDD4)|_BV(DDD3)|_BV(DDD2)|_BV(DDD1)|_BV(DDD0);

 PORTD = 0x00;

 uint8_t i;

 while(1)

 {

  for(i=0;i<15;i++)

  {

   PORTD= segment_look_up[i];

   _delay_ms(2000);

  }


 }


}

Line Follower Robot

int last_direction; // variable declaration

#define forward 0 //flag define

#define left 1    //flag define

#define right 2   //flag define



void robot_move(int direc)//define a function named robot_move which will return direction

{

if(direc==forward)        //if the direction is forward

{

 PORTD=0x05;              //IN1=1, In2=0, IN3=1, IN4=0 ,ie both motor will move forward

 Delay_ms(1);            //wait for 10 mili second

}


if(direc==left)          //if the direction is left

{

 PORTD=0x01;             //IN1=1,In2=0,IN3=0,IN4=0

 Delay_ms(1);           //wait for 10 mili second ie left motor will stop, right will move forward

}

if(direc==right)         //if the direction is right

{

 PORTD=0x04;             //IN1=0,IN2=0,IN3=1,IN4=0 ;ie right motor will stop, left will move forward

 Delay_ms(1);           //wait for 10 mili second

}

}




void main()

{

   TRISD=0x00;                  //PORTD is output

   TRISB1_bit=1;               //Declare RB1 as input

   TRISB2_bit=1;               //Declare RB2 as input

   while(1)

   {


   if(RB1_bit==1&& RB2_bit==1) // When both sensors are on black line

   {

    robot_move(forward);      //move forward

    last_direction=forward;   //save forward in the variable named last direction


    }

   else if(RB1_bit==1&& RB2_bit==0) // When left sensor is on black line

   {

    robot_move(left);               //move left

    last_direction=left;             //save left in the variable named last direction



    }

   else if(RB1_bit==0&&RB2_bit==1)// When right sensor is on black line

   {

    robot_move(right);            //move right

    last_direction=right;         //save right in the variable named last direction


   }


   else if (RB1_bit=0&&RB2_bit==0)// When none of the sensors is on black line

   {


    robot_move(last_direction);  //move to last direction


   }

   }


}