|
我的源程序如下
#include "msp430x22x4.h"
unsigned int RxByteCtr;
/*********************************************************
* 数据定义
*********************************************************/
unsigned char read_Buf, //读缓冲区
write_Buf, //写缓冲区
ctrl_Buf, //用于存放控制指令等的中间缓冲区
addr_Buf1, //高地址缓冲
addr_Buf2,
ack_Flag; //应答标识
/************************************************
* 延迟
************************************************/
void iic_Delay(void)
{
_NOP();
_NOP();
_NOP();
}
/*********************************************************
* 启动IIC
*********************************************************/
void start_IIC(void)
{
//P3DIR &= 11111001;//P4OUT &= 0x3f; //设置P4OUT
P3DIR |= BIT1+BIT2;//P4DIR &= 0x7f; //SDA = 1
P3OUT |=BIT1;
iic_Delay();
iic_Delay();
//P3DIR |= BIT2;
P3OUT |=BIT2;//P4DIR &= 0xbf; //SCL = 1
iic_Delay();iic_Delay();
P3OUT &= ~BIT1;//P4DIR |= 0x80; //SDA = 0
iic_Delay();iic_Delay();
P3OUT &= ~BIT2;//P4DIR |= 0x40; //SCL = 0
iic_Delay();iic_Delay();
}
/*******************************************************
* 停止IIC
*******************************************************/
void stop_IIC(void)
{
P3DIR |= BIT1+BIT2;
P3OUT &=~BIT1; //P4DIR |= 0x80; //SDA = 0
iic_Delay();iic_Delay();
P3OUT |=BIT2;//P4DIR &= 0xbf; //SCL = 1
iic_Delay();iic_Delay();
P3OUT |=BIT1;//P4DIR &= 0x7f; //SDA = 1
iic_Delay();iic_Delay();
P3OUT &=~BIT1;//P4DIR |= 0x80; //SDA = 0
iic_Delay();iic_Delay();
P3OUT &= ~BIT2;//P4DIR |= 0x40; //SCL = 0
}
/*******************************************************
* 发送 0
*******************************************************/
void send_Zero(void)
{
P3DIR |= BIT1+BIT2;
P3OUT&= ~BIT1;//P4DIR |= 0x80; //SDA = 0
iic_Delay();iic_Delay();
P3OUT|= BIT2;//P4DIR &= 0xbf; //SCL = 1
iic_Delay();iic_Delay();
P3OUT &=~BIT2;//P4DIR |= 0x40; //SCL = 0
iic_Delay();iic_Delay();
}
/*******************************************************
* 发送 1
*******************************************************/
void send_One(void)
{
P3DIR |= BIT1+BIT2;
P3OUT|= BIT1;//P4DIR &= 0x7f; //SDA = 1
iic_Delay();
iic_Delay();
P3OUT|= BIT2;//P4DIR &= 0xbf; //SCL = 1
iic_Delay();
iic_Delay();
P3OUT&= ~BIT2;//P4DIR |= 0x40; //SCL = 0
iic_Delay();
iic_Delay();
P3OUT&= ~BIT1;//P4DIR |= 0x80; //SDA = 0
iic_Delay();iic_Delay();
}
/*******************************************************
* 发送一个字节数据
*******************************************************/
void send_Char(void)
{
unsigned char cnt,tmp=0x80;
for(cnt=0;cnt<8;cnt++)
{
if((ctrl_Buf & tmp )> 0)
{
send_One(); // 发送1
}else
{
send_Zero(); // 发送0
}
tmp /= 2; // tmp右移一位
}
}
/*******************************************************
* 读一个byte数据
*******************************************************/
void read_Char(void)
{
unsigned char cnt,tmp=0x80;
read_Buf = 0x00;
for(cnt=0;cnt<8;cnt++)
{
P3DIR |= BIT1+BIT2;
P3OUT |=BIT1;//P4DIR &= 0x7f; //SDA = 1
iic_Delay();
P3OUT|=BIT2;//P4DIR &= 0xbf; //SCL = 1
iic_Delay();
P3DIR &= ~BIT1;
if((P3IN & BIT1) == BIT1)//if((P4IN & 0x80) > 0x00)
{ // 收到 1
read_Buf |= tmp;
}
P3OUT&=~BIT2;//P4DIR |= 0x40; //SCL = 0
iic_Delay();
tmp = tmp/2;
}
}
/*************************************************
* 应答信号
*************************************************/
void iic_ACK(void)
{
ack_Flag = 0x00;
P3DIR |= BIT1+BIT2;
P3OUT|=BIT1;//P4DIR &= 0x7f; //SDA = 1
iic_Delay();
iic_Delay();
P3OUT|= BIT2;//P4DIR &= 0xbf; //SCL = 1
iic_Delay();iic_Delay();
P3DIR &= ~BIT1;
if ((P3IN & BIT1) == BIT1 )//if ((P4IN & 0x80) == 0x80 )
{
ack_Flag = 0x01;
}
P3OUT&=~BIT2;//P4DIR |= 0x40; //SCL = 0
iic_Delay();iic_Delay();
}
/***********************************************
* iic_NACK
************************************************/
void iic_NACK(void)
{
P3DIR |= BIT1+BIT2;
P3OUT |=BIT1;//P4DIR &= 0x7f; //SDA = 1
iic_Delay();
P3OUT |= BIT2;//P4DIR &= 0xbf; //SCL = 1
iic_Delay();
P3OUT &=~BIT2;//P4DIR |= 0x40; //SCL = 0
iic_Delay();
P3OUT &=~BIT1;//P4DIR |= 0x80; //SDA = 0
iic_Delay();
}
/*************************************************
* iic_SENDACK
*************************************************/
void iic_SENDACK(void) //send the answer bit
{
P3DIR |= 10;//SDA=0;
iic_Delay();
P3DIR &= 11111011;//SCL=1;
iic_Delay();//NOP();
P3DIR |= 100;//SCL=0;
iic_Delay();
P3DIR &=11111101;//SDA=1;
iic_Delay();
}
/***************************************************
* iic_Write
***************************************************/
void iic_Write()
{
do
{iic_Delay();iic_Delay();iic_Delay();iic_Delay();
iic_Delay();iic_Delay();iic_Delay();iic_Delay();
iic_Delay();iic_Delay();
start_IIC();//I2cStartBit();
ctrl_Buf=0X70;//addr_Buf1;
send_Char();//I2cWriteByte(slaw);
iic_ACK();//I2cTestAck();
}
while(ack_Flag==1);//while(F0==1);
ctrl_Buf=0X06;//write_Buf;
send_Char();//I2cWriteByte(Buffer[Index]);
iic_ACK();//I2cTestAck();
stop_IIC();
}
/***************************************************
* iic_Read
***************************************************/
void iic_Read()
{
//unsigned char OneByte;
//unsigned char i;
do
{
start_IIC();//I2cStartBit();
ctrl_Buf=0X4f;//addr_Buf2;
send_Char();//I2cWriteByte(slaw);
iic_ACK();//I2cTestAck();
}
while(ack_Flag==1);//while(F0==1);
read_Char();
iic_NACK();
stop_IIC();
}
void main()
{
WDTCTL=WDTPW+WDTHOLD; //关看门狗
BCSCTL1 =CALBC1_1MHZ; //设定DCO 为1MHZ
DCOCTL =CALBC1_1MHZ;
//start_IIC();//I2cStartBit();
//ctrl_Buf=0X00;//addr_Buf2;
//send_Char();//I2cWriteByte(slaw);
//iic_ACK();//I2cTestAck();
while(1)
{
iic_Write();
}
}
|
|