新手上路
- 积分
- 40
- 金钱
- 40
- 注册时间
- 2016-6-6
- 在线时间
- 49 小时
|
1金钱
#include "stm32f2xx.h"
/************************************************
ALIENTEK 战舰STM32F103开发板实验0
工程模板
注意,这是手册中的新建工程章节使用的main文件
技术支持:www.openedv.com
淘宝店铺:http://eboard.taobao.com
关注微信公众平台微信号:"正点原子",免费获取STM32资料。
广州市星翼电子科技有限公司
作者:正点原子 @ALIENTEK
************************************************/
void My_USART1_Init(void)
{
GPIO_InitTypeDef GPIO_InitStrue;
USART_InitTypeDef USART_InitStrue;
NVIC_InitTypeDef NVIC_InitStrue;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC,ENABLE);//①
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4,ENABLE);
//RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
GPIO_InitStrue.GPIO_OType=GPIO_OType_PP;
GPIO_InitStrue.GPIO_Mode=GPIO_Mode_AF;
GPIO_InitStrue.GPIO_Pin=GPIO_Pin_10;
GPIO_InitStrue.GPIO_Speed=GPIO_Speed_50MHz;
// GPIO_InitStrue.GPIO_PuPd=GPIO_PuPd_NOPULL;
GPIO_InitStrue.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOC,&GPIO_InitStrue);//②
GPIO_InitStrue.GPIO_OType=GPIO_OType_OD;
//GPIO_InitStrue.GPIO_Mode=GPIO_Mode_AF;
//GPIO_InitStrue.GPIO_Mode=GPIO_Mode_IN;
GPIO_InitStrue.GPIO_Pin=GPIO_Pin_11;
GPIO_InitStrue.GPIO_Speed=GPIO_Speed_50MHz;
//GPIO_InitStrue.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStrue.GPIO_PuPd=GPIO_PuPd_NOPULL;
GPIO_Init(GPIOC,&GPIO_InitStrue);//②
USART_InitStrue.USART_BaudRate=115200;
USART_InitStrue.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
USART_InitStrue.USART_Mode=USART_Mode_Tx|USART_Mode_Rx;
USART_InitStrue.USART_Parity=USART_Parity_No;
USART_InitStrue.USART_StopBits=USART_StopBits_1;
USART_InitStrue.USART_WordLength=USART_WordLength_8b;
USART_Init(UART4,&USART_InitStrue);//③
USART_Cmd(UART4,ENABLE);//使能串口4
USART_ITConfig(UART4,USART_IT_RXNE,ENABLE);//开启接收中断
NVIC_InitStrue.NVIC_IRQChannel=UART4_IRQn;
NVIC_InitStrue.NVIC_IRQChannelCmd=ENABLE;
NVIC_InitStrue.NVIC_IRQChannelPreemptionPriority=1;
NVIC_InitStrue.NVIC_IRQChannelSubPriority=1;
NVIC_Init(&NVIC_InitStrue);
}
void USART1_IRQHandler(void)
{
u8 res;
if(USART_GetITStatus(UART4,USART_IT_RXNE))
{
res= USART_ReceiveData(UART4);
USART_SendData(UART4,res);
}
}
int main(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
My_USART1_Init();
while(1);
}
|
|