这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » 综合技术 » 基础知识 » 149,8583 【原创】求149对8583的读写C语言程序

共2条 1/1 1 跳转至

149,8583 【原创】求149对8583的读写C语言程序

院士
2006-09-17 18:14:16     打赏
149,8583 【原创】求149对8583的读写C语言程序



关键词: 原创     读写     语言程序    

院士
2006-12-22 22:43:00     打赏
2楼
问 我自己写得程序总是不对,SDA和SCA管角总被拉底,救大家帮帮忙,我的邮箱是caosongyz126.com 1: 怎么没有人回答呢,我真的是好急呀 2: #include <MSP430x14x.h>
#define SDA_1 P3OUT |= BIT3 /*SDA = 1*/
#define SDA_0 P3OUT &=~ BIT3 /*SDA = 0*/
#define SCL_1 P3OUT |= BIT2 /*SCL = 1*/
#define SCL_0 P3OUT &=~ BIT2 /*SCL = 0*/
#define DIR_IN P3DIR &=~ BIT3; SDA_1 /*I/O口为输入*/
#define DIR_OUT P3DIR |= BIT3 /*I/0口为输出*/
#define SDA_IN ((P3IN >> 3) & 0x01) /*Read SDA*/

unsigned int a[50];

static void Delay(unsigned int n)
{
unsigned int i;
for (i=0; i<5; i++)
{;;}
}

void Init(void)
{
SCL_1;
Delay(5);
SDA_1;
Delay(5);
}

void Start(void)
{
SDA_1;
Delay(5);
SCL_1;
Delay(5);
SDA_0;
Delay(5);
SCL_0;
Delay(5);
}

void Stop(void)
{
SDA_0;
Delay(5);
SCL_1;
Delay(5);
SDA_1;
Delay(5);
}

void WriteByte(unsigned char WriteData)
{
unsigned char i;
for (i=0; i<8; i++)
{
SCL_0;
Delay(5);
if (((WriteData >> 7) & 0x01) == 0x01)
{
SDA_1;
}
else
{
SDA_0;
}
Delay(5);
SCL_1;
WriteData = WriteData << 1;
Delay(5);
}
SCL_0;
SDA_1;
Delay(5);
Delay(5);
}

unsigned char ReadByte(void)
{
unsigned char i;
unsigned char TempBit = 0;
unsigned char TempData = 0;
SCL_0;
Delay(5);
SDA_1;
for (i=0; i<8; i++)
{
Delay(5);
SCL_1;
Delay(5);
DIR_IN;
if (SDA_IN == 0x01 /*sda==1*/)
{
TempBit = 1;
}
else
{
TempBit = 0;
}
DIR_OUT;
TempData = (TempData << 1) | TempBit;
SCL_0;
}
Delay(5);
return(TempData);
}

void ReceiveAck(void)
{
unsigned char i = 0;
SCL_1;
Delay(5);
DIR_IN;
while ((SDA_IN == 0x01 /*sda==1*/) && (i < 255)/*调试方便,可以不要*/)
{
i++;
}
DIR_OUT;
SCL_0;
Delay(5);
}

void Acknowledge(void)
{
SCL_0;
Delay(5);
DIR_OUT;
SDA_0;
SCL_1;
Delay(5);
SCL_0;
}

unsigned int ReadWord(unsigned char unit/*address*/)
{
unsigned char HighData = 0;
unsigned char LowData = 0;
unsigned int TempData = 0;
Start();
WriteByte(0xa0);
ReceiveAck();
WriteByte(unit);
ReceiveAck();
Start();
WriteByte(0xa1);
ReceiveAck();
LowData = ReadByte();
Acknowledge();
HighData = ReadByte();
Stop();
TempData = (HighData << 8) + LowData;
Delay(1000);
return(TempData);
}

void ReadWords(unsigned char unit/*address*/)
{
unsigned char i;
unsigned char HighData = 0;
unsigned char LowData = 0;
unsigned int TempData = 0;
Start();
WriteByte(0xa0);
ReceiveAck();
WriteByte(unit);
ReceiveAck();
Start();
WriteByte(0xa1);
ReceiveAck();
for (i=0; i<49; i++)
{
LowData = ReadByte();
Acknowledge();
HighData = ReadByte();
Acknowledge();
a[i]= (HighData << 8) + LowData;
}
LowData = ReadByte();
Acknowledge();
HighData = ReadByte();
Stop();
a[49] = (HighData << 8) + LowData;
Delay(1000);
}

void WriteWord(unsigned char unit/*address*/, unsigned int WriteData)
{
unsigned char LowData = 0;
unsigned char HighData = 0;
LowData = (unsigned char)WriteData;
HighData = (unsigned char)(WriteData >> 8);
Start();
WriteByte(0xa0);
ReceiveAck();
WriteByte(unit);
ReceiveAck();
WriteByte(LowData);
ReceiveAck();
WriteByte(HighData);
ReceiveAck();
Stop();
Delay(2000);
}

void main(void)
{
unsigned char i,j,miao,fen,shi;
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
P1DIR |= 0x01; // Set P1.2 to output direction

do{
DIR_OUT;//设SDA为输出
miao=ReadWord(0x02);
fen=ReadWord(0x03);
shi=ReadWord(0x04);
P1OUT ^= 0x01; // Toggle P1.0 using exclusive-OR

}while(1);
}
这是我的程序请高手指点

共2条 1/1 1 跳转至

回复

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