sensor

23
Easy PIC-III PIC Microcontroller Trainer for Beginners Amer Iqbal Qureshi User Manual Microtronics Pakistan | www.electronicspk.com

Upload: -

Post on 29-Nov-2014

220 views

Category:

Documents


11 download

TRANSCRIPT

Page 1: Sensor

Easy PIC-III PIC Microcontroller Trainer for Beginners

Amer Iqbal Qureshi

User Manual

Microtronics Pakistan | www.electronicspk.com

Page 2: Sensor

Easy PIC-III Microcontrollers and other programmable electronic devices are the future of electronics and therefore technology. You will hardly come across a product that now uses discreet components only to implement a desired logic. Indeed discreet components are used only to supplement the functionality of microcontrol-lers. With the advent of flash based microcontrollers it has now become increasingly easy and affordable to learn and explore these incredibly powerful devices.

Making a microcontroller based project is fairly easy, as you concentrate only on given part of the function-ality, yet to learn the device properly you need to experiment with most commonly encountered scenarios. These include LCD, switches, analog sensors, sound, and serial communication etc. Not only that but also the programming basics like concepts of variables, loops, interrupts, timers conditional execution using IF..Then ..Else etc. are equally important.

Easy PIC-III has been designed to address most of these issues. It has a reasonably broad range of periph-eral devices on board. It supports most 18 pin PIC microcontrollers that are pin compatible from Microchip. These microcontrollers are nevertheless less powerful, but only limited in number of I-O Lines . Once you have mastered them you can move on to more feature enriched microcontrollers. Practically a given project if well targeted does not require too many I-O Lines.

Features • 18 Pin PIC Microcontroller

• HD44780 based 16x2 character LCD interface

• 4 LED indicators

• 1 channel analog input

• DC motor / relay controller (100ma)

• 38KHz modulated infra red remote controller sensor

• 2 push switches

• PIZO Buzzer

• Serial Port with RS232 Level converter and DB-9 Connector

• On board 5V 1A power regulator

• In circuit serial programming Interface (ICSP)

• JDM type In Circuit Serial Programmer

Package Contents: • Easy PIC-III Motherboard

• 16x2 character LCD with Backlight (optional)

• LM-35 temperature sensor (optional with 16F819)

• Serial Cable (optional)

• JDM Programmer

• Software:

• Proton Basic Lite version (Supports 16F628A)

• PIC PGM Programmer software

Page 3: Sensor

An Overview of the Hardware

Figure 1 shows arrangement and location of various devices on Easy PIC-III board. In order to use these devices in your program one needs to know exactly where they are connected on the microcontroller. All I-O lines are usually general purpose lines and can be used to connect the devices the way you want in your project. However some lines have dedicated internal hardware attached to them, so if you want to use that functionality the particular device must be connected to the specified pins. This board does not use any dedicated hardware so the devices have been connected to Microcontroller solely on the basis of ease to draw circuit. Serial communication however has been connected to special I-O lines, we shall talk about it in appropriate section.

18 PIN PIC Microcontroller Microchip® makes microcontrollers in different sizes in terms of number of I-O lines. Since not all projects require too many lines we have a choice of microcontrollers with 8, 14,18,20,28,40 and even 100 or more lines. The difference is only in capacity to control many or less devices. We have chosen 18 pin microcon-trollers for this board that gives reasonably good number of lines.

By 18 pin we mean total number of pins on the integrated circuit. Out of these 18 pins two are for power supply (VSS=5V and GND=0V), one line is for Master Clear (MCLR) which is for reset as well as used to put the microcontroller in programming mode to accept a new program. Two additional lines are for Oscil-lator circuit (20 MHz Crystal) in this case. Some microcontrollers also have somewhat slower internal os-

Serial Communication Connector MAX 232 Push Switches

Infra Red Sensor

ICSP

Analog Input

FAN / DC Load 4 LED Indicators Buzzer

Buzzer Enable Jumper

LCD Connector

Fig. 1 Layout of Devices

Microcontroller

Page 4: Sensor

cillator. So in your projects you can configure the microcontroller to use that oscillator and then you can use those two pins as I-O lines as well. This board however does not use those pins, as I-O and they are tied to crystal oscillator.

When talking about conventional integrated circuits we usually talk about pin numbers. This is not so in terms of microcontrollers. Indeed we talk about a particular pin by its name. this makes it easier to switch from a smaller microcontroller to bigger or vice versa. Thus if in our design and program we are talking about RB0 for example, and this RB0 can be pin 6 on 1 microcontroller and it can be pin 27 on another. As long as we talk about RB0 in our program we do not need to think which pin number it will be on actual processor.

The usual way to talk about I-O lines is by their port name (an internal register connected to lines) and its bit names (individual bits of port connected to individual lines).

