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.

PIC16F877A How to enable PORT RB1, RB2, RB3, RB4

Status
Not open for further replies.

sajay

Newbie level 4
Joined
Apr 11, 2015
Messages
6
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
64
Dear Guys,
I got below code on other forum, my question is how to enable PORTB RB1, RB2, RB3, RB4, below code only enable for PORTB RB0 (if (INTCON.INTF) ).

I mean is when RB1 toggle then PORTC & PORTD ON, one more toggle PORTC & PORTD OFF.

Code:
int cnt = 127;          

void main() {

TRISB = 0xFF;
TRISC = 0X00;
PORTC = 0x00;
TRISD=0x00;
PORTD=0x40;

ADCON1 = 0x7F;          // all pins digital

0xD0;          // gie i int enabled
OPTION_REG = 0x00;

PWM1_Init(5000);         // pwm freq = 5000hz
PWM1_Set_Duty(cnt);
PWM1_Start();

while (1)
  {

  }
}

void interrupt (void)
{
if (INTCON.INTF)
{
  //motor changes direction on each interrupt:
  PORTD.f7=~PORTD.f7;             // D.f7=1 (motor turns clockwise);

  PORTD.f0=~PORTD.f0;             // LED on D.f1 changes state on each interrupt
  INTCON.INTF = 0;                // reset int flag
}

if (INTCON.INTF)
{
  //motor changes direction on each interrupt:
  PORTD.f6=~PORTD.f6;             // D.f7=1 (motor turns clockwise);

  PORTD.f1=~PORTD.f1;             // LED on D.f1 changes state on each interrupt
  INTCON.INTF = 0;                // reset int flag

}
}
 

Hi,

use TRISB to change INPUT/OUTPUT = portB direction.
use PORTB to set/clear (high/low, 1/0) PORT B bits.

both are 8 bit registers for 8 bits = 8 data lines of port B.

Read datasheet on this.

Klaus
 

Because of an example I'm assuming you try to write code in MikroC

If you want to define PORTB pins RB1, RB2, RB3, RB4 as outputs write this:

Code:
void main()
{
  while(1) {
    //define RB1, RB2, RB3, RB4 as outputs
    TRISB.B1 = 0;
    TRISB.B2 = 0;
    TRISB.B3 = 0;
    TRISB.B4 = 0;

    //Turn on Output:
    PORTB.B1 = 1;
    PORTB.B2 = 1;
    PORTB.B3 = 1;
    PORTB.B4 = 1;

    delay_ms(1000);  //paus for 1sec
    
   //Turn off Output:
    PORTB.B1 = 0;
    PORTB.B2 = 0;
    PORTB.B3 = 0;
    PORTB.B4 = 0;
}

If you want to define PORTB pins RB1, RB2, RB3, RB4 as inputs write this:

Code:
void main()
{
  while(1) {

    //define RB1, RB2, RB3, RB4 as inputs
    TRISB.B1 = 1;
    TRISB.B2 = 1;
    TRISB.B3 = 1;
    TRISB.B4 = 1;
    //define RB0 as output
    TRISB.B0 = 0;
    
   //Turn on Output RB0 if one of inputs RB1, RB2, RB3, RB4 = 1;
    if(PORTB.B1 == 1) {
       PORTB.B0 = 1;
    }
    else if (PORTB.B2 == 1) {
       PORTB.B0 = 1;
    }
    else if (PORTB.B3 == 1) {
       PORTB.B0 = 1;
    }
    else if (PORTB.B4 == 1) {
       PORTB.B0 = 1;
    }
    else {
       PORTB.B0 = 0;  //Turn off RB0 if non of RB1, RB2, RB3, RB4  are pressed
    }
  }
}
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top