j__choi 发表于 2011-11-19 09:46:16

马老师,我做了你书中用一个按键控制秒表的练习,能帮我看下哪里错了么?

This program was produced by the
CodeWizardAVR V2.03.4 Standard
Automatic Program Generator
?Copyright 1998-2008 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com

Project :
Version :
Date    : 2011-11-18
Author:
Company :
Comments:


Chip type         : ATmega16
Program type      : Application
Clock frequency   : 16.000000 MHz
Memory model      : Small
External RAM size   : 0
Data Stack size   : 256
*****************************************************/

#include <mega16.h>

flash unsigned char led_buffer={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
flash unsigned char position={0xfe,0xfd,0xfb,0xf7,0xef,0xdf};
unsigned char time;
unsigned char display_buffer;
//unsigned char   point_on;
unsigned char timecout,count,keyflag,flag;

bit timeflag;
void time_to_buffer(void)
{
unsigned char i,j=0;
    for(i=0;i<=3;i++)
      {
            display_buffer=time%10;
            display_buffer=time/10;
      }
}
void display(void)
{
    PORTC=0XFF;
    PORTA=led_buffer];
    if(count==2||count==4)PORTA|=0X80;
    PORTC=position ;
    if(++count>=6)count=0;
}
// Timer 0 output compare interrupt service routine
interrupt void timer0_comp_isr(void)
{
    display();
    if(++timecout>=5)
      {
            timecout=0;
            timeflag=1;
            if(flag==1)
                {
                  ++time;
                }
            
      }

}
unsigned char key_scan(void)
{
static unsigned char key_state;   
unsigned char key_return;
bit key_temp;
key_temp=PIND.1;
switch(key_state)
    {
      case 0:
            if(!key_temp) key_state++;
            break;
      case 1:
            if(!key_temp)
                {
                  key_state++;
                  ++keyflag;
                  key_return=1 ;
                }
      else    key_state=0;
            break;
      case 2:
            if(key_temp)key_state=0;
            break;         
    }
return key_return;
}

// Declare your global variables here

void main(void)
{
DDRA=0xFF;
PORTC=0xFF;
DDRC=0xFF;
PORTD=0x01;
DDRD=0x00;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 62.500 kHz
// Mode: CTC top=OCR0
// OC0 output: Disconnected
TCCR0=0x0C;
TCNT0=0x00;
OCR0=0x7c;
TIMSK=0x02;
time=0;time=0;time=0;
time_to_buffer();
// Global enable interrupts
#asm("sei")

while (1)
      {
      if(timeflag)
            {
                timeflag=0;
                if(key_scan())
                  {
                        switch(keyflag)
                            {
                              case 0:
                                    ;
                                    break;
                              case 1:
                                    flag=1;
                                    break;
                              case 2:
                                    flag=0;
                                    break;
                              case 3:
                                    keyflag=0;
                                    time=0;time=0;time=0;
                                    break;
                            }
                  }
                  
            }
         if(time>=100)
            {
                time=0;
                if(++time>=60)
                  {
                        time=0;
                        ++time;
                  }
            }
            time_to_buffer();
      };
}

avr-qq 发表于 2011-11-19 11:21:21

像托儿所一样

icefier 发表于 2011-11-19 11:25:30

呵呵,看别人的程序是很痛苦的事,没注释的更痛苦,但是也往往也是在看别人好代码的后,提高自己编程规范性的

zhaoxukiller 发表于 2011-11-22 14:30:30

time清零了吗?

不清楚你那什么现象,主程序while(1)循环中不太对

timeflag清零了吗。

xuanfong1 发表于 2014-4-27 21:15:27

路过路过赞下吧
页: [1]
查看完整版本: 马老师,我做了你书中用一个按键控制秒表的练习,能帮我看下哪里错了么?