Menu

ReadAD10(AN3) for PIC12F683 not work.

Help
2015-04-20
2015-04-27
1 2 > >> (Page 1 of 2)
  • BASIL HATZILAIOS

    Hi,
    I use a PIC12F683 to control the current of a led string. The current controlled by a mosfet with a resistor 0.22Ω at source and 0V.The gate of the mosfet goes to GPO.2, while the source of the mosfet goes to GPO.4(AN3).The pin GPO.3 goes HIGH with a switch to control the intensity of leds. The max current must be about 1A , so the voltage at sense resistor must not exceed 0.22V.I make a test to see if ReadAD10 for GPO.4(AN3) works. in the code, i give a value 45 to var.CL as current limit. I noticed something strange at the test. When the led current surpass 1A , the GOTO not worked,but when i pressed the switch the GOTO worked.However, GPO.3 pin not support ADC input,and the current sense voltage is connected at GPO.4(AN3) pin.
    I send you part of code for your opinion.Is there anything wrong with the code or chip config , and how can I use this ADC pin to limit the led current.
    Chip Settings

    ~~~~
    #chip 12F683,4
    #config CPD=OFF, CP=OFF, MCLRE=OFF, WDT=OFF, OSC=INTRC_OSC_NOCLKOUT

    ;Variables
    Dim CL As word
    
    Dir GPIO.5 Out
    Dir GPIO.4 In
    Dir GPIO.3 In
    Dir GPIO.0 Out
    Dir GPIO.2 Out
    Dir GPIO.1 Out
    Main:
    CL = ReadAD10(AN3)
    Wait 1 ms
    SW1:
    If GPIO.3=1 Then
        Wait 400 ms
        WL_ON50:
        HPWM 1, 20, 20
    Else
        Goto SW1
    End If
    If GPIO.3=1 Then
        Wait 400 ms
        Goto WL_ON75
    Else
        Goto WL_ON50
    End If
    WL_ON75:
    HPWM 1, 20, 45
    If GPIO.3=1 Then
        Wait 400 ms
        Goto WL_ON100
    Else
        Goto WL_ON75
    End If
    WL_ON100:
    HPWM 1, 20, 80
    Wait 100 ms
    If GPIO.3=1 Then
        PWMOff
        Wait 400 ms
        Goto WL_ON100B
    End If
    If CL > 45 Then
        Goto SW1
    End If
    Goto WL_ON100
    

    Many thanks in advance.
    BASIL

     

    Last edit: Anobium 2015-04-20
  • Anobium

    Anobium - 2015-04-20

    What version of GCB? I this the recent download?

    Thank you.

     
  • Anobium

    Anobium - 2015-04-20
     
    • BASIL HATZILAIOS

      The version is 1.0 , 23/6/12 . Is the latest version ?
      I used the pwm code but no change.I think that must make some time improvements in the code.
      Many thanks Anobium.

       
  • kent_twt4

    kent_twt4 - 2015-04-20

    Just because I have written LED flashlight software... :).

    If it were me, I would stick to a hardware solution on limiting current to the led string, and scrap the READAD10.

    When using PWM, the current is max for each on cycle of the period, regardless of the number of cycles. In the current code form, too much time is wasted in monitoring the switch time and blocking the ReadAD10(AN3).

    A huge time improvement could be had by using the GPIOChange interrupt and advance a counter variable to change the PWM duty in the Main program (e.g. Select Case command). Enable the GPIO weak pullup for the switch pin, or install an external one if MCLR pin is used. Adding a 0.1uf cap usually debounce's the switch without resorting to software.

    A standard way of limiting current through a Nfet is using the sense resistor to saturate the base of an npn transistor at 0.7Vbe. So for a 1A max led current a sense resistor of 0.7ohm would be required. The collector of the npn would be tied to the gate of the Nfet/CCP1 and the emitter to ground. Lots of schematics out there to look at.

    Reducing the HPWM frequency could lead to an increase in LED brightness.

     
    • BASIL HATZILAIOS

      Many thanks Kent_twt4, for your time. I know this way of current limit, but because I have 54 leds (type2835, 18 strings of 3 in parallel) in relatively small pcb area, and the mosfet is at the same pcb side , I'm afraid that high temperature (leds must be work at upper limit for many hours) maybe lead to unstable current limit, or usually to reduced current. If you consider that the circuit must be work at wide input voltage (10-18V DC),the stable current is not an easy goal.So, I would prefer if it's possible a better current control with software.I'll appreciate if you give me your opinion about that, with some code samples.

       
  • kent_twt4

    kent_twt4 - 2015-04-21

    You mean 18 parallel strings of 3 in series? Based on the type 2835 led, then you would be looking at a max 60ma? They make linear CC led regulators, and I would be tempted to use those. An example would be for each series string (qty 18) of these type of devices http://www.mouser.com/ProductDetail/ON-Semiconductor/NSI45060JDT4G/?qs=wmFqszA%252be7N2hhXq6U%2FMMw%3D%3D Could be others like them, but easy to use, simple set and forget. Then PWM the supply with the Nfet.

    A much more involved way is to use an SPI or I2C CC PWM led drivers like the TLC5940 or the NXP PCA series. These could be 16 channels per device, so if 18 is absolutely needed then two chips would be needed. Lot more software involved, much steeper learning curve, but perhaps cheaper in the long run. I have used this approach before for individual led control.

    I like the idea of individual led string control. This could be due to not having tried multiple parallel and series strings of leds.

    I am unsure where and how current control and the software is linked without the aid of some analog type of device? By using PWM (i.e. software) and constant current (some analog device) the perceived brightness of the leds is adjusted up and down. That's how I think of it.

     
  • kent_twt4

    kent_twt4 - 2015-04-21

    And here is some code for a three level brightness led plus off with a momentary switch. Coding will be slightly different for 12f683:

    ~~~~

    chip 12f1822,8

    define SWITCH PORTA.3 'Momentary switch

    Dir SWITCH In
    Dir PortA.2 Out 'CCP1 or PWM
    Set NOT_WPUEN OFF 'Enable individual weak pullups
    Set WPUA3 on 'Enable weak pullup PortA.3
    SET IOCAN3 ON 'Waiting for negative going edge
    ON Interrupt PORTBCHANGE CALL ChangeMode 'momentary switch pressed

    HPWM 1,10,25 'Start led in low
    Mode = 1

    Main:
    wait 1 s
    goto Main

    SUB ChangeMode
    Set IOCAF3 OFF
    Mode += 1
    If Mode = 5 Then Mode = 1
    Select Case Mode
    Case 1
    HPWM 1, 10, 25
    Case 2
    HPWM 1, 10, 75
    Case 3
    HPWM 1, 10, 255
    Case 4
    HPWM 1, 10, 0
    end Select
    END SUB

     
  • BASIL HATZILAIOS

    Many thanks kent_twt4 for the info.I made a code test with PIC12F683, but only the level 1
    works.The led's intensity must start from off.The first level works,but with the second pressing of switch there is no change at intensity.Below is the code.Maybe there is something wrong.Your opinion?
    Thanks in advance.

    chip 12F683,8

    config OSC=INTRC_OSC_NOCLKOUT

    Dir GPIO.3 In
    Dir GPIO.4 In
    Dir GPIO.2 Out
    Set NOT_GPPU OFF
    Set INTEDG OFF
    Set IOC3 On
    ON Interrupt GPIOCHANGE CALL ChangeMode
    HPWM 1,10,0
    Mode = 4

    Main:
    wait 50 ms
    goto Main

    SUB ChangeMode
    Set IOC3 OFF
    Mode = Mode + 1
    if Mode= 5 Then Mode= 1
    Select Case Mode
    Case 1
    HPWM 1,10,20
    Case 2
    HPWM 1,10,40
    Case 3
    HPWM 1,10,70
    Case 4
    HPWM 1,10,0
    end Select
    END SUB

     
  • kent_twt4

    kent_twt4 - 2015-04-22

    A few things about the 12f683: 1) You can't set the edge for GPIO, only the INT pin, which happens to be the CCP1 pin. 2) The hardware debounce doesn't seem to be working to well for me like the 12f1822, so must rely on a software debounce routine. 3) No internal pullups for the MCLR pin like the 12f1822. Not sure if it makes any difference because I believe a stronger pullup is advisable.

    My momentary switch has a 0.1uf cap across the leads, and one side uses a 10k pullup to Vcc, the other to ground.

    I like to start off with the led on, so here goes for the 12f683:

    #chip 12F683,8
    #config OSC=INTRC_OSC_NOCLKOUT
    
    Dir GPIO.3 In
    Dir GPIO.2 Out
    Set IOC3 On
    ON Interrupt GPIOCHANGE CALL ChangeMode
    HPWM 1,10,20
    Mode = 1
    
    Main:
    nop
    goto Main
    
    SUB ChangeMode
    debounce = 0
    glitch = 0
    CheckSwitch:
    If debounce => 3 Then goto Debounced
      If GPIO.3 Off Then
         debounce += 1
      else
         glitch += 1
      end if
    wait 10 ms
    If glitch => 2 Then Goto Glitched
    goto CheckSwitch
    Debounced:
    
    Mode = Mode + 1
    if Mode= 5 Then Mode= 1
    Select Case Mode
      Case 1
      HPWM 1,10,20
      Case 2
      HPWM 1,10,75
      Case 3
      HPWM 1,10,255
      Case 4
      HPWM 1,10,0
    end Select
    Glitched:
    END SUB
    
     
    • BASIL HATZILAIOS

      Thank you very much kent_twt4 ! I'll make some tests with the code,with some modifications in the hardware.

       
  • William Roth

    William Roth - 2015-04-23

    The title of this thread is "ReadAD10(AN3) for PIC12F683 not work." However, ReadAD10 works just fine. It is the current limiting strategy and/or code implementation that is not working.

    The rise time of the PWM signal is too fast for the PIC to read the signal across the sense resistor with ReadADC10, and then turn off the FET before the current reaches 1 amp.

    Basil stated that, "The max current must be about 1A , so the voltage at sense resistor must not exceed 0.22V". To me this implies a 1 amp "peak" current. If you meant "average" current then you need to state this clearly.

    If you are trying to limit "average" current then there needs to be an integrator (filter) from the current sense resistor to convert the PWM to a DC voltage. We have not seen a schematic diagram, so it is difficult to tell what exactly you are trying to do with the ADC.

    William

     
    • BASIL HATZILAIOS

      William, thanks for the info and the well-aimed notices.You're right for the title of thread,but initially I was thinking that there was a problem with ReadAD. So, the "not work" must be delete (how can I do that?).About current limit control the 1A is average.The max. current of 54 LEDS (2835 SMD) must be 18X0.06A=1.08A , so I think 1A is a good choice.I add a initial schematic(EAGLE) for your opinion.
      Many thanks again.

       
  • kent_twt4

    kent_twt4 - 2015-04-23

    Another option is going the dedicated LED driver route. I have used the Diodes AL8805 chip (30V 1A CC with PWM input) to drive a LED desklamp from an AGM car battery. The lamp uses three Cree XTE white leds. It needs/uses approx. 3 in dia. of computer heatsink to keep things cool for continuous use. Missed putting a low pass filter on the PWM :( so it can be a little blinky on certain settings, otherwise it's great.

     
    • BASIL HATZILAIOS

      Kent,the code works fine.Do you have an idea on how to measure the time that the momentary switch is pressed, so after that the leds goes to off?
      thanks again.

       
      • kent_twt4

        kent_twt4 - 2015-04-24

        The interrupt uses approximately 3 loops of 10ms each (plus instruction overhead including the interrupt) to debounce the switch. These loops could be reduced in wait time by taking an empirical approach on your switch setup.

        Look to Help for Timer1 example on how to measure change in switch state.

         
        • BASIL HATZILAIOS

          ok, many thanks kent.

           
  • William Roth

    William Roth - 2015-04-24

    @Basil

    The title was mentioned only to clarify the actual problem/issue. As far as I know there is no way to change a thread title.

    Thanks for providing a schematic. It is a good start.

    "2835" is a package type and tells very little about the voltage/current specs of your actual LED'S. To provide better help we need to know the forward voltage and current specs for your specific LED's. If you do not know, then can you please provide a manufacturer/model or a link to the datasheet? If no actual specs are available, then then all bets are off and design must be based upon empirical data (actual measurements).

    If, for example, the max forward voltage of each LED is 2.2 volts, then there is a big problem to overcome given that that LED supply voltage can vary from 10 to 18 volts.

    Suppose that supply is 10v and the LED max forward voltage is 2.2v. Each LED will be supplied with ~3.3 volts. This will fry the LEDs rather quickly unless the current is controlled. With an 18V supply it will be even worse with each LED being provided 6 volts. This is literally off the charts for ANY 2835 that I have seen. Controlling the PWM duty cycle can limit the "average" current. But the peak current must also be considered. And remember, the max specs assume a die temperature of 25C, so they must be de-rated according to temperature.

    Before going any further I need to know the LED specs to see if your approach will work at all. My guess is that it will not work, given the input voltage specs and with three LEDs per string. However, depending upon the LED specs, some changes might could be made to allow it or something similar to work.

    If it were me, I would use an LED controller chip of some kind. There are so many inexpensive choices that will do a much better job than this kind of software solution.

    William

     
    • BASIL HATZILAIOS

      William,
      many thanks for your time.I prefer to use the software way to control the current limit of leds.There is no enough space at pcb for a step-down controller.At the other hand,I need to control all operations of the circuit with one only switch, that is difficult to make a circuit without an MCU,and with very few components. About input voltage,you're right it's big problem that will affect temperature stability and not only, so I'll reduce the upper limit to 15V. It's not very difficult I think, to control the leds current with PWM,so I make some tests with several parts of codes.
      I send you the data sheet of 2835.I would like to hear your idea about to how can I use the ADC input to control the current limit.

       
  • BASIL HATZILAIOS

    William,
    here is the data sheet of 2835.

     
  • kent_twt4

    kent_twt4 - 2015-04-24

    Since the addition of a simple SOT-23 npn transistor to control the device current (at the very least) cannot be considered, I'm bowing out of the conversation. An unregulated source voltage and device current is sure to make for some interesting dynamics.

    Kent

     
    • BASIL HATZILAIOS

      You have right kent, the npn transistor is a good idea.I'll test it.About the code with Select Case Mode,is it possible to add a case in that the leds will blink (500ms ON,100ms OFF)?
      thanks.

       
      • kent_twt4

        kent_twt4 - 2015-04-25

        Yes on the blinky led. Set a flag (e.g. Blinky=True) in the Select Case/Interrupt, then test for the flag in Main. Make sure Blinky=False in all other Select Cases.

        Main:
          Do While Blinky=True
            blinky stuff
          Loop
        ...
        ...
        ...
        goto Main
        
         
  • kent_twt4

    kent_twt4 - 2015-04-25

    Looky here, it appears that the 12f1822 and other like minded devices can replace the npn transistor feedback loop to the nfet gate using various internal analog modules (FVR, DAC, Comparator, TMR2, CCP1, and ECCP).

    How is that possible?
    1) Enable and set 1.024V FVR, and send it to the DAC.
    2) Enable DAC as internal source and set Positive source as FVR Buffer2 output. Set the DAC voltage bits to match the current required and the sense resistor used (< 1.024V).
    3) Enable Comparator and set register bits for C1VP to DAC and C1VN to C1IN1-. C1IN1- would be the sense resistor voltage.
    4) Setup the CCP1/PWM registers (or possibly use HPWM here?)
    5) Setup the ECCP register to enable the PWM auto-shutdown feature and choose the source as the Comparator C1 output high bits. Enable the auto-restart feature.

    This method has proven to operate as a "concept", without getting into the details, so no absolute guarantees :).

     
1 2 > >> (Page 1 of 2)

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.