|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区
您需要 登录 才可以下载或查看,没有账号?立即注册
×
//=============================================================================
//文件名称:usart1.c
//功能概要:串口1驱动文件
//更新时间:2016-11
//技术提供:mcudev.taobao.com
//=============================================================================
#include "USART1.h"
unsigned short adtet=0;
/* USART初始化 */
void USART1_Init(uint32_t USART1_Baud)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStruct;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); //使能GPIOA的时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);//使能USART的时钟
/* USART1的端口配置 */
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_1);//配置PA9成第二功能引脚 TX
GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_1);//配置PA10成第二功能引脚 RX
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* USART1的基本配置 */
USART_InitStructure.USART_BaudRate = USART1_Baud; //波特率
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_Cmd(USART1, ENABLE); //使能USART1
/* USART1的NVIC中断配置 */
NVIC_InitStruct.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStruct.NVIC_IRQChannelPriority = 0x02;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStruct);
}
//=============================================================================
//文件名称:
//功能概要:USART1中断函数
//参数说明:无
//函数返回:无
//=============================================================================
unsigned char uart1_buffer[64]={0};//接收数据指针
unsigned char uart1_count=0;//接收数据指针
void USART1_IRQHandler(void)
{
//while( USART_GetITStatus(USART1, USART_IT_RXNE) )
{
uart1_buffer[uart1_count]=(unsigned char)USART_ReceiveData(USART1);
uart1_count++;
if( uart1_count>=2 )
{
uart1_count = 0;
adtet = uart1_buffer[0]<<8|uart1_buffer[1];
}
}
USART_ClearFlag( USART1, USART_FLAG_ORE|USART_FLAG_CM );
}
//=============================================================================
//文件名称:
//功能概要:USART1输出函数
//参数说明:无
//函数返回:无
//=============================================================================
void USART1_Tx(unsigned char *DATA_P,unsigned int nCount)
{
unsigned int i;
for(i=0;i<nCount;i++)//启动数据转发程序
{
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); //检查发送寄存器是否为空
USART_SendData(USART1, *DATA_P);
DATA_P++;
}
} |
|