Menu

variable PWM with 12F1572

Help
2019-08-05
2019-08-06
  • HackInBlack

    HackInBlack - 2019-08-05

    adapted from an example file;the pushbutton 'should' change the duty-cycle on channel 1,but won't do anything...can anyone see whats wrong?

    chip 12F1572, 32

       ' #config mclr=on   'use bushbutton on GPIO.3 as input
        #define SWITCH_PORT GPIO.3
    
        Dir SWITCH_PORT In
    
        Dir PORTA Out
    
        Do
      If SWITCH_PORT = 0 Then
    
        #define HPWM16_1_Freq 100         '100khz
        #define HPWM16_1_Duty 5           '5% duty DS2 barely lit
        HPWM16On ( 1 )
    
        else
    
        #define HPWM16_1_Freq 200         '200khz
        #define HPWM16_1_Duty 100         '100% duty
        HPWM16On ( 1 )
    
        #define HPWM16_3_Freq 300         '300khz
        #define HPWM16_3_Duty 60          '60% duty DS3 brighter
        HPWM16On ( 3 )
    
        End If
    Loop
    
     
  • kent_twt4

    kent_twt4 - 2019-08-05

    The Switch_Port In is overwritten by PortA Out. Could set each individual pin, or use TrisA = 0b001000

     
  • HackInBlack

    HackInBlack - 2019-08-06

    i've changed 'DIR porta out' to 'TrisA = 0b001000' but the switch still has no effect;the chip is outputting the correct PWM but it won't change on the button press...

     
  • Anobium

    Anobium - 2019-08-06

    You are using fixed mode PWM. The frequency and duty are fixed at the frequency and duty as specified by the PWM constants. As you cannot change the constants in your running program what you are getting is the correct behaviour for fixed mode.

    You need to use Variable mode PWM.

     
  • HackInBlack

    HackInBlack - 2019-08-06

    i thought obout that just before you posted! its 'set and forget' PWM...now to try the same idea using variable PWM.if i can get my head round the examples...still at least it does output 2 PWM waveforms albeit on seperate pins! why only2? simple..the lowpincount board only has 2 'linked' LEDs.

     

    Last edit: HackInBlack 2019-08-06
  • Anobium

    Anobium - 2019-08-06

    See C:\GCB@Syn\GreatCowBasic\Demos\pwm_solutions\pic_pwm16_examples in your installation. There are a lot of demos specific to your chosen chip.

     
  • HackInBlack

    HackInBlack - 2019-08-06

    #chip 12F1572, 32

    #define SWITCH_PORT GPIO.3
    Dir PORTA Out
    TrisA = 0b001000
    
     Do
      If SWITCH_PORT = 0 Then
    
    HPWM16(1, 10, 16384)   '10 kHz, 25% duty cycle (16384/65535)
    
    else
    
    HPWM16(1, 80, 16384)   '80 kHz, 25% duty cycle (16384/65535)
    
    HPWM16(3, 30, 16384)   '30 kHz, 25% duty cycle (16384/65535)
    
    End if
    loop
    

    ' not sure if the 80 is right but at least its visible! and it works as expected

     
  • Anobium

    Anobium - 2019-08-06

    Excellent news.

    Enjoy

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.