Unusual problem


Closed Thread
Results 1 to 8 of 8

Thread: Unusual problem

  1. #1
    Join Date
    Jul 2004
    Location
    CA
    Posts
    78

    Default Unusual problem

    Hello Folks, I have been working on this for quite a while now and am not getting the results that I wanted to acheive. I am using a a few switches to control the sequence of LEDs through PWM. I have pasted my code here ... Hope someone can help me find the solution to this.

    ==============Code starts here===================
    start:

    'Values of Variables being used
    Cycle1 = 2
    Save = 0


    'start of sequence
    For Duty = 0 to 2000 'All OFF ---> BLUE
    If(Stop_Switch = 0) Then Flag1 = 1 'Set FLAG to Zero because Ext. Int. Occurred

    if(Flag1 = 1) then
    Duty = Duty - 1 'PAUSE PWM actually occurs here

    'jumping to subroutines

    if(INC_BRIGHT = 0) then GoSub INCREASE_BRIGHT 'bright switch

    endif

    if(Resume_Switch = 0) && (Stop_Switch = 1) then 'if resume switch is pressed then return back and continue
    Flag1 = 0 'Since Pausing is no longer performing, Flag is refreshed back to 1
    Endif 'to resume PWM where it was left off


    For cycles = 0 to Cycle1 'if Nothing is pressed then Perform normal PWM sequence
    Portb = %00000000
    Pauseus 2000 - Duty
    Portb = %11100000
    Pauseus Duty

    Next cycles
    Next Duty

    'start of subroutine

    INCREASE_BRIGHT:
    While(INC_BRIGHT = 0)
    if(Save = 0) then 'making sure whether INCreasing or DECreasing
    'Global Brightness switch is pressed first
    A = 0 'if Increasing switch is pressed first then start
    'with initial values
    else 'OtherWise
    A = Save 'if the Decrease global Switch is pressed first, then
    'start with the values stored in the variable SAVE
    Endif 'ending the condition loop
    For i = A to 100 'if increasing switch is pressed continuously then
    PortB.4 = 0 'Normal PWM
    Pauseus 100 - i
    PortB.4 = 1
    Pauseus i
    Save = i 'saving the value of the PWM state just incase the DEC-GlOB is pressed

    if(INC_BRIGHT = 1) && (DEC_GLOB_BRIGHT_SWITCH = 1) then 'if button is let go then return
    Return
    endif

    Next i 'if switch is kept pressed then proceed to next increment
    Wend

    ==============================================

    I am performing a simple software pwm with three leds connected to PORTB.7, PORTB.6, and PORTB.5 ... Here is what I want to do with the code I have attached above. When the increase brightness switch is pressed (at random) I want the brightness to change without noticing the flickering. I notice that once the code jumps into the subroutine the LEDs become full brightness, and does not stay at a random (PWM VALUE) this is because the last thing that is executed in the main loop is
    PORTB = %11100000 this keeps the three LEDs full brightness.

    For example lets say that the button is pressed when all of the three LEDS are 40% ON. and for the amount of time the button is pressed, the leds increases in brightness. How do I prevent the behavior I see from my code. Any help on this would be greatly appreciated. many Thanks in advance

    srigopal

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


    Did you find this post helpful? Yes | No

    Default

    Hi srig, sorry if i didn't work on this since a lot of time but i just have one suggestion to avoid flickering.
    • 1. have a PIC with 1 internal PWM
      2. Use this output with a transistor or mosfet to source voltage to LEDS

    so that will be more easy to create your code. The only thing you will have to work with is with HPWM.

    better like that ?
    Steve

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

  3. #3
    Join Date
    Jul 2004
    Location
    CA
    Posts
    78


    Did you find this post helpful? Yes | No

    Default hpwm

    For Hardware PWM .. lets say you want the pwm to go from 0 to 100% so lets say that i have the folllowing in my code


    hpwm 1, 256, 1000

    Will this command ramp the LED from 0 brightness slowly to 100% brightness

    or


    will this command just make the LED full Bright, all the time


    srigopal

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


    Did you find this post helpful? Yes | No

    Default

    For a ramp from 0-100%

    [php]
    For z=0 to 255
    HPWM 1,z,1000
    ;
    ;
    next
    [/php]

    where 0 is full time OFF and 255 is full time ON
    Steve

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

  5. #5
    Join Date
    Jul 2004
    Location
    CA
    Posts
    78


    Did you find this post helpful? Yes | No

    Default HPW

    Now will this use up any of the CPU execution cycles or will it run in the background..?

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


    Did you find this post helpful? Yes | No

    Default

    Since it's an Hardware function of your PIC, it will run in background without any effect caused by others.
    Steve

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

  7. #7
    Join Date
    Jul 2004
    Location
    CA
    Posts
    78


    Did you find this post helpful? Yes | No

    Default

    but what about the
    For z = 0 to 255
    HPWM 1, Z, 1000
    Next Z


    The For loop will take some cycles away from the CPU wouldn;t it?.. the only reason why I am asking this is because whenever I run a FOr loop I have come across some flickering. but never tried the hardware modules before. maybe it works different. Can you explain more why this does not work

    and are there any other DEFINES I need to incorporate into my code to use the HPWM command or that is all I need to do to run the HARDWARE PWM module

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


    Did you find this post helpful? Yes | No

    Default

    The For loop will take some cycles away from the CPU wouldn;t it?.. the only reason why I am asking this is because whenever I run a FOr loop I have come across some flickering. but never tried the hardware modules before. maybe it works different. Can you explain more why this does not work
    The major differerence is because it's an hardware function. In your previous code, you must wait untill key is press/depress this is what cause the flickering. Your pseudo PWM wait for executing the next function. If it's waiting, LED will be Full on or Full of depending where you get the pushbutton action in your FOR TO NEXT loop.


    In case of using HPWM, it can have a little flickering when changing duty value... must have great eyes to see it since it's done really fast by PIC.

    for the DEFINE, it's usually use to redirect PWM to different PINS of PIC when this one have the ability of.
    Steve

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

Similar Threads

  1. Unusual Interrupts Application Problem
    By Joe Rocci in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 8th May 2009, 11:55
  2. Problem to compile my program
    By wagner in forum Off Topic
    Replies: 5
    Last Post: - 7th July 2008, 20:17
  3. Problem with ASM IRQ using FSR0 on PIC18F252
    By mytekcontrols in forum mel PIC BASIC Pro
    Replies: 10
    Last Post: - 11th March 2008, 20:48
  4. USART Problem , but don't know where, in pc? or in PIC?
    By precision in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 15th July 2007, 08:12
  5. 1 slave 1 master 1 MAX232 1 problem ?
    By SuB-ZeRo in forum mel PIC BASIC Pro
    Replies: 19
    Last Post: - 31st July 2005, 22:59

Members who have read this thread : 0

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