CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Send Nibble by printf- pic18f26k22

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
AlbertCharles



Joined: 15 Sep 2023
Posts: 13

View user's profile Send private message Send e-mail

Send Nibble by printf- pic18f26k22
PostPosted: Tue Aug 12, 2025 5:35 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Aug 12, 2025 6:17 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Aug 12, 2025 8:14 am     Reply with quote

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

View user's profile Send private message Visit poster's website

Re: Send Nibble by printf- pic18f26k22
PostPosted: Tue Aug 12, 2025 8:15 am     Reply with quote

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

View user's profile Send private message Send e-mail

PostPosted: Tue Aug 12, 2025 8:27 am     Reply with quote

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

View user's profile Send private message Send e-mail

PostPosted: Tue Aug 12, 2025 8:51 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Aug 12, 2025 9:05 am     Reply with quote

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

View user's profile Send private message Send e-mail

PostPosted: Tue Aug 12, 2025 9:13 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Aug 12, 2025 9:19 am     Reply with quote

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

View user's profile Send private message Send e-mail

PostPosted: Tue Aug 12, 2025 9:23 am     Reply with quote

Mny thnks....I´ll try.
AlbertCharles



Joined: 15 Sep 2023
Posts: 13

View user's profile Send private message Send e-mail

PostPosted: Tue Aug 12, 2025 10:03 am     Reply with quote

Mr Thelmah:

Original value: 0000 // => 9000 ???
High byte: 90
Low byte: 00
gaugeguy



Joined: 05 Apr 2011
Posts: 332

View user's profile Send private message

PostPosted: Tue Aug 12, 2025 1:21 pm     Reply with quote

Post your current code.
Ttelmah



Joined: 11 Mar 2010
Posts: 19928

View user's profile Send private message

PostPosted: Wed Aug 13, 2025 2:00 am     Reply with quote

%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

View user's profile Send private message

PostPosted: Wed Aug 13, 2025 7:37 am     Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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