Académique Documents
Professionnel Documents
Culture Documents
CCP Module
Objective:
The objective of this lab is to understand CCP (compare/capture module)
module in a PIC microcontroller
Description:
PWM(Pulse Width Modulation)
Pulse width modulation (PWM) is a technique of controlling the amount of
power delivered to an electronic load using an on-off digital signal. The
fraction of the period for which the signal is on is known as the Duty
Cycle.
C code:
#include <p18f4520.h>
#include <stdlib.h>
#include <delays.h>
#include <timers.h>
#include <pwm.h>
#pragma
config OSC = HS
#pragma
#pragma
PORTCbits.RC0
void main(void) {
CCP1CON=0X05;
T3CON=0x0;
T1CON=0x0;
//TRISB=0;
//TRISD=0;
TRISCbits.TRISC2=1;
CCPR1L=0;
CCPR1H=0;
TRISCbits.RC1=0x00;
TRISCbits.RC3=0x00;
TRISCbits.RC0=0x00;
TRISB=0x00;
TRISA=0x00;
PORTCbits.RC1=0x00;
PORTCbits.RC3=0x00;
PORTCbits.RC0=0x00;
PORTB=0x00;
//
Delay10KTCYx(200);
//
init();
//Delay10KTCYx(100);
//prntscrn(ar);
//Delay10KTCYx(200);
//init();
while(1){
char ar[]="F=" ;
char ar1[]="kHz" ;
char strNumber[10];
int res;
//char num;
TMR1H=0x00;
TMR1L=0x00;
PIR1bits.CCP1IF=0;
while(PIR1bits.CCP1IF==0);
T1CONbits.TMR1ON=1;
PIR1bits.CCP1IF=0;
while(PIR1bits.CCP1IF==0);
T1CONbits.TMR1ON=0;
//PORTB=CCPR1L;
//PORTD=CCPR1H;
res=50000/CCPR1;
PORTA=res;
itoa(res,strNumber);
//num= (char)res;
init();
Delay10KTCYx(100);
prntscrn(ar);
prntscrn(strNumber);
prntscrn(ar1);
//print(res);
}
}
void init(void){
TRISB=0x00;
PORTB=0x38;
RW_PIN=0;
RS_PIN=0;
E_PIN=1;
E_PIN=0;
Delay10KTCYx(100);
PORTB=0x0F;
RW_PIN=0;
RS_PIN=0;
E_PIN=1;
E_PIN=0;
Delay10KTCYx(100);
PORTB=0x01;
RW_PIN=0;
RS_PIN=0;
E_PIN=1;
E_PIN=0;
Delay10KTCYx(100);
PORTB=0x06;
RW_PIN=0;
RS_PIN=0;
E_PIN=1;
E_PIN=0;
Delay10KTCYx(100);
}
}
void print(char b){
TRISB=0x00;
PORTB=b;
RW_PIN=0;
RS_PIN=1;
E_PIN=1;
E_PIN=0;
Delay10KTCYx(100);
}
void prntscrn(char ar[]){
int count=0;
while(ar[count] != '\0')
{
print(ar[count]);
count++;
Delay10KTCYx(100);
}
}
Conclusion:
The signal is captured for 3 duty cycles and then divided by 3 to get the
time period this time period is then inversed to get the frequency