Pic 18 IO Push & Blink an LED

Let’s Unveil IO’s PIC18 Capabilities — Debut pic — Episode#02

J3
Jungletronics
3 min readNov 15, 2019

--

Hi, in this second episode of unveiling PIC18 Family’s IO capabilities, here is the code ( ❤ git):

Fig 1. Push and turn it off (but…hold it and it blinks…)

To advance in time let’s take the opportunity to reuse the previous code :)

Open mikroC PRO for PIC, create a new project, give it a name, copy & paste the first episode code and let’s focus on today’s business: blink an LED (PORTD) as soon as we push a momentary button (PORTB).

It’s easy! In the while loop there is this combination:

There are:

Assignment Operators: Assignment operators are used to assigning value to a variable. The left side operand of the assignment operator is a variable and the right side operand of the assignment operator is a value.

Bitwise Operators: The Bitwise operators are used to perform bit-level operations on the operands.

=~: This operator is a combination of = and ~ operators. This operator first negates the current value of the variable on right and assigns to the value on left.

The result: every time the loop is executed, the LED’s logic value is inverted.

Please note the register LAT. This is a patch to Read Modify Write Problem.

In a hush: this guarantees that the PORTD’s logic signal will be rescued by the buffer, not by the pin’s state. Always use the LAT register if you intend to make a quick operation on PIC’s pin. (please read this article for more information).

Another observation lies in the fact that if you maintain your finger pressing the button the LED will blink forever. That does not what we want, right?

In the next lesson let’s fix it and deal with the debounce issue.

And that’s it

Thanks a lot! See you in the next episode.

Bye!

Related posts:

1º Episode — IO: Debut pic — Pic 18 Hello World \o/ — Just initiating a magic journey throughout Microchip’s best seller pastille

2º Episode — IO: Debut pic — Pic 18 Push & Blink an LED — Let’s Unveil IO’s PIC18 Capabilities

3º Episode — IO: Debut pic — PIC 18 Push & Debounce! — Treating buttons’ Debounce

4º Episode — IO: Debut pic — PIC 18 Lighting LEDs In a Row! — Nice Special Effect

5º Episode — IO: Debut pic — PIC 18 Chasing LEDs — The net result is that LEDs seem to be chasing each other

6º Episode — IO: Debut pic — PIC 18 Double Chasing — The LEDs chase each other in both directions

7º Episode — IO: Debut pic — PIC 18 Ambulance Light — Algorithm for ambulance-flashing-light

8º Episode — IO: Debut pic — PIC 18 Random Flashing — Generating a random number

9º Episode — IO: Debut pic — PIC 18 internal weak pull-ups — How to programming WPUR

10º Episode — IO: Debut pic — Button Library — How to use mikroC PRO for PIC library

11º Episode — IO: Debut pic — Up Down Counter — How to use mikroC PRO for PIC library

Fig 2. Blinking an LED’s episode

References & Credits

GitHub: pic18f

Microgenios

--

--

J3
Jungletronics

😎 Gilberto Oliveira Jr | 🖥️ Computer Engineer | 🐍 Python | 🧩 C | 💎 Rails | 🤖 AI & IoT | ✍️