|
|
View previous topic :: View next topic |
Author |
Message |
silvalooky
Joined: 18 Mar 2010 Posts: 3
|
Urgent!!! need conver (bin => dec / hex) for RS232+24c16 |
Posted: Thu Mar 18, 2010 6:47 pm |
|
|
Hello everyone. Please, I want just a small assistance for this program!
When I want post of a memory a code ASCII (for example the " F" who is written in the memory in the form " 0100 0110") I meet an compatibility issue with the TERMINAL of RS232 because it knows only the characters binary written in the form of 0bxxxxxxxx (for the " F" it false writing 0b01000110), so for the DEC and the HEX its normal.
How a solution I think has convert the data of the memory into format of DEC or HEX .......!!! But I don't find a good program!
Have you another solution? plz !!!
Code: |
#include <16F877A.h>
#include <stdio.h>
#use delay(clock=4000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7, bits=8)
//-------------------DEC2BIN---------------------//
int bin_to_hex(int8 bin)
{
int hex;
if ( bin == 00110000) hex = 0b00110000; // => 0
else if ( bin == 00110001) hex = 0b00110001; // => 1
else if ( bin == 00110010) hex = 0b00110010; // => 2
else if ( bin == 00110011) hex = 0b00110011; // => 3
else if ( bin == 00110100) hex = 0b00110100; // => 4
else if ( bin == 00110101) hex = 0b00110101; // => 5
else if ( bin == 00110110) hex = 0b00110110; // => 6
else if ( bin == 00110111) hex = 0b00110111; // => 7
else if ( bin == 00111000) hex = 0b00111000; // => 8
else if ( bin == 00111001) hex = 0b00111001; // => 9
else if ( bin == 01000001) hex = 0b01000001; // => A
else if ( bin == 01000010) hex = 0b01000010; // => B
else if ( bin == 01000011) hex = 0b01000011; // => C
else if ( bin == 01000100) hex = 0b01000100; // => D
else if ( bin == 01000101) hex = 0b01000101; // => E
else if ( bin == 01000110) hex = 0b01000110; // => F
else hex = 0b00000000;
return hex;
}
//**********************************************//
int y=0;
void main(){
while(1) {
y=bin_to_hex(0b00111000);
putc(0b00111000);
delay_ms(500);
}
} |
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Mar 18, 2010 11:28 pm |
|
|
I'm not certain what you want. Do you really just want a printf statement ?
The printf() function will convert an integer to ASCII, so it can be displayed.
Example:
The following program will display this on the terminal window:
Code: |
#include <16F877A.H>
#fuses XT, NOWDT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
//======================================
void main()
{
int8 y;
y = 123;
printf("%u", y);
while(1);
}
|
|
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Fri Mar 19, 2010 2:40 am |
|
|
I think silvalooky has a bigger problem.
From looking at the code it looks like you are trying to represent a binary value use a decimal number (I will explain in a min) and then to convert that to a hex value!
bin == 00110000
bin is defined as an int 8.
The value 00110000 is the decimal value 110000 = one hundred and 10 thousand NOT the binary value 0b00110000 (dec 48).
Your first problem is that an int8 can not hold a value that big. to be able to store the binary representation of a value in this way the variable needs to be able to hold the value 11,111,111 (11 million) so you need at least a 24 bit variable.
Secondly, you actually call the routine with the binary representation
bin_to_hex(0b00111000); (0b00111000)
Unfortunately it is very difficult to figure out what you are actually trying to do.
Where is this value comming from ?
What type is it stored in, int8, int32, char[] ?
How do you want to display it (binary, hex, decimal or ASCII char) ?
You may need to show some of the original code. |
|
|
silvalooky
Joined: 18 Mar 2010 Posts: 3
|
|
Posted: Fri Mar 19, 2010 3:33 am |
|
|
Thank you for your reply.
=> Wayne_: sorry for this erroneous code, because it's a code modify and I left name them such as it.
I just want a printf statement for a mini project with the smart cards (16f876+24c16).
but if I have "y=00111000" this code is received from a memory and I want compatible with the print's function of RS232! because the function "putc" for example, read only one binary code when is written in the following form "y=0b00111000". So "putc" can not print the binary digit who I have read from a memory !
Code: |
#include <16F877A.H>
#fuses XT, NOWDT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
//-------------------------Code----------------------------
void main()
{
int8 y;
y = 00111000; // this code is received from a memory and I want
// compatible with the print's function of RS232
putc(y);
} |
|
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Fri Mar 19, 2010 7:15 am |
|
|
y = 00111000;
This is trying to set y to one hundred eleven thousand, which does not fit in 8 bits.
I think you want:
y = 0b00111000;
which is the same to the compiler as
y = 0x38; or y = 56; or y = '8';
If you are receiving a byte as a string of decimal digits (very strange) you will need to write a looping routine to interpret each decimal digit and add them up. _________________ The search for better is endless. Instead simply find very good and get the job done.
Last edited by SherpaDoug on Fri Mar 19, 2010 12:34 pm; edited 1 time in total |
|
|
silvalooky
Joined: 18 Mar 2010 Posts: 3
|
|
Posted: Fri Mar 19, 2010 12:25 pm |
|
|
Wayne_ wrote: | I think you are still getting a little confused.
When you read a value from memory 8bit and store it in a variable it will be stored in the pic as a binary value
0b00111000
This is the same value as 0x38 (hex) and 56 (decimal) and the character '8' in ASCII.
When you specify
y = 00111000;
You have left off the 0b which means the number you are using is a decimal representation = 111 Thousand 111,000 NOT the decimal value of 56 (0b00111000).
So the first issue is that when you read your memory which contains the value 56 (0b00111000) this is the same as
y = 0b00111000;
OR
y = 56;
OR
y = 0x38;
Now, what you want to do is display thjis in binary form.
printf will convert the value to an ASCII format:-
printf("%U", y); // This will display the decimal value "56"
printf("%X", y); // This will display the hex value "0x38"
printf("%c", y); // OR
putc(y); // Will both try and display the character '8'
There is no printf option to display it in binary format, you will have to write a routine:-
Code: |
void PrintBin(int8 val)
{
int8 i;
for (i = 0; i < 8; i++)
{
if (val & 0x80) // test the MSB (Most significant bit (bit7))
putc('1'); // If it is a 1 the output a 1
else
putc('0'); // if it is a 0 then output a 0
val <<= 1; // Shift left y by 1 bit
}
}
|
|
thank you MAN !!!
really I have make a BIIIIG errore, thank you very well !
take a care! |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|