Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

PIC16F819

Status
Not open for further replies.

Namams

New Member
I'm a newbie to this site, so please excuse me if I'm posting in the wrong forum section, or if my question is too elementary. I've just recently started using MPLab to program a PIC16F819 I'm trying to write a code in which a single LED is switch on if 2 certain switches are pressed within 2 seconds of each other. I've started a basic code in which I have 4 inputs (switches) and four outputs (LED's) at the moment each switch corresponds to a single LED so if I was to press switch1 then LED1 lights up.

How would I code the PIC so that LED1 would light up if I was to press switch1 and then switch2 (within 2 seconds of each other)?
Thanks in advance.

Current code:

#include <16F819.h>
#fuses INTRC_IO,NOWDT,NOPROTECT,MCLR,NOBROWNOUT
#use delay(clock=4000000)

#define SW2 PIN_B7
#define SW2 PIN_B6
#define SW3 PIN_B5
#define SW4 PIN_B4
#define LED1 PIN_B3
#define LED2 PIN_B2
#define LED3 PIN_B1
#define LED4 PIN_B0

void main()
{
port_b_pullups(TRUE);

while(1)
{
if(!input(SW1)) output_high(LED1);
if(!input(SW2)) output_high(LED2);
if(!input(SW3)) output_high(LED3);
if(!input(SW4)) output_high(LED4);

}
}
 
You need a 2 second count down timer. If the timer hasn't reached zero and a button (switch?) is pressed then light the LED. Reset the timer to 2 seconds whenever any key is pressed should do all you need.

Mike.
 
Thank you, could you please explain briefly how I could program a countdown timer or point me in the direction of any sources that might be helpful?
 
Delay 20mS, decrement count, read keys, count>0 then flash LED, else count = 100 (1/20mS*2 = seconds).

Mike.
 
Also, clearly define which pins are input and which are output, don't see that in your code. Default on power up is all pins are input.
 
This is the current code I've create it uses a timer 0 overflow of 65.5 ms. When switch 1 is pressed "counter 1" is given a value of 0. I wanted the value of counter1 to then increment with every following interrupt until it reaches 32 interrupts( roughly 2s) but I'm not sure how to implement that in my code.

Current code:
#include <16F819.h>
#fuses INTRC_IO,NOWDT,NOPROTECT,MCLR,NOBROWNOUT
#use delay(clock=4000000)

#define SW1 PIN_B7
#define SW2 PIN_B6
#define LED1 PIN_B3
int GO = 0;
int SW1_PRESSED = 0;
int SW2_PRESSED = 0;
int LIGHT_LED1 = 0;
int Counter1 = 0;

#INT_RB void
RB_isr(void)
{
if(!input(SW1)) {
SW1_PRESSED = 1; }
else {
SW1_PRESSED = 0; }
if(!input(SW2)) {
SW2_PRESSED = 1; }
else {
SW2_PRESSED = 0; } }

#INT_TIMER0
void TIMER0_isr(void)
{
GO = 1;
}

void main()
{
port_b_pullups(0xF0); setup_timer_0(T0_INTERNAL|T0_DIV_256);
enable_interrupts(INT_RB); enable_interrupts(INT_TIMER0); enable_interrupts(GLOBAL);

while(1)
if(GO==1) {
GO =0; }

if(SW1_pressed==1) { LIGHT_LED1 = 1;
Counter1=0;
Counter1++; }

if(Counter1 > 31) { LIGHT_LED1 = 0; }

if((SW2_pressed==1)& (Counter1 <31)) { LIGHT_LED1 = 2; }

If(LIGHT_LED1 ==2) { output_high(LED1); }
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top