PDA

View Full Version : Erratic behavior on a HPWM on a 16F88



lerameur
- 24th April 2013, 18:21
HI,
Wrote a short program to be able to input two ADCIN so I could have a variable duty cycle and variable frequency at my HPWM.
Duty cycle works fine, but the frequency output is very erratic,
example at 1v I get 500Hz
1.2v 2.2Khz
1.3v 300Hz
1.4v 620Hz
I mean the frequency is all over the place, , here is my code:


INCLUDE "modedefs.bas" 'Includes support for PicBasic language

OSCCON = %01110000 '8 Mhz
DEFINE OSC 8

CMCON = 7

' Define ADCIN parameters
Define ADC_BITS 8 ' Set number of bits in result
Define ADC_CLOCK 3 ' Set clock source (3=rc)
Define ADC_SAMPLEUS 50 ' Set sampling time in uS

DEFINE CCP1_REG PORTB
DEFINE CCP1_BIT 0

ANSEL = %00000011 ' set AN0 & AN1 as analog, others to digital

ADCON1 = %00000010 ' Set PORTA analog and RIGHT justify result

TRISB = %01000010
TRISA = %00000111

DutyCycle var byte
Frequency var byte

Mainloop:
'ADCON0 is binary '11000001' to read from AN0, and binary '11001001' to read from AN1.
ADCON0 = %11000001 'Start Conversion
pause 100
ADCIN 0, DutyCycle 'Read channel PORTA.0 Duty Cycle
pause 30

ADCON0 = %11001001 'Start Conversion
pause 100
ADCIN 1, Frequency 'Read channel PORTA.1 Frequency
pause 30

HPWM 1,DutyCycle,Frequency 'channel, dutycycle, frequency
pause 20

goto Mainloop
END

Any ideas ??

thanks
ken

Acetronics2
- 24th April 2013, 18:50
Hi, Ken

1) just modify Duty and Frequency ... ONLY if pots inputs changes.
no need any PAUSE then ...

2) "Frequency" is a Byte ... and the minimum available frequency is ... 489 Hz !!!
rings you a bell somewhere ???



Alain

Demon
- 24th April 2013, 19:21
Do you need PAUSE 100 after setting ADCON0?

Robert

lerameur
- 24th April 2013, 20:29
Alain,
why should it change the data on 'only' if POT changes. It still should give me the same value and not going from 100hz then 2khz !!!
Y I have 256 bits so I SHOULD BE GETTING MAX 256 Hz .......
Like I said the duty cycle works fine, on both AN0 And AN1 !!
I will try the Dont change thing, because i have no more idea
ken

lerameur
- 24th April 2013, 21:06
I am getting 1200 Hz with this HPWM command:
HPWM 1,DutyCycle,350 'channel, dutycycle, frequency
Do not make sense..... My OSCON is good at 8Mhz

Demon
- 24th April 2013, 22:43
Try fixed values just to test setup.

Robert

wdmagic
- 24th April 2013, 23:12
could it be a ADCON1 issue, maybe its not justifing? are you using a crystal or internal osc? throwing ideas here.
Do you have to set CCP1CON

CCP1CON = %00??1111
see page 81

oh and you need to modify TRISB = %01000010 to TRISB = %01000011
see page 82, your pwm port must be configured as a input port.

lerameur
- 25th April 2013, 10:00
changed the TRIS but that didn,t do it,
I only have this line of code:
HPWM 1,127,150 'channel, dutycycle, frequency
pause 20
and its giving me 8.3kHz at 50% DC ......

AvionicsMaster1
- 25th April 2013, 14:59
Could it be your defines for your ADC being in mixed case? I was reschooled in that recently.

On your OSCCON statement, are the last 4 bits what you want? Assuming you're using an internal oscillator, Bit 3 should be 1 for internal clock, Bit 2 should be 1 for stable clock, Bits 0-1 as 00 uses Fosc to set oscillator.

ADCON1 bits 0-3 are undefined. I'm assuming you want your Vref to be right justified with Vref and AVss so the ADCON1 should look like this 10110000. It says in the data sheet you need to set ANSEL AN2 and AN3 to inputs to use Vref. So make sure to change your ANSEL settings to 00001111 for AN0 to AN3 as inputs.

I'm also wondering if your ADCON0 bit 2 should be a 1 to start the conversion process. If it's a 0 I wonder if the conversion process will even begin.

I'm also assuming TRISB is for a part of the program not shown.

All that said I'm not a true "programmer" and I must just be creating more fodder. Best wishes.

lerameur
- 25th April 2013, 16:03
Hi,
thanks for answering, changed the OSCON but that did not help,
this is my program:

Mainloop:
hPWM 1,127,150 'channel, dutycycle, frequency
pause 20

goto Mainloop
END

Therefore no ADCON to worry about, and I'm getting in the Kilohertz output...
something wrong with the clocking output of the frequency

K

peterdeco1
- 25th April 2013, 16:29
Your code is very similar to something I did a long time ago. I haven't tried it but try this:


OSCCON = $70 '8mhz
adcon1 = 7 ' set inputs to digital - the adcin command automatically converts them to analog
@ DEVICE MCLR_ON, INTRC_OSC, WDT_ON, LVP_OFF, BOD_OFF, PWRT_ON, PROTECT_ON

DEFINE OSC 8

CMCON = 7

TRISB = %01000010
TRISA = %00000111

DutyCycle var byte
Frequency var byte

Mainloop:
ADCIN 0, DutyCycle 'Read channel PORTA.0 Duty Cycle
ADCIN 1, Frequency 'Read channel PORTA.1 Frequency
HPWM 1,DutyCycle,Frequency 'channel, dutycycle, frequency
goto Mainloop
END

HenrikOlsson
- 25th April 2013, 17:55
Hi,
Alain already wrote this in the very first reply but it seems to have gone unnoticed. When using the CCP-module in the PIC to generate a PWM output (which is what the HPWM command does) there's lower limit on the frequency depending on the frequency of the PICs oscillator. For a 14-bit PIC (which the 16F88 is) running at 8MHz that lower limit is 489Hz. All according to the manual. You're trying to set the frequency to 150Hz when it can't go lower than 489Hz, it's probably overflowing.

And, which Alain also wrote, in the original code you had the Frequency variable declared as a BYTE. That won't work since the lowest possible value you can assign to that variable when the PIC is running at 8MHz is again 489 so you must declare Frequency as a WORD.

Finally, I seem to remember something about HPWM not being suitable for continous update like this, probably because it sets up the CCP module for PWM each and every time but I'm not sure. I know that Darrel (of course...) wrote a routine better suited for that - and that was able to use the full 10bit resolution when available. Search for HPWM10 or something like that.

/Henrik.

lerameur
- 25th April 2013, 20:28
Hi,
thanks, I must of read it quickly, I thought that was the maximum frequency!!
thank you
.. working now with higher frequency