PDA

View Full Version : adcin and HPWM



lerameur
- 19th September 2008, 02:03
HI

I have a program here, I guess it is somewhat working. I get a very erratic output, I need a steady frequency with a varying duty cycle. What I see on the scope is varying duty cycle frequency going in all direction, without me touching the circuit. I have set a pot on analog pin 1 . I can see the voltage varying from 0 to 5 with the multimeter , so this is working, I have a 47k on the MCLR pin and 22pF cap on the 20Mhz oscillator. Can somebody see the problem,
I may have gotten the initialization wrong or something like it
Thanks

INCLUDE "modedefs.bas" 'Includes supoprt for PicBasic language
@ DEVICE pic16F88, HS_OSC , CCPMX_ON
DEFINE OSC 20 'use external 20mhz crystal
PAUSE 100 ' start-up delay

'/////////////////////////
'// PIN configuration //
'/////////////////////////

DEFINE CCP1_REG PORTB 'Define output port of pulses out
DEFINE CCP1_BIT 3 'Define output port of pulses out
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 uSec

CMCON = 7 ' Disable analog comparator
ANSEL = %00000010 ' set AN6 (RA1) as analog, others to digital
ADCON1.7 = 1 ' Right justified results... duh... really needed in 8 bits???
ADCON0 = %11000001 ' Configure and turn on A/D Module

TRISB = %00000000 ' Set PORTB to all output
TRISA = %11111111 ' Set PORTA to all input


'///////////////////////////////////////////////
'// Variable Declaration and initialization //
'///////////////////////////////////////////////

DutyCycle var byte 'Create adval to store result

'//////////////////////////
'// Program starts here //
'//////////////////////////

Mainloop:
ADCON0.2 = 1 'Start Conversion

ADCIN 1, DutyCycle 'analog pin 1 get the 8 bit result

HPWM 1,DutyCycle,10500

GOTO Mainloop
end

skimask
- 19th September 2008, 03:25
I have a program here, I guess it is somewhat working.
It's a start eh? :)


I get a very erratic output, I need a steady frequency with a varying duty cycle. What I see on the scope is varying duty cycle frequency going in all direction,
Are you sure it's actually the frequency varying and not the 'scope triggering at different points?

Do you have an LCD (or anything similar) that you can output the variable DUTYCYCLE to, so you can monitor it?
How about a small cap across the ADC input pin to smooth that out just a bit? Most pot's aren't exactly clean...
And try left justify...16F88 has a 10 bit ADC. PBP manual says 8 bit ADC results should be LEFT justified. Maybe a high sampling time.

lerameur
- 19th September 2008, 03:45
Ya thats it, ADCON1.7 = 0

works fine

thanks skimask

K

skimask
- 19th September 2008, 04:02
Ya thats it, ADCON1.7 = 0
works fine
thanks skimask
K

I'd still put a small cap across the A/D input you're using.
Might kill off any stray spikes you get from a dirty pot...

lerameur
- 19th September 2008, 13:24
Hi

works better with cap.
Although I still have problem with the HPWM. I decided to put a value in for the duty cycle, and I am not getting the right duty cycle out. I mean it is going from 10 to 80% to 50%, its all over the place:


INCLUDE "modedefs.bas" 'Includes supoprt for PicBasic language
@ DEVICE pic16F88, HS_OSC , CCPMX_ON
DEFINE OSC 20 'use external 20mhz crystal
PAUSE 100 ' start-up delay

'/////////////////////////
'// PIN configuration //
'/////////////////////////

DEFINE CCP1_REG PORTB 'Define output port of pulses out
DEFINE CCP1_BIT 3 'Define output port of pulses out
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 uSec

CMCON = 7 ' Disable analog comparator
ANSEL = %00000010 ' set AN1 (RA1) as analog, others to digital
ADCON1.7 = 0 ' Right justified results... duh... really needed in 8 bits???
ADCON0 = %11000001 ' Configure and turn on A/D Module

TRISB = %00000000 ' Set PORTB to all output
TRISA = %11111111 ' Set PORTA to all input


'///////////////////////////////////////////////
'// Variable Declaration and initialization //
'///////////////////////////////////////////////

DutyCycle var byte 'Create adval to store result

'//////////////////////////
'// Program starts here //
'//////////////////////////

Mainloop:
ADCON0.2 = 1 'Start Conversion

ADCIN 1, DutyCycle 'analog pin 1 get the 8 bit result

HPWM 1,166,10800

GOTO Mainloop
end

skimask
- 19th September 2008, 13:25
Don't keep overwriting the duty cycle value. Just write it once and let it run unless it changes.
If you look at the datasheet, you'll see that a few things tend to reset themselves whenever the duty cycle register is written, thereby screwing up your duty cycle.

lerameur
- 19th September 2008, 14:31
I just tested the program above to test the HPWM command, in reality I want to use the following:

