这是PS/2接口的键盘的通讯程序,程序只是实现了接收数据和发送数据,其中还有很多BUG之处,但功能完全可以实现,放在这城供大家学习,如果有人用于商业用途,请和我联系。magicchip@163.com
struct ps2struct ps2;
/**********************************************************************************
** 函数名称: void EINT3_Init(void)
** 函数功能: 对外部中断3进行初始化,.
** 入口参数: 无
** 出口参数: 无
** 说明:
** create by: magicchip
** create data: 2007/12/05
*************************************************************************************/
void EINT3_Init(void)
{
PINSEL1 |= ((PINSEL1 & 0xccffffff) | (0x20000000)); //EINT3,p0.30,clk = p0.30,sda = p0.28
EXTMODE |= 0x08; //EINT3边沿中断
EXTPOLAR |= 0x00; //EINT3下降沿中断
EXTINT |= 0x08; //清除EINT3中断标志
install_irq(EINT3_INT,EINT3Prio,(void *)IRQ_EINT3);
ps2.flag = (0x00 | ps2parity);
ps2.bits = 0x00;
ps2.bytes = 0x00;
}
/**********************************************************************************
** 函数名称: void ps2txd(uint8 txdbuf)
** 函数功能:
** 入口参数: 无
** 出口参数: 无
** create by: magicchip
** create data: 2007/12/06
*************************************************************************************/
void ps2txd(uint8 txdbuf)
{
uint16 a;
if (ps2.bits<10) //接收位数小于10
{
ps2.flag &= ~ps2txdok;
ps2.flag |= ps2txdflag ;
PINSEL1 = (PINSEL1 & 0xcfffffff); //GPIO P0.30.
IO0DIR |= (PS2CLK|PS2DATA); //out
IO0CLR = PS2CLK; //clk = 0
DelayNS(10);
ps2.bits = 0x00;
ps2.bytes = 0x00;
ps2.buf[0] = (((uint16)txdbuf) | (stopbit>>1) | (paritybit>>1));
IO0CLR = PS2DATA; //data = 0
IO0DIR &= ~PS2CLK; //clk in
PINSEL1 |= ((PINSEL1 & 0xccffffff) | (0x20000000)); //EINT3,p0.30,clk = p0.30,sda = p0.28
}
}
/**********************************************************************************
** 函数名称: void __irq IRQ_EINT3(void)
** 函数功能:
** 入口参数: 无
** 出口参数: 无
** create by: magicchip
** create data: 2007/12/06
*************************************************************************************/
void __irq IRQ_EINT3(void)
{
uint16 *string;
if ((ps2.flag & ps2txdflag) == ps2txdflag) //发送数据.
{
DelayNS(1);
if (ps2.bits < 10)
{
if ((ps2.buf[0] & (1<<ps2.bits)) == (1<<ps2.bits)) //send 1
{
IO0SET = PS2DATA;
ps2.buf[0] ^= (paritybit>>1);
ps2.bits++;
}
else //send 0
{
IO0CLR = PS2DATA;
ps2.bits++;
}
}
/*
else if (ps2.bits == 9) //send stop
{
IO0SET = PS2DATA;
ps2.bits++;
}
*/
else //ack
{
IO0DIR &= ~PS2DATA; //DATA in
ps2.flag &=~ps2txdflag;
ps2.bits = 0x00;
}
}
else //接收数据
{
if ((IO0PIN & PS2DATA) == PS2DATA)
{
ps2.buf[ps2.bytes] |= (1<<ps2.bits);
if (ps2.bits < 9)
ps2.flag ^= ps2parity;
}
else
{
ps2.buf[ps2.bytes] &= ~(1<<ps2.bits);
}
ps2.bits++;
if (ps2.bits == 11)
{
ps2.bits = 0x00;
ps2.flag |= ps2rxdflag; //接收数据标志.
ps2.buf[ps2.bytes] = ((ps2.buf[ps2.bytes]>>1) & 0x00ff);
string =ps2.buf;
ps2.bytes=0x00;
// 起始位,停止位,奇偶校验位检查。
/*
if ((((ps2buf[ps2bytes] & paritybit) >> 2) == (ps2flag & ps2parity))
&& ((ps2buf[ps2bytes] & stopbit) == stopbit)
&& ((ps2buf[ps2bytes] & startbit) != startbit))
{
ps2.flag |= ps2parity;
}
*/
}
}
EXTINT |= 0x08; //清除EINT3中断标志
VICVectAddr = 0; // 通知中断控制器中断结束
}
用户1447181 2008-9-5 13:21