Pic 18 IO Hello World \o/

Just initiating a magic journey throughout Microchip’s best seller pastille — Debut pic — Episode#01

J3
Jungletronics
5 min readNov 8, 2019

--

Hi, this is all about Microchip’s PIC18 Family. vid and ❤ git :)

Two chips are in focus: PIC18F4520 & PIC18F45K22. and…

Fig 1. PIC18F4520(picGenios) & PICF18F45K22(EasyPIC v7)

Hardware: Development Boards (your options are):

PicGenios: PIC18F4520 chip (tested:)
or EasyPIC™ v7: PIC18F45K22 chip; EasyPIC™ v8: PIC18F47K42, but for both not tested:/ here though. Tell me if it works! Comment below.

Software: Development Programmer/Debugger:

Microchip’s PICkit™ 2, Microchip’s PICkit™ 3, or the new MPLAB PICkit™ 4 and mikroProg Suite for PIC (this last native for easyPIC v7).

GitHub: pic18f

Welcome!

Here is the first code:

Fig 2. GitHub repo
Fig 3. Simple blinking — Hello World!

Code Analyses:

Let’s blink an LED on PORTD’s bit 0 and config Button on PORTB’s bit 0 as input (KEY) for future implementation?

In my board, 8 LEDs are connected to PORTD and 8 buttons are on PORTB of PIC18F4520 uC, running under 8 MHZ crystal (please set sw1 dip = 9, LEDs on PORTD).

Fig 4. Locations’s LEDs on picGenios Board

Let’s get it on!
PDL is a free-format English-like text that describes the flow of control and data in a program. PDL is not a programming language. It is a collection of some keywords that enable a programmer to describe the operation of a program in a stepwise and logical manner:
Paste this to the mikroC’s Text Editor:

Step-by-step:
This is the main method.

Let's initialize and treat, first, the code for PIC18F4520.

In the 4520’s chip there are 4 I/O Ports: Ports A, B, C, D, E;
Our buttons are in PORTB:

Fig 5. PORTB’s keys distribution (unimplemented here:)

So we need it digital, but on a Power-on Reset (POR), RB<4:0> are configured as analog inputs by default and read as ‘0’;

By programming the Configuration bit, ADCON1, we’ll configure PORTB<4:0> as digital.

Alternatively by programming the PORT B A/D Enable Configuration bit, CONFIG3H.PBADEN, RB<4:0>, will too be configured as digital inputs on POR (see: TABLE 4–4: INITIALIZATION CONDITIONS FOR ALL REGISTERS on PIC18F2420/2520/4420/4520 data sheet’s page 52),

Fig 6. ADCON1 configs bits (we use 0x0F → 1111)

That is the same as writing in binary format like this:

Fine!

Now, configure all PORTD as digital output (init all LED’s as off).

Our LEDs are on PORTD, right? Type this:

This will Configure all PORTD’s pins as Output (Remember O stands for Output is equal zero — O=0:) and will turn off all LED’s attached to it.

Now let’s deal with another chip: PIC18F45K22 (present at easyPIC v7).
Let’s make a Preprocessor Directive — C; these directives allow to include or discard part of the code of a program if a certain condition is met. Do like this right below the main method:

For PIC18F45K22 (easyPIC v7) all we need to do is configure all the PORTB & PORTD’s pins as digital by clearing ANSEL bytes.

At last, let’s use while to make an infinite loop:

Do you want to see how this compiler calls its registers? just put the mouse’s cursor on PORTD and right-click it. Now choose Find Declaration and there you have it! — use Edit > Find (ctrl+F) and search for PORTD.

For PORTD, you can use PORTD.RD0, PORTD.B0 or RD0_bit notations.

And that’s it!

In the next post, let’s initialize a PIC study, right?

Thank you very much to read this post.
Your host, J3, is signing off…
Bye!

END:)

Fig 7. Please see this video for this post:)

Related posts:

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

Credits & References:

PIC18F2420/2520/4420/4520 Data Sheet

PIC18(L)F2X/4XK22 Data Sheet

GitHub pic18f

Microgenios

--

--

J3
Jungletronics

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