You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
160 lines
2.8 KiB
160 lines
2.8 KiB
#include "myiic.h" |
|
#include "delay.h" |
|
////////////////////////////////////////////////////////////////////////////////// |
|
//本程序只供学习使用,未经作者许可,不得用于其它任何用途 |
|
//ALIENTEK精英STM32开发板 |
|
//IIC驱动 代码 |
|
//正点原子@ALIENTEK |
|
//技术论坛:www.openedv.com |
|
//修改日期:2012/9/9 |
|
//版本:V1.0 |
|
//版权所有,盗版必究。 |
|
//Copyright(C) 广州市星翼电子科技有限公司 2009-2019 |
|
//All rights reserved |
|
////////////////////////////////////////////////////////////////////////////////// |
|
|
|
//初始化IIC |
|
void MYIIC_Init(void) |
|
{ |
|
GPIO_InitTypeDef GPIO_InitStructure; |
|
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOC, ENABLE ); //使能GPIOB时钟 |
|
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14|GPIO_Pin_13; |
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP ; //推挽输出 |
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; |
|
GPIO_Init(GPIOC, &GPIO_InitStructure); |
|
GPIO_SetBits(GPIOC,GPIO_Pin_14|GPIO_Pin_13); |
|
} |
|
|
|
//产生IIC起始信号 |
|
void MYIIC_Start(void) |
|
{ |
|
SDA_OUT(); //sda线输出 |
|
IIC_SDA=1; |
|
IIC_SCL=1; |
|
delay_us(4); |
|
IIC_SDA=0;//START:when CLK is high,DATA change form high to low |
|
delay_us(4); |
|
IIC_SCL=0;//钳住I2C总线,准备发送或接收数据 |
|
} |
|
//产生IIC停止信号 |
|
void MYIIC_Stop(void) |
|
{ |
|
SDA_OUT();//sda线输出 |
|
IIC_SCL=0; |
|
IIC_SDA=0;//STOP:when CLK is high DATA change form low to high |
|
delay_us(4); |
|
IIC_SCL=1; |
|
IIC_SDA=1;//发送I2C总线结束信号 |
|
delay_us(4); |
|
} |
|
//等待应答信号到来 |
|
//返回值:1,接收应答失败 |
|
// 0,接收应答成功 |
|
u8 MYIIC_Wait_Ack(void) |
|
{ |
|
u8 ucErrTime=0; |
|
SDA_IN(); //SDA设置为输入 |
|
IIC_SDA=1;delay_us(1); |
|
IIC_SCL=1;delay_us(1); |
|
while(READ_SDA) |
|
{ |
|
ucErrTime++; |
|
if(ucErrTime>250) |
|
{ |
|
MYIIC_Stop(); |
|
return 1; |
|
} |
|
} |
|
IIC_SCL=0;//时钟输出0 |
|
return 0; |
|
} |
|
//产生ACK应答 |
|
void IIC_Ack(void) |
|
{ |
|
IIC_SCL=0; |
|
SDA_OUT(); |
|
IIC_SDA=0; |
|
delay_us(2); |
|
IIC_SCL=1; |
|
delay_us(2); |
|
IIC_SCL=0; |
|
} |
|
//不产生ACK应答 |
|
void IIC_NAck(void) |
|
{ |
|
IIC_SCL=0; |
|
SDA_OUT(); |
|
IIC_SDA=1; |
|
delay_us(2); |
|
IIC_SCL=1; |
|
delay_us(2); |
|
IIC_SCL=0; |
|
} |
|
//IIC发送一个字节 |
|
//返回从机有无应答 |
|
//1,有应答 |
|
//0,无应答 |
|
void IIC_Send_Byte(u8 txd) |
|
{ |
|
u8 t; |
|
SDA_OUT(); |
|
IIC_SCL=0;//拉低时钟开始数据传输 |
|
for(t=0;t<8;t++) |
|
{ |
|
IIC_SDA=(txd&0x80)>>7; |
|
txd<<=1; |
|
delay_us(2); //对TEA5767这三个延时都是必须的 |
|
IIC_SCL=1; |
|
delay_us(2); |
|
IIC_SCL=0; |
|
delay_us(2); |
|
} |
|
} |
|
//读1个字节,ack=1时,发送ACK,ack=0,发送nACK |
|
u8 IIC_Read_Byte(unsigned char ack) |
|
{ |
|
unsigned char i,receive=0; |
|
SDA_IN();//SDA设置为输入 |
|
for(i=0;i<8;i++ ) |
|
{ |
|
IIC_SCL=0; |
|
delay_us(2); |
|
IIC_SCL=1; |
|
receive<<=1; |
|
if(READ_SDA)receive++; |
|
delay_us(1); |
|
} |
|
if (!ack) |
|
IIC_NAck();//发送nACK |
|
else |
|
IIC_Ack(); //发送ACK |
|
return receive; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|