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
}
frequency/2
. \$\endgroup\$