PIC MCU and Python Serial Communication

PIC MCU and Python Serial Communication

Hiya, guys! On this undertaking I’ll attempt to clarify my experiments on PIC MCU and Python serial communication. Over the web, there are various tutorials and movies on tips on how to talk with PIC MCU over digital terminal which may be very helpful. Nevertheless, in my fundamental undertaking one of many necessities is to determine serial communication between PIC MCU and Python which I could not discover good tutorial. So, let’s get began ๐Ÿ™‚

Trainer Notes

Academics! Did you employ this instructable in your classroom?

Add a Trainer Word to share the way you included it into your lesson.

Step 1: What Do We Want?

So, to start with let’s examine what we’d like. When it comes to :

  • Absolutely, PIC MCU which is in my case PIC16f877a (You do not want that board. That is with a purpose to simplify some soiled connections)
  • USB to TTL converter with a purpose to talk with PIC MCU over USB port through the use of TX and RX pins.
  • MCU programmer which is in my case Okay150 programmer, however you should use PicKit 2,three or anything.
  • And at last a pc ๐Ÿ™‚

When it comes to software program:

  • An IDE with a purpose to write python code which is in my case Pycharm, however you should use common Python IDE additionally.
  • An atmosphere for programming the MCU which is in my case MPLAB X IDE with CCS C compiler.

Step 2: Connections

Within the image connected connections are given that are between PIC MCU and USB TTL converter as under:

RC7 (RX) —————> TXD

RC6 (TX) —————> RXD

GND —————-> GND

You need not join VCC pin of the USB TTL converter (Nevertheless, if you need you are able to do). These three connections are fairly sufficient.

Step three: Python Software program

Let’s begin to write software program for Python aspect which shall be transmitter in our case, as a result of it can ship the string to the MCU.

import serial #import serial library

knowledge = '24' #knowledge that we'll ship knowledge = a+'' ser = serial.Serial('COM17', baudrate=9600, timeout=1) #connect with the port ser.write(a.encode()) #ship the info

To start with serial library is imported with a purpose to use its futures. We need to ship an instance string knowledge with a purpose to verify in MCU code that we accepted it. I would like right here to notice one factor. That is why we added ‘’ to the string. It is because, on the MCU aspect it’s not possible to learn the string completely. It’s learn character by character. So, we need to know the top of the string with a purpose to cease studying. So, we add ‘’ to the string which signifies the top of the string. Then we connect with the port that’s related to the MCU. You may decide that port by looking out in ‘Machine Supervisor’. So, watch out that you’re on the identical port. In spite of everything, we ship the info to the MCU. “.encode()” ought to be added to the string knowledge so as to have the ability to ship it to the receiver.

Step four: Microcontroller Software program

So, let’s examine our code for MCU. To start with I need to present you the “config.h” file’s inside which isn’t required, however I did it for simplicity. Right here simply change frequency of your MCU.

#ifndef CONFIG_H

#outline CONFIG_H

#embrace <16F877A.h> #gadget ADC=16

#FUSES NOWDT //No Watch Canine Timer #FUSES NOBROWNOUT //No brownout reset #FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O

#use delay(crystal=6000000)

Now let’s examine the primary code:



#use rs232 (baud=9600, xmit=pin_C6, rcv=pin_C7, parity=N, cease=1)

#outline LED_RED PIN_D0 char inp; char cmp_[]="24"; char buffer[3];

#int_rda void serial_communication_interrupt()

void fundamental(void)

Firstly we embrace string library which we shall be useful in string operations which in our case is string evaluate operation (strcmp). So, our function on this code is to activate the led related to the pin D0 if the transmitted worth is identical as our given worth which is “cmp_” equals to “24”.

To start with we allow interrupt “rda” which can trigger interrupt when knowledge is transmitted.

Secondly, let’s look inside ISR (interrupt service routine) which known as “serial_communication_interrupt”. Inside we firstly disable interrupt flag with a purpose to learn the acquired worth and trigger interrupt additional. After that we learn the string character by character till reaching ‘’. Whereas studying inside string we additionally write every char to the buffer with a purpose to get acquired string.

On the finish, we once more come inside whereas. Right here we evaluate our buffer string which is acquired string and cmp_ string with a purpose to see whether or not we get the string accurately. If they’re equal then I activate led, in any other case flip off.*

*In code I did reverse as a result of my board inverts the D port pin values. In your code change it to:

if(strcmp(buffer, cmp_) == zero) output_high(LED_RED);

else output_low(LED_RED);

Lastly, compile it and add to your MCU after which run code in Python. You need to see led activate.

Step 5: Conclusion

We’ve completed one activity efficiently. I hope that it is going to be helpful for you. When you have any query, please do not hesitate to ask ๐Ÿ™‚ Till subsequent undertaking.



Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.