標(biāo)題: 單片機(jī)直流電機(jī)調(diào)速的程序 [打印本頁(yè)]

作者: XXSSSD    時(shí)間: 2023-7-19 19:42
標(biāo)題: 單片機(jī)直流電機(jī)調(diào)速的程序
#include <REGX52.H>
#include "Delay.h"
#include "Key.h"
#include "Nixie.h"
#include "Timer0.h"

sbit Motor=P1^0;

unsigned char Counter,Compare;        //計(jì)數(shù)值和比較值,用于輸出PWM
unsigned char KeyNum,Speed;

void main()
{
        Timer0_Init();
        while(1)
        {
                KeyNum=Key();
                if(KeyNum==1)
                {
                        Speed++;
                        Speed%=4;
                        if(Speed==0){Compare=0;}        //設(shè)置比較值,改變PWM占空比
                        if(Speed==1){Compare=50;}
                        if(Speed==2){Compare=75;}
                        if(Speed==3){Compare=100;}
                }
                Nixie(1,Speed);
        }
}

void Timer0_Routine() interrupt 1
{
        TL0 = 0x9C;                //設(shè)置定時(shí)初值
        TH0 = 0xFF;                //設(shè)置定時(shí)初值
        Counter++;
        Counter%=100;        //計(jì)數(shù)值變化范圍限制在0~99
        if(Counter<Compare)        //計(jì)數(shù)值小于比較值
        {
                Motor=1;                //輸出1
        }
        else                                //計(jì)數(shù)值大于比較值
        {
                Motor=0;                //輸出0
        }
}







歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1