Showing posts with label 16f88. Show all posts
Showing posts with label 16f88. Show all posts

Friday, July 10, 2009

Using new ADC libraries

Since several weeks or months, quite a lot of effort has been put into the development and update of ADC libraries. Why ? Because when Guru Stef Mientki first developed this library, he designed it for quite a few PICs (but most used ones at this time). Since jallib is to support all (or as much as) possible PICs, it was time to have a look on this huge piece of code...


Many different cases...

As usual, Microchip PICs offers a wide choice configuring ADC:
  • Not all PICs have ADC module (...)
  • Analog pins are dispatched differently amongst PICs, still for user's sake, they have to be automatically configured as input. We thus need to know, for each PIC, where analog pins are...
  • Some PICs have their analog pins dependent from each other, and some are independent (more on this later)
  • Clock configuration can be different
  • Some PICs have 8-bits low resolution ADC module, some have 10-bits high resolution ADC module (and some have 12-bits, those aren't currently handled, that's a restriction...)
  • Some PICs can have two voltage references (VRef+ and VRef-), only one voltage reference (Vref+) and some can't handle voltage references at all
  • (and probably other differences I can't remember :)...
Luckily most of these differences are transparent to users (hey, that was the goal of this refactoring/rewriting...).


Dependent and independent analog pins

OK, let's write some code ! But before this, you have to understand one very important point: some PICs have their analog pins dependent from each other, some PICs have their analog pins independent from each other. "What is this suppose to mean ?" I can hear... Let's consider two famous PICs: 16F877 and 16F88. 16F877 datasheet explains how to configure the number of analog pins, and vref, setting PCFG bits:


Want 6 analog pins, no Vref ? Then PCFG bits must be set to 0b1001. What will then be the analog pins ? RA0, RA1, RA2, RA3, RA5 and RE0. "What if I want 7 analog pins, no Vref ?" You can't because you'll get a Vref pin, no choice. "What if I want 2 analog pins being RE1 and RE2 ?" You can't, because there's no such combination. So, for this PIC, analog pins are dependent from each other, driven by a combination. In this case, you'll have to specify:
  • the number of ADC channels you want,
  • and amongst them, the number of Vref channels
Note: this is the behavior of the original Stef's lib adc_hardware.jal

Now, let's consider 16F88. In this case, there's no such table:


Mmmh... OK, there are ANS bits, one for each analog pins. Setting an ANS bit to 1 sets the corresponding pin to analog. This means I can set whatever pin I want to be analog. "I can have 3 analog pins, configured on RA0, RA4 and RB6. Freedom !"

Analog pins are
independent from each other in this case, you can do what you want. As a consequence, since it's not driven by a combination, you won't be able to specify the number of ADC channels here. Instead, you'll use set_analog_pin() procedure, and if needed, the reverse set_digital_pin() procedure. These procedures takes a analog pin number as argument. Say analog pin AN5 is on pin RB6. To turn this pin as analog, you just have to write set_analog_pin(5), because this is about analog pin AN5, and not RB6.

Note: As a consequence, these procedures don't exist when analog pins are dependent as in our first case.

Careful:
it's not because there are PCFG bits that PICs have dependent analog pins. Some have PCFG bits which act exactly the same as ANS bits (like some of recent 18F)

"Stop it, show me some code !"

Basically, all the logic accessing ADC module has been kept. Most of the work was about configuring the ADC module to handle all PICs. You'll find adc_read() and adc_read_low_res() functions, as usual. Changes only occur while configuring the ADC module.

16F877 : dependent analog pins

The following examples briefly explains how to setup ADC module when analog pins are dependent from each other, using PIC 16F877.

The following diagram is here to help knowing where analog pins are and where Vref pins are:



Example 1: 16F877, with only one analog pin, no voltage reference

-- beginning is about configuring the chip
-- this is the same for all examples for about 18F877
include 16f877
-- setup clock running @20MHz
pragma target OSC HS
pragma target clock 20_000_000
-- no watchdog
pragma target WDT disabled
pragma target LVP disabled
enable_digital_io()
include delay

-- ok, now setup serial, we'll use this
-- to get ADC measures
const serial_hw_baudrate = 19_200
include serial_hardware
serial_hw_init()


-- ok, now let's configure ADC
-- we want to measure using low resolution
-- (that's our choice, we could use high resolution as well)
const bit ADC_HIGH_RESOLUTION = false
-- we said we want 1 analog channel...
const byte ADC_NCHANNEL = 1
-- and no voltage reference
const byte ADC_NVREF = 0
-- now we can include the library
-- note it's now named "adc", not "adc_hardware" anymore
include adc
-- and run the initialization step
adc_init()


-- will periodically send those chars
var byte measure
forever loop
-- get ADC result, on channel 0
-- this means we're currently reading on pin RA0/AN0 !
measure = adc_read_low_res(0)
-- send it back through serial
serial_hw_write(measure)

-- and sleep a litte to prevent flooding serial...
delay_1ms(200)
end loop

Example 2: 16F877, with 5 analog pins, 1 voltage reference, that is, Vref+

This is almost the same as before, except we now want 5 (analog pins) + 1 (Vref) = 6 ADC channels (yes, I consider Vref+ pin as an ADC channel).

The beginning is the same, here's just the part about ADC configuration and readings:

const bit ADC_HIGH_RESOLUTION = false
-- our 6 ADC channel
const byte ADC_NCHANNEL = 6
-- and one Vref pin
const byte ADC_NVREF = 1
-- the two parameters could be read as:
-- "I want 6 ADC channels, amongst which 1 will be
-- reserved for Vref, and the 5 remaining ones will be
-- analog pins"
include adc
adc_init()

-- will periodically send those chars
var byte measure
forever loop
-- get ADC result, on channel 0
-- this means we're currently reading on pin RA0/AN0 !
measure = adc_read_low_res(0)
-- send it back through serial
serial_hw_write(measure)

-- same for pin RA1/AN1
measure = adc_read_low_res(1)
serial_hw_write(measure)

-- same for pin RA2/AN2
measure = adc_read_low_res(2)
serial_hw_write(measure)

-- pin RA3/AN3 can't be read, since it's Vref+

-- same for pin RA5/AN4
-- 4 is from from "AN4" !
measure = adc_read_low_res(4)
serial_hw_write(measure)

-- same for pin RE10/AN5
measure = adc_read_low_res(5)
serial_hw_write(measure)

-- and sleep a litte to prevent flooding serial...
delay_1ms(200)
end loop


16F88 : independent analog pins

The following example is about setting up ADC module with PIC 16F88, where analog pins are independent from each other.

The following diagram is here to help knowing where analog pins are and where Vref pins are:


Example 1: 16F88, analog pins on RA0/AN0, RA4/AN4 and RB6/AN5. No voltage reference.

-- beginning is about configuring the chip
include 16f88
-- We'll use internal oscillator. It work @ 8MHz
pragma target CLOCK 8_000_000
pragma target OSC INTOSC_NOCLKOUT
OSCCON_IRCF = 0b_111
pragma target WDT disabled
enable_digital_io()

-- ok, now setup serial, we'll use this
-- to get ADC measures
const serial_hw_baudrate = 19_200
include serial_hardware
serial_hw_init()

-- now configure ADC
const bit ADC_HIGH_RESOLUTION = false
const byte ADC_NVREF = 0
-- we can't specify a number of ADC channel here,
-- or we'll get an error !
include adc
adc_init()
-- now we declare the pin we want as analog !
set_analog_pin(0) -- RA0/AN0
set_analog_pin(4) -- RA4/AN4
set_analog_pin(5) -- RB6/AN5

-- reading is then the same
var byte measure
forever loop

measure = adc_read_low_res(0)
serial_hw_write(measure)

measure = adc_read_low_res(4)
serial_hw_write(measure)

measure = adc_read_low_res(5)
serial_hw_write(measure)

end loop



Whether you would want to turn RB6/AN5 into a digital pin again, you'd just call:

set_digital_pin(5)



OK, enough for now. Next time, we'll see how it works for real, with a "Step by Step" tutorial. We'll probably use a 16F88 for this, using serial.

As of July 19th, 2009, this ADC libraries aren't release yet (but expected to be soon). If you want to have fun with them, you'll have to get them from jallib SVN repository. See instructions here:

http://code.google.com/p/jallib/source/checkout


You can also get files one by one from SVN:

Anyway, browse SVN as needed :)



Sébastien Lelong

Saturday, April 11, 2009

Step by step: interfacing a Sharp GP2D02 IR ranger

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...)


