#include <16F877A.h>
#fuses XT,NOWDT,NOPROTECT,LVP
#use delay(clock=4000000)
#define LCD_CD PIN_A1
#define LCD_WR PIN_A2
#define LCD_RD PIN_A3
#define LCD_CS PIN_A0
#define LCD_RES PIN_A4
#define CLR_CD OUTPUT_LOW(LCD_CD)
#define SET_CD OUTPUT_HIGH(LCD_CD)
#define CLR_CS OUTPUT_LOW(LCD_CS)
#define SET_CS OUTPUT_HIGH(LCD_CS)
#define CLR_RESET OUTPUT_LOW(LCD_RES)
#define SET_RESET OUTPUT_HIGH(LCD_RES)
#define CLR_WR OUTPUT_LOW(LCD_WR)
#define SET_WR OUTPUT_HIGH(LCD_WR)
#define CLR_RD OUTPUT_LOW(LCD_RD)
#define SET_RD OUTPUT_HIGH(LCD_RD)
// color definitions
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
/*************************************************/
void write_command(int16 command)
{
CLR_CS;
CLR_CD;
SET_RD;
SET_WR;
output_b(command>>8);
CLR_WR;
SET_WR;
delay_us(1);
output_b(command);
CLR_WR;
delay_us(1);
SET_WR;
SET_CS;
}
/*************************************************/
void write_data(int16 data)
{
CLR_CS;
SET_CD;
SET_RD;
SET_WR;
output_b(data>>8);
CLR_WR;
SET_WR;
delay_us(1);
output_b(data);
CLR_WR;
delay_us(1);
SET_WR;
SET_CS;
}
/*************************************************/
void initialization()
{
SET_RD;
SET_WR;
SET_CS;
SET_CD;
output_b(0x00);
CLR_RESET;
delay_ms(1000);
SET_RESET;
delay_ms(50);
write_command(0x0001); // Driver Output Control Register (R01h)
write_data(0x0000); // Page 14, SPFD5408B Datasheet
write_command(0x0002); // LCD Driving Waveform Control (R02h)
write_data(0x0700); // Page 15, SPFD5408B Datasheet
write_command(0x0003); // Entry Mode (R03h)
write_data(0x50A0); // Page 15, SPFD5408B Datasheet
write_command(0x0004); // Scaling Control register (R04h)
write_data(0x0000); // Page 16, SPFD5408B Datasheet
write_command(0x0008); // Display Control 2 (R08h)
write_data(0x0207); // Page 17, SPFD5408B Datasheet
write_command(0x0009); // Display Control 3 (R09h)
write_data(0x0000); // Page 18, SPFD5408B Datasheet
write_command(0x000A); // Frame Cycle Control (R0Ah)
write_data(0x0000); // Page 19, SPFD5408B Datasheet
write_command(0x000C); // External Display Interface Control 1 (R0Ch)
write_data(0x0000); // Page 20, SPFD5408B Datasheet
write_command(0x000D); // Frame Maker Position (R0Dh)
write_data(0x0000); // Page 21, SPFD5408B Datasheet
write_command(0x000F); // External Display Interface Control 2 (R0Fh)
write_data(0x0000); // Page 21, SPFD5408B Datasheet
write_command(0x0010); // Power Control 1 (R10h)
write_data(0x0000); // Page 22, SPFD5408B Datasheet
write_command(0x0011); // Power Control 2 (R11h)
write_data(0x0007); // Page 23, SPFD5408B Datasheet
write_command(0x0012); // Power Control 3 (R12h)
write_data(0x0000); // Page 24, SPFD5408B Datasheet
write_command(0x0013); // Power Control 4 (R13h)
write_data(0x0000); // Page 25, SPFD5408B Datasheet
delay_ms(200);
write_command(0x0007); // Display Control (R07h)
write_data(0x0101); // Page 16, SPFD5408B Datasheet
write_command(0x0010); // Power Control 1 (R10h)
write_data(0x12B0); // Page 22, SPFD5408B Datasheet
write_command(0x0011); // Power Control 2 (R11h)
write_data(0x0007); // Page 23, SPFD5408B Datasheet
write_command(0x0012); // Power Control 3 (R12h)
write_data(0x01BB); // Page 24, SPFD5408B Datasheet
delay_ms(50);
write_command(0x0013); // Power Control 4 (R13h)
write_data(0x1300); // Page 25, SPFD5408B Datasheet
write_command(0x0029); // NVM read data 2 (R29h)
write_data(0x0010); // Page 30, SPFD5408B Datasheet
delay_ms(50);
write_command(0x0030); // Gamma Control 1
write_data(0x000A); // Page 32, SPFD5408B Datasheet
write_command(0x0031); // Gamma Control 2
write_data(0x1326); // Page 32, SPFD5408B Datasheet
write_command(0x0032); // Gamma Control 3
write_data(0x0A29); // Page 32, SPFD5408B Datasheet
write_command(0x0033); // Gamma Control 4
write_data(0x290A); // Page 32, SPFD5408B Datasheet
write_command(0x0034); // Gamma Control 5
write_data(0x2613); // Page 32, SPFD5408B Datasheet
write_command(0x0035); // Gamma Control 6
write_data(0x0A0A); // Page 32, SPFD5408B Datasheet
write_command(0x0036); // Gamma Control 7
write_data(0x1E03); // Page 32, SPFD5408B Datasheet
write_command(0x0037); // Gamma Control 8
write_data(0x031E); // Page 32, SPFD5408B Datasheet
write_command(0x0038); // Gamma Control 9
write_data(0x0706); // Page 32, SPFD5408B Datasheet
write_command(0x0039); // Gamma Control 10
write_data(0x0303); // Page 32, SPFD5408B Datasheet
write_command(0x003A); // Gamma Control 11
write_data(0x0E04); // Page 32, SPFD5408B Datasheet
write_command(0x003B); // Gamma Control 12
write_data(0x0E01); // Page 32, SPFD5408B Datasheet
write_command(0x003C); // Gamma Control 13
write_data(0x010E); // Page 32, SPFD5408B Datasheet
write_command(0x003D); // Gamma Control 14
write_data(0x040E); // Page 32, SPFD5408B Datasheet
write_command(0x003E); // Gamma Control 15
write_data(0x0303); // Page 32, SPFD5408B Datasheet
write_command(0x003F); // Gamma Control 16
write_data(0x0607); // Page 32, SPFD5408B Datasheet
write_command(0x0050); // Window Horizontal RAM Address Start (R50h)
write_data(0x0000); // Page 32, SPFD5408B Datasheet
write_command(0x0051); // Window Horizontal RAM Address End (R51h)
write_data(0x00EF); // Page 32, SPFD5408B Datasheet
write_command(0x0052); // Window Vertical RAM Address Start (R52h)
write_data(0x0000); // Page 33, SPFD5408B Datasheet
write_command(0x0053); // Window Vertical RAM Address End (R53h)
write_data(0x013F); // Page 33, SPFD5408B Datasheet
write_command(0x0060); // Driver Output Control (R60h)
write_data(0x2700); // Page 33, SPFD5408B Datasheet
write_command(0x0061); // Driver Output Control (R61h)
write_data(0x0001); // Page 35, SPFD5408B Datasheet
write_command(0x006A); // Vertical Scroll Control (R6Ah)
write_data(0x0000); // Page 35, SPFD5408B Datasheet
write_command(0x0080); // Display Position - Partial Display 1 (R80h)
write_data(0x0000); // Page 35, SPFD5408B Datasheet
write_command(0x0081); // RAM Address Start - Partial Display 1 (R81h)
write_data(0x0000); // Page 35, SPFD5408B Datasheet
write_command(0x0082); // RAM Address End - Partial Display 1 (R82h)
write_data(0x0000); // Page 36, SPFD5408B Datasheet
write_command(0x0083); // Display Position - Partial Display 2 (R83h)
write_data(0x0000); // Page 36, SPFD5408B Datasheet
write_command(0x0084); // RAM Address Start - Partial Display 2 (R84h)
write_data(0x0000); // Page 36, SPFD5408B Datasheet
write_command(0x0085); // RAM Address End - Partial Display 2 (R85h)
write_data(0x0000); // Page 36, SPFD5408B Datasheet
write_command(0x0090); // Panel Interface Control 1 (R90h)
write_data(0x0010); // Page 36, SPFD5408B Datasheet
write_command(0x0092); // Panel Interface Control 2 (R92h)
write_data(0x0000); // Page 37, SPFD5408B Datasheet
write_command(0x0093); // Panel Interface control 3 (R93h)
write_data(0x0103); // Page 38, SPFD5408B Datasheet
write_command(0x0095); // Panel Interface control 4 (R95h)
write_data(0x0210); // Page 38, SPFD5408B Datasheet
write_command(0x0097); // Panel Interface Control 5 (R97h)
write_data(0x0000); // Page 40, SPFD5408B Datasheet
write_command(0x0098); // Panel Interface Control 6 (R98h)
write_data(0x0000); // Page 41, SPFD5408B Datasheet
write_command(0x0007); // Display Control (R07h)
write_data(0x0173); // Page 16, SPFD5408B Datasheet
}
/*************************************************/
void Display_Home()
{
write_command( 0x0020); // GRAM Address Set (Horizontal Address) (R20h)
write_data( 0x0000);
write_command( 0x0021); // GRAM Address Set (Vertical Address) (R21h)
write_data( 0x0000);
write_command(0x0022); // Write Data to GRAM (R22h)
}
/*************************************************/
void display_rgb(int16 data)
{
int16 i,j;
Display_Home();
for(i=0;i<320;i++)
{
for(j=0;j<240;j++)
{
write_data(data);
}
}
}
/*************************************************/
void LCD_test()
{
int16 i,j;
Display_Home();
write_data(BLACK);
delay_ms(1000);
write_data(BLUE);
delay_ms(1000);
write_data(GREEN);
delay_ms(1000);
write_data(CYAN);
delay_ms(1000);
write_data(RED);
delay_ms(1000);
write_data(MAGENTA);
delay_ms(1000);
write_data(YELLOW);
delay_ms(1000);
write_data(WHITE);
}
/*************************************************/
void main()
{
output_low(PIN_C2);
output_b(0xF8); // pull-up on switches, all others off
output_a(0xFE); // all pins high
// Initialize the display
initialization();
LCD_test();
blinkLED: output_low(PIN_C2); //Blinks LED upon completion
delay_ms(1000);
output_high(PIN_C2);
delay_ms(1000);
goto blinkLED;
}
/*************************************************/