Untitled
unknown
plain_text
21 days ago
4.4 kB
7
Indexable
Never
UART void USART2_init(void); void USART2_write(int c); void delayMs(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_USART2_UART_Init(); /* USER CODE BEGIN 2 */ /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ USART2_init(); while (1) { /* USER CODE END WHILE */ USART2_write('K'); USART2_write('P'); USART2_write('I'); USART2_write('T'); delayMs(200); /* USER CODE BEGIN 3 */ } /* USER CODE END 3 */ } void USART2_init(void){ RCC->AHB1ENR |= (1<<0); //Enable GPIOA clock RCC->APB1ENR |= (1<<17); //Enable USART2 clock GPIOA->MODER |= (1<<5); GPIOA->MODER &= ~(1<<4); //Enable alternate function for PA2 GPIOA->AFR[0] &= ~(1<<11); GPIOA->AFR[0] |= (1<<10); GPIOA->AFR[0] |= (1<<9); GPIOA->AFR[0] |= (1<<8); //AF7 = 0111 USART2->CR1 &= ~(1<<15); //OVERSAMPLING BY 16 USART2->CR1 &= ~(1<<12); //8 DATA BITS USART2->CR1 |= (1<<3); //TE ENABLED USART2->BRR = 0X0683; USART2->CR2 &= ~(1<<13); USART2->CR2 &= ~(1<<12); //NO NEED BY DEFAULT THE REGISTER IS 0x0000 USART2->CR3 &= ~(1<<9); USART2->CR3 &= ~(1<<8); //NO NEED BY DEFAULT THE REGISTER IS 0x0000 USART2->CR1 |= (1<<13); //ENABLE USART } void USART2_write(int ch){ while(!(USART2->SR & 0x0080)){} USART2->DR = (ch & 0xFF); } void delayMs(int n){ int i; for(;n>0;n--){ for(i=0;i<2000;i++); } } **************************************************************** ADC #include "main.h" #include "stm32h7xx.h" #include <string.h> #include <stdlib.h> #include <stdio.h> char message[50]; uint16_t raw; while (1) { /* USER CODE END WHILE */ HAL_ADC_Start(&hadc1); if(HAL_ADC_PollForConversion(&hadc1,100)==HAL_OK){ raw=HAL_ADC_GetValue(&hadc1); uint32_t Temp=(raw * 3.3 / 4096) / 0.010; sprintf(message,"%ld\r\n",Temp); HAL_UART_Transmit(&huart3,(uint8_t *)message,strlen(message),10); } /* USER CODE BEGIN 3 */ } *************************************************************** VI)Steps to configure USART2 for transmitting data 1) Enable the Clock to GPIOA. 2) Enable the Clock to USART2. 3) Select the peripheral function AF7 for PA2 (USART2_TxD) pin using the GPIO_MODER and GPIO AFRL registers. 4) Set the baud rate for USART2 using USART2_BRR register. USART2->BRR = 0X0683; 5) Configure the CR1 (Control 1) register for oversampling rate, character size (8-bit or 9-bit) and enabling transmit (TE). 6) Configure the CR2 (Control 2) register for number of stop bit(s) and so on. 7) Configure the CR3 (Control 3) register for no hardware flow control and so on. 8) Enable USART2 after configuration complete. 9) Wait until the TXE (Transmit Empty) bit of the USART_SR register is set. 10) Write a byte to DR Register to be transmitted. 11) To transfer the next character, go to step 9. **************************************************************************************** VI)Steps to configure USART for receiving data 1) Enable the Clock to GPIOΑ. 2) Enable the Clock to USART2. 3) Select the peripheral function AF7 for PA3 (USART2_RxD) pin using GPIO_MODER registers and GPIO_AFRL registers 4) Set the baud rate for USART2 using USART2_BRR register. 5) Configure the CR1 (Control 1) register for oversampling rate, character size (8-bit or 9bit) and enabling receive (RE). 6) Configure the CR2 (Control 2) register for number of stop bit(s) and so on. 7) Configure the CR3 (Control 3) register for no hardware flow control and so on. 8) Enable USART2 after configuration complete. 9) Wait until the RXNE (Receive Not Empty) bit of the USART_SR register is set. 10) Read a byte from USART2_DR register that was received. 11) To receive the next character, go to step 9.
Leave a Comment