高中生
最后登录1970-1-1
在线时间 小时
注册时间2017-5-22
|
主函数#include "stm32f10x.h"
#include "main.h"
uint16_t Data[8]={0x01,0x03,0x02,0x05,0x07,0x04,0x44,0x09};
uint16_t cout=0,USART_RX_COUNT=0;
int main(void)
{
uint16_t i;
USART1_Config();
while (1)
{
if(cout==0)
{
GPIO_SetBits(GPIOC,GPIO_Pin_10);
cout=1;
for(i=0;i<8;i++)
{
UART1SendByte(Data);
while (USART_GetFlagStatus(USART1,USART_FLAG_TC) !=SET);
USART_SendData(USART1, Data);
LED_Delay(1000);
}
LED_Delay(12000);
}
GPIO_ResetBits(GPIOC,GPIO_Pin_11); /
}
}
USART配置
#include "stm32f10x.h"
#include "bsp_usart.h"
#include "stm32f10x_usart.h"
void USART1_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 , ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOC, &GPIO_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);
NVIC_Config_USART1(); //中断配置
USART_ClearFlag(USART1, USART_FLAG_TC);//防止第一个数据被覆盖
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //接收中断使能
USART_Cmd(USART1, ENABLE); //使能
}
static void NVIC_Config_USART1(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void UART1SendByte(uint16_t SendData)
{
USART_SendData(USART1,SendData);
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
}
unsigned char UART1GetByte(unsigned char* GetData)
{
if(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET)
{ return 0;
}
*GetData = USART_ReceiveData(USART1);
return 1;
}
|
|