-2

I'm using a 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.

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 looks like this: 000000000 0000 0000 000000000 when tt should be sending pure 0000 every time (and depending on my pressure on the sensor, from 0000 to 1023).

   char *temp = "0000";
   unsigned int adc_value;
   int i;
   int average = 0;

   void main() 
   {
     OSCCON = 0x77;  //8MHz
     ANSEL = 0b00000100;  //ANS2
     CMCON0 = 0X07;  
     TRISA = 0b00001100;
     UART1_Init(9600);
     Delay_ms(100);

     while (1)  //infinte loop
     { 
       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<4; i++){
         UART1_Write(temp[i]);
       }
       UART1_Write(13); // back slash
       //delay_ms(10);
     }
   }
rxmxn
  • 499
  • 1
  • 5
  • 17
  • 1
    `char *temp = "0000";` points to a *string literal* so you must not try to write to it with `temp[0] = average/1000+48;` etc. Better to declare it as `char temp[] = "0000";` – Weather Vane Nov 10 '15 at 16:23

1 Answers1

0

you are writing a line feed, or \r where you'd probably want to send either \r\n or only \n depending on your terminal. I'd recomend some tool to read your terminal in hex. In linux cat /dev/ttyUSB0 | hexdump would work.

Ps. using + '0' makes your character conversion somewhat more legible, but i think xc8 comes with stdlib.h so you can use itoa().

Good luck

David van rijn
  • 2,108
  • 9
  • 21