//
/*******************************************************************************
* 實驗名 : 動態顯示數碼管實驗
* 使用的IO :
* 實驗效果 : 數碼管顯示76543210。
* 注意 :當位選用P1口的時候注意可能會有一位不亮,那么調整J21
*******************************************************************************/
#include<reg51.h>
//--定義使用的IO口--//
#define GPIO_DIG P0
#define GPIO_PLACE P1
#define GPIO_TRAFFIC P2
sbit RED10 = P2^0; //上人行道紅燈
sbit GREEN10 = P2^1; //上人行道綠燈
sbit RED11 = P2^2;
sbit YELLOW11= P2^3;
sbit GREEN11 = P2^4;
sbit RED00 = P3^0; //右人行道紅燈
sbit GREEN00 = P3^1; //右人行道綠燈
sbit RED01 = P2^5;
sbit YELLOW01= P2^6;
sbit GREEN01 = P2^7;
//--定義全局變量--//
unsigned char code DIG_PLACE[8] = {
0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};//位選控制 查表的方法控制
unsigned char code DIG_CODE[17] = {
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
//0、1、2、3、4、5、6、7、8、9、A、b、C、d、E、F的顯示碼
unsigned char DisplayData[8];
//用來存放要顯示的8位數的值
unsigned char Time, Second; //用來存放定時時間
//--聲明全局函數--//
void DigDisplay(); //動態顯示函數
void Timer0Cofig(void);
/*******************************************************************************
* 函 數 名 : main
* 函數功能 : 主函數
* 輸 入 : 無
* 輸 出 : 無
*******************************************************************************/
void main(void)
{
Second = 1;
Timer0Cofig();
while(1)
{
if(Second == 70)
{
Second = 1;
}
//--寶田路通行,30秒--//
if(Second < 31)
{
DisplayData[0] = 0x00;
DisplayData[1] = 0x00;
DisplayData[2] = DIG_CODE[(30 - Second) % 100 / 10];
DisplayData[3] = DIG_CODE[(30 - Second) %10];
DisplayData[4] = 0x00;
DisplayData[5] = 0x00;
DisplayData[6] = DisplayData[2];
DisplayData[7] = DisplayData[3];
DigDisplay();
//--寶田路通行--//
GPIO_TRAFFIC = 0xFF; //將所有的燈熄滅
RED00 = 1;
GREEN00 = 1;
GREEN11 = 0; //寶田路綠燈亮
GREEN10 = 0; //寶田路人行道綠燈亮
RED01 = 0; //前進路紅燈亮
RED00 = 0; //前進路人行道紅燈亮
}
//--黃燈等待切換狀態,5秒--//
else if(Second < 36)
{
DisplayData[0] = 0x00;
DisplayData[1] = 0x00;
DisplayData[2] = DIG_CODE[(35 - Second) % 100 / 10];
DisplayData[3] = DIG_CODE[(35 - Second) %10];
DisplayData[4] = 0x00;
DisplayData[5] = 0x00;
DisplayData[6] = DisplayData[2];
DisplayData[7] = DisplayData[3];
DigDisplay();
//--黃燈階段--//
GPIO_TRAFFIC = 0xFF; //將所有的燈熄滅
RED00 = 1;
GREEN00 = 1;
YELLOW11 = 0; //寶田路黃燈亮
RED10 = 0; //寶田路人行道紅燈亮
YELLOW01 = 0; //前進路紅燈亮
RED00 = 0; //前進路人行道紅燈亮
}
//--前進路通行--//
else if(Second < 66)
{
DisplayData[0] = 0x00;
DisplayData[1] = 0x00;
DisplayData[2] = DIG_CODE[(65 - Second) % 100 / 10];
DisplayData[3] = DIG_CODE[(65 - Second) %10];
DisplayData[4] = 0x00;
DisplayData[5] = 0x00;
DisplayData[6] = DisplayData[2];
DisplayData[7] = DisplayData[3];
DigDisplay();
//--黃燈階段--//
GPIO_TRAFFIC = 0xFF; //將所有的燈熄滅
RED00 = 1;
GREEN00 = 1;
RED11 = 0; //寶田路紅燈亮
RED10 = 0; //寶田路人行道紅燈亮
GREEN01 = 0; //前進路綠燈亮
GREEN00 = 0; //前進路人行道綠燈亮
}
//--黃燈等待切換狀態,5秒--//
else
{
DisplayData[0] = 0x00;
DisplayData[1] = 0x00;
DisplayData[2] = DIG_CODE[(70 - Second) % 100 / 10];
DisplayData[3] = DIG_CODE[(70 - Second) %10];
DisplayData[4] = 0x00;
DisplayData[5] = 0x00;
DisplayData[6] = DisplayData[2];
DisplayData[7] = DisplayData[3];
DigDisplay();
//--黃燈階段--//
GPIO_TRAFFIC = 0xFF; //將所有的燈熄滅
RED00 = 1;
GREEN00 = 1;
YELLOW11 = 0; //寶田路黃燈亮
RED10 = 0; //寶田路人行道紅燈亮
YELLOW01 = 0; //前進路紅燈亮
RED00 = 0; //前進路人行道紅燈亮
}
}
}
/*******************************************************************************
* 函 數 名 : DigDisplay
* 函數功能 : 使用數碼管顯示
* 輸 入 : 無
* 輸 出 : 無
*******************************************************************************/
void DigDisplay()
{
unsigned char i;
unsigned int j;
for(i=0; i<8; i++)
{
GPIO_PLACE = DIG_PLACE; //發送位選
GPIO_DIG = DisplayData; //發送段碼
j = 10; //掃描間隔時間設定
while(j--);
GPIO_DIG = 0x00; //消隱
}
}
/*******************************************************************************
* 函 數 名 : Timer0Cofig
* 函數功能 : 配置定時器
* 輸 入 : 無
* 輸 出 : 無
*******************************************************************************/
void Timer0Cofig(void)
{
TMOD = 0x01; //定時器0選擇工作方式1
TH0 = 0x3C; //設置初始值,定時50MS
TL0 = 0xB0;
EA = 1; //打開總中斷
ET0 = 1; //打開定時器0中斷
TR0 = 1; //啟動定時器0
}
/*******************************************************************************
* 函 數 名 : Timer0
* 函數功能 : 定時器0中斷函數
* 輸 入 : 無
* 輸 出 : 無
*******************************************************************************/
void Timer0() interrupt 1
{
TH0 = 0x3C; //設置初始值
TL0 = 0xB0;
Time++;
if(Time == 20)
{
Second ++;
Time = 0;
}
}
|