/*
* File: main.c
* Author: DrG
* Use it at your own risk *
*/
#include <xc.h>
#pragma config FOSC=INTOSC, PLLEN=OFF, WDTE=OFF, MCLRE=ON,
#pragma config CLKOUTEN=OFF, IESO=OFF, FCMEN=OFF,CP=OFF, CPD=OFF,BOREN=OFF
#pragma config WRT=OFF,STVREN=ON,BORV=LO,LVP=OFF
#define _XTAL_FREQ 16000000 // this is used by the __delay_ms(xx) and __delay_us(xx) functions
unsigned char Dcount =0;
const char SINETABLE[40]=
{
50,55,60,65,70,75,80,85,90,95,
100,95,90,85,80,75,70,65,60,55,
50,45,40,35,30,25,20,15,10,5,
0,5,10,15,20,25,30,35,40,45
};
/*
* These are the duty cycle percentages for the sine wave
50, 58, 65, 73, 79, 85, 90, 95, 98, 99,
100, 99, 98, 95, 90, 85, 79, 73, 65, 58,
50, 42, 35, 27, 21, 15, 10, 5, 2, 1,
0, 1, 2, 5, 10, 15, 21, 27, 35, 42
*/
const unsigned char DCThigh[40]=
{
128,148,166,186,202,217,230,242,250,252,
255,252,250,242,230,217,202,186,166,148,
128,107, 89, 69, 54, 38, 26, 13, 5, 2,
0, 2, 5, 13, 26, 38, 54, 69, 89,107
};
const unsigned char DCTlow[40]=
{
2,0,3,1,2,3,2,1,0,2,
0,2,0,1,2,3,2,1,3,0,
2,0,1,3,2,1,2,3,0,2,
0,2,0,3,2,1,2,3,1,0
};
void main(){
OSCCON = 0x7A;
// Fosc = 16 MHz with internal oscillator
__delay_us(100);
// Timer2 configuration for PWM
PR2 = 0xfe;
// PWM period register for 40 kHz
T2CON = 0x4;
// PWM 1 configuration
CCP1CONbits.CCP1M=0x0C; // select PWM mode for CCP module
CCP1CONbits.P1M=0x00; // select single output on CCP1 pin (RA2) PIN 5
// PWM1 on, PWM 1 output enable
CCPR1L = 0x00; // clear high 8 bits of PWM duty cycle
CCP1CONbits.DC1B=0x00; // clear low 2 bits of PWM Duty cycle
// load starting duty cycle
CCPR1L=DCThigh[Dcount];
CCP1CONbits.DC1B=DCTlow[Dcount];
PIE1bits.TMR2IE =1;
// Timer2 interrupt enable
INTCON =0xC0;
TRISA=0x00;
ANSELA=0x00;
here: goto here;
}
void interrupt Timer2_ISR(void)
{
if (TMR2IF)
{
++Dcount;
// Increment the counter variable by 1
if(Dcount == 39)
{
Dcount = 0;
}
CCPR1L=DCThigh[Dcount];
CCP1CONbits.DC1B=DCTlow[Dcount];
TMR2IF = 0;
}
}