/*********************合泰单片机IO口模拟UART串口通讯实验**********************/
UART源文件
#include "UART.h"
#include "HT66F70A.h"
#include "TM.h"
#include "String.h"
//全局变量区
volatile unsigned int flag = 0;
char buff[num];
char Buff[32] = "\0";
unsigned int count = 0;
void UART_INIT(void)
{
TM0_INIT();
}
void Waitflag(void)
{
while (!flag);
flag=0;//用flag模拟51单片机的计时器溢出标志TF0
}
void WByte(unsigned char input)
{
unsigned char i=8;
TXD=0;
_t0on=1; //开始计时
Waitflag();
while(i--)
{
TXD=input & 0x01;
input >>= 1;
Waitflag();
}
TXD=1;
Waitflag();
_t0on = 0;
}
void Send(char *buf)
{
while(*buf != '\0')
{
WByte(*buf);
buf++;
}
}
void Send_num(unsigned int s,char *buf)
{
while(s--)
{
WByte(*buf);
buf++;
}
}
void Send_Array(void)
{
unsigned int i;
for(i = 0;i < num;i++)
WByte(buff[i]);
}
unsigned char RByte(void)
{
unsigned char receive;
unsigned char i=8;
while(RXD);
_t0on=1; //开始计时
Waitflag();
while(i--)
{
receive >>= 1;
if(RXD)
receive |= 0x80;
Waitflag();
}
_t0on = 0;
return receive;
}
void Receive(void)
{
for(count = 0;count < num;count++)
{
buff[count] = RByte();
}
}
void Receive_num(unsigned int s,char *buf)
{
while(s--)
{
*buf = RByte();
buf++;
}
}
void Receive_if(char *buf)
{
char get;
while(1)
{
get = RByte();
if(get == '\n')
break;
else
{
*buf = get;
buf++;
}
}
}
#ifndef _UART_H_
#define _UART_H_
//定义TXD RXD
#define TXD _pc2
#define RXD _pc3
//定义接收字符串长度
#define num 16
void UART_INIT(void);
void Waitflag();
void WByte(unsigned char input);
unsigned char RByte(void);
void Send(char *buf);//发送char型字符串
void Send_num(unsigned int s,char *buf);//发送s个字符
void Send_Array(void);//发送缓冲区
void Receive(void);//接收num个字符到缓冲区
void Receive_num(unsigned int s,char *buf);//接收s个字符
void Receive_if(char *buf);//以'\n'结尾的条件接收
#endif
#include "HT66F70A.h"
#include "TM.h"
//专用于模拟串口的定时器
void TM0_INIT(void)
{
//TXD RXD初始化
_pcc2 = 0;
_pcc3 = 1;
_pcpu3 = 1;
_pcpu2 = 1;
_tm0c0=0x10;//计数器暂停运行控制,时钟选择,TM总开关TnPAU=1 暂停
_tm0c1=0xc1;//工作模式,清零条件位
_tm0al=0x41;//设置波特率为9600
_tm0ah=0x03;
// _tm0al=0x05;//设置波特率为2400
// _tm0ah=0x0d;
_t0ae = 1;
_t0af = 0;
_t0on = 0;
_emi = 1;
_mf0e = 1;
_mf0f = 0;
}
#ifndef _TM_H_
#define _TM_H_
void TM0_INIT(void);
#endif
#ifndef _INTERRUPT_H_
#define _INTERRUPT_H_
#endif
#define ISR_TMR0//定义为TM0或TM2
void __attribute((interrupt(0x14))) ISR_tmr0_tmr2(void)//TM0和TM2公用中断入口
{
//定义为ISR_TMR0时
#ifdef ISR_TMR0
extern volatile unsigned int flag;
_emi = 0;
_t0af = 0;
flag = 1;
_emi = 1;
//定义为ISR_TMR2时
#else
_emi = 0;
_t2af = 0;
_emi = 1;
#endif
}
TEST主函数#include "HT66F70A.h" #include "Interrupt.h" #include "TM.h" #include "UART.h" #include "String.h" //全局变量区 extern char buff[num]; extern char Buff[32]; extern unsigned int count; //函数声明区 void delay1s(void); //主程序入口 void main() { //TO DO _wdtc = 0xab;//看门狗失能 unsigned char key; UART_INIT(); Send("test"); Send("单片机UART通讯测试"); delay1s(); Receive(); Send_Array(); Receive_if(Buff); //Send_num(8,Buff); Send(Buff); } while(1); } void delay1s(void) //误差 -1us { unsigned char a,b,c,n; for(c=205;c>0;c--) for(b=171;b>0;b--) for(a=8;a>0;a--); for(n=1;n>0;n--); }
*此版为2018/4/3最新稳定更新版(copy可用版),还是老问题,接收时用Receive_if()做条件接收./****************************END******************************/