DsPIC30F4011 Development Board OLED example

In this article we look at the A look at a DsPIC30F4011 Development Board again. This time we will use the SPI OLED fitted to the board

Lets look at the DsPic and the OLED on the schematic. You can see that the OLED is connected to the RB port

 

Now here is the pinout of the DsPic

Here is a little schematic of the actual OLED that is fitted

Hardware

The board costs about $25

Description Link
DsPIC30F4011 Development Board DsPIC 30F4011 Development Board/dsPIC Experimental Board/System Board/

Code

This is the main code example, the complete project will be posted below which includes all the required source files including ones to drive the OLED, we have included main.c and a the oled ones here for reference

[codesyntax lang=”cpp”]

/************************************************* ********************/
/*File description: OLED display experiment*/
/*Hardware configuration: MCU: dsPIC30F4011 development board, OSC: use off-chip 7.3728MHzX16PLL=117.9648MHz oscillator.*/
/*Software configuration: development environment MPLAB X IDE v2.35 compiler XC16 v1.21 */
/************************************************* *******************/
#include "p30f4011.h"
#include "oled.h"
//Configuration bits
_FOSC(0Xc307); //FCKSM = 00; clock switch enable FOS = 0111 main oscillator XT crystal mode external 7.3728MHz
_FWDT(0X0000);//Off dog
_FBORPOR(0X83A2);//MCLREN enable Undervoltage 2.7V Power-up delay 16MS
_FGS(0X03);
unsigned char CN0_Flag = 0;//External interrupt flag
void Delay_1ms(unsigned int t)//t = 1000 about 1s
{
    unsigned int i,j;
    for(i = 0;i <t;i ++)
        for(j = 0;j <2000;j ++);
}
//Oscillator configuration
void System_Colck(void)
{
    //Generate Fosc = 7.3728MHz 117.9648MHz 30MIPS
    while (OSCCONbits.COSC!= 0b011)
    while (OSCCONbits.LOCK!= 1) {};//PLL is locked
}
void System_Init(void)
{
    PWMCON1 = 0x0000; //General IO
    TRISE = 0x0000; //Set output
    PORTE = 0x0000;//
}
int main()
{
    unsigned char i;
    unsigned char m;
    System_Colck();
    System_Init();
    OLED_Init(); //Initialize OLED
    OLED_Clear();
    //disn(unsigned char X,unsigned char Y,unsigned char n);
    
    for(m=33;m<59;m+=1)
    {
disn(1,1,m); //A to Z
        Delay_1ms(500);
    }
    
    for(m=65;m<91;m+=1)
    {
disn(3,1,m); //a to z
        Delay_1ms(500);
    }
    
    for(m=1;m<=9;m+=1)
    {
disn(5,1,m); //1 to 9
        Delay_1ms(500);
    }
    
    disn(2,2,23);//-
    disn(3,2,24);//.
    disn(4,2,25);// /
    disn(5,2,26);//:
    disn(6,2,27);//;
    disn(7,2,28);// <
    disn(8,2,29);// =
    disn(9,2,30);//>
    disn(10,2,31);//?
    disn(11,2,32);// @
    
    disn(3,3,11);//!
    disn(4,3,12);// "
    disn(5,3,13);// #
    disn(6,3,14);// $
    disn(7,3,15);//%
    disn(8,3,16);// &
    disn(9,3,17);// '
    disn(10,3,18);// (
    disn(11,3,19);//)
    disn(12,3,20);// *
    disn(13,3,21);// +
    disn(14,3,22);//,
    
    
    while(1)
    {
        i ++;
        PORTE = i;
        Delay_1ms(1000);
        //disn(14,3,i%100/10);disn(15,3,i%10);
    }
}

[/codesyntax]

OLED header – oled.h

[codesyntax lang=”cpp”]

#ifndef OLED_H
#define OLED_H
#include "p30f4011.h"
#include "font.h"
#include "bmp.h"
#include "delay.h"
#define u8 unsigned char
#define u32 unsigned int
#define OLED_CMD 0 //Write command
#define OLED_DATA 1 //write data
#define OLED_MODE 0
#define OLED_CS LATBbits.LATB1 //chip selection
#define OLED_RST LATBbits.LATB6//Reset
#define OLED_DC LATBbits.LATB2//data/command control
#define OLED_SCL LATBbits.LATB8//clock D0 (SCLK?
#define OLED_SDIN LATBbits.LATB7//D1 (MOSI) data
#define OLED_CS_Clr() OLED_CS=0
#define OLED_CS_Set() OLED_CS=1

