各位路过的大侠好! 本人第一次使用硬件IIC, 在使用的时候参考了很多例程,都无法实现。debug的时候程序总是停留在第一个检测位,有的时候会走到下一个位。 采用内部108Mhz,4分频27MHZ到 pclk1 . 本人已经闭门造车2天了,希望大侠帮我找找问题。程序如下: #include "sys.h" #include "usart.h" #include "delay.h" //#include "led.h" //#include "beep.h" //#include "key.h" //#include "timer.h" //#include "adc.h" #include "output.h" //#include "output.h" //#include "ir.h" //#include "hym8563.h" //#include "stmflash.h" //#include "TFT018.h" //extern U8 bmp[]; //#include "stm32f10x_i2c.h" #define DEVICE_IP 1 #define iic_address 0x0a #define iic_address2 0x0b /* I2C Events */ /* EV1 */ #define I2C_EVENT_SLAVE_TRANSMITTER_ADDRESS_MATCHED ((u32)0x00060082) /* TRA, BUSY, TXE and ADDR flags */ #define I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED ((u32)0x00020002) /* BUSY and ADDR flags */ #define I2C_EVENT_SLAVE_TRANSMITTER_SECONDADDRESS_MATCHED ((u32)0x00860080) /* DUALF, TRA, BUSY and TXE flags */ #define I2C_EVENT_SLAVE_RECEIVER_SECONDADDRESS_MATCHED ((u32)0x00820000) /* DUALF and BUSY flags */ #define I2C_EVENT_SLAVE_GENERALCALLADDRESS_MATCHED ((u32)0x00120000) /* GENCALL and BUSY flags */ /* EV2 */ #define I2C_EVENT_SLAVE_BYTE_RECEIVED ((u32)0x00020040) /* BUSY and RXNE flags */ #define EV2 I2C_EVENT_SLAVE_BYTE_RECEIVED /* EV3 */ #define I2C_EVENT_SLAVE_BYTE_TRANSMITTED ((u32)0x00060084) /* TRA, BUSY, TXE and BTF flags */ #define EV3 I2C_EVENT_SLAVE_BYTE_TRANSMITTED /* EV4 */ #define I2C_EVENT_SLAVE_STOP_DETECTED ((u32)0x00000010) /* STOPF flag */ #define EV4 I2C_EVENT_SLAVE_STOP_DETECTED /* EV5 */ #define I2C_EVENT_MASTER_MODE_SELECT ((u32)0x00030001) /* BUSY, MSL and SB flag */ #define EV5 I2C_EVENT_MASTER_MODE_SELECT /* EV6 */ #define I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED ((u32)0x00070082) /* BUSY, MSL, ADDR, TXE and TRA flags */ #define I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED ((u32)0x00030002) /* BUSY, MSL and ADDR flags */ /* EV7 */ #define I2C_EVENT_MASTER_BYTE_RECEIVED ((u32)0x00030040) /* BUSY, MSL and RXNE flags */ #define EV7 I2C_EVENT_MASTER_BYTE_RECEIVED /* EV8 */ #define I2C_EVENT_MASTER_BYTE_TRANSMITTED ((u32)0x00070084) /* TRA, BUSY, MSL, TXE and BTF flags */ #define EV8_1 I2C_EVENT_MASTER_BYTE_TRANSMITTED /* --EV8 */ #define I2C_EVENT_MASTER_BYTE_TRANSMITTING ((u32)0x00070080) /* TRA, BUSY, MSL, TXE flags */ /* EV9 */ #define I2C_EVENT_MASTER_MODE_ADDRESS10 ((u32)0x00030008) /* BUSY, MSL and ADD10 flags */ #define EV9 I2C_EVENT_MASTER_MODE_ADDRESS10 /* EV3_2 */ #define I2C_EVENT_SLAVE_ACK_FAILURE ((u32)0x00000400) /* AF flag */ #define EV3_2 I2C_EVENT_SLAVE_ACK_FAILURE ErrorStatus I2C_CheckEvent(I2C_TypeDef* I2Cx, u32 I2C_EVENT) { u32 lastevent = 0; u32 flag1 = 0, flag2 = 0; ErrorStatus status = ERROR; /* Check the parameters */ assert_param(IS_I2C_ALL_PERIPH(I2Cx)); assert_param(IS_I2C_EVENT(I2C_EVENT)); /* Read the I2Cx status register */ flag1 = I2Cx->SR1; flag2 = I2Cx->SR2; flag2 = flag2 << 16; /* Get the last event value from I2C status register */ lastevent = (flag1 | flag2) & 0x0000ffff; /* Check whether the last event is equal to I2C_EVENT */ if (lastevent == I2C_EVENT ) { /* SUCCESS: last event is equal to I2C_EVENT */ status = SUCCESS; } else { /* ERROR: last event is different from I2C_EVENT */ status = ERROR; } /* Return status */ return status; } void I2C_GenerateSTART(I2C_TypeDef* I2Cx, FunctionalState NewState) { if (NewState != DISABLE) { /* Generate a START condition */ I2Cx->CR1 |= 1<<8; } else { /* Disable the START condition generation */ I2Cx->CR1 &= ~(1<<8); } } void I2C_GenerateSTOP(I2C_TypeDef* I2Cx, FunctionalState NewState) { if (NewState != DISABLE) { /* Generate a STOP condition */ I2Cx->CR1 |= 1<<9; } else { /* Disable the STOP condition generation */ I2Cx->CR1 &= ~(1<<9); } } void I2C_SendData(I2C_TypeDef* I2Cx, u8 Data) { /* Check the parameters */ assert_param(IS_I2C_ALL_PERIPH(I2Cx)); /* Write in the DR register the data to be sent */ I2Cx->DR = Data; } u8 I2C_ReceiveData(I2C_TypeDef* I2Cx) { /* Check the parameters */ assert_param(IS_I2C_ALL_PERIPH(I2Cx)); /* Return the data in the DR register */ return (u8)I2Cx->DR; } //========================================== int main(void) { u8 i; u8 x; u16 t; u16 u16_flag,u16_flag1; u8 dir=1; Stm32_Clock_Init(9); //ϵͳʱÖÓÉèÖÃ9*12MÄÚ²¿HSI delay_init(108); //ÑÓʱ³õʼ»¯36--108 /*LED_Init(); //³õʼ»¯ÓëLEDÁ¬½ÓµÄÓ²¼þ½Ó¿Ú //BEEP_Init(); //³õʼ»¯·äÃùÆ÷¶Ë¿Ú KEY_Init(); //³õʼ»¯ÊäÈë¶Ë¿Ú Output_Init(); //³õʼ»¯Êä³ö¿ØÖÆ¶Ë¿Ú HYM8563_init(); uart_init(108,115200); uart3_init(27,115200); TIM2_Int_Init(5400,0); printf("U16_UP\r\n"); USART3->DR = 0x55; RF_init(); Adc_Init(); */ //uart2_init(27,9600); //printf("U16_UP\r\n"); //³õʼ»¯i2C1 ; RCC->APB2ENR|=1<<3; //ʹÄÜPORTBʱÖÓ RCC->APB1ENR|=1<<21; RCC->APB1RSTR|=1<<21; //ADC1¸´Î» RCC->APB1RSTR&=~(1<<21);//¸´Î»½áÊø AFIO->MAPR &= ~(1<<1);//I2C1ûÓÐÖØÓ³Éä //AFIO->MAPR |= ((u32)0x00000000);//0x03<<24;½ûÖ¹JTAG¹¦Äܽţ¬ÉèÖÃPB3PB4ΪIO¿Ú GPIOB->CRL&=0X00FFFFFF; GPIOB->CRL|=0XFF000000; // PBout(6)=1; PBout(7)=1; //======================================= I2C1->CR1=0; /* Enable the selected I2C peripheral */ I2C1->CR1 |= 1<<0; I2C1->CR1 |= 1<<3;//´Ó //======================================= I2C1->CR2=0; I2C1->CR2 |= 27<<0; //======================================= I2C1->OAR1=0x4000; I2C1->OAR1 &=~(1<<15);//7bit address I2C1->OAR1 |= (iic_address&0xfe); I2C1->OAR2=0x0000; I2C1->OAR2 |=(iic_address2&0xfe); I2C1->OAR2 |= 1<<0;//ÆßλµØÖ·Ë«µØַģʽ£» //=======cCCR================================ /* DisEnable the selected I2C peripheral */ I2C1->CR1 &= ~(1<<0); I2C1->CCR =0; I2C1->CCR &=~(1<<15);// f/s ¿ìËÙģʽ ±ê׼ģʽ I2C1->CCR &=~(1<<14);// DUTY ¿ìËÙģʽϵÄÕ¼¿Õ±È L/H=2 //ÉèÖÃÖ÷ģʽÊǵķ¢ËÍʼÖÕËÙ¶È200khz I2C1->CCR |= (27000/100) ; //======================================= I2C1->TRISE=(27+1);//ÉÏÉýÑØ //======================================= //I2C1->SR1=0; //======================================= //I2C1->SR2=0; //======================================= //I2C1->DR=0; //======================================= /* Enable the selected I2C peripheral */ I2C1->CR1 |= 1<<0;//ʹÄÜ //while((I2C1->SR2&(1<<1))); while(1) { //============================================================================= I2C1->CR1 |= 1<<8;//I2C_GenerateSTART(I2C1, 1);//¿ªÊ¼ÐźŠdelay_us(100); //EV5ʼþ while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_MODE_SELECT)); //========address================================ I2C1->DR=0xa2; //============================== //EV6 EV8_1£¨¸ÃʼþÅжÏͬʱÅжÏÁËEV8_1ʼþ£© while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)); //============================================== I2C1->DR=0x55; //EV8ʼþ I2C_EVENT_MASTER_BYTE_TRANSMITTED while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTING)) ; //========================================= I2C1->DR=0x02; while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED)); //I2C1->CR1 |= 1<<9;//I2C_GenerateSTOP(I2C_TypeDef* I2Cx, FunctionalState NewState) I2C_GenerateSTOP(I2C1,ENABLE); /*EV8_2ʼþÖеijÌÐòдֹͣ*/ // printf(" I2C1->CR1:%x\r\n" ,I2C1->CR1); delay_us(20000); } } |
V00 IIC.zip
下载1.41 MB, 下载次数: 4, 下载积分: ST金币 -1
å·¥ç¨æ件
有解决了的大侠可以转发我学习一下, 另外其他和我一样的新手要是有疑问的也可以问我, 本人尽力回答。
幻想一下,比较期待能弄出来嘛!