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.

[PIC] analog input and digital output on the same port

Status
Not open for further replies.

muntahajameel

Newbie level 3
Joined
Nov 24, 2014
Messages
4
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
42
I am having difficulties with having both analog input and digital output on the same port on PIC16F88.
On PIC16F88, PORTA is bidirectional I/O pins can be used for both analog inputs or digital inputs. I would like to have; RA1/AN1 as analog input and RA0/AN0 as digital output which will be connected to an LED.
it works with RA1/AN1 by configuring :
TRISA = 0x02; // set AN1 pin as input
ANSEL = 0x02; // set AN1 pin as analog
i connected a temperature sensor with RA1/AN1 and i have received the data correctly.
to configuring RA0/AN0 as digital output as:
ANSEL = 0X00; //configure AN0 pin as digital
TRISA = 0X00; //configure AN0 pin as an output pin
with that configuring the data revived from the sensor was in correct why?
is that configure was correct?
this is the program code :


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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
float temp_res=0;
float temp;
const unsigned short VREF = 4.75;
char address;
char txt[4];
char FanStatus=0x66;     // fals(f)=0x66
 
void main() {
ANSEL = 0x02;               //Configure AN1 pin as analog
TRISA = 0x02;               //Configure AN1 pin as input
ANSEL = 0X00;              //configure AN0 pin as digital
TRISA = 0X00;               //configure AN0 pin as an output pin
TRISB  = 0;                 //configure POrt B as an output port
PORTB=0;
 
        ADC_Init();                               //Initialize the ADC
        UART1_Init(9600);                         //Initialize the UART with 9600Kbps Baud Rate
        Delay_ms(100);                           // Wait for UART module to stabilize
        PORTA.f0 = 0;                            // FAN initially OFF
 while(1){    temp_res = ADC_Get_Sample(1);      //Get temp readings from ADC module
              temp = (temp_res * VREF)/10.240;
              floatToStr(temp, txt);
            /*  temp_res = ADC_Get_Sample(1);      //Get temp readings from ADC module
              temp = (temp_res * VREF)/10.240;
              floatToStr(temp, txt);
 
           if (UART1_Tx_Idle()==1)
                                    {
               UART1_Write_Text("y");
               UART1_Write_Text(txt);
               UART1_Write_Text("end");
                                     } */
 
           if (UART1_Tx_Idle()==1)                 // end if idel
              {
                 UART1_Write_Text("z");
                 UART1_Write(FanStatus);
                 UART1_Write_Text(txt);
                 UART1_Write_Text("end");
               }
               // PORTA.f0 = 0;                       // FAN initially OFF
            if (UART1_Data_Ready()==1)              //If data is received
               { address=UART1_Read();              // read the address
                  if(address==(0x63))               //if the address=m means turn on
                  {
                    PORTA.f0 = 1;
                    FanStatus=0x74;  //     true
                  }
            else if (address==(0x64))            //if the address=n means turn off
                  {
                       PORTA.f0 = 0;
                       FanStatus=0x66;
                  }
               }                                   // end if ready
                     delay_ms(10000);
          }                                       // end while
 
            }                                     //end main

 
Last edited by a moderator:

your question looks confusing to me first but if i understand correctly do you want to use two different pins one as analog input and other as digital output ?
and if my assumption is correct then you read the data at analog input now you want to do some control for example led as you mentioned in your question.
then I think this single initialization do both of your task.

Code C - [expand]
1
2
ANSEL = 0x03;               //Configure AN1 pin as analog
    TRISA = 0x03;               //Configure AN1 pin as input



ANSEL = 0x00;
because this makes all PORT A pins as digital inputs.
 
Last edited:

To use PortA for both analog input and also digital output then you need to configure your LSB of ADCON1 register of your microcontroller to set some pins as analog input and some digital Output

Code:
   TRISA = 0x00;
   ADCON1 = 0B00000100; // this make AN0 and AN1 as analog input and the rest pins as Digital . 
[\CODE]

Configure the ADCON1 register to your own desire

[COLOR="silver"][SIZE=1]- - - Updated - - -[/SIZE][/COLOR]

To use PortA for both analog input and also digital output then you need to configure your LSB of  ADCON1 register of your microcontroller to set some pins as analog input and some digital Output 

[CODE]

   TRISA = 0x00;
   ADCON1 = 0B00000100; // this make AN0 and AN1 as analog input and the rest pins as Digital .

Configure the ADCON1 register to your own desire
 

thanks for your replying , is the=at code is correct?????
TRISA = 0x02; // set AN1 pin as input
ANSEL = 0x02; // set AN1 pin as analog
ADCON1=0x01; // set AN0 PIN as digital
 

The ADCON1 programming suggested in post #3 isn't correct for PIC16F88, read the datasheet. The other setting are O.K.
 

If the ADCON1 isn't correct for PIC16F88 ,why it is work!!!!
 

Why don't you review the datasheet yourself?

ADCON1 is using only upper 4 bits in PIC16F88. Your setting is identical to default register content 00 and doesn't hurt.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top