Vous êtes sur la page 1sur 8

PIC18f Microcontroller PWM with

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.

CCP(compare capture mode)


The Capture/Compare/PWM (CCP) module in the PIC18F4520
microcontroller is very versatile. The Capture and Compare features
integrate closely with the 16-bit TMR1 and the PWM feature uses the third
timer, the 8-bit TMR2. The CCP module has two 8-bit registers, called
CCPR1L and CCPR1H. Together they form a 16-bit register that can be
used for capture, compare or to form the duty cycle of a PWM stream. The
CCP module is controlled by the CCP1CON register.

C code:
#include <p18f4520.h>
#include <stdlib.h>
#include <delays.h>
#include <timers.h>
#include <pwm.h>

#pragma

config OSC = HS

#pragma

config WDT = OFF

#pragma

config LVP = OFF

#define RW_PIN PORTCbits.RC1 /* PORT for RW */


#define RS_PIN PORTCbits.RC3 /* PORT for RS */
#define E_PIN

PORTCbits.RC0

//#define 8BIT PORTB


void print (char );
void comd(unsigned int );
void init(void);
void prntscrn(char []);

void main(void) {

CCP1CON=0X05;
T3CON=0x0;
T1CON=0x0;

//TRISB=0;
//TRISD=0;

TRISCbits.TRISC2=1;
CCPR1L=0;
CCPR1H=0;

//char f[]="speed 3" ;

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);

// char ar[]="hello world";


//char ar[20] = "abc";
//har ar[] = "Is Where Its At!";

//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 comd(unsigned int a){


TRISB=0x00;
PORTB=a;
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

Vous aimerez peut-être aussi