• 51学习笔记之利用定时器0产生最大周期为16s的PWM波(STC12C5A60S2)


    硬件:STC12C5A60S2 

    功能:实现PWM波生成。该程序周期为256ms,输出端口为P1.3。

    代码:

    #include "reg51.h"
    #include "intrins.h"
    #define FOSC 11059260L
    #define T1MS  (65536-FOSC/12/1000)
    
    //定时器产生的脉冲大约为1MS,PWM经过256分频后的时间是256ms
    //通过调T1MS的数值,PWM的最大周期时间是16s。
    
    typedef unsigned char BYTE;
    typedef unsigned int WORD;
    
    
    
    /*Declare SFR associated with the PCA */
    sfr CCON = 0xD8; //PCA control register
    sbit CCF0 = CCON^0; //PCA module-0 interrupt flag
    sbit CCF1 = CCON^1; //PCA module-1 interrupt flag
    sbit CR = CCON^6; //PCA timer run control bit
    sbit CF = CCON^7; //PCA timer overflow flag
    sfr CMOD = 0xD9; //PCA mode register
    sfr CL = 0xE9; //PCA base timer LOW
    sfr CH = 0xF9; //PCA base timer HIGH
    sfr CCAPM0 = 0xDA; //PCA module-0 mode register
    sfr CCAP0L = 0xEA; //PCA module-0 capture register LOW
    sfr CCAP0H = 0xFA; //PCA module-0 capture register HIGH
    sfr CCAPM1 = 0xDB; //PCA module-1 mode register
    sfr CCAP1L = 0xEB; //PCA module-1 capture register LOW
    sfr CCAP1H = 0xFB; //PCA module-1 capture register HIGH
    sfr PCAPWM0 = 0xf2;
    sfr PCAPWM1 = 0xf3;
    
    void Delay_xms(unsigned int z){        
        unsigned char i, j;
        unsigned int k;
        for(k=0;k<z;k++){            //
            _nop_();
            _nop_();
            i = 12;
            j = 84;
            do{while (--j);} 
            while (--i);
        }
        
    }
    
    //定时器0初始化
    
    void Time0Init()
    {
        TMOD = 0x01;
        TL0=T1MS;
        TH0=T1MS>>8;
        TR0=1;
        ET0=1;
        EA=1;    
    }
    
    void PWMInit()
    {
        CCON = 0; //Initial PCA control register
        //PCA timer stop running
        //Clear CF flag
        //Clear all module interrupt flag
        CL = 0; //Reset PCA base timer
        CH = 0;
    //    CMOD = 0x00; //Set PCA timer clock source as Fosc/2
        CMOD = 0x04;
        //Disable PCA timer overflow interrupt
        CCAP0H = CCAP0L = 0x80; //PWM0 port output 50% duty cycle square wave
        CCAPM0 = 0x42; //设备为定时器0溢出脉冲
        CCAP1H = CCAP1L = 0xff; //PWM1 port output 0% duty cycle square wave
        PCAPWM1 = 0x03;
        CCAPM1 = 0x42; //PCA module-1 work in 8-bit PWM mode
        //and no PCA interrupt
        CR = 1; //PCA timer start run
    }
    
    
    
    void PWMSet(float a)   //输入a表示占空比
    {
        CCAP0H =CCAP0L =(BYTE)((1-a)*256);
    }
    
    void main()
    {
    
        Time0Init();
      PWMInit();
        PWMSet(0.1);
    while(1)
    {
    
            PWMSet(0.5);    //占空比为百分之50
        Delay_xms(2000);
    }
    }
    
    
    //定时器0中断
    void tm0_isr() interrupt 1 using 1
    {
        TL0=T1MS;
        TH0=T1MS>>8;
    }
  • 相关阅读:
    linux中iptables的用法
    Git介绍及安装配置
    第一个shell脚本
    Nginx配置优化解读
    Python中print格式化输出
    python 程序构架浅析
    Python 常用字符串操作
    Python入门学习:网络刷博器爬虫
    vSphere SDK for Java
    vROPS中获取虚拟机在VC中的UUID
  • 原文地址:https://www.cnblogs.com/dafang/p/7221661.html
Copyright © 2020-2023  润新知