单片机实验报告3.docx

上传人:b****5 文档编号:2779867 上传时间:2022-11-15 格式:DOCX 页数:12 大小:100.83KB
下载 相关 举报
单片机实验报告3.docx_第1页
第1页 / 共12页
单片机实验报告3.docx_第2页
第2页 / 共12页
单片机实验报告3.docx_第3页
第3页 / 共12页
单片机实验报告3.docx_第4页
第4页 / 共12页
单片机实验报告3.docx_第5页
第5页 / 共12页
点击查看更多>>
下载资源
资源描述

单片机实验报告3.docx

《单片机实验报告3.docx》由会员分享,可在线阅读,更多相关《单片机实验报告3.docx(12页珍藏版)》请在冰豆网上搜索。

单片机实验报告3.docx

单片机实验报告3

单片机程序设计实验

实验报告

 

实验名称

学号

姓名

日期

 

一、实验内容

单片机I/O口控制实验

利用单片机与7279芯片,实现键盘对数码管的控制。

涉及7279的功能及用法与单片机的I/O口用法。

二、程序流图及说明

 

三、程序清单

 

/*IO实验*/

//Target:

M128

//Crystal:

11.059Mhz

#include

#include

unsignedcharkey_number;

voiduart1_init()

{

UCSR1B=0x00;//disablewhilesettingbaudrate

UCSR1A=0x00;//异步正常模式

UCSR1C=0x06;//USART1异步操作,无奇偶校验,1个停止位,8个数据位,时钟位异步模式

UBRR1H=0x00;

UBRR1L=0x11;//波特率位38400

UCSR1B=0x18;//允许发送接受中断和使能

}

voiddelay_6ms(inttime)

{

inti;

for(;time>0;time--)

for(i=0;i<1140;i++);

}

voiduart_send_char(intc)//发送一个字符

