Problem with turn on led by button.

General discussion on mikroPascal.
Post Reply
Author
Message
max_123
Posts: 3
Joined: 02 May 2011 19:58

Problem with turn on led by button.

#1 Post by max_123 » 02 May 2011 20:06

Hello, first i must sorry for my bad english :)

I have problem with turning on led by pressing button. I want turn green led - RC0 for 4sec next red led - RC1 for 4sec and green led again - RC0 for 4sec.
I try something like this :

Code: Select all

program led;

begin
     OSCCON := %01110010;
     PORTC := 0x00;
     TRISC := 0x00;
     PORTA := 0x00;
     TRISA := 0xFF;
     ANSEL  := 0;
     ANSELH := 0;

While TRUE do
   begin
        if (PORTA=0x01) then
           begin
                 PORTC:=0x01;       // Turn ON led on PORTC
                  Delay_ms(4000);
                   PORTC:=0x02;
                     Delay_ms(8000);
                       PORTC:=0x01;
                         Delay_ms(4000);
                         PORTC:=0x00;
                             end;
end;
end.
But it doesn't working good. After connect DC to PIC and send '1' for RA0 - nothing happens. But if i touch "+" in battery or some pins RA its starting. :/
If anyone can help me i will be grateful. :)

piort
Posts: 1379
Joined: 28 Dec 2005 16:42
Location: Laval,Québec,Canada,Earth... :-)
Contact:

Re: Problem with turn on led by button.

#2 Post by piort » 04 May 2011 10:59

hi,
you tell us which pic you use.... but this is a commons mistake.... read carefully ADC section of the datasheet of your pic and put the comparator at off .

hth a bit ;-)

Dany
Posts: 3854
Joined: 18 Jun 2008 11:43
Location: Nieuwpoort, Belgium
Contact:

Re: Problem with turn on led by button.

#3 Post by Dany » 04 May 2011 12:19

Code: Select all

if (PORTA=0x01) then
should be

Code: Select all

if (PORTA.0 = 1) then
With the first type of statement all pins of PortA except pin 0 have to be zero! The 2nd statement only tests pin 0.
Kind regards, Dany.
Forget your perfect offering. There is a crack in everything, that's how the light gets in... (L. Cohen)
Remember when we were young? We shone like the sun. (David Gilmour)

max_123
Posts: 3
Joined: 02 May 2011 19:58

Re: Problem with turn on led by button.

#4 Post by max_123 » 04 May 2011 20:36

@piort i'm very beginner. Can you tell me how to put off comparators??

@Dany I'll try your method and then i reply. :)

piort
Posts: 1379
Joined: 28 Dec 2005 16:42
Location: Laval,Québec,Canada,Earth... :-)
Contact:

Re: Problem with turn on led by button.

#5 Post by piort » 06 May 2011 14:35

max_123 wrote:@piort i'm very beginner. Can you tell me how to put off comparators??
which Pic do you use ?

max_123
Posts: 3
Joined: 02 May 2011 19:58

Re: Problem with turn on led by button.

#6 Post by max_123 » 06 May 2011 21:19

I using pic 16f685

I try Dany method but now i have other problem. When i connect VCC and GND the leds turning on at once. And i don't know why, because I declare PortC:= 0x00 So they shouldn't be on :/

My program look now like that:

Code: Select all

program LED;
begin
     OSCCON := %01110010;
     PORTA := 0x00;
     TRISA := 0xFF;
     PORTC := 0x00;
     TRISC := 0x00;
     ANSEL  := 0;
     ANSELH := 0;
     

While TRUE do
   begin
        if (PORTA.0=1) then
           begin
                 PORTC.0:=1;       // Turn ON diodes on PORTB
                  Delay_ms(4000);
                   PORTC.0:=0;
                   PORTC.1:=1;
                     Delay_ms(8000);
                     PORTC.1:=0;
                       PORTC.0:=1;
                         Delay_ms(4000);
                         PORTC.0:=0;
                             end;

end;
end.
And my bits configuration with Mikropascal:
Oscillator - Internal RC No Clock
Watchdog Timer - OFF
Power Up Timer - OFF
Master Clear Enable - Disable
Code Protect - OFF
Data EE Read Protect - OFF
Brown Out Detect - BOD and SBOREN disable
Internal External Switch Over Mode - disable
Monitor Clock Fail-safe - disable

piort
Posts: 1379
Joined: 28 Dec 2005 16:42
Location: Laval,Québec,Canada,Earth... :-)
Contact:

Re: Problem with turn on led by button.

#7 Post by piort » 07 May 2011 16:50

hi,
comparator off :

Code: Select all

CM1CON0.C1ON := 0;
CM2CON0.C2ON := 0;
for the start up prob, try to invert port and tris like :

Code: Select all

TRISA := 0xFF;
TRISC := 0x00;
PORTA := 0x00;
PORTC := 0x00;
HTH a bit ;-)

Dany
Posts: 3854
Joined: 18 Jun 2008 11:43
Location: Nieuwpoort, Belgium
Contact:

Re: Problem with turn on led by button.

#8 Post by Dany » 07 May 2011 19:53

max_123 wrote:I try Dany method but now i have other problem. When i connect VCC and GND the leds turning on at once. And i don't know why, because I declare PortC:= 0x00 So they shouldn't be on :/
This can happen if the porta.0 input is not properly connected to earth, e.g. there is no resistor to ground. Do you use a push buttom between portA.0 and Vcc?

I also think the following statement is not necessary:

Code: Select all

PORTA := 0x00;
It only has a meaning if PortA is configured as output.
Kind regards, Dany.
Forget your perfect offering. There is a crack in everything, that's how the light gets in... (L. Cohen)
Remember when we were young? We shone like the sun. (David Gilmour)

Post Reply

Return to “mikroPascal General”