Spradecom
Joined: 01 Jun 2011 Posts: 19 Location: INDIA
|
Problem In EEPROM writing |
Posted: Wed Jun 01, 2011 7:07 am |
|
|
I am writing code for internal EEPROM using 16F684 for keypad interface and generate PWM, and store last PWM value in EEPROM, but value is not stored. Can anyone help to solve my problem.
Code for program:
Code: |
#rom 0x2100={1}
#DEFINE LAST_DATA 1
signed int16 duty;
signed int8 count;
signed int16 temp_duty;
void key_check(void);
void save_last_data(void);
//---------------------------- Write Duty --------------------------------------
void write_int_to_eeprom(unsigned int8 adress_to_write,signed int16 data_to_write);
signed int16 read_int_from_eeprom(unsigned int8 adress_to_read);
//------------------------------------------------------------------------------
void main()
{
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DIV_BY_16,249,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
setup_oscillator(OSC_4MHZ);
setup_ccp1(CCP_PWM);
set_pwm1_duty(0);
output_low(PIN_C4);
count=0;
duty=2;
write_int_to_eeprom(LAST_DATA,duty);
delay_ms(2);
temp_duty=read_int_from_eeprom(LAST_DATA);
duty=temp_duty;
while(TRUE)
{
set_pwm1_duty(duty);
key_check();
}
}
//------------------------------------------------------------------------------
void write_int_to_eeprom(unsigned int8 adress_to_write,signed int16 data_to_write)
{
unsigned int8 array[2];
array[0] = (unsigned int8)(data_to_write & 0x00FF);
array[1] = (unsigned int8)((data_to_write >> 8) & 0x00FF);
write_eeprom(adress_to_write,array[0]);
adress_to_write ++;
write_eeprom(adress_to_write,array[1]);
}
//------------------------------------------------------------------------------
signed int16 read_int_from_eeprom(unsigned int8 adress_to_read)
{
unsigned int8 array[2];
unsigned int16 readed_data;
array[0] = read_eeprom(adress_to_read);
adress_to_read ++;
array[1] = read_eeprom(adress_to_read);
readed_data = array[1];
readed_data <<= 8;
readed_data &= 0xFF00;
readed_data |= array[0];
return(readed_data);
}
//------------------------------------------------------------------------------
void key_check()
{
if(!input (PIN_C2))
{
delay_ms(30);
if(!input (PIN_C2))
{
if(count==0)
{
count=1;
duty=temp_duty;
set_pwm1_duty(duty);
delay_ms(2);
write_int_to_eeprom(LAST_DATA,duty);
delay_ms(2);
while(!input (PIN_C2));
}
else
{
count=0;
duty=0;
while(!input (PIN_C2));
}
}
}
//------------------------------------------------------------------------------
if(!input (PIN_A1))
{
delay_ms(30);
if(!input (PIN_A1))
{
duty=temp_duty;
if(duty < 1012)
{
duty += 10;
}
else
{
duty=1023;
}
temp_duty=duty;
delay_ms(2);
write_int_to_eeprom(LAST_DATA,temp_duty);
delay_ms(2);
}
}
//------------------------------------------------------------------------------
if(!input (PIN_A0))
{
delay_ms(30);
if(!input (PIN_A0))
{
duty=temp_duty;
if(duty > 12)
{
duty -= 10;
}
else
{
duty=2;
}
temp_duty=duty;
delay_ms(2);
write_int_to_eeprom(LAST_DATA,temp_duty);
delay_ms(2);
}
}
} |
|
|