Overview of GP2D02 IR ranger

GP2D02 IR ranger is able to measure distances between approx. 10cm and 1m. Results are available as digital values you can access through a dedicated protocol. One pin, Vin, will be used to act on the ranger. Another pin, Vout, will be read to determine the distance. Basically, getting a distance involves the following:

  1. First you wake up the ranger and tell it to perform a distance measure
  2. Then, for each bit, you read Vout in order to reconstitute the whole byte, that is, the distance
  3. finally, you switch off the ranger

The following timing chart taken from the datasheet will explain this better.



Note: the distances obtained from the ranger aren't linear, you'll need some computation to make them so.

Sharp GP2D02 IR ranger looks like this:


  • Red wire is for +5V
  • Black wire ground
  • Green wire is for Vin pin, used to control the sensor
  • Yellow wire is for Vout pin, from which 8-bits results read
(make a mental note of this...)


Interfacing the Sharp GP2D02 IR ranger

Interfacing such a sensor is quite straight forward. The only critical point is Vin ranger pin can't handle high logic level of the PIC's output, this level mustn't exceed 3.3 volts. A zener diode can be used to limit this level.

Note: be careful while connecting this diode. Don't forget it, and don't put it in the wrong side. You may damage your sensor. And I'm not responsible for ! You've been warned... That's said, I already forgot it, put it in the wrong side, and thought I'd killed my GP2D02, but this one always got back to life. Anyway, be cautious !

Here's the whole schematic. The goal here is to collect data from the sensor, and light up a LED, more or less according to the read distance. That's why we'll use a LED driven by PWM.




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:



Finally, in order to easily plug/unplug the sensor, I've soldered nice polarized connectors:



Writing the program

jallib >=0.3 contains a library, ir_ranger_gp2d02.jal, used to handle this kind of rangers. The setup is quite straight forward: just declare your Vin and Vout pins, and pass them to the gp2d02_read_pins(). This function returns the distance as a raw value. Directly passing pins allows you to have multiple rangers of this type (many robots have many of them arranged in the front and back sides, to detect and avoid obstacles).

Using PWM libs, we can easily make our LED more or less bright. In the mean time, we'll also transmit the results through a serial link.

var volatile bit gp2d02_vin is pin_a4
var volatile bit gp2d02_vout is pin_a6
var bit gp2d02_vin_direction is pin_a4_direction
var bit gp2d02_vout_direction is pin_a6_direction
include ir_ranger_gp2d02
-- set pin direction (careful: "vin" is the GP2D02 pin's name,
-- it's an input for GP2D02, but an output for PIC !)
gp2d02_vin_direction = output
gp2d02_vout_direction = input

var byte measure
forever loop
-- read distance from ranger num. 0
measure = gp2d02_read_pins(gp2d02_vin,gp2d02_vout)
-- results via serial
serial_hw_write(measure)
-- now blink more or less
pwm1_set_dutycycle(measure)
end loop

Note: I could directly pass pin_A4 and pin_A6, but to avoid confusion, I prefer using aliases.

A sample, 16f88_ir_ranger_gp2d02.jal, is available in jallib SVN repository, and also in jallib released packages, starting from version 0.3. You can access downloads here.


Building the whole on a breadboard

Putting all this stuff on a breadboard should be easy. Just follow the schematics :)




I usually power two tracks on the side, used for the PIC and for the ranger:


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...



Testing (and the video)


Time to test this nice circuit ! Power the whole, and check no smoke is coming from the PIC or (and) the ranger. Now get an object, like you hand, more or less closed to the ranger and observe the LED, or the serial output... Sweet !








