BLINKING USING DSPIC30F2010 USING MIKRO C

Photo of author

By Jackson Taylor

Home » Electronics Topics » dsPIC Microcontroller » BLINKING USING DSPIC30F2010 USING MIKRO C

As an Amazon Associate we earn from qualifying purchases.

Blinking an LED is the first program to try on any new microcontroller. It confirms that your compiler, programmer and hardware all work, and it teaches you how to set a pin as an output and how to create a delay. This article shows several ways to blink an LED on the dsPIC30F2010 using Mikro C (mikroC PRO for dsPIC) and the matching mikroBasic compiler.

All examples use PORTE. The C examples assume a 16 MHz crystal, as noted in the code comments. If you want to know more about the chip before you start, read about the dsPIC30F2010 features.

What you need

Connect the LED anode through the resistor to the PORTE pin you are using, and the cathode to ground. With this wiring, a high level on the pin turns the LED on.

Hardware Hack

Stop Buying New Batteries for Your Tech

Electronics are expensive, but their power sources don't have to be. Discover the simple engineering trick to recondition dead batteries and bring your gadgets back to 100% capacity.

Watch the Free Video

How LED blinking works on a dsPIC

Two registers matter for this program:

  • TRISE sets the direction of each PORTE pin. A bit set to 0 makes the pin an output, and 1 makes it an input.
  • LATE and PORTE carry the output level. Writing to LATE is generally the safer choice for outputs, because PORTE reads the actual pin level. The C examples below use PORTE, and the Basic examples use LATE.

Pins on PORTE are shared with other functions on this device, so check the datasheet if a pin does not behave as expected.

The blink itself is simple: set the pin high, wait, set it low, wait, and repeat forever. The wait can be a software delay or a hardware timer.

C LANGUAGE

LED BLINKING AT PIN E0

This is the simplest version. It sets RE0 as an output and uses Delay_ms() to hold each state for one second, so the LED completes one full blink every two seconds.

//i have used crystal of 16 MHz
void main()
{
    TRISE.F0=0;
    while (1)
    {
        PORTE.F0=1;
        Delay_ms(1000);
        PORTE.F0=0;
        Delay_ms(1000);
    }
}

Note that Delay_ms() is only accurate if the clock frequency in your Mikro C project settings matches the real oscillator. If the LED blinks at the wrong speed, check that setting first.

See also
Smart Digital Energy Meter using power line communication modem

LED blinking at pin E0

LED BLINKING USING TIMER REGISTERS

A software delay keeps the CPU busy doing nothing. Using Timer1 is a better habit, because the hardware counts time for you and the CPU is free to do other work between events. This version polls the Timer1 interrupt flag instead of calling a delay function.

//i have used crystal of 16 MHz
void main()
{
    PORTE = 0x0000;
    TRISE.F0 = 0;
    T1CON = 0; //Turn off Timer1 and clear settings
    TMR1 = 0; //Start Timer1 at zero
    PR1 = 10000; //Set period register value
    T1CON = 0x8030; //Turn on Timer1 with 1:256prescaler
    //T1CON = 0x8020; //Turn on Timer1 with 1:64prescaler
    // T1CON = 0x8000   //Turn on Timer1 with 1:1prescaler --ABOVE 500KHz
    //T1CON = 0x8010; //Turn on Timer1 with 1:8prescaler
    while (1)
    {
        while(!IFS0bits.T1IF); //Wait for timer period
        IFS0bits.T1IF = 0; //Clear timer flag for next period
        PORTE.F0 ^= 1; //Toggle LED
    }
}

LED blinking using timer registers

How the timer code works

  1. T1CON = 0 stops Timer1 and clears any old settings.
  2. TMR1 = 0 resets the counter.
  3. PR1 = 10000 sets the period. When the counter reaches this value, the T1IF flag is set and the counter restarts.
  4. T1CON = 0x8030 turns the timer on (bit 15) and selects the 1:256 prescaler (bits 5:4).
  5. The loop waits for T1IF, clears it, and toggles RE0 with an XOR.

