这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » 综合技术 » 基础知识 » 帮忙帮忙,急急!!!

共2条 1/1 1 跳转至

帮忙帮忙,急急!!!

院士
2006-09-17 18:14:16     打赏
帮忙帮忙,急急!!!



关键词: 帮忙     急急    

院士
2006-12-22 22:43:00     打赏
2楼
问 我用的周立工的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);
}

共2条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]