Home Code PIC16F877 and UART mikroC example

PIC16F877 and UART mikroC example

by shedboy71

In this example we will show a basic example of reading and writing text to the UART (serial) using a PIC. We will use this in later examples when we look at sensors and we will output via a serial port.

Our test setup is an Open16F877A development board from Waveshare, we will show an example in mikroC Pro for Pic and I am using a PicKit 3.5 programmer to upload the hex file

 

Parts List

Name link
Open16F877A kit Open16F877A Package B # PIC16F877A-I/P PIC16F877A PIC 8-bit RISC Evaluation Development Board +14 Accessory Modules

 

Layout

 

Trying to find some good parts but as stated i connect a PL2303 module from Waveshare to the USART connection of the Waveshare Open16f877a

The connection is as follows

  • VCCIO ↔ 3.3V or 5V output (the module is powered from USB, onboard jumper should be shorted to 3.3V or 5V)
  • GND ↔ GND
  • TXD ↔ MCU.RX (signal direction: MCU.RX << PL2303 << PC.TX)
  • RXD ↔ MCU.TX (signal direction: MCU.TX >> PL2303 >> PC.RX)

 

Code Examples

The following example is written in MikroC – we all also include another example which we haven’t tested

[codesyntax lang=”cpp”]

char uart_rd;

void main() {
  //ANSEL  = 0;                     // Configure AN pins as digital
  //ANSELH = 0;

  UART1_Init(9600);               // Initialize UART module at 9600 bps
  Delay_ms(100);                  // Wait for UART module to stabilize

  UART1_Write_Text("Start");
  UART1_Write(13);
  UART1_Write(10);
  UART1_Write_Text("PIC UART example in MIKROC");
  UART1_Write(13);
  UART1_Write(10);

  while (1) {                     // Endless loop
    if (UART1_Data_Ready()) {     // If data is received,
      uart_rd = UART1_Read();     // read the received data,
      UART1_Write(uart_rd);       // and send data via UART
    }
  }
}

[/codesyntax]

This is the example that comes from Waveshare – untested

[codesyntax lang=”cpp”]

/*
*
*
* File                : USART.c
* Hardware Environment:	OpenPIC16F877A && rs232 &&  5v voltage && 4M crystal oscillator
* Build Environment   : MPLAB IDE && sscom32
* Version             : V8.76
* By                  : Zhou Jie
*
*                                  (c) Copyright 2011-2016, WaveShare
*                                       http://www.waveShare.net
*                                          All Rights Reserved
*
*
*/
#include<pic.h>         
 __CONFIG(0xFF32);        


void main()
 {
  TRISC=0X0;                 
  SPBRG=0XC;                 
  TXSTA=0X24;               
  RCSTA=0X90;               
  RCIE=0X1;                  
  GIE=0X1;                   
  PEIE=0X1;                
  while(1)                 
   {;}
  }


void interrupt usart(void)
  {
   if(RCIE&&RCIF)           
     {
      TXREG=RCREG;          
     }
   }

[/codesyntax]

Output

Open a terminal program like TeraTerm, choose the com port of the PL2303 and check for messages -if you type a key it should be repeated back to the terminal

You should see something like this

Download

 

Share

You may also like

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More