Is this all of your code? If it is, can you explain what you "think" should be happening, and how you know it's not?
Folks here love to help, but what you're showing here doesn't indicate even a modest amount of homework..;o)
Is this all of your code? If it is, can you explain what you "think" should be happening, and how you know it's not?
Folks here love to help, but what you're showing here doesn't indicate even a modest amount of homework..;o)
If it is the entire code, maybe looping back to main would help? Otherwise I agree with Bruce.
-Bert
The glass is not half full or half empty, Its twice as big as needed for the job!
http://foamcasualty.com/ - Warbird R/C scratch building with foam!
Something like this might help so it doesn't drop-off the end and restart from the beginning. It also loads the new duty-cycle values.
It's untested, but should work. I didn't check the A/D part, but it looks ok at a glance.Code: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 GoTo Main
You might want to insert a PAUSE before the GOTO Main?
Last edited by Bruce; - 13th July 2011 at 11:46.
Hello,
Thank you very much for your answers guys ! For you that code is nothing but for me it is what I wrote after spending hours on the forum and on the datasheet... trying to understand what i'm writing. So I am sorry if you feel that way..
Bruce, thank you for putting in the right order my code, it is working now but somehow when my potentiometer is at its maximum i'm only able to have 50% duty cycle ? Where does this come from ?
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.
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....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![]()
Hi bruce,
Thank you for your quick reply, and yes apparently this chip is pretty complicated but well I do not mindit's challenging.
I have tried your code, changing the register "PTPERH" seems to do changing, I now have around 66% duty cycle.
Now i'm looking at the datasheet of the pic page 187, and I am wondering one thing and maybe it has to do something with my duty cycle problem.
Figure 18-11 shows the duty cycle comparison. Now PTMR(H and L) has 14 bits with 2 reserved for a clock and PDC(H and L) register has 14 bits as well.
However, tell me if I am wrong but the analog digital converter module only operate on 10 bit. Would this be one of the reason ?
If I could figure this out, it'd be amazing.. Thank you again for your help, I really appreciate !
You're getting the picture..
12kHz you tried before bumped it up over 10-bit resolution. If you're stuck with 10-bit A/D just figure out a frequency with 10-bit resolution.
I think 12kHz worked out to around 10.7-bit, but it was enough to shift it up >10-bits.
Bookmarks