• AVR Freaks

Hot!Cant use RA0,RA1,RC4 and RC5 I/O pins together as outputs in pic 16f676.

Author
buildlab
New Member
  • Total Posts : 18
  • Reward points : 0
  • Joined: 2019/06/26 11:50:03
  • Location: 0
  • Status: offline
2019/09/17 04:36:43 (permalink)
0

Cant use RA0,RA1,RC4 and RC5 I/O pins together as outputs in pic 16f676.

Hi,
    I started writing a code for pic 16f676 .
The problem started when i tried to turn RA0 and RA1 pin as outputs. Except RA0 and RA1, i am using RC4 and RC5  as outputs to glow leds. i am not able to use RA0 and RA1 together as outputs with the existing code(only one pin works). only one pin RA0 works as an output when i burn into microcontroller. in proteus 8 it says error, cant simulate real time because of excessive load.
I am using external oscillators and MCLR pin. so i really need this to work because i dont have free I/O pins other than changing to a bigger PIC controller. Please help
INPUT pins
RA2,RC0,RC1,RC2,RC3
OUTPUT pins 
RC4 ,RC5 ,RA0  ,RA1 
 
/*
* File: adctest.c
* Author: Welcome
*
* Created on 11 July, 2019, 3:49 PM
*/

// CONFIG
#pragma config FOSC = HS // Oscillator Selection bits (INTOSC oscillator: I/O function on RA4/OSC2/CLKOUT pin, I/O function on RA5/OSC1/CLKIN)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT enabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = ON // RA3/MCLR pin function select (RA3/MCLR pin function is MCLR)
#pragma config BOREN = ON // Brown-out Detect Enable bit (BOD enabled)
#pragma config CP = OFF // Code Protection bit (Program Memory code protection is disabled)
#pragma config CPD = OFF // Data Code Protection bit (Data memory code protection is disabled)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

#include
#include
#define _XTAL_FREQ 8000000
int lt_low,lt_high,ht_low,ht_high,dryrun, status,controls,motorstatus;

int readAdc(int);
void main(void) {
ANSEL = 0X00;
TRISA = 0X00;
TRISC = 0X00;
ANS6 = 1;
ANS5 = 1;
ANS4 = 1;
ANS2 = 1;
ANS7 = 1;
TRISC0 = 1;
TRISA2 = 1;
TRISC2 = 1;
TRISC1 = 1;
TRISC3 = 1;

PORTA = 0X00;
PORTC = 0X00;
ADCON1 = 0b100;
status = 0;
controls = 0;
motorstatus = 0;
// __delay_ms(600);

while(1)
{
lt_low = readAdc(2);
lt_high = readAdc(4);
ht_low = readAdc(5);
ht_high = readAdc(6);
dryrun = readAdc(7);
if ((dryrun > 500)&&(motorstatus == 0))
{
RC5 = 1;
motorstatus = 1;
}
if ((dryrun < 500)&&(motorstatus == 1))
{
RC5 = 0;
motorstatus = 0;
}
//////////////////////////////////////////////////////////////////////////
if (status == 1)
{
RA0 = 1;
}
if (status == 0) // These are the part where i am facing problems
{
RA0 = 0;
}
//////////////////////////////////////////////////////////////////////////
 if (ht_low < 500)
 {
 RA1 = 1;
 }
 if (ht_low > 500)
 {
 RA1 = 0;
}
//////////////////////////////////////////////////////////////////////////
if((lt_low > 500)&&(lt_high > 500)&&(status == 0))
{
status = 1;
}
if((lt_low < 500)&&(status == 1))

{
status = 0;
controls = 0;
}
if((ht_low < 500)&&(ht_high < 500)&&(status == 1)&&(controls == 0))
{
// __delay_ms(15000);
RC4 = 1;
controls = 1;
}
if ((ht_low < 500)&&(ht_high < 500)&&(status == 0))
{
RC4 = 0;
}
if ((ht_high > 500) &&(status == 1))
{
RC4 = 0;
controls = 0;
}

}
}
int readAdc(int channel){
int vol;
ADCON0 = 0x81 | (channel << 2);

__delay_us(20);
GO = 1;
while(GO == 1);
vol = (ADRESH << 8) + ADRESL;
__delay_us(5);
return vol;

}

 
post edited by buildlab - 2019/09/17 04:39:12
#1

