共2条
1/1 1 跳转至页
问
我用的周立工的7290键盘芯片,i2c方式读出键盘值
不知为何读出的键盘值总是0,请高手指点指点,到底是为何啊
时序我是严格根据它的要求啊,急死我了!!
程序如下:
//置P2.3,P2.4为 输出口P2.3为SCL,P2.4为SDA,p2.2触发中断
#include <MSP430x14x.h>
#define zlg7290 0x70 //ZLG7290的IIC地址
unsigned char KeyFlag;
unsigned char KeyValue;
unsigned char I2CACK;
void DelayMS(unsigned char uMs);
void Initiali_I2C(void);
void Start_I2C(void);
void Stop_I2C(void);
void SendByte(unsigned char c);
unsigned char RcvByte(void);
void Ack_I2C(unsigned char a);
unsigned char ISendByte(unsigned char sla,unsigned char c);
unsigned char ISendStr(unsigned char sla,unsigned char suba,unsigned char *s,unsigned char no);
unsigned char IRcvByte(unsigned char sla,unsigned char *c);
unsigned char IRcvStr(unsigned char sla,unsigned char suba,unsigned char *s,unsigned char no);
//unsigned char ZLG7290_SendData(unsigned char SubAdd,unsigned char Data);
//unsigned char ZLG7290_SendCmd(unsigned char Data1,unsigned char Data2);
unsigned char ZLG7290_GetKey(void);
//void KeyFunction(unsigned char FunctionID);
void main(void)
{
WDTCTL=WDTPW+WDTHOLD; //stop dog
BCSCTL1 &=~XT2OFF;
BCSCTL2 |= SELM1+DIVM1;
P2IES|=BIT2; //P2.2触发中断为下降沿
P2IE|=BIT2; //P2.2中断允许
KeyFlag = 0x00;
KeyValue= 0;
Initiali_I2C(); //初始化i2c
_EINT();
}
//--------------------------------------------------------
interrupt[PORT2_VECTOR]void Port2(void) //键盘中断处理
{
if(P2IFG&BIT2)
{
//键盘中断处理函数
KeyValue = ZLG7290_GetKey();
if(KeyValue!=0)
{
KeyFlag =0x01;
}
P2IFG&=~BIT2;// 清除中断标志
}
}
//-------------------------------------------------------------------
void DelayMS(unsigned char uMs)
{
unsigned char j,k;
for(k=0;k<uMs;k++)
for(j=0;j<1;j++);
}
//-----------------------------------------------------------
void Initiali_I2C(void)
{
P2OUT|=BIT3; //p2.3,p2.4置高
P2OUT|=BIT4;
P2DIR |=BIT4+BIT3; //p2.3,p2.4输出状态
_NOP();
_NOP();
P2OUT |=BIT4;
//Delay(5);
P2OUT |=BIT3;
// Delay(5);
}
//------------------------------------------------------------------------------------------------------
void Start_I2C(void)
{
P2DIR |=BIT4; /*发送起始条件的数据信号*/
_NOP();
_NOP();
P2DIR |=BIT3;
_NOP(); _NOP();
_NOP();_NOP();
_NOP();_NOP();
_NOP();_NOP();
_NOP();_NOP(); /*起始条件建立时间大于4.7us,延时*/
P2DIR &=~BIT4; /*发送起始信号*/
_NOP(); _NOP();
_NOP();_NOP();
_NOP();_NOP();
_NOP();_NOP();
_NOP();_NOP();
/* 起始条件锁定时间大于4μs*/
P2DIR &=~BIT3; /*钳住I2C总线,准备发送或接收数据 */
_NOP(); _NOP();
_NOP();_NOP();
}
//------------------------------------------------------------------------------------------------------
void Stop_I2C(void)
{
//置P2.3,P2.4为 输出口P2.3为SCL,P2.4为SDA
P2DIR &=~BIT4; /*发送结束条件的数据信号*/
_NOP();
_NOP();
P2DIR |=BIT3; /*发送I2C总线结束信号*/
_NOP();_NOP();
_NOP();_NOP();
_NOP();_NOP();
_NOP();_NOP();
_NOP();_NOP();
P2DIR |=BIT4;
_NOP(); _NOP();
_NOP();_NOP();
_NOP();_NOP();
_NOP();_NOP();
}
//------------------------------------------------------------------------------------------------------
void SendByte(unsigned char c)
{
unsigned char BitCnt;
for(BitCnt=0;BitCnt<8;BitCnt++) /*要传送的数据长度为8位*/
{
if((c<<BitCnt)&0x80) P2DIR |=BIT4; /*判断发送位*/
else P2DIR &=~BIT4;
_NOP();
_NOP();
P2DIR |=BIT3; /*置时钟线为高,通知被控器开始接收数据位*/
_NOP();_NOP();
_NOP();_NOP();
_NOP();_NOP();
_NOP();_NOP();
_NOP();_NOP();
/*保证时钟高电平周期大于4μs*/
P2DIR &=~BIT3;
}
_NOP(); _NOP();
_NOP();_NOP();
P2DIR |=BIT4; //8位发送完后释放数据线,准备接收应答?
_NOP(); _NOP();
_NOP();_NOP();
P2DIR |=BIT3;
_NOP();_NOP();
_NOP();_NOP();
_NOP();_NOP();
if(P2IN&0x10) I2CACK=0;
else I2CACK=1; /*判断是否接收到应答信号*/
P2DIR &=~BIT3;
_NOP();_NOP();
_NOP();_NOP();
}
//------------------------------------------------------------------------------------------------------
unsigned char RcvByte(void)
{
unsigned char retc;
unsigned char BitCnt;
retc=0;
P2DIR |=BIT4; //SDA P2.4置高?
for(BitCnt=0;BitCnt<8;BitCnt++)
{
_NOP(); _NOP();
P2DIR &=~BIT3; /*置时钟线为低,准备接收数据位*/
_NOP(); _NOP();
_NOP();_NOP();
_NOP();_NOP();
_NOP();_NOP();
_NOP();_NOP();
/*时钟低电平周期大于4.7μs*/
P2DIR |=BIT3; /*置时钟线为高使数据线上数据有效*/
_NOP(); _NOP();
_NOP();_NOP();
retc=retc<<1;
if(P2IN&0x10) retc=retc+1; /*读数据位,接收的数据位放入retc中 */
_NOP(); _NOP();
_NOP();_NOP();
}
P2DIR &=~BIT3;
_NOP();_NOP();
_NOP();_NOP();
return(retc);
}
//------------------------------------------------------------------------------------------------------
void Ack_I2C(unsigned char a)
{
if(a==0) P2DIR &=~BIT4;
else P2DIR |=BIT4;
_NOP(); _NOP();
_NOP();_NOP();
_NOP();_NOP();
P2DIR |=BIT3;
_NOP();_NOP();
_NOP();_NOP();
_NOP();_NOP();
_NOP();_NOP();
_NOP();_NOP();
P2DIR &=~BIT3;
_NOP();_NOP();
_NOP();_NOP();
}
//------------------------------------------------------------------------------------------------------
unsigned char ISendByte(unsigned char sla,unsigned char c)
{
Start_I2C(); /*启动总线*/
SendByte(sla); /*发送器件地址*/
if(I2CACK==0)return(0);
SendByte(c); /*发送数据*/
if(I2CACK==0)return(0);
Stop_I2C(); /*结束总线*/
return (1);
}
//------------------------------------------------------------------------------------------------------
unsigned char ISendStr(unsigned char sla,unsigned char suba,unsigned char *s,unsigned char no)//sla=112,suba=1
{
unsigned char i;
Start_I2C(); /*启动总线*/
SendByte(sla); /*发送器件地址*/
if(I2CACK==0) return(0);
SendByte(suba); /*发送器件子地址*/
if(I2CACK==0) return(0);
for(i=0;i<no;i++)
{
SendByte(*s); /*发送数据*/
if(I2CACK==0) return(0);
s++;
}
Stop_I2C(); /*结束总线*/
return (1);
}
//---------------------------------------------------------------------------------------
unsigned char IRcvByte(unsigned char sla,unsigned char *c)
{
Start_I2C(); /*启动总线*/
SendByte(sla+1); /*发送器件地址*/
if(I2CACK==0)return(0);
*c=RcvByte(); /*读取数据*/
Ack_I2C(1); /*发送非就答位*/
Stop_I2C(); /*结束总线*/
return (1);
}
//---------------------------------------------------------------------------------------
unsigned char IRcvStr(unsigned char sla,unsigned char suba,unsigned char *s,unsigned char no)
{
unsigned char i;
Start_I2C(); /*启动总线*/
SendByte(sla); /*发送器件地址*/
if(I2CACK==0)return(0);
SendByte(suba); /*发送器件子地址*/
if(I2CACK==0)return(0);
Start_I2C();
SendByte(sla+1);
if(I2CACK==0)return(0);
for(i=0;i<no-1;i++)
{
*s=RcvByte(); /*发送数据*/
Ack_I2C(0); /*发送就答位*/
s++;
}
*s=RcvByte();
Ack_I2C(1); /*发送非应位*/
Stop_I2C(); /*结束总线*/
return (1);
}
//-----------------------------------------------------------------------------------------------------
/*unsigned char ZLG7290_SendData(unsigned char SubAdd,unsigned char Data)
{
if(SubAdd>0x17)
return 0;
ISendStr(zlg7290,SubAdd,&Data,1);
DelayMS(10);
return (1);
}*/
//------------------------------------------------------------------------------------------------------
/*unsigned char ZLG7290_SendCmd(unsigned char Data1,unsigned char Data2)
{
unsigned char Data[2];
Data[0]=Data1;
Data[1]=Data2;
ISendStr(zlg7290,0x07,Data,2);
DelayMS(10);
return (1);
} */
//---------------------------------------------------------------
unsigned char ZLG7290_GetKey(void)
{
unsigned char rece;
rece=0;
IRcvStr(zlg7290,1,&rece,1);
DelayMS(10);
return (rece);
}
不知为何读出的键盘值总是0,请高手指点指点,到底是为何啊
时序我是严格根据它的要求啊,急死我了!!
程序如下:
//置P2.3,P2.4为 输出口P2.3为SCL,P2.4为SDA,p2.2触发中断
#include <MSP430x14x.h>
#define zlg7290 0x70 //ZLG7290的IIC地址
unsigned char KeyFlag;
unsigned char KeyValue;
unsigned char I2CACK;
void DelayMS(unsigned char uMs);
void Initiali_I2C(void);
void Start_I2C(void);
void Stop_I2C(void);
void SendByte(unsigned char c);
unsigned char RcvByte(void);
void Ack_I2C(unsigned char a);
unsigned char ISendByte(unsigned char sla,unsigned char c);
unsigned char ISendStr(unsigned char sla,unsigned char suba,unsigned char *s,unsigned char no);
unsigned char IRcvByte(unsigned char sla,unsigned char *c);
unsigned char IRcvStr(unsigned char sla,unsigned char suba,unsigned char *s,unsigned char no);
//unsigned char ZLG7290_SendData(unsigned char SubAdd,unsigned char Data);
//unsigned char ZLG7290_SendCmd(unsigned char Data1,unsigned char Data2);
unsigned char ZLG7290_GetKey(void);
//void KeyFunction(unsigned char FunctionID);
void main(void)
{
WDTCTL=WDTPW+WDTHOLD; //stop dog
BCSCTL1 &=~XT2OFF;
BCSCTL2 |= SELM1+DIVM1;
P2IES|=BIT2; //P2.2触发中断为下降沿
P2IE|=BIT2; //P2.2中断允许
KeyFlag = 0x00;
KeyValue= 0;
Initiali_I2C(); //初始化i2c
_EINT();
}
//--------------------------------------------------------
interrupt[PORT2_VECTOR]void Port2(void) //键盘中断处理
{
if(P2IFG&BIT2)
{
//键盘中断处理函数
KeyValue = ZLG7290_GetKey();
if(KeyValue!=0)
{
KeyFlag =0x01;
}
P2IFG&=~BIT2;// 清除中断标志
}
}
//-------------------------------------------------------------------
void DelayMS(unsigned char uMs)
{
unsigned char j,k;
for(k=0;k<uMs;k++)
for(j=0;j<1;j++);
}
//-----------------------------------------------------------
void Initiali_I2C(void)
{
P2OUT|=BIT3; //p2.3,p2.4置高
P2OUT|=BIT4;
P2DIR |=BIT4+BIT3; //p2.3,p2.4输出状态
_NOP();
_NOP();
P2OUT |=BIT4;
//Delay(5);
P2OUT |=BIT3;
// Delay(5);
}
//------------------------------------------------------------------------------------------------------
void Start_I2C(void)
{
P2DIR |=BIT4; /*发送起始条件的数据信号*/
_NOP();
_NOP();
P2DIR |=BIT3;
_NOP(); _NOP();
_NOP();_NOP();
_NOP();_NOP();
_NOP();_NOP();
_NOP();_NOP(); /*起始条件建立时间大于4.7us,延时*/
P2DIR &=~BIT4; /*发送起始信号*/
_NOP(); _NOP();
_NOP();_NOP();
_NOP();_NOP();
_NOP();_NOP();
_NOP();_NOP();
/* 起始条件锁定时间大于4μs*/
P2DIR &=~BIT3; /*钳住I2C总线,准备发送或接收数据 */
_NOP(); _NOP();
_NOP();_NOP();
}
//------------------------------------------------------------------------------------------------------
void Stop_I2C(void)
{
//置P2.3,P2.4为 输出口P2.3为SCL,P2.4为SDA
P2DIR &=~BIT4; /*发送结束条件的数据信号*/
_NOP();
_NOP();
P2DIR |=BIT3; /*发送I2C总线结束信号*/
_NOP();_NOP();
_NOP();_NOP();
_NOP();_NOP();
_NOP();_NOP();
_NOP();_NOP();
P2DIR |=BIT4;
_NOP(); _NOP();
_NOP();_NOP();
_NOP();_NOP();
_NOP();_NOP();
}
//------------------------------------------------------------------------------------------------------
void SendByte(unsigned char c)
{
unsigned char BitCnt;
for(BitCnt=0;BitCnt<8;BitCnt++) /*要传送的数据长度为8位*/
{
if((c<<BitCnt)&0x80) P2DIR |=BIT4; /*判断发送位*/
else P2DIR &=~BIT4;
_NOP();
_NOP();
P2DIR |=BIT3; /*置时钟线为高,通知被控器开始接收数据位*/
_NOP();_NOP();
_NOP();_NOP();
_NOP();_NOP();
_NOP();_NOP();
_NOP();_NOP();
/*保证时钟高电平周期大于4μs*/
P2DIR &=~BIT3;
}
_NOP(); _NOP();
_NOP();_NOP();
P2DIR |=BIT4; //8位发送完后释放数据线,准备接收应答?
_NOP(); _NOP();
_NOP();_NOP();
P2DIR |=BIT3;
_NOP();_NOP();
_NOP();_NOP();
_NOP();_NOP();
if(P2IN&0x10) I2CACK=0;
else I2CACK=1; /*判断是否接收到应答信号*/
P2DIR &=~BIT3;
_NOP();_NOP();
_NOP();_NOP();
}
//------------------------------------------------------------------------------------------------------
unsigned char RcvByte(void)
{
unsigned char retc;
unsigned char BitCnt;
retc=0;
P2DIR |=BIT4; //SDA P2.4置高?
for(BitCnt=0;BitCnt<8;BitCnt++)
{
_NOP(); _NOP();
P2DIR &=~BIT3; /*置时钟线为低,准备接收数据位*/
_NOP(); _NOP();
_NOP();_NOP();
_NOP();_NOP();
_NOP();_NOP();
_NOP();_NOP();
/*时钟低电平周期大于4.7μs*/
P2DIR |=BIT3; /*置时钟线为高使数据线上数据有效*/
_NOP(); _NOP();
_NOP();_NOP();
retc=retc<<1;
if(P2IN&0x10) retc=retc+1; /*读数据位,接收的数据位放入retc中 */
_NOP(); _NOP();
_NOP();_NOP();
}
P2DIR &=~BIT3;
_NOP();_NOP();
_NOP();_NOP();
return(retc);
}
//------------------------------------------------------------------------------------------------------
void Ack_I2C(unsigned char a)
{
if(a==0) P2DIR &=~BIT4;
else P2DIR |=BIT4;
_NOP(); _NOP();
_NOP();_NOP();
_NOP();_NOP();
P2DIR |=BIT3;
_NOP();_NOP();
_NOP();_NOP();
_NOP();_NOP();
_NOP();_NOP();
_NOP();_NOP();
P2DIR &=~BIT3;
_NOP();_NOP();
_NOP();_NOP();
}
//------------------------------------------------------------------------------------------------------
unsigned char ISendByte(unsigned char sla,unsigned char c)
{
Start_I2C(); /*启动总线*/
SendByte(sla); /*发送器件地址*/
if(I2CACK==0)return(0);
SendByte(c); /*发送数据*/
if(I2CACK==0)return(0);
Stop_I2C(); /*结束总线*/
return (1);
}
//------------------------------------------------------------------------------------------------------
unsigned char ISendStr(unsigned char sla,unsigned char suba,unsigned char *s,unsigned char no)//sla=112,suba=1
{
unsigned char i;
Start_I2C(); /*启动总线*/
SendByte(sla); /*发送器件地址*/
if(I2CACK==0) return(0);
SendByte(suba); /*发送器件子地址*/
if(I2CACK==0) return(0);
for(i=0;i<no;i++)
{
SendByte(*s); /*发送数据*/
if(I2CACK==0) return(0);
s++;
}
Stop_I2C(); /*结束总线*/
return (1);
}
//---------------------------------------------------------------------------------------
unsigned char IRcvByte(unsigned char sla,unsigned char *c)
{
Start_I2C(); /*启动总线*/
SendByte(sla+1); /*发送器件地址*/
if(I2CACK==0)return(0);
*c=RcvByte(); /*读取数据*/
Ack_I2C(1); /*发送非就答位*/
Stop_I2C(); /*结束总线*/
return (1);
}
//---------------------------------------------------------------------------------------
unsigned char IRcvStr(unsigned char sla,unsigned char suba,unsigned char *s,unsigned char no)
{
unsigned char i;
Start_I2C(); /*启动总线*/
SendByte(sla); /*发送器件地址*/
if(I2CACK==0)return(0);
SendByte(suba); /*发送器件子地址*/
if(I2CACK==0)return(0);
Start_I2C();
SendByte(sla+1);
if(I2CACK==0)return(0);
for(i=0;i<no-1;i++)
{
*s=RcvByte(); /*发送数据*/
Ack_I2C(0); /*发送就答位*/
s++;
}
*s=RcvByte();
Ack_I2C(1); /*发送非应位*/
Stop_I2C(); /*结束总线*/
return (1);
}
//-----------------------------------------------------------------------------------------------------
/*unsigned char ZLG7290_SendData(unsigned char SubAdd,unsigned char Data)
{
if(SubAdd>0x17)
return 0;
ISendStr(zlg7290,SubAdd,&Data,1);
DelayMS(10);
return (1);
}*/
//------------------------------------------------------------------------------------------------------
/*unsigned char ZLG7290_SendCmd(unsigned char Data1,unsigned char Data2)
{
unsigned char Data[2];
Data[0]=Data1;
Data[1]=Data2;
ISendStr(zlg7290,0x07,Data,2);
DelayMS(10);
return (1);
} */
//---------------------------------------------------------------
unsigned char ZLG7290_GetKey(void)
{
unsigned char rece;
rece=0;
IRcvStr(zlg7290,1,&rece,1);
DelayMS(10);
return (rece);
}
共2条
1/1 1 跳转至页
回复
有奖活动 | |
---|---|
【有奖活动】分享技术经验,兑换京东卡 | |
话不多说,快进群! | |
请大声喊出:我要开发板! | |
【有奖活动】EEPW网站征稿正在进行时,欢迎踊跃投稿啦 | |
奖!发布技术笔记,技术评测贴换取您心仪的礼品 | |
打赏了!打赏了!打赏了! |
打赏帖 | |
---|---|
与电子爱好者谈读图四被打赏50分 | |
与电子爱好者谈读图二被打赏50分 | |
【FRDM-MCXN947评测】Core1适配运行FreeRtos被打赏50分 | |
【FRDM-MCXN947评测】双核调试被打赏50分 | |
【CPKCORRA8D1B评测】---移植CoreMark被打赏50分 | |
【CPKCORRA8D1B评测】---打开硬件定时器被打赏50分 | |
【FRDM-MCXA156评测】4、CAN loopback模式测试被打赏50分 | |
【CPKcorRA8D1评测】--搭建初始环境被打赏50分 | |
【FRDM-MCXA156评测】3、使用FlexIO模拟UART被打赏50分 | |
【FRDM-MCXA156评测】2、rt-thread MCXA156 BSP制作被打赏50分 |