Friday, May 28, 2021

Beginning dsPIC30F1010 coding using XC16

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.

Start dsPIC30F1010 coding using XC8

dsPIC30F1010 reference image

It has DIP and SMD packages options. DIP package is friendly for breadboard prototyping. I choose a 28-pin SMD package.

Start dsPIC30F1010 coding using XC16

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)
#pragma config GSS = GSS_OFF            // General Segment Code Protection (Disabled)

// FOSCSEL
#pragma config FNOSC = FRC              // Oscillator Mode (Internal Fast RC (FRC))

// FOSC
#pragma config POSCMD = PRIOSC_OFF      // Primary Oscillator Source (Primary Oscillator Disabled)
#pragma config OSCIOFNC = OSC2_IO       // OSCI/OSCO Pin Function (OSCO pin has digital I/O function)
#pragma config FRANGE = FRC_HI_RANGE    // Frequency Range Select (High Range)
#pragma config FCKSM = CSW_FSCM_OFF     // Clock Switching and Monitor (Sw Disabled, Mon Disabled)

// FWDT
#pragma config WDTPS = WDTPOST_PS32768  // Watchdog Timer Postscaler (1:32,768)
#pragma config FWPSA0 = WDTPRE_PR128    // WDT Prescaler (1:128)
#pragma config WWDTEN = WINDIS_OFF      // Watchdog Timer Window (Non-Window mode)
#pragma config FWDTEN = FWDTEN_OFF      // Watchdog Timer Enable (Disable)

// FPOR
#pragma config FPWRT = PWRT_128         // POR Timer Value (128ms)

// FICD
#pragma config ICS = ICS_PGD            // Comm Channel Select (Use PGC/EMUC and PGD/EMUD)

// FUID0

// FUID1

Its schematic diagram is shown below.

Start dsPIC30F1010 coding using XC16
Schematic Diagram

Click here to download its zip file.

Start dsPIC30F1010 coding using XC16

XC16 in MPLABX IDE

Start dsPIC30F1010 coding using XC16

Circuit wiring on breadboard

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.

 


Saturday, May 22, 2021

ATMega32 LCD and Keypad Interfacing Example

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.

ATMega32 LCD and Keypad Interfacing Example
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.

ATMega32 LCD and Keypad Interfacing Example
Schematic

If you want a standard PCB for ATMega32 micro-controller, you can order my AVR Microcontroller project from PCBWay with a reasonable price. Click here to get a free $5 credit for new account.


See Also,

Saturday, May 15, 2021

PIC18F2550 interfacing with Nokia 5110 LCD

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. 

PIC18F2550 interfacing with Nokia 5110 LCD
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:

  1. VCC - Positive supply voltage (from 2.7 V to 3.3 V)
  2. GND - Ground connection
  3. SCE - Chip Select (active low)
  4. RST - Reset (active low)
  5. D/C - Data/Command mode ( low for command and high for data)
  6. DIN - SPI MOSI
  7. SCLK - SPI serial clock
  8. 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.

PIC18F2550 interfacing with Nokia 5110 LCD
Program testing on physical prototyping

Actual schematic is shown below.

PIC18F2550 interfacing with Nokia 5110 LCD
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.

PIC18F2550 interfacing with Nokia 5110 LCD
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.

/*
 * PIC18F2550 interfaces to Nokia 5110
 * SPI LCD using xc8
 */

#include <xc.h>
#include "pic18f2550Config.h"
#include "fonts.h"

#define _XTAL_FREQ 20000000

#define CS      LB0
#define RST     LB1
#define D_C     LB2
#define SDO     LB3
#define SCK     LB4

#define LCD_C     0
#define LCD_D     1

#define LCD_X     84
#define LCD_Y     48

unsigned char xCount=0;

void LcdCharacter( char charactor);
void LcdClear(void);
void LcdInitialize(void);
void LcdString( char *charactor);
void LcdWrite( char dc,unsigned char _data);
void SerialOut(unsigned char dat);
void lcdNewLine();

void main() {
    LcdInitialize();
    LcdClear();
    LcdString("akiTechnical");
    LcdString("Nokia5110 LCD");
    lcdNewLine();
    LcdString("interfaces");
    lcdNewLine();
    LcdString("with PIC18F2550");


    while(1){
    }
}

void LcdCharacter( char character){
     int i;
     static int xT=0;
     LcdWrite(LCD_D,0x00);
     for(i=0;i<5;i++){
         LcdWrite(LCD_D,ASCII[character-0x20][i]);
     }
     LcdWrite(LCD_D,0x00);
     xT+=1;
     if(xT>=12){
         xT=0;
     }
     xCount=xT;
}

void LcdClear(void){
     int i;
     for(i=0;i<LCD_X*LCD_Y/8;i++) LcdWrite(LCD_D,0x00);
}

void lcdNewLine(){
    int temp;
    temp=12-xCount;
    if(temp!=0){
     for(int i=0;i<temp;i++)
         LcdCharacter(' ');
    }
}

void LcdInitialize(void){
     TRISB=0x00;
     CS=1;
     RST=0;
     RST=1;

     LcdWrite(LCD_C,0x21); // LCD Extended Commands
     LcdWrite(LCD_C,0xB1); // SET LCD CONTRAST
     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.

Tuesday, May 11, 2021

RS-232 Example using PIC18F2550

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.


RS-232 Example using PIC18F2550
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.

RS-232 Example using PIC18F2550
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.



Search This Blog

Labels

25AA010A (1) 8051 (7) 93AA46B (1) ADC (30) Analog Comparator (1) Arduino (15) ARM (6) AT89C52 (7) ATMega32 (56) AVR (57) CCS PICC (28) DAC (1) DHT11 (2) Display (105) Distance Sensor (3) DS18B20 (3) dsPIC (2) dsPIC30F1010 (2) EEPROM (5) Environment Sensor (4) esp8266 (1) I2C (29) Input/Output (67) Interrupt (19) Keil (5) Keypad (10) LCD (47) Master/Slave (1) MAX7221 (1) MCP23017 (5) MCP23S17 (4) Meter (3) MikroC (2) Motor (15) MPLABX (71) Nokia 5110 LCD (3) OLED (2) One-Wire (6) Oscillator (8) PCB (6) PCD8544 (3) PCF8574 (5) PIC (107) PIC12F (2) PIC16F628A (2) PIC16F630 (1) PIC16F716 (3) PIC16F818 (10) PIC16F818/819 (2) PIC16F84A (15) PIC16F876A (1) PIC16F877A (9) PIC16F88 (1) PIC16F887 (60) PIC18 (19) PIC18F1220 (4) PIC18F2550 (3) PIC18F4550 (12) PWM (11) RTC (8) Sensor (10) SH1106 (1) Shift Register (11) Shift Registers (3) SPI (24) STM32 (6) STM32 Blue Pill (6) STM32CubeIDE (6) STM32F103C8T6 (6) SysTick (3) temperature sensor (11) Thermometer (21) Timer/Counter (31) TM1637 (2) UART (7) Ultrasonic (4) Voltmeter (7) WDT (1) XC16 (2) XC8 (94)