. . . .
//--------------------------------------------------------------------------- //程序所包含的头文件 #include
#include \#include \#include \
#include
//--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource \
#include \#include \#define SEC 60 TForm1 *Form1;
int sec=SEC,counter=0;//交通灯发生转换的时间60秒,当到3秒时,转成黄灯 bool ledn,change=1,flash,start_stop;//几个用于状态切换的布尔变量 unsigned int wAccuracy1;
unsigned int TimerID_1ms1=0;
unsigned char code[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};//显示数字的共阴极编码
union unLed //控制灯的状态的联合体 高4位控制绿灯,低4位控制红灯 { struct stLed {
unsigned RNorth :1; //0 unsigned RWest :1; //1 unsigned REast :1; //2 unsigned RSouth :1; //3 unsigned GNorth :1; //4 unsigned GWest :1; //5 unsigned GEast :1; //6 unsigned GSouth :1; //7 }LedBit;
unsigned char LedChar; } TrafficLed;
//--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner)
... . ... .
. . . .
{ }
//---------------------------------------------------------------------------
void PASCAL TimeProc1(UINT wTimerID,UINT msg,DWORD dwUser,DWORD dw1,DWORD dw2)//1ms定时中断处理代码 {
ledn=!ledn; if(ledn)
{ //b口连段码,c口连位选码
WriteIO(U8255C,0xfe);//led显示屏的最后一位用于显示秒的个位 WriteIO(U8255B,code[sec]); } else {
WriteIO(U8255C,0xfd);
WriteIO(U8255B,code[sec/10]);//显示秒的十位,没一ms刷新一次 } }
//--------------------------------------------------------------------------- void Stop_Timer()//停止1ms计时 { if (TimerID_1ms1==NULL) return; timeKillEvent(TimerID_1ms1); timeEndPeriod(wAccuracy1); return; }
bool Start_Timer(int m_Timer)//1ms计时开始 { TIMECAPS tc; if(timeGetDevCaps(&tc,sizeof(TIMECAPS))==TIMERR_NOERROR)//获取系统最小时间 { wAccuracy1=min(max(tc.wPeriodMin,1),tc.wPeriodMax); timeBeginPeriod(wAccuracy1); if((TimerID_1ms1=timeSetEvent(m_Timer,wAccuracy1,(LPTIMECALLBACK)TimeProc1,
... . ... .
. . . .
NULL,TIME_PERIODIC))==0) { ShowMessage(\不能计时\ } return FALSE; } return TRUE; }
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)//初始化卡板 {
if(!OpenPlx9052())ShowMessage(\打开失败!\ WriteIO(U8255Z,0x80);//初始化8255 }
//---------------------------------------------------------------------------
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action) {
ClosePlx9052(); }
//---------------------------------------------------------------------------
void __fastcall TForm1::N2Click(TObject *Sender) {
Form2->ShowModal(); }
//---------------------------------------------------------------------------
void __fastcall TForm1::startClick(TObject *Sender) {
start_stop=!start_stop; if(start_stop) {
Start_Timer(1);
start->Caption=\停止\ Timer1->Enabled=true; sec=SEC;
... . ... .
. . . .
//初始化8255 WriteIO(U8255A,0);//全灭
//状态1:南北绿,东西红 初始状态 TrafficLed.LedBit.GNorth= 1; TrafficLed.LedBit.GWest = 0; TrafficLed.LedBit.GEast = 0; TrafficLed.LedBit.GSouth= 1; TrafficLed.LedBit.RNorth= 0; TrafficLed.LedBit.RWest = 1; TrafficLed.LedBit.REast = 1; TrafficLed.LedBit.RSouth= 0; WriteIO(U8255A,TrafficLed.LedChar); } else {
start->Caption=\开始\ Stop_Timer();
Timer1->Enabled=false; //初始化8255 WriteIO(U8255A,0);//全灭 WriteIO(U8255B,0);//全灭 WriteIO(U8255C,0);//全灭
} }
//---------------------------------------------------------------------------
void __fastcall TForm1::quitClick(TObject *Sender) { WriteIO(U8255A,0);//全灭 WriteIO(U8255B,0);//全灭 WriteIO(U8255C,0);//全灭 Close();
... . ... .
. . . .
}
//---------------------------------------------------------------------------
void __fastcall TForm1::N1Click(TObject *Sender) { WriteIO(U8255A,0);//全灭 WriteIO(U8255B,0);//全灭 WriteIO(U8255C,0);//全灭 Close(); }
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender) { //系统定时器,每隔250毫秒中断一次 if(++counter==4) {
counter=0; sec--; }
if(sec>0&&sec<=3)//状态2: 南北灯由绿转黄闪三秒,东西红 {
flash=!flash;
TrafficLed.LedBit.GSouth= flash; TrafficLed.LedBit.GNorth=flash; TrafficLed.LedBit.RSouth= flash; TrafficLed.LedBit.RNorth= flash; TrafficLed.LedBit.RWest = change; TrafficLed.LedBit.REast = change; WriteIO(U8255A,TrafficLed.LedChar);
}
if(sec==0)//状态3: 东西绿,南北红 {
sec=SEC;
TrafficLed.LedBit.GNorth= !change; TrafficLed.LedBit.GWest = change; TrafficLed.LedBit.GEast = change; TrafficLed.LedBit.GSouth= !change; TrafficLed.LedBit.RNorth= change;
... . ... .