#include "stm32f10x.h"
#include "delay.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_rcc.h"
//INIT LED IO
void UART_GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1| RCC_APB2Periph_AFIO, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void UART_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = 115200;
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;
USART_Init(USART1, &USART_InitStructure);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
USART_Cmd(USART1, ENABLE);
}
void NVIC_UART_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0X2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0X2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
int main(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOD, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_Init(GPIOD, &GPIO_InitStructure);
delay_init();
GPIO_ResetBits(GPIOA,GPIO_Pin_8);
GPIO_ResetBits(GPIOD,GPIO_Pin_2);
UART_GPIO_Configuration();
UART_Configuration();
NVIC_UART_Configuration();
while(1)
{
//u8 str[]="zhe shi yi ge usart licheng";
//while(USART_GetFlagStatus(USART1, USART_FLAG_TXE)==SET)
// {
//u8 i=0;
USART_SendData(USART1, 0X01);
//while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
//i++;
//}
GPIO_SetBits(GPIOD,GPIO_Pin_2);
delay_ms(100);
GPIO_ResetBits(GPIOD,GPIO_Pin_2);
delay_ms(100);
GPIO_SetBits(GPIOD,GPIO_Pin_2);
delay_ms(100);
GPIO_ResetBits(GPIOD,GPIO_Pin_2);
delay_ms(100);
GPIO_SetBits(GPIOD,GPIO_Pin_2);
delay_ms(100);
GPIO_ResetBits(GPIOD,GPIO_Pin_2);
delay_ms(100);
delay_ms(1400);
}
}
void USART1_IRQHandler(void)
{
u8 RxBuffer=0;
if(USART_GetITStatus(USART1, USART_IT_RXNE) != SET)
{
RxBuffer = USART_ReceiveData(USART1);
USART_SendData(USART1, RxBuffer);
GPIO_SetBits(GPIOA,GPIO_Pin_8);
delay_ms(100);
GPIO_ResetBits(GPIOA,GPIO_Pin_8);
delay_ms(100);
GPIO_SetBits(GPIOA,GPIO_Pin_8);
delay_ms(100);
GPIO_ResetBits(GPIOA,GPIO_Pin_8);
delay_ms(100);
GPIO_SetBits(GPIOA,GPIO_Pin_8);
delay_ms(100);
GPIO_ResetBits(GPIOA,GPIO_Pin_8);
delay_ms(100);
}
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>