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:
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).
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:
BEGIN
Pre-compilation Directives (PORTB & PORTD as digital);
Configure PORTB register bit 0 as digital input;
Configure all PORTD as digital output (init all LEDs off)
DO FOREVER
Turn PORTD reg. bit 0 on
Wait .3s
Turn PORTD reg. bit 0 off
Wait .3s
ENDDO
END
Step-by-step:
This is the main method.
// BEGIN
void main()
{ ... }
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:
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),
ADCON1 |= 0X0F;
That is the same as writing in binary format like this:
ADCON1 = OB00001111;
Fine!
Now, configure all PORTD as digital output (init all LED’s as off).
Our LEDs are on PORTD, right? Type this:
TRISD = 0;
PORTD = 0;
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:
#ifdef P18F45K22
ANSELB = 0;
ANSELD = 0;
#else
ADCON1 |= 0X0F; // Config all ADC’s pins as digital
#endif
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 FOREVER
while(1)
{
PORTD.RD0 = 1;
Delay_ms(300);
PORTD.RD0 = 0;
Delay_ms(300);
}// ENDDO
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:)
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: