Society of Robots - Robot Forum
Software => Software => Topic started by: benji on April 13, 2008, 02:20:29 PM
-
i did connect my sharp ir to my pc serial port
what im doing is that ,my atmega is reading adc value ,store it in a variable, send this variable through uart
i can see my data on the hyper terminal screen but its coded,, its a font(ascii),,how do i turn it back to hexa or decimal?
-
hyperterminal bad, bray's terminal (http://braypp.googlepages.com/terminal) good :D
-
something even more awesome, if you use linux, KontrollerLab is a fully featured IDE and it has this terminal built in, it doesn't interfere with any other program reading the same port, and it has different graphing functions too like a o'scope or a histogram
-
i can see my data on the hyper terminal screen but its coded,, its a font(ascii),,how do i turn it back to hexa or decimal?
that's not really what a terminal emulator like HyperTerminal is designed to do.
why not just transmit the 3 ASCII characters that make up the number?
something like this:
void transmit_ascii_num(int i)
{
char h = (i / 100) + '0' ; // h = hundreds
char t = ((i % 100) / 10) + '0' ; // t = tens
char o = (i % 10) + '0' ; // o = ones
tx(h); // where "tx(char)" is your UART transmit function.
tx(t);
tx(o);
return;
}
dunk.
-
theres also a standard function itoa() that does this. It converts the int value into an array of characters which have the related ascii symbols of the number. So giving it the number 12 will return 2 characters 1 is 0x31 and the other is 0x32. When sent sent to hyperterminal these show the ascii characters of 1 and 2 ie.12
to use it you must send it an int and also give it a char array to return its characters to, and also state the base radix that you would like returned.
unsigned char[5] outstring; //the char array that will store the converted number
int mynum = 12; //the number to convert
itoa(mynum,outstring,10); //send the int and char to itoa and tell it we want to use a base radix of 10.
uartputS(outstring); // finally send the converted number to hyperterminal
once the number 12 gets sent to itoa() the array outstring contains the following:-
outstring[0] = 0x31 //this is the ascii code for the number 1
outstring[1] = 0x32 //this is the ascii code for the number 2
outstring[2] = undefined // not used
outstring[3] = undefined
outstring[4] = undefined
If you just send the number 12 to hyperterminal the screen will become blank because 12 (or 0x0C) is the ascii code for starting a new page, so the itoa function converts the decimal value into hex characters that display as the decimal value but have no relation to the actual number 12.
If you cant find the itoa() function in any of your built in libraries, let me know and ill post it for you
-
thanks paul this is awsome,, and yea i have the function,i just have a question about it
should the outstring contain 5 elements? cuz my input isnt an int ,its 8 bits variables
so actually 3 elements would do
if one of the outstring elements is undefined,would it send it via uart?what does this show on hyperterminal?
thanks
-
The outstring can be any size, I usually just use 5 because an int has the possibility of a 5 digit value (32000). It can be reduced if you know that it isnt going to overflow.
The undefined values in the outstring simply wont be printed.
Im not sure if it works properly by sending just an 8 bit char, it might only accept an int but you could do the following:
char mychar = 0x08;
unsigned char[3] outstring;
itoa((int)mychar,*outstring,10)
So you send a character but write (int) in brackets before the char name to convert its value for this purpose only to an int.
Also you will notice that there is a * before outstring, you need to do this because you are just sending a pointer to the array (i forgot to put it in the original post)
-
as i told u bfore in my earlier post use this code, it is tested and debugged
# include<stdio.h>
# include<avr/io.h>
# include<util/delay.h>
# include<avr/interrupt.h>
# include<stdlib.h>
# include<string.h>
# include<avr/pgmspace.h>
#define USART_BAUDRATE 38400
#define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL)))- 1)
char u[11]="adcvalue\r\n";
char *d;
char b[10];
char *p;
unsigned int a;
void adcinitialize();
void uartinitialize();
void adcgetvalue();
void transferusart();
void printvalue(char *);
int main(void)
{
adcinitialize();
uartinitialize();
while(1)
{
adcgetvalue();
p=itoa(a,b,10);
strcat_P(b,PSTR("\r\n"));
d=&u[0];
transferusart();
}
return(0);
}
void adcinitialize()
{
ADCSRA|=_BV(7)|_BV(ADPS2)|_BV(ADPS1);
ADMUX=0x60;
ADCSRA|=(1<<ADSC);
while(!(ADCSRA & (1<<ADIF)));
ADCSRA|=(1<<ADIF);
TCCR1B|=(1<<CS12);
}
void uartinitialize()
{
UCSRB= (1<<RXEN) | (1<<TXEN);
UCSRC= (1 << URSEL) | (1 << UCSZ0) | (1 << UCSZ1);
UBRRL=BAUD_PRESCALE;
UBRRH=(BAUD_PRESCALE>>8);
}
void adcgetvalue()
{
ADMUX=0x61;
ADCSRA|=(1<<ADSC);
while(!(ADCSRA & (1<<ADIF)));
ADCSRA|=(1<<ADIF);
a=ADCH;
}
void transferusart()
{
while (*d != '\0')
{
while ((UCSRA & (1 << UDRE)) == 0) {}; // Do nothing until UDR is ready for more data to be written to it
UDR =*d ; // Echo back the received byte back to the computer
d++;
}
TCNT1=0;
while(TCNT1<=60000){};
TCNT1=0;
printvalue(p);
}
void printvalue(char *l)
{
while (*l != 0x00)
{
while ((UCSRA & (1 << UDRE)) == 0) {}; // Do nothing until UDR is ready for more data to be written to it
UDR =*l ; // Echo back the received byte back to the computer
l++;
}
TCNT1=0;
while(TCNT1<=60000){};
TCNT1=0;
}
-
thanks paul,, and sorry superchiku i should have seen it,thanks anyways
-
no problem , do post the sensor digital o/p and graph when you use the code
-
hyperterminal bad, bray's terminal good
maan this is one great piece of art, thanks
-
whats bray's terminal?
-
check the link.
two dudes decided hyperterminal sucked. one of them was bray and finished his terminal with a whole bunch of options for debugging. the other one, izua, found bray's terminal and never finished his own terminal software :P
-
hm.. u use which software to make the terminal?
-
c++ mostly.
-
is it advanced C or embedded
-
it was a terminal software for the pc. if you consider the intel cpu as embedded, then yes, it was embedded.
i use the term software for PC software, and firmware for embedded software (like avr, pic, etc..)
offtopic: this is way offtopic.
-
this isnt offtopic its realted to programming and may be this can be used in robots also
-
everyone bashes hyperterminal . . . I like it tho . . .
Anyway, benji, can you show us the output you have in the terminal? Are you sure its just not an improper baud config?
To convert from ascii, do this:
your_variable = your_variable & 0x0F;
-
no ,, the baud rate is right,, hyper terminal worked good, but i wanted to see my real numbers,,not coded stuff
try bray's terminal,, its just more robotic,, ;D
-
you can see have you tried to use itoa function which convers to char and ascii, have you used strcat and attached /r/n at the end of your coverted string?
and have you tried using the progrem function if your using avr
-
there is no point to add stuff to my code just to use hyperterminal,,bray's terminal is perfect
-
hmm.. if pre made codes make your life easy then that doesnt mean you cant learn anything , if you try to make your own code , you will be able to learn a lot more..
-
maybe he uses assembler, and an itoa function or ftoa is pretty evil to write.
-
he uses C too..
-
its not that i dont want to learn,, its that i would save my time to learn somthin that helps me with my project ,,to improve it,, not learn stuff that i can do the easy way,,thats just my point of view
if you have 2 solutions , get the easier one and save time for another problems,, ;)
-
REMEMBER ALWAYS "THE EASIEST WAY IS NOT THE BEST WAY ALWAYS"
-
yea but in this case i think it is,, cause i would be happy if the code gets smaller,, i have tons of things to program other than this little sensor,,
-
your wish
-
Are there any programs for COM ports which are able to graph data properly? Terminal can, but in poor quality. Also it saves data in hex or ASCI , no decimal option for saving available. So, if I want to graph data, for example, in Maple, I need to do a lot of things ( cut prologue and epilogue for every string, convert it to decimal and graph). Do anybody knows easier way?
-
no decimal option for saving data??? i saved my data in digital numerical form not is hexa or ascii
-
What program are you talking about? In Br@y++ terminal there are no options for decimal saving, only for decimal listing. In hyperterminal too.
-
no hyperterminal ill giive u my readings if u want
-
Can you tell what settings did you use saving data in decimal? Did you use atoi? I am looking for simple solution without atoi to plot graph from my data from UART on PC screen. I tried a lot of programs, only Bray terminal can plot, but relatively poor.
P.S. I have just done Excel program to plot graphs. In my ancient Ex. 2000, there are no hex to dec, but it was not very hard to implement. However, it takes time to save hex file in ComPort Toolkit and graph it.
-
yes man i used itoa , a relative son could come if i created some kinda software for u ppl but i dont know how to create a software out of the code i have written so thats the problem