找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 2008|回復: 0
打印 上一主題 下一主題
收起左側

51單片機外部中斷測馬達轉(zhuǎn)速,數(shù)碼管顯示

[復制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:803227 發(fā)表于 2021-1-21 14:31 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
本帖最后由 fangbl 于 2021-1-27 09:30 編輯

#include <reg52.h>

sbit COM1 = P2^0;
sbit COM2 = P1^6;
sbit COM3 = P1^5;
sbit COM4 = P1^3;
sbit COM5 = P1^4;
sbit COM6 = P1^7;

bit flag200ms;
unsigned char T0RH;
unsigned char T0RL;
unsigned int Cnt;
unsigned char LedBuff[6] = {0x28,0x28,0x28,0x28,0x28,0x28};
unsigned char code LedChar[10] =
{
        0x28, 0xEE, 0x58, 0x4A, 0x8E,
        0x0B, 0x09, 0x6E, 0x08, 0x0A
};

void ConfigTimer0(unsigned int ms);
void LedScan();
void InitCPU();
void SpeedCount();

void main()
{
        InitCPU();
        ConfigTimer0(1);

        while (1)
        {               
          if (flag200ms)         //200ms更新一次轉(zhuǎn)速
          {
             flag200ms = 0;
             SpeedCount();
          }
        }
}

void InitCPU()
{
        P1 = 0x03;          //COM輸出低電平,數(shù)碼管不亮
        COM1 = 0;        
        PEDGE = 0x03;   // Both edge trigger
        EX0 = 1;            // Enable INT0 interrupt
        EAL = 1;            // Enable all interrupt
}

void SpeedCount()
{
        float rmp;
        unsigned int RMP;
        unsigned int Temp;
        
        EX0 = 0;                       //關閉外部中斷0
        Temp = Cnt;                    //讀取200ms外部中斷信號數(shù)量
        rmp = (float)Temp;             //轉(zhuǎn)為浮點運算
        rmp = rmp / 12;                //馬達轉(zhuǎn)一圈有12個外部中斷信號(6對磁環(huán))
        rmp = (rmp + 0.05) * 100;      //四舍五入保留2位小數(shù)
        RMP = (unsigned int)rmp;       //轉(zhuǎn)回整型數(shù)
        RMP = RMP * 3;                 //200ms * (5 * 60) = 每分鐘轉(zhuǎn)速,再除以100以抵消前面保留的2位小數(shù)
        if (RMP <= 15)                 //無轉(zhuǎn)速時清零  
        {
                RMP = 0;
        }
        LedBuff[0] = LedChar[RMP%10];     //數(shù)碼管第6位
        LedBuff[1] = LedChar[RMP/10%10];    //數(shù)碼管第5位
        LedBuff[2] = LedChar[RMP/100%10];    //數(shù)碼管第4位
        LedBuff[3] = LedChar[RMP/1000%10];    //數(shù)碼管第3位
        LedBuff[4] = LedChar[RMP/10000%10];    //數(shù)碼管第2位
        LedBuff[5] = LedChar[RMP/100000%10];    //數(shù)碼管第1位               
        Cnt = 0;                       //中斷信號數(shù)量清零
        EX0 = 1;                       //重新打開外部中斷0
}

void LedScan()                   //數(shù)碼管掃描
{
        static unsigned char i;
        
        P0 = 0xFF;                   //清SEG        
        switch (i)                     //6位數(shù)碼管掃描
        {
                case 0:COM6=0; COM1=1; P0 = LedBuff[5]; i++;break;
                case 1:COM1=0; COM2=1; P0 = LedBuff[4]; i++;break;
                case 2:COM2=0; COM3=1; P0 = LedBuff[3]; i++;break;
                case 3:COM3=0; COM4=1; P0 = LedBuff[2]; i++;break;
                case 4:COM4=0; COM5=1; P0 = LedBuff[1]; i++;break;
                case 5:COM5=0; COM6=1; P0 = LedBuff[0]; i=0;break;
                default: break;        
        }
}

void ConfigTimer0(unsigned int ms)                     //配置定時器0
{
        unsigned long tmp;

        tmp = 12000000/12;                           
        tmp = (tmp * ms)/1000;
        tmp = 65536 - tmp;
        tmp = tmp + 12;
        T0RH = (unsigned char)(tmp >> 8);
        T0RL = (unsigned char)tmp;
        TMOD &= 0xF0;
        TMOD |= 0x01;                                        //工作方式1
        TH0 = T0RH;
        TL0 = T0RL;
        ET0 = 1;
        TR0 = 1;
}

void InterruptTimer0() interrupt ISRTimer0         //定時器0中斷
{
        static unsigned char i;
        static unsigned char j;
        
        TH0 = T0RH;
        TL0 = T0RL;
        i++;
        if (i >= 200)
        {
                i = 0;
                flag200ms = 1;
        }
        LedScan();                                //數(shù)碼管掃描
}

void InterruptInt0() interrupt ISRInt0                         //外部中斷2
{
        Cnt++;                                    //中斷信號數(shù)量累加
}

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏1 分享淘帖 頂 踩
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網(wǎng)

快速回復 返回頂部 返回列表