機(jī)電之家資源網(wǎng)
單片機(jī)首頁|單片機(jī)基礎(chǔ)|單片機(jī)應(yīng)用|單片機(jī)開發(fā)|單片機(jī)文案|軟件資料下載|音響制作|電路圖下載 |嵌入式開發(fā)
培訓(xùn)信息
贊助商
稍復(fù)雜一點(diǎn)的流水燈電路
稍復(fù)雜一點(diǎn)的流水燈電路
 更新時(shí)間:2010-1-28 11:02:37  點(diǎn)擊數(shù):0
【字體: 字體顏色

其實(shí)現(xiàn)程序:

 

#i nclude

 

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

 

unsigned char code LEDDisplayCode[] = { 0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8, //0~7
                              0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E,0xFF};

 

void Display(unsigned char Value)
{
 P3 = LEDDisplayCode[Value];
}

 

void LEDFlash(unsigned char Count)
{
 unsigned char i;
 bit Flag;
 for(i = 0; i {
  Flag = !Flag;
  if(Flag)
   Display(RunMode);
  else
   Display(0x10);
  Delay1ms(100);
 }
 Display(RunMode);
}

 

unsigned char GetKey(void)
{
 unsigned char KeyTemp,CheckValue,Key = 0x00;
 CheckValue = P2&0x32;
 if(CheckValue==0x32)
  return 0x00;
 
 Delay1ms(10);
 KeyTemp = P2&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 啟動(dòng)
 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)
{
 P1 = ~(LEDStatus&0x00FF);
 P0 = ~((LEDStatus>>8)&0x00FF);
}

 

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

 

 P1 = 0x00;
 P0 = 0x00;
 P2 = 0xFF;
 P3 = 0x00;
 Delay1ms(500);
 P1 = 0xFF;
 P0 = 0xFF;
 P2 = 0xFF;
 P3 = 0xFF;
 SetSpeed(SystemSpeedIndex);
 Display(RunMode);
}

 

//Mode 0
unsigned int LEDIndex = 0;
bit LEDDirection = 1,LEDFlag = 1;
void Mode_0(void)
{
 LEDShow(0x0001< LEDIndex = (LEDIndex+1)%16;
}
//Mode 1
void Mode_1(void)
{
 LEDShow(0x8000>>LEDIndex);
 LEDIndex = (LEDIndex+1)%16;
}
//Mode 2
void Mode_2(void)
{
 if(LEDDirection)
  LEDShow(0x0001< else
  LEDShow(0x8000>>LEDIndex);
 if(LEDIndex==15)
  LEDDirection = !LEDDirection

LEDIndex = (LEDIndex+1)%16;
}
//Mode 3
void Mode_3(void)
{
 if(LEDDirection)
  LEDShow(~(0x0001< else
  LEDShow(~(0x8000>>LEDIndex));
 if(LEDIndex==15)
  LEDDirection = !LEDDirection;
   LEDIndex = (LEDIndex+1)%16;
}

 

 

//Mode 4
void Mode_4(void)
{
 if(LEDDirection)
 {
  if(LEDFlag)
   LEDShow(0xFFFE<     else
   LEDShow(~(0x7FFF>>LEDIndex));
 }
 else
 {
  if(LEDFlag)
   LEDShow(0x7FFF>>LEDIndex);
  else
   LEDShow(~(0xFFFE< }
 if(LEDIndex==15)
 {
  LEDDirection = !LEDDirection;
  if(LEDDirection) LEDFlag = !LEDFlag;
 }
    LEDIndex = (LEDIndex+1)%16;
}


 

//Mode 5
void Mode_5(void)
{
 if(LEDDirection)
  LEDShow(0x000F< else
  LEDShow(0xF000>>LEDIndex);
 if(LEDIndex==15)
  LEDDirection = !LEDDirection;
    LEDIndex = (LEDIndex+1)%16;
}


 

//Mode 6
void Mode_6(void)
{
 if(LEDDirection)
  LEDShow(~(0x000F< else
  LEDShow(~(0xF000>>LEDIndex));
 if(LEDIndex==15)
  LEDDirection = !LEDDirection;
    LEDIndex = (LEDIndex+1)%16;
}


 

//Mode 7
void Mode_7(void)
{
 if(LEDDirection)
  LEDShow(0x003F< else
  LEDShow(0xFC00>>LEDIndex);
 if(LEDIndex==9)
  LEDDirection = !LEDDirection;
    LEDIndex = (LEDIndex+1)%10;
}


 

//Mode 8
void Mode_8(void)
{
 LEDShow(++LEDIndex);
}


 

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();
 }
}


 

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&0x01)
 {
  LEDDirection = 1;
  LEDIndex = 0;
  LEDFlag = 1;
  RunMode = (RunMode+1)%9;
  Display(RunMode);
 }
 if(Key&0x02)
 {
  if(SystemSpeedIndex>0)
  {
   --SystemSpeedIndex;
   SetSpeed(SystemSpeedIndex);
  }
  else
  {
   LEDFlash(6);
  }
 }
 if(Key&0x04)
 {
  if(SystemSpeedIndex<28)
  {
   ++SystemSpeedIndex;

參考鏈接:http://www.picavr.com/news/2008-06/6103.htm

SetSpeed(SystemSpeedIndex);
  }
  else
  {
   LEDFlash(6);
  }
 }
}

 

//***********************************************************************************
main()
{
 unsigned char Key;
 InitialCPU();
 InitialTimer2();

 

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


參考鏈接:http://www.picavr.com/news/2008-06/6103p2.htm

  • 上一篇: 單片機(jī)讀寫24C01的C51程序
  • 下一篇: 沒有了
  • 發(fā)表評(píng)論   告訴好友   打印此文  收藏此頁  關(guān)閉窗口  返回頂部
    熱點(diǎn)文章
     
    推薦文章
     
    相關(guān)文章
    網(wǎng)友評(píng)論:(只顯示最新5條。)
    關(guān)于我們 | 聯(lián)系我們 | 廣告合作 | 付款方式 | 使用幫助 | 機(jī)電之家 | 會(huì)員助手 | 免費(fèi)鏈接

    點(diǎn)擊這里給我發(fā)消息66821730(技術(shù)支持)點(diǎn)擊這里給我發(fā)消息66821730(廣告投放) 點(diǎn)擊這里給我發(fā)消息41031197(編輯) 點(diǎn)擊這里給我發(fā)消息58733127(審核)
    本站提供的機(jī)電設(shè)備,機(jī)電供求等信息由機(jī)電企業(yè)自行提供,該企業(yè)負(fù)責(zé)信息內(nèi)容的真實(shí)性、準(zhǔn)確性和合法性。
    機(jī)電之家對(duì)此不承擔(dān)任何保證責(zé)任,有侵犯您利益的地方請(qǐng)聯(lián)系機(jī)電之家,機(jī)電之家將及時(shí)作出處理。
    Copyright 2007 機(jī)電之家 Inc All Rights Reserved.機(jī)電之家-由機(jī)電一體化網(wǎng)更名-聲明
    電話:0571-87774297 傳真:0571-87774298
    杭州濱興科技有限公司提供技術(shù)支持

    主辦:杭州市高新區(qū)(濱江)機(jī)電一體化學(xué)會(huì)
    中國行業(yè)電子商務(wù)100強(qiáng)網(wǎng)站

    網(wǎng)站經(jīng)營許可證:浙B2-20080178-1