Sébastien Lelong

Saturday, February 14, 2009

Step by Step: having fun with PWM and a piezo buzzer (or a speaker) (part 2)

The last time, 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.


What is a piezo buzzer ?

It's a "component" with a material having piezoelectric ability. Piezoelectricity is the ability for a material to produce voltage when it get distorted. The reverse is also true: when you produce a voltage, the material gets distorted. When you stop producing a voltage, it gets back to its original shape. If you're fast enough with this on/off voltage setting, then the piezo will start to oscillate, and will produce sound. How sweet...


Constant duty cycle ? Why ?

So we now know why we need to vary the frequency. This will make the piezo oscillates more and less, and produces sounds at different levels. If you produce a 440Hz frequency, you'll get a nice A3.

But why having a constant duty cycle ? What is the role of the duty cycle in this case ? Remember: when making a piezo oscillate, it's not the amount of volts which is important, it's how you turn the voltage on/off:
  • when setting the duty cycle to 10%: during a period, piezo will get distorted 10% on the time, and remain inactive 90% on the time. The oscillation proportion is low.
  • when setting the duty cycle to 50%: the piezo is half distorted, half inactive. The oscillation proportion is high, because the piezo oscillates at the its maximal amplitude, it's half and equally distorted and inactive.
  • when setting the duty cycle to 90%: the piezo will get distorted during 90% of a period, then nothing. The oscillation proportion is low again, because the proportion between distortion and inactivity is not equal.
(I guess this is about energy or something like that. One guru could explain the maths here...)

So, to summary, what is the purpose of the duty cycle in our case ? The volume ! You can vary the volume of the sound by modifying the duty cycle. 0% will produce no sounds, 50% will be the max volume. Between 50% and 100% is the same as between 0% and 50%. So, when I say when need a constant duty cycle, it's not that true, it's just that we'll set it at 50%, so the chances we hear something are high :)


Let's produce sounds !

The schematics will use is exactly the same as on the previous post with the LED, except the LED is replaced with a piezo buzzer, like this:




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...


Writing the software


The software part has a lot of similarities with the previous experiment. The initialization is the same, I let you have a look. Only the forever loop has changed:

var dword counter = 0
forever loop

for 100_000 using counter loop
pwm_set_frequency(counter)
-- Setting @50% gives max volume
-- must be re-computed each time the frequency
-- changes, because it depends on PR2 value
pwm1_set_percent_dutycycle(50)
end loop

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 :)





"Where can I download the files ?"

As usual, you'll need the last jallib pack (at least 0.2 version). You'll also find the exact code we used here.




Sébastien Lelong

Saturday, February 7, 2009

Step by Step: having fun with PWM and a LED (part 1)

Part 1 : controlling the brightness of a LED


In this "Step-by-Step" tutorial, we're going to (try to) have some fun with PWM. PWM stands for Pulse Width Modulation, and is quite weird when you first face this (this was at least my first feeling).


So, how does PWM look like ?...

PWM is about switching one pin (or more) high and low, at different frequencies and duty cycles. This is a on/off process. You can either vary:
  • the frequency,
  • or the duty cycle, that is the proportion where the pin will be high


Both have a 50% duty cycle (50% on, 50% off),
but the upper one's frequency is twice the bottom



Three different duty cycle (10%, 50% and 90%),
all at the same frequency


But what is PWM for ? What can we do with it ? Many things, like:
  • producing variable voltage (to control DC motor speed, for instance)
  • playing sounds: duty cycle is constant, frequency is variable
  • playing PCM wave file (PCM is Pulse Code Modulation)
  • ...


One PWM channel + one LED = fun

For now, and for this first part, we're going to see how to control the brightness of a LED. If simply connected to a pin, it will light at its max brightness, because the pin is "just" high (5V).

Now, if we connect this LED on a PWM pin, maybe we'll be able to control the brightness: as previously said, PWM can be used to produce variable voltages. If we provide half the value (2.5V), maybe the LED will be half its brightness (though I guess the relation between voltage and brightness is not linear...). Half the value of 5V. How to do this ? Simply configure the duty cycle to be 50% high, 50% low.

