Sei sulla pagina 1di 19

Programming with dsPIC Microcontroller

dsPIC
Microcontroller

pg. 1
Programming with dsPIC Microcontroller

INTRODUCTION
EMBEDDED SYSTEMS
dsPIC30F4011

The dsPIC 30F4011 PIC microcontroller is one of the most popular general purpose
microcontrollers. It is of 16-bit which means the most available operations are limited to 16-
bits.

Ports:
There are 5 ports. Port B, Port C, Port D, Port E, Port F.

Port B : Port B is of 9 – bit wide.


Port C : Port C is of 3 – bit wide.
Port D : Port D is of 4 – bit wide.
Port E : Port E is of 7 – bit wide.
Port F : Port F is of 7 – bit wide.

pg. 2
Programming with dsPIC Microcontroller

pg. 3
Programming with dsPIC Microcontroller

microC Pro For dsPic Microcontroller IDE:

microC Pro for dsPic Microcontroller IDE is a integrated toolset for the development of
embedded application on microchip IC and dsPIC microcontroller.

Install microC Pro For dsPIC Microcontroller IDE by following the instructions sets provided
in your software.

Lab.1 - LED Blinking Using dsPIC 30f4011 Microcontroller using microC pro for
dsPIC Microcontroller.

Open the micro C Pro for dsPIC Microcontroller and you will find a window opened as
shown in the above image.

Navigate Project > New Project as shown in the above image.

pg. 4
Programming with dsPIC Microcontroller

Once you navigate Project > New Project, you will find a New Project Wizard opened
like as shown in the above image.
Just click Next as shown in the above image.

Once you click Next, you will find a new screen opened as shown in the above image. You
need to follow some steps as shown in the image above.

1) Change the Device Name as P30F4011


2) Change the Device Clock as 20 MHz. and click Next as shown in the image above.

pg. 5
Programming with dsPIC Microcontroller

Once you click Next, another window will be opened which will allow you to Select the files
you want to add to project.
Here, without making any changes, just click Next as shown in the above image.

Once you click Next, another window will be opened which will allow you to Select initial
state for library manager.
Here, without making any changes, just click Next as shown in the above image.

pg. 6
Programming with dsPIC Microcontroller

Once you click Next, you will find a window stating You have successfully created a new
project. Click “Finish” to close a wizard.
Just click on Finish without making any changes.

Once you click on Finish, you will find an editor opened as shown in the above image.

Once you find the editor, just paste the code of LED Blinking as given below and then save
and Build it by clicking on Build icon as shown in the image below.

pg. 7
Programming with dsPIC Microcontroller

Once you build the code, you will find “Building Finished Successfully” in the console as
shown in the image above. If you have some errors in your code, then your building will be
unsuccessful and it will show you errors in red fonts inside the console and you need to check
what is the error in your code.

These steps will be followed for each new project which you need to create.

Code :
void main() {

ADPCFG = 0xFFFF; // Configure AN pins as digital I/O


TRISB = 0; // Initialize PORTB as output
TRISC = 0; // Initialize PORTC as output
TRISD = 0; // Initialize PORTD as output
TRISF = 0; // Initialize PORTF as output

LATB = 0; // Set PORTB to zero


LATC = 0; // Set PORTC to zero
LATD = 0; // Set PORTD to zero
LATF = 0; // Set PORTF to zero

pg. 8
Programming with dsPIC Microcontroller

while(1) {
LATB = ~LATB; // Invert PORTB value
LATC = ~LATC; // Invert PORTC value
LATD = ~LATD; // Invert PORTD value
LATF = ~LATF; // Invert PORTF value
Delay_ms(1000);
}
}

pg. 9
Programming with dsPIC Microcontroller

Lab 2 - Controlling LED With Switch Using dsPIC 30f4011


Microcontroller using microC pro for dsPIC Microcontroller.

CODE:
unsigned int oldstate;

void main() {
ADPCFG = 0xFFFF; // initialize AN pins as digital
TRISD = 0xFFFF; // initialize portd as input
TRISB = 0x0000; // initialize portb as output

do {
if (Button(&PORTD, 0, 1, 1)) // detect logical one state
oldstate = 1;
if (oldstate && Button(&PORTD, 0, 1, 0)) { // detect logical one to logical zero transition
LATB = ~LATB; // toggle portb
oldstate = 0;
}
} while(1);
}

pg. 10
Programming with dsPIC Microcontroller

Lab 3 - To display a message on LCD using pic controller

Code:
// LCD module connections
sbit LCD_RS at LATD0_bit;
sbit LCD_EN at LATD1_bit;
sbit LCD_D4 at LATB0_bit;
sbit LCD_D5 at LATB1_bit;
sbit LCD_D6 at LATB2_bit;
sbit LCD_D7 at LATB3_bit;

sbit LCD_RS_Direction at TRISD0_bit;


sbit LCD_EN_Direction at TRISD1_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
// End LCD module connections

char txt1[] = "hello";


char i; // Loop variable
void main()
{
Lcd_Init(); // Initialize LCD

Lcd_Cmd(_LCD_CLEAR); // Clear display


Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
Lcd_Out(1,6,txt1); // Write text in first row

pg. 11
Programming with dsPIC Microcontroller

Lab 4 : Interfacing Analog Sensor(Temperature Sensor) to dsPIC 30f4011


// LCD module connections
sbit LCD_RS at LATD0_bit;
sbit LCD_EN at LATD1_bit;
sbit LCD_D4 at LATF0_bit;
sbit LCD_D5 at LATF1_bit;
sbit LCD_D6 at LATF2_bit;
sbit LCD_D7 at LATF3_bit;

sbit LCD_RS_Direction at TRISD0_bit;


sbit LCD_EN_Direction at TRISD1_bit;
sbit LCD_D4_Direction at TRISF0_bit;
sbit LCD_D5_Direction at TRISF1_bit;
sbit LCD_D6_Direction at TRISF2_bit;
sbit LCD_D7_Direction at TRISF3_bit;
// End LCD module connections

unsigned adcRes;
char txt[6];
void main() {

Lcd_Init(); // Initialize LCD


Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off

TRISB.F1 = 1; // set pin as input - needed for ADC to work


ADC1_Init();
while (1) {
adcRes = ADC1_Get_Sample(1);
adcRes = (adcRes * 0.4884);
WordToStr(adcRes, txt);
Lcd_Out(1,1,”TEMP:”); // Write text in first row

Lcd_Out(1,7,txt); // Write text in first row


Delay_ms(1000);
}
}

pg. 12
Programming with dsPIC Microcontroller

Lab 5 – Serial Communication

Code:
char uart_rd;

void main() {

UART1_Init(9600); // Initialize UART module at 9600 bps


Delay_ms(100); // Wait for UART module to stabilize

UART1_Write_Text("Start");
UART1_Write(10);
UART1_Write(13);

while (1) { // Endless loop


if (UART1_Data_Ready()) { // If data is received,
uart_rd = UART1_Read(); // read the received data,
UART1_Write(uart_rd); // and send data via UART
}
}
}

pg. 13
Programming with dsPIC Microcontroller

Lab 6 – Keypad (4*4) Interfacing

Code:

unsigned short kp, oldstate = 0;


char txt[6];

// Keypad module connections


unsigned keypadPort at PORTB;
unsigned keypadPort_Direction at TRISB;
// End Keypad module connections

void main() {
ADPCFG = 0xFFFF;
UART1_Init(9600);
Delay_ms(100);
Keypad_Init(); // Initialize Keypad

do {
kp = 0; // Reset key code variable

// Wait for key to be pressed and released


do
// kp = Keypad_Key_Press(); // Store key code in kp variable
kp = Keypad_Key_Click(); // Store key code in kp variable
while (!kp);
// Prepare value for output, transform key to it's ASCII value
switch (kp) {
//case 10: kp = 42; break; // '*' // Uncomment this block for keypad4x3
//case 11: kp = 48; break; // '0'
//case 12: kp = 35; break; // '#'
//default: kp += 48;

case 1: kp = 49; break; // 1 // Uncomment this block for keypad4x4


case 2: kp = 50; break; // 2
case 3: kp = 51; break; // 3
case 4: kp = 65; break; // A
case 5: kp = 52; break; // 4
case 6: kp = 53; break; // 5
case 7: kp = 54; break; // 6
case 8: kp = 66; break; // B
case 9: kp = 55; break; // 7
case 10: kp = 56; break; // 8
case 11: kp = 57; break; // 9
case 12: kp = 67; break; // C
case 13: kp = 42; break; // *
case 14: kp = 48; break; // 0
case 15: kp = 35; break; // #
case 16: kp = 68; break; // D

pg. 14
Programming with dsPIC Microcontroller

}
UART1_Write(kp); // Send value of pressed button to UART
} while (1);
}

pg. 15
Programming with dsPIC Microcontroller

Lab 7 – Interfacing GSM & Sending A Messaage

Code:
void main() {

UART1_Init(9600); // Initialize UART module at 9600 bps


Delay_ms(1000); // Wait for UART module to stabilize

UART1_Write_Text("AT");
UART1_Write(10);
UART1_Write(13);
Delay_ms(1000);

UART1_Write_Text("AT+IPR=9600;");
UART1_Write(10);
UART1_Write(13);
Delay_ms(5000);

UART1_Write_Text("AT");
UART1_Write(10);
UART1_Write(13);
Delay_ms(1000);

UART1_Write_Text("AT+CMGF=1");
UART1_Write(10);
UART1_Write(13);
Delay_ms(1000);

UART1_Write_Text("AT+CMGS=\"9902337012\"");
UART1_Write(10);
UART1_Write(13);
Delay_ms(1000);

UART1_Write_Text("Hi");
UART1_Write(0x1A);
UART1_Write(10);
UART1_Write(13);
Delay_ms(2000);

pg. 16
Programming with dsPIC Microcontroller

pg. 17
Programming with dsPIC Microcontroller

Lab 8 – Interfacing EEPROM with dsPIC (using I2C):


void main(){
ADPCFG = 0xFFFF; // initialize AN pins as digital

LATB = 0;
TRISB = 0; // Configure PORTB as output

I2C1_Init(100000); // initialize I2C communication


I2C1_Start(); // issue I2C start signal
I2C1_Write(0xA2); // send byte via I2C (device address + W)
I2C1_Write(2); // send byte (address of EEPROM location)
I2C1_Write(0xF0); // send data (data to be written)
I2C1_Stop(); // issue I2C stop signal

Delay_ms(100);

I2C1_Start(); // issue I2C start signal


I2C1_Write(0xA2); // send byte via I2C (device address + W)
I2C1_Write(2); // send byte (data address)
I2C1_Restart(); // issue I2C signal repeated start
I2C1_Write(0xA3); // send byte (device address + R)
LATB = I2C1_Read(1u); // Read the data (NO acknowledge)
I2C1_Stop(); // issue I2C stop signal
}

pg. 18
Programming with dsPIC Microcontroller

pg. 19

Potrebbero piacerti anche