📄 uart.c
字号:
/****************************Copyright (c)*********************************************
**
** (c) Copyright 2006-2008, hui lian. luo, china, zj. hz
** All Rights Reserved
**
** 深圳市英蓓特信息技术有限公司
** http://www.embedinfo.com
** 博格达科技有限公司
** http://www.bogodtech.com
**
**--------------文件信息--------------------------------------------------------------
** 文 件 名: uart.c
** 创 建 人: 罗辉联
** 创建日期: 2007年12月28日
** 描 述: uart函数实体部分
** 技术顾问: 楼东武(副教授) 浙江大学信电系
**
**---------- 版本信息------------------------------------------------------------------
** 版 本: V1.0
** 说 明: uart 相关函数主要是UART配置
**
**-------------------------------------------------------------------------------------
**************************************************************************************/
/* Includes ------------------------------------------------------------------*/
#include "config.h"
/* Private typedef -----------------------------------------------------------*/
typedef char * uart_va_list;
/* Private define ------------------------------------------------------------*/
#define uart_sizeof_int(n) ((sizeof(n) + sizeof(int) - 1) & ~(sizeof(int) - 1))
#define uart_va_start(ap,v) (ap = (uart_va_list)&v +uart_sizeof_int(v))
#define uart_va_arg(ap,t) (*(t *)((ap += uart_sizeof_int(t)) - uart_sizeof_int(t)))
#define uart_va_end(ap) (ap = (uart_va_list)0)
#define UART_SEND_BYTE(ch) USART_SendChar(USART1, (u8*)ch)
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Public variables ---------------------------------------------------------*/
/****************************************************************************************
** 函数名称: UART_onfiguration
** 功能描述: UART1配置
** 参 数: None
** 返 回 值: None
** 作 者: 罗辉联
** 日 期: 2008年1月7日
**---------------------------------------------------------------------------------------
** 修 改 人:
** 日 期:
**--------------------------------------------------------------------------------------
****************************************************************************************/
void UART_onfiguration (void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
/* Enable USART1 clocks */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 , ENABLE);
/* Configure USARTx_Tx as alternate function push-pull */
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);
/* Configure USARTx_Rx as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* USARTx configuration ------------------------------------------------------*/
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_InitStructure.USART_Clock = USART_Clock_Disable;
USART_InitStructure.USART_CPOL = USART_CPOL_Low;
USART_InitStructure.USART_CPHA = USART_CPHA_2Edge;
USART_InitStructure.USART_LastBit = USART_LastBit_Disable;
/* Configure the USARTx */
USART_Init(USART1, &USART_InitStructure);
/* Enable the USART Receive interrupt: this interrupt is generated when the
USART1 receive data register is not empty */
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
/* Enable the USARTx */
USART_Cmd(USART1, ENABLE);
}
/****************************************************************************************
** 函数名称: USART_SendChar
** 功能描述: UART1 发送字符
** 参 数: USARTx: 串口号, Data: 待发送数据
** 返 回 值: None
** 作 者: 罗辉联
** 日 期: 2008年1月7日
**---------------------------------------------------------------------------------------
** 修 改 人:
** 日 期:
**--------------------------------------------------------------------------------------
****************************************************************************************/
void USART_SendChar(USART_TypeDef* USARTx, u8* Data)
{
/* Transmit Data */
USARTx->DR = (Data[0] & (u16)0x01FF);
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
}
/*********************************************************************************************************
;** 函数名称: printf
;** 功能描述: 以查询的方式向串口发送字符串
;**
;** 参 数: format: 将要发送的字符串USART_SendData(USART1);
;**
;** 返 回 值: 无
;**
;** 作 者: 李海军
;** 日 期: 2006年5月21日
;**-------------------------------------------------------------------------------------------------------
;** 修 改 人:
;** 日 期:
;**------------------------------------------------------------------------------------------------------
;********************************************************************************************************/
void printf(char * format, ...)
{
char * s = 0;
int d = 0, i = 0;
unsigned char ch[sizeof(int) * 2 + 2] = {0};
uart_va_list ap = 0;
uart_va_start(ap, format);
while (* format)
{
if (* format != '%')
{
UART_SEND_BYTE(format++);
continue;
}
switch (*(++format))
{
case 's':
case 'S':
s = uart_va_arg(ap, char *);
for ( ; *s; s++)
{
UART_SEND_BYTE(s);
}
break;
case 'c':
case 'C':
UART_SEND_BYTE(uart_va_arg(ap, char));
break;
case 'd':
case 'D':
d = uart_va_arg(ap, int);
for(i = 0; i < sizeof(int) * 2; i++)
{
ch[i] = (unsigned char)(d&0x0f) + '0';
if(ch[i] > '9')
ch[i] += 7;
d >>= 4;
}
ch[i++] = 'x';
ch[i] = '0';
for(i = sizeof(int) * 2 + 2; i > 0; i--)
UART_SEND_BYTE(&ch[i -1]);
break;
default:
UART_SEND_BYTE(format);
break;
}
format++;
}
uart_va_end(ap);
}
/****************************** http://www.bogodtech.com *******END OF FILE******************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -