GPIO, In- och utmatning Demonstration och konsultation ST407 GPIO konfigurering och användning Arbetsbokens avsnitt 4 och 5 LCD ASCII-display Keyboard Introduktion till laboration 2 Demonstrationsövning 3 1
Dessa OH bilder listar den kod som konstruerades på DEMO 3 (Och nära relaterad kringkod). Demonstrationsövning 3 2
Keyboard kopplad till Port D. Demonstrationsövning 3 3
Definitioner: Namnger de olika minnes mappade kontroll registren för portkontroll. #define PORT_D_BASE 0x40020C00 #define portd_moder ((volatile uint32_t *) (PORT_D_BASE)) #define portd_otyper ((volatile unsigned short *) (PORT_D_BASE+0x4)) #define portd_ospeedr ((volatile unsigned int *) (PORT_D_BASE+0x8)) #define portd_pupdr ((volatile unsigned int *) (PORT_D_BASE+0xC)) #define portd_idrlow ((volatile unsigned char *) (PORT_D_BASE+0x10)) #define portd_idrhigh ((volatile unsigned char *) (PORT_D_BASE+0x11)) #define portd_odrlow ((volatile unsigned char *) (PORT_D_BASE+0x14)) #define portd_odrhigh ((volatile unsigned char *) (PORT_D_BASE+0x15)) Demonstrationsövning 3 4
Konfigurering av port Moder: 0x55005555 Pupdr: 0x00AA0000 Otyper: 0x00000000 (in/ut) (input pull-down) (ut push-pull) Demonstrationsövning 3 5
I C ser denna konfigurering ut såhär: *portd_moder = 0x55005555; *portd_pupdr = 0x00AA0000; /* Input, pull down */ *portd_otyper = 0x00000000; /* outputs are push/pull */ Detta är del av init_app funktionen. Demonstrationsövning 3 6
Översättning av (rad,col) till knappvärde unsigned char keyb(void) { unsigned char key[]={1,2,3,0xa,4,5,6,0xb,7,8,9,0xc,0xe,0,0xf,0xd; int row, col; for (row=1; row <=4 ; row++ ) { kbdactivate( row ); if( (col = kbdgetcol () ) ){ return key [4*(row-1)+(col-1) ]; *portd_odrhigh = 0; return 0xFF; Demonstrationsövning 3 7
void kbdactivate( unsigned int row ){ switch( row ) { case 1: *portd_odrhigh = 0x10 ; break; case 2: *portd_odrhigh = 0x20 ; break; case 3: *portd_odrhigh = 0x40 ; break; case 4: *portd_odrhigh = 0x80 ; break; case 0: *portd_odrhigh = 0x00; break; int kbdgetcol ( void ){ unsigned short c; c = *portd_idrhigh; if ( c & 0x8 ) return 4; if ( c & 0x4 ) return 3; if ( c & 0x2 ) return 2; if ( c & 0x1 ) return 1; return 0; Demonstrationsövning 3 8
Output på 7-segment display Demonstrationsövning 3 9
void out7seg( unsigned char c ){ static unsigned char segcode[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07, 0x7F,0x67,0x77,0x7C,0x39,0x5E,0x79,0x71 ; if( c > 15){ *portd_odrlow = 0; return; *portd_odrlow = segcode[c]; Demonstrationsövning 3 10
ASCII Display Demonstrationsövning 3 11
#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 portidrlow ((volatile unsigned char *)(PORT_DISPLAY_BASE+0x10)) #define portidrhigh ((volatile unsigned char *)(PORT_DISPLAY_BASE+0x10+1)) #define portodrlow ((volatile unsigned char *)(PORT_DISPLAY_BASE+0x14)) #define portodrhigh ((volatile unsigned char *)(PORT_DISPLAY_BASE+0x14+1)) Demonstrationsövning 3 12
Intressanta bit-positioner #define B_E 0x40 /* enable */ #define B_SELECT 4 /* välj ascii/lcd */ #define B_RW 2 /* read/write */ #define B_RS 1 /* register select */ Demonstrationsövning 3 13
Sätta/rensa bit void ascii_ctrl_bit_set( unsigned char x ){ unsigned char c; c = *portodrlow; c = ( B_SELECT x ); *portodrlow = c; void ascii_ctrl_bit_clear( unsigned char x ){ unsigned char c; c = *portodrlow; c &= ( B_SELECT ~x ); *portodrlow = c; Demonstrationsövning 3 14
ASCII wait ready void ascii_wait_ready(void){ while( ( ascii_read_status() & 0x80)== 0x80 ); delay_micro( 8 ); Demonstrationsövning 3 15
Skriv tecken på ASCII display void ascii_write_char( unsigned char c){ ascii_wait_ready(); ascii_write_data( c ); delay_micro( 39 ); Demonstrationsövning 3 16
ASCII gotoxy void ascii_gotoxy( unsigned char x, unsigned char y) { unsigned char address; if(y!=1) address=0x40 (x-1); else address=x-1; ascii_write_cmd( 0x80 address); Demonstrationsövning 3 17
Initiera display void ascii_init(){ ascii_wait_ready(); ascii_write_cmd( 0x38 ); /* Function set */ delay_micro( 39 ); ascii_wait_ready(); ascii_write_cmd( 0x0C ); /* Display on */ delay_micro( 39 ); ascii_wait_ready(); ascii_write_cmd( 1 ); /* Clear display */ delay_milli( 2 ); ascii_wait_ready(); ascii_write_cmd( 6 ); /* Entry mode set */ delay_micro( 39 ); Demonstrationsövning 3 18
Main int main(int argc, char **argv){ unsigned char *s ; unsigned char test1[] = "Alfanumerisk "; unsigned char test2[] = "Display - test"; // unsigned char test1[] = "xxxxxxxxxxxxxxxxxxxx"; // unsigned char test2[] = "iiiiiiiiiiiiiiiiiiii"; init_app(); ascii_init(); ascii_gotoxy(1,1); s = test1; while( *s ) ascii_write_char( *s++ ); /* skriv tecken tills tecken är 0 */ ascii_gotoxy(1,2); s = test2; while( *s ) ascii_write_char( *s++ ); return 0; Demonstrationsövning 3 19