ClubEnsayos.com - Ensayos de Calidad, Tareas y Monografias
Buscar

Curso De Curso_assembler_pic_892.doc

jazb7716 de Mayo de 2014

46.903 Palabras (188 Páginas)242 Visitas

Página 1 de 188

My First PIC Projects

An introduction to the PIC processor.

© Bubble Software 2000

mICro’s First Projects

Introduction Flash That LED Define The Problem Writing The Software Mnemonics The Assembler Labels Using a text assembler The Flowchart MicroPlan Assembler The Simulator The Real World 7 segment LED display The Counter Using Switches

Introduction

Welcome to MICRO’s. I know you are itching to get started with your new software and begin programming PICs as soon as possible, so this introduction will get you familiar with the MICRO’s suite of software and during the process you will also start writing some small programs and hopefully get them working in the real world.

If you have the MICRO’s Experimenters Kit, then going through the projects will be quite easy. If you do not have the Experimenters Kit, then you may like to get a PIC programmer so that you can program the software into a 16F84 chip to complete the projects.

Before we get going, you have to understand that a PIC, or any other microcontroller chip for that matter, is just a piece of silicon wrapped in plastic with pins sticking out to connect it to the outside world. It does not have any brains, nor can it think for itself, so anything the chip does is the direct result of our intelligence and imagination. Sometimes you may get the feeling that these things are alive and are put here to torment your every waking minute, but this is usually due to bugs in your software, not a personality inside the chip. So please remember:

The PIC will always do what you tell it to, not necessarily what you want it to.

One other thing that can cause problems is in the way you handle the chip itself. Your body is more than likely charged with Static Electricity and is usually the zap you feel when you touch a metal object after walking on nylon carpet or similar. The PIC’s most definitely do not like this high voltage discharging into them. It can destroy the functionality of the chip either totally or partially, so always try to avoid touching the pins with your fingers.

The PIC 16F84 data sheet is available in PDF format on the CD ROM in the Acrobat directory.

Flash That LED

This would have to be the universal number one project for new PIC programmers. If you have had anything to do with writing software for PC’s, then it would be the equivalent of writing “hello world” on the monitor for the first time.

You might be thinking at this stage...“What a boring project. I want to create a robot that does amazing things, not mess around with silly ‘hello world’ or LED flash programs.”

Patience my friend. Things like that will come in due course, and as the old saying goes, “You have to crawl before you can walk”.

OK then, so how do we get started?

You might be tempted to jump straight in and write volumes of code right from the start, but I can only say, that in all probability, your software will not work. Now this might sound a bit tedious, but “planning” is the best way to begin any piece of new software. Believe me, in the long run, your code will stand a much better chance of working and it will save you valuable time. Other benefits are that your code will be structured and documented much better, which means you can read through and understand it more easily in the future if the need arises.

So just how do we get this piece of silicon to do our bidding? In this case - flash a LED.

Fundamentally, the PIC needs three things to make it work.

1) 5 volt power source.

2) Clock source

3) Software

The 5 volt supply is there to power the chip. The clock source gives the chip the ability to process instructions. The software is a list of instructions that we create. The PIC will follow these to the letter with no exceptions, so we must make sure that they are written correctly or our program will not work as intended.

Define the problem

To begin planning we must first define the LED flash problem that is going to be solved by using a PIC. This is the physical needs of the project. You can’t write reams of software without knowing what the PIC is going to control. You may find that you need to alter hardware and software as you progress, but don’t be discouraged. This is normal for a lot of projects and is called ‘Prototyping’.

We can start this discussion by saying that we must have a voltage source connected to the LED to make it light. Usually we put a resistor in series with the LED to limit the current through it to a safe level and in most LED’s the maximum current is about 20mA.

Quite obviously, if the PIC is going to turn the LED on and off for us, then the LED must be connected to one of it’s pins. These pins can be set as inputs or as outputs and when they are set as outputs we can make each individual pin have 5 volts connected or 0 volts connected by writing either a Logic 1 or a Logic 0 to them. We can now define this by saying we set a pin as an output high or as an output low.

When a pin is an output high it will have 5 volts connected to it and is able to source 20mA of current. When a pin is an output low it will have 0 volts connected to it and can sink 25mA of current.

A red LED will consume about 2 volts across it when it is being used. We know that an output pin will have 5 volts connected to it, so that means the series resistor needs to consume the remaining 3 volts. 5V - 2V = 3V. By using Ohms law we can calculate the value of the resistor which must drop 3 volts with 3mA of current flowing through it and the LED.

V = I R or R = V / I R = 3 / 0.003

Therefore R = 1000 ohms, or 1K.

Our circuit so far is a 1K ohm resistor in series with a red LED.

Which pin are we going to use to drive this LED? On the 16F84 there are 13 pins available for us to use and these are divided into 2 Ports.

PortA has 5 pins which are numbered RA0 - RA4. PortB has 8 pins which are numbered RB0 - RB7.

At this stage you might think that we can use any one of these, and you would be right - except for one thing. Pin RA4 is an open collector, which means that it can only connect a pin to 0 volts not 5 volts. Therefore our LED would not turn on if the LED was connected between this pin and ground. It would need to be connected between this pin and 5 volts.

There are lots of little hidden “gotcha’s” that exist in the world of microcontrollers and the best way of knowing about them is by close examination of the data book. You will remember most of these tricks after you become familiar with a particular chip, but even the most experienced programmers can get caught with these problems sometimes.

Even though we can use any pin to drive our LED circuit, for this example we will use the open collector pin RA4. We are going to use this pin because it highlights the fact that it operates differently from all the others.

To summarise our project so far, we are going to flash a red LED in series with a 1K ohm resistor from PortA pin RA4. RA4 can only sink current to ground, so the LED cathode is connected to this pin via a 1K ohm resistor. As you may already know, a LED can only work if the Cathode is more negative than the Anode, so the Anode side of the LED will be connected to the 5 volt supply.

You can easily tell the cathode from the anode. The cathode pin will have a flat surface moulded next to it into the red plastic, and the anode pin is longer than the cathode pin.

Now that we have our LED circuit figured out, the next stage is to write the software that will flash it for us.

To find out more about how the PIC Port Pins work, click on the blue link to run the program called MicroPort. This is quite a lengthy program, so at this stage you may like to continue instead.

Look at the file called program.pdf to see how to build the Experimenters Kit. These files are located in the same directory where the software was installed. You can click on the blue links to activate them.

Writing The Software

There are quite a few ways to create software for the PIC. You can write it with a simple text editor, or use MPLAB from Microchip, a Basic compiler such as MicroBasic, or you can use the assembler called MicroPlan.

The PIC doesn’t care what method you use to write the software because it only understands raw HEX code which is placed into the chip by using an appropriate programmer.

This is a sample of HEX code.

:1000000008308500003086008316173085003E30AA

:10001000860083128F0190018221900B0C283330CF

:1000200091003030920034309300413094005821D8

:100030000F1817284C300E060319362856300E06B6

It’s not very meaningful is it, but that does not matter to us. We are not computers, so it is not in our best interests to understand what this data means. What does interest us as programmers, is just how do we create a data listing like this.

The answer is - by using an Assembler.

This is quite an ingenious piece of software because it can read a text file that we have created for 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.

HEX numbers may be new to you so it will be best to have a quick look at them. At some stage, you may need to use them in your programs as well as Decimal and Binary numbers.

Number Systems

...

Descargar como (para miembros actualizados) txt (250 Kb)
Leer 187 páginas más »
Disponible sólo en Clubensayos.com