But we also said PWM is just about switching a pin on/off. That is, either the pin will be 0V, or 5V. So how will we be able to produce 2.5V ? Technically speaking, we won't be able to produce a real 2.5V, but if PWM frequency is high enough, then, on the average, and from the LED's context, it's as though the pin outputs 2.5V.


Building the whole

Enough theory, let's get our hands dirty. Connecting a LED to a PWM pin on a 16f88 is quite easy. This PIC has quite a nice feature about PWM, it's possible to select which pin, between RB0 and RB3, will carry the PWM signals. Since I use tinybootloader to upload my programs, and since tiny's fuses are configured to select the RB0 pin, I'll keep using this one (if you wonder why tinybootloader interferes here, read this post).




On a breadboard, this looks like this:


The connector brings +5V on the two bottom lines
(+5V on line A, ground on line B).


LED is connected to RB0


Writing the software

For this example, I took one of the 16f88's sample included in jallib distribution (16f88_pwm_led.jal), and just adapt it so it runs at 8MHz, using internal clock. It also select RB0 as the PWM pin.

So, step by step... First, as we said, we must select which pin will carry the PWM signals...

pragma target CCP1MUX RB0 -- ccp1 pin on B0

and configure it as output

var volatile bit pin_ccp1_direction is pin_b0_direction
pin_ccp1_direction = output
-- (simply "pin_b0_direction = output" would do the trick too)

Then we include the PWM library.

include pwm_hardware

Few words here... This library is able to handle up to 5 PWM channels (PIC using CCP1, CCP2, CCP3, CCP4 and CCP5 registers). Using conditional compilation, it automatically selects the appropriate underlying PWM libraries, for the selected target PIC.

Since 16f88 has only one PWM channel, it just includes "pwm_ccp1" library. If we'd used a 16f877, which has two PWM channels, it would include "pwm_ccp1" and "pwm_ccp2" libraries. What is important is it's transparent to users (you).

OK, let's continue. We now need to configure the resolution. What's the resolution ? Given a frequency, the number of values you can have for the duty cycle can vary (you could have, say, 100 different values at one frequency, and 255 at another frequency). Have a look at the datasheet for more.

What we want here is to have the max number of values we can for the duty cycle, so we can select the exact brightness we want. We also want to have the max frequency we can have (ie. no pre-scaler).

pwm_max_resolution(1)

If you read the jalapi documentation for this, you'll see that the frequency will be 7.81kHz (we run at 8MHz).

PWM channels can be turned on/off independently, now we want to activate our channel:

pwm1_on()

Before we dive into the forever loop, I forgot to mention PWM can be used in low or high resolution. On low resolution, duty cycles values range from 0 to 255 (8 bits). On high resolution, values range from 0 to 1024 (10 bits). In this example, we'll use low resolution PWM. For high resolution, you can have a look at the other sample, 16f88_pwm_led_highres.jal. As you'll see, there are very few differences.

Now let's dive into the loop...



forever loop
var byte i
i = 0
-- loop up and down, to produce different duty cycle
while i < 250 loop
pwm1_set_dutycycle(i)
_usec_delay(10000)
i = i + 1
end loop
while i > 0 loop
pwm1_set_dutycycle(i)
_usec_delay(10000)
i = i - 1
end loop
-- turning off, the LED lights at max.
_usec_delay(500000)
pwm1_off()
_usec_delay(500000)
pwm1_on()

end loop


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 ?





"I wanna try, where are the files ?"

To run this sample, you'll need the last jallib pack (at least 0.2 version). You'll also find the exact code we used here.



Sébastien Lelong

Sunday, January 25, 2009

Step by Step: interfacing a HD44780-compatible LCD display

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 seem to be most widely used.


Setting up the hardware

