Learn To Write Code For 8051, Arduino, AVR, dsPIC, PIC, STM32 ARM Microcontroller, etc.
Coding Embedded Controller With C/C++.
Printed Circuit Board (PCB) Project For Electronics Hobbyists.
Microchip dsPIC30F1010 is a 16-bit micro-controller, targeting for Switch Mode Power Supply (SMPS) Digital Signal Controller (DSC). It comes with many features that I don't show them all here. It has DSP (Digital Signal Processor) engine. It's 10-bit ADC module is able to convert analog signal up to 2Msps. Central Processing Unit (CPU) executes up to 30MIPS.
dsPIC30F1010 reference image
It has DIP and SMD packages options. DIP package is friendly for breadboard prototyping. I choose a 28-pin SMD package.
Pin diagrams of dsPIC30F1010
XC16 compiler is a C compiler developed by Microchip Technology. It has a free version that allow the programmer to code without code size limitation. Another C compiler for this 16-bit device is MikroC for dsPIC. However MikroC has code size limitation of 2kB of program space. MikroC has many library functions including peripheral libraries and external device interface libraries. Here I select XC16 v1.32 and MPLABX IDE v1.51.
To get started I select pin RE5 of Port E to toggle at the rate of 500mS. The device will operate using its internal fast RC oscillator (FRC) with the nominal frequency of 15MHz at +5V DC supply voltage. Main C program shows below.
/*
* Blinking dsPIC30F1010 using XC16
* in MPLABX IDE
*/
#include <p30F1010.h>
#include "config.h"
/*Nominal internal fast oscillator frequency of 15MHz*/
#define FCY 15000000UL
#include <libpic30.h>
void main(void){
/*Additionaly select the maximum nominal frequency of 15MHz*/
OSCTUNbits.TUN=0x07;
/*Clear I/O of PORTE*/
PORTE=0x0000;
LATE=0x00000;
/*PORTE direction as output*/
TRISE=0x0000;
while(1){
/*Toggling RE5*/
LATEbits.LATE5^=1;
/*Wait for 0.5 second*/
__delay_ms(500);
}
}
It need configuration setting that store in another C header file "config.h" within project directory.
// DSPIC30F1010 Configuration Bit Settings
#include <p30Fxxxx.h>
// FBS
#pragma config BWRP = BWRP_OFF // Boot Segment Write Protect (Boot Segment may be written)
#pragma config BSS = NO_BOOT_CODE // Boot Segment Program Flash Code Protection (No Boot Segment)
// FGS
#pragma config GWRP = GWRP_OFF // General Code Segment Write Protect (General Segment may be written)
I bought an SMD version of dsPIC30F1010. It was solder on SMD to DIP adapter that will be pinned on breadboard. As we have seen on the figure above, dsPIC30F1010 is placed on breadboard. ICSP adapter of PICKIT 2 connects to this microcontroller for in system programming and powering via USB bus. The controller operates at +5V from USB bus. It does not require an external crystal oscillator, as its internal fast RC oscillator is used.
In previous example I showed a simple keypad programming example using C in Atmel Studio 7. It displays key value on a typical single seven segments display. We can add a character LCD as an output device.
In this programming example we use a 4x4 keypad matrix as a digital input, and a character LCD bases on HD44780 as an output device. A 20x4 character LCD is used to show information a key value.
Program starts up
Port C is 8-bit wide, connects to a 4x4 matrix keypad as shown in the picture above. Port D connects to a character LCD in 8-bit mode. Read/Write pin is ignored, and it's connects to ground as the LCD just accept the data/command from micro-controller.
Atmega32 operates at 16MHz supplies at +5V DC supply voltage.
C main program shows below. It requires LCD and Keypad driver files that I don't show them here.
/*
* m32LcdKeyPad.c
*
* Created: 5/18/2021 10:15:15 PM
* Author : aki-technical
*/
#include <avr/io.h>
#define F_CPU 16000000UL
#include <util/delay.h>
#include "keypad4x4.h"
#include "m32Lcd.h"
void writeTitle(void);
unsigned char getKey=0;
int main(void)
{
unsigned char pressCount=0,newLine=0,lineCount=1;
DDRD=0xFF;
PORTD=0x00;
keyInit();
lcdInit();
setXy(0,0);
writeString("Atmega32 LCD KeyPad");
setXy(0,1);
writeString("Interfacing Example");
setXy(0,2);
writeString("Programing in C ");
setXy(0,3);
writeString("in Atmel Studio 7");
_delay_ms(2500);
writeTitle();
while(1)
{
getKey=scan();
if (getKey!=0)
{
writeChararacter(getKey);
_delay_ms(250);
pressCount+=1;
getKey=0;
}
if (pressCount==20)
{
newLine=1;
lineCount++;
pressCount=0;
}
if (newLine)
{
newLine=0;
setXy(0,lineCount);
}
if (lineCount==4)
{
lineCount=1;
writeTitle();
}
}
}
void writeTitle(void){
/*Clear LCD*/
writeCommand(LCD_CLEAR);
setXy(0,0);
writeString("Enter Key Value: ");
writeCommand(CURSOR_ON);
setXy(0,1);
}
Click here to download this example in zip file. I additionally add its schematic diagram below.
Nokia 5110 LCD module is a popular graphical LCD module for electronics hobbyists. It has a built-in LCD controller, PCD8544 48x84 pixels matrix LCD controller/driver. There are some popular modules of this graphical LCD such as, SparkFun, and Adafruit.
A sample of prototyping
Serial Peripheral Interface (SPI) is communication interface between a microprocessor and this module. It is a high speed communication protocol. In this LCD module, there are only five wires relates to SPI, and other three wires for power and LED.
A popular one's from SparkFun
Hence there are 8 pins to connect to this module:
VCC - Positive supply voltage (from 2.7 V to 3.3 V)
GND - Ground connection
SCE - Chip Select (active low)
RST - Reset (active low)
D/C - Data/Command mode ( low for command and high for data)
DIN - SPI MOSI
SCLK - SPI serial clock
LED - LED backlight supplies at 3.3V maximum voltage
I will not list all its technical detail of programming here due to repetition with previous post. For programming detail, please see this post which shows the ATMega32 SPI interface to this LCD. This post show how to interface this LCD with PIC16F876A using MPLABX XC8 compiler. However it work only in software simulator without physical hardware testing.
In this post, I use my own prototyping board for PIC18F2550 to test this LCD module that I have not physically tested it before. I don't use internal SPI peripheral of PIC18F2550. The XC8 program will bit-bang the SPI via software. However using this method it will not create a high speed communication.
Program testing on physical prototyping
Actual schematic is shown below.
Schematic and program simulation
I use Proteus VSM 8 to draw its schematic, and also allow the simulation of program. Supply voltage for PIC18F2550 is +5V regulated DC voltage. Pin 20 and 8 of PIC18F2550 are VDD and GND for supply voltage. They are not shown in this schematic. In physical prototyping they must be properly wired to power bus.
Nokia 5110 LCD module interface with PIC18F2550 using +5V TTL logic level. However it requires a +3.3V regulated DC voltage to work properly. As it's shown in the schematic above, VCC and LED pin of LCD module connects to +3.3V power pin. This voltage source regulated by 78L33 +3.3V linear voltage regulator IC in TO-92 package.
78L33 +3.3V voltage regulator in TO-92 package
This chip is able to source up to 100mA output, suitable to drive this small LCD module. External crystal oscillator for PIC18F2550 is 20MHz in frequency without using Phase Lock Loop (PLL) of the controller.
Source code for this example program is written using XC8 v1.35 with MPLABX v1.51, which are the earlier versions for both compiler and IDE.
LcdWrite(LCD_C,0x04); // set temp coefficient 0x04
LcdWrite(LCD_C,0x14); // LCD bias
LcdWrite(LCD_C,0x20);
LcdWrite(LCD_C,0x0C); // LCD normal Mode
}
void LcdString( char *character){
while(*character) LcdCharacter(*character++);
}
void LcdWrite( char _dc, char _data){
D_C=_dc;
CS=0;
SerialOut(_data);
CS=1;
}
void SerialOut(unsigned char dat){
unsigned char temp;
for(int i=0;i<8;i++){
temp=dat;
SCK=0;
// TESTING MSB IF 0 SHIFT 0 OTHERWISE 1
((temp&0x80)==0x00)?SDO=0:SDO=1;
SCK=1;
dat<<=1;
}
}
Header files for device configuration, and font file are placed in the project folder. I will not show them here. Click here to download this example program.
In previous post, I have show an introductory example of using RS-232 library functions in CCS PICC. Here we will try to make more programming example using this compiler on the same topic. In this programming example, the program keep received character on this data memory, until the present of keyboard ENTER key "0x0D". At this point the micro-controller will send back all received characters.
CCS PICC Program:
#include <18f2550.h>
#include <string.h>
/*use external 20MHz crystal oscillator*/
#fuses HS,NOWDT,PLL1,CPUDIV1
#use delay(clock=20M)
#use rs232(uart1,baud=9600)
char tmp,i=0;
char txt[20];
void main(void){
//Clear PortC
output_C(0x00);
//PortC as output
set_tris_C(0x00);
printf("CCS PICC String Example Using PIC18F2550\n\r");
while(1){
/*Test if there is character in the buffer*/
if(kbhit()){
tmp=getc();
putc(tmp);
txt[i]=tmp;
i++;
}
/*Look for the present of ENTER key*/
if(tmp==0x0D){
//sendText(txt);
printf("Received Text: %s\n",txt);
/*Clear character*/
tmp='\0';
/*Clear text*/
while(i>0){
txt[i]='\0';
i--;
}
}
}
}
I use my own PIC18F2550 Test board to test this program getting an actual result.
Program testing on PIC18F2550 USB board
I send character via Serial Monitor tool of CCS PICC compiler. Its baud rate is 9600. COM1 is default for most desktop PC that come with RS-232 port.
Using Serial Monitor of CCS PICC compiler to send/receive ASCII character
As it's targeted PIC18F2550 this example program uses 2% of RAM, and 1% of ROM.