I am using pic 18F4520 running at 20Mhz.
I am using it to log the values from 4 ADC + timer 0 (set at 0.1 msec). I am sending the data to USART running at a baud rate of 1000000. The code is running as expected but I am getting time resolution on the logged data of about 10msec.
When using an Arduino Uno board and using the micros() built in Arduino function I get a time resolution of approximately 0.6 msec.
I was expecting the PIC running at 20Mhz to perform at the least the same. I am attaching the relevant part of the code for reference.
Any idea to improve is highly appreciated.
Code: Select all
if(mode6) // this would send timer reading and ADC values to the USB.
{
/* using the 20MHZ and usart baud rate of 1000000 we get about 3ms for reading the ADC.
another 3 ms to format the counter and send it to the USART.
the total time to do this loop is approx 10 ms.
Hence we have a time resolution of about 10ms.
*/
if(receive=='S' && usart_logging==0){
cnt=0;
usart_logging=1;
Glcd_Write_Text("Logging", 50, 5 , 1);
}
if(receive=='R' && usart_logging==1 ){
usart_logging=0;
Glcd_Write_Text(" ", 50, 5 , 1);
}
if(usart_logging){
ADCON0.chs0=0; ADCON0.chs1=0; ADCON0.chs2=0; // This is used to select RA0/AN0 as analogue input
ADCON0.Go_Done=1;
while (ADCON0.Go_Done==1) ;
read0= (ADRESH<<8) + ADRESL ;
ADCON0.chs0=1; // This is used to select RA1/AN1 as analogue input
ADCON0.Go_Done=1;
while (ADCON0.Go_Done==1) ;
read1= (ADRESH<<8) + ADRESL ;
ADCON0.chs0=1; ADCON0.chs1=1; // This is used to select RA3/AN3 as analogue input
ADCON0.Go_Done=1;
while (ADCON0.Go_Done==1) ;
read3= (ADRESH<<8) + ADRESL ;
ADCON0.chs0=0; // This is used to select RA2/AN2 as analogue input
ADCON0.Go_Done=1;
while (ADCON0.Go_Done==1) ;
read2= (ADRESH<<8) + ADRESL ;
ADCON0.chs0=1; ADCON0.chs1=1; // This is used to select RA3/AN3 as analogue input
ADCON0.Go_Done=1;
while (ADCON0.Go_Done==1) ;
read3= (ADRESH<<8) + ADRESL ;
txt[0]= cnt/1000000000 + 48 ;
txt[1]= (cnt/100000000) %10 + 48 ;
txt[2]= (cnt/10000000) %10 + 48 ;
txt[3]= (cnt/1000000) %10 + 48 ;
txt[4]= (cnt/100000) %10 + 48 ;
txt[5]= (cnt/10000) %10 + 48 ;
txt[6]= (cnt/1000) %10 + 48 ;
txt[7]= (cnt/100) %10 + 48 ;
txt[8]= (cnt/10) %10 + 48 ;
txt[9]= cnt%10 + 48 ;
//LongWordToStrWithZeros(cnt,txt);
if (read0temp != read0){
//txt[12] = read0 / 1000 + 48; // prepare value for display
txt[11] = (read0 / 100) % 10 + 48;
txt[12] = (read0 / 10) % 10 + 48;
txt[13]= read0 % 10 + 48;
}
if (read1temp != read1){
//txt[18] = read1 / 1000 + 48; // prepare value for display
txt[15] = (read1 / 100) % 10 + 48;
txt[16] = (read1 / 10) % 10 + 48;
txt[17]= read1 % 10 + 48;
}
if (read2temp != read2){
//txt[24] = read2 / 1000 + 48; // prepare value for display
txt[19] = (read2 / 100) % 10 + 48;
txt[20] = (read2 / 10) % 10 + 48;
txt[21]= read2 % 10 + 48;
}
if (read3temp != read3){
//txt[30] = read3 / 1000 + 48; // prepare value for display
txt[23] = (read3 / 100) % 10 + 48;
txt[24] = (read3 / 10) % 10 + 48;
txt[25]= read3 % 10 + 48;
}
if (UART1_Tx_Idle() == 1) {
UART1_Write_text(txt);
// UART1_Write(0x0D);
// UART1_Write(0x0A);
}
read0temp = read0;
read1temp = read1;
read2temp = read2;
read3temp = read3;
}
}
[\code]