Verifying PIC24EP Clock Frequency by UART Module

So the complete UART1 ISR is implemented as follows

void __attribute__((interrupt, auto_psv)) _ISR _U1RXInterrupt()
     char dat;
     while(U1STAbits.URXDA)   {
	dat=U1RXREG&0xff; // clean up by zeroing upper 8-bit
             dat=toupper(dat);          // convert to uppercase character
             if((dat=='\r')||(dat=='\n'))  {  // response when [Enter] is hit
              INB[Rx1Cnt]=dat ;    // put in global buffer
            //-------------naive command interpreter ------------
            if(INB[Rx1Cnt-2]=='T' && INB[Rx1Cnt-1]=='O' && INB[Rx1Cnt]=='N' ){
                  _T1IE = 1;            // enable timer 1
                  PutStrU1("\r\nTimer 1 ON\r\n");
            if(INB[Rx1Cnt-3]=='T' && INB[Rx1Cnt-2]=='O' && INB[Rx1Cnt-1]=='F' &&   	INB[Rx1Cnt]=='F' )   {
                  LEDA = 0;
                 _T1IE = 0;          // disable timer 1
                 PutStrU1("\r\nTimer 1 OFF\r\n");
            if (Rx1Cnt>=maxINB-1) { Rx1Cnt=maxINB-1; } // should stay only in allocated buffer
      IFS0bits.U1RXIF = 0; // clear interrupt flag

A couple of points worth mentioning.

  • In order for the program to work correctly, the data read from URXDA must be cleaned up by zeroing the upper 8-bit part. Then the characters are converted to upper case. This simplifies command interpreation.
  • Receiving characters are pushed into a global buffer INB. While doing so, the ISR checks whether a string of “TON” or “TOFF” is found. If that is the case, timer 1 is turned on or off by setting _T1IE = 1 or _T1IE = 0, respectively, where _T1IE represents interrrupt enable bit of timer 1. A text response is transmitted to host using PutStrU1( ) function.
  • Whenever a user presses [Enter], a response “Ready” is sent.
  • As a C programmer should always beware, never write beyond the allocated buffer size.
  • Do not forget to clear interrupt flag before leaving the ISR.

For your convenience, the complete C source file for this example can be downloaded here.

uart_ main.c

As shown in the video below, UART communication is established successfully at 115200 bps. Timer 1 can be controlled via TON and TOFF commands as desired. This result also verifies that PIC24E is indeed running at 70 MIPS.

The experiment was captured using a webcam included with my monitor. Sorry for the poor quality. I just want to show it alongside the messages in terminal program.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s