Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // A demonstration of this project can be found here: https://bit.ly/2Ynx09x
- /*
- Parts used to make the dispensary:
- [1] PIC18F4520
- [1] 12-24v DC water pump motor
- [1] 30 mA LED
- [1] relay module
- [1] 5v power supply
- [1] 330 ohm resistor
- [1] amazon box (12 inch by 24 inch)
- [1] fly-back diode
- [2] obstacle avoidance sensors
- [20+] jumper wires
- [16] AA batteries
- [5] 10k resistors
- */
- #include <xc.h>
- #include <pic18f4520.h>
- #include "ShotDelivSystemConfigbits.h"
- #define MOTOR LATEbits.LE2
- const char ON = 1;
- const char OFF = 0;
- char picIsOn = 0;
- void __interrupt() HIGH_ISR(void);
- void bitSetupLED(void);
- void bitSetupObstacleAvoidanceSensors(void);
- void bitSetupOscillator(void);
- void bitSetupInterrupt(void);
- void bitSetupMotor(void);
- void handleMotor(char);
- void main(void)
- {
- bitSetupLED();
- bitSetupObstacleAvoidanceSensors();
- bitSetupOscillator();
- bitSetupInterrupt();
- bitSetupMotor();
- while(1)
- {
- if(picIsOn == 1)
- {
- if(PORTBbits.RB0 == 0)
- {
- handleMotor(ON);
- }
- else
- {
- handleMotor(OFF);
- }
- LATDbits.LD1 = 1;
- if(PORTBbits.RB2 == 1) //If the system does not detect a car, then it will go into sleep mode.
- {
- handleMotor(OFF);
- LATDbits.LD1 = 0;
- picIsOn = 0;
- Sleep();
- }
- }
- }
- return;
- }
- void __interrupt() HIGH_ISR()
- {
- INTCONbits.GIEH = 0;
- if(INTCON3bits.INT2IF)
- {
- picIsOn = 1;
- __delay_ms(30); //this delay prevents a glitch that
- //happens when the car is barely in range.
- INTCON3bits.INT2IF = 0;
- }
- INTCONbits.GIEH = 1;
- }
- void bitSetupLED()
- {
- TRISDbits.RD1 = 0;
- LATDbits.LD1 = 0;
- }
- void bitSetupObstacleAvoidanceSensors()
- {
- TRISBbits.RB0 = 1;
- TRISBbits.RB2 = 1;
- }
- void bitSetupOscillator()
- {
- OSCCONbits.IDLEN = 0; //this allows the PIC to enter sleep mode after
- //the sleep function is called
- OSCCONbits.OSTS = 1;
- OSCCONbits.IRCF = 0b111;
- OSCCONbits.SCS = 0b10;
- while(OSCCONbits.IOFS != 1);
- }
- void bitSetupInterrupt()
- {
- RCONbits.IPEN = 1;
- INTCON3bits.INT2IE = 1;
- INTCON3bits.INT2IP = 1;
- INTCON2bits.INTEDG2 = 1;
- INTCONbits.GIEH = 1;
- }
- void bitSetupMotor()
- {
- TRISEbits.RE2 = 0;
- MOTOR = 0;
- }
- void handleMotor(char motorState)
- {
- if(motorState == 1)
- {
- MOTOR = 1;
- }
- else
- {
- MOTOR = 0;
- }
- }
Add Comment
Please, Sign In to add comment