📄 setusart.c
字号:
/******************************************************************************/
/* SERIAL.C: Low Level Serial Routines */
/******************************************************************************/
/* This file is part of the uVision/ARM development tools. */
/* Copyright (c) 2005-2007 Keil Software. All rights reserved. */
/* This software may only be used under the terms of a valid, current, */
/* end user licence from KEIL for a compatible version of KEIL software */
/* development tools. Nothing else gives you the right to use this software. */
/******************************************************************************/
#include <stm32f10x_lib.h> /* STM32F10x Library Definitions */
/*宏定义*/
#define Usart_DR_Address ((u32)0x40013804) //参考编程手册第二章及相关寄存器偏移地址得出
/*参数申明*/
extern u8 CAN_ReceiveData[18];
/*函数原型申明*/
int GetChar (void);
int SendChar (int ch);
void SetupUART(void);
/*函数定义*/
void SetupUART(void)
{
// DMA_InitTypeDef DMA_InitStructure;
// /* DMA Channel1 Configuration ----------------------------------------------*/
// DMA_DeInit(DMA_Channel1); //将通道1的相关寄存器复位为默认值
// DMA_InitStructure.DMA_PeripheralBaseAddr = Usart_DR_Address;//外设地址
// DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&CAN_ReceiveData;//内存地址
// DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;//dma传输方向:外设作为目标
// DMA_InitStructure.DMA_BufferSize = 18;//设置DMA在传输时缓冲区的长度 word
// DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;//设置DMA的外设递增模式,一个外设
//
// DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;//设置DMA的内存递增模式
// DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;//外设数据字长 8bits
// DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;//内存数据字长 8bits
// DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;//设置DMA的传输模式:连续不断的循环模式
// DMA_InitStructure.DMA_Priority = DMA_Priority_High;//设置DMA的优先级别
// DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;//设置DMA的2个memory中的变量互相访问
// DMA_Init(DMA_Channel1, &DMA_InitStructure);//设置新的通道寄存器值
//
// DMA_Cmd(DMA_Channel1, ENABLE); /* Enable DMA Channel1 */
USART_InitTypeDef USART_InitStructure;
/* USART1 configured as follow:
- BaudRate = 115200 baud
- Word Length = 8 Bits
- One Stop Bit
- No parity
- Hardware flow control disabled (RTS and CTS signals)
- Receive and transmit enabled
- USART Clock disabled
- USART CPOL: Clock is active low
- USART CPHA: Data is captured on the middle
- USART LastBit: The clock pulse of the last data bit is not output to
the SCLK pin
*/
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;
USART_Init(USART1, &USART_InitStructure);
// USART_DMACmd(USART1,USART_DMAReq_Rx,ENABLE);//关键
USART_Cmd(USART1, ENABLE);
// USART_SetAddress(USART1, 0x01);
//
//
// /* Select the USART1 WakeUp Method */
// USART_WakeUpConfig(USART1, USART_WakeUp_AddressMark);
// USART_ReceiverWakeUpCmd(USART1, ENABLE);
}
/* Implementation of putchar (also used by printf function to output data) */
int SendChar (int ch)
{ /* Write character to Serial Port */
USART_SendData(USART1, (unsigned char) ch);
while (!(USART1->SR & USART_FLAG_TXE)){}; //等待传输结束
return (ch);
}
int GetChar (void)
{ /* Read character from Serial Port */
while (!(USART1->SR & USART_FLAG_RXNE)){}; //等待接受完成
return (USART_ReceiveData(USART1));
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -