Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

care to explain? uart..

Status
Not open for further replies.

butters

Member
can anybody care to explain the codes below in detail?
step by step...
coz it confuses me with basic uart sending and receiving programs... thanks... :)

Code:
/*
 * where c:\mcc18 is the directory in which the compiler is installed.
 * This sample application is designed for use with the MPLAB ICD2, the
 * PICDEM 2 Plus demo board, and the PIC18F452 device.  This sample covers the
 * following items:
 * 
 * 1.  Creating large data objects
 * 2.  Reading from and writing to the USART. 
 * 3.  Interrupt handling (#pragma interrupt, interrupt vectors, and
 *     interrupt service routines)
 * 4.  System header files
 * 5.  Processor-specific header files
 * 6.  #pragma sectiontype
 * 7.  Inline assembly
 */

#include <p18f452.h>
#include <usart.h>

void rx_handler (void);

#define BUF_SIZE 25

/*
 * Step #1  The data is allocated into its own section.
 */
#pragma idata bigdata
char data[5][BUF_SIZE+1] = {
  { "String #0\n\r" },
  { "String #1\n\r" },
  { "String #2\n\r" },
  { "String #3\n\r" },
  { "Invalid key (0-3 only)\n\r" }};

#pragma idata

#pragma code rx_interrupt = 0x8
void rx_int (void)
{
  _asm goto rx_handler _endasm
}
#pragma code

#pragma interrupt rx_handler
void rx_handler (void)
{
  unsigned char c;

  /* Get the character received from the USART */
  c = ReadUSART();
  if (c >= '0' && c <= '3')
    {
      c -= '0';
      /* Display value received on LEDs */
      PORTB = c;

      /*
       * Step #2  This example did not need an additional
       * pointer to access the large memory because of the
       * multi-dimension array.
       *
       * Display the string located at the array offset
       * of the character received
       */
      putsUSART (data[c]);// writes string of character from data[c]
    }
  else
    {
      /*
       * Step #2  This example did not need an additional
       * pointer to access the large memory because of the
       * multi-dimension array.
       *
       * Invalid character received from USART.
       * Display error string.
       */
      putsUSART (data[5]);

      /* Display value received on LEDs */
      PORTB = c;
    }

    /* Clear the interrupt flag */
    PIR1bits.RCIF = 0;
}

void main (void)
{
  /* Configure all PORTB pins for output */
  TRISB = 0;

  /*
   * Open the USART configured as
   * 8N1, 2400 baud, in polled mode
   */
  OpenUSART (USART_TX_INT_OFF &
             USART_RX_INT_ON &
             USART_ASYNCH_MODE &
             USART_EIGHT_BIT &
             USART_CONT_RX &
             USART_BRGH_HIGH, 103);

  /* Display a prompt to the USART */
  putrsUSART (
    (const far rom char *)"\n\rEnter a digit 0-9!\n\r");

  /* Enable interrupt priority */
  RCONbits.IPEN = 1;

  /* Make receive interrupt high priority */
  IPR1bits.RCIP = 1;

  /* Enable all high priority interrupts */
  INTCONbits.GIEH = 1;

  /* Loop forever */
  while (1)
    ;
}
 
Last edited:
You need to explain which part you are having difficulty understanding. All of the code appears easy to read and has excellent comments.
 
You need to explain which part you are having difficulty understanding. All of the code appears easy to read and has excellent comments.

okay.. sure thing..

Code:
#pragma interrupt rx_handler
void rx_handler (void)
{
  unsigned char c;

  /* Get the character received from the USART */
  c = ReadUSART();
  if (c >= '0' && c <= '3')
    {
      c -= '0';
      /* Display value received on LEDs */
      PORTB = c;

      /*
       * Step #2  This example did not need an additional
       * pointer to access the large memory because of the
       * multi-dimension array.
       *
       * Display the string located at the array offset
       * of the character received
       */
      putsUSART (data[c]);// writes string of character from data[c]
    }
  else
    {
      /*
       * Step #2  This example did not need an additional
       * pointer to access the large memory because of the
       * multi-dimension array.
       *
       * Invalid character received from USART.
       * Display error string.
       */
      putsUSART (data[5]);

      /* Display value received on LEDs */
      PORTB = c;
    }

    /* Clear the interrupt flag */
    PIR1bits.RCIF = 0;
}

i will.. thanks..

does this code meant for interrupts? and i assume if there is interrupt, the LED would light up?
 
Yes, the code uses interrupts. You can see the relevant parts below.

Code:
This sample covers the
 * following items:
 * 
 * 1.  Creating large data objects
 * 2.  Reading from and writing to the USART. 
[b] * 3.  Interrupt handling (#pragma interrupt, interrupt vectors, and
 *     interrupt service routines)[/b]

The call that starts the USART is below, with the relevant constant to enable interrupt on receive.

Code:
  OpenUSART (USART_TX_INT_OFF &
             [b]USART_RX_INT_ON[/b] &
             USART_ASYNCH_MODE &
             USART_EIGHT_BIT &
             USART_CONT_RX &
             USART_BRGH_HIGH, 103);

When a receive interrupt is generated, the following things happen:

  • The data is read from the USART
  • The data is checked to ensure it is a character between 0 and 3
  • The number received is displayed (presumably on LEDs) on PORTB
  • A string is sent back over the USART, from the array ("String #0".. etc)
  • The interrupt flag is cleared, so that it can be generated again later

This is a little simplified, but follows the first part of the interrupt handler. So to answer your question yes, LEDs would light on PORTB depending what character/number you send via the USART.
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top