UART
UART
UART
Câu 1:
Bài 1: Xây dựng các hàm gửi dữ liệu theo các nguyên mẫu hàm sau:
• UART_print_int(UART_HandleTypeDef *huart, int data);
• UART_print_string(UART_HandleTypeDef *huart, char* s);
• UART_print_float(UART_HandleTypeDef *huart, float data);
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
TIM_HandleTypeDef htim1;
UART_HandleTypeDef huart1;
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
if(flag_uart == 1)
{
// doc gia tri cam bien LM35
HAL_ADC_Start(&hadc1);
HAL_ADC_PollForConversion(&hadc1,1000);
value = HAL_ADC_GetValue(&hadc1);
t = (value/4096.0)*3.3*100.0;
UART_printf_int(&huart1, t);
flag_uart = 0;
HAL_TIM_Base_Start_IT(&htim1);
}
}
/* USER CODE END 3 */
}
Bài 3: Viết chương trình sử dụng LCD như một màn hình hiển thị văn bản gõ
được từ bàn phím
(gửi dữ liệu qua cửa sổ phần mềm Hercules).
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2022 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
flag_lcd = 0;
memset((char*) UART_Buffer, '\0', 100); // xoa buffer
index_buf = 0;
}
LCD_setCursor(0,2);
LCD_printf((char*) UART_Buffer);
}
/* USER CODE END 3 */
}
I2C:
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2022 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
UART_HandleTypeDef huart1;
DS1307.date = RTC_BCD2DEC(DS1307.I2C_Buffer[4]);
DS1307.month = RTC_BCD2DEC(DS1307.I2C_Buffer[5]);
DS1307.year = RTC_BCD2DEC(DS1307.I2C_Buffer[6]);
DS1307.sec = RTC_BCD2DEC(DS1307.I2C_Buffer[0]);
DS1307.min = RTC_BCD2DEC(DS1307.I2C_Buffer[1]);
DS1307.hour = RTC_BCD2DEC(DS1307.I2C_Buffer[2]);
}
void RTC_SetTime(uint8_t hour, uint8_t min, uint8_t sec, uint8_t date, uint8_t month, uint8_t
year)
{
DS1307.I2C_Buffer[0] = 0x00;
DS1307.I2C_Buffer[1] = RTC_DEC2BCD( sec );
DS1307.I2C_Buffer[2] = RTC_DEC2BCD( min );
DS1307.I2C_Buffer[3] = RTC_DEC2BCD( hour );
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
RTC_GetTime();
LCD_setCursor(0,4);
LCD_printf("%02d/%02d/%02d",DS1307.date, DS1307.month,DS1307.year);
LCD_setCursor(1,4);
LCD_printf("%02d:%02d:%02d",DS1307.hour, DS1307.min,DS1307.sec);
HAL_Delay(1000);
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}