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

Search This Blog

Labels

25AA010A (1) 8051 (7) 93AA46B (1) ADC (30) Analog Comparator (1) Arduino (15) ARM (6) AT89C52 (7) ATMega32 (56) AVR (57) CCS PICC (28) DAC (1) DHT11 (2) Display (105) Distance Sensor (3) DS18B20 (3) dsPIC (2) dsPIC30F1010 (2) EEPROM (5) Environment Sensor (4) esp8266 (1) I2C (29) Input/Output (67) Interrupt (19) Keil (5) Keypad (10) LCD (47) Master/Slave (1) MAX7221 (1) MCP23017 (5) MCP23S17 (4) Meter (3) MikroC (2) Motor (15) MPLABX (71) Nokia 5110 LCD (3) OLED (2) One-Wire (6) Oscillator (8) PCB (6) PCD8544 (3) PCF8574 (5) PIC (107) PIC12F (2) PIC16F628A (2) PIC16F630 (1) PIC16F716 (3) PIC16F818 (10) PIC16F818/819 (2) PIC16F84A (15) PIC16F876A (1) PIC16F877A (9) PIC16F88 (1) PIC16F887 (60) PIC18 (19) PIC18F1220 (4) PIC18F2550 (3) PIC18F4550 (12) PWM (11) RTC (8) Sensor (10) SH1106 (1) Shift Register (11) Shift Registers (3) SPI (24) STM32 (6) STM32 Blue Pill (6) STM32CubeIDE (6) STM32F103C8T6 (6) SysTick (3) temperature sensor (11) Thermometer (21) Timer/Counter (31) TM1637 (2) UART (7) Ultrasonic (4) Voltmeter (7) WDT (1) XC16 (2) XC8 (94)