atm16 多功能秒表

来源:互联网 发布:淘宝客qq推广教程 编辑:程序博客网 时间:2024/04/30 22:01

这是我写的第一个秒表程序,是用ICC写的,用protel仿真的

 

 

//包含所需头文件
#include <ioM16v.h>
#include <macros.h>

 


/*------宏定义------*/
#define uchar unsigned char
#define uint unsigned int
 uchar table[]={0x77,0x44,0xB5,0xD5,0xC6,0xD3,0xF3,0x45,0xF7,0xD7};/*0,1,2,3,4,5,6,7,8,9,*/
 uchar table1[]={0x7F,0x4C,0xBD,0xDD,0xCE,0xDB,0xFB,0x4D,0xFF,0xDF};/*0,1,2,3,4,5,6,7,8,9,*/

uchar ran[4], Ms[200],S[200],Min[200];
uchar ms,s,min,d,is_ran=0;
//*********************************

void delayMS(uint t)
{
uint i,j;
 for(i=0;i<t;i++)
  for(j=0;j<1140;j++);
}

//端口初始化
void port_init(void)
{
 PORTA = 0x00;
 DDRA  = 0xFF;
 PORTB = 0xFF;
 DDRB  = 0xFF;
 PORTC = 0x00;
 DDRC  = 0x00;
 PORTD = 0xFF;
 DDRD  = 0x00;
}


//定时器T0初始化
void timer0_init(void)
{
 TCCR0  = 0x00;//停止定时器
 TCNT0  = 0xB2;//初始值
 TIMSK |= 0x01;//中断允许
 TCCR0  = 0x05;//启动定时器
 
}


//T0溢出中断服务程序
#pragma interrupt_handler timer0_ovf_isr:10
void timer0_ovf_isr(void)
{
 TCNT0 = 0xB2;
 if(is_ran)
 {
   ran[0]=ms%10;
   ran[1]=ms/10;
   ran[2]=s%10;
   ran[3]=s/10;
 }

 for(d=0;d<=3;d++)//显示
 {PORTB&=~BIT(d);
 switch(d)
 {
   case 0:PORTA=table[ran[3]];break;
   case 1:PORTA=table1[ran[2]];break;
   case 2:PORTA=table[ran[1]];break;
   case 3:PORTA=table[ran[0]];break;
   default:break;
  }
   delayMS(1);
   PORTA=0x00;
   PORTB|=BIT(d);
 }
}


//定时T1初始化
void timer1_init(void)
{
 TCCR1B = 0x00;//停止定时器
 TIMSK |= 0x04;//中断允许
 TCNT1H = 0xD8;
 TCNT1L = 0xF0;//初始值
 TCCR1A = 0x00;
 TCCR1B = 0x00;//启动定时器
}


//定时器T1溢出中断服务程序
#pragma interrupt_handler timer1_ovf_isr:9
void timer1_ovf_isr(void)
{
 TCNT1H = 0xD8; //重装值高位
 TCNT1L = 0xF0; //重装值低位
 
 ms++;
 if(ms==100)
 {
  ms=0;
  s++;
  if(s==60)
  { s=0;
   min++;
   if(min==100)
   min=0;
  }
 
 }
}


void init_devices(void)
{
 CLI(); //禁止所有中断
 MCUCR  = 0x00;
 MCUCSR = 0x80;//禁止JTAG
 GICR   = 0x00;
 port_init();
 timer0_init();
 timer1_init();
 SEI();//开全局中断
}
//主函数
void main(void)
{
 uchar cl,cou=0,f=cou/10,cout=0;
 init_devices();
 //在这继续添加你的代码
 while(1)
 {
  if((PIND&BIT(3))==0)//开始/暂停
  {
  delayMS(5);
   if((PIND&BIT(3))==0)
   is_ran=1;
    TCCR1B ^= 0x02;    //按位异或(相同是为0不同时为1)
   while((PIND&BIT(3))==0);
  }
  //**************************************
  if((PIND&BIT(4))==0)//显示现在数据
  {
   delayMS(10);
   if((PIND&BIT(4))==0)
  {
 cout++;
  is_ran=0;
  Ms[cout]=ms;
  S[cout]=s;
  ran[0]=Ms[cout]%10;
  ran[1]=Ms[cout]/10;
  ran[2]=S[cout]%10;
  ran[3]=S[cout]/10;
  while((PIND&BIT(4))==0);
 }
  }
 //***************************************
   if((PIND&BIT(5))==0)//重新显示
    {delayMS(2);
 if((PIND&BIT(5))==0)
  {
   
    is_ran=1;
 
 /* if(is_ran)
  {
     is_ran=0;
       cou=cou+1;
   ran[0]=S[cou]%10;
   ran[1]=S[cou]/10;
   ran[2]=Min[cou]%10;
   ran[3]=Min[cou]/10;
   if(cou>cout)
  {cou=0;}
  }*/
  
    while((PIND&BIT(5))==0);
  }
 
 
 }
 //*********************************************/清空数据
 if((PIND&BIT(6))==0)
 {
  delayMS(2);
  if((PIND&BIT(6))==0)
  {
     TCCR1B = 0x00;
  for(cl=0;cl<100;cl++)
  Ms[cl]=0;
  S[cl]=0;
  cout=0;
  ms=0;
  s=0;
  min=0;
  cou=0;
  f=0;
    is_ran=0;
  Ms[cout]=ms;
  S[cout]=s;
  Min[cout]=min;
  ran[0]=Ms[cout]%10;
  ran[1]=Ms[cout]/10;
  ran[2]=S[cout]%10;
  ran[3]=S[cout]/10;
  while((PIND&BIT(6))==0);
  }
 }
  //*******************************************显示保存数据
  if((PIND&BIT(2))==0)
  {
   delayMS(10);
   if((PIND&BIT(2))==0)
   { is_ran=0;
  /* if(Ms[cou]==S[cou]==0)
  { cou=0;}*/
     cou=cou+1;
  ran[0]=cou%10;
  ran[1]=cou/10;
  ran[2]=f%10;
  ran[3]=cou/100;
  delayMS(300);
     ran[0]=Ms[cou]%10;
  ran[1]=Ms[cou]/10;
  ran[2]=S[cou]%10;
  ran[3]=S[cou]/10;
 
 
  while((PIND&BIT(2))==0);if(cou>cout)
  {cou=0;}
   }
  }
 
  /*//*******************************************
  if((PIND&BIT(2))==0)
  {
   delayMS(1);
   if((PIND&BIT(2))==0)
   {
   
  is_ran=0;
  if(cou==f)
     { cou=cout+11;}
      cou=cou-11;
     ran[0]=Ms[cou]%10;
  ran[1]=Ms[cou]/10;
  ran[2]=S[cou]%10;
  ran[3]=S[cou]/10;
 
  while((PIND&BIT(2))==0);
   }
  }
  //*******************************************/
 }
}
//********************************************

 

 

不买后悔 最后冲刺 三皇冠大放送 休闲韩国版无袖背心 亏本10元

弹性超好,透气性好,穿着舒适!

性感的方领设计,完美的修身效果,

充分展现男性的阳刚之美与性感魅力!

7种颜色,款款经典!

绝对是您不容错过的时尚背心!

夏天单穿,秋冬打底穿,一年四季都可穿着!

图片

地址 http://s.click.taobao.com/t_1?i=rE2%2FTWI2C5Mb%2BQ%3D%3D&p=mm_16390215_0_0&n=11高质量版!采用2*2纯棉针织面料,