打印
[Atmel]

用AtmelStudio6.2跑mega16例程(34)INT0INT1记数

[复制链接]
820|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
ddllxxrr|  楼主 | 2014-10-16 22:09 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
本程序,同时允许INT0,INT1中断。GICR = 0XC0也可写作 GICR = _BV(INT0) | _BV(INT1);

MCUCR = 0x0A是将两个中断设为下降沿触发.

以下是仿真的电路图:



以下是Studion6.2编译截图:

以下是程序:
/*
* GccApplication20.c
*
* Created: 2014-10-16 21:18:46
*  Author: Administrator
*/

#define F_CPU 0x8000000UL
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <stdint.h>

#define K1_CLEAR_ON() ((PIND & 0x10)== 0x00)
#define K2_CLEAR_ON() ((PIND & 0x20)== 0x00)

const uint8_t SEG_CODE[] = { 0xc0,0xf9,0xa4,0x80,0x99,0x92,0x82,0xf8,0x80,0x90,0xff};
const uint8_t SCAN_BITs[] = {0x20,0x10,0x08,0x04,0x02,0x01};
uint8_t Buffer_Counts[] = {0,0,0,0,0,0};
uint16_t Count_A = 0,Count_B = 0;

void Show_Counts()
{
     uint8_t i;
         
         Buffer_Counts[2] = Count_A/100;
         Buffer_Counts[1] = Count_A %100/10;
         Buffer_Counts[0] = Count_A %10;
         
         if(Buffer_Counts[2]== 0)
         {
                Buffer_Counts[2] = 10;
                if(Buffer_Counts[1] == 0) Buffer_Counts[1] = 10;
          }
         
         Buffer_Counts[5] = Count_B/100;
         Buffer_Counts[4] = Count_B % 100 /10;
         Buffer_Counts[3] = Count_B % 10;
         
         if(Buffer_Counts[5] == 0)
     {
                 Buffer_Counts[5] = 10;
                 if(Buffer_Counts[4] == 0)Buffer_Counts[4] = 10;
         }         
     
         for(i=0;i<6;i++)
         {
                 PORTC = SCAN_BITs[i];
                 PORTB = SEG_CODE[Buffer_Counts[i]];
                 _delay_ms(1);
         }
}
int main(void)
{
    DDRB = 0xFF;PORTB = 0xFF;
        DDRC = 0xFF;PORTC = 0xFF;
        DDRD = 0x00;PORTD = 0xFF;
        MCUCR = 0x0A;
        GICR = 0xC0;
        sei();
       
        while(1)
    {
        if(K1_CLEAR_ON())Count_A = 0;
                if(K2_CLEAR_ON())Count_B = 0;
                Show_Counts();   
        }
}


ISR(INT0_vect)
{
        Count_A++;
}

ISR(INT1_vect)
{
        Count_B++;
}

相关帖子

发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:http://shop34182318.taobao.com/ http://shop562064536.taobao.com

2398

主题

6953

帖子

67

粉丝