I have a circuit using a 12F683. It uses a PWM output that controls LED brightness. When the user turns off the circuit and turns it back on - it needs to remain at the same brightness as when was turned off. The problem is it doesn't always save the last setting.
Can someone look at my code and evaluate? I can provide diagram of circuit - but I tihnk the code is my culprit.
thanks everyone..
#CONFIG
cfg = _INTOSCIO
cfg&= _WDT_ON
cfg&= _PWRTE_OFF
cfg&= _MCLRE_OFF
cfg&= _CP_OFF
cfg&= _BOD_ON
__CONFIG cfg
#ENDCONFIG
' ======= Common Settings =================================
'OSCCON = %01110001 ' Internal 8MHz osc.
'DEFINE OSC 4
CMCON0 = 7
ANSEL = 0
OUTPUT GPIO.1
INPUT GPIO.4
PLEDOUT var GPIO.2 ' PWM output
BUTTEN var GPIO.4 ' BUTTON IN
HERTZ VAR byte
DUTY VAR byte
EE_DUTY var word
HERTZ = 10
pause 500
read EE_Duty, Duty ' read from EEPROM address EE_Duty to Duty variable
if Duty > 128 then Duty = 128 ' limit the duty to 128 if it is > 128
MAIN:
pwm pledout,duty,HERTZ ' PLM output
IF BUTTEN = 0 THEN ' is button pushed?
DUTY = DUTY - 1 ' decrement duty cycle by 1
IF DUTY < 5 THEN DUTY = 115 ' prevents all led power off
write EE_Duty, Duty ' save the new duty to eeprom
ENDIF
write EE_Duty, Duty ' save the new duty to eeprom
GOTO MAIN
END
Bookmarks