找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2404|回复: 11
收起左侧

神奇的事情,用STC15W204S单片机做的程序,按加速键200多次程序才能正常 请高人指点迷津

[复制链接]
ID:879080 发表于 2022-3-3 16:46 | 显示全部楼层 |阅读模式
#include <REG51.H>
#include <intrins.h>
#define uchar unsigned char   
#define uint unsigned int
//#define  led P1

sfr     IAP_DATA    =   0xC2;
sfr     IAP_ADDRH   =   0xC3;
sfr     IAP_ADDRL   =   0xC4;
sfr     IAP_CMD     =   0xC5;
sfr     IAP_TRIG    =   0xC6;
sfr     IAP_CONTR   =   0xC7;

unsigned char RunMode;

void  cc(uint addr);
void  xcx(uint addr,uchar dat);
uchar dcx(uint addr);
void  Q0();

sfr P1M1 = 0x91;
sfr P1M0 = 0x92;
sfr P3M1 = 0xb1;
sfr P3M0 = 0xb2;


/*┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
函数:擦除某一扇区(每个扇区512字节)
入口:addr = 某一扇区首地址         
┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈*/
void cc(uint addr)// 0x83(晶振<5M)   0x82(晶振<10M)   0x81(晶振<20M)   0x80(晶振<40M)
{
    IAP_CONTR = 0x81;
    //IAP_TPS = 12;

    IAP_CMD   = 3;         
    IAP_ADDRL = addr;         
    IAP_ADDRH = addr>>8;      
        EA =0;   
    IAP_TRIG = 0x5a;         
    IAP_TRIG = 0xa5;         
    _nop_();
    Q0();        
}
/*┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
函数:写一字节
入口:addr = 扇区单元地址 , dat = 待写入数据
┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈*/
void xcx(uint addr,uchar dat)
{
    IAP_CONTR = 0x81;
  //IAP_TPS = 12;         
    IAP_CMD   = 2;            
    IAP_ADDRL = addr;        
    IAP_ADDRH = addr>>8;      
    IAP_DATA  = dat;         
    EA = 0;
    IAP_TRIG = 0x5a;         
    IAP_TRIG = 0xa5;         
    _nop_();
    Q0();         // 关闭ISP/IAP
}
/*┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
函数:读一字节
入口:addr = 扇区单元地址
出口:dat  = 读出的数据
┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈*/
uchar dcx(uint addr)
{   
    uchar dat;
        IAP_CONTR = 0x81;
  //IAP_TPS = 12;        
    IAP_CMD   = 1;         // 用户可以对"Data Flash/EEPROM区"进行字节读
    IAP_ADDRL = addr;         
    IAP_ADDRH = addr>>8;      
    EA = 0;
    IAP_TRIG = 0x5a;         
    IAP_TRIG = 0xa5;         
    _nop_();
    dat = IAP_DATA;         // 取出数据
        Q0();         // 关闭ISP/IAP         
        return dat;
}
/*┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
函数:关闭ISP/IAP操作
┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈*/
void Q0()
{
    IAP_CONTR = 0;            // 关闭IAP功能
    IAP_CMD   = 0;            // 待机模式,无ISP操作
    IAP_TRIG  = 0;
        IAP_ADDRH=0x80;
        IAP_ADDRL=0;
        EA =1;            // 关闭IAP功能, 清与ISP有关的特殊功能寄存器
}
void  Delay1ms(unsigned int count)
{
        unsigned int i,j;
        for(i=0;i<count;i++)
                for(j=0;j<120;j++);
}
uchar code led0[]={0xd6,0xd5,0xd3,0xce,0xcd,0xcb,
                                   
                                        };
uchar code led1[]={0xf8,0xff,0xf8,0xff,0xf8,0xff,0xf8,0xff,0xf8,0xff,0xf8,0xff,
                                   0xf8,0xff,0xf8,0xff,0xf8,0xff,0xf8,0xff,0xf8,0xff,0xf8,0xff,
                                   0xc7,0xff,0xc7,0xff,0xc7,0xff,0xc7,0xff,0xc7,0xff,0xc7,0xff,
                                   0xc7,0xff,0xc7,0xff,0xc7,0xff,0xc7,0xff,0xc7,0xff,0xc7,0xff,
                                        };

uchar code led2[8]={0xde,0xdd,0xdb,0xd7,0xee,0xed,0xeb,0xe7};
uchar code led3[28]={0xde,0xdc,0xe8,0xe0,0xd0,0xd0,0xe0,0xe0,0xdf,0xdf,0xee,0xec,
                                        0xd8,0xd0,0xe0,0xe0,0xd0,0xdf,0xef,0xee,0xdc,0xd8,0xe0,0xe0,
                                        0xd0,0xd0,0xef,0xef,
                                        };
