PIC32 Digital Ports Project 2

Flashing LED's

This chapter is all about using the digital ports in a practical way and how to so some basic input output to the real world. Using the BV513 there is a built in LED which is used for indicating when the SD Card is accessing data. This is connected to port "c15", that is port name 'c', bit 15. To see physically where this is connected see the port diagrams in the hardware section.

Flashing C13

// Flash on board led
constant LED$ "c15"

function p_ff
dim j
    setport LED$ as output
    for j = 1 to 10
        port LED$ 1
        wait 100
        port LED$ 0
        wait 100
    next
    setport LED$ as input
endf

The above uses a constant to specify a port name and bit to use, C15 is connected to the on board LED and so no bread boarding is required yet. The first line sets the port to be an output and the statements within the loop alternately turn the LED on and off. The delay (wait 100) is necessary in order to see the flash, it would be two quick otherwise. The last line simply returns "c15" to an input again so that the SD Card software can use it.

External LED's

The following is a simple project to flash 6 LED's. These can be arranged as a dice and used for randomly selecting a number between 1 and 6.

breadboard of dicecircuit diagram of dice  

The circuit diagram and practical layout are shown above. Port 'd' 0 to 5 is used to power the LED's and port 'd6' is used as an input. The 'switch' is made up of a resistor connected to port 'd6' and +V which hold the port at logic 1. The switch which is just a bit of wire shorts the pot to ground thus making it zero. R1 can be any value between 1 and 100k

// Six Leds
constant LEDP$ "d"
constant SW$ "d6"

// sets ports for input or output
// as required
function p_init
dim j
    for j=0 to 5
        setport LEDP$+j as output
    next
    setport SW$ as input
endf

The set up uses two constants, this makes it a bit easier to change the port should that be required in the future. The constant only specifies the port name, the actual initialisation routine specifies the bits to be used. This is done by 'LEDP$"+j. Because of automatic type conversion there is no need to specify the 'j' as a sting. If j for example is 2 then 'LEDP$+j' will be the equivalent of "d2". The switch is an input and this is specified in the constant.

// flashes LEDS in turn until SW$ port
// goes low
function p_spin
dim j=0
    while 1=1
    port LEDP$+j 0 // on
    exitif port(SW$)=0
    wait 20
    port LEDP$+j 1 // off
    j=j+1 if j>5 then
        j=0
    endif
    wend
endf

The above will flash the LED's in turn, 'j' is the LED number which is incremented and reset to 0 when 5 is reached. The function is exited when the switch port goes low. Although this will generate a number from 1 to 6, the position of the LED's depicts the number, it would be better to illuminate a number of LEDS representing the number.

// light 1 to 6 leds function
p_light(n)
dim j
    for j = 0 to 5
        port LEDP$+j 1 // off
    next
    // turn on number of leds
    while n > 0
        j=n-1
        port LEDP$+j 0 // on
        n=n-1
    wend
endf

Here the above function will illuminate the specified number of LED's give by 'n' supplies to the function. So for example:

p_light(3)

Will illuminate the first three LED's, supplying a value of 0 will turn off all of the LED's. To put this function to use p_spin can once again be used but below is another version that uses random numbers.

function p_spin
dim j=7
    while 1=1
        exitif port(SW$)=0
        wait 20
        p_light(rand(1,6))
        j=j+1
        if j>5 then
            j=0
        endif
    wend
endf

This is the same as the first spin except that it will illuminate a number of LED's representing the number chosen. The 'rand()' function is used to generate a random number between 1 and 6, this is passed to the p_light function to illuminate that number of LED's