|
#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;
/*┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
函數(shù):擦除某一扇區(qū)(每個(gè)扇區(qū)512字節(jié))
入口:addr = 某一扇區(qū)首地址
┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈*/
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();
}
/*┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
函數(shù):寫一字節(jié)
入口:addr = 扇區(qū)單元地址 , dat = 待寫入數(shù)據(jù)
┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈*/
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(); // 關(guān)閉ISP/IAP
}
/*┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
函數(shù):讀一字節(jié)
入口:addr = 扇區(qū)單元地址
出口:dat = 讀出的數(shù)據(jù)
┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈*/
uchar dcx(uint addr)
{
uchar dat;
IAP_CONTR = 0x81;
//IAP_TPS = 12;
IAP_CMD = 1; // 用戶可以對(duì)"Data Flash/EEPROM區(qū)"進(jìn)行字節(jié)讀
IAP_ADDRL = addr;
IAP_ADDRH = addr>>8;
EA = 0;
IAP_TRIG = 0x5a;
IAP_TRIG = 0xa5;
_nop_();
dat = IAP_DATA; // 取出數(shù)據(jù)
Q0(); // 關(guān)閉ISP/IAP
return dat;
}
/*┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
函數(shù):關(guān)閉ISP/IAP操作
┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈*/
void Q0()
{
IAP_CONTR = 0; // 關(guān)閉IAP功能
IAP_CMD = 0; // 待機(jī)模式,無ISP操作
IAP_TRIG = 0;
IAP_ADDRH=0x80;
IAP_ADDRL=0;
EA =1; // 關(guān)閉IAP功能, 清與ISP有關(guān)的特殊功能寄存器
}
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,0xf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00,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);
}
}
}
|
|