Presentation is loading. Please wait.

Presentation is loading. Please wait.

C Programming Interfacing C to Hardware. 68040 LCD Interface Code /* define X and Y PIT registers on bus slot D */ #define PIT0xFFF58300 #define XPGCR(*

Similar presentations


Presentation on theme: "C Programming Interfacing C to Hardware. 68040 LCD Interface Code /* define X and Y PIT registers on bus slot D */ #define PIT0xFFF58300 #define XPGCR(*"— Presentation transcript:

1 C Programming Interfacing C to Hardware

2 68040 LCD Interface Code /* define X and Y PIT registers on bus slot D */ #define PIT0xFFF58300 #define XPGCR(* (volatile char *) (PIT+0x01)) #define XPADDR(* (volatile char *) (PIT+0x05)) #define XPBDDR(* (volatile char *) (PIT+0x07)) #define XPACR(* (volatile char *) (PIT+0x0D)) #define XPBCR(* (volatile char *) (PIT+0x0F)) #define XTCR(* (volatile char *) (PIT+0x21)) #define XTIVR(* (volatile char *) (PIT+0x23)) #define XCPRH(* (volatile char *) (PIT+0x27)) #define XCPRM(* (volatile char *) (PIT+0x29)) #define XCPRL(* (volatile char *) (PIT+0x2B)) #define XTSR(* (volatile char *) (PIT+0x35)) #define XPORTA(* (volatile char *) (PIT+0x11)) #define XPORTB(* (volatile char *) (PIT+0x13)) #define YPGCR(* (volatile char *) (PIT+0x41)) #define YPADDR(* (volatile char *) (PIT+0x45)) #define YPACR(* (volatile char *) (PIT+0x4D)) #define YPORTA(* (volatile char *) (PIT+0x51))

3 LCD Delay Code /* define time delay */ #define DTIME0xFF /* Time delay */ void delay(t) int t; { register int i; for (i = t; i > 0; i--) {;} }

4 Delay Code Analysis zTime delay y25 MHz 68040 => 40 ns clock cycle y1-2 clock cycles/instruction yLoop of 2 instructions => 80-160 ns/loop y256 iterations => 20-41 us delay zUse timer interrupt instead? yNot for short delays xinterrupt latency ~10 us, context switch ~1 ms yReally want handshaking, big I/O buffer

5 LCD Write Routines void lcd_controlwrite(c) char c; { XPORTB = c; /* c on data lines */ XPORTA = 0x01; /* Enable clock high, write control reg */ delay(DTIME); XPORTA = 0x00; /* Enable clock low */ delay(DTIME); } void lcd_datawrite(c) char c; { XPORTB = c; /* c on data lines */ XPORTA = 0x03; /* E clock high, write data reg */ delay(DTIME); XPORTA = 0x02; /* negate E clock */ delay(DTIME); }

6 Write Routine Analysis zMust obey setup and hold times in timing specification zConstant delay reduces performance yNot really an issue for application yBut ties up CPU cycles zAlternative yOne write routine, specify data or control in argument ylcd_write(c,reg)

7 LCD Write Timing Diagram

8 Initial PIT Port void xport_init(void) { XPGCR = 0x00; /* Mode 0, disable H12/H34, H1-4 active low */ XPADDR = 0xFF; /* XPORTA direction is output */ XPBDDR = 0xFF; /* XPORTB direction is output */ XPACR = 0xF0; /* Submode 1X, H2 negated, no interrupt */ XPBCR = 0xF0; /* Submode 1X, H2 negated, no interrupt */ }

9 LCD Initialization void lcd_init(void) { XPORTA = 0x00; /* Set LCD enable low */ lcd_controlwrite(0x01); /* clear display */ delay(DTIME); /* extra delay for clear */ lcd_controlwrite(0x38); /* func set, 8 bits, 1 line, 5x7 */; lcd_controlwrite(0x06); /* entry mode, inc cursor, shift */ lcd_controlwrite(0x0F); /* display on, cursor on, blink on */ }

10 Initialization Analysis zNicer interface ideas yDefine constants instead of bit vectors xOne for each command field xCombine together in initialization routine yPass as flags to initialization routine

11 Home and Write /* home display */ void lcd_home(void) { lcd_controlwrite(0x02); } /* Write text string to LCD */ void writestring(s) char *s; { while (*s != '\0') { lcd_datawrite(*s++); }

12 Analysis zCould have home as argument to a command write rather than separate function zChar is 8 bits, so should be able to write non-ASCII characters yLCD can do other character sets

13 Main Routine /* Initialize the X Port, initialize the LCD, write * "HELLO!!!!!!", home cursor */ * Note only first 6 characters print, so "HELLO!!!” * * Note main is type int as required by compiler */ int main(void) { xport_init(); lcd_init(); writestring("HELLO!!!!!!"); lcd_home(); }

14 Summary zCan easily write modular LCD interface yPort initialization yLCD initialization yData interface yCommand interface

15 Attention to Data Types zUser must specify data types zE.g. must use 8-bit data with 8-bit device zTypes yshort int, int, long int - signed 8, 16, 32 bits xlength depends on architecture and compiler ychar, unsigned it, - unsigned 8, 16 bits yfloat, double - floating point 32, 64 bits

16 Access Types zVolatile yTells compiler that data should not be kept in register, e.g. LCD status register zNormally not an issue if appropriate define statements are used, so variable access always causes memory access y#define STAT (volatile char *) 0xFFFF0000 yY = *STAT;

17 Pointers vs. Values zUse I/O register addresses as pointers y#define STAT (volatile char *) 0xFFFF0000 ySTAT points to byte at 0xFFFF0000 zRead/write by dereferencing pointer yY = *STAT; y*STAT = Y;

18 Indirect Addressing #define STAT (volatile char *) 0xFFFF0000 register char *sptr; /* address register */ sptr = STAT; Y = *sptr; /* indirect addressing */ Sptr++; /* increment to next byte */

19 I/O Device in Structure struct IO_dev { char funct; /* function code */ char *control; /* control reg addr */ char *readdata; /* data reg addr */ char input_char; /* input destination */ char error_stat; /* error destiation */ } zStructure offsets overlay I/O register locations

20 ACIA Example int main(void) { register char input; struct IO_dev ACIA; struct IO_dev *PA = &ACIA; PA->control = (char *) 0x8000; PA->readdata = (char *) 0x8002; PA->function = 1; Get_data(PA); input = PA->input_char; }

21 Analysis zStructure is allocated in memory by compiler zInitialization sets field values to point to I/O register locations zAdvantage yCan easily handle several of same type of I/O device in system


Download ppt "C Programming Interfacing C to Hardware. 68040 LCD Interface Code /* define X and Y PIT registers on bus slot D */ #define PIT0xFFF58300 #define XPGCR(*"

Similar presentations


Ads by Google