#include "MSP430F5438A.H"
#define uchar unsigned char
#define uint unsigned int
#define at2464_PIN P9OUT
#define at2464_RED P9IN
#define at2464_DIR P9DIR
#define at2464_SCL_H at2464_PIN|=BIT1
#define at2464_SCL_L at2464_PIN&=~BIT1
#define at2464_SCL BIT1
#define at2464_SDA_H at2464_PIN|=BIT0
#define at2464_SDA_L at2464_PIN&=~BIT0
#define at2464_SDA BIT0
#define at2464_SDA_IN at2464_DIR&=~at2464_SDA
#define at2464_SDA_OUT at2464_DIR|=at2464_SDA
uchar ack()
{
uchar error;
at2464_SDA_IN;
at2464_SCL_H;
if(at2464_RED&at2464_SDA) error=1;
else error=0;
at2464_SCL_L;
at2464_SDA_OUT;
return error;
}
void sent_byte(uchar dat)
{
uchar i;
at2464_SDA_OUT;
for(i=0;i<8;i++)
{
at2464_SCL_L;
if(dat&0x80) at2464_SDA_H;
else at2464_SDA_L;
at2464_SCL_H;_NOP();
dat<<=1;
}
at2464_SCL_L;
}
void write_byte(uchar byte_addr_h,uchar byte_addr_l,uchar dat)
{
uchar error=1;
at2464_SDA_OUT;
while(error)
{
at2464_SCL_H;
at2464_SDA_H;
at2464_SDA_L;
at2464_SCL_L;//start bit
sent_byte(0xA0);//device address write
error=ack();
sent_byte(byte_addr_h);//word address
error=ack();
sent_byte(byte_addr_l);
error=ack();
sent_byte(dat);//data
error=ack();
at2464_SCL_H;
at2464_SDA_L;
at2464_SDA_H;
at2464_SCL_L;//stop bit
}
}
uchar read_byte()
{
uchar buf,i,error=1;
while(error)
{
at2464_SDA_OUT;
at2464_SCL_H;
at2464_SDA_H;
at2464_SDA_L;
at2464_SCL_L;//start bit
sent_byte(0xA1);//device address read
error=ack();
at2464_SDA_IN;
for(i=0;i<8;i++)
{
buf>>=1;
at2464_SCL_H;
if(at2464_RED&at2464_SDA) buf|=0x80;
at2464_SCL_L;_NOP();
}
at2464_SDA_OUT;
at2464_SCL_H;
at2464_SCL_L;
at2464_SCL_H;
at2464_SDA_L;
at2464_SDA_H;
at2464_SCL_L;//stop bit
}
return (buf);
}
void main()
{
uchar t;
WDTCTL = WDTPW + WDTHOLD;
P10DIR=0XFF;
P10OUT=0XAA;
at2464_DIR|=at2464_SCL;
write_byte(0x00,0x0a,0x33);
t=read_byte();
P10OUT=t;
while(1);
}对着时序图看不知哪出错了,麻烦大家帮忙看看
此帖出自
小平头技术问答
一周热门 更多>