Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

need help getting ascii from a char array

Status
Not open for further replies.

aj9999

Junior Member level 3
Joined
Feb 5, 2010
Messages
28
Helped
1
Reputation
2
Reaction score
0
Trophy points
1,281
Location
Missouri
Activity points
1,456
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.
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;
}
Thanks
 

Try this:

get the character from the array into a variable, lets call it BCDChar.

High ascii character is ((BCDChar & 0xf0) >> 4) + 0x30
Low ascii character is (BCDChar & 0x0f) +0x30

If you have a function like 'ctoa()' use that instead of adding 0x30.

Brian.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top