怎么把数据存在字符串中?
最近写单片机一个控制程序 遇到了瓶颈,请大神指点,问题如下:
8路AD采集采集 采集到的数据存在measurement[i] 中
利用采集的数据来发送串口指令实施控制,指令格式:UartSendStr("#1P500T100\r\n")//#1P代表第一个舵机#8P代表第8个舵机、500代表转动-90度2500代表转动+90度、T100\r\n为固定字符。
AD采集中断程序如下:
void ADC0_ISR (void) interrupt 15
{
static unsigned char channel = 0; // ADC mux 通道 (0-8)
unsigned short data temp;
ADC0CN&=0xdf;
AD0INT = 0; // 清除ADC转换完成标志
//result[channel] = ADC0; // 读ADC值
temp = ADC0H;
temp = temp*256 + ADC0L;
Result[channel] = temp; // 读ADC值
channel++; // 改变通道
if (channel == 9)
{
channel = 0;
}
AMX0SL = channel; // 设置mux到下个通道
//add++;
// if(add==80)
//{
m=1;
//add=0;
// }
}
发送串口指令程序如下:
void UartSendStr(unsigned char *pStr)
{
while(*pStr != 0)
{
SBUF0 = *pStr++;
//Delay(100);
while(TI0==0);
TI0=0;
}
}
void UartSendChar(unsigned char ch)
{
SBUF0 = ch;
while(TI0==0);
TI0=0;
}
void UartCharPro(unsigned char ch)
{
//P0 = ~ch;
switch(ch)
{
case '\b': // 退格键
if(g_ucCur > CMD_TAG)
{
UartSendChar('\b');
UartSendChar(' ');
UartSendChar('\b');
if(g_ucLen)
{
g_ucLen--;
}
g_ucCur--;
}
break;
case '\r': // 回车键
UartSendChar('\r');
UartSendChar('\n');
g_ucCmd[g_ucLen] = 0;
UartSendStr(g_ucCmd);
UartSendChar('\r');
UartSendChar('\n');
UartSendChar('>');
g_ucLen = 0;
g_ucCur = 1;
break;
default: // 其它字符
UartSendChar(ch);
g_ucCur++;
if(g_ucLen
{
g_ucCmd[g_ucLen++] = ch;
}
else
{
g_ucCmd[g_ucLen] = 0;
}
}
}
void PWM_Init()
{
//UartSendStr("#1P2500T100\r\n");
delay(10000); // 延时1s
UartSendStr("#1P500T100\r\n");
//delay(10000);
}
void UART0_ISR (void) interrupt 4
{
unsigned char ch;
ch = SBUF0;
if(RI0)
{
RI0 = 0;
//UartSendChar(ch);
//P0 = ~ch;
UartCharPro(ch);
}
}
/
怎么把采集到的数据measurement[i] (数值为500-2500的整数) 存到 需要发送的 UartSendStr("#1P500T100\r\n") 指令中,求大神指点,能帮忙写出来相应代码最好!
------解决思路----------------------
#include "C8051F020.h"
#include "stdio.h"
#include "intrins.h"
#include "math.h"
#include "string.h"
#define CMD_LEN 75 // 命令长度
#define CMD_TAG 1 // 提示符长度
unsigned char xdata g_ucCmd [CMD_LEN + 1] = {0};
unsigned char xdata g_ucCmd1[CMD_LEN + 1] = {0};
unsigned char g_ucLen = 0; // 命令长度
unsigned char g_ucCur = 1; // 光标位置
#define BAUDRATE 115200
#define SYSCLK 22118400
unsigned long xdata Result[8],measurement[8];
//unsigned long xdata measurement[8];
sbit LED=P3^3;
int m;
void UART0_Init (void) {
SCON0=0x50; //串口方式1
TMOD=0x20; //选用定时器1作为波特率发生器
TH1=0xF4;
TL1=0xF4;
PCON=0x80; //波特率为9600
TR1=1; //定时器启动
}
void ADC0_Init (void) {
ADC0CN = 0x04; // 关闭ADC0; 低能追踪模式
// DC0使用定时器Timer3溢出作为转换源;
//使用左对齐输出模式
REF0CN = 0x03; // 打开温度传感器, on-chip VREF,
// VREF输出缓存器
AMX0SL = 0x00; // 选择AIN0作为ADC mux输出
ADC0CF = 0x40; // PGA gain = 1
EIE2
------解决思路----------------------
= 0x02; // 打开ADC中断
}
void OSCILLATOR_Init (void) {
int i; // delay counter
OSCXCN = 0x67; // start external oscillator with
// 22.1184MHz crystal
for (i=0; i
while (!(OSCXCN & 0x80)) ; // Wait for crystal osc. to settle
OSCICN = 0x88; // select external oscillator as SYSCLK
// source and enable missing clock
// detector
}
void PORT_Init (void) {
XBR0 = 0x04; // Route UART0 to crossbar
XBR2
------解决思路----------------------
= 0x40; // Enable crossbar, weak pull-ups
P0MDOUT
------解决思路----------------------
= 0x01; // enable TX0 as a push-pull output
P3MDOUT
------解决思路----------------------
= 0xff; // enable LED as push-pull output
//P0MDOUT
------解决思路----------------------
= 0x01; // Set TX1 pin to push-pull
//P1MDOUT
------解决思路----------------------
= 0x40; // Set P1.6(LED) to push-pull
}
void TIMER3_Init (void) {
TMR3CN = 0x04; // 使用系统时钟分频作为定时器3 的时钟源(22M)
TMR3RLL = 0xd1;
TMR3RLH = 0xFF; // 用定时器3 作为 AD0的启动转换标志 每 100us启动一次采集。
TMR3L = 0xd1;
TMR3H = 0xFF;
}
void delay(unsigned int z) {
int x,y;
for(x=0;x
for(y=0;y<121;y++);
}
void UartSendStr(unsigned char *pStr)
{
while(*pStr != 0)
{
SBUF0 = *pStr++;
//Delay(100);
while(TI0==0);
TI0=0;
}
}
void UartSendChar(unsigned char ch)
{
SBUF0 = ch;
while(TI0==0);
TI0=0;
}
void UartCharPro(unsigned char ch)
{
//P0 = ~ch;
switch(ch) {
case '\b': // 退格键
if(g_ucCur > CMD_TAG) {
UartSendChar('\b');
UartSendChar(' ');
UartSendChar('\b');
if(g_ucLen) {
g_ucLen--;
}
g_ucCur--;
}
break;
case '\r': // 回车键
UartSendChar('\r');
UartSendChar('\n');
g_ucCmd[g_ucLen] = 0;
UartSendStr(g_ucCmd);
UartSendChar('\r');
UartSendChar('\n');
UartSendChar('>');
g_ucLen = 0;
g_ucCur = 1;
break;
default: // 其它字符
UartSendChar(ch);
g_ucCur++;
if(g_ucLen
g_ucCmd[g_ucLen++] = ch;
} else {
g_ucCmd[g_ucLen] = 0;
}
}
}
void main() {
int i;
//char str1[]={"#1P1000T10\r\n"};
//char str2[]={"#1P2000T10\r\n"};
WDTCN = 0xde; // Disable watchdog timer
WDTCN = 0xad;
OSCILLATOR_Init (); // Initialize oscillator
PORT_Init (); // Initialize crossbar and GPIO
UART0_Init (); // Initialize UART1
TIMER3_Init (); // Initialize Timer2 to overflow at 1 mS
ADC0_Init (); // Init ADC
AD0EN = 1; // Enable ADC
delay(50);
EA = 1;
//UartSendStr("#1P1000T10\r\n");
//delay(2000);
//UartSendStr("#1P1000T10\r\n");
//delay(2000);
TI0=1; // Enable global interrupts
while(1) {
if(m==1) {
//EA=0;
m=0;
TMR3CN&=0xFB; // 关定时器3
//delay(1500);
/* for(i=0;i<3;i++)
{
for(i=0; i<8; i++)
{
measurement[i] = (Result[i] * 2.430 / 4095);
LED=!LED;
//sprintf(g_ucCmd1,"#%dP%dT100\r\n",i+1,(measurement[i]*800)+500);
//UartSendStr(g_ucCmd1);
//delay(1500);
}
}
*/
for(i=0; i<8; i++) {
measurement[i] = (unsigned long)(Result[i] * 2430.0 / 4095.0);
//measurement1[i]=(measurement[i]*800)+500;
printf("AIN0.%d voltage: %lu mV\n", i+1, measurement[i]);
LED=!LED;
sprintf(g_ucCmd1,"#%dP%luT100\r\n",i+1,(unsigned long)(measurement[i]*0.8+500.0));
printf("g_ucCmd1 is %s",g_ucCmd1);
UartSendStr(g_ucCmd1);
delay(500);
}
//sprintf(g_ucCmd1,"#%dP%dT100\r\n",i,measurement[i]);
//UartSendStr(g_ucCmd1);
//delay(10000); // 延时1s
TMR3CN
------解决思路----------------------
=0x04; //开定时器3
//UartSendStr("#1P500T100\r\n");
//delay(10000);
}
}
}
void UART0_ISR (void) interrupt 4
{
unsigned char ch;
ch = SBUF0;
if(RI0) {
RI0 = 0;
//UartSendChar(ch);
//P0 = ~ch;
UartCharPro(ch);
}
}
void ADC0_ISR (void) interrupt 15
{
static unsigned char channel = 0; // ADC mux 通道 (0-7)
unsigned short data temp;
ADC0CN&=0xdf;
AD0INT = 0; // 清除ADC转换完成标志
//result[channel] = ADC0; // 读ADC值
temp = ADC0H;
temp = temp*256 + ADC0L;
Result[channel] = temp; // 读ADC值
channel++; // 改变通道
if (channel >= 8) {
channel = 0;
}
AMX0SL = channel; // 设置mux到下个通道
m=1;
}
------解决思路----------------------
#include "C8051F020.h"
#include "stdio.h"
#include "intrins.h"
#include "math.h"
#include "string.h"
#define CMD_LEN 75 // 命令长度
#define CMD_TAG 1 // 提示符长度
unsigned char xdata g_ucCmd [CMD_LEN + 1] = {0};
unsigned char xdata g_ucCmd1[CMD_LEN + 1] = {0};
unsigned char xdata DebugStr[8][CMD_LEN + 1];
unsigned char g_ucLen = 0; // 命令长度
unsigned char g_ucCur = 1; // 光标位置
#define BAUDRATE 115200
#define SYSCLK 22118400
unsigned long xdata Result[8],measurement[8];
//unsigned long xdata measurement[8];
sbit LED=P3^3;
int m;
void UART0_Init (void) {
SCON0=0x50; //串口方式1
TMOD=0x20; //选用定时器1作为波特率发生器
TH1=0xF4;
TL1=0xF4;
PCON=0x80; //波特率为9600
TR1=1; //定时器启动
}
void ADC0_Init (void) {
ADC0CN = 0x04; // 关闭ADC0; 低能追踪模式
// DC0使用定时器Timer3溢出作为转换源;
//使用左对齐输出模式
REF0CN = 0x03; // 打开温度传感器, on-chip VREF,
// VREF输出缓存器
AMX0SL = 0x00; // 选择AIN0作为ADC mux输出
ADC0CF = 0x40; // PGA gain = 1
EIE2
------解决思路----------------------
= 0x02; // 打开ADC中断
}
void OSCILLATOR_Init (void) {
int i; // delay counter
OSCXCN = 0x67; // start external oscillator with
// 22.1184MHz crystal
for (i=0; i
while (!(OSCXCN & 0x80)) ; // Wait for crystal osc. to settle
OSCICN = 0x88; // select external oscillator as SYSCLK
// source and enable missing clock
// detector
}
void PORT_Init (void) {
XBR0 = 0x04; // Route UART0 to crossbar
XBR2
------解决思路----------------------
= 0x40; // Enable crossbar, weak pull-ups
P0MDOUT
------解决思路----------------------
= 0x01; // enable TX0 as a push-pull output
P3MDOUT
------解决思路----------------------
= 0xff; // enable LED as push-pull output
//P0MDOUT
------解决思路----------------------
= 0x01; // Set TX1 pin to push-pull
//P1MDOUT
------解决思路----------------------
= 0x40; // Set P1.6(LED) to push-pull
}
void TIMER3_Init (void) {
TMR3CN = 0x04; // 使用系统时钟分频作为定时器3 的时钟源(22M)
TMR3RLL = 0xd1;
TMR3RLH = 0xFF; // 用定时器3 作为 AD0的启动转换标志 每 100us启动一次采集。
TMR3L = 0xd1;
TMR3H = 0xFF;
}
void delay(unsigned int z) {
int x,y;
for(x=0;x
for(y=0;y<121;y++);
}
void UartSendStr(unsigned char *pStr)
{
while(*pStr != 0)
{
SBUF0 = *pStr++;
//Delay(100);
while(TI0==0);
TI0=0;
}
}
void UartSendChar(unsigned char ch)
{
SBUF0 = ch;
while(TI0==0);
TI0=0;
}
void UartCharPro(unsigned char ch)
{
//P0 = ~ch;
switch(ch) {
case '\b': // 退格键
if(g_ucCur > CMD_TAG) {
UartSendChar('\b');
UartSendChar(' ');
UartSendChar('\b');
if(g_ucLen) {
g_ucLen--;
}
g_ucCur--;
}
break;
case '\r': // 回车键
UartSendChar('\r');
UartSendChar('\n');
g_ucCmd[g_ucLen] = 0;
UartSendStr(g_ucCmd);
UartSendChar('\r');
UartSendChar('\n');
UartSendChar('>');
g_ucLen = 0;
g_ucCur = 1;
break;
default: // 其它字符
UartSendChar(ch);
g_ucCur++;
if(g_ucLen
g_ucCmd[g_ucLen++] = ch;
} else {
g_ucCmd[g_ucLen] = 0;
}
}
}
void main() {
int i;
//char str1[]={"#1P1000T10\r\n"};
//char str2[]={"#1P2000T10\r\n"};
WDTCN = 0xde; // Disable watchdog timer
WDTCN = 0xad;
OSCILLATOR_Init (); // Initialize oscillator
PORT_Init (); // Initialize crossbar and GPIO
UART0_Init (); // Initialize UART1
TIMER3_Init (); // Initialize Timer2 to overflow at 1 mS
ADC0_Init (); // Init ADC
AD0EN = 1; // Enable ADC
delay(50);
EA = 1;
//UartSendStr("#1P1000T10\r\n");
//delay(2000);
//UartSendStr("#1P1000T10\r\n");
//delay(2000);
TI0=1; // Enable global interrupts
while(1) {
if(m==1) {
//EA=0;
m=0;
TMR3CN&=0xFB; // 关定时器3
//delay(1500);
/* for(i=0;i<3;i++)
{
for(i=0; i<8; i++)
{
measurement[i] = (Result[i] * 2.430 / 4095);
LED=!LED;
//sprintf(g_ucCmd1,"#%dP%dT100\r\n",i+1,(measurement[i]*800)+500);
//UartSendStr(g_ucCmd1);
//delay(1500);
}
}
*/
for(i=0; i<8; i++) {
printf("DebugStr[%d]=%s\n",i,DebugStr[i]);
measurement[i] = (unsigned long)(Result[i] * 2430.0 / 4095.0);
//measurement1[i]=(measurement[i]*800)+500;
printf("AIN0.%d voltage: %lu mV\n", i+1, measurement[i]);
LED=!LED;
sprintf(g_ucCmd1,"#%dP%luT100\r\n",i+1,(unsigned long)(measurement[i]*0.8+500.0));
printf("g_ucCmd1 is %s",g_ucCmd1);
UartSendStr(g_ucCmd1);
delay(500);
}
//sprintf(g_ucCmd1,"#%dP%dT100\r\n",i,measurement[i]);
//UartSendStr(g_ucCmd1);
//delay(10000); // 延时1s
TMR3CN
------解决思路----------------------
=0x04; //开定时器3
//UartSendStr("#1P500T100\r\n");
//delay(10000);
}
}
}
void UART0_ISR (void) interrupt 4
{
unsigned char ch;
ch = SBUF0;
if(RI0) {
RI0 = 0;
//UartSendChar(ch);
//P0 = ~ch;
UartCharPro(ch);
}
}
void ADC0_ISR (void) interrupt 15
{
static unsigned char channel = 0; // ADC mux 通道 (0-7)
unsigned short data tempH;
unsigned short data tempL;
ADC0CN&=0xdf;
AD0INT = 0; // 清除ADC转换完成标志
//result[channel] = ADC0; // 读ADC值
tempH = ADC0H;
tempL = ADC0L;
Result[channel] = tempH*256+tempL; // 读ADC值
sprintf(DebugStr[channel],"channel=%d,ADC0H=%hu,ADC0L=%hu,Result[%d]=%lu",channel,tempH,tempL,channel,Result[channel]);
channel++; // 改变通道
if (channel >= 8) {
channel = 0;
}
AMX0SL = channel; // 设置mux到下个通道
m=1;
}
------解决思路----------------------
#include "C8051F020.h"
#include "stdio.h"
#include "intrins.h"
#include "math.h"
#include "string.h"
#define AVGN 20 //各个通道采集AVGN个数据然后再求平均值以削平单次采集误差
#define CMD_LEN 75 // 命令长度
#define CMD_TAG 1 // 提示符长度
unsigned char xdata g_ucCmd [CMD_LEN + 1] = {0};
unsigned char xdata g_ucCmd1[CMD_LEN + 1] = {0};
unsigned char xdata DebugStr[8][CMD_LEN + 1];
unsigned char g_ucLen = 0; // 命令长度
unsigned char g_ucCur = 1; // 光标位置
#define BAUDRATE 115200
#define SYSCLK 22118400
unsigned long xdata Result[8][AVGN],measurement[8][AVGN],avgV;
sbit LED=P3^3;
int m;
int a=0;//当前采集次数0..AVGN-1
int flag=0;//标记是否已采集够AVGN个数据
void UART0_Init (void) {
SCON0=0x50; //串口方式1
TMOD=0x20; //选用定时器1作为波特率发生器
TH1=0xF4;
TL1=0xF4;
PCON=0x80; //波特率为9600
TR1=1; //定时器启动
}
void ADC0_Init (void) {
ADC0CN = 0x04; // 关闭ADC0; 低能追踪模式
// DC0使用定时器Timer3溢出作为转换源;
//使用左对齐输出模式
REF0CN = 0x03; // 打开温度传感器, on-chip VREF,
// VREF输出缓存器
AMX0SL = 0x00; // 选择AIN0作为ADC mux输出
ADC0CF = 0x40; // PGA gain = 1
EIE2
------解决思路----------------------
= 0x02; // 打开ADC中断
}
void OSCILLATOR_Init (void) {
int i; // delay counter
OSCXCN = 0x67; // start external oscillator with
// 22.1184MHz crystal
for (i=0; i
while (!(OSCXCN & 0x80)) ; // Wait for crystal osc. to settle
OSCICN = 0x88; // select external oscillator as SYSCLK
// source and enable missing clock
// detector
}
void PORT_Init (void) {
XBR0 = 0x04; // Route UART0 to crossbar
XBR2
------解决思路----------------------
= 0x40; // Enable crossbar, weak pull-ups
P0MDOUT
------解决思路----------------------
= 0x01; // enable TX0 as a push-pull output
P3MDOUT
------解决思路----------------------
= 0xff; // enable LED as push-pull output
//P0MDOUT
------解决思路----------------------
= 0x01; // Set TX1 pin to push-pull
//P1MDOUT
------解决思路----------------------
= 0x40; // Set P1.6(LED) to push-pull
}
void TIMER3_Init (void) {
TMR3CN = 0x04; // 使用系统时钟分频作为定时器3 的时钟源(22M)
TMR3RLL = 0xd1;
TMR3RLH = 0xFF; // 用定时器3 作为 AD0的启动转换标志 每 100us启动一次采集。
TMR3L = 0xd1;
TMR3H = 0xFF;
}
void delay(unsigned int z) {
int x,y;
for(x=0;x
for(y=0;y<121;y++);
}
void UartSendStr(unsigned char *pStr)
{
while(*pStr != 0)
{
SBUF0 = *pStr++;
//Delay(100);
while(TI0==0);
TI0=0;
}
}
void UartSendChar(unsigned char ch)
{
SBUF0 = ch;
while(TI0==0);
TI0=0;
}
void UartCharPro(unsigned char ch)
{
//P0 = ~ch;
switch(ch) {
case '\b': // 退格键
if(g_ucCur > CMD_TAG) {
UartSendChar('\b');
UartSendChar(' ');
UartSendChar('\b');
if(g_ucLen) {
g_ucLen--;
}
g_ucCur--;
}
break;
case '\r': // 回车键
UartSendChar('\r');
UartSendChar('\n');
g_ucCmd[g_ucLen] = 0;
UartSendStr(g_ucCmd);
UartSendChar('\r');
UartSendChar('\n');
UartSendChar('>');
g_ucLen = 0;
g_ucCur = 1;
break;
default: // 其它字符
UartSendChar(ch);
g_ucCur++;
if(g_ucLen
g_ucCmd[g_ucLen++] = ch;
} else {
g_ucCmd[g_ucLen] = 0;
}
}
}
void main() {
int i,j;
//char str1[]={"#1P1000T10\r\n"};
//char str2[]={"#1P2000T10\r\n"};
WDTCN = 0xde; // Disable watchdog timer
WDTCN = 0xad;
OSCILLATOR_Init (); // Initialize oscillator
PORT_Init (); // Initialize crossbar and GPIO
UART0_Init (); // Initialize UART1
TIMER3_Init (); // Initialize Timer2 to overflow at 1 mS
ADC0_Init (); // Init ADC
AD0EN = 1; // Enable ADC
delay(50);
EA = 1;
//UartSendStr("#1P1000T10\r\n");
//delay(2000);
//UartSendStr("#1P1000T10\r\n");
//delay(2000);
TI0=1; // Enable global interrupts
while(1) {
if(m==1) {
//EA=0;
m=0;
TMR3CN&=0xFB; // 关定时器3
for(i=0; i<8; i++) {
printf("DebugStr[%d]=%s\n",i,DebugStr[i]);
measurement[i][a] = (unsigned long)(Result[i][a] * 2430.0 / 4095.0);
if (flag) {
avgV=0;
for (j=0;j
avgV/=AVGN;
printf("AIN0.%d voltage: %lu mV\n", i+1, avgV);
LED=!LED;
sprintf(g_ucCmd1,"#%dP%luT100\r\n",i+1,(unsigned long)(avgV*0.8+500.0));
printf("g_ucCmd1 is %s",g_ucCmd1);
UartSendStr(g_ucCmd1);
}
delay(500);
}
//sprintf(g_ucCmd1,"#%dP%dT100\r\n",i,measurement[i]);