以下是交通灯的程序
解释一下,哪句话是控制交通灯亮灭时间的
具体应该怎样改动可以施行
#include "scancode.h"
#define SPSA0 *(unsigned int *)0x38
#define SPSD0 *(unsigned int *)0x39
#define REGISTERCLKMD (*(unsigned int *)0x58)
#define TIM *(int *)0x24
#define PRD *(int *)0x25
#define TCR *(int *)0x26
#define IMR *(int *)0x0
#define IFR *(int *)0x1
#define PMST *(int *)0x1d
#define nStatusNSGreenEWRed 160
#define nStatusNSFlashEWRed 184
#define nStatusNSYellowEWRed 200
#define nStatusNSRedEWYellow 216
#define nStatusNSRedEWGreen 376
#define nStatusNSRedEWFlash 400
#define nStatusNSRedEWYellow1 416
#define nStatusNSYellowEWRed1 432
#define nTotalTime 448
#define nStatusHold 160
#define statusNSGreenEWRed 0
#define statusNSFlashEWRed 1
#define statusNSYellowEWRed 2
#define statusNSRedEWYellow 3
#define statusNSRedEWGreen 4
#define statusNSRedEWFlash 5
#define statusHold 6
#define LCDDELAY 1
#define LCDCMDTURNON 0x3f
#define LCDCMDTURNOFF 0x3e
#define LCDCMDSTARTLINE 0xc0
#define LCDCMDPAGE 0xb8
#define LCDCMDVERADDRESS 0x40
ioport unsigned int port3004;
// CTR扩展寄存器定义
ioport unsigned int port8000;
ioport unsigned int port8001;
ioport unsigned int port8002;
ioport unsigned int port8003;
ioport unsigned int port8004;
ioport unsigned int port8005;
ioport unsigned int port8007;
#define CTRGR port8000
#define CTRLCDCMDR port8001
#define CTRKEY port8001
#define CTRCLKEY port8002
#define CTRLCDCR port8002
#define CTRLCDLCR port8003
#define CTRLCDRCR port8004
#define CTRLA port8005
#define CTRLR port8007
void InitDSP();
void InitTimer();
void InitICETEKCTR();
void interrupt time(void);
void interrupt xint2(void); // XINT2中断服务程序
void SetLEDArray(int nNumber); // 修改显示内容
void RefreshLEDArray(); // 刷新显示
void EndICETEKCTR();
void TurnOnLCD();
void LCDCLS();
void Delay(unsigned int nDelay);
unsigned int uWork,nTimeCount;
unsigned int uLightStatusEW,uLightStatusSN;
unsigned int bHold;
unsigned char ledbuf[8],ledx[8];
unsigned char led[40]=
{
0x7E,0x81,0x81,0x7E,0x00,0x02,0xFF,0x00,
0xE2,0x91,0x91,0x8E,0x42,0x89,0x89,0x76,
0x38,0x24,0x22,0xFF,0x4F,0x89,0x89,0x71,
0x7E,0x89,0x89,0x72,0x01,0xF1,0x09,0x07,
0x76,0x89,0x89,0x76,0x4E,0x91,0x91,0x7E
};
unsigned char ledkey[10][8]=
{
{0x00,0x00,0x7C,0x82,0x82,0x82,0x7C,0x00}, //0
{0x00,0x00,0x00,0x84,0xFE,0x80,0x00,0x00}, //1
{0x00,0x00,0x84,0xC2,0xA2,0x92,0x8C,0x00}, //2
{0x00,0x00,0x44,0x92,0x92,0x92,0x6C,0x00},
{0x00,0x00,0x30,0x28,0x24,0xFE,0x20,0x00},
{0x00,0x00,0x4E,0x92,0x92,0x92,0x62,0x00},
{0x00,0x00,0x7C,0x92,0x92,0x92,0x64,0x00},
{0x00,0x00,0x02,0xC2,0x32,0x0A,0x06,0x00},
{0x00,0x00,0x6C,0x92,0x92,0x92,0x6C,0x00},
{0x00,0x00,0x4C,0x92,0x92,0x92,0x7C,0x00}
};
main()
{
int nWork1,nWork2,nWork3,nWork4,tKey;
int nNowStatus,nOldStatus,nOldTimeCount,nSaveTimeCount,nSaveStatus;
unsigned int nScanCode;
nTimeCount=0; bHold=0;
uLightStatusEW=uLightStatusSN=0;
nNowStatus=0; nOldStatus=1; nOldTimeCount=0;
InitDSP(); // 初始化DSP,设置运行速度
InitICETEKCTR(); // 初始化显示/控制模块
InitTimer(); // 设置定时器中断
// 根据计时器计数切换状态
// 根据状态设置计数和交通灯状态
while ( 1 )
{
if ( bHold && nNowStatus==statusHold )
{
if ( nTimeCount>=nStatusHold )
{
nNowStatus=nSaveStatus;
nTimeCount=nSaveTimeCount;
bHold=0;
}
}
else if ( nTimeCount<nStatusNSGreenEWRed ) nNowStatus=statusNSGreenEWRed;
else if ( nTimeCount<nStatusNSFlashEWRed ) nNowStatus=statusNSFlashEWRed;
else if ( nTimeCount<nStatusNSYellowEWRed ) nNowStatus=statusNSYellowEWRed;
else if ( nTimeCount<nStatusNSRedEWYellow ) nNowStatus=statusNSRedEWYellow;
else if ( nTimeCount<nStatusNSRedEWGreen ) nNowStatus=statusNSRedEWGreen;
else if ( nTimeCount<nStatusNSRedEWFlash ) nNowStatus=statusNSRedEWFlash;
else if ( nTimeCount<nStatusNSRedEWYellow1 ) nNowStatus=statusNSRedEWYellow;
else if ( nTimeCount<nStatusNSYellowEWRed1 ) nNowStatus=statusNSYellowEWRed;
if ( nNowStatus==nOldStatus )
{
switch ( nNowStatus )
{
case statusNSFlashEWRed:
nWork1=nTimeCount-nStatusNSGreenEWRed;
nWork2=nStatusNSYellowEWRed-nStatusNSFlashEWRed;
nWork3=nWork2/3;
nWork4=nWork3/2;
if ( nWork1>=0 && nWork2>0 && nWork3>0 && nWork4>0 )
uLightStatusSN=( (nWork1%nWork3)<=nWork4 )?(0x49):(0x40);
break;
case statusNSRedEWFlash:
nWork1=nTimeCount-nStatusNSRedEWGreen;
nWork2=nStatusNSRedEWYellow1-nStatusNSRedEWFlash;
nWork3=nWork2/3;
nWork4=nWork3/2;
if ( nWork1>=0 && nWork2>0 && nWork3>0 && nWork4>0 )
uLightStatusEW=( (nWork1%nWork3)<=nWork4 )?(0x09):(0x00);
break;
case statusNSGreenEWRed:
nWork1=nStatusNSGreenEWRed/20;
if ( nWork1>0 )
{
nWork2=20-nTimeCount/nWork1;
if ( bHold )
{
if ( nWork2>10 )
{
nTimeCount=nWork1*10;
nWork2=10;
}
}
if ( nOldTimeCount!=nWork2 )
{
nOldTimeCount=nWork2;
SetLEDArray(nWork2);
}
}
break;
case statusNSRedEWGreen:
nWork1=(nStatusNSRedEWGreen-nStatusNSRedEWYellow)/20;
if ( nWork1>0 )
{
nWork2=20-(nTimeCount-nStatusNSRedEWYellow)/nWork1;
if ( bHold )
{
if ( nWork2>10 )
{
nTimeCount=nStatusNSRedEWYellow+nWork1*10;
nWork2=10;
}
}
if ( nOldTimeCount!=nWork2 )
{
nOldTimeCount=nWork2;
SetLEDArray(nWork2);
}
}
break;
case statusHold:
nWork1=nStatusHold/20;
if ( nWork1>0 )
{
nWork2=20-nTimeCount/nWork1;
if ( nOldTimeCount!=nWork2 )
{
nOldTimeCount=nWork2;
SetLEDArray(nWork2);
}
}
break;
}
}
else
{
if ( bHold )
{
nSaveStatus=nNowStatus;
nSaveTimeCount=nTimeCount;
nNowStatus=statusHold;
nTimeCount=0;
if ( nSaveStatus==statusNSFlashEWRed || nSaveStatus==statusNSYellowEWRed )
{
nSaveStatus=statusNSRedEWGreen;
nSaveTimeCount=nStatusNSRedEWYellow;
}
else if ( nSaveStatus==statusNSRedEWFlash || nSaveStatus==statusNSRedEWYellow )
{
nSaveStatus=statusNSGreenEWRed;
nSaveTimeCount=0;
}
}
nOldStatus=nNowStatus;
switch ( nNowStatus )
{
case statusNSGreenEWRed:
uLightStatusEW=0x24; uLightStatusSN=0x49;
SetLEDArray(20);
break;
case statusNSFlashEWRed:
uLightStatusEW=0x24; uLightStatusSN=0x49;
SetLEDArray(0);
break;
case statusNSYellowEWRed:
uLightStatusEW=0x24; uLightStatusSN=0x52;
SetLEDArray(20);
break;
case statusNSRedEWYellow:
uLightStatusEW=0x12; uLightStatusSN=0x64;
SetLEDArray(20);
break;
case statusNSRedEWGreen:
uLightStatusEW=0x09; uLightStatusSN=0x64;
SetLEDArray(20);
break;
case statusNSRedEWFlash:
uLightStatusEW=0x09; uLightStatusSN=0x64;
SetLEDArray(0);
break;
case statusHold:
uLightStatusEW=0x24; uLightStatusSN=0x64;
SetLEDArray(20);
break;
}
}
CTRLR=uLightStatusEW; CTRLR=uLightStatusSN; // 设置交通灯状态
RefreshLEDArray(); // 刷新发光二极管显示
nScanCode=port8001; // 读键盘扫描码
nScanCode&=0x0ff;
tKey=CTRCLKEY;
if ( nScanCode==SCANCODE_9 ) break;
}
EndICETEKCTR();
exit(0);
}
// 定时器中断服务程序,进行时钟计数
void interrupt time(void)
{
nTimeCount++;
nTimeCount%=nTotalTime;
}
// 设置发光二极管显示内容
void SetLEDArray(int nNumber)
{
int i,ShowNumber;
ShowNumber=nNumber/2;
if(ShowNumber>9) ShowNumber=0;
CTRLCDCMDR=LCDCMDPAGE; // 设置操作页=0
Delay(LCDDELAY);
CTRLCDCR=0;
Delay(LCDDELAY);
CTRLCDCMDR=LCDCMDVERADDRESS; // 起始列=0
Delay(LCDDELAY);
CTRLCDCR=0;
Delay(LCDDELAY);
for ( i=0;i<8;i++ )
{
CTRLCDLCR=ledkey[ShowNumber][i];
Delay(LCDDELAY);
CTRLCDCR=0;
Delay(LCDDELAY);
}
}
// 将缓存中点阵送发光二极管显示
void RefreshLEDArray()
{
int i;
for ( i=0;i<8;i++ )
{
CTRGR=ledx[i];
CTRLA=ledbuf[i];
}
}
// 初始化DSP,设置运行速度=8MHz
void InitDSP()
{
REGISTERCLKMD=0; // 复位PLL
REGISTERCLKMD=0x1007; // 速度设置=16MHz
}
// 设置定时器参数、允许中断
void InitTimer()
{
unsigned int k;
asm(" ssbx INTM"); // 关中断,进行关键设置时不许打扰
// 设置通用定时器
k=PMST; // 设置PMST寄存器
PMST =k&0xff; // 中断向量表起始地址=80H
IMR = 0x0c; // 使能TINT
TCR = 0x41f; // 预分频系数为16
TIM = 0; // 时钟计数器清0
PRD = 0x0f423; // 周期寄存器为0ffH
TCR = 0x42f; // 复位、启动
IFR = 0x0c; // 清中断标志位
port3004=0; // 使能XINT2
asm(" rsbx INTM"); // 开中断
}
// 初始化ICETEK-CTR板上设备
void InitICETEKCTR()
{
int k;
CTRGR=0; // 初始化ICETEK-CTR
CTRGR=0x80;
CTRGR=0;
CTRLR=0; // 关闭东西方向的交通灯
CTRLR=0x40; // 关闭南北方向的交通灯
CTRLR=0x0c1; // 开启发光二极管显示阵列
CTRLR=0xc8; //使能外部中断
for ( k=0;k<8;k++ )
{
ledbuf[k]=0x0ff; // 显示为空白
ledx[k]=(k<<4); // 生成显示列控制字
}
k=CTRCLKEY; // 清除键盘缓冲区
TurnOnLCD(); // 打开显示
LCDCLS(); // 清除显示内存
CTRLCDCMDR=LCDCMDSTARTLINE; // 设置显示起始行
CTRLCDCR=0;
}
void interrupt xint2(void) // XINT2中断服务程序
{
bHold=1;
}
void EndICETEKCTR()
{
int k;
CTRLR=0; // 关闭东西方向的交通灯
CTRLR=0x40; // 关闭南北方向的交通灯
CTRLR=0x0c0; // 关闭发光二极管显示阵列
k=CTRCLKEY; // 清除键盘缓冲区
}
void TurnOnLCD()
{
CTRLCDCMDR=LCDCMDTURNON;
Delay(LCDDELAY);
CTRLCDCR=0;
Delay(LCDDELAY);
CTRLCDCMDR=LCDCMDSTARTLINE;
Delay(LCDDELAY);
CTRLCDCR=0;
Delay(LCDDELAY);
}
void LCDCLS()
{
int i,j;
CTRLCDCMDR=LCDCMDSTARTLINE;
Delay(LCDDELAY);
CTRLCDCR=0;
Delay(LCDDELAY);
for ( i=0;i<8;i++ )
{
CTRLCDCMDR=LCDCMDPAGE+i;
Delay(LCDDELAY);
CTRLCDCR=0;
Delay(LCDDELAY);
CTRLCDCMDR=LCDCMDVERADDRESS;
Delay(LCDDELAY);
CTRLCDCR=0;
Delay(LCDDELAY);
for ( j=0;j<64;j++ )
{
CTRLCDLCR=0;
Delay(LCDDELAY);
CTRLCDCR=0;
Delay(LCDDELAY);
}
CTRLCDCMDR=LCDCMDPAGE+i;
Delay(LCDDELAY);
CTRLCDCR=0;
Delay(LCDDELAY);
CTRLCDCMDR=LCDCMDVERADDRESS;
Delay(LCDDELAY);
CTRLCDCR=0;
Delay(LCDDELAY);
for ( j=0;j<64;j++ )
{
CTRLCDRCR=0;
Delay(LCDDELAY);
CTRLCDCR=0;
Delay(LCDDELAY);
}
}
}
void Delay(unsigned int nDelay)
{
int ii,jj,kk=0;
for ( ii=0;ii<nDelay;ii++ )
{
for ( jj=0;jj<1024;jj++ )
{
kk++;
}
}
} |