This manual is not intended to be a tutorial on PIC microcontrollers, so we shall not go into too many tech-nical details. The objective is to introduce the board and how to use it.

Figure 2 shows the typical arrangement of pins on an 18 pin PIC microcontroller. You will come across a large number of PIC microcontrollers with 18 pins, all of them usually have this configuration. Therefore they are called pin-compatible.

A few may differ so it is always a good idea to consult the datasheet before using. So what is the fun in making a large variety with similar pin counts? Well these microcontrollers differ in terms of internal memory size and various hardware based devices incorporated inside. Like analog channels, Pulse width modulation cannels USART etc. The price also depends upon the memory size and number of devices incor-porated into the processor itself.

So on Easy PIC-III you can use any 18 pin microcontroller, as microcontroller has been mounted on a base, it can be un-plugged and new one inserted.

Most people use 16F84 as an 18-pin microcontroller to learn. This microcontroller has been superseded by better proces-sors like 16F628A, 16F819 or 16f88 etc. We will use 16F819 on this board because of one main reason! It has analog chan-nels inside. Most data in real world exists as analog like tem-perature, pressure, vibration, light etc. in order to interface with this data we have to have an analog to digital con-verter.16F819 has 5, 10-bit resolution ADCs inside it. We have used one only on this board so that you can learn the basis of using analog data in your applications.

The general purpose input and output channels are named as PORTS. 18 pin PIC microcontrollers have two PORTS, called PORTA and PORTB. Individual bits of these ports are named as RB0, RB1, RB3…. And so on.

We have 8 bits available on PORTB named from RB0..RB7 and 5 on PORTA named RA0..RA4. In future we shall refer to them by these names only. MCLR pin is kept HIGH (close to 5V) when we want the pro-gram inside the microcontroller to execute. When this pin is connected to ground (LOW) for a short time the processor gets reset and starts the program again. We have not implemented reset circuitry in this board rather a fixed 10K resistor connects this pin to 5V supply to always keep the pin high.

The same MCLR pin is also used to inform the processor that we want to upload a new program. This is done automatically by a device called programmer. The programmer gives 12V on this pin to halt process-ing of existing program and to put the microcontroller into ‘Program’ mode.

The two pins OSC1 and OSC2 have been connected with 20MHz Crystal oscillator. The faster is this oscil-lator faster is program execution however it also consumes more power. 20MHz is maximum 16F series can go. In you applications where battery operation is required and very fast processing is not an issue use a

Fig-2 Pin Names on 18 Pin PIC Microcontrollers

Page 5: Sensor

lower frequency oscillator like 4MHz. This will prolong the battery life. The 16F819 has 2K flash program memory. Although in this world of gigabytes 2K memory looks very small, yet it is enough for most com-monly developed applications and programs. Certainly this is a small one-chip computer its not going to run windows !

Power Supply 5V regulated DC power supply is the industry standard. However newer versions of surface mounted mi-crocontrollers are being produced that require 3.3V. In our case these DIP type processors run on 5V sup-ply. Easy PIC-III makes it simple to accept 9-12V supply from a battery or adapter and converts it into regulated ripple free 5V supply. It uses 7805 1A 5V regulator IC In addition the external 0.1uf decoupling capacitors provide additional ripple free supply to allow smooth running.

LED Indicators (RB4, RB5, RB6, RB7) Four LED indicators have been placed on board. These are connected through 220 Ohms current limiting resistors to RB4..RB7. Therefore if the corresponding port pin is made high the attached LED will glow. LED on RB7 has been connected through 1.5K resistor instead of 220 as this pin is also used for in circuit programming and attached LED would steal the signals. This LED will therefore glow a little dim.

The LEDs actually indicate some other device in your actual project. This may be re-lay, a motor a solenoid or anything. The objective here is to learn how to turn these devices ON and OFF based upon some logic. In real world project the microcontroller I/O lines will then turn the base of a tran-sistor ON/OFF and in turn drive the heavy load like relay etc.

Nevertheless LEDs are also used in projects to indicate status of some process, so these LEDs can be used for that as well. You can experiment various ways to control, the LEDs. These also indicate providing some sort of pulses to the next stage circuit, you can make them blink indicating pulses to the output stage.

LCD Module (RB0, RB3, RB4,RB5,RB6,RB7) The LCD module itself contains a microcontroller to control various characters to be displayed. This is commonly used in projects to communicate with the user. Learning this part teaches how one microcontrol-ler can communicate with another to achieve a desirable effect.

Although this communication is quite complex and needs quite a bit of understanding of the way your LCD works, we have provided an LCD with HD44780 controller. This is most commonly used and almost universally available display. Therefore most high level compilers like Proton Basic, MikroBasic, Mikro C etc already contain predefined libraries to address this display. Therefore using this display is not difficult with these compilers.

