I 've build an electric circuit with 2 magnet sensors to control an engine.
When 1 sensor gets a signal de engine should start, then when the other one gets a signal the engine has to stop. I need to control al this with a microcontroller (PIC12F683). But something is wrong. Don't know if I've build the circuit wrong, or the code of my chip. I've used next code:
( GPIO.F0 = sensor 1, GPIO.F1 = sensor 2, GPIO.F4 = engine)
Code:
void init() {
// Configure GPIO Port
ANSEL = 0b00000001; // Configure all GPIO pins as DIGITAL
TRISIO = 0b00000011; // GP0 and GP1 are input, rest is output
GPIO = 0x00; // Set all output pins to low
WPU = 0x00; // Weak-pull-up disabled
// Configure AD-onvertor
ADCON0 = 0x00; // AD disabled
ADRESH = 0x00; // Init the AD-register
// Configure Comparator
CMCON0 = 0xFF; // Comparator is turned OFF
CMCON1 = 0x00; // Comparator is turned OFF
// Interrupt Configuration
INTCON = 0x00; // Disable all interrupts
}
void main() {
init();
while(1)
{
if (GPIO.F0){
GPIO.F4 = 1 ;
}
if (GPIO.F1){
GPIO.F4 = 0;
}
}
}
I hope someone can tell me this is correct or not.
in order to eliminate debouncing. If you are not using the MikroC write the debuoncing function yourself as sensors have bin known to "flicker"
also check the logic inside your while loop. what if both bit 0 and bit1 of GPIO are 1 ? you will on/off the engine, I'm sure your intent was not to flick it, so, you have to decide what your "default" value in all four cases is (0,0; 0,1; 1,0; 1,1) as you cannot expect the input to always be only (0,1; 1,0).