3 channel PWM with customize duty cycle


Closed Thread
Results 1 to 40 of 57

Hybrid View

  1. #1
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,612


    Did you find this post helpful? Yes | No

    Default Re: 3 channel PWM with customize duty cycle

    Hi,
    That looks to be correct but I haven't tried it. Obviously you have to write code in the interrupt handler to take care of whatever needs to be taken care of when it sees the signal but yes, interrupts work like that.

    You declare the interrupt at the beginning of the program. Then, whenever PortB.0 goes high the execution will jump to the label you specify (Faultsignal in this case). It executes the code there and when it sees the Resume keyword it returns to where it left off.

    Don't try to incorporate it in your application directly, play with it a bit first. Make a LED blink continously and have the interrupt toggle another LED so you see how it works, then move on to incorporate it in your application.

    With that said ON INTERRUPT isn't the most effective of handling interrupts but I won't go into that right now. There are TONS of info on interrupts on this forum, do a little searching and reading.

  2. #2


    Did you find this post helpful? Yes | No

    Default Re: 3 channel PWM with customize duty cycle

    hi,

    OK i will play it using LED first. The port B0 that i declare says that when the fault signal detect 5V is the normal operation, when suddenly changes to 0v, port B0 automatically goes to my interrupt handler to do whatever need to be done.
    So, my question is:

    1. did portb0 can detect directly 5v without adding extra codes that define the values of voltages?
    2. if yes, can i use pull-up resistor with push-up button to create a changes signal to the portb0 and see the interrupt works or not?
    3. if not, what others method to create a changes signal to the portb0?

    thanks,
    photoelectric

  3. #3
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,612


    Did you find this post helpful? Yes | No

    Default Re: 3 channel PWM with customize duty cycle

    Hi,
    The interrupt can be triggered on either the rising or falling edge of the signal connected to PortB.0 (ie the interrupt fires when the signal changes state). You select rising/falling edge by setting or clearing OPTION_REG.6

    If, during normal operation, the signal connected to PortB.0 is high and it goes low when there's a problem you want to set it up so it interrupts on the falling edge. (OPTION_REG.6 = 0)

    Yes, to test it, pull up PortB.0 with a resistor and use a switch/button/whatever to pull it low and trig the interrupt. Note that you are likely to get some contact bounce with a mecanical switch/button which may trig the interrupt several time for each press of the button.

  4. #4


    Did you find this post helpful? Yes | No

    Default Re: 3 channel PWM with customize duty cycle

    hi,
    i try to test my simple interrupt on my real PIC simulation.
    below is my codes:

    Code:
    led     VAR     PORTC.4
    
            TRISB.0 = 1 'set port bo to input
            ON INTERRUPT GoTo myint ' Define interrupt handler
            INTCON = %10010000      'Enable RB0 INTERRUPT
            OPTION_REG.6 = 0        'Interrupt on rising edge of RB0/INT pin
    
    loop:   High led                ' Turn LED on
            GoTo loop               ' Do it forever
    
    
    ' Interrupt handler
            Disable                 ' No interrupts past this point
    myint:  
    Low led                 ' If we get here, turn LED off
            'Pause   500             ' Pause .5 seconds        
            INTCON.1 = 0            ' Clear interrupt flag
            Resume                  ' Return to main program
            Enable
            
            End
    turns out when i enable Pause my interrupt works ok but i turns off pause it wont interrupt at all.
    Please help explain what cause the problem.

  5. #5
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,612


    Did you find this post helpful? Yes | No

    Default Re: 3 channel PWM with customize duty cycle

    Hi,
    I bet it works just fine. It's just that when you don't have the Pause there you don't see it becase the main routine keeps turning ON the LED all the time.

    The interrupt routine turns it OFF and a couple of microseconds later the main routine turns it ON again.

  6. #6


    Did you find this post helpful? Yes | No

    Default Re: 3 channel PWM with customize duty cycle

    Hi,

    OIC. tq
    So, i notice that the led will turn off whenever the faultsignal is change BUT after a few milisec it turns ON back.
    The problem on my design is that i dont know how much time the fault signal will be interrupted before it back to normal operation

    What should i do if i want the led to turn off until the fault signal back to normal operation which 5V again?

    any example for me?

    photoelectric

  7. #7
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,612


    Did you find this post helpful? Yes | No

    Default Re: 3 channel PWM with customize duty cycle

    Like I said, it turns ON again because YOU turn it on in your main loop - continously. If you don't want it to turn on again then stop turning it on. Set it one time, before you enter the loop.

    If you want it to trig (turn off the LED) with an interrupt but then start "working" again as soon as the fault signal "goes away" then simply read the state of the pin in your Main routine and turn on the LED once it's high:
    Code:
    Main:
    If PortB.0 = 1 THEN     'Turn on LED only when signal is high.
      HIGH LED
    ENDIF
    'Do whatever else needs to be done.
    Goto Main
    If want it to trig with an interrupt and stay off untill another input "restart it" then use a flag/semaphore:
    Code:
    FAULT VAR BIT
    LED VAR PortC.4
    Restart VAR PortC.5   'Pulled up thru resistor, pull to GND to "activate"
     
    High LED
     
    Main:
     If Fault = 1 then            'If we are in fault state...
      If Restart = 0 then        '...we check the restart button....
       HIGH LED                '....if it's pressed we turn LED on again...
       Fault = 0                '...and reset the fault flag/state
      ENDIF
     ENDIF
    'Do whatever else here.
    Goto Main
    Then, in your interrupt routine you turn off the LED and set the Fault flag, like:
    Code:
    LOW LED
    Fault = 1    'Set flag
    Finally, be careful with the word Loop as a label, in newer versions of PBP that is a reserved word and can't be used as a label, what version are you using.

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