728x90

728x90

Sunday, April 25, 2021

PIC18f2550 RS-232 Programming example in CCS PICC

In previous post, we showed a DIY prototyping board for PIC18F2550. That board has a block of RS-232 to TTL converter but it was not tested. We will test that block here separately. 

CCS PICC has a built-in library of RS-232. It doesn't need to configure all related registers in SFR to make this module work. Its primary task is to add the #use rs232() directive in source code. This directive needs the #use delay(clock=x) directive to be declared first. For example,

#use delay (clock=4M)
#use RS232(uart1,baud=9600)

C directive showed above inform the compiler to use 4MHz clock frequency. Default UART is uart1 for most of PIC micro-controller that come with USART module inside. The baud rate is set to 9600.

UART communication for 8-bit are RC6 for transmission (Tx), and RC7 for reception (Rx). UART data I/O functions are just like the ANSI C library function. For example printf() is use for outputting ASCII data from micro-controller from its Tx pin. Other ANSI C functions are usable in CCS PICC such as getc(), putc(), kbhit(), etc.

This example will show how to use these RS-232 functions with PIC18F2550 micro-controller.

#include <18f2550.h>
//use external 20MHz crystal oscillator 
#fuses HS,NOWDT,PLL1,CPUDIV1
#use delay(clock=20M)
#use rs232(uart1,baud=9600)

void main(void){
   char temp1=0;
   //Clear PortC
   output_C(0x00);
   //PortC as output
   set_tris_C(0x00);
   printf("PIC18F2550 USB Board\n\r");
   printf("Please enter number between 0 and 3\n\r");
   printf("to toggle RC1 and RC2.\n\r");
   while(1){    
   //Test buffer
      if(kbhit()){
            temp1=getc();
            putc(temp1);
      }
      switch(temp1){
         case '0':
            output_low(pin_c1);
            temp1=0;
            printf("\tRC1 is OFF.\n\r");
            break;
         case '1':
            output_high(pin_c1);
            temp1=0;
            printf("\tRC1 is ON.\n\r");
            break;
         case '2':
            output_low(pin_c2);
            temp1=0;
            printf("\tRC2 is OFF.\n\r");
            break;
         case '3':
            output_high(pin_c2);
            temp1=0;
            printf("\tRC2 is ON.\n\r");
            break;
         case 0:
            break;
         default:
            printf("\n\rPlease enter number between 0 and 3\n\r");
            printf("to toggle RC1 and RC2.\n\r");
            temp1=0;
            break;
      }
   }
}

I tested this example on my prototyping board.

PIC18f2550 RS-232 Programming example in CCS PICC
Program testing on prototyping board

PIC18f2550 RS-232 Programming example in CCS PICC
Host PC and PIC18F2550 side sending/receive data

I run this testing using a desktop PC with COM port on Windows 7 64-bit. The serial terminal showed above is a part of CCS PICC compiler IDE.

No comments:

Post a Comment

320x50

Search This Blog

tyro-728x90