![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
pacman91
Joined: 17 Jun 2011 Posts: 28 Location: Malaysia
|
Help me take a look on my code |
Posted: Mon Jul 04, 2011 5:06 am |
|
|
Code: | #include <18F4520.h>
#DEVICE ADC=10
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=4000000)
//#define PWN_PIN PIN_B0
#include <flex_lcd.c>
void main(void)
{
long ADC_Result;
output_high(LCD_POWER); // Turn on power to LCD
output_low(LCD_RW);
lcd_init();
setup_adc(ADC_CLOCK_INTERNAL);//enables the a/d module
//and sets the clock to internal adc clock
setup_adc_ports(AN0); //sets all the adc pins to analog
set_adc_channel(0); //the next read_adc call will read channel 0
setup_ccp1(CCP_PWM);
setup_timer_2(T2_DIV_BY_16,115,10); //1.84ms
set_pwm1_duty(0);
while (1) {
ADC_Result = read_ADC();
set_pwm1_duty(ADC_Result);
printf(lcd_putc,"\f%3lu", ADC_Result); //Vary motor direction with ADC result.
delay_ms(18);
delay_us(160); //18.16ms
}
}
|
I using the potentiometer to control the ADC_result, to set the duty cycle 0%-100% of 1.84ms, then delay 18.16ms to get the 20ms period cycle.
But when I running the program in the PIC connect the PIN RC2( CCP1) to the oscilloscope, the output waveform period is 0ms ~ 4ms, changing when i adjusting the potentiometer, didn't get any period of 20ms, why?
Code: | printf(lcd_putc,"\f%3lu", ADC_Result); |
confusing about printf command
if i didn't put "\f%3lu", the LCD wont show out any value, and i saw other people program have alot more command like "\n\r", "\f TEST \nInitial- 600us", where can I learn about it? |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19366
|
|
Posted: Mon Jul 04, 2011 8:28 am |
|
|
You won't.
The PWM, generates a _continuous_ stream of pulses at the programmed period/duty cycle. It doesn't stop after one pulse. The stream keeps going at the speed you have set, hence no 18.16mSec. All the delay is doing, is slowing when the next update of the PWM duty cycle comes.
To get a single pulse, you could use the CCP, not in PWM mode, to 'clear on match'. Turn the pin 'on', and then after the programmed time the CCP will turn the pin off, or simply turn the pin on, and delay for the time you want before turning it off.
Now some comments about the rest of the code:
1). Is ADC_CLOCK_INTERNAL recommended for 4MHz operation?.....
2) You remark 'sets all the adc pins to analog'. Your setting sets _just_ A0 to analog.
3) How long does the ADC take to acquire a signal after selection. Will you have this time on the first read?. - at 4Mhz, probably, but _check_.
4) How long will the print take?. A single character only takes perhaps 50uSec, but a '/f' (CLS), typically takes nearly 2mSec. Your timing is going to be 'out' by this amount....
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|