As usual, there are plenty resources on the web. I found this one quite nice, covering lots of thing. Basically, LCDs can be accessed with two distinct interfaces: 4-bit or 8-bit interfaces. 4-bit interface requires less pins (4 pins), but is somewhat slow, 8-bit interface requires more pins (8 pins) but is faster. jallib comes with the two flavors, it's up to you deciding which is most important, but usually, pins are more precious than speed, particularly when using a 16F88 which only has 16 I/O pins (at best). Since 4-bit interface seems to be most used, and we'll use this one here...

So, I've never used LCD, to be honest. Most guys consider it as an absolute minimum thing to have, since it can help a lot when debugging, by printing messages. I tend to use serial for this... Anyway, I've been given a LCD, so I can't resist anymore :)

The LCD I have seems to be quite a nice beast ! It has 4 lines, is 20 characters long.



Looking closer, "JHD 204A" seems to be the reference. Near the connector, only a "1" and a "16". No pin's name.


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.


For this tutorial, we're going to keep it simple:
  • as previously said, we'll use 4-bit interface. This means we'll use DB4, DB5, DB6 and DB7 pins (respectively pin 11, 12, 13 and 14).
  • we won't read from LCD, so R/W pin must be grounded (pin 5)
  • we won't use contrast as well, V5 pin (or Vee) must be grounded (pin 3)
Including pins for power, we'll use 10 pins out of the 16 available, 6 being connected to the PIC (RS, EN and 4 data lines).

For convenience, I soldered a male connector on the LCD. This will help when building the whole on a breadboard.



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.



Using a breadboard, it looks like this:



Writing the software

For this tutorial, we'll use one of the available samples from jallib repository. I took one for 16f88, and adapt it to my board (specifically, I wanted to use PORTA when connecting the LCD, and let PORTB's pins as is).

As usual, writing a program with jallib starts with configuring and declaring some parameters. So we first have to declare which pins will be connected:

-- LCD IO definition
var bit lcd_rs is pin_a6 -- LCD command/data select.
var bit lcd_rs_direction is pin_a6_direction
var bit lcd_en is pin_a7 -- LCD data trigger
var bit lcd_en_direction is pin_a7_direction

var byte lcd_dataport is porta_low -- LCD data port
var byte lcd_dataport_direction is porta_low_direction

-- 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
lcd_init() -- initialize LCD

For this example, we'll also use the print.jal library, which provides nice helpers when printing variables.

include print

Now the main part... How to write things on the LCD.

  • You can either use a procedure call: lcd_writechar("a")
  • or you can use the pseudo-variable : lcd = "a"
  • lcd_setcursor(x,y) will set the cursor position. x is the line, y is the row, starting from 0
  • finally, lcd_clearscreen() will, well... clear the screen !
Full API documentation is available on jalapi.

So, for this example, we'll write some chars on each line, and print an increasing (and incredible) counter:

const byte str1[] = "Hello world!" -- define strings
const byte str2[] = "third line"
const byte str3[] = "fourth line"

print_string(lcd, str1) -- show hello world!
lcd_setcursor(2,0) -- to 3rd line
print_string(lcd, str2)
lcd_setcursor(3,0) -- to 4th line
print_string(lcd, str3)

var byte counter = 0

forever loop -- loop forever

counter = counter + 1 -- update counter
lcd_setcursor(1,0) -- second line
print_byte_hex(lcd, counter) -- output in hex format
delay_100ms(3) -- wait a little

if counter == 255 then -- counter wrap
lcd_setcursor(1,1) -- 2nd line, 2nd char
lcd = " " -- clear 2nd char
lcd = " " -- clear 3rd char
end if

end loop


The full and ready-to-compile code is available on jallib group's file section:

You'll need last jallib-pack, available on jallib's download section.



How does this look when running ?

Here's the video !






Sébastien Lelong

Tuesday, January 13, 2009

Common pitfall: digital pins aren't digital unless explicitely set as digital

If you've read some jallib samples, you may have often seen this line:

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

What does this mean and why ? This simply means you want to set all pins as digital pins. Because, by default, pins are configured as analog pins.

Where does this come from ? From the holy datasheet, of course ! Consider the 16f88 chip. PORTA provides the following functions:



