Chinaunix首页 | 论坛 | 博客
  • 博客访问: 308164
  • 博文数量: 78
  • 博客积分: 773
  • 博客等级: 军士长
  • 技术积分: 779
  • 用 户 组: 普通用户
  • 注册时间: 2011-09-24 23:52
个人简介

victory for prepared

文章分类

全部博文(78)

文章存档

2023年(1)

2022年(2)

2021年(1)

2018年(8)

2017年(3)

2016年(20)

2015年(12)

2013年(5)

2012年(25)

2011年(1)

我的朋友

分类: 其他平台

2018-07-31 22:54:54

1,搞清楚流程
2,涉及的结构起有时钟,GPIO,USART,NVIC
3,先打开串口所在总线时钟, GPIO时钟
4,GPIO参数设置,此处需要注意的是使用AF模式时,需要将管脚config一下
5,USART设置
6,NVIC设置
7,中断开关打开
8,中断程序(另外一个单独的程序),写一些中断需要用到的一些操作。

void chuankou()
{
USART_ClockInitTypeDef USART_ClockInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;

USART_ClockInitStructure.USART_Clock = USART_Clock_Disable;
USART_ClockInitStructure.USART_CPHA = USART_CPHA_1Edge;
USART_ClockInitStructure.USART_CPOL = USART_CPOL_High;
USART_ClockInitStructure.USART_LastBit = USART_LastBit_Enable;

USART_ClockInit(USART1, &USART_ClockInitStructure);

RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);

RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);

//USART1_TX PA.9
/* Configure AF mux to connect USART1 to PA9 */
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_7);

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GPIOA, &GPIO_InitStructure);

//USART1_RX PA.10
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_7);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; //?

GPIO_Init(GPIOA, &GPIO_InitStructure);

USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;

NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;

NVIC_Init(&NVIC_InitStructure);

USART_Init(USART1, &USART_InitStructure);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //开启中断
USART_Cmd(USART1, ENABLE);

}


阅读(3922) | 评论(0) | 转发(0) |
0

上一篇:stm32 PWM产生

下一篇:步进电机

给主人留下些什么吧!~~