I am doing an ADC12 conversion and sending this value to ADC12MEM0. Once the ADC12 is complete, I am sending the ADC12MEM0 values upper and lower 8 bits to the hyperterminal. I understand that it is simply sending the hex values in the register over as ascii, but I would like to display these values as hex on the hyperterminal. I would like to copy the register exactly as it appears to the hyperterminal (0x0FFF on ADC12MEM0 would make hyperterminal display 0x0FFF... etc.). I have looked at hex to ascii tables and I understand how this works, but I have no idea how to write this as code in code composer studio. Is there some advice you could give me or an example code I can look at to better understand. Below I have provided my code. Thanks!
# include <msp430xG46x.h>volatile unsigned int temp;volatile unsigned int i;void main(void){WDTCTL = WDTPW+WDTHOLD; // Stop watchdog// Initialization of ADC12//P6SEL |= 0x01; // Enable A/D channel A0ADC12CTL0 &= ~ENC;ADC12CTL0 = ADC12ON + SHT0_2 + REFON + REF2_5V+MSC; // turn on ADC12, set samp timeADC12CTL1 = SHP+CONSEQ_2; // Use sampling timerADC12MCTL0 = SREF_1 + INCH_0; // Vr+=VeREF+ (external)ADC12CTL0 |= ENC; // Enable conversions// Initialization of Rs-232//FLL_CTL0 |= XCAP14PF; // Configure load capsdo{IFG1 &= ~OFIFG; // Clear OSCFault flag//_delay_cycles(50000); // Time for flag to set}while ((IFG1 & OFIFG)); // OSCFault flag still set?P2SEL |= 0x30; // P2.4,5 = USCI_A0 RXD/TXDUCA0CTL1 |= UCSSEL_1; // CLK = ACLKUCA0BR0 = 0x03; // 32k/9600 - 13.65UCA0BR1 = 0x00; //UCA0MCTL = 0x06; // ModulationUCA0CTL1 &= ~UCSWRST; // **Initialize USCI state machine**IE2 |= UCA0TXIE + UCA0RXIE; // enable RXD and TXD interrupt;while (1){// ADC loop //ADC12CTL0 |= ADC12SC; // Start conversions//while (!(ADC12IFG & 0x0001)); // Conversion done?temp = ADC12MEM0; // Move result__no_operation(); // SET BREAKPOINT HERE// RS-232 loop //UCA0TXBUF = temp; // send lower part__no_operation();//while(!(IFG2 & UCA0TXIFG))//{//_delay_cycles(1000); // wait for first transmit//}UCA0TXBUF = temp >> 8;// send upper part__no_operation();// _delay_cycles(1000);//UCA0TXBUF = temp;}}
printf( ... ); will do that (and a lot more than that).
Preston LortieI am sending the ADC12MEM0 values upper and lower 8 bits to the hyperterminal. I understand that it is simply sending the hex values in the register over as ascii
No, you have a slight misunderstanding there!
You are sending pure binary (or "hex") data, and hyperterminal is interpreting it as ASCII.
That is, you send the hex value 0x0D, and hyperterminal thinks, "what character has ASCII code value 0x0D?", and displays that character - in this case, a Carriage Return (CR).
0x0FFF on ADC12MEM0 would make hyperterminal display 0x0FFF
Remember that the "0x" prefix is merely a 'C' notation to indicate that what follows is to be interpreted as a hexadecimal number - the "0x" does not exist as part of the ADC value! Therefore, if you want to see "0x" on hyperterminal, your code will have to generate those two characters.
I have looked at hex to ascii tables and I understand how this works, but I have no idea how to write this as code in code composer studio
'C' code is 'C' code - irrespective of what tool you use to write it!
Is there some advice you could give me
Write out all 16 possible values of a hex digit, 0..F, in a column.Then write out the ASCII codes of the 16 characters '0' to 'F' in another column alongside.Then look at the two columns, and see what pattern you can see between the hex digit values and the ASCII codes...
Hint: there are actually two patterns: one for 0..9, and another for A..F
A great deal of computer programming is about spotting patterns - so this is a very good learning exercise!
old_cow_yellow printf( ... ); will do that
Indeed it will!
As will sprintf(), and a number of other functions from the standard 'C' library...
However, the ability to represent a hex digit value as a "displayable" character is so fundamental that it is probably worth the OP learning (and understanding) now how to do it.
(and a lot more than that).
Indeed. But therein lies a potential problem for small embedded systems: because printf() does do such a lot more, it can significantly "bloat" the size of your code...
Andy Neil quote(and a lot more than that)./quote Indeed. But therein lies a potential problem for small embedded systems: because printf() does do such a lot more, it can significantly "bloat" the size of your code...
quote(and a lot more than that)./quote
HI Andy, you never was in the nerd status?
So why not answer in an helpful manner?
Why bother with an hidden hint and not show how hex and ascii tables are related to?
Why also at OCW answer you replyed in a so bad look? On 46x that is plenty of flash I also use (s)printf also if I have my library to sending out hex on a one line based code.
Sometimes guru level has to be settled down to deal with person too and understand what they want. This forum as I see is not a high level one specialized but an intermix of beginner and advanced so I hope some hint to TI are to be send to focus on what level question are to be answered.
Regards
Roberto
Please login & click Verify Answer if this post answered your question.
Roberto RomanoSo why not answer in an helpful manner?
There is an old saying:
Give a man a fish, and you have fed him for one meal; Teach a man to fish, and you have fed him for life.
Teach a man to fish, and you have fed him for life.
I think that it is far more "helpful" to get the OP to think for himself than to just give him the answer "on a plate".
Roberto Romanoat OCW answer you replyed in a so bad look?
Why do you think it's a "bad look"?
If I had a dollar for every forum post saying, "My code is so simple (sic), but overflows the memory space - what could be wrong?" - where the answer is the use of printf() - then I would be very rich indeed!
In this case, the set of values to be converted is so small (only 16), that it can easily & economically be done with just a lookup table...
Andy sorry but you forgot the more important part: we have to build something better, so TI probably has to think split forum to beginner/student/hobbyst/professional, this way we prepare teaching material about and we can concentrate on real colleague problem. In my point of view some library can be loaded to forum and left here with instruction on how to use, yes I agree with you our work cannot be wasted away, I decided to dedicate some free time to this formum and I do when I can.
example:
char bin2hex(int a)
{
a&=0xf;
if(a>9)
return('A'-1+a);
return('0'+a);
}
or lookup table (tricky)
declaration
char BIN2HEX[]="0123456789ABCDEF";
usage
hexchar=BIN2HEX[a&0xf];
I am sure these codes alone create troubles to our friend, why not release with some explanations? I agree tons of example exist but why not collect here to MSP area?
Hint about?
Thanks for the replies guys, but honestly you haven't answered my question. As I have said I understand that the values being sent through serial are hex values, but are being treated as ASCII characters (and I understand the table of hex to ASCII). I am having my problem with converting these hex values to ASCII, since my program will repeatedly do an ADC conversion and send this to the hyperterminal. So my question is how do I CONTINUALLY convert the hex value to ASCII characters that will reflect the ADC12MEM0 register (and yes i understand I must put a 0x0030 and 0x0078 to get the "0x" prefix I want). My problem is mainly coding, since I know there is a way to keep reading and converting these values to ASCII without going through all 4095 different possibilities.
Preston Lortiemy question is how do I CONTINUALLY convert the hex value to ASCII
If you know how to do one hex value, then doing it "continually" is just a matter of doing the same thing each time you get a new value!
ie, in pseudo-code:
repeat: read a value; convert it to ASCII text; send the ASCII text
Preston Lortieconverting these values to ASCII without going through all 4095 different possibilities.
You convert each digit in turn, to build the full string.
As already noted, printf() will do this for you (though best not called in an ISR).
Preston Lortie My problem is mainly coding, since I know there is a way to keep reading and converting these values to ASCII without going through all 4095 different possibilities.
My problem is mainly coding, since I know there is a way to keep reading and converting these values to ASCII without going through all 4095 different possibilities.
Hi Preston, sorry to see Andy has full reason in some writing, what do you think to have a correct transfer of byte values without a supporting protocol still ascii or binary? Your code transmitted bynary codes and hyperterminal interpret it, you can store them but I think you lose some codes due to ascii control codes interpreteation. Hyperterminal is not the best program to do this and your coding style still is wrong due impossibility of select where byte start and where finish. A better approach is to use 7 bit encoding and define a start symbol grouping some bytes on a frame.
Again what do you want to do? Hyperterminal is not good to dump binary, choose a better program or write your code.
Transmit from MSP as HEX then add a cr lf comma or tab to separate one word from the other.
Preston Lortie Thanks for the replies guys, but honestly you haven't answered my question. As I have said I understand that the values being sent through serial are hex values
Thanks for the replies guys, but honestly you haven't answered my question. As I have said I understand that the values being sent through serial are hex values
Hi Preston, here is where you and Andy are in a disagree point.
I cannot say you have reason but this is your fault: values sent to serial are BINARY values and has nothing to do with decimal hex or octal values, they are binary and hiperterminal interpret as ascii codes.
Value 65 decimal is interpreted as 0100 0001 in binary as 'A' in ascii value 65 as decimal 0x41 in hexadecimal and o101 in octal so if you wish to print it you have to send equivalent ascii stream to serial line.
I know about this mistake by my student fault too.
Andy, this can clarify what is about my idea?
Roberto RomanoTransmit from MSP as HEX
No!
You need to transmit ASCII-coded text strings!
Hyperterminal is perfectly adequate to receive this.
Roberto Romanoadd a cr lf comma or tab to separate one word from the other.
Yes - good point!
Andy Neil Roberto RomanoTransmit from MSP as HEX No! You need to transmit ASCII-coded text strings! Hyperterminal is perfectly adequate to receive this.
Andy, sometimes you are more worst than a nerd ;)
To all community my apologizes about HEX used instead as HEX ascii coded value representing binary value of variable, I was thinking transmitting as HEX signify transmitting a string coded from {0..,a..f} char set from character encoding ascii or whichever is used to transmit data.
The thing is, when it comes to computer programming, attention to details is absolutely key!!
If one doesn't clearly understand the difference between sending a pure biary number, and sending its text representation - then one will come unstuck!
However, I do realise that it can be hard to express such nuances when you're having to write in a foreign language. So, please, take such clarifications just as clarifications - not as criticisms!
Alright I tried using the printf command and it would not print serially to PUTTY. I tried to do a much simpler example code using printf, and this would not work either. I am using CCS5.1. Just to be clear here is the simple printf code I am running that will not work.
#include <stdio.h>void main( void ){ printf("Hello World! %i\n", 123 );}
This does not output anything via serial. Is this the version of CCS I am using or do I have to do anything special to get the printf command to work?
output anything via serial. Is this the version of CCS I am using or do I have to do anything special to get the printf command to work?