The prescaler options in the comments map to the T1CON value like this: 0x8000 is 1:1, 0x8010 is 1:8, 0x8020 is 1:64 and 0x8030 is 1:256. A larger prescaler slows the timer down, which gives a longer blink period.

Estimating the blink rate

The timer counts at the instruction clock (Fcy), which is the oscillator frequency divided by 4. If the chip runs from the 16 MHz crystal with no PLL, Fcy is 4 MHz. With a 1:256 prescaler the timer ticks at 15,625 Hz, so 10000 counts take about 0.64 seconds. The LED toggles each time, so a full on-off cycle takes about 1.3 seconds.

Sustainable Tech

Keep Toxic E-Waste Out of Landfills

Don't throw away your failing laptop, phone, or car batteries. Learn how to recondition them at home in just a few hours. It's better for the planet—and your wallet.

Learn How It Works

If you use a PLL or a different oscillator mode, the numbers change. Recalculate with your own Fcy, or adjust PR1 until the speed looks right. PR1 is a 16-bit register, so the value cannot exceed 65535.

See also
Voltage Gain Enhancement for Step-Up Converter Constructed by KY and Buck-Boost Converters

MIKROBASIC LANGUAGE

The remaining examples use the Basic syntax of the compiler (the original post labelled this section Pascal, but the wend and dim keywords are Basic). They use LATE for the output level and blink at a 500 ms rate.

BLINKING PORTE

This program makes all of PORTE an output, then switches the whole port low and high every half second. Any LED on a PORTE pin will blink.

program BLINK
main:
    TRISE=0
    while TRUE
        LATE=0
        Delay_ms(500)
        LATE=0xff
        Delay_ms(500)
    wend
end.

BLINKING SINGLE PIN RE0 IN PORTE

To blink only one pin, address a single bit. Here RE0 is set as an output with TRISE.0=0, and only that bit is changed in LATE.

program BLINK
main:
    TRISE.0=0
    while TRUE
        LATE.0=0
        Delay_ms(500)
        LATE.0=1
        Delay_ms(500)
    wend
end.

BLINKING SINGLE PIN RE1 IN PORTE (PIN DECLARED METHOD)

You can also give a pin a meaningful name with sbit. This makes the code easier to read, because LEDRED says more than LATE.1. The example below uses RE1 (the original heading said RE0, but the code works on bit 1).

program BLINK
dim LEDRED as sbit at LATE1_bit
main:
    TRISE.1=0
    while TRUE
        LEDRED=0
        Delay_ms(500)
        LEDRED=1
        Delay_ms(500)
    wend
end.

Troubleshooting

  • The LED does not light: check the LED polarity, the resistor and that the pin is set as an output in TRISE.
  • The blink speed is wrong: make sure the oscillator frequency in the project settings matches your crystal and configuration bits.
  • The chip does not run: check the oscillator configuration, the supply and decoupling, and the MCLR connection.
  • Nothing changes with a different pin: change both the TRISE bit and the LATE or PORTE bit to the same pin number.

What to try next

Once blinking works, you can move on to input. A good next step is push button interfacing, where a switch controls the LED. After that, try a simple PWM example to fade the LED, or add UART communication to send messages to a PC.

See also
STM8S003K3 BLINKING IN ARDUINO

FAQ

Why use TRISE before blinking an LED?

TRISE controls whether a pin is an input or an output. Pins default to inputs after reset, so you must clear the matching TRISE bit before the LED can be driven.

Should I use PORTE or LATE to write to a pin?

LATE is the better choice for writing outputs, since it holds the value you want to drive. PORTE reflects the actual pin level, which can cause problems when you toggle bits in quick succession.

Is the timer method better than Delay_ms()?

For a simple blink either works. The timer method is better for larger programs, because the delay is handled by hardware and the CPU is not blocked for the whole period.

How do I change the blink speed in the timer example?

Change PR1 or the prescaler bits in T1CON. A larger PR1 or a larger prescaler gives a slower blink, and a smaller one gives a faster blink.

Revive Your Dead Electronics

Got a drawer full of dead gadgets or a failing car battery? Instead of paying a premium for replacements, use this simple trick to easily recondition them right from home.

See How It Works