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.

Setting ALL PORTS of PIC16F877A as digital INPUT/OUTPUT in MikroC Pro

Status
Not open for further replies.

nooobboy4321

Junior Member level 1
Joined
May 22, 2012
Messages
18
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,447
Hi there,
I have problems in configuring my MCU as Input and also Output in my simple project. My goal on my program is like example, when RA0 of PORTA is toggled, all PORTB will blink ON-OFF, if I toggle RB0 of PORTB, all PORTC will blink ON-OFF, and if I toggle RC0, all PORTD will blink ON-OFF, etc....
I have a little background of programming using Mikro C. So far I able to do a simple blinking LED on a Breadboard using PORTB and PORTD as Input/Output.
I use this simple codes in Mikro C Pro:
---------------------------------------------------------------------

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
void_main()
 {
  TRISB = 0xFF;           >> Set PORTB as Input
  TRISD = 0xFF;           >> Set PORTD as Input
 
  do
   { 
    if ( portb.f0 == 1 )   >> If RB0 is Toggled
     {
      TRISD = 0x00;       >> Set PORTD as Output
      PORTD = 0xFF;      >> Toggle On
      Delay_ms(500);
      PORTD = 0x00;      >> Toggle OFF
      Delay_ms(500);
     }
    if ( portd.f0 == 1 )   >> If RD0 is Toggled
     {
      TRISB = 0x00;       >> Set PORTB as Output
      PORTB = 0xFF;      >> Toggle On
      Delay_ms(500);
      PORTB = 0x00;      >> Toggle OFF
      Delay_ms(500);
     }
   }while(1);
 }


---------------------------------------------------------------------------
My Question is how to use PORTA, PORTC and PORTE same as I use PORTB and PORTD as Digital Input/Output?
Thanks in advance.
 
Last edited by a moderator:

To use PORTA and PORTE as input output port, you have include the line at the starting

ADCON1 = 0x0F;
 

My Question is how to use PORTA, PORTC and PORTE same as I use PORTB and PORTD as Digital Input/Output?
Thanks in advance.

Setting the corresponding TRIS bits to 1 will configure the pins as input and setting them to 0 will configure the pins as output.

You must disable the ADC and comparator if you want to use the pins that are multiplexed to the ADC and comparator, as digital IO's.

To disable the ADC, use either of the 2:
Code:
ADCON1 = 6;
or
Code:
ADCON1 = 7;

See page 130 of the datasheet.

To disable the comparator, use:
Code:
CMCON = 7;

See page 138 of the datasheet.

Hope this helps.
Tahmid.

- - - Updated - - -

To use PORTA and PORTE as input output port, you have include the line at the starting

ADCON1 = 0x0F;

By using ADCON1 = 0x0F, he won't be able to use AN0, AN2 and AN3 - RA0, RA2 and RA3. See page 130 of datasheet.
 

Try this and zip and post your proteus .dsn file.


Code:
void main()
 {
  TRISA = 0xFF;
  TRISB = 0xFF;           // Set PORTB as Input
  TRISC = 0xFF;
  TRISD = 0xFF;           // Set PORTD as Input
  PORTA = 0x00;
  PORTB = 0x00;
  PORTC = 0x00;
  PORTD = 0x00;
  CMCON = 0x07;
  ADCON1 = 0b10000110;
  // or
  // ADCON1 = 0b10000111;
  do
   {

    if(PORTA.F0 == 1)     // If RA0 is Toggled
     {
       TRISB = 0x00;       // Set PORTB as Output
       PORTB = 0xFF;       // Toggle On
       Delay_ms(500);
       PORTB = 0x00;       // Toggle OFF
       Delay_ms(500);
       TRISB = 0xFF;
     }

    if(PORTB.F0 == 1)  // If RB0 is Toggled
     {
       TRISC = 0x00;       // Set PORTC as Output
       PORTC = 0xFF;       // Toggle On
       Delay_ms(500);
       PORTC = 0x00;       // Toggle OFF
       Delay_ms(500);
       TRISC = 0xFF;
     }

     if(PORTC.F0 == 1) // If RC0 is Toggled
     {
        TRISD = 0x00;       // Set PORTD as Output
        PORTD = 0xFF;       // Toggle On
        Delay_ms(500);
        PORTD = 0x00;       // Toggle OFF
        Delay_ms(500);
        TRISD = 0xFF;
     }

    }while(1);
 }

When TRISA = 0xFF; and when you click the button connected to RA0 then TRISB = 0x00; i.e., the PORTB will be configured as output and at that time you cannot read from RB0-RB7. The same applies for PORTC and PORTD.
 

Attachments

  • LED BLINK.rar
    16.4 KB · Views: 85
Last edited:

Hello
I have some kind of problem
using pic16f877a and have lm324 output at RA0 and RA1
and checking them and glowing led until input as 1

Code:
#include<htc.h>
#include<conio.h>
#include<stdio.h>
#include <xc.h>

// CONFIG
//__CONFIG(HS & WDTDIS & PWRTDIS & BORDIS & LVPEN & WRTEN & UNPROTECT & DEBUGEN);
#pragma config FOSC = HS        // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = OFF      // Brown-out Reset Enable bit (BOR disabled)
#pragma config LVP = ON         // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3/PGM pin has PGM function; low-voltage programming enabled)
#pragma config CPD = OFF        // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF        // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF         // Flash Program Memory Code Protection bit (Code protection off)

#define _XTAL_FREQ 2000000

void main()
{


 TRISB=0; //portb as output
TRISA=1; //porta as input

ADCON1=7; //all input digital
CMCON=7;  // disable comparator
PORTB=0x00;
PORTA=0x00;

 
    while(!RA0)
    {
        PORTB=0x01;
        __delay_ms(10000);
    }

    __delay_ms(40000);
    

    while(!RA1)
    {
        PORTB=0x02;
        __delay_ms(10000);
    }
}


Problem is that RA0 input works fine but RA1 does not take any input as such.

plz help very urgent...plz plz plz..

-------------------------update------------------
I found that using RA0 and RE0 are working fine can can one tell me why so ..
why cant I use RA0 and RA1 to gather..

Thank you.
 
Last edited:

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top