Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. /* Main.c file generated by New Project wizard
  2. *
  3. * Created: vie mar 23 2018
  4. * Processor: PIC18F45K22
  5. * Compiler: MPLAB XC8
  6. */
  7.  
  8. #include <xc.h>
  9. #define _XTAL_FREQ 8000000
  10.  
  11. int count = 0;
  12. int apretat0 = 0;
  13. int apretat1 = 0;
  14.  
  15. void configLEDS(int augmenta) {
  16. switch (count) {
  17. case 0:
  18. PORTAbits.RA0 = 0;
  19. break;
  20.  
  21. case 1:
  22. if (augmenta)
  23. PORTAbits.RA0 = 1;
  24. else
  25. PORTAbits.RA1 = 0;
  26. break;
  27.  
  28. case 2:
  29. if (augmenta)
  30. PORTAbits.RA1 = 1;
  31. else
  32. PORTAbits.RA2 = 0;
  33. break;
  34.  
  35. case 3:
  36. if (augmenta)
  37. PORTAbits.RA2 = 1;
  38. else
  39. PORTAbits.RA3 = 0;
  40. break;
  41.  
  42. case 4:
  43. if (augmenta)
  44. PORTAbits.RA3 = 1;
  45. else
  46. PORTAbits.RA4 = 0;
  47. break;
  48.  
  49. case 5:
  50. if (augmenta)
  51. PORTAbits.RA4 = 1;
  52. break;
  53. }
  54. }
  55.  
  56. void main(void) {
  57. //Config
  58.  
  59. //Set as digital port
  60. ANSELA = 0x00;
  61. ANSELB = 0x00;
  62.  
  63. //Set RA ports as output
  64. TRISA = 0x00;
  65.  
  66. //Set RB0 and RB1 as input
  67. TRISBbits.RB0 = 1;
  68. TRISBbits.RB1 = 1;
  69.  
  70. while (1) {
  71. if (PORTBbits.RB1 && !apretat0) {
  72. //Augmentar
  73. apretat0 = 1;
  74.  
  75. __delay_ms(20);
  76. if (count < 5) ++count;
  77. configLEDS(1);
  78. }
  79. else if (!PORTBbits.RB1 && apretat0 == 1)
  80. apretat0 = 0;
  81. else if (PORTBbits.RB0 && !apretat1) {
  82. //Disminuir
  83. apretat1 = 1;
  84.  
  85. __delay_ms(20);
  86. if (count > 0) --count;
  87. configLEDS(0);
  88. }
  89. else if (!PORTBbits.RB0 && apretat1 == 1)
  90. apretat1 = 0;
  91. }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement