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.

Problem with LED

Status
Not open for further replies.

peterzatko

New Member
I am beginner.
Please I need help with microprocesor - LED´s (ON/OFF).
What is wrong in my code.
I need :

When is on PORTC.0 - Ground then RB1 (LED2 ON)
When is on PORTC.0 - Power supply (5V) then RB1 (LED1 ON)
When PORTC.0 not equal 0 or 1 (is in third estate) then RB1 (LED3 ON)

- PORTC is and must be as digital input.

Code:

digital:


If PORTC = 0 then
PORTB.0 = 0
PORTB.1 = 1
PORTB.2 = 0
delay_ms(2)
goto digital
end If

IF PORTC.0 = 1 then
PORTB.0 = 1
PORTB.1 = 0
PORTB.2 = 0
delay_ms(2)
goto digital
end If

If (PORTC <> 1) or (PORTC <> 0) then
PORTB.0 = 0
PORTB.1 = 0
PORTB.2 = 1
end If
delay_ms(2)




goto digital
 
I am beginner.
Please I need help with microprocesor - LED´s (ON/OFF).
What is wrong in my code.
I need :

When is on PORTC.0 - Ground then RB1 (LED2 ON)
When is on PORTC.0 - Power supply (5V) then RB1 (LED1 ON)
When PORTC.0 not equal 0 or 1 (is in third estate) then RB1 (LED3 ON)

- PORTC is and must be as digital input.

Code:

digital:


If PORTC = 0 then //is this PORTC OR PORTC.0 ?
PORTB.0 = 0
PORTB.1 = 1
PORTB.2 = 0
delay_ms(2)
goto digital
end If

IF PORTC.0 = 1 then
PORTB.0 = 1
PORTB.1 = 0
PORTB.2 = 0
delay_ms(2)
goto digital
end If

If (PORTC <> 1) or (PORTC <> 0) then
PORTB.0 = 0
PORTB.1 = 0
PORTB.2 = 1
end If
delay_ms(2)




goto digital


I'm not familiar with this language, is it assemble? for PIC?

Also, what exactly is PORTC.0? Is it referring to the state of the a physical pin (like in the picture)?
If so, your wiring is wrong.

With the way you presented it, it seems to me that you want to detect the state of a pin (PORTC.0 configured as INPUT) that is connected to the switch.

If you are reading it as digital input, it can only have TWO states: HIGH or LOW. There is no such thing as "PORTC.0 not equal 0 or 1 (is in third estate)".
Maybe you're referring to the OUTPUT state? If you do, I'm warning you now. You might destroy your MCU with your wiring.
 
Yes it mikrobasic.i change it but in third estate always LED3 ON.I want to LED2 ON
Here is new code

digital:
temp_res = ADC_Read(0) ' Result of A/D conversion is copied to temp_res
aa = temp_res ' 8 LSBs are moved to PORTD
OUT1 = 0
OUT2 = 0

If PORTC.0 = 0 then
PORTB.0 = 0
PORTB.1 = 1
PORTB.2 = 0
delay_ms(2)
goto digital
end If

IF PORTC.0 = 1 then
PORTB.0 = 1
PORTB.1 = 0
PORTB.2 = 0
delay_ms(2)
goto digital
end If

If (PORTC.0 <> 1) or (PORTC.0 <> 0) then
PORTB.0 = 0
PORTB.1 = 0
PORTB.2 = 1
end If
delay_ms(2)




goto digital
 
This will ALWAYS be the case

Code:
If (PORTC.0 <> 1) or (PORTC.0 <> 0) then
   PORTB.0 = 0
   PORTB.1 = 0
   PORTB.2 = 1
end If
delay_ms(2)

if portc.0 is not on or portc.0 is on.... this covers everthing on that pin

If you need to evaluate this you'll need a chip with a comparator OR an ADC input... if you want to detect high impedance, its the only way...
 
Last edited:
Again, there is no such thing as "not 0 or 1" in digital. It IS either 1 or 0. I don't get how you will get a state of "not 0 or 1".

temp_res = ADC_Read(0)

OK, ADC. Does ADC_Read() give out codewords or just a high or low (0 or 1)? I'm just assuming here, but from what I see, ADC is used for ANALOG, not digital. Getting an input from an analog pin won't give you ONLY 1 or 0. It will give you an integer codeword(commonly ADC in MCUs have Vref=5V, unipolar). If the MCU uses 10-bit ADC, then HIGH is 1023(voltage = Vref) and LOW is 0.

but in third estate always LED3 ON
So no matter what you do with the switch, LED 3 is always ON? This is probably because the pin is now configured as analog.


I'll tell you again, if you want digital, there are only TWO states. There is no such thing as third state. It's either ON or OFF. No more, no less.


Regarding the switch SW1, please help me here. This is how I understand the switch.:
Pin 5 is the common pin in the switch.
If the top-most slider is turned on, pin 5 will short with pin 1 which is connected to 5V? Right?
Like wise, turning on the second to the top slider means connecting pin 5 to pin 2 which is connected to ground.
Am I right?

Now, what you want to ask here is if you turn top most slider ON, LED 1 will light and LED 2 and 3 are off. (because pin 5 is connected to 5V)
Next, if you turn slider 2 on, LED 2 will light up while 1 and 3 are off. (Because pin 5 is now connected to ground)
Finally, if you are referring about the "third state" as both of the sliders are on, you are SHORTING 5V with ground and that is VERY VERY bad. Furthermore, PORTC.0 will have a random value (either 1 or 0) which you don't have any control over.

But if I'm wrong about my first assumption about the switch, then it must be your lucky day. You won't have to re-solder a new voltage regulator, yay!;)
 
Would change the wiring diagram I need to fix the logic analyzer using these ports. This means that, if the input logic unit lights up when LED1 is a logical zero LED2 lights up. If anything is not connected to LED3 lights (lights me if a logical zero).
 
Would change the wiring diagram I need to fix the logic analyzer using these ports. This means that, if the input logic unit lights up when LED1 is a logical zero LED2 lights up. If anything is not connected to LED3 lights (lights me if a logical zero).

Okay. LED1 and LED2 can be configured to light up with the conditions you want. But LED3 is impossible.
If the implementation is digital (i think even if analog), you won't be able to detect if "anything is not connected". Like I told before, your input pin (portc.0) CAN detect logic high or low, but if there is nothing connected, there WON'T be a third state. With that wiring, there's just no way you can detect if "anything is not connected". Just forget the third state.
Why? Because if you leave portc.0 unconnected to anything, it will automatically assume some state (high or low) and therefore you still will have a HIGH or LOW input.

If (PORTC.0 <> 1) or (PORTC.0 <> 0) then
PORTB.0 = 0
PORTB.1 = 0
PORTB.2 = 1
end If
delay_ms(2)
Also, take a look at this code. I'm assuming that the operator <> means not equal in mikrobasic.
What you are saying here is that if portc.0 is not equal to HIGH or LOW, LED3 should light up.
You are using the "or" operator here, which means even if one of the two conditions is true, you will execute the codes inside the if statement. Think about what I just said really hard and go to basic truth table for OR, you will get what Ian Rogers and me are saying. ;)


PS: Even after thinking and understanding what we are saying about your code, the LED3-lights-up-when-nothing-is-connected thing still won't be possible, sorry.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top