Please I'm facing difficultly understanding how to calculate the sampling rate of the PIC16F688 for ADC. My clock frequency(MCU) is 8MHz. and I configured ADCON1 to the following:
ADCON1 &= 0b01000000; //clear bits 6 through 0
ADCON1 |= 0b01000000; //set bits 6 though 0.
I did that according to the datasheet of the PIC. Because it has internal oscillator, and that means it works on Fosc/4, and according to table 8-1. So I'm trying to find the sampling rate. What code related to it? I think ADCON1 is the one responsible for clock period. i.e. sampling rate.
I don't think delay_ms(1000) is matter in my infinite loop. So it is not my sampling rate. Or either UART1_Init(9600).
Would you mind to help me out with that, I would appreciate that. Thanks.
char temp[5];
unsigned int adc_value;
char uart_rd;
int i;
unsigned int d[10]={0};
int average = 0;
int counter =0;
void main()
{
temp[0]='1';
temp[1]='2';
temp[2]='3';
temp[3]='4';
temp[4]=' ';
OSCCON = 0x77;
//ANSEL = 0;
ANSEL = 0b00000100;
CMCON0 = 0X07;
TRISA = 0b00001100;
// ADCON0 =0b1011;
// ADCON1 &= 0b01000000;
//ADCON1 |= 0b01000000;
UART1_Init(9600);
Delay_ms(100);
while (1)
{
average=0;
for(i=0;i<10;i++)
{
average+= ADC_Read(2);
}
average/=10;
temp[0] = average/1000+48;
temp[1] = (average/100)%10+48;
temp[2] = (average/10)%10+48;
temp[3] = average%10+48;
for (i=0;i<5; i++)
{
UART1_Write(temp[i]);
}
}
}
//Updated the code using Interrupt.// But have problem reading from ANS2.
enter code here
char temp[5];
unsigned int adc_value;
int i;
unsigned int d[10]={0};
int average = 0;
void interrupt(){
if (INTCON.T0IF) {
INTCON.T0IF = 0 ;// clear T0IF (Timer interrupt flag).
average= ADC_Read(2);
temp[0] = average/1000+48;
temp[1] = (average/100)%10+48;
temp[2] = (average/10)%10+48;
temp[3] = average%10+48;
for (i=0;i<5; i++)
{
UART1_Write(temp[i]);
}
}
TMR0 = 178;
}
void main() {
temp[0]='1';
temp[1]='2';
temp[2]='3';
temp[3]='4';
temp[4]=' ';
OSCCON= 0x77; //8MHz
ANSEL = 0b00000100; //ANS2
CMCON0 = 0X07; //
TRISA = 0b00001100;
UART1_Init(9600);
TMR0 = 178 ;
// CMCON0 = 0X04; // turn off compartor.
OPTION_REG = 0x87; //
INTCON =0xA0;
while(1);
}