4292706gdl 发表于 2007-12-12 18:14:27

刚开始接触AVR,写了个按键数码管显示加1的程序,不知道为什么老是不正确,望指教

马老师:
你好,刚开始接触过AVR,以前全用汇编写的,也是做51和PIC的,现在刚开始接触AVR,写了个按键数码管显示加1的程序,不知道为什么老是不正确,望指教,程序如下:/ICC-AVR application builder : 2007-12-12 17:27:22
// Target : M16
// Crystal: 14.7456Mhz

#include <iom16v.h>
#include <macros.h>
const led_table={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
typedef unsigned char uint8;
uint8 i;

void port_init(void)
{
PORTA = 0xFF;
DDRA= 0xFF;
PORTB = 0xFf;
DDRB= 0xFE;
PORTC = 0xFF; //m103 output only
DDRC= 0xFF;
PORTD = 0x00;
DDRD= 0x00;
}

//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();

MCUCR = 0x00;
GICR= 0x00;
TIMSK = 0x00; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
void delay(void)
{unsigned int j;
for(j=0;j<1000;j++)
{;}
}
void main(void)
{int temp;
init_devices();
while(1){

if(PINB==0XFf)
{PORTA=led_table;
PORTB=0XFe;
PORTC=0X00;
}
else
{delay();
if(PINB==0XFE)
{do{i=i+1;
   if(i==10)i=0;
   PORTA=led_table;
   PORTB=0XFe;
   PORTC=0XFf;}while(PINB==0XFF);}
else
{PORTA=led_table;
PORTB=0XFe;
}
}
}}

ganghu1111 发表于 2007-12-12 19:59:59

好奇怪的晶振哟。。。:)
开发环境描述一下先??

4292706gdl 发表于 2007-12-12 22:33:30

呵呵,我用的是14。7456晶震,30P磁片电容,我自己用万用板焊接的学习扳,好像是消抖那里有问题,按下键现实1,再按下现实9,再按下4,等等,反正不是按照我的要求出现的,马老师能给我看看吗?

machao 发表于 2007-12-12 22:50:06

请下载本栏置顶栏中红色的我编写教材的前11章电子版文档仔细学习.里面有按键的设计例子,在第9章.建议能认真学习,打好基础.
页: [1]
查看完整版本: 刚开始接触AVR,写了个按键数码管显示加1的程序,不知道为什么老是不正确,望指教