Back to basics...

This chapter is about exploring basic tutorials. As a beginner, these are the very first steps you should experiment and fully understand before going further. As an advanced user, these tutorials are also here to help you testing new chips, or... when things go wrong and you can't figure out why, going back to basics

Don't worry, everything is gonna be alright...

Blink A Led (Your First Project)

In this tutorial we are going to learn how to connect our first circuit and blink our first led.

Where to we start?

Let’s make a led blink on and off, how fun is that!

So, you’ve followed the installation guide and now have a Programming language (JALv2) + Libraries (JALLIB) + Editor. We will be using JALEdIt for our first example.

Setup your workspace

Start by getting out your programmer and connect it to your PC. Some connect by serial port, some connect via USB. I actually use a serial port programmer attached to a USB-to-Serial adapter to free up my serial port for other projects.

If you are using a serial port programmer you need to check that you have a regular serial cable and is not a null modem cable. Using your multimeter, check that each pin of your serial cable matches, if pins 7 & 8 are crossed, it is a null modem cable.

Get out your PIC microcontroller (we will now refer to it as a PIC). You can use PIC’s 16f877, 16f877A, 18F2550 , 18F452 or 18F4550 for this project since the port pin outs are the same for all of them. I will use 16f877A for this blink a led project.

Now check PC connectivity to your programmer. Open your programming software on your PC, check the settings within your software to change the serial port number and programmer type (if available). Your programmer software may tell you that your board is connected, if not, put your PIC in your programmer and do some basic tests such as “read chip”, “blank / erase chip”

If you are using Micropro, click on “file” -> “port”, and “file” -> “programmer” -> (your programmer type). If you do not know the programmer type, you will have to guess until Micropro says something like “K149-BC board connected”, Put your PIC in your programmer and choose your PIC type from the “Chip Selector” text box. Now do some basic read/erase tests.

Build your circuit

Well, it looks like we’re all set to go, so grab your breadboard and other components, put together the following circuit:

And here’s what it looks like. Notice the additional orange wire to the left of my PIC, this ensures that I always put my PIC in the correct position after programming. Do not connect your power 5v supply till your circuit is complete and checked over at least twice. You will burn your PIC if power is on while building your circuit. You will want an on/off switch for your power supply.

