1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
| #include<p18f4431.h>
#include <math.h>
#pragma config LVP =OFF
#pragma config WDTEN = OFF
#pragma config OSC=HS
#include <math.h>
unsigned char p=0;
unsigned char q=0;
unsigned char r=0;
unsigned char x=0;
float f1=96.66;
unsigned char sinee [64];
float factor;
float frequency,val,pr2;
unsigned char sine [64]={0, 4, 8, 13, 17, 21, 25, 29, 33, 37, 41, 44, 48, 51, 55, 58, 61, 64, 66, 69, 72, 74, 76, 78, 79, 81, 82, 83, 84, 85, 86, 86, 86, 86, 86, 85, 84, 83, 82, 81, 79, 78, 76, 74, 72, 69, 66, 64, 61, 58, 55, 51, 48, 44, 41, 37, 33, 29, 25, 21, 17, 13, 8, 4};
float factor;
void deadtime (unsigned char d) // function for dead time
{
unsigned char t=0;
while(t<d)
{
t=t+1;
}
}
void pwm (void)
{
for( p=0; p<64; p=p+1) //loop for implementing look-up table, writing duty cycle value to CCPR1L register which is copied in CCPR1H on each interupt
//i.e when a match between TMR2 & PR2 occura
{
CCPR1L=sinee[p];
PIR1bits.TMR2IF=0;
while(PIR1bits.TMR2IF==0); //when interupt occurs TMR2IF bit sets to 1, showing that inerupt has occured
}
//CCPR1L=0; //after the above loop executed 64 times i.e for 10ms, square waves are inverted
//deadtime(50); // insertion of dead time between two PWM channels
for( q=0; q<64; q=q+1) //loop for implementing look-up table, writing duty cycle value to CCPR1L register which is copied in CCPR1H on each interupt
{ //i.e when a match between TMR2 & PR2 occur
CCPR2L=sinee[q];
PIR1bits.TMR2IF=0;
while( PIR1bits.TMR2IF==0);
}
}
void setup(void) //function for adc setup
{
//ADCON0bits.ACONV=1; // contineous mode
ADCON0bits.ACONV=0; // single shoot mode
ADCON0bits.ACSCH=0;
ADCON0bits.ACMOD0=0; //sequential mode
ADCON0bits.ACMOD1=0;
//ADCON0bits.ACMOD0=0; //contineous mode single channel mode
//ADCON0bits.ACMOD1=0;
ADCON1bits.VCFG0=0;
ADCON1bits.VCFG1=0;
ADCON2bits.ADFM=0; // left justified
ADCON2bits.ACQT0=1; // aquction time=2*tad
ADCON2bits.ACQT1=0;
ADCON2bits.ACQT2=0;
ADCON2bits.ACQT3=0;
ADCON2bits.ADCS0=0; // tad=32tosc
ADCON2bits.ADCS1=1;
ADCON2bits.ADCS2=0;
ADCHSbits.GASEL0=0;
ADCHSbits.GASEL1=0;
ANSEL0=1;
TRISC=0x00; //pwm setup
CCP1CON=0x00;
CCP2CON=0x00;
CCPR1L=0;
// CCPR2L=0; /
CCP1CON=0xC;
CCP2CON=0xC;
T2CON=6;
}
int ADCRead(unsigned char ch)
{
ADCON0bits.ADON=1; //switch on the adc module
ADCON0bits.GO=1; //Start conversion
while(ADCON0bits.GO); //wait for the conversion to finish
ADCON0bits.ADON=0; //switch off adc
frequency=0.1953125*ADRESH; //0-5Volts of adc ----> 0---->50Hz output, using 8 MSB's of adc
pr2 =( 2456 *(pow(frequency,-1.002)))-0.888; // period register calculation formula
factor=pr2/f1; //contant factor that should be multiplied with basic sine lookup table to get the table for desired frequency
PR2=pr2;
for (r=0;r<64;r=r+1)
{
sinee[r]=factor*sine[r];
}
return PR2;
}
void main()
{
setup();
ADCRead(0);
pwm();
deadtime(50000000);
} |