完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
void RTC_Init(void)
{ I2C_Initial(); PCF8563_Init(); } void PCF8563_Init(void) { I2C_Write(PCF8563_CONTRL_STATE_1,0x00);//оƬ¹¤×÷ÔÚÆÕͨģʽ£¬Ð¾Æ¬Ê±ÖÓÔËÐÐ I2C_Write(PCF8563_CONTRL_STATE_2,0x00);//AIE,TIEÖжϽûÖ¹ I2C_Write(PCF8563_SENCOND_AE_REGISTER,0x80);//·ÖÖÓ±¨¾¯ÎÞЧ I2C_Write(PCF8563_HOUR_AE_REGISTER,0x80);//ʱ±¨¾¯ÎÞЧ I2C_Write(PCF8563_DAY_AE_REGISTER,0x80);//ÈÕ±¨¾¯ÎÞЧ I2C_Write(PCF8563_WEEK_AE_REGISTER,0x80);//Öܱ¨¾¯ÎÞЧ I2C_Write(PCF8563_TIME_CONTRL,0x82);//ʹÄܶ¨Ê±¼ÆÊýÆ÷£d¶¨Ê±Ê±ÖÓƵÂʵÄÑ¡ÔñΪ1hz I2C_Write(PCF8563_TIME_COUNT_DOWN,0x3C);//µ¹¼ÆÊýÖÜÆÚΪ1s = 60/1HZ£¬£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿ I2C_Write(PCF8563_MONTH_REGISTER,0x00);//Ô¼ĴæÆ÷×î¸ßλд0£¬±íʾ20ÊÀ¼Í } void I2C_Initial(void) { GPIO_InitTypeDef GPIO_InitStructure; I2C_InitTypeDef I2C_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1,ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //------------RTC_SDA->PB_11RTC_SCL->PB_10---------------------------------------- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10|GPIO_Pin_11; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD; GPIO_Init(GPIOA,&GPIO_InitStructure); //------------I2C_INIT-------------------------------------------- I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;//ÉèÖÃΪI2Cģʽ I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2; I2C_InitStructure.I2C_OwnAddress1 = PCF8563_ADDR_W;//I2CµÄ´ÓµØÖ·ºÍPCF8563µÄдµØÖ·ÊÇÒ»¸öµØÖ·Â𣿣¿£¿£¿£¿ I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;//ʹÄÜÓ¦´ð I2C_InitStructure.I2C_ClockSpeed = I2C_Speed; //400000hz I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;//Ó¦´ð7λµØÖ· I2C_Init(I2C1,&I2C_InitStructure); //---------NVIC_INIT---------------------------------------------------- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);//ÓÅÏȼ¶·Ö×é NVIC_InitStructure.NVIC_IRQChannel = I2C1_EV_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); I2C_Cmd(I2C1,ENABLE); } void I2C_Write(u8 WriteAddr,u8 Dat) { while(I2C_GetFlagStatus(I2C1,I2C_FLAG_BUSY)); I2C_GenerateSTART(I2C1,ENABLE); while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_MODE_SELECT)); I2C_Send7bitAddress(I2C1,PCF8563_ADDR_W,I2C_Direction_Transmitter); while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)); I2C_SendData(I2C1,WriteAddr); while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED)); I2C_SendData(I2C1,Dat); while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED)); I2C_GenerateSTOP(I2C1,ENABLE); } u8 I2C_Read(u8 ReadAddr) { u8 a; while(I2C_GetFlagStatus(I2C1,I2C_FLAG_BUSY)); I2C_GenerateSTART(I2C1,ENABLE); while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_MODE_SELECT)); I2C_Send7bitAddress(I2C1,PCF8563_ADDR_W,I2C_Direction_Transmitter); while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)); I2C_SendData(I2C1,ReadAddr); while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED)); I2C_GenerateSTART(I2C1,ENABLE); while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_MODE_SELECT)); I2C_Send7bitAddress(I2C1,PCF8563_ADDR_R,I2C_Direction_Receiver); while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)); a = I2C_ReceiveData(I2C1); while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_RECEIVED)) I2C_AcknowledgeConfig(I2C1,DISABLE); I2C_GenerateSTOP(I2C1,ENABLE); I2C_AcknowledgeConfig(I2C1,ENABLE); return a; } void I2C_Write_nByte(u8 WriteAddr,u8* Dat, u8 NumByte) { while(I2C_GetFlagStatus(I2C1,I2C_FLAG_BUSY)); I2C_GenerateSTART(I2C1,ENABLE); while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_MODE_SELECT)); I2C_Send7bitAddress(I2C1,PCF8563_ADDR_W,I2C_Direction_Transmitter); while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)); I2C_SendData(I2C1,WriteAddr); while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED)); while(NumByte--) { I2C_SendData(I2C1,*Dat); Dat++; while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED)); } I2C_GenerateSTOP(I2C1,ENABLE); } void I2C_Read_nByte(u8 ReadAddr,u8* Buffer,u8 NumByte) { while(I2C_GetFlagStatus(I2C1,I2C_FLAG_BUSY)); I2C_GenerateSTART(I2C1,ENABLE); while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_MODE_SELECT)); I2C_Send7bitAddress(I2C1,PCF8563_ADDR_W,I2C_Direction_Transmitter); while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)); I2C_SendData(I2C1,ReadAddr); while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED)); I2C_GenerateSTART(I2C1,ENABLE); while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_MODE_SELECT)); I2C_Send7bitAddress(I2C1,PCF8563_ADDR_R,I2C_Direction_Receiver); while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)); while(NumByte) { if(NumByte == 1) { I2C_AcknowledgeConfig(I2C1,DISABLE); I2C_GenerateSTOP(I2C1,ENABLE); } if(I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_RECEIVED)) { *Buffer = I2C_ReceiveData(I2C1); Buffer++; NumByte--; } } I2C_AcknowledgeConfig(I2C1,ENABLE); } |
|
|
|
好的,不好意思,我给选错了,谢谢您的提醒。
|
|
|
|
如果没有开启中断 是不需要中断服务函数的 基本就是通过while 等待相应的事件发生 如果硬件 读写程序都没有问题的话 没有响应的话 可能就是你等待的事件不对
|
|
|
|
楼主最好贴一下整个过程,从开始到最后的I2C通讯过程。
|
|
|
|
本帖最后由 toofree 于 2018-2-27 11:55 编辑
好好整理一下问题吧。 用的哪个型号的MCU,具体型号?用的哪个版本的库?要控制的IIC外设是什么? 我只能告诉你,我在第一次用I2C读写EEPROM AT24CXX的时候,也遇到过类似这样的死循环出不来的问题,是有办法解决的。 解决方法大致有三种: 1、IO模拟I2C 2、操作完后,不能马上去读标志位,得延时一小会儿 3、while()循环中,增加一个超时退出自加变量 |
|
|
|
void RTC_Init(void) { I2C_Initial(); PCF8563_Init(); } void PCF8563_Init(void) { I2C_Write(PCF8563_CONTRL_STATE_1,0x00); I2C_Write(PCF8563_CONTRL_STATE_2,0x00); I2C_Write(PCF8563_SENCOND_AE_REGISTER,0x80); I2C_Write(PCF8563_HOUR_AE_REGISTER,0x80); I2C_Write(PCF8563_DAY_AE_REGISTER,0x80); I2C_Write(PCF8563_WEEK_AE_REGISTER,0x80); I2C_Write(PCF8563_TIME_CONTRL,0x82); I2C_Write(PCF8563_TIME_COUNT_DOWN,0x3C); I2C_Write(PCF8563_MONTH_REGISTER,0x00); } void I2C_Initial(void) { GPIO_InitTypeDef GPIO_InitStructure; I2C_InitTypeDef I2C_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1,ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //------------RTC_SDA->PB_11RTC_SCL->PB_10---------------------------------------- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10|GPIO_Pin_11; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD; GPIO_Init(GPIOA,&GPIO_InitStructure); //------------I2C_INIT-------------------------------------------- I2C_InitStructure.I2C_Mode = I2C_Mode_I2C; I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2; I2C_InitStructure.I2C_OwnAddress1 = PCF8563_ADDR_W; I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; I2C_InitStructure.I2C_ClockSpeed = I2C_Speed; //400000hz I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; I2C_Init(I2C1,&I2C_InitStructure); //---------NVIC_INIT---------------------------------------------------- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); NVIC_InitStructure.NVIC_IRQChannel = I2C1_EV_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); I2C_Cmd(I2C1,ENABLE); } void I2C_Write(u8 WriteAddr,u8 Dat) { while(I2C_GetFlagStatus(I2C1,I2C_FLAG_BUSY)); I2C_GenerateSTART(I2C1,ENABLE); while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_MODE_SELECT)); I2C_Send7bitAddress(I2C1,PCF8563_ADDR_W,I2C_Direction_Transmitter); while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)); I2C_SendData(I2C1,WriteAddr); while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED)); I2C_SendData(I2C1,Dat); while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED)); I2C_GenerateSTOP(I2C1,ENABLE); } u8 I2C_Read(u8 ReadAddr) { u8 a; while(I2C_GetFlagStatus(I2C1,I2C_FLAG_BUSY)); I2C_GenerateSTART(I2C1,ENABLE); while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_MODE_SELECT)); I2C_Send7bitAddress(I2C1,PCF8563_ADDR_W,I2C_Direction_Transmitter); while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)); I2C_SendData(I2C1,ReadAddr); while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED)); I2C_GenerateSTART(I2C1,ENABLE); while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_MODE_SELECT)); I2C_Send7bitAddress(I2C1,PCF8563_ADDR_R,I2C_Direction_Receiver); while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)); a = I2C_ReceiveData(I2C1); while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_RECEIVED)) I2C_AcknowledgeConfig(I2C1,DISABLE); I2C_GenerateSTOP(I2C1,ENABLE); I2C_AcknowledgeConfig(I2C1,ENABLE); return a; } void I2C_Write_nByte(u8 WriteAddr,u8* Dat, u8 NumByte) { while(I2C_GetFlagStatus(I2C1,I2C_FLAG_BUSY)); I2C_GenerateSTART(I2C1,ENABLE); while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_MODE_SELECT)); I2C_Send7bitAddress(I2C1,PCF8563_ADDR_W,I2C_Direction_Transmitter); while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)); I2C_SendData(I2C1,WriteAddr); while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED)); while(NumByte--) { I2C_SendData(I2C1,*Dat); Dat++; while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED)); } I2C_GenerateSTOP(I2C1,ENABLE); } void I2C_Read_nByte(u8 ReadAddr,u8* Buffer,u8 NumByte) { while(I2C_GetFlagStatus(I2C1,I2C_FLAG_BUSY)); I2C_GenerateSTART(I2C1,ENABLE); while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_MODE_SELECT)); I2C_Send7bitAddress(I2C1,PCF8563_ADDR_W,I2C_Direction_Transmitter); while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)); I2C_SendData(I2C1,ReadAddr); while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED)); I2C_GenerateSTART(I2C1,ENABLE); while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_MODE_SELECT)); I2C_Send7bitAddress(I2C1,PCF8563_ADDR_R,I2C_Direction_Receiver); while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)); while(NumByte) { if(NumByte == 1) { I2C_AcknowledgeConfig(I2C1,DISABLE); I2C_GenerateSTOP(I2C1,ENABLE); } if(I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_RECEIVED)) { *Buffer = I2C_ReceiveData(I2C1); Buffer++; NumByte--; } } I2C_AcknowledgeConfig(I2C1,ENABLE); } |
|
|
|
MCU:stm32f103rbt,库:CMSIS Cortex-M3 Core V1.30,IIC外设:PCF8563 |
|
|
|
407的我测试过可以的,过程和你的一样,103以前我用的是模拟I2C,硬件好像有点问题。不知道后来大家都是使用硬件还是模拟了。大家一块讨论一下吧。
|
|
|
|
还是用软件模拟IIC比较可靠。
|
|
|
|
我觉得我卡死在while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));里面是我写完7位地址后,没有应答,网上说是从设备慢的问题,但我不太清楚。您觉得是什么原因导致的呢? |
|
|
|
楼主写的从设备地址是多少?8563应该是0xA2。
|
|
|
|
嗯嗯是的,: #define PCF8563_ADDR_W 0xA2 #define PCF8563_ADDR_R 0xA3 |
|
|
|
我想问一下,是不是得写一个用于I2C接收应答的中断服务函数呢?因为我是参照书上的例程写的,没有看到中断服务函数,网上的例子也很少。 |
|
|
|
我用中断都是在前面通讯完成后,读取数据或者发送数据的时候用。最好用示波器看一下是设备是否应答。
|
|
|
|
|
|
|
|
可以先用IO模拟IIC试一下,确定一下硬件没有问题。
STM32下IO口模拟IIC通讯的代码: #define SCL_PORT GPIOA #define SCL_PIN GPIO_Pin_1 #define SCL_HIGH GPIOA->BSRR=(uint32_t)GPIO_Pin_1 #define SCL_LOW GPIOA->BRR=(uint32_t)GPIO_Pin_1 #define SDA_PORT GPIOA #define SDA_PIN GPIO_Pin_2 #define SDA_HIGH GPIO_>BSRR=(uint32_t)GPIO_Pin_2 #define SDA_LOW GPIOA->BRR=(uint32_t)GPIO_Pin_2 #define SDA_READ (uint16_t)(GPIOA->ID&GPIO_Pin_2) #define SDA_OUT GPIOA->MODER|=((uint32_t)GPIO_Mode_OUT)<<(2*2)) #define SDA_IN GPIOA->MODER&=(GPIO_MODER_MODERO)<<(2*2)) #define SCL_OUTH() SCL_HIGH #define SCL_OUTL() SCL_LOW #define SDA_OUTH() SDA_HIGH #define SDA_OUTL() SDA_LOW #define SDA_SETIN() SDA_IN #define SDA_READ() SDA_READ void I2C_Delay(void) { uint8_t i = 200; while(i--); } void SDA_SETOUT(void) { SDA_IN; SDA_OUT; } void I2C_Start(void) { SCL_OUTH(); SDA_OUTH(); I2C_Delay(); SDA_OUTL(); I2C_Delay(); SCL_OUTL(); I2C_Delay(); } void I2C_Stop(void) { SCL_OUTL(); SDA_OUTL(); I2C_Delay(); SCL_OUTH(); I2C_Delay(); SDA_OUTH(); Delay(Delay5ms); //Delay() 为系统延时,用于确保数据传输正确 } uint8_t I2C_WriteByte(uint8_t Data) { uint8_t i,bAck=0; for(i=0;i<8;i++) { SCL_OUTL(); if(Data & 0x80) SDA_OUTH(); else SDA_OUTL(); I2C_Delay(); SCL_OUTH(); I2C_Delay(); Data <<=1; } SCL_OUTL(); I2C_Delay(); SCL_OUTH(); I2C_Delay(); SDA_SETIN(); if(SDA_READ()) bAck=1; else bAck=0; SCL_OUTL(); SDA_SETOUT(); I2C_Delay(); return ((uint8_t)(!bAck)); } uint8_t I2C_ReadByte(uint8_t bLSByte) { uint8_t i,Data=0; SDA_SETIN(); for(i=8;i!=8;i--) { SCL_OUTL(); Data = Data<<1; I2C_Delay(); SCL_OUTH(); I2C_Delay(); if(SDA_READ()) Data |=0x01; else Data &=0xFE; } SCL_OUTL(); SDA_SETOUT(); if(bLSByte) SDA_OUTH(); else SDA_OUTL(); I2C_Delay(); SCL_OUTH(); I2C_Delay(); SCL_OUTL(); I2C_Delay(); return(Data); } uint8_t DAC_Write_1byte(uint8_t Slave,uint8_t Regist_Addr,uint8_t Data) { uint8_t succ,time=0; I2C_Start(); succ=I2C_ReadByte(Slave); while((succ!=1)&&(time<3)) //从机没有响应,重试三次 { I2C_Stop(); I2C_Start(); succ=I2C_WriteByte(Slave); time++; } succ=I2C_WriteByte(Regist_Addr); //发送寄存器地址 succ=I2C_WriteByte(Data); //发送数据 I2C_Stop(); return succ; } uint8_t DAC_Read_1byte(uint8_t Slave,uint8_t,Regist_Addr) { uint8_t succ,time=0; uint8_t dat; I2C_Start(); succ=I2C_WriteByte(Slave+1) //加1表示读数据 while((succ!=1)&&(time<3)) { I2C_Stop(); I2C_Start(); succ=I2C_WriteByte(Slave+1); time++; } succ=I2C_WriteByte(Regist_Addr); dat=I2C_ReadByte(0); I2C_Stop(); return dat; } |
|
|
|
好的,非常感谢您 |
|
|
|
该贴的问题还没有解决,我最近有其他事要忙,如果问题解决了,我会再发贴说明的,谢谢大家,以后有问题再请教大家,非常感谢。
|
|
|
|
如果用IIC模拟都不行的话,估计是硬件问题了。
先上个原理图 中文资料 模拟IIC的驱动代码 https://wenku.baidu.com/view/3107b4be1a37f111f1855b77.html |
|
|
|
你正在撰写答案
如果你是对答案或其他答案精选点评或询问,请使用“评论”功能。
2049 浏览 1 评论
AD7686芯片不传输数据给STM32,但是手按住就会有数据。
1893 浏览 3 评论
4485 浏览 0 评论
如何解决MPU-9250与STM32通讯时,出现HAL_ERROR = 0x01U
2040 浏览 1 评论
hal库中i2c卡死在HAL_I2C_Master_Transmit
2549 浏览 1 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-21 08:21 , Processed in 0.956396 second(s), Total 107, Slave 91 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号