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.

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

Status
Not open for further replies.

milan.rajik

Banned
Advanced Member level 5
Joined
Apr 1, 2013
Messages
2,524
Helped
540
Reputation
1,078
Reaction score
524
Trophy points
1,393
Visit site
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.
 


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

 
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.
 

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
 
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.
 


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.
 
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.
 
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
 
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.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top