8 Replies Related Threads

    ric
    Super Member
    • Total Posts : 23860
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Cant use RA0,RA1,RC4 and RC5 I/O pins together as outputs in pic 16f676. 2019/09/17 04:51:32 (permalink)
    +1 (1)
    You never initialise the CMCON register, so RA0 and RA1 are in analog mode.
    The datasheet does warn you about that in the "PORTA" chapter.
     
    That means every time you write to an RA# pin, it will set the other RA pin to zero.
    post edited by ric - 2019/09/17 04:53:01

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #2
    ric
    Super Member
    • Total Posts : 23860
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Cant use RA0,RA1,RC4 and RC5 I/O pins together as outputs in pic 16f676. 2019/09/17 05:14:03 (permalink)
    +1 (1)
    I would also suggest initialising your PORT registers BEFORE your TRIS registers to avoid glitches on the pins during startup.
    e.g. change:
    TRISA = 0X00;
    TRISC = 0X00;
    ANS6 = 1;
    ANS5 = 1;
    ANS4 = 1;
    ANS2 = 1;
    ANS7 = 1;
    TRISC0 = 1;
    TRISA2 = 1;
    TRISC2 = 1;
    TRISC1 = 1;
    TRISC3 = 1;

    PORTA = 0X00;
    PORTC = 0X00;

    to
    PORTA = 0;
    PORTC = 0;
    TRISA = 0b000100;    //RA2 is input
    TRISC = 0b001111;    //RC3-0 all inputs
    ANSEL = 0b11110100;    //AN2,4,5,6,7 all used (RA2, RC0, 1, 2, 3)
    CMCON = 0;    //make sure RA0 and RA1 are digital.


    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #3
    buildlab
    New Member
    • Total Posts : 18
    • Reward points : 0
    • Joined: 2019/06/26 11:50:03
    • Location: 0
    • Status: offline
    Re: Cant use RA0,RA1,RC4 and RC5 I/O pins together as outputs in pic 16f676. 2019/09/17 05:30:25 (permalink)
    0
    Hi ric,
            i tried to set CMCON = 0x00; after ANSEL .
    it didnt work. RA1 is the one which is working. RA0 is not responding to any conditions. 
    #4
    ric
    Super Member
    • Total Posts : 23860
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Cant use RA0,RA1,RC4 and RC5 I/O pins together as outputs in pic 16f676. 2019/09/17 05:40:32 (permalink)
    0
    Please show the exact code you're using now.
    I guess that if you swap the code for RA1 and RA0 (so you set RA1 first), then it would become RA1 that appears not to work.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #5
    buildlab
    New Member
    • Total Posts : 18
    • Reward points : 0
    • Joined: 2019/06/26 11:50:03
    • Location: 0
    • Status: offline
    Re: Cant use RA0,RA1,RC4 and RC5 I/O pins together as outputs in pic 16f676. 2019/09/17 10:49:39 (permalink)
    0
    Sorry for the late reply.

    Hi ric,
       I edited the code as you mentioned. still i am getting the same problem as before.
    in proteus simulator the error i am getting is cant simulate real time because of excessive load. in MCU the pin RA1 dont work.i am posting the code below.
     
     
     
    / CONFIG
    #pragma config FOSC = HS // Oscillator Selection bits (INTOSC oscillator: I/O function on RA4/OSC2/CLKOUT pin, I/O function on RA5/OSC1/CLKIN)
    #pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT enabled)
    #pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
    #pragma config MCLRE = ON // RA3/MCLR pin function select (RA3/MCLR pin function is MCLR)
    #pragma config BOREN = ON // Brown-out Detect Enable bit (BOD enabled)
    #pragma config CP = OFF // Code Protection bit (Program Memory code protection is disabled)
    #pragma config CPD = OFF // Data Code Protection bit (Data memory code protection is disabled)
    // #pragma config statements should precede project file includes.
    // Use project enums instead of #define for ON and OFF.
    #include <xc.h>
    #include<pic.h>
    #define _XTAL_FREQ 8000000
    int lt_low,lt_high,ht_low,ht_high,dryrun, status = 0,controls= 0,motorstatus = 0 ;
    int readAdc(int);
    void main(void) {
    PORTA = 0;
    PORTC = 0;
    TRISA = 0b000100; //RA2 is input
    TRISC = 0b001111; //RC3-0 all inputs
    ANSEL = 0b11110100; //AN2,4,5,6,7 all used (RA2, RC0, 1, 2, 3)
    CMCON = 0; //make sure RA0 and RA1 are digital.
    ADCON1 = 0b100;

    while(1)
    {
    lt_low = readAdc(2);
    lt_high = readAdc(4);
    ht_low = readAdc(5);
    ht_high = readAdc(6);
    dryrun = readAdc(7);
    if ((dryrun > 500)&&(motorstatus == 0))
    {
    RC5 = 1;
    motorstatus = 1;
    }
    if ((dryrun < 500)&&(motorstatus == 1))
    {
    RC5 = 0;
    motorstatus = 0;
    }
    //////////////////////////////////////////////////////////////////////////
    if (status == 1)
    {
    RA0 = 1;
    }
    if (status == 0) // These are the part where i am facing problems
    {
    RA0 = 0;
    }
    //////////////////////////////////////////////////////////////////////////
    if (ht_low < 500)
    {
    RA1 = 1;
    }
    if (ht_low > 500)
    {
    RA1 = 0;
    }
    //////////////////////////////////////////////////////////////////////////
    if((lt_low > 500)&&(lt_high > 500)&&(status == 0))
    {
    status = 1;
    }
    if((lt_low < 500)&&(status == 1))
    {
    status = 0;
    controls = 0;
    }
    if((ht_low < 500)&&(ht_high < 500)&&(status == 1)&&(controls == 0))
    {
    // __delay_ms(15000);
    RC4 = 1;
    controls = 1;
    }
    if ((ht_low < 500)&&(ht_high < 500)&&(status == 0))
    {
    RC4 = 0;
    }
    if ((ht_high > 500) &&(status == 1))
    {
    RC4 = 0;
    controls = 0;
    }
    }
    }
    int readAdc(int channel){
    int vol;
    ADCON0 = 0x81 | (channel << 2);
    __delay_us(20);
    GO = 1;
    while(GO == 1);
    vol = (ADRESH << 8) + ADRESL;
    __delay_us(5);
    return vol;
    }

     
    #6
    nice
    Super Member
    • Total Posts : 1089
    • Reward points : 0
    • Joined: 2004/09/18 11:42:25
    • Location: Germany
    • Status: offline
    Re: Cant use RA0,RA1,RC4 and RC5 I/O pins together as outputs in pic 16f676. 2019/09/17 22:10:43 (permalink)
    +1 (1)
    buildlab
    CMCON = 0; //make sure RA0 and RA1 are digital.



    Register setting and comment disagree. Read the data sheet, espicially section 6.2 and figure 6-2.
    #7
    ric
    Super Member
    • Total Posts : 23860
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Cant use RA0,RA1,RC4 and RC5 I/O pins together as outputs in pic 16f676. 2019/09/17 22:30:32 (permalink)
    +1 (1)
    That's my fault. It was meant to be
    CMCON = 0x07;
     
    Oddly, the example in the "PORTS A & C" section writes 0x05, which is also wrong.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #8
    buildlab
    New Member
    • Total Posts : 18
    • Reward points : 0
    • Joined: 2019/06/26 11:50:03
    • Location: 0
    • Status: offline
    Re: Cant use RA0,RA1,RC4 and RC5 I/O pins together as outputs in pic 16f676. 2019/09/18 01:59:50 (permalink)
    0
    Thank you ric :D
    that was the mistake,it worked like a charm after correction.
    #9
    Jump to:
    © 2019 APG vNext Commercial Version 4.5