Serial port control RGB light program

Purpose:

  1. Send data (characters) to the serial port through the host computer;
  2. STM32 returns the data to the host computer intact , and generates corresponding interrupts to operate according to the received information. (1-red led 2 –bule led...);


source code

bsp_usart.c

#include "bsp_usart.h"

static void NVIC_Configuration(void)
{
  NVIC_InitTypeDef NVIC_InitStructure;

  /* Nested vectored interrupt controller group selection */
  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);

  /* configure USART as interrupt source */
  NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
  /* steal priority */
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  /* child priority */
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  /* enable interrupts */
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  /* Initialize configuration NVIC */
  NVIC_Init(&NVIC_InitStructure);
}

void USART_Config(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;

	// Turn on the clock of the serial port GPIO
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

	// Turn on the clock of the serial peripheral
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);

	// Configure the GPIO of USART Tx to push-pull multiplexing mode
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);

        // Configure the GPIO of USART Rx as floating input mode
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
	GPIO_Init(GPIOA, &GPIO_InitStructure);

	// Configure the working parameters of the serial port 
	// Configure the baud rate
	USART_InitStructure.USART_BaudRate = 115200;
	// Configure pin data word length
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;
	// configure stop bits
	USART_InitStructure.USART_StopBits = USART_StopBits_1;
	// configure check digit
	USART_InitStructure.USART_Parity = USART_Parity_No ;
	// configure hardware flow control
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	// Configure the working mode, send and receive together
	USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
	// Complete the initial configuration of the serial port
	USART_Init(USART1, &USART_InitStructure);

	// Serial port interrupt priority configuration
	NVIC_Configuration();

	// Enable serial port receive interrupt
       USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);

	// enable serial port
	USART_Cmd(USART1, ENABLE);
}

/* Send a byte 8 bits*/ 
/*STM32 USART can only send 8 bits at a time*/ 
void Usart_SendByte(USART_TypeDef* pUSARTx, uint8_t data)
{
	USART_SendData(pUSARTx, data);
	while( USART_GetFlagStatus(pUSARTx, USART_FLAG_TXE) == RESET );
}

/* Send two bytes of data* 16 bits*/ 
/*Send in two pieces*/ 
void Usart_SendHalfWord(USART_TypeDef* pUSARTx, uint16_t data)
{
	uint8_t temp_h,temp_l; /*High and low 8 bits*/

	temp_h = (data&0xff00) >> 8 ;
	temp_l = data&0xff;
	/*Send the upper 8 bits first*/
	USART_SendData(pUSARTx, temp_h);
	while ( USART_GetFlagStatus(pUSARTx, USART_FLAG_TXE) == RESET );
	 /*Send lower 8 bits*/
	USART_SendData(pUSARTx, temp_l);
	while( USART_GetFlagStatus(pUSARTx, USART_FLAG_TXE) == RESET );
}

/* Send an array of 8-bit data*/ 
void Usart_SendArray(USART_TypeDef* pUSARTx, uint8_t *array, uint8_t num)
{
	uint8_t i;
	for( i=0; i<num; i++ )
  {
		Usart_SendByte(pUSARTx, array[i]);
	}
	//Sending 8 bits is for reading, TXE, greater than 8 bits to judge TC 
	while ( USART_GetFlagStatus(pUSARTx, USART_FLAG_TC) == RESET );
}

/* Send string */ 
void Usart_SendStr(USART_TypeDef* pUSARTx, uint8_t *str)
{
	uint8_t i=0;
	do
  {
		Usart_SendByte(pUSARTx, *(str+i));
		i++;
	}while(*(str+i) != '\0');
	while( USART_GetFlagStatus(pUSARTx, USART_FLAG_TC) == RESET );
}

///Redirect the c library function printf to the serial port. After redirection, you can use the printf function 
int fputc( int ch, FILE *f)
{
		/* Send a byte of data to the serial port */
		USART_SendData(USART1, (uint8_t) ch);

		/* Wait for the sending to finish*/ 
		while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);

		return (ch);
}

/ / Redirect the c library function scanf to the serial port, rewrite the backward can use scanf, getchar and other functions 
int fgetc(FILE *f)
{
		/* Wait for serial input data*/ 
		while (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET);

		return (int)USART_ReceiveData(USART1);
}

bsp_usart.h

#ifndef __BSP_USART_H
#define __BSP_USART_H

#include "stm32f10x.h"
#include <stdio.h>

void USART_Config(void);
void Usart_SendByte(USART_TypeDef* pUSARTx, uint8_t data);
void Usart_SendHalfWord(USART_TypeDef* pUSARTx, uint16_t data);
void Usart_SendArray(USART_TypeDef* pUSARTx, uint8_t *array,uint8_t num);
void Usart_SendStr(USART_TypeDef* pUSARTx, uint8_t *str);



#endif


main.c

#include "stm32f10x.h"
#include "bsp_led.h"
#include "bsp_usart.h"



/**
  * @brief main function
  * @param none
  * @retval None
  */
int main(void)
{
	uint8_t ch;
	USART_Config(); //Initialize USART1
	LED_GPIO_Config(); //Initialize GPIO

	printf ( " This is a program to control RGB lights by serial port\n " ); //Send a string to the upper computer

	while (1)
	{
		ch = getchar (); //Get a character sent by the host computer, and execute the interrupt service function printf ( " ch=%c\n ",ch ); //Return to the host computer every time an interrupt is received
	        

		switch(ch)
   {
			case '1': LED_RED;
				break;

			case '2': LED_GREEN;
			  break;

			case '3': LED_BLUE;
			  break;

			default: LED_RGBOFF;
		    break;
	 }
	}
}


stm32f10x_it.c

// Serial port interrupt service function 
void USART1_IRQHandler( void )
{
  uint8_t ucTemp;
	if(USART_GetITStatus(USART1,USART_IT_RXNE)!=RESET)
	{
		ucTemp = USART_ReceiveData(USART1);
    USART_SendData(USART1,ucTemp);
	}
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325092746&siteId=291194637