The Tutorial Book: Have Fun With PIC Microcontrollers, Jal v2 and Jallib
The Tutorial Book: Have Fun With PIC Microcontrollers, Jal v2 and Jallib
Contents
1
Back to basics...
Topics: 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,
• TODO: Introduction
these tutorials are also here to help you testing new chips, or... when things go wrong and
• Installation
you can't figure out why, going back to basics
• Getting Started
• Blink A Led (Your First Don't worry, everything is gonna be alright...
Project)
• Setting up a serial link
(UART) -- TODO
6 | Jallib Tutorials | Back to basics...
Jallib Tutorials | Back to basics... | 7
TODO: Introduction
8 | Jallib Tutorials | Back to basics...
Installation
Jallib Group
Jallib Group
Windows Install:
1. Download jalpack (installer executable) from http://jaledit.googlecode.com/files/JALPack_2.4_0.4_0.6.1.0.exe
2. Run the setup file
3. Run JalEdit.exe from the jaledit directory
4. (optional) Click Tools Menu -> Environment Options -> Programmer, Then Set the Programmer Executable Path
You should get something like this on windows:
Linux Install
1. Go to http://code.google.com/p/jallib/downloads/list, get the link location of the jallib-pack (.tar.gz file)
2. Go to the directory you wish to install JALv2
3. Download the package with: $ wget [link location of the jallib-pack] or simply use your
favorite browser to download archive in the appropriate directory.
4. unzip the package with: $ tar xzf [filename.tar.gz]
Note: Jaledit runs under Wine on Linux
Getting Started
Matthew Schinkel
Jallib Group
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.
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.
Some of these have USB capability. I would suggest one of the following: 18F452, 18F4550, 18F2550. These PIC’s
will also work in our getting started “blink a led” tutorial with the same circuit diagram. For future tutorials, the
circuits may need some modifications.
Here’s a price chart from the manufacturer’s sales website:
OK, enough of this boring stuff, lets build something! Start with the Blink A Led Tutorial.
14 | Jallib Tutorials | Back to basics...
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.
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”
Jallib Tutorials | Back to basics... | 15
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.
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.
Jallib Tutorials | Back to basics... | 17
Your circuit is done, and it looks pretty, but it doesn’t do anything :o(..
compiler – holds the jalv2.exe compiler program to convert your JAL code to microcontroller hex code
18 | Jallib Tutorials | Back to basics...
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\)
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
end loop
--
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
Jallib Tutorials | Back to basics... | 23
2
PIC peripherals
Topics: This chapter covers main and widely used PIC microcontroller peripherals, like PWM,
ADC, etc... For each section, you'll find some basic theory explaining how things works,
• Pulse Width Modulation
then a real-life example.
(PWM)
• Analog-to-Digital
Converter (ADC) --
TODO
• I²C
26 | Jallib Tutorials | PIC peripherals
Jallib Tutorials | PIC peripherals | 27
Pulse Width ModulationIn this "Step-by-Step" tutorial, we're going to (try to) have some fun with PWM. PWM stands
for , and is quite weird when you first face this (this was at least my first feeling).
Both have a 50% duty cycle (50% on, 50% off), but the upper one's frequency is twice the bottom
Figure 1: PWM: same duty cycle, different frequencies.
Three different duty cycle (10%, 50% and 90%), all at the same frequency
Figure 2: PWM: same frequency, different duty cycles
But what is PWM for ? What can we do with it ? Many things, like:
Jallib Tutorials | PIC peripherals | 29
The connector brings +5V on the two bottom lines (+5V on line A, ground on line B).
So, step by step... First, as we said, we must select which pin will carry the PWM signals...
end loop
32 | Jallib Tutorials | PIC peripherals
Quite easy right ? There are two main waves: one will light up the LED progressively (0 to 250), another will turn
it off progressively (250 to 0). On each value, we set the duty cycle with pwm1_set_dutycycle(i) and wait a
little so we, humans, can see the result.
About the result, how does this look like ? See this video: http://www.youtube.com/watch?v=r9_TfEmUSf0
Having fun with PWM and a piezo buzzer (or a speaker) (part 2)
Sébastien Lelong
Jallib Group
In previous tutorial, we had fun by controlling the brightness of a LED, using PWM. This time, we're going to have
even more fun with a piezo buzzer, or a small speaker.
If you remember, with PWM, you can either vary the duty cycle or the frequency. Controlling the brightness of a
LED, ie. produce a variable voltage on the average, can be done by having a constant frequency (high enough) and
vary the duty cycle. This time, this will be the opposite: we'll have a constant duty cycle, and vary the frequency.
1
I guess this is about energy or something like that. One guru could explain the maths here...
34 | Jallib Tutorials | PIC peripherals
By the way, how to observe the "duty cycle effect" on the volume ? Just program your PIC with the previous
experiment one, which control the brightness of a LED, and power on the circuit. I wanted to show a video with
sounds, but the frequency is too high, my camera can't record it...
Anyway, that's a little bit boring, we do want sounds...
end loop
Quite straightforward:
• we "explore" frequencies between 0 and 100 000 Hz, using a counter
• we use pwm_set_frequency(counter) to set the frequency, in Hertz. It takes a dword as parameter (ie.
you can explore a lot of frequencies...)
• finally, as we want a 50% duty cycle, and since its value is different for each frequency setting, we need to re-
compute it on each loop.
Note: jallib's PWM libraries are coming from a "heavy refactoring" of Guru Stef Mientki's PWM library.
While integrating it to jallib, we've modified the library so frequencies can be set and changed during
program execution. This wasn't the case before, because the frequency was set as a constant.
So, how does this look like ? Hope you'll like the sweet melody :)
http://www.youtube.com/watch?v=xZ9OhQUKGtQ
I²C
38 | Jallib Tutorials | PIC peripherals
i2c is a nice protocol: it is quite fast, reliable, and most importantly, it's addressable. This means that on a single 2-
wire bus, you'll be able to plug up to 128 devices using 7bits addresses, and even 1024 using 10bits address. Far
enough for most usage... I won't cover i2c in depth, as there are plenty resources on the Web (and I personally like
this page).
Example: consider the following address (8-bits long, last bit is for operation type)
0x5C => 0b_0101_1100 => write operation
The same address for read operation will be:
0x93 => 0b_0101_1101 => read operation
Note: jallib currently supports up to 128 devices on a i2c bus, using 7-bits long addresses (without the
8th R/W bits). There's currently no support for 10-bits addresses, which would give 1024 devices on the
same bus. If you need it, please let us know, we'll modify libraries as needed !
OK, enough for today. Next time, we'll see how two PICs must be connected for i2c communication, and we'll check
the i2c bus is fully working, before diving into the implementation.
2
some PICs have MSSP, this means they can also be used as i2c hardware Master
Jallib Tutorials | PIC peripherals | 39
In previous tutorial, we saw a basic overview of how to implement an i2c slave, using a finite state machine
implementation. Today, we're going to get our hands a little dirty, and starts connecting our master/slave together.
In this circuit, both PIC have a LED connected, which will help us understand what's going on. On a breadboard, this
looks like that:
40 | Jallib Tutorials | PIC peripherals
The master is on the right side, the slave on the left. I've put the two pull-ups resistors near the master:
Jallib Tutorials | PIC peripherals | 41
The goal of this test is simple: check if the i2c bus is properly built and operational. How ? PIC 16F88 and its SSP
peripheral is able to be configured so it triggers an interrupts when a Start or Stop signal is detected. Read this page
(part of an nice article on i2c, from last post's recommandations).
How are we gonna test this ? The idea of this test is simple:
1. On power, master will blink a LED a little, just to inform you it's alive
2. On the same time, slave is doing the same
3. Once master has done blinking, it sends a i2c frame through the bus
4. If the bus is properly built and configured, slave will infinitely blink its LED, at high speed
Note master will send its i2c frame to a specific address, which don't necessarily need to be the same as the slave one
(and I recommand to use different addresses, just to make sure you understand what's going on).
What about the sources ? Download last jallib pack, and check the following files (either in lib or sample
directories):
• i2c_hw_slave.jal: main i2c library
• 16f88_i2c_sw_master_check_bus.jal: code for master
• 16f88_i2c_hw_slave_check_bus.jal: code for slave
The main part of the slave code is the way the initialization is done. A constant is declared, telling the library to
enable Start/Stop interrupts.
const SLAVE_ADDRESS = 0x23 -- whatever, it's not important, and can be
-- different from the address the master wants
-- to talk to
-- with Start/Stop interrupts
const bit i2c_enable_start_stop_interrupts = true
-- this init automatically sets global/peripherals interrupts
i2c_hw_slave_init(SLAVE_ADDRESS)
42 | Jallib Tutorials | PIC peripherals
In previous parts of this tutorial, we've seen a little of theory, we've also seen how to check if the i2c bus is
operational, now the time has come to finally build our i2c slave. But what will slave will do ? For this example, slave
is going to do something amazing: it'll echo received chars. Oh, I'm thinking about something more exciting: it will
"almost" echo chars:
• if you send "a", it sends "b"
• if you send "b", it sends "c"
• if you send "z", it sends "{"3
Two connectors are used for earch port, PORTA and PORTB, to plug daughter boards, or a breadboard in our case.
The i2c initialization part is quite straight forward. SCL and SDA pins are declared, we'll use a standard speed,
400KHz:
-- I2C io definition
var volatile bit i2c_scl is pin_b4
var volatile bit i2c_scl_direction is pin_b4_direction
var volatile bit i2c_sda is pin_b1
var volatile bit i2c_sda_direction is pin_b1_direction
-- i2c setup
const word _i2c_bus_speed = 4 ; 400kHz
const bit _i2c_level = true ; i2c levels (not SMB)
include i2c_software
i2c_initialize()
We'll also use the level 1 i2c library. The principle is easy: you declare two buffers, one for receiving and one for
sending bytes, and then you call procedure specifying how many bytes you want to send, and how many are expected
to be returned. Joep has written a nice post about this, if you want to read more about this. We'll send one byte at a
time, and receive one byte at a time, so buffers should be one byte long.
const single_byte_tx_buffer = 1 -- only needed when length is 1
var byte i2c_tx_buffer[1]
var byte i2c_rx_buffer[1]
include i2c_level1
What's next ? Well, master also has to read chars from a serial line. Again, easy:
const usart_hw_serial = true
const serial_hw_baudrate = 57_600
include serial_hardware
serial_hw_init()
-- Tell the world we're ready !
serial_hw_write("!")
Jallib Tutorials | PIC peripherals | 45
So when the master is up, it should at least send the "!" char.
Then we need to specify the slave's address. This is a 8-bits long address, the 8th bits being the bit specifying if
operation is a read or write one (see part 1 for more). We then need to collect those chars coming from the PC and
sends them to the slave.
The following should do the trick (believe me, it does :))
var byte icaddress = 0x5C -- slave address
forever loop
if serial_hw_read(pc_char)
then
serial_hw_write(pc_char) -- echo
-- transmit to slave
-- we want to send 1 byte, and receive 1 from the slave
i2c_tx_buffer[0] = pc_char
var bit _trash = i2c_send_receive(icaddress, 1, 1)
-- receive buffer should contain our result
ic_char = i2c_rx_buffer[0]
serial_hw_write(ic_char)
end if
end loop
The whole program is available on jallib SVN repository here.
end procedure
• state 3: master initiates a read operation, it wants to get the echo back. Slave should send its processed char.
procedure i2c_hw_slave_on_state_3() is
pragma inline
i2c_hw_slave_write_i2c(data)
end procedure
• state 4: master still wants to read some information. This should never occur, since one char is sent and read at a
time. Slave should thus produce an error.
procedure i2c_hw_slave_on_state_4() is
pragma inline
-- This shouldn't occur in our i2c echo example
i2c_hw_slave_on_error()
end procedure
• state 5: master hangs up the connection. Slave should reset its state.
procedure i2c_hw_slave_on_state_5() is
pragma inline
data = 0
end procedure
Finally, we need to define a callback in case of error. You could do anything, like resetting the PIC, and sending log/
debug data, etc... In our example, we'll blink forever:
procedure i2c_hw_slave_on_error() is
pragma inline
-- Just tell user user something's got wrong
forever loop
led = on
_usec_delay(200000)
led = off
_usec_delay(200000)
end loop
end procedure
Once callbacks are defined, we can include the famous ISR library.
include i2c_hw_slave_isr
So the sequence is:
1. include i2c_hw_slave, and setup your slave
2. define your callbacks,
3. include the ISR
The full code is available from jallib's SVN repository:
• i2c_hw_slave.jal
• i2c_hw_slave_isr.jal
• 16f88_i2c_sw_master_echo.jal
• 16f88_i2c_hw_slave_echo.jal
All those files and other dependencies are also available in last jallib-pack (see jallib downloads)
Everything is ready...
48 | Jallib Tutorials | PIC peripherals
What now ?
We've seen how to implement a simple i2c hardware slave. The ISR library provides all the logic about the finite state
machine. You just have to define callbacks, according to your need.
i2c is a widely used protocol. Most of the time, you access i2c devices, acting as a master. We've seen how to be
on the other side, on the slave side. Being on the slave side means you can build modular boards, accessible with a
standard protocol. For instance, I've built a DC motor controller daughter board using this. It's a module, a unit on its
own, just plug, and send/receive data, with just two wires.
And I also plan to build a LCD controller board, but that's for another "Step by Step" post :)4
4
and actually this LCD controller was being built by Jallib guys (Joep, Albert, Richard, Rob), and known as "LCD interface" project. See here for
more.
Chapter
3
Experimenting external parts
Topics: You now have learned enough and can start to interface your PIC with externals parts.
Without being exhaustive, this chapter explains how to use a PIC with several widely used
• SD Memory Cards
parts, like LCD screen.
• Hard Disks - IDE/PATA
• Interfacing a Sharp
GP2D02 IR ranger
• Interfacing a HD44780-
compatible LCD display
50 | Jallib Tutorials | Experimenting external parts
Jallib Tutorials | Experimenting external parts | 51
SD Memory Cards
Matthew Schinkel
Jallib Group
In this tutorial we will learn how to use an SD Card for mass data storage.
SD Card Introduction
SD Cards (Secure Digital Cards) are quite popular these days for things like digital camera's, video camera's, mp3
players and mobile phones. Now you will have one in your project! The main advantages are: small size, large data
storage capability, speed, cost. It has flash storage that does not require power to hold data. The current version of the
sd card library that we will be using in this tutorial works with "standard capacity" sd cards up 4gb in size. I hope to
find time to add "high capacity" and "extended capacity" capability to the library.
SD Card have 2 data transfer types "SD Bus" and "SPI Bus". Most PIC's have an SPI port. The "SD Bus" is faster,
however uses more pins. We will be using SPI in our circuit. For more info on SPI visit http://en.wikipedia.org/wiki/
Serial_Peripheral_Interface_Bus. The SPI mode for SD Cards is 1,1.
We are not responsible for your data or SD card. Make sure you have nothing important on your SD card before you
continue.
These SD Cards are 3.3v devices, therefore a 5v to 3v conversion is needed between the PIC and the sd card. We
will use resistors to do the conversion, however there are many other methods. See http://www.microchip.com/3v/ for
more information. Another setup may be needed if you are putting more devices on the same SPI bus.
This circuit will use 16F877 If you are using a different PIC for your project, refer to the PIC's datasheet for pin
output levels/voltage. For example, 18F452 has many pins that are 5v-input that give 3v-output. These pins show
as "TTL / ST" - TTL compatible with CMOS level outputs in the datasheet and they will not require any voltage
conversion resistors. If you are not sure, set a pin as an output, and make it go high then test with a volt meter.
Power It Up
Plug your circuit into your PC for serial port communication at 38400 baud rate. Now turn it on. Press the reset button
in your circuit, you should get a result similar to this:
ASCII output
Hex output
Jallib Tutorials | Experimenting external parts | 55
As you can see from the first image, we got some actual readable data off the sd card as well as a bunch of junk. The
sample file reads the first sector (512 bytes) from the sd card. My sd card is formated with fat32, this is why I can
read some of the data output.
In the second image (after clearing the output and resetting the circuit), there was too much data to show it all. It
only shows the last bytes received. If you get the same hex output "66 22" followed by many "00", your circuit has
successfully written data and read it back again. You now have a working sd card circuit!
SPI Settings
Here you may change the chip select pin "pin_SS" and "pin_SS_direction" to another pin. SDI, SDO and SCK must
stay the same for the SPI hardware library.
You may notice that we are not defining/aliasing pins sdi, sdo and sck. We do not need to define them with a line
like "alias pin_sdo is pin_c5" becase they are set within the PIC and cannot be changed. If we use the SPI hardware
library, we must use the spi hardware pins. We only need to define there direction like this "pin_sdo_direction =
output".
You may also choose the SPI rate. According to the SPI hardware library, you can use SPI_RATE_FOSC_4
SPI_RATE_FOSC_16, SPI_RATE_FOSC_64 or SPI_RATE_TMR. The fastest is FOSC_4 (oscillator frequency / 4).
You may require a breadboard for the fastest speed, keep your SD Card as close to the PIC as possible.
-- setup spi
include spi_master_hw -- includes the spi library
-- define spi inputs/outputs
pin_sdi_direction = input -- spi input
pin_sdo_direction = output -- spi output
pin_sck_direction = output -- spi clock
-- spi chip select pin
ALIAS sd_chip_select_direction is pin_SS_direction
ALIAS sd_chip_select is pin_SS
sd_chip_select_direction = output -- chip select/slave select pin
sd_chip_select = high -- disable the sd card
--
spi_init(SPI_MODE_11,SPI_RATE_FOSC_16) -- choose spi mode and speed
If you have more then one SPI device on the SPI bus, do not interrupt or switch devices until the complete read
process has finished with sd_stop_read, do not allow the chip select pin to go high.
_usec_delay(100_000) -- wait for power to settle
var byte low_byte, high_byte -- vars for sending and recieving data
Now you can put whatever you want on your SD card, or possibly read lost data off of it.
If you want to read files stored on the card by your PC, there wil soon be a FAT32 library and tutorial so you can
easily browse, read and write to files and folders stored on your card.
What are you waiting for, go build something cool!
Jallib Tutorials | Experimenting external parts | 59
Actual Size
The most common drive sizes today are 3.5" and 2.5". The 3.5 inch drives are commonly used in desktop computers,
2.5" drives are used in laptops. The 2.5" drives are nice for your circuit because they do not require a 12v supply
voltage, and they use much less power.
If you wish to use a 2.5" laptop hard drive, you may need a 2.5" to 3.5" IDE adapter like this one:
Jallib Tutorials | Experimenting external parts | 61
Circuit Power
It is very important that you have enough power to drive your circuit. Hard drives need a lot of amps to run,
especially the 3.5" drives, so make sure you have a decent 5v and 12v power supply. I suggest that you DO NOT use
your PC's power supply to drive your circuit. You can easily short circuit your power supply and blow up your PC.
If you really insist on doing this, you better put a fuse on both 5v and 12v between your PC and your circuit. Just
remember that I told you not to!
Here's what the completed circuit should look like (don't turn on the power yet):
64 | Jallib Tutorials | Experimenting external parts
Jallib Tutorials | Experimenting external parts | 65
Power It Up
Plug your circuit into your PC for serial port communication at 38400 baud rate. Now turn it on. It should do the
following in this order:
1. Drive will power up with the led on, after power up the led will go off.
2. The led will blink once quickly and the drive will "spin down".
3. The led will turn on while the drive now "spins up"
4. The led will blink once quickly and the drive will "spin down" again.
5. The led will turn on while the drive now "spins up" again.
6. The led will turn on and off a few times and send some data to your PC's serial port.
7. The PIC now "spin down" the drive at the end of the program.
ASCII output
Hex output
66 | Jallib Tutorials | Experimenting external parts
In the first image, If your disk is formatted with fat32 you may be able to see some readable data as well as some
junk. There is too much data for me to show it all in the image, on my drive formatted with fat32 I can read "Invalid
partition t ableError loading operating system..."
In the second image (after clearing the output and resetting the circuit), there was too much data to show it all again.
It only shows the last bytes received. If you get the same hex output "CC DD" followed by many "FF", your circuit
has successfully written data and read it back again. You now have a working hard disk circuit!
pata_hd_register_write(PATA_HD_COMMAND_REG,PATA_HD_SPIN_UP) -- turn on
motor
Wait 10 seconds before next example
_usec_delay(10_000_000) -- wait 10 seconds before next example
Jallib Tutorials | Experimenting external parts | 69
Read the first and second sector from the hard drive
Now that we know we are able to write to the registers, we can try to read some data. One sector is 512 bytes. Since
data is transfered 2 bytes at a time, we will loop 256 times to read one full sector while sending the data via serial
port.
Reading is easy, there are 3 procedures within the library that MUST be used. You will notice this process is similar
to the SD card tutorial.
pata_hd_start_read(0) - start reading at specified sector (sector 0)
pata_hd_read_data(byte1, byte2) - actually read data from the card (2 bytes at a time)
pata_hd_stop_read() - stop the read process
You can also use the pata_hd_read_pulse(number) procedure to skip past data. For every 1 value added, there will be
2 bytes skipped since this procedure simply reads data and ignores the input.
-- Read one sector
for 256 loop -- 256 words, 512 bytes per sector
pata_hd_read_data(in_b, in_a) -- read data
send_word(in_b, in_a) -- send data via serial port
end loop
The drive is now has data for us, so let's read it. Notice that the input data bytes (in_b & in_a) are backwards for
identify drive (don't ask me why).
-- Read 512 bytes
for 256 loop -- 256 words, 512 bytes per sector
pata_hd_read_data(in_b, in_a) -- read data
send_word(in_a, in_b ) -- send data via serial port
end loop -- drive info high/low bytes are in reverse
order
Wait 10 seconds before the next example
_usec_delay(10_000_000) -- wait 10 seconds before next example
Write data to the drive
Just like reading, there are 3 procedures that MUST be used.
pata_hd_start_write(20) - start writing at specified sector (sector 20)
pata_hd_read_data(byte1, byte2) - write to the card (2 bytes at a time)
pata_hd_stop_write() - stop the read process
When writing to your hard drive, you MUST write 512 bytes at a time. In this example, we are writing (256x2) = 512
bytes + (250x2) = 500 bytes for a total of 1012 bytes. This means we have written one sector (512 bytes), as well as
500 bytes of the next sector. The second sector (500 bytes) that we have written, will not actually be written to the
hard drive until we finish the sector with data.
For this reason, you will need to use the pata_hd_write_to_sector_end(value) procedure. This procedure will
automatically finish the sector for you with the "value" data specified. In our case we are writing 0xFF till the end of
the 512 bytes (end of the sector).
Here's an example write, Please note that we are starting to write at sector 200
pata_hd_start_write(200) -- tell hd to get ready for reading
-- if you want, you can read back the last 6 bytes that are 0xFF
for 6 loop
pata_hd_read_data(in_b, in_a) -- read data
send_word(in_b, in_a) -- send data via serial port
end loop
If you want, you can turn off the hard drive motor at the end of the program
-- We're done, lets turn off the hd motor
pata_hd_register_write(PATA_HD_COMMAND_REG,PATA_HD_SPIN_DOWN)
Your Done!
That's it, Now you can read & write to all those hard drives you have laying around. You can read raw data from
drives and possibly even get back some lost data.
Alright, go build that hard disk thingy you where dreaming about!
72 | Jallib Tutorials | Experimenting external parts
Sharp IR rangers are widely used out there. There are many different references, depending on the beam pattern, the
minimal and maximal distance you want to be able to get, etc... The way you got results also make a difference: either
analog (you'll get a voltage proportional to the distance), or digital (you'll directly get a digital value). This nice
article will explain these details (and now I know GP2D02 seems to be discontinued...)
Note: the distances obtained from the ranger aren't linear, you'll need some computation to make them
so.
Here's the ranger with the diode soldered on the green wire (which is Vin pin, using your previously created mental
note...):
I've also added thermoplastic rubber tubes, to cleanly join all the wires:
Jallib Tutorials | Experimenting external parts | 75
Finally, in order to easily plug/unplug the sensor, I've soldered nice polarized connectors:
76 | Jallib Tutorials | Experimenting external parts
A sample, 16f88_ir_ranger_gp2d02.jal, is available in jallib SVN repositoryjallib released packages, and also in ,
starting from version 0.3. You can access downloads here.
I usually power two tracks on the side, used for the PIC and for the ranger:
78 | Jallib Tutorials | Experimenting external parts
Using the same previously created mental note, I connected the yellow Vout pin using a yellow wire, and the green
Vin pin using a green wire...
In this "Step by Step" tutorial, we're going to (hopefully) have some fun with a LCD display. Particularly one
compatible with HD44780 specifications, which seems to be most widely used.
Looking closer, "JHD 204A" seems to be the reference. Near the connector, only a "1" and a "16". No pin's name.
80 | Jallib Tutorials | Experimenting external parts
Googling the whole points to a forum, and at least a link to the datasheet. A section describes the "Pin Assignement".
Now I'm sure about how to connect this LCD.
Jallib Tutorials | Experimenting external parts | 81
So we now have everything to build the circuit. Here's the schematic. It also includes a LED, it will help us checking
everything is ok while powering up the board.
82 | Jallib Tutorials | Experimenting external parts
-- set direction
lcd_rs_direction = output
lcd_en_direction = output
lcd_dataport_direction = output
This is, pin by pin, the translation of the schematics. Maybe except porta_low. This represents pin A0 to A3, that
is pins for our 4 lines interface. porta_high represents pin A4 to A7, and porta reprensents the whole port, A0 to
A7. These are just "shorcuts".
We also have to declare the LCD geometry:
const byte LCD_ROWS = 4 -- 4 lines
const byte LCD_CHARS = 20 -- 20 chars per line
Once declared, we can then include the library and initialize it:
include lcd_hd44780_4 -- LCD library with 4 data lines
86 | Jallib Tutorials | Experimenting external parts
end loop
The full and ready-to-compile code is available on jallib repository:
• blog_16f88_sl_lcd_hd44780_4.jal
You'll need last jallib-pack, available on jallib's download section.
We, Jallib Group, want this book to be as open and free as possible. We decided to release it under Creative Common
Attribution-Noncommercial-Share Alike 3.0 license.
Basically (and repeating what's on Creative Common website), you are free:
• to Share - to copy, distribute, and transmit the work
• to Remix - to adapt the work
Under the following conditions:
• Attribution - You must attribute the work in the manner specified by the author or licensor (but not in any way
that suggests that they endorse you or your use of the work).
• Noncommercial - You may not use this work for commercial purposes.
• Share Alike - If you alter, transform, or build upon this work, you may distribute the resulting work only under
the same, similar or a compatible license.
Full license lecal code can be read at: http://creativecommons.org/licenses/by-nc-sa/3.0/legalcode
This license applies to the book content itself, not on codes, libraries, examples, etc... you may find, or when
it's explicitely stated work is released under another license. For instance, most work on Jallib is released under
BSD and ZLIB license, not under this Creative Common license. In doubt, please ask on Jallib Group (http://
groups.google.com/group/jallib)
88 | Jallib Tutorials | License
Appendix
90 | Jallib Tutorials | Appendix
In this tutorial, we're going to build a serial port that can connect your PIC's TX and RX pins to your pc or other
hardware using a max232 chip.
Many circuits will require some serial port communication, you may buy yourself a rs232 to TTL adapter off the net
for as little as $10, or you can build one yourself. The max232 is a very popular chip. It converts your 5v circuit to the
12v required for serial communication to things like your PC. Many microcontrollers have RX and TX output pins.
Here is a image of the max232 adapter I purchased. It has input pins for RX, TX, CT, RT as well as GND and 5v. The
RX and TX pins can be directly connected to your PIC.
I am going to use a cut serial port cord since it already has leads on it, and is long enough to reach my pc. Use your
multi-meter to find the pin numbers, and touch up the wires with solder so they’ll go into your breadboard easily.
Now build the circuit, As you can see, you will need the max232 chip from your local electronics store and a few 1uf
capacitors.
92 | Jallib Tutorials | Appendix
Great job, now connect the RX and TX pins to your circuit, and plug the rs232 port directly your pc, or to a usb-to-
serial adapter, or even to a bluetooth-to-serial adapter for short range wireless.
I strongly suggest you make this on a PCB with pins that will plug to your breadboard. you’ll use it a lot!
Jallib Tutorials | Appendix | 93
You can use serial_hardware lib or serial_software lib to transmit data to your pc, check for it in the other jallib
projects. I suggest the software realterm for sending/receiving data to your PIC
Open Source REALTERM http://realterm.sourceforge.net/
It can be downloaded for free from http://sourceforge.net/projects/realterm/files/
Open the software, click “Port”, choose your speed and port number and press “open”
Hex output
94 | Jallib Tutorials | Appendix
Ascii output
Jallib Tutorials | Appendix | 95
In Circuit Programming
Matthew Schinkel
Jallib Group
What is ICSP?
ICSP stands for In-Circuit Serial Programming. More information can be found at http://ww1.microchip.com/
downloads/en/DeviceDoc/30277d.pdf
Benefits of ICSP
1. You may program your PIC while it is in your breadboard circuit
2. You may program your PIC while it is on a soldered circuit board
3. You will save time programming so you can write more code faster
4. You can reset your circuit from your PC
5. You can program surface mount PIC's that are on soldered circuit board
6. You won't bend or break any pins
7. You won't damage your PIC by placing it in your breadboard wrong
8. With a remote desktop software like VNC, you can program your PIC from anywhere around the world.
9. I can program my PIC in my livingroom on my laptop while I watch tv with my wife! (I keep my mess in my
office)
You will also need to connect your PIC's power and ground pins to your power supply with the 5v power supply on.
Your done! Try to program your chip!
98 | Jallib Tutorials | Appendix
Changelog
Jallib Group
Jallib Group