We're just messing with you when you miss something simple. Don't take any of it personal..
When you forget to loop back to Main it runs off the end and loops back to the start. But that wasn't the only problem.
PCPWM on these can be a bit intimidating. Give this a shot.
Code:
DEFINE OSC 20
Duty VAR Word
TRISA = %11111111
TRISB = %00000000
TRISC = %00000010
TRISD = %00000000
TRISE = %00000000
' Analog setup
' Single shot, single channel, Group A is taken and converted, A/D converter enabled
ADCON0 = %00000001
ADCON1 = %00010000
ADCON2 = %11001111 ' Right justified, 24 Tad, Frc
ADCON3 = 0 ' Disable all triggers
ADCHS = 0 ' Channel select for AN0
ANSEL0 = %00000001 ' AN0 analog input, rest digital
PORTB = 0
PORTD = 0 ' clear port
PTCON0 = 0 ' timebase
PTPERL = 0 ' ~19.4kHz at 20MHz
PTPERH = 1
PWMCON0 = %01001111
PWMCON1 = %00000001
DTCON = 0
OVDCOND = 0
OVDCONS = 0
PTCON0 = 0
PTCON1 = %10000000
OVDCOND = %00000010 'PWM on PWM1
Main:
ADCON0.1 = 1 ' Start the conversion
While ADCON0.1=1 ' Wait for it to complete
Wend
Duty.HighByte = ADRESH ' get result from AN0
Duty.LowByte = ADRESL ' Increment buffer pointer
PDC0L = Duty.LowByte
PDC0H = Duty.HighByte
PDC1L = Duty.LowByte
PDC1H = Duty.HighByte
PDC2L = Duty.LowByte
PDC2H = Duty.HighByte
PAUSE 500
GoTo Main
All-in-all I would say you're doing pretty darn good just getting started with one of the hardest to use/understand PIC types in this range....
Bookmarks