0. 파일
1. STM32Cube
I2c 를 사용하려면
2가지 방법이 있습니다.
첫번째는 GPIO를 사용하여 직접 제어 는
장점 - Pull up 저항 안달아도됨
직관적인 제어
두번째는 내부 함수를 사용해서 제어하는 방법입니다.
근데 하다보니 Clock Configuration 에서 2Mhz 이상 설정하면 I2C가 안되는 문제가 있는거 같습니다.
그래서 위에 Cube 설정에서 2Mhz 로 맞춘 이유가 그것입니다.
2. 소스
user_I2c.c |
#include "stm32f1xx_hal.h" int at24_HAL_WriteBytes(I2C_HandleTypeDef *hi2c,uint16_t DevAddress,uint16_t MemAddress, uint8_t *pData,uint16_t TxBufferSize) { printf("Write"); /* * program just get the DevAddress of the Slave (not master) and for the next step * You know that the most of the EEprom address start with 0xA0 * give MemAddress for the location you want to write to * give Data buffer so it can write Data on this location */ //Note that this function works properly to 31 bytes if(MemAddress+TxBufferSize > 16) { //Write to 16bytes while(HAL_I2C_Mem_Write(hi2c,(uint16_t)DevAddress,(uint16_t)MemAddress,I2C_MEMADD_SIZE_8BIT,pData,(uint16_t)16-MemAddress,1000)!= HAL_OK); //write remaining bytes *pData = *pData + (16-MemAddress); while(HAL_I2C_Mem_Write(hi2c,(uint16_t)DevAddress,(uint16_t)16,I2C_MEMADD_SIZE_8BIT,pData,(uint16_t)((MemAddress+TxBufferSize)-16),1000)!= HAL_OK); } else { while( (TxBufferSize-16)>0 ) { //if your data is more than 16 bytes,you are here while(HAL_I2C_Mem_Write(hi2c,(uint16_t)DevAddress,(uint16_t)MemAddress,I2C_MEMADD_SIZE_8BIT,pData,(uint16_t)16,1000)!= HAL_OK); TxBufferSize-=16; pData+=16; MemAddress+=16; } //remaining data while(HAL_I2C_Mem_Write(hi2c,(uint16_t)DevAddress,(uint16_t)MemAddress,I2C_MEMADD_SIZE_8BIT,pData,(uint16_t)TxBufferSize,1000)!= HAL_OK); } return 1; } int at24_HAL_ReadBytes(I2C_HandleTypeDef *hi2c,uint16_t DevAddress,uint16_t MemAddress, uint8_t *pData,uint16_t RxBufferSize) { int TimeOut; printf("Read"); /* * program just get the DevAddress of the Slave (not master) and for the next step * You know that the most of the EEprom address start with 0xA0 * get the MemAddress for the location you want to write data on it * get the Data buffer so it can write Data on this location */ //Note that this function works properly to 31bytes while( (RxBufferSize-16)>0 ) { //if your data is more than 16 bytes,you are here TimeOut = 0; while(HAL_I2C_Mem_Read(hi2c,(uint16_t)DevAddress,(uint16_t)MemAddress,I2C_MEMADD_SIZE_8BIT,pData,(uint16_t)16,1000)!= HAL_OK && TimeOut < 10) { TimeOut++; } RxBufferSize-=16; pData+=16; MemAddress+=16; } // //remaining data TimeOut = 0; while(HAL_I2C_Mem_Read(hi2c,(uint16_t)DevAddress,(uint16_t)MemAddress,I2C_MEMADD_SIZE_8BIT,pData,(uint16_t)RxBufferSize,1000)!= HAL_OK && TimeOut < 10) { TimeOut++; } return 1; } |
main.c |
#include "main.h" #include "stm32f1xx_hal.h" /* USER CODE BEGIN Includes */ /* USER CODE END Includes */ /* Private variables ---------------------------------------------------------*/ I2C_HandleTypeDef hi2c1; UART_HandleTypeDef huart1; /* USER CODE BEGIN PV */ /* Private variables ---------------------------------------------------------*/ /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ void SystemClock_Config(void); static void MX_GPIO_Init(void); static void MX_I2C1_Init(void); static void MX_USART1_UART_Init(void); /* USER CODE BEGIN PFP */ /* Private function prototypes -----------------------------------------------*/ /* USER CODE END PFP */ /* USER CODE BEGIN 0 */ int fputc(int ch, FILE *f) { uint8_t temp[1]={ch}; HAL_UART_Transmit(&huart1, temp, 1, 2); return(ch); } /* USER CODE END 0 */ /** * @brief The application entry point. * * @retval None */ int main(void) { /* USER CODE BEGIN 1 */ uint8_t d[1]={0x04}; uint8_t c[1]={0x00}; uint8_t e[1]={0x00}; /* USER CODE END 1 */ /* MCU Configuration----------------------------------------------------------*/ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init(); /* USER CODE BEGIN Init */ /* USER CODE END Init */ /* Configure the system clock */ SystemClock_Config(); /* USER CODE BEGIN SysInit */ /* USER CODE END SysInit */ /* Initialize all configured peripherals */ MX_GPIO_Init(); MX_I2C1_Init(); MX_USART1_UART_Init(); /* USER CODE BEGIN 2 */ printf("test\r\n"); /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */
HAL_Delay(100); at24_HAL_WriteBytes(&hi2c1, 0xA0, 0, d, 1); HAL_Delay(500); at24_HAL_ReadBytes(&hi2c1, 0xA0, 0, c, 1); HAL_Delay(500); printf("%d\r\n",c[0]); at24_HAL_WriteBytes(&hi2c1, 0xA0, 0, e, 1); HAL_Delay(500); at24_HAL_ReadBytes(&hi2c1, 0xA0, 0, c, 1); HAL_Delay(500); printf("clear %d\r\n",c[0]);
while (1) { /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ } /* USER CODE END 3 */ } |
'공부 > STM32F1' 카테고리의 다른 글
31.[stm32f103][hal] OLED SSD1306 (0) | 2018.10.22 |
---|---|
30.[stm32f103][hal] us delay 만들기 (0) | 2018.10.12 |
28.[stm32f103][hal] CDC-Virtual Port Com (1) | 2018.10.09 |
27.[stm32f103][hal] ADC (3) -DMA ADC (3) | 2018.02.28 |
26.[stm32f103][hal] ADC (2) - Polling Multi 모드 (2) | 2017.12.21 |