LCD module needs two sets of connections: 1: data connections and 2: Control Connections

Data connections again can be 8 bit wide or 4 bit wide, and control connections need a register select and other enable control lines. You can connect the LCD to any lies you want in your project, however you need to inform the compiler as to what are your connections so that it can generate appropriate signals.

Easy PIC-III uses 4 bit data mode, and the data pins are connected as DB4, DB5,DB6, DB7 of LCD to RB4, RB5,RB6 and RB7 of microcontroller. These are the same lines where LEDs are also connected. So you can either use LCD or LEDs. However with some clever techniques of programming you can use both.

The Register Select (RS pin of LCD) is connected to RB3 and enable to RB0 of microcontroller. RB0 and RB3 are not connected anywhere else so they can be easily used for LCD controlling.

You will come across various designs on internet using this display, they use a variable resistor to control the contrast, in order to reduce the complexity of circuit we have simply provided contrast through 1.5K resistor to contrast pin of LCD. Therefore there is no need to adjust contrast if appropriate power is being

Page 6: Sensor

applied.

Serial Port Communication (RB1,RB2) Serial communication is one of the most time honored communication protocols used fro a long time. Al-though serial ports are rapidly disappearing from desktop computers they are still commonly used in indus-try and many other equipment. In order to work with this kit you will in any case need a computer with at least one serial port. When talking microcontroller to microcontroller, the transmit pin of one is connected to receiver of other and vice versa. The data is transmitted using TTL level of transmission, that is 0V or 5V however the serial port of computer and many other devices use a different standard. Like –12V for logic 1 and +12V for logic 0. Therefore to communicate with such devices we need a level conversion chip that is connected on one side to microcontroller and on other side through a suitable connector to PC. This chip MAX232 will convert PCs –12V and +12V data to logical 0V and 5V and also convert reverse from Microcontroller to PC.

Although lot has been written about serial com-munication we shall use the simplest form. It uses two lines of Microcontroller one called Rx (receiver) and other (Tx) transmitter and of course the ground. These lines have been con-nected to RB1 and RB2.

Hardware USART: Since serial communication is so commonly used most microcontrollers have in built module to handle its delicacies. The corre-sponding Tx and Rx pins are labeled on micro-controller. In case of 18 pin PIC microcontrollers they are always RB2 and RB3. However certain microcontrollers like 16F819 lack this internal module so the communication protocols have to be ad-dressed through software. When software is used to control serial communication protocols you can use any two I-O lines. 16F628A has this module therefore we can take its advantage.

What difference does it make: Well if a hardware module is present the job is mostly handled by it and soft-ware overhead is reduced a lot. Using software to handle all this increases program size and therefore re-duces space for other useful code.

For our purpose it will not be difficult as this is such a common scenario, that most compilers provide two libraries for USART communication. One will use the hardware module (and therefore generate smaller code) the other will handle the things itself.

In many projects you need to communicate with two or more devices. So you can connect one device to hardware module and other to software implemented pins. So its important to learn both.

Analog Input (RA0 0—5V) Most of the real world data to which microcontrollers need to interact exists in analog form. Most sensors give us the output in terms of voltage. For example Microphone gives sound levels in terms of varying volts, may it be in milli-volts but the data is analog not digital. Similarly temperature, pressure, height and light etc all can be used as input into a control system, but the data is analog. Microprocessor can process of digital data. So one of the commonly required process is to acquire data and convert it into digital format. There are dedicated integrated circuits for that called ADC (Analog to Digital Converter). Many microcon-trollers have this functionality built into the microcontroller, and a few lack this. So the choice of microcon-troller for a given project will be affected if we need analog channel or not. The lines used are same as for digital connections. However using software internal registers are set if we want a particular line to use ana-log input or digital input.

Different PIC microcontrollers vary in number of analog channels they have. 16F819 has 5 ADC channels, named as AN0..AN4 and they are all mapped on RA0..RA4 pins. We have used AN0 connected to RA0 pin

Page 7: Sensor

Of the microcontroller. The input has been implemented as a header so that you can connect any source of analog input to it. The center pin of header is data and connected to AN0 (RA0) pin. The other two pins are +5V and GND. GND will any case be required to con-nect, +5V can be used to power the analog input device.

Beware the 5V supply is from main board and if connected to power supply of analog device having more than 5V they will reach the board and damage it. So be sure of you analog source first.

The tolerance of analog input is also upto a max of 5V thus your analog data must lie within this range. In case your sensor is giving more volts you can use appropriate resistors to reduce the volts and compensate for this decrease in your software.

LM35 / LM34 Temperature sensors

We have provided LM35 or LM34 temperature sensors as source of analog data. The LM35 gives tempera-ture in centigrade while LM34 in Fahrenheit. Both sensors can work on 5V supply. The output volts corre-spond to measured temperature. There is a linear response of 10mv change per degree change in tempera-ture. So using this kit you can easily make temperature measuring device, temperature logger etc.

Fan Control & Buzzer (RA1 High:Fan Low:Buzzer) Pin RA1 is used to control both Fan output and buzzer, therefore only one can be used at a time. The buzzer therefore has a jumper which disconnects it from the pin. The buzzer has built in 6V circuit to produce sound (1KHz) so we have connected its positive to 5V supply and negative (via jumper) to pin RA1. Thus when pin RA1 will be taken low (0V) current will pass through buzzer from 5V to RA1 (sinking current) and buzzer will beep.

The fan controller is simple one transistor switch the base of which is con-nected to RA1 and emitter to GND. The Collector is connected through a header to Power supply directly from the source. It therefore bypasses the 5V power and the Fan will get Full 9V or 12V supply depending upon the adapter volts. Keep it in mind that this current will flow through a simple transistor (NPN BC548) that can tolerate a max of 500mA (see its data sheet). So you can not drive really big loads here. We have provided brushless DC Fan that use almost 50-100ma and is suitable for demonstration purpose. This a logic 1 or making RA1 high will turn the load ON. You can also use Pulse width modula-tions to experiment how to increase or decrease the speed of FAN using digital systems (PWM).

Push Switches (RA2, RA3 Active Low) Push switches are commonly used in microcontroller applications to get user response. Depending upon the nature project there can be No, 1 or many switches. Sometimes switches are arranged in the form of a matrix to form what is called keypad. We have not included keypad in this board due to limited I-O lines but included two push switches. You can use programming logic to use even a single switch to affect many activities. However for beginner it is best to use one switch for one activity. The switches are connected to RA2 and RA3 lines. Each of these lines is also tied to +5V using 10K resistor (called Pulled up) the switch is connected to ground. Thus when the switch is not pressed the cor-responding line will be High (due to pull up resistor) when the switch is pressed it connects ground to line and therefore the microcontroller gets Low on IO line when switch is pressed. This is called active low. No doubt you can connect them other way round in your projects, but it is important to know which lines to test and what will be the state of line when switch is being pressed.

Page 8: Sensor

Infra Red Remote Sensor Infra red light is commonly used to transmit data among microcontrollers. The common ex-ample is TV remote control. Infra red sensors and transmitters come as LEDs so the produce analog data by lowering their resistance when infra red light falls on them. With a suitable transistor / OP Amp circuit this can be con-verted into digital. However infra red light is so abundant in our environment that false data can be received. To counter act this 38KHz modulation stan-dard was developed. All TV, CD remote controls produce Infra red signals as bursts of 38KHz pulses. So small bursts of 38KHz IR signals indicate logic 1. The corresponding sensor will respond only to this frequency IR signals, and therefore avoid environmental noise. Moreover the sensor has built in cir-cuitry to convert analog data into digital. So what we get is a Low signal on its output when 38KHz IR signal is received. To decode the data sent is a hectic task, as every manufacturer has its own standards. And coding is in terms of time duration of pulses rather than bytes of data. SONY IR remote controls are very popular as they have simple design and logic. Fortunately Proton Basic now has a library to decode the SONY remote controls.

Therefore with this you get a complete keypad as well as number of other switches that you can make your kit response.

Summary of Connections

Device IO Lines Used Comments

LED Indicators RB4..RB7 MCU Sourcing Current

LCD Data: RB4..RB7 Ctrl: RB0, RB3

USART RB1, RB2

Buzzer RA1 Active Low

Motor RA1 Active High

Analog AN0 (RA1) Max 5V

IR Sensor RA4 38 KHz Modulated

Switches RA2..RA3 Active Low

Page 9: Sensor

Setting up the Hardware Well now after going through a basic overview of your board its time to set the things into action. Basically you have to set two things. 1: The hardware connections and 2: Install appropriate software.

Setting up your hardware is not difficult. Take out the programmer, and serial cable.

1: Connect The serial cable to your PC. Your PC must have a dedicated serial port. This pro-grammer will not work with USB to serial con-verters. In case your PC does not have serial port, or you want to work with Laptops you will need to buy our USB based PICkit-2 Program-mer.

2: Attach the serial cable to the programmer.

3: Attach the 6+6 wire connector cable to the programmer output. Note the Pin Named MCLR on programmer output. This pin will be con-nected to Pin named VPP on In Circuit pro-grammer header.

As you attach the header from programmer to board, the power light on board should turn ON. Even though you have not plugged in the bat-tery. This indicates correct placement of header as well as correct placement of serial cable. However this power is not enough to support the board and you will need to plug in the 9V battery or adapter to power the board properly.

3: Once the basic connectivity has been tested remove either the serial cable from programmer, or the header cable, so that the board does not get anything from programmer.

Now to setup the software.

You will need to setup two pieces of software on your computer.

1: A Compiler that will accept your program, in English like language and compile it to processor under-standable file called hex file. The hex file has an extension .hex and this is the target file that needs to be transferred into the microcontroller.

2: a suitable programmer software that can work with our JDM programmer. A number of programming software are available for free to download from internet. They all does the same basic job, but differ in number of microcontrollers supported and other tools. Most people use ICPROG or PICPGM. We would show you here how to setup PIC PGM programmer as it has two very good features. 1: It automatically de-tects the presence of programmer and serial port on which its connected as well as automatically detects the microcontroller. Although it is limited in number of controllers supported, yet its good for most of the com-monly used controllers. It has support for both 16f819, 16f628A and 16f84 etc.

PIC PGM is available in two configurations,1: windows based, 2: Command line although using command line is more versatile, as you can set your compiler to automatically call command line utility, but it be-comes little tedious for a beginner to set all this. So we shall use the windows based utility.

Open the software folder on CD and then open the PICPGM folder. Unzip the WinPICPGM file and run the winpicpgm.exe executable file. After formal dialog boxes it will install the PIC PGM software and create a desktop icon for it.

Now plug in the hardware as previously described, serial cable to programmer and programmer to ICSP header on board. Turn the power of board ON. You may keep the LCD inserted or remove if you want. It is always better to keep it un-plugged if your project is not using it.

Now run the PICPGM software by clicking the desktop icon. The red LED on programmer should blink for a while indicating activity and the PICPGM screen should pop-up. The screen should show that your pro-grammer is connected and detected. It should also detect the microcontroller on your board. If these two

MCLR Pin

Page 10: Sensor

things are OK, you can proceed further otherwise check the whole thing again.

The above screen shot shows how it should look. Note the areas where Programmer and microcontroller have been automatically detected. Note the Button on toolbar that will be used to activate program transfer into the microcontroller. Also note the Select hex file section where you will select the .hex file to be trans-ferred. Note if you have selected a .hex file and transferred it, you do not need to close the PICPgm when you recompile the program to generate a newer version of .hex, when you press the program button it will automatically detect that hex file already loaded and one on file do not match so it will load the new one automatically. Alternately if you close it and run again, it will load the new file automatically. However if file name has been changed you have to browse for the new .hex.

Compiler A number of programming languages and compilers are available. You can chose any of your choice. Com-monly used are Proton Basic, MikroBasic, MikroC, MPLAB Assembler, Hi-techC, CCS-C, JAL and so on.

Most of these have free to download demo versions with some limitations available. We shall use PRO-TON BASIC for this demonstration. Note Free to download PROTON BASIC supports 16F628A but not 16F819. Since most of the examples can be run on 16F628A the free version is OK. However if you want to work with analog data you need to work with 16F819 that will require Full version of PROTON BASIC.

MikroBASIC from (www.mikroe.com) on the other hand also gives demo version free to download. It sup-ports all microcontrollers, but limit is code size of 2K. This is enough for beginner. So you can work with analog section using MikroBASIC, or if you are comfortable work entirely in MikroBASIC. MikroBASIC however does not has library to decode the IR remote control codes, rest of it is fine.

We supply Easy PIC-III in two configurations:

1: Standard Configuration, with 16F628A (includes Free Version of Proton Basic, and does not include LM35 sensor)

2: Extended Configuration, with 16F819 (includes Full version of Proton Basic and LM35 Temperature sensor)

Depending upon your purchase install the Proton Basic Compiler. The exact method will be written in the software folder. Once Compiler has been installed, and configured your Integrated development environ-ment should look like this.

Programmer detected COM Port where Pro-grammer is Connected

PIC Automatically Detected

Program Button

Browse and select the .hex file to be pro-grammed

Page 11: Sensor

The Proton Compiler has two parts, an IDE (Integrated development environment) the one which is shown in this screen shot and an actual compiler that runs in background when IDE instructs to compile. When free version is installed this is set automatically, but when full version will be installed you will have to tell the IDE where to find the compiler. This is done by clicking view > Compiler and program options.

As you can see we have already pointed it to the folder containing our compiler files. If you need to set it use Find Manually button and locate the appropriate folder.

To setup the Programmer is optional. As you have already setup your PICPGM you will have to load it every time a new hex file is generated and needs to be transferred. Although not a big issue but it will be more convenient if we get PICPGM popup right from the Proton IDE. To do that click on programmer tab.

Page 12: Sensor

When you firs time run, it has no idea about your programmer indeed it believes in its own. So click over Install New Programmer.

