Hi all I am using the 16F684 to control a stepper motor in an application where it needs to remember it's position and direction when the power is turned off. This information is read on power-on, and if it is not valid data, the operator is signaled to manually reset the mechanism to 0. I am having a terrible time trying to get the eeprom code to work and would really appreciate some help. The project is for one of my kids, who is making an unusual electro-mechanical device. Thanks in advanced. Here is my code; #include #include #pragma DATA 0x2007, _WDT_OFF & _BOD_OFF & _INTOSCIO & _PWRTE_OFF & _MCLRE_OFF #pragma CLOCK_FREQ 4000000 #define x_error porta.2 unsigned char count; //for tracking rolling count in the interrupt routine unsigned char data_ok, auto_cw; //character for storing position data_ok flag and direction int max_position, cur_position; //16 bit integers for tracking position unsigned char position_high, position_low; //for storing high and low bytes of cur_position void write(char data, char address); char read(char address); void main(void) { //initialize PIC //read variables stored previously auto_cw = read(0); //retrieve position, count, direction and data_ok flag position_low = read(1); position_high = read(2); count = read(4); data_ok = read(3); //data_ok is a flag to detect if power was interrupted //during normal power down, a specific value is stored cur_position = position_high<<8; //reconstruct 16 bit cur_position from 8 bit words cur_position |= position_low; if(data_ok!=0xF0) //check flag & if data is not valid, then { x_error = 1; //flag operator - turn on fault led cur_position=0; //reset variables auto_cw=1; x_error = 0; //turn off fault led } pie1 = 0; //disable interrupt during eeprom write data_ok=0; //store 0 in data_ok so that if power is interupted, write(data_ok,3); //it will be detected on power up. pie1 = 0b00000001; //enable timer1 overflow interrupt while(1) //main loop { //do the main loop motor control, until operator initiates power off sequence if (test_bit(porta,0)) //test for pwr_off { intcon = 0b00000000; //disable interrupts during eeprom write //store count, auto_cw and cur_position data_ok = 0xF0; //store data_ok flag to indicate valid position position_low = cur_position & 0x00FF; position_high = cur_position>>8; write(auto_cw, 0); //write position and direction data, and data_ok flag write(position_low,1); write(position_high,2); write(count,4); write(data_ok,3); while (test_bit(porta,0)); //then wait here until power goes off, or pwr_off input changes intcon = 0b11000000; //if power goes back on, enable interrupts } } } void write(char data, char address) //writes 8 bit data to address in eeprom { eedata = data; eeadr = address; eecon1 = 0b00000100; eecon2 = 0x55; eecon2 = 0xAA; eecon1 = 0b00000110; while(test_bit(eecon1,1)); //wait for write to complete eecon1 = 0; pir1 =0; //clear interrupt flages - PIR1<7> } char read(char address) //reads 8 bit data from address in eeprom { eeadr = address; eecon1 = 0b00000001; pir1 = 0; return eedata; } -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist