在单片机调试中经常要用到串口调试工具,不过如果直接用的话,显示的数据只能是十六进制的形式或者是乱码,看起来很不爽。不过还好,可以在程序中先将十六进制数改为字符型的数据,然后再通过串口传送到上位机。这里主要用到的是一个ltoa函数。
下面看看itoa函数的使用:
itoa()函数的用法 int to char
itoa(i,num,10);
i 需要转换成字符的数字
num 转换后保存字符的变量
10 转换数字的基数(进制)10就是说按照10进制转换数字。还可以是2,8,16等等你喜欢的进制类型
程序例:
#include "generic.h"
#include "TC77.h"
#include "Console.h"
#include "string.h"
#include "MSPI.h"
unsigned char U8temp,U8FLAG,U8comdata;
unsigned char U8T_data_H = 0x01,U8T_data_L = 0x02,U8RH_data_H = 0x03,U8RH_data_L = 0x04,U8checkdata = 0x05;
unsigned char U8T_data_H_temp,U8T_data_L_temp,U8RH_data_H_temp,U8RH_data_L_temp,U8checkdata_temp;
void COM(void)
{
unsigned char i;
for(i=0;i<8;i++)
{
U8FLAG=2;
while((!RA4)&&U8FLAG++);
Delay_10us();
Delay_10us(); //时序要求较高,精确延时
Delay_10us();
U8temp=0;
if(RA4)U8temp=1;
U8FLAG=2;
while((RA4)&&U8FLAG++);
//超时则跳出for循环
if(U8FLAG==1)break;
//判断数据位是0还是1
// 如果高电平高过预定0高电平值则数据位为 1
U8comdata<<=1;
U8comdata|=U8temp; //0
}//rof
}
void RH(void)
{
//主机拉低 1-5ms
TRISA4 = 0;
LATA4=0;
Delay(15);
LATA4=1;
//总线由上拉电阻拉高 主机延时40us
Delay_10us();
Delay_10us(); //这个地方的时序要求不高
Delay_10us();
//主机设为输入 判断从机响应信号
LATA4=1;
TRISA4 = 1;
//判断从机是否有低电平响应信号 如不响应则跳出,响应则向下运行
if(!RA4) //T !
{
U8FLAG=2;
//判断从机是否发出 80us 的低电平响应信号是否结束
while((!RA4)&&U8FLAG++);
U8FLAG=2;
//判断从机是否发出 80us 的高电平,如发出则进入数据接收状态
while((RA4)&&U8FLAG++);
//数据接收状态
COM();
U8RH_data_H_temp=U8comdata;
COM();
U8RH_data_L_temp=U8comdata;
COM();
U8T_data_H_temp=U8comdata;
COM();
U8T_data_L_temp=U8comdata;
COM();
U8checkdata_temp=U8comdata;
// LATA4=1;
//数据校验
U8temp=(U8T_data_H_temp+U8T_data_L_temp+U8RH_data_H_temp+U8RH_data_L_temp);
if(U8temp==U8checkdata_temp)
if(1)
{
U8RH_data_H=U8RH_data_H_temp;
U8RH_data_L=U8RH_data_L_temp;
U8T_data_H=U8T_data_H_temp;
U8T_data_L=U8T_data_L_temp;
U8checkdata=U8checkdata_temp;
}//fi
}//fi
}
BYTE GetTC77String( char *buffer )
{
typedef union _SIGNED_WORD_VAL
{
unsigned int uVal;
int Val;
struct
{
BYTE LSB;
BYTE MSB;
} byte;
} SIGNED_WORD_VAL;
BYTE GIEHsave;
char *ptr;
char *pTempString;
SIGNED_WORD_VAL RawHum;
SIGNED_WORD_VAL RawTemp;
signed long ScaledTemp;
BYTE strLen;
char tempString[10];
GIEHsave = GIEH;
GIEH = 0;
RH();
RawHum.byte.MSB = U8RH_data_H; // Get high 8 bits
RawHum.byte.LSB = U8RH_data_L; // Get low 5 bits + "Bit 2" + 2 don't care bits (LSb side)
RawTemp.byte.MSB = U8T_data_H; // Get high 8 bits
RawTemp.byte.LSB = U8T_data_H; // Get low 5 bits + "Bit 2" + 2 don't care bits (LSb side)
GIEH = GIEHsave;
if ((RawHum.uVal == 0x0000) || (RawHum.uVal == 0xFFFF))
{
// strcpy( buffer, (const char *)"Problem reading TC77." );
}
else
{
ScaledTemp = (long)RawHum.Val;
pTempString = ltoa( ScaledTemp, tempString ); //将获取的数据转换为字符型
strLen = strlen( tempString );
ptr = buffer;
*ptr++ = 'H';
*ptr++ = 'U';
*ptr++ = 'M';
*ptr++ = ':';
*ptr++ = ' ';
if(tempString[0] == '-')
{
*ptr++ = '-';
strLen--;
pTempString++;
}
if(strLen < 2)
{
*ptr++ = '0';
*ptr++ = '.';
memcpy( (void *)ptr, (void *)tempString, strLen );
*ptr += strLen;
}
else
{
for(;strLen >= 2; strLen--)
*ptr++ = *pTempString++;
*ptr++ = '.';
for(;strLen > 0; strLen--)
*ptr++ = *pTempString++;
}
*ptr++ = '%';
*ptr++ = ' ';
*ptr++ = 'H';
*ptr++ = '/n';
}
if ((RawTemp.uVal == 0x0000) || (RawTemp.uVal == 0xFFFF))
{
// strcpy( buffer, (const char *)"Problem reading TC77." );
}
else
{
ScaledTemp = (long)RawTemp.Val;
// Convert the integer to a string and find its length
pTempString = ltoa( ScaledTemp, tempString );
strLen = strlen( tempString );
*ptr++ = 'T';
*ptr++ = 'E';
*ptr++ = 'M';
*ptr++ = ':';
*ptr++ = ' ';
if(tempString[0] == '-')
{
*ptr++ = '-';
strLen--;
pTempString++;
}
// Write "0." and then the number if this number is too close to zero; eg: 0, 0.0625, -0.125
if(strLen < 2)
{
*ptr++ = '0';
*ptr++ = '.';
memcpy( (void *)ptr, (void *)tempString, strLen );
*ptr += strLen;
}
else
{
for(;strLen >= 2; strLen--)
*ptr++ = *pTempString++;
*ptr++ = '.';
for(;strLen > 0; strLen--)
*ptr++ = *pTempString++;
}
// Write 癈
*ptr++ = ' ';
*ptr++ = 'C';
*ptr = '/0';
}
return strlen( buffer );
}
串口中可以显示如下结果(不再是乱码,或只能以十六进制的形式显示):