# How to used pins as analog and digital?

#### BABAR2017

Joined Jul 30, 2020
2
I have a code for power factor calculation. I want to used two pins digital and 6 as analog . i have change value of ADCON1=0b0000 1001 but its not working . help me where is problem?Code is here..

C:
sbit LCD_RS at RB2_bit;
sbit LCD_EN at RB3_bit;
sbit LCD_D4 at RB4_bit;
sbit LCD_D5 at RB5_bit;
sbit LCD_D6 at RB6_bit;
sbit LCD_D7 at RB7_bit;
sbit LCD_RS_Direction at TRISB2_bit;
sbit LCD_EN_Direction at TRISB3_bit;
sbit LCD_D4_Direction at TRISB4_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D7_Direction at TRISB7_bit;
//End LCD Module Connections
int powerFactor()
{
int a=0,b=0,t=0,x=0;
float tm,pf;
char txt[5];
TMR1L=0;
TMR1H=0;
do
{
if(PORTA.F3 == 1){
T1CON.TMR1ON = 1;}
else if(PORTA.F3 == 0 && T1CON.TMR1ON == 1)
{
T1CON.TMR1ON = 0;
break;
}
}while(1);
a = (TMR1L | (TMR1H<<8)) * 2;
TMR1L=0;
TMR1H=0;
do
{
if(PORTA.F3 == 1)
{
T1CON.TMR1ON=1;
if(PORTA.F4==1)
{
T1CON.TMR1ON=0;
break;
}
}
}while(1);

b = TMR1L | (TMR1H<<8);
tm = (float)b/a;
inttostr(a,txt);
Lcd_Out(2,1,txt);
Delay_us(1000);
inttostr(b,txt);
Lcd_Out(2,7,txt);
Delay_us(1000);
pf = cos(tm*2*3.14);
x=abs(ceil(pf*100));

return x;
}

void main()
{
char c[]="0.00";
int a,b,d,x,f,e;
float tm,pf;

Lcd_Init();
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
TRISA=1;
TRISB=0;
TRISA.F3 = 1; // Makes First pin of PORTA as input
TRISA.F4 = 1; //Makes Second pin of PORTA as input

while(1)
{
a = powerFactor();
Delay_us(1);
b = powerFactor();
Delay_us(1);
d = powerFactor();
Delay_us(1);
e = powerFactor();
Delay_us(1);
f = powerFactor();

x = (a+b+d+f+e)/5;
c[3]=x%10 + 0x30;
x=x/10;
c[2]=x%10 + 0x30;
x=x/10;
c[0]=x%10 + 0x30;

Lcd_Out(1,1,"Power Factor");
Lcd_Out(2,1,c);

Delay_ms(250);
}
}
Mod edit- code tags JohnInTX

Last edited by a moderator:

#### trebla

Joined Jun 29, 2019
215
If all the pins of PORTA are either analog or digital inputs you must set TRISA first to all inputs (TRISA = 0xFF)

#### jpanhalt

Joined Jan 18, 2008
10,963
Please tell us which microcontroller you are using?

#### BABAR2017

Joined Jul 30, 2020
2

#### trebla

Joined Jun 29, 2019
215
You have in your code set up all PORTA to digital pins and pin RA0, RA3, RA4 to inputs. If you change ADCON1 to 0b0000 1001 then the RE1 and RE2 become digital and RE0, RA0-RA3, RA5 become analog. Be sure to modify your scematics accordingly.

But your code is not using any analog to digital conversion routines so far.

Last edited: