Log in

View Full Version : HW PWM not working



comwarrior
- 20th July 2009, 05:27
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...


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
HPWM 1,127,1000 and i've checked the syntax.

Can anyone see whats wrong?

Thanks

Darrel Taylor
- 20th July 2009, 11:33
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?

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.

include "bs1defs.bas"

8 bits is default to begin with. This line does nothing.


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).


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.

INTCON = 0 ' Disable all interupts

Sets PORTA AND PORTE as Analog.
And yes, ADCON1 already defaults to 0.

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.

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.

PIE1.0 = 0 ' Disable TMR1 interupt (double check)

comwarrior
- 22nd July 2009, 03:37
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...

comwarrior
- 23rd July 2009, 00:41
now getting pulses into tmr1...

Thats another one for the FAQ's

Thanks darrel

Darrel Taylor
- 23rd July 2009, 02:10
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,

comwarrior
- 31st July 2009, 17:16
darrel,
apologies, should have said...

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