Revision: 1601
Author: [email protected]
Date: Sun Jan 17 23:35:40 2010
Log: serial port tutorial finnished and added to book
http://code.google.com/p/jallib/source/detail?r=1601
Added:
/trunk/doc/dita/tutorials/images/serial_communication_16f877_pinout.jpg
/trunk/doc/dita/tutorials/images/serial_communication_realterm_65.jpg
/trunk/doc/dita/tutorials/images/serial_communication_realterm_ab.jpg
/trunk/doc/dita/tutorials/images/serial_communication_realterm_blink.jpg
/trunk/doc/dita/tutorials/images/serial_communication_realterm_hello.jpg
/trunk/doc/dita/tutorials/images/serial_communication_realterm_mode.jpg
/trunk/doc/dita/tutorials/images/serial_communication_realterm_port.jpg
/trunk/doc/dita/tutorials/images/serial_communication_realterm_send.jpg
Deleted:
/trunk/doc/dita/tutorials/images/serial_communication_16f877_pinout.JPG
Modified:
/trunk/doc/dita/tutorials/tutorial_blink_a_led.xml
/trunk/doc/dita/tutorials/tutorial_serial_communication.xml
/trunk/doc/dita/tutorials/tutorials.ditamap
=======================================
--- /dev/null
+++ /trunk/doc/dita/tutorials/images/serial_communication_16f877_pinout.jpg
Sun Jan 17 23:35:40 2010
Binary file, no diff available.
=======================================
--- /dev/null
+++ /trunk/doc/dita/tutorials/images/serial_communication_realterm_65.jpg
Sun Jan 17 23:35:40 2010
Binary file, no diff available.
=======================================
--- /dev/null
+++ /trunk/doc/dita/tutorials/images/serial_communication_realterm_ab.jpg
Sun Jan 17 23:35:40 2010
Binary file, no diff available.
=======================================
--- /dev/null
+++
/trunk/doc/dita/tutorials/images/serial_communication_realterm_blink.jpg
Sun Jan 17 23:35:40 2010
Binary file, no diff available.
=======================================
--- /dev/null
+++
/trunk/doc/dita/tutorials/images/serial_communication_realterm_hello.jpg
Sun Jan 17 23:35:40 2010
Binary file, no diff available.
=======================================
--- /dev/null
+++ /trunk/doc/dita/tutorials/images/serial_communication_realterm_mode.jpg
Sun Jan 17 23:35:40 2010
Binary file, no diff available.
=======================================
--- /dev/null
+++ /trunk/doc/dita/tutorials/images/serial_communication_realterm_port.jpg
Sun Jan 17 23:35:40 2010
Binary file, no diff available.
=======================================
--- /dev/null
+++ /trunk/doc/dita/tutorials/images/serial_communication_realterm_send.jpg
Sun Jan 17 23:35:40 2010
Binary file, no diff available.
=======================================
--- /trunk/doc/dita/tutorials/images/serial_communication_16f877_pinout.JPG
Fri Jan 15 20:48:12 2010
+++ /dev/null
Binary file, no diff available.
=======================================
--- /trunk/doc/dita/tutorials/tutorial_blink_a_led.xml Sun Nov 22 11:01:49
2009
+++ /trunk/doc/dita/tutorials/tutorial_blink_a_led.xml Sun Jan 17 23:35:40
2010
@@ -1,5 +1,6 @@
<?xml version='1.0' encoding='UTF-8'?>
-<!DOCTYPE topic PUBLIC "-//OASIS//DTD DITA Topic//EN" "topic.dtd">
+<!-- This document was created with Syntext Serna Free. -->
+<!DOCTYPE topic PUBLIC "-//OASIS//DTD DITA Topic//EN" "topic.dtd" []>
<topic id="template_id">
<title>Blink A Led (Your First Project)</title>
<prolog>
@@ -204,6 +205,7 @@
<p>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.</p>
<p>It’s alive! You should see your led blinking! Congratulations on
your first JALv2 + JALLIB circuit!</p>
<p>Here's a youtube video of the result: <xref
href="http://www.youtube.com/watch?v=PYuPZO7isoo" format="html"/></p>
+ <p>I strongly suggest you do this tutorial next: <xref class="-
topic/xref " href="tutorial_serial_communication.xml">Serial Port &
RS-232 for communication</xref>.</p>
</section>
</body>
</topic>
=======================================
--- /trunk/doc/dita/tutorials/tutorial_serial_communication.xml Fri Jan 15
20:48:12 2010
+++ /trunk/doc/dita/tutorials/tutorial_serial_communication.xml Sun Jan 17
23:35:40 2010
@@ -10,30 +10,238 @@
<p id="firstcontent">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.</p>
<section>
<title>What is a serial port?</title>
- <p>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, Firewire, and Blue
Tooth, 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.</p>
+ <p>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 Blue Tooth, 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.</p>
<image href="images/serial_communication_pinout.jpg"/>
<p>At one time, there where a wide range of devices that used the
serial port such as a mouse, keyboard, a old GPS, modems and other
networking.</p>
- <p>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. LCD's can be an expensive
addition to your circuit.</p>
+ <p>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. LCD's & display's can
be an expensive addition to your circuit.</p>
</section>
<section>
<title>What is RS-232?</title>
- <p>RS-232 is the data transfer standard used on serial ports.
Basically this is composed of one start bit, some data bits, 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 witch it is transferred since the PIC does it for
us. We will only need to know the following: </p>
+ <p>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 witch it is transferred since the PIC
does it for us. We will only need to know the following: </p>
<p>1. The number of start bits (always 1)</p>
- <p>2. The number of data bits (usually 8)</p>
- <p>3. The number of stop bits (1 or 2)</p>
- <p>4. The data transmission speed</p>
- <p>You will be able to choose the transmission speed your self. The
Jallib library we will be using will use 1 start bit, 8 data bits, and 1
stop bit. Your other device, such as your PC will also need to know this
information.</p>
+ <p>2. The Parity (usually no parity)</p>
+ <p>3. The number of data bits (usually 8)</p>
+ <p>4. The number of stop bits (1 or 2)</p>
+ <p>5. The data transmission speed</p>
+ <p>6. The port number on your PC</p>
+ <p>You will be able to choose the transmission speed your self. 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.</p>
</section>
- <section><title>What do I need?</title>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:<p>1. A PIC
that has TX and RX Pin names. Most PIC's have them. Check your pinout
diagram in the PIC's datasheet.</p><image
href="images/serial_communication_16f877_pinout.JPG"/><p>2. A serial port
board. You can buy one on the net, or build your own. <xref
href="tutorial_serial_port_board.xml">See Here</xref> for more information.
A serial port board is needed for voltage conversion. Serial ports output
voltages up to 12v and go lower then 0v.</p><image
href="images/serial_communication_board_1.jpg"/><image
href="images/serial_communication_board_2.jpg"/><image
href="images/serial_communication_board_3.jpg"/><p>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.</p><image
href="images/serial_communication_cable.jpg"/><image
href="images/serial_communication_usb.jpg"/></section>
+ <section><title>What do I need?</title>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:<p>1. A PIC
that has TX and RX Pin names. Most PIC's have them. Check your pinout
diagram in the PIC's datasheet.</p><image
href="images/serial_communication_16f877_pinout.jpg"/><p>2. A serial port
board. You can buy one on the net, or build your own. <xref
href="tutorial_serial_port_board.xml">See Here</xref> for more information.
A serial port board is needed for voltage conversion. Serial ports output
voltages up to 12v and go lower then 0v.</p><image
href="images/serial_communication_board_1.jpg"/><image
href="images/serial_communication_board_2.jpg"/><image
href="images/serial_communication_board_3.jpg"/><p>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.</p><image
href="images/serial_communication_cable.jpg"/><image
href="images/serial_communication_usb.jpg"/></section>
<section>
<title>Build your circuit</title>
<p>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:</p>
<image href="images/serial_communication_schematic.jpg" width="500"
otherprops="clickable"/>
- <p>When If your circuit doesn't work when you test it, your
serial port board may have TX and RX switched. You can try to switch the
two pins around.</p>
</section>
<section>
- <title>Write your code</title>
- <p>Since this is one of the first circuits you will be building,
lets continue with your code from "Blink a led". We will modify
it to send data to your PC.</p>
+ <title>Test your circuit</title>
+ <p>Before you write your own code, you should make sure your circuit
actually works. </p>
+ <p>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.</p>
+ <p>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:</p>
+ <xref href="http://realterm.sourceforge.net/"
format="html">http://realterm.sourceforge.net/</xref>
+ <p>Open RealTerm and click on the "Port" tab, we need to
select the port & speed, etc to the following values:</p>
+ <p>1. The Parity = no parity</p>
+ <p>2. The number of data bits = 8</p>
+ <p>3. The number of stop bits = 1</p>
+ <p>4. The data transmission speed = 115200</p>
+ <p>5. The port number on your PC</p>
+ <image href="images/serial_communication_realterm_port.jpg"
width="400" otherprops="clickable"/>
+ <p>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. </p>
+ <image href="images/serial_communication_realterm_hello.jpg"
width="400" otherprops="clickable"/>
+ <p>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 PC's have more then
one serial port.</p>
+ <p>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. </p>
+ <image href="images/serial_communication_realterm_send.jpg"
width="400" otherprops="clickable"/>
+ <p>Now please change your RealTern settings to receive decimal
numbers by clicking on the "Display" tab, and choos
"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.</p>
+ <p>int8 - shows integer numbers in RealTerm</p>
+ <p>Ascii - shows ascii text</p>
+ <p>Hex[space] - shows hex numbers with a space between each</p>
+ <image href="images/serial_communication_realterm_65.jpg"
width="400" otherprops="clickable"/>
+ </section>
+ <section>
+ <title>Write code to send data from PIC to PC</title>
+ <p>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:</p>
+ <codeblock>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
+--</codeblock>
+ <p>First we need to add serial functionality, I got the following
code from 16f877a_serial_hardware.jal</p>
+ <codeblock>-- ok, now setup serial;@jallib section serial
+const serial_hw_baudrate = 115_200
+include serial_hardware
+serial_hw_init()</codeblock>
+ <p>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"</p>
+ <p>This code will set your baud rate (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.</p>
+ <p>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:</p>
+ <codeblock>serial_hw_data = 65</codeblock>
+ <p>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.</p>
+ <p>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"</p>
+ <codeblock>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</codeblock>
+ <p>Or, if you wish to send Ascii letters to your PC instead, you
could use the following:</p>
+ <codeblock>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</codeblock>
+ <p>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:</p>
+ <codeblock>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</codeblock>
+ <p>Compile and burn your code to your PIC, then turn on your
circuit. You should get this while your led is blinking:</p>
+ <image href="images/serial_communication_realterm_ab.jpg"
width="400" otherprops="clickable"/>
+ <p>Awsome, 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.</p>
+ <p>If you feel your programming skills are not as good as they
should be, practice practice practice! Continue using the language
reference at <xref href="http://www.casadeyork.com/jalv2/jalv2/index.html"
format="html">http://www.casadeyork.com/jalv2/jalv2/index.html</xref></p>
+ </section>
+ <section>
+ <title>Write code to send data from PC to PIC</title>
+ <p>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 on to other things.
</p>
+ <p>Here we are going to get the PIC to recieve 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.</p>
+ <p>We will now learn to use the following variables from
serial_hardware.jal:</p>
+ <p><b>serial_hw_data_available</b> - If the PIC recieved data, this
variable will equal TRUE, otherwise FALSE</p>
+ <p><b>serial_hw_data</b> - If data is available, this variable will
contain the data</p>
+ <p>So let's modify your current loop:</p>
+ <codeblock>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</codeblock>
+ <p>First change it so it will send the number 65 to your PC every
one second:</p>
+ <codeblock>forever loop
+ _usec_delay(1_000_000) -- one second delay
+ serial_hw_data = 65 -- send 65 via serial port
+end loop</codeblock>
+ <p>We now can add an if statement to find out if there is serial
data available:</p>
+ <codeblock>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</codeblock>
+ <p>You will need to create a variable "x" before your
"forever loop", this variable will hold the data when you want to
recieve it:</p>
+ <codeblock>var byte x</codeblock>
+ <p>Now you have a place to store the data, so you may now write a
line within your "if" statement to get the data:</p>
+ <codeblock>x = serial_hw_data</codeblock>
+ <p>Then build a for loop after that to blink the led "x"
number of times</p>
+ <codeblock>for x loop -- loop (x number of times) using data
recieved on serial port
+ led = on
+ _usec_delay(250000)
+ led = off
+ _usec_delay(250000)
+end loop</codeblock>
+ <p>Here is your completed code:</p>
+ <codeblock>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</codeblock>
+ <p>As you can see, this code will do the following:</p>
+ <p>1. delay 1 second</p>
+ <p>2. send the number 65 via serial port</p>
+ <p>3. see if there is data waiting for us, if so, get it and blink
the led (the number of times of the data recieved)</p>
+ <p>4. loop back to the start</p>
+ <p>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
awsome!</p>
+ </section>
+ <section>
+ <title>PIC to PIC ommunication via serial port</title>
+ <p>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!</p>
+ <p>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 recieve data, so all you have to
do is write some sending code on one PIC and recieving code on the
other.</p>
+ <p>Build another circuit the same as your current one, then do the
following:</p>
+ <p>1. connect the TX pin from PIC # 1 to the RX pin of PIC # 2</p>
+ <p>2. connect the RX pin from PIC # 1 to the TX pin of PIC # 2</p>
+ <p>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"</p>
+ <p>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"</p>
+ <p>You should then see your led blinking on your second PIC.</p>
+ <p>Wow, that was a lot, now I think you know your stuff!</p>
</section>
</body>
</topic>
=======================================
--- /trunk/doc/dita/tutorials/tutorials.ditamap Sun Dec 6 09:49:25 2009
+++ /trunk/doc/dita/tutorials/tutorials.ditamap Sun Jan 17 23:35:40 2010
@@ -25,11 +25,10 @@
</booklists>
</frontmatter>
<chapter href="tutorial_basics.xml">
- <topicref navtitle="TODO: Introduction"/>
<topicref href="tutorial_installation.xml"/>
<topicref href="tutorial_getting_started.xml"/>
<topicref href="tutorial_blink_a_led.xml"/>
- <topicref navtitle="Setting up a serial link (UART) -- TODO"/>
+ <topicref href="tutorial_serial_communication.xml"/>
</chapter>
<chapter href="tutorial_peripherals.xml">
<topichead navtitle="Pulse Width Modulation (PWM)">
--
You received this message because you are subscribed to the Google Groups
"jallib" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/jallib?hl=en.