You have no items in your shopping cart.

Subtotal: 0.00

Example Programming with DsPIC Processor

C Program to switch ON and OFF LED using dsPIC

The Interfacing LED with dsPIC30F program is very simple and straight forward, that uses a delay procedure loop based software delay. In C programs you cannot be sure of delay, because it depends on compiler how it optimizes the loops as soon as you make changes in the options the delay changes. The realization is carried out by using the Microchip C30 compiler for dsPIC30F microcontrollers.

 

 

 

 

 


dspic30f-microcontrollers

#include "p30f4011.h"

#include "stdio.h"

 

#define rs _LATD3

#define en _LATD2

#define d0 _LATF0

#define d1 _LATF1

#define d2 _LATF4

#define d3 _LATF5

 

void cmd_write();

void delay(int x);

void cmd_single(unsigned char cmds);

void data_single(unsigned char data);

void data_write(unsigned char *str,

unsigned char addr,unsigned int no);

 

int j;

 

int main()

{

led = 0;

tled = 0;

cmd_write();

cmd_single(0x80);

data_single('G');

}

 

 

void data_disp(unsigned char data_out)

{

d0 = data_out & 0x01;

d1 = (data_out & 0x02) >> 1;

d2 = (data_out & 0x04) >> 2;

d3 = (data_out & 0x08) >> 3;

}

 

void cmd_single(unsigned char cmds)

{

unsigned char msb,lsb;

 

msb = (cmds & 0xF0) >> 4;

lsb = cmds & 0x0F;

 

delay(20);

rs = 0;

data_disp(msb);

en = 1;

delay(5);

en = 0;

 

delay(20);

rs = 0;

data_disp(lsb);

en = 1;

delay(5);

en = 0;

}

 

void data_single(unsigned char data)

{

unsigned char msb,lsb;

 

msb = (data & 0xF0) >> 4;

lsb = data & 0x0F;

 

delay(20);

rs = 1;

data_disp(msb);

en = 1;

delay(5);

en = 0;

 

delay(20);

rs = 1;

data_disp(lsb);

en = 1;

delay(5);

en = 0;

}

 

void cmd_write()

{

int i;

unsigned char a[7]={0x33,0x32,0x28,0x01,0x06,0x0c,0x80};

delay(200);

_TRISD3 = 0;

_TRISD2 = 0;

 

_TRISF0 = 0;

_TRISF1 = 0;

_TRISF4 = 0;

_TRISF5 = 0;

 

cmd_single(0x33);

cmd_single(0x32);

cmd_single(0x28);

cmd_single(0x0f);

cmd_single(0x01);

delay(200);

cmd_single(0x01);

delay(200);

cmd_single(0x80);

delay(20);

}

 

void delay(int x)

{

unsigned int k,l;

for(k = x;k>0;k--)

for(l = 0;l<x;l++);

}

 

void data_write(unsigned char *str,

unsigned char addr,unsigned int no)

{

int i;

for(i=0;i<no;i++)

 

{

cmd_single(addr);

data_single(*str);

++str;

++addr;

}

}
LCD module by using 4-bit interface

This example shows the connection of an alpha-numeric LCD module 2x16 characters to a dsPIC30F microcontroller by using a 4-bit interface. The realization is carried out by using the Microchip C30 compiler for dsPIC30F microcontrollers.




dspic30f-microcontrollers-port-line




#include 

#define rs _LATD3

#define en _LATD2

#define d7 _LATF0

#define d6 _LATF1

#define d5 _LATF4

#define d4 _LATF5

#define d3 _LATF2

#define d2 _LATF3

#define d1 _LATF6

#define d0 _LATD0

 

void cmd_write();

void delay(int x);

void cmd_single(unsigned char cmds);

void data_single(unsigned char data);

void data_write(unsigned char a[],

unsigned char addr,unsigned int no);

unsigned int flag;

int main()

{

cmd_write();

while(1)

data_single('G');

}

 

void data_disp(unsigned char data_out)

{

d0 = data_out & 0x01;

d1 = (data_out & 0x02) >> 1;

d2 = (data_out & 0x04) >> 2;

d3 = (data_out & 0x08) >> 3;

d4 = (data_out & 0x10) >> 4;

d5 = (data_out & 0x20) >> 5;

d6 = (data_out & 0x40) >> 6;

d7 = (data_out & 0x80) >> 7;

}

 

void cmd_single(unsigned char cmds)

{

delay(20);

 

rs = 0;

data_disp(cmds);

en = 1;

delay(5);

en = 0;

}

 

void data_single(unsigned char data)

{

delay(20);

rs = 1;

data_disp(data);

en = 1;

delay(5);

en = 0;

}

 

void cmd_write()

{

int i;

unsigned char a[5]={0x38,0x01,0x06,0x0c,0x80};

delay(200);

 

_TRISD0 = 0;

_TRISD2 = 0;

_TRISD3 = 0;

_TRISF0 = 0;

_TRISF1 = 0;

_TRISF2 = 0;

_TRISF3 = 0;

_TRISF4 = 0;

_TRISF5 = 0;

_TRISF6 = 0;

for(i=0;i<5;i++)

{

cmd_single(a[i]);

delay(200);

}

}

void delay(int x)

{

unsigned int k, l;

for(k = x; k > 0; k--)

for(l = 0; l < x; l++);

}

 

void data_write(unsigned char a[],

unsigned char addr,unsigned int no)

{

int i;

for(i=0;i<no;i++)

{

cmd_single(addr);

data_single(a[i]);

//++str;

++addr;

}

}
LCD module by using 8-bit interface

This example shows the connection of an alpha-numeric LCD module 2x16 characters to a dsPIC30F microcontroller by using a 8-bit interface. The realization is carried out by using the Microchip C30 compiler for dsPIC30F microcontrollers




dspic30f-microcontrollers-port-line


#include 

#include"g_lcd.h"

 

unsigned char ch1[1024] =

{0, 34, 8, 20, 8, 34,0,128,192,192,192,192,192,192,192,196, 200,

208,240,216,204,208,192,192,192,192,192,192,192,192,192,192,

192,192,192,248,196,194,130,130,114,124,132, 2, 31,33,194,6,

129, 65,145, 66, 54, 14, 4,206, 38, 34, 66,134, 4, 20,204,104, 0, 0,

0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 64,128,128, 0, 0, 0, 0,

0, 0, 0, 0, 0, 0, 0, 0, 0,252, 0, 0, 0,0, 0, 0, 0, 0,128,64, 32,0, 0,

0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 28, 22, 19,241, 16, 16, 31, 16, 16, 16,144,159, 159,144,144,144,159, 16, 16, 16, 31, 16, 16, 16, 16, 31, 31, 16, 16, 16, 16, 31, 16, 16,241, 19,246,

92,128,255, 66, 95,120, 16, 39, 88, 16,127, 39, 66,131, 0, 0, 16,

56, 61,105,198, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,128, 64, 32,

 

 

32, 16, 16, 32, 32, 64,129, 2, 4, 4, 8, 0,128, 64, 32, 32, 32, 16,

16, 17, 32, 32, 32, 64, 64,132,130, 1, 0,128, 64, 32, 16, 8, 8, 4, 8,

8, 16, 96,128,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,255, 0,

0, 0, 0, 0, 0, 3, 2, 130,130,130,130,131,128,128, 0, 0, 0, 0, 0,

0, 0, 0, 0, 0, 0, 0, 0, 0, 0,255, 0,255, 0, 1, 2, 3, 1, 7,248, 8,

4, 2,254, 6, 5, 2, 3, 6, 4, 4, 2, 2, 1, 0, 0, 0, 0, 0, 0, 0,128,

64, 48, 8, 6, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 6, 6, 25, 33, 64,

128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,192, 48, 9, 7, 3,2, 0, 0, 0, 0,

0, 0, 0, 0, 0, 0, 3, 12, 16, 96,128, 0, 0, 0, 0, 0, 0, 0, 0, 0,

0,0,255, 0, 0, 0, 0, 0, 0, 0, 0, 255, 2,243, 10, 7, 6,255, 0, 0,

0, 30, 18, 18, 30, 18, 18, 30, 0, 0, 0, 0, 0,255, 0,255, 0, 0, 0, 0,

0,128,127, 0, 0, 0,255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,128,

96, 24, 6, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,

0, 0, 0, 1, 2, 12, 16, 96, 128, 0, 0,192, 48, 12, 3, 0, 0, 0, 0, 0,

0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,128,192, 96, 32, 33, 38, 56,112,128,

0, 0, 0, 0, 0, 0, 0, 7, 8, 8, 8, 8, 8, 8, 8, 8, 63, 40, 47, 40, 40, 40,

63, 8, 8, 8, 8,136,136,136,136,136, 136,136,136,136,136, 72, 79,

72, 71, 64, 64, 76, 74, 73, 72, 40, 40, 24, 24, 25, 26, 28, 8, 8, 8, 8,

4, 4, 4, 4, 4, 4, 6, 5, 4, 4, 2, 2,130, 66, 66, 2, 2, 2, 2, 2, 2, 2,

2, 2, 6, 6, 2, 2, 2, 2, 2, 6, 4, 4, 4, 4, 4, 4, 4, 7, 5, 4, 4, 4, 4, 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4, 2, 2, 2, 3, 0, 0, 0, 0, 0,

0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,

0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 2, 3,

0, 0, 0, 0, 0, 0, 0, 0, 0 };

 

void A_pict(unsigned int dat);

void A_pict1(unsigned int dat);

 

unsigned long int page,row,cs,i,j;

 

int main()