Amongst them, some provide analog function, such Analog-to-Digital-Conversion (ADC). And it's explicitly said the PORTA pins are configured as analog inputs on Power-on Reset:


Datasheet is less explicit for PORTB. Two pins provide analog functions, for the last two ADC channels (pins RB6 and RB7):


Those pins are configured with ANSEL register, which is read as 1 on Power-on Reset, which means by default, pins are configured as analog:


Phew... Now why don't we, at jallib, set all pins as digital ? After all, most of the time, you need digital pins, and even if not, you could just set them as analog pins as needed. Yes, you could. But we decided to follow the holy datasheet. And this means if you read and know the datasheet, when you type this:

include 16f88

you expect to get analog pins, because the datasheet says so. To switch them to digital pins, just call
enable_digital_io(). This procedure is the same for every PIC: whatever the device file you're using, calling this procedure will configure and setup all registers so you have digital pins (and there are many differencies and inconsistencies between PICs, fortunately thanks to Rob's hard work, this is made easy for users).

So, one important point using jallib is: "be explicit". While this may need more typing from you, you'll get a better understanding of what's going on, and you'll finally get a cleaner and more maintainable code.

Sébastien Lelong

Sunday, January 4, 2009

Common pitfall: setting up registers while using bootloaders

I've recently been in trouble understanding how the 16F88's internal clock is working. Starting from a supposed i2c speed problem, I dive into internal clock's internals, with Joep, my guide... Here's what I learned.

First of all, I like using internal clock, because it requires less parts, provides still nice performances and consume less energy, which is good when working on embedded systems. Maybe the major drawback is you can't reach a decent 115 200 bds for your serial communication.

Anyway, I used to setup my 16f88 to run at 8 MHz. Yeah, just say:

pragma target CLOCK 8_000_000
pragma target OSC INTOSC_NOCLKOUT

and that's it, you'll run at 8 MHz... Will you ? Well, all my 16f88 always run @ 8MHz using this configuration. But Joep, the Grand Master of the Hidden Clock, said: "no you don't, because the holy datasheet says, by default (on power), this configuration will setup a clock running @ 31.25 KHz". Errr... 31.25 KHz, how can this be possible ? I'm sure it runs @ 8MHz. And why 31.25 KHz ? "Because IRCF bits are read as 0 when powered, see OSCCON register", said Joep the Grand Master.

So I looked at OSCCON register:



Yes, now I can see OSCON_IRCF bits selects the appropriate internal clock speed. IRCF = 0b_000 by default, and not 0b_111, which is the correct setting for an 8MHz clock.

Still, I'm sure it's running @ 8MHz. It's like my 16f88 decided to set IRCF to 0b_111, despite what datasheet can say... How can this be possible ?

Bootloaders... Yes, bootloaders can be sometime quite surprising. Because when using them, what you specify in your code about registers might not be what's programmed ! I use Tiny bootloader, a very nice and small one. And I use the internal clock version for 16f88, running @ 8MHz. And Tiny bootloader code correctly setup the internal clock. So the "default" value becomes what Tiny has set:
IRCF = 0b_111 that is, it runs @ 8MHz. Of course, you can still select the speed you want, by playing with IRCF, but the apparent default value is not what the datasheet says... Confusing :)

So, to be accurate, the appropriate way to specify an internal clock running @ 8MHz for a 16f88 is:

pragma target CLOCK 8_000_000
pragma target OSC INTOSC_NOCLKOUT
OSCCON_IRCF = 0b_111


Another pitfall I usually fall into is CCP1MUX. With this register, you can specify which pin, RB0 or RB3, will produce PWM for instance. Every time I set it to RB3, it just doesn't work. Why ? Because Tiny bootloader has set it to RB0. And this time, it cannot be changed, because Tiny bootloader (as any bootloader for PIC ?) cannot re-program such as register !

If using a bootloader, remember, the actual program being originally uploaded to your PIC is not your program, it's the bootloader itself. So remember its proper configuration might conflict with your own, or at least confuse you...

Solution ? Don't use bootloader, or be careful !