原创 AVR学习日志(三十三)-- 用计数器中断实现100以内的按键计数

2010-6-15 01:56 3627 4 4 分类: MCU/ 嵌入式


          用一下计数器:


   /******************************************************************
//文件名称:Timer0_int0
//功    能:学习使用定时器0计数,与中断0配合使用
//作    者:懒猫爱飞
//日    期:2010.05.28
*******************************************************************/


#include <iom8515v.h>
#include <macros.h>


#define uchar unsigned char
#define uint  unsigned int


//宏定义左移,右移函数crol为左移,cror为右移
#define crol(a,n) a=(a<<n)|(a>>(8-n))
#define cror(a,n) a=(a>>n)|(a<<(8-n))


uchar cunt="0";


const uchar Dis_code_cp[]=
{
  0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,
 0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,0xbf,0xff//共阳极0.1.2.3.4.5.6.7.8.9.A.b.C.d.E.F.-
};
const uchar Dis_code_cn[]=
{
  0x3f,0x06,0x5b,0x4f,0X66,0x6d,0x7d,0x07,0x7f,
 0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0X40,0X00//共阴极0.1.2.3.4.5.6.7.8.9.A.b.C.d.E.F.-
};


/****************************************************************
//函数名称:port_init(void)
//功    能:端口初始化
//入口参数:无
//出口参数:无
*****************************************************************/
void port_init(void)
{
 DDRA  = 0xff;  //A口
 PORTA = 0x00;
 
 DDRB  = 0x00;  //B口
 PORTB = 0x01;
 
 DDRC  = 0xff;  //C口
 PORTC = 0x00;


 DDRD  = 0x00;  //D口
 PORTD = 0xff;


}


/****************************************************************
//函数名称:init_devices(void)
//功    能:初始化器件
//入口参数:无
//出口参数:无
*****************************************************************/
void init_devices(void)
{
 //stop errant interrupts until set up
 CLI();       //disable all interrupts
 port_init();


 MCUCR = 0x00; //
 EMCUCR = 0x00;
 GICR = 0x40; //低电平触发
 
 TCCR0|=(1<<CS02)|(1<<CS01);//8M晶振,下降沿输入
 
 TIMSK|=(1<<TOIE0);//0x02;
 
 SEI();       //re-enable interrupts
}


/****************************************************************
//函数名称:int0_isr(void)
//功    能:中断处理函数
//入口参数:无
//出口参数:无
*****************************************************************/
#pragma interrupt_handler int0_isr:iv_INT0
void int0_isr(void)
{
  TCNT0=0;
}
/****************************************************************
//函数名称:void main(void)
//功    能:主函数
//入口参数:无
//出口参数:无
*****************************************************************/
void main(void)
{
 init_devices();
 while(1)
 {
  PORTA="Dis"_code_cn[TCNT0/10];  //显示十位
  PORTC="Dis"_code_cn[TCNT0%10];   //显示个位
 }
}


 


每天进步一点点,开心多一点^_^


 


 

PARTNER CONTENT

文章评论0条评论)

登录后参与讨论
我要评论
0
4
关闭 站长推荐上一条 /1 下一条