PDA

View Full Version : PIC 12f683 AD/PWM Problems:



surfer0815
- 7th December 2005, 14:20
Hi!
Iīve got a Problem again. This time I read an 10 bit AD Value an want to put it Out as PWM on Port GP2
Something is wrong with the AD module.
Here is my Code:

'READ 10 BIT AD Value and put out as PWM

@ device pic12f683, fcmen_off, ieso_off, intrc_osc, wdt_off
@ device pic12f683, pwrt_on, mclr_off, protect_off

GPIO = %00000000 ' All outputs low
CMCON0 = 7 ' Comparators off
TRISIO = %00000001 ' Set GP0 in and Rest Out 0=Output
ANSEL = %00000001 'Set PORTGPIO.0 analog rest digital
'WPU=%00110000 ' Pull-ups on for GPIO.4 & 5 inputs
'OPTION_REG.7=0 ' Enable internal pull-ups

' Define ADCIN parameters
Define ADC_BITS 10 ' 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 GPIO 'PWM Output on gpio
Define CCP1_BIT 2 'Bit 2

adval Var Word ' Create adval to store result
Pause 100

loop:
ADCIN 0, adval ' Read channel AN0 to adval

HPWM 1,adval,500 ' PWM between 0-1024 , 500Hz on GP2

Pause 100

Goto loop ' Do it forever

End
---------------------------------------------------------------------

If I donīt Use the ADCIN, and would say adval = 100 or something else between 0 an 255, I can see the right PWM with the scope coneccted to GP2
but if adval gets the Value from ADCIN, the PWM Output sometimes is on or off, It looks like that there some definition is missing. I couldnīt imagine which one this could be.

Acetronics2
- 7th December 2005, 15:12
hi, Surfer

The ADC result is between ???
The PWM valid input range is between ???

SCALING forgotten ...

Alain

Melanie
- 7th December 2005, 16:47
Surfer... go check in the PICBasic documentation and come back and tell me if the HPWM command allows eight bits or ten bits...

CocaColaKid
- 7th December 2005, 16:48
HPWM and PWM is between 0-255 and a 10 bit read for the a/d results in a 0-1023 reading. Divide a/d result by 4 or reduce resolution to 8 bit.