Home
MicroCamp2.0 - SparkFun Electronics
Contents
1. 16 digit per line 16 Figure 8 1 Details of SLCD16x2 s jumper selections 3 Baudrate select jumper 2 selections as 2400 and 9600 bps bit per second with 8N1 data format 8 bit data no parity bit and 1 stop bit 4 Interface signal jumper 2 selections as Invert logic TL C MOS level or RS 232 IN and Direct logic TL CMOS level DI SLC D16x2 provides a brightness adjustment with variable resistor at BRIGHTNESS position Interfacing connector has 3 pins 5V Supply voltage Serial data input S and Ground G MicroCamp ATmega8 Activity Kit Manual 77 8 1 3 Interfacing SLC D16x2 with MicroCamp The J ST3AB 8 cable is required for connecting between SLCD with MicroCamp controller board This cable wire assignment can show below 2 54mm pin space 2 00mm pin space Sy Ga The J ST3AB 8 cable has the one end as2 54mm housing call Beend and another end as2 00mm housing call A end The A end will be connect to J STconnector of any port PO to P4 of MicroCamp controller board The Bend is connected to the input connector of SLCD16x2 After conecting set all jumpers following the figure below i e Select command mode to Standard ST e Select the lines display to 16 digit per line 16 e Select baudrate to 9600 bps 96 e Select the interface siganl to Direct DI 78 MicroCamp ATmega8 Activity Kit Manual 8 1 4 Data and Command sending On
2. 10 2 How the IR Ranger Module works Measuring range can be done in many ways The easiest to understand is through ultra sonic where sound wavesare sent to the object and the time it takesto reflect back is measured This is because sounds waves do not travel fast and can be measured by present day equipment However in the case of infrared light the time it takes to hit an obstacle and reflect back can not be measured because infrared light travels fast No measurement equipment is available yet Therefore the following theory must be used The infrared light is sent out from a transmitter to the object in front by passing through a condense lensso that the light intensity isfocused ona certain point Refraction occursonce the light hits the surface of the object Part of the refracted light will be sent back to the receiver end in which another lens will combine these lights and determine the point of impact The light will then be passed on to an amay of photo transistors The position in which the light fallscan be used to calculate the distance L from the transmitter to the obstacle using the following formula Te F A X Therefore L equals _FXA X L Thus the distance value from the phototransistors will be sent to the Signal Evaluation Module before it is changed to voltage resulting in a change of voltage according to the measured distance Transmit LED Photo array GP2D120 ix MicroCamp Almega8 Activity K
3. 0000000001000110 0000000010011100 AND 0000000001000110 0000000000000100 gt 0x0004 or 0x04 2 result2 0000000010011100 0000000001000110 0000000010011100 OR 0000000001000110 0000000011011110 gt 0x00DE or 0xDE 3 result3 0000000010011100 0000000001000110 0000000010011100 XOR 0000000001000110 0000000011011010 gt 0x00DA or OxDA 4 result4 0000000010011100 Invert all bit 1111111101100011 gt OxFF63 50 MicroCamp Almega8 Activity Kit Manual 5 3 2 Shift bit operation In the shifting bit operation you must determine the number of shifting such as dat dat lt lt 4 It means shifting to the left of the dat variable 4 times and storing the result into the dat again Another example is dat dat gt gt 1 If means shifting to the right of the dat variable 1 bit and storing the result into the dat again Example 5 11 int dat xl x2 dat 0x93 Find the result of 1 x1 dat lt lt 1 2 x2 dat lt lt 2 Solution dat 0x93 gt 0000000010010011 dat 0000000010010011 X1 0000000100100110 X2 0000001001001100 1 x1 is Shifting left 1 bit results in a dat vairable Thus x1 0x0126 or 294 in decimal number 2 x2 is Shifting left 2 bits results in a dat vairable Thus x2 0x024C or 588 in decimal number Example 5 12 Int ana bag es a Ox7A b 0x16 c OxFD Find the result of 1 a amp Ox3C 2 b 0x51 3 c OxDO MicroC
4. 1 lt lt TOV2 TCCR2 0 Config and Start up timer 2 Calculate delay for baudrate Calculate Delay for baudrate Stop timer Ensure Overflow flag clearing Load Prescaler from calculate Load interval Wait until count success Ensure Clear Overflow flag Stop timer 2 Send Data 1 Byte void serout_byte char tx unsigned char dat int i out_c tx 0 delay baud base for i 0 i lt 8 i out_c tx dat amp 0x01 delay_baud base dat dat gt gt 1 out_c tx 1 delay_baud base Send More Than 1 byte start bit Delay for Send data Delay for Shift for stop bit Delay for void serout_text char tx unsigned char p while p serout_byte tx p endif INNOVATIVE EXPERIMENT MicroCamp ATmega8 Activity Kit Manual 129 start bit bit calculate base next bit stop bit 130 MicroCamp ATmega8 Activity Kit Manual COPYRIGHTS This documentation is copyright 2006 2007 by Innovative Experiment Co Ltd INEX By downloading or obtaining a printed copy of this documentation or software you agree that it is to be used exclusively with INEX products Any other uses are not permitted and may represent a violation of INEX copyrights legally punishable according to Federal copyright or intellectual property laws Any duplication of this documentation for commercial uses is expressly prohibited by INEX Duplic
5. 5V below y 2mm ca 1 3 3 CX 4 serial portcable This is used to connect between the computer s RS 232 serial port and the target orextemal device such asa Microcontroller board eg The MicroCamp controller board The connectors end uses a DB 9 female connector and the other end uses a Modular plug RJ 11 6P4C 6 pins form and 4 contacts Its Length is 1 5 meters In the kit thiscable is used to connect between RS 232 seral port and PX 400 programmer box The wire assignment is shown in the diagram below 2mm pitch GND 5v gt S Data DB 9 female connector RJ11 6P4C modular plug 7 Ek conductor side G TxD DTR G GND MicroCamp ATmega8 Activity Kit Manual 9 1 4 ATmega8 microcontroller Overview The ATmega8 is a low power CMOS 8 bit microcontroller based on the AVR enhanced RISC architecture By executing powerful instructions in a single clock cycle the ATmega8 achieves throughputs approaching 1 MIPS per MHz allowing the sytem designer to optimize power consumption versus processing speed The ATmega8 which use in MicroCamp board is 28 pin DIP package The pin assignment shows in the figure 1 4 1 4 1 ATmega8 features e It isa low power 8 bit microcontroller based on the AVR RISC architecture e 8K bytesof In System Programmable Flash with Read While Write capabilities 10 000 times erase cycle 512 bytes of EEPROMwith 100 000 times erase cycle 1K byte of SRAM and 32 general purpose wor
6. Data input in Slave mode of SPI bus and ISP Data output in Master mode of SPI busand ISP m foe Input Output PB4 Digital port Input Output Data input in Master mode of SPI bus and ISP Data output in Slave mode of SPI bus and ISP Input Output PBS Digital port Input Output Clcok input in Slave mode of SPI bus and ISP Clcok output in Master mode of SPI bus and ISP Input Output PB6 Digital port when config CPU operate with internal clock Input External clock input Connect with Crystal or Ceramic Resonator Input Not use when config CPU operate iwth internal clock m Oo Input Output PB7 Digital port when config CPU operate with internal clock Input Connect with Crystal or Ceramic Resonator Output Clock output when config CPU operate with internal clock U DU Q WF A a m Ko Table 1 1 Pin function summary of ATmega8 microcontroller continue MicroCamp ATmega8 Activity Kit Manual 13 Name Pin number Type Description PCO 23 Input Output PCO port ADCO Input Analog input channel 0 PC1 24 Input Output PC1 port ADC1 Input Analog input channel 1 PC2 25 Input Output PC2 port ADC2 Input Analog input channel 2 PC3 26 Input Output PC3 port ADC3 Input Analog input channel 3 PC4 27 Input Output PC4 port ADC4 Input Analog input channel 4 SDA Input Output Serial data in 2 Wire bus interface PC5 28 Input Output PC5 port ADC5 Input Ana
7. both regularand rechargeable Transmits serial data using the RS 232 standard with 1200 bps baud rate and 8N1 data format 8 data bit no parity 1 stop bit O d e 5 gt 8 re C O Figure 11 1 Shows the photo board layout and Schematic of ER 4 remote control ER 4 remote control and ZX IRM 38kHz Infrared receiver module are optional componnets of MicroCamp2 0 Beginner kit and bundled in Standard kit 110 MicroCamp ATmega8 Activity Kit Manual 11 1 1 Format of data sent by Easy Remote4 To make it easier forthe receiverto read the switch value from the remote control the ER 4 transmit serial data isaccording to the RS 232 standard with a baud rate of 1 200 bpsand 8N1 format Characters are transmitted according to what switch is pressed on the remote The switch positions are displayed in Figure 11 1 Press switch A the large cap A followed by small cap A a issent Press switch B the large cap B followed by small cap B b is sent Press switch C the large cap C followed by small cap C c issent Press switch D the large cap D followed by small cap D d is sent The reason that we have to altemate large cap and small cap letters is so that the receiver can differentiate if a user presses continuously or if the user represses If a user represses the large cap character will be sent the first time If the user represses the
8. dat 4 9 dat 5 11 dat 6 13 dat 7 15 For calling of individual cells after which char i j 1 3 j dat lie Jj dat lal gt j dat 3 gt j 7 The result is j 7 Example 4 15 char dat 4 abcd This declares the array dat It has 4 cells and the value for each cell is as follows dat 0 a dat 1 b dat 2 c dat 3 d For calling of individual cells after which char i j I 39 j dat il j Gat i gt j dat 3 gt j d The result is j d The array variable can be declared as a global variable or a local variable It can be used in parameters when transferring of data into the function 42 MicroCamp ATmega8 Activity Kit Manual 4 8 1 2 The 2 Dimension Array The declaration form of this two dimension amay is type name x yl This command shows a 2 dimensional amay type variable type is the Data type of Amay variable name is the Aray variable name x is the Number of row in the amay y isthe Number of column in the amay For example int al2 5 It is declaring that a isa 2 dimensional anay It has integer types values in 10 cells a 0 0 a 0 1 a 0 2 a 0 3 a 1 0 a 1 1 a 1 2 a 1 3 For the setting of the cell values this can be done as such int menu 3 4 1 3 4 9 2 8 0 5 This would mean that menu 0 0 1 menu 0 1
9. else PORTC amp _BV _bit void out_d char bit char dat DDRD _BV _bit if _ dat PORTD BV _bit else PORTD amp _BV _bit endif MicroCamp ATmega8 Activity Kit Manual 119 sleep h Delay function library ifndef _sleep_ define sleep _ void sleep unsigned int ms unsigned int i j for i 0 i lt ms i for j 0 5 lt 795 j endif ana log h Analog reading input library unsigned int analog unsigned char channel unsigned int adc_val ADMUX 0x40 ADMUX channel Single end mode ADCSRA 0xC6 while ADCSRA amp 1 lt lt ADSC adc_val ADCL adc_val ADCH 256 return adc_val sound h Sound generator library include lt in_out h gt include lt sleep h gt void delay _sound unsigned int ms unsigned int i j for i 0 i lt ms 1i for j 0 5 lt 200 j void sound int freg int time int dt 0 m 0 Keep value and dt 5000 freq Keep active logic delay time 5 time dt Keep counter for generate sound for m 0 m lt time m Loop for generate sound Toggle logic P0 12 out_d 4 1 delay _sound dt Delay for sound out_d 4 0 delay _sound dt Delay for sound void sound_cnt unsigned char cnt int fregq int time unsigned char i for i 0 i lt cnt 1i sound freq time sleep 300 120 MicroCamp ATmega8 Activity Kit Manual led h contol library
10. o WW gt DC input_ a 4 8 6Vdc 1k T ToS NT c3 c4 N 1000 6 3V 1000 6 3V LED1 150 on NS ad 5V Vm O 16 8 C7 c8 c5 Bs T 150V 0 1 50V ae 0 1 50V 47R AW RESET C6 MOSI T RESET 1 R7 sek AAW 17 pB3 MOSI MISO R8 150R 18 11 ANA PB4 MISO ae R31 150R 19 ith 5l Oo j EN 0 1 50V O Se je K3 R10 150R MOTOR A 14 LED3 4yY GND Ler R27 R28 R25 4 7k 4 7k SW1 150R aie 4 lw o o R26 SW2 5 150R A o oe 9 CRI 16MHz 10 c12 10 16V l 0 1 50V 1k SP1 LEDS LED4 PIEZO ETO moa 5 IND1 L C11 0 1 50V Pele Figure 1 3 MicroCamp controller board schematic MicroCamp ATmega 8 Activity Kit Manual 7 1 2 MicroCamp contoller board circuit desc nption The heart of this controller board is ATmega8 microcontroller It uns on a 16MHz clock from crytal which is connected at PB6 and PB7 pin For PCO to PC4 port is defined as the new name to PO to P4 It is labeled on the circuit board for easy reference All ports can programmable to analog or digital input output Analog signal from these port would passthrough the Analog to Digital Converter module within ATmega8 The resolution conversion is at 10 bit PB3 PB4 and PB5 are In System Programming port They are connected to ISP connector for connect with the extemal ISP programmer box PC6 RESET pin is connected with the RESET swt
11. once its done 2 3 Copying Library You will need to copy the library file H file from the MicroCamp_include folder in the Cd ROM It is better to copy these files to a folder where you save your programming codes During the program development of MicroCamp with AVR Studio and WinAVR you will need to define or set the path of all the tools to integrate with the Micro Camp_include folder Ensure that the path of the MicroCamp_include folder is correct This is very important as if the path details are not clear or missing the whole compilation process will have errors i INNOVATIVE EXPERIMENT 22 MicroCamp ATmega 8 Activity Kit Manual MicroCamp ATmega8 Activity Kit Manual 23 Chapter 3 C programming development for MicroCamp kit with AVR Studio and WinAVR compiler 3 1 The heart is the C compiler In actual fact writing of the C program for the microcontroller is not the actual code that issent to the microcontrollers program memory The real data isin the machine code which is being compiled from the written C code and compiled with the C Compiler software The steps in C programming development are as follows 1 Write the C programs with the text editor Project IDE that is provided 2 Compile the C code into assembly Code for the microcontroller 3 The Assembly Code will be converted into Machine Code into HEX file format 4 Download this code into the program memory of the microcontroller 5 Ru
12. 0 j 0 void main while in_d 2 1 while 1 if analog 3 lt ADO amp amp analog 4 lt AD1 j backward 30 sleep 10 motor _stop ALL Eor i 07 eg pales sound 2500 100 sleep 50 forward 100 sleep 300 if analog 3 gt AD0 amp amp analog 4 gt AD1 forward 100 if analog 3 lt ADO s lert 100 y if analog 4 lt AD1 a righe 100 MicroCamp ATmega8 Activity Kit Manual 97 Motor control library Sensor reference value Crossing counter variable Wait for SW1 to be pressed to ff Stere Detect line crossing Move backward for a short time to brake Motor brake function Repeat the loop of crossing detection Drive sound Move forward to closs over the line Both sensor detect white surface Move forward Left sensor detects black line ff Ween lerr Right sensor detects black line Turn right gt d Listing A8 1 The C program code for controlling the MicroCamp robot to moves along the black line and detect the line crossing INNOVATIVE EXPERIMENT MicroCamp Almega8 Activity Kit Manual 99 Chapter 10 MicroCamp robot with IR Ranger Capability One of the special sensors in robotics is the It isan Infrared Distance sensor Some people call it the IR Ranger With the GP2D120 module it adds the distance measuring and Obstacle detection usi
13. 100 Generate sound if SW1 is pressed MicroCamp ATmega8 Activity Kit Manual 55 6 1 2 Sending data to output port function This function determines the port pin configures it to output and sends the value to that port These function does not retum any values out b Port B output sending function out c PortC output sending function out d Port D output sending function Function format out _ b char bit char _ dat out _c char bit char _ dat out _d char bit char dat Parameter _bit select port s pin Range is 0 to 7 _dat detemine the output value 0 or 1 to output pin Example 6 4 out e 50 Send logic 0 to PC5 port out_d 1 1 Send logic 1 to PD1 port Example 6 5 include lt avr io h gt Includes the Standard input output port library include lt in_out h gt Includes the Port control library include lt sound h gt Includes the Sound generator library void main while 1 Looping out d 171 Outs logic 1 via PD1 The LED2 indicator on sleep 300 Delays 0 3 second out_d 1 0 Outs logic 0 via PD1 The LED2 indicator off sleep 300 Delays 0 3 second 56 MicroCamp ATmega8 Activity Kit Manual 6 1 4 Invert logic output port function toggle b Port B output invert logic function toggle c PortC output invert logic function toggle d Port D output invert logic function Function
14. 3 menu 0 2 4 menu 1 0 2 menu 1 1 8 menu 1 2 0 menu 2 0 0 menu 2 1 0 menu 2 2 0 i INNOVATIVE EXPERIMENT afo 4 a 1 4 menu 0 3 9 menu 1 3 5 menu 2 3 0 MicroCamp ATmega8 Activity Kit Manual 43 Chapter5 Operators of WinAVR compiler The Operation in C program of Win AVR compiler can be divided into 3 groups which are the Arthmetic operator Relation amp logic operator and Bitwise operator 5 1 Anthmetic operator This group can be summarized into the following Operator Meaning Addition Subtraction X Multiplication Division Modulo Increment Decrement Add with the Right hand value Subtract with the Right hand value Multiply by the Right hand value Divide by the Right hand value Modulo by the Right hand value 5 1 1 Addition and Subtraction Example 5 1 int a 12 a a 3 The result is a 15 Operation Begin with a 12 Add a with 3 and store the result to a It means 12 3 15 store 15 to a 44 MicroCamp ATmega8 Activity Kit Manual Example 5 2 int a 12 a a 3 The result is a 9 Operation Begin with a 12 Subtract a with 3 and store the result to a It means 12 3 9 store 9 to a 5 1 2 and division The different of both division is 1 isthe division of numbers which will retum an integer 2 is the division of numbers which will etum with the remainder Called as
15. During the robot moves microcontroller shows the operation message on SLCD screen too The message will change if the button pressing is chenged Listing A13 1 The C program for Microcamp robot with Remote control activity final A13 6 Place the robot on the floor Tum on power A13 7 Press the ER 4 s button to send data to MicroCamp robot The direction of sending light must straight The communication will be complete Observe the robot s movement MicroCamp robot moves following the button s function a ee f INNOVATIVE EXPERIMENT 118 MicroCamp ATmega8 Activity Kit Manual MicroCamp 2 0 libranes source program in o outh Read and Wite the digital data with any port ifndef IN OUT_ define _IN OUT_ define toggle b x DDRB define toggle c x DDRC define toggle d x DDRD char in _b char _bit DDRB amp 1 lt lt bit A _BV x PORTB _BV x BV x PORTC _BV x BV x PORTD BV x return PINB amp _BV _bit gt gt _bit char in_c char _bit DDRC amp 1 lt lt bit return PINC amp _BV _bit gt gt _bit char in_d char bit DDRD amp 1 lt lt bit return PIND amp _BV _bit gt gt _bit void out_b char bit char dat DDRB BV _ bit if _ dat PORTB BV _bit else PORTB amp _BV _bit void out_c char bit char dat DDRC _BV _bit if _ dat PORTC _BV _bit
16. If more than it means the obstacle is far from the robot less than 8cm Control the robot to move backward 0 8 second and tum left 0 6 second The speed is set at 50 You can change to any value fro more suitable operation 4 If the detect value is less than 320 the robot still move forward 5 Loop the opertion Listing A11 1 The C program code for controlling the MicroCamp robot to avoid the obstacles without any physical contact with the application of GP2D120 module INNOVATIVE EXPERIMENT MicroCamp ATmega8 Activity Kit Manual 109 Chapter 11 MicroCamp robot with Remote contol In chapter 8 we introduced the simple serial communication activity by using the SLCD module It isa sending activity only This chapter is different The new component is introduced asthe Infrared remote control ER 4 This remote control works with the serial communication The button data will be modulated with 38kHz camer frequency The MicroCamp robot must connect the 38kHz Infrared receiver module to demodulate and get the seral data to processing late You can us the ER 4 remote controlto controlthe robot smovement Some activity in this chapter will show this operation 11 1 ER 4 4 ch Infrared Remote Contol Operational distance is 4 to 8 meters in open space The 4 channel switch operates in an on off mode Uses low power Automatically resumes powersave mode once data issent Uses only 2 4 3 0 V from two AA batteries
17. Library for LED indicator by Timer 2 interrupt every 5 ms include lt avr interrupt h gt include lt avr signal h gt include lt in_out h gt unsigned char LED 0 unsigned char LED cnt SIGNAL SIG _OVERFLOW2 Interval 10 ms TCNT2 178 Reload interval 10 ms TCNT2 178 LED cnt Increment Counter if LED cnt gt 30 Check Counter 10 ms X 30 LED cnt 0 Clear Counter if LED 1 Check LED1 Enable toggle _c 5 else if LED 2 Check LED2 Enable toggle d 1 else if LED 3 Check LED1 and LED2 Enable toggle _c 5 toggle d 1 void interval _init Config and Start up timer 0 TCCR2 1 lt lt CS22 1 lt lt CS21 1 lt lt CS20 Prescaler 1024 16 MHz 1 MC 1024 16M 64us count TIFR 1 lt lt TOV2 Clear Tov2 clear TIMSK 1 lt lt TOIE2 Enable Timer2 Overflow Interrupt TCNT2 178 Interval 10 ms sei Enable all interrupt void ledi1_on Start Blinking LED1 interval init LED 1 lt lt 0 void ledl off Stop Blinking LED1 LED amp BV 0 void led2_on interval _init Start Blinking LED2 LED 1 lt lt 1 void led2 off LED amp _BV 1 Stop Blinking LED2 motor h Dc motor contol library Hardware Configuration MOTOR1 PD7 Connect to 1B port PD6 Connect to 1A port PB1 Connect to 1E port MOTOR2 PBO Connect to 2A port PD5 Connect to 2B port PB2 C
18. Members fpack struct Optimization 00 Aa vi Memory Settings b is A a IV Create Hex File I Generate Map File I Generate List File _ Custom Optio Cancel Help MicroCamp ATmega8 Activity Kit Manual 29 3 3 5 3 Click on this icon and the Include Directories for determining the path of library file Find and select the library file and click on the Add button Forexample is C AVR_ROBOT include After determining the path you will found the list for selection 3 3 5 4 Select the icon Libraries to links to all the libraries with the main file Switch_LED Project Options bs Include Directories Qa C avr_robot iinclude ww General Fa Include Directories Include Directories Ex Libraries p os Memory Settings Custom Optio al Remove Move Up Move Down 3 3 5 5 At the boxs Available Link Objects click to select the item libm a and click the Right Arrow button to copy the item libm a which appears at the Link with These Objects window Click the OK button to finish Switch_LED Project Options ss Library Directories i T General laal Include Directories Libraries libprintt_min a libscant_flta p libscanf_min a os Memory Settings Z Custom Option Available Link Objects ECZA Hove dovn Link with These Objects Add Library gt Remove Object Add Object Move up Move down 30 Micr
19. Project Wizard Open Project g Save Project Rerent Projects gt MicroCamp ATmega 8 Activity Kit Manual 27 3 3 3 The properties project window will appear Set the parameter as follows 3 3 3 1 Click on this to select AVR GCC item within Project type for select type of project file to program in C 3 3 3 2 Set the project name as Switch_LED an example name This will cause the initial file section to be created This project hasa main C program file called Switch_LED c 3 3 3 3 Select the project s path in Location Example is G Work2006 AVR ROBOT Code After this click on the Finish button r Create new project Project type Project name Atmel AVR Assembler fs witch_LED RAVA GCC W Create initial fle 7 Ciest olde Initial file Switch _LED Q Location G work2006 AVR ROBOT Code Ver 4 12 462 V Show dialog at startup Finish Cancel Help 3 3 4 The Switch_LED project environment will be created as shown in the diagram below AYR Studio Switch_LED c J D Work2006 Tile Project Dad C it View Tools Debug Window telp DS XB Oo EB XB AR Poo H O avr AVR GCC P eta eens B AVR ROBOT amp source E teoder res 5 O capture EY External Dependencies m oR E O Fow 9 include O labo H O labo2 E Ie cor Switch_LED O Test Board Test_Board H O test_lib Onar Bs Gal M swih DB test_lib2 5 O test
20. Sec soft _serout_init 9600 Initital 9600 8N1 serout text 2 MicroCamp Send Text MicroCamp to SLCD while 1 Stop Program description This code will refer 2 libraries soft serout h for sending the serial data and sleep h for delaying The program operation are 1 Delay 1 second for SLC D16xz2 initial 2 Set the baudrate In this program is 9600 bit per second 3 Send mthe message MicroCamp to P2 port to display on the SLCD16x2 4 Loop operation Listing A4 1 The simple program for sending the message to display on the SLCD16x2 ICR CAMP MicroCamp ATmega8 Activity Kit Manual 85 Activity 5 Control the SLC D16x2 with command User can control many the display operation of SLCD16x2 such as set the line display clear screen select the display format etc with seding the control command to SLCD16x2 For the Standard command mode start byte must start with OxFE and following the command User can see the LCD command in SLCD information topic in this chapter A5 1 Open the AVR Studio to create the new project and write the C program following the Listing A5 1 and build this project A5 2 Connect the PX 400 programmer to the MicroCamp board on The MicroCamp robot at the In System Prog connector Tum on the Robot Downlaod the HEX code to the robot A5 3 Tum off powerand Remove the ISP cable A5 4 Tum on the power and observe the SLCD16x2 operation The SLCD16x2 show many message
21. buff 0 Convert float to string used libm a Cross address long type Cross l parameter Send data to LCD Increase address 1 time 128 MicroCamp ATmega8 Activity Kit Manual soft serouth Serial data output library for any port of ATmega8 microcontoller not use UART include lt avr io h gt include lt in_out h gt ifndef soft _serout_ define _soft_serout_ define PRESCALER 1 1 lt lt CS20 define PRESCALER 8 1 lt lt CS21 define PRESCALER 32 1 lt lt CS21 define PRESCALER 64 1 lt lt CS22 define OFFSET DELAY120 1 lt lt CS20 YH define OFFSET DELAY218 unsigned int base 0 unsigned char base_start_rcv 0 unsigned char TCCR2_ cal 0 unsigned int base unsigned int baud 9600 1 16M 0 0625 us per MC 8 16M 0 5 us per MC 32 16M 2 us per MC 64 16M 4 us per MC for out function used 20 us for out function used 20 us void soft _serout_init char tx unsigned long baud_ unsigned long tick 0 out_c tx 1 if baud_ lt 4800 tick 1000000 baud_ OFFSET_DELAY1 4 TCCR2 cal PRESCALER 64 else if baud_ gt 4800 amp amp baud_ lt 9600 tick 1000000 baud_ OFFSET_DELAY2 2 TCCR2 cal PRESCALER_ 32 TCCR2 0 TIFR 1 lt lt TOV2 base 255 tick base start rev 255 tick 2 Delay for baudrate void delay baud unsigned int _tick TCNT2 _tick TCCR2 TCCR2 cal while TIFR amp 1 lt lt TOV2 j TIFR
22. char arg pp char ptr buff 16 pp amp p ptr p arg pp arg 2 if uart_ini 0 uart_ini 1 uart_set_baud _baud while ptr if TEST CHAR TYPE ptr uart_putc toascii arg argt 2 ptr else if TEST INT TYPE ptr Pointer of point s arg offset 0 s arg i 0 Copy address Copy address of p point Cross 2 time go to Origin of first parameter Check data pointer 0 Cross address char type Cross d parameter p uart_puts p arg 2 ptr else if TEST_LONG TYPE ptr i MicroCamp Almega8 Activity Kit Manual 127 ltoa unsigned int arg amp buff 0 10 Cross Cross address int type a parameter Cross address long type p ltoa long arg amp buff 0 10 uart puts p arg 4 ptr t else if TEST FLOAT TYPE ptr p dtostrf float uart puts p arg 4 ptr t else ptr uart_putc ptr char uart_getkey a char _c key if uart_ini 0 uart_ini 1 uart_set_baud _baud _key 0 return _ c char uart_getkey unsigned int timeout unsigned int cnt 1 char _c 0 if uart_ini 0 uart_ini 1 uart_set_baud _baud while _ key amp amp cnt lt timeout ent _c _key _key 0 return c Li Cross l parameter arg 2 F PREC amp
23. direction to its left to avoid the object 72 MicroCamp ATmega8 Activity Kit Manual include lt in_ out h gt include lt sleep h gt include lt motor h gt void main while in_d 2 1 ee while 1 bil if in_c 2 0 bi backward 100 sleep 400 s lett 50 silleep 300 else if in_c 3 0 backward 100 Mi Ml sleep 400 s right 50 M siiecpkeooi else forward 100 bd bi Loop until SW1 is pressed to start the program Repeat loop Check status of the right switch If ther is a collision the robot moves backward for 0 4 second and turns left for 0 3 second Check status of the left switch If ther is a collision the robot moves backward for 0 4 second and turns right 0 3 second No collision is deteced the robot moves forward continually Robot attacks the object in the left Robot attacks the object in the right Listing A2 1 The C Program for Object Collision detection MicroCamp ATmega8 Activity Kit Manual 73 Actvity 2 2 Trapped in a comer situation When the Robot isin a comer it iscaught in between whereby to the left orright is a wall This causes continous hitting of the walls and thus trapping the robot in this comer The solution is to modify your exiting C Code from A2 1 to that which is shown in A2 2 A2 8 Create a new project file for making the C program according to Listing A2 2 A2 9 Connect the PX 400
24. format toggle _b x toggle _c x toggle d x Parameter x Determines the port number The value is 0 to 7 Example 6 6 toggle 5 Invert logic at PC5 port toggle d 1 Invert logic at PD1 port 6 2 Delay function in sleep h library This library only has one function It is the deep function Developers can use this function to pause or delay the operation in millisecond unit Function format void sleep unsigned int ms Parameter ms time value in millisecond unit Range is 0 to 65 535 Example 6 7 sleep 20 Delays 20 millisecond approximation sleep 1000 Delays 1 minute approximation MicroCamp ATmega8 Activity Kit Manual 57 6 3 analog h library Analog input reading library 6 3 1 analog function This is reading of an analog value It reads from PCO to PC4 pins Analog signals will pass through the Analog to Digital Converter inside ATmega8 microcontroller The converter resolution is 10 bit The digital output value in decimal number is 0 to 1 023 refer 0 to 5V DC voltage Function format unsigned int analog unsigned char channel Parameter channel select the analog input Range is 0 to 4 It means PCO to PC4 Retum value The digital data from the conversion range is 0 to 1 023 in decimal number Example 6 8 int adc _val 0 Set the variable for storing the analog reading data adc_val analog 0 Read from analog channel 0 PCO and
25. is 0 to 1 023 A10 1 Open the AVR Studio to create the new project and write the C program following the Listing A10 1 A10 2 Add the analog library file into the project file Build this project A10 3 Connect the PX 400 programmer to the MicroCamp board on The MicroCamp robot atthe In System Prog connector Tum on the Robot Downlaod the HEX code to the robot A10 4 Tum off powerand Remove the ISP cable A10 5 Tum on the powerand place some object at the front of GP2D120 module Observe the SLCD16x2 operation A10 6 Adjust the distance of the object fom MicroCamp robot and observe the result From testing you will found the GP2D 120 can detect the object in range 4 to 30cm correctly 196 Distance 12 include lt stdlib h gt MicroCamp ATlmega 8 Activity Kit Manual 105 For CONVERT type of data include lt soft_serout h gt Software serial communication include lt sleep h gt include lt analog h gt define m 2933 define b 20 define k 1 void main Delay Analog to Digital Converter Contance for convert data to centimeters Main Program unsigned char dec 4 dec2 4 for save ascii after convertion unsigned int gp2 0 cm 0 Read data from adc sleep 1000 Delay 1 Sec soft _serout_init 2 9600 Initial Serial Comm 9600 8N1 For SLCD while 1 gp2 analog 0 Read cm m gp2 b k Convert Data to Centimeter utoa gp2 dec 10 C
26. main unsigned int 1 sensor 0 r sensor 0 unsigned char dec1 4 dec2 4 sleep 1000 Delay 1 Sec soft _serout_init 2 9600 Initial Serial Comm 9600 8N1 while 1 1 sensor analog 3 Read Left Sensor r sensor analog 4 Read Right Sensor utoa l_sensor dec1 10 Convert Integer to decimal Ascii utoa r_sensor dec2 10 Convert Integer to decimal Ascii lcd command 2 0x80 Command First Line serout text 2 L Sensor We lcd command 2 0x8A Command First Line serout text 2 decl1 Show Ascii data lcd _ command 2 0xCO0 Command First Line serout text 2 R Sensor toe lcd _ command 2 0xCA Command First Line serout_text 2 dec2 show centimeter data sleep 300 Program description The ATmega8 microcntroller in MicroCamp robot would read the result data from the A D converter module that get from both Infrared reflector sensors at P3 and P4 port The data is displayed on the SLCD16x2 screen Listing A6 1 The program code for testing black and white area MicroCamp ATmega8 Activity Kit Manual 93 A6 6 Place the MicroCamp robot that programmed ready from step A6 4 above the white surface of the testing chart Tum on the robot See the reading value at SLCD screen and record it After that change to read value of black surface and record the value also PNAS MSSM The result is The white surface value is dur
27. make I D 1 S 0 D 1 C 0 B 0 N 1 F 0 DDRAM Address 00 Serial input timing diagram ee start Do D7 Stop lt T gt start po bi D2 Processing time OT 5 ms P MIN Figure 8 2 SLCD16x2 command summary and timing diagram 8 1 5 LCD Characters MicroCamp ATmega8 Activity Kit Manual 79 Most of the LCD characters Figure E cannot be changed because they are stored In the ROM However the first eight symbols comesponding to ASCII O through 7 are stored in the RAM By Writing new values to the character generator RAM CGRAM you can alter these characters as you want in 5x8 dots size HEX DEC 00h 20h 30h 40h 50h 60h 0 32 40 48 56 64 72 80 88 96 104 112 120 fi NOTE Custom characters occupy ASCII 0 7 ASCII 8 15 repeat the custom characters ASCII 128 159 are used for Extended Mode Command only AOh BOh COh DOh E0h FOh 160 168 176 184 192 200 208 216 224 232 240 248 LCD character set Built in character on HD44780A or SED1278F0A Create your symbols by pointing to the CGRAM location then write the first line whose bits form the desired pattem and point to next CGRAM address to write bits later Repeat this procedure until 8 times one character your character is ready to use now CGRAM Oislocated on CGRAM Address 00h 07h CGRAM 1 0n 08h 0Fh C G RAM 2 on 10h 17h until CGRAM 7 on 38h 3Fh See figure below byte 0 byte 1 byte 2 byt
28. programmer box to the MicroCamp board on The MicroCamp robot at the In System Prog connector Tum on the Robot A2 10 Prepare the demonstration area by placing and securing boxes or objects on the surface A2 11 Bring the robot into the demonstration area Tum on the power and observe the robot The robot will move forward and check for collision If this happens over 5 times consecutively the robot will spin 180 degrees to change its direction include lt in_out h gt include lt sleep h gt include lt motor h gt include lt sound h gt Sound library void main unsigned char cnt_ 0 Declare variable for counting the number of collision both left and right whale amid 2 1 Wait for SW1 is pressed to start operation sound 3000 100 Beep at once while 1 Looping de iua 2 Check the right side collision me Cite 42 Check the counter as even number or not I yes means the previous collision is left side collision cnt _ Increment the counter else If not left side collision cnt 0 clear the counter backward 100 Move backward 0 4 second sleep 400 Vii s left 50 ff naa iene ie ent SS Check the counter over 5 or not sleep 700 u over turn left more 0 7 second sound 3000 100 Drive sound to piezo speaker mic lt 0 Clear counter Listing A2 2 The C program for MicroCamp robot in Trapping wall s
29. the name of function and put the suitable parameters or data which the function requires The data which is passed to all parameters in each func tion is called an Argument The calling function has this fom function name agumentl agument2 Thus function_name jis the specific name of the function which was declared agument is the data which is passed from the function parameters If the function has no parameters no arguments are required Example 4 1 void tone void sound 3000 100 Sound generator function generate 3kHz signal in 0 1 second sleep 1000 Delay 1 second sound 3000 100 Sound generator function generate 3kHz signal in 0 1 second from the code above it is a declaration of a tone function This function does not return the result and has no parameters This function operation is to generate the sound signal of 3kHz for 0 1 second and repeat itself again after 1 second Developers can use this function inside a main function as follows void main Dena dedi at etra a casas Any instruction tone Call tone function ink fsa con EE canst ar EA Any instruction Note This function requires 2 libraries to be included in the C program sound h and sleep h Example 4 2 void tone unsigned int delay sound 3000 100 Sound generator function sleep delay Delay from parameter sound 3000 100 Sound generator function This example
30. the test_lib hex into the microcontroller 6 Observe the operation of the program in the MicroCamp controller board LED at PC5 pin of ATmega8 will blink 10 times MicroCamp ATmega8 Activity Kit Manual 37 4 4 Data type in C programming of WinAVR WinAVR is a suite of executable open source software development tools for the ATMEL AVR series of RISC microprocessors hosted on the Windows platform In cludes the GNU GCC compiler for C and C Thus the Data types are compliant with AVR GCC and the summary of all Data typesare Data type char unsigned char int 32 767 unsigned int long unsigned long long long unsigned long long float and double arrays pointers structures Size 8 bit Integer signed number Range is 128 to 127 8 bit Integer unsigned number Range is 0 to 255 16 bit Integer signed number Range is 32 768 to 16 bit Integer unsigned number Range is 0 to 65535 32 bit Integer signed number Range is 2 147 483 648 to 2 147 483 647 32 bit Integer unsigned number Range is 0 to 4294967295 64 bit Integer sgned number Range is 9223372036854775808 to 9223372036854775807 64 bit Integer unsigned number Range is 0 to 18446744073709551616 32 bit floating point Data or Variable group are same data types and store in address continue The index data to access the memory address Data or Vanable group are different data types 38 MicroCamp ATmega8 Activity Kit Man
31. users itisadviced NOTto enter the advance button ofthiswindow as there are manyadvanced configuration Program Verify Read Pesan ve iia which requires more experience for Device adjusting and changing ATmega zI Advanced ffanysetting are incorect ATMEGA8will notbe successfully programmed via ISP EEPROM 3 5 6 Click at the Program button in the Flash command The Switch_LED hex file will now be downloaded into the ATmega8 microcontroller in the MicorCamp controller board 3 5 7 When the download is finished the program will un automatic Press the button swtich SW1 and SW2 on MicroCamp controller board Observe the LED operation The LED will turn on and off when the switch is pressed and blink if the switch is released i INNOVATIVE EXPERIMENT MicroCamp ATmega8 Activity Kit Manual 33 Chapter 4 Library and Function of C programming In C a function is equivalent to a subroutine ora procedure A function pro vides a convenient way to encapsulate some computation which can then be used without wonying about its implementation With propery designed functions it is pos sible to ignore how a job is done knowing what is done is sufficient C makes the sue of functions easy convinient and efficient you will often see a short function defined and called only once just because it clarifies some piece of code All C programs must have a main function that contains the code which will be run fir
32. A12 3 Connect the PX 400 programmer to the MicroCamp board on The MicroCamp robot must install the ZX IRM and SLCD ready at the In System Prog connector Tum on the Robot Downlaod the HEX code to the robot A12 4 Tum off powerand Remove the ISP cable A12 5 Put the 2 of AA battenes inot Batter holder of ER 4 Remote control A12 6 Tum on power Pressthe button switch on ER 4 Remote controlto send data to Z IRM at the MicroCamp robot Observe the operation at SLCD screen If all correct SLCD screen would be show the button data that received 114 MicroCamp ATmega8 Activity Kit Manual include include include include include include unsigned unsigned lt stdlib h gt lt soft_serout h gt lt sleep h gt lt serial h gt lt sound h gt lt motor h gt char key flag 0 char dec 4 bin 9 void main sleep 1000 soft_serout_init 0 9600 uart_set_baud 1200 Standard Library For SLCD Module For Delay For Receiving Data from Remote Control For Generate Sound For Control Motor Delay 1 Sec Initial Serial Comm 9600 8N1 Set IR remote control Baudrate sound 2000 200 Status Sound serout_byte 0 0xFE serout_byte 0 0x01 Clear Screen SLCD serout_byte 0 0xFE serout_byte 0 0x80 Show Text on First Line serout text 0 You Press Key while 1 key uart_getkey 20000 key flag amp amp key gt 0x40 am
33. ATmega8 Activity Kit Manual include lt avr io hs include lt in_out h gt ifndef soft _serout_ define soft _serout_ define PRESCALER 1 1 lt lt CS20 1 16M 0 0625 us per MC define PRESCALER 8 1 lt lt CS21 8 16M 0 5 us per MC define PRESCALER 32 1 lt lt CS21 1 lt lt CS20 32 16M 2 us per MC define PRESCALER 64 1 lt lt CS22 64 16M 4 us per MC define OFFSET DELAY1 20 for out function used 20 us define OFFSET DELAY2 18 for out function used 20 us unsigned int base 0 unsigned char base start _rcv 0 unsigned char TCCR2_ cal 0 unsigned int base unsigned int baud 9600 void soft _serout_init unsigned long baud_ unsigned long tick 0 if baud_ lt 4800 iene amp 1000000 baud_ OFFSET DELAY1 4 TCCR2_cal PRESCALER 64 else if baud_ gt 4800 amp amp baud_ lt 9600 tick 1000000 baud_ OFFSET_DELAY2 2 MCCRAmC ctle webb S CAINE Ramo 2T TCCR2 OF TIFR 1 lt lt TOV2 base 255 TiClk base start rev 255 tick 2 Delay For baudrate void delay baud unsigned int _tick TCNT2 _tick id TCCR2 TCCR2 cal a while TIFR amp 1 lt lt TOV2 TIFR 1 lt lt TOV2 TCCR2 0 bi Send Data 1 Byte void serout_byte char tx unsigned char dat slime Ak 9 Gut Ele 0 7 delay_baud base for 1 0 1 lt 8 1 out_c tx dat amp 0x01 Config and Start up timer 2 Calculate D
34. C digital interface Port B digital interface Multiplex L and 2 Wires Analog to ADC Digital interface interface AGND Converter AREF ea f ae Timer F 0 Counter Oscillator Program Counter Pointer i Internal Flash Program RAM Oscillator Data Memory y Watchdog Oscillator Timer Instruction i register General purpose register i i y y 4 Instruction decoder MCU control and Timing vo Control signal j Interrupt unit AVR CPU EEPROM Data Memory USART Comparator interface rl Port D digital interface Port D drivers buffers Port D PD7 PD0 Figure 1 5 ATmega8 Block Diagram 12 MicroCamp ATmega 8 Activity Kit Manual Name Pin number Type Description Vec 7 Input Supply voltage 4 5 to 5 5V GND 8 22 Ground Supply voltage 5V for ADC module of ATmega8s Reference voltage input for ADC module of ATmega8 Port B Name Pin number Type Description PBO 14 Input Output PBO Digital port Input Input Capture 1 15 Input Output PB1 Digital port Output Output Compare PWM 1A 16 Input Output PB2 Digital port Output Output Compare PWM 1B Input Slave input for SPI and In System Programming ISP 17 Input Output PB3 Digital port Output Output Compare PWM 2 Input Output
35. E x x amp amp x 1 d x 1 D endif ifndef TEST _LONG_ TYPE x define TEST LONG TYPE x x amp amp x 1 1 x 1 L endif ifndef TEST FLOAT TYPE x define TEST FLOAT TYPE x x amp amp x 1 f x 1 F endif ifndef TEST STRING TYPE x define TEST STRING TYPE x x amp amp x 1 s x 1 S endif ifndef F PREC define F_ PREC 3 endif unsigned int _baud 9600 char uart_ini 0 char _key 0 ifndef USE EVEN UART REC SIGNAL SIG_UART_RECV endif _key UDR void uart_set_baud unsigned int baud _baud baud uart_ini 1 Set baud rate UBRRH unsigned int UART_BAUD CALC baud F OSC gt gt 8 UBRRL unsigned int UART BAUD CALC baud F_ OSC UBRRH 00 for 9600 bps UBRRL 51 126 MicroCamp ATmega8 Activity Kit Manual Enable receiver and transmitter Asynchronous 8N1 enable RX interrupt UCSRB 1 lt lt RXEN 1 lt lt TXEN 1 lt lt RXCIE UCSRC 1 lt lt URSEL 3 lt lt UCSZO sei enable interrupts unsigned int uart_gets_baud void uart_putc unsigned char c return _ baud if uart_ini 0 uart_ini 1 uart_set_baud _baud while UCSRA amp 1 lt lt UDRE UDR C send character void uart_puts char s while s uart_putc s S void uart char p
36. MicroCamp2 0 ATmega8 Activity Kit manual I INNOVATIVE EXPERIMENT www inex co th www inexglobal com 2 MicroCamp ATmega 8 Activity Kit Manual Content Chapter 1 MicroCamp Activity kit hardWware s ssssssssnunnnnnnnnnnnnnnnnnnnnnnnnn Chapter 2 Development software for MicroCamp Kit ccscssssessesesseseeees Chapter 3 C programming development for MicroCamp kit with AVR Studio and WinAVR C compiler ccscseeeesseeeeeesneeeseeeeeenens Chapter 4 Library and Function of C programming csccseseeseeeeeeeeesneeeeees Chapter 5 Operator of WinAVR C compiler ccccceseeseeeeseeeeeesseeeseeneeeenes Chapter 6 Library and Specific command in MicroCamp Kit cssessesseees Chapter 7 Building robot with MicroCamp Kit ccccscscseseesceseeeseeeeseeeeeeeees Activity 1 Basic movement Of MicroCamp rObOt scsscscseeeneennenes 68 Activity 2 Object detection With CoOlliSiOn ccccscccccsecenneceneeceneneanes 71 Chapter 8 Serial LCD Activity with MicroCamp sccccscssseceeeseeeeseeseeeeenenes Activity 3 Install SLODIG2 s tisscssinduaniguvatesr Sud ii vvdeteimiavaedinakesisatees 80 Activity 4 SLCD16x2 Simple PrOGraMIMinG sccsscsscesnesneenneeneennes 84 Activity 5 Control SLCD16X2 With COMIMANGA csecceceseseeeeenneeenneees 85 Chapter 9 MicroCamp robot with Line tracking activitieS c sscssesssesees Activity 6 Testing black and white afea ssssssssssssnsserrrrrssssnss 91 Activity 7 Robot moves al
37. Modulo Example 5 3 int x y Z x 10 y 2 33 Z X 3 The result is y 9andz 1 Operation y x 3 Dy 10 3 gt y 3 Returns an Integer z x 3 gt z 10 3 gt z 1 Returns only the remainder 5 1 3 and operation Example 5 4 int y 5 ytt The result is y 6 Operation Begin with y 5 Next y 1 6 and store to y Thus y command gives the result similar to y y 1 command Example 5 5 int y 5 yr The result is y 4 Operation Begin with y 5 Next y 1 4 and store to y Thus y command gives the result similar to y y 1 command MicroCamp ATmega8 Activity Kit Manual 45 5 1 4 and operation The operation of both operations can be summarized as follows y a gives the result similar to y y a Y a gives the result smilarto y y a Example 5 6 int x 100 x 10 The result is x 110 5 15 and operation The operation of all operators can be summanied as follows y a gives the result similar to y y a y a gives the result similar to y y a y a gives the result similar to y y a x 4 Yy 4 Z 4 The result is x 480 y 30 and z 0 46 MicroCamp ATlmega8 Activity Kit Manual 5 2 Relation amp logic operator The results of these operators are 1 if the condition is tue and 0 if the condi tion is false These operators can be summanzed as follows Operator Meani
38. Now the library file led1 h is created 36 MicroCamp ATmega8 Activity Kit Manual 4 3 2 How to use library After creating the library file developers can call all functions inside the library files by including them into the head of the C program include lt library_ filename gt or include library filename Directive include helps the C program to recognize all functions inside the library file Example 4 6 1 Create the new project in name fest_lib 2 Type the C code below in the fest_lib c window include lt in_out h gt Standard Library include lt ledl h gt get blink function from here void main blink 10 Blink LED 10 times Description The test_lib program will use 2 library files One is the standard input output port library of the ATmega8 microcontroller in_out h Anothe one is the led1 h file that is created on your own Inside the led1 library has 2 functions blink and sleep blink function determines the PC5 port to output for driving LED and sending logic 1 and 0 sleep function determines the delay time for the LED operation The blink function works until it reaches the value that is being declared by the programmer 3 Set the path for led1 h library from Project gt Configuration Options Select the icon Include Directories After that set the path for led1 h file 4 Build this project The result file test_lib hex will be created 5 Download
39. OCRIBL duty _duty2 duty void motor char _channel int _power if _power gt 0 pwm channel power if _channel 2 out_d 7 1 out_d 6 0 else if _channel 1 out_d 5 0 out_b 0 1 else pwm _channel abs _power if _channel 2 out_d 7 0 out_d 6 1 else if _channel 1 out_d 5 1 out_b 0 0 void motor _stop char _ channel pwm _channel 100 if _channel 2 channel 3 out_d 7 0 out_d 6 0 if _channel 1 _channel 3 out_d 5 0 out_b 0 0 void motor off pwm 3 0 out_d 7 0 out_d 6 0 out_d 5 0 out_b 0 0 void forward int speed motor 1 speed motor 2 speed void backward int speed motor 1 speed 1 motor 2 speed 1 void s_left int speed motor 1 speed motor 2 speed 1 void s right int speed motor 1 speed 1 motor 2 speed MicroCamp ATmega8 Activity Kit Manual 123 124 MicroCamp ATmega8 Activity Kit Manual timer h Timer library include lt C WinAVR avr include avr interrupt h gt include lt C WinAVR avr include avr signal h gt RRKKKKKKKKKKKKKKKEKE Timer 0 Interrupt RRR k d k RRR RR RI RR RR KR KK RRR RR RRR RRR RRR RRR RR Interval 1 ms 5 mI kk kk kkk IR k k k k k kk IO k k k k k k k k k k unsigned long _ms 0 SIGNAL SIG_OVERFLOWO Interval 1 ms TCNTO 6 Interval 1 ms _mst voi
40. Press SW1 Move forward and g Move forward and Turn right Turn left Listing A1 3 The C Program for movement selection of the Microcamp Robot MicroCamp ATmega 8 Activity Kit Manual 71 Activity 2 Object detection with Collision Actvity 2 1 Smple collision detection This activity is program the robot to detect the collison of both switchesat the front of the MicroCamp robot Aftera collision is encountered the robot will move backward and change the its direction of movement A2 1 Create a new project file and write the following C Codes shown in A1 4 Build this project A2 2 Connect the PX 400 programmer box to the MicroCamp board on The MicroCamp robot at the In System Prog connector Tum on the Robot A2 3 Download the HEX code to the robot A2 4 Tum off power and Remove the ISP cable A2 6 Prepare the demonstration area by placing and securing boxes or objects on the surface A2 7 Bring the robot into the demonstration area Tum on the power and observe the robot The MicroCamp robot will read both switch status from PD2 and PC3 port If any switch is pressed or touches some object the result is logic 0 In a normal operation the robot will move forward continually If the Left Switch module touches any object the robot will move backward and change its moving direction to its right to avoid the object If the Right Switch module touches any object the robot will move backward and change its moving
41. R CAMP MicroCamp ATmega8 Activity Kit Manual 81 8 2 soft serout library For more comfortable to interface SLCD16x25 with MicroCamp the serial data communication library is nescessary The suitable library is soft_serout h file This library is serial data transfering from host to any slave serial device in 8N1 format The full sourcecode of this library is shown in Listing 8 1 User can select any MicroCamp port PO to P4 for interfacing the sral device This library contains 3 functions as follows 8 2 1 soft_serout init This function is used for setting Baudrate value The maximum is 9600 bit persecond Syntax void soft serout_init unsigned long baud_ Example soft serout_ init 9600 Set the bauadrate to 9600 bit per second 8 2 2 serout_ byte This function is used for sending one byte data from any port Syntax void serout_byte char tx unsigned char dat Thus tx isMicroCamp port The value is O to 4 for PO to P4 port dat isthe sending 8 bit data Example serout_byte 2 0x80 Send 0x80 data to P2 port 8 2 3 serout text This function is used to send the string data or many the byte data to the specific port Syntax void serout text char tx unsigned char p Thus tx is MicroCamp port The value is O to 4 for PO to P4 port p isthe sending string or data Example serout_text 2 MicroCamp Send the message MicroCamp to P2 port of MicroCamp 82 MicroCamp
42. Time Display Off Display ON serout_byte 2 0xFE serout_byte 2 0x00 7 serout_byte 2 0xFE serout_byte 2 0x80 serout_byte 2 0xFE serout_byte 2 0xC0 serout_text 2 Innovative serout_text 2 Experiment Command Initial LCD Command First Line First Char Show Text Innovative Commmand Second Line First Char Show Text Experiment Listing A5 1 The experiment code about testing the SLCD16x2 display in many formats Do not type the broken line in the listing because it is used for program description reference continue MicroCamp ATmega8 Activity Kit Manual 87 Program description Part 1 Initial the communication module in microcontroller and SLCD16x2 Part2 Select the target line to display The SEN ine O issetto show message The bottom line OxC0 is set to show message Part 3 Send the Clear screen command 0x01 and defind the first letter at 5th digit on the top line of LCD 0x85 to show Fim message Part 4 Send the shift left command 0x07 and loop to shift the Fr message to left direction Part 5 Send the shift right command 0x05 and loop to shift the Frar message back to start Part 6 Loop for sending the Tum off display command 0x08 and Tum on display command 0x0C and swap It cause the message Frer will be blink Part 7 The opearion is same Part 2 si aS the message on top line as f iue and bottom line as Esper ipreri Listing A5 1 Th
43. _lib3 H O test_relay O testSw E gt tyttt 28 MicroCamp ATmega 8 Activity Kit Manual The folder Switch_LED will be created in G Work2006 AVR ROBOT Code In the same folder the file Switch_LED aps and main C program file Switch_LED c will be created 3 3 5 Next step is to determine the microcontroller information and path of all the library file which is being used in this project 3 3 5 1 Select the command at Project gt Configuration Options AVR Studio Switch_LED c Build Edit View Tools Debug Window Help Project Wizard jo amp EH EP SS X YS AYR Prog New Project Open Project Save Project Close Project Recent Projects X Configuration Options Bi Switch_LED c After that the window Switch _LED Project Options will appear for setting the properties See the left of this window Developers will found 5 icons as General Include Directories Libraries Memory Settings Custom Options 3 3 5 2 At General icon determine all data follows Device atmega8 Frequency 16000000 Hz Switch_LED Project Options Active Configuration default x Edit Configurations I Use External Makefile ean must equal pro Include Directories Output File Name Switch_LED elf i Output File Directory defaults es Libraries Derce atmega8 x M Unsigned Chars funsigned char IV Unsigned Bitfields funsigned bitfields Frequency 16000000 hz f IV Pack Structure
44. adoa while 1 By ctype h lcd P kPa P_abs 1 E in_out h sleep 100 E interrupt h interrupt h E inttypes h E io h E iom16 h B ipst h E led h led h 2 libm a EJ motor h B portpins h E seament h E serial h Bl sfr_defs h x gt GAVR GCC Kalyo view nfo pressure_sensor c 4d gt Build x a Program 7578 bytes 46 3 Full H i fee ak eee Build window shows all status of operations which aes EAE NN includes the compiled result error in compilation HEX AEPS AA SOAN file size and any other waming messages Build succeeded with 6 Warnings v lt Build ti Message SAFind in Files Al Breakpoints and Tracepoints int4 colt NUM 3 2 1 Fle menu Includes the command as follows New File Create empty text file Open File Open a file in text editor or an object file for debugging Close Close the active text file Save Save current text file Save As Save curent text file under given name Save All Save all files and project settings Print Print active text file Print Preview Preview active text file Print Setup Setup printer Exit Exit AVR Studio project are saved when exiting 3 3 2 Project menu MicroCamp ATmega8 Activity Kit Manual 25 Includes the command as follows Project Wizard New Project Open Project Save Project Close Project Recent Projects Open the project wizard You must close the current project first Open the new project dialo
45. amp ATmega8 Activity Kit Manual 51 Solution 1 From a amp 0x3c itis equala a amp 0x3C It means get the value of a 0x7A AND with 0x3C and store the result back to a again It is equivalent to a 0000000001111010 amp 0000000000111100 0000000001111010 AND 0000000000111100 0000000000111000 gt 0x0038 or 0x38 2 From b 0x51 itis equalb b 0x51 It means get the value of b 0x16 OR with 0x51 and store the result back to b again It is equivalent to b 0000000000010110 0000000001010001 0000000000010110 OR 0000000001010001 0000000001010111 gt 0x0057 or 0x57 A 3 From c oxbo itis equalc c OxbD0 It means get the valuse of c OxFD XOR with OxDO and store the result back to c again It is equivalent to c 0000000011111101 0000000011010000 0000000011111101 XOR 0000000011010000 0000000000101101 gt 0x002D or 0x2D i INNOVATIVE EXPERIMENT 52 MicroCamp ATmega 8 Activity Kit Manual MicroCamp ATmega8 Activity Kit Manual 53 Chapter 6 Library and Specific command in MicroCamp kit The MicroCamp Activity kit comes with a lot of libraries to support developers and leamers It includes Input Output port control library Analog input reading library Delay time library Sound library and Motor control library The summary of all libraries are as follows in_outh Library for Sending digital data to the output port and Reading the Digi
46. ation for educational use is permitted subject to the following Conditions of Duplication INEX grants the user a conditional right to download duplicate and distribute this text without INEX s permission This right is based on the following conditions the text or any portion thereof may not be duplicated for commercial use it may be duplicated only for educational purposes when used solely in conjunction with INEX products and the user may recover from the student only the cost of duplication All text and figure is subject to publisher s approval We are not responsible for mistakes misprints or typographical errors Innovative Experiment Co Ltd INEX assumes no responsibility for the availability
47. b false 0 and a gt b a gt c true 1 a b a gt c true 1 a b a 6b true 1 I a gt c a b false 0 48 MicroCamp ATmega8 Activity Kit Manual 5 3 Bitwise Operator The operators can be summanzed as follows Operator gt amp Meaning Invert bit Bit AND Bit OR Bit XOR Shift Left Shift Right Shift left and Store the result to variable Shift right and Store the result to vanable AND operation with Store the result to variable OR operation with Store the result to variable XOR operation with Store the result to variable 5 3 1 Bit logic Operation operation can summary as follows Operation 0 1 Result 1 0 amp operation can summary as follows Operation 0 amp 0 0 amp 1 1 amp 0 1 amp 1 Result 0 0 0 1 operation can summary as follows Operation 0 0 0 1 1 0 1 1 Result FPrrP Oo operation can summary as follows Operation 0 0 0 l 1 0 Tel Result 0 1 1 0 MicroCamp ATmega8 Activity Kit Manual 49 Example 5 10 Determine int x y result1 result2 result3 result4 x Ox9C y 0x46 Find the result of 1 resultl x amp y 2 result2 x y A 2 3 result3 x y 4 result4 x Solution Firstly convert all value to a binary number x 0x9C gt 0000000010011100 because int data type is 16 bit wide y 0x46 gt 0000000001000110 1 result1 0000000010011100 amp
48. backward bottom button orA ora button Move left left button orC orc button Move right right button orB orb button Get all button data to create the program for controlling the robot s movement with remote contol A13 1 Open the AVR Studio to create the new project and write the C program following the Listing A13 1 A13 2 Add the serial h library file into the project file Build this project A13 3 Connect the PX 400 programmer to the MicroCamp board on The MicroCamp robot must Z IRM and SLCD ready at the In System Prog connector Tum on the Robot Downlaod the HEX code to the robot A13 4 Tum off powerand Remove the ISP cable A13 5 Put the 2 of AA batteries inot Batter holder of ER 4 Remote control 116 MicroCamp ATmega8 Activity Kit Manual include lt stdlib h gt fi include lt soft_serout h gt include lt sleep h gt Hi include lt serial h gt Hi include lt sound h gt hi include lt motor h gt if unsigned char key flag 0 void main Hilf Hit Md sleep 1000 soft_serout_init 0 9600 uart_set_baud 1200 sound 2000 200 serout_byte 0 0xFE serout_byte 0 0x01 serout_byte 0 0xFE serout_byte 0 0x80 serout text 0 Press any IR Key while 1 key uart_getkey 65000 if Kkey a key A MU PINN backward 100 aie ECUS serout_byte 0 0xFE serout_byte 0 0xC0 serout_text 0 Backward if Ikevej il p else
49. board RESET switch Battery Terminal 6Vdc max ON RESET POWER indicator _ a gt e af es 5 POWER switch nonno Gy A In System B py Programming Ge ATmega8 connector E microcontroller ISP YA OLLE i a2 aoe SS Motor B indicator sis ieee E Input O utput lt a 4 e Motor A indicator connector m Ae om a Motor A output PO PCO ADCO ol eu TOEA Motor B output lt DA P1 PC1 ADC1 p P2 PC2 ADC2 38kHz Infrared SN 5v P3 PC3 ADC3 a Receiver module P4 PC4 ADC4 _ connector S DATA i Piezo speaker PD4 i its S DATA Serial data communication port eee LED1 indicator PC5 Olcenp iz SW2 PD3 a GND 5V SW1 PD2 Ble LED2 indicator PD1 apo Figure 1 1 MicroCamp board layout 4 MicroCamp ATmega8 Activity Kit Manual 1 1 Hardware of MicroCamp Actvity kit 1 1 1 MicroCamp controller board The main microcontroller is the 8 bit AVR microcontroller from Atmel ATmega8 It has many features of modem microcontroller such as the 10 bit Analog to Digial Converter module ADC Flash program memory 8KB with 10 000 times erase write cycles Data EEPROM 512 bytes and RAM 512 bytes too Main clock frequency 16MHz from Xtal 5 channels Programmable 3 pin Input Output port User can programmable all port pins for usages aS a Digial Input port Digital Output port and an Analog input port The 3 pins are Supply voltage nommally is 5V Signal or Data and Grou
50. cable from the PX 400 programmer box to the In System Prog ISP connector on MicroCamp controller board UC ON 232 UC O N 232S USB to Serial port converter lf computer has not RS 232 serial port must use USB to Serial port E C converter 7x N Dii 7x N Te RS 232 READY susy RS MicroCamp controller board Figure 3 1 Connection diagram of PX 400 programmer box and MicroCamp controller board for downloading the program 32 MicroCamp ATmega 8 Activity Kit Manual 3 5 3 Switch to AVR Studio program select the command at menu Tool gt AVR Prog AYR Studio Switch_LED c File Project Build Edit view Tools Debug Window Help Sugg RS X fo AVR Prog AYR GCC x 5 ICE50 Upgrade R switch_LED default ICESO Selftest yy Source Files ITAGICE mkII Upgrade B Switch LED c Sy Header Files AVRISP mkII Upgrade m 4J External Dependencies bet oone ae bob E in_out h Customize void main E inttypes h T E io h SEA while 1 E iom8 h Show Key Assignments i l gt if in_d 2 E libm a Plug in Manager in_d 2 ieee ae S 2 3 5 4 The AVRprog window will not appear 3 5 5 At the AVRprog window click on the Browse button to find the path of Switch_LED hex file for selection the HEX file require to download AVRprog BAR Hex file G Work20064 Switch_LED hex Browse Exit Verity Read Fornovice
51. ce the SLCD16x2 is propery connected and configured data and command can be sent senally For data sending you can send any message such as Hello via serial I O directly Hello message will be shown on your LCD For command sending you can send standard instruction set to LCD see Figure 8 2 by precede it with the instruction prefix character ASCII 254 OFE hex or 11111110 binary SLC D16x2 treats the byte immediately after prefix as an instruction then automatically retums to data mode An example To clear screen on LCD clear instruction is 00000001 binary or ASCII 1 send 254 and 1 to SLC D16x2 where parentheses in symbols mean single bytes set to these values 1 j 1 Initial LCD S 0 Automatic cursor shift after byte 1 Cursor not moved 2 Clear LCD 1 D 0 After byte decrease cursor position 1 After byte increase cursor position eeturm Home when S 1 cursor won t be shifted D 0 Display OFF 1 Display ON C 0 Cursor OFF 1 Cursor ON B 0 Cursor not blink 1 Cursor blink 6 Shift Display i i o o 1 Pr S C 0 Cursor shift 1 Display Shift 7 Function Settin 3 R L 0 Left shift 1 Right shift 8 Set CGRAM Address N 0 1 8 Duty 1 1 16 Duty 9 Set DDRAM not recommend to set this bit use jumper setting instead F 0 5x7 dot size 1 5x10 dot size Standard instrction command set summary except Initial LCD is addition command A0 to A7 are CGRAM or DDRAM Address Initialize
52. conditional loop is false It then continueswith the second conditionalloop If SW2 at PD3 portis press the Program will stop both motors The Robot will stop its movement Listing A1 2 The C program for MicroCamp robot move circle shape activity 70 MicroCamp ATlmega 8 Activity Kit Manual Activity 1 3 Square shape movement contol A1 9 Create a new project file and write the following C Codes shown in A1 3 Connect the PX 400 programmer box to the MicroCamp board on The MicroCamp robot at the In System Prog connector Tum on the Robot Downlaod the HEX code to the robot A1 10 Tum off powerand Remove the ISP cable Make sure the robot ison a flat surface Tum on the power and observe the robot The robot will be activated if SWI or SW2 is being pressed If you Press SW1 the robot will move forward and turn left continually making a square If you press SW2 the operation is vice versa include lt in out h gt include lt sleep h gt include lt motor h gt void main while 1 Looping if in_d 2 0 Check SW1 pressing while 1 forward 100 Move forward with full speed 0 9 second sleep 900 s right 50 Turn right with 50 speed 0 3 second sleep 300 if in_d 3 0 Check SW2 pressing while 1 forward 100 Move forward with full speed 0 9 second sleep 900 s left 50 Turn left with 50 speed 0 3 second sleep 300 Q ME Em Press SW2
53. d Welcome to AVM Studio from Atmel Corporation AVR Studio ie a Development Tool for the AVR of microcontrollers The AVR Studio is Pidi ral carla sted ade A hed dealer wa JAVR Sturli nnaliles the user te fully contol reculion of peogianes on the AVR Infact of on the included AVR Instruction Set Simulator AVR Studio supports source level i assembled with the Atmol Corporation s neludod AVR Studio is continously developing In order to get latest ungrades of AVR Studio please visit our web ste won chek cont and check out the AVH page MicroCamp ATmega 8 Activity Kit Manual 17 2 1 4 Choose Destination Location wondows will appear You can change the path by clicking on the Change button and setting the new path After this click on the Next button AVRStudio4 InstallShield Wizard estimation Local ider where setup will install file InstallShield 2 1 5 The Driver USB Upgrade window will now appear Click on the Next button to pass this step 2 1 6 In the begin installation window click on the Install button to start installation AVRStudio4 InstallShield Wizard Ready to Install the Program e wizard is ready to begin installation tings o ok Back Click Can fasta iad ee a E 2 1 7 After installation is complete click on the Finish button to end the installation of AVR Studio 18 MicroCamp ATmega8 Activity Kit Manual 2 1 8 To launch the AVR Studio program Click on Star
54. d timer start void Config and Start up timer 0 TCCRO 1 lt lt CS01 1 lt lt CS00 Prescaler 64 16MHz 1 MC 64 16M 4us count TIFR 1 lt lt TOVO Clear TOVO clear TIMSK 1 lt lt TOIEO Enable TimerO Overflow Interrupt TCNTO 6 Interval 1 ms sei Enable all interrupt ms 0 void timer_stop TCCRO 0 Stop timer and TCNTO 0 TIMSK amp BV TOIEO Clear bit TOIEO ms 0 Clear time void timer pause void timer resume TCCRO 0 Stop timer and not clear time TCCRO 1 lt lt CS01 1 lt lt CS00 Prescaler 64 16 MHz 1 MC 64 16M 4us count unsigned long msec unsigned long sec return _ms return _ms 1000 MicroCamp Almega8 Activity Kit Manual 125 senal h ATmega8 s UART Serial data communication library include lt avr io h gt include lt avr interrupt h gt include lt avr signal h gt include lt stdlib h gt include lt string h gt include lt ctype h gt define F_ OSC 16000000 oscillator frequency in Hz define UART BAUD CALC x F_OSC F_OSC x 161 1 define even_uart_rec SIGNAL SIG UART_RECV ff POS Ss eS a Sa Condition test parameter data type for displa ifndef TEST CHAR TYPE x define TEST CHAR TYPE x x amp amp x 1 c x 1 C endif ifndef TEST _INT_TYPE x define TEST_INT_TYP
55. ded in the AVR Studio and can be found in the tools menu and works with the Avr Ospll software as well Model Numbers of microcontoller supported in AVR Prog AT90S1200 AT90S2313 AT90S2323 AT90S2343 AT90S4433 AT90S8515 AT90S8535 ATmega 128 ATmega 16 Almegal161 ATmega 162 ATmegal163 ATmegal64P ATmega 165 ATmega 168 AIlmega32 Almega64 Almega8 Almega8g515 ATmega8535 ATtiny12 ATtiny13 ATtiny15L ATtiny2313 ATtiny26 Model Numbers of microcontoller supported in Avr OSP li AT90C AN128 AT90C AN32 AT9OC AN64 ATOOPWM 2 AT9OPWM 3 AT90S1200 AT90S2313 AT90S2323 AT90S2343 AT90S4414 AT90S4433 AT90S4434 AT90S8515 AT90S8515c omp AT90S8535 A T90S8535c omp ATmega 103 ATmega103comp ATmega 128 ATmega 1280 A mega 1281 ATmega16 ATmega 161 ATmega161comp ATmega 162 ATmega 163 A Tmega 165 ATmega 168 ATmega 169 ATmega2560 ATmega2561 ATmega32 ATmega 323 ATmega 325 ATmega3250 ATmega 329 ATmega 3290 ATmega 406 ATmega 48 ATmega 64 ATmega 640 ATmega 644 ATmega 645 ATmega 6450 ATmega 649 ATmega6490 ATmega8 A Tmega8515 ATmega 8535 A Tmega88 ATtiny11 ATtiny12 ATtiny13 ATtiny15 ATtiny22 ATtiny2313 A Ttiny24 ATtiny25 A Ttiny26 ATtiny261 ATtiny28 AtTtiny44 ATtiny45 ATtiny461 ATtiny84 ATtiny85 ATtiny861 6 MicroCamp ATmega 8 Activity Kit Manual yet hd O 5V swi w if POWER R1 D1 50 0 47R 1N4001 HH 3 0 01 50V Kl
56. displaying following the specific by program ICR CAMP 86 MicroCamp ATmega8 Activity Kit Manual include lt soft_serout h gt include lt sleep h gt void main unsigned char i 0 while 1 sleep 1000 1 soft_serout_init 2 9600 serout_byte 2 0xFE serout_byte 2 0x00 Main Program Delay 1 Sec Initial Serial Comm 9600 8N1 Command Initial LCD Module serout_byte serout_text serout_byte serout_text sleep 2000 2 MicroCamp 2 0xFE serout_byte 2 0x80 2 0xFE serout_byte 2 0xC0 2 Microcontroller Command First Line First Char Show Text MicroCamp Command Second Line Fisrt Char Show Text Microcontroller serout_byte 2 0xFE serout_byte 2 0x01 serout_byte 2 0xFE serout_byte 2 0x85 3 serout_text 2 From sleep 500 Command Clear Screen Command First Line 5 th Char Show Test From serout_byte 2 0xFE serout_byte 2 0x07 for i 0 1 lt 9 1i 4 serout_byte 2 0x20 sleep 200 Shift Text Left and Inc Address 9 Time Loop lt lt lt lt lt lt Shift Data Left serout_byte 2 0xFE serout_byte 2 0x05 for i 0 1 lt 9 1i 5 serout_byte 2 0x20 sleep 200 Shift Text Right and Inc Address Shift Data Right 9 Time for i 0 1 lt 9 1i sleep 200 6 serout_byte 2 0xFE serout_byte 2 0x08 serout_byte 2 0xFE serout_byte 2 0x0C sleep 200 Blinking 9
57. e 3 byte 4 byte 5 byte 6 byte 7 Bitmap Layout Ce eee ea cae Byte Values 5 556 5 binary decimal OOOO xxx00000 0 JIB xxx00100 4 JIB xxx00010 2 TTT Tt xxx11111 31 JID xxx00010 2 BE EE xxx00100 4 CCOIL xxx00000 0 CICILILIL xxx00000 0 Defining custom symbols Example Load arrow symbol on CGRAM 3 a program would send the following bytes to the SLCD controller 254 01011000 b 0 254 01011001 b 4 254 01011010 b 2 254 01011011 b 31 254 01011100 b 2 254 01011101 b 4 254 01011110 b 0 254 01011111 b 0 80 MicroCamp ATmega 8 Activity Kit Manual Activity 3 Installation SLC D16x2 A3 1 Connect the B end of J ST3AB 8 cable to SLCD16x2 input connector Becareful the connection pin must corect and attach the straight joiner with both holes of SLCD16x2 by 3x1l0mm screws and 3mm nuts A3 2 Insert 3x35mm screw through the 25mm spacer and attach with MicroCamp body with 3mm nut on the opposite sde which attached the Swtiches Make two sets and do not tight the nut Su 35mm screw am spacer 3mm nut Insert screw and tight the nut tempolary A3 3 Attach SLCD16x2 with the screw and spacer from the previous step by inserted the end of straight joiner between head screw and the top of spacer After that tighten the screw Connect the A end of J SI3AB cable to P2 port on the MicroCamp controller board IC
58. e experiment code about testing the SLCD16x2 display in many formats final INNOVATIVE EXPERIMENT MicroCamp ATmega8 Activity Kit Manual 89 Chapter 9 MicroCamp robot with Line tacking activities From the activities in chapter 7 these show how to read the digital input signals and data to control the robot s movement In this activity there will be many activities about reading analog inputs and processing the data for the detection of black and white areas It also detects Black and white line to control the robot to move along the line with variable conditions The MicroCamp robot has 5 analog inputs that directly connects to the PCO to PC4 of ATmega8 microcontroller This microcontroller contains the 10 bit analog to digital converter ADC module The digital conversion data is 0 to 1 023 in decimal number format C programming for this activity requires a library file This is the analog h file Functions in this library will define relate the input port to the analog input and reads data from ADC module to store in its memory The resulting data range is 0 to 1 023 in decimals or OOOOH to O3FFH in hexadecimals The important devices in this activity is the 2 Infrared Reflector modules They are installed at the bottom of the robot base They are used to detect the surface s color black and white including the white and black line The Line tracking robot activity is the classic activity It shows the basic robot s pr
59. e position for attached box holder box holder MicroCamp ATmega 8 Activity Kit Manual 65 5 Inserta 3x15mm machine screw through the Infrared Reflector sensor followed by 2 of the 3mm spacer Do on both sides for this 3 x 15mm machine screw amp 3mm spacer x 2 6 Attach both the Infrared Reflector structures from step 5 at the suitable holes at the bottom and front of the robot base Tighten with a 3mm nut Hole position for attached Infrared Reflector sensor Distance is 3 to 5 mm 8 Place MicroCamp board on the box holder Connect sensor cables and motor cables following the diagrams shown PO for Right sensor and P1 for Left sensor s 7 ac Te a COT Yo Ej my eN Lc oe a f Ta Dii A fi W D A Right sensor able be Ca Ae i p i Black Red Infrared Reflector sensor connection A a AA LA E 66 MicroCamp ATmega 8 Activity Kit Manual 9 Attach the Straight joiner with robot base at front rght side by 3x 10mm machine screw and 3mm nut Attach 2 pieces Bre 10 Connect the Obtuse joiner at the end of Straight joiner Attach the right angle joiner with Switch module by 3 x 10mm machine screw and 3mm nut Make 2 sets Bring these structures to connect at the end of the Obtuse joiner Connect 2 sides 11 Connect the Left Switch module cable to the P2 PC 2 connectorand the Right Switch module cable to the P3 PC3 connector Put 4 AA batteries in
60. ector Tum on the Robot Downlaod the HEX code to the robot A11 4 Tum off powerand Remove the ISP cable A11 5 Place the robot on the floor Tum on the powerand observe its operation A11 6 Try to place any object at the front of the robot and see its operation The robot will check the distance of the object in 8cm range If not any obstacle robot will move forward continue If found the object it will move backward turn left and move forward again lt stdlib h gt lt motor h gt lt Ssleep h gt lt sound h gt lt analog h gt include include include include include void main unsigned int sensor 0 unsigned char i 0 sleep 200 sound 4000 50 while 1 sensor 0 for i1 0 1 lt 5 1i sensor sensor 5 if sensor gt 260 sensor sensor analog 0 j backward 50 sleep 800 s_left 50 sleep 600 else forward 50 Program description 1 tart with beep a sound fi fi ee Ml Te ll MicroCamp ATmega8 Activity Kit Manual 107 For convert type of data Motor control Delay Analog to Digital Converter Main Program start with beep Read GP2D120 5 Times Average Data data more than 10 CM away from object do not any object Let go 2 Read the data from GP2D120 and store into sensorvanable Read 5 timesto calculate the average for more precision 3 Check the value of sensor more than 320 or not
61. elay for baudrate Calculate Delay for baudrate Stop timer Ensure Clear Overflow flag Load Prescaler form calculate Load interval Wait until count success Ensure Clear Overflow flag Stop timer 2 Vi Bears loatic Delay for start bit Send data bit Listing 8 1 The soft_serout library file sourcecode continue MicroCamp ATmega8 Activity Kit Manual 83 delay _ baud base Delay for calculate base dat dat gt gt 1 Shift for next bit owe Exe L stop bit delay baud base Delay for stop bit Send More Than 1 byte void serout_text char tx unsigned char p while p serout_byte tx p endif Listing 8 1 The soft_serout library file sourcecode final 84 MicroCamp ATmega 8 Activity Kit Manual Activity 4 SLCD16x2 smple programming A4 1 Open the AVR Studio to create the new project and write the C program following the Listing A4 1 A4 2 Add the soft_serout library file into the project file Build this project A4 3 Connect the PX 400 programmer to the MicroCamp board on The MicroCamp robot at the In System Prog connector Tum on the Robot Downlaod the HEX code to the robot A4 4 Tum off powerand Remove the ISP cable A4 5 Tum on the power and observe the SLCD16x2 operation The SLCD16x2 show message tiroit on the top line include lt soft_serout h gt include lt sleep h gt void main Main Program sleep 1000 Delay 1
62. ettings in the source editor 26 MicroCamp ATmega 8 Activity Kit Manual 3 2 5 View menu This menu includes the command as follows Toolbars Sub menu toggles toolbars on off access to customize dialog Described here Status Bar Toggle status bar on of status bar is the line in the bottom of the screen Disassembler Toggle on off the disassembly window Watch Toggle on off the watch view Memory Toggle on off the memory view Memory 2 Toggle on off the memory view 2 Memory 3 Toggle on off the memory view 3 Register Toggle on off the register view 3 2 6 Tools menu This is the hardware interfacing command menu AVR Studio can interface many hardware fordevelopment For the MicroCamp kit developers must select the AVRprog This is the operating software for the PX 400 Serial Port In System Programmer box Developers must connect the PX 400 box to their COM port before open the AVRprog software 3 2 7 Debug menu This menu have many commands that relates to the program simulation and debugging The MicroCamp kit does not require much usage of this feature 3 3 Building C project file in AVR Studio 3 3 1 Open the AVR Studio If there is any project running developers can close by select the menu Project gt Close Project 3 3 2 To create the new project Select the command at menu Project gt New Project AVR Studio Ele Project Build View Tools Debug Help Eere Mss SAKES POH un HP evnR iaee XG LTO
63. f programs on the AVR In Circuit Emulator or on the built in AVR Instruction Set Simulator AVR Studio supports source level execution of Assembly programs assembled with the Atmel Corporation s AVR Assembler and C programs compiled with WinAVR open source C Compiler AVR Studio runs under Microsoft Windows95 and Microsoft Windows NT Now Windows XP SP2 is commended Free download this software at www atmel com 2 WinAVR WinAVR isa set of tools for the C compiler these tools include avrgcc the command line compiler avr libc the compiler library that is essential for avrgcc avr as the assembler avrdude the programming interface avarice J TAG ICE interface avr gdb the de bugger programmers notepad editor and a few others These tools are all compiled for Microsoft Windows and put together with a nice installer program Free download of the updated version is located at http sourceforge net projects winavr For the MicroCamp Activity kit C programming will be with WinAVR V20050214 User will need to install AVR Studio first and WinAVR after which AVR Studio s mechanism integrates automatically with WINAVR With this feature it assist the user in the development of C language and programming on AVR Studio which is much easier and more powerful compared to WinAVR The compiled file is a HEX file in which case the user hasto download it into the program memory of the AVR microcontroller Board 3 Library These are the
64. g You must close the current project first Open a new project either an APS project file oran object file Save the curent project with all settings Close the curent project Show a list of recent project select one to open Configuration Options This option isonly available when the projectisa code wrting 3 2 3 Build menu project Eg an assembler or AVR GCC project This com mand open the configuration dialog for the current project Includes the command as follows Build the curent project Rebuild all the modules in the project Build and if error free tart debugging session Compile the current source file Clean the current project Save the curent settings in a new make file Undo last editor action Redo any undo action Cut and copy selected text from editor Copy selected text from editor Build Rebuild All Build and run Compile Clean Export Makefile 3 2 4 Edit menu Includes the command as follows Undo Redo Cut Copy Paste Toggle Bookmark Remove Bookmarks Find Find in Files Next Enor Show whitespace Font and color Paste any text from clipboard to the editor Toggle bookmark on off at the selected line in the editor Remove all bookmarks Open a find dialog to search through the curent source file Open a find in files dialog to search through all project files Locate and jump to the next build enor if any Toggle on off whitespace markings Open a font dialog to view edit font s
65. he counting after a pause from timer pause function Function format void timer resume void MicroCamp ATmega8 Activity Kit Manual 61 6 7 5 msec function Read the timer value in milliseconds Function format unsigned long msec Retum value Time value is in millisecond The data type isa long variable 6 7 6 sec function Read the timer value in seconds Function format unsigned long sec Retum value Time value is in second The data type isa long vanable Example 6 14 include lt in_out h gt include lt sleep h gt include lt timer h gt void main Main program timer start Set the startin point of timer while 1 Endless loop if msec gt 500 Check timer value Is it more than 500 timer _stop Stop and clear the timer value toggle _c 5 Toggle LED indicator every 0 5 second timer start Start timer counting again i INNOVATIVE EXPERIMENT 62 MicroCamp ATmega 8 Activity Kit Manual MicroCamp ATmega 8 Activity Kit Manual 63 Chapter 7 Building robot with MicroCamp kit This chapter focus leaming the applications of the MICROCAMP microcontroller The building of a robot integrates knowledge and technology which includes electronics programming mechanical movements and thinking process The Microcamp Activity kit supports this concept This kit includes all parts for building a simple mobile robot Use
66. he junction or line with the same 2 sensors All you have to do is to edit your program code When the robot moves to the black line T junction both sensors will detect the black line You must add the program for support this scenario The improved C program is shown in the Listing A8 1 A8 1 Improve the simple black line sheet from Activity 7 Add some cross lines Add as many junctions as you like However make sure that they are at least 2 robots width apart A8 2 Create the new project file and make the C program following the Listing A8 1 Build this project file A8 3 Connect the PX 400 programmer box with MicroCamp robot and download the HEX code to the robot Tum off power and unplug ISP cable from the robot A8 4 Place the robot across the black line on the sheet Tum on power and press SW1 switch The Robot will move along the black line When the robot detects the crossing it will brake and drive sound once When it finds the second crossing the robot will drive sound twice and this will increase for the subsequent crossings Note In the motor brake operation robot will stop and lock the motor s shaft immediately But sometimes this is not enough You must program the robot to move backwards for a short time This will cause the robot to stop at its position include lt in_out h gt include lt sound h gt include lt analog h gt include lt motor h gt unsigned int AD0 350 AD1 350 unsigned char i
67. ich for resetting to restart the microcontroller operation from user PDO RxD pin is the serial receiver pin It is shared with IRM connector for 38kHz Infrared Receiver Module and 5 pin of Serial data communication port PD1 TxD pin isthe sral transmit pin It is snared to drive the LED5 IND2 label and TxD pin of 5 pin of Serial data communication port For LED4 or IND1 is direct connected to PC5 of ATmega8 microcontroller with curent limit resistor The MicroCamp board is equipped with 2 Push button switches They are connected to PD2 and PD3 and connected 4 7kQ resistor pull up for setting the logic level to High in a nomal operation and changing to logic Low or 0 when switch iS pressed PD4 pin is connected with a Piezo speaker via coupling capacitor 10uF The MicroCamp controller board includes the DC motor driver circuit It has 2 outputs The driver IC is L293D H Brdge driver One DC motor driver circuit requires 3 Signal pins to control A and Binput for applying the signal to select the spin direction of motor E control pin is used for enble and stop operation of driver circuit In addition the user can control the motor speed with apply PWM signal to this pin If the width of PWM is wide it means the high level of voltage sent to motor output At the output of L293D bi color LED is connected to indicate the voltage pole at the output Green color indicates forward Red color indicates backward The Powe
68. if key d key D forward 100 ii flagl 2 BE serout_byte 0 0xFE serout_byte 0 0xC0 serout_text 0 Forward eee ELACZ else if key c key C Hi s_left 100 it Cr bagl s serout_byte 0 0xFE serout_byte 0 0xC0 serout text 1 Watiunain Left Wr flag 3 else if key b key B bd Standard Library For SLCD Module For Delay For Receieve Data from ER 4 For Generate Sound For Control Motor Delay 1 Sec Initial Serial Comm 9600 8N1 Set IR remote control Baudrate Status Sound Clear Screen SLCD Show Text on First Line Infinite Loop wait and read key from ER 4 in 33ms key for Backward Show Backward on SLCD D key for Forward Show Forward on SLCD C key for Forward Show Turn Left on SLCD D key for Forward Listing A13 1 The C program for Microcamp robot with Remote control activity continue MicroCamp Almega8 Activity Kit Manual 117 s right 100 aie elle EA Show Turn Right on SLCD serout_byte 0 0xFE Serout byte 0 0x 0 sero ut text 0 TUrn RG inital y flag 4 else motor_off lag 0 Stop motor if no key press Program description Thisc ode isdevelped from Listing A12 1 to control the robot s movement with ER 4 Remote control The code check the button s data that received asA B C Dora B C d In this code does not support case sensitive
69. ing 600 to 950 The black surface value is during 100 to 300 The example reference value for detecting the line is 600 100 2 350 ICR CAMP 94 MicroCamp ATmega 8 Activity Kit Manual Activity 7 Robotmovesalong the black line The robot moving along the line has 3 scenarios 1 Both sensors read values that are white The robot will move forward Thus this program is written so that the robot moves forward nomally 2 The left sensor reads black while the right sensor reads white This oc curs when the robot is slightly tumed to the right Thus the program is written for the robot to move back left to resume its nomal path 3 The left sensor read white while the right sensor reads black This occ urs when the robot is sightly tumed to the left Thus the program is written for the robot to move back to the right to resume its nomal path From all scenarios you can make the C program as follows in the listing A7 1 Scenario 1 Scenario 2 Scenario 3 Both sensors put across Left sensor detects the line Right sensor detects the line MicroCamp ATmega8 Activity Kit Manual 95 include lt in_out h gt include lt sound h gt include lt analog h gt include lt motor h gt Motor diver library unsigned int AD0 350 AD 350 Determine the sensor reference value void main while in_d 2 1 Wait SW1 pressing to start the program while 1 if analog 3 gt ADO amp amp a
70. is different from the previous example at the Sleep function The function needs the parameter delay which is being declared in the tone for setting the time delay in milliseconds MicroCamp ATmega8 Activity Kit Manual 35 4 3 Library A Library isa file which includes one or many functions that operates similarly Nomally the name of library file will according with the function for easy remembrance and referencing To use libraries programmers need to declare the prototype of the library at the head of the main C program The Correct path which contains the library file must be set when creating the AVR Studio Project File 4 3 1 How to make library The library file is similar to a C program but without any Main program or main functions After write the codes it must be saved asa h file For example create the library file led1 h The steps for creating this file are as follows 1 Create the new file from File gt New File to open the new editor window 2 Type in the code of the Blink function as follows void sleep unsigned int ms unsigned int i j for 1 0 i lt ms i for j 0 43 lt 795 5 void blink unsigned int cnt unsigned int _cnt 0 DDRC BV 5 Set PC5 gt Output while _ cnt lt cnt 2 Test Counter PORTC BV 5 Toggle PC5 bit sleep 300 Delay 0 3 Second _ centt 3 Save the file by selecting File gt Save As You need to save itasa h file
71. it Manual 101 10 3 Reading GP2D120 with A D converter The GP2D120 s output voltage will change acoording to the detection distance For example Vout 0 5V is equal 26cm distance and Vout 2V is equal 6cm distance The table 10 1 shows the summary of GP2D120 s Vout and Distance relation For interfacing with A D converter module within microcontroller the result is raw data from the A D conversion The user will need to use the software to convert the raw data to the exact distance Forexample see the Table10 1 The raw data from conversion is 307 It is equal 8cm distance ee20130 Vout Rew data Distance Table 10 1 Shows the relation about the GP2D120 s voltage output A D conversion raw data and the calculation distance 102 MicroCamp ATmega 8 Activity Kit Manual Activity 9 Installaton GP2D120 with MicroCamp Part list Straight joiner x4 3x10mm sc rew x4 S X W i 3mm nut x4 GP2D120 module bundled in MicroCamp2 0 Standard kit Angled joiner x2 Obtuse joiner x2 DA Fir MicroCamp that install SCD ready Warning for the signal cable of the GP2D120 The GP2D120 module has a different pin arrangement then that of the MicroCamp board even though it looks similar Therefore a special signal cable has already been connected to the GP2D120 module The user just needs to connect the other end of the cable to the connection points of the MicroCamp board DO NOT remove the cable fro
72. king registers e 23 General I O lines manage to 3 groups 1 Port B PBO to PB7 Use 2 pin PB6 and PB7 for connect crystal for clock generator circuit PB2 to PB5 nomally are reserved for In system porogramming port Thus PBO and PBI free for general purpose application 2 PortC PCO to PC6 7 pins PCO to PC5 are analog input pins PC6 nomally use for RESET pin 3 Port D PDO to PD7 8 pins This port can support general purpose application Pages wy PC6 RESET 1 PC5 ADC5 SCL PDO RxD PC4 ADC4 SDA PD1 TxD PC3 ADC3 PD2 INTO PC2 ADC2 PD3 INT1 5 PC1 ADC1 PD4 T0 XCK 6 n PCO ADCO Vcc D GND GND 8 AREF PB6 TOSC1 XTAL1 9 AVcc PB7 TOSC2 XTAL2 a PB5 SCK PD5 T1 PB4 MISO PD6 AINO PB3 MOSI OC2 PD7 AIN1 PB2 SS OC1B PBO ICP1 PB1 OC1A Figure1l 4 ATmega8 microcontroller pin assignment 10 MicroCamp ATmega 8 Activity Kit Manual e Two 8 bit Timer Counters with Separate Prescaler one Compare Mode e 16 bit Timer Counter with Separate Prescaler Compare Mode and Capture Mode e Real Time Counter with Separate Oscillator e Three PWM Channels e 6 channel ADC 10 bit Accuracy e Byte oriented Two wire Serial Interface e Programmable Serial USART e Master Slave SPI Serial Interface e Programmable Watchdog Timer with Separate On chip Oscillator e On chip Analog Comparator e Power on Reset and Programmable Brown out Detection e Intemal Calibrated RC Oscillator e Extemal and Intemal In
73. l A1 5 Create a new project file and write the following C Codes shown in A1 2 A1 6 Connect the PX 400 programmerto the MicroCamp board on The MicroCamp robot at the In System Prog connector Tum on the Robot Downlaod the HEX code to the robot A1 7 Tum off power and Remove the ISP cable A1 8 Make sure the robot ison a flat surface Tum on the power and observe the robot The robot will be activated when you press SW and move in circles continually until you press the SW2 to stop the robot movement include lt in out h gt include lt sleep h gt include lt motor h gt void main while 1 while in_d 2 1 motor 1 100 Ai motor 2 30 id while in_d 3 1 Md motor off Program description Loop for checking SW1 pressed Apply full power for Motor 1 Apply 30 power for Motor 2 Loop for checking if SW2 pressed Stop all motors In Listing A1 2 the forward and backward com mands are not used for driving the robot The MOTOR function is used instead This function can control both motor outputs separately Thismeansthat you can control both the motor s peed differently When both speeds are not equal the robot will move towardsthe direction where the motorisofa lower speed If the speed difference is great the MicroCamp robot will move in circles The While command is used in this program If SW1 at PD2 port is being pressed the LOGIC value of O is retumed The first
74. log input channel 5 SCL Output Serial Clcok output in 2 Wire bus interface PC6 1 Input Output PC6 port RESET Input External reset Name Pin number Type Description PDO 2 Input Output PDO Digital port RxD Input USART receiving input PD1 3 Input Output PD1 Digital port TxD Output USART transmit output PD2 4 Input Output PD2 Digital port INTO Input External interrupt channel 0 PD3 5 Input Output PD3 Digital port INT1 Input External interrupt channel 1 PD4 6 Input Output PD4 Digital port XCK Input Output USART external clock TO Input Timer 0 External input PD5 11 Input Output PD5 Digital port T1 Input Timer 1 External input PD6 12 Input Output PD6 Digital port AINO Input Analog comparator input channel 2 PD7 13 Input Output PD7 Digital port AIN1 Input Analog comparator input channel 1 Table 1 1 Pin function summary of ATmega8 microcontroller finish i INNOVATIVE EXPERIMENT 14 MicroCamp ATmega8 Activity Kit Manual MicroCamp ATmega8 Activity Kit Manual 15 Chapter 2 Development software for MicroCamp Actvity kit Programming development in MicriCamp Activity kit is C language The software tools that are installed for programming are the following 1 AVR Studio This software tool is developed by Atmel Corporation AVR Studio isa Development Tool for the AVR microcontrollers AVR Studio enables the user to fully control execution o
75. m the module and do not replace it with signal cables from other sensor modules A9 1 Screw in 2 of 3 x 10mm machine screws into the installation sot on the GD2D120 module Tighten point flextible to less adjustment loosely followed by 3mm nuts Do not tighten Straight joiners A9 2 Place the straight joiner between the screw and nut and tighten the screw Leave it loose enough to change the angle Obtuse joiners A9 3 Attach an obtuse joinerto the otherend of the straight joiner Repeat for both sides i A9 4 Attach a straight joiner to the other ena of A Angled joiners the obtuse joiner Then place an angled joiner to the other end of the straight joiner MicroCamp ATlmega8 Activity Kit Manual 103 A9 5 Remove the Switch sstructure at the front of robot first Afterthat install the GP2D120 structure instread by use a 3x10mm machine screw to screw it in loosely with a 3mm nut and on the right and left sides A9 6 Connect the GPD120 s cable to PO port of MicroCamp robot as seen in the picture below ICR CAMP 104 MicroCamp ATmega8 Activity Kit Manual Activity 10 Measure distance with GP2D120 The MicroCamp robot can read the data from GP2D120 with analog function in C programming To convertthe A D conversion raw data to the distance value the suitable formula will be used as follows R 2933 V 20 1 Thus Ras Distance in Centimetre unit V asthe raw data from A D conversion The range
76. movement of MicroCamp robot Activity 1 1 Forward and Backward movement A1 1 Open the AVR Studio to create the new project and write the C program following the Listing A1 1 Build this project A1 2 Connect the PX 400 programmer to the MicroCamp board on The MicroCamp robot at the In System Prog connector Tum on the Robot Downlaod the HEX code to the robot A1 3 Tum off powerand Remove the ISP cable A1 4 Make sure the robotison a flat surface Tum on the powerand observe the operation The MicroCamp robot moves forward See both LED motor indicators light in green color After 1 second both indicators change color to red and the robot moves backward If this is incorrect you will need to re connect the motor cable to its opposite port polarity Do this until your robot moves correctly Once its done Use this motor port configuration for all your programming activities from now on The robot will move forward and backward continually until you turn off its power include lt in out h gt include lt sleep h gt include lt motor h gt Motor driver library void main while 1 Endless loop forward 100 Move the robot forward sleep 1000 Delays 1 second backward 100 Move the robot backward sleep 1000 Delays 1 second Listing A1 1 The C Program that allows the Microcamp Robot to move in circles MicroCamp ATmega 8 Activity Kit Manual 69 Activity 1 2 Circle shape movement conto
77. n the microcontroller Go back to step 1 if you have enors Steps 2 and 3 will not be shown as the C Compiler will do all of these in its background After installing AVR Studio and WINAVR software the library files are required to be copied in order to support the MicroCamp kit The MicroCamp Library files are contained in the MicroCamp_include folder in the CDROM that is included in this kit In the C programming development platform in AVR Studio developers need to compile it into project file format After the codes are being compiled into HEX file using the same name as the project filename the file is needs to be downloaded into the ATMEGA8 Microcontroller For example Name the project file to test_segment After compiled the result file is test_segment hex 24 MicroCamp ATmega 8 Activity Kit Manual 3 2 The AVR Studio V4 0 windows details The figure below shows the main components in the main window of the AVR Studio software File and Libraries that link with the current project Toe Current file is under development E Eile Project Build Edit View Tools Dendd D Sag amp Sh AA fl m oo AVR GCC x include lt ipst h gt l g pressure _sensor default a float P_abs char analog_channel SY Source Files F 3 i f 10 55 __ E pressure_sensor c Main C program file of this project L Header Files External Dependencies Tag A
78. nalog 4 gt AD1 Both sensor detect the white surface forward 100 Move forward if analog 0 lt ADO Left sensor detects black line s_left 100 ff Ween lee if analog 1 lt AD1 Right sensor detects black line s right 100 ff Wise FAC late Listing A7 1 The C program code for controlling the MicroCamp robot to move along a black line A7 1 Make a simple black path sheet You can use any conventional black insulating tape ona white cardboard paper Leave most of the surface area white with only the black line significant You may use a standard black marker and white paper as well A7 2 Create the new project file and make the C program following the Listing A7 1 Build this project file A7 3 Connect the PX 400 programmer box with MicroCamp robot and download the HEX code to the robot Tum off power and unplug ISP cable from the robot A7 4 Place the robot across the black line on the sheet Tum on power and press SW1 switch The Microcamp Robot will move along the black line It is possible that the robot moves out of the line You can improve the precision by editing the program with adjusting the sensor reference value and adjust to the position of both the Infrared Reflector sensors ICR CAMP 96 MicroCamp ATmega 8 Activity Kit Manual Activity 8 Line crossing detection From the activity 7 you can improve the MicroCamp robot so that it moves along the black line and detects t
79. nd respectively Reserve a port for connecting 38kHz Infrared Receiver module This port will be assigned to share with Serial Receiving signal RxD to extemal serial data communication device Piezo speaker for sound beeps 2 Push button switches RESET switch 2 LED indicators active when logic is High 2 channels of DC motor drivers They drive 4 5 to 6V 600mA DC motor with LED indic ators Supply voltage of 4 8 to 6V from 4 of AA size batteries Contain in battery holder at the back of controller board On board switching regulator circuit to maintain the 5V supply voltage when motors function and consume more cunent 1 1 2 PX 400 The serial portinterfac e In System Programmerbox This programmer is used for programming the code into flash memory within the AVR microcontroller It can work a wide vanety of AVR microcontrollers Its features are Connection with computer serial port via RS 232 If the computer has only USB port a USB to Serial port converter can be used The UCON 232S is highly recommended for this purpose Program the AVR microcontroller via ISP cable Supports Read Write Erase and Data protection functions MicroCamp ATmega8 Activity Kit Manuale 5 PX400 ne Figure 1 2 Shows PX 400 In Systrem Programmer box for AVR microcontroller Require 5V supply voltsge from target microcontroller board Operate with AVR Prog software This software is inclu
80. nect the Z IRM cable to IRM port on the MicroCamp controller board following the photo below IRM port Right angle joiner poe 112 MicroCamp ATmega8 Activity Kit Manual 11 3 Senal data receiving function C programming for interfacing the 38kHz Infrared Receiver module requires a specific library It is serial h file This library file contains the function about receive and transmit data via UART module within ATmega8 microcontroller The functions consist of uart_set_baud and uart_get_key The detail can describe as follows 11 3 1 uart set baud It is baudrate setting function for UART module of ATmega8 microcontroller Syntax void uart set baud unsigned int baud For working with ER 4 Remote control must set the baudrate as1200 bit persecond The example of this function is uart_set_baud 1200 113 2 uart get key It is 1 byte receiving data function at RxD pin of ATmega8 microcontroller Usercan set the waiting time at timeout parameter If set to 20 000 the timeout is 30ms Syntax char uart_getkey unsigned int timeout Example key uart_getkey 20000 Get data l byte and store to key variable MicroCamp ATmega 8 Activity Kit Manual 113 Activity 13 Getting data from ER 4 Remote contol A12 1 Open the AVR Studio to create the new project and write the C program following the Listing A12 1 A12 2 Add the serial h library file into the project file Build this project
81. ng Equal Not equal More than Less than More than or Equal Yav lt Less than or Equal NOT amp amp AND OR Example 5 8 a 10 b 4 c OxA0 Operation Condition Result a gt b true 1 a gt c false 0 a gt C true 1 because 0xAO 10 al b true 1 al c false 0 5 2 1 amp amp and operation NOT can be summarized as follows Operation Result false true 1 true false 0 In summary the result of NOT is to reverse the value of the input S amp S AND can be summarized as follows Operation Result false amp amp false false 0 false amp amp true false 0 true amp amp false false 0 true amp amp true true 1 In summary the result of AND will be false if one of condition or both are false MicroCamp ATmega8 Activity Kit Manual 47 OR can be summarized as follows Operation Result false false false 0 false true true 1 true false true 1 true true true 1 In Summary the result of OR will be true if one of the condition or both are true Example 5 9 Determine a 10 b 4 and c OxA0 Operation Condition Result a gt b true 1 a gt b true 1 a gt c false 0 a gt c true 1 because 0xA0 10 al b true 1 al c false 0 or a gt b false 0 a gt c true 1 I a gt c false 0 a b false 0 a c true 1 or Operation Result a gt b amp amp a gt c false 0 a b amp amp a gt c true 1 a b amp amp I a
82. ng infrared light feature to your robot Your MicroCamp robot Can avoid obstacles without having to make any physical contact 10 1 GP2D120 features e Uses Infrared light reflection to measure range e Can measure a range from 4to 30cm e 4 5 to 5V power supply and 33mA electric current e The output voltage range is 0 4 to 2 4V when supplied by 5V Output voltage V Infrared LED transmitter Infrared Receiver GP2D120 Vout GND Vcc 0 4 8 12 16 20 24 28 32 spy oS Distance cm 253496 ee Use Kodak R 27 gray white atest eel _ paper The white side has a Nessu reign 1st measure 2nd measure Grmeave 90 reflection rate made N N N from a material that reflects Vout Not stable 1st output X 2nd output n output light for range measurement gt 5 ms Figure 10 1 GP2D120 pin assignment operation and characteristic curve GP2D120 module is optional component for MicroCamp2 0 Beginner kit and bundled in Standard kit 100 MicroCamp ATmega8 Activity Kit Manual GP2D120 Infrared Rangermodule has3 terminals Powerinput Vcc Ground GND and Voltage output Vout To read the voltage values from the GP2D120 you must wait till after the acknowledgement period which is around 32 to 52 9 ms The output voltage of GP2D120 at a range of 30 cm and 5V power supply is between 0 25 to 0 55V with the mean being 0 4V At the range of 4 cm the output voltage will change at 2 25V 0 3V
83. nit Build succeeded with 1 Warnings If any error occurs such as an illegal command or a link error the Build Output window will appear Developers need to edit the program pfix all errors and re comple the code until it is correct and the HEX file is being compiled properly After compilation the file Swifch_LED hex will be made and stored in the folder of that project file For example The result file Switch_LED hex is stored at the folder Switch_LED hex is stored at G Work2006 AVRROBOT Code Switch_LED default MicroCamp ATmega8 Activity Kit Manual 31 3 4 How to develop the previously project file Developers can open the previously project file for editing or improvement Enter to menu Project gt Open Project and select the path that store the target project file The project file is saved as aps file Example If would like to open the Swifch_LED project file select to Project gt Open Project and access to the path or folder which contains the Swifch_LED aps file Open this file for editing Developers can save with the same name or different 3 5 Downloading and Testing the program The next step after compiling the project file is to download the HEX file to MicroCamp controlelr board In this example the result file is saved as Swifch_LED hex The step of downloading and testing are as follows 3 5 1 Tum on the POWER switch The green LED at ON labeled ison 3 5 2 Connect the download cable ISP
84. nt It s different from b and c It need s to be converted to a long data type with the long a command The result of c 1150 40 MicroCamp ATmega8 Activity Kit Manual 4 8 Type of vanable in WinAVR compiler 4 8 1 Array 4 8 1 1 One dimension Anay The declaration form of this one dimension anay is type name size Thus type is Data type of an Amay variable name is the Amay variable name size is the Numberof size of Anay optional Accessing the member of each anay has the general fom as follows name index Thus index is the Index value for pointing to any member in array This param eter can be a number ora variable but these must be integer format Example 4 13 From declaration char arr 4 It means arr is an array variable It has 4 sub variables such as arr 0 It is the first member but index value is 0 arr 1 It is the second member but index value is 1 arr 2 It is the third member but index value is 2 arr 3 It is the forth member but index value is 3 arr 0 arr 1 arr 2 and arr 3 variable are char data type All variable size are 1 byte Thus declaration of the arr variable requires 4 bytes of space MicroCamp ATmega8 Activity Kit Manual 41 Example 4 14 char dat 8 1 3 5 7 9 11 13 15 This declares the array dat It is 8 cells and the value for each cell is as follows dat 0 1 dat 1 3 dat 2 5 dat 3 7
85. oCamp ATmega 8 Activity Kit Manual 3 3 6 Next write the C code in the Switch_LED c file TThis file controls the microcontroller to On and off the LED when the switch is pressed The details amp codes of this file is shown in the Listing 3 1 include lt in_out h gt include lt sleep h gt void main while 1 iE im C2 toggle _c 5 dE ia Ca toggle d 1 j sleep 200 Listing 3 1 C code of Switch_LED c 3 3 7 Compile the target file to Switch_LED hex by selecting the command at the menu Build gt Build or press F7 button or click at X button The status of this operation will be shown at the Build or Output window at the bottom of the main window of the AVR Studio as shown in the diagram Build rm rf Switch_LED o Switch_LED elf dep Switch_LED hex Switch_LED eep Build succeeded with 0 Warnings avr gcec exe I C avr_robot iinclude Switch_LED c 4 warning return type of main is not int avr gcc exe mmcu atmegaS Switch_LED o lm o Switch_LED elf avr objcopy 0 ihex R eeprom Switch_LED elf Switch_LED hex avr objcopy j eeprom set section flags eeprom alloc load change section lma eeprom 0 0 ihex Switch_LED elf 3w mmcu atmegas Wall gdwarf 2 DF_CPU 16000000UL 00 funsimed char funsigned AVR Memory Usage Device atmega s Program 1078 bytes 13 2 Full text data bootloader Data 0 bytes 0 0 Full data bss noi
86. ogramming performance Signal connector Infrared Reflector Figure 9 1 ZX 03 Infrared Reflector information 90 MicroCamp ATmega 8 Activity Kit Manual 9 1 ZX 03 Infrared Reflector The heart of this sensor is TCRT5000 reflective object sensor It is designed for close proximity infrared IR detection There s an infrared diode behind its transparent blue window and an infrared transistor behind its black window When the infrared emitted by the diode reflects off a surface and retums to the black window it strikes the infrared transistors base causing it to conduct current The more infrared incident on the transistors base the more cunrent it conducts When used as an analog sensor the ZX 03 can detect shades of gray on paper and distances over a short range if the light in the room remains constant The suitable distance from sensor to line or floor is during 3 to 8 mm The output voltage is during 0 1 to 4 8V and digital value from10 bit A D converter is 20 to 1 000 Thus ZX 03 will suitable to apply to line tracking sensor 9 2 analog Read analog signal function of analog h library This function is suitable for reading the analog data from PCO to PC5 pin These ports are set to analog input The analog to digital converter resolution is 10 bit The conversion result is during O to 1 023 for scaling voltage O to 5V Syntax unsigned int analog unsigned char channel Parameter channel Select the
87. olution activity continue 74 MicroCamp ATmega8 Activity Kit Manual else sleep 300 eles am iets C2 10 ae ene 2 S i1 cnt else cnt _ 0 backward 100 sleep 400 s right 50 sleep 300 else forward 100 ve a Lee li hd If counter is less than 5 Set time value for turning to 0 3 second Check the leftt side collision Counter is odd number or not If yes the previous collision is right side Increment counter TE aoe Clear Counen Robot move backward for 0 4 second Turn right for 0 3 second If not collision move forward Listing A2 2 The C program for MicroCamp robot in Trapping wall solution activity final INNOVATIVE EXPERIMENT MicroCamp ATmega8 Activity Kit Manual 75 Chapter 8 Serial LCD module activity with MicroCamp The SLC D16x2 is the 16 characters 2 lines LCD module that communicates by serial interface It receives data senally and displays on the LCD Accept serial data at 2400 or9600 baud rate and acceptseitherTILorRS 232 levelsby selection of 2 jumpers Supports the standard LCD controller HITAC HI HD44780 or SEIKO EPSON SED1278 compatible Both 1 8 Duty and 1 16 Duty of 1x16 LCD Module can be used by jumper selection too Normally LCD interfacing requires at least 6 wires but SLCD16x2 need only one signal wire This dilay module is suitable for MicroCamp robot 8 1 SLCD16x2 informa
88. ong the black lie icsissescsssenenneeneennenaes 94 Activity 8 Line crossing detection ssssssssssssssssererrerrrrrrsresnse 96 Chapter 10 MicroCamp robot with IR Ranger Capability c scsseseeeesseees Activity 9 Installation GP2D120 with MiCrOCAMDP scssceeeeeneenes 102 Activity 10 Measure distance With GP2D120 cccccsecceenneseneeeens 104 Activity 11 Non contact Object Avoidance RODOl sccsseeneenes 106 Chapter 11 MicroCamp robot with Remote CONntrol c scsecsesseeeessesseseeeeseees Activity 12 Install the 38kKHZ Receivcer MOMUIE cccseceeenneeeane 111 Activity 13 Getting data from ER 4 Remote control cscccs00 113 Activity 14 Infrared Remote control rODOt csccsssneceseecaneseeneees 115 MicroCamp2 0 libraries source programM s s ssssssssssunnnnnnnnnnnnunnnnnnnnnn MicroCamp ATmega8 Activity Kit Manual 3 Chapter 1 MicroCamp Actvity kit hardware MicroCamp is a set of Microcontroller Activity kit for leaming about Micro controller operation via Robotic activities with C language programming You will leam about simple operation of microcontroller and how to interface with external components in real word applications This activity kit includes Microcontroller board will call MicroCamp board Swtich module Infrared Reflector module DC motor gearboxes and many other mechnical parts for building a programmable robot Figure 1 1 shows the layout of MicroCamp main controller
89. onnect to 2E port include lt avr io h gt include lt avr signal h gt include lt avr interrupt h gt define ALL 3 define all 3 unsigned char _dutyl 0 duty2 0 char pwm_ini 0 SIGNAL SIG OVERFLOW1 OCRIAL _dutyl OCRIBL _duty2 void pwm_init TCCRIA 1 lt lt WGM10 TCCRIB 1 lt lt CS12 1 lt lt CS10 1 lt lt WGM12 TCCRIB 1 lt lt CS12 1 lt lt WGM12 TIFR 1 lt lt TOV1 sei void pwm char channel unsigned int duty duty duty 255 100 if pwm_ini 0 pwm_init pwm_ini 1 if channel 2 TCCR1IA _BV COM1A1 DDRB _BV PB1 OCRIAL duty _dutyl duty else if channel 1 TCCRIA _BV COM1B1 DDRB _BV PB2 OCRIBL duty _duty2 duty MicroCamp Almega8 Activity Kit Manual 121 Clear all motor Clear all motor duty cycle variable Flag for check initial Interval 1 ms Duty Cycle 1 Read Duty Cycle 2 Read Set Prescaler Set Prescaler Clear TOVO clear TIMSK 1 lt lt TOIE1 Enable TimerO Overflow Interrupt timer_enable int _BV TOIE1 Convert 0 100 to 0 255 PWM Initial If no Intitial it show now initial 122 MicroCamp ATmega8 Activity Kit Manual else if channel 3 TCCRIA _BV COMI1A1 DDRB BV PB1 OCRIAL duty _dutyl duty TCCRIA _BV COMI1B1 DDRB BV PB2
90. onvert Integer to decimal Ascii utoa cm dec2 10 Convert Integer to decimal Ascii serout_byte 2 0xFE serout_byte 2 0x00 Command Clear Screen serout_byte 2 0xFE serout_byte 2 0x80 Command First Line sero ut text 2 RAW Data 1 serout_text 2 dec Show Ascii data serout_byte 2 0xFE serout_byte 2 0xC0 Command Second Line serout text 2 Ubistance mi serout_text 2 dec2 show centimeter data serout_byte 2 0xFE serout_byte 2 0xCE Command 2nd line position 14 serout text 27 VCM sleep 500 Program description 1 Intitial the serial data communication and SLCD16x2 2 Loop to read the analog data at PO port w data to Centimetre unitby cm m gp2 b k formula 4 Covert the calculation data to ASCII for displaying at SLCD16x2 3 Convert the ra 5 Loop the operaition every 0 5 second Listing A10 1 The C program for reading the GP2D120 s raw data and convert to distance unit and display on the SLCD16x2 ICR CAMP 106 MicroCamp ATmega8 Activity Kit Manual Activity 11 Non Contactobjectavoidance Robot A11 1 Open the AVR Studio to create the new project and write the C program following the Listing A11 1 A11 2 Add the analog library file into the project file Build this project A11 3 Connect the PX 400 programmer to the MicroCamp board on The MicroCamp robot must install GP2D120 and SLCD ready atthe In System Prog conn
91. oose Install Location window appears User can change the path and the folder for installation of WinAVR by clicking at the Browse button and selecting the respective folder The proposed folder is C WinAVR After selection click on the Next button to continue to the next step amp WinAVR 20050214 Setup Choose Install Location Choose the folder in which to install WinAYR 20050214 Setup will install Wind R 20050214 in the Following Folder To install in a different folder click Browse and select another Folder Click Next to continue Destination Folder Space required 71 6MB Space available 12 1GB MicroCamp ATmega8 Activity Kit Manual 21 2 2 6 In the Choose Components window select the components which you want to install or follow according to the below diagram Click on the Install button to begin installation WinAVR 20050214 Setup fic Choose Components Choose which features of Win4YR 20050214 you want to install D Check the components you want to install and uncheck the components you don t want to install Click Install to start the installation Select components to install tories to PATH Recommended Add Shortcuts to Desktop F Install Programmers Notepad Space required 71 6MB 2 2 7 The installation process starts and reports the status back on the screen The User needs to wait until the installation is complete Click on the Finish button to end
92. osite direction Example 6 11 motor 2 100 Drive motor channel 2 with full power 100 6 5 2 motor stop function It is the brake motor function The motor shaft will lock after active this function Function format void motor stop char _ channel Parameter _channel select motor output channel This parameter has 3 values 1 for braking motor at OUTI channel 2 for braking motor at OUT2 channel ALL for braking all motor channel Example 6 12 motor _stop 1 Brake motor channel 1 motor stop 2 Brake motor channel 2 motor stop ALL Brake motor both channel 1 and 2 MicroCamp ATmega8 Activity Kit Manual 59 6 5 3 motor_off function This function is used for stopping the motor operation and to tum off the voltage of all motor outputs This function is similar to the motor function which sets the power value to 0 Function format void motor off 6 5 4 forward function This function is used for driving DC motor to move the robot in forward direction Function format void forward int speed Parameter speed determine the powerapplied to motor output Range isO to 100 6 5 5 backward function This function is used for driving DC motor to move the robot in a backward direction Function format void backward int speed Parameter speed determine the power applied to motor output Range isO to 100 6 5 6 s left function This function is used for dri
93. p amp key lt 0x7F if Program serout_byte 0 0xFE serout_byte 0 0x8E serout byte 0 key flag key description Infinite Loop read key from remote control 33 mS Test Key in Range and not same key Show on SLCD This code requires 2 imporatant libraries include serial h and soft serouth The operation step as follows 1 Delay 1 second to wait for SLCD initailize 2 Set the baudrate for SSCD communication to 9 600 bit per second Set the baudrate for receiving data from ER 4 Remote control to 1 200 bit per second 4 Generate the sound for starting the operation 3 5 Send the clearscreen command to SLCD and send message You Press Key to display 6 Loop the program forchecking the button pressing at ER 4 Remote control If press the illegal button ordo not press any button program will not get the button data 7 If the pressing is corect program will get the button data to display on SLCD screen Listing A12 1 The C program code for getting the button data from ER 4 Remote control ICR CAMP MicroCamp Almega8 Activity Kit Manual 115 Activity 14 Infrared Remote contol Robot From the Activity 12 MicroCamp robot can get data from ER 4 Remote control 8 valuesasA B C Dand a b c d The button s position on ER 4 are designed to suitable for direction movement control as follows Move forward top button orD ord button Move
94. r suppy circuit of this board is switching type circuit TL499A is set to sep up 5V switching regulator for supply voltage to all microcontroller circuit except for the motor driver With this circuit it helps microcontoller voltage supply to be more stabilized Although DC motors require more power during operation but the supply voltage of microcontroller is still fixed at 5V 8 MicroCamp Almega 8 Activity Kit Manual 1 3 MicroCamp activity kits cable assignment The MicroCamp activity kit includes some signal cables for the interfacing be tween the controller board sensor module and the computer They includes the ISP cable for programming the microcontroller PC B3AA 8 cables for interconnection to the sensor module and a Seral port cable for interfacing with the computer 1 3 1 ISP cable It is 10 wires ribbon cable Both endsare attached to the female 10 pin IDC header It is used for interfacing between ISP programmer box and Microcontroller board at ISP connector This ISP cable s assignment is compatible with Atmel s programming tools standard The wire assignment can show with the diagram below 5V MOSI GND N A GND RST GND SCK GND MISO 1 3 2 J S3AA 8 cable This is an INEX standard cable 3 wires combined with 2mm The J STconnectoris at each end 8 inches 20cm in length Used for connecting between microcontroller board and all the sensor modulesin MicroCamp kit The wire assignment is shown in the diagram
95. required analog input The value is O to 4 for PO to P4 port of MicroCamp Retum value Oto 1023 from the analog to digital converter module within microcontroller MicroCamp ATmega8 Activity Kit Manual 91 Activity 6 Testing black and white area The MicroCamp robot is attached with 2 of Infrared Reflector module at bottom of the robot base ready Thus this activity will only dwell on programming Before develop the robot to track the line developers must program the robot to detect the difference between black and white surface A6 1 Open the AVR Studio to create the new project and write the C program following the Listing A6 1 A6 2 Add the analog library file into the project file Build this project A6 3 Connect the PX 400 programmer to the MicroCamp board on The MicroCamp robot at the In System Prog connector Tum on the Robot Downlaod the HEX code to the robot A6 4 Tum off powerand Remove the ISP cable A6 5 Make the black amp white testing sheet The white surface area is 30 x 30 cm and black surface is 30 x 30cm as shown below 92 MicroCamp ATmega8 Activity Kit Manual include lt stdlib h gt For convert type of data include lt soft_serout h gt Software serial communication For LCD include lt analog h gt include lt motor h gt include lt sleep h gt void lcd command unsigned char pin unsigned char command serout_byte pin OxFE serout_byte pin command int
96. rs can leam about programming and how to apply the microcontroller aspects via robotic activities The Mobile robot in MICROCAMP has 2 DC Motor gearboxes for moving and 4 sensors for detecting extemal values These are 2 touch sensors and 2 Infrared Reflector Line tracking sensors for use in black and white line following Part li 48 1 DC motor gearbox x2 Circle base plate MicroCamp board Box holderx 1 l Wheel and JJJ i wt Tire set x 2 A S00 Plastic spacersetx1 Nut and Screw set x 1 Ploastic joiners Straight Right angle and Obtuse SS J 6 oe Infrared reflector x 2 Swithc module x 2 2mm Self tap ping 25mm metal screw x2 spacerx2 64 MicroCamp Almega8 Activity Kit Manual Construction 1 Fix on the 2 wheels with the rubbertiresand attach them to the DC Gearbox with the 2 of the 2mm self tapping screws provided in the kit 2 Install both the DC Gearboxeson the circularbase plate at the specific positions shown in the picture with 4 of 3 x 6mm machine screws A BP motor attached position 3 Insert the 3 x 10mm machine screws through the hole at the comer of the Box holder with 25mm and 2 of 3 mm spacers 25mm metal spacer Hole position for 2 of 3mm plastic spacer attached spacers 4 Place the Box holder from step 3 on the top of the Circle base plate and attach them with 3 x 10mm screws at the specific positions Hole position for attached Hol
97. same button again the small cap characterwill be sent the second time If the userpressescontinually the lastcharacterwillbe sent repeatedly 11 2 ZX IRM Infrared Receiver module In transmitting the data modulated with infrared light forlong distance isabout 5 to 10 meters similar TV remote control The camer frequency is 38kHz Thus the receiver must demodulate 38kHz camer frequency After this transfer seral data to microcontroller If the sensordoesnot detect the 38kHz frequency with the infrared light the output will be logic 1 Otherwise if it detects the 38kHz frequency the output logic is 0 23 B20 Infrared Receiver Figure 11 2 Shows the photo of 38kHz Infrared Receiver module pin assignment and schematic diagram MicroCamp Almega8 Activity Kit Manual 111 Activity 12 Installation 38kHz Infrared receiver module Part list 3x10mm screw and Obtuse joiner x 1 3mm nut x 2 Angled joinerx 1 ZX IRM 38kHz Infrared Receiver module x 1 A12 1 Insert a 3x10mm screw through the Z IRM and the Obtuse joiner Tighten with 3mm nut A12 2 Fix the Angled joiner at the front of the MicroCamp robot opposte sde of SLCD installed with 3x10mm screw However the postion could be change to the best location for receiveing the infrared light from ER 4 remote control A12 3 Insert the ZX IRM structure from step A12 1 with the end of the right angle joiner of step A12 2 Con
98. sotre in adc_val 6 4 LED blinking function in led h library The MicroCamp board provides 2 LEDs at PC5 LED1 and PD1 LED2 pins The LED blink operation is a very simple method which sends logic 0 and 1 toggle always However developers can use a function to allow this operation to run concurently with other functions with using the LED blinking function in led h library led1_on enable LED1 PC5 blinking led1 off disable LED1 PC5 blinking led2_on enable LED2 PD1 blinking led2_ off disable LED2 PD1 blinking Example 6 9 void main led1_on LED1 still blink although the Main program execute finished 58 MicroCamp ATmega8 Activity Kit Manual 6 5 motorh Motor contol library 6 5 1 motor function This function is used for controlling the DC motor driver circuit on the MicroCamp controller board Function format void motor char _channel int _ power Parameter _channel select motoroutput channel On MicroCamp control board has 2 channels 1 and 2 _ power detemine the power apply for motor output Range is 100 to 100 If value is positive 1 to 100 the motor will spin in a direction lf value is negative 1 to 100 the motorspin the otherdirection If the value is 0 motor will stop but this do not lock the motors shaft Example 6 10 motor 1 60 Drive motor channel 1 with 60 of power eee it 60 i _ Drive motor channel 1 with 60 of power in the opp
99. st when the program executes Other sub C programs functions can be linked to this main function Therefore function capability is a vital component in C programming 4 1 Function declaration It has general format return type function_name parameterl parameter2 command list 1 command list n thus function name is the name of function return type is the type of the data resulting from each function Within this function the command return value is used for sending the result data The target vanable that the retum value will be applied on must be the same aseach other to avoid any variable mismatch Any function without a retum value void parameter at the return_type must be parameter isa part of data or variable that relate with function Some functions require many parameters while some functions have none If no parameters are required a void can be declared Some function need many parameter but some functrion not In function that have not any parameter can ignore or declare to void command list 1 command list nisa command within this function At the end of each command a semi colon symbol is required to close and separate the commands 34 MicroCamp Almega 8 Activity Kit Manual 4 2 How to using the function All functions in the C program that are declared can be called in the main function and other functions as well In the process of calling a function developers are required to specify
100. support files which allows the user to develop their C language program more comfortably An example isthe Port control library forc ontrolling both Digital and Analog Input Output Motor control instructions etc 4 Programmer software This software is used to download the compiled HEX file to the AVR Microcontroller Included in this kit is the AVRProg It is Atmel s software and an add in feature in AVR Studio AVR Prog software works with the PX 400 Serial port In system programmer box The PX 400 programmer is bundled in the MicroCamp Activity kit 16 MicroCamp ATmega8 Activity Kit Manual 2 1 Installation AVR Studio Installation of AVR Studio in Windows XP 2 1 1 Insert the MicroCamp CD ROM and look for this file in the AVR Studio directory aStudio4b460 exe Double click this file amp software a00 File Edit View Favorites Tools Help ay Qw B J eah E Folders E Address E AVR_project Example_avr_ipst software Bo Size Type _ Date Modified File and Folder Tasks 5 ii 16 516KB Application ATASA ta Rename this fie winave 20050214 install exe 13 721 KB Application 1 12 2548 16 29 B Move this file IP Copy this file 2 1 2 Enter Installation Wizard Click on the Next button to continue AVRStudio4 InstallShield Wizard 2 1 3 In the license agreement window Select the box I accept the tems of the license agreement and Click on the Next button AYRStudio4 InstallShield Wizar
101. t Declare start vairable as float data type int x y Declare 2 variables x and y Data typesare int float p q T Declare 3 variables p q and r Data typesare float In addition programmers can declare the varablesand set the initial value such as int 100 Declare x variable Data type is an integer and the initial value is 100 int x 15 y 78 Declare x andy variable Data type is an integer and the initial value are x 15 and y 78 long p 47L q 31L Declare p and q variable Data type is long and initial value are p 47 and q 31 4 7 Data type conversion The general form of the conversion is type variable Thus type is The result data type that is required variable is the variable that is required to convert the data type Example 4 11 int x 100 float y 43 67 2Z z y float x Example 4 12 int a 50 long b 23L c c b long a Declare x variable as integer type and set its initial value to 100 Declare y and z variable as float types and set y 43 67 Set the value of z to be the addtion of y and x x data is originally int It needs to be converted to a float with float x command The result of z 143 67 Declare a variable as an integer type and set its initial value to 50 Declare b and c variable as long data type and set b to 23 Set the value of c to be the multiplication between b and a a data is originally i
102. t gt Programs gt Atmel AVR Tools gt AVR Studio 4 The main window of the AVR Studio program will appear AVR Studio File Project Build View Tools Debug Help De HEGLBRESLSRWAAZAKRESE PHI C OPC OR BeBSe Trace Disabled gt O GF oF 2 GR YS Se Te YO View x _Name Value Bits Kalyo view inro Message Loaded plugin STK500 E ih J gt 3 Build message ZAFind in Files Mi ereakpoints and Tracepoints Oo CAP NUM SCRL MicroCamp ATmega8 Activity Kit Manual 19 2 2 Instalaltion of WinAVR Please note that installation of WinAVR is done after the installation of AVR Studio Please ensure this is being done before proceeding Installation of WinAVR in Windows XP 2 2 1 Insert the MicroCamp CD ROM and find the installation file of WinAVR WinAVR 20050214 install exe Double click this file software Scie File Edit yew Favortes Tools Help T Q gt 8 J ran roes i yE Address E E AVR_project Example_avr jpstisoftware Name a Sie Type Date Modified File and Folder Tasks Bolastidingh4en exe 46 516 KR Application 17 11 2548 11 45 D 13 721 K6 Applicaton 1fl2jzo4e 16 29 Date Created 23 2 2549 11 16 Sge 13 3 MB S E mail this the 2 2 2 Installation language dialog box will appear for selection the language of this installation Seect your prefered language from the sliding bar After that click on the OK button Installer Lang
103. tal input port sleep h Delay function library analog h Analog input reading library Assist in reading of analog data from PO to P4 port led h LED control library motorh DC motor control library sound h Sound generator library timer h Timer function library All libraries must be stored in the same folder for proper linking of paths and to avoid any enors Leamers can see details of all libraries from the MicroCamp_include folder in CD ROM which is bundled with the MicroCamp Activity kit 54 MicroCamp ATmega8 Activity Kit Manual 6 1 Command in in_outh library 6 1 1 Digital input port reading function in_b Port B input reading function in _c PortC input reading function in_d Port D input reading function Function format char ina char in b x char in c char in d x Parameter x determines the number of the input port that will be used The value isO to 7 Retum value o or el h Example 6 1 char x 0 Declare x to store the result x in b 2 Get PB2 value to store in x Example 6 2 char x 0 Declare x to store the result x in d 4 Get PD4 value to store in x Example 6 3 include lt avr io h gt Includes the Standard input output port library include lt in_out h gt Includes the Port control library include lt sound h gt Includes the Sound generator library void main while 1 if in d 2 0 Check SW1 pressed sound 3000
104. terrupt Sources e 5Sleep Modes Idle ADC Noise Reduction Power save Power down and Standby e Operating Voltage 4 5 5 5V e Speed Grades 0 to 16 MHz 1 4 2 Block diagram of ATmega8 Figure 1 5 shows the ATmega8 microcontroller block diagram The AVR core combines a rs instruction set with 32 general purpose working registers The ATmega8 provides the following features 8K bytes of In System Programmable Flash with Read While Write capabilities 512 bytes of EEPROM 1K byte of SRAM 23 general purpose I O lines 32 general purpose working registers three flexible Timer Counters with compare modes intemal and extemal interupts a ral programmable USART a byte oriented Two wire Serial Interface a 6 channel ADC with 10 bit accuracy a programmable Watchdog Timer with Intemal Oscillator an SPI serial port and five software selectable power saving modes The Idle mode stops the CPU while allowing the SRAM Timer Counters SPI port and interrupt system to continue functioning 1 4 3 ATmega8 pin function Table 1 1 is ummary information about ATmega8 pin function MicroCamp ATmega8 Activity Kit Manual 11 A ZF External RESET H oscillator circuit Port C PC6 PC0 Port B PB7 PB AAAAAA Vcc A RY YVVVVVY Port C drivers buffers Port B drivers buffers Port
105. tion 8 1 1 Features Serial Input RS 232 or Invert Non invert TL C MOS logic level 1 8 or 1 16 Duty can be selected by jumper Scott Edwards s LCD Serial Backpack command compatible addition with Extended Command that make LCD control easier Easy to interface with the microcontroller Operation with 5 to 12 Vdc supply 8 1 2 Setting up In the figure 8 1 it shows the detail of SLCD16x2 backside User will see 4 jumpers to configuration as follows 1 Mode command jumper Select the command modes SLCD16x2 has 2 modes One is Standard command ST This mode compatible with Scott Edwards s LCD Serial Backpack Another mode is Extended mode command EX For MicroCamp activities select Standard command mode ST 2 Lines jumper Select the line displays 1 8 and 1 16 Duty 1 8 Duty means displaying 8 digit per line 1 16 Duty means displaying 16 digit per line or more Normally set to 1 16 76 MicroCamp ATmega8 Activity Kit Manual Pibg SS REPR RARO FUAGAATEO La sal Wal Ww IEC TOR R7 s IRD f Ega Jmummnnmunnm Screen brightness adjustment TON ETI66 Y j N94 m Ground Data Signalinput 100 innovative Experiment 5V supply Command mode I Extended EX Line display Baudrate Interface signal I 9600 bps 96 I w nae chee a 8 digit per line 8 a i Standard ST a a P 2400 bps 24 i Direct logic DI
106. to battery holder at the back of MicroCamp board The MicroCamp robot is ready for programming now MicroCamp ATmega 8 Activity Kit Manual 67 Leaming about the Switch circuit The switch that is used with the MicroCamp has the following schematic Signal connector Indicator Switch Pressing the switch results in two occurrences When the switch is not pressed let the results be logic 1 When the switch is pressed let the results be logic 0 and LED1 light up Since the switch can give two results it is considered to be a digital input component More information of Infrared Reflector Signal connector Infrared Reflector sensor The heart of this sensor circuit is the sensor that detects reflections from infrared light It consists of the Infrared LED which emits infrared light to the surface Photo transistors will then receive the reflected infrared lights If no infrared light is received the OUT terminal will have low voltage when measured In the case that it receives infrared light whether low or high current passes through the photo transistor depends on the intensity of the light received which in turn varies according to the distance of the reflection Sensor TCRT5000 can be used at a distance of 0 1 1 5 centimeters Therefore 0 5 5V can be measured at the OUT terminal and the MicroCamp will get a value of 30 to 1023 68 MicroCamp ATmega 8 Activity Kit Manual Activity 1 Basic
107. uage D Please select a language English v 2 2 3 The Welcome installation software window appearsand show the instalaltion infomation Click on the Next button E WinAVR 20050214 Setup la Welcome to the WinAVR 20050214 Setup Wizard This wizard will guide you through the installation of WinAYR 20050214 It is recommended that you close all other applications before starting Setup This will make it possible to update relevant system Files without having to reboot your computer Click Next to continue 20 MicroCamp ATmega 8 Activity Kit Manual 2 2 4 In the License agreement window Click on the I agree button amp WinAVR 20050214 Setup License Agreement Please review the license terms before installing WinAYR 20050214 Press Page Down to see the rest of the agreement WinAYR Licensing Information GNU Binutils is distributed under the GNU GPL License GNU Compiler Collection GCC is distributed under the GNU GPL License avr libe is distributed under a BSD License avrdude is distributed under the GNU GPL License lavrdude qui is distributed under the GNU GPL License uisp is distributed under the GNU GPL License GNU Debugger GDB is distributed under the GNU GPL License Insight is distributed under the GNU GPL License If you accept the terms of the agreement click I Agree to continue You must accept the agreement to install WinAYR 20050214 2 2 5 Ch
108. ual 4 5 Numencal system in C program of WinAVR WinAVR compiler has 3 types of numerical system in C program 1 Decimal number 2 Binary number The format is Oo BBBBBBBB Thus B is O or 1 3 Hexadecimal number The format is OXHHHHHHHH Thus H is O to 9 A to F Example 4 7 The 8 bit binary number 0610010010 is equal to 146 in decimal number The calculation 1x27 0x28 0x2 1x24 0x23 0x22 1x2 Ox2 146 Example 4 8 The 16 bit binary number 0b1111010011101101 is equal to 62701 in decimal number The calculation 1x24 1x214 1x29 1x2 Ox2 1x2 0x2 0x28 1x27 1x26 1x25 0x24 1x23 1x2 0x2 1x2 62701 Example 4 9 The hexadecimal number OxFF is equal to 255 in decimal number The calculation 15x16 15 16 255 and OxFF gt 0b11111111 in binary number Example 4 10 The hexadecimal number 0x31 is equal to 49 in decimal number The calculation 3x16 1x16 49 and 0x31 gt 0b00111111 in binary number 4 6 Vanable declaration Variable declaration in C program of WinaVR is similar to ANSI C programming The General form is type variable name Thus type is The result data type variable name is variable declared such as MicroCamp ATlmega8 Activity Kit Manual 39 int a Declare a variable as int data type long result Declare result variable as long data type float star
109. ving the DC motor to spin the robot in a left direction Function format void s_left int speed Parameter speed determine the power applied to motor output Range is 0 to 100 6 5 7 s right function This function is used for driving the DC motor to spin the robot in a right direction Function format void s_right int speed Parameter speed determine the power applied to motor output Range isO to 100 60 MicroCamp ATmega 8 Activity Kit Manual 6 6 sound h Sound generator library This func tion is used for setting the sound frequency which drives the piezo speaker on the MicroCamp controller board to produce sounds Function format void sound int freg int time Parameter freq determine the frequency output in Hertz Hz unit time determine the time value of sound output signal in millisecond unit Example 6 13 sound 2000 500 Generate 2kHz signal for 500 millisecond 6 7 Counting time function in tmerh library 6 7 1 tmer_start function Determine the start point of the timer After this function the timer value will be cleared Function format void timer start void 6 7 2 timer_stop function This stops the timer and clears the counting value Function format void timer stop void 6 7 3 tmer_pause function Pause timer counting The value still remains Function format void timer pause void 6 7 4 timer_resume function Resume t
Download Pdf Manuals
Related Search
Related Contents
Samsung XQB55-E86 用户手册 SAARANG MAGIC PLUS DIGITAL TANPURA OPERATING MANUAL Advanced Hand Dryer - Instructions Manual.indd korantin bh 50 User Manual for COMe Eval Type 2 Carrier Board Honeywell Process Instrument Explorer Manual Urgent Field Safety Notice IP Wireless / Wired Camera User Manual (Thailand) Co., Ltd. g-shock. jp Copyright © All rights reserved.
Failed to retrieve file