daily pastebin goal
34%
SHARE
TWEET

Untitled

a guest Jan 17th, 2019 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdint.h>
  3.  
  4. typedef union {
  5.     struct tagPORTABITS {
  6.         unsigned char RA0:1;
  7.         unsigned char RA1:1;
  8.         unsigned char RA2:1;
  9.         unsigned char RA3:1;
  10.         unsigned char RA4:1;
  11.         unsigned char RA5:1;
  12.         unsigned char RA6:1;
  13.         unsigned char RA7:1;
  14.     };
  15.     unsigned char giallo;
  16. } PORTABITS;
  17. /*extern*/ volatile PORTABITS PORTAbits;
  18.  
  19. int x __attribute__ ((aligned (2)));
  20.  
  21. void to_bin (void * number, unsigned char num_size, char *string);
  22.  
  23. int main (int argv, char *argc[])   {
  24.     char string[8 * sizeof(int) + 1];
  25.     //PORTABITS PORTAbits;
  26.     PORTAbits.RA0 = 1;
  27.     PORTAbits.RA1 = 1;
  28.     PORTAbits.RA2 = 0;
  29.     PORTAbits.RA3 = 0;
  30.     PORTAbits.RA4 = 1;
  31.     PORTAbits.RA5 = 1;
  32.     PORTAbits.RA6 = 0;
  33.     PORTAbits.RA7 = 1;
  34.     to_bin((void*) &PORTAbits, sizeof(PORTABITS), string);
  35.     printf("PORTAbits = %s\n", string);
  36.     PORTAbits.giallo = 0xFF;
  37.     to_bin((void*) &PORTAbits, sizeof(PORTABITS), string);
  38.     printf("PORTAbits = %s\n", string);
  39.     x = 0x0FFFFFFF;
  40.     to_bin((void*) &x, sizeof(int), string);
  41.     printf("x = %s\n", string);
  42.     return 0;
  43. }
  44.  
  45. void to_bin (void * number, unsigned char num_size, char *string)   {
  46.     int i, e, f;
  47.     unsigned char *num;
  48.     for(f = 0, e = 8 * num_size -1; f < num_size;  f++) {
  49.         num = number + f;
  50.         for(i = 0; i < 8; i++, e--) string[e] = ((*num >> i) & 1) ? '1' : '0';
  51.     }
  52.     string[8 * num_size] = '\0';
  53. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top