12F683 PWM output not remembering.


Closed Thread
Results 1 to 7 of 7
  1. #1
    Join Date
    Apr 2016
    Location
    Plainfield, Illinois
    Posts
    73

    Default 12F683 PWM output not remembering.

    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
    Last edited by pescador; - 26th April 2018 at 01:22.
    My dad never liked you...

  2. #2
    Join Date
    Apr 2014
    Location
    OK
    Posts
    557


    Did you find this post helpful? Yes | No

    Default Re: 12F683 PWM output not remembering.

    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.

  3. #3
    Join Date
    Apr 2016
    Location
    Plainfield, Illinois
    Posts
    73


    Did you find this post helpful? Yes | No

    Default Re: 12F683 PWM output not remembering.

    I will try -thankyou...
    My dad never liked you...

  4. #4
    Join Date
    Apr 2016
    Location
    Plainfield, Illinois
    Posts
    73


    Did you find this post helpful? Yes | No

    Default Re: 12F683 PWM output not remembering.

    Mixed results on that help. it doesn't remember every time I cycle power - 1 out 10 times it fails to remember
    My dad never liked you...

  5. #5
    Join Date
    Apr 2014
    Location
    OK
    Posts
    557


    Did you find this post helpful? Yes | No

    Default Re: 12F683 PWM output not remembering.

    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.

  6. #6
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default Re: 12F683 PWM output not remembering.

    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).

  7. #7
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default Re: 12F683 PWM output not remembering.

    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?
    Attached Images Attached Images  
    Last edited by sayzer; - 1st May 2018 at 08:27.
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

Similar Threads

  1. 12F683 2 x PWM Outputs
    By retepsnikrep in forum mel PIC BASIC Pro
    Replies: 29
    Last Post: - 23rd July 2021, 20:20
  2. PWM using the 12F683
    By pescador in forum General
    Replies: 6
    Last Post: - 26th April 2016, 01:13
  3. 12F683 Logic Output Problem?
    By shockwave in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 25th February 2012, 19:48
  4. What's the best way to output 30 to 40 kHz from a 12F683 pin?
    By fizyxman in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 1st June 2009, 00:08
  5. 12f683 comparator and pwm
    By Automan in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 23rd May 2007, 18:05

Members who have read this thread : 0

You do not have permission to view the list of names.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts