[mw_shl_code=c,true]
这是IIC的实现
#include "IIC.h"
#include "delay.h"
#include "USART.h"
#include "stdio.h"
/**************************????????********************************************
*???????: void IIC_Init(void)
*????????: ??????I2C????????????????
*******************************************************************************/
void IIC_Init(void)
{
/*
GPIO_InitTypeDef GPIO_InitStructure;
I2C_InitTypeDef I2C_InitStructure;
// ?????? I2C1 ???????±??
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE );
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1,ENABLE);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_I2C1);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource9, GPIO_AF_I2C1);
// PB8-I2C1_SCL??PB9-I2C1_SDA
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_Init(GPIOB, &GPIO_InitStructure);
// I2C ????
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C ;
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStructure.I2C_ClockSpeed = 50000;
// I2C1 ??????
I2C_Init(I2C1, &I2C_InitStructure);
// ???? I2C1
I2C_Cmd(I2C1,ENABLE);
//???í????????
I2C_AcknowledgeConfig(I2C1, ENABLE);
printf("IIC ×??????????ê??....
");
*/
// ?í??????IIC
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE );
// PB8-SCL PB9-SDA
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
//??????????GPIOB
GPIO_Init(GPIOB, &GPIO_InitStructure);
printf("IIC×??????????ê??...
");
}
/**************************????????********************************************
*???????: void IIC_Start(void)
*????????: ?ú?úIIC????????
*******************************************************************************/
void IIC_Start(void)
{
SDA_OUT(); //sda??????
IIC_SDA_H;
IIC_SCL_H;
delay_us(4);
IIC_SDA_L; //START:when CLK is high,DATA change form high to low
delay_us(4);
IIC_SCL_L; //??×?I2C×?????×?±?·????ò????????
}
/**************************????????********************************************
*???????: void IIC_Stop(void)
*????????: //?ú?úIIC????????
*******************************************************************************/
void IIC_Stop(void)
{
SDA_OUT();//sda??????
IIC_SCL_L;
IIC_SDA_L;//STOP:when CLK is high DATA change form low to high
delay_us(4);
IIC_SCL_H;
IIC_SDA_H;//·???I2C×????á??????
delay_us(4);
}
/**************************????????********************************************
*???????: u8 IIC_Wait_Ack(void)
*????????: ????????????????
//·???????1???????????§°?
// 0??????????????
*******************************************************************************/
u8 IIC_Wait_Ack(void)
{
u8 ucErrTime=0;
SDA_IN(); //SDA?è????????
IIC_SDA_H;delay_us(1);
IIC_SCL_H;delay_us(1);
while(READ_SDA)
{
ucErrTime++;
if(ucErrTime>50)
{
IIC_Stop();
return 1;
}
delay_us(1);
}
IIC_SCL_L;//?±??????0
return 0;
}
/**************************????????********************************************
*???????: void IIC_Ack(void)
*????????: ?ú?úACK????
*******************************************************************************/
void IIC_Ack(void)
{
IIC_SCL_L;
SDA_OUT();
IIC_SDA_L;
delay_us(2);
IIC_SCL_H;
delay_us(2);
IIC_SCL_L;
}
/**************************????????********************************************
*???????: void IIC_NAck(void)
*????????: ?ú?úNACK????
*******************************************************************************/
void IIC_NAck(void)
{
IIC_SCL_L;
SDA_OUT();
IIC_SDA_H;
delay_us(2);
IIC_SCL_L;
delay_us(2);
IIC_SCL_L;
}
/**************************????????********************************************
*???????: void IIC_Send_Byte(u8 txd)
*????????: IIC·???????×???
*******************************************************************************/
void IIC_Send_Byte(u8 txd)
{
u8 t;
SDA_OUT();
IIC_SCL_L;//????±??????????????
for(t=0;t<8;t++)
{
if((txd&0x80)>>7 == 1) // IIC_SDA=(txd&0x80)>>7;
IIC_SDA_H;
else
IIC_SDA_L;
txd<<=1;
delay_us(2);
IIC_SCL_H;
delay_us(2);
IIC_SCL_L;
delay_us(2);
}
}
/**************************????????********************************************
*???????: u8 IIC_Read_Byte(unsigned char ack)
*????????: //??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_L;
delay_us(2);
IIC_SCL_H;
receive<<=1;
if(READ_SDA)
receive++;
delay_us(2);
}
if (ack)
IIC_Ack(); //·???ACK
else
IIC_NAck();//·???nACK
return receive;
}
/**************************????????********************************************
*???????: unsigned char I2C_ReadOneByte(unsigned char I2C_Addr,unsigned char addr)
*????????: ???????¨?è±? ???¨?????÷????????
???? I2C_Addr ??±ê?è±????·
addr ?????÷???·
·??? ??????????
*******************************************************************************/
unsigned char I2C_ReadOneByte(unsigned char I2C_Addr,unsigned char addr)
{
unsigned char res=0;
IIC_Start();
IIC_Send_Byte(I2C_Addr); //·??????ü??
res++;
IIC_Wait_Ack();
IIC_Send_Byte(addr); res++; //·??????·
IIC_Wait_Ack();
IIC_Start();
IIC_Send_Byte(I2C_Addr+1); res++; //????????????
IIC_Wait_Ack();
res=IIC_Read_Byte(0);
IIC_Stop();//?ú?ú????????????
return res;
}
/**************************????????********************************************
*???????: u8 IICreadBytes(u8 dev, u8 reg, u8 length, u8 *data)
*????????: ???????¨?è±? ???¨?????÷?? length????
???? dev ??±ê?è±????·
reg ?????÷???·
length ??????×?????
*data ????????????????·???????
·??? ????????×???????
*******************************************************************************/
u8 IICreadBytes(u8 dev, u8 reg, u8 length, u8 *data){
u8 count = 0;
u8 temp;
IIC_Start();
IIC_Send_Byte(dev); //·??????ü??
IIC_Wait_Ack();
IIC_Send_Byte(reg); //·??????·
IIC_Wait_Ack();
IIC_Start();
IIC_Send_Byte(dev+1); //????????????
IIC_Wait_Ack();
for(count=0;count<length;count++){
if(count!=(length-1))
temp = IIC_Read_Byte(1); //??ACK????????
else
temp = IIC_Read_Byte(0); //×??ó????×???NACK
data[count] = temp;
}
IIC_Stop();//?ú?ú????????????
return count;
}
/**************************????????********************************************
*???????: u8 IICwriteBytes(u8 dev, u8 reg, u8 length, u8* data)
*????????: ???à??×??????????¨?è±? ???¨?????÷
???? dev ??±ê?è±????·
reg ?????÷???·
length ??????×?????
*data ???????????????×???·
·??? ·?????·?????
*******************************************************************************/
u8 IICwriteBytes(u8 dev, u8 reg, u8 length, u8* data){
u8 count = 0;
IIC_Start();
IIC_Send_Byte(dev); //·??????ü??
IIC_Wait_Ack();
IIC_Send_Byte(reg); //·??????·
IIC_Wait_Ack();
for(count=0;count<length;count++)
{
IIC_Send_Byte(data[count]);
IIC_Wait_Ack();
}
IIC_Stop();//?ú?ú????????????
return 1; //status == 0;
}
/**************************????????********************************************
*???????: u8 IICreadByte(u8 dev, u8 reg, u8 *data)
*????????: ???????¨?è±? ???¨?????÷????????
???? dev ??±ê?è±????·
reg ?????÷???·
*data ????????????????·??????·
·??? 1
*******************************************************************************/
u8 IICreadByte(u8 dev, u8 reg, u8 *data){
*data=I2C_ReadOneByte(dev, reg);
return 1;
}
/**************************????????********************************************
*???????: unsigned char IICwriteByte(unsigned char dev, unsigned char reg, unsigned char data)
*????????: ???????¨?è±? ???¨?????÷????×???
???? dev ??±ê?è±????·
reg ?????÷???·
data ??????????×???
·??? 1
*******************************************************************************/
unsigned char IICwriteByte(unsigned char dev, unsigned char reg, unsigned char data){
return IICwriteBytes(dev, reg, 1, &data);
}
/**************************????????********************************************
*???????: u8 IICwriteBits(u8 dev,u8 reg,u8 bitStart,u8 length,u8 data)
*????????: ?? ???? ?? ???¨?è±? ???¨?????÷????×??? ?????à????
???? dev ??±ê?è±????·
reg ?????÷???·
bitStart ??±ê×???????????
length ???¤??
data ??·???±???±ê×?????????
·??? ???? ??1
?§°???0
*******************************************************************************/
u8 IICwriteBits(u8 dev,u8 reg,u8 bitStart,u8 length,u8 data)
{
u8 b;
if (IICreadByte(dev, reg, &b) != 0) {
u8 mask = (0xFF << (bitStart + 1)) | 0xFF >> ((8 - bitStart) + length - 1);
data <<= (8 - length);
data >>= (7 - bitStart);
b &= mask;
b |= data;
return IICwriteByte(dev, reg, b);
}
else {
return 0;
}
}
/**************************????????********************************************
*???????: u8 IICwriteBit(u8 dev, u8 reg, u8 bitNum, u8 data)
*????????: ?? ???? ?? ???¨?è±? ???¨?????÷????×??? ????1????
???? dev ??±ê?è±????·
reg ?????÷???·
bitNum ????????±ê×?????bitNum??
data ??0 ?±????±ê????±???0 ·??ò??±?????
·??? ???? ??1
?§°???0
*******************************************************************************/
u8 IICwriteBit(u8 dev, u8 reg, u8 bitNum, u8 data){
u8 b;
IICreadByte(dev, reg, &b);
b = (data != 0) ? (b | (1 << bitNum)) : (b & ~(1 << bitNum));
return IICwriteByte(dev, reg, b);
}
//------------------End of File----------------------------
[/mw_shl_code]
[mw_shl_code=c,true]
/*
** ????????
*/
#define SDA_IN() {GPIOB->MODER&=0X0FFFFFFF;GPIOB->MODER|=0x80000000;}
/*
** ????????
*/
#define SDA_OUT() {GPIOB->MODER&=0X0FFFFFFF;GPIOB->MODER|=0x30000000;}
/*
** SCL - PB8
*/
#define IIC_SCL_H GPIO_SetBits(GPIOB, GPIO_Pin_8)
#define IIC_SCL_L GPIO_ResetBits(GPIOB, GPIO_Pin_8)
/*
** SDA - PB9
*/
#define IIC_SDA_H GPIO_SetBits(GPIOB, GPIO_Pin_9)
#define IIC_SDA_L GPIO_ResetBits(GPIOB, GPIO_Pin_9)
#define READ_SDA GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_9)[/mw_shl_code]
keil粘贴代码注释就乱了,请前辈们指点错在哪? 谢谢!
好像你的也是用到I2C了,不知道M4的是否已经解决这个问题,你也可从这上了解下。
另外M3的RTC时钟也有问题,很难起振,不知道M4这个问题是否解决掉了,有知道的也请告知下,谢谢!
---------------------------------
我用的是IO口模拟啊! 调试跟踪发现程序死在在mpu_init();了!
---------------------------------
嗯,我看过原子哥你的IIC例程,感觉没问题;
仿真试了下程序执行在这块就死在那了
imv_mpu.c
if (i2c_write(st.hw->addr, st.reg->pwr_mgmt_1, 1, &(data[0])))
return -1;
delay_ms(100);
我推断还是我的IO口模拟IIC没配置好,但真不知道是那配置错了!
一周热门 更多>