Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdint.h>
- typedef union {
- struct tagPORTABITS {
- unsigned char RA0:1;
- unsigned char RA1:1;
- unsigned char RA2:1;
- unsigned char RA3:1;
- unsigned char RA4:1;
- unsigned char RA5:1;
- unsigned char RA6:1;
- unsigned char RA7:1;
- };
- unsigned char giallo;
- } PORTABITS;
- /*extern*/ volatile PORTABITS PORTAbits;
- int x __attribute__ ((aligned (2)));
- void to_bin (void * number, unsigned char num_size, char *string);
- int main (int argv, char *argc[]) {
- char string[8 * sizeof(int) + 1];
- //PORTABITS PORTAbits;
- PORTAbits.RA0 = 1;
- PORTAbits.RA1 = 1;
- PORTAbits.RA2 = 0;
- PORTAbits.RA3 = 0;
- PORTAbits.RA4 = 1;
- PORTAbits.RA5 = 1;
- PORTAbits.RA6 = 0;
- PORTAbits.RA7 = 1;
- to_bin((void*) &PORTAbits, sizeof(PORTABITS), string);
- printf("PORTAbits = %s\n", string);
- PORTAbits.giallo = 0xFF;
- to_bin((void*) &PORTAbits, sizeof(PORTABITS), string);
- printf("PORTAbits = %s\n", string);
- x = 0x0FFFFFFF;
- to_bin((void*) &x, sizeof(int), string);
- printf("x = %s\n", string);
- return 0;
- }
- void to_bin (void * number, unsigned char num_size, char *string) {
- int i, e, f;
- unsigned char *num;
- for(f = 0, e = 8 * num_size -1; f < num_size; f++) {
- num = number + f;
- for(i = 0; i < 8; i++, e--) string[e] = ((*num >> i) & 1) ? '1' : '0';
- }
- string[8 * num_size] = '\0';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement