Getting Started with the TExaS Simulator



[pic]10.2. Arrays

array is made of elements of equal precision

precision is the size of each element

length is the number of elements

origin is the index of the first element.

zero-origin indexing the origin is zero

const char data[4]={0x05,0x06,0x0A,0x09};

const puts it in ROM

In assembly, we define

data fcb $05,$06,$0A,$09

where it is positioned relative to org puts in in ROM

[pic]

Figure 10.1. A byte array with 4 elements.

| org $3800 |unsigned char index=0; |

|index rmb 1 | |

|org $4000 | |

|step ldab index |void step(void){ |

|ldx #data |PORTB = data[index]; |

|ldaa B,x | |

|staa PORTB | |

|incb |index=0x03&(index+1); |

|andb #$03 | |

|stab index | |

|rts |} |

Program 10.3. Assembly code to access a byte array.

Consider a constant word array, shown in Figure 10.2, and defined by the following C code.

const short powers[5]=

{1,10,100,1000,10000};

In assembly, we define a word constant using fdb

powers fdb 1,10,100,1000,10000

[pic]

Figure 10.2. A word array with 5 elements.

If I is the index and Base is the base address of the array, then the address of the element at I is

Base+2*I

|power lslb |unsigned short power( |

|ldx #powers |unsigned char exp){ |

|ldd B,x |return powers[exp]; |

|rts |} |

Program 10.4. Code to access a word array.

If length varies dynamically.

• saves the length of the array as the first element.

• use termination code

const char data[5]=

{4,0x05,0x06,0x0A,0x09};

const short powers[6]=

{5,1,10,100,1000,10000};

We could define these variable length arrays in assembly as

data fcb 4,$05,$06,$0A,$09

powers fdb 5,1,10,100,1000,10000

|ASCII |code |name |

|NUL |$00 |null |

|ETX |$03 |end of text |

|EOT |$04 |end of transmission |

|LF |$0A |line feed |

|FF |$0C |form feed |

|CR |$0D |carriage return |

|ETB |$17 |end of transmission block |

|ESC |$1B |escape |

Table 10.1. Typical termination codes

The following array has a null-termination.

char data[9]=

{0x05,0x06,0x0A,0x09,0,0,0,0,0};

no const puts it in RAM

In assembly, we define

data fcb $05,$06,$0A,$09,0,0,0,0,0,0

where it is positioned relative to org puts it in RAM

We can use the termination code to determine when to reset the index back to zero.

| org $3800 |unsigned char index=0; |

|index rmb 1 | |

|org $4000 | |

|step ldab index |void step(void){ |

|ldx #data |PORTB = data[index]; |

|ldaa B,x | |

|staa PORTB | |

|incb |index++; |

|tst B,x |if(data[index] == 0) |

|bne ok | |

|clrb |index = 0; |

|ok stab index | |

|rts |} |

Program 10.5. Assembly code to access a variable length byte array.

The stepper motor can be changed from full steps to half steps simply by changing the array, without modification to the program code. In particular, all we need to do is change the values to:

char data[9]={0x05,0x04,0x06,

0x02,0x0A,0x08,0x09,0x01,0};

In assembly, we define

data fcb $05,$04,$06,$02,$0A,$08,$09,$01,0

6.5. LCD interfacing

The IO->LCD… command allows you to connect an integrated liquid crystal display (LCD)

PA0 means PA3,PA2,PA1,PA0

PB4 means PB7,PB6,PB5,PB4

[pic]

Figure 6.10. Dialog box for interfacing a simple LCD display.

Each of the 4 connections has a specific function

1) sign, ddd point field specifies 4 bits used for the

sign 1 for minus, 0 for plus

.ddd 1 for decimal point in the leftmost position

d.dd 1 for decimal point between left and middle digits

dd.d 1 for decimal point between middle and right digits

If this field is left blank there will be no sign or points

2) most signficant digit specifies 4 bits used for leftmost digit

0,1,2,3,4,5,6,7,8,9,A,b,C,d,E,F

If blank there will be no most signficant digit

3) middle digit specifies 4 bits used for the middle digit

0,1,2,3,4,5,6,7,8,9,A,b,C,d,E,F

If blank there will be no middle digit

4) least significant digit specifies 4 bits used for rightmost digit

0,1,2,3,4,5,6,7,8,9,A,b,C,d,E,F

If blank there will be no least significant digit

[pic]

Figure 6.11. Equivalent circuit for each digit of LCD display.

[pic]

|4 bit value |LCD digit |

|0000 |0 |

|0001 |1 |

|… |… |

|1001 |9 |

|1010 |A |

|… |… |

|1111 |F |

[pic]

|sign |display |

|0000 |+123 |

|0001 |+12.3 |

|0010 |+1.23 |

|0100 |+.123 |

|1000 |-123 |

|1001 |-12.3 |

|1010 |-1.23 |

|1100 |-.123 |

Table 6.5. LCD display examples.

[pic]

Data be an unsigned 16-bit integer, and a range of 0 to 999. Let digit1, digit2, digit3 be 4-bit decimal digits

1) Do an integer divide by 100 getting quotient and remainder

digit1 = data/100 and r = data%100

digit1 is an integer 0 to 9, r is an integer 0 to 99.

2) output digit1

3) Divide r by 10 getting another quotient and remainder

digit2 = r/10 and digit3 = r%10

4) output (digit2 ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download