Programmering av inbyggda system
Demonstrationsövning 4 1
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
Programmering av inbyggda system
Demonstrationsövning 4 2
#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 */
Definitioner: Intressanta bit positioner
Programmering av inbyggda system
Demonstrationsövning 4 3
En display. 2 ”controllers”.
cs1 cs2
Programmering av inbyggda system
Demonstrationsövning 4 4
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 */
Programmering av inbyggda system
Demonstrationsövning 4 5
Addr X 0 - 63 Addr X 0 - 63
Y 0
Y 7
cs1=1cs2=0
cs2=1cs1=0
En bit per pixel (på eller av)
Programmering av inbyggda system
Demonstrationsövning 4 6
Exempel: cs1 = 1, cs2 = 0, x addr = 7, y addr = 0
Programmering av inbyggda system
Demonstrationsövning 4 7
Exempel: cs1 = 0, cs2 = 1, x addr = 7, y addr = 0
Programmering av inbyggda system
Demonstrationsövning 4 8
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))
Programmering av inbyggda system
Demonstrationsövning 4 9
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 */}
Programmering av inbyggda system
Demonstrationsövning 4 10
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 );}
Programmering av inbyggda system
Demonstrationsövning 4 11
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;
}}
Programmering av inbyggda system
Demonstrationsövning 4 12
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 );
}
Programmering av inbyggda system
Demonstrationsövning 4 13
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;}
Programmering av inbyggda system
Demonstrationsövning 4 14
Sid 10 Hitachi, datablad visar att man behöver göra 2 st läsningarvarav den första resulterar i ett ”dummy” värde.
static unsigned char graphic_read(unsigned char controller){
display_read(controller);return display_read(controller);
}
Programmering av inbyggda system
Demonstrationsövning 4 15
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 );
}
Programmering av inbyggda system
Demonstrationsövning 4 16
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);
}
Programmering av inbyggda system
Demonstrationsövning 4 17
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);
}
Programmering av inbyggda system
Demonstrationsövning 4 18
Grafik rutiner
• clear_screen (sid 88 arbetsboken) • pixel
Programmering av inbyggda system
Demonstrationsövning 4 19
Addr X 0 - 63 Addr X 0 - 63
Y 0
Y 7
cs1=1cs2=0
cs2=1cs1=0
#define LCD_SET_ADD 0x40 /* Set X adress (0 – 63) */ #define LCD_SET_PAGE 0xB8 /* Set Y adress (0 – 7) */
Programmering av inbyggda system
Demonstrationsövning 4 20
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);}
}}
Programmering av inbyggda system
Demonstrationsövning 4 21
Implementera pixel: logiska koordinater (arbetsbok sidan 89)
1 128
1
64
pixel(x,y)
Programmering av inbyggda system
Demonstrationsövning 4 22
Implementera pixel: översättning till fysiska koordinater1 128
1
64
pixel(x,y)
X-led: Vilken kontroller skall aktiveras?
Y-led: Kan endast läsa/skriva hela bytes!
Programmering av inbyggda system
Demonstrationsövning 4 23
Implementera pixel: översättning till fysiska koordinater1 128
1
64
pixel(x,y)
X-led: Vilken kontroller skall aktiveras?
Y-led: Kan endast läsa/skriva hela bytes!
X-led: x <=64 → cs1=1, cs2=0x > 64 → cs1=0, cs2=1
Y-led: (y – 1) / 8 → vilken rad (0 – 7) av bytes.
(y – 1) % 8 → bit i denna byte som vi är intresserade av.
Programmering av inbyggda system
Demonstrationsövning 4 24
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;
Programmering av inbyggda system
Demonstrationsövning 4 25
if(x > 64){controller = B_CS2;x = x - 65;
} else {controller = B_CS1;x = x-1;
}
Programmering av inbyggda system
Demonstrationsövning 4 26
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);
}
Programmering av inbyggda system
Demonstrationsövning 4 27
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 */
}}
Main
Programmering av inbyggda system
Demonstrationsövning 4 28
THE END!
Programmering av inbyggda system
Demonstrationsövning 4 29
Anekdot: 90tal. MS-Dos, Intel 486 16Mb RAM, 66MHz, VGA grafik adapter.
void set_vga_mode_320x200() { /* x86 assembler */_asm {
mov ah, 0mov al, 0x13 int 0x10 }
}
Programmering av inbyggda system
Demonstrationsövning 4 30
320
200
1 byte per pixel → 64000 bytes totalt 1 byte per pixel → 256 olika färger per pixel
Grafik minne
Programmering av inbyggda system
Demonstrationsövning 4 31
64000Bytes i minnet från address 0xA0000000 bestämmer vadsom visas på skärmen av VGA adaptern.
unsigned char *vga = (unsigned char *)0xA0000000;
Programmering av inbyggda system
Demonstrationsövning 4 32
320
200
x
y
Pixel position i minnes ”array” = 320 * y + x
Programmering av inbyggda system
Demonstrationsövning 4 33
VGA set_pixel
void set_pixel(int x, int y, unsigned char color) {vga[320 * y + x] = color;
}
Top Related