PDA

View Full Version : 12F683 PWM output not remembering.



pescador
- 26th April 2018, 01:07
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

mpgmike
- 26th April 2018, 05:56
Where have you given EE_Duty a value? Instead of "EE_Duty VAR WORD" try using "EE_Duty CON 0" where you are using EEPROM Address 0 as a storage place for your Duty.

pescador
- 26th April 2018, 13:28
I will try -thankyou...

pescador
- 28th April 2018, 02:22
Mixed results on that help. it doesn't remember every time I cycle power - 1 out 10 times it fails to remember

mpgmike
- 28th April 2018, 14:46
I use the EEPROM Address in my commands. For example, "READ 0, Duty" and "WRITE 0, Duty". Writing to the EEPROM takes an eternity in computer time.

Another method might be to use a rather large capacitor for the PIC power supply so that it takes a couple hundred milliseconds for it to drop below threshold Vdd voltage. Use an input to monitor when the main switch is turned off. Write to the EEPROM only when the main switch powers down. EEPROMs have a limited number of writes, usually rated around 100,000 times. If you are writing to the EEPROM every time your value changes, you could be using up those 100,000 Writes in a relatively short product lifetime. I use a 2200 uF cap in one of my projects to ballast noise. There is sufficient Vdd for over a second after I power down, ample time to write to the EEPROM.

Art
- 28th April 2018, 18:22
Yes that code is going to kill the EEPROM location quickly.
You need to write only every “so many” loops.

You could try turning brownout detect on, to ensure you have enough current because EEPROM writing is the most power hungry thing the pic can do,
and if you’re on the edge, that’s when it will fail.
and also check the EECON1 register after the write to verify the self timing worked, and the write actually completed (if not, try again).

sayzer
- 1st May 2018, 08:22
1. When the user pressed button, there is no button pause or bounce. Before the user releases button, duty changes thousands of time So give it some delay like 250ms so .
1. For eeprom safety, have a loop scanning through eeprom locations; Everytime write to a different address.
3. For powerloss detection, see the circuit sample.

Note: 12F683 already has onboard PWM. Why use soft pwm?