我正在使用 PIC16F688 从模拟通道 2 读取并平均压力传感器的值,然后使用 ASCII 字符方法将 4 个字节转换为数字。代码非常简单。将结果发送到我的问题后,我不需要任何延迟。UART1_Write(temp[i]);
我的问题是 UART 13 不能作为回车正常工作。终端的输出如下所示:
000000000
0000
0000
000000000
它应该0000
每次都发送,并且取决于我对传感器的压力,一个从0000
to的值1023
。
char temp[5];
unsigned int adc_value;
char uart_rd;
int i;
unsigned int d[10]={0};
int average = 0;
int counter =0;
void main() {
temp[0]='0';
temp[1]='0';
temp[2]='0';
temp[3]='0';
temp[4]='\r';
OSCCON = 0x77; //8MHz
ANSEL = 0b00000100;
CMCON0 = 0X07; //
TRISA = 0b00001100;
UART1_Init(9600); // Initialize UART module at 9600 bps
Delay_ms(100); // Wait for UART module to stabilize
while (1) {
average=0;
for(i=0;i<10;i++){
average+= ADC_Read(2);
}
average/=10;
temp[0] = average/1000+48;
temp[1] = (average/100)%10+48;
temp[2] = (average/10)%10+48;
temp[3] = average%10+48;
for (i=0;i<5; i++)
{
UART1_Write(temp[i]);
}
UART1_Write(13); // back slash
}
}