I'm having a bit of trouble trying to get an adjustable pwm signal out of a 16F684. My version of MPLAB is 8.0 and MPASM is 5.14. The code was pieced together from posts on this forum and a few other sites as well. What I'm hoping to do is get an adjustable 6khz pwm signal with a 0-80% duty cycle. Pin 12 is connected to the wiper of a 10k pot. Looking at the output it seems the duty cycle is changing very slightly (1-2%) over the entire travel. Anything look out of place?
INCLUDE "modedefs.bas" 'Includes supoprt for PicBasic language
OSCCON = %01110001
PAUSE 100 ' start-up delay
'/////////////////////////
'// PIN configuration //
'/////////////////////////
'DEFINE CCP1_REG PORTC '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
CMCON0 = 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
TRISC = %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,6000
Endif
GOTO Mainloop
end
Bookmarks