Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. #include <reg51.h>
  2.  
  3. sbit button = P1^1; // Botão de pressão: 0 -> pressionado, 1 -> não pressionado
  4.  
  5. unsigned char counter = 0; // Contador de 50 ms
  6.  
  7. void write(unsigned char character)
  8. {
  9. SBUF = character; // Carrega o caracter a ser transmitido para o buffer
  10. while (!TI); // Aguarda até a transmissão terminar
  11. TI = 0; // Limpa a flag
  12. }
  13.  
  14. unsigned char read()
  15. {
  16. while (!RI); // Aguarda até a receção terminar
  17. RI = 0; // Limpa a flag
  18. return SBUF; // Retorna o caracter recebido
  19. }
  20.  
  21. void write_msg(unsigned char* msg)
  22. {
  23. unsigned char i;
  24.  
  25. for (i = 0; msg[i] != '\0'; i++)
  26. write(msg[i]);
  27. }
  28.  
  29. void timer0() interrupt 1 // Rotina de interrupção do Timer 0
  30. {
  31. TH0 = 0x4C;
  32. TL0 = 0x24;
  33. counter++;
  34. }
  35.  
  36. void delay_ms(unsigned int delay)
  37. {
  38. unsigned int i, j;
  39.  
  40. for (i = 0; i < delay; i++)
  41. {
  42. for (j = 0; j < 100; j++);
  43. }
  44. }
  45.  
  46. void main()
  47. {
  48. unsigned char no_code[] = {0xC0, 0xF9, 0xA4, 0xB0, 0X99, 0x92, 0x82, 0xF8, 0x80, 0x90}; // 0123456789
  49. unsigned char pos; // Posição do array a ser acedida
  50. unsigned char count_flag = 0; // Flag de contagem: 0 -> parado, 1 -> a contar
  51. unsigned char bt_up = 1; // Posição anterior do botão de pressão: 0 -> pressionado, 1 -> não pressionado
  52. bit count_mode = 1; // Modo de contagem: 0 -> descrescente 1 -> crescente
  53.  
  54. TMOD = 0x21; // Timer 1: modo 2, Timer 0: modo 1
  55. SCON = 0x50; // Habilitar receção de dados
  56. TH1 = 0xFD; // Definir valor do Timer 1 para baudrate de 9600
  57. TR1 = 1;
  58.  
  59. TH0 = 0x4C; // Valor do Timer 0: 50ms
  60. TL0 = 0x24;
  61. TR0 = 0; // Contagem do Timer 0: 0 -> parado, 1 -> a contar
  62. EA = 1; // Interrupções: 0 -> desabilitadas, 1 -> habilitadas
  63. ET0 = 1; // Interrupções do Timer 0: 0 -> desabilitadas, 1 -> habilitadas
  64.  
  65. do
  66. {
  67. write_msg("Insira um número de 0 a 9:\n");
  68. pos = read() - 48;
  69. } while (pos < 0 || pos > 9);
  70.  
  71. P2 = no_code[pos]; // Apresenta o valor no display
  72.  
  73. write_msg("Pressione o botão para iniciar a contagem\n");
  74. write_msg("(0/1) altera o modo de contagem\n");
  75.  
  76. while (1)
  77. {
  78. if (!button && bt_up) // Botão pressionado
  79. {
  80. delay_ms(50); // Debounce
  81. if (!button && bt_up) // Botão pressionado
  82. {
  83. count_flag = !count_flag;
  84. bt_up = !bt_up;
  85. TR0 = !TR0;
  86. counter = 0;
  87.  
  88. if (count_flag)
  89. write_msg("Contador está a contar\n");
  90. else
  91. write_msg("Contador está parado\n");
  92. }
  93. }
  94.  
  95. if (button && !bt_up) // Botão não pressionado
  96. {
  97. delay_ms(50); // Debounce
  98. if (button && !bt_up) // Botão não pressionado
  99. bt_up = !bt_up;
  100. }
  101.  
  102. if (count_flag && counter == 20) // Timer atingiu 1s
  103. {
  104. if (count_mode)
  105. pos += (pos == 9) ? -9 : 1; // Incrementa o valor do display
  106. else
  107. pos -= (pos == 0) ? -9 : 1;
  108. P2 = no_code[pos]; // Apresenta o valor no display
  109. counter = 0;
  110. }
  111.  
  112. if (RI)
  113. {
  114. if (SBUF == '0')
  115. {
  116. count_mode = 0;
  117. write_msg("Modo de contagem: decrescente\n");
  118. }
  119. else if (SBUF == '1')
  120. {
  121. count_mode = 1;
  122. write_msg("Modo de contagem: crescente\n");
  123. }
  124. else
  125. write_msg("Caracter não suportado!\n(0/1) altera o modo de contagem\n");
  126. RI = 0;
  127. }
  128. }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement