【STM8学习笔记】STM8系列串口中断发送例程

目标MCU:STM8L051F3

功能:串口中断发送。(区别于串口轮询发送)

drv_usart.c

/*****************************************************************************************
 * Confidential and Proprietary Information of xxx Corporation
 * (C) 2019 ,xxx Corporation . All rights reserved.
 * file :    drv_usart.c
 * brief :  .c files
 * History:    Author        Version          ChangeContent            Date
 *               xxx                             NewFile             2019.04.28
 *****************************************************************************************/

/**********************************************
* 头文件引用
**********************************************/
#include "drv_usart.h"


/**********************************************
 * 静态全局变量定义
**********************************************/


/**********************************************
 *全局变量定义
**********************************************/
drv_usart_s m_usart_s;



/**********************************************
 * 内部函数声明
 **********************************************/ 
static void Drv_USART_EndTransmit_IT(void);
static void Drv_USART_ParaInit(void);
static void Drv_USART_SendByte( u8 sendData );


/**********************************************
 * 全局函数实现体
 **********************************************/
 /****************************************************************************************
 * FunctionName   : drvUsart_init
 * Abstract       : Init Usart driver.
 * Argument1(in)  : void
 * Argument2(out) : 
 * Return Value   : void
 * Remarks        :
 * Create         : 2019/04/28 , DJWT_zhenggp  New
 * History        :
****************************************************************************************/
void drvUsart_init(void)
{
	
	/*PA2 --> USART_TX
	*PA3 --> USART_RX*/
	// PA3 USART_RX config
	PA_DDR_DDR3 = 0;		 //输入
	PA_CR1_C13	= 1;		 //上拉输入
	PA_CR2_C23	= 0;
	// PA2 USART_TX config
	PA_ODR_ODR2 = 1;
	PA_CR1_C12	= 1;		 //推挽输出
	PA_CR2_C22	= 1;		 //输出摆率10M	  
	PA_DDR_DDR2 = 1;		 //输出高电平,TX空闲状态为高电平,如果不设置,会莫名奇妙的发送0x00

	/* USART2 --> PCKEN33, USART3 --> PCKEN34*/
	CLK_PCKENR1_PCKEN15 = 1;					//开启USART1外设时钟

	//开启引脚的UART功能,功能复用为USART功能引脚
	//00: USART1_TX on PC2 and USART1_RX on PC3
	//01: USART1_TX on PA2 and USART1_RX on PA3
	//10: USART1_TX on PC5 and USART1_RX on PC6
	SYSCFG_RMPCR1_USART1TR_REMAP = 1;			// system configuration PA2,PA3

	//设置串口工作方式
	USART1_CR1_M	 = 0;				// 1 Start bit, 8 Data bits
	USART1_CR3_STOP0 = 0;				// 1 STOP bit
	USART1_CR3_STOP1 = 0;	
	USART1_CR1_PCEN  = 0;				/* No Parity : Parity control disabled */

	//设置波特率
	//波特率设置为9600
	// 2000000/9600=208.333
	//208.333(DEC)=00D0(HEX)
	USART1_BRR2 = 0x00;				//the BRR2 should be programmed before BRR1
	USART1_BRR1 = 0x0D;

//	USART1_CR2_TEN = 1;			//使能发送
	//	USART1_CR2_TIEN=0;		//打开发送中断
//	USART1_CR2_TCIEN = 1;			//打开发送完成中断
	USART1_CR2_REN = 1;				//使能接收
	USART1_CR2_RIEN = 1;			//打开接收中断

	USART1_CR1_USARTD = 0;			//Enable the USART peripheral

	Drv_USART_ParaInit();
}

/****************************************************************************************
 * FunctionName   : Drv_USART_ParaInit
 * Abstract       : 
 * Argument1(in)  : void
 * Argument2(out) : 
 * Return Value   : void
 * Remarks        :
 * Create         : 2018/12/08 , DJWT_zhenggp  New
 * History        :
****************************************************************************************/
static void Drv_USART_ParaInit(void)
{
	m_usart_s.sendBuf_p = NULL;
}


 /****************************************************************************************
 * FunctionName   : Drv_USART_Rx_IRQHandler
 * Abstract       : usart rx interrupt handler.
 * Argument1(in)  : void
 * Argument2(out) : 
 * Return Value   : void
 * Remarks        :
 * Create         : 2019/04/28 , DJWT_zhenggp  New
 * History        :
****************************************************************************************/
#pragma vector = USART_R_RXNE_vector
__interrupt void Drv_USART_Rx_IRQHandler(void)
{
	if( 1 == USART1_SR_RXNE )
	{

	}
}

 /****************************************************************************************
 * FunctionName   : Drv_USART_Tx_IRQHandler
 * Abstract       : usart tx interrupt handler.
 * Argument1(in)  : void
 * Argument2(out) : 
 * Return Value   : void
 * Remarks        :
 * Create         : 2019/04/28 , DJWT_zhenggp  New
 * History        :
****************************************************************************************/
#pragma vector = USART_T_TC_vector
__interrupt void Drv_USART_Tx_IRQHandler(void)
{
	if( (RESET != USART1_SR_TC) && (RESET != USART1_CR2_TCIEN) )
	{
		Drv_USART_EndTransmit_IT();
	}
}

