void main() {
OSCCONbits.IRCF = 0b110; // set oscillator frequency to 4 MHz
OSCCONbits.SCS = 0b10; // use internal oscillator
TRISA = 0b00000000; // set port A as output
TRISC = 0b00000000; // set port C as output
TRISD = 0b00010000; // set port D as output except for RD4 (temperature compensation variable resistor)
ANSELA = 0b00000000; // set port A as digital
ANSELC = 0b00000000; // set port C as digital
ANSELD = 0b00000000; // set port D as digital
T2CONbits.T2CKPS = 0b11; // set timer 2 prescaler to 1:64
T2CONbits.TMR2ON = 1; // enable timer 2
PR2 = 124; // set timer 2 period to 125 cycles (4 us)
CCP1CONbits.CCP1M = 0b1100; // set CCP1 as PWM mode
CCP1CONbits.DC1B = 0; // set PWM duty cycle bits to 0
CCPR1L = 0; // set PWM duty cycle to 0
unsigned char index = 0;
while (1) {
unsigned char sine_value = get_sine_value(index); // get sine value from lookup table
CCPR1L = sine_value >> 1; // set PWM duty cycle to half of sine value
CCP1CONbits.DC1B = sine_value & 0b00000001; // set PWM duty cycle LSB according to sine value
char frequency_string[17];
float frequency = (sine_value + 128) * 50.0 / 256.0; // calculate frequency from sine value
sprintf(frequency_string, "Frequency: %.2f Hz", frequency); // format frequency as string
LCD_SetCursor(0, 0); // set LCD cursor to first row, first column
LCD_Print(frequency_string); // print frequency to LCD screen
__delay_ms(10); // wait for 10 ms
index++; // increment index to cycle through lookup table
if (index == 100) {
index = 0; // reset index to 0 after reaching end of lookup table
}
}
}