LPC1788的IIC使用

时间:2023-12-06 12:50:26

#ifndef __IIC0_H_

#define __IIC0_H_

#include "common.h"

#include "delay.h"

//IIC通讯过程中的指定状态

#define STATUS_SENDSTART        0X08    //已经发送起始条件

#define STATUS_REPEATSTART      0X10    //已经发送重复的起始条件

#define STATUS_SENDSLAVE_ACK    0X18    //已发送从机写地址,接收到ACK

#define STATUS_SENDSLAVE_NACK   0X20    //已发送从机写地址,未接收到ack

#define STATUS_SENDDATA_ACK     0X28    //已发送从机写数据,并接收到ack

#define STATUS_SENDDATA_NACK    0X30    //已发送从机写数据没收到ack

#define STATUS_LOSS             0X38    //丢失总线仲裁

#define STATUS_READADDR_NACK    0X48    //已经发送从机读地址未收到ack

#define STATUS_READADDR_ACK     0X40    //已发送从机读地址并接受到ack

#define STATUS_RECV_ACK         0X50    //已接受数据字节ack已返回

#define STATUS_RECV_NACK         0X58   //已接受数据字节nack已返回

void iic0_init(u16 div);//参数为时钟分频数 不得超过65535 + 65535

u8 iic0_start(void);//发送起始信号

u8 iic0_send_write_addr(u8 addr);//发送设备写地址

u8 iic0_send_data(u8 data);//发送数据

u8 iic0_send_stop(void);//发送停止

u8 iic0_send_read_addr(u8 addr);//发送读地址

u8 iic0_read_data(u8* value,u8 ack);//读取数据

u8 iic0_repeat_start(void);//重新发送start

#endif

#include "iic0.h"

#define I2EN  6     //接口使能

#define STA   5     //起始标志

#define STO   4     //停止标志

#define SI    3     //中断标志

#define AA    2     //应答标志

#define I2ENC 6     //清除iic使能

#define STAC  5     //清除起始

#define SIC   3     //清除中断

#define AAC   2     //清除应答

#define STATE (LPC_I2C0->STAT & 0xf8)   //IIC总线的实际状态信息

void iic0_init(u16 div)

{

//打开时钟,同时打开iic和gpio时钟

LPC_SC->PCONP |= (1<<15)|(1<<7);//打开时钟

//配置引脚功能

LPC_IOCON->P0_27 = 0x00;

LPC_IOCON->P0_27 |= (1<<0)|(1<<8);//打开iic功能,禁止滤波器 与引脚配置相关 根据实际引脚进行修改

LPC_IOCON->P0_28 = 0x00;//选择gpio功能,禁止迟滞 不反向 正常推挽

LPC_IOCON->P0_28 |= (1<<0)|(1<<8);//打开iic功能,禁止滤波器 与引脚配置相关 根据实际引脚进行修改

LPC_I2C0->CONCLR |= (1<<I2ENC);//首先禁止IIC接口

LPC_I2C0->CONCLR |= (1<<AAC)|(1<<SIC)|(1<<STAC);//清除应答,中断,起始标志

LPC_I2C0->SCLH = div/2;//设置IIC时钟

LPC_I2C0->SCLL = div/2;

LPC_I2C0->CONSET |= (1<<I2EN);//使能iic

}

//等待指定的状态

//失败返回1 成功返回0

static u8 WaitResponse(u8 response)

{

u8 retry = 200;

while(--retry)

{

DelayUs(1);

if(STATE == response)break;

}

if(retry)return 0;

else

{

iic0_send_stop();

return 1;

}

}

u8 iic0_start(void)//发送起始信号

{

LPC_I2C0->CONCLR = (1<<SIC)|(1<<STAC)|(1<<AAC);

//Start

LPC_I2C0->CONSET = (1<<STA);//发送start,等待iic 状态寄存器达到指定状态

return WaitResponse(STATUS_SENDSTART);//等待起始条件发送成功

}

u8 iic0_send_write_addr(u8 addr)    //发送设备写地址

{

//dev_addr

LPC_I2C0->DAT = addr;

LPC_I2C0->CONSET = (1<<AA);//自动ack 好像这一位无关紧要现在

LPC_I2C0->CONCLR = (1<<SIC)|(1<<STAC);//停止中断与start

return WaitResponse(STATUS_SENDSLAVE_ACK);//等待设备ack

}

u8 iic0_send_data(u8 data)  //发送数据

{

LPC_I2C0->DAT = data;

LPC_I2C0->CONSET = (1<<AA);//自动ack

LPC_I2C0->CONCLR = (1<<SIC)|(1<<STAC);;//清除中断和start

return WaitResponse(STATUS_SENDDATA_ACK);

}

u8 iic0_send_stop(void) //发送停止

{

LPC_I2C0->CONCLR = (1<<SIC)|(1<<STAC)|(1<<AAC);

LPC_I2C0->CONSET = (1<<STO);

LPC_I2C0->CONCLR = (1<<SIC);

return 0;

}

u8 iic0_send_read_addr(u8 addr) //发送读地址

{

LPC_I2C0->DAT = addr;

LPC_I2C0->CONSET = (1<<AA);

LPC_I2C0->CONCLR = (1<<SIC)|(1<<STAC);

return WaitResponse(STATUS_READADDR_ACK);

}

u8 iic0_read_data(u8* value,u8 ack) //读取数据 1发送ack 0不发送

{

if(ack)

{

LPC_I2C0->CONSET = (1<<AA);

LPC_I2C0->CONCLR = (1<<SIC);

if( WaitResponse(STATUS_RECV_ACK))return 1;

}

else

{

LPC_I2C0->CONCLR = (1<<AAC)|(1<<SIC);

if(WaitResponse(STATUS_RECV_NACK))return 1;

}

*value = LPC_I2C0->DAT;

return 0;

}

u8 iic0_repeat_start(void)  //重新发送start

{

LPC_I2C0->CONCLR = (1<<SIC);//清除中断再次发送start

LPC_I2C0->CONSET = (1<<STA);

return WaitResponse(STATUS_REPEATSTART);//等待重复start成功

}