{

cmd_write();

 

page_clear();

while(1)

{

for(page=0;page<8;page++) // page = 8.

{

row = page * 128;

cmd_single(0xb8+page);

cmd_single(0x40);

A_pict(row);

cmd_single(0xb8+page);

cmd_single(0x40);

A_pict1(row+64);

}

}

}

 

void A_pict(unsigned int col)

{

int i;

cs1 = 1;

cs2 = 0;

for(i=col;i<(col+64);i++)

data_single(ch1[i]);

}

 

 

void A_pict1(unsigned int col)

{

int i;

cs1 = 0;

cs2 = 1;

for(i=col;i<(col+64);i++)

data_single(ch1[i]);

}
Graphical alpha-numeric LCD module (GLCD)

The example shows the connection of a «dot-matrix» graphical alpha-numeric LCD module (GLCD) to a dsPIC30F > microcontroller. The example covers the initialization of the GLCD and writing some text. The realization is carried out by using the Microchip C30 compiler for dsPIC30F microcontrollers.




graphical-alpha-numeric-lcd-module


#include 

 

void delay(int x);

unsigned int Read_ADC();

void datasing_UART1(unsigned char datasingle);

void UART1_conv(unsigned long int UART_value);

void data_UART1(unsigned char *str,unsigned int no);

void Init_ADC(unsigned int Ch,unsigned char No);

 

unsigned int Volt=0;

 

int main()

{

Init_ADC(0x0000,0);

while(1)

{

Volt = Read_ADC() * (float)(0.09775171065494); // 5V = 100V.

data_UART1(" Volt Reading = ",16);

UART1_conv(Volt);

}

}

 

unsigned int Read_ADC()

{

unsigned int adc1;

 

ADCON1bits.SAMP =1;

delay(20);

ADCON1bits.SAMP =0;

while (!ADCON1bits.DONE);

adc1 = ADCBUF0;

ADCON1bits.DONE =0;

return(adc1);

}

void delay( int x )

{

unsigned int k, l;

for(k = x; k > 0; k--)

for(l = 0; l < x; l++);

}

void datasing_UART1(unsigned char datasingle)

{

while(!U1STAbits.TRMT);

U1TXREG = datasingle;

}

void UART1_conv(unsigned long int UART_value)

{

unsigned int conv,conv1,conv2,conv3,conv4;

conv = UART_value;

conv1 = conv / 100;

conv2 = conv % 100;

 

conv3 = conv2 / 10;

conv4 = conv2 % 10;

datasing_UART1(conv1+0x30);

datasing_UART1(conv3+0x30);

datasing_UART1(conv4+0x30);

delay(600);

}

 

void data_UART1( unsigned char *str,unsigned int no)

{

int i;

for(i=0;i<no;i++)

{

datasing_UART1( *str );

++str;

}

}

 

void Init_ADC(unsigned int Ch, unsigned char No)

{

ADCON1bits.FORM = 0;

// Data Output Format -Integer (DOUT = 0000 00dd dddd dddd)

ADCON1bits.SSRC = 7;

// Conversion Trigger Source Select bits-

ADCON1bits.ASAM = 1; // A/D Sample Auto-Start bit

 

ADCON1bits.SIMSAM = 0;

// Sequencial Sample Select bit (when CHPS = 01 or 1x)

// 0 = Samples CH0 sequencially (when CHPS = 0x)

ADCON2bits.SMPI = 1;

// Sample/Convert Sequences Per Interrupt Selection bits

// 0001 = Interrupts at the completion of conversion

//for each 2nd sample/convert sequence

ADCON2bits.CHPS = No;

// 0 = Samples CH0 sequencially (when CHPS = 0x)

ADCON3bits.SAMC = 0;

// Auto-Sample Time bits

// 00000 = 0 TAD (only allowed if performing sequential conversions

// using more than one S/H amplifier)

ADCON3bits.ADCS = 2;

// A/D Conversion Clock Select bits

// 000010 = 3TCY/2 • (ADCS + 1) = 1.5TCY

ADPCFG = 0x0000;

// A/D Port Configuration Register

// 0 = Analog input pin in Analog mode, port read input disabled,

//A/D samples pin voltage

ADCHS = Ch;

// A/D Input Select Register

ADCSSL = 0x0000;

// A/D Input Scan Select Register

// 0 = Skip ANx for input scan

 

ADCON1bits.ADON = 1;

// A/D converter module is operating ON

}
Operating an AD converter for Temp Sensor

The example shows sampling by an AD converter and sends it as a text via UART1. The realization is carried out by using the Microchip C30 compiler for dsPIC30F microcontrollers.




operating-an-ad-converter-for-temp-sensor


#include

 

unsigned long int freq_value=5;

 

int main()

{

PTCON = 0x800E; // 1110 - E.11 - 64,prescale.10 - up/down count.

PTPER = 31250; // 5Hz.

PWMCON1 = 0x0011;

PDC1 = 31250;

while(1)

{

freq_value = 5;

}

}