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 support@ccsinfo.com

Convert 74165 decimal result to Binary

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



Joined: 21 May 2015
Posts: 181

View user's profile Send private message

Convert 74165 decimal result to Binary
PostPosted: Mon Dec 12, 2016 7:19 pm     Reply with quote

Hi,

I've build a program to get result from 8 input push button using 74165 shift register.
The program works well. However the result is in decimal (0~127, -1 ~ -128).
I would like to get the result in binary number such as 11111111 instead of (-128).
Hope someone can help me to solve this problem

Code:

#include <18F4550.h>
#fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
#use delay(clock=48000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#include <string.h>
#include <input.c>
#include <stdio.h>
#include <stdlib.h>
#include <usb_cdc.h>
#include <74165.c>

void main()
   {
      char c;
      int8 data ;
      unsigned char d;
      unsigned char key;

   usb_init_cs();
   
   while (TRUE)
         {
         usb_task();

            if (usb_cdc_kbhit())
               {
                 c=usb_cdc_getc();
                 if (c=='\n') {putc('\r'); putc('\n');}
                 if (c=='\r') {putc('\r'); putc('\n');}                                     

                 while(true)
                     {
                  d = usb_cdc_getc();

                         if(d=='U')   // push W
                             {
                                while (key!=32) // push SPACE BAR to stop
                                   {
                                 if(usb_cdc_kbhit())
                                   {key=usb_cdc_getc();}
                             
                                    read_expanded_inputs(&data);
                                    delay_ms(500);
                                                                         
                                    printf(usb_cdc_putc,"\r%d ", data);
                                     
                                   } key=0;
                             }

                     }
               }       
         }             
}                     
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Dec 12, 2016 8:54 pm     Reply with quote

See the display_binary() routine in this thread:
http://www.ccsinfo.com/forum/viewtopic.php?t=1523
art



Joined: 21 May 2015
Posts: 181

View user's profile Send private message

PostPosted: Mon Dec 12, 2016 9:27 pm     Reply with quote

Hi PCM,

Thanks for the idea.
I've change my code and it works as I plan



Code:

#include <18F4550.h>
#DEVICE ADC=10
#fuses HSPLL,NOWDT,PROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
#use delay(clock=48000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#include <string.h>
#include <input.c>
#include <stdio.h>
#include <stdlib.h>
#include <usb_cdc.h>
#include <74165.c>


void main()
   {
      char c;
      int8 data ;
      unsigned char d;
      unsigned char key;
                                                 
                                         
   usb_init_cs();
   
   
   while (TRUE)
         {
         usb_task();


            if (usb_cdc_kbhit())
               {
                 c=usb_cdc_getc();
                 if (c=='\n') {putc('\r'); putc('\n');}
                 if (c=='\r') {putc('\r'); putc('\n');}

                                                                                                                   

                 while(true)
                 
                     {
                 

                  d = usb_cdc_getc();



                         if(d=='U')   // push W
                             {
                                while (key!=32) // push SPACE BAR to stop
                                   {

                                 if(usb_cdc_kbhit())
                                   {key=usb_cdc_getc();}

                             
                                    read_expanded_inputs(&data);
                                    delay_us(5);
                                   
                                      if(bit_test(data,0))   // 1
                                        m1=1;
                                      else
                                        m1=0;
                                     
                                     
                                                                         
                                                                         
                                      if(bit_test(data,1))  //2
                                        m2=1;
                                      else
                                        m2=0;                                                                       
                                                                         
                                                                         
                                      if(bit_test(data,2))  //3
                                        m3=1;
                                      else
                                        m3=0;                                                                         
                                                                         
                                                                         
                                      if(bit_test(data,3))  //4
                                        m4=1;
                                      else
                                        m4=0;
                                   

                                      if(bit_test(data,4))   // 5
                                        m5=1;
                                      else
                                        m5=0;
                                                                         
                                                                         
                                      if(bit_test(data,5))  //6
                                        m6=1;
                                      else
                                        m6=0;                                                                       
                                                                         
                                                                         
                                      if(bit_test(data,6))  //7
                                        m7=1;
                                      else
                                        m7=0;                                                                       
                                                                         
                                                                         
                                      if(bit_test(data,7))  //8
                                        m8=1;
                                      else
                                        m8=0;                                     
                                     
                                                       
                                                                         
                                                                         
                                    printf(usb_cdc_putc,"\r%d%d%d%d%d%d%d%d ", m1,m2,m3,m4,m5,m6,m7,m8);
                                     
                                   } key=0;
                             }

                     }
               }       
         }             
}   
art



Joined: 21 May 2015
Posts: 181

View user's profile Send private message

PostPosted: Mon Dec 12, 2016 9:29 pm     Reply with quote

Do you have ideas to simplify the code?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Dec 12, 2016 11:03 pm     Reply with quote

Your code uses a programming technique called "unrolling a loop".
You could make it into a for() loop.
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