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.

How to read if port is high or low in mikroC PRO AVR ?

Status
Not open for further replies.

milan.rajik

Banned
Joined
Apr 1, 2013
Messages
2,528
Helped
540
Reputation
1,078
Reaction score
523
Trophy points
1,393
Activity points
0
How to read if port is high or low in mikroC PRO AVR ?

I have an LED connected to ATMega328P PORTB.F5 and I want to check if LED is ON or OFF. How to do that ? I tried these but they are not working.

I turned ON LED using PORTB.F5 = 1 and then used


Code C - [expand]
1
2
3
4
5
6
7
if(PORTB.F5)//do this
else //do this
 
and
 
if(PINB.F5)//do this
else //do this



but these are not working.
 

ud23

Advanced Member level 3
Joined
Apr 20, 2011
Messages
926
Helped
184
Reputation
368
Reaction score
180
Trophy points
1,323
Activity points
6,138

Code C - [expand]
1
2
3
4
5
6
DDRA.3 = 1 ' Set PORTA.3 as output
DDRB.7 = 0 ' Set PORTB.7 as input
 
if PINB.7 = 0 then ....
//  read input state of PINB.7
else

 

milan.rajik

Banned
Joined
Apr 1, 2013
Messages
2,528
Helped
540
Reputation
1,078
Reaction score
523
Trophy points
1,393
Activity points
0
I know how to read input pin. I want to know how to read the output pin. I have an LED and what to check if LED is ON or OFF.
 

ud23

Advanced Member level 3
Joined
Apr 20, 2011
Messages
926
Helped
184
Reputation
368
Reaction score
180
Trophy points
1,323
Activity points
6,138
do like this

Code C - [expand]
1
2
3
4
5
6
7
8
DDRB.5 = 1 ' Set PORTB.7 as output
 
PINB.5 = 0;
 
DDRB.5 = 0
 
if(PINB.5)
else



you can change the pin direction but for that you need to do some specific time as output and some specific time as input and specific time as output
 

milan.rajik

Banned
Joined
Apr 1, 2013
Messages
2,528
Helped
540
Reputation
1,078
Reaction score
523
Trophy points
1,393
Activity points
0
No, if I change the pin to input type when LED is ON then LED will turn OFF. There should be some other way to read an output port.
 

ud23

Advanced Member level 3
Joined
Apr 20, 2011
Messages
926
Helped
184
Reputation
368
Reaction score
180
Trophy points
1,323
Activity points
6,138

Code C - [expand]
1
2
3
4
5
int status;
         status = PINB;
 
 //or do this 
      status = inb(PINB);



would read the value of the PINB register and store it in the variable status.
i think you can read the portB register status.
 

FvM

Super Moderator
Staff member
Joined
Jan 22, 2008
Messages
48,288
Helped
14,229
Reputation
28,719
Reaction score
12,923
Trophy points
1,393
Location
Bochum, Germany
Activity points
279,616
I'm not familiar with mikroC and it's AVR specific definitions. The mikroC for AVR manual I have even doesn't know PORTB.F5 and INPB.F5 syntax.

Referring to AVR hardware, you can either read the output register (PORT) or the input port (INP). Both should give same state for a correctly connected output pin, but not for an uncorrectly connected pin, e.g. "shorted" by a LED without series resistor.
 

mjasoj

Junior Member level 2
Joined
Nov 9, 2012
Messages
21
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,283
Activity points
1,423
You should check by using following code. I read status using this code successfully for MikroC PRO for PIC, not for AVR. Hope this helps!
Code:
if(PORTB.F5==1)//do this
else //do this
 
and
 
if(PINB.F5==1)//do this
else //do this
 

milan.rajik

Banned
Joined
Apr 1, 2013
Messages
2,528
Helped
540
Reputation
1,078
Reaction score
523
Trophy points
1,393
Activity points
0
The below code worked.


Code C - [expand]
1
2
if(PORTB.B5)//do this
else if(!PORTB.B5)//do this



I don't know why it didn't work the first time.
 
  • Like
Reactions: mjasoj

    mjasoj

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top