PWM function of PIC16F877A microcontroller

Publisher:恬淡如云Latest update time:2016-08-07 Source: eefocusKeywords:PIC16F877A Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
Description: Set PWM operation

 To configure the CCP module for PWM mode, follow these steps:
1. Write to the PR2 register to program the PWM period.
2. Write to the DCxB9:DCxB0 bits to program the PWM duty cycle.
3. Clear the appropriate TRIS bit to make the CCPx pin an output.
4. Write to T2CON to program the TMR2 prescaler and enable Timer2.
5. Configure the CCP module for PWM mode.

program:

#include
#define uint unsigned int
#define uchar unsigned char
__CONFIG(0x3B31);

void initPWM();
void Delay1ms(uint time);

/******************************************
***Main function: PWM initialization, PWM duty cycle periodic change
*******************************************/
void main()
{
 uchar i;
 initPWM();
 while(1)
 {
  for(i=0;i<256;i++)
  {
   CCPR1L = i;
   TMR2ON = 1;
   Delay1ms(100);
   TMR2ON = 0;   
  }
 } 
 
}

/******************************************************************
***Function: PWM initialization, set PWM period;
PWM period = [(PR2)+1]*4*TOSC*(TMR2 pre-scaling ratio)
       = (255+1)*4*(1/4000000)*16 s = 4.096 ms

PWM duty cycle = (DCxB9:DCxB0)*Tosc*(TMR2 prescaler ratio)
  = 1024*(1/4000000)*16 s = 4.096 ms

Note: If the PWM duty cycle value is greater than the PWM period, the CCPx pin will
      not be cleared and the duty cycle will reach 100%;

*********************************************************************/
void initPWM()
{
 INTCON = 0x00; //Disable general interrupt and external interrupt
 PR2 = 0xff; //Write to PR2 register to set PWM period, PWM period = [(PR2)+1]*4*TOSC*(TMR2 prescaler)
 CCP1CON = 0x3C; //Set the lower 2 bits of PWM duty cycle to 11, set to PWM mode
 CCPR1L = 0x00; //Set the higher 8 bits of PWM duty cycle
 TRISC = 0xfb; //Set CCP1 port to output
 T2CON = 0x02; //Set TMR2 prescaler to 16; 
}

void Delay1ms(uint time)
{
 uint i,j;
 for(i=time;i>0;i--)
  for(j=110;j>0;j--);
}

PIC16F877A MCU PWM - czc0713 - czc0713's blog

 

Keywords:PIC16F877A Reference address:PWM function of PIC16F877A microcontroller

Previous article:Design of Intelligent Heat Meter Based on PIC16C73 Single Chip Microcomputer
Next article:Operation of PIC Internal EEPROM

Recommended ReadingLatest update time:2024-11-16 18:10

PIC16F877A AD Learning
Start writing the ADC program tonight! Below is a schematic diagram of a 3*3AD keyboard Program header file #ifndef ADC_H #define ADC_H #include "main.h" void init_adc()  ; char get_key() ; #endif subroutine #include "adc.h" void init_adc() {  //Define port direction  TRISA0=1 ;  ADCS1=0 ;//Selec
[Microcontroller]
PIC16F877A AD Learning
PIC16F877A microcontroller (external interrupt)
1 Basic principles 2 Implementation Code /*----------------Function function:     External interrupt RB0/INT Press the button (that is, generate an interrupt), and the LED lights up --------------------------*/ #include pic.h // Call the header file of PIC16f87XA microcontroller //#include"delay.h"//Heade
[Microcontroller]
PIC16F877A microcontroller (external interrupt)
PIC16F877A MCU SPI Learning
PIR1 Register      The PIR1 register contains the individual flag bits for the peripheral  interrupts. PIR1 REGISTER  PSPIF   ADIF  RCIF  TXIF  SSPIF  CCP1IF  TMR2IF  TMR1IF SSPIF:Synchronous Serial Port Interrupt Flag bit  1 = The SSP interrupt condition has occurred and must be cleared in software      before return
[Microcontroller]
PICKIT3 failed to program PIC16F877A
1. Fault prompt: (I forgot to record the specific text, but the general meaning is) 1 The target device cannot be found. . . . ; 1.2 Too much current flows into VDD, check the clock line and signal line. . . ;   2. Reason: The MCU chip is connected reversely or soldered incorrectly.
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号