STM32L0 with DS1307 problem - Page 1
Forum section: electronics / beginners

author: Adono
author_rank: Newbie
timestamp: July 14, 2017, 01:19:29 pm
content: Hello,I'm a electronics student and I've been problems with STMCubeMX and KeilUvision5.I'm trying to do a menu for a rtc clock,in this menu I'd like to set the minutes,hours,day,etc... with 3 buttons.Below it's my teacher's code,it's just a example from the clock working,but i dont understande this line:HAL_I2C_Master_Transmit(&hi2c1, 0xD0, &rtc_radr, 1, 20);Could someone explain to me how I am having access to all the array memory locations?Could someone write a code where I for example read the data and send only the minutes, if you have how to do this of course, from now I thank you.Code:/********************************************************************************* File Name : main.c* Description : Main program body******************************************************************************** This notice applies to any and all portions of this file* that are not between comment pairs USER CODE BEGIN and* USER CODE END. Other portions of this file, whether* inserted by the user or by software development tools* are owned by their respective copyright owners.** COPYRIGHT(c) 2017 STMicroelectronics** Redistribution and use in source and binary forms, with or without modification,* are permitted provided that the following conditions are met:* 1. Redistributions of source code must retain the above copyright notice,* this list of conditions and the following disclaimer.* 2. Redistributions in binary form must reproduce the above copyright notice,* this list of conditions and the following disclaimer in the documentation* and/or other materials provided with the distribution.* 3. Neither the name of STMicroelectronics nor the names of its contributors* may be used to endorse or promote products derived from this software* without specific prior written permission.** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.********************************************************************************//* Includes ------------------------------------------------------------------*/#include "main.h"#include "stm32l0xx_hal.h"/* USER CODE BEGIN Includes */#include "stdio.h" // inclui a biblioteca para usar o sprintf/* USER CODE END Includes *//* Private variables ---------------------------------------------------------*/I2C_HandleTypeDef hi2c1;/* USER CODE BEGIN PV *//* Private variables ---------------------------------------------------------*/// ---- Matrizes para o I2C ------uint8_t rtc_adj; // Matriz responsavel para ajustar o DS1307.uint8_t rtc_read;// Matriz responsavel para fazer a leitura do DS1307.uint8_t rtc_init={0x00,0x00}; // Matriz responsavel para fazer o ajuste inicial.uint8_t rtc_radr = 0x00;// Variavel responsavel para fazer a leitura de endereço( Read Adress)uint8_t ds=0x00;// -------------------------------uint8_t menu=1,troca=0,hr=0x00,u=0,d=0,x=0,i=0;char disp;// 2 é o numero de espaços no displayuint8_t min=0,hor=0,dia=0,mes=0,ano=0,dm=0;/* USER CODE END PV *//* Private function prototypes -----------------------------------------------*/void SystemClock_Config(void);static void MX_GPIO_Init(void);static void MX_I2C1_Init(void);/* USER CODE BEGIN PFP *//* Private function prototypes -----------------------------------------------*//* USER CODE END PFP *//* USER CODE BEGIN 0 */// ------ Rotina de Escrita para LCD 16x2 - 4 Bits ----------/* write_lcd - escrita de dados ou comandos no lcd */void write_lcd(uint8_t data, uint8_t cmd_data){// identifica se é comando ou dadoif(cmd_data){HAL_GPIO_WritePin(RS_GPIO_Port,RS_Pin,GPIO_PIN_SET);}else{HAL_GPIO_WritePin(RS_GPIO_Port,RS_Pin,GPIO_PIN_RESET);}// 4 bits highif(data & 0x80) // dado 7 é igual a 1?{HAL_GPIO_WritePin(D7_GPIO_Port,D7_Pin,GPIO_PIN_SET);// Se sim,D7=1;}else{HAL_GPIO_WritePin(D7_GPIO_Port,D7_Pin,GPIO_PIN_RESET);// Se não,D7=0;}if(data & 0x40) // dado 6 é igual a 1?{HAL_GPIO_WritePin(D6_GPIO_Port,D6_Pin,GPIO_PIN_SET);// Se sim,D6=1;}else{HAL_GPIO_WritePin(D6_GPIO_Port,D6_Pin,GPIO_PIN_RESET);// Se não,D6=0;}if(data & 0x20) // dado 6 é igual a 1?{HAL_GPIO_WritePin(D5_GPIO_Port,D5_Pin,GPIO_PIN_SET);// Se sim,D5=1;}else{HAL_GPIO_WritePin(D5_GPIO_Port,D5_Pin,GPIO_PIN_RESET);// Se não,D5=0;}if(data & 0x10) // dado 6 é igual a 1?{HAL_GPIO_WritePin(D4_GPIO_Port,D4_Pin,GPIO_PIN_SET);// Se sim,D4=1;}else{HAL_GPIO_WritePin(D4_GPIO_Port,D4_Pin,GPIO_PIN_RESET);// Se não,D4=0;}// Habilitar o Enable do LCDHAL_GPIO_WritePin(EN_GPIO_Port,EN_Pin,GPIO_PIN_SET); // Habilita o enable do lcdHAL_Delay(2); // Espera 2 milisegundos(dependendo do display)HAL_GPIO_WritePin(EN_GPIO_Port,EN_Pin,GPIO_PIN_RESET); // // Desabilita o enable lcd.// 4 bits low -> nibble menos significativoif(data & 0x08) // dado 7 é igual a 1?{HAL_GPIO_WritePin(D7_GPIO_Port,D7_Pin,GPIO_PIN_SET);// Se sim,D7=1;}else{HAL_GPIO_WritePin(D7_GPIO_Port,D7_Pin,GPIO_PIN_RESET);// Se não,D7=0;}if(data & 0x04) // dado 6 é igual a 1?{HAL_GPIO_WritePin(D6_GPIO_Port,D6_Pin,GPIO_PIN_SET);// Se sim,D6=1;}else{HAL_GPIO_WritePin(D6_GPIO_Port,D6_Pin,GPIO_PIN_RESET);// Se não,D6=0;}if(data & 0x02) // dado 6 é igual a 1?{HAL_GPIO_WritePin(D5_GPIO_Port,D5_Pin,GPIO_PIN_SET);// Se sim,D5=1;}else{HAL_GPIO_WritePin(D5_GPIO_Port,D5_Pin,GPIO_PIN_RESET);// Se não,D5=0;}if(data & 0x01) // dado 6 é igual a 1?{HAL_GPIO_WritePin(D4_GPIO_Port,D4_Pin,GPIO_PIN_SET);// Se sim,D4=1;}else{HAL_GPIO_WritePin(D4_GPIO_Port,D4_Pin,GPIO_PIN_RESET);// Se não,D4=0;}// Habilitar o Enable do LCDHAL_GPIO_WritePin(EN_GPIO_Port,EN_Pin,GPIO_PIN_SET); // Habilita o enable do lcdHAL_Delay(2); // Espera 2 milisegundos(dependendo do display)HAL_GPIO_WritePin(EN_GPIO_Port,EN_Pin,GPIO_PIN_RESET); // // Desabilita o enable lcd.}/* Rotina para Inicizalizar o Display LCD 16x2 - 4 Bits */void init_lcd(void){write_lcd(0x33,0);write_lcd(0x32,0);write_lcd(0x28,0);write_lcd(0x06,0); // deslocamentowrite_lcd(0x0C,0); // cursorwrite_lcd(0x01,0); // Limpa display}/* string_lcd - função para escrever string no LCD 16x2 */void string_lcd(char *string){while(*string){write_lcd(*string++,1); // manda o dado e desloca a posição.}}/* USER CODE END 0 */int main(void){/* USER CODE BEGIN 1 *//* 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();/* USER CODE BEGIN 2 */init_lcd(); // Inicializa o display de lcd.write_lcd(0x01,0); // Limpa o display de lcd.string_lcd(" : : "); // Espeço reservado para horaswrite_lcd(0xC0,0); // Escreve na linha dois do display.string_lcd(" / / ");write_lcd(0xCC,0);string_lcd("5.13");// ----- Inicialização do RTC ----------rtc_adj = 0x00; //endereço do registradorrtc_adj = 0x00; //segundosrtc_adj = 0x44; //minutosrtc_adj = 0x13; //horartc_adj = 0x06; //dia da semanartc_adj = 0x09; //diartc_adj = 0x07; //mêsrtc_adj = 0x17; //anortc_adj = 0x10; //controle// -- Escrita dos dados no DS1307 ----HAL_I2C_Master_Transmit(&hi2c1,0xD0,rtc_adj,9,20);// escrita, vai enviar a matriz rtc_adj,9 bytes dessa matriz, e espera 20miligundos.HAL_I2C_Master_Transmit(&hi2c1,0xD0,rtc_init,2,20);// escrita, vai enviar a matriz rtc_init,2 bytes dessa matriz, e espera 20 milisegundos./* USER CODE END 2 *//* Infinite loop *//* USER CODE BEGIN WHILE */while (1){/* USER CODE END WHILE *//* USER CODE BEGIN 3 */// --- Leitura RTC através de I2C ---/* leitura do RTC */HAL_I2C_Master_Transmit(&hi2c1, 0xD0, &rtc_radr, 1, 20); // envio do endereço do registradorHAL_I2C_Master_Receive(&hi2c1, 0xD1, rtc_read, 8, 20); //write_lcd(0x80,0); //horawrite_lcd(((rtc_read & 0x30)>>4) + 0x30, 1);write_lcd((rtc_read & 0x0F) + 0x30, 1);write_lcd(0x83, 0); //minutowrite_lcd(((rtc_read & 0x70 )>>4) + 0x30, 1);write_lcd((rtc_read & 0x0F) + 0x30, 1);write_lcd(0x86, 0); //segundoswrite_lcd(((rtc_read & 0x70)>>4) + 0x30, 1);write_lcd((rtc_read & 0x0F) + 0x30, 1);write_lcd(0xC0, 0); //dia do mêswrite_lcd(((rtc_read & 0x30)>>4) + 0x30, 1);write_lcd((rtc_read & 0x0F) + 0x30, 1);write_lcd(0xC3, 0); // mêswrite_lcd(((rtc_read & 0x70)>>4) + 0x30, 1);write_lcd((rtc_read & 0x0F) + 0x30, 1);write_lcd(0xC6, 0); //anowrite_lcd(((rtc_read & 0x70)>>4) + 0x30, 1);write_lcd((rtc_read & 0x0F) + 0x30, 1);//--- Dia da Semana ---switch(rtc_read){case 1:write_lcd(0x8D,0);string_lcd("SUN");break;case 2:write_lcd(0x8D,0);string_lcd("MON");break;case 3:write_lcd(0x8D,0);string_lcd("TUE");break;case 4:write_lcd(0x8D,0);string_lcd("WED");break;case 5:write_lcd(0x8D,0);string_lcd("THR");break;case 6:write_lcd(0x8D,0);string_lcd("FRI");break;case 7:write_lcd(0x8D,0);string_lcd("SAT");break;}min = rtc_read;hor = rtc_read;dia = rtc_read;dm = rtc_read;mes = rtc_read;ano = rtc_read;//------------------------------------------------------------------------------------------if(!HAL_GPIO_ReadPin(SW1_GPIO_Port,SW1_Pin)){HAL_Delay(5);min=0x08;HAL_I2C_Master_Transmit(&hi2c1,0xD0,rtc_adj,9,20);// escrita, vai enviar a matriz rtc_adj,9 bytes dessa matriz, e espera 20miligundos.HAL_I2C_Master_Transmit(&hi2c1,0xD0,rtc_init,2,20);// escrita, vai enviar a matriz rtc_init,2 bytes dessa matriz, e espera 20 milisegundos.while(!HAL_GPIO_ReadPin(SW1_GPIO_Port,SW1_Pin));}}/* USER CODE END 3 */}/** System Clock Configuration*/void SystemClock_Config(void){RCC_OscInitTypeDef RCC_OscInitStruct;RCC_ClkInitTypeDef RCC_ClkInitStruct;RCC_PeriphCLKInitTypeDef PeriphClkInit;/**Configure the main internal regulator output voltage*/__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);/**Initializes the CPU, AHB and APB busses clocks*/RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI;RCC_OscInitStruct.MSIState = RCC_MSI_ON;RCC_OscInitStruct.MSICalibrationValue = 0;RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_5;RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK){_Error_Handler(__FILE__, __LINE__);}/**Initializes the CPU, AHB and APB busses clocks*/RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_MSI;RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK){_Error_Handler(__FILE__, __LINE__);}PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_I2C1;PeriphClkInit.I2c1ClockSelection = RCC_I2C1CLKSOURCE_PCLK1;if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK){_Error_Handler(__FILE__, __LINE__);}/**Configure the Systick interrupt time*/HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);/**Configure the Systick*/HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);/* SysTick_IRQn interrupt configuration */HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);}/* I2C1 init function */static void MX_I2C1_Init(void){hi2c1.Instance = I2C1;hi2c1.Init.Timing = 0x00000708;hi2c1.Init.OwnAddress1 = 0;hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;hi2c1.Init.OwnAddress2 = 0;hi2c1.Init.OwnAddress2Masks = I2C_OA2_NOMASK;hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;if (HAL_I2C_Init(&hi2c1) != HAL_OK){_Error_Handler(__FILE__, __LINE__);}/**Configure Analogue filter*/if (HAL_I2CEx_ConfigAnalogFilter(&hi2c1, I2C_ANALOGFILTER_ENABLE) != HAL_OK){_Error_Handler(__FILE__, __LINE__);}/**Configure Digital filter*/if (HAL_I2CEx_ConfigDigitalFilter(&hi2c1, 0) != HAL_OK){_Error_Handler(__FILE__, __LINE__);}}/** Configure pins as* Analog* Input* Output* EVENT_OUT* EXTI*/static void MX_GPIO_Init(void){GPIO_InitTypeDef GPIO_InitStruct;/* GPIO Ports Clock Enable */__HAL_RCC_GPIOA_CLK_ENABLE();__HAL_RCC_GPIOB_CLK_ENABLE();/*Configure GPIO pin Output Level */HAL_GPIO_WritePin(GPIOA, D7_Pin|D6_Pin|D5_Pin|D4_Pin|EN_Pin|RS_Pin, GPIO_PIN_RESET);/*Configure GPIO pins : D7_Pin D6_Pin D5_Pin D4_PinEN_Pin RS_Pin */GPIO_InitStruct.Pin = D7_Pin|D6_Pin|D5_Pin|D4_Pin|EN_Pin|RS_Pin;GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;GPIO_InitStruct.Pull = GPIO_NOPULL;GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);/*Configure GPIO pins : SW3_Pin SW2_Pin SW1_Pin */GPIO_InitStruct.Pin = SW3_Pin|SW2_Pin|SW1_Pin;GPIO_InitStruct.Mode = GPIO_MODE_INPUT;GPIO_InitStruct.Pull = GPIO_PULLUP;HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);}/* USER CODE BEGIN 4 *//* USER CODE END 4 *//*** @brief This function is executed in case of error occurrence.* @param None* @retval None*/void _Error_Handler(char * file, int line){/* USER CODE BEGIN Error_Handler_Debug *//* User can add his own implementation to report the HAL error return state */while(1){}/* USER CODE END Error_Handler_Debug */}#ifdef USE_FULL_ASSERT/*** @brief Reports the name of the source file and the source line number* where the assert_param error has occurred.* @param file: pointer to the source file name* @param line: assert_param error line source number* @retval None*/void assert_failed(uint8_t* file, uint32_t line){/* USER CODE BEGIN 6 *//* User can add his own implementation to report the file name and line number,ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) *//* USER CODE END 6 */}#endif/*** @}*//*** @}*//************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

author: Yansi
author_rank: Super Contributor
timestamp: July 14, 2017, 02:35:30 pm
content: Just a sidenote: Why would one be bothered with such old junk asi DS1307, if the STM32L0 does have full featured and well behaved RTC inside?