单片机实验报告2

  • 格式:docx
  • 大小:35.88 KB
  • 文档页数:5

单片机程序设计实验
实验报告
实验名称
学号
姓名
日期
一、实验内容
I/O控制数码管
利用单片机I/O输出直接控制数码管二、程序流图及说明
三、程序清单
/*IO实验*/
// Target : M128
// Crystal: 11.059Mhz
#include <iom128v.h>
#include <macros.h>
//#define a[]={0x3f,0x06,0x5B,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
void delay_1ms(void)//延时1ms
{
int i;
for (i=0;i<1140;i++);
}
void delay(int n)//延时n ms
{
int i=0;
for (i=0;i<n;i++)
delay_1ms();
}
void port_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;
}
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
XDIV = 0x00; //xtal divider
XMCRA = 0x00; //external memory
port_init();
MCUCR = 0x00;
EICRA = 0x00; //extended ext ints
EICRB = 0x00; //extended ext ints
EIMSK = 0x00;
TIMSK = 0x00; //timer interrupt sources
ETIMSK = 0x00; //extended timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
//
void main(void)
{
unsigned char a[10]={0x3f,0x06,0x5B,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; char m;
int n;
init_devices();//初始化
while(1)
{
DDRA = 0xff; //A端口输出
DDRB = 0x00; //B端口输入
PORTB = 0xff;
//m=PORTB;
//m & 0x00;//点亮D1(LED)
for(n=5;n<10|n>=0;)
{
if(!(PINB&1))
{
PORTA=a[n];
delay(1000);//延时20ms
n++;
if(n>9)
{
n=0;
}
}
else
{
PORTA=a[n];
delay(1000);//延时20ms
n--;
if(n<0)
{
n=9;
}
}
}
}
}
四、存在问题及解决办法
基本实现计数之后,在及时转换加减时出现问题,最后发现程序中对键盘的判定不准确,更改后实现了及时转换功能;
在实现及时转换功能之后,转换之后的运算并不能正常输出,之后发现是范围超出,对程序加上限制之后很好地实现了功能;。