-5

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

             }

           }
Mogsdad
  • 44,709
  • 21
  • 151
  • 275
  • What are you asking of us? We have no hardware, no code, no compiler, no linker, no environment, no debugger, no logger and have no idea whether you have a software problem, a hardware problem or both. – Martin James Nov 10 '15 at 03:59
  • That is my code that has the problem could anyone help find why UART1_write didn't return each four bytes aside, I mean like a back slash \n. – Ahmed Mostfa Nov 10 '15 at 13:26

1 Answers1

0

I'd expect temp[4] to evaluate to 0x0D (CR), so every iteration of the 'while' loop is writing four digits, followed by two CRs, without any Linefeed. Try writing 0x0A to the UART after the 'for' loop, instead of 13.

If you're not clear on the difference between CR and LF, this answer does a good job of explaining it: https://stackoverflow.com/a/3098328/5544939

Community
  • 1
  • 1
Simon DeWolf
  • 400
  • 3
  • 10