{

while(!

(UCSR1A&(1<

UDR1=c;//将待发送的数据存入USART1I/O数据寄存器

delay_6ms(10);//延时10ms

}

voiddelay_1ms(void)//延时1ms

{

inti;

for(i=0;i<1140;i++);

}

voidlong_delay(void)

{

inti;

for(i=0;i<1140;i++);

}

voidshort_delay(void)

{

inti;

for(i=0;i<11;i++);

}

voiddelay(intn)//延时nms

{

inti=0;

for(i=0;i

delay_1ms();

}

 

unsignedcharreceive_byte(void)

{

unsignedchari,in_byte;

unsignedcharp;

PORTB=(PORTB&0xfe);//cs=0;

DDRB=(DDRB&0xf7);

PORTB&=0xf7;

//PORTB=(PORTB|0x08);//data=1;

//long_delay();

PORTB=(PORTB&0xfb);//clk=0;

for(i=0;i<8;i++)

{

PORTB=(PORTB|0x04);//clk=1;

long_delay();

in_byte=in_byte*2;

p=(PINB&0x08);

if(p)

{

in_byte=in_byte|0x01;

}

PORTB=(PORTB&0xfb);//clk=0;

long_delay();

}

PORTB=(PORTB&0xf7);//data=0;

PORTB=(PORTB|0X01);//

returnin_byte;

}

 

voidsend_byte(unsignedcharout_byte)

{

unsignedchari;

PORTB=(PORTB&0xfe);//cs=0;

long_delay();

for(i=0;i<8;i++)

{

if(out_byte&0x80)

{

PORTB=(PORTB|0x08);//data=1;

}

else

{

PORTB=(PORTB&0xf7);//data=0;

}

PORTB=(PORTB|0x04);//clk=1;

short_delay();

PORTB=(PORTB&0xfb);//clk=0;

short_delay();

out_byte=out_byte*2;

}

PORTB=(PORTB|0X01);

}

unsignedcharread7279(unsignedcharcommand)

{

send_byte(command);

return(receive_byte());

}

voidport_init(void)//IO口初始化

{

PORTA=0x00;

DDRA=0x00;

PORTB=0x00;

DDRB=0x00;

PORTC=0x00;

DDRC=0x00;

PORTD=0x00;

DDRD=0x00;

PORTE=0x00;

DDRE=0x00;

PORTF=0x00;

DDRF=0x04;

PORTG=0x00;

DDRG=0x00;

}

//callthisroutinetoinitializeallperipherals

voidinit_devices(void)

{

//stoperrantinterruptsuntilsetup

CLI();//disableallinterrupts

XDIV=0x00;//xtaldivider

XMCRA=0x00;//externalmemory

port_init();

MCUCR=0x00;

EICRA=0x00;//extendedextints

EICRB=0x00;//extendedextints

EIMSK=0x00;

TIMSK=0x00;//timerinterruptsources

ETIMSK=0x00;//extendedtimerinterruptsources

SEI();//re-enableinterrupts

//allperipheralsarenowinitialized

}

 

voidmain(void)

{

unsignedchara[10]={0,1,2,3,4,5,6,7,8,9};

charm,j,k,l;

intn=0;

intb=0;

intc=0;

intd=0;

init_devices();//初始化

uart1_init();

DDRB=0xff;//B端口输出

DDRA=0x00;//A端口输入

PORTA=0xff;

send_byte(0xa4);

key_number=0xff;

while

(1)

{

/*uart_send_char(0x01+0X30);//将读取的数据发送到串行调试助手中显示

uart_send_char(0x0A);*/

//send_byte(0xbf);

if(!

(PINA&0x01))

{

send_byte(0x15);

PORTB=PORTB|0x10;

long_delay();

key_number=receive_byte();

uart_send_char(key_number);//将读取的数据发送到串行调试助手中显示

uart_send_char(0x0A);

/*PORTB=PORTB|0x10;*/

send_byte(0xc8);

send_byte(a[n]);

send_byte(0xc9);

send_byte(a[b+2]);

send_byte(0xca);

send_byte(a[c]);

send_byte(0xcb);

send_byte(a[d]);

//PORTB=PORTB|0x10;

if(!

(key_number&0x80))

{

n++;

PORTB=PORTB|0x10;

if(n>9)

{

n=0;

b++;

if(b>9)

{

b=0;

c++;

if(c>9)

{

c=0;

d++;

if(d>9)

{

d=0;

}

}

}

}

send_byte(0xc8);

send_byte(a[n]);

send_byte(0xc9);

send_byte(a[b]);

send_byte(0xca);

send_byte(a[c]);

send_byte(0xcb);

send_byte(a[d]);

/*PORTB=PORTB|0x10;*/

delay(1000);//延时20ms

}

else

{

if(!

(key_number&0xfd))

{

n--;

if(n<0)

{

n=9;

b--;

if(b<0)

{

b=9;

c--;

if(c<0)

{

c=9;

d--;

if(d<0)

{

d=9;

}

}

}

}

send_byte(0xc8);

send_byte(a[n]);

send_byte(0xc9);

send_byte(a[b]);

send_byte(0xca);

send_byte(a[c]);

send_byte(0xcb);

send_byte(a[d]);

/*PORTB=PORTB|0x10;*/

delay(1000);//延时20ms

}

else

{

if(!

(key_number&0xfb))

{

n=0;

b=0;

c=0;

d=0;

send_byte(0xc8);

send_byte(a[n]);

send_byte(0xc9);

send_byte(a[b]);

send_byte(0xca);

send_byte(a[c]);

send_byte(0xcb);

send_byte(a[d]);

}

}

}

}

}

}

 

四、存在问题及解决办法

基本实现计数之后,在及时转换加减时出现问题,最后发现程序中对键盘的判定不准确,更改后实现了及时转换功能;

在实现及时转换功能之后,转换之后的运算并不能正常输出,之后发现是范围超出,对程序加上限制之后很好地实现了功能;

对于键盘按键判断,利用7279自带功能实现,根据时序图,对7279进行赋值,并将CLK,DATA,KEY分时复用,在一个周期内,先实现输出,后将7279值输入到单片机中。

对于调试采用方法:

将KEY-NUMBER的值利用仿真器,输入到电脑中,实现对值的实时读取与判断程序运行状态,效果好。

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 工程科技 > 能源化工

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1