The list does not have our particular Hardware (JDM) listed. So we need to create a new custom program entry. Select Create a Custom Program entry option and click Next.

In the display name choose anything you want, this is just a name that will be displayed in list of program-mers. Now click Next.

In programmer Filename textbox enter WinPicPGM.exe as shown. And click next

Page 13: Sensor

Now you have to locate the folder where PICPGM was installed. Select Find Manually and locate the folder.

As you can see here that program folder has been selected.

The parameters list can pass the programmer name of .hex file. If you leave this empty PICGM will load and ask you to select the hex file. If you want the entire thing to go automatic enter $hex-filename$ here.

Click Finished. Now your Programmer has been set to load from within the Proton Basic IDE.

Writing Your First test program Well now is the time to write a first ‘Hello World’ program. This is the usual program most people do to get a response sort of thing to see if everything works. In Microcontroller world this usually means turning LED’s ON. Since our LEDs are connected to PORTB pins RB4..RB7 turning these pins high will light up the LEDs.

So in ProtonIDE click File > new and a new program entry editor will open the left pan will show the vari-ous registers of chosen microcontroller (16f628A by default) and other variables that you will be declaring

Page 14: Sensor

in your program. The right pane is empty where you can write your program. So write this program. Device=16F819 'use 16f628 if you have that processor Xtal = 20 All_Digital true Output PORTB PORTB=255 End

Notice as you type various keywords the IDE highlights them to show that they are commands (Bold Black) Declares (Bold red) or PORT Names (Bold Green)

Also notice that the processor name is in bold. This all gets automatically. If the processor is not supported by your compiler this will not get bold and left pane (code explorer) will not show the processor name.

So in this program we have declared that PORTB will act as output and set its value to 255 that will turn all pins of PORTB high including ones on which LEDs are connected.

Now lets compile the program. To be on the safer side click a small arrow on side of the program button on toolbar and make sure your newly created programmer entry is selected.

Make sure your hardware is connected and powered on.

Now click ‘Compile and Program’ Button this will bring up compiler and then automatically load PIC PGM. Note if the program has not been saved before it will ask you to save it first. So create a folder in some accessible location (better not on desktop or my documents) as the compiler can not work on long paths. I prefer to create a folder named ‘PICProjects’ in D:\ and then create subfolders for each project.

Lets say we name this program Hello it will be saved as Hello.bas (this is your source file). The compiler will generate a number of intermediate files, and finally a .hex file , Hello.hex in our case.

When the PICPGM shows up click browse to locate your hello.hex. Now click program button of PICPGM and the new program should be transferred into the microcontroller.

After the program has been successfully transferred, disconnect the board from programmer. Turn the power OFF and ON again and your LEDs should be ON. See screen shots next.

Page 15: Sensor

See Microtronics PICPGM is in list and selected.

Compiler invoked and compiling the application

Page 16: Sensor

After Compilation

Notice I had set the $hex-filename$ parameter so my .hex file has been automatically loaded and pro-grammed.

Now turn power off and disconnect programmer. Turn Power ON and All 4 LEDs should be ON.

If you got this result, congratulations you have successfully created a complete cycle of editing and pro-gramming the software into microcontroller.

Now lets say we want to modify the same program to blink these LEDs.

So back to PROTON IDE and modify the program like this. Device=16F819 'use 16f628 if you have that processor Xtal = 20 All_Digital true Output PORTB loop: PORTB=255 DelayMS 500 PORTB=0 DelayMS 200 GoTo loop End

This time I will not show you the screenshots, follow the same steps, Connect hardware, turn power ON, and click ‘Compile and Program’ when the new program has been successfully transferred turn power off disconnect programmer, turn Power on and you should see blinking LEDs.

Page 17: Sensor

Example Projects In this section we shall develop small projects to give you an idea how different devices on Easy PIC-III can be used. This section only serves as a guide as to how these devices should be used. This section does not act as a tutorial to programming PIC microcontrollers.

Using LED Indicators Four LED indicators are provided on board. They have been connected to RB4..RB7 of PORTB. The posi-tive side of LEDs is connected through a current limiting resistor to microcontroller pin and negative side has been grounded.

This example declares friendly name LED1, LED2 etc to port pins PORTB.4, PORTB.5 etc. the sequen-tially makes them high and low with a delay of 500 ms in between.

Using LCD LCD is the most commonly used output device in projects for communication with the user. The LCD con-nections in Easy PIC-III use 6 I-O lines. The data is transmitted through RB4..RB7 to D4..D7 pins of LCD

Register Select (RS ) pin of LCD is connected to PORTB.3 and Enable (E) pin of LCD is connected to PORTB.0.

We need to inform this information to the compiler so that it can generate signals on appropriate lines of

