View previous topic :: View next topic |
Author |
Message |
AlbertCharles
Joined: 15 Sep 2023 Posts: 13
|
Send Nibble by printf- pic18f26k22 |
Posted: Tue Aug 12, 2025 5:35 am |
|
|
Code: |
/*
Hi guys, i need to send one size of file with 36k = 36 * 1024 = 36864 bytes
36kb, converted to Hexadecimal , i have 0x9000.
However, I need to show that hexa byte in parts.
In the following way: spliting 0x900 in two parts , like that, 0x90 and 0x00.
Can someone help me? ( CCS 5.115)
*/
#include <18F26K22.h>
#fuses NOWDT,NOBROWNOUT,NOPUT,NOMCLR ,PLLEN ,INTRC_IO
#use delay(clock= 64MHZ)
#use rs232(baud=9600, xmit=PIN_C0, rcv=PIN_C1)
void main(){
unsigned char myByte = 0x9000;
unsigned char FirstP;
unsigned char SecondP;
FirstP = (myByte >> 8) & 0x0F;
SecondP = myByte & 0x0F;
printf("Original byte: %X\n", myByte); // ==> 00 ??
printf("First part: %X\n", FirstP); // ==> 00 ??
printf("Second part: %X\n", SecondP); // ==> 00 ??
}
|
Last edited by AlbertCharles on Tue Aug 12, 2025 9:05 am; edited 2 times in total |
|
 |
gaugeguy
Joined: 05 Apr 2011 Posts: 332
|
|
Posted: Tue Aug 12, 2025 6:17 am |
|
|
0x9000 is not a byte. It is a 16 bit integer.
0x90 is not a nibble. It is a byte (8 bit integer). |
|
 |
temtronic
Joined: 01 Jul 2010 Posts: 9568 Location: Greensville,Ontario
|
|
Posted: Tue Aug 12, 2025 8:14 am |
|
|
The biggest hurdle is that 'USE RS-232' doesn't allow for 4 bit data transmission ! You'll need to create your own 'driver' or 'function' to send the 4 bits( aka nibble or nybble). This will have to be 'bit banged' and NOT use the internal UART.
Splitting bytes into nibbles is easy though.
Curious as to what is going to receive the data. Communicating using 4 data bits is not very common. |
|
 |
Jerson
Joined: 31 Jul 2009 Posts: 131 Location: Bombay, India
|
Re: Send Nibble by printf- pic18f26k22 |
Posted: Tue Aug 12, 2025 8:15 am |
|
|
AlbertCharles wrote: | Code: |
/*
Hi guys, i need to send one size of file with 36k = 36 * 1024 = 36864 bytes in nibbles.
36kb, converted to Hexadecimal , i have 0x9000.
However, on the other side, I need to receive the hexa byte in nibbles.
In the following way: spliting 0x900 in 0x90 and 0x00.
Can someone help me? ( CCS 5.115)
*/
#include <18F26K22.h>
#fuses NOWDT,NOBROWNOUT,NOPUT,NOMCLR ,PLLEN ,INTRC_IO
#use delay(clock= 64MHZ)
#use rs232(baud=9600, xmit=PIN_C0, rcv=PIN_C1)
void main(){
unsigned char myByte = 0x9000;
unsigned char highNibble;
unsigned char lowNibble;
highNibble = (myByte >> 4) & 0x0F;
lowNibble = myByte & 0x0F;
printf("Original byte: %X\n", myByte); // ==> 00 ??
printf("High nibble: %X\n", highNibble); // ==> 00 ??
printf("Low nibble: %X\n", lowNibble); // ==> 00 ??
}
|
|
You could try this
unsigned int myWord = 0x9000;
and then
printf("the word is %04X\n", myWord); // print 4 hex digits with leading 0s
which will print out 9000 <LF> where <LF> is the expansion for the \n specifier.
If you'd rather do it in bytes, then you would print an unsigned char with the %02X specifier which will put out 2 hex digits |
|
 |
AlbertCharles
Joined: 15 Sep 2023 Posts: 13
|
|
Posted: Tue Aug 12, 2025 8:27 am |
|
|
Gaugeguy, ( Tnks fou u repply!)
I don't know if I expressed myself badly, but in short, I need to convert the number 38864 into hexadecimal which gives me 0x9000. After that, I need to divide them into parts (nibble) and send them (e.g.: 0x90 then 0x00). Each one by printf.
Last edited by AlbertCharles on Tue Aug 12, 2025 8:54 am; edited 1 time in total |
|
 |
AlbertCharles
Joined: 15 Sep 2023 Posts: 13
|
|
Posted: Tue Aug 12, 2025 8:51 am |
|
|
Mr. gaugeguy, Mr. temtronic and Mr Jerson,
i edited and corrected my post. Please review it.!!
Mny thnks fou u repply. |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 19928
|
|
Posted: Tue Aug 12, 2025 9:05 am |
|
|
As others have said, you seem to be misunderstanding what a nibble 'is'.
On the hexadecimal number 0x8000, each of the digits corresponds to a
nibble. A nibble is just 4 bits. So in 'nibbles', there are four characters
to send. Yet you are talking about sending bytes 0x80, is a byte, not a
nibble.
Are you then wanting to send bytes?. If so, what format do you want to
send them in?. As a raw byte?. As hex. As decimal?.
You also then try to store a 16bit value into an int8. Not going to fit.
You seem to be trying to send bytes in hex. If so:
Code: |
unsigned int16 myWhole = 0x9000; //this value won't fit in a char
printf("Original value: %04X\n", myWhole); // ==> 9000
printf("High byte: %02X\n", make8(myWhole,1)); // ==> 90
printf("Low byte: %02x\n", make8(myWhole,0)); // ==> 00
|
|
|
 |
AlbertCharles
Joined: 15 Sep 2023 Posts: 13
|
|
Posted: Tue Aug 12, 2025 9:13 am |
|
|
Mr. Thelmah, regarding the nibbles, I really expressed myself poorly. I know the difference between them. I corrected my post above.
Do I still apply your correction to the code? |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 19928
|
|
Posted: Tue Aug 12, 2025 9:19 am |
|
|
Yes.
What I posted (if I typed it right, on a touch screen, not my normal desktop),
Will output the separate bytes as the comments show. |
|
 |
AlbertCharles
Joined: 15 Sep 2023 Posts: 13
|
|
Posted: Tue Aug 12, 2025 9:23 am |
|
|
Mny thnks....I´ll try. |
|
 |
AlbertCharles
Joined: 15 Sep 2023 Posts: 13
|
|
Posted: Tue Aug 12, 2025 10:03 am |
|
|
Mr Thelmah:
Original value: 0000 // => 9000 ???
High byte: 90
Low byte: 00 |
|
 |
gaugeguy
Joined: 05 Apr 2011 Posts: 332
|
|
Posted: Tue Aug 12, 2025 1:21 pm |
|
|
Post your current code. |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 19928
|
|
Posted: Wed Aug 13, 2025 2:00 am |
|
|
%04Lx
Problem is that 'x' on its own handles 8 bit integers. For the 16 bit value
you need the added 'L' (long).
I would have expected there to have been a warning message on the
compile for this. |
|
 |
jeremiah
Joined: 20 Jul 2010 Posts: 1396
|
|
Posted: Wed Aug 13, 2025 7:37 am |
|
|
Code: |
unsigned char myByte = 0x9000;
|
needs to be changed to:
Code: |
unsigned int16 myWord = 0x9000;
|
So it is big enough to hold the value you want. You'll need to change all references of myByte to myWord |
|
 |
|