quinta-feira, 7 de novembro de 2019

How to design a better blink led?

Simple! Check the code I wrote for the Blink post "Blink - PIC18F - MPLABX - PICkit3"
link:  https://projetosdoborba.blogspot.com/2019/11/blink-pic18f-mplabx-pickit3.html

What can go wrong with a simple blink LED routine and who does care?

Writing complex algorithms to solve hard problems all starts with good writing practices. 


Let's start with a block diagram of the blink algorithm. The flowchart will help you to clarify your thoughts and write better codes. 



From the flowchart, we can write the code skeleton with more informative and constructive comments.

//initialize the microcontroller (uC)

//initialize pins

//while forever 

//turn led on

//delay

//turn led off 

//delay


It is also necessary to understand what each register (TRISB, PORTB) means. Then, you will need to check the datasheet. When we are learning how to program, reading the datasheet seems to be overwhelming and hard, it is actually quite simple, you just have to learn where to find the information you need. 

Please, refer to the datasheet available on https://ww1.microchip.com/downloads/en/DeviceDoc/40001412G.pdf

I posted some screenshots from the datasheet at the end of this post. Please, take a look there.

Here is the code with more comments 


//initialize the microcontroller (uC)

#include <xc.h>

//delay function
void DELAY_ms(unsigned int ms_Count);

int main()
{

//initialize pins
//configure port B as a digital output. Page 132 datasheet. 
TRISB = 0; //  PortB as Output 


//while forever 
while(1)
  {

    //turn led on
    PORTB = 0xFF;  

    //delay
    DELAY_ms(100); // 100 ms Delay

    //turn led off 
    PORTB = 0x0;   

    //delay
    DELAY_ms(100); // 100 ms Delay
  }
}


void DELAY_ms(unsigned int ms_Count)

{

    unsigned int i,j;

    for(i=0;i<ms_Count;i++)

    {

        for(j=0;j<100;j++);

    }

}



A final thought before closing this post: There is a timing issue in the void DELAY_ms function because the for(j=0;j<100;j++) is not calibrated. I am mean, nothing can guarantee that for(j=0;j<100;j++)  is exactly 1 milliseconds. I will go over about delay function calibration later on and why we should not use delay functions like in this program. 



Please, see the screenshot I took from the datasheet. I highlighted the most important thing there. 








Again in the datasheet, we can check why when we write PORTB = 0xFF;  // LED ON the led turns on. The register PORTB is used to read or write values in and from the pin. Please, check page 127 of the datasheet.  







terça-feira, 5 de novembro de 2019

Blink - PIC18F - MPLABX - PICkit3



The blink led is a great start (or re-start in my case) with microcontrollers. In this project, I will describe how to set up the MPLBX and run a simple blink (hello word) C program.

The great advantage of starting (or re-starting) programming microcontrollers with a simple code, such as Blink, is that you can focus on installing and configuring the compiler and potentially many other bugs while installing or setting up your microcontroller environment.

I love programming and for me, life is kind of boring if I am not "thinkering" or working with embedded systems. I believe that 8-bit platforms, but if you can start with 32-bits that works too. The most important is to learn how to configure your microcontroller, how to read a datasheet, what are the limitations, and most importantly what is your main goal.

Today, and in the next series of posts, I will be using the PIC 18F45k22 and the development board PIC-EK. The PIC-EK is manufactured by LogiFind and I bought it on Ebay.

The PIC-EK is a versatile development board and supports an infinity of microcontrollers including families with 8, 10, 14, 18, 20, 28, and 40 pins. Moreover, the PIC-EK has multiple peripherals including LEDs, switches, 7 segment display, joystick, memory, buzzer, and others.


Development board  - PIC - EK 

I am using the PIC18F45k22 with a 4MHz crystal.



PIC18F45k22 with a 4MHz crystal.


To program the PIC, I am using the PICKit3. 



Once the code is written and compiled, you connect the  PICKit3 in the PIC-EK and download the binary file.





To program and write code for my embedded systems application, I could use the Mikro C pro or the MPLABx. The major advantage of MikroC is the number of libraries available. On the other side, the pro (full version) costs 250 dollars. A free version, the MPLABx requires more work, but it is free and it gives you full access to the writing memory. I will be using the MPLABx and I will teach you (or at least document) how to install and configure your enviroment for your first program.


Download MPLAB® X IDE v5.25 (Link to windows) and install the drivers.   

If you have had experience with creating a new project then you can go to the bit setting part. If not, I suggest you follow these steps:


1) Start a new project:



2) Choose the microcontroller family and your pic



3) Select the compiler. If this is your first time using MPLABx, you have to click the Download XC8 option. (I had already done this and just selected the XC8)


4) Choose the name and location of your project (I forgot to take a print screen, but this part is very straight forward)

Okay, your project has been created.

5) Now you have to click on the helper above the Sources folder and select New >> main.c



6) Okay, now we have a project and a main. Before you start programming, we have to tweak the configuration bits (Warning: if you have no programming experience, this part may seem quite confusing, but it is nothing like a seven-headed array. The microcontroller is a very flexible device, and you have to "adjust" your preferences so that everything works just fine (unfortunately or fortunately) you will need to read the microcontroller datasheet.


7) setting bit: Click on the tab.


8) In this project, I modified only 3 bits: oscillator (HSMP), watchdog (OFF) and, PORTB AD (OFF)



9) Click on Generate Source Code ..




10) Copy and paste the code into the program.











Step 11) Ready! Now we can start programming!


In this project, I just want to blink (flash) some LEDs that I will connect to port B. I used the code similar to this website:https://www.exploreembedded.com/wiki/PIC_I/O_Register_Configuration

#include <xc.h>

void DELAY_ms(unsigned int ms_Count);

int main()
{
  TRISB = 0; //RB0 as Output PIN
  while(1)
  {
    PORTB = 0xFF;  // LED ON
    DELAY_ms(100); // 100 ms Delay
    PORTB = 0x0;  // LED OFF
    DELAY_ms(100); // 100 ms Delay
  }
  return 0;
}
void DELAY_ms(unsigned int ms_Count)
{
    unsigned int i,j;
    for(i=0;i<ms_Count;i++)
    {
        for(j=0;j<100;j++);
    }
}

Then just compile (on the hammer), connect PICKit3 to PIC-EK and click on the icon with the green arrow pointing down (Make and program)



A window will pop up for you to select your programmer (PICKit3)


Some microcontrollers work with 3.3V and others with5V. The PIC18F45k22 is at 5V. This option, I just clicked OK. (I am not responsible if you burn your PIC because of the wrong voltage. Read everything carefully and always be cautious!)


Here is the result:







I hope you have been able to follow the step by step and get rid of the PIC microcontroller bits ai. Please post comments with questions, program suggestions, and improvements.



Next steps:


Put the code in github (public)

Create an .h file for configuration functions.

Explore pushbutton LED routines and timer and hardware interrupt routines.


Cheers!