Traffiic lights with LCD and LEDS using ATMEGA32 with PROTEUS







Main code:

#include <avr/io.h>

#include "LCD.h"

#include "timer.h"

#define F_CPU 8000000UL

#include <util/delay.h>

#include "LED.h"

#include <avr/interrupt.h>

volatile unsigned char counter1=0;

volatile unsigned char counter_green=10;

volatile unsigned char counter_yellow=5;

volatile unsigned char counter_red=7;

int main(void)

{

LED_vInit('D',0);

LED_vInit('D',1);

LED_vInit('D',2);

LCD_vInit();

timer_CTC_init_interrupt();

    while(1)

    {

counter_green=10;

counter_yellow=5;

counter_red=7;

LCD_clearscreen();

LCD_vSend_string("remaining 10 sec");

LED_vTurnOn('D',0);

while(counter_green>0)

{

if(counter1>=100)

{

counter1=0;

counter_green--;

LCD_movecursor(1,11);

LCD_vSend_char(' ');

LCD_vSend_char((counter_green%10)+48);

}

}

_delay_ms(500);

LED_vTurnOn('D',1);

LED_vTurnOff('D',0);

LCD_clearscreen();

LCD_vSend_string("remaining  5 sec");

while(counter_yellow>0)

{

if(counter1>=100)

{

counter1=0;

counter_yellow--;

LCD_movecursor(1,11);

LCD_vSend_char(' ');

LCD_vSend_char((counter_yellow%10)+48);

}

}

_delay_ms(500);

LED_vTurnOn('D',2);

LED_vTurnOff('D',1);

LCD_clearscreen();

LCD_vSend_string("remaining  7 sec");

while(counter_red>0)

{

if(counter1>=100)

{

counter1=0;

counter_red--;

LCD_movecursor(1,11);

LCD_vSend_char(' ');

LCD_vSend_char((counter_red%10)+48);

}

}

_delay_ms(500);

LED_vTurnOff('D',2);

}

         

 }



ISR(TIMER0_COMP_vect)

{

counter1++;

}

Timer code: 

/*

 * timer.c

 *

 * Created: 9/25/2019 7:22:07 PM

 *  Author: Mohamed Zaghlol

 */ 

 

#include "std_macros.h"

#include <avr/io.h>

#include <avr/interrupt.h>

void timer_CTC_init_interrupt(void)

{

/* select CTC mode*/

SET_BIT(TCCR0,WGM01);

/* load a value in OCR0 */

OCR0=78;

/* select timer clock */

SET_BIT(TCCR0,CS00);

SET_BIT(TCCR0,CS02);

/* enable interrupt*/

sei();

SET_BIT(TIMSK,OCIE0);

}



void timer_wave_nonPWM(void)

{

/* set OC0 as output pin */

SET_BIT(DDRB,3);

/* select CTC mode*/

SET_BIT(TCCR0,WGM01);

/* load a value in OCR0 */

OCR0=64;

/* select timer clock */

SET_BIT(TCCR0,CS00);

SET_BIT(TCCR0,CS02);

/* toggle OC0 on compare match*/

SET_BIT(TCCR0,COM00);

}


void timer_wave_fastPWM(void)

{

/* set OC0 as output pin */

SET_BIT(DDRB,3);

/* select fast PWM mode*/

SET_BIT(TCCR0,WGM00);

SET_BIT(TCCR0,WGM01);

/* load a value in OCR0 */

OCR0=64;

/* select timer clock */

SET_BIT(TCCR0,CS00);

SET_BIT(TCCR0,CS02);

/* Set OC0 on compare match, clear OC0 at BOTTOM,(inverting mode)*/

SET_BIT(TCCR0,COM00);

SET_BIT(TCCR0,COM01);

}



void timer_wave_phasecorrectPWM(void)

