天天看點

2021-06-09 STC8單片機序列槽驅動函數

STC8單片機序列槽驅動函數

單片機型号為 STC8A8K64S4A12

預設時鐘頻率為 22.1184MHz

序列槽設定為 9600-n-8-1

使用的序列槽為 序列槽1(RXD–P30 TXD–P31)

實驗現象為收發一緻

以下為實作現象

2021-06-09 STC8單片機序列槽驅動函數

以下為完整程式代碼

/**
  *************************************************************************************************************************
  * @file    usart.c
  * @author  底色悲涼
  * @version V1.0
  * @date    2021-06-09
  * @brief   STC8序列槽
  *************************************************************************************************************************
  * @attention
  *
  * 
  *
  *************************************************************************************************************************
  */

/* Includes -------------------------------------------------------------------------------------------------------------*/
#include "usart.h"

/* define ---------------------------------------------------------------------------------------------------------------*/

void USART1_Init(void)
{
	//序列槽1配置 
	//9600-n-8-1 RXD--P30  TXD--P31
	SCON = 0x50;  //REN=1允許串行接受狀态,序列槽工作模式2     	   
	TMOD = 0x20;  //定時器工作方式2       8位 自動重裝載定時器  實作波特率                
	AUXR = 0X40;	//開啟1T模式   
  	TH1 = 0xB8;	  //設定波特率為9600  公式 TH1=256-(22118400/32/9600)=256-72=184  0xB8
	TL1 = 0xB8;
	TR1  = 1;     //開啟定時器1                                                      
	ES   = 1;     //開序列槽中斷                  
	EA   = 1;     //開總中斷 
}

/**
  * @brief  序列槽1發送位元組
  * @param  dat
  * @retval None
  */
void USART1_SendByte(unsigned char dat)
{
	SBUF = dat; 	//SUBF接受/發送緩沖器(又叫串行通信特殊功能寄存器)
 	while (!TI);	//等特資料傳送	(TI發送中斷标志)
  	TI = 0;				//清除資料傳送标志	
}

/**
  * @brief 序列槽1中斷處理函數 
  * @param  
  * @retval 
  */
void USART_Int(void) interrupt 4 using 1
{ 
	unsigned char tmp;  
	
	if (RI) 
  	{
		tmp = SBUF;
		
		USART1_SendByte(tmp);

		RI = 0;
  }
}

/*****************************************************END OF FILE*********************************************************/	

           
/**
  *************************************************************************************************************************
  * @file    usart.h
  * @author  底色悲涼
  * @version V1.0
  * @date    2021-06-09
  * @brief   STC8序列槽
  *************************************************************************************************************************
  * @attention
  * 
  * 
  * 
  *************************************************************************************************************************
  */

#ifndef __USART_H
#define	__USART_H

/* Includes -------------------------------------------------------------------------------------------------------------*/
#include "stc8.h"
#include "stdio.h"

/* define ---------------------------------------------------------------------------------------------------------------*/


/* function -------------------------------------------------------------------------------------------------------------*/
void USART1_Init(void);                   //序列槽1初始化
void USART1_SendByte(unsigned char dat);  //序列槽1發送位元組

#endif /* __USART_H */

/*****************************************************END OF FILE*********************************************************/	

           
/**
  *************************************************************************************************************************
  * @file    main.c
  * @author  底色悲涼
  * @version V1.0
  * @date    2021-06-09
  * @brief   序列槽測試
  *************************************************************************************************************************
  * @attention
  *
  * 
  *
  *************************************************************************************************************************
  */

/* Includes -------------------------------------------------------------------------------------------------------------*/
#include "stc8.h"
#include "usart.h"

/* define ---------------------------------------------------------------------------------------------------------------*/

	
/**
 * @brief  
 * @param  None
 * @retval None
 */
void main(void)
{
	USART1_Init();
	
	while(1);
}
	
           

附完整工程的百度網盤連結:https://pan.baidu.com/s/1_Q8o70ustiGtqcumRenJiw

提取碼:1234

繼續閱讀