/* Main.c file generated by New Project wizard
*
* Created: 周五 1月 6 2017
* Processor: 8086
* Compiler: Digital Mars C
*
* Before starting simulation set Internal Memory Size
* in the 8086 model properties to 0x10000
*/
#define BASE8255 8000H //设定值
#define PA BASE8255+00H*2 //PA口
#define PB BASE8255+01H*2 //PB口
#define PC BASE8255+02H*2 //PC口(本次未使用)
#define COM8255 BASE8255+03H*2 //工作方式
#define BBASE8255 9000H //设定值
#define BPA 9000H //PA口
#define BPB 9040H //PB口
#define BPC 9080H //PC口(本次未使用)
#define COMB8255 90C0H //工作方式
#define ADC 0E002H
char xiangxu[8]={02H,06H,04H,0CH,08H,09H,01H,03H}; //相序表
void outp(unsigned int addr, char data) {
__asm {
mov dx, addr
mov al, data
out dx, al
}
}
// Read a byte from the specified I/O port
char inp(unsigned int addr) {
char result;
__asm {
mov dx, addr
in al, dx
mov result, al
}
return result;
}
void delay(){
for(int i=0;i<50;++i){
for(int j=0;j<120;++j)
;
}
}
void delayms(int ms){
for(int i=0;i<ms;++i)
for(int j=0;j<122;++j){}
}
char tmp,judge;
int m_i=0,t_i=0;
char tmp1,tmp2;
int spd=0;
unsigned int speed=0;
unsigned char speed1=0,speed2=0,speedshow=0;
void main(void)
{
//设置工作方式:10001010(特征位=1,A组0方式=00,PA输出=0,PC上半输入=1,B组0方式=0,PB输入=1,PC下半输出=0)
outp(COM8255,08AH);
//设置工作方式:10001000(特征位=1,A组0方式=00,PA输出=0,PC上半输出=1,B组0方式=0,PB输出=0,PC下半输出=0)
outp(COMB8255,088H);
outp(PA,0FFH);
outp(PC,0FFH);
outp(PB,00H);
while (1){
tmp=inp(PB);//车灯判断,用到PB口低三位,输出到PA口低四位
tmp=tmp&06H;
if(tmp==04H){
tmp=inp(PB);
tmp1=(tmp)&01H;
if(tmp1==00H)tmp=0b11111010;
if(tmp1==01H)tmp=0b11111011;
outp(PA,tmp);
}
else if(tmp==02H){
tmp=inp(PB);
tmp1=(tmp)&01H;
if(tmp1==00H)tmp=0b11111100;
if(tmp1==01H)tmp=0b11111101;
outp(PA,tmp);
}
else {
tmp=inp(PB);
tmp1=(tmp)&01H;
if(tmp1==00H)tmp=0b11111110;
if(tmp1==01H)tmp=0b11111111;
outp(PA,tmp);
}
unsigned char tmp3=inp(PB);//速度判断,用到PB口3~6位,输出到PC口低四位
tmp3=tmp3&0b11111000;
switch(tmp3){
case 0b11110000:spd=-1;break;
case 0b11101000:spd=0;break;
case 0b11011000:spd=1;break;
case 0b10111000:spd=2;break;
case 0b01111000:spd=3;break;
}
if(spd!=-1 && spd!=0){
outp(PC,xiangxu[m_i]);
if(m_i>0)--m_i;
else m_i=7;
tmp=inp(PA);
tmp=tmp&0b11111111;
outp(PA,tmp);
if(speed<22*spd)speed+=(spd+1);
else speed-=(spd+1);
delayMS((4-spd)*35);
}
else if(spd==-1){
outp(PC,xiangxu[m_i]);
if(m_i<7)++m_i;
else m_i=0;
tmp3=inp(PA);
tmp3=tmp3&0b11110111;
outp(PA,tmp3);
if(speed>5)speed-=2;
else speed=5;
delayMS(50);
}
else if(spd==0){
if(speed>0)speed-=1;
delayMS(50);
}
tmp=inp(PA);//转方向判断,用到PC口高四位,输出到PA口高四位
tmp1=tmp&(0FH);
tmp=inp(PC);
tmp2=tmp&(030H);
if(tmp2==20H){
if(t_i>0)--t_i;
else t_i=7;
tmp3=(xiangxu[t_i])<<4;
tmp3=tmp3+tmp1;
outp(PA,tmp3);
delayMS(50);
}
else if(tmp2==10H){
if(t_i<7)++t_i;
else t_i=0;
tmp3=(xiangxu[t_i])<<4;
tmp3=tmp3+tmp1;
outp(PA,tmp3);
delayMS(50);
}
speed1=speed/10;//速度输出
speed2=speed%10;
speedshow=(speed1<<4)+speed2;
outp(BPA,speedshow);
tmp=inp(BPC)&0b00001111;//故障灯输出
if(tmp==0EH){
outp(BPB,0FFH);
delayMS(100);
outp(BPB,0F0H);
}
else {
outp(BPB,0FFH);
}
for(int i=0;i<9;++i){
outp(ADC,0);
tmp=inp(ADC);
}
tmp3=tmp;
tmp=inp(BPC);
tmp3=tmp|(tmp3&01H);
outp(BPC,tmp3);
}
}