Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <wiringPi.h>
  3.  
  4. int binary(int num) {
  5. int mult = 1;
  6. int res = 0;
  7. while(num > 1) {
  8. int d = num % 2;
  9. res += d * mult;
  10. mult *= 10;
  11. num /= 2;
  12. }
  13. res += num * mult;
  14. return res;
  15. }
  16.  
  17. int main (void)
  18. {
  19. int num;
  20. printf("Dame el numero para pasar a binario: ");
  21. scanf("%d", &num);
  22.  
  23. wiringPiSetup();
  24. pinMode(0, OUTPUT);
  25. digitalWrite(0, LOW);
  26. pinMode(1, OUTPUT);
  27. digitalWrite(1, LOW);
  28. pinMode(2, OUTPUT);
  29. digitalWrite(2, LOW);
  30. pinMode(4, OUTPUT);
  31. digitalWrite(4, LOW);
  32.  
  33. int res = binary(num);
  34. if(res % 10 == 1) {
  35. digitalWrite(4, HIGH);
  36. } else {
  37. digitalWrite(4, LOW);
  38. }
  39. res /= 10;
  40. if(res % 10 == 1) {
  41. digitalWrite(2, HIGH);
  42. } else {
  43. digitalWrite(2, LOW);
  44. }
  45. res /= 10;
  46. if(res % 10 == 1) {
  47. digitalWrite(1, HIGH);
  48. } else {
  49. digitalWrite(1, LOW);
  50. }
  51. res /= 10;
  52. if(res % 10 == 1) {
  53. digitalWrite(0, HIGH);
  54. } else {
  55. digitalWrite(0, LOW);
  56. }
  57. return 0 ;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement