Tom's Main Menu

Physical Computing Home

Intro to Physical Computing Syllabus

Networked Objects

Sustainable Practices

blog

Resources

code, circuits, & construction

my del.icio.us links

 

Lab Assignment

 

Serial output

   

Minimum parts needed: (new parts in bold. see parts list for details)

  • Prototyping board (breadboard)
  • Power supply connector
  • 5-15VDC power supply
  • Assorted wires
  • 5V regulator
  • PIC 18F452 or BX-24
  • Serial cable
  • DB9 female serial connector & headers (2)
  • LED's
  • Switch
  • 10Kohm resistors
  • 220 ohm resistors

Step 1:

Connect a serial cable between the appropriate pins of your microcontroller (RC6 and RC7 on a PIC, 11 and 12 on a BX-24) and ground as shown:

PIC 18F452 with serial connector on pins RC6 and RC7.

BX-24 with serial connector on pins 11 and 12, left. The image at right shows the ground connection hidden beneath the connector.

Program the microcontroller to send a string of serial information out as follows:

PIC:

' serial out is on portc.6
' serial in is on portc.7
' a digital input is on portb.0

'set a constant with the baudmode 9600-8-n-1-inverted:
inv9600 con 16468

' a byte to send out data:
thisByte var byte

' set portb.0 to input:
input portb.0

pause 500 ' start program with a half-second delay

main:	

  ' read the switch, convert it to a readable ASCII value:
	 thisByte = portb.0

  ' send it out the serial port:
	 serout2 portc.6, inv9600, [DEC thisByte]

goto main

BX24:

dim inputBuffer(1 To 13) As Byte '4-byte output buffer.
dim outputBuffer(1 To 50) As Byte '1-byte output buffer.
dim thisByte as byte

sub main ()
  call delay(0.5)     ' start program with a half-second delay
  ' define which pins COM3 will be 
  ' (input pin, output pin, baud mode):
	 call defineCom3(12,11,bx1000_1000)

  ' set aside memory for input and output:
	 call openQueue(inputBuffer, 13)
	 call openQueue(outputBuffer, 50)

  ' open COM3:
	 call openCom(3, 9600, inputBuffer, outputBuffer)
	
	 do
	
    ' read the switch, convert it to a readable ASCII value:
	   thisByte = getPin(13) + 48

    ' send it out the serial port:
	   call putQueue(OutputBuffer, thisByte, 1)
	 loop

end sub

Open Hyperterminal on a PC, or zterm on a mac. Set the connection settings to 9600 baud, 8 bits, no parity, 1 stop bit. Make sure your serial cable is connected to the port your software is listening to. Connect the serial cable to your microcontroller. See what comes up in the Hyperterminal or zTerm window.

Question: In the BX24 code, why add 48 to the value of the input before sending it out? What instruction in the PIC code does the same thing for us?

 
Step 2:

Connect an analog sensor to one of your analog inputs (see the analog in lab if you forgot how). Read its value and send it out the serial port. Modify the program above as follows:

PIC:

Add the analog input DEFINE statements as needed.

Add a word variable to read the ADC value into (call it ADCvar)

Convert ADCVar to a byte. If its range is 0 to 1024, you can do it as follows:

thisByte = ADCvar / 4

BX-24:

In the main do loop change the getPin() line to this:

thisByte = cByte(getADC(14)\4)

This will read the ADC on pin 14 (as an integer), divide the result by 4, and convert that result to a byte.

Question: why convert the result of the ADC reading to a byte?

Question: why divide the result by 4?

Question: why does the result look like gibberish in hyperTerm?

Step 3:

Detach the serial cable from your serial output pin. Attach an LED to the serial output pin, as follows:

Notice how the LED blinks when you are sending serial messages. This can be an excellent way to test to make sure your chip is properly sending serial data out.

 
Step 4:

Make a Luv-o-Meter like the kind often seen in cheesy bars. Have the microcontroller give the user text feedback about his or her interaction with the sensors.

For BX-24 users, here's how to send a string of text from the BX-24:

First, make sure your output buffer is larger than the string you want to send. For example, if you plan to send a 20 byte string, your output buffer would need to be at least 29 bytes. Make sure the maximum string length is set to at least 29 bytes in the editor's environment menu. You can set it much higher if you want.

Second, use the putQueueStr command to put your string in the output queue (for more details, see the system Library):

dim inputBuffer(1 To 13) as byte
dim outputBuffer(1 To 50) as byte


Sub main()
	call delay(0.5)  ' start  program with a half-second delay 

    dim myString As String

 ' define which pins COM3 will be 
' (input pin, output pin, baud mode):
	call defineCom3(12,11,bx1000_1000)

 ' set aside memory for input and output:
	call openQueue(inputBuffer, 13)
	call openQueue(outputBuffer, 50)

 ' open COM3:
	call openCom(3, 9600, inputBuffer, outputBuffer)
do
    myString = "hello world!" & "switch state: " & chr(getPin(13))
    call putQueueStr(outputBuffer, myString)

    ' Append carriage return and line feed
    call putQueueStr(outputBuffer, chr(13) & chr(10))
loop

End Sub