找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 894|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

做調(diào)快慢的單片機(jī)呼吸燈效果,有呼吸效果了,按鍵卻不能調(diào)

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:879080 發(fā)表于 2023-11-25 21:17 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
#include <reg51.h>
#include "intrins.h"

#define uchar unsigned char   
#define uint unsigned int

uchar zkb,pwm;
uchar RunMode;
sfr     P1M1    =   0x91;
sfr     P1M0    =   0x92;

sfr     P3M1    =   0xb1;
sfr     P3M0    =   0xb2;

//**********************************System Fuction*************************************************
void Delay1ms( uint count)
{               
            uint i,j;
        for(i=0;i<count;i++)
        for(j=0;j<120;j++);
}


uchar GetKey(void)
{
        uchar KeyTemp,CheckValue,Key = 0x00;
        CheckValue = P1&0x43;
        if(CheckValue==0x43)
                return 0x00;

        Delay1ms(10);
        KeyTemp = P1&0x43;
        if(KeyTemp==CheckValue)
                return 0x00;

        if(!(CheckValue&0x01))
                Key|=0x01;
        if(!(CheckValue&0x02))
                Key|=0x02;
        if(!(CheckValue&0x40))
                Key|=0x40;
        return Key;
}
uint TimerCount,SystemSpeed,SystemSpeedIndex;
void InitialTimer0(void) //100us@11.0592MHZ
{
   // AUXR &= 0x7F;                        //定時(shí)器時(shí)鐘12T模式

        TMOD = 0x00;                        //設(shè)置定時(shí)器模式
        TL0 = 0xA4;                                //設(shè)置定時(shí)初始值
        TH0 = 0xFF;                                //設(shè)置定時(shí)初始值
        TR0 = 1;                                //定時(shí)器0開始計(jì)時(shí)
        ET0=1;
        EA=1;

}
uint code SpeedCode[]={   1,   2,   3,   5,   8,  10,  14,  17,  20,  30,
                                  40,  50,  60,  70,  80,  90, 100, 120, 140, 160,
                                  180, 200, 300, 400, 500, 600, 700, 800, 900,1000};//30
void SetSpeed( uchar Speed)
{
        SystemSpeed =SpeedCode[Speed];
}

void LEDShow( uint LEDStatus)
{
        P3 = (LEDStatus&0xFF);

}

void InitialCPU(void)
{
        RunMode = 0x00;
        TimerCount = 0;
        SystemSpeedIndex = 10;

        P1 = 0xFF;

        P3 = 0xFF;
        Delay1ms(500);
        P1 = 0xFF;

        P3 = 0xFF;
        SetSpeed(SystemSpeedIndex);

}


unsigned int LEDIndex = 0;
//bit LEDDirection = 1,LEDFlag = 1;
void Mode_0(void)
{
   SystemSpeed=1;

                if(pwm>zkb)
        {
                LEDShow(LEDIndex=0xff);
}
        else
{
        LEDShow(LEDIndex=0x00);        
}
}
//Mode 1
void Mode_1(void)
{
        LEDShow(0x80>>LEDIndex);
        LEDIndex = (LEDIndex+1)%8;
}


void TimerEventRun(void)
{
        if(RunMode==0x00)
        {
                Mode_0();        
        }
        else if(RunMode ==0x01)
        {
                Mode_1();
        }

}
void Timer0(void) interrupt 1 using 1
{
                 static  uchar max=0;
        if(++TimerCount>=SystemSpeed)
        {
                                TimerCount = 0;
                                TimerEventRun();
}        
        pwm++;           //震蕩周期變量
  if(pwm>200)     //如果數(shù)值大于100
{
pwm=0;             //周期歸零
if(max==0){zkb++;}     //MAX占空比漸加漸減標(biāo)志位 默認(rèn)max為0.每一周期占空比漸加
if(max==1){zkb--;}      //MAX占空比漸加漸減標(biāo)志位 如果max為1.每一周期占空比漸減
if(zkb==200){max=1;}  //如果占空比的值為100      標(biāo)志位MAX置為1.漸減
if(zkb==0){max=0;}     //如果占空比的值為0         標(biāo)志位MAX置為0.漸加
                 
}
               
  }     

unsigned char MusicIndex = 0;
void KeyDispose(unsigned char Key)
{
        if(Key&0x01)
        {
                LEDDirection = 1;
                LEDIndex = 0;
                LEDFlag = 1;
                RunMode = (RunMode+1)%2;

        }
        if(Key&0x02)
        {
                if(SystemSpeedIndex>0)
                {
                        --SystemSpeedIndex;
                        SetSpeed(SystemSpeedIndex);
                }
                else
                {

                }
        }
        if(Key&0x40)
        {
                if(SystemSpeedIndex<28)
                {
                        ++SystemSpeedIndex;
                        SetSpeed(SystemSpeedIndex);
                }
                else
                {

                }
        }        
}

//***********************************************************************************
main()
{
         unsigned char Key;
        P1M0 = 0x00;
    P1M1 = 0x00;
    P3M0 = 0x00;
    P3M1 = 0x00;


        InitialCPU();
        InitialTimer0();

        while(1)
        {
                Key = GetKey();
                if(Key!=0x00)
                {
                        KeyDispose(Key);
                }
        }
}


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

使用道具 舉報(bào)

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

本版積分規(guī)則

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

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

快速回復(fù) 返回頂部 返回列表