码迷,mamicode.com
首页 > 其他好文 > 详细

STM32F030 UART应用

时间:2015-07-21 18:43:50      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:stm32

//==文件uart.h============================================================
#ifndef _UART_H_
#define _UART_H_

#define			V_UART_RX			0   //接收时状态
#define			V_UART_TX			1		//发送时状态

#define			V_RX_NUM		11	//接收数据字节长

#define			V_TX_NUM		8 //发送数据字节长

#define			VT_ClrRxIndex		5		//50ms没有接收到数据 清除接收索引

//==================================================
extern uint8 Rx_Buf[V_RX_NUM] ;
extern uint8 Tx_Buf[V_TX_NUM] ;
extern uint8 Rx_Index ;
extern uint8 Tx_Index ;

extern void uart_putc(unsigned char c);
extern void uart_puts(char *s ) ;
extern void USART_INIT(void) ;

extern void USART_GPIO_Init(void) ;
extern void USART_Param_Init(uint32 Invalue) ;
extern void Init_Uart_SendDat(uint8 Invalue) ;

extern uint8 CalcateCheckSumUart(uint8 *a,uint8 DataLong) ;

#endif


//==文件uart.c============================================================
#include "global.h" 
#include "uart.h"


#define    Init_Uart_CALL

uint8 Rx_Buf[V_RX_NUM] ;
uint8 Tx_Buf[V_TX_NUM] ;

uint8 Rx_Index = 0 ;
uint8 Tx_Index = 0 ;

 
#ifdef  Init_Uart_CALL

//*************************************
// 函数名称:USART_GPIO_Init
// 函数功能:uart GPIO初始化
// 入口参数:无
// 出口参数:无
// 备注:
//***************************************
void USART_GPIO_Init(void)
{
		GPIO_InitTypeDef  GPIO_InitStructure;
 
		RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOB, ENABLE);
								
		//-----PB6 TX------------- -----PB7 RX------------- 											 
		GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7;                 
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; 
		GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
		GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
		GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
		GPIO_Init(GPIOB, &GPIO_InitStructure);
		
		GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_0);
		GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_0);		
}	
//======串口 发送时IO状态初始化======================================
void USART_Param_Init(uint32 Invalue)
{
		GPIO_InitTypeDef  GPIO_InitStructure;

		RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOB, ENABLE);

		//-----PB7 RX-------------		接收为UART								 
		GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;                 
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; 
		GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
		GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
		GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
		GPIO_Init(GPIOB, &GPIO_InitStructure);	
		GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_0);
			
		if(Invalue == V_UART_RX)	//接收时状态
		{
			//-----PB6 TX------------- 		发送为输入		 						 
			GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;                 
			GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; 
			GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //GPIO_OType_OD ;//
			GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;//GPIO_PuPd_DOWN;//;
			GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
			GPIO_Init(GPIOB, &GPIO_InitStructure);
			
			USART_ClearITPendingBit(USART1,USART_IT_RXNE) ;	 //清除接收中断标志
			USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);//接收中断使能
			USART_ITConfig(USART1,USART_IT_TC,DISABLE); //发送中断不使能 USART_IT_TXE	
		}
		else if(Invalue == V_UART_TX)		//发送时状态				
		{
			//-----PB6 RX-------------		发送为UART								 
			GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;                 
			GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; 
			GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
			GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
			GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
			GPIO_Init(GPIOB, &GPIO_InitStructure);
			
			GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_0);
				
			USART_ITConfig(USART1,USART_IT_RXNE,DISABLE );//接收中断不使能
			USART_ClearITPendingBit(USART1,USART_IT_TC) ;		//清除发送中断标志
			USART_ITConfig(USART1,USART_IT_TC,ENABLE); //发送中断使能 USART_IT_TXE	
					
		}
				
}	
//*************************************
// 函数名称:USART_INIT
// 函数功能:uart初始化
// 入口参数:无
// 出口参数:无
// 备注:
//***************************************
void USART_INIT(void)
{
		USART_InitTypeDef USART_InitStructure;
		NVIC_InitTypeDef 	NVIC_InitStructure;
	
		RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE );		
		
		NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
		NVIC_InitStructure.NVIC_IRQChannelPriority = 2;
		NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
		NVIC_Init(&NVIC_InitStructure);

		   
		USART_InitStructure.USART_BaudRate = 4800;//9600
		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_Cmd(USART1, ENABLE);
		
		USART_ClearITPendingBit(USART1,USART_IT_RXNE) ;	 //清除接收中断标志
		USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);//接收中断使能
		
		USART_ClearITPendingBit(USART1,USART_IT_TC) ;		//清除发送中断标志
//		USART_ITConfig(USART1,USART_IT_TC,ENABLE); //发送中断使能 USART_IT_TXE
}
//*************************************
// 函数名称:uart_putc
// 函数功能:uart发送一字符
// 入口参数:无
// 出口参数:无
// 备注:
//***************************************
void uart_putc(unsigned char c)
{
	while(!((USART1->ISR)&(1<<7)));
	USART1->TDR=c;	
}
//*************************************
// 函数名称:uart_puts
// 函数功能:uart发送一字符串
// 入口参数:无
// 出口参数:无
// 备注:
//***************************************
void uart_puts(char *s )
{
	while (*s)
	uart_putc(*s++);
}

//*************************************
// 函数名称:uart_puts
// 函数功能:uart发送一字符串
// 入口参数:无
// 出口参数:无
// 备注:
//***************************************
void Init_Uart_SendDat(uint8 Invalue)
{
	
	Tx_Buf[0] = 0xc3 ;		
	Tx_Buf[1] = 0x00 ;		
	Tx_Buf[2] = 0x00 ;	
	Tx_Buf[3] = 0x00 ;		
	Tx_Buf[4] = 0x00 ;	
	Tx_Buf[5] = 0x00 ;		
	Tx_Buf[6] = Invalue ;	
	
	Tx_Buf[V_TX_NUM-1] = CalcateCheckSumUart(&Tx_Buf[0],(V_TX_NUM-1)) ;

	USART_Param_Init(V_UART_TX) ; //发送数据时 初始化端口
	Tx_Index = 0 ;
	uart_putc(Tx_Buf[Tx_Index]) ;	
	Tx_Index ++ ;
}

//*************************************
// 函数名称:uart_puts
// 函数功能:uart发送一字符串
// 入口参数:无
// 出口参数:无
// 备注:
//***************************************
uint8 CalcateCheckSumUart(uint8 *a,uint8 DataLong)
{
	uint8 i ;
	uint8 Tmp=0 ;
	
	for(i=0;i<DataLong;i++)
	{
		Tmp += a[i] ;	
	}
	Tmp = (Tmp^0xff)+1 ;	
	
	return Tmp ;
}

#endif //Init_Uart_CALL





版权声明:本文为博主原创文章,未经博主允许不得转载。

STM32F030 UART应用

标签:stm32

原文地址:http://blog.csdn.net/libiaojs/article/details/46988997

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!