Escolar Documentos
Profissional Documentos
Cultura Documentos
,
aD
u
w
(ij)1
I\j
u
w
Vout (RMS!
, I\j
jL-
;;;;;;;;;
r-
I.eii'
Vlt
r.
i.l
I
\Ale
I V 13
1 17
3 16
4
u
,
'IK
14
*
"
7805A
6 13 FJP
PWM
7
12
"4
n &ND Clut
S 11
12V
lN75QA vtUl (11V)
I
I 10
"I
38
Appendix C: CCode for PIC16F88 Microcontroller
/*
This program initializes the PIC16F88 for PWM mode running at 204KHz.
Everytime an interrupt from TIMER 2 occurs, the duty cycle is changed
according to the PWM_Table which mimics a sinusoidal waveform
by Mike Newbry
edited 5/31/2009
*/
#define sbi(a, b) ((a) 1= 1 (b)) //sets bit Bin variable A
#define cbi(a, b) ((a) &= ~ ( 1 (b))) //c1ears bit Bin variable A
#define tbi(a, b) ((a) 1\= 1 (b)) //toggles bit Bin variable A
const int pwm_tableL[41] ={ 104,118,133,146,159,170,179,187,193,196,197,
196,193,187,179,170,159,146,133,118,104,
89,75,61,49,38, 28, 20, 15, 11, 10,
11, 15,20, 28,38,49,61,75, 89,104} ;
const int pwm_tableH[41] ={ 0,3,0,2,0,0,3,1,0,1,2,
1,0,1,3,0,0,2,0,3,0,
1,0,2,0,0,1,3,0,3,2,
3,0,3,1,0,0,2,0,1,0 };
unsigned load;
int duty-cycle;
void mainO {
ANSEL = 0; / /set as digitalljO
TRISA =0; / / Configure PORTA as output
OSCCON =ObOl110000; / / b6.o4 =111 =8M Hz
PORTA =0; // Initialize PORTA
INTCON =OxCO; / / Enable GIE, PEIE
PIEl = Ox02; / / Enable TMR21E
PR2 =207; / / sets period to 2.404kHz
CCPRlL = 103;
39
TRISB = OxFE; / / Port B Bit 0 set to output
T2CON = Ob00000101; / / Prescaler = 4
CCP1CON = ObOOOOllOO; / / Set for PWM Mode
while (1) {
if (load==l) {
duty-cycle++;
if (duty_cycle >= 40) {
duty-cycle = 0;
}
CCPRlL = pwm_tableL[duty-cycle]; //Ioad 8 MSB's
if (pwm_tableH[duty-cycle] == 0) { / /Ioad 2 MSB's algorithm
cbi(CCP1CON,5);
cbi(CCP1CON,4);
} else if (pwm_tableH[duty-cycle] == 1) {
cbi(CCP1CON,5);
sbi(CCP1CON,4};
} else if (pwm_tableH[duty-cycle] == 2) {
sbi(CCP1CON,5);
cbi(CCP1CON,4);
} else if (pwm_tableH[duty-cycle] == 3) {
sbi(CCP1CON,5);
sbi(CCP1CON,4);
}
load = 0;
}
}
}
void interruptO {
if (PIR1 & Ox02) { / / see if TMR2 matched PR2
TMR2 = 0;
load = 1;
PIR1 = OxOO; / / clear TMR2 interrupt flag
}
}