My 12 year old son is building a circuit to activate/deactivate a KNEX motor based on a timing circuit (on for ~55 seconds). I am trying to help him get a jump start in understanding how to do it using a 16F628A programming in C
We have been googling a ton and trying to understand how it all works (with a 1 week deadline). We are unable to understand how to use the config settings and the code as written doesn't even turn on the LED yet! The code below is our attempt to turn on LED based on switch input.
The end goal is a program that when powered up turns on LED1 and looks for input from SW1. Upon input from SW1 it should turn on LED2 and delay for 3 seconds while watching SW2 (for cancel). If no input from SW2 in 3 seconds then turn on the Relay for 55 seconds while also activating LED2 to indicate the program is running. After 55 seconds expires go back to watching SW1 with LED1 active.
Currently the test circuit has:
MCLR and VDD tied together with an 8K ohm resistor
VDD tied to 5V input
VSS tied to ground
RB0 to switch to ground
RB1 to switch to ground
RB2 to 330ohm resistor to LED to ground
RB3 to 330ohm resistor to LED to ground
Please help!
We have been googling a ton and trying to understand how it all works (with a 1 week deadline). We are unable to understand how to use the config settings and the code as written doesn't even turn on the LED yet! The code below is our attempt to turn on LED based on switch input.
The end goal is a program that when powered up turns on LED1 and looks for input from SW1. Upon input from SW1 it should turn on LED2 and delay for 3 seconds while watching SW2 (for cancel). If no input from SW2 in 3 seconds then turn on the Relay for 55 seconds while also activating LED2 to indicate the program is running. After 55 seconds expires go back to watching SW1 with LED1 active.
Currently the test circuit has:
MCLR and VDD tied together with an 8K ohm resistor
VDD tied to 5V input
VSS tied to ground
RB0 to switch to ground
RB1 to switch to ground
RB2 to 330ohm resistor to LED to ground
RB3 to 330ohm resistor to LED to ground
Please help!
Rich (BB code):
/************************
* Description: LED Test with buttons
* Turn on LED for x seconds
* Pin Assignments:
* RB0 = Switch 1
* RB1 = Switch 2
* RB2 = LED1
* RB3 = LED2
* RB4 = Relay connection
**************************/
#define _XTAL_Frec 4000000
#include<xc.h>
#define LED_ON 1 // LED is connected cathode to ground
#define LED_OFF 0
#define RELAY_ON 1 //RELAY is connected to ground
#define RELAY_OFF 0 //RELAY not connect to ground
#define BUTTON_ON 0 // Button input is low when button pressed
#define BUTTON_OFF 1 // (this is how a pull-up resistor button works)
#define I_O_OUT 0 // standard TRIS definitions
#define I_O_IN 1 // zero is 0ut, one is 1n
#define SW_A RB0 // switch A is on PORTB.0
#define SW_B RB1
#define SW_A_TRIS TRISB0 // switch B is on PORTB.1
#define SW_B_TRIS TRISB1
#define LED_A RB2 // LED A is on PORTB.2
#define LED_B RB3 // LED B is on PORTB.3
#define RELAY RB4 // RELAY is connected to ground
#define LED_A_TRIS TRISB2
#define LED_B_TRIS TRISB3
#define RELAY _TRIS TRISB4
void main(void)
{
SW_A_TRIS = I_O_IN; // set up I/O pin directions
SW_B_TRIS = I_O_IN;
LED_A_TRIS = I_O_OUT;
LED_B_TRIS = I_O_OUT;
// now loop forever to sense buttons
while(1)
{
if (SW_A == BUTTON_ON) // test the first switch
LED_A = LED_ON;
else
LED_A = LED_OFF;
if (SW_B == BUTTON_ON) // test the second switch
LED_B = LED_OFF;
else
LED_B = LED_ON;
}
}