CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Digital Capture and PWM Control

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
jake1272



Joined: 15 Mar 2021
Posts: 37

View user's profile Send private message

Digital Capture and PWM Control
PostPosted: Mon Apr 05, 2021 1:56 am     Reply with quote

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();
}
}
 
temtronic



Joined: 01 Jul 2010
Posts: 9097
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Mon Apr 05, 2021 5:18 am     Reply with quote

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.
jake1272



Joined: 15 Mar 2021
Posts: 37

View user's profile Send private message

PostPosted: Mon Apr 05, 2021 5:46 am     Reply with quote

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);
}
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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