<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.4//EN" "http://www.oasis-open.org/docbook/xml/4.4/docbookx.dtd">
<article>
<!-- 
To create a v5 document remove DOCTYPE above and do this:
<article xmlns="http://docbook.org/ns/docbook" version="5.0">

Overview of customisation options:
http://www.sagehill.net/docbookxsl/

List of valid tags:
http://www.docbook.org/tdg/en/html/part2.html

-->


<title>Embedded systems Projects</title>


 
<sect2 id="sec:admin"><title>Administration and Evaluation</title>
<para>
	You will need to submit a formal report concerning your project work.
	The report should be approximately 4,000 words in length, and should be submitted to the Teaching Office on the first teaching day of the Lent Term.
	Students are also encouraged to submit their lab books.
</para>
<para>
	The report will normally contain sections detailing the problem being addressed, the options considered for solving the problem, the calculations made
	to ensure that the proposed solution can meet the technical limits and requirements (for example memory capacites, limits of throughput on connections),
	and the way in which it is proposed to test to show that the solution does in fact work.
</para>
<para>
	Since some of these projects have not been tried before, it may well be that the eventual solution is different from the one originally envisaged.
	If this is the case, then add a section explaining the difficulty encountered with the original approach to the problem, and details of the
	workaround or improvement made.
</para>
</sect2>








<sect2 id="sec:support"><title>Support</title>
<para>
	Many of the projects use the same basic functionality. We have made a Printed Circuit Board [PCB] available, which may help you.
	Details are available here: <ulink url="http://www.cl.cam.ac.uk/teaching/1011/P31/project/project_pcb.pdf"/>
</para>
<para>
	You may have ideas for a project other than the ones on the project_suggestions list. If so, talk to Dr Wassell or Brian Jones early on, to make
	sure both that the project is suitable, and that we can get hold os any parts required. There is a huge range of sensors available, many of which
	produce a voltage, and so are easy to interface to a microcontroller, plus a large number of sensors and peripherals use interconnections for
	which there is support built into the microcontroller, for example I2C bus or SPI bus.
</para>
<para>
	To make writing your report easier, and also to make it easier for the demonstrators to help you with your project, keep paper or electronic copies
	of relevant datasheets, and keep notes in a lab book of key items which are relevant to the project, such as any unusual properties for the parts you are using.
</para>
</sect2>




<sect2 id="sec:advice"><title>General project advice</title>
<sect3>
<para>
Choice of microcontroller
</para>

<para>
	There are basically 4 choices which we have available in a Dual In Line (DIL) package. (There are lots more available in surface mount packages, but they require Printed Circuit Boards rather than the prototyping boards we have).
	All 4 devices have a  built in temerature sensor, and all three are available in a 'V' version with a lower maximum clock speed but the ability to work at supply voltages down to 1.8V instead of 2.7V
	All 4 devices have much the same functionality available.  Data sheets for each are in the docs folder.
</para>
	

<itemizedlist>
<listitem>
<para>
		ATMEGA168 - the one you have been using.  1K of ram, 1 USART
</para>
<para>
		The makefile settings for the MCU (line2 of the Makefile) and the avrdude programming are:
</para>
<code>
			MCU=atmega168
		avrdude -p m168p
</code>
</listitem>
	
	
<listitem>
<para>
		ATMEGA328 - the one you have been using, but with 2K of ram, 1 USART
</para>
<para>
		The makefile settings for the MCU (line2 of the Makefile) and the avrdude programming are:
</para>
<code>
			MCU=atmega328
		avrdude -p m328p
</code>
</listitem>
	
	
	
<listitem>
<para>
	ATTINY45 - a physically small, 8 pin device, with much the same functionality as the ATMEGA168, but usually the pin count .
	  Only 256 bytes of ram on this device.  You might want to use this one where space is at a premium, or where you are using it as a sensor.
	  When using this device it may be necessary to move it to a programmer rather than program it in place.
</para>
<para>
		The makefile settings for the MCU (line2 of the Makefile) and the avrdude programming are:
</para>
<code>
		MCU=attiny45
		avrdude -p t45
</code>
</listitem>


<listitem>
<para>
	ATMEGA644 - 2 USARTs, 4K of RAM, physically large (50 mm long x 18 mm wide). GPS and serial interfacing projects may need to use 2 USARTS.
	Data logging projects which write large amounts of data to SD cards will need the RAM capacity.
</para>
<para>
		The makefile settings for the MCU (line2 of the Makefile) and the avrdude programming are:
</para>
<code>
		MCU=atmega644p
		avrdude -F -p m644p
</code>
</listitem>
</itemizedlist>
</sect3>



<sect3>

<para>

Choice of speed
</para>
<para>

	If the project will eventually be battery powered, choose a frequency for the microcontroller within the range suitable for the battery voltage.
	You can always test on the bench using 5V, but changing the operating speed part way through the project is likely to cause problems.
	In general you should choose top operate the CPU as fast as possible, unless you are really trying to minimise the current drawn. It uses the same power
	to finish the processing in half the time and go into sleep mode, as to run at half the CPU speed.
