利用系统外部中断做PS键盘信号接收

 
#include  < iom8v.h >
#include 
< macros.h >

/*-----------------*
* PC0 ------ DATA  *
* PD2 ------ CLOCK *
* PB0~7 --- LED1~8 *
*-----------------
*/


#define  PC0          0
#define  PC1          1
#define  PC2          2
#define  PC3          3
#define  PC4          4
#define  PC5          5
#define  PC6          6
#define  PC7          7

#define  CUR_DATA (PINC & BIT(PC0) ? BIT(0) : 0x00)     // 当前传送的DATA值
#define  LED_DATA PORTB     // LED灯接口

void  Device_Init( void )     // 系统初始化
{
    Port_Init();
    
    
//stop errant interrupts until set up
    CLI(); //disable all interrupts

    MCUCR 
= 0x02;    //设置成INT0端口下降沿触发
    GICR  = 0x40;
    TIMSK 
= 0x00//timer interrupt sources
    SEI(); //re-enable interrupts
 
//all peripherals are now initialized
}

void  Port_Init( void )
{
    DDRB 
= 0xFF;
    DDRD 
= 0x00;
    DDRC 
&= ~BIT(PC0);
    PORTC 
= 0xFF;
    PORTB 
= 0xFF;
}


#pragma  interrupt_handler int0_isr:iv_INT0

void  int0_isr( void )
{
    
//external interupt on INT0    
    static UINT8 s_chData = 0;    //用来保存读取的数据

    
static UINT8 s_chCur_Bit = 0;    //用来计数,表示当前获取到第几位    
    if(s_chCur_Bit > 10)                //如果已经获取完整个数据
    {
        s_chCur_Bit 
= 0x00;          //计数器置零
        s_chData = 0x00;            //数据置零
    }

    
else if(s_chCur_Bit >= 1 && s_chCur_Bit <= 8)      //当前数据为有用位
    {
        s_chData 
|= CUR_DATA << (s_chCur_Bit - 1);      //因为数据是从最低位开始传输的
    }

    
if(s_chCur_Bit == 8)                                              //如果数据获取完整
    {
        LED_DATA 
= ~s_chData;                              //用LED灯显示结果
    }

    s_chCur_Bit
++;                       //计数器加一
}


void  main()
{    
    Device_Init();
    
    
while(1)      //程序空转,碰到中断执行中断函数
    {;}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值