uchar code led4[90]={0xce,0xcd,0xcb,0xf6,0xf5,0xf3,0xce,0xcd,0xcb,0xf6,0xf5,0xf3,
                                   0xce,0xcd,0xcb,0xf6,0xf5,0xf3,0xce,0xcd,0xcb,0xf6,0xf5,0xf3,
                                   0xce,0xcd,0xcb,0xf6,0xf5,0xf3,0xcb,0xcd,0xce,0xf3,0xf5,0xf6,

                                        0xcb,0xcd,0xce,0xf3,0xf5,0xf6,0xcb,0xcd,0xce,0xf3,0xf5,0xf6,
                                        0xcb,0xcd,0xce,0xf3,0xf5,0xf6,0xcb,0xcd,0xce,0xf3,0xf5,0xf6,
                                        0xcf,0xcf,0xcf,0xf0,0xf0,0xf0,0xcf,0xcf,0xcf,0xf0,0xf0,0xf0,
                                        0xcf,0xcf,0xcf,0xf0,0xf0,0xf0,0xcf,0xcf,0xcf,0xf0,0xf0,0xf0,
                                        0xcf,0xcf,0xcf,0xf0,0xf0,0xf0,
                                        };
uchar code led5[10]={0xfe,0xfd,0xfb,0xf7,0xef,0xde,0xdd,0xdb,0xd7,0xcf
                                        };

uchar code led6[12]={0xde,0xdc,0xd8,0xf0,0xe0,0xe0,0xc0,0xc0,0xc0,0xe0,0xff,0xff};
uchar code led7[6]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf};
uchar code led8[12]={0xfe,0xfc,0xf8,0xf0,0xe0,0xc0,0xc0,0xc0,0xc0,0xc0,0xff,0xff};
uchar code led9[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xef,0xf7,0xfb,0xfd,
                                        0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xef,0xf7,0xfb,0xfd,
                                        0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xef,0xf7,0xfb,0xfd,
                                        0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xef,0xf7,0xfb,0xfd,
                                        0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xef,0xf7,0xfb,0xfd,
                                        0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xef,0xf7,0xfb,0xfd,
                                        0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xef,0xf7,0xfb,0xfd,
                                        0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xef,0xf7,0xfb,0xfd,
                                        0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xef,0xf7,0xfb,0xfd,
                                        0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xef,0xf7,0xfb,0xfd,
                                        0xc1,0xc2,0xc4,0xc8,0xd0,0xe0,0xd0,0xc8,0xc4,0xc2,
                                        0xc1,0xc2,0xc4,0xc8,0xd0,0xe0,0xd0,0xc8,0xc4,0xc2,
                                        0xc1,0xc2,0xc4,0xc8,0xd0,0xe0,0xd0,0xc8,0xc4,0xc2,
                                        0xc1,0xc2,0xc4,0xc8,0xd0,0xe0,0xd0,0xc8,0xc4,0xc2,
                                        0xc1,0xc2,0xc4,0xc8,0xd0,0xe0,0xd0,0xc8,0xc4,0xc2,
                                        0xc1,0xc2,0xc4,0xc8,0xd0,0xe0,0xd0,0xc8,0xc4,0xc2,
                                        0xc1,0xc2,0xc4,0xc8,0xd0,0xe0,0xd0,0xc8,0xc4,0xc2,
                                        0xc1,0xc2,0xc4,0xc8,0xd0,0xe0,0xd0,0xc8,0xc4,0xc2,
                                        0xc1,0xc2,0xc4,0xc8,0xd0,0xe0,0xd0,0xc8,0xc4,0xc2,
                                        0xc1,0xc2,0xc4,0xc8,0xd0,0xe0,0xd0,0xc8,0xc4,0xc2,
                                        0xff,0xff,0xff,0x00,0x00,0x00,0xff,0xff,0xff,0x00,0x00,0x00,
                                        0xff,0xff,0xff,0x00,0x00,0x00,0xff,0xff,0xff,0x00,0x00,0x00,
                                        0xff,0xff,0xff,0x00,0x00,0x00,0xff,0xff,0xff,0x00,0x00,0x00,
                                   };
unsigned char GetKey(void)
{
        unsigned char KeyTemp,CheckValue,Key = 0x00;
        CheckValue = P3&0x4c;
        if(CheckValue==0x4c)
                return 0x00;
        Delay1ms(10);
        KeyTemp = P3&0x4c;
        if(KeyTemp==CheckValue)
                return 0x00;

        if(!(CheckValue&0x04))
                Key|=0x01;
        if(!(CheckValue&0x08))
                Key|=0x02;
        if(!(CheckValue&0x40))
                Key|=0x04;
        return Key;
}

