View previous topic :: View next topic |
Author |
Message |
PIC_ECM
Joined: 23 Apr 2007 Posts: 2
|
Hex data on RS232. |
Posted: Tue Apr 24, 2007 9:52 pm |
|
|
Hello all,
I am using a PIC16f690 with compiler version 4.
I have a MAX232 level translator connected to the hardware UART of the PIC.
In my code I am trying to output the HEX value 0x0a to the rs232.
I am viewing the output of the rs232 with the serial port monitor within the CCS program. When viewing the two screens, ie Hex and ASCII, 0a is apearing in the ASCII window and the HEX value 30 61 is apearing in the hex window.
I have pasted a copy of my code below all of this.
Why is this so? Should'nt the value 0a appear in the Hex window given that I have specified the value in HEX?
Have I specified something wrong in my code?
#include <stdio.h>
#include <stdlib.h>
unsigned int val = 0;
void main()
{
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF);
setup_spi(FALSE);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard
setup_oscillator(False);
val = 0a;
while(1)
{
printf("%x", val);
}
}
I belive that there nust be something that I am doing incorrectly.
Ultimately I would like to be able to print a hex string to the serial port but I would like to get just a single byte first then expand on that.
Any help that anyone can offer would be greatly appreciated.
Many thanks in advanced.
Carlos. |
|
|
jecottrell
Joined: 16 Jan 2005 Posts: 559 Location: Tucson, AZ
|
|
Posted: Tue Apr 24, 2007 10:08 pm |
|
|
Always remember that every number the PIC deals with is always binary. We only change the format of how we see that binary number.
I can see what's happening with your code but the pros would have to explain it.
Should be:
That way the compiler will know that you are feeding it a hex value.
The output you are getting is the ASCII for 0 and a.
I think if you change the assignment statement above and change your printf to:
You should see what you want.
Good luck,
John |
|
|
libor
Joined: 14 Dec 2004 Posts: 288 Location: Hungary
|
|
Posted: Wed Apr 25, 2007 2:20 am |
|
|
If you want to output the value 0A hex itself.
val = 0x0A;
printf("%c", val);
or you can also use:
putc(val); |
|
|
|