Embedded C Programming Guidance Needed.

Thread Starter

Peaches41

Joined Dec 15, 2016
70
Good evening forum-
I am looking for guidance in completing an embedded c code for a PIC 16F684 that I have been working on. I am new to c for about a week coming over from pic assembly. Please see my code below for reference. This is just code for converting decimal to hexadecimal. My code performs what I need for a 8 bit integer dec to hex in MPLAB, X but I have the result stored in 2 eight bit variables and want it to be stored as one 8 bit integer in hex.
Could someone please guide me accomplishing this?

Shauna
******************************************************************

Code:
#include <xc.h>
#include <pic.h>


int mod, num = 148;
int k = 0, t = 0;


void main()

{  
  
  while(num>16)
  {
  mod = num%16;
  num = num/16;
  
  
  
  
  } //elihw
  
      if (num>9)
     {
       switch(num)
       {
         case 10:
         k='A';
         break;
         case 11:
         k='B';
         break;
         case 12:
         k='C';
         break; 
         case 13:
         k='D';
         break;
         case 14:
         k='E';
         break;
         case 15:
         k='F';
         break;
         default:
         NOP();
    }//hctiws
  }//fi
  
  if (mod>9)
   {
    switch(mod)
    {
      case 10:
      t='A';
      break;
      case 11:
      t='B';
      break;
      case 12:
      t='C';
      break; 
      case 13:
      t='D';
      break;
      case 14:
      t='E';
      break;
      case 15:
      t='F';
      break;
     default:
     NOP();
  
  
  }//hctiws
  }//fi  
  
  
  while(1==1);
}
Moderator edit: added code tags
 
Last edited by a moderator:

AlbertHall

Joined Jun 4, 2014
12,346
I have the result stored in 2 eight bit variables and want it to be stored as one 8 bit integer in hex.
You can't do that. Suppose your number is 0xAA. Both k and t will be the character 'A'. That is 0x41 (65 decimal) and uses up 7 of the eight bits.

Incidentally this is not a good way of converting to hex. Ask Mr Google and he will show you better ways of doing it.
 

Thread Starter

Peaches41

Joined Dec 15, 2016
70
You can't do that. Suppose your number is 0xAA. Both k and t will be the character 'A'. That is 0x41 (65 decimal) and uses up 7 of the eight bits.

Incidentally this is not a good way of converting to hex. Ask Mr Google and he will show you better ways of doing it.
You can't do that. Suppose your number is 0xAA. Both k and t will be the character 'A'. That is 0x41 (65 decimal) and uses up 7 of the eight bits.

Incidentally this is not a good way of converting to hex. Ask Mr Google and he will show you better ways of doing it.

Code:
#include <xc.h>
#include <pic.h>


int mod, num = 148;
int k = 0, t = 0;


void main()

{

  while(num>16)
  {
  mod = num%16;
  num = num/16;




  } //elihw

      if (num>9)
     {
       switch(num)
       {
         case 10:
         k='A';
         break;
         case 11:
         k='B';
         break;
         case 12:
         k='C';
         break;
         case 13:
         k='D';
         break;
         case 14:
         k='E';
         break;
         case 15:
         k='F';
         break;
         default:
         NOP();
    }//hctiws
  }//fi

  if (mod>9)
   {
    switch(mod)
    {
      case 10:
      t='A';
      break;
      case 11:
      t='B';
      break;
      case 12:
      t='C';
      break;
      case 13:
      t='D';
      break;
      case 14:
      t='E';
      break;
      case 15:
      t='F';
      break;
     default:
     NOP();


  }//hctiws
  }//fi


  while(1==1);
}
[/QUOTE]
You can't do that. Suppose your number is 0xAA. Both k and t will be the character 'A'. That is 0x41 (65 decimal) and uses up 7 of the eight bits.

Incidentally this is not a good way of converting to hex. Ask Mr Google and he will show you better ways of doing it.
Mr. Hall,
Thank you for your reply. I did not want to simply cut and paste code I found on google but rather wanted to try and learn it by figuring it out, but I do see your direction there. I guess I will say this then, is it possible to do the following:
1) 0x09 and 0x0c are 2, 8 bit integers(9 and 12)
2) Swap bits, 0x09 becomes 0x90
3) Combine 0x90 and 0x0c to get 0x9C
4) 0x9c = 156 in decimal

This may not be possible but I thought I would post it to the forum to find out.

Shauna-

Moderator edit: added code tags
 
Last edited by a moderator:

AlbertHall

Joined Jun 4, 2014
12,346
Yup, that works.
If you are familiar with PIC assembly then you will know the 'swapf' command which does in 1 instruction what '>>4' does in four. You might hope that the compiler would be smart enough to perform the simplification. I don't know whether it does. You can use assembler in 'C' programs too (asm(); or the #asm construct).
 

Thread Starter

Peaches41

Joined Dec 15, 2016
70
Thank you all for your prompt, and respectful help in answering my question. I learned from this and that's what I was hoping for.

Shauna
 
Top