unsigned int TimerCount,SystemSpeed,SystemSpeedIndex;
void InitialTimer0(void)
{

    TMOD=0x01;
        TH0=(65536-3000)/256;
        TL0=(65536-3000)%256;
        
        ET0=1;
        TR0=1;                  
    EA = 1;
        
}

unsigned int code SpeedCode[]={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};//27
void SetSpeed(unsigned char Speed)
{
         SystemSpeed =SpeedCode[Speed];
}

void LEDShow(unsigned int LEDStatus)
{
        P1 = LEDStatus;
}

void InitialCPU(void)
{
               
        RunMode = dcx(0x0000);
        TimerCount = 0;
        SystemSpeedIndex = dcx(0x0200);
                 // SystemSpeedIndex = 10;
               
        P3 = 0xff;
        Delay1ms(500);
        //P1 = 0xff;
        P3 = 0xFF;
                 
       SetSpeed( SystemSpeedIndex);
}
//Mode 0
unsigned int num0,num1,num2,num3,num4,num5,num6,num7,num8,num9,LEDIndex = 0;
bit LEDDirection=1 ,LEDFlag=1 ;

void Mode_0(void)
{
        LEDShow(LEDIndex=led0[num0]);
        num0++;
                if(num0==6)
        {
                num0=0;
               
}
}
//Mode 1
void Mode_1(void)
{        
        LEDShow(LEDIndex=led1[num1]);
                num1++;
         if(num1==48)
{
        num1=0;

}
}
//Mode 2
void Mode_2(void)
{
        LEDShow(LEDIndex=led2[num2]);
                num2++;
         if(num2==8)
{
        num2=0;

}
}
//Mode 3
void Mode_3(void)
{
        LEDShow(LEDIndex=led3[num3]);
                num3++;
         if(num3==28)
{
        num3=0;

}
}
void Mode_4(void)
{
        LEDShow(LEDIndex=led4[num4]);
                num4++;
         if(num4==90)
{
                num4=0;

}
}
void Mode_5(void)
{
  LEDShow(LEDIndex=led5[num5]);
                num5++;
         if(num5==10)
{
                num5=0;

}
}
void Mode_6(void)
{
                LEDShow(LEDIndex=led6[num6]);
                num6++;
         if(num6==12)
{
                num6=0;

}        
}
void Mode_7(void)
{
                LEDShow(LEDIndex=led7[num7]);
                num7++;
         if(num7==6)
{
                num7=0;

}
}
void Mode_8(void)                                
{
                LEDShow(LEDIndex=led8[num8]);
                num8++;
         if(num8==12)
{
                num8=0;

}

}
void Mode_9(void)                              
{
                LEDShow(LEDIndex=led9[num9]);
                num9++;
         if(num9==236)
{
                num9=0;

}

}
void TimerEventRun(void)
{

        if(RunMode==0x00)
        {
                Mode_0();        
        }
        else if(RunMode ==0x01)
        {
                Mode_1();
        }
       else if(RunMode ==0x02)
        {
                Mode_2();
        }
        else if(RunMode ==0x03)
        {
                Mode_3();
        }
        else if(RunMode ==0x04)
        {
                Mode_4();
        }
                else if(RunMode ==0x05)
        {
                Mode_5();
        }
                else if(RunMode ==0x06)
        {
                Mode_6();
        }
        else if(RunMode ==0x07)
        {
                Mode_7();
        }
        else if(RunMode ==0x08)
        {
                Mode_8();
        }
                else if(RunMode ==0x09)
        {
                Mode_9();
        }
}

void Timer0(void) interrupt 1 using 1
{
                TH0=(65536-3000)/256;
                TL0=(65536-3000)%256;
                    
        if(++TimerCount>=SystemSpeed)
        {
                                TimerCount = 0;
                                TimerEventRun();
                                 
}
               
}        