{

/* set OC0 as output pin */

SET_BIT(DDRB,3);

/* select phase correct PWM mode*/

SET_BIT(TCCR0,WGM00);

/* load a value in OCR0 */

OCR0=64;

/* select timer clock */

SET_BIT(TCCR0,CS00);

SET_BIT(TCCR0,CS02);

/* Set OC0 on compare match when up-counting. Clear OC0 on compare match when down counting.*/

SET_BIT(TCCR0,COM00);

SET_BIT(TCCR0,COM01);

}


Led code:


/*
* LED.c
*
* Created: 2/9/2018 7:16:44 PM
*  Author: Mohamed Zaghlol
*/
#include "DIO.h"
void LED_vInit(unsigned char portname,unsigned char pinnumber)
{
DIO_vsetPINDir(portname,pinnumber,1);//Set the given pin in the given port as an output
}
void LED_vTurnOn(unsigned char portname,unsigned char pinnumber)
{
DIO_write(portname,pinnumber,1);//Set the given pin in the given port to one(on)
}
void LED_vTurnOff(unsigned char portname,unsigned char pinnumber)
{
DIO_write(portname,pinnumber,0);//Set the given pin in the given port to zero(off)
}
void LED_vToggle(unsigned char portname,unsigned char pinnumber)
{
DIO_toggle(portname,pinnumber);//Set the given pin in the given port to zero if it is one or set it to one if it is zero
}



LCD code: 



/*
 * LCD.c
 *
 * Created: 2/23/2018 4:38:26 PM
 *  Author: Mohamed Zaghlol
 */ 
#include "LCD.h"
#define  F_CPU 8000000UL 
#include <util/delay.h>

void LCD_vInit(void)
{
_delay_ms(200);
#if defined eight_bits_mode
DIO_vsetPINDir('A',0,1);
DIO_vsetPINDir('A',1,1);
DIO_vsetPINDir('A',2,1);
DIO_vsetPINDir('A',3,1);
DIO_vsetPINDir('A',4,1);
DIO_vsetPINDir('A',5,1);
DIO_vsetPINDir('A',6,1);
DIO_vsetPINDir('A',7,1);
DIO_vsetPINDir('B',EN,1);
DIO_vsetPINDir('B',RW,1);
DIO_vsetPINDir('B',RS,1);
DIO_write('B',RW,0);
LCD_vSend_cmd(EIGHT_BITS); //8 bit mode
_delay_ms(1);
LCD_vSend_cmd(CURSOR_ON_DISPLAN_ON);//display on cursor on
_delay_ms(1);
LCD_vSend_cmd(CLR_SCREEN);//clear the screen
_delay_ms(10);
LCD_vSend_cmd(ENTRY_MODE); //entry mode
_delay_ms(1); 
#elif defined four_bits_mode
DIO_vsetPINDir('A',4,1);
DIO_vsetPINDir('A',5,1);
DIO_vsetPINDir('A',6,1);
DIO_vsetPINDir('A',7,1);
DIO_vsetPINDir('B',EN,1);
DIO_vsetPINDir('B',RW,1);
DIO_vsetPINDir('B',RS,1);
    DIO_write('B',RW,0);
LCD_vSend_cmd(RETURN_HOME); //return home
_delay_ms(10);
LCD_vSend_cmd(FOUR_BITS); //4bit mode
_delay_ms(1);
LCD_vSend_cmd(CURSOR_ON_DISPLAN_ON);//display on cursor on
_delay_ms(1);
LCD_vSend_cmd(CLR_SCREEN);//clear the screen
_delay_ms(10);
LCD_vSend_cmd(ENTRY_MODE); //entry mode
_delay_ms(1);
#endif
}


static void send_falling_edge(void)
{
DIO_write('B',EN,1);
_delay_ms(2);
DIO_write('B',EN,0);
_delay_ms(2);
}
void LCD_vSend_cmd(char cmd)
{
#if defined eight_bits_mode
DIO_write_port('A',cmd);
DIO_write('B',RS,0);
send_falling_edge();
#elif defined four_bits_mode
write_high_nibble('A',cmd>>4);
DIO_write('B',RS,0);
send_falling_edge();
write_high_nibble('A',cmd);
DIO_write('B',RS,0);
send_falling_edge();
#endif
_delay_ms(1);
}

