ATMEGA8 Counter

Publisher:数字奇迹Latest update time:2016-09-14 Source: eefocusKeywords:ATMEGA8 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
//Can measure 1MHZ frequency

#include
#include
#define uchar unsigned char
#define uint unsigned int
/***********The corresponding digital tubes display********************/
const uchar table[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
unsigned long load=0;
uchar data[4]={0,0,0,0};//
uint cnt=0;//Number of interrupts in timing
uchar temp=0;//This number is required when it exceeds 65536
//
void display(uchar *p)
{
 uchar i,j,sel=0x01;
 for(j=0;j<2;j++)
  for(i=0;i<4;i++)
  {PORTB=~table[p[i]];
   PORTC=sel;
   Delay_us(2000);
   sel=sel<<1;
   }
}
void process(unsigned long t,uchar *p)//Data processing
{unsigned long i=t; 
 if(i<=9999)
  PORTD=PORTD|0X03;
  else if((i<=99999)&&(i>9999))
  { 
  i=i/100;
  PORTD=PORTD&0 XFE;
  PORTD=PORTD|0X02;
  }
  else if(i>99999)
 {
  PORTD=PORTD|0X01;
  PORTD=PORTD&0XFD;
  i=i/1000;
  }
  p[3]=i/1000;
 i=i%1000;
 p[2]=i/100;
 i=i%100;
 p[1]=i/10;
 i=i%10;
 p[0]=i;
 }
void int_io(void)
{DDRB=DDRC=0XFF;
 DDRD=0XDF;
 PORTC=PORTB=0XFF;
 PORTD=0XFF;
 SREG=0X80;//Global enable
 TCCR1B=0X06;//Counting mode: T1 falling edge
 TCNT1H=0X00;//Initial value of counter
 TCNT1L=0X00;
  }
void main(void )
{
 int_io();
 PORTB=0X00;
 PORTC=0XFF;
 Delay_ms(500);
 PORTB=0XFF;
 /****************************/
 TCNT0=0X06;//The initial value of timing count is 6
 TCCR0=0X04;//T/C0 works in timing mode, system clock is divided by 256
 TIMSK=0X01;//Timing local enable T/C0
 while(1)
 {
  process(load,da ta);
  display(da ta);
  if(TIFR&0X04)//Use the flag bit to handle events when >65536
   { // if(TIFR&0X20)
       temp++;
    TIFR=TIFR&0X04;
 }
  }
 }
 /************************/
 ////////////////The following is the timer interrupt of T0
#pragma interrupt_handler time0:10
void time0(void)

  uint tep=0;//Read the value of high word TCNT1L
   cnt++;
  if(125==cnt)//125*256us, and this is used to adjust the accuracy;
    {
  cnt=0;//
  /********The following is the value of reading the counter***********/
  load=0;
  load=TCNT1L; //Read the low bit value of the counter
  tep=TCNT1H*256; //Read the high bit value of the counter and process it
  load=tep+load; //High bit plus low bit
  load=load+65536*temp; //Processed data
  /***********************/
  TCNT1H=0X00;//Reassign initial value
     TCNT1L=0X00;
  temp=0;
 }
}
//TO is used for timing,

Keywords:ATMEGA8 Reference address:ATMEGA8 Counter

Previous article:External asynchronous clock operation
Next article:AVR MCU fuse setting tips

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号