Hello,
I am working on a project that is using a PIC12F1571 to run the operation. The goal is to have the microcontroller store the LED state in memory and read it.
At start up, the default state is set and the microcontroller goes asleep. When the buttom is pressed, the state is read, the LEDs are turned on, the state is incremented and stored back into memory. This is repeated until it hits the last address and then it resets back to the default state. If the button is held, the microcontroller is put back to sleep.
Here are the various functions to read/write and update the state.
When I debug, it looks like the update state does read and increment but when it write the incremented state, it doesn't stick. So it is always reading the default state over and over again and not getting to the next state.
Thoughts?
I am working on a project that is using a PIC12F1571 to run the operation. The goal is to have the microcontroller store the LED state in memory and read it.
At start up, the default state is set and the microcontroller goes asleep. When the buttom is pressed, the state is read, the LEDs are turned on, the state is incremented and stored back into memory. This is repeated until it hits the last address and then it resets back to the default state. If the button is held, the microcontroller is put back to sleep.
Here are the various functions to read/write and update the state.
C:
void updateStateMachine(void){
uint16_t machineState;
readLEDMemory();
machineState = PMDATH;
machineState = (machineState << 8) | PMDATL;
//Read the state address
updateLEDs(LEDSOFF);
updateLEDs(machineState);
machineState = machineState + 0x0001;
if(machineState == 0x00BD){
machineState == BLUE;
}
writeLEDMemory(machineState);
}
void readLEDMemory(){
uint8_t tempVal;
PMCON1bits.CFGS = 0;
tempVal = LEDSTATELOCATION;
PMADRL = tempVal;
tempVal = (LEDSTATELOCATION >> 8);
//tempVal = tempVal & 0x7F;
PMADRH = tempVal;
PMCON1bits.RD = 1;
NOP();
NOP();
}
void writeLEDMemory(uint16_t stateVal){
INTCONbits.GIE = 0;
eraseFlash();
PMCON1bits.CFGS = 0;
uint8_t tempVal;
tempVal = LEDSTATELOCATION;
PMADRL = tempVal;
tempVal = (LEDSTATELOCATION >> 8);
PMADRH = tempVal;
PMCON1bits.FREE = 0;
PMCON1bits.LWLO = 1;
PMCON1bits.WREN = 1;
tempVal = stateVal;
PMDATL = tempVal;
tempVal = (stateVal >> 8);
PMDATH = tempVal;
PMCON1bits.LWLO = 0;
writeUnlockSequence();
PMCON1bits.WREN = 0;
INTCONbits.GIE = 1;
}
void eraseFlash(){
PMCON1bits.CFGS = 0;
uint8_t tempVal;
tempVal = LEDSTATELOCATION;
PMADRL = tempVal;
tempVal = (LEDSTATELOCATION >> 8);
PMADRH = tempVal;
PMCON1bits.FREE = 1;
PMCON1bits.WREN = 1;
writeUnlockSequence();
PMCON1bits.WREN = 0;
}
void writeUnlockSequence(void){
PMCON2 = 0x55;
PMCON2 = 0xAA;
PMCON1bits.WR = 1;
NOP();
NOP();
}
Thoughts?
Last edited by a moderator: