Conversion of data recieved by Xbee ....

Bluetooth, XBee, RF......

Conversion of data recieved by Xbee ....

Postby Kromuald13 » Sun Mar 11, 2012 1:53 am

Hi to All
i am Romuald , and i am working on a project which required an established wireless communications between two parties .
I Used xbee for the communication .. and so far everything is fine , as i can send characters and strings
However , i will like to send some computational data , that is an integer that could be used at the receiving part for further calculation .

I am using pic 16f877a , with HI TECH compilers , And i will like to convert the string received by xbee to an integer
that is from "123" as string to 123 as integer ..

THX
Kromuald13
Novice
 
Posts: 26
Joined: Wed Feb 09, 2011 2:03 am

Re: Conversion of data recieved by Xbee ....

Postby ABSF » Sun Mar 11, 2012 7:50 am

Kromuald13 WROTE:I am using pic 16f877a , with HI TECH compilers , And i will like to convert the string received by xbee to an integer
that is from "123" as string to 123 as integer ..


Did you check the atoi() function on page 182 of the old Hi Tech Manual?
Another manual of the same name is on chapter 7 - Library functions.

Allen
The next war will determine NOT who is right BUT what is left.
User avatar
ABSF
Professional
 
Posts: 810
Joined: Wed Nov 10, 2010 9:32 am
Location: E Malaysia

Re: Conversion of data recieved by Xbee ....

Postby Kromuald13 » Sun Mar 11, 2012 6:33 pm

THX Allen for your reply
i am being through the libraire but as i try it
i am having the following error :

Error [499] ; 0. undefined symbol:
_code(DataConversion.obj)


here below are my codes

CODE: SELECT_ALL_CODE
#include<pic.h>
#include<stdlib.h>
#include<math.h>
#include <stdio.h>
//#include <conio.h>
//#include <stdlib.h>
#include<htc.h>
__CONFIG (0x3F32);

#ifndef _XTAL_FREQ
#define _XTAL_FREQ 4000000
#endif

#define lcd        PORTD
#define rs         RB4
#define e          RB5

unsigned char data;
unsigned char code[];
int value;
int i,n;

// Prototypes================================================================================

//void delay(unsigned short i);

void send_config(unsigned char data);
void send_char(unsigned char data);
void lcd_goto(unsigned char data);
void lcd_clr(void);
void send_string(const char *s);
void dis_num(unsigned long data);
void e_pulse(void);
//int atoi(unsigned char code[]);
int atoi (const char * s);




void  main(void)
{
   ADCON1=0b00000110;
        TRISB=0b00000011;
        TRISC=0b01000000;
        TRISD=0b00000000;
   

        PORTC=0;
      PORTD=0;
       PORTB=0;



   send_config(0b00001001);   //clear display at lcd
   send_config(0b00000010);   //Lcd Return to home
   send_config(0b00000110);   //entry mode-cursor increase 1
   send_config(0b00001100);   //diplay on, cursor off and                //cursor blink off
   send_config(0b00111000);   //function   
   
   char code[4] ="1234";

   
while(1)
{
   //gets(code);
 value = atoi(code);
lcd_goto(5);
send_string(" the value is ");
lcd_goto(20);

dis_num(value);
}


}






//Functions




void send_char(unsigned char data)               //send lcd character
{
    rs=1;                        //set lcd to display mode
   lcd=data;                     //lcd data port = data
   e=1;                        //pulse e to confirm the data
   __delay_ms(10); //before 10
   e=0;
   __delay_ms(10);
}


void lcd_goto(unsigned char data)               //set the location of the lcd cursor
{                           //if the given value is (0-15) the
    if(data<16)                     //cursor will be at the upper line
   {                        //if the given value is (20-35) the
       send_config(0x80+data);               //cursor will be at the lower line
   }                        //location of the lcd cursor(2X16):
   else                        // -----------------------------------------------------
   {                        // | |00|01|02|03|04|05|06|07|08|09|10|11|12|13|14|15| |
       data=data-20;                  // | |20|21|22|23|24|25|26|27|28|29|30|31|32|33|34|35| |
      send_config(0xc0+data);               // -----------------------------------------------------   
   }
}


void lcd_clr(void)                     //clear the lcd
{
    send_config(0x01);
__delay_ms(60);
}

void send_string(const char *s)                  //send a string to display in the lcd
{         
     while (s && *s)send_char (*s++);
}


void e_pulse(void)
{
   e=1;
   __delay_ms(10); //before 50000
   e=0;
   __delay_ms(10); // before 50000
}

void send_config(unsigned char data)
{
    rs=0;
   lcd = data;
   __delay_ms(10); // before 50000
   e_pulse();
}





void dis_num(unsigned long data)
{
   unsigned char hundred_thousand;
   unsigned char ten_thousand;
   unsigned char thousand;
   unsigned char hundred;
   unsigned char tenth;

   hundred_thousand = data/100000;               
   data = data % 100000;
   ten_thousand = data/10000;
   data = data % 10000;
   thousand = data / 1000;
   data = data % 1000;
   hundred = data / 100;
   data = data % 100;
   tenth = data / 10;
   data = data % 10;

   if(hundred_thousand>0)
   {
      send_char(hundred_thousand + 0x30);   //0x30 added to become ASCII code
      send_char(ten_thousand + 0x30);
      send_char(thousand + 0x30);
      send_char(hundred + 0x30);
      send_char(tenth + 0x30);
      send_char(data + 0x30);
   }

   else if(ten_thousand>0)
   {
      send_char(ten_thousand + 0x30);   //0x30 added to become ASCII code
      send_char(thousand + 0x30);
      send_char(hundred + 0x30);
      send_char(tenth + 0x30);
      send_char(data + 0x30);
   }
   else if(thousand>0)
   {
       send_char(thousand + 0x30);   //0x30 added to become ASCII code
      send_char(hundred + 0x30);
      send_char(tenth + 0x30);
      send_char(data + 0x30);
   }
   else if(hundred>0)
   {
       send_char(hundred + 0x30);   //0x30 added to become ASCII code
      send_char(tenth + 0x30);
      send_char(data + 0x30);
   }
   else if(tenth>0)
    {
      send_char(tenth + 0x30);   //0x30 added to become ASCII code
      send_char(data + 0x30);
   }
   else send_char(data + 0x30);   //0x30 added to become ASCII code
}


In this program , i want to convert a string code " 1234" into an integer value
and display it on a LCD screen ..
thx
Kromuald13
Novice
 
Posts: 26
Joined: Wed Feb 09, 2011 2:03 am

Re: Conversion of data recieved by Xbee ....

Postby ABSF » Sun Mar 11, 2012 9:22 pm

Kromuald13 WROTE:In this program , i want to convert a string code " 1234" into an integer value
and display it on a LCD screen ..


I am confused here. Why you need to convert ASC to integer in order to display on the LCD? The LCD can take a string and display using send_string("string") and an ASC character and display using send_char(char). The atoi() is used to convert ASC 1 to 5 chars to 16-bit INT for storage.

Is it possible to send code[4]="1234" to LCD like this?

char code[4]="1234"

lcd_goto(1);
for (unsigned int i=0;i<4;i++)
{ send_char(code[i]);
}

Allen
The next war will determine NOT who is right BUT what is left.
User avatar
ABSF
Professional
 
Posts: 810
Joined: Wed Nov 10, 2010 9:32 am
Location: E Malaysia

Re: Conversion of data recieved by Xbee ....

Postby Kromuald13 » Sun Mar 11, 2012 9:31 pm

Hey thx for ur reply allen
sending to the lcd is just a test for myself to check if the conversion from string to integer had been successful
my prime aim , is to be able to collect data of integer or double type from a xbee
And since , the xbee output are string or characters and want to convert those into integers or doubles for further computational work
....

thx once more
From my last post , i advance a little bit
the stuff , compile successfully but i am having strange sign at the LCD ..
have a look on this code pls

CODE: SELECT_ALL_CODE
#include<pic.h>
#include<stdlib.h>
#include <stdio.h>
#include<htc.h>
__CONFIG (0x3F32);

#ifndef _XTAL_FREQ
#define _XTAL_FREQ 4000000
#endif

#define lcd        PORTD
#define rs         RB4
#define e          RB5

unsigned char data;
unsigned char c[3] = "123";
int value;
int i,n;

// Prototypes================================================================================

//void delay(unsigned short i);

void send_config(unsigned char data);
void send_char(unsigned char data);
void lcd_goto(unsigned char data);
void lcd_clr(void);
void send_string(const char *s);
void dis_num(unsigned long data);
void e_pulse(void);


void  main(void)
{
   ADCON1=0b00000110;
        TRISB=0b00000011;
        TRISC=0b01000000;
        TRISD=0b00000000;
   

        PORTC=0;
      PORTD=0;
       PORTB=0;



   send_config(0b00001001);   //clear display at lcd
   send_config(0b00000010);   //Lcd Return to home
   send_config(0b00000110);   //entry mode-cursor increase 1
   send_config(0b00001100);   //diplay on, cursor off and                //cursor blink off
   send_config(0b00111000);   //function   
   
  unsigned char c[3] = "123";

   
while(1)
{
int value = atoi(c[3]);
lcd_goto(0);
send_string("the value is");
lcd_goto(20);

dis_num(value);
}


}



//Functions


void send_char(unsigned char data)               //send lcd character
{
    rs=1;                        //set lcd to display mode
   lcd=data;                     //lcd data port = data
   e=1;                        //pulse e to confirm the data
   __delay_ms(500); //before 10
   e=0;
   __delay_ms(500);
}


void lcd_goto(unsigned char data)               //set the location of the lcd cursor
{                           //if the given value is (0-15) the
    if(data<16)                     //cursor will be at the upper line
   {                        //if the given value is (20-35) the
       send_config(0x80+data);               //cursor will be at the lower line
   }                        //location of the lcd cursor(2X16):
   else                        // -----------------------------------------------------
   {                        // | |00|01|02|03|04|05|06|07|08|09|10|11|12|13|14|15| |
       data=data-20;                  // | |20|21|22|23|24|25|26|27|28|29|30|31|32|33|34|35| |
      send_config(0xc0+data);               // -----------------------------------------------------   
   }
}


void lcd_clr(void)                     //clear the lcd
{
    send_config(0x01);
__delay_ms(600);
}

void send_string(const char *s)                  //send a string to display in the lcd
{         
     while (s && *s)send_char (*s++);
}


void e_pulse(void)
{
   e=1;
   __delay_ms(500); //before 50000
   e=0;
   __delay_ms(500); // before 50000
}

void send_config(unsigned char data)
{
    rs=0;
   lcd = data;
   __delay_ms(500); // before 50000
   e_pulse();
}





void dis_num(unsigned long data)
{
   unsigned char hundred_thousand;
   unsigned char ten_thousand;
   unsigned char thousand;
   unsigned char hundred;
   unsigned char tenth;

   hundred_thousand = data/100000;               
   data = data % 100000;
   ten_thousand = data/10000;
   data = data % 10000;
   thousand = data / 1000;
   data = data % 1000;
   hundred = data / 100;
   data = data % 100;
   tenth = data / 10;
   data = data % 10;

   if(hundred_thousand>0)
   {
      send_char(hundred_thousand + 0x30);   //0x30 added to become ASCII code
      send_char(ten_thousand + 0x30);
      send_char(thousand + 0x30);
      send_char(hundred + 0x30);
      send_char(tenth + 0x30);
      send_char(data + 0x30);
   }

   else if(ten_thousand>0)
   {
      send_char(ten_thousand + 0x30);   //0x30 added to become ASCII code
      send_char(thousand + 0x30);
      send_char(hundred + 0x30);
      send_char(tenth + 0x30);
      send_char(data + 0x30);
   }
   else if(thousand>0)
   {
       send_char(thousand + 0x30);   //0x30 added to become ASCII code
      send_char(hundred + 0x30);
      send_char(tenth + 0x30);
      send_char(data + 0x30);
   }
   else if(hundred>0)
   {
       send_char(hundred + 0x30);   //0x30 added to become ASCII code
      send_char(tenth + 0x30);
      send_char(data + 0x30);
   }
   else if(tenth>0)
    {
      send_char(tenth + 0x30);   //0x30 added to become ASCII code
      send_char(data + 0x30);
   }
   else send_char(data + 0x30);   //0x30 added to become ASCII code
}





