硬件课程设计报告信科091班.docx
《硬件课程设计报告信科091班.docx》由会员分享,可在线阅读,更多相关《硬件课程设计报告信科091班.docx(34页珍藏版)》请在冰豆网上搜索。
硬件课程设计报告信科091班
《硬件课程设计》
班级:
信科09-班
姓名:
设计时间:
2011.10~2011.11
一、概要
二、焊接工艺总结
在硬件课程设计前,自己对那些电子元器件根本就不认识,跟不知道它们有什么区别,能完成什么样的功能。
这次硬件课程设计给了我一次熟悉与本专业有关的电子器件,更增强了自己的动手能力。
由于之前没有用过电烙铁也不会焊板子,所以一开始拿电烙铁时手会不由自主的抖,焊的拐脚也不是很好看,一块板子用了整整半天的功夫才勉强完成了。
这是课设让我明白了平时看起来很轻松的事,真正做起来却是很难得,一定要加强自己的动手能力。
三、编程思想
在充分了解51单片机各拐脚的功能后以及对它们的控制方式后,利用C语言,通过编程,生成烧写文件,控制单片机各拐脚高低电平的输入输出,来控制学习板上的资源完成一定的功能。
四、程序框图
五、源代码
1、点亮LED灯
#include
voidmain()
{
P0=0xAA;
P2=0x55;
while
(1);
}
2、流水灯的实现
//1、利用循环左移实现十六个LED的流水亮灭
#include
#include
#defineuintunsignedint
#defineucharunsignedchar
voidDelay(uintdel)
{
uinti,j;
for(i=del;i>0;i--)
for(j=110;j>0;j--);
}
voidmain()
{
uinti;
while
(1)
{
P2=0xff;
Delay(500);
P0=0xfe;
for(i=0;i<8;i++)
{
Delay(500);
P0=_crol_(P0,1);
}
P0=0xff;
Delay(500);
P2=0xfe;
for(i=0;i<8;i++)
{
Delay(500);
P2=_crol_(P2,1);
}
}
}
//利用取与表达式完成8个流水灯
#include
#defineucharunsignedchar
#defineuintunsignedint
ucharcodetable[9]={0xff,0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
voidDelay(uintdel)
{
uinti,j;
for(i=del;i>0;i--)
for(j=110;j>0;j--);
}
voidmain()
{
uchari;
P0=0xff;
P2=0xff;
while
(1)
{
for(i=1;i<=8;i++)
{
P0=table[i];
Delay(128);
}
P0=table[0];
for(i=1;i<=8;i++)
{
P2=table[i];
Delay(128);
}
P2=table[0];
}
}
3、八位数码管的静态显示
//八位数码管的静态显示:
八个数码管全部显示同一个字符
#include
#defineucharunsignedchar
ucharcodeduan[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
voidmain()
{
P2=0x00;
P0=duan[10];
}
4、八位数码管的动态显示
//数码管的动态显示1:
数码管从左往右依次显示“0~7”字符
#include
#defineucharunsignedchar
#defineuintunsignedint
ucharcodewei[]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe};
ucharcodeduan[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x39,0x5e,0x79,0x71};
voiddelay(uintms)
{
uinti,j;
for(i=ms;i>0;i--)
for(j=120;j>0;j--);
}
voidmain()
{
uchari=0;
while
(1)
{
P2=wei[i];
P0=duan[i];
delay
(1);
i++;
if(i==8)i=0;
}
}
//数码管的动态显示2:
数码管显示“HELLO!
”字符
#include
#defineucharunsignedchar
#defineuintunsignedint
ucharcodeduan[]={0x76,0x79,0x38,0x38,0x3f};
ucharcodewei[]={0x7f,0xbf,0xdf,0xef,0xf7};
voiddelay(uintms)
{
uinti,j;
for(i=ms;i>0;i--)
for(j=120;j>0;j--);
}
voidmain()
{
uchari=0;
while
(1)
{
P0=duan[i];
P2=wei[i];
delay
(1);
i++;
if(i==5)i=0;
}
}
5、输入端口的应用(按键检测)
/*检测输入端口是否存在独立按键K5、K6高低电平的变化,
并根据其变化来控制LED的亮灭。
*/
#include
#defineucharunsignedchar
#defineuintunsignedint
sbitK5=P1^6;
sbitK6=P1^7;
sbitLED3=P0^2;
sbitLED5=P0^4;
voiddelay(ucharms)
{
uinti,j;
for(i=ms;i>0;i--)
for(j=110;j>0;j--);
}
voidkey()
{
if(K5==0||K6==0)
{
delay(10);
if(K5==0||K6==0)
{
if(K5==0)
{LED3=~LED3;}
else
{LED5=~LED5;}
}
delay(500);
}
}
voidmain()
{
while
(1)
{
key();
}
}
6、99秒倒计时
//利用定时器中断来控制数码管实现99秒倒计时的功能。
#include
#defineucharunsignedchar
#defineuintunsignedint
ucharcodeduan[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
ucharnum1=0,num2=99,shi=9,ge=9;
voidT0_time_Init();
voiddisplay(ucharshi1,ucharge1);
voiddelay(ucharms)
{
uinti,j;
for(i=ms;i>0;i--)
for(j=110;j>0;j--);
}
voidmain()
{
T0_time_Init();
while
(1)
{
display(shi,ge);
}
}
voidT0_time_Init()
{
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
}
voiddisplay(ucharshi1,ucharge1)
{
P2=0xff;
P0=duan[shi1];
P2=0xfd;
delay(5);
P2=0xff;
P0=duan[ge1];
P2=0xfe;
delay(5);
}
voidT0_time()interrupt1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
num1++;
if(num1==20)
{
num1=0;
num2--;
if(num2==-1)num2=99;
shi=num2/10;
ge=num2%10;
}
}
7、定时器的应用
/*定时器的应用1:
利用计时器0在工作方式1下完成0~99的计数(每500ms加1)
而且在按下独立按键K5时暂停计数,按下K6时复位清零。
*/
#include
#defineucharunsignedchar
#defineuintunsignedint
sbitK5=P1^6;
sbitK6=P1^7;
ucharcodeduan[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
ucharnum1=0,num2=0,shi=0,ge=0;
voidT0_count_Init();
voiddisplay(ucharshi1,ucharge1);
voiddelay(ucharms)
{
uinti,j;
for(i=ms;i>0;i--)
for(j=110;j>0;j--);
}
voidmain()
{
T0_count_Init();
while
(1)
{
if(K6==0)
{
delay(5);
if(K6==0)
{num2=0;shi=0;ge=0;}
delay(20);
}
if(K5==0)
{
delay(5);
if(K5==0)
{TR0=~TR0;}
delay(20);
}
if(num1==10)
{
num1=0;
num2++;
if(num2==100)num2=0;
shi=num2/10;
ge=num2%10;
}
display(shi,ge);
}
}
voidT0_count_Init()
{
TMOD=0x01;
TH1=(65536-50000)/256;
TL1=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
}
voiddisplay(ucharshi1,ucharge1)
{
P2=0xff;
P0=duan[shi1];
P2=0xfd;
delay
(1);
P2=0xff;
P0=duan[ge1];
P2=0xfe;
delay
(1);
}
voidT0_time()interrupt1
{
TH1=(65536-50000)/256;
TL1=(65536-50000)%256;
num1++;
}
8、计数器的应用
/*计数器的应用:
利用计时器0在工作方式1下每按一次按键K3加一
完成0~59的计数
缺点:
由于在读取T0端口时存在抖动现象,所以每按一次按键会有很多变化
解决方案:
可以让T0端口(P3.4口)连接到其他某一端口,让该端口输出周期性的方波,可以周期计数*/
#include
#defineucharunsignedchar
#defineuintunsignedint
ucharcodeduan[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
ucharnum=0,shi=0,ge=0;
voidT0_count_Init();
voiddisplay(ucharshi1,ucharge1);
voiddelay(ucharms)
{
uinti,j;
for(i=ms;i>0;i--)
for(j=110;j>0;j--);
}
voidmain()
{
ucharth=0,th0=0,tl0=0;
T0_count_Init();
while
(1)
{
while
(1)
{
th0=TH0;
tl0=TL0;
th=TH0;
if(th0==th)break;//循环读取T0寄存器中的高地位,且高位读两次,
//确认读取高位时没有发生进位
}
num=th*256+tl0;
if(num>=60)
{
num=0;
TH0=0;
TL0=0;
}
shi=num/10;
ge=num%10;
display(shi,ge);
}
}
voidT0_count_Init()
{
TMOD=0x05;
TH0=0;
TL0=0;
TR0=1;
}
voiddisplay(ucharshi1,ucharge1)
{
P2=0xff;
P0=duan[shi1];
P2=0xfd;
delay(5);
P2=0xff;
P0=duan[ge1];
P2=0xfe;
delay(5);
}
9、外部中断的应用
/*外部中断的应用:
利用中断INT1完成0-59的循环计数。
并且利用中断INT0完成计数清零复位。
*/
#include
#defineucharunsignedchar
#defineuintunsignedint
ucharcodeduan[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
ucharnum=0,shi=0,ge=0;
voidINT0_Init();
voidINT1_Init();
voiddisplay(ucharshi1,ucharge1);
voiddelay(ucharms)
{
uinti,j;
for(i=ms;i>0;i--)
for(j=110;j>0;j--);
}
voidmain()
{
INT0_Init();
INT1_Init();
while
(1)
{
if(num>=60)num=0;
shi=num/10;
ge=num%10;
display(shi,ge);
}
}
voidINT0_Init()
{
EX0=1;//开外部中断0
IT0=1;//负边沿触发
EA=1;//开总中断
}
voidINT1_Init()
{
EX1=1;
IT1=1;
EA=1;
}
voidINT1_Int()interrupt2
{
EX1=0;
delay(5);
if(INT1==0)
{
num++;
while(INT1==0)
{
display(shi,ge);
}
}
EX1=1;
}
voidINT0_Int()interrupt0
{
EX0=0;
delay(3);
if(INT0==0)
{
num=0;
while(INT0==0)
{
display(shi,ge);
}
}
EX0=1;
}
voiddisplay(ucharshi1,ucharge1)
{
P2=0xff;
P0=duan[shi1];
P2=0xfd;
delay(5);
P2=0xff;
P0=duan[ge1];
P2=0xfe;
delay(5);
}
10、RS-232串口通信
/*RS-232串口通信的应用:
在上位机上用串口调试助手发送一个字符X,单片机收到字符后返回给上位
机"IgetX",串口波特率设为9600bps(波特率正常,SMOD取0)。
由于在12MHz下进行串口通信,定时初值不是一个整数会产生累计误差,故不能正确显示*/
#include
#defineucharunsignedchar
#defineuintunsignedint
ucharflag,a,i;
ucharcodetable[]="Iget";
voidinit()
{
TMOD=0x20;//设定T1定时器工作方式2
TH1=0xfc;//T1定时器装初值(12Mhz下12.025227)
TL1=0xfc;//在11.0592MHz下的计数初值为0xfd
TR1=1;//启动T1定时器
REN=1;//允许串口接收
SM0=0;//设定串口工作方式1
SM1=1;
EA=1;//开总中断
ES=1;//开串口中断
}
voidmain()
{
init();
while
(1)
{
if(flag==1)//已收到数据
{
ES=0;//关闭串口中断,发送数据
for(i=0;i<6;i++)
{
SBUF=table[i];
while(!
TI);
TI=0;//软件清零
}
SBUF=a;
while(!
TI);
TI=0;
ES=1;
flag=0;//标志传输完毕
}
}
}
voidser()interrupt4
{
RI=0;//软件清零
a=SBUF;
flag=1;//用于判断是否已经收到数据
}
11、蜂鸣器与音乐的应用
#include
#defineucharunsignedchar
#defineuintunsignedint
ucharCount;
sbitSpeak=P1^4;//蜂鸣器器控制脚
/*以下数组是音符编码*/
unsignedcharcodeSONG[]={//祝你平安
0x26,0x20,0x20,0x20,0x20,0x20,0x26,0x10,0x20,0x10,0x20,0x80,0x26,0x20,0x30,0x20,
0x30,0x20,0x39,0x10,0x30,0x10,0x30,0x80,0x26,0x20,0x20,0x20,0x20,0x20,0x1c,0x20,
0x20,0x80,0x2b,0x20,0x26,0x20,0x20,0x20,0x2b,0x10,0x26,0x10,0x2b,0x80,0x26,0x20,
0x30,0x20,0x30,0x20,0x39,0x10,0x26,0x10,0x26,0x60,0x40,0x10,0x39,0x10,0x26,0x20,
0x30,0x20,0x30,0x20,0x39,0x10,0x26,0x10,0x26,0x80,0x26,0x20,0x2b,0x10,0x2b,0x10,
0x2b,0x20,0x30,0x10,0x39,0x10,0x26,0x10,0x2b,0x10,0x2b,0x20,0x2b,0x40,0x40,0x20,
0x20,0x10,0x20,0x10,0x2b,0x10,0x26,0x30,0x30,0x80,0x18,0x20,0x18,0x20,0x26,0x20,
0x20,0x20,0x20,0x40,0x26,0x20,0x2b,0x20,0x30,0x20,0x30,0x20,0x1c,0x20,0x20,0x20,
0x20,0x80,0x1c,0x20,0x1c,0x20,0x1c,0x20,0x30,0x20,0x30,0x60,0x39,0x10,0x30,0x10,
0x20,0x20,0x2b,0x10,0x26,0x10,0x2b,0x10,0x26,0x10,0x26,0x10,0x2b,0x10,0x2b,0x80,
0x18,0x20,0x18,0x20,0x26,0x20,0x20,0x20,0x20,0x60,0x26,0x10,0x2b,0x20,0x30,0x20,
0x30,0x20,0x1c,0x20,0x20,0x20,0x20,0x80,0x26,0x20,0x30,0x10,0x30,0x10,0x30,0x20,
0x39,0x20,0x26,0x10,0x2b,0x10,0x2b,0x20,0x2b,0x40,0x40,0x10,0x40,0x10,0x20,0x10,
0x20,0x10,0x2b,0x10,0x26,0x30,0x30,0x80,0x00,
//路边的野华不要采
0x30,0x1C,0x10,0x20,0x40,0x1C,0x10,0x18,0x10,0x20,0x10,0x1C,0x10,0x18,0x40,0x1C,
0x20,0x20,0x20,0x1C,0x20,0x18,0x20,0x20,0x80,0xFF,0x20,0x30,0x1C,0x10,0x18,0x20,
0x15,0x20,0x1C,0x20,0x20,0x20,0x26,0x40,0x20,0x20,0x2B,0x20,0x26,0x20,0x20,0x20,
0x30,0x80,0xFF,0x20,0x20,0x1C,0x10,0x18,0x10,0x20,0x20,0x26,0x20,0x2B,0x20,0x30,
0x20,0x2B,0x40,0x20,0x20,0x1C,0x10,0x18,0x10,0x20,0x20,0x26,0x20,0x2B,0x20,0x30,
0x20,0x2B,0x40,0x20,0x30,0x1C,0x10,0x18,0x20,0x15,0x20,0x1C,0x20,0x20,0x20,0x26,
0x40,0x20,0x20,0x2B,0x20,0x26,0x20,0x20,