PIC16F877A CCP Usage

Publisher:朱颜素韵Latest update time:2016-11-04 Source: eefocusKeywords:PIC16F877A Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
//*************************************************************

//    |-|        |-|       |-|
//    | |        | |       | |
//  __| |________| |_______|_|_____------>PWM1
//
//    |----|     |----|    |----|
//    |    |     |    |    |    |
//  __|    |_____|    |____|    |_------>PWM2

//This experiment uses the CCP module function of the PIC microcontroller to generate PWM function. The period of PWM is:
//(PR2+1)*4TOSC*(TMR2 pre-division value)
//Understand the function of the CCP1CON register of the CCP module,
//Understand the relationship between TMR2 and the CCP module,
//Master the initialization and setting of the relevant registers of the CCP module
//Software platform: XC8 programmer and MPLAB X IDE integrated development environment
//Author: Sun An-qing
//The PWM signal generated by the CCP1 module is output from the RC2 pin,
//At the same time, the PWM signal generated by the CCP2 module is output from the RC1 pin. Both can be
observed with an oscilloscope //The PWM waveform.

Modified by: Leo

Date: 20101223

The display effect makes the LED light display effect go from bright to dark, then back to bright, then back to dark.

 

 

 

/* * 设置步骤见datasheet 的page 68 setup for PWM operation */
#include #define uchar unsigned char #define uint unsigned int // CONFIG #pragma config FOSC = HS // 12MHZ ???? #pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT enabled) #pragma config PWRTE = ON // Power-up Timer Enable bit (PWRT disabled) #pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled) #pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3/PGM pin has PGM function; low-voltage programming enabled) #pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off) #pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control) #pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
void delay1ms(uint DelayTime)
{
uint temp;
for(;DelayTime>0;DelayTime--)
{
for(temp=0;temp<270;temp++)
{;}
}
}
void main(void)
{
//***********************************************************
CCP1M3=1;//Enable CPP1 Work as PWM mode
CCP1M2=1;
CCP1M1=1;
CCP1M0=1;
TRISC2=0;//Enable RC2 work as output
CCPR1L=0x10;//PWM脉宽值,改变PWM的脉宽,只需改变CCPR1L中的值
CCP1X=0;//以及CCP1CON中的CCP1X和CCP1Y这两个位的值
CCP1Y=0;

//***********************************************************
CCP2M3=1;//Enable CPP2 Work as PWM mode
CCP2M2=1;
CCP2M1=1;
CCP2M0=1;
TRISC1=0;//Enable RC1 work as output
CCPR2L=0x20;//PWM脉宽值,改变PWM的脉宽,只需改变CCPR2L中的值
CCP2X=0;//以及CCP2CON中的CCP2X和CCP2Y这两个位的值
CCP2Y=0;

//***********************************************************
TOUTPS3=0;//Enable TMR2--->1:1
TOUTPS2=0;
TOUTPS1=0;
TOUTPS0=0;
T2CKPS1=1;//Enable TMR2--->1:16
T2CKPS0=1;
TMR2ON=1;//Enable TMR2 Working
TMR2=0;

while(1)
{
for(;num <= 1023; num++)
{
PR2=num;//PWM周期,改变PWM的周期,只需改变PR2中的值即可
delay1ms(10);
}
}
}

Keywords:PIC16F877A Reference address:PIC16F877A CCP Usage

Previous article:PIC Read-Modify-Write Operations
Next article:PIC16F877A CCP Usage

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号