/*****the address of 8255*****/ #define C8255 XBYTE[0xF22B] #define P8255A XBYTE[0xF228] #define P8255B XBYTE[0xF229] #define P8255C XBYTE[0xF22A] #define P8255 XBYTE[0xF22A] /***** three tables *****/
unsigned char code table[]={ //Segment value for LED 0x06,0x5b,0x4f,0x66, 0x6d,0x7d,0x07,0x7f};
unsigned char code table1[]={ 0x7f,0xbf,0xdf,0xef, 0xf7,0xfb,0xfd,0xfe}; unsigned
0x80,0x40,0x20,0x10, 0x08,0x04,0x02,0x01}; void init(); void keyscan();
void currentshow(unsigned char); void delay(); void delayms(); unsigned int move; unsigned int currentfloor; unsigned int direction; unsigned int destination[9]; char times=0;
unsigned int repairs; //0->do not repair; 1->repairing
/**************************************** ***********************************/ void main() {
unsigned char i,j;
init(); /*init the elevator*/ currentshow(currentfloor); while(1)
//2s //20ms
char
//->Extinguish the led on current floor
code
table2[]={ //P1=table1[elevator.currentlevel]|table2[elevator.currentlevel]
//0x7f->Light P1^0 ... //8255command
第 4 页 共 12 页
常州大学硬件实习报告
{
while(repairs<=250) // repairs<=250 need to repair repairs=P3^3; move=P3^1; keyscan();
while(move==254&&destination[0]!=0) //moving key was pressed {
while(repairs<=250) // repairs<=250 need to repair
repairs=P3^3;
//and at least 1 level wanted
keyscan(); move=P3^1; switch(direction) {
case 2:{
if(currentfloor==0) //declining { } else {
{ }
direction=1;
if(destination[j]==0x1)
break;
for(j=currentfloor;j>0;j--)
//weather
move=0; direction=1;
there are destinations downstairs
if(j<=0) else {
if(destination[currentfloor+1]==1) {
move=0;
destination[0]--; direction=0;
destination[currentfloor]=0; P1|=table2[currentfloor];
第 5 页 共 12 页
常州大学硬件实习报告
//Extinguish the corresponding led
}
//rising
if(currentfloor==8) //weather there are destinations { } else {
for(j=currentfloor+1;j<9;j++) { } if(j>8) { }
{
move=0;
destination[0]--; direction=0;
destination[currentfloor+1]=0; P1|=table2[currentfloor]; }
currentshow(currentfloor); currentfloor++; direction=2; else
if(destination[j]==0x1)
break;
move=0; direction=2;
}
// \"|\"->bit or
}
currentfloor--;
currentshow(currentfloor);
} break;
case 1:{
upstairs
if(destination[currentfloor+1]!=0)////////////////
//Extinguish the corresponding led// \"|\"->bit or
第 6 页 共 12 页
常州大学硬件实习报告
{
}
}
}
}
} }
}
break;
default:{ //stationary
for(i=1;i<9;i++) //decide up or down {
move=0;
destination[0]--; direction=0;
destination[currentfloor]=0;//the destination is
//do nothing but extiguish the led
if(destination[i]!=0)
break;
if(idirection=2; direction=1;else if(i>currentfloor)//should up else
current level,
P1|=table2[currentfloor-1];
}
delay(); //2s/level
void init() //timer,elevator
unsigned char i;
/****timer and external interrupt init*******/ TMOD=0x01;
TH0=(65536-45872)/256; //50ms TL0=(65536-45872)%256; EA=1; ET0=1; EX0=1; EX1=1;
第 7 页 共 12 页
常州大学硬件实习报告
}
IT1=1;
/******elevator init******/ move=0;
/*0->the elevator now will not move,commands comes from p3^0*/
for(i=0;i<9;i++)
destination[i]=0; //no destination
direction=0; //the elevator is stationary at the start currentfloor=0;//the elevator stops at first level at the start /*****8255 init*****/
C8255=0x82; //PB in;PA,PC out repairs=P3^3;
void keyscan()
{ /*save total number of destination to destination[0]*/ /*set destination[1-9] if the correponding level wanted*/ unsigned char temp,key; P8255C=0x2; temp=P8255B;
if(temp!=0xff) //key pressed {
delayms(); temp=P8255B;
if(temp!=0xff) //confirmed {
temp=P8255B; switch(temp) { }
destination[key+1]=1; //set the corresponding level destination[0]++; //total number of destination
case
0xfe:key=0;P1&=table1[key];break;
//P1=table1[key]->light the corresponding led
case 0xfd:key=1;P1&=table1[key];break; case 0xfb:key=2;P1&=table1[key];break; case 0xf7:key=3;P1&=table1[key];break; case 0xef:key=4;P1&=table1[key];break; case 0xdf:key=5;P1&=table1[key];break; case 0xbf:key=6;P1&=table1[key];break; case 0x7f:key=7;P1&=table1[key];break;
第 8 页 共 12 页
常州大学硬件实习报告
}
}
temp=P8255B; while(temp!=0xff) temp=P8255B; }
void currentshow(unsigned char num) { }
void delayms() { }
void delay() { }
void T0_timer() interrupt 1 { }
void extern0() interrupt 0 //wait to repair {
EX1=0; if(repairs==0) { }
repairs=1;
TH0=(65536-45872)/256; //50ms TL0=(65536-45872)%256; times++; TR0=1;
while(times!=40) times=0; TR0=0;
unsigned int i,j; //delay for 20ms for(i=20;i>0;i--)
for(j=110;j>0;j--); P8255C=0x0; //wei xuan P8255A=table[num];
第 9 页 共 12 页
常州大学硬件实习报告
}
else { } EX1=1;
repairs=0;
void extern1() interrupt 3 //emergency { }
direction=0; //stop the elevator move=0;
5.设计心得
本次实习我们选择了模拟日常生活中常见的电梯运行过程。 这次实习让我对程序的设计、调试有了更深刻的认识。尤其是写单片机这类的与硬件相关的程序。在调试的时候会有不少意想不到的结果。这要求除了对程序充分了解,还要对硬件的特性,电路的连接也深刻认识。
关于程序,总结如下:必须养成在写之前仔细分析需求、要求,画出流程图,先明确各个模块的作用。最后分块细化,从底向上设计的习惯。否则很容易出现程序写了一半发现理解出错,功能达不到的情况。还有程序注释必须及时,不能等到写完了再注释,否则可能之后连自己也看不懂程序。及时的修改记录也是必要的。
对于硬件、电路:要熟练掌握如何用c、汇编语言操作芯片。要掌握常用芯片的用法、特性,至少要熟悉其datasheet,需要使用时通过查阅手册,能快速、熟练的使用该芯片。
参考文献
[1].王玉芳 钟旭.微机原理与接口技术实验教程.广东高等教育出版社.2005年5月第2次印刷.
[2]赵德安.单片机原理与应用[M].北京:机械工业出版社,2008年5月 [3]向艳,周天彤.C语言程序设计[M].北京:清华大学出版社,2008年9月.
[4]宁飞,王维华,孔宇.微型计算机原于是与接口实践[M].北京:清华大学出版社,2006.
[5]周炯如,潘操.EAT单片机应用开发系统实验指导书[M].常州:常州大学信息科学与工程学院.2010年7月.
第 10 页 共 12 页