PWM instruction not working as I expect


Closed Thread
Results 1 to 10 of 10
  1. #1
    Join Date
    Feb 2005
    Posts
    130

    Question PWM instruction not working as I expect

    Hi,

    I have this code for the 12F683 pic (4mhz int clock):

    Code:
    TRISIO = %00000000 
    
    conta var byte
    ciclos var byte
    pasos var byte
    
    red var gpio.1
    green var gpio.2
    blue var gpio.4
    
    
    ciclos=10
    pasos=16
    
    
    high red 'this goes to low unexpectedly when the following for-next is executed
    low blue
    low green
    
    for conta=0 to 255  step pasos   'blue rise
    pwm blue, conta, ciclos
    next conta
    high blue 'this goes to low unexpectedly when the following for-next is executed
    
    for conta=0 to 255  step pasos  'red fall
    pwm  red, 255-conta, ciclos
    next conta
    low red
    
    'rest of the program removed
    
    end
    I expect:

    1st loop
    red remain high while 1st for-next is executed
    blue increase till high

    2nd loop
    blue remains high from last loop
    red decrease till low

    what happens

    no matter the previous state of the port, when it enters a loop only the port being incremented/decremented is high, the other is low.

    what can be wrong?


    Thanks in advance

  2. #2
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    Are you disabling A/D & comparators?
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  3. #3
    Join Date
    Feb 2005
    Posts
    130


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Bruce
    Are you disabling A/D & comparators?
    bingo

    Thanks

  4. #4
    Join Date
    Feb 2005
    Posts
    130


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by peu
    bingo

    Thanks
    not that bingo after all...

    Here is the full code:

    Code:
    trisio  =%00000000
    ansel   =%00000000
    
    conta   var byte
    ciclos  var byte
    pasos   var byte
    
    red     var gpio.1
    green   var gpio.2
    blue    var gpio.4
    'pote    var gpio.0
    
    ciclos  =10
    low     red
    high    blue
    high    green
    
    pasos   =16
    loop:
    
    'ADCIN pote, pasos
    
    for conta=0 to 255  step pasos 'blue fall
    pwm  blue, 255-conta, ciclos
    next conta
    low blue
    
    for conta=0 to 255  step pasos 'red rise
    pwm  red, conta, ciclos
    next conta
    high red
    
    for conta=0 to 255  step pasos    'green fall
    pwm  green, 255-conta, ciclos
    next conta
    low green
    
    for conta=0 to 255  step pasos   'blue rise
    pwm blue, conta, ciclos
    next conta
    high blue
    
    for conta=0 to 255  step pasos  'red fall
    pwm  red, 255-conta, ciclos
    next conta
    low red
    
    for conta=0 to 255  step pasos 'green rise
    pwm  green, conta, ciclos
    next conta
    high green
     
    goto loop
    end
    red is supposed to stay high until the for-next that put it back to low, but as soon the rise for-next is finished it goes off.

    any idea?


    Pablo

  5. #5
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    CMCON0=7


    about now?
    Last edited by mister_e; - 19th January 2006 at 00:11.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  6. #6
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    Hi Pablo,

    Look in the data sheet under Comparator Module. Look at the "default" POR (power on reset) value in CMCON0.

    Now look at the drawing in FIGURE 8-3: COMPARATOR I/O OPERATING MODES.

    Place CMCON0 = X in the initialization section of your code.

    Replace X with the value shown in this section that will turn "Comparator Off".
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  7. #7
    Join Date
    Feb 2005
    Posts
    130


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Bruce
    Hi Pablo,

    Look in the data sheet under Comparator Module. Look at the "default" POR (power on reset) value in CMCON0.

    Now look at the drawing in FIGURE 8-3: COMPARATOR I/O OPERATING MODES.

    Place CMCON0 = X in the initialization section of your code.

    Replace X with the value shown in this section that will turn "Comparator Off".
    Thanks Bruce, your answer helped me two ways, to solve the problem and to understand a little more how to read datasheets, I have the 12F683 printed&binded datasheet next to me, but since I didn't know where to look exactly I wast lost in the sea of trial&error.

    Thanks again, and thanks Steve who also gave me the right answer!


    Pablo

  8. #8
    Join Date
    Feb 2005
    Posts
    130


    Did you find this post helpful? Yes | No

    Default

    Hi Bruce,

    I won't ask howto but where to look for the answer

    I'm trying to use gpio.3 as an input, but I dont know how to set it as such.

    Setting trisio=%00001000 does not help me

    Thanks in advance

    Quote Originally Posted by Bruce
    Hi Pablo,

    Look in the data sheet under Comparator Module. Look at the "default" POR (power on reset) value in CMCON0.

    Now look at the drawing in FIGURE 8-3: COMPARATOR I/O OPERATING MODES.

    Place CMCON0 = X in the initialization section of your code.

    Replace X with the value shown in this section that will turn "Comparator Off".

  9. #9
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    Hi Pablo,

    PBP has default config fuse settings in device specific header files. Look in your PBP directory for a file named 12F683.INC. Open this file.

    You'll see the PBP default config fuse settings for this target PIC. It looks somthing like this;

    This is used only if you're using the default PM asembler that ships with PBP.

    device pic12F683, intrc_osc_noclkout, wdt_on, mclr_on, protect_off

    And this one is used when you're using the Microchip MPASM assembler.

    __config _INTRC_OSC_NOCLKOUT & _WDT_ON & _MCLRE_ON & _CP_OFF

    Notice how the default settings for /MCLR (GPIO.3) all have the reset function enabled (mclr_on, _MCLRE_ON)?

    You can over-ride these default config fuse settings, and make GPIO.3 available as an input by editing the file, and changing it to mclr_off or _MCLRE_OFF, save the file, and re-compile.

    Or simply add this line to the beginning of your code (if using the PM asembler);

    @ device pic12F683, intrc_osc_noclkout, wdt_on, mclr_off, protect_off

    Now you just need to make the pin an input by setting the appropriate TRIS bit.

    Melanie posted a nice tutorial on config settings here:
    http://www.picbasic.co.uk/forum/showthread.php?t=543 so I won't elaborate.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  10. #10
    Join Date
    Feb 2005
    Posts
    130


    Did you find this post helpful? Yes | No

    Default

    Thanks again Bruce, just added that line, and now I can use this extra pin !!

    I trully appreciate your answers.


    Pablo

Similar Threads

  1. HW PWM not working
    By comwarrior in forum General
    Replies: 5
    Last Post: - 31st July 2009, 17:16
  2. Variable PWM PIC18F2431
    By trr1985 in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 12th June 2009, 06:03
  3. PWM: Vernacular/Function (Newbee)
    By TerryN in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 3rd July 2008, 16:58
  4. PWM setting PIC16F887
    By Gevo in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 12th June 2008, 07:24
  5. Tidying Up PWM Routine
    By Tissy in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 21st February 2005, 00:26

Members who have read this thread : 1

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

Posting Permissions

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