LCD Display och Grafik Demonstration och konsultation Arbetsbokens avsnitt 5 och 6 LCD Grafisk display Introduktion till laboration 3 Målsättning: Efter lektionen ska alla självständigt kunna slutföra förberedelserna inför laboration 3 Demonstrationsövning 4 1
Definitioner: Intressanta bit positioner #define B_E 0x40 /* Enable */ #define B_RST 0x20 /* Reset */ #define B_CS2 0x10 /* Controller Select 2 */ #define B_CS1 8 /* Controller Select 1 */ #define B_SELECT 4 /* ASCII or Graphics select */ #define B_RW 2 /* Read/Write */ #define B_DI 1 /* Data or Instruction : 1 data, 0 Instr */ Demonstrationsövning 4 2
En display. 2 controllers. cs1 cs2 Demonstrationsövning 4 3
Definitoner: Kommandon (Sid 17 HITACHI datablad) #define LCD_ON 0x3F /* Display på */ #define LCD_OFF 0x3E /* Av */ #define LCD_SET_ADD 0x40 /* Set X adress (0 63) */ #define LCD_SET_PAGE 0xB8 /* Set Y adress (0 7) */ #define LCD_DISP_START 0xC0 /* Start adress i display minne */ #define LCD_BUSY 0x80 /* läsa ut busy status. R/W skall vara hög */ Demonstrationsövning 4 4
cs1=1 cs2=0 cs2=1 cs1=0 Addr X 0-63 Addr X 0-63 Y 0 Y 7 En bit per pixel (på eller av) Demonstrationsövning 4 5
Exempel: cs1 = 1, cs2 = 0, x addr = 7, y addr = 0 Demonstrationsövning 4 6
Exempel: cs1 = 0, cs2 = 1, x addr = 7, y addr = 0 Demonstrationsövning 4 7
Port E #define PORT_DISPLAY_BASE 0x40021000 /* MD407 port E */ #define portmoder ((volatile unsigned int *) (PORT_DISPLAY_BASE)) #define portotyper ((volatile unsigned short *)(PORT_DISPLAY_BASE+0x4)) #define portospeedr ((volatile unsigned int *) (PORT_DISPLAY_BASE+0x8)) #define portpupdr ((volatile unsigned int *) (PORT_DISPLAY_BASE+0xC)) #define portidr ((volatile unsigned short *)(PORT_DISPLAY_BASE+0x10)) #define portidrhigh ((volatile unsigned char *)(PORT_DISPLAY_BASE+0x11)) #define portodrlow ((volatile unsigned char *)(PORT_DISPLAY_BASE+0x14)) #define portodrhigh ((volatile unsigned char *)(PORT_DISPLAY_BASE+0x14+1)) Demonstrationsövning 4 8
Konfigurera portar void init_app(void) /* PORT E */ *portmoder = 0x55555555; /* all bits outputs */ *portotyper = 0x00000000; /* outputs are push/pull */ *portospeedr = 0x55555555; /* medium speed */ *portpupdr = 0x55550000; /* inputs are pull up */ Demonstrationsövning 4 9
Bekant sedan tidigare: Men det skiljer sig i detaljerna. static void graphic_ctrl_bit_set( unsigned char x ) *portodrlow = ( ~B_SELECT & x ); static void graphic_ctrl_bit_clear( unsigned char x ) *portodrlow &= ( ~B_SELECT & ~x ); Demonstrationsövning 4 10
Implementera select_controller static void select_controller(unsigned char controller) switch(controller) case 0: graphic_ctrl_bit_clear(b_cs1 B_CS2); break; case B_CS1 : graphic_ctrl_bit_set(b_cs1); graphic_ctrl_bit_clear(b_cs2); break; case B_CS2 : graphic_ctrl_bit_set(b_cs2); graphic_ctrl_bit_clear(b_cs1); break; case B_CS1 B_CS2 : graphic_ctrl_bit_set(b_cs1 B_CS2); break; Demonstrationsövning 4 11
Koncepten kända sedan tidigare (ASCII Display). static void graphic_wait_ready(void) unsigned char c; graphic_ctrl_bit_clear( B_E ); *portmoder = 0x00005555; /* b15-8 are inputs, 7-0 are outputs */ graphic_ctrl_bit_clear( B_DI ); graphic_ctrl_bit_set( B_RW ); delay_500ns(); while( 1 ) graphic_ctrl_bit_set( B_E ); delay_500ns(); c = *portidrhigh & 0x80; if( c == 0 )break; graphic_ctrl_bit_clear( B_E ); delay_500ns(); *portmoder = 0x55555555; /* all bits outputs */ graphic_ctrl_bit_set( B_E ); Demonstrationsövning 4 12
static unsigned char display_read(unsigned char controller) unsigned char c; *portmoder = 0x00005555; /* b15-8 are inputs, 7-0 are outputs */ select_controller( controller ); graphic_ctrl_bit_clear( B_E ); graphic_ctrl_bit_set( B_DI B_RW ); delay_500ns(); graphic_ctrl_bit_set( B_E ); delay_500ns(); c = *portidrhigh; graphic_ctrl_bit_clear( B_E ); *portmoder = 0x55555555; /* all bits outputs */ if( controller & B_CS1 ) select_controller( B_CS1); graphic_wait_ready(); if( controller & B_CS2 ) select_controller( B_CS2); graphic_wait_ready(); return c; Demonstrationsövning 4 13
Sid 10 Hitachi, datablad visar att man behöver göra 2 st läsningar varav den första resulterar i ett dummy värde. static unsigned char graphic_read(unsigned char controller) display_read(controller); return display_read(controller); Demonstrationsövning 4 14
Implementera graphic_write static void graphic_write(unsigned char val, unsigned char controller) *portodrhigh = val; select_controller( controller ); delay_500ns(); graphic_ctrl_bit_set( B_E ); delay_500ns(); graphic_ctrl_bit_clear( B_E ); if( controller & B_CS1 ) select_controller( B_CS1); graphic_wait_ready(); if( controller & B_CS2 ) select_controller( B_CS2); graphic_wait_ready(); *portodrhigh = 0; graphic_ctrl_bit_set( B_E ); select_controller( 0 ); Demonstrationsövning 4 15
Skriva kommandon och data static void graphic_writecommand(unsigned char commandtowrite, unsigned char controller) graphic_ctrl_bit_clear( B_E ); graphic_ctrl_bit_clear( B_DI B_RW ); graphic_write(commandtowrite, controller); static void graphic_writedata(unsigned char data, unsigned char controller) graphic_ctrl_bit_clear( B_E ); graphic_ctrl_bit_set( B_DI ); graphic_ctrl_bit_clear( B_RW ); graphic_write(data, controller); Demonstrationsövning 4 16
Initiering (sid 88 arbetsboken) void graphic_initalize(void) graphic_ctrl_bit_set( B_E ); delay_micro(10); graphic_ctrl_bit_clear(b_cs1 B_CS2 B_RST B_E); delay_milli( 30 ); graphic_ctrl_bit_set(b_rst); delay_milli( 100 ); graphic_writecommand(lcd_off, B_CS1 B_CS2); graphic_writecommand(lcd_on, B_CS1 B_CS2); graphic_writecommand(lcd_disp_start, B_CS1 B_CS2); graphic_writecommand(lcd_set_add, B_CS1 B_CS2); graphic_writecommand(lcd_set_page, B_CS1 B_CS2); select_controller(0); Demonstrationsövning 4 17
Grafik rutiner clear_screen (sid 88 arbetsboken) pixel Demonstrationsövning 4 18
cs1=1 cs2=0 cs2=1 cs1=0 Addr X 0-63 Addr X 0-63 Y 0 Y 7 #define LCD_SET_ADD 0x40 /* Set X adress (0 63) */ #define LCD_SET_PAGE 0xB8 /* Set Y adress (0 7) */ Demonstrationsövning 4 19
void graphic_clearscreen(void) unsigned char i, j; for(j = 0; j < 8; j++) graphic_writecommand(lcd_set_page j, B_CS1 B_CS2 ); graphic_writecommand(lcd_set_add 0, B_CS1 B_CS2 ); for(i = 0; i <= 63; i++) graphic_writedata(0, B_CS1 B_CS2); Demonstrationsövning 4 20
Implementera pixel: logiska koordinater (arbetsbok sidan 89) 1 1 128 pixel(x,y) 64 Demonstrationsövning 4 21
Implementera pixel: översättning till fysiska koordinater 1 128 1 pixel(x,y) 64 X-led: Vilken kontroller skall aktiveras? Y-led: Kan endast läsa/skriva hela bytes! Demonstrationsövning 4 22
Implementera pixel: översättning till fysiska koordinater 1 128 1 pixel(x,y) 64 X-led: Vilken kontroller skall aktiveras? X-led: x <=64 cs1=1, cs2=0 x > 64 cs1=0, cs2=1 Y-led: Kan endast läsa/skriva hela bytes! Y-led: (y 1) / 8 vilken rad (0 7) av bytes. (y 1) % 8 bit i denna byte som vi är intresserade av. Demonstrationsövning 4 23
void pixel( int x, int y, int set ) unsigned char mask,c, controller; int index; if( (x > 128 ) (y > 64) ) return; index = (y-1)/8; switch( (y-1)%8 ) case 0: mask = 1; break; case 1: mask = 2; break; case 2: mask = 4; break; case 3: mask = 8; break; case 4: mask = 0x10; break; case 5: mask = 0x20; break; case 6: mask = 0x40; break; case 7: mask = 0x80; break; if( set == 0) mask = ~mask; Demonstrationsövning 4 24
if(x > 64) controller = B_CS2; x = x - 65; else controller = B_CS1; x = x-1; Demonstrationsövning 4 25
graphic_writecommand(lcd_set_add x, controller ); graphic_writecommand(lcd_set_page index, controller ); c = graphic_read( controller ); graphic_writecommand(lcd_set_add x, controller ); if( set ) mask = mask c; else mask = mask & c; graphic_writedata( mask, controller); Demonstrationsövning 4 26
Main int main(int argc, char **argv) POBJECT p = &ball; init_app(); graphic_initalize(); graphic_clearscreen(); p->set_speed( p, 8, 1); while( 1 ) p->move( p ); delay_milli(40); /* 25 frames/sec */ Demonstrationsövning 4 27
THE END! Demonstrationsövning 4 28
Anekdot: 90tal. MS-Dos, Intel 486 16Mb RAM, 66MHz, VGA grafik adapter. void set_vga_mode_320x200() /* x86 assembler */ _asm mov ah, 0 mov al, 0x13 int 0x10 Demonstrationsövning 4 29
Grafik minne 320 200 1 byte per pixel 64000 bytes totalt 1 byte per pixel 256 olika färger per pixel Demonstrationsövning 4 30
64000Bytes i minnet från address 0xA0000000 bestämmer vad som visas på skärmen av VGA adaptern. unsigned char *vga = (unsigned char *)0xA0000000; Demonstrationsövning 4 31
x 320 y 200 Pixel position i minnes array = 320 * y + x Demonstrationsövning 4 32
VGA set_pixel void set_pixel(int x, int y, unsigned char color) vga[320 * y + x] = color; Demonstrationsövning 4 33