Port direct bit access

General discussion on mikroC.
Post Reply
Author
Message
buracek0110
Posts: 1
Joined: 09 Jun 2011 23:04

Port direct bit access

#1 Post by buracek0110 » 09 Jun 2011 23:21

Hello. I'm a begginer and trying to solve this for the last 2 hours, but cannot find anything. I'm trying to access the individual bit of ports with variable.
This isn't working:

for (val = 0; variable < 8; variable++) {
PORTB.variable = 0;
delay_ms(1000);
PORTB.variable = 1;
delay_ms(1000);
}

This isn't woking either:

int out [] = {rb0_bit, rb1_bit, rb2_bit, rb3_bit, rb4_bit, rb5_bit, rb6_bit, rb7_bit};
out[val] = 0;

This is just an example, but what am I doing wrong?

Thanks

User avatar
slavisa.zlatanovic
mikroElektronika team
Posts: 1321
Joined: 07 Apr 2009 09:39

Re: Port direct bit access

#2 Post by slavisa.zlatanovic » 10 Jun 2011 10:04

Hi!

You should write something like this:

Code: Select all

char variable;

void main(){
TRISB = 0;
PORTB = 255;

 for (variable = 0; variable < 8; variable++) {
  PORTB &=  ~(1  << variable);
  Delay_ms(1000);
  PORTB |=  1  << variable;
  Delay_ms(1000);
 }

}
Best regards
Slavisa

Post Reply

Return to “mikroC General”