- 打卡等级:偶尔看看I
- 打卡总天数:10
- 最近打卡:2025-05-01 10:09:20
已绑定手机
注册会员
- 积分
- 110
|
发表于 2025-3-25 11:12:20
|
显示全部楼层
您好,下面是我的写的,我是初学者我找了半天好像没有找到您说的串口1和串口2共用的寄存器,也可能是我学的不好没有找到,您能再给些建议嘛?非常感谢
#include "Config.h"
#include "STC8H.h"
#include "Manchester.h"
#include "STC8G_H_GPIO.h"
#include "STC8G_H_UART.h"
#include "STC8G_H_Delay.h"
#include "STC8G_H_NVIC.h"
#include "STC8G_H_Switch.h"
/*************** 配置IO *****************/
void GPIO_config(void)
{
GPIO_InitTypeDef GPIO_InitStructure; //结构定义
GPIO_InitStructure.Pin = GPIO_Pin_2;
GPIO_InitStructure.Mode = GPIO_OUT_PP;
GPIO_Inilize(GPIO_P3,&GPIO_InitStructure); //初始化
GPIO_InitStructure.Pin = GPIO_Pin_0 | GPIO_Pin_1;
GPIO_InitStructure.Mode = GPIO_PullUp;
GPIO_Inilize(GPIO_P1,&GPIO_InitStructure); //初始化
}
/*************** 串口初始化函数 *****************/
void UART_config(void)
{
COMx_InitDefine COMx_InitStructure; //结构定义
COMx_InitStructure.UART_Mode = UART_8bit_BRTx;
COMx_InitStructure.UART_BaudRate = 115200ul;
COMx_InitStructure.UART_RxEnable = ENABLE;
UART_Configuration(UART2, &COMx_InitStructure);
NVIC_UART2_Init(ENABLE,Priority_1);
UART2_SW(UART2_SW_P10_P11);
}
/*****************主函数*****************/
void main()
{
u8 i;
u8 byte1, byte2; // 用于存储接收到的两个字节
u8 eff_data[6]; // 数据字节
u8 checksum; // 校验字节
EA=1;
EAXSFR(); /* 扩展寄存器访问使能 */
GPIO_config();
UART_config();
while(1)
{
delay_ms(1);
if(COM2.RX_TimeOut > 0) //超时计数
{
if(--COM2.RX_TimeOut == 0)
{
if(COM2.RX_Cnt >0)
{
byte1 = RX2_Buffer[0]; // 获取帧头字节
byte2 = RX2_Buffer[1]; // 获取数据字节
// 获取数据字节
for (i = 2; i < 8; i++)
{
eff_data = RX2_Buffer;
}
// 获取校验字节
checksum = RX2_Buffer[7];
// 发送帧头字节
sendManchesterData(byte1);
sendManchesterData(byte2);
// 发送后6位数据字节
for (i = 2; i < 8; i++)
{
sendManchesterData(eff_data);
}
}
COM2.RX_Cnt = 0;
}
}
}
}
|
|