USART SPEED

General discussion on mikroC PRO for PIC.
Post Reply
Author
Message
ifriad
Posts: 61
Joined: 28 Jan 2011 06:58

USART SPEED

#1 Post by ifriad » 20 Jan 2023 20:25

Hello all.

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]

Mince-n-Tatties
Posts: 2780
Joined: 25 Dec 2008 15:22
Location: Scotland

Re: USART SPEED

#2 Post by Mince-n-Tatties » 23 Jan 2023 23:37

what is the CPU instruction cycle time (for most instructions)?

with your pic running at 20Mhz?

with your Arduino running at 16Mhz?

this is an import physical architecture constraint to go learn about. The key search is for MIPS or Million Instructions Per Second

(why not use 8Mhz internal and turn on the x 4 PLL = 32Mhz, or use an external xtal of 10Mhz with 4xPLL = 40Mhz), you will get a shock when you do a little research!
Best Regards

Mince

ifriad
Posts: 61
Joined: 28 Jan 2011 06:58

Re: USART SPEED

#3 Post by ifriad » 24 Jan 2023 00:24

Dear Mince-n-Tatties,

Many thanks for the information and the advice.

Best regards.

Ihab

Post Reply

Return to “mikroC PRO for PIC General”