+ Post New Thread
Results 1 to 5 of 5
  1. #1
    Junior Member level 1
    Points: 234, Level: 2

    Join Date
    May 2012
    Posts
    18
    Helped
    0 / 0
    Points
    234
    Level
    2

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

    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 bigdogguru; 29th July 2012 at 20:50. Reason: Added Code or Syntax Tags

    •   Alt29th July 2012, 16:34

      advertising

        
       

  2. #2
    Advanced Member level 3
    Points: 11,557, Level: 25
    xpress_embedo's Avatar
    Join Date
    Jul 2011
    Location
    India
    Posts
    945
    Helped
    151 / 151
    Points
    11,557
    Level
    25
    Blog Entries
    1

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

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

    ADCON1 = 0x0F;



    •   Alt29th July 2012, 18:27

      advertising

        
       

  3. #3
    Advanced Member level 5
    Points: 38,906, Level: 48
    Tahmid's Avatar
    Join Date
    Jun 2008
    Location
    Cornell University, Ithaca, New York, USA (from Dhaka, Bangladesh)
    Posts
    4,712
    Helped
    1663 / 1663
    Points
    38,906
    Level
    48
    Blog Entries
    19

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

    Quote Originally Posted by nooobboy4321 View Post
    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 - - -

    Quote Originally Posted by arunsharma0731 View Post
    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.



    •   Alt29th July 2012, 20:37

      advertising

        
       

  4. #4
    Banned
    Points: 6,265, Level: 18

    Join Date
    Jul 2012
    Posts
    1,192
    Helped
    169 / 169
    Points
    6,265
    Level
    18

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

    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.
    Last edited by internetuser2k12; 30th July 2012 at 08:17.



  5. #5
    Newbie level 5
    Points: 83, Level: 1

    Join Date
    Feb 2013
    Posts
    8
    Helped
    0 / 0
    Points
    83
    Level
    1

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

    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 by hemantc007; 14th May 2013 at 06:15. Reason: update



+ Post New Thread
Please login