微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > STM32串口通信测试程序

STM32串口通信测试程序

时间:11-28 来源:互联网 点击:

{

USART_SendData(USART2,(u8) i); //回发给PC

while(USART_GetFlagStatus(USART2, USART_FLAG_TXE)==RESET)

{

}

// return i;

}

void Com2TxStr( vu8 *pt, vu8 length )

{

vu8 i;

for(i=0;i

Com2TxChar(*pt++);

}

// delay(10);

}

void Com3TxChar( vu8 i )

{

USART_SendData(USART3,(u8) i); //回发给PC

while(USART_GetFlagStatus(USART3, USART_FLAG_TXE)==RESET)

{

}

// return i;

}

void Com3TxStr( vu8 *pt, vu8 length )

{

vu8 i;

for(i=0;i

Com3TxChar(*pt++);

}

// delay(10);

}

void Com4TxChar( vu8 i )

{

USART_SendData(UART4,(u8) i); //回发给PC

while(USART_GetFlagStatus(UART4, USART_FLAG_TXE)==RESET)

{

}

// return i;

}

void Com4TxStr( vu8 *pt, vu8 length )

{

vu8 i;

for(i=0;i

Com4TxChar(*pt++);

}

// delay(10);

}

void TIM2_IRQHandler(void)

{

vu8 i;

if (TIM_GetITStatus(TIM2, TIM_IT_CC1) != RESET)

{

TIM_ClearITPendingBit(TIM2, TIM_IT_CC1);

cnt1++;

cnt2++;

if(cnt1>=500)

{

// polling=1;

cnt1=0;

}

if(cnt2>=nt2) //RUNLED,WDI

{

GPIO_WriteBit(GPIOA, GPIO_Pin_0, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOA, GPIO_Pin_0)));

if(Alert_flag==1)

{

GPIO_WriteBit(GPIOA, GPIO_Pin_6, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOA, GPIO_Pin_6)));

}

cnt2=0;

}

Time20msCnt++; // 秒级硬件延时

if( Time20msCnt>=50 && T1sCnt ){

Time20msCnt=0;

T1sCnt--;

}

if(T20msCnt){ //20ms单位延时

T20msCnt--;

}

capture1 = TIM_GetCapture1(TIM2);

TIM_SetCompare1(TIM2, capture1 + CCR1_Val );

}

if (TIM_GetITStatus(TIM2, TIM_IT_CC2) != RESET)

{

TIM_ClearITPendingBit(TIM2, TIM_IT_CC2);

capture2 = TIM_GetCapture2(TIM2);

TIM_SetCompare2(TIM2, capture2 + CCR2_Val );

}

}

//********串口COM5配置*****************************

void UART5_Configuration(void)

{

USART_InitTypeDef USART_InitStructure; //串口设置恢复默认参数

USART_InitStructure.USART_BaudRate = 115200; //波特率115200

USART_InitStructure.USART_WordLength = USART_WordLength_8b; //字长8位

USART_InitStructure.USART_StopBits = USART_StopBits_1; //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; //打开Rx接收和Tx发送功能

//USART_Init(USART1, &USART_InitStructure);

USART_Init(UART5, &USART_InitStructure); //初始化

// USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);

// USART_ITConfig(USART1, USART_IT_TXE, ENABLE);

USART_ITConfig(UART5, USART_IT_RXNE, ENABLE); // 若接收数据寄存器满,则产生中断

//USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);

// USART_ITConfig(UART5, USART_IT_TXE, ENABLE);

USART_Cmd(UART5, ENABLE); //启动串口

//USART_Cmd(USART1, ENABLE);

//-----如下语句解决第1个字节无法正确发送出去的问题-----//

USART_ClearFlag(UART5, USART_FLAG_TC); // 清标志

//USART_ClearFlag(USART1, USART_FLAG_TC);

}

//************串口COM1~COM44配置*********************************
void USART_Configuration(void)

{

USART_InitTypeDef USART_InitStructure; //串口设置恢复默认参数

USART_InitStructure.USART_BaudRate = 1200; //波特率1200

USART_InitStructure.USART_WordLength = USART_WordLength_8b; //字长8位

USART_InitStructure.USART_StopBits = USART_StopBits_1; //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; //打开Rx接收和Tx发送功能

USART_Init(USART1, &USART_InitStructure);

USART_Init(USART2, &USART_InitStructure);

USART_Init(USART3, &USART_InitStructure);

USART_Init(UART4 , &USART_InitStructure);

USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);// 若接收数据寄存器满,则产生中断

USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);

USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);

USART_ITConfig(UART4 , USART_IT_RXNE, ENABLE);

USART_Cmd(USART1, ENABLE); //启动串口

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top