Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] Pushbutton counter PIC16F819

Status
Not open for further replies.

stephansavic

Newbie level 2
Joined
Dec 26, 2011
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,293
So I wanna control 2 LED's with Microcontroller.

I am using PIC16F819.

Here's the code :
Code:
void main()
{
 int counter,state,last;
 TRISA=0;
 TRISB=0;
 PORTB=1;
 PORTA=0b00000001;
 ADCON1=7;
 OSCCON=0b01100000;
 counter=0;
 state=1;
 last=1;
 while ( 1 == 1 )
 {
    state=RA0_bit;
    if ( state != last && state == 0 ) counter++;
    if ( counter % 2 == 0 ) { RA2_bit=0; RA1_bit=1;  }
    else
    if ( counter % 2 == 1 ) { RA1_bit=0; RA2_bit=1;  }
    last=state;

 }
}
I am using pull-up resistor 4,7kOhm.
RA1 is LED1.
RA2 is LED2.
RA0 is input pin.
It seems that I can't change state.
Pushbutton works only when it's pressed once ( if I press it for the 1st time,then he switches the LED's ).
Can you tell me where I'm wrong?
Thanks in advance! :)
 

So I wanna control 2 LED's with Microcontroller.

I am using PIC16F819.

Here's the code :
Code:
void main()
{
 int counter,state,last;
 TRISA=0;
 TRISB=0;
 PORTB=1;
 PORTA=0b00000001;
 ADCON1=7;
 OSCCON=0b01100000;
 counter=0;
 state=1;
 last=1;
 while ( 1 == 1 )
 {
    state=RA0_bit;
    if ( state != last && state == 0 ) counter++;
    if ( counter % 2 == 0 ) { RA2_bit=0; RA1_bit=1;  }
    else
    if ( counter % 2 == 1 ) { RA1_bit=0; RA2_bit=1;  }
    last=state;

 }
}

I am using pull-up resistor 4,7kOhm.
RA1 is LED1.
RA2 is LED2.
RA0 is input pin.

It seems that I can't change state.
Pushbutton works only when it's pressed once ( if I press it for the 1st time,then he switches the LED's ).
Can you tell me where I'm wrong?
Thanks in advance! :)

What compiler are you using, Microchip or Hi-Tech?

Also, can you post your configuration bit settings? I assume you have set them in your IDE, MPLAB, etc.

Just glancing at your program, I noticed a few issues:

You have PORTA set as output only, no inputs defined. You state RA0 is an input pin, therefore your TRISA statement should be:

TRISA = 0x01;

Not

TRISA = 0;

Also if RA0 is an input, why are you initializing it high?

PORTA=0b00000001;

Did you intend this:

PORTA=0b00000010;

Which would set LED1 on and LED2 off, initially. However as soon as your code runs the states of the LEDs should flip.

Examine this if loop:

if ( state != last && state == 0 )

I'm not sure it is what you intended.

To implement a SuperLoop:

while (1)

is sufficient, instead of:

while ( 1 == 1 )


You are using a 4.7kΩ pullup resistor for the button, have you also put current limiting resistors inline with your LEDs, otherwise they will quick burnout.

Or is this a Proteus Simulation?

BigDog
 
Thanks for reply! :)
I am using mikroC PRO.
Here's configuration :

Code:
Oscillator : INTRC RA6 is PORT I/O
Watchdog Timer : Off
Power Up Timer : Off
MCLR Select bit : RA5 is digital I/O,MCLR tied to VDD
Brown Out detect : Off
Low Voltage Program : Disabled
Data EE Read Protect : Disabled
Flash Program Write : Write Protection Disabled
Background Debug : Disabled
CCP1 Mux : RB2
Code Protect : Disabled

Thanks again! :)

---------- Post added at 10:21 ---------- Previous post was at 10:16 ----------

Thanks mate !!!
It works!!!
Marking it as Solved. ;)
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top