#define OLED_RST_Clr() OLED_RST=0
#define OLED_RST_Set() OLED_RST=1

#define OLED_DC_Clr() OLED_DC=0
#define OLED_DC_Set() OLED_DC=1

#define OLED_SCLK_Clr() OLED_SCL=0
#define OLED_SCLK_Set() OLED_SCL=1

#define OLED_SDIN_Clr() OLED_SDIN=0
#define OLED_SDIN_Set() OLED_SDIN=1;
#define SIZE 16
#define XLevelL 0x02
#define XLevelH 0x10
#define Max_Column 128
#define Max_Row 64
#define Brightness 0xFF
#define X_WIDTH 128
#define Y_WIDTH 64

void OLED_WR_Byte(u8 dat,u8 cmd);
void OLED_Set_Pos(unsigned char x, unsigned char y);
void OLED_Display_On(void);
void OLED_Display_Off(void);
void OLED_Clear(void);
void disn(unsigned char X,unsigned char Y,unsigned char n);
void picture(void);
void OLED_Init(void);


#endif /* OLED_H */

[/codesyntax]

This is OLED.cpp

[codesyntax lang=”cpp”]

#include "oled.h"
//spi
void OLED_WR_Byte(u8 dat,u8 cmd)
{
	u8 i;
	if(cmd)
	  OLED_DC_Set();
	else
	  OLED_DC_Clr();
	OLED_CS_Clr();
	for(i=0;i<8;i++)
	{
		OLED_SCLK_Clr();
		if(dat&0x80)
			{
		   OLED_SDIN_Set();
			}
else
		   OLED_SDIN_Clr();
				OLED_SCLK_Set();
		dat<<=1;
	}
	OLED_CS_Set();
	OLED_DC_Set();
}
//Set the row and column address
void OLED_Set_Pos(unsigned char x, unsigned char y)
{
	OLED_WR_Byte(0xb0+y,OLED_CMD);
	OLED_WR_Byte(((x&0xf0)>>4)|0x10,OLED_CMD);
	OLED_WR_Byte((x&0x0f)|0x01,OLED_CMD);
}
//Turn on the OLED display
void OLED_Display_On(void)
{
	OLED_WR_Byte(0X8D,OLED_CMD);  //SET DCDC command
	OLED_WR_Byte(0X14,OLED_CMD);  //DCDC ON
	OLED_WR_Byte(0XAF,OLED_CMD);  //DISPLAY ON
}
//Turn off the OLED display
void OLED_Display_Off(void)
{
	OLED_WR_Byte(0X8D,OLED_CMD); //SET DCDC command
	OLED_WR_Byte(0X10,OLED_CMD); //DCDC OFF
	OLED_WR_Byte(0XAE,OLED_CMD); //DISPLAY OFF
}
//清屏函数!
void OLED_Clear(void)
{
	u8 i,n;
	for(i=0;i<8;i++)
	{
		OLED_WR_Byte (0xb0+i,OLED_CMD); //Set page address (0~7)
		OLED_WR_Byte (0x00,OLED_CMD); //Set display position? Column low address
		OLED_WR_Byte (0x10,OLED_CMD); //Set display position? Column high address
		for(n=0;n<128;n++)OLED_WR_Byte(0,OLED_DATA);
	} 
}
/**************************Functions************************ ***********************
*Prototype: disn(unsigned char X, unsigned char Y, unsigned char n);
*Function: Number.
************************************************** *****************************/
void disn(unsigned char X,unsigned char Y,unsigned char n)
{
	unsigned char m;
	OLED_WR_Byte(0xb7-(Y<<1),OLED_CMD);
	if(X%2)
		OLED_WR_Byte(0x08,OLED_CMD);
	else
		OLED_WR_Byte(0x00,OLED_CMD);
	OLED_WR_Byte(0x10+(X>>1),OLED_CMD);
	for(m=0;m<=15;m+=2)
		OLED_WR_Byte(*(num[n]+m),OLED_DATA);

	OLED_WR_Byte(0xb7-(Y<<1)-1,OLED_CMD);
	if(X%2)
		OLED_WR_Byte(0x08,OLED_CMD);
	else
		OLED_WR_Byte(0x00,OLED_CMD);
	OLED_WR_Byte(0x10+(X>>1),OLED_CMD);
	for(m=1;m<=15;m+=2)
		OLED_WR_Byte(*(num[n]+m),OLED_DATA);
}
/**************************Functions************************ ***********************
*Prototype: picture(void);
*Function: Image display.
************************************************** *****************************/
void picture(void)
{
    unsigned char i,j;
	for(i=0;i<8;i++)
		{
			OLED_WR_Byte(0xB0+i,OLED_CMD);//Set display position?
			OLED_WR_Byte(0x00,OLED_CMD); //Set display position? Column low address
			OLED_WR_Byte(0x10,OLED_CMD); //Set display position? Column high address
			for(j=0;j<128;j++)
		    OLED_WR_Byte(nBitmapDot[128*i+j],OLED_DATA);
		}
}
//Initialize SSD1306
void OLED_Init(void)
{
	TRISBbits.TRISB6 = 0;
	TRISBbits.TRISB8 = 0;
	TRISBbits.TRISB7 = 0;
	TRISBbits.TRISB1 = 0;
	TRISBbits.TRISB2 = 0; //OLED SPI OUT
	OLED_RST_Set();
	delay_ms(100);
	OLED_RST_Clr();
	delay_ms(100);
	OLED_RST_Set();
	OLED_WR_Byte(0xAE,OLED_CMD);//--turn off oled panel
	OLED_WR_Byte(0x00,OLED_CMD); //---set low column address
	OLED_WR_Byte(0x10,OLED_CMD); //---set high column address
	OLED_WR_Byte(0x40,OLED_CMD); //---set start line address Set Mapping RAM Display Start Line (0x00~0x3F)
	OLED_WR_Byte(0x81,OLED_CMD);//--set contrast control register
	OLED_WR_Byte(0xCF,OLED_CMD); // Set SEG Output Current Brightness
	OLED_WR_Byte(0xA1,OLED_CMD);//--Set SEG/Column Mapping 0xa0 inverted around 0xa1 normal
	OLED_WR_Byte(0xC0,OLED_CMD); //Set COM/Row Scan Direction 0xc0 upside down 0xc8 normal
	OLED_WR_Byte(0xA6,OLED_CMD);//--set normal display
	OLED_WR_Byte(0xA8,OLED_CMD); //---set multiplex ratio(1 to 64)
	OLED_WR_Byte(0x3f,OLED_CMD);//--1/64 duty
	OLED_WR_Byte(0xD3,OLED_CMD);//-set display offset Shift Mapping RAM Counter (0x00~0x3F)
	OLED_WR_Byte(0x00,OLED_CMD);//-not offset
	OLED_WR_Byte(0xd5,OLED_CMD);//--set display clock divide ratio/oscillator frequency
	OLED_WR_Byte(0x80,OLED_CMD);//--set divide ratio, Set Clock as 100 Frames/Sec
	OLED_WR_Byte(0xD9,OLED_CMD); //---set pre-charge period
	OLED_WR_Byte(0xF1,OLED_CMD);//Set Pre-Charge as 15 Clocks & Discharge as 1 Clock
	OLED_WR_Byte(0xDA,OLED_CMD);//--set com pins hardware configuration
	OLED_WR_Byte(0x12,OLED_CMD);
	OLED_WR_Byte(0xDB,OLED_CMD);//--set vcomh
	OLED_WR_Byte(0x40,OLED_CMD);//Set VCOM Deselect Level
	OLED_WR_Byte(0x20,OLED_CMD);//-Set Page Addressing Mode (0x00/0x01/0x02)
	OLED_WR_Byte(0x02,OLED_CMD);//
	OLED_WR_Byte(0x8D,OLED_CMD); //---set Charge Pump enable/disable
	OLED_WR_Byte(0x14,OLED_CMD);//--set(0x10) disable
	OLED_WR_Byte(0xA4,OLED_CMD); // Disable Entire Display On (0xa4/0xa5)
	OLED_WR_Byte(0xA6,OLED_CMD); // Disable Inverse Display On (0xa6/a7)
	OLED_WR_Byte(0xAF,OLED_CMD);//--turn on oled panel
	OLED_WR_Byte(0xAF,OLED_CMD); /*display ON*/
	OLED_Clear();
	OLED_Set_Pos(0,0);
}

[/codesyntax]

Upload this to your development board, I connected my Pickit4 to the ICSP connection – its a 1 to 1 connection. Once programming is completed you should various characters displayed on the OLED

You can see the OLED in this image

Link

https://github.com/getelectronics/PICLearning/tree/master/DsPIC30F4011%20Development%20Board/5.OLED.X

Related posts

DsPIC30F4011 Development Board push button example

DsPIC30F4011 Development Board flashing LED example

A look at a DsPIC30F4011 Development Board

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Read More