|
50黑幣
3P口24LED跑馬燈程序求助。目前P0和P1口正常,請教增加P2口程序怎么修改呢?還有通電時(shí)不要讓LED全部亮要怎么修改呢?
效果:
3P口跑馬燈.gif (355.07 KB, 下載次數(shù): 45)
下載附件
2021-12-21 13:08 上傳
單片機(jī)程序:
- #include <REG52.H>
- unsigned char RunMode;
- //**********************************System Fuction*************************************************
- void Delay1ms(unsigned int count)
- {
- unsigned int i,j;
- for(i=0;i<count;i++)
- for(j=0;j<120;j++);
- }
- unsigned char GetKey(void)
- {
- unsigned char KeyTemp,CheckValue,Key = 0x00;
- CheckValue = P3&0x32;
- if(CheckValue==0x32)
- return 0x00;
-
- Delay1ms(10); //延遲1ms(10)
- KeyTemp = P3&0x32;
- if(KeyTemp==CheckValue)
- return 0x00;
- if(!(CheckValue&0x02))
- Key|=0x01;
- if(!(CheckValue&0x10))
- Key|=0x02;
- if(!(CheckValue&0x20))
- Key|=0x04;
- return Key;
- }
- unsigned int TimerCount,SystemSpeed,SystemSpeedIndex;
- void InitialTimer2(void)
- {
- T2CON = 0x00; //16 Bit Auto-Reload Mode
- TH2 = RCAP2H = 0xFC; //重裝值,初始值 TL2 = RCAP2L = 0x18;
- ET2=1; //定時(shí)器 2 中斷允許
- TR2 = 1; //定時(shí)器 2 啟動
- EA=1;
- }
- unsigned int 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(unsigned char Speed)
- {
- SystemSpeed =SpeedCode[Speed];
- }
- void LEDShow(unsigned int LEDStatus)
- {
- P0 = ~(LEDStatus&0x00FF);
- P1 = ~((LEDStatus>>8)&0x00FF);
-
- }
- void InitialCPU(void)
- {
- RunMode = 0x00;
- TimerCount = 0;
- SystemSpeedIndex = 10;
- P1 = 0x00;
- P0 = 0x00;
- P2 = 0x00;
- P3 = 0xFF;
- Delay1ms(500);
- P1 = 0xFF;
- P0 = 0xFF;
- P2 = 0xFF;
- P3 = 0xFF;
- SetSpeed(SystemSpeedIndex);
-
- }
- //Mode 0
- unsigned int LEDIndex = 0;
- bit LEDDirection = 1,LEDFlag = 1;
- void Mode_0(void)
- {
- if(LEDDirection)
- LEDShow(0x0001<<LEDIndex);
- else
- LEDShow(0x8000>>LEDIndex);
- if(LEDIndex==15)
- LEDDirection = !LEDDirection;
- LEDIndex = (LEDIndex+1)%16;
- }
- void TimerEventRun(void)
- {
-
- if(RunMode ==0x00)
- {
- Mode_0();
- }
- }
- void Timer2(void) interrupt 5 using 3
- {
- TF2 = 0; //中斷標(biāo)志清除( Timer2 必須軟件清標(biāo)志!)
- if(++TimerCount>=SystemSpeed)
- {
- TimerCount = 0;
- TimerEventRun();
- }
- }
- unsigned char MusicIndex = 0;
- void KeyDispose(unsigned char Key)
- {
-
- if(Key&0x02)
- {
- if(SystemSpeedIndex>0)
- {
- --SystemSpeedIndex;
- SetSpeed(SystemSpeedIndex);
- }
- else
- {
-
- }
- }
- if(Key&0x04)
- {
- if(SystemSpeedIndex<28)
- {
- ++SystemSpeedIndex;
- SetSpeed(SystemSpeedIndex);
- }
- else
- {
-
- }
- }
- }
- //***********************************************************************************
- main()
- {
- unsigned char Key;
- InitialCPU();
- InitialTimer2();
- while(1)
- {
- Key = GetKey();
- if(Key!=0x00)
- {
- KeyDispose(Key);
- }
- }
- }
復(fù)制代碼
|
-
-
24LED流水燈仿真.rar
2021-12-21 16:21 上傳
點(diǎn)擊文件名下載附件
122.22 KB, 下載次數(shù): 12
最佳答案
查看完整內(nèi)容
如果,上面一個(gè)有問題的話,用這個(gè):
void Mode_0(void)
{
unsigned char temp;
if(LEDDirection)
{
temp = (0x01 >3) == 0) P0 = ~temp;
else P0 = 0xff;
if((LEDIndex>>3) == 1) P1 = ~temp;
else P1 = 0xff;
if((LEDIndex>>3) == 2) P2 = ~temp;
else P2 = 0xff;
}
else
{
temp = (0x80 >> (LEDIndex & 0x07));
if((LEDIndex>>3) == 2) P0 = ~temp;
else P0 = 0xff ...
|