ALAT PENGUJI LCD HANDPHONE BERBASIS MIKROKONTROLER PIC - Unika Repository

  LAMPIRAN S ource Code

  #in clude <16F877a.h> #device adc=8 #includ e <string.h> #FUSES NOWDT //No Watch Dog Timer #FUSES XT //Crystal Osc (<= 4mhz) #FUSES PUT //Power Up Timer #FUSES NOPROTECT //Code not protected from reading #FUSES NODEBUG //No Debug mode for ICD #FUSE S NOBROWNOUT //Don't Reset when brownout detected #FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) use d for I/O #FUSES NOCPD //No EE protection #FUSE S NOWRT //Program memory not write protecte d #use delay(clock=4000000) #define LCD_RESET PIN_A4 #define LCD_CS PIN_A5 #define SPI_CLK PIN_C3 #define SPI_DI PIN_C4 //unused #define SPI_DO PIN_C5 #define LED PIN_D1 #define LCD_led PIN_C2 #define buzz PIN_D0 #bit SSPEN = 0x14.5 // bit to turn on/off hardware SPI // Epson S1D15G10 Command Set #define DISON 0xaf #define DISOFF 0xae #define DISNOR 0xa6 #define DISINV 0xa7 #define SLPIN 0x95 #define SLPOUT 0x94 #define COMSCN 0xbb #define DISCTL 0xca #define PASET 0x75 #define CASET 0x15 #define DATCTL 0xbc #define RGBSET8 0xce #define RAMWR 0x5c

  #define RAMRD 0x5d # define PTLIN 0xa8 #define PTLOU T 0xa9 # define RMWIN 0xe0 #define RMWOUT 0 xee #define ASCSE T 0xaa #define SCSTART 0xab # define OSCON 0xd1 # define OSCOFF 0xd2 #define PWRCTR 0x20 #define VOLCTR 0x81 #define VOLUP 0xd6 #define VOLDOWN 0xd7 #define TMPGRD 0x82 #define EPCTIN 0xcd #define EPCOUT 0xcc #define EPM WR 0xfc #define EPMRD 0xfd #define EPSRRD1 0x7c # define EPSRRD2 0x7d #define NOP 0x25 #define ENDPAGE 132 #define ENDCOL 130 // 12-bit color definitions #define WHITE 0xFFF #define BLACK 0x000 #define RED 0xe00 #define GREEN 0x0F0 # define BLUE 0x00F #define CYAN 0x0FF #define MAGENTA 0xF0F # define YELLOW 0xFF0 #define BROWN 0xB22 #define ORANGE 0xFA0 #define PINK 0xF6A //// START OF MAIN .C SOURCE FILE //// //#include "<path to .h file cu t from above>" #use fast_io(C) #define STRING_SIZE

  51 const unsigned char testcol[ 9]={ red,green,blue, green,blue,red, blue,red,green}; const unsigned char TABEL1[16*16] = { 0x00,0x00,0x00,0x00,0x00 ,0x00,0x00,0x00, // space 0x20 0x20,0x20,0x20,0x20,0x20,0 x00,0x20,0x00, // ! 0x50,0x50,0x50,0x00,0x00 ,0x00,0x00,0x00, // " 0x50,0x50,0xF8,0x50,0xF8, 0x50,0x50,0x00, // # 0x20,0x78,0xA0,0x70,0x28 ,0xF0,0x20,0x00, // $ 0xC0,0xC8,0x10,0x20,0x40 ,0x98,0x18,0x00, // % 0x40,0xA0,0xA0,0x40,0xA8 ,0x90,0x68,0x00, // & 0x30,0x30,0x20,0x40,0x00, 0x00,0x00,0x00, // ' 0x10,0x20,0x40,0x40,0x40 ,0x20,0x10,0x00, // ( 0x40,0x20,0x10,0x10,0x10,0x 20,0x40,0x00, // ) 0x00,0x20,0xA8,0x70,0x70, 0xA8,0x20,0x00, // * 0x00,0x20,0x20,0xF8,0x2 0,0x20,0x00,0x00, // + 0x00,0x00,0x00,0x00,0x30 ,0x30,0x20,0x40, // , 0x00,0x00,0x00,0xF8,0x00 ,0x00,0x00,0x00, // - 0x00,0x00,0x00,0x00,0x00 ,0x30,0x30,0x00, // . 0x00,0x08,0x10,0x20,0x40, 0x80,0x00,0x00}; // / (forward slash) const unsigned char TABEL 2[16*16] = { 0x70,0x88,0x88,0xA8,0x 88,0x88,0x70,0x00, // 0 0x30 x20,0x60,0x20,0x20,0x20,0x20,0x70,0x00, // 1 0x70,0x88,0x08,0x70,0x80, 0x80,0xF8,0x00, // 2 0xF8,0x08,0x10,0x30,0x08 ,0x88,0x70,0x00, // 3 0x10,0x30,0x50,0x90,0xF 8,0x10,0x10,0x00, // 4 0xF8,0x80,0xF0,0x08,0x 08,0x88,0x70,0x00, // 5 0x38,0x40,0x80,0xF0,0x 88,0x88,0x70,0x00, // 6 0xF8,0x08,0x08,0x1 0,0x20,0x40,0x80,0x00, // 7 0x70,0x88,0x88,0x70,0x 88,0x88,0x70,0x00, // 8 0x70,0x88,0x88,0x78,0 x08,0x10,0xE0,0x00, // 9 0x00,0x00,0x20,0x00,0 x20,0x00,0x00,0x00, // : 0x00,0x00,0x20,0x00,0x20,0 x20,0x40,0x00, // ; 0x08,0x10,0x20,0x40,0x20 ,0x10,0x08,0x00, // < 0x00,0x00,0xF8,0x00,0xF 8,0x00,0x00,0x00, // = 0x40,0x20,0x10,0x08,0x10 ,0x20,0x40,0x00, // > 0x70,0x88,0x08,0x30, 0x20,0x00,0x20,0x00}; // ? c onst unsigned char TABEL3[16*16] = { 0x70,0x88,0xA8,0xB8,0xB0,0x80,0x78,0x00, // @ 0x40 x20,0x50,0x88,0x88,0xF8,0x88,0x88,0x00, // A 0xF0,0x88,0x88,0xF0,0x88,0x88,0xF0,0x00, // B 0x70,0x88,0x80, 0x80,0x80,0x88,0x70,0x00, // C 0xF0,0x88,0x88,0x88,0x88,0x8 8,0xF0,0x00, // D xF8,0x80,0x80,0xF0,0x80,0x80,0xF8,0x00, // E 0xF8,0x80,0x80,0xF0,0x80,0x80, 0x80,0x00, // F 0x78,0x88,0x80 ,0x80,0x98,0x88,0x78,0x00, // G 0x88,0x88,0x88 ,0xF8,0x88,0x88,0x88,0x00, // H

  0x70,0x20,0x20,0x20,0x20,0x20,0x70,0x00, // I x38,0x10,0x10,0x10,0x10,0x90,0x60,0x00, // J 0x88,0x90,0xA0,0xC0,0xA0,0x90,0x88,0x 00, // K 0x80,0x80,0x80,0x80,0x80,0x80,0xF8,0x00, // L 0x88,0xD8,0xA8,0xA8,0xA8,0x88,0x88,0x00, // M 0x88,0x88,0xC8,0xA8,0x98,0x88,0x88,0x00, // N 0x70,0x88,0x88,0x88,0x88,0x88,0x70,0x00}; // O const unsigned char TABEL4[16*16] = { 0xF0,0x88,0x88,0xF0,0x80,0x80,0x80,0x00, // P 0x

  50 0x70,0x88,0x88,0x88,0xA8,0x90,0x68,0x00, // Q 0xF0,0x88,0x88,0xF0,0xA0,0x90,0x88,0x00, // R 0x70,0x88,0x80,0x70,0x08,0x88,0x70,0x00, // S 0xF8,0xA8,0x20,0x20,0x20,0x20,0x20,0x00, // T 0x88,0x88,0x88,0x88,0x88,0x88,0x70,0x00, // U 0x88,0x88,0x88,0x88,0x88,0x50,0x20,0x00, // V 0x88,0x88,0x88,0xA8,0xA8,0xA8,0x50,0x00, // W 0x88,0x88,0x50,0x20,0x50,0x88,0x88,0x00, // X 0x88,0x88,0x50,0x20,0x20,0x20,0x20,0x00, // Y 0xF8,0x08,0x10,0x70,0x40,0x80,0xF8,0x00, // Z 0x78,0x40,0x40,0x40,0x40,0x40,0x78,0x0 0, // [ 0x00,0x80,0x40,0x20,0x10,0x08,0x00,0x00, // \ (back s lash) 0x78,0x08,0x08,0x08,0x08,0x08,0x78,0x00, // ] 0x20,0x50,0x88,0x00,0x00,0x00,0x00,0x00, // ^ 0x00,0x00,0x00,0x00,0x00,0x00,0xF8,0x00}; // _ const unsigned char TABEL5[16*16] = { 0x60,0x60,0x20,0x10,0x00,0x00,0x00,0x00, // ` 0 x60 0x00,0x00,0x60,0x10,0x70,0x90,0x78,0x00, // a 0x80,0x80,0xB0,0xC8,0x88,0xC8,0xB0,0x00, // b 0x00,0x00,0x70,0x88,0x80,0x88,0x70,0x00, // c 0x08,0x08,0x68,0x98,0x88,0x98,0x68,0x00, // d 0x00,0x00,0x70,0x88,0xF8,0x80,0x70,0x00, // e 0x10,0x28,0x20,0x70,0x20,0x20,0x20,0x00, // f 0x00,0x00,0x70,0x98,0x98,0x68,0x08,0x70, // g 0x80,0x80,0xB0,0xC8,0x88,0x88,0x88,0x00, // h 0x20,0x00,0x60,0x20,0x20,0x20,0x70,0x00, // i 0x10,0x00,0x10,0x10,0x10,0x90,0x60,0x00, // j 0x80,0x80,0x90,0xA0,0xC0,0xA0,0x90,0x 00, // k 0x60,0x20,0x20,0x20,0x20,0x20,0x70,0x00, // l 0x00,0x00,0xD0,0xA8,0xA8,0xA8,0xA8,0x00, // m 0x00,0x00,0xB0,0xC8,0x88,0x88,0x88,0x00, // n 0x00,0x00,0x70,0x88,0x88,0x88,0x70,0x00}; // o const unsigned char TABEL6[16*16] = { 0x00,0x00,0xB0,0xC8,0xC8,0xB0,0x80,0x80, // p 0x70 0x00,0x00,0x68,0x98,0x98,0x68,0x08,0x08, // q 0x00,0x00,0xB0,0xC8,0x80,0x80,0x80,0x00, // r 0x00,0x00,0x78,0x80,0x70,0x08,0xF0,0x00, // s

  0x20,0x20,0xF8,0x20,0x20,0x28,0x10,0x00, // t 0x00,0x00,0x88,0x88,0x88,0x98,0x68,0x00, // u 0x00,0x00,0x88,0x88,0x88,0x50,0x20,0x00, // v 0x00,0x00,0x88,0x88,0xA8,0xA8,0x50,0x00, // w 0x00,0x00,0x88,0x50,0x20,0x50,0x88,0x00, // x 0x00,0x00,0x88,0x88,0x78,0x08,0x88,0x70, // y 0x00,0x00,0xF8,0x10,0x20,0x40,0xF8,0x00, // z 0x10,0x20,0x20,0x40,0x20,0x20,0x10,0x0 0, // { 0x20,0x20,0x20,0x00,0x20,0x20,0x20,0x00, // | 0x40,0x20,0x20,0x10,0x20,0x20,0x40,0x00, // } 0x40,0xA8,0x10,0x00,0x00,0x00,0x00,0x00, // ~ 0x70,0xD8,0xD8,0x70,0x00,0x00,0x00,0x00}; // DEL void pset(unsigned char color, unsigned char x, un signed char y); void spi_command(int); void spi_data(int); void LCDClearScreen(int colour); void LCDSetPixel(int x, int y, int color); void LCDSetLine(int x0, int y0, int x1, int y1, int c olor); void LCDPutChar( char c,int x, int y, int fColor, int bColor); void LCDPutStr(char *pString, int x, int y, int fColor, int bCo lor); void init_lcd(); void LCDSetLine(int x0, int y0, int x1, int y1, int color); void lcdfill(int x0,int y0,int dx,int dy,int color); void testRGB_v(); void testRGB_h(); void on_buz(); void main() { int picx,picy,countcol; char pesan[30]; char pesan1[1]; setup_adc_ports(NO_ANALOGS); setup_adc(ADC_OFF); setup_spi(SPI_MASTER|SPI_L_TO_H|SPI_XMI T_L_TO_H|SPI_CLK_DIV_4); setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1 ); setup_timer_1(T1_DISABLED); setup_timer_2(T2_DISABLED, 0, 1); set_tris_a(0x00); set_tris_c(0x00); // all outputs, can change to ma sk only hardware SPI bits if needed set_tris_b(0xff); port_b_pullups(true); // reset display awal: countcol=0; init_lcd(); LcdClearScreen(red); on_buz(); picy=0; strcpy(pesan," Yudi HS "); LCDPutstr(pesan,10,0,green,blue); strcpy(pesan," 02.50.0026 "); LCDPutstr(pesan,2,0,green,blue); strcpy(pesan," Created By "); for (picx=0;picx<16;picx++){ LCDPutstr(pesan,18,130-(picy+=8),RED,ORANGE); delay_ms(300); }; strcpy(pesan,"COLOR LCD TESTER"); LCDPutstr(pesan,122,0,red,green); strcpy(pesan,"Test Display RGB"); LCDPutstr(pe san,114,0,green,blue); strcpy(pesan,"1.Test Vertikal "); LCDPutstr(pesan,106,0,green,blue); strcpy(pesan,"2.T est Horizontl"); LCDPutstr(pesan ,98,0,green,blue); while (1) { s trcpy(pesan," Yudi HS "); LCDPutstr(pesan,10,0, testcol[countcol],testcol[countcol+1]); strcpy(pesan," 02.50.0026 "); LCDPutstr(pesan ,2,0,testcol[countcol+1],testcol[countcol+2]); if (!input(pin_b0)){ on_buz(); te stRGB_v(); while(input(pin_b2)){ }; on_buz(); goto awal; } else if (! input(pin_b1)){ on_buz( ); testRGB_h(); while(input(pin_b2)){ }; on_buz();

goto awal; }; countcol+=3; if (countcol= =9){ countcol=0;} delay_ms(30 0); } } void testRGB_v(){ int c,d; for (d=0;d<3;d++){ for (c=0;c<3;c++){ lcdfill(0,c*44,131,43,testcol[c+(d*3)]); } } } void on_buz(){ output_low(buzz); delay_ms(1000); output_high(buzz); } void testRGB_h(){ int c,d; for (d=0;d<3;d++){ for (c=0;c<3;c ++){ lcdfill(c*44,0,43,131,testcol[c+(d* 3)]); } } } void lcdfill(int x0,int y0,int dx,int dy,int color){ int i,j,k; for (i=y0;i <= (y0+dy);i ++){ for (j = x0; j <= (x0+dx); j ++ ) { lcdsetpixel(j ,i,color); } } } void LCDS etLine(int x0, int y0, int x1, int y1, int color) {

int16 dy; int16 dx,fractio n; int16 stepx, stepy; dy = y1 - y0; dx = x1 - x0; fo r (stepy=0;stepy<132;stepy++){ for (stepx=0;stepx<1 32;stepx++){ lcdsetp ixel(x0+stepx,y0+stepy,color); } } } vo id LCDPutStr(char *pString, int x, int y, int fColor, int bColor) { // loop until null -terminator is seen while (*pString != 0 x00) { // draw the character LC DPutChar(*pString++, x, y, fColor, bColor); y = y + 8; // bail out if y exceeds 131 if (y > 131) break; } } void LCD PutChar(char c,int x, int y, int fColor, int bColor) { int i,j; unsigned int nCols; unsigned int nRows; un signed int nBytes; un signed char PixelRow; un signed char Mask; u nsigned char indexc; u nsigned int Word0; u nsigned int Word1; unsigned int tabel;

  // get the nColumns, nRows and nBytes nCols = 8; nRows = 8; n Bytes =8; if ((c>=0x20)& &(c<0x30)){ indexc=((c-0 x20)+1)*8; tabel=1;} else if ((c>=0x30)&&(c<0x40)){ indexc=((c-0x30)+1)*8; tabel=2;} els e if ((c>=0x40)&&(c<0x50)){ i ndexc=((c-0x40)+1)*8; tabel=3;} els e if ((c>=0x50)&&(c<0x60)){ indexc=((c-0x50)+1)*8; tabel=4;} e lse if ((c>=0x60)&&(c<0x70)){ indexc=((c-0x60)+1)*8; tabel=5;} e lse{ indexc=((c-0x70)+1 )*8; tabel=6;} indexc=indexc-1; // Row address set ( command 0x2B) Sp i_Command(PASET); Sp i_Data(x); S pi_Data(x + nRows - 1); // Column address set (command 0x2A) S pi_Command(CASET); S pi_Data(y); S pi_Data(y + nCols - 1); // WRIT E MEMORY Spi_Command(RAM WR); // loop on each row, w orking backwards from the bottom to the top for (i = 0; i < 8; i++) { // copy pixel row from font table and then decrement row if (tabel==1) PixelRow = TABEL 1[indexc-i]; else if (tabel==2) PixelRow = TABEL2[indexc-i]; else if (tabe l==3) PixelRow = TABEL3[indexc-i]; else if (tabel ==4) PixelRow = TABEL4[indexc- i]; else if (tabel==5) PixelRow = TABEL5[indexc-i]; else PixelRow = TABEL6[index c-i]; // loop on each pixel in the row (left to right) // Note: we d o two pixels each loop Mask = 0x80; for (j = 0; j < 8; j += 2) { // if pixel bit set, use foreground co lor; else use the background color // now get the pixel color fo r two successive pixels if ((PixelRow & Mask) == 0) Word0 = bColor; else Word0 = fCo lor; M ask = Mask >> 1; if ((PixelRow & Mask) == 0) Word1 = bColor; else Word1 = fColor; Mask = Mask >> 1; // use this information to ou tput three data bytes S pi_Data((Word0 >> 4) & 0xFF); Spi_Data(((Word0 & 0xF) << 4) | ((Word 1 >> 8) & 0xF)); Spi_Data(Word1 & 0xFF) ; } } // terminate the Write Memory command Spi_Command(NOP); } void LCDSetPixel(int x, int y, int color) { // Row address set (command 0x2B) S pi_Command(PASET); Spi_Data(x); Spi_Data(x);

  // Column address set (command 0x2A) Spi_Command(CASET); Spi_Data(y); Spi_Data(y); // Now illuminate the pixel (2nd p ixel will be ignored) Spi_Command(RA MWR); Spi_Data((color >> 4) & 0xFF); Spi_D ata(((color & 0xF) << 4) | ((color >> 8) & 0xF)); Spi_Data(color & 0xFF); } void LCDClearScreen(int colour) { long i; // loop c ounter // Row address set (command 0x2B) Spi_Command(PASET); Spi_Data(0); Spi_Data(131); // Co lumn address set (command 0x2A) Spi_Command(CA SET); Spi_Data(0); Spi_Data(131); // set the display memory to BLACK Spi_Command(RA MWR); for(i = 0; i < ((131 * 131) / 2); i++) { Spi_Data((colour>> 4) & 0xFF); Spi_Data(((colour& 0xF) << 4) | ((c olour>> 8) & 0xF)); Spi_Data(colour & 0xFF); } Sp i_Command(NOP); } v oid pset(unsigned char color, unsigned char x, unsigned char y){ // sets the starting page(row) and column ( x & y) coordinates in ram, // then writes the colour to display memory. The ending x & y are left // maxed out so one can continue sen ding colour data bytes to the 'open' // RAMWR command to fill further memory. issuing any other command // finishes RA MWR. x += 2; // for some reason starts at 2 spi_command(PASET); // page start/end ram spi_data(x); spi_data(END PAGE); spi_command(CASET); // column start/end ram spi_data(y); spi_data(ENDCOL); spi_command(RAMWR) ; // write s pi_data(color); } void init_lcd(void){ o utput_high(lcd_led); output_low (SPI_CL K); output_low (SPI_DO); output_high (LCD_CS); output_low (PIN_A3); delay_ms(50 0); output_high (P

  IN_A3); spi_command(DISCTL); // display co ntrol spi_data(0x0C); // 12 = 1100 - CL dividing ratio [don't divide] switching period 8H (de fault) spi_data(0x20 ); // 32 = (128/4)-1 (round up) number of display lines for s canning spi_data(0x0C); // 12 = 1100 - n umber of lines to be inversely highlighted // spi_data(0x00); spi_command(COMSCN); // common scanning directi on spi_data(0x01);

  // 0 0 0 = 1 -> 80 81 -> 160 // 0 0 1 = 1 -> 80 81 <- 160

  // 0 1 0 = 1 <- 80 8 1 -> 160 // 0 1 1 = 1 <- 80 81 <- 160 spi_command(OSCON); // internal oscialltor ON spi_command(SLPOUT); // sleep out spi_command(VOLCTR); // electronic volume, this is the contrast/brig htness spi_data(0x0); // volume (contrast) setting - fine tuning spi_data(0x30); // i nternal resistor ratio - coarse adjustment spi_command(PWRCTR); // power ctrl spi_data(0x0f); //everything on, no external reference resistors delay_ms(100); spi_command(DISINV ); // invert display mode spi_command(DATCTL); // data control Spi_Data(0x 01); // P1: 0x01 = page address inverted, column address normal, address scan in column direction

  Spi_Data(0x00); // P2: 0x00 = RGB sequence (default value) Spi_Data(0x02); // P3: 0x02 = Gray scale -> 16 (selects 12-bit color, type A) spi_command(D

  ISON); // display on delay_ms(100);} void spi_command(int dat){ output_low(LCD_CS); // enable chip SSPEN = 0; // sh ut off hardware SPI allowing direct access to SPI in/out pins output_low (SPI_ DO); // output low on data out (9th bit low = command) output_high (SPI_CLK); de lay_cycles(1); // send clock pulse output_low (SPI_CLK); SSPEN=1; // turn hardware SPI back on spi_write(dat); // make PIC do the work for the command byte output_high(LCD_CS); // disable } vo id spi_data(int dat){ output_low(LCD_CS); // enable chip SSPEN = 0; // turn off hardware SPI allowing us direct access to SPI in/out pins output_high (SPI_DO); // outp ut high on data out (9th bit high = data) output_high (SPI_CLK); delay_cycles(1); // send clo ck pulse output_low (SPI_CLK); SSPEN=1; // turn hardware SPI back on spi_write(dat); // make PIC do the work for the data byte output_high(LCD_CS); // disable }