Created
June 19, 2020 00:55
-
-
Save gutierrezps/b21d71fdae245fb1177997fce3a8a7aa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdint.h> | |
// separa cada conjunto de bits do byte de status | |
struct MotorStatusBits | |
{ | |
unsigned motorIndex : 4; | |
unsigned currentPosition : 3; | |
unsigned isMoving : 1; | |
}; | |
// o status do motor pode tanto ser acessado como | |
// um único byte, ou como vários conjuntos de bits | |
union MotorStatus { | |
uint8_t status; | |
MotorStatusBits bits; | |
}; | |
MotorStatus motor10 = { | |
.status = 10 // inicializa o status, com motorIndex = 10 | |
}; | |
void DecimalToBinary (int n) | |
{ | |
int binaryNumber[100], num = n; | |
int i = 0; | |
while (i < 8) | |
{ | |
binaryNumber[i] = n % 2; | |
n = n / 2; | |
i++; | |
} | |
for (int j = i - 1; j >= 0; j--) | |
printf("%d", binaryNumber[j]); | |
printf("\n"); | |
} | |
int main () | |
{ | |
printf ("Motor status: "); | |
DecimalToBinary(motor10.status); | |
motor10.bits.isMoving = 1; | |
printf ("Motor status: "); | |
DecimalToBinary(motor10.status); | |
motor10.bits.isMoving = 0; | |
printf ("Motor status: "); | |
DecimalToBinary(motor10.status); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment