TRISx, PORTx, LATx dumb it down for me!

Are you new to MPLAB? Do you want to work with PIC microcontrollers? You feel setting up the project is too complicated? So let’s dumb it down for ourselves.

GPIO

First thing you set out to do when you get your hands on a new microcontroller is right the HelloWorld of our world, the blinky code. In the blinky project, an LED connected to microcontrollers GPIO pins. GPIO or General Purpose Input Output pins are used as an input or output pins. In the blinky project case, we use it as output.

Configuring GPIO pin as an output

Now if you have worked with Arduino it’s quite simple. You simply use the pinMode() command to set any pin to OUTPUT or INPUT. But as you start working with more advanced controllers and have a deeper access to the controller there are few other things that have to be configured to be able to use a pin as INPUT or OUTPUT.

Setting up GPIO pins for PIC microcontrollers

There are three registers that you need to familiarize yourself when working with PIC microcontrollers specifically, others may be the same or different.

  • TRISx
  • PORTx
  • LATx

TRISx

TRIS means TriState or three different settings. Now, this particular register corresponding to a GPIO pin is used to select whether the pin is being used as an input or an output. But you may ask what’s the third state as the name suggest? It’s a high impedance state. This is the input state where due to high impedance no current flows into the microcontroller. The other two states are 0 and 1 for the output pin.

This is how tristate can be visualized. B is the tristate switch (in our case a register) and C is the pin.

Fun fact

Tristate is listed as a trade mark thus microchip uses TRIS as shortform meaning the same thing.

https://trademarks.justia.com/876/18/tristate-87618512.html

PORTx

The PORT register corresponding to each pin is used to OUTPUT to the pin. Whatever you want on the pin, you set the pin as OUTPUT using TRISx register and then write the value to PORTx.

LATx

The LAT register corresponding to a GPIO pin is used to read the value on the pin. Remember to use a GPIO as INPUT corresponding TRISx register should be set accordingly.

Hope this helps. Happy coding!

Sending a file to a slack channel using API

For the past 3 years, I have been working with data-obsessed management. This management would want all the reporting to happen in one place with the most accessibility. Call it old school but some are really comfortable with excel files. As a data analyst, we always tend to move to a solution that would an additional (somewhat unnecessary) learning for the management. So I decided to send them these formant data report files over slack something that they had on their fingertips.

As per the API documentation of slack, you can send files easily using “files.upload” method. According to the documentation, the file can be sent using multipart/form-data or using POST var called content. For this article, we will be using multipart/form-data.

Setting up an app on slack

To create a new app on slack follow the following steps:

  • Goto slack create app page by clicking this link:
  • Click the “Create New App” button as shown in the above image.
  • Enter app name and select the slack workspace that you want to use this app for as in the image below:
  • Click the “Create App” button.
  • We will now add features for our app to access files feature. To do this click the “OAuth & Permission” button under features as shown in the image below:
  • Under scopes click the “Add an OAuth scope” button and search for “files:write:user” to add this scope as in the image below:
  • Click install to the workspace.
  • You will get OAuth access token which you will be for uploading app later on:

We are now done with setting up the slack app. It’s time to get our hands dirty with coding in PYTHON.

Coding API call in PYTHON

We will create a simple *.txt file and send it through PYTHON script as under:

import requests

# link to files.upload method
url = "https://slack.com/api/files.upload"

# this is where you add your query string. Please chage token value
querystring = {"token":"xxx-xxx-xxx-xxx"}

# this is where you define who do you want to send it to. Change channels to your target one
payload = {
"channels":"xxxxxxx"}

file_upload = {
    "file":("hello-world.txt", open("hello-world.txt", 'rb'), 'text/plain')
}
headers = {
    "Content-Type": "multipart/form-data",
    }

response = requests.post(url, data=payload,  params=querystring, files=file_upload)

You can change the file type to xls or csv however you like it by changing the text/plain parameter.