void LCD_vSend_char(char data)
{
#if defined eight_bits_mode
DIO_write_port('A',data);
DIO_write('B',RS,1);
send_falling_edge();
#elif defined four_bits_mode
write_high_nibble('A',data>>4);
DIO_write('B',RS,1);
send_falling_edge();
write_high_nibble('A',data);
DIO_write('B',RS,1);
send_falling_edge();
#endif
_delay_ms(1);
}


void LCD_vSend_string(char *data)
{
while((*data)!='\0')
{
LCD_vSend_char(*data);
data++;
}
}
void LCD_clearscreen()
{
LCD_vSend_cmd(CLR_SCREEN);
_delay_ms(10);
}
void LCD_movecursor(char row,char coloumn)
{
char data ;
if(row>2||row<1||coloumn>16||coloumn<1)
{
data=0x80;
}
else if(row==1)
{
data=0x80+coloumn-1 ;
}
else if (row==2)
{
data=0xc0+coloumn-1;
}
LCD_vSend_cmd(data);
_delay_ms(1);
}

Dio code:



#include <avr/io.h>
#include "std_macros.h"
void DIO_vsetPINDir(unsigned char portname,unsigned char pinnumber,unsigned char direction)
{
switch(portname)
{
case 'A':
if(direction==1)
{
SET_BIT(DDRA,pinnumber);//Set the direction of the given pin in port A as output
}
else
{
CLR_BIT(DDRA,pinnumber);//Set the direction of the given pin in port A as input
}
break;
case 'B':
if(direction==1)
{
SET_BIT(DDRB,pinnumber);//Set the direction of the given pin in port B as output
}
else
{
CLR_BIT(DDRB,pinnumber);//Set the direction of the given pin in port B as input
}
break;
case 'C':
if(direction==1)
{
SET_BIT(DDRC,pinnumber);//Set the direction of the given pin in port C as output
}
else
{
CLR_BIT(DDRC,pinnumber);//Set the direction of the given pin in port C as input
}
break;
case 'D':
if(direction==1)
{
SET_BIT(DDRD,pinnumber);//Set the direction of the given pin in port D as output
}
else
{
CLR_BIT(DDRD,pinnumber);//Set the direction of the given pin in port D as input
}
break;
default: 
break;
}
}


void DIO_write(unsigned char portname,unsigned char pinnumber,unsigned char outputvalue)
{
switch(portname)
{
case 'A' :
if(outputvalue==1)
{
SET_BIT(PORTA,pinnumber);//Set the value of the given pin in port A as High
}
else
{
CLR_BIT(PORTA,pinnumber);//Set the value of the given pin in port A as Low
}
break ;
case 'B':
if(outputvalue==1)
{
SET_BIT(PORTB,pinnumber);//Set the value of the given pin in port B as High
}
else
{
CLR_BIT(PORTB,pinnumber);//Set the value of the given pin in port B as Low
}
break ;
case 'C' :
if(outputvalue==1)
{
SET_BIT(PORTC,pinnumber);//Set the value of the given pin in port C as High
}
else
{
CLR_BIT(PORTC,pinnumber);//Set the value of the given pin in port C as Low
}
break ;
case 'D':
if(outputvalue==1)
{
SET_BIT(PORTD,pinnumber);//Set the value of the given pin in port D as High
}
else
{
CLR_BIT(PORTD,pinnumber);//Set the value of the given pin in port D as Low
}
break ;
default: break ;
}
}


