#include
#include
#include
#include
#include
#include
#define A8251 0x308
#define Com8255 0x303
#define PA8255 0x300
#define PB8255 0x301
#define PC8255 0x302
#define data51 0x308 /* 8251 数据口地址 */
#define ctrl51 0x309 /* 8251 控制口地址 */
#define ctrl55 0x303 /* 8255 控制口地址 */
#define timer2 0x306 /* 8253 counter 2 port address */
#define timctrl 0x307 /* 8253 控制口地址 */
#define clk00 0x12 /* 8251 时钟频率为 1.193 Mhz */
#define clk01 0x34de
#define factor 16
initscreen() /* 显示模式初始化 */
{
int graphdriver=VGA;
int graphmode=VGAHI;
initgraph(&graphdriver,&graphmode,"");
cleardevice();
/* 绘制道路 */
line(150,200,280,200);
line(360,200,490,200);
line(150,280,280,280);
line(360,280,490,280);
line(280,80,280,200);
line(360,80,360,200);
line(280,280,280,400);
line(360,280,360,400);
}
lightsCTRL(int i) /* 信号灯控制 */
{
int s1,s2;
switch(i)
{
case 0:s1=0;s2=0;break;
case 1:s1=0;s2=1;break;
case 2:s1=1;s2=0;break;
case 3:s1=2;s2=2;break;
default:printf("error!");
}
switch(s1) /* 垂直方向行人通行信号灯控制 */
{
case 0:setcolor(RED);setfillstyle(SOLID_FILL,RED);break;
case 1:setcolor(GREEN);setfillstyle(SOLID_FILL,GREEN);break;
case 2:setcolor(YELLOW);setfillstyle(SOLID_FILL,YELLOW);break;
default:printf("error!");
}
bar(220,200,260,209); /* 绘制斑马线 */
bar(220,218,260,227);
bar(220,236,260,245);
bar(220,254,260,263);
bar(220,272,260,280);
bar(380,200,420,209);
bar(380,218,420,227);
bar(380,236,420,245);
bar(380,254,420,263);
bar(380,272,420,280);
switch(s2) /* 水平方向行人通行信号灯控制 */
{
case 0:setcolor(RED);setfillstyle(SOLID_FILL,RED);break;
case 1:setcolor(GREEN);setfillstyle(SOLID_FILL,GREEN);break;
case 2:setcolor(YELLOW);setfillstyle(SOLID_FILL,YELLOW);break;
default:printf("error!");
}
bar(280,140,289,180);
bar(298,140,307,180);
bar(316,140,325,180);
bar(334,140,343,180);
bar(352,140,360,180);
bar(280,300,289,340);
bar(298,300,307,340);
bar(316,300,325,340);
bar(334,300,343,340);
bar(352,300,360,340);
switch(s2) /* 水平方向车辆通行信号灯控制 */
{
case 0:setcolor(RED);setfillstyle(SOLID_FILL,RED);break;
case 1:setcolor(GREEN);setfillstyle(SOLID_FILL,GREEN);break;
case 2:setcolor(YELLOW);setfillstyle(SOLID_FILL,YELLOW);break;
default:printf("error!");
}
pieslice(320,240,135,225,15); /* 绘制信号灯 */
pieslice(320,240,0,45,15);
pieslice(320,240,315,360,15);
switch(s1) /* 垂直方向车辆通行信号灯控制 */
{
case 0:setcolor(RED);setfillstyle(SOLID_FILL,RED);break;
case 1:setcolor(GREEN);setfillstyle(SOLID_FILL,GREEN);break;
case 2:setcolor(YELLOW);setfillstyle(SOLID_FILL,YELLOW);break;
default:printf("error!");
}
pieslice(320,240,45,135,15);
pieslice(320,240,225,315,15);
}
void display(int x)
{
if(x==0) lightsCTRL(0);
if(x==1) lightsCTRL(1);
if(x==2) lightsCTRL(2);
if(x==3) lightsCTRL(3);
if(x==4) exit(-1);
}
void ST_8255(void)
{
outportb(Com8255,0x80); /* 初始化 */
outportb(Com8255,0x0d); /* 按位置位复位命令 */
/* 设置 8255 的 PC6=1 来打开 8253 的 GATE2 , 输出方波 */
}
void ST_8253(void)
{
int bps=1200;
int datah;
int datal;
long tp;
tp = 1193182/(16*bps);
datah = tp>> 8;
datal = tp & 0x00ff;
outportb(timctrl,0x0b6); /* 8253 初始化 */
outportb(timer2,datal);
outportb(timer2,datah);
}
void ST_8251(void)
{
int i;
outport(ctrl51,0); /* 激活 8251 */
outport(ctrl51,0x40); /* 8251 内部复位 */
outport(ctrl51,0x4e); /* 8251 方式命令 */
outport(ctrl51,0x27); /* 8251 工作命令 */
}
int AD_state(void)
{
int n,pc4,pa;
outportb(0x303,0x88); /*初始化8255状态字,A口输出*/
/*outportb(0x300,0);*/ /*A口清0*/
outportb(0x300,1); /*送通道号至A口*/
outportb(0x303,0x03); /*pc1置1进行地址锁存*/
/*delay(50);*/
outportb(0x303,0x98); /*重写8255方式字,A口输入,C口低四位输出,C口高四位输入*/
outportb(0x303,0x01); /*使PC0置高发启动信号*/
outportb(0x303,0x02);
/*delay(50);*/
outportb(0x303,0x00); /*使PC0置低*/
do
{
pc4=inportb(0x302)&0x10;
}while(pc4==0); /*查询PC4的状态,直至为高电位,即,转换完成*/
outportb(0x303,0x05); /*pc2=1,允许读转换结果*/
pa=inportb(0x300); /*读转换结果*/
/* 显示转换结果*/
delay(50);
return (pa);
}
void communication2() /* 发送源信号 */
{
int sletter=(AD_state()/2);
int status;
status=inportb(ctrl51);//获取8253 TXRDY 状态
if((status&0x01)!=0)//已准备好,则将键入的字符发送出去
{
outportb(data51,sletter);/* 发送采集到的数据 */
}
}
int communication(void)
{
char i,rletter='z';
int j,status;
status=inportb(ctrl51);/*已准备好,即有数据传送过来,则接收1个字符*/
if((status&0x02)!=0)
{
rletter=inportb(data51);
printf("\n%c",rletter);
}
i=rletter;
if(i=='1')return(1);
else
if(i=='2')return(2);
else return(3);
}
void main(void)
{
int i=1,k;
/*initscreen();*/
ST_8253();
ST_8251();
ST_8255();
for(;;)
{
for(k=100;k>0;k--)
{
i=communication();
printf("receive=%d",i);
}
communication2();printf("sent");
/*if(i==3)
{
clrscr();
exit(1);
}*/
/*display(i);*/
}
}
Turbo Link Version 5.1 Copyright (c) 1992 Borland International
Error: Underfined symbol _setcolor in module c:\temp\cpp\cpp1.cpp
Error: Underfined symbol _setfillstyle in module c:\temp\cpp\cpp1.cpp
Error: Underfined symbol _bar in module c:\temp\cpp\cpp1.cpp
Error: Underfined symbol _line in module c:\temp\cpp\cpp1.cpp
Error: Underfined symbol _cleardevice in module c:\temp\cpp\cpp1.cpp
Error: Underfined symbol _initgraph in module c:\temp\cpp\cpp1.cpp
Error: Underfined symbol _pieslice in module c:\temp\cpp\cpp1.cpp
Available memory 4054152