Kromuald13
Novice
 
Posts: 26
Joined: Wed Feb 09, 2011 2:03 am

Re: Conversion of data recieved by Xbee ....

Postby Kromuald13 » Sun Mar 11, 2012 9:54 pm

And now the lcd is showing nothing at all ,
and once the sentence appeared : " the value is " and it ouputs ' o'
Kromuald13
Novice
 
Posts: 26
Joined: Wed Feb 09, 2011 2:03 am

Re: Conversion of data recieved by Xbee ....

Postby ABSF » Sun Mar 11, 2012 9:57 pm

CODE: SELECT_ALL_CODE
void send_dec(unsigned long data,unsigned char num_dig)      //convert binary number and display number in decimal
{
   if(num_dig>=10)                                 
   {
      data=data%10000000000;
      send_char(data/1000000000+0x30);
   }   
   if(num_dig>=9)
   {
      data=data%1000000000;
      send_char(data/100000000+0x30);
   }   
   if(num_dig>=8)
   {
      data=data%100000000;
      send_char(data/10000000+0x30);
   }   
   if(num_dig>=7)
   {
      data=data%10000000;
      send_char(data/1000000+0x30);
   }   
   if(num_dig>=6)
   {
      data=data%1000000;
      send_char(data/100000+0x30);
   }   
   if(num_dig>=5)
   {
      data=data%100000;
      send_char(data/10000+0x30);
   }   
   if(num_dig>=4)
   {
      data=data%10000;
      send_char(data/1000+0x30);
   }
   if(num_dig>=3)
   {
      data=data%1000;
      send_char(data/100+0x30);
   }
   if(num_dig>=2)
   {
      data=data%100;
      send_char(data/10+0x30);
   }
   if(num_dig>=1)
   {
      data=data%10;
      send_char(data+0x30);
   }
}


I found the above code in PR25. To call it you have to specify the number to display and the number of digits as well.
In your case your code would be

while(1)
{
int value = atoi(c[3]);
lcd_goto(0);
send_string("the value is");
lcd_goto(20);

send_dec(value,3);
}


Allen
The next war will determine NOT who is right BUT what is left.
User avatar
ABSF
Professional
 
Posts: 810
Joined: Wed Nov 10, 2010 9:32 am
Location: E Malaysia

Re: Conversion of data recieved by Xbee ....

Postby Kromuald13 » Sun Mar 11, 2012 10:25 pm

hey Allen
thx for the code , but though i have read PR25
, i can't still understand what does the( value, 3)
why the " 3" pls
thx
Kromuald13
Novice
 
Posts: 26
Joined: Wed Feb 09, 2011 2:03 am

Re: Conversion of data recieved by Xbee ....

Postby Kromuald13 » Sun Mar 11, 2012 10:34 pm

the Lcd is working perectly now
problem from the IC
but now the value shown is " 000"
don't know....
pls help
Kromuald13
Novice
 
Posts: 26
Joined: Wed Feb 09, 2011 2:03 am

Re: Conversion of data recieved by Xbee ....

Postby ABSF » Mon Mar 12, 2012 9:54 am

Kromuald13 WROTE:hey Allen
thx for the code , but though i have read PR25
, i can't still understand what does the( value, 3)
why the " 3" pls
thx


void send_dec(unsigned long data,unsigned char num_dig) 


send_dec requires 2 parameters. data is 32 bit binary number (not sure) while num_dig is an 8-bit number telling the number of digits to display. If your value is 34 and you use send_dec(value,3); It will display 034 on the LCD.

Kromuald13 WROTE:the Lcd is working perectly now
problem from the IC
but now the value shown is " 000"
don't know....
pls help


May be it has something to do with the atoi() function. I am not sure it can convert Array like char c[3] in "int value=atoi(c[3]);" to an integer value. May be you should convert c[3] into a string then use atoi(). Do some experiments yourself.

Allen
The next war will determine NOT who is right BUT what is left.
User avatar
ABSF
Professional
 
Posts: 810
Joined: Wed Nov 10, 2010 9:32 am
Location: E Malaysia

Next

Return to Wireless Device

Who is online

Users browsing this forum: No registered users and 18 guests

cron