初级会员

- 积分
- 159
- 金钱
- 159
- 注册时间
- 2015-10-12
- 在线时间
- 0 小时
|
5金钱
#include "stm32f10x.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_tim.h"
#include "stm32f10x_usart.h"
#include "delay.h"
void USART_config(u32 bound)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
//RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2|RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO,ENABLE); //这里一定要配置复用 AFIO
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2 ,ENABLE);
USART_DeInit(USART2);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode =GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode =GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = bound;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_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;
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
USART_Init(USART2, &USART_InitStructure);
USART_Cmd(USART2, ENABLE);
}
}
void USART2_IRQHandler(void)
{
//u8 RX_dat;
if(USART_GetITStatus(USART2,USART_IT_RXNE)!=RESET) //判断发生接收中断
{
USART_ClearITPendingBit(USART2, USART_IT_RXNE); // 清除中断标志
while(USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == RESET) // 等待接收
{
}
//USART_ITConfig(USART2, USART_IT_RXNE, DISABLE); ///这句话是关闭中断
}
}
int main(void)
{
GPIO_Config();
Timer_Config();
NVIC_Config();
SystemInit(); //看英文单词 意思为系统初始化(系统初始化包括时钟)
KEY_Config();
USART_config(9600);
delay_init(72);
LED(0xff);
while(1)
{
USART_SendData(USART2,0x31);
}
}
|
最佳答案
查看完整内容[请看2#楼]
比较简单的办法,不用printf 的话
char test[] = "菜鸟小新\r\n";
int i;
while(1)
{
for(i=0; i<sizeof(test); i++)
{
USART_SendData(USART2,test);
}
for(i=0; i<10000; i++);
}
|