定时器实验报告模板

定时器实验试验报告

成员:             

  20##年12月

 一、实验要求:

1.实验1:修改秒表程序,分别使用溢出中断或比较匹配中断实现计时功能,时钟显示为0~9.99.9s,外部中断实现停表显示和继续功能;

2.实验2修改时钟功能,使用溢出中断或比较匹配中断实现计时,使用外部中断INT0实现位选设置,外部中断INT1实现选定位数量的调整,实现可设定时间的时钟。时间显示为0~5分59.9秒;

 二、硬件原理图

1.  硬件原理图

2.原理图分析:

三、软件设计系统

  1. 软件设计思想与流程图

   软件流程图如下(标准流程图画法)……:

实验1、秒表

定时器实验报告模板

定时器实验报告模板

 2.软件代码:

实验一:

#include

#include

#include

#include

unsigned

char led_7[10]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};

unsigned char position[4]={0xef,0xdf,0xbf,0x7f};

unsigned char time[2];

unsigned char dis_buff[4];

unsigned char time_counter;

unsigned char point_on;

unsigned char posit;

unsigned char time_1s_ok;

unsigned char i;

SIGNAL(SIG_OUTPUT_COMPARE2)

    time_1s_ok=1;

}

ISR(INT0_vect)

{

   if (++i>=2)

   i=0;

}

ISR(INT1_vect)

 {

     time[1]=00;time[0]=00;i=0;time_to_disbuffer();

     _delay_ms(200);

 }

void display(void)

{

    unsigned char i;

    for(i=0;i<=3;i++)

     { 

       PORTB=led_7[dis_buff[i]];

       if(i==2)PORTB&=point_on;

       PORTD=position[i];

       _delay_ms(7);

       PORTD=0xff;

      }

}

void time_to_disbuffer(void)

{

   unsigned char i,j=0;

   for(i=0;i<=1;i++)

    {

       dis_buff[j++]=time[i]%10;

       dis_buff[j++]=time[i]/10;

     }

}

void init_devices(void)

{

    cli();    //禁止所有中断

    MCUCR  = 0x0A;

    MCUCSR = 0x80;//禁止JTAG

    GICR   = 0xC0;

    GIFR = 0XC0;

    sei();    //开全局中断

}

void main()

{

    PORTB=0x00;

    DDRB=0xFF;

    PORTD=0xf0;

    DDRD=0xf0;

    TCCR2=0X0d;

    TCNT2=0X00;

    OCR2=0X7c;

    TIMSK=0xC0;

init_devices();

   point_on=0x7f;

   time[1]=00;time[0]=00;

   posit=0;

    i=1;

    time_to_disbuffer();

   while(1)

   {

     display();

      if(i>=1){

      if(time_1s_ok)

      {

        time_1s_ok=0;

        point_on=0x7f|~point_on;

        if(++time[0] >=99)

          {

            time[0]=0;

            if(++time[1] >=99)

              {

                time[1]=0;

               }

           }

         time_to_disbuffer();

       }

    };

}

}

实验二:

#include

#include

Const unsigned char led_7[10] = {0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};

const unsigned char position[5] = {0xEF,0xDF,0xBF,0x7F,0xFF};

unsigned char time[2];            

unsigned char dis_buff[4]; 

volatile unsigned char ready;                          //50.2ready即为1

unsigned char point_on;                           //point_on1时分割点亮

unsigned char stop=0;                                 //stop1时停止计时

unsigned char a;                                   //a=变化选位用辅助变量

int b;                                               //b=恢复用常量

void delay(unsigned char time)                                       //延时

{

       unsigned int i;

       for(;time>0;time--)

              for(i=0;i<420;i++);

}

ISR (INT0_vect)                                                      //外部中断(INT0选位,INT1增加数字)

{

       b=0;                           //按下INT0就清零b,以免调整过程中恢复计时(b=600时就会恢复计时)

       stop=1;                       //stop=1时暂停计时

       a++;                           //按下INT0即换一位选择

       if(a>=4) a=0;              //当第4位选过后自动回到第一位

       delay(20);                  //消抖

}

ISR (INT1_vect)            

{

       b=0;                           //INT0同原理

       switch(a)                    //第几位选中,就相应增加该位数字

       {

              case 0: time[0]++;            break;

              case 1: time[0]=time[0]+10;    break;

             case 2: time[1]++;            break;

             case 3: time[1]=time[1]+10;    break;

              default:break;

       }

       delay(20);

}

ISR(TIMER0_OVF_vect)                                               //定时器2溢出中断(六次为一秒)

{

       ready++;                    //ready每加一就累计0.2

       if(ready==5)                     //五次即为一秒

       {

              ready=0;

              if(stop==0)   time[0]++;    //stop1(也就是暂停),则不增加时间,只记时

              point_on = ~point_on;    

       };

       TCNT0 = 256-(1000000/1024)*0.2;

}

void display(void)                                               //扫描函数

{

    unsigned char i;

    for (i=0;i<=3;i++)

    {

       PORTB = led_7[dis_buff[i]];       

       if( point_on &&(i == 2 ))       PORTB &= ~(1<<7);       //分隔点

       if(i!=a||point_on==0)                             //INT0按下,分割点亮时,某位不亮(选中位闪烁)

       {

              PORTD &= position[i];

       }                          

       delay(2);

       PORTD |=~position[i];                        

    }

}

void time_to_disbuffer(void)                                    //赋值函数

{

    unsigned char i,j = 0;

    for(i = 0;i <= 1;i++)

       {

           dis_buff[j++] = time[i] % 10;

          dis_buff[j++] = time[i] / 10;

       }

}

int main(void)                                                     //主函数

{

      PORTB = 0xff;

       DDRB  = 0xff;

       PORTD = 0xff;

       DDRD  = 0xff;

       DDRD &=~(1<将外部中断PD口打开                                 

       PORTD |= (1<

       DDRD &=~(1<

       PORTD |= (1<

//     MCUCR |=(1<

//     MCUCR |=(1<

       MCUCR =0x0a;                            //下降沿触发

       GICR |=(1<开外部中断

       GIFR |=(1<清中断标志

       TCNT0 = 256-(1000000/1024)*0.2;           //0.2秒一次溢出中断

       TIMSK |= 0x01;

       TIFR  = 0xC0;

       TCCR0 = 0x05;                      //普通模式1024分频

       sei();

       ready=0;

       a=4;

       time[1] = 58;time[0] = 55;

      

       time_to_disbuffer();

    while(1)

    {

              display();                   //显示

                 if(time[0] >= 60)        //进制运算循环

                 {

                     time[0] = 0;

                     if(++time[1] >= 60)

                     {

                            time[1] = 0;

                     }

               }

              if(stop==1&&(++b>=600))           //时间恢复函数(b上限值决定等待时间)

              {

                     stop=0;

                     a=4;      

              }

              if(time[1] >= 60)time[1] = 0;   //调试状态时分位满60清零

               time_to_disbuffer();        //赋值

       };

}

. 成员分工及完成情况

……

. 意见及建议

……

相关推荐