5 - Photovore using the C++ library

Submitted by Webbot on April 27, 2008 - 10:27am.

Unzip the three files from the 'C++ Photovore.zip' file into another folder. I will assume its called 'c:/SoR/testCPP'. Here is a description of the files:-

 

main.hpp

This is the main header files that includes all of the required header files from the C++ library and from the avr library.

 

main.cpp

This is the main loop of the Photovore.

Line 7: 'TIMER g_timer' defines a timer that can be accessed in your own code but is also used by some of the library routines.

 

Line 9: 'LED g_led('D',1) defines an LED on D1

 

Lines 12 to 18: Either defines that you are using servos on D2 and D3 (as per the $50 Tutorial) or, if you uncomment line 5, will define that your servos are connected on B1 and B2 and will use PWM to set their speed. If you use the PWM route then make sure that pins B1 and B2 are connected to your motor supply rather than your +5v microcontroller supply.

 

The remaining code is very similar to the $50 Tutorial except:

Line 28: - stores the current time of day in milliseconds

Line 57: - waits until 20milliseconds has passed since the start of the loop.

 

makefile

The key entries in the makefile on lines 22 and 23. These should be changed to the microcontroller you are using and the clock speed. (The library currently only supports 1MHz and 8Mhz). NB If you change either of these values in the makefile then you will also need to change the same values in the makefile for the C++ library and then rebuild it using 'make clean', followed by 'make' - see Installation. If this becomes a real pain then you could rename each compiled library ie you could rename 'cpplib.a' to 'cpplib_Mega8_1MHz.a'.

 

Line 30: LIBS = c:\SoR\C++\bin\cpplib.a

This tells the linker to use the compiled code from the C++ library so you may need to change this to point to the folder where you have created it on your system.

 

Compiling your program

From a DOS/Command window - change to your project folder:-

CD c:/SoR/testcpp

 

Compile your program by typing:-

make

 

 

When the program is compiled you can upload the .hex file in the normal way.