</para>

</sect3>



<sect3>
<para>
Storage
</para>
<para>
	If you can process your data on the microcontroller, do that.
	If you need to store up to 2 Mbytes, you can add flash memory to the SPI bus, but will need to write a 256 bytes page at a time.
	If you need to store up to 2 Gbytes, then you can use an external MicroSD card, but will need to use a microcontroller with at least 2K RAM, plus you need to provide
	   a function to provide a timestamp for your files (you can either add a Real Time Clock (RTC) or provide a dummy function to write a plausible timestamp.
</para>
	   
</sect3>
	   
	   
	   
<sect3>
<para>
Libraries
</para>
<para>

There is software support in the form of example implementations or libraries for the following devices:
</para>
<itemizedlist>
<listitem>
<para>
	DS1302 Real Time Clock  <ulink url="http://www.cl.cam.ac.uk/teaching/1011/P31/docs/rtc.c"/>
</para>
</listitem>

<listitem>
<para>
	M25P16 SPI Flash memory <ulink url="http://www.cl.cam.ac.uk/teaching/1011/P31/code/serial_ram.c"/> requires SPI support  <ulink url="http://www.cl.cam.ac.uk/teaching/1011/P31/code/spi.c"/>
</para>
</listitem>

<listitem>
<para>
	MicroSD memory card and FAT 16 filesystem   <ulink url="http://www.cl.cam.ac.uk/teaching/1011/P31/code/ff.c"/>    Requires a real time clock (or dummy functions), and SPI support  <ulink url="http://www.cl.cam.ac.uk/teaching/1011/P31/code/spi.c"/>
</para>
</listitem>

<listitem>
<para>
	SCA3000-E01 3 axis accelerometer   <ulink url="http://www.cl.cam.ac.uk/teaching/1011/P31/code/accelerometer.c"/>   requires SPI support   <ulink url="http://www.cl.cam.ac.uk/teaching/1011/P31/code/spi.c"/>
</para>
</listitem>
</itemizedlist>
</sect3>



<sect3>
<para>
 	Soldering a row of 5 pins in to JP3/4 and connecting 2->3 with a shunt will form a loopback (echo) connection.
</para>
<para>
Serial 
</para>
<para>
 	You have used serial communications using the RS232 protocol, using the serial_to_USB converters.
 	If you need to use the COM1 ports on the PC instead, then you need to know that they are device /dev/ttyS0 in Linux.
 	They are there permanently - they do not appear and disappear on plug in/out like USB devices.
 	You will find on first use that you don't have access to /dev/ttyS0.  To fix this type
 	sudo chown &lt;crsid&gt; /dev/ttyS0
 	replacing &lt;crsid&gt; with your crsid. You will be asked for your login password - this is the Operating system's way of
 	getting confimation that you want to run a priveleged command (in this case chown)
</para>
<para>
 	If you are using the level converter PCB, then, once connected to the PC you can look at JP3 and JP4 pin3 to see the incoming signal from the PC.
 	Typing characters into minicom with the port set to /dev/ttyS0 should be visible with an oscilloscope.
</para>
</sect3>




<sect3>
<para>
6 pin programming header
</para>
<para>
	The Tuxgraphics programmers you have used so far have a 5 pin connector.
	On some of the Printed Circuit boards, you may see a 6 way connector, whose pin connections are as follows:
<code>	
	GND
	Reset
	(+5V)  rarely used or connected
	SCK
	MISO
	MOSI
</code>
	I will provide adaptors where necessary
</para>
</sect3>
	
 
<sect3>
<para>
Devices
</para>
<para>
	We buy most of our devices from Farnell (onecall.farnell.com) or RS components (rswww.com)
	If the part or the bag it is in has a 6 or 7 digit number, it is very likely to be either the Farnell or RS part number
	and you can use the website to look up data for the part.
</para>
</sect3>




<sect3>
 	
<para>
	Development
</para>

<para>	
	During the planning stage, work out a series of steps to get to the eventual goal, and how you will test each stage.
	Take notes as you go, which should help you when writing the report at the end of the project.
</para>

<para>	
	Work incrementally, testing as you go, or you run the risk of getting stuck at a complex part of the project, and after considerable time discovering
	that the fault was in something simple from an earlier stage which is not working as expected.
</para>

<para>	
	Even if the project will eventually be battery powered and mobile, do as much development and testing as possible in the lab environment, and only
	go mobile when you can do no more in the lab.
</para>

<para>	
	You should consider using several computers to speed your development - develop, read code and program your device from one, and have minicom or other 
	test programs ready to go on another.  Remember that you can be logged in to multiple computers, and that you can have multiple terminal windows open on each.
</para>

<para>	
	When developing, as you get to a stage where something is working, add a brief comment to the file, and store a copy. That way you can go back to a working
	version later. 
</para>
</sect3>


</sect2>







<sect2 id="sec:power"><title>Notes on power consumption</title>
<para>

<orderedlist>

<listitem>
<para>
	Sleep. Spend as much time as possible in the lowest power sleep mode. Beware though, of turning off the clock which
	is running the wake-up source.
</para>
</listitem>

<listitem>
<para>
	Turn off parts of the microcontroller which aren't in use. As above, beware turning off the clock which runs the timer
	which you need to wake.  There are no warnings to tell you that you have done this.
</para>
</listitem>

<listitem>
<para>
	Don't let inputs float. Pull them up, or turn them into outputs, and assign them to be high or low depending whether the load is
	attached to the supply or ground respectively.
</para>
</listitem>

<listitem>
<para>
	Beware of switches on inputs. As inputs the pullups will cost power if the switch is closed. If you aren't using a switch, make the pin
	an output and set it low. Alternatively, choose the default position of the switch to be all open.
</para>
</listitem>

<listitem>
<para>
	 The eye will detect a 20mS flash on an LED, so you can keep flashes to this length to save power. LEDs need a few mA to light up.
</para>
</listitem>

<listitem>
<para>
	On the project PCB, if you aren't using the voltage reference IC2, remove the link to the positive supply, as the reference needs 210uA to work.
</para>
</listitem>

<listitem>
<para>
	Also on the project PCB, be aware that if C1 is fitted it will take time to charge through the resistor, which will disrupt any current measurements you make.
</para>
</listitem>

</orderedlist>

</para>
</sect2>




<sect2 id="sec:pitfalls"><title>Common pitfalls</title>

<para>
	Here is a list of common pitfalls, in no particular order.
</para>



<sect3 id="sec:speed_power"><title>Speed vs power and working voltage</title>

<para>
	The microcontroller uses much less power at 3.3 Volts than at 5 Volts, and less still at lower voltages, but beware that 
	below 2.7 Volts it cannot run at full speed.
</para>
<para>
	If your project needs to be battery powered, use the Lithium batteries, and run the micro either at battery
voltage, or at 3.3 Volts through a voltage regulator.  A voltage regulator costs 17uA of idle current, but the
reduction in consumption of the other circuitry probably saves you more.
</para>
</sect3>
	
	



<sect3 id="sec:interrupts"><title>Interrupts</title>

<para>
	One of the easiest traps to fall into is to spend so long in the interrupt routine, that another interrupt has occurred before the first one is finished. 
	If timing is critcal, then the extra latency before the second interrupt is serviced will probably cause a failure. 
	This suggests doing the minimum possible in the interrupt service routine (ISR), eg setting flags, and the maximum outside the ISR.
</para>
<para>
	A different strategy to the above is as follows:
</para>
<para>
	Do everything in the interrupt routines, and nothing in main. Make sure that each interrupt handler sets up the conditions for the next interrupt to occur. 
	If only one interrupt is enabled at a time, then there is no potential to clash.
</para>
<para>
	Make sure that all interrupts are set up and conditions cleared before the Master Interrupt Enable bit is set, otherwise an interrupt may occur immediately.
</para>
<para>
	If you use global variables which are changed in more than one place, declare such variables as <code>volatile</code>, otherwise the compiler will optimise the code, and not notice that the variable has changed.
</para>

</sect3>
	


<sect3 id="sec:io"><title>I/O</title>

<para>
	It is very easy when modifying code to forget to define a new output. Everything will compile correctly, but the pin will not behave as you expect.
</para>
<para>
	It is useful to copy a line such as <code> PORTB |= (1&lt;&lt;PB1)</code>, changing it to <code>PORTB &amp;= ~(1&lt;&lt;PB1)</code>. 
	However, omitting the brackets in the second case will cause problems because of operator precedence.
</para>
<para>
	Except while initialising, it is <emphasis>very</emphasis> rare that you will need assignment such as <code>PORTB = (1&lt;&lt;PB2); </code>
	You almost always need <code> |= </code> or <code> &amp;= </code>
</para>

</sect3>
	
	

<sect3 id="sec:atod"><title>Analogue to Digital converter</title>

<para>
	The most common pitfall with the A to D is forgetting to supply a reference volatge at AVcc. Zero or very low readings are a symptom of this.
</para>
<para>
	The next most common is having too high a clock rate. If it is too fast then only the first few bits of the data will be correct.
</para>
<para>
	Selecting the wrong analogue input is the next most common error.
</para>

</sect3>
	
	
	
<sect3 id="sec:fuses"><title>Fuse errors</title>

<para>
	If you can no longer program the fuses, have you disabled the clock by mistake ? 
	This is particularly easy to do when chaning from an internal to an external or crystal derived clock.
</para>
<para>
	If you reprogram the fuses, the device does not automatically restart. You must power cycle it. 
	Changing the program <emphasis>does</emphasis> however force a restart.
</para>

</sect3>
	

</sect2>



</article>

