Tidying Up PWM Routine


Closed Thread
Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2005
    Location
    Essex, UK
    Posts
    154

    Default Tidying Up PWM Routine

    I have a piece of code which does PWM on Consecutive outputs PORTC.0-4. However the code seems quite long (taking up unnecessary code space on the PIC). I'm sure it can be shortened, but a little unsure how as each of the variables (LED Colours) have been defined. I need to leave the other bits (getout) in which relate to an interupt routine. Can it be shortened ?

    Many thanks,

    Red VAR PORTC.0 ' All LEDs
    Green VAR PORTC.1 ' Connected between
    Blue VAR PORTC.2 ' RC pins and ground
    UV VAR PORTC.3 ' via resistor
    White VAR PORTC.4 '

    Bright VAR byte
    getout VAR byte

    FadeAll:
    'FadeRED
    Low White
    bright = 0
    while (Bright !=255) and (GetOut=0)
    PWM Red,Bright,3
    bright = bright + 1
    wend
    while (bright !=0) and (getout = 0)
    PWM Red,Bright,3
    bright=bright - 1
    wend
    'FadeGreen
    bright = 0
    while (Bright !=255) and (GetOut=0)
    PWM Green,Bright,3
    bright = bright + 1
    wend
    while (bright !=0) and (getout = 0)
    PWM Green,Bright,3
    bright=bright - 1
    wend
    'FadeBlue
    bright = 0
    while (Bright !=255) and (GetOut=0)
    PWM Blue,Bright,3
    bright = bright + 1
    wend
    while (bright !=0) and (getout = 0)
    PWM Blue,Bright,3
    bright=bright - 1
    wend
    'FadeUV:
    bright = 0
    while (Bright !=255) and (GetOut=0)
    PWM UV,Bright,3
    bright = bright + 1
    wend
    while (bright !=0) and (getout = 0)
    PWM UV,Bright,3
    bright=bright - 1
    wend
    'FadeWhite:
    bright = 0
    while (Bright !=255) and (GetOut=0)
    PWM White,Bright,3
    bright = bright + 1
    wend
    while (bright !=0) and (getout = 0)
    PWM White,Bright,3
    bright=bright - 1
    wend
    return

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


    Did you find this post helpful? Yes | No

    Default

    try this and post result
    Code:
    PwmOnLed var byte
    Bright VAR byte
    getout var bit
    
    FadeAll:
        For pwmonled = 8 to 12
            bright = 0
            while (Bright !=255) and (getout=0)
                PWM pwmonled,Bright,3 
                bright = bright + 1
            wend
            while (bright !=0) and (getout=0)
                PWM pwmonled,Bright,3 
                bright=bright - 1
            wend
        next
        goto fadeall
    Steve

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

  3. #3
    Join Date
    Feb 2005
    Location
    Essex, UK
    Posts
    154


    Did you find this post helpful? Yes | No

    Default

    Yup, that works great and a lot shorter, you're the man !!

    Now the question so i can learn how you did it.

    How does the PWM command know to output on pins PORTC.0-4. I'm guessing its to do with the 8 - 12 you have placed in the for next loop. But if that were true, why is it not outputing to pins PORTB.0-4.

    I base that on the following:

    0-7 = PORTA.0 - 7
    8-14 = PORTB.0 - 7
    15-21 = PORTC.0 - 7

    Thanks again.

    Steve

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


    Did you find this post helpful? Yes | No

    Default

    As i remind you have a 40Pins PIC ?!?

    look into your PBP manual section 4.11 page 26
    Steve

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

  5. #5
    Join Date
    Feb 2005
    Location
    Essex, UK
    Posts
    154


    Did you find this post helpful? Yes | No

    Default

    It so simple when you read it !!!

    Knowing that will hopefully make things a little easier when i come to change the program from using a PIC16C876 to a PIC16C628A.

    Thanks again,

    Steve

Similar Threads

  1. Half-bridge PWM with a 16F684 ?
    By Byte_Butcher in forum General
    Replies: 7
    Last Post: - 17th January 2010, 23:18
  2. Variable PWM PIC18F2431
    By trr1985 in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 12th June 2009, 07:03
  3. PWM: Vernacular/Function (Newbee)
    By TerryN in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 3rd July 2008, 17:58
  4. PWM setting PIC16F887
    By Gevo in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 12th June 2008, 08:24
  5. Slowing Down RGB PWM Routine
    By Tissy in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 8th March 2005, 10:22

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