user-0 提问: 怎么把数据存在字符串中？

回复:
user: user-1
content: #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 < 256; i++) ;           // wait for oscillator to start     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<z;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 < CMD_LEN) {             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<AVGN;j++) avgV+=measurement[i][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]);              //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][a] = tempH*256+tempL;             // 读ADC值     sprintf(DebugStr[channel],"channel=%d,ADC0H=%hu,ADC0L=%hu,Result[%d][%d]=%lu",channel,tempH,tempL,channel,a,Result[channel][a]);      channel++;                          // 改变通道     if (channel >= 8) {         channel = 0;         a++;         if (a>=AVGN) {             a=0;             flag=1;         }     }      AMX0SL = channel;                   // 设置mux到下个通道     m=1; }

user: user-0
content: 还有没有更好的数据处理方法？除了平均值法？

user: user-1
content: 引用 3 楼 qq_30890213 的回复:为什么采集到的数据不对呢 ，是不是measurement[i]   %d 数据类型有错误呢？ 该怎么处理吖 measurement[i]的类型是啥，对应%啥 %d int Signed decimal integer. %u unsigned int %g double or float %s char *

user: user-0
content: 为什么采集到的数据不对呢 ，是不是measurement[i]   %d 数据类型有错误呢？ 该怎么处理吖

user: user-0
content: 在这个基础上，怎么让各个通道采集20个数据然后再求平均值进行呢，单次采集误差有点大，   有什么好的编程方法么    谢谢老师帮忙啦

user: user-1
content: #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 < 256; i++) ;           // wait for oscillator to start     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<z;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 < CMD_LEN) {             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; }

user: user-0
content: 硬件的问题，，程序差不多了 ， 就是采集电压的时候，采集到的电压变化值之间延迟比较长

user: user-0
content: 还是不行，采集到的数据一直显示 2430或者0， 不是实际的电压值呢

user: user-1
content: #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 < 256; i++) ;           // wait for oscillator to start     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<z;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 < CMD_LEN) {             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; }

user: user-0
content: #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 < 256; i++) ;           // wait for oscillator to start    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<z;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 < CMD_LEN)  			{  	           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] = Result[i] * 2430 / 4095; 		 //measurement1[i]=(measurement[i]*800)+500;          printf("AIN0.%d voltage: %ld mV\n",i, measurement[i]);          LED=!LED;  		 sprintf(g_ucCmd1,"#%dP%dT100\r\n",i+1,measurement[i]*0.8+500); 		 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-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到下个通道    m=1;  }       这个采集到的为什么不对并，且采集到的变化量一直是定值呢

user: user-1
content: #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; // 光标位置 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 < CMD_LEN) {             g_ucCmd[g_ucLen++] = ch;         } else {             g_ucCmd[g_ucLen] = 0;         }     } } void PWM_Init() {     //UartSendStr("#1P2500T100\r\n");     delay(10000);   // 延时1s     //把采集到的数据 （数值为500-2500的整数） 存到 需要发送的 UartSendStr("#1P500T100\r\n") 指令中     sprintf(g_ucCmd1,"#1P%dT100\r\n",measurement[i]);     UartSendStr(g_ucCmd1);     //delay(10000); } void UART0_ISR (void) interrupt 4 {     unsigned char ch;     ch = SBUF0;     if(RI0) {     RI0 = 0;     //UartSendChar(ch);     //P0 = ~ch;     UartCharPro(ch);     } } /////////////////////////////////////////

user: user-0
content: #define CMD_LEN 75 // 命令长度 #define CMD_TAG 1 // 提示符长度 unsigned  char xdata g_ucCmd[CMD_LEN + 1] = {0}; unsigned char g_ucLen = 0; // 命令长度 unsigned char g_ucCur = 1; // 光标位置