Trường Đại Học Khoa Học Tự Nhiên – DHQG
Họ và tên: Trần Kiều Hảo
MSSV: 20207031
Môn: Vi điều Khiển
Lớp: DTVT1-CLC
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);
/* 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"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "string.h"
#include "stdio.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
UART_HandleTypeDef huart1;
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART1_UART_Init(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
void UART_printf_string(UART_HandleTypeDef *huart, char *s)
{
HAL_UART_Transmit(huart, (uint8_t*) s, strlen(s), 1000);
}
void UART_printf_int(UART_HandleTypeDef *huart, int data)
{
char str[100];
sprintf(str, "so int la: %d\r\n", data);
UART_printf_string(huart, str);
}
void UART_printf_float(UART_HandleTypeDef *huart, float data)
{
char str[100];
sprintf(str, "so float la: %.3f\r\n", data);
UART_printf_string(huart, str);
}
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
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_USART1_UART_Init();
/* USER CODE BEGIN 2 */
UART_printf_string(&huart1,"TRANKIEUHAO\r\n");
UART_printf_int(&huart1, 10);
UART_printf_float(&huart1, 15.025);
HAL_Delay(1000);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
Bài 2: Đọc nhiệt độ từ LM35 và gửi ra UART COM port 3 giây/lần.
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "string.h"
#include "stdio.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
ADC_HandleTypeDef hadc1;
TIM_HandleTypeDef htim1;
UART_HandleTypeDef huart1;
/* USER CODE BEGIN PV */
uint8_t flag_uart = 1;
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_ADC1_Init(void);
static void MX_USART1_UART_Init(void);
static void MX_TIM1_Init(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
void UART_printf_string(UART_HandleTypeDef *huart, char *s)
{
HAL_UART_Transmit(huart, (uint8_t*) s, strlen(s), 1000);
}
void UART_printf_int(UART_HandleTypeDef *huart, float data)
{
char str[100];
sprintf(str, "Gia tri nhiet do la: %.3f\r\n", data);
UART_printf_string(huart, str);
}
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if(htim->Instance == TIM1)
{
flag_uart = 1;
}
}
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
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_ADC1_Init();
MX_USART1_UART_Init();
MX_TIM1_Init();
/* USER CODE BEGIN 2 */
uint16_t value;
HAL_Delay(1000);
HAL_ADCEx_Calibration_Start(&hadc1);
float t;
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
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"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <stdlib.h>
#include "lcd.h"
#include <string.h>
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
UART_HandleTypeDef huart1;
/* USER CODE BEGIN PV */
uint8_t tempRx;
uint8_t UART_Buffer[100];
uint8_t index_buf;
uint8_t flag_lcd;
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART1_UART_Init(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if(huart->Instance == USART1)
{
if(tempRx == '@')
{
flag_lcd = 1; // cho phep gui data len lcd
tempRx = '\0';
}
UART_Buffer[index_buf] = tempRx;
index_buf ++;
HAL_UART_Receive_IT(&huart1, &tempRx, 1);
}
}
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
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_USART1_UART_Init();
/* USER CODE BEGIN 2 */
LCD_init();
LCD_clear();
HAL_UART_Receive_IT(&huart1, &tempRx, 1);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
if(flag_lcd == 1)
{
// HAL_UART_Transmit(&huart1, UART_Buffer,
strlen((char*) UART_Buffer), 1000);
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"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <stdio.h>
#include <lcd.h>
/* USER CODE END Includes */
/* Private variables ---------------------------------------------------------*/
I2C_HandleTypeDef hi2c1;
UART_HandleTypeDef huart1;
/* USER CODE BEGIN PV */
#define DS1307_ADDRESS (0xD1)
typedef struct{
uint8_t sec;
uint8_t min;
uint8_t hour;
uint8_t day;
uint8_t date;
uint8_t month;
uint8_t year;
uint8_t I2C_Buffer[8];
}DS1307_t;
DS1307_t DS1307;
/* 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 */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
uint8_t RTC_BCD2DEC(uint8_t c)
{
return (c>>4)*10 + (c&0x0f);
}
uint8_t RTC_DEC2BCD(uint8_t c)
{
return (c/10)<<4|(c%10);
}
void I2C_WriteBuffer(I2C_HandleTypeDef *hi, uint8_t DEV_ADDR, uint8_t sizebuf)
{
while(HAL_I2C_Master_Transmit(hi, DEV_ADDR, DS1307.I2C_Buffer, sizebuf, 1000));
}
void I2C_ReadBuffer(I2C_HandleTypeDef *hi, uint8_t DEV_ADDR, uint8_t sizebuf)
{
while(HAL_I2C_Master_Receive(hi, DEV_ADDR, DS1307.I2C_Buffer, sizebuf, 1000));
}
void RTC_GetTime(void)
{
DS1307.I2C_Buffer[0] = 0x00;
I2C_WriteBuffer(&hi2c1, DS1307_ADDRESS, 1);
while(HAL_I2C_GetState(&hi2c1) != HAL_I2C_STATE_READY);
I2C_ReadBuffer(&hi2c1, DS1307_ADDRESS, 7);
[Link] = RTC_BCD2DEC(DS1307.I2C_Buffer[3]);
[Link] = RTC_BCD2DEC(DS1307.I2C_Buffer[4]);
[Link] = RTC_BCD2DEC(DS1307.I2C_Buffer[5]);
[Link] = RTC_BCD2DEC(DS1307.I2C_Buffer[6]);
[Link] = RTC_BCD2DEC(DS1307.I2C_Buffer[0]);
[Link] = RTC_BCD2DEC(DS1307.I2C_Buffer[1]);
[Link] = 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 );
DS1307.I2C_Buffer[5] = RTC_DEC2BCD( date );
DS1307.I2C_Buffer[6] = RTC_DEC2BCD( month );
DS1307.I2C_Buffer[7] = RTC_DEC2BCD( year );
I2C_WriteBuffer(&hi2c1, DS1307_ADDRESS, 8);
HAL_Delay(100);
}
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
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();
MX_USART1_UART_Init();
/* USER CODE BEGIN 2 */
HAL_Delay(1000);
LCD_init();
LCD_clear();
RTC_SetTime(05,19,59,11,05,22);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
RTC_GetTime();
LCD_setCursor(0,4);
LCD_printf("%02d/%02d/%02d",[Link], [Link],[Link]);
LCD_setCursor(1,4);
LCD_printf("%02d:%02d:%02d",[Link], [Link],[Link]);
HAL_Delay(1000);
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}