Advertisement
Guest User

Untitled

a guest
Aug 21st, 2017
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.85 KB | None | 0 0
  1.  
  2. #include <xc.h>
  3. #include <stdio.h>
  4.  
  5. // CONFIG1H
  6. #pragma config OSC = HS         // Oscillator Selection bits (HS oscillator)
  7. #pragma config OSCS = OFF       // Oscillator System Clock Switch Enable bit (Oscillator system clock switch option is disabled (main oscillator is source))
  8.  
  9. // CONFIG2L
  10. #pragma config PWRT = ON       // Power-up Timer Enable bit (PWRT disabled)
  11. #pragma config BOR = OFF        // Brown-out Reset Enable bit (Brown-out Reset disabled)
  12. #pragma config BORV = 20        // Brown-out Reset Voltage bits (VBOR set to 2.0V)
  13.  
  14. // CONFIG2H
  15. #pragma config WDT = OFF        // Watchdog Timer Enable bit (WDT disabled (control is placed on the SWDTEN bit))
  16. #pragma config WDTPS = 128      // Watchdog Timer Postscale Select bits (1:128)
  17.  
  18. // CONFIG3H
  19. #pragma config CCP2MUX = ON     // CCP2 Mux bit (CCP2 input/output is multiplexed with RC1)
  20.  
  21. // CONFIG4L
  22. #pragma config STVR = ON        // Stack Full/Underflow Reset Enable bit (Stack Full/Underflow will cause RESET)
  23. #pragma config LVP = OFF         // Low Voltage ICSP Enable bit (Low Voltage ICSP enabled)
  24.  
  25. // CONFIG5L
  26. #pragma config CP0 = OFF        // Code Protection bit (Block 0 (000200-001FFFh) not code protected)
  27. #pragma config CP1 = OFF        // Code Protection bit (Block 1 (002000-003FFFh) not code protected)
  28. #pragma config CP2 = OFF        // Code Protection bit (Block 2 (004000-005FFFh) not code protected)
  29. #pragma config CP3 = OFF        // Code Protection bit (Block 3 (006000-007FFFh) not code protected)
  30.  
  31. // CONFIG5H
  32. #pragma config CPB = OFF        // Boot Block Code Protection bit (Boot Block (000000-0001FFh) not code protected)
  33. #pragma config CPD = OFF        // Data EEPROM Code Protection bit (Data EEPROM not code protected)
  34.  
  35. // CONFIG6L
  36. #pragma config WRT0 = OFF       // Write Protection bit (Block 0 (000200-001FFFh) not write protected)
  37. #pragma config WRT1 = OFF       // Write Protection bit (Block 1 (002000-003FFFh) not write protected)
  38. #pragma config WRT2 = OFF       // Write Protection bit (Block 2 (004000-005FFFh) not write protected)
  39. #pragma config WRT3 = OFF       // Write Protection bit (Block 3 (006000-007FFFh) not write protected)
  40.  
  41. // CONFIG6H
  42. #pragma config WRTC = OFF       // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) not write protected)
  43. #pragma config WRTB = OFF       // Boot Block Write Protection bit (Boot Block (000000-0001FFh) not write protected)
  44. #pragma config WRTD = OFF       // Data EEPROM Write Protection bit (Data EEPROM not write protected)
  45.  
  46. // CONFIG7L
  47. #pragma config EBTR0 = OFF      // Table Read Protection bit (Block 0 (000200-001FFFh) not protected from Table Reads executed in other blocks)
  48. #pragma config EBTR1 = OFF      // Table Read Protection bit (Block 1 (002000-003FFFh) not protected from Table Reads executed in other blocks)
  49. #pragma config EBTR2 = OFF      // Table Read Protection bit (Block 2 (004000-005FFFh) not protected from Table Reads executed in other blocks)
  50. #pragma config EBTR3 = OFF      // Table Read Protection bit (Block 3 (006000-007FFFh) not protected from Table Reads executed in other blocks)
  51.  
  52. // CONFIG7H
  53. #pragma config EBTRB = OFF      // Boot Block Table Read Protection bit (Boot Block (000000-0001FFh) not protected from Table Reads executed in other blocks)
  54.  
  55. #define _XTAL_FREQ 16000000
  56.  
  57. #define RS(value) {if (value) {PORTC|= 0b10000000;} else {PORTC&=01111111;}}
  58. #define ENABLE(value) {if (value) {PORTC|= 0b01000000;} else {PORTC&=10111111;}}
  59. #define RW(value) {if (value) {PORTD|= 0b00001000;} else {PORTD&=11110111;}}
  60.  
  61. const int COUNT_UP = 1;
  62. const int COUNT_DOWN = 0;
  63. const int PRESS_ACK = 6;
  64.  
  65. static int operation;
  66. static short currentNumber;
  67. static char pressCounter = 0;
  68. static char buttonPressed = 0;
  69.  
  70. void pulseEnable() {
  71.     ENABLE(0);
  72.     __delay_us(1);
  73.     ENABLE(1);
  74.     __delay_us(1);
  75.     ENABLE(0);
  76.     __delay_us(100);
  77. }
  78.  
  79. void write4Bits(char bits) {
  80.     bits<<= 4;
  81.     PORTB = bits;
  82.     pulseEnable();
  83. }
  84.  
  85. void sendCommand(char cmd) {
  86.     RS(0);
  87.     RW(0);
  88.     write4Bits(cmd >> 4);
  89.     write4Bits(cmd);
  90. }
  91.  
  92. void writeChar(char c) {
  93.     RS(1);
  94.     RW(0);
  95.     write4Bits(c >> 4);
  96.     write4Bits(c);
  97. }
  98.  
  99. void writeString(char *s) {
  100.     while (*s) {
  101.         writeChar(*(s++));
  102.     }
  103. }
  104.  
  105. void interrupt debouncer(void) {
  106.     TMR0H = 0xD1;
  107.     TMR0L = 0x20;
  108.     INTCONbits.TMR0IF = 0;
  109.    
  110.     if (PORTB & 0x01) {
  111.         if (pressCounter <= PRESS_ACK) {
  112.             if (pressCounter == PRESS_ACK) {
  113.                 buttonPressed = 1;
  114.                 ++pressCounter;
  115.             }
  116.             ++pressCounter;
  117.         }
  118.     } else {
  119.         pressCounter = 0;
  120.         buttonPressed = 0;
  121.     }
  122. }
  123.  
  124. void logic() {
  125.     if (buttonPressed) {
  126.         if (operation == COUNT_UP) {
  127.             operation = COUNT_DOWN;
  128.         } else {
  129.             operation = COUNT_UP;
  130.         }
  131.         buttonPressed = 0;
  132.     }
  133.    
  134.     if (operation == COUNT_UP) {
  135.         if (currentNumber < 100) {
  136.             ++currentNumber;
  137.         }
  138.     } else if (currentNumber > 0) {
  139.         --currentNumber;
  140.     }
  141. }
  142.  
  143. void displayCurrentNumber() {
  144.     sendCommand(0xC0);
  145.     switch (currentNumber) {
  146.         case  0: writeString("0  "); break;
  147.         case  1: writeString("1  "); break;
  148.         case  2: writeString("2  "); break;
  149.         case  3: writeString("3  "); break;
  150.         case  4: writeString("4  "); break;
  151.         case  5: writeString("5  "); break;
  152.         case  6: writeString("6  "); break;
  153.         case  7: writeString("7  "); break;
  154.         case  8: writeString("8  "); break;
  155.         case  9: writeString("9  "); break;
  156.         case 10: writeString("10 "); break;
  157.         case 11: writeString("11 "); break;
  158.         case 12: writeString("12 "); break;
  159.         case 13: writeString("13 "); break;
  160.         case 14: writeString("14 "); break;
  161.         case 15: writeString("15 "); break;
  162.         case 16: writeString("16 "); break;
  163.         case 17: writeString("17 "); break;
  164.         case 18: writeString("18 "); break;
  165.         case 19: writeString("19 "); break;
  166.         case 20: writeString("20 "); break;
  167.         case 21: writeString("21 "); break;
  168.         case 22: writeString("22 "); break;
  169.         case 23: writeString("23 "); break;
  170.         case 24: writeString("24 "); break;
  171.         case 25: writeString("25 "); break;
  172.         case 26: writeString("26 "); break;
  173.         case 27: writeString("27 "); break;
  174.         case 28: writeString("28 "); break;
  175.         case 29: writeString("29 "); break;
  176.         case 30: writeString("30 "); break;
  177.         case 31: writeString("31 "); break;
  178.         case 32: writeString("32 "); break;
  179.         case 33: writeString("33 "); break;
  180.         case 34: writeString("34 "); break;
  181.         case 35: writeString("35 "); break;
  182.         case 36: writeString("36 "); break;
  183.         case 37: writeString("37 "); break;
  184.         case 38: writeString("38 "); break;
  185.         case 39: writeString("39 "); break;
  186.         case 40: writeString("40 "); break;
  187.         case 41: writeString("41 "); break;
  188.         case 42: writeString("42 "); break;
  189.         case 43: writeString("43 "); break;
  190.         case 44: writeString("44 "); break;
  191.         case 45: writeString("45 "); break;
  192.         case 46: writeString("46 "); break;
  193.         case 47: writeString("47 "); break;
  194.         case 48: writeString("48 "); break;
  195.         case 49: writeString("49 "); break;
  196.         case 50: writeString("50 "); break;
  197.         case 51: writeString("51 "); break;
  198.         case 52: writeString("52 "); break;
  199.         case 53: writeString("53 "); break;
  200.         case 54: writeString("54 "); break;
  201.         case 55: writeString("55 "); break;
  202.         case 56: writeString("56 "); break;
  203.         case 57: writeString("57 "); break;
  204.         case 58: writeString("58 "); break;
  205.         case 59: writeString("59 "); break;
  206.         case 60: writeString("60 "); break;
  207.         case 61: writeString("61 "); break;
  208.         case 62: writeString("62 "); break;
  209.         case 63: writeString("63 "); break;
  210.         case 64: writeString("64 "); break;
  211.         case 65: writeString("65 "); break;
  212.         case 66: writeString("66 "); break;
  213.         case 67: writeString("67 "); break;
  214.         case 68: writeString("68 "); break;
  215.         case 69: writeString("69 "); break;
  216.         case 70: writeString("70 "); break;
  217.         case 71: writeString("71 "); break;
  218.         case 72: writeString("72 "); break;
  219.         case 73: writeString("73 "); break;
  220.         case 74: writeString("74 "); break;
  221.         case 75: writeString("75 "); break;
  222.         case 76: writeString("76 "); break;
  223.         case 77: writeString("77 "); break;
  224.         case 78: writeString("78 "); break;
  225.         case 79: writeString("79 "); break;
  226.         case 80: writeString("80 "); break;
  227.         case 81: writeString("81 "); break;
  228.         case 82: writeString("82 "); break;
  229.         case 83: writeString("83 "); break;
  230.         case 84: writeString("84 "); break;
  231.         case 85: writeString("85 "); break;
  232.         case 86: writeString("86 "); break;
  233.         case 87: writeString("87 "); break;
  234.         case 88: writeString("88 "); break;
  235.         case 89: writeString("89 "); break;
  236.         case 90: writeString("90 "); break;
  237.         case 91: writeString("91 "); break;
  238.         case 92: writeString("92 "); break;
  239.         case 93: writeString("93 "); break;
  240.         case 94: writeString("94 "); break;
  241.         case 95: writeString("95 "); break;
  242.         case 96: writeString("96 "); break;
  243.         case 97: writeString("97 "); break;
  244.         case 98: writeString("98 "); break;
  245.         case 99: writeString("99 "); break;
  246.         default: writeString("100"); break;
  247.  
  248.     }
  249.    
  250. }
  251.  
  252. void main(void) {
  253.    
  254.     TRISB = 0b00000001;
  255.     TRISC = 0b00000000;
  256.     TRISD = 0X00;
  257.     __delay_ms(50);
  258.     PORTB = 0x00;
  259.     PORTC = 0x00;
  260.     PORTD = 0x00;
  261.    
  262.     operation = COUNT_UP;
  263.     currentNumber = 0;
  264.    
  265.    
  266.     INTCONbits.GIE = 1;
  267.     INTCONbits.PEIE = 1;
  268.     INTCONbits.TMR0IF = 0x0;
  269.     INTCONbits.TMR0IE = 1;
  270.    
  271.     T0CONbits.T08BIT = 0;
  272.     T0CONbits.T0CS = 0;
  273.     T0CONbits.PSA = 1;
  274.     TMR0H = 0xD1;
  275.     TMR0L = 0x20;
  276.     T0CONbits.TMR0ON = 1;
  277.    
  278.    
  279.    
  280.    
  281.    
  282.     write4Bits(0x03);
  283.     __delay_us(4500);
  284.     write4Bits(0x03);
  285.     __delay_us(4500);
  286.     write4Bits(0x03);
  287.     __delay_us(150);
  288.     write4Bits(0x02);
  289.    
  290.     // Finally, set number of lines, font size, etc.
  291.     sendCommand(0b00101000);
  292.    
  293.     __delay_us(40);
  294.    
  295.    
  296.     // Turn the display on with no cursor or blinking default
  297.     sendCommand(0b00001100);
  298.    
  299.     __delay_us(40);
  300.    
  301.     // Clear
  302.     sendCommand(0b00000001);
  303.     __delay_us(2000);
  304.     sendCommand(0b00000110);
  305.    
  306.     sendCommand(0x80);
  307.     writeString("Value:");
  308.    
  309.     __delay_ms(1000);
  310.    
  311.     displayCurrentNumber();
  312.    
  313.    
  314.     for (;;) {
  315.        
  316.         logic();
  317.         displayCurrentNumber();
  318.        
  319.         __delay_ms(500);
  320.     }
  321.     return;
  322. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement