Files
SM-1000M/IAPV1.1/Driver/usart/usart.c
2026-04-23 10:50:18 +08:00

135 lines
4.3 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "usart.h"
#include "pbdata.h"
char USART1_TX_BUF[USART1_MAX_SEND_LEN]; //发送缓冲,最大USART2_MAX_SEND_LEN字节
void UART_PutChar(USART_TypeDef* USARTx, uint8_t Data)
{
USART_SendData(USARTx, Data);
while(USART_GetFlagStatus(USARTx, USART_FLAG_TC) == RESET){}
}
void UART_PutStr (USART_TypeDef* USARTx, char *str)
{
while (0 != *str)
{
UART_PutChar(USARTx, *str);
str++;
}
}
void USART_OUT(USART_TypeDef* USARTx, uint8_t *Data,uint16_t Len)
{
uint16_t i;
for(i=0; i<Len; i++){
USART_SendData(USARTx, Data[i]);
while(USART_GetFlagStatus(USARTx, USART_FLAG_TC)==RESET);
}
}
int fputc(int ch,FILE *f)
{
USART_ClearFlag(USART1,USART_FLAG_TC);
USART_SendData(USART1,(u8)ch);
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);//等待发送数据完毕);
return ch;
}
void USART1_Configuration(u32 baund)
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); //使能USART1
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); //GPIOA时钟
USART_DeInit(USART1); //复位串口1
//USART1_TX PA.9
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//USART1_RX PA.10
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);
USART_InitStructure.USART_BaudRate = baund;
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);//开启USART1的接收中断
//Usart1 NVIC 配置
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;//串口1中断通道
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;//抢占优先级3
NVIC_InitStructure.NVIC_IRQChannelSubPriority =0; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器、
USART_Cmd(USART1,ENABLE);
USART_ClearFlag(USART1,USART_FLAG_TC); //清除发送完成标志位
}
//使用串口读取SDI先复位串口并且重新配置一下IO
void USART_RS232_DEinit(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_Cmd(USART1,DISABLE);
USART_DeInit(USART1);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); //使能PB端口时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9|GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; //推挽输出
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推免
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_400KHz; //IO口速度为400KHz
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_SetBits(GPIOA,GPIO_Pin_9);
GPIO_SetBits(GPIOA,GPIO_Pin_10);
}
//使用串口读取SDI先复位串口并且重新配置一下IO
void USART_SDI_DEinit(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_Cmd(USART1,DISABLE);
USART_DeInit(USART1);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE); //使能PB端口时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; //推挽输出
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推免
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_400KHz; //IO口速度为400KHz
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_SetBits(GPIOB,GPIO_Pin_6);
GPIO_SetBits(GPIOB,GPIO_Pin_7);
}
void USART1_IRQHandler(void)
{
u8 ucCh;
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)//接收到数据
{
ucCh =USART_ReceiveData(USART1);
USART_SendData(USART1, ucCh);
while(USART_GetFlagStatus(USART1, USART_FLAG_TC)==RESET);
}
}