unsigned char DIO_u8read(unsigned char portname,unsigned char pinnumber)
{
unsigned char return_value=0;
switch(portname)
{
case 'A' :
return_value=READ_BIT(PINA,pinnumber);//Read the value from the given pin in port A
break;
case 'B' :
return_value=READ_BIT(PINB,pinnumber);//Read the value from the given pin in port B
break;
case 'C' :
return_value=READ_BIT(PINC,pinnumber);//Read the value from the given pin in port C
break;
case 'D' :
return_value=READ_BIT(PIND,pinnumber);//Read the value from the given pin in port D
break;
default: 
break;
}
return return_value ;
}
void DIO_toggle(unsigned char portname,unsigned char pinnumber)
{
switch(portname)
{
case 'A':
TOG_BIT(PORTA,pinnumber);//Toggle the value of the given pin in port A
break;
case 'B':
TOG_BIT(PORTB,pinnumber);//Toggle the value of the given pin in port B
break;
case 'C':
TOG_BIT(PORTC,pinnumber);//Toggle the value of the given pin in port C
break;
case 'D':
TOG_BIT(PORTD,pinnumber);//Toggle the value of the given pin in port D
break;
default: break;
}
}
void DIO_set_port_direction(unsigned char portname,unsigned char direction)
{
switch(portname)
{
case 'A' :
DDRA=direction; //set the direction of port A
break ;
case 'B':
DDRB=direction; //set the direction of port B
break ;
case 'C' :
DDRC=direction; //set the direction of port C
break ;
case 'D':
DDRD=direction; //set the direction of port D
break ;
default:
break ;
}
}
void DIO_write_port(unsigned char portname,unsigned char portvalue)
{
switch(portname)
{
case 'A' :
PORTA=portvalue; //Write the given value to the port A
break ;
case 'B':
PORTB=portvalue; //Write the given value to the port B
break ;
case 'C' :
PORTC=portvalue; //Write the given value to the port C
break ;
case 'D':
PORTD=portvalue; //Write the given value to the port D
break ;
default:
break ;
}
}

unsigned char DIO_read_port(unsigned char portname)
{
unsigned char return_val=0;
switch(portname)
{
case 'A' :
return_val=PINA; // read the value of port A 
break ;
case 'B':
return_val=PINB; // read the value of port B 
break ;
case 'C' :
return_val=PINC; // read the value of port C 
break ;
case 'D':
return_val=PIND; // read the value of port D 
break ;
default:
break ;
}
return return_val;
}

void DIO_vconnectpullup(char portname ,char pinnumber, char connect_pullup)
{
switch(portname)
{
case 'A':
if(connect_pullup==1)
{
SET_BIT(PORTA,pinnumber);
}
else
{
CLR_BIT(PORTA,pinnumber);
}
break;
case 'B':
if(connect_pullup==1)
{
SET_BIT(PORTB,pinnumber);
}
else
{
CLR_BIT(PORTB,pinnumber);
}
break;
case 'C':
if(connect_pullup==1)
{
SET_BIT(PORTC,pinnumber);
}
else
{
CLR_BIT(PORTC,pinnumber);
}
break;
case 'D':
if(connect_pullup==1)
{
SET_BIT(PORTD,pinnumber);
}
else
{
CLR_BIT(PORTD,pinnumber);
}
break;
}
}

void write_low_nibble(unsigned char portname,unsigned char value)
{
value&=0x0f;
switch(portname)
{
case 'A':
PORTA&=0xf0;
PORTA|=value;
case 'B':
PORTB&=0xf0;
PORTB|=value;
case 'C':
PORTC&=0xf0;
PORTC|=value;
case 'D':
PORTD&=0xf0;
PORTD|=value;
}
}

void write_high_nibble(unsigned char portname,unsigned char value)
{
value<<=4;
switch(portname)
{
case 'A':
PORTA&=0x0f;
PORTA|=value;
case 'B':
PORTB&=0x0f;
PORTB|=value;
case 'C':
PORTC&=0x0f;
PORTC|=value;
case 'D':
PORTD&=0x0f;
PORTD|=value;
}
}


Commentaires

Posts les plus consultés de ce blog

Digital clock " DS1307 RTC" with PIC16F877A microcontroller and I2C

Smart Elevator simulation with arduino uno and Proteus

Temperature sensor with PIC16F877a using Analog to Digital convertion.