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

STM32串口通信测试程序

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

GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP; //推挽输出模式 Out_PP

GPIO_Init(GPIOC,&GPIO_InitStructure); //E组GPIO初始化

//--------将USART1,USART2 的TX 配置为复用推挽输出 AF_PP---------------------//

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 |GPIO_Pin_2 | GPIO_Pin_0 | GPIO_Pin_6;

GPIO_Init(GPIOA, &GPIO_InitStructure);

//--------将USART3 的TX 配置为复用推挽输出 AF_PP---------------------//

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;

GPIO_Init(GPIOB, &GPIO_InitStructure);

//--------将UART4 的TX 配置为复用推挽输出 AF_PP---------------------//

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;

GPIO_Init(GPIOC, &GPIO_InitStructure);

//--------将UART5 的RX 配置为复用浮空输入 IN_FLOATING---------------------//

GPIO_InitStructure.GPIO_Pin=GPIO_Pin_2; //管脚位置定义

//输入模式下配置输出速度无意义

//GPIO_InitStructure.GPIO_Speed=GPIO_Speed_2MHz; //输出速度2MHz

GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING; //浮空输入 IN_FLOATING

GPIO_Init(GPIOD,&GPIO_InitStructure); //C组GPIO初始化

//--------将USART1 USART2 的RX 配置为复用浮空输入 IN_FLOATING---------------------//

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 |GPIO_Pin_3;

GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING; //浮空输入 IN_FLOATING

GPIO_Init(GPIOA, &GPIO_InitStructure);

//--------将USART3 的RX 配置为复用浮空输入 IN_FLOATING---------------------//

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;

GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING; //浮空输入 IN_FLOATING

GPIO_Init(GPIOB, &GPIO_InitStructure);

//--------将UART4 的RX 配置为复用浮空输入 IN_FLOATING---------------------//

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;

GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING; //浮空输入 IN_FLOATING

GPIO_Init(GPIOC, &GPIO_InitStructure);

}

//==============================================================

void TIM2_Configuration(void)

{

TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;//定时器初始化结构

//TIM_ICInitTypeDef TIM_ICInitStructure; //通道输入初始化结构

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

TIM_TimeBaseStructure.TIM_Period = 0xffff;

TIM_TimeBaseStructure.TIM_Prescaler = 499; //时钟分频

TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; //时钟分割

TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); //基本初始化

TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;

TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

TIM_OCInitStructure.TIM_Pulse = CCR1_Val;

TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;

TIM_OC1Init(TIM2, &TIM_OCInitStructure);

TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Disable);

TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

TIM_OCInitStructure.TIM_Pulse = CCR2_Val;

TIM_OC2Init(TIM2, &TIM_OCInitStructure);

TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Disable);

//TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

//TIM_OCInitStructure.TIM_Pulse = 255;

//TIM_OC4Init(TIM2, &TIM_OCInitStructure);

TIM_Cmd(TIM2, ENABLE);

TIM_ClearFlag(TIM2, TIM_FLAG_Update);

TIM_ClearITPendingBit(TIM2, TIM_FLAG_Update); //清除TIM2等待中断更新中断标志位

TIM_ITConfig(TIM2, TIM_IT_CC1 |TIM_IT_CC2 , ENABLE);

}

//================================================

void Com5TxChar( vu8 i )

{

USART_SendData(UART5,i); //回发给PC

while(USART_GetFlagStatus(UART5, USART_FLAG_TXE)==RESET);

}

//---------------------

void Com5TxStr( vu8 *pt, vu8 length )

{

vu8 i;
for( i=0;i

{
Com5TxChar(*pt++);
}
// delay(10);

}

//----------------------------

void Com1TxChar( vu8 i )

{

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

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

{

}

// return i;

}

void Com1TxStr( vu8 *pt, vu8 length )

{

vu8 i;

for(i=0;i

Com1TxChar(*pt++);

}

// delay(10);

}

void Com2TxChar( vu8 i )

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

网站地图

Top