PIC16F914 outputs adjustable duty cycle PWM waveform

Publisher:真实幻想Latest update time:2019-11-21 Source: 51heiKeywords:PIC16F914 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

#include
typedef unsigned int uint;
typedef unsigned char uchar;
#define KEY0_DOWN() (RB4==0)//define key pressed
__CONFIG(0X1014);//internal clock
uchar KEY0_FLAG=0;
/*****************************/ 
//OSC SET; INTER 4MHZ PERIOD 1US
/*****************************/ 
void OSC_INIT()
{
  OSCCON=0X67;//set internal clock 4MHZ
}
/*****************************/ 
//DELAY FUNCTION();
/*****************************/ 
void delay(uint z)
{
   uint x,y;
   for(x=z;x>0;x--)
     for(y=110;y>0;y--);
}
/*************************/ 
//GPIO SET;
/*****************************/ 

void GPIO_INIT()
{
  TRISC5=0;//RC5 SET PWM OUT;
  TRISD2=0;//RD2 SET PWM OUT;
  RC5=RD2=0;
  TRISB4=1;
  IOCB4=1;//Enable RB4 level change interrupt
  WPUB=0X10;//Enable pull-up
  RBIE=1;
  RBIF=0;
  PEIE=1;
  GIE=1;
}
/*************************/ 
//PWM MODEL INIT;
/*************************/ 
void PWM_INIT()
{
  CCP1CON=0B00001100;//PWM mode
  CCP2CON=0B00001100;//PWM mode
  T2CKPS1=1;
  PR2=155;//PWM period = [PR2+1]*4*TOSC*T2 pre-division value, so PWM frequency = 1/period = 400HZ
  TMR2ON=1;
  CCPR1L=0;//Duty cycle = CCPR1L:CCP1CON<5:4>/4(PR2+1);
  CCPR2L=0;//Duty cycle = CCPR2L:CCP2CON<5:4>/4(PR2+1);
}
void main()
{
  GPIO_INIT();
  PWM_INIT();
  while(1);//Initialization is complete and waiting for interrupt response is OK
}  

//Interrupt response entry
void interrupt ALL_ISR()//Output 10%-100% duty cycle PWM waveform
{
  if(RBIF)
    {
     delay(5);
     if(KEY0_DOWN())
        {
            KEY0_FLAG++;
            if(KEY0_FLAG==11)KEY0_FLAG=0;
        }
     switch(KEY0_FLAG)
          {
             case 0:CCPR1L=CCPR2L=0;break;
             case 1:CCPR1L=CCPR2L=16;break;
             case break; case 10:CCPR1L=CCPR2L=156              ;
             break
             ;              default              :              break              ;              }           RBIF        =0              ;    } }











Keywords:PIC16F914 Reference address:PIC16F914 outputs adjustable duty cycle PWM waveform

Previous article:MCP2510 CAN communication program (pic microcontroller)
Next article:Introduction to PIC32 SPI (Master/Slave Mode)

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号