1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

Using PWM on a 18F13K22

Discussion in 'Microcontrollers' started by MrDEB, Nov 27, 2013.

  1. MrDEB

    MrDEB Active Member

    Joined:
    Apr 16, 2007
    Messages:
    4,414
    Likes:
    23
    If I understand this correctly, I get each led color matched to current draw so all three colors are even.
    Then I need a method to vary the duty cycle on the Blue and Green leds as per the hex information on the chart.
    Sounds easier than the route I have gone BUT I then need to come up with a code to output the correct duty cycle as per chart.
    When I get back later today I am going to invistagate this idea as it might work with the code using DUTYVAL etc instead of the one I wrote using CONST ARRAYS.
     
  2. MrDEB

    MrDEB Active Member

    Joined:
    Apr 16, 2007
    Messages:
    4,414
    Likes:
    23
    I think I finally got it to do the undulating candle flame.
    I studied the chart Jonsea posted and see that the only color I really need to change is the green with red full on and blue dimmed with resistor.
    Using PWM and a CONST array I found I could produce a very realistic appearing flame and changing the duty of the green to variying degrees. Contemplated a Random Number generator but really no need. Now to connect up the actual 18F13K22 and see what takes place. Again THANKS for the direction JonSea with the color chart. Somewhere in the future I would like to look into using the HEX numeration in a code to produce the entire color spectrum in the chart.
    Code (text):
    Device = 18F2420
    Clock = 8

    Include "InternalOscillator.bas"
    Include "RandGen.bas"
    Include "Utils.bas"
    // import PWM module...
    Include "PWM2.bas"
    const color(6) as byte = (100, 70, 90, 60, 50, 80)
    // local duty variable...
    Dim Duty As Byte
    dim x as word
    // main program...
    pwm.SetFreq(5000)
    pwm.Start1

    While true
    for x = 0 to 6
    Duty = 0
    Repeat
    pwm.SetDuty1Percent(Duty)
    Inc(Duty)
    DelayMS(5)
    Until Duty > color(x)
    Repeat
    pwm.SetDuty1Percent(Duty)
    Dec(Duty)
    DelayMS(10)
    Until Duty = 0
    next
    Wend

     
     
  3. Little Ghostman

    Little Ghostman Well-Known Member Most Helpful Member

    Joined:
    Jan 7, 2011
    Messages:
    3,737
    Likes:
    433
    Location:
    SCOTLAND
    yeah thanks Jonsea great job!;)
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. MrDEB

    MrDEB Active Member

    Joined:
    Apr 16, 2007
    Messages:
    4,414
    Likes:
    23

    ERRO in trying to program pic. I have an external voltage source of 45 v but using TROUBLESHOOT in PICKIt2 I keep getting a Vpp volta error. Tried w/ external voltage and W/O external voltage
    Checked and double checked connections but ?
     
  6. JonSea

    JonSea Well-Known Member

    Joined:
    Oct 1, 2012
    Messages:
    1,199
    Likes:
    110
    Location:
    Seattle, WA
    If you truly have an external voltage of 45 volts, throw everything including the PICkit 2 in the trash. It's all toast.

    If you have an external supply voltage of 4.5 volts, please proofread a bit more before posting so that the world doesn't have to speculate about your problems.

    I've found this error is most common if /MCLR is shorted to ground or tied directly to Vcc instead of being pulled high by a 10k resistor. The PICkit 2 has a strong self-preservation instinct and goes into a protective mode when this happens. It may appear to be working, but after this error occurs, you need to take some steps to reset it:
    1. Close the GUI (assuming you are using the stand-alone GUI instead of MPLab)
    2. Disconnect the PICkit 2 from the board and from USB. Find and fix the problems on the board.
    3. If the PICkit 2 is connected via a USB hub, you may need to disconnect the hub from the computer USB and external power supply (if it has one) to reset its power protection circuits.
    4. Reconnect the PICkit 2 USB (and hub if you're using one).
    5. Restart the GUI.
    6. Set the device type in the GUI and import your hex file. Un-check (un-tick) the Vdd box in the GUI if using external power.
    7. Reconnect the board and try again.
    If the error reoccurs, spend more time tracking down the problem with /MCLR, repeat all of the above steps (even the ones you don't think are required) and try again.

    If the GUI locks up and can't be shut down, reverse steps 1 & 2 above and it will be possible to close it after the PICkit 2 is disconnected.
     
  7. Little Ghostman

    Little Ghostman Well-Known Member Most Helpful Member

    Joined:
    Jan 7, 2011
    Messages:
    3,737
    Likes:
    433
    Location:
    SCOTLAND
    try all the good advice from jonsea but it might persist in that case try 47V err i mean 4.7V the K22's and most other K chips can be funny about voltage
    i also got nice surprise in post today dad got me some of the chips so when i get time i will have a crack at this
     
  8. JonSea

    JonSea Well-Known Member

    Joined:
    Oct 1, 2012
    Messages:
    1,199
    Likes:
    110
    Location:
    Seattle, WA
    It's so rewarding to take time to provide a detailed answer to a question and it apparently ignored. When will I learn?!?
     
  9. Little Ghostman

    Little Ghostman Well-Known Member Most Helpful Member

    Joined:
    Jan 7, 2011
    Messages:
    3,737
    Likes:
    433
    Location:
    SCOTLAND
    i appreciate it :D all the good bits get put in the book! (actually its now a folder)
     
  10. MrDEB

    MrDEB Active Member

    Joined:
    Apr 16, 2007
    Messages:
    4,414
    Likes:
    23
    Have checked everything, found I had a 8K resistor instead of 22K on the MCLR (Vcc to MCLR) Replaced and still get 7.4 volts on Vpp Chip only requires 8v. Took a break to convert AC mig welder to DC. They call this a breather. If nothiong comes up the I will replace the pic. ONLY THING LEFT that I haven't checked.
     
  11. JonSea

    JonSea Well-Known Member

    Joined:
    Oct 1, 2012
    Messages:
    1,199
    Likes:
    110
    Location:
    Seattle, WA
    Uh huh. And did everything include following exactly the steps I outlined above?

    This is a 10 minute problem, not a two day problem. Check connections. Check for shorts. Try another board. If that works, replace the PIC. They don't often fail but it is a possibility.

    Oh, and acknowledge that you've taken the time to read, understand and perhaps even follow the suggestions that people have taken time to post for you. The pool of people willing to help you is ever-shrinking.
     
  12. MrDEB

    MrDEB Active Member

    Joined:
    Apr 16, 2007
    Messages:
    4,414
    Likes:
    23
    I have followed ALL the suggestions except assembling another board. Thats next. One thing I noticed is when I first enable the Pickit2 I get a Vpp error . On to assembling a new board. I realize this is a simple solution and if I was working on all the time then yes I must be slow or ? but I get as chance to work on this when its night time as I have other items in the household that need attention as well as other projects. I know it seems ridiculous to spend this amount of time tracking down what appears to be a short. YES I understand all that has been suggested.
     
  13. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,309
    Likes:
    914
    Location:
    Rochdale UK
    ONLINE
    Can I verify this please.... Not sure if a pic will run at 7.4v let alone 8v..

    Have you purposely chosen this voltage? Or is this for programming only?.... Check in your config reg's to see what LVP is set to...

    Sorry more!!
    The progaming voltage is only there for a small while you won't be able to measure it with a multimeter as it's quite a short time. You are allowed up to 11 volts on those chips, but as you so rightly say minimum of 8v..

    A 8k pullup should have been ok!...
     
    Last edited: Dec 4, 2013
  14. JonSea

    JonSea Well-Known Member

    Joined:
    Oct 1, 2012
    Messages:
    1,199
    Likes:
    110
    Location:
    Seattle, WA
    1. Don't assemble another board to test this. Use any of the PIC boards you have. It's to check if the PICkit 2 is functional. If it works on a different board, then there is a problem with the board or the PIC.

    2. The PICkit 2 needs to be calibrated for its supply voltage from the USB port. The nominal supply voltage on a USB port is 5 volts, but this can vary over a wide range. Get a decent DVM and follow the instructions under the tools section on the stand-alone GUI. Do not select the calibration option unless you have a DVM to measure the voltage from the PICkit 2. Follow the directions on the screen.

    Note – this calibration will only be valid when the PICkit is connected to this USB port. If you move it to a different port (a different computer or hub), check it again.

    Note – do not run the PICkit 2 from an unpowered USB hub. This will not work reliably.
     
  15. MrDEB

    MrDEB Active Member

    Joined:
    Apr 16, 2007
    Messages:
    4,414
    Likes:
    23
    Using the Pickit2 TOOLS/ TEST is when it says 7.6v under Vpp test. The voltage setting for this pic is 2.3v - 5.5v as per data sheet.
    I already assembled a new board using same pic and same results. Changed the Pic and the Vpp says its good (8.2v if I recall using Vpp test) set to go but dealing with clock settings but can't find an INCLUDE file with this pic. Looked several places including data sheet but keep getting compile errors.
    Using internal clock
    NOTE I tested the Pickit2 by itself using voltmeter and according to the TEST voltages all is good. I did this before building a new board.
     
  16. tumbleweed

    tumbleweed Member

    Joined:
    Jan 25, 2007
    Messages:
    66
    Likes:
    19
    You will have to make changes to some of the libraries to support the 18F13K22, including InternalOscillator.bas and the SetAllDigital() routine in utils.bas.

    Also, when you switch to the 13K22 you will have to change the PWM2 module since it doesn't support that chip. Change
    Code (text):

    #if _device in (18F1220, 18F1320)
      Dim FPWM1Pin As PORTB.3  // RB3 connected to CCP1 module
    #else
      Dim FPWM1Pin As PORTC.2  // RC2 connected to CCP1 module
      Dim FPWM2Pin As PORTC.1  // RC1 connected to CCP2 module
    #endif
     
    to
    Code (text):

    #if _device in (18F1220, 18F1320)
      Dim FPWM1Pin As PORTB.3  // RB3 connected to CCP1 module
    #elseif _device in (18F13K22)
      Dim FPWM1Pin As PORTC.5  // RC5 connected to CCP1 module
    #else
      Dim FPWM1Pin As PORTC.2  // RC2 connected to CCP1 module
      Dim FPWM2Pin As PORTC.1  // RC1 connected to CCP2 module
    #endif
     
     
    • Like Like x 1
  17. MrDEB

    MrDEB Active Member

    Joined:
    Apr 16, 2007
    Messages:
    4,414
    Likes:
    23
    I realize I need to make some changes so here is where I am
    Code (text):
    Device = 18F13K22
    Clock = 8
    Include "oscKseries.bas"

    // alias to port pin...
    Dim LED1 As PORTB.5
    Dim led2 As PORTB.6
    Dim Led3 As PORTC.2
    // main program...


    Output(LED1)
    Output(led2)
    Output(Led3)

    High(LED1)
    High (led2)
    High (Led3)
    While 1 = 1
      Low (led2)
      DelayMS (500)
      High (led2)
      DelayMS(500)
    Wend
     
    and here is my include file for the clock but still tinkering

    Code (text):
    Module oscKseries
    OSCCON = %01100110 ' IRCF<6:4> and SCS<1:0> => 8MHz internal osc
        OSCCON2 = %00000100 ' PRI_SD<2> = Osc drive circuit on
        OSCTUNE = %11000000 ' INTRC = 1
        CM1CON0.7 = 0 ' comp1 off
        CM2CON0.7 = 0 ' comp2 off
        ANSEL = %00000000 ' all digital
        ANSELH = %00000000 ' all digital
        TRISb = %00000000 ' portb cleared for output
     
    Have yet to get it working but tinkering at best. Making changes as per data sheet
     
  18. MrDEB

    MrDEB Active Member

    Joined:
    Apr 16, 2007
    Messages:
    4,414
    Likes:
    23
    Am attempting to just blink an LED on portB.6 to verify the oscillator is running correctly before attempting the PWM .
    Will make changes to the PWM module as per Tumblweeds suggestion after I verify I have a running pic.
     
  19. Little Ghostman

    Little Ghostman Well-Known Member Most Helpful Member

    Joined:
    Jan 7, 2011
    Messages:
    3,737
    Likes:
    433
    Location:
    SCOTLAND
    it is a good idea to set ALL fuses in your code so you know exactly what state the pic is in, especially stuff like LVP=off, it can save a great deal of time if you set each and every fuse at the start. Also from memory your not using a PK2 but a junebug? if so check all the tutor board parts are off, it should make a difference but on a couple of occasions has caught me out.
    Very unlikely to be the pic,
     
  20. tumbleweed

    tumbleweed Member

    Joined:
    Jan 25, 2007
    Messages:
    66
    Likes:
    19
    Great advice.

    Try adding at least
    Code (text):

    config FOSC = IRC,      ' internal oscillator
           PLLEN = OFF
     
    and change OSCTUNE = %00000000 to make sure the PLL is off
     
  21. Little Ghostman

    Little Ghostman Well-Known Member Most Helpful Member

    Joined:
    Jan 7, 2011
    Messages:
    3,737
    Likes:
    433
    Location:
    SCOTLAND
    there are alot of fuses you should be setting for example

    PCLKEN = ON
    Primary clock enabled
    FCMEN = OFF
    Fail-Safe Clock Monitor disabled
    IESO = OFF
    Oscillator Switchover mode disabled


    MCLRE = ON
    MCLR pin enabled, RA3 input pin disabled
     

Share This Page