ВУЗ: Не указан

Категория: Не указан

Дисциплина: Не указана

Добавлен: 14.06.2025

Просмотров: 485

Скачиваний: 0

ВНИМАНИЕ! Если данный файл нарушает Ваши авторские права, то обязательно сообщите нам.

The computer uses a base 2 or binary number system because it only has 2 Logic states to work with. If we only have the numbers 1 and 0 to use, it would seem obvious that after we count 0 then 1, we have to start adding more numbers to the left. If we count from 0 to 15 this is how it would look in decimal and binary.

Now you may be able to see how a computer can represent numbers from 0 to 15.

To do this it would need at least 4 switches because the number 15 represented in Binary is 4 digits long. Another way to say that, is the number is 4 binary digits long. The term binary digits is often abbreviated to bits, hence our binary number is now 4 bits long, or just 4 bits. This table emphasises the bit count. Remember, 0 bits represent a logic 0 value so we should not leave them out.

Decimal

Binary

0

0

1

1

2

10

3

11

4

100

5

101

6

110

7

111

8

1000

9

1001

10

1010

11

1011

12

1100

13

1101

14

1110

15

1111

Decimal

Binary

0

0000

1

0001

2

0010

3

0011

4

0100

5

0101

6

0110

7

0111

8

1000

9

1001

10

1010

11

1011

12

1100

13

1101

14

1110

15

1111

The hex number system is Base 16, that is you count 16 numbers before adding an extra digit to the left. This is illustrated in the table below.

Notice how letters A - F are used after the number 9

Perhaps you can see why we only counted up to the number 15. This is a nice even 4 bit number and in computer terms this is called a nibble.

Decimal

Binary

Hex

0

0000

0

1

0001

1

2

0010

2

3

0011

3

4

0100

4

5

0101

5

6

0110

6

7

0111

7

8

1000

8

9

1001

9

10

1010

A

11

1011

B

12

1100

C

13

1101

D

14

1110

E

15

1111

F

DIY K81 Project - Page 10


If you look at the data sheet for the PIC 16F84A, you will see that it is an 8 bit device. That means it can only deal with binary numbers that are 8 bits wide. This is how 8 bit numbers are represented.

Looking at this table, you can see that an 8 bit binary number can have a maximum value of

255 in decimal or FF in hex. 8 bit binary numbers are called bytes.

Our binary numbers can get quite large, and as they do so they will get more complicated and harder to understand. We are not computers, but we want to understand what we write for them, so with the help of an assembler we can use decimal, binary and hex numbers in our software.

Have a look at a 32 bit binary number.

10001100101000001000110010100000

Decimal

Binary

Hex

0

00000000

00

1

00000001

01

2

00000010

02

3

00000011

03

4

00000100

04

5

00000101

05

6

00000110

06

7

00000111

07

8

00001000

08

9

00001001

09

10

00001010

0A

11

00001011

0B

12

00001100

0C

13

00001101

0D

14

00001110

0E

15

00001111

0F

16

00010000

10

17

00010001

11

18

00010010

12

- -

- -

- -

252

11111100

FC

253

11111101

FD

254

11111110

FE

255

11111111

FF

It’s not hard to see why hex and decimal are easier to use. Imagine a 64 bit number and larger still. These are common in today’s computers and are sometimes used with PIC’s.

Lets try to see what this value equals.

First off, split the number into bytes.

10001100

10100000

10001100

10100000

Looking Easier - Now split these into nibbles.

1000 1100 1010 0000 1000 1100 1010 0000

Even easier - Now convert these into hex. This may be difficult at first, but persevere. After awhile you will be able to convert binary to hex and vice versa quite easily.

8

C

A

0

8

C

A

0

Combine the hex numbers for our result.

8CA08CA0

DIY K81 Project - Page 11


To show that we are talking in hex values we put in a little (h) after the number like this.

8CA08CA0h

If you are dealing with PIC programming and are working with a PIC assembler then hex numbers that begin with a letter should be preceded by 0x.

0xFF45AC 0xA786097

You can also use this notation if the value starts with a number.

0x8CA08CA0 0x000011

Lets convert that hex number back to decimal again.

8

C

A

0

8

C

A

0

Remember in decimal, each value in the columns increases by a power of 10 as we move to the left, and in binary they increase by a power of 2. In hex, as you may have guessed, they increase by a power of 16. Be like me if you wish to, cheat and use a calculator to convert between the three number systems, it is much easier. In fact having a calculator that does these conversions is a very good investment for a programmer.

Lets start from right to left and convert each individual hex number to decimal.

Hex

Decimal

Multiplier

Calculate

Result

0

0

1

0

x 1

0

A

10

16

10

x 16

160

C

12

256

12

x 256

3,072

8

8

4,096

8

x 4,096

32,768

0

0

65,536

0

x 65,536

0

A

10

1,048,576

10

x 1,048,576

10,485,760

C

12

16,777,216

12

x 16,777,216

201,326,592

8

8

268,435,456

8

x 268,435,456

2,147,483,648

Add up the last column and we get a total of 2,359,332,000.

Therefore:

10001100101000001000110010100000 = 8CA08CA0h = 2,359,332,000.

Isn’t it lucky that we don’t have to think like computers.

DIY K81 Project - Page 12


The PIC can only deal with individual 8 bit numbers, but as your programming skills increase and depending on your software requirements, you will eventually need to know how to make it work with larger numbers.

When you combine 2 bytes together, the binary number becomes a word.

Remember:

Break large problems into many smaller ones because the overall problem will be much easier to solve. This is when planning becomes important.

Well then, just how do we create a hex file like this.

The answer is - by using an Assembler.

The Assembler

Assembler is easy - easy - easy.

An assembler is quite an ingenious piece of software because it can read a text file that we have written as our program and turn it into a data file similar to the sample above.

We can actually write our programs by collecting all the hex values that our program will use, and then create a data file ourselves. The trouble with that idea is that it is a very tedious task and it would be terribly painful to try and find errors in it especially in large projects.

The program data file that we saw earlier is just a list of hex numbers. Most of these numbers represent the program instructions and any data that these instructions need to work with.

To combine data and instructions together, the PIC uses a special binary number that is 14 bits wide. If you look at the data sheet, you will see that the PIC 16F84A has 1024 14 bit words available for program storage. In computer language 1024 means 1K, so this PIC has 1K of program space.

Assembler language enables us to write code in such a way that we can understand and write it easily.

DIY K81 Project - Page 13

Some people do not want to write code in assembler because they think it is too hard to learn, and writing code in a language such as BASIC is much easier. This can be true, but sometimes you cannot write tight and fast code with these higher level languages and this may not be the best course of action for your particular project.

This is especially so with PIC’s because there are only 35 instructions to work with. Sometimes you will get into difficulty with assembler such as solving problems with multiply and divide, but these routines are freely available from many sources including the internet. Once you have them, it is usually only a simple matter of pasting them into your code if needed. Mostly, you will not even care how they work, but it is a good exercise to learn the techniques involved because it builds up your own individual knowledge.

Saving special code routines in a directory on your PC can be very productive and this is termed a Code Library.

Writing code for an assembler is quite easy, but as with most things there are a few things to learn.

First off, the code you are writing for the assembler is calledSource Code. After the assembler has assembled the source code successfully, it generates another file that is called Object Code. This is the hex data file we saw earlier.

When you use the assembler program, there are some options that you can enable or disable such as, generating an error file, case sensitivity in your source code and a few others. Mostly you can ignore these and leave the default settings, but when you get more advanced at programming, you may like to change them to make the assembler work more suitably for your particular needs.

The assembler must be able to understand every line of source code that you write or it will generate an error. If this happens, the object code will not be created and you will not be able to program a chip.

Each line of code in the source file can contain some or all of these following types of information. The order and position of these items on each line is also important.

Labels

Mnemonics

Operands

Comments

DIY K81 Project - Page 14


Labels must start in the left most column of the text editor.

Mnemonics can start in column two and any other past this point.

Operands come after the mnemonic and are usually separated by a space.

Comments can be on any line usually after an operand, but they can also take up an entire line and are followed by the semi colon character (;).

One or more spaces must separate the labels, mnemonics and operands. Some operands are separated by a comma.

Here is a sample of the text that an assembler expects.

Title "Simple Program"

list p=16f84A

; processor type

;

;-------------

;PROGRAM START

;-------------

org 0h

; startup

address = 0000

start

movlw 0x04

;

simple code

movwf 0x06

goto start

;

do this

loop forever

end

You can see the label called start in column 1. Then comes a tab or space(s) to separate the label from the mnemonic movlw. Then comes another tab or space(s) to separate the mnemonic from the operand 0x04. Finally, you can see the comment which comes after the semi colon ; simple code

Lets have a look at his a bit closer.

The first line has a Title directive.

This is a predefined part of the assemblers internal language and lets you define a name for your source code listing.

The next line has a list assembler directive and tells the assembler what type of processor it is assembling for - in this case, a 16F84A chip. This feature can be helpful if you write too much code for this particular processor to use.

DIY K81 Project - Page 15

The assembler will check how much code it has assembled and let you know if it is too much to fit in that particular chip. Directives like these control how the assembler builds the final object code.

The next lines are just comments put there to help you the programmer know what is going on.

;

;-------------

;PROGRAM START

;-------------

It is vital that you get into the habit of writing comments all over your code. This will help you understand what you have written especially when you come back to read it another time. Believe me, it is very easy to get confused trying to follow the meaning of your code if there is no explanation on how it works. Notice how comments begin with a semi colon (;). Any line that begins with one of these is ignored.

Org 0h is another directive that tells the assembler to set the current memory address where the following instructions will be placed into. Remember that the

16F84A has 1024 location available for code use.Org 0h tells the assembler to start loading the instructions starting from memory address 0. In other words the movlw 0x00 instruction will occupy memory location 0 and the movwf 0x05 instruction will occupy memory location 1. goto start will occupy memory location 2.

One thing you may have missed here, is the fact that memory addresses start from location 0, not location 1. The code memory space is actually from 0 to 1023, not from 1 to 1024. Remember 0 is a valid binary number.

It is probably best to use the correct terminology now to refer to code memory locations as ROM addresses - Read Only Memory.

goto start is an instruction mnemonic followed by a label name. The assembler knows that the instruction start movlw 0x00 is at ROM address

0, so when it sees the instruction goto start, it will generate code to tell the

PIC’s processor to goto ROM address 0 to fetch the next instruction.

So what happens if there is no label called start anywhere in the source listing?

The assembler will complain that it cannot find the label, generate an error and will not complete the assembly process. Usually these errors are written to a separate error file and in a listing file as well. These files have the same name as your source file but with *.err and *.lst extensions.

DIY K81 Project - Page 16