unsigned int MusicIndex=1 ;
void KeyDispose(unsigned char Key)
{
               
                                
        if(Key&0x01)
        {
                LEDDirection = 1;
                LEDIndex = 0;
                LEDFlag = 1;
                RunMode = (RunMode+1)%10;
                            cc(0x0000);           
                                    xcx(0x0000, RunMode);
        }
                                 
                                
                        
        if(Key&0x02)
        {
                if(SystemSpeedIndex>0)
                {
                        --SystemSpeedIndex;
                                                SetSpeed(SystemSpeedIndex);
                                            cc(0x0200);
                                     xcx(0x0200,SystemSpeedIndex);
                                       
                                }        
                                
                }
                                       

        if(Key&0x04)
        {
                if(SystemSpeedIndex<26)
                {
                        ++SystemSpeedIndex;
                        SetSpeed(SystemSpeedIndex);
                                        cc(0x0200);
                                     xcx(0x0200,SystemSpeedIndex);
                                                
                }
                                                               
        }  
                                                                        
                                                                                                                                                                                                      
}

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

        while(1)
        {
                Key = GetKey();

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


回复

使用道具 举报

ID:584814 发表于 2022-3-3 20:36 | 显示全部楼层
很认真地看完了程序,竟然没看出哪个是加速键、在哪里加速、200多次后正常是否能上高速 ......
回复

使用道具 举报

ID:86926 发表于 2022-3-4 08:32 | 显示全部楼层
可能是上电后void InitialCPU(void)中的SetSpeed( SystemSpeedIndex);前没有初始化SystemSpeedIndex;读到的内部数据是0xff就拿来用了
回复

使用道具 举报

ID:879080 发表于 2022-3-4 09:10 | 显示全部楼层
man1234567 发表于 2022-3-3 20:36
很认真地看完了程序,竟然没看出哪个是加速键、在哪里加速、200多次后正常是否能上高速 ......

     if(Key&0x02)
        {
                if(SystemSpeedIndex>0)
                {
                        --SystemSpeedIndex;
                                                SetSpeed(SystemSpeedIndex);
                                            cc(0x0200);
                                     xcx(0x0200,SystemSpeedIndex);
                                       
                                }        
                                
                }
按这个按键200多次后程序才能正常运行
回复

使用道具 举报

ID:879080 发表于 2022-3-4 09:18 | 显示全部楼层
本帖最后由 hanxingyijian 于 2022-3-4 09:20 编辑
蓝魄 发表于 2022-3-4 08:32
可能是上电后void InitialCPU(void)中的SetSpeed( SystemSpeedIndex);前没有初始化SystemSpeedIndex;读到 ...

当我用STC12C2052AD单片机的时候,下载第2次程序就能正常工作(当然要改擦写寄存器和相应的命令,程序不改)
回复

使用道具 举报

ID:213173 发表于 2022-3-4 09:36 | 显示全部楼层
hanxingyijian 发表于 2022-3-4 09:10
if(Key&0x02)
        {
                if(SystemSpeedIndex>0)

功能似乎很简单,洋洋洒洒一大堆代码写的很复杂,看着头大。你把具体的功能要求讲清楚才好判断。
回复

使用道具 举报

ID:161164 发表于 2022-3-4 12:29 | 显示全部楼层
蓝魄 发表于 2022-3-4 08:32
可能是上电后void InitialCPU(void)中的SetSpeed( SystemSpeedIndex);前没有初始化SystemSpeedIndex;读到 ...

同意,应加上判断程序以防读错地址
  1. void InitialCPU(void)
  2. {
  3.                
  4.         RunMode = dcx(0x0000);
  5.         TimerCount = 0;
  6.         SystemSpeedIndex = dcx(0x0200);
  7.                 if(SystemSpeedIndex > 26)
  8.                 {
  9.                         SystemSpeedIndex = 10;
  10.                 }
  11.                
  12.         P3 = 0xff;
  13.         Delay1ms(500);
  14.         //P1 = 0xff;
  15.         P3 = 0xFF;
  16.                  
  17.        SetSpeed( SystemSpeedIndex);
  18. }
复制代码
回复

使用道具 举报

ID:139866 发表于 2022-3-4 13:12 | 显示全部楼层
要在我们公司,代码注释写成这样, 头都给你锤爆
回复

使用道具 举报

ID:123289 发表于 2022-3-4 13:57 | 显示全部楼层
证明一下,你按的200多次,每次都被程序“收到”了。
提示:设整个程序执行时间是100,其中读键程序发生在50-70之间,这时只有在50-70之间发生的按键才被认可。
回复

使用道具 举报

ID:879080 发表于 2022-3-4 15:09 | 显示全部楼层
lkc8210 发表于 2022-3-4 12:29
同意,应加上判断程序以防读错地址

按您的方法问题解决了。多谢指导。我想增加一个P1口渐亮渐灭的程序。却不知道能不能实现
回复

使用道具 举报

ID:879080 发表于 2022-3-4 15:12 | 显示全部楼层
天ノ忆 发表于 2022-3-4 13:12
要在我们公司,代码注释写成这样, 头都给你锤爆

见谅!我是个刚步入单片机应用大门的小白,有很多问题还需要跟师傅们学习。
回复

使用道具 举报

ID:879080 发表于 2022-3-4 15:27 | 显示全部楼层
wulin 发表于 2022-3-4 09:36
功能似乎很简单,洋洋洒洒一大堆代码写的很复杂,看着头大。你把具体的功能要求讲清楚才好判断。

主要是我刚学习,想做个有趣的练练,功能是三个按键控制6路输出,一个按键调花样,另外2个按键调快慢。稍微复杂点的都无从下手。如果让熟练的师傅们写程序,代码估计可以少很多。感谢!
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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