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 |