Blinking an LED as a function of signal frequency

cmicrocontrollerpic

I wrote a function for interacting with my 'PIC16F1788'. The code sends pulses out the 'RC4' port at a specific frequency, to make an LED blink according to that frequency. So the LED will blink faster or slower depending on the magnitude of the frequency. I decided to take a blinking code and vary the delay time according to my input frequency as follows.

void blink(int frequency){
/* starts the LED blinking at a particular frequency */

     PORTCbits.RC4=0XFF;  // RC4 on
     __delay_ms(1000/frequency);   // time in ms dealing with frequency values in kHz
     PORTcbits.RC4=0X00;   // RC4 off
     __delay_ms(1000/frequency);   // time in ms dealing with frequency values in kHz

}

would this code work, is this a good idea ,and is there anyway that I could improve it?

Update #1

I changes my code based on some of your inputs But still no luck running it. it compiles just fine but the LED doesn't Blink =(

#include <htc.h>
#include <pic.h>
#include <pic16f1788.h>
#include <xc.h>
// Config word
#define frequ  100000
#define _XTAL_FREQ   20000000


void main()

{

   // output

TRISB5 = 0;

// configure CCP3

CCP3CONbits.CCP3M = 0x0C;   // set it to PWM mode; and active high

// configure Timer 2
PR2 = ((_XTAL_FREQ)/(4*16*frequ))-1;  // Timer 2 max value is (20MHz / 4*16*f)-1 = max duty, fosc=20MHz, prescaler of 16
CCPR3 = 0.5*(PR2+1);        // CCP3 compare value is 50% duty cycle
T2CONbits.T2OUTPS = 0x02;   // Timer 2 prescaler 16
T2CONbits.TMR2ON = 1;       // Timer 2 on

}

Best Answer

My guess to your problem is that your problem is with the data type being used for the attributes of the delay function. When you are using 'integer' as the attribute data type, you can't express values less than 1 but greater than 0. If your frequency is greater than 1000, the code won't work. you need to manipulate the frequency variable in such a way that the division won't go below 1 but will still be related to frequency. Try dividing frequency with a constant.