- 打卡等级:初来乍到
- 打卡总天数:5
- 最近打卡:2024-09-24 09:38:07
注册会员
- 积分
- 109
|
/*********************************************************/
// #define MAIN_Fosc 24000000L //定义主时钟
// #define MAIN_Fosc 22118400L //定义主时钟
#define MAIN_Fosc 11059200L //定义主时钟
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#include "math.h"
#include <stdarg.h>
#include "STC8xxxx.H"
/************* 功能说明 **************
请先别修改程序, 直接下载"01-串口1中断收发-C语言"里的"
UART1.hex"测试, 主频选择11.0592MHZ. 测试正常后再修改移植.
串口1全双工中断方式收发通讯程序。
通过PC向MCU发送数据,
MCU收到后通过串口把收到的数据原样返回.
默认参数:
串口1设置均为 1位起始位, 8位数据位, 1位停止位, 无校验.
串口1(P3.0 P3.1): 115200bps.
******************************************/
/************* 本地常量声明 **************/
#define RX1_Length 128
u8 xdata RX1_Buffer[RX1_Length];
u8 xdata logstr[50];
u8 TX1_read,RX1_write; //读写索引(指针).
bit B_TX1_Busy; // 发送忙标志
/************* 本地函数声明 **************/
void UART1_config(u32 brt, u8 timer, u8 io);
void UART1_PrintString(u8 *puts);
//========================================================================
// 函数: void main(void)
// 描述: 主函数
// 参数: none.
// 返回: none.
// 版本: VER1.0
// 日期: 2018-4-2
// 备注:
//========================================================================
void main(void)
{
u8 i;
u8 count = 0;
UART1_config(115200UL, 1, 0);
EA = 1;
for(i=0; i<RX1_Length; i++) RX1_Buffer[i] = 0;
B_TX1_Busy = 0;
TX1_read = 0;
RX1_write = 0;
UART1_PrintString("STC8G系列 UART1 Test!\r\n");
while (1)
{
if((TX1_read != RX1_write) && !B_TX1_Busy)
{
B_TX1_Busy = 1; //标志发送忙
SBUF = RX1_Buffer[TX1_read]; //发一个字节
if(++TX1_read >= RX1_Length) TX1_read = 0; //避免溢出处理
}
count++;
if(count > 100)
{
count = 0;
memset(logstr, 0x00, sizeof(logstr));
sprintf(logstr,"recive count=%d\r\n", RX1_write);
UART1_PrintString(logstr);
}
}
}
//========================================================================
// 函数: SetTimer2Baudraye(u16 dat)
// 描述: 设置Timer2做波特率发生器。
// 参数: dat: Timer2的重装值.
// 返回: none.
// 版本: VER1.0
// 日期: 2018-4-2
// 备注:
//========================================================================
void SetTimer2Baudraye(u16 dat)
{
AUXR &= ~(1<<4); //Timer stop
AUXR &= ~(1<<3); //Timer2 set As Timer
AUXR |= (1<<2); //Timer2 set as 1T mode
TH2 = (u8)(dat >> 8);
TL2 = (u8)dat;
IE2 &= ~(1<<2); //禁止中断
AUXR |= (1<<4); //Timer run enable
}
//========================================================================
// 函数: void UART1_config(u32 brt, u8 timer, u8 io)
// 描述: UART1初始化函数。
// 参数: brt: 通信波特率.
// timer: 波特率使用的定时器, timer=2: 波特率使用定时器2, 其它值: 使用Timer1做波特率.
// io: 串口1切换到的IO, io=0: 串口1切换到P3.0 P3.1, =1: 切换到P3.6 P3.7, =2: 切换到P1.6 P1.7, =3: 切换到P4.3 P4.4.
// 返回: none.
// 版本: VER1.0
// 日期: 2018-4-2
// 备注:
//========================================================================
void UART1_config(u32 brt, u8 timer, u8 io) // brt: 通信波特率, timer=2
{
brt = 65536UL - (MAIN_Fosc / 4) / brt;
if(timer == 2) //波特率使用定时器2
{
AUXR |= 0x01; //S1 BRT Use Timer2;
SetTimer2Baudraye((u16)brt);
}
else //波特率使用定时器1
{
TR1 = 0;
AUXR &= ~0x01; //S1 BRT Use Timer1;
AUXR |= (1<<6); //Timer1 set as 1T mode
TMOD &= ~(1<<6); //Timer1 set As Timer
TMOD &= ~0x30; //Timer1_16bitAutoReload;
TH1 = (u8)(brt >> 8);
TL1 = (u8)brt;
ET1 = 0; // 禁止Timer1中断
INT_CLKO &= ~0x02; // Timer1不输出高速时钟
TR1 = 1; // 运行Timer1
}
if(io == 1) {S1_USE_P36P37(); P3n_standard(0xc0);} //切换到 P3.6 P3.7
else if(io == 2) {S1_USE_P16P17(); P1n_standard(0xc0);} //切换到 P1.6 P1.7
else if(io == 3) {S1_USE_P43P44(); P4n_standard(0x18);} //切换到 P4.3 P4.4
else {S1_USE_P30P31(); P3n_standard(0x03);} //切换到 P3.0 P3.1
SCON = (SCON & 0x3f) | (1<<6); // 8位数据, 1位起始位, 1位停止位, 无校验
// PS = 1; //高优先级中断
ES = 1; //允许中断
REN = 1; //允许接收
}
//========================================================================
// 函数: void UART1_PrintString(u8 *puts)
// 描述: 串口1字符串打印函数
// 参数: puts: 字符串指针.
// 返回: none.
// 版本: VER1.0
// 日期: 2018-4-2
// 备注:
//========================================================================
void UART1_PrintString(u8 *puts)
{
for (; *puts != 0; puts++)
{
B_TX1_Busy = 1; //标志发送忙
SBUF = *puts; //发一个字节
while(B_TX1_Busy); //等待发送完成
}
}
//========================================================================
// 函数: void UART1_ISR (void) interrupt UART1_VECTOR
// 描述: 串口1中断函数
// 参数: none.
// 返回: none.
// 版本: VER1.0
// 日期: 2018-4-2
// 备注:
//========================================================================
void UART1_ISR (void) interrupt UART1_VECTOR
{
if(RI)
{
RI = 0;
RX1_Buffer[RX1_write] = SBUF;
if(++RX1_write >= RX1_Length) RX1_write = 0;
}
if(TI)
{
TI = 0;
B_TX1_Busy = 0;
}
}
串口输出的log信息一直都是:
recive count=225
|
|