Device=16F819 'use 16f628 if you have that processor Xtal = 20 All_Digital true Output PORTB Symbol LED1 = PORTB.4 Symbol LED2 = PORTB.5 Symbol LED3 = PORTB.6 Symbol LED4 = PORTB.7 PORTB=0 ' Turn entire portb low AA: High LED1 DelayMS 500 Low LED1 High LED2 DelayMS 500 Low LED2 High LED3 DelayMS 500 Low LED3 High LED4 DelayMS 500 Low LED4 GoTo AA

Page 18: Sensor

microcontroller.

This program first declared the LCD connections that this board is using, then displays a message on line 1, “hello world”

It then runs a loop on variable X, varying its value from 0 to 100. each time it changes the value of X it is displayed on LCD. To show you how you can display variables on display.

You can see how dramatically Proton Basic has reduced the headache of programmer (you) to display data on LCD.

Using Buzzer Buzzer is connected to PORTA.1 and it will be activated when PORTA.1 is Low. Make sure that the Jumper near Buzzer has been connected to enable it.

This command will turn the buzzer ON for half a second and then turn it Off. A more useful command called sound is present in Proton, you can try it to produce varying sounds. However this command works best with simple pizo, or a speaker. This buzzer has in built circuit for 1KHz, so its not ideal for making varying frequency sound. You can however make various notes by enableing and disabling it for various timings.

Device=16F819 Xtal=20 All_Digital true 'LCD Declarations LCD_DTPin PORTB.4 ' Lcd data starts at portb.4 LCD_RSPin PORTB.3 LCD_ENPin PORTB.0 Dim x As Byte ' Just a variable to hold some value Loop: Cls ' clear lcd and initialize it Print At 1,1, "Hello World" For x=0 To 100 Print At 2,1,"X = ", Dec x DelayMS 1000 ' wait for 1 second Next x GoTo Loop

Device=16F819 Xtal=20 All_Digital true Output PORTA.1 Symbol Buzzer PORTA.1 Loop: Low Buzzer DelayMS 500 High Buzzer DelayMS 1000 GoTo Loop

Page 19: Sensor

Motor Control Truly speaking this is not a motor control, but a simple DC load control. It uses one NPN transistor to pro-vide ground for the load. The other end of load is directly connected to positive of adapter. Thus if 12V adapter is being used the DC load will operate at 12V. The NPN transistor can drive a maximum load of 100-200 mA. This is enough for a brushless DC Fan, or a small relay. If you are using a relay, make sure you place a diode across its Ground and positive connections of coil. Because when the current is switched ON and OFF the magnetic recoil produces a high spike that can damage the transistor.

Since DC Load control is also connected to PORTA.1 it is recommended to take the buzzer jumper off. The DC load however gets activated when the PORTA.1 pin is high.

Analog Input Analog input connected to PORTA.0 can be used only with microcontrollers containing ADC channels. 16F819 has this channel, whereas 16F628A does not have it. Notice in previous examples we have been declaring All_Digital True statement. This indicates that we want to use Analog input pins in digital mode. So when we want to use these pins as analog we do not issue this statement. However not issueing this statement means all portA pins will be analog, and we can not perform any digital I-O on these pins. There-fore we have to configure a few internal registers in 16F819 to select which pins are analog and what others are digital.

Device=16F819 Xtal=20 All_Digital true Output PORTA.1 Symbol FAN PORTA.1 Loop: High FAN ' Turn Fan ON DelayMS 5000 'keep it on for 5 seconds Low FAN ' Turn Fan Off DelayMS 2000 'Keep it Off for 2 seconds GoTo Loop

Device=16F819 Xtal=20 'LCD Declarations LCD_DTPin PORTB.4 ' Lcd data starts at portb.4 LCD_RSPin PORTB.3 LCD_ENPin PORTB.0 Declare Adin_Res 10 ' Select 10 bit resolution Declare Adin_Tad FRC ' Internal RC oscoilator for timing of analog samling Declare Adin_Stime 50 ' Allow 50 uS time for conversion ADCON1=%10001110 ' Select AN0 as analog and all others as digital Input PORTA.0 ' Accept analog data on Porta.0 Dim x As Word ' variable to accept input 10 bit data Dim y As Float ' to hold actual volts Cls Print At 1,1, "Analog data" DelayMS 2000 Cls Loop: x=ADIn 0 Cls

Page 20: Sensor

Apart from that there are few settings that need to be done to configure the analog module, like in what resolution we want the data to be converted. A 10 bit resolution means, it will break the 0-5V data into 1024 steps. So at full 5.0 V input the output register will give us a value of 1023. thus each division or in-crement represents (5/1023=0.004887 V) thus the input register value multiplied by this number will give us the exact input volts. You can use any Input source, a simple method is to take a variable resistor like POT, and connect to the input header. You can measure the input volts at center pin, exactly same volts must be shown on LCD Display.

