I'm using PIC16F688 to read from analog channel 2 and average the values of the pressure sensor and then convert the 4 bytes to digital using ASCII character method.
The code is pretty simple. I don't need any kind of delay after sending the results to UART1_Write(temp[i]);
My problem is that the UART 13 is not working properly as carriage return.
The output from terminal is looks like this:
000000000
0000
0000
000000000
It should be sending 0000
every time, and depend on my pressure on the sensor, a value from 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
}
}