Jump to content
 
  • entries
    31
  • comments
    46
  • views
    21,983

Contributors to this blog

Getting Started with Embedded software Pt 2


N9WXU

951 views

 Share

Time for part 2! 

Last time, I gave you the homework of downloading and installing MPLAB and finding a Curiosity Nano DM164144 .  

Once you have done your homework, it is time for STEP 3, get that first project running.

Normally my advice would be to breakout Mplab Code Configurator and get the initialization code up and running, but I did not assign that for homework!  So we will go old school and code straight to the metal.  Fortunately, our first task is to blink an LED.

Step 1: Find the pin with the LED.

A quick check of the schematic finds this section on page 3.

image.png

This section reveals that the LED is attached to PORT A bit 2.  

With the knowledge of the LED location, we can get to work at blinking the LED.

The first step is to configure the LED pin as an output.  This is done by clearing bits in the TRIS register.  I will cheat and simply clear ALL the bits in this register.

Next we go into a loop and repeatedly set and clear the the PORT A bit 2.  
 

#include <xc.h>

void main(void) {
    TRISA = 0;
    while(1)
    {
        PORTA = 0;
        PORTA = 0x04;
    }
    return;
}

Let us put this together with MPLAB and get it into the device.

First we will make a new project:

image.png

Second, we will create our first source file by selecting New File and then follow the Microchip Embedded -> XC8 Compiler -> main.c

image.png

give your file a name (I chose main.c)

image.png

And you are ready to enter the program above.

image.png

And this is what it looks like typed into MPLAB.

image.png

But does it work?

Plug in your shiny demo board and press this button:

image.png

And Voila!, the LED is lit... but wait, my code should turn the LED ON and OFF... Why is my LED simply on?

To answer that question I will break out my trusty logic analyzer.  That is my Saleae Logic Pro 16.  This device can quickly measure the voltage on the pins and draw a picture of what is happening.

image.png

One nice feature of this device is it can show both a simple digital view of the voltage and an analog view.  So here are the two views at the same time.  Note the LED is on for 3.02µs (microseconds for all of you 7'th graders).  That is 0.00000302 seconds.  The LED is off for nearly 2µs.  That means the LED is blinking at 201.3kHz. (201 thousand times per second).  That might explain why I can't see it.  We need to add a big delay to our program and slow it down so humans can see it.

One way would be to make a big loop and just do nothing for a few thousand instructions.  Let us make a function that can do that.

Here is the new program.

#include <xc.h>

void go_slow(void)
{
    for(int x=0;x<10000;x++)
    {
        NOP();
    }
}

void main(void) {
    TRISA = 0;
    while(1)
    {
        PORTA = 0;
        go_slow();
        PORTA = 0x04;
        go_slow();
    }
    return;
}

Note the new function go_slow().  This simply executes a NOP (No Operation) 10,000 times.  I called this function after turning the LED OFF and again after turning the LED ON.  The LED is now blinking at a nice rate.  If we attach the saleae to it, we can measure the new blink.

image.png

Now is is going at 2.797 times per second.  By adjusting the loop from 10,000 to some other value, we could make the blink anything we want.

To help you make fast progress, please notice the complete project Step_3.zip attached to this post.

Next time we will be exploring the button on this circuit board.  For your homework, see if you can make your LED blink useful patterns like morse code.

Good Luck

Step_3.zip

image.png

image.png

  • Like 2
 Share

5 Comments


Recommended Comments

  • Member

The defaults worked just fine!  Of course there may have been a watchdog timer occurring someplace.

Link to comment
  • Member

Here is the advanced course version in assembly language

bank0	udata
delayLow	res 1
delayHigh	res 1
    
RES_VECT  CODE    0x0000            ; processor reset vector
    GOTO    START                   ; go to beginning of program

MAIN_PROG CODE                      ; let linker place main program

 #define delayValue 10000
  
go_slow
   banksel  delayLow
   movlw    low delayValue
   movwf    delayLow
   movlw    high delayValue
   movwf    delayHigh
slow_loop
   decfsz   delayLow
   goto	    slow_loop
   decfsz   delayHigh
   goto	    slow_loop
   return
 
START

    BANKSEL TRISA
    CLRF    TRISA

loop
    BANKSEL PORTA
    BSF	    PORTA,2
    call    go_slow
    BANKSEL PORTA
    BCF	    PORTA,2
    call    go_slow
    GOTO    loop

    END

It works exactly the same way as the C version.

Link to comment
  • Member

How do you compile that? That #define does not look like "normal" ASM if there is such a thing ... perhaps I should say how do I find out more about writing ASM code for this device, assembling it into machine code and running it. Can I set breakpoints etc.? 

Sorry I do not usually work with ASM. 🙂

Link to comment
  • Member

Standby for a introduction to embedded... the ASM version.

But suffice to say, the PIC assembler (MPASM) does understand #define for creating macro's just like in C.

Setting breakpoints works the same way as in C but it is a bit more clear because you can set the breakpoint on exactly the instruction you want to stop at.

The biggest hassle with ASM programming is the lack of any kind of support anything not supported by the ISA.

Link to comment
Guest
Add a comment...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...