⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 周期计算.c

📁 通过对avr单片机编程
💻 C
字号:
//ICC-AVR application builder : 2007-8-6 下午 10:34:43
// Target : M8
// Crystal: 8.0000Mhz

#include <iom8v.h>
#include <macros.h>
#define uchar unsigned char
#define uint unsigned int
uchar i=0,j=0,flag=0,k;
uint  dnew,dold,dmid,c;
const uchar   Display[]={0x3f,0x06,0x5b,0x4f,0x66,
                  0x6d,0x7d,0x07,0x7f,0x6f  };
				  
				 
void delay_1ms(void)
{ unsigned int i;
 for(i=1;i<10;i++)
    {;}
}
void delay_ms(unsigned int n)
{
 unsigned int i=0;
   while(i<n)
   {delay_1ms();
    i++;
   }
}
				  
				  
void port_init(void)
{
PORTB = 0x00;
DDRB = 0xff;
PORTC = 0x00; //m103 output only
DDRC = 0xff;
PORTD = 0x00;
DDRD = 0x00;
}

//TIMER0 initialize - prescale:1024
// desired value: 20mSec
// actual value: 19.968mSec (0.2%)
void timer1_init(void)
{TCCR1B=0x00;
 TCNT1H=0xFF;
  TCNT1L=0xFE;
  OCR1AH=0x00;
  OCR1AL=0x00;
  OCR1BH=0x00;
  OCR1BL=0x00;
  TCCR1B=0x01;
 //TCCR0 = 0x00; //stop
//TCNT0 = 0xfe; //set count
//TCCR0 = 0x05; //start timer
}

#pragma interrupt_handler timer1_ovf_isr:9
void timer1_ovf_isr(void)
{
  TCNT1H=0xFF;
  TCNT1L=0xFE;
  PORTB^=0x01;
  
  c++;
  k=PIND&0x01; 
  if(k==1)
  {dnew++;}
   
if(c>=1000)
  {c=0;
if(dnew==dold)
  {PORTC=0x01;
   delay_ms(100);}
   if(dnew>196)
   {PORTC=0x02;
   delay_ms(100); }
   dold=dnew;
   dnew=0;}

//TCNT0 = 0xfe; //reload counter value
//i++;
//if(i==1)
}

void init_devices(void)
{
//stop errant interrupts until set up
CLI();//disable all interrupts
port_init();
timer1_init();
MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x1C; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}




void main(void)
{
c=0;
dold=0;
dnew=0;
init_devices();
while(1)
{;}
}
	 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -