Advertisement
Guest User

One Shot Practice C language

a guest
Dec 6th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. /////////////////////////////////////////////////////////
  2. //-------Made by Dr. Rodolfo Castello------------------//
  3. /////////////////////////////////////////////////////////
  4.  
  5. #include <xc.h>
  6. //OSCILLATOR SOURCE AND DIGITAL I/O CONFIGURATION BITS
  7. //====================================================
  8. #pragma config OSC = IRCIO67 // CONFIG1H (0-3) = 0010: INTIO2 oscillator, Internal oscillator block, port function on RA6 and RA7.
  9. #pragma config MCLRE = ON // CONFIG3H.7 = 1: Pin de RESET habilitado y Entrada RE3 desactivado.
  10. #pragma config PBADEN = OFF // CONFIG3H.1 = 0: PORTB.0 -- PORTB.4 as Digital I/O.
  11. #pragma config LVP = OFF // CONFIG3H.2 = 0: Single-Supply ICSP disabled so that PORTB.5 works as Digital I/O.
  12. //PICIT-3 DEBUGGER SETUP CONFIGURATION BITS
  13. //=========================================
  14. #pragma config WDT = OFF // CONFIG2H (0) = 0: Watchdog Timer Disabled.
  15. //GLOBAL VARIABLES
  16. //================
  17. unsigned char ons = 0; // One-shot flag.
  18. unsigned char acc = 0; // Counter.
  19. // GLOBAL COMPILER?S CONSTANTS
  20. //=============================
  21. #define Button1 PORTBbits.RB0
  22. #define Button2 PORTBbits.RB1
  23. #define LED PORTBbits.RB3
  24. //FUNCTION' PROTOTYPE
  25. //===================
  26. void One_Shot(void);
  27. void Reset_One_Shot(void);
  28. void Delay(void);
  29.  
  30. void main(void){
  31. //CLOCK FREQUENCY CONFIGURATION
  32. //============================
  33. OSCCON = 0x60; // 4 MHz internal oscillator
  34. //DISABLE PORT's ANALOG FUNCTIONS
  35. //===============================
  36. CMCON = 0xFF; // Comparators OFF, to use PORT_Ds LSN
  37. CVRCONbits.CVREN = 0; // Comparator Voltge Reference Module OFF
  38. ADCON1 = 0x0F; // All ports as DIGITAL I/O
  39. //CONFIGURATION OF PORT B
  40. //=======================
  41. PORTB = 0x00; // Initialize PORTB
  42. LATB = 0x00; // Clear PORTB
  43. TRISBbits.TRISB0 = 1; // PORTB.0 as Input
  44. TRISBbits.TRISB1 = 1; // PORTB.1 as Input
  45. TRISBbits.TRISB3 = 0; // PORTB.3 as Output
  46. while(1){
  47. if (Button1 == 1) // IF RB0 ON THEN trigger the One-Shot.
  48. One_Shot();
  49. if (ons == 1) // IF One-Shot?s Trigger is ON THEN Reset-One-Shot
  50. Reset_One_Shot();
  51. if (Button2 == 1){ // IF Reset ON THEN:
  52. LED = 0; // Turn-OFF LED.
  53. acc = 0; // Restart Counter.
  54. }
  55. }
  56. }
  57.  
  58. void One_Shot(){
  59. if (ons == 1) // IF One-Shot has been triggered THEN EXIT.
  60. return;
  61. Delay(); // Eliminate Bouncing.
  62. if (Button1 == 1){ // IF RB0 still ON THEN:
  63. acc++; // Increase Counter.
  64. if (acc == 5) // IF Counter = 5 Then Turn-ON LED.
  65. LED= 1;
  66. ons = 1; // Turn-ON one-shot flag.
  67. }
  68. }
  69. void Reset_One_Shot(){
  70. if (Button1 == 0) // IF RB0 OFF THEN Eliminate Bouncing
  71. Delay();
  72. else // ELSE EXIT
  73. return;
  74. if (Button1 == 0) // IF RB0 OFF THEN reset one-shot flag.
  75. ons = 0;
  76. }
  77. void Delay(){
  78. unsigned char L1REG = 0;
  79. unsigned char L2REG;
  80. while(L1REG++ < 13){
  81. L2REG = 0;
  82. while(L2REG++ < 255);
  83. }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement