Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Problem with Internal EEPROM data accessing of Atmega8

Status
Not open for further replies.

hardik.patel

Full Member level 1
Joined
Aug 15, 2012
Messages
95
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Activity points
2,021
I am trying to wireless device control using atmega8 n i need to store status of all device status on every update in internal EEPROM.

If i remove part of EEPROM then the manual control of device is perfect...device is toggling. But i need to add facility of EEPROM, so status before power off, will be as it is after power on again.

Somehow, The status is not saved in EEPROM.
Cant understand wheather that writing problem or reading problem...


Compiler - AVR GCC
Xtal- 7.3728Mhz

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

hardik.patel

Full Member level 1
Joined
Aug 15, 2012
Messages
95
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Activity points
2,021
Both files are in above code..see at bottom side.
N btw i had solved this problem.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top