aj9999
Junior Member level 3
I am trying to get data from a temp sensor using MPLAB and C18, I poll the sensor and feed the values into a char array and then pass that array to another function, then send it to the LCD. I cannot get the LCD to display the number in decimal, presumably i need to change the BCD in the char array to ascii but my attempts were unsuccesful. THe BCD values that i am getting appear correct so what command can i use to convert it? i tried atoi but that didnt work.
Thanks
Code:
#define LED1 PORTBbits.RB1
#define LED2 PORTBbits.RB2
#define button PORTAbits.RA4
unsigned char line1[17] = "! 1st line !";
unsigned char line2[17] = " ";
int i,l,h,ans = 0;
unsigned char *bcd_temp;
unsigned char *SPI_get(void);
void main()
{
TRISA = 0x10;
TRISB = 0x00;
TRISD = 0x00;
TRISE = 0x00;
PORTB = 0x00;
PORTD = 0x00;
LCD_init();
while(1)
{
ans = 0;
bcd_temp = SPI_get();
i=0;
while(i<=16)
{
line1[i] = bcd_temp[i] + 0x30; //converts to display 16bit bcd in ascii
i++; //currently 0000 1011 1001 1111 after +0x30
}
for (i=0; i<=8; i++) // get rid of lower 7 bits making 0 0001 0111 =23 or similar
{
line2[i+7] = bcd_temp[i] + 0x30; //
}
// ans = atoi (line2);
// h = ans/0x0a + 0x30;
// l = ans%0x0a + 0x30;
//
// line1[7] = ' ';
// line1[8] = h;
// line1[9] = l;
// line1[10] = ' ';
LCD_write_line(1,line1); //currently 0000 1011 1001 1111, upper 9 bits give 23
LCD_write_line(2,line2);
}
}
unsigned char *SPI_get(void)
{
SPI_SDI_DIR = 1;
SPI_SCK_DIR = 0;
SPI_CS_DIR = 0;
ADCON1 = 0x0f;
counter = 0x10;
SPI_SCK = 0;
SPI_CS = 0;
i=0;
while(i<=16)
{
SPI_SCK = 1;
temp[i] = SPI_SDI;
SPI_SCK = 0;
i++;
}
SPI_CS = 1;
return temp;
}