stm32 USART 发送字符串程序(经测试)

/* Includes ------------------------------------------------------------------*/
#include "stm32f10x_conf.h"
#include "stm32f10x.h"
#include "stm32_eval.h"
#include


//#define unsigned char uchar;

unsigned char TxBuffer5[10] ;
unsigned int i;
unsigned int flag;

//-------------------------------------------GPIO初始化-----------------------------------------//
void RCC_Config(void)//初始化时钟
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1| RCC_APB2Periph_GPIOA
| RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
}

void GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 ;//| GPIO_Pin_10 | GPIO_Pin_9
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);

/* Configure USART1 Tx (PA.09) as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USART1 Rx (PA.10) as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);

/* Configure USART2 Tx (PA.02) as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USART2 Rx (PA.03) as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);

}

void GPIO_UART_Config(void)//引脚初始化
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Configure USART1 Tx (PA.09) as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USART1 Rx (PA.10) as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);

/* Configure USART2 Tx (PA.02) as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USART2 Rx (PA.03) as input flo

ating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}


void USART_Config(void) //串口初始化
{
USART_InitTypeDef USART_InitStructure;
USART_https://www.360docs.net/doc/131934535.html,ART_BaudRate = 4800;
USART_https://www.360docs.net/doc/131934535.html,ART_WordLength = USART_WordLength_8b;
USART_https://www.360docs.net/doc/131934535.html,ART_StopBits = USART_StopBits_1;
USART_https://www.360docs.net/doc/131934535.html,ART_Parity = USART_Parity_No;
USART_https://www.360docs.net/doc/131934535.html,ART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_https://www.360docs.net/doc/131934535.html,ART_Mode = USART_Mode_Rx | USART_Mode_Tx;
/* Configure USART1 */
USART_Init(USART1, &USART_InitStructure);
/* Enable USART1 Receive and Transmit interrupts */
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_ITConfig(USART1, USART_IT_TXE, ENABLE);


/* Enable the USART1 */
USART_Cmd(USART1, ENABLE);
//USART_ClearFlag(USART1, USART_FLAG_TC);

/* Configure USART2 */
USART_Init(USART2, &USART_InitStructure);
/* Enable USART2 Receive and Transmit interrupts */
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
USART_ITConfig(USART2, USART_IT_TXE, ENABLE);

/* Enable the USART1 */
USART_Cmd(USART2, ENABLE);
//USART_ClearFlag(USART2, USART_FLAG_TC);
}




void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
#ifdef VECT_TAB_RAM
/* Set the Vector Table base location at 0x20000000 */
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else /* VECT_TAB_FLASH */
/* Set the Vector Table base location at 0x08000000 */
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif
//设置NVIC优先级分组为Group2:0-3抢占式优先级,0-3的响应式优先级
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
/* enabling interrupt */
NVIC_InitStructure.NVIC_IRQChannel=USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}

unsigned char RxBuffer[100];
unsigned char RxCounter;
unsigned char flag_send=0;
unsigned char ii,j;

void USART2_IRQHandler(void)
{
u16 usarnum;

/*
if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
{
//Disable USART3 RXNE Interrupt
USART_ITConfig(USART2, USART_IT_RXNE, DISABLE);
usarnum=USART_ReceiveData(USART2);
USART_SendData(USART2, usarnum);
while (USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
USART_ITConfig( USART2,USART_IT_RXNE, ENABLE);
}
*/

if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
{
//Disable USART3 RXNE Interrupt
USART_ITConfig(USART2, USART_IT_RXNE, DISABLE);
//usarnum=USART_

ReceiveData(USART2);
RxBuffer[ii]=USART_ReceiveData(USART2);
ii++;
if(ii==4) flag_send=1;
//USART_SendData(USART2, usarnum);
//while (USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
USART_ITConfig( USART2,USART_IT_RXNE, ENABLE);
}


if(flag_send==1)
{
for(j=0;j<4;j++)
{
USART_SendData(USART2,RxBuffer[j]);
while (USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
}
flag_send=0;
ii=0;
}


}

int main(void)
{
u8 i,j;

RCC_Config(); //时钟初始化,USART1,USART2时钟配置
GPIO_Config(); //GPIO串口初始化,端口4、5为输出模式,初始化SART1,USART2端口
USART_Config(); //UART1,UART2初始化
NVIC_Configuration();
I2C_Configuration();
CDCE_ConfigGenericRegester();


//uint16_t Rec[20];
//unsigned char rec1[20];

//Uart2_PutString(TxBuf, 12); //定义字符变量

USART_SendData(USART2, *TxBuf1);


while(1)
{ }
}

相关文档
最新文档