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

📄 iar9-3.c

📁 手把手教你学AVR单片机C程序设计实验程序
💻 C
字号:
#include <iom16.h>
#include<intrinsics.h>
#define uchar unsigned char
#define uint unsigned int
#define CPL_BIT(x,y) (x^=(1<<y))
__flash uchar SEG7[10]={0x3f,0x06,0x5b, 
	  0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
__flash uchar ACT[4]={0xfe,0xfd,0xfb,0xf7}; 
uint cnt; 
uchar start_flag; 
uchar i; 
#define S1 (PIND&0x10)
#define xtal 8

//****************************************
void Delay_1ms(void)		
{ uint i;
 for(i=1;i<(uint)(xtal*143-2);i++)
    ;
}
//=============================================
void Delay_nms(uint n)		
{
 uint i=0;
   while(i<n)
   {Delay_1ms();
    i++;
   }
}

void port_init(void)
{
 PORTA = 0x00; 
 DDRA  = 0xFF; 
 PORTC = 0xFF; 
 DDRC  = 0xFF; 
 DDRD  = 0xFF;
 PORTD = 0xFF; 
 DDRD  = 0x00;

}

void timer0_init(void)
{
 TCNT0 = 0x83; 
 TCCR0 = 0x03; 
}

void timer1_init(void) 
{
 TCNT1H = 0xD8; 
 TCNT1L = 0xF0;
}

void init_devices(void)
{
 __disable_interrupt(); //disable all interrupts 
 port_init();
 timer0_init();
 timer1_init();
 MCUCR = 0x02; 
 GICR  = 0x40; 
 TIMSK = 0x05;
 __enable_interrupt();//re-enable interrupts 
}

void scan_s1(void)
{
if(S1==0)
 {
  Delay_nms(10);
 if(S1==0)cnt=0;
 }
}

void main(void) 
{
init_devices();
  while(1) 
  {
  if(start_flag==0x01)TCCR1B = 0x02; 
if(start_flag==0x00){TCCR1B = 0x00;scan_s1();}
							
  }						
}	

#pragma vector=INT0_vect
__interrupt void int0_isr(void)
{
  CPL_BIT(start_flag,0);
  Delay_nms(10);
}

#pragma vector=TIMER0_OVF_vect
__interrupt void timer0_ovf_isr(void)
{
 SREG=0x80;
 TCNT0 = 0x83; 
 if(++i>3)i=0; 
 switch(i)
 {
 case 0: PORTA=SEG7[cnt%10]; PORTC=ACT[i];break;
 case 1: PORTA=SEG7[(cnt/10)%10]; PORTC=ACT[i];break;
 case 2: PORTA=SEG7[(cnt/100)%10]|0x80; PORTC=ACT[i];break;
 case 3: PORTA=SEG7[cnt/1000]; PORTC=ACT[i];break;
 default:break;
 }
}

#pragma vector=TIMER1_OVF_vect
__interrupt void timer1_ovf_isr(void)
{
 TCNT1H = 0xD8; 
 TCNT1L = 0xF0; 
 if(++cnt>9999)cnt=0;
}

⌨️ 快捷键说明

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