新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > STC12C5A60S2單片機(jī)PWM程序

STC12C5A60S2單片機(jī)PWM程序

作者: 時(shí)間:2012-08-10 來源:網(wǎng)絡(luò) 收藏
兩個(gè)頭文件main.c#include REG51.H>#include intrins.h>#define U8 unsigned char#define U16 unsigned intvoid DelayMs(U8 ms);void PWM_clock(U8 clock);void PWM_start(U8 module,U8 mode);////////////////////// 延時(shí)子程序/////////////////////////////void DelayMs(U8 ms) //在11.0592M晶振下,stc10f系列()的ms級(jí)延時(shí){     U16 i;while(ms--){for(i = 0; i  850; i++);}}////////////////////主函數(shù)入口////////////////////////////sfr CCON     = 0xD8; //PCA控制寄存器sfr CMOD     = 0xD9; //PCA模式寄存器sfr CCAPM0   = 0xDA; //PCA模塊0模式寄存器 //模塊0對(duì)應(yīng)P1.3/CEX0/PCA0/PWM0(2系列)  sfr CCAPM1   = 0xDB; //PCA模塊1模式寄存器 //模塊1對(duì)應(yīng)P1.4/CEX1/PCA1/PWM1(2系列)sfr CL       = 0xE9;    //PCA 定時(shí)寄存器 低位sfr CH       = 0xF9; //PCA 定時(shí)寄存器 高位sfr CCAP0L   = 0xEA; //PCA模塊0的 捕獲寄存器 低位sfr CCAP0H   = 0xFA;    //PCA模塊0的 捕獲寄存器 高位sfr CCAP1L   = 0xEB; //PCA模塊1的 捕獲寄存器 低位sfr CCAP1H   = 0xFB; //PCA模塊1的 捕獲寄存器 高位sfr PCA_PWM0 = 0xF2; //PCA PWM 模式輔助寄存器0sfr PCA_PWM1 = 0xF3; //PCA PWM 模式輔助寄存器1sbit CF   = 0xDF;     //PCA計(jì)數(shù)溢出標(biāo)志位sbit CR   = 0xDE;     //PCA計(jì)數(shù)器 運(yùn)行控制位sbit CCF1 = 0xD9;     //PCA模塊1中斷標(biāo)志sbit CCF0 = 0xD8;     //PCA模塊0中斷標(biāo)志//* CCAPOH = CCAPOL = 0XC0; //模塊0輸出 占空因數(shù)為25%//* CCAPOH = CCAPOL = 0X80; //模塊0輸出 占空因數(shù)為50%//* CCAPOH = CCAPOL = 0X40; //模塊0輸出 占空因數(shù)為75%void PWM_clock(U8 clock);void PWM_start(U8 module,U8 mode);void PWM_clock(U8 clock){CMOD |= (clock1);CL = 0x00;CH = 0x00;}void PWM_start(U8 module,U8 mode,U8 zkb){CCAP0L = 0XC0;CCAP0H = 0XC0;   //模塊0初始輸出 占空因數(shù)為25%CCAP1L = 0XC0;CCAP1H = 0XC0;   //模塊1初始輸出 占空因數(shù)為25%if(module==0){switch(mode){case 0: CCAPM0 = 0X42;break; //模塊0設(shè)置為8位PWM輸出,無中斷case 1: CCAPM0 = 0X53;break; //模塊0設(shè)置為8位PWM輸出,下降沿產(chǎn)生中斷case 2: CCAPM0 = 0X63;break; //模塊0設(shè)置為8位PWM輸出,上升沿產(chǎn)生中斷case 3: CCAPM0 = 0X73;break; //模塊0設(shè)置為8位PWM輸出,跳變沿產(chǎn)生中斷default: break;}}elseif(module==1){switch(mode){case 0: CCAPM1 = 0X42;break; //模塊1設(shè)置為8位PWM輸出,無中斷case 1: CCAPM1 = 0X53;break; //模塊1設(shè)置為8位PWM輸出,下降沿產(chǎn)生中斷case 2: CCAPM1 = 0X63;break; //模塊1設(shè)置為8位PWM輸出,上升沿產(chǎn)生中斷case 3: CCAPM1 = 0X73;break; //模塊1設(shè)置為8位PWM輸出,跳變沿產(chǎn)生中斷default: break;}}elseif(module==2){switch(mode){case 0: CCAPM0 = CCAPM1 = 0X42;break; //模塊0和1設(shè)置為8位PWM輸出,無中斷case 1: CCAPM0 = CCAPM1 = 0X53;break; //模塊0和1設(shè)置為8位PWM輸出,下降沿產(chǎn)生中斷case 2: CCAPM0 = CCAPM1 = 0X63;break; //模塊0和1設(shè)置為8位PWM輸出,上升沿產(chǎn)生中斷case 3: CCAPM0 = CCAPM1 = 0X73;break; //模塊0和1設(shè)置為8位PWM輸出,跳變沿產(chǎn)生中斷default: break;}}CR=1; //PCA計(jì)數(shù)器開始計(jì)數(shù)}void PCA_Intrrpt(void) interrupt 7{if(CCF0) CCF0=0;if(CCF1) CCF1=0;   //軟件清零if(CF)   CF=0;    //軟件清零}void main(){TMOD|=0x02; /* timer 0 mode 2: 8-Bit reload */TH0=0xff;TR0=1;    PWM_clock(2);      // PCA/PWM時(shí)鐘源為 定時(shí)器0的溢出PWM_start(0,0); // 模塊0,設(shè)置為PWM輸出,無中斷,初始占空因素為25%DelayMs(250);IT0=1;       //下降沿觸發(fā)EX0=1;       //開中斷0EA=1;        //開總中斷while(1);    //等待中斷產(chǎn)生,按下S15按鍵產(chǎn)生中斷信號(hào)}#include "reg51.h"#define uchar unsigned char#define uint  unsigned intsbit di=P3^7;     uchar jishu=0x00;  //用來記錄中斷的次數(shù)void delay(uchar z)              {uchar i;for(i=0;iz;i++);}//#####################################//外部中斷0子函數(shù)void PWM_Select(void) interrupt 0{U8 modjishu++;     //中斷一次加1mod=jishu%3;switch(mod){case 0: CCAPM0 = 0X42;break; //0設(shè)置為25%PWM輸出case 1: CCAPM0 = 0X53;break; //0設(shè)置為50%PWM輸出case 2: CCAPM0 = 0X63;break; //0設(shè)置為75%PWM輸出default: break;}delay(255);}

pwm相關(guān)文章:pwm是什么


51單片機(jī)相關(guān)文章:51單片機(jī)教程


pwm相關(guān)文章:pwm原理


晶振相關(guān)文章:晶振原理


評(píng)論


技術(shù)專區(qū)

關(guān)閉