找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 333|回复: 0
打印 上一主题 下一主题
收起左侧

做调快慢的单片机呼吸灯效果,有呼吸效果了,按键却不能调

[复制链接]
跳转到指定楼层
楼主
ID:879080 发表于 2023-11-25 21:17 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#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;                        //定时器时钟12T模式

        TMOD = 0x00;                        //设置定时器模式
        TL0 = 0xA4;                                //设置定时初始值
        TH0 = 0xFF;                                //设置定时初始值
        TR0 = 1;                                //定时器0开始计时
        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)     //如果数值大于100
{
pwm=0;             //周期归零
if(max==0){zkb++;}     //MAX占空比渐加渐减标志位 默认max为0.每一周期占空比渐加
if(max==1){zkb--;}      //MAX占空比渐加渐减标志位 如果max为1.每一周期占空比渐减
if(zkb==200){max=1;}  //如果占空比的值为100      标志位MAX置为1.渐减
if(zkb==0){max=0;}     //如果占空比的值为0         标志位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空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表