Get Adobe Flash player
FacebookTwitterGoogle+
English Arabic French German Italian Portuguese Russian Spanish

Did you know?

Mobile phones will be discarded at a rate of 130 million since 2005, resulting in 65,000 tons of waste
 

Help us stay online:

small donate

 

blink led_view_electronics_base_comIt is common opinion that “blink LED” project is the most difficult and the most useful example in microcontroller programming. That is understandable because you must spend a lot of time to learn installation process and usage of IDE (Integrated Development Environment), to learn architecture of specific microcontroller and so on. We will explain in this article how to make new project in MPLAB IDE for dsPIC30f4013 and how to blink LED as simplest project with MCU.

 

 

 

As hardware we use our custom board that is described in article Microchip dsPIC30f4013 microcontroller and Telit GM862 GSM modem – hardware implementation.  Next video shows how to make new project in MPLAB and how to add .c and .h files in project.

Our first project shows how to blink LED without timer just using dummy loops. It is possible to make precise timing in this way, but it is better to use embedded MCU timer. Before start, read these articles about input/output usage of dsPIC MCU:

DSPIC INPUT/OUTPUT PINS-usage of registers

INPUT/OUTPUT PINS

Here is simplest program and you can download whole project HERE .

#include<p30fxxxx.h>
//_FOSC(CSW_FSCM_OFF & XT_PLL4); //instruction clock is same as external oscillator
//CSW_FSCM_OFF forbid changing from external to internal oscilator during work
_FOSC(CSW_FSCM_OFF & FRC_PLL4); //instruction clock is same as internal oscillator
_FWDT(WDT_OFF); //watch dog timer off
unsigned int number1,number2; //variables for getting loop delay
void main(void)
{
ADPCFGbits.PCFG9=1;// pin B9 is digital, not analog (only for port B with ADC inputs)
//ADPCFGbits.PCFG5=1;// pin B5 is digital, not analog
TRISBbits.TRISB9=0;//pin B9 is configured as output
TRISBbits.TRISB5=1;//pin B5 is configured as input
TRISDbits.TRISD0=1;//in D0 is configured as input
LATBbits.LATB9 = 0;
while(1)
{
for(number1=0;number1<500;number1++) //random delay
for(number2=0;number2<2000;number2++);
LATBbits.LATB9=~LATBbits.LATB9; //NOT operator...if LED was ON, then next state will be OFF
//LATBbits.LATB5=~LATBbits.LATB5;
//LATBbits.LATB5=~LATBbits.LATB5;
/*The NOT operator is a little different than the rest because it operates on a single number rather than two numbers.
The NOT operator will result in a 1 if the operating bit is a 0, or a 0 if the operating bit is a 1. In other words,
the result is the opposite of the operator. In most programming languages the symbol for the NOT operator is ‘~’, 
this is also known as the tilde character and is typically found to the left of the '1.' Here’s an example of how 
the NOT operator would work on a larger number:
NOT 
X 01001011
Result 10110100 */
for(number1=0;number1<500;number1++) //random delay
for(number1=0;number1<2000;number1++);
}//while
}//main

Read carefully comments after every line of code. Remember to put your own comments in code since it will be huge help mostly to you.

Second project also blink LED, but in this case embedded timer is used. All timer configurations are set in separate files, timerT1.c and timerT1.h. Try to split your project at more logical parts. Read carefully comments in code. For more details read datasheet of dsPIC30F4013 or put questions at our forum. Download complete project.

BlinkLEDTimer.c:

#include <p30fxxxx.h>
#include <stdlib.h>
#include <string.h>
#include "timerT1.h"
#include <math.h>
_FOSC(CSW_FSCM_OFF & FRC_PLL4);//instruction clock use internal oscillator
_FWDT(WDT_OFF);
unsigned int counter = 0;
//-----------------------------------------------------------------
void __attribute__ ((__interrupt__)) _T1Interrupt(void) // TIMER T1 TIMER T1 TIMER T1
//-----------------------------------------------------------------
// interrupt routune of Timer T1
{
TMR1 =0; //Stop Timer
counter++;//COUNTER FOR FUNCTION Delay_ms()
IFS0bits.T1IF = 0; // clear interrupt flag
}
//-----------------------------------------------------------------
void Delay_ms(unsigned int wanteddelay) //Delay in ms
//-----------------------------------------------------------------
// delay function in ms
{
counter = 0;
while(counter < wanteddelay);
}
void main(void)
{ 
Init_T1(); //timer T1 initialization
ADPCFGbits.PCFG9=1; //pin B9 is digital, not analog (only for port B with ADC inputs)
TRISBbits.TRISB9=0; //pin B9 is configured as output
while (1)
{
LATBbits.LATB9=~LATBbits.LATB9; //NOT operator...if LED was ON, then next state will be OFF
Delay_ms(300); //delay 1 second 
}//while
}//main

timerT1.h:

#include <p30fxxxx.h>
void Init_T1(void);

timerT1.c:

#include <p30fxxxx.h>
#include "timerT1.h"
#define TMR1_period 7372 /* Fosc = 7.3728 MHz,*/
//-------------------------------------
void Init_T1(void)
{
TMR1 = 0;
PR1 = TMR1_period;
T1CONbits.TCS = 0; // 0 = Internal clock (FOSC/4)
//IPC0bits.T1IP = 3; // T1 interrupt priority (0-7) 
//SRbits.IPL = 3; // CPU interrupt priority is 3(11) 
IFS0bits.T1IF = 0; // clear interrupt flag
IEC0bits.T1IE = 1; // enable interrupt
T1CONbits.TON = 1; // T1 on 
}
//--------------------------------------