Infra Red Remote Control Sensor Many electronic devices use infra-red remote controls to command the processor, like to increase the vol-ume, change channel etc. These infra red remote controls send data at 38KHz modulation. The sensor on Easy PIC-III board is optimized to sense this data only. So its output pin, connected to PORTA.2 will go low when such a signal is detected.

Detection of signal is not the end of story. Next we have to figure out which key was pressed on remote control. This is quite difficult because there is no universal standard and all manufacturers have their own protocols. Fortunately Proton Basic has built-in function to decode the Philips and Sony remote controls.

In this example we shall use the Sony remote control and show the data on LCD.

The SonyIn command returns a 16 bit number. The number has two parts a high byte and a low byte. The High Byte contains a code for the type of equipment for which this remote control is made, like TV, VCR,

Print At 1,1,"Raw:", Dec x y=(5/1023) * x Print At 2,1,"Volts:", Dec y DelayMS 1000 GoTo Loop

Device=16F819 Xtal=20 'LCD Declarations LCD_DTPin PORTB.4 ' Lcd data starts at portb.4 LCD_RSPin PORTB.3 LCD_ENPin PORTB.0 All_Digital true Sonyin_Pin PORTA.2 ' PORTA.2 is connected to IR Sensor Dim SONYIN_WORD As Word ' Create a WORD variable to receive the SIRC data Dim SONY_COMMAND As SONYIN_WORD.LowByte ' Alias the COMMAND byte to SONYIN_WORD low byte Dim SONY_SYSTEM As SONYIN_WORD.HighByte ' Alias the COMMAND byte to SONYIN_WORD high byte Cls Print "Waiting for" loop: Repeat SONYIN_WORD=SonyIn 'get remote control data Until SONY_COMMAND <> 255 Cls Print At 1,1,"System:", Dec SONY_SYSTEM Print At 2,1,"Command:", Dec SONY_COMMAND GoTo loop

Page 21: Sensor

etc. and Low Byte contains the actual command or code of the button that has been pressed.

If no key is being pressed, the Command byte gives 255. Therefore a loop is placed to continuously read the IR sensor, until a valid data is received. Once the data has been received into variables, you can per-form any task depending upon the data.

Push Buttons Two push buttons have been provided they give a logic ‘0’ when pressed to the accompanying pin of mi-crocontroller. To read their state simple IF statement can be used.

Serial Communication (USART) Universal Serial Asynchronous Receive and Transmit. This has been the industry standard for communica-tion among devices. It uses a single wire to transmit and other to receive data. While microcontroller ex-pects this data to be at TTL levels, i.e 5V is logic ‘1’ and 0V is logic ‘0’. This is OK if you want communi-cation between microcontrollers directly. However PC and certain other devices use 12V and –12V as stan-dards of communication. These voltage levels therefore must be translated to TTL levels. This is done by MAX232 chip on Easy PIC-III board. So once you have programmed the Microcontroller, just connect the serial cable to DB-9 connector on Easy PIC-III board. Turn the power OFF. And run a suitable hypertemi-nal program. Proton Basic has built in program. Just Press F4 in Proton and Serial communicator window will POP up. Set the Baud Rate to 9600, and chose the COM port where your cable is connected, usually this is COM1.

Press the connect button on communicator. The two panel window will appear. The top panel is for sending data to microcontroller, and bottom panel is what microcontroller is sending.

Our first program will only receive “Hello World” message from microcontroller.

Note the serial communication module is different in 16F628A and therefore its program will be slightly different. Here we are writing the program for 16F819. 16f628A has built-in module that makes communi-cation quite easy and simple. 16F819 does not have built-in USART module, therefore entire logic to com-municate is built using software. Fortunately Proton Basic and many other languages have built-in libraries to handle this job.

Device=16F819 Xtal=20 All_Digital true Symbol SW1 = PORTA.3 Symbol SW2 = PORTA.4 Symbol LED1= PORTB.4 Symbol LED2= PORTB.5 Input PORTA.3 Input PORTA.4 Output PORTB.4 Output PORTB.5 Loop: If SW1= 0 Then Toggle LED1 DelayMS 200 EndIf If SW2=0 Then Toggle LED2 DelayMS 200 EndIf GoTo Loop

Page 22: Sensor

Device=16F819 Xtal=20 All_Digital true Serial_Baud 9600 Rsout_Pin PORTB.2 Rsin_Pin PORTB.1 Rsout_Mode TRUE Loop: RSOut "Hello World",10,13 DelayMS 1000 GoTo Loop

Page 23: Sensor

You can also write custom programs in VB or Visual C etc to communicate with microcontroller using se-rial communication.

After this basic overall introduction you are now ready to dig deeply through more complete programming references.