Your circuit is done, and it looks pretty, but it doesn’t do anything :o(..

Understand the jalv2 directory structure

First take a look at your jalv2 installation directory on your PC, wherever you installed it.

compiler – holds the jalv2.exe compiler program to convert your JAL code to microcontroller hex code

JALEdIt – JAL text editor where you will write your code

lib – A set of libraries to make things work

sample – Working examples.

Create yourself a folder called workspace, and in that folder create a folder called blink_a_led (eg. C:\jalv2\workspace\blink_a_led\)

Setup your editor & .jal file

Open up your favorite text editor. I will use JALEdIt. Run jaledit.exe from the JALEdIt directory. Start a new document, and save it in jalv2\workspace\blink_a_led\ and name it blink_a_led.jal (eg: C:\jalv2\workspace\blink_a_led\blink_a_led.jal)

Let’s write some code

So now we’re going to write the code that will make our led blink. All code will be in highlighted text. You can read more about JAL language usage here: http://www.casadeyork.com/jalv2/language.html

Title & Author Block

Start out by writing a nice title block so everyone know’s who created it. Here’s an example Title block from Rob Hamerling’s working 16f877a_blink.jal blink a led example in the sample directory. Every PIC has at least one working sample. You can see that two dashes “-“ declare a comment so your notes get ignored by the compiler. The character “;” can also be used for comments. We will comment our code as we go along so it is easier for us to read our own code.

-- ------------------------------------------------------
-- Title: Blink-a-led of the Microchip pic16f877a
--
-- Author: Rob Hamerling, Copyright (c) 2008..2009, all rights reserved.
--
-- Adapted-by:
--
-- Compiler: 2.4l
--
-- This file is part of jallib  (http://jallib.googlecode.com)
-- Released under the BSD license (http://www.opensource.org/licenses/bsd-license.php)
--
-- Description:
-- Sample blink-a-led program for Microchip PIC16f877a.
--
-- Sources:
--
-- Notes:
--  - File creation date/time: 14 Oct 2009 20:24:20.
--
-- ------------------------------------------------------

Choose your PIC

Write the following code to choose the PIC you are using, change 16f877a to whatever PIC you have:

include 16f877a                    -- target PICmicro

Choose your crystal speed

Write the following code according to the speed of the crystal you are using in your circuit. I suggest 20mhz for 16f877. You can check your chip’s datasheet for it’s max speed. Higher speeds may not work the way you want them to on a temporary breadboard.

-- This program assumes a 20 MHz resonator or crystal
-- is connected to pins OSC1 and OSC2.
pragma target clock 20_000_000     -- oscillator frequency

Configure PIC Settings

The following code sets some of the PIC’s internal settings, called fuses. A OSC setting of HS tells the PIC there is an external clock or crystal oscillator source. You must disable analog pins with enable_digital_io() , you don’t need to worry about the others.

-- configuration memory settings (fuses)
pragma target OSC  HS              -- HS crystal or resonator
pragma target WDT  disabled        -- no watchdog
pragma target LVP  disabled        -- no Low Voltage Programming
--
enable_digital_io()                -- disable analog I/O (if any)
--

Choose an output pin

Let’s choose an output pin to control our led. As you can see from the circuit, our led is connected to pin #2. Let’s check our datasheet to find the pin name from the pin out diagram.

The PDF datasheet for this PIC and for all others can be downloaded from the microchip website. Here is the datasheet for this PIC http://ww1.microchip.com/downloads/en/DeviceDoc/30292c.pdf , and here is the pin out diagram from the datasheet:

As you can see, we are using the pin RA0/ANO at pin #2. RA0 is the pin name we are looking for. AN0 is another name for this same pin (used in the analog to digital tutorial), but we can ignore it in this tutorial. In the JAL language RA0 is written as pin_A0

Now let’s read the details of this pin in the datasheet on page 10. As you can see RA0 is a TTL Digital I/O pin. We are checking this to make sure it is not a open drain output. Open drain outputs (like pin RA4) require a pull-up resistor from the pin to V+

Now write code for pin A0. We are writing an “alias” only because in the future we can refer to pin 2 (A0) as “led”. This way we no longer need to remember the name of the pin (except for the directional register in the next line of code we will write).

--
-- You may want to change the selected pin:
alias   led      is pin_A0

Configure the pin as an input or output

Now we must tell the PIC if this is an input or an output pin. The directional setting is always named (pin_ + pinname_ + direction). Since we are writing data to the port, to turn the led on, it is an output.

pin_A0_direction =  output

We could make an alias for this as well: “alias led_direction is pin_A0_direction”, then write “led_direction = output”. This way, we can change it from output to input in the middle of the program without knowing the pin name. But in this case, we will only use pin_A0_direction once in our program so there is no need to make an alias.

Write your program

So, now that we have the led under our control, let’s tell it what to do.

We will want our led to continue doing whatever we want it to do forever, so we’ll make a loop

forever loop

It is good practice to indent before each line within the loop for readability. 3 spaces before each line is the standard for Jallib.

In this loop, we will tell the led to turn on.

   led = ON

now have some delay (250ms) a quarter of a second so we can see the led on.

   _usec_delay(250000)

turn the led off again

   led = OFF

and have another delay before turning it back on again

   _usec_delay(250000)

close our loop, when the PIC gets to this location, it will go back to the beginning of the loop

end loop
--

And that’s it for our code. Save your file, It should look something like this:

-- ------------------------------------------------------
-- Title: Blink-a-led of the Microchip pic16f877a
--
-- Author: Rob Hamerling, Copyright (c) 2008..2009, all rights reserved.
--
-- Adapted-by:
--
-- Compiler: 2.4l
--
-- This file is part of jallib  (http://jallib.googlecode.com)
-- Released under the BSD license (http://www.opensource.org/licenses/bsd-license.php)
--
-- Description:
-- Sample blink-a-led program for Microchip PIC16f877a.
--
-- Sources:
--
-- Notes:
--  - File creation date/time: 14 Oct 2009 20:24:20.
--
-- ------------------------------------------------------
--
include 16f877a                    -- target PICmicro
--
-- This program assumes a 20 MHz resonator or crystal
-- is connected to pins OSC1 and OSC2.
pragma target clock 20_000_000     -- oscillator frequency
-- configuration memory settings (fuses)
pragma target OSC  HS              -- HS crystal or resonator
pragma target WDT  disabled        -- no watchdog
pragma target LVP  disabled        -- no Low Voltage Programming
--
enable_digital_io()                -- disable analog I/O (if any)
--
-- You may want to change the selected pin:
alias   led      is pin_A0
pin_A0_direction =  output
--
forever loop
   led = on
   _usec_delay(250000)
   led = off
   _usec_delay(250000)
end loop
--

Compile your code to .hex

Now let’s get this beautiful code onto our PIC. Your PIC cannot understand JAL, but it does understand hex, this is what the compiler is for. The compiler takes people readable code and converts it to code your PIC can understand.

If you are using JALEdIt, click the compile menu at the top and choose compile.

If you are using your own text editor in windows, you will need to open windows command prompt. Click start -> run and type cmd and press OK. Now type (path to compiler) + (path to your .jal file) + (-s) + (path to JALLIB libraries) + (options) Here’s an example:

C:\jalv2\compiler\jalv2.exe "C:\jalv2\workspace\blink_a_led\blink_a_led.jal" -s "C:\jalv2\lib" -no-variable-reuse

The option -no-variable-reuse will use more PIC memory, but will compile faster.

If all this went ok, you will now have a blink_a_led.hex located in the same directory as your blink_a_led.jal, If there where errors or warnings, the compiler will tell you.

A error means the code has an problem and could not generate any .hex file. If there is a warning, the hex file was generated ok and may run on your PIC but the code should be fixed.

Write the hex file to your PIC

Take your PIC out of your circuit and put it in your programmer. With your programming software, open the blink_a_led.hex file. You should see that hex data loaded in your software. Now click the Write button. Your software will tell you when it is done.

Let's Try It

Put your PIC back into your circuit, double check your circuit if you haven’t already, and make sure your PIC is facing the correct direction. Apply power to your circuit.

It’s alive! You should see your led blinking! Congratulations on your first JALv2 + JALLIB circuit!

Here's a youtube video of the result: http://www.youtube.com/watch?v=PYuPZO7isoo

I strongly suggest you do this tutorial next: Serial Port & RS-232 for communication.

Getting Started

Guide to getting started with PIC microcontrollers JALv2 & Jallib

So, you’ve heard all the hype about PIC microcontrollers & JALv2 and want to hear more?

Why use PIC microcontrollers, JALv2, and this book?

Simple usage:

Yes, that’s right, microcontrollers are simple to use with the help of this open source language JAL. Not only are microcontrollers simple to use, but many other complex external hardware is made easy such as: USB, Analog to digital conversion (ADC), serial communication, Hard Disks, SD Cards, LCD displays, sensors and many more.

All you will need is a small amount of knowledge about general electronics. We will teach you the rest you need to know!

If you already know about how to setup and use microcontrollers, I suggest you start with the "Blink a led" tutorial, then read the "Jallib Starters Guide". The starters guide will give you more detailed information about the language JalV2 and Jallib. It also has some more advanced technical information and examples. It can be download from: http://code.google.com/p/jallib/downloads/list

Circuit Simplicity:

Would you like to reduce the size of your circuits? What are you currently using to build your digital circuits?

When I got started, I liked to use things like the 74LS series, simple CMOS gate chips, 555 timers etc. You can build just about anything with these simple chips, but how many will you need to complete your project? One of the projects I built some time ago used five 74ls chips. With a microcontroller, I can now reduce my circuit to 1 microcontroller.

Bigger Projects:

When I say bigger, I mean cooler projects! There is no limit to what you can build! Choose from our small projects to build a large project of your own. What functionality do you need for your project? Check out our tutorial section for a complete list of compatible features you can use for your circuit.

What do I need to get started?

You will need the following:

  1. PIC microcontroller chip
  2. PIC programmer
  3. Programming language (JALv2) + Libraries (JALLIB) + Editor, see our installation guide.
  4. Computer (preferably one with a serial port)
  5. PIC programming / burning software
  6. Regular electronic stuff such as breadboard, resistors, wire, multimeter etc.
  7. Oscilloscope is not required but suggested for some advanced projects.

Follow our Installation Guide for free programming language, libraries & text editor

How much will it cost?

Yes, getting started with microcontrollers has it’s price. A microcontroller can cost you anywhere between $1 to $10 USD, and a programmer will cost $20 to $50. But you can't put a price on FUN!

The programming language JALv2 is FREE, other languages will cost you somewhere between $200 and $2000.

When you compare this price to the price you are currently spending on those many IC’s you currently require to build your circuits, this may be cheaper. You will not need many of your smaller IC’s, and some specialty chips can be replaced. Of course you’re going to save time and breadboard space as well!

As an example... Instead of buying a UART chip for serial communication, you can now use the microcontroller’s internal UART for communication to your PC or other projects.

What PIC microcontroller should I buy?

PIC16F877 or PIC16F877A seem to be the most popular mid-range PIC at the moment (in the image above). You should be able to find them at your local electronics store for around $10. This microcontroller has many features and a good amount of memory. It will be sufficient for most of your projects. We will build our first project on this chip. I warn you however, you may eventually want to move to an 18F PIC for more memory, for example, you can run a SD Card, but you cannot use FAT32. I only suggest 16f877A because it will be easy to find at a store.

There are many low-end PIC’s to choose from, PIC16F84, PIC16F88 are smaller chips for around $5. There are also very low end 8 pin PIC’s such as 12F675 for $1.

If you’re looking for speed, functionality, and a whole lot of memory space, you can go with a PIC18Fxxx chip. I suggest one of the following: 18f452, 18F4620, 18F4550. These PIC’s will also work in our getting started “blink a led” tutorial with the same circuit diagram. If you can, get a 18F PIC. My current favorite is the 40 pin 18f4620.

You will notice that the better 18F series chips are actually cheaper then the outdated 16F chips.

Here’s a price chart from the manufacturer’s sales website:

PIC Price USD
16F877 $5.92
16F877A $5.20
16F84 $5.01
12F88 $1.83
18F675 $1.01
18F452 $4.14
18F4550 $4.47
18F2550 $4.51
18F4620 $4.27

What programmer should I buy?

Any pic programmer will do. The only suggestions I have is to make sure it can program a wide variety of PIC’s such as the ones listed above, and make sure it has a ICSP port for future use. ICSP is for in-circuit programming.

Here are some images of programmers we use:

What editor should I use?

Any text editor is fine, but if you are on a windows machine. We suggest the free editor “JAL Edit” which will highlight & color important text as well as compile your JAL program to a hex file for burning to your microcontroller. If you followed our installation guide, you will already have this editor.

What programming/burning software should I use?

Did your programmer come with software? There are many to choose from so use whatever you prefer. I use “Micropro” from http://www.ozitronics.com/micropro.html. It’s a free, open source software for programming a wide range of PIC’s. However, it will most likely not support your programmer. I suggest you use the software that came with your programmer. You may see this programmer in other tutorials for demonstration only.

OK, enough of this boring stuff, lets build something! Start with the Blink A Led Tutorial.

Serial Port and RS-232 for communication

In this tutorial we are going to learn how use TX & RX pins for serial communication to your PC, and also learn communicate with another PIC or external device via RS-232.

What is a serial port?

You may have forgotten about this important part of history "The serial port". You have forgotten because you have been too up-to-date on all the new technologies such as USB and Bluetooth, but you have left the good old technologies in the past. Well, now it's time to put that funny looking port on the back of your PC to some good use! If you don't have a serial port on your PC, you can get a USB to serial converter/adapter.

At one time, there was a wide range of devices that used the serial port such as a mouse, keyboard, old GPS, modems and other networking.

In our case, we will use a serial port to send data to our PC, or to send data a second PIC. I find it most useful for troubleshooting my code, and for sending other readable information to my PC without the use of additional hardware such as a LCD. LCDs & displays can be an expensive addition to your circuit.

What is RS-232?

RS-232 is the data transfer standard used on serial ports. Basically this is composed of one start bit, some data bits, parity bit, and one or two stop bits. The transfer speed as well as the number of start, stop and data bits must match for both the transmitter and receiver. We will not need to cover the way in which it is transferred since the PIC does it for us. We will only need to know the following:

1. The number of start bits (always 1)

2. The Parity (usually no parity)

3. The number of data bits (usually 8)

4. The number of stop bits (1 or 2)

5. The data transmission speed

6. The port number on your PC

You will be able to choose the transmission speed yourself. The Jallib library we will be using will use 1 start bit, 8 data bits, no parity, and 1 stop bit. Your other device, such as your PC will also need to know this information.

What do I need?

In the first part of this tutorial I will show you how to hook your serial port up to your PC. I will show you how to connect it to another PIC later on in this tutorial. I feel that connectivity to your PC is quite important. You will need:

1. A PIC that has TX and RX Pin names. Most PIC's have them. Check your pinout diagram in the PIC's datasheet.

2. A serial port board. You can buy one on the net, or build your own. See Here for more information. A serial port board is needed for voltage conversion. Serial ports output voltages up to 12v and go lower than 0v.

3. A regular RS-232 Cable (make sure it is not a null modem cable, they look the same). You can check what type of cable you have with your multimeter. Put your multimeter on each pin of your cable starting with pin 1. Check for a zero ohm reading. This will check that the pins are the same at both ends. Null modem cables have some pins crossed.

Build your circuit

The circuit will be quite simple, you can take your blink a led circuit, and attach your serial port board. Here's a schematic with 16F877. We will be using the TX and RX pins:

Test your circuit

Before you write your own code, you should make sure your circuit actually works.

Go into the sample directory within your jalv2/jallib installation. Find your pic, and look for a serial hardware sample such as 16f877a_serial_hardware.jal. Then compile it and burn it to your PIC. Don't turn on your circuit yet, we are not ready.

On your PC, you will have to install some serial communications program such as RealTerm. RealTerm is free and open source. I will use RealTerm for this tutorial. You can download it here:

http://realterm.sourceforge.net/

Open RealTerm and click on the "Port" tab, we need to select the port & speed, etc to the following values:

1. The Parity = no parity

2. The number of data bits = 8

3. The number of stop bits = 1

4. The data transmission speed = 115200

5. The port number on your PC

Now press "Open" in RealTerm and turn on your circuit. If you now see "Hello serial world......" showing in within RealTerm on your PC, you are able to receive data.

If your circuit doesn't work, your serial port board may have TX and RX switched (you can try switching your TX/RX wires around), or you may have selected the wrong port number, some PCs have more than one serial port.

Now click on RealTerm's "send" tab, type in the number "65" in the first box and press "Send Numbers". If it sent ok, the PIC will echo this value back to you. You will see the ASCII character "A", which is the same as decimal 65. You can see a full ASCII chart at asciitable.com.

Now please change your RealTerm settings to receive decimal numbers by clicking on the "Display" tab, and choose "int8" under "Display As" at the left side. You will now continuously see the number "46" come in, and try sending the number "65" again. You will get the same number back on your screen.

int8 - shows integer numbers in RealTerm

Ascii - shows ascii text

Hex[space] - shows hex numbers with a space between each

Write code to send data from PIC to PC

Since this is one of the first circuits you will be building, I will try to give you detailed information so you can get some programming experience. We will continue with your code from "Blink a led". We will modify it to send data to your PC, Here's your original code:

include 16f877a                    -- target PICmicro
--
-- This program assumes a 20 MHz resonator or crystal
-- is connected to pins OSC1 and OSC2.
pragma target clock 20_000_000     -- oscillator frequency
-- configuration memory settings (fuses)
pragma target OSC  HS              -- HS crystal or resonator
pragma target WDT  disabled        -- no watchdog
pragma target LVP  disabled        -- no Low Voltage Programming
--
enable_digital_io()                -- disable analog I/O (if any)
--
-- You may want to change the selected pin:
alias   led      is pin_A0
pin_A0_direction =  output
--
forever loop
   led = on
   _usec_delay(250000)
   led = off
   _usec_delay(250000)
end loop
--

First we need to add serial functionality, I got the following code from 16f877a_serial_hardware.jal

-- ok, now setup serial;
const serial_hw_baudrate = 115_200
include serial_hardware
serial_hw_init()

So, now copy and past this into your code, I would put it somewhere after the line "enable_digital_io", and somewhere before your main program which starts at "forever loop".

This code will set your baudrate (speed), it will include the correct library file "serial_hardware", and it will initialize the library with "serial_hw_init()". You can change the speed if you wish, but you must change the speed in RealTerm as well.

Now we can put some code that will send data to your PC. If you want to send the number 65 to your PC, you must use this code:

serial_hw_data = 65

This code works because it is a procedure/function within serial_hardware.jal, and you have already included the serial_hardware library. serial_haredware.jal can be found in the "lib" folder of your jallib installation. You can open that file and read notes within it for more information and for other usable variables, functions and procedures.

Let's make your code send the number 65 when the led turns on, and send the number 66 when your led turns off. Just place your code after your "led = on", and after "led = off"

forever loop
   led = on
   serial_hw_data = 65 -- send 65 via serial port
   _usec_delay(250000)
   led = off
   serial_hw_data = 66 -- send 66 via serial port
   _usec_delay(250000)
end loop

Or, if you wish to send Ascii letters to your PC instead, you could use the following:

forever loop
   led = on
   serial_hw_data = "A" -- send letter A via serial port
   _usec_delay(250000)
   led = off
   serial_hw_data = "B" -- send letter B via serial port
   _usec_delay(250000)
end loop

Both of the above loops will continuously send the decimal number's 65 and 66 via your serial port each time your led turns on or off. Your completed code should look like this:

include 16f877a                    -- target PICmicro
--
-- This program assumes a 20 MHz resonator or crystal
-- is connected to pins OSC1 and OSC2.
pragma target clock 20_000_000     -- oscillator frequency
-- configuration memory settings (fuses)
pragma target OSC  HS              -- HS crystal or resonator
pragma target WDT  disabled        -- no watchdog
pragma target LVP  disabled        -- no Low Voltage Programming

enable_digital_io()                -- disable analog I/O (if any)

-- ok, now setup serial;@jallib section serial
const serial_hw_baudrate = 115_200
include serial_hardware
serial_hw_init()

-- You may want to change the selected pin:
alias   led      is pin_A0
pin_A0_direction =  output

forever loop
   led = on
   serial_hw_data = 65 -- send 65 via serial port
   _usec_delay(250000)
   led = off
   serial_hw_data = 66 -- send 66 via serial port
   _usec_delay(250000)
end loop

Compile and burn your code to your PIC, then turn on your circuit. You should get this while your led is blinking:

Awesome, now that you can send data to your pc! This was an important step since it will greatly help you with your troubleshooting by sending you readable information such as text, numbers and other types of data.

If you feel your programming skills are not as good as they should be, practice practice practice! Continue using the language reference at http://www.casadeyork.com/jalv2/jalv2/index.html

Write code to send data from PC to PIC

In the beginning, you may not have a use for sending data from your PC to your circuit, so you may skip this and go onto other things.

Here we are going to get the PIC to receive data from the PC. We will write some code that will only start blinking a led when you send data to the PIC. Also we will tell the PIC to send the number 65 to the PC once per second.

We will now learn to use the following variables from serial_hardware.jal:

serial_hw_data_available - If the PIC received data, this variable will equal TRUE, otherwise FALSE

serial_hw_data - If data is available, this variable will contain the data

So let's modify your current loop:

forever loop
   led = on
   serial_hw_data = 65 -- send 65 via serial port
   _usec_delay(250000)
   led = off
   serial_hw_data = 66 -- send 66 via serial port
   _usec_delay(250000)
end loop

First change it so it will send the number 65 to your PC every one second:

forever loop
   _usec_delay(1_000_000) -- one second delay
   serial_hw_data = 65    -- send 65 via serial port
end loop

We now can add an if statement to find out if there is serial data available:

forever loop
   _usec_delay(1_000_000) -- one second delay
   serial_hw_data = 65 -- send 65 via serial port

   if serial_hw_data_available then -- check if there is data available
   end if
end loop

You will need to create a variable "x" before your "forever loop", this variable will hold the data when you want to receive it:

var byte x

Now you have a place to store the data, so you may now write a line within your "if" statement to get the data:

x = serial_hw_data

Then build a for loop after that to blink the led "x" number of times

for x loop   -- loop (x number of times) using data received on serial port    
   led = on
   _usec_delay(250000)
   led = off
   _usec_delay(250000)
end loop

Here is your completed code:

include 16f877a                    -- target PICmicro
--
-- This program assumes a 20 MHz resonator or crystal
-- is connected to pins OSC1 and OSC2.
pragma target clock 20_000_000     -- oscillator frequency
-- configuration memory settings (fuses)
pragma target OSC  HS              -- HS crystal or resonator
pragma target WDT  disabled        -- no watchdog
pragma target LVP  disabled        -- no Low Voltage Programming

enable_digital_io()                -- disable analog I/O (if any)

-- ok, now setup serial;@jallib section serial
const serial_hw_baudrate = 115_200
include serial_hardware
serial_hw_init()

-- You may want to change the selected pin:
alias   led      is pin_A0
pin_A0_direction =  output

var byte x

forever loop              -- continue forever
   _usec_delay(1_000_000) -- one second delay
   serial_hw_data = 65    -- send 65 via serial port
   
   if serial_hw_data_available then -- check if data is ready for us
      x = serial_hw_data            -- get the data
      for x loop                    -- loop "x" number of times
         led = on                   -- turn the led on
         _usec_delay(250000)        -- 250ms delay
         led = off                  -- turn the led off
         _usec_delay(250000)        -- 250ms delay
      end loop                      -- loop
   end if                           -- end the if statement

end loop

As you can see, this code will do the following:

1. delay 1 second

2. send the number 65 via serial port

3. see if there is data waiting for us, if so, get it and blink the led (the number of times of the data received)

4. loop back to the start

So, turn it on, you will start getting decimal numbers: "65 65 65 65 65" or ascii: "AAAAAA" in RealTerm. Now send your PIC the number 5, you will see your led blink 5 times. Now isn't that awesome!

PIC to PIC communication via serial port

Sending data to your PC is not the only use. If you have an extra PIC laying around, we can get two PIC's to talk to each other. And it's quite easy too!

I think you can do this on your own by now, you know how to make one PIC send data, and how to make a PIC receive data, so all you have to do is write some sending code on one PIC and receiving code on the other.

Build another circuit the same as your current one, then do the following:

1. connect the TX pin from PIC # 1 to the RX pin of PIC # 2

2. connect the RX pin from PIC # 1 to the TX pin of PIC # 2

On one of your PIC's, make it send data every one second, like we did before at Write code to send data from PIC to PC.

On the other PIC, make it loop continuously. Put an if statement in the loop that will see if there is data available, and if there is, make the led blink once, like we did at Write code to send data from PC to PIC.

You should then see your led blinking on your second PIC.

Wow, that was a lot, now I think you know your stuff!

Your Next Step

Now that you know how serial works, I suggest you take a look at the print and format libraries which will help you format numbers and strings in an easy & readable. Check out this tutorial:

Print & Format