iot

In this third post we will see how to communicate with the PIC18LF24K40 using the serial channel RS232. Our purpose is to connect the PIC to our desktop and establish bi-directional communication. In the next post we'll see how to make a wireless connection to computers and phones, we now need a reliable channel for testing and debugging.

To do this we need a TTL/EIA transceiver working at 3V (NS-RS232-01) and an RS232/USB converter (PL-2303RA).

post3

From computer side we can communicate with the micro through a terminal program such as CoolTerm.

We now open the IDE and launch the MCC plugin; we will configure the pic registres to activate the serial channel (EUSART).

Schermata 2017 11 29 alle 17.21.28 

In the left column under "Peripherals" click twice on the EUSART entry. "Easy Setup" will open in the central window where you can enable and set the serial port. The values to use in the example are those shown in the figure. The PIC has the serial transmission set on the RC6 port, reception can be set to any other port between B and C (this indicates the orange open padlock on figure); I chose the RC7 port for reception by clicking on the Port C 7 box (green padlock). In brief, you need to connect the TTL/EIA converter to the following pins:

  • PIC TX RC6 -> converter input (RX)
  • PIC RX RC7 -> converter output (TX)

By enabling the "Redirect STDIO to USART" option, we tell to the compiler to output printf() and other stdio functions to the serial channel; this makes life easier for us to send text strings to the desktop.

Finally I've marked the box "Enable EUSART Interrupts" so that I have an IRQ every time the external computer sends strings to the PIC, the alternative would be to monitor the status of the receive buffer at each cycle. Similarly for the outgoing IRQ that allows you to send text strings without worrying to check from time to time if the transmitting buffer is free.

Click Project Resource - System - Pin Module than disable the analogue function of the serial pin:

Schermata 2017 11 29 alle 17.18.36 

By clicking on the "Generate" button, we end the setting of the PIC registers.

MCC has created new configuration files for us and updated previously created files. For the sake of clarity I renamed the output port to which the LED was connected, this caused the rewriting of the pin_manager.c file where the program renamed the port and all the functions related to it; not bad, isn't it?

MCC has also created interrupt_manager.c and eusart.c files for us, we will focus on the details of these files in the future, the important thing now is to take advantage of these conveniences to send a text string to the external computer. There is nothing easier, as shown in the main() function below. As suggested by MCC, you need to enable global and peripheral interrupts then with the printf() statement we send to the outside world via RS232 the strings we want.

Schermata 2017 11 30 alle 13.24.44

And here's the verification of what's being sent to the desktop:

Schermata 2017 11 30 alle 13.22.51

Important personal note (2018/02/28)

Sometimes unexplained things happen. The example was done using the 3V version of the PIC18LF24K40 micro. The same micro at 5V (PIC18F24K40 - 1633Q1J) does not work as expected. In particular, the printf() and even memset() and memcpy() statements do not work. Until update I do not recommend the use of that micro.

download project