/****************************************************************************************
 * FunctionName   : Drv_USART_SendByte
 * Abstract       : usart send byte data.
 * Argument1(in)  : void
 * Argument2(out) : 
 * Return Value   : void
 * Remarks        :
 * Create         : 2018/12/18 , DJWT_zhenggp  New
 * History        :
****************************************************************************************/
static void Drv_USART_SendByte( u8 sendData )
{
	USART1_DR = sendData;
}

/****************************************************************************************
 * FunctionName   : Drv_USART_EndTransmit_IT
 * Abstract       : Wrap up transmission in non-blocking mode.
 * Argument1(in)  : void
 * Argument2(out) : 
 * Return Value   : void
 * Remarks        :
 * Create         : 2018/12/18 , DJWT_zhenggp  New
 * History        :
****************************************************************************************/
static void Drv_USART_EndTransmit_IT(void)
{
	static const u8* strEnd = "\r\n";
	if( NULL != m_usart_s.sendBuf_p ){
		if( '\0' == *m_usart_s.sendBuf_p ){			
			m_usart_s.sendBuf_p = (u8*)strEnd;
		}

		Drv_USART_SendByte(*(m_usart_s.sendBuf_p));

		if( '\n' == *m_usart_s.sendBuf_p ){
			m_usart_s.sendBuf_p = NULL;
		}else{
			m_usart_s.sendBuf_p++;
		}
	}else{
		/*关闭发送使能*/
		USART1_CR2_TEN = DISABLE;	
		/* 关闭发送完成中断 */
		USART1_CR2_TCIEN = DISABLE;
	}
	
}

/****************************************************************************************
 * FunctionName   : Drv_USART_EndTransmit_IT
 * Abstract       : Wrap up transmission in non-blocking mode.
 * Argument1(in)  : void
 * Argument2(out) : 
 * Return Value   : void
 * Remarks        :
 * Create         : 2018/12/18 , DJWT_zhenggp  New
 * History        :
****************************************************************************************/
void Drv_USART_TransmitData(u8* StrData_p)
{
	if( NULL == StrData_p ){
		return;
	}
	m_usart_s.sendBuf_p = StrData_p;

	/* 打开发送完成中断 */
	USART1_CR2_TCIEN = ENABLE;
				
	Drv_USART_SendByte(*(m_usart_s.sendBuf_p++));

	/* 使能发送 */
	USART1_CR2_TEN = ENABLE;
}

drv_usart.h

/*****************************************************************************************
 * Confidential and Proprietary Information of xxx Corporation
 * (C) 2019 ,xxx Corporation . All rights reserved.
 * file :    drv_usart.h
 * brief :  .h files
 * History:    Author        Version          ChangeContent            Date
 *               zhenggp                                 NewFile             2019.04.28
 *****************************************************************************************/

#ifndef __DRV_USART_H__
#define __DRV_USART_H__

/**********************************************
* 头文件引用
**********************************************/
#include "../typedef.h"

/**********************************************
 * 宏定义
**********************************************/



/**********************************************
 * 枚举型定义
**********************************************/


/**********************************************
 *结构体类型定义
**********************************************/
typedef struct __DRV_USART_S__{
	u8* sendBuf_p;
}drv_usart_s;


/**********************************************
 * 全局函数声明
**********************************************/
extern void drvUsart_init(void);
extern void Drv_USART_TransmitData(u8* StrData_p);

#endif

 

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
是的,CSDN上有很多关于STM32F103C8T6的串口中断例程。你可以在CSDN上搜索“STM32F103C8T6 串口中断”来查找相关的例程。以下是一个简单的例程,可以在串口接收到数据时触发中断,并且在中断服务程序中将接收到的数据回传: ```c #include "stm32f10x.h" #include <stdio.h> #define USARTx USART1 void USART_Configuration(void) { USART_InitTypeDef USART_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1, ENABLE); /* Configure USART1 Rx (PA10) as input floating */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure); /* Configure USART1 Tx (PA9) 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 USART1 */ 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_Init(USARTx, &USART_InitStructure); /* Enable USART1 Receive interrupt */ USART_ITConfig(USARTx, USART_IT_RXNE, ENABLE); /* Configure and enable USART1 interrupt */ NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /* Enable USART1 */ USART_Cmd(USARTx, ENABLE); } void USART1_IRQHandler(void) { if (USART_GetITStatus(USARTx, USART_IT_RXNE) != RESET) { char received_char = USART_ReceiveData(USARTx); USART_SendData(USARTx, received_char); while (USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET); USART_ClearITPendingBit(USARTx, USART_IT_RXNE); } } int main(void) { USART_Configuration(); while (1) { // Do something else here } } ``` 这个例程中,我们使用了USART1作为串口,配置了串口的GPIO引脚、波特率和中断。在中断服务函数中,我们读取了接收到的数据,并将其回传。您可以根据自己的需求修改此例程

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值