![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
jake1272
Joined: 15 Mar 2021 Posts: 37
|
Digital Capture and PWM Control |
Posted: Mon Apr 05, 2021 1:56 am |
|
|
How can I implement a code to input a frequency value via RS232 and set the output to it?
Here is my attempt.
Code: | #include <16F1847.h>
#fuses INTRC_IO,NOWDT,NOPROTECT, MCLR, NOLVP
#use delay(clock=8000000)
//#USE PWM(FREQUENCY=25000,DUTY=50,OUTPUT=PIN_B3) //Configure initial PWM signal
#use RS232(baud=9600, UART1) //RS232 on Pin B1 and Pin B2
//Oscilloscope on CCP pin B3
//float freq,
float TMR2count;
unsigned int16 duty;
void SET_SIGNAL(){
setup_timer_2(T2_DIV_BY_16,TMR2count,1); //Setup Timer2 with calculated counr from input frequency from RS232
setup_ccp1(CCP_PWM); //Configure CCP3 for PWM
duty = 500;
set_pwm1_duty(duty);
}
void main(){
float freq;
//delay_ms(200);
while(1){
printf("\nWhat is your frequency?\n");
scanf("%f",&freq);
freq = getc();
TMR2count=((1/(freq*(4/8000000)*16))-1);
SET_SIGNAL();
}
}
|
|
|
![](templates/subSilver/images/spacer.gif) |
temtronic
Joined: 01 Jul 2010 Posts: 9170 Location: Greensville,Ontario
|
|
Posted: Mon Apr 05, 2021 5:18 am |
|
|
Some problems I see...
1) TMR2count cannot be a float.
2) 'ERROR' has to be added to the #USE RS232( ...options....)
3) Some form of range checking of the 'desired frequency' needs to be added.
1 and 2 are easy fixes.. #3 depends upon what the datasheet says are valid numbers, based upon clock frequency and timer setups.
4) Not too sure what 'freq=getc' is supposed to do. |
|
![](templates/subSilver/images/spacer.gif) |
jake1272
Joined: 15 Mar 2021 Posts: 37
|
|
Posted: Mon Apr 05, 2021 5:46 am |
|
|
Following your instructions.
Here is my second attempt
Code: | #include <16F1847.h>
#fuses INTRC_IO,NOWDT,NOPROTECT, MCLR, NOLVP
#use delay(clock=8000000)
//#USE PWM(FREQUENCY=25000,DUTY=50,OUTPUT=PIN_B3) //Configure initial PWM signal
#use RS232(baud=9600, UART1,Errors) //RS232 on Pin B1 and Pin B2
//Oscilloscope on CCP pin B3
//float freq,
void SET_SIGNAL();
float Getfreq;
//int TMR2count;
float TMR;
void main(){
while(1){
delay_ms(200);
printf("\nWhat is your frequency?\n");
if (scanf("%f",&Getfreq)){
printf("\r\nFrequency is: %f",Getfreq);
TMR =(1.0/(Getfreq*(4.0/8000000.0)*16.0))-1.0;
if (TMR>255) TMR=255;
SET_SIGNAL();
}
SET_SIGNAL();
}
}
void SET_SIGNAL(){
unsigned int16 duty;
int TMR2count=TMR;
setup_ccp1(CCP_PWM); //Configure CCP1 for PWM
setup_timer_2(T2_DIV_BY_16,TMR2count,1); //Setup Timer2 with calculated counr from input frequency from RS232
duty = 500;
set_pwm1_duty(duty);
} |
|
|
![](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
|