Mainloop:
ADCON0.2 = 1 'Start Conversion
ADCIN 1, DutyCycle 'analog pin 1 get the 8 bit result
HPWM 1,DutyCycle,10800
GOTO Mainloop
end

but even with
HPWM 1,166,10800
I am not getting the right duty cycle.

Do I need to define HPWM1_TIMER?

k

skimask
- 19th September 2008, 14:36
What are you getting for an output?
Skip the ADC for now, and just run a HPWM command, nothing else, with a duty cycle of 127. Should output a nice clean 50% on, 50% off square wave.

Acetronics2
- 19th September 2008, 14:36
Hi, Ken

Just change the dutycycle IF there's a change in ADC result ...

Alain

skimask
- 19th September 2008, 14:38
Just change the dutycycle IF there's a change in ADC result ...
Isn't that what I said earlier? :D

lerameur
- 19th September 2008, 14:58
oK I will try this when I get home tonight.

I did some change in my program, mostly in the Pin Configuration section.
If this do not work, I will remove all the ADC stuff and just try the HPWM


INCLUDE "modedefs.bas" 'Includes supoprt for PicBasic language
@ DEVICE pic16F88, HS_OSC , CCPMX_ON
DEFINE OSC 20 'use external 20mhz crystal
PAUSE 100 ' start-up delay

'/////////////////////////
'// PIN configuration //
'/////////////////////////

DEFINE CCP1_REG PORTB 'Define output port of pulses out
DEFINE CCP1_BIT 3 'Define output port of pulses out

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 uSec

CMCON = 7 ' Disable analog comparator
ANSEL = %00000010 ' set AN1 (RA1) as analog, others to digital
ADCON1 = %00000000 ' Left justified results in 8 bits
ADCON0 = %10000001 ' Configure and turn on A/D Module

TRISB = %00000000 ' Set PORTB to all output
TRISA = %11111111 ' Set PORTA to all input

'///////////////////////////////////////////////
'// Variable Declaration and initialization //
'///////////////////////////////////////////////

DutyCycle var byte 'Create adval to store result

'//////////////////////////
'// Program starts here //
'//////////////////////////

Mainloop:
ADCON0.2 = 1 'Start Conversion

ADCIN 1, DutyCycle 'analog pin 1 (RA1) get the 8 bit result
pause 50

HPWM 1,DutyCycle,10800
pause 100

GOTO Mainloop
end

skimask
- 19th September 2008, 15:05
Maybe try this instead:


INCLUDE "modedefs.bas" 'Includes supoprt for PicBasic language
@ DEVICE pic16F88, HS_OSC , CCPMX_ON
DEFINE OSC 20 'use external 20mhz crystal
PAUSE 100 ' start-up delay

'/////////////////////////
'// PIN configuration //
'/////////////////////////

DEFINE CCP1_REG PORTB 'Define output port of pulses out
DEFINE CCP1_BIT 3 'Define output port of pulses out

Define ADC_BITS 8 ' Set number of bits in result
Define ADC_CLOCK 3 ' Set clock source (3=rc)
Define ADC_SAMPLEUS 100 ' Set sampling time in uSec

CMCON = 7 ' Disable analog comparator
ANSEL = %00000010 ' set AN1 (RA1) as analog, others to digital
ADCON1 = %00000000 ' Left justified results in 8 bits
ADCON0 = %10000001 ' Configure and turn on A/D Module

TRISB = %00000000 ' Set PORTB to all output
TRISA = %11111111 ' Set PORTA to all input

'///////////////////////////////////////////////
'// Variable Declaration and initialization //
'///////////////////////////////////////////////

DutyCycle var byte 'Create adval to store result
LastDutyCycle var byte 'hold last time around

'//////////////////////////
'// Program starts here //
'//////////////////////////

Mainloop:
ADCON0.2 = 1 'Start Conversion

ADCIN 1, DutyCycle 'analog pin 1 (RA1) get the 8 bit result
pause 50

If dutycycle <> lastdutycycle then
lastdutycycle = dutycycle
HPWM 1,DutyCycle,10800
Endif

GOTO Mainloop
end

rmteo
- 19th September 2008, 15:12
I'm guessing that whenever this is executed:

HPWM 1,DutyCycle,10800
PBP is stopping, setting up and restarting the HPWM.

If you do this in the loop,

CCPR1L = DutyCycle
then your problem should go away. I don't know if PBP allows you to do this. If not, you will have to set up the hardware manually.

PR2 = 462 '10,800Hz frequency
CCPR1L = 0 'Initial duty cycle
CCP1CON = %1100 'PWM mode
T2CON.2 = 1 'Start TIMER2

lerameur
- 20th September 2008, 00:35
I tried the following program, very simple, still not getting anything out..
using internal osc, trying to cut down on the hardware... :)



'Test HPWM command
'
INCLUDE "modedefs.bas" 'Includes supoprt for PicBasic language
@ DEVICE pic16F88, CCPMX_ON, INTRC_OSC_NOCLKOUT

OSCCON = %110 ' Set OSC TO 4MHZ 'select the clock


'DEFINE OSC 20 'use external 20mhz crystal
PAUSE 100 ' start-up delay

'/////////////////////////
'// PIN configuration //
'/////////////////////////

DEFINE CCP1_REG PORTB 'Define output port of pulses out
DEFINE CCP1_BIT 3 'Define output port of pulses out

CMCON = 7 ' Disable analog comparator

TRISB = %00000000 ' Set PORTB to all output
TRISA = %11111111 ' Set PORTA to all input

'///////////////////////////////////////////////
'// Variable Declaration and initialization //
'///////////////////////////////////////////////

'//////////////////////////
'// Program starts here //
'//////////////////////////
HPWM 1,126,3000
PAUSE 200 ' delay

Mainloop:

GOTO Mainloop
end

skimask
- 20th September 2008, 00:53
I tried the following program, very simple, still not getting anything out..
using internal osc, trying to cut down on the hardware... :)

Which pin on the PIC are you looking at? And how are you looking at it?

lerameur
- 20th September 2008, 01:09
I am looking at B.3 through my scope.
I am using the same hardware as my digital thermometer now, which was using B.3 to get the temperature and have a LCDout. (and that circuit is working fine, but no a/d or HPWM)
So I removed that wire on B.3 (of course) , placed the new chip in place and I am not seeing anything to the scope

k

lerameur
- 20th September 2008, 01:13
CCPMX_ON .... that should be off....

lerameur
- 20th September 2008, 01:25
I found a working program from steve in another thread:
@ DEVICE MCLR_ON, INTRC_OSC, WDT_ON, LVP_OFF, BOD_OFF, PWRT_ON, PROTECT_OFF, CCPMX_ON
OSCCON=$60 ' use internal 4MHZ osc
PAUSE 100 ' start-up delay
TRISB=0
DEFINE CCP1_REG PORTB
DEFINE CCP1_BIT 3
HPWM 1,128,1000
HERE: GOTO HERE

I just change the oscillation to 20Mhz, and it si not working: I think this is the problem..
I change the program to:

@ DEVICE MCLR_ON, HS_OSC, WDT_ON, LVP_OFF, BOD_OFF, PWRT_ON, PROTECT_OFF, CCPMX_ON
DEFINE OSC 20 'use external 20mhz crystal 'OSCCON=$60 ' use internal 4MHZ osc
PAUSE 100 ' start-up delay
TRISB=0
DEFINE CCP1_REG PORTB
DEFINE CCP1_BIT 3
HPWM 1,128,1000
HERE: GOTO HERE

skimask
- 20th September 2008, 01:37
CCPMX_ON .... that should be off....
That's one problem I'm thinking of. Not reading the right pin...


I found a working program from steve in another thread:

Look at your manual under HPWM. Can't do 1000 Hz at 20Mhz...

lerameur
- 20th September 2008, 01:57
19531Hz for a 20Mhz

I remember using 5Khz for one of my project for motor driver PWM.

How can I change steve's program to use a 20Mhz crystal?

skimask
- 20th September 2008, 05:07
19531Hz for a 20Mhz

I remember using 5Khz for one of my project for motor driver PWM.

How can I change steve's program to use a 20Mhz crystal?

The MINIMUM is 1221 hz, the max is 19531 hz. Can't do a lot about is, built into the hardware.
Steve has a program called MultiCalc. Check it out...

lerameur
- 20th September 2008, 12:32
well 10800hz fall well within those range , so no problem there.

k

skimask
- 20th September 2008, 15:52
well 10800hz fall well within those range , so no problem there.

Ok... In the last couple of posts, the freq wasn't 10.8khz.

lerameur
- 20th September 2008, 16:38
I changed the header to
INCLUDE "modedefs.bas" 'Includes supoprt for PicBasic language
@ DEVICE pic16F88,CCPMX_ON

' Setup Crystal oscillator Frequency to be used by PIC chip in MHz
DEFINE OSC 20 'use external 20mhz crystal


and it is now working.

Ok... In the last couple of posts, the freq wasn't 10.8khz.
....really? I only see 10800hz in my post, where is the typo

k

skimask
- 20th September 2008, 16:49
I changed the header to
INCLUDE "modedefs.bas" 'Includes supoprt for PicBasic language
@ DEVICE pic16F88,CCPMX_ON
' Setup Crystal oscillator Frequency to be used by PIC chip in MHz
DEFINE OSC 20 'use external 20mhz crystal
and it is now working.
Ok... In the last couple of posts, the freq wasn't 10.8khz.
....really? I only see 10800hz in my post, where is the typo
k

In post #18, you've got 1000hz...

So, changing the header fixed it and all is good now?
Nuts ain't it? :D