单键控制秒表实验程序

上一篇 / 下一篇  2007-10-03 14:26:20 / 天气: 阴雨 / 心情: 平静 / 精华(1) / 置顶(1) / 个人分类:AVR单片机

/****************************************************
/****************************************************
名称:单键控制秒表程序
参数:使用外部晶振(4M)
用法:将SAVR-51板上的PORTD--数码管的段码,PORTA口数码管的位。
      PORTB.7接K1
功能:按第一次K1,秒表开始计时,按第二次K1,秒表停止计时,并显示当前的计时时间
      按第三次K1,秒表清零。
设计人:kaiser-朱
日期:
/****************************************************
/****************************************************/
#include <mega16.h>
#define uchar unsigned char
#define uint unsigned int
#define key_input  PINB.7   // 按键输入口
#define key_state_0 0
#define key_state_1 1
#define key_state_2 2

flash char tab_data[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x27,0x7f,0x6f,/*A*/0x77,0x7c,0x39,0x5e,0x79,0x71,0x40};
               /*   0   1     2    3    4    5    6    7    8    9         A    B    C    D    E    F    -  */
flash char position[8]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
char temp;     
char   sec100,sec,min;
char key_stime_counter;// 时间计数单元,
bit key_stime_ok; 
char stop_temp;
char dis_buff[8]; // 显示缓冲区,存放要显示的8个字符的段码值
char posit; 
char time[4];

void display(void) // 6位LED数码管动态扫描函数
{
 PORTA = 0xff;
 PORTD = tab_data[dis_buff[posit]];
 PORTA = position[posit];
 if (++posit >=6 ) posit = 0;
}


char read_key(void)
{
  static char key_state = 0;
  char key_press, key_return = 0;

  key_press = key_input;    // 读按键I/O电平
  switch (key_state)
  {
   case key_state_0:    // 按键初始态
    if (!key_press) key_state = key_state_1; // 键被按下,状态转换到键确认态
    break;
   case key_state_1:    // 按键确认态
    if (!key_press)
    {
     key_return = 1;   // 按键仍按下,按键确认输出为"1" (1)
     key_state = key_state_2; // 状态转换到键释放态
    }
    else
     key_state = key_state_0; // 按键已抬起,转换到按键初始态
    break;
   case key_state_2:
    if (key_press) key_state = key_state_0; //按键已释放,转换到按键初始态
    break;
  } 
     return key_return;
}
      
void time_to_disbuffer(void)    // 时钟时间送显示缓冲区函数
{
  char i,j=0;
  time[0]=min;
  time[1]=sec;
  time[2]=sec100;
  for (i=0;i<=3;i++)
   {
    dis_buff[j++] = time[i] / 10;
    dis_buff[j++] = time[i] % 10;
   }
}


// Timer 0 比较匹配中断服务,2ms定时
interrupt [TIM0_COMP] void timer0_comp_isr(void)
{
   display();// LED扫描显示
   if (++key_stime_counter >=5)
     {
       key_stime_counter = 0;
       key_stime_ok = 1;// 10ms到  
       if(stop_temp==1)
        {
         sec100++;
         if(sec100>=100)
           {
             sec100=0;
             sec++;
             if(sec>=60)
               {
                 sec=0;
                 min++;
                 if(min>=60)
                   {
                     min=0;
                   }
               }
           }
         }
      
     }
 
}

void main(void)
{
  PORTD = 0x00; // 显示控制I/O端口初始化
  DDRD = 0xFF;
  PORTB = 0xFF;
  DDRB = 0x00  ; 
  PORTA =0xff;
  DDRA=0xff;
  // T/C0 初始化
  TCCR0=0x0B;// 内部时钟,64分频(4M/64=62.5KHz),CTC模式
  TCNT0=0x00;
  OCR0=0x7C; // OCR0 = 0x7C(124),(124+1)/62.5=2ms
  TIMSK=0x02;// 允许T/C0比较匹配中断  
  sec100=0;sec=0;min=0;
  time_to_disbuffer();
  #asm("sei")// 开放全局中断
  while (1)
   {
      if (key_stime_ok)    
       {
         key_stime_ok = 0; // 10ms到
         if (read_key())
           { // 有按键按下
             if(temp++>=3)temp=1;
             switch(temp)
               {
                  case 1:
                         stop_temp=1;//开始计时
                         break;
                  case 2:
                         stop_temp=0;//停止计时
                         break;
                  case 3: 
                         stop_temp=0;//停止计时
                         sec100=0;sec=0;min=0;//计时器清0 
                         break;
               }
           }
           time_to_disbuffer();//更新显示缓冲区
       }
 
    };
}


TAG: AVR单片机

 

评分:0

我来说两句

显示全部

:loveliness: :handshake :victory: :funk: :time: :kiss: :call: :hug: :lol :'( :Q :L ;P :$ :P :o :@ :D :( :)

我的栏目

日历

« 2008-11-20  
      1
2345678
9101112131415
16171819202122
23242526272829
30      

我的存档

数据统计

  • 访问量: 710
  • 日志数: 5
  • 商品数: 1
  • 建立时间: 2007-10-02
  • 更新时间: 2007-10-03

RSS订阅

Open Toolbar