phillip_bourdon234

ShotDeliverSystem_Dispensary_Main.c

Jun 10th, 2020 (edited)
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.72 KB | None | 0 0
  1. // A demonstration of this project can be found here: https://bit.ly/2Ynx09x
  2.  
  3. /*
  4.     Parts used to make the dispensary:
  5.     [1] PIC18F4520
  6.     [1] 12-24v DC water pump motor
  7.     [1] 30 mA LED
  8.     [1] relay module
  9.     [1] 5v power supply
  10.     [1] 330 ohm resistor
  11.     [1] amazon box (12 inch by 24 inch)
  12.     [1] fly-back diode
  13.     [2] obstacle avoidance sensors
  14.     [20+] jumper wires
  15.     [16] AA batteries
  16.     [5] 10k resistors
  17. */
  18.  
  19. #include <xc.h>
  20. #include <pic18f4520.h>
  21. #include "ShotDelivSystemConfigbits.h"
  22.  
  23. #define MOTOR LATEbits.LE2
  24.  
  25. const char ON = 1;
  26. const char OFF = 0;
  27.  
  28. char picIsOn = 0;
  29.  
  30. void __interrupt() HIGH_ISR(void);
  31.  
  32. void bitSetupLED(void);
  33. void bitSetupObstacleAvoidanceSensors(void);
  34. void bitSetupOscillator(void);
  35. void bitSetupInterrupt(void);
  36. void bitSetupMotor(void);
  37.  
  38. void handleMotor(char);
  39.  
  40. void main(void)
  41. {      
  42.     bitSetupLED();
  43.     bitSetupObstacleAvoidanceSensors();
  44.     bitSetupOscillator();
  45.     bitSetupInterrupt();
  46.     bitSetupMotor();
  47.    
  48.     while(1)
  49.     {
  50.         if(picIsOn == 1)
  51.         {            
  52.             if(PORTBbits.RB0 == 0)
  53.             {
  54.                 handleMotor(ON);
  55.             }
  56.             else
  57.             {
  58.                 handleMotor(OFF);
  59.             }
  60.             LATDbits.LD1 = 1;
  61.             if(PORTBbits.RB2 == 1) //If the system does not detect a car, then it will go into sleep mode.
  62.             {
  63.                 handleMotor(OFF);
  64.                 LATDbits.LD1 = 0;
  65.                 picIsOn = 0;
  66.                 Sleep();
  67.             }
  68.         }
  69.     }
  70.    
  71.     return;
  72. }
  73.  
  74. void __interrupt() HIGH_ISR()
  75. {
  76.     INTCONbits.GIEH = 0;
  77.    
  78.     if(INTCON3bits.INT2IF)
  79.     {
  80.         picIsOn = 1;
  81.         __delay_ms(30); //this delay prevents a glitch that
  82.                         //happens when the car is barely in range.
  83.         INTCON3bits.INT2IF = 0;
  84.     }
  85.    
  86.     INTCONbits.GIEH = 1;
  87. }
  88.  
  89. void bitSetupLED()
  90. {
  91.     TRISDbits.RD1 = 0;
  92.     LATDbits.LD1 = 0;
  93. }
  94.  
  95. void bitSetupObstacleAvoidanceSensors()
  96. {
  97.     TRISBbits.RB0 = 1;
  98.     TRISBbits.RB2 = 1;
  99. }
  100.  
  101. void bitSetupOscillator()
  102. {
  103.     OSCCONbits.IDLEN = 0; //this allows the PIC to enter sleep mode after
  104.                           //the sleep function is called
  105.     OSCCONbits.OSTS = 1;
  106.     OSCCONbits.IRCF = 0b111;
  107.     OSCCONbits.SCS = 0b10;
  108.     while(OSCCONbits.IOFS != 1);
  109. }
  110.  
  111. void bitSetupInterrupt()
  112. {
  113.    
  114.     RCONbits.IPEN = 1;
  115.    
  116.     INTCON3bits.INT2IE = 1;
  117.     INTCON3bits.INT2IP = 1;
  118.     INTCON2bits.INTEDG2 = 1;
  119.    
  120.     INTCONbits.GIEH = 1;
  121. }
  122.  
  123. void bitSetupMotor()
  124. {
  125.     TRISEbits.RE2 = 0;
  126.     MOTOR = 0;
  127. }
  128.  
  129. void handleMotor(char motorState)
  130. {
  131.     if(motorState == 1)
  132.     {
  133.         MOTOR = 1;
  134.     }
  135.     else
  136.     {
  137.         MOTOR = 0;
  138.     }
  139. }
Add Comment
Please, Sign In to add comment