#include <avr/io.h>
#include <inttypes.h>
#include <util/delay.h>
#include<util/delay.h>
#include "eeprom.h"
#include "eeprom.c"
#define tbi(x,y) x ^= _BV(y) //toggle bit - using bitwise XOR operator
char data;
char a,b,c,d,e,f=0;
void USARTInit(uint16_t ubrr_value)
{
UBRRL = ubrr_value;
UBRRH = (ubrr_value>>8);
UCSRC=(1<<URSEL)|(3<<UCSZ0);
UCSRB=(1<<RXEN)|(1<<TXEN);
}
char USARTReadChar()
{
while(!(UCSRA & (1<<RXC)))
{
//Do nothing
}
return UDR;
}
void USARTWriteChar(char data)
{
while(!(UCSRA & (1<<UDRE)))
{
//Do nothing
}
UDR=data;
}
void b4_power()
{
PORTC=EEPROMRead(0);
PORTC=EEPROMRead(2);
PORTC=EEPROMRead(3);
PORTC=EEPROMRead(4);
PORTC=EEPROMRead(5);
PORTB=EEPROMRead(6);
PORTB=EEPROMRead(7);
PORTB=EEPROMRead(8);
}
void compare()
{
int flag=0;
int d0,d1,d2,d3,d4,d5,d6,d7,d8;
b4_power();
while(1)
{
data=USARTReadChar(); a=data; USARTWriteChar(a);//_delay_ms(5);
data=USARTReadChar(); b=data; USARTWriteChar(b);//_delay_ms(5);
data=USARTReadChar(); c=data; USARTWriteChar(c);//_delay_ms(5);
data=USARTReadChar(); d=data; USARTWriteChar(d);//_delay_ms(5);
data=USARTReadChar(); e=data; USARTWriteChar(e);//_delay_ms(5);
if((a=='8') && (flag==0))
{
flag=1;
}
if((b=='0') && (flag==1))
{
flag=2;
}
if((c=='2') && (flag==2))
{
flag=3;
}
if((d=='1') && (flag==3))
{
flag=4;//PORTC=0x01;
}
if((flag==4) && (e=='A'))
{
tbi(PORTC,PC0); //here the toggling takes place
d0 = bit_is_set(PINC,0); //status of PC0 wheather CLEARED/SET is stored in d0
EEPROMWrite(0,d0); // That status of d0 will be store at 0th EEPROM Location
}
if((flag==4) && (e=='B'))
{
tbi(PORTC,PC1);
d1 = bit_is_set(PINC,1);
EEPROMWrite(2,d1);
}
if((flag==4) && (e=='C'))
{
tbi(PORTC,PC2);
d2 = bit_is_set(PINC,2);
EEPROMWrite(3,d2);
}
if((flag==4) && (e=='D'))
{
tbi(PORTC,PC3);
d3 = bit_is_set(PINC,3);
EEPROMWrite(4,d3);
}
if((flag==4) && (e=='E'))
{
tbi(PORTC,PC4);
d4 = bit_is_set(PINC,4);
EEPROMWrite(5,d4);
}
if((flag==4) && (e=='F'))
{
tbi(PORTC,PC5);
d5 = bit_is_set(PINC,5);
EEPROMWrite(6,d5);
}
if((flag==4) && (e=='G'))
{
tbi(PORTB,PB0);
d6 = bit_is_set(PINB,0);
EEPROMWrite(7,d6);
}
if((flag==4) && (e=='H'))
{
tbi(PORTB,PB1);
d7 = bit_is_set(PINB,1);
EEPROMWrite(8,d7);
}
if((flag==4) && (e=='I'))
{
tbi(PORTB,PB2);
d8 = bit_is_set(PINB,2);
EEPROMWrite(8,d8);
}
}
}
void main()
{
USARTInit(47); //UBRR = 51
DDRC=0xFF;
DDRD=0x00; // For RXD input
DDRB=0xFF;
//PORTB=0x00; //For Leds
EEPROMWrite(0,0xFF);
PORTC=0b111111;PORTB=0xFF; _delay_ms(400); PORTC=0b000000;PORTB=0x00;
//Loop forever
while(1)
{
//b4_power(); //delay_ms(400);
//PORTC=0x00;
compare();
//flag=0;a,b,c,d,e,f=0;
//data=USARTReadChar();
//USARTWriteChar(data);//while(1);
}
}
//while(1)
//{
////DDRC=0xFF;
// //PORTC=0x00; _delay_ms(150); PORTC=0xFF;
////Read data
//
//
// //USARTWriteChar('{');
// USARTWriteChar(data);
// //USARTWriteChar('}');
//// }
//}
************************eeprom.c*************
#include <avr/io.h>
#include <inttypes.h>
#include "eeprom.h"
uint8_t EEPROMRead(uint16_t uiAddress)
{
/* Wait for completion of previous write */
while(EECR & (1<<EEWE));
/* Set up address register */
EEAR = uiAddress;
/* Start eeprom read by writing EERE */
EECR |= (1<<EERE);
/* Return data from data register */
return EEDR;
}
void EEPROMWrite(uint16_t uiAddress, uint8_t ucData)
{
/* Wait for completion of previous write */
while(EECR & (1<<EEWE));
/* Set up address and data registers */
EEAR = uiAddress;
EEDR = ucData;
/* Write logical one to EEMWE */
EECR |= (1<<EEMWE);
/* Start eeprom write by setting EEWE */
EECR |= (1<<EEWE);
}
****************eeprom.h***********
#ifndef EEPROM_H
#define EEPROM_H
#include <inttypes.h>
void EEPROMWrite(uint16_t uiAddress, uint8_t ucData);
uint8_t EEPROMRead(uint16_t uiAddress);
#endif