使用实验箱的例程改出接收程序,正常接收到协议数据,并且能保存到数组。
/*---------------------------------------------------------------------*/
/* --- STC MCU Limited ------------------------------------------------*/
/* --- STC 1T Series MCU Demo Programme -------------------------------*/
/* --- Mobile: (86)13922805190 ----------------------------------------*/
/* --- Fax: 86-0513-55012956,55012947,55012969 ------------------------*/
/* --- Tel: 86-0513-55012928,55012929,55012966 ------------------------*/
/* --- Web: www.STCAI.com ---------------------------------------------*/
/* --- Web: www.STCMCUDATA.com ---------------------------------------*/
/* --- BBS: www.STCAIMCU.com -----------------------------------------*/
/* --- QQ: 800003751 -------------------------------------------------*/
/* 如果要在程序中使用此代码,请在程序中注明使用了STC的资料及程序 */
/*---------------------------------------------------------------------*/
/************* 功能说明 **************
本例程基于STC8H8K64U为主控芯片的实验箱9进行编写测试,STC8G、STC8H系列芯片可通用参考.
串口2全双工中断方式收发通讯程序。
通过PC向MCU发送数据, MCU收到后通过串口2把收到的数据原样返回.
用定时器做波特率发生器,建议使用1T模式(除非低波特率用12T),并选择可被波特率整除的时钟频率,以提高精度。
下载时, 选择时钟 22.1184MHz (用户可自行修改频率).
******************************************/
#include "../comm/stc8h.h"
#include "intrins.h"
#include "pelco_d.h"
#define MAIN_Fosc 24000000L //定义主时钟(精确计算115200波特率)
typedef unsigned char u8;
typedef unsigned int u16;
typedef unsigned long u32;
#define Baudrate2 9600L
#define UART2_BUF_LENGTH 64
u8 TX2_Cnt; //发送计数
u8 RX2_Cnt; //接收计数
bit B_TX2_Busy; //发送忙标志
u8 xdata RX2_Buffer[UART2_BUF_LENGTH]; //接收缓冲
u8 pelco_d_num = 0;
u8 pelco_d_r[8] = 0;
u8 pelco_d_to = 0;
u8 pelco_d_c;
void UART2_config(u8 brt); // 选择波特率, 2: 使用Timer2做波特率, 其它值: 无效.
void PrintString2(u8 *puts);
void PrintPelcoD(void);
//========================================================================
// 函数: void main(void)
// 描述: 主函数。
// 参数: none.
// 返回: none.
// 版本: VER1.0
// 日期: 2014-11-28
// 备注:
//========================================================================
void pelco_d_main(void)
{
P_SW2 |= 0x80; //扩展寄存器(XFR)访问使能
//P0M1 = 0x30; P0M0 = 0x30; //设置P0.4、P0.5为漏极开路(实验箱加了上拉电阻到3.3V)
//P1M1 = 0x30; P1M0 = 0x30; //设置P1.4、P1.5为漏极开路(实验箱加了上拉电阻到3.3V)
//P2M1 = 0x3c; P2M0 = 0x3c; //设置P2.2~P2.5为漏极开路(实验箱加了上拉电阻到3.3V)
//P3M1 = 0x50; P3M0 = 0x50; //设置P3.4、P3.6为漏极开路(实验箱加了上拉电阻到3.3V)
//P4M1 = 0x3c; P4M0 = 0x3c; //设置P4.2~P4.5为漏极开路(实验箱加了上拉电阻到3.3V)
//P5M1 = 0x0c; P5M0 = 0x0c; //设置P5.2、P5.3为漏极开路(实验箱加了上拉电阻到3.3V)
//P6M1 = 0xff; P6M0 = 0xff; //设置为漏极开路(实验箱加了上拉电阻到3.3V)
//P7M1 = 0x00; P7M0 = 0x00; //设置为准双向口
P1M1 = 0x00; P1M0 = 0x00;
XOSCCR = 0xc0; //启动外部晶振
while(!(XOSCCR & 0x01)); //等待时钟稳定
CLKDIV = 0x00; //时钟不分频
CLKSEL = 0x01; //选择外部晶振
UART2_config(2); // 选择波特率, 2: 使用Timer2做波特率, 其它值: 无效.
EA = 1; //允许全局中断
PrintString2("STC8H8K64U UART2 Test Programme!\r\n"); //UART2发送一个字符串
pelco_d_r[7] = 0;
while (1)
{
if((TX2_Cnt != RX2_Cnt) && (!B_TX2_Busy)) //收到数据, 发送空闲
{
//S2BUF = RX2_Buffer[TX2_Cnt];
pelco_d_c = RX2_Buffer[TX2_Cnt];
//B_TX2_Busy = 1;
if(++TX2_Cnt >= UART2_BUF_LENGTH) TX2_Cnt = 0;
//超时,接收归零
//if(pelco_d_to > 100)
//{
// pelco_d_num = 0;
//}
//在空闲状态下收到0xff,则进入等待状态。
/*if(pelco_d_num == 0)
{
if(RX2_Buffer[TX2_Cnt] == 0xff)
{
pelco_d_r[pelco_d_num] = 0xff;
pelco_d_num = 1;
}
}
*/
if(pelco_d_c == 0xff)
{
pelco_d_r[0] = 0xff;
pelco_d_num = 1;
}
//在等待状态下,接收到的新字节放到数组,num递增,7个后归零
else if(pelco_d_num > 0)
{
pelco_d_r[pelco_d_num] = pelco_d_c;
pelco_d_num ++;
if(pelco_d_num > 6)
{
pelco_d_num = 0;
PrintString2("pelco_d data is:");
PrintPelcoD();
PrintString2("\r\n");
}
}
}
}
}
//========================================================================
// 函数: void PrintString2(u8 *puts)
// 描述: 串口2发送字符串函数。
// 参数: puts: 字符串指针.
// 返回: none.
// 版本: VER1.0
// 日期: 2014-11-28
// 备注:
//========================================================================
void PrintString2(u8 *puts)
{
for (; *puts != 0; puts++) //遇到停止符0结束
{
while(B_TX2_Busy);
S2BUF = *puts;
B_TX2_Busy = 1;
while(B_TX2_Busy);
}
}
//========================================================================
// 函数:
// 描述:
// 参数:
// 返回:
// 版本:
// 日期:
// 备注:
//========================================================================
void PrintPelcoD(void)
{
u8 i = 0;
for (; i<7; i++) //遇到停止符0结束
{
while(B_TX2_Busy);
S2BUF = '0';
B_TX2_Busy = 1;
while(B_TX2_Busy);
S2BUF = 'x';
B_TX2_Busy = 1;
while(B_TX2_Busy);
if((pelco_d_r[i]/16) > 9)
{
S2BUF = ('a' + (pelco_d_r[i]/16) - 10);
}
else
{
S2BUF = ('0' + (pelco_d_r[i]/16));
}
B_TX2_Busy = 1;
while(B_TX2_Busy);
if((pelco_d_r[i]%16) > 9)
{
S2BUF = ('a' + (pelco_d_r[i]%16) - 10);
}
else
{
S2BUF = ('0' + (pelco_d_r[i]%16));
}
B_TX2_Busy = 1;
while(B_TX2_Busy);
S2BUF = ' ';
B_TX2_Busy = 1;
while(B_TX2_Busy);
}
}
//========================================================================
// 函数: SetTimer2Baudraye(u16 dat)
// 描述: 设置Timer2做波特率发生器。
// 参数: dat: Timer2的重装值.
// 返回: none.
// 版本: VER1.0
// 日期: 2014-11-28
// 备注:
//========================================================================
void SetTimer2Baudraye(u16 dat) // 使用Timer2做波特率.
{
AUXR &= ~(1<<4); //Timer stop
AUXR &= ~(1<<3); //Timer2 set As Timer
AUXR |= (1<<2); //Timer2 set as 1T mode
T2H = dat / 256;
T2L = dat % 256;
IE2 &= ~(1<<2); //禁止中断
AUXR |= (1<<4); //Timer run enable
}
//========================================================================
// 函数: void UART2_config(u8 brt)
// 描述: UART2初始化函数。
// 参数: brt: 选择波特率, 2: 使用Timer2做波特率, 其它值: 无效.
// 返回: none.
// 版本: VER1.0
// 日期: 2014-11-28
// 备注:
//========================================================================
void UART2_config(u8 brt) // 选择波特率, 2: 使用Timer2做波特率, 其它值: 无效.
{
/*********** 波特率固定使用定时器2 *****************/
if(brt == 2)
{
SetTimer2Baudraye(65536UL - (MAIN_Fosc / 4) / Baudrate2);
S2CON &= ~(1<<7); // 8位数据, 1位起始位, 1位停止位, 无校验
IE2 |= 1; //允许中断
S2CON |= (1<<4); //允许接收
P_SW2 &= ~0x01;
P_SW2 |= 0; //UART2 switch to: 0: P1.0 P1.1, 1: P4.6 P4.7
B_TX2_Busy = 0;
TX2_Cnt = 0;
RX2_Cnt = 0;
}
}
//========================================================================
// 函数: void UART2_int (void) interrupt UART2_VECTOR
// 描述: UART2中断函数。
// 参数: nine.
// 返回: none.
// 版本: VER1.0
// 日期: 2014-11-28
// 备注:
//========================================================================
void UART2_int (void) interrupt 8
{
if((S2CON & 1) != 0)
{
S2CON &= ~1; //Clear Rx flag
RX2_Buffer[RX2_Cnt] = S2BUF;
if(++RX2_Cnt >= UART2_BUF_LENGTH) RX2_Cnt = 0;
}
if((S2CON & 2) != 0)
{
S2CON &= ~2; //Clear Tx flag
B_TX2_Busy = 0;
}
}
复制代码