stm32f030 UART Applications

Source: Internet
Author: User

= = File uart.h============================================================ #ifndef _uart_h_#define _UART_H_#defineV _uart_rx0//Receive status #definev_uart_tx1//send status #definev_rx_num11//receive data byte long #definev_tx_num8//Send data byte length #definevt_ CLRRXINDEX5//50MS received no data purge receive index//==================================================extern uint8 Rx_Buf[V_RX_NUM];  extern uint8 Tx_buf[v_tx_num]; extern uint8 rx_index; extern uint8 tx_index; extern void Uart_putc (unsigned char c); extern void Uart_puts (char *s), extern void Usart_init (void), extern void Usart_gpio_init (void), extern void Usart_param_init (U Int32 invalue), extern void Init_uart_senddat (Uint8 invalue), extern uint8 Calcatechecksumuart (uint8 *a,uint8 datalong); #endif//== file uart.c============================================================ #include "global.h" #include " Uart.h "#define INIT_UART_CALLUINT8 rx_buf[v_rx_num]; uint8 tx_buf[v_tx_num]; uint8 rx_index = 0; Uint8 tx_index = 0; #ifdef init_uart_call//*************************************//function Name: Usart_gpio_init//function: UART GPIO initialization//Ingress parameters: none//Export parameters: none//Note://***************************************void usart_gpio_init ( void) {Gpio_inittypedef gpio_initstructure; Rcc_ahbperiphclockcmd (Rcc_ahbperiph_gpiob, ENABLE);//-----PB6 TX------------------PB7 RX-------------Gpio_ Initstructure.gpio_pin = gpio_pin_6|                 gpio_pin_7; Gpio_initstructure.gpio_mode = GPIO_MODE_AF; Gpio_initstructure.gpio_otype = gpio_otype_pp; GPIO_INITSTRUCTURE.GPIO_PUPD = gpio_pupd_up; Gpio_initstructure.gpio_speed = Gpio_speed_50mhz; Gpio_init (Gpiob, &gpio_initstructure); Gpio_pinafconfig (Gpiob, Gpio_pinsource6, gpio_af_0); Gpio_pinafconfig (Gpiob, Gpio_pinsource7, gpio_af_0);}  ====== serial port Send IO status initialization ======================================void usart_param_init (UInt32 invalue) {gpio_inittypedef Gpio_initstructure; Rcc_ahbperiphclockcmd (Rcc_ahbperiph_gpiob, ENABLE);//-----PB7 RX-------------received as UART Gpio_initstructure.gpio_pin =                 gpio_pin_7; Gpio_initstructure.gpio_mode = GPIO_MODE_AF; Gpio_initstruCture. Gpio_otype = gpio_otype_pp; GPIO_INITSTRUCTURE.GPIO_PUPD = gpio_pupd_up; Gpio_initstructure.gpio_speed = Gpio_speed_50mhz; Gpio_init (Gpiob, &gpio_initstructure);  Gpio_pinafconfig (Gpiob, Gpio_pinsource7, gpio_af_0); if (invalue = V_UART_RX)//Receive status {//-----PB6 TX-------------sent as input                 Gpio_initstructure.gpio_pin = Gpio_pin_6; Gpio_initstructure.gpio_mode = gpio_mode_in; Gpio_initstructure.gpio_otype = gpio_otype_pp; Gpio_otype_od;//gpio_initstructure.gpio_pupd = gpio_pupd_up;//gpio_pupd_down;//; Gpio_initstructure.gpio_speed = Gpio_speed_50mhz; Gpio_init (Gpiob, &gpio_initstructure); Usart_clearitpendingbit (USART1,USART_IT_RXNE); Clear receive Interrupt flag usart_itconfig (usart1,usart_it_rxne,enable);//Receive interrupt enable Usart_itconfig (usart1,usart_it_tc,disable); Send interrupt does not enable Usart_it_txe}else if (Invalue = = v_uart_tx)//Send status {//-----PB6 RX-------------sent as UART Gpio_                 Initstructure.gpio_pin = Gpio_pin_6; Gpio_initstructure.gpio_mode = GPIO_MODE_AF; Gpio_initstructure.gpio_otype = Gpio_oTYPE_PP; GPIO_INITSTRUCTURE.GPIO_PUPD = gpio_pupd_up; Gpio_initstructure.gpio_speed = Gpio_speed_50mhz; Gpio_init (Gpiob, &gpio_initstructure); Gpio_pinafconfig (Gpiob, Gpio_pinsource6, gpio_af_0); Usart_itconfig (usart1,usart_it_rxne,disable);//Receive interrupt does not enable Usart_clearitpendingbit (USART1,USART_IT_TC);// Clear Send Interrupt flag usart_itconfig (usart1,usart_it_tc,enable); Send interrupt enable usart_it_txe}}//*************************************//function name: usart_init//function: UART initialization//inlet parameter: none//Export parameter: none// Remark://***************************************void usart_init (void) {usart_inittypedef usart_initstructure; Nvic_inittypedef nvic_initstructure; Rcc_apb2periphclockcmd (Rcc_apb2periph_usart1, ENABLE); Nvic_initstructure.nvic_irqchannel = USART1_IRQN; Nvic_initstructure.nvic_irqchannelpriority = 2; Nvic_initstructure.nvic_irqchannelcmd = ENABLE;   Nvic_init (&nvic_initstructure); Usart_initstructure.usart_baudrate = 4800;//9600usart_initstructure.usart_wordlength = USART_WordLength_8b; Usart_initstructure.usart_stopbits = Usart_stopbits_1; usart_initstructure.usart_parity = Usart_parity_no; Usart_initstructure.usart_hardwareflowcontrol = Usart_hardwareflowcontrol_none; Usart_initstructure.usart_mode = Usart_mode_rx | Usart_mode_tx; Usart_init (USART1, &usart_initstructure); Usart_cmd (USART1, ENABLE); Usart_clearitpendingbit (USART1,USART_IT_RXNE); Clear receive Interrupt flag usart_itconfig (usart1,usart_it_rxne,enable);//Receive interrupt enable Usart_clearitpendingbit (USART1,USART_IT_TC);// Clear Send Interrupt flag//usart_itconfig (usart1,usart_it_tc,enable); Send interrupt enable usart_it_txe}//*************************************//function name: uart_putc//function: UART send one character//inlet parameter: none//Export parameter: none// Note://***************************************void uart_putc (unsigned char c) {while (!) ( (USART1-&GT;ISR) & (1<<7)); Usart1->tdr=c;} Function Name: uart_puts//function: UART sends a string//entry parameter: none//Export parameter: none//Note://*************** void Uart_puts (char *s) {while (*s) UART_PUTC (*s++);} Function Name: uart_puts//function function: UART sends a string//entry parameterNumber: no//Export parameters: none//Note://***************************************void init_uart_senddat (uint8 invalue) {tx_buf[0] = 0xc3; TX_BUF[1] = 0x00; TX_BUF[2] = 0x00; TX_BUF[3] = 0x00; TX_BUF[4] = 0x00; TX_BUF[5] = 0x00; TX_BUF[6] = Invalue; Tx_buf[v_tx_num-1] = Calcatechecksumuart (&tx_buf[0], (v_tx_num-1)); Usart_param_init (V_UART_TX); Initialize port when sending data Tx_index = 0; UART_PUTC (Tx_buf[tx_index]); Tx_index + +;} Function Name: uart_puts//function: UART sends a string//entry parameter: none//Export parameter: none//Note://*************** Uint8 Calcatechecksumuart (uint8 *a,uint8 datalong) {uint8 i; uint8 tmp=0; for (i=0;i< datalong;i++) {Tmp + = A[i];} TMP = (tmp^0xff) +1; return tmp;} #endif//init_uart_call

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

stm32f030 UART Applications

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.