HW PWM not working


Closed Thread
Results 1 to 6 of 6
  1. #1

    Default HW PWM not working

    I've got HWPWM 1 (RC2/CCP1) feeding into TMR1 counter input (RC0/T1CKI).
    I want the PWM set at 1KHz @ 50%...

    I've programmed the PIC (16F877A I/P) and it's running but TMR1 is not incrementing... A standard Digital multimeter reports a solid 5 volts on RC2/CCP1...

    When i simulate the software in oshonsoft PIC simulator i set up an simulated signal generator to RC0/T1CKI tmr1 input and i can see TMR1L and TMR1H registers increment... However, HWPWM1 goes high and stays high... I've left it running for an hour on my PC (>65,000uS simulation time)...

    The following code is used to initialise my PIC...

    Code:
    Define  CONF_WORD = 	$3F3A
    include "bs1defs.bas"
    Define  OSC				24				' Set clock speed
    DEFINE  SER2_BITS		8				' Ser2 number of data bits
    SEROUT2 PORTB.5,16390,[10,13,10,13]
    SEROUT2 PORTB.5,16390,["INITIALISING",10,13]
    Define  ADC_BITS        10     			' Set number of bits in result
    Define  ADC_SAMPLEUS    100    			' Set sampling time in uS
    ADCON0.6 = 1							' ADC clock set to Fosc/64
    ADCON0.7 = 1							' ADC clock set to Fosc/64
    ADCON1.6 = 0							' ADC clock set to Fosc/64
    INTCON = 0								' Disable all interupts
    'RCSTA = %10010000						' Enable serial port and continuous receive
    'TXSTA = %00100100						' Enable transmit and asynchronous mode with BRGH 1
    'SPBRG = 20								' Set baud rate to 57600 with 20mhz clock (BRGH = 1)
    
    ###A load of variables go here###
    
    TRISE = %00000111							' Set PortE to all inputs
    TRISD = %00000000							' Set PortD to all outputs
    TRISC = %00000001							' Set PortC to all outputs
    TRISB = %00000000							' Set PortB to all outputs
    TRISA = %11111111	 						' Set PORTA to all input
    ADCON1 = 0 									' PORTA is analog
    
    PORTB = 0
    PORTC = 0
    PORTD = 0
    
    gosub TMR1setup
    HPWM 1,127,1000 
    SEROUT2 PORTB.5,16390,["INITIALISED",10,13]
    
    TMR1setup:
    TMR1L = 0							' reset TMR1 low to 0
    TMR1H = 0							' reset TMR1 high to 0
    PIE1.0 = 0							' Disable TMR1 interupt (double check)
    T1CON.5 = 0 						' Setup TMR1 timings 1:1
    T1CON.4 = 0							' Setup TMR1 timings 1:1
    T1CON.3 = 1							' Oscillator is on
    T1CON.2 = 1							' Do not synchronize external clock input
    T1CON.1 = 1							' External clock from pin RC0
    T1CON.0 = 1							' Enables Timer1
    return
    so the HW PWM is set with
    Code:
    HPWM 1,127,1000
    and i've checked the syntax.

    Can anyone see whats wrong?

    Thanks

  2. #2
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by comwarrior View Post
    I've got HWPWM 1 (RC2/CCP1) feeding into TMR1 counter input (RC0/T1CKI).
    I want the PWM set at 1KHz @ 50%...
    You've turned on the Timer1 Oscillator (T1CON.3), which would be used if you had a 32khz crystal on Timer1.
    For pulses on T1CKI the T1OSC needs to be turned OFF.

    The CCP module has a minimum frequency of 1,465hz when using a 24mhz crystal. But you shouldn't be running a 16F877A at 24mhz anyway. The max is 20Mhz.
    But even at 20mhz, the minimum PWM frequency is 1221hz.

    The following line does absolutely nothing. It does NOT set the configs.
    Which brings the question ... What are your CONFIGs set to?
    Code:
    Define  CONF_WORD = 	$3F3A
    The bs1defs.bas include file can help when trying to use a Basic Stamp 1 program with PicBasic Pro.
    If you're writing the program from scratch in PBP, it's of no use.
    Code:
    include "bs1defs.bas"
    8 bits is default to begin with. This line does nothing.
    Code:
    DEFINE  SER2_BITS		8	' Ser2 number of data bits
    If you will be using ADCIN then you need to put the AD clock setting in the ADC_CLOCK define.
    By setting the individual bits in ADCON0, they will be overwritten every time an ADCIN statement is used.
    And PBP defaults to AD clock 3 (internal osc).
    Code:
    Define  ADC_BITS        10     		' Set number of bits in result
    Define  ADC_SAMPLEUS    100    		' Set sampling time in uS
    ADCON0.6 = 1				' ADC clock set to Fosc/64
    ADCON0.7 = 1				' ADC clock set to Fosc/64
    ADCON1.6 = 0				' ADC clock set to Fosc/64
    Interrupts are automatically disabled on any Reset.
    No need for this line.
    Code:
    INTCON = 0				' Disable all interupts
    Sets PORTA AND PORTE as Analog.
    And yes, ADCON1 already defaults to 0.
    Code:
    ADCON1 = 0 				' PORTA is analog
    You GOSUB to TMR1setup, then program execution "Falls into" the TMR1setup routine again.
    When it hits the RETURN, the program will jump off into la-la land.
    But maybe you just didn't include the parts in-between.
    Code:
    gosub TMR1setup
    HPWM 1,127,1000 
    SEROUT2 PORTB.5,16390,["INITIALISED",10,13]
    
    TMR1setup:
    TMR1L = 0							' reset TMR1 low to 0
    TMR1H = 0							' reset TMR1 high to 0
     ...
    Bit's do not randomly turn themselves ON/OFF, and PIE1 defaults to all 0's on RESET.
    If the bit wasn't set before (it wasn't) then there's no need to turn it off.
    Code:
    PIE1.0 = 0			' Disable TMR1 interupt (double check)
    DT

  3. #3


    Did you find this post helpful? Yes | No

    Default

    You've turned on the Timer1 Oscillator (T1CON.3), which would be used if you had a 32khz crystal on Timer1.
    For pulses on T1CKI the T1OSC needs to be turned OFF.
    I tried it both ways when i discoverd i had a problem and i couldn't find a yes or no as to wether it should be on or off...

    The CCP module has a minimum frequency of 1,465hz when using a 24mhz crystal. But you shouldn't be running a 16F877A at 24mhz anyway. The max is 20Mhz.
    But even at 20mhz, the minimum PWM frequency is 1221hz.
    Datasheet mentions nothing about a minimum frequency...
    As for running at 24MHz... it runs very nicely, i'm tempted to try a 33MHz xtal just to see if it runs...

    The following line does absolutely nothing. It does NOT set the configs.
    Which brings the question ... What are your CONFIGs set to?
    I'm aware this line doesn't do what it's supposed to... i havn't found the correct line yet...
    basically, everything off, xtal set as HS

    The bs1defs.bas include file can help when trying to use a Basic Stamp 1 program with PicBasic Pro.
    If you're writing the program from scratch in PBP, it's of no use.
    Indeed, it was quick and dirty...

    8 bits is default to begin with. This line does nothing.
    I decided it safer not to trust the 'default' settings and set them anyways. that way i don't need to worrie about the defaults nor do i need to worrie about something getting set/reset uppon a brownout...

    If you will be using ADCIN then you need to put the AD clock setting in the ADC_CLOCK define.
    By setting the individual bits in ADCON0, they will be overwritten every time an ADCIN statement is used.
    And PBP defaults to AD clock 3 (internal osc).
    oops, missed a setting... now added ADC_CLOCK 3 line

    You GOSUB to TMR1setup, then program execution "Falls into" the TMR1setup routine again.
    When it hits the RETURN, the program will jump off into la-la land.
    But maybe you just didn't include the parts in-between.
    I have a goto main line at the end of my init... i allready said the software has been running in sim and in live PIC...

    Thanks for the tips darrel it's appreciated...

  4. #4


    Did you find this post helpful? Yes | No

    Default

    now getting pulses into tmr1...

    Thats another one for the FAQ's

    Thanks darrel

  5. #5
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    At this point, I'm not sure which part helped.

    But I'm happy to have provided any tidbits that might have applied.

    Best regards,
    DT

  6. #6


    Did you find this post helpful? Yes | No

    Default

    darrel,
    apologies, should have said...

    it seems to be the minimum PWM frequency...
    Having just said that, it seems to have stopped working

Similar Threads

  1. Half-bridge PWM with a 16F684 ?
    By Byte_Butcher in forum General
    Replies: 7
    Last Post: - 17th January 2010, 22:18
  2. Variable PWM PIC18F2431
    By trr1985 in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 12th June 2009, 06:03
  3. PWM setting PIC16F887
    By Gevo in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 12th June 2008, 07:24
  4. PWM instruction not working as I expect
    By peu in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 20th January 2006, 19:39
  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 : 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