Top Banner

of 64

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript

SK40C + PIC16F887

STARTER KIT MANUAL BEGINNER GUIDE

ROBOT . HEAD to TOE SK40C + 16F887 GETTING STARTED

SK40C + 16F887 STARTER KIT MANUAL

BEGINNER GUIDE

BY

Page 2

ROBOT . HEAD to TOE SK40C + 16F887 GETTING STARTED

CONTENTSINTRODUCTION COMPONENT REQUIRED WHAT EXACTLY INSIDE SK40C GETTING STARTED MPLAB IDE & HITECH C PRO Installation CREATE PROJECT PICKIT 2 SETUP OPEN PROJECT HARDWARE SETUP I/O PORT PROJECT 0 CONNECTION CODE OVERVIEW PROJECT 1 CONNECTION ADDITIONAL INFORMATION CODE OVERVIEW PROJECT 2 CONNECTION ADDITIONAL INFORMATION CODE OVERVIEW PROJECT 3 CONNECTION ADDITIONAL INFORMATION CODE OVERVIEW PROJECT 4 CONNECTION ADDITIONAL INFORMATION CODE OVERVIEW PROJECT 5 CONNECTION ADDITIONAL INFORMATION CODE OVERVIEW PROJECT 6 CONNECTION ADDITIONAL INFORMATION CODE OVERVIEW 4 5 6 8 8 9 10 11 11 12 13 13 13 17 17 17 18 20 20 20 21 23 23 23 24 26 26 26 27 30 30 31 32 33 33 34 35 PROJECT PROJECT PROJECT PROJECT PROJECT PROJECT PROJECT PROJECT PROJECT 7 CONNECTION ADDITIONAL INFORMATION CODE OVERVIEW 8 CONNECTION ADDITIONAL INFORMATION CODE OVERVIEW 9 CONNECTION ADDITIONAL INFORMATION CODE OVERVIEW 10 CONNECTION ADDITIONAL INFORMATION CODE OVERVIEW 11 CONNECTION ADDITIONAL INFORMATION CODE OVERVIEW 12 CONNECTION ADDITIONAL INFORMATION CODE OVERVIEW 13 CONNECTION ADDITIONAL INFORMATION CODE OVERVIEW 14 CONNECTION ADDITIONAL INFORMATION CODE OVERVIEW 15 CONNECTION ADDITIONAL INFORMATION CODE OVERVIEW 39 39 40 40 42 42 42 43 44 44 45 46 47 47 48 49 51 51 51 53 54 54 55 56 57 57 58 59 60 60 61 61 62 62 63 63

Page 3

ROBOT . HEAD to TOE SK40C + 16F887 GETTING STARTED

INTRODUCTION

HOW TO USE IT This starter kit manual are start off with a simple introduction of SK40C, software installation, electronic component needed and interfacing, hardware, some useful additional information and lastly the C code explanation . We will also describe separately the code and also the hardware connection in some detail and how it works. WHAT YOU WILL NEED

SK40C is another enhanced version of 40 pins PIC microcontroller start up kit. It is designed to offer an easy to start board for PIC MCU user. Users are able to utilize the function of PIC by directly plugging in the I/O components in whatever way that is convenient to user. With UIC00B connector on board, user can start developing projects and have fun with this kit right away. This kit comes WITHOUT PIC microcontroller to provide the freedom for user to choose PIC type. In here , we are going to explain to you the method of using SK40C with PIC16F887 by interfacing with others common use electronic components such as sensor, LED, UART, PS2 controller and etc. There is total of 15 interesting project from Project_0 to Project_15 which have explain the method to use SK40C with PIC16F887 interfacing with electronic component. This will help user on deeply and easily understanding the function of SK40C, microcontroller and also HITECH C programming. SK40C board comes with basic element for user to begin project development. It offer plug and use features.

First, you will need access to internet and go to www.cytron.com.my to download the sample code of 15 interesting project and also any code libraries that is required in the projects. Second, you will need to prepare the electronic component which will be use in the project such as LED, Ultrasonic sensor, UC00A and etc. Then you also need a well lit table or other surface to lay down all your components and this will be next to your computer for enable you to easily upload the code into the SK40C. Always beware that you are working with electricity (although is low voltage) but safety is a MUST to avoid damages on yourself and components Besides that, also of some benefit, although not essential, may be a pair of wire cutters, a pair of long nosed pliers and a wire stripper for making a jumper wires. Lastly, the most important thing is the willingness to learn. Coming project are design as a simple way to help you get involve to the microcontroller electronics and nothing is too hard to learn.

Page 4

ROBOT . HEAD to TOE SK40C + 16F887 GETTING STARTED

COMPONENT REQUIRED

SK40C

PIC16F887

UIC00B

LEDS

RESISTOR

BREADBOARD 16x2 PARALLEL LCD

POTENTIALMETER

LM35 TEMPRETURE SENSOR

ULTRASONIS RANGE FINDER EZ1

PIEZO BUZZER

IR DISTANCE SENSOR

LDR

MD10C

7 SEGMENT

CD4511 7SEGMENT DECODER

USB TO UART CONVERTER

RELAY

JUMPER WIRES

DC POWER SUPPLY

SKPS CONTROLLER STARTER KIT

1N5817 SCHOTTKY DIODE

SONY PS2 CONTROLLER

BB-PSJ ANALOG JOYSTICK BREAKOUT BOARD

Page 5

ROBOT . HEAD to TOE SK40C + 16F887 GETTING STARTED

WHAT EXACTLY INSIDE SK40CBOARD OVERVIEW SK40C, another enhanced version after SK40B which can support for 40 pins PIC such as PIC16F and PIC18F family. This board comes with basic element that mostly use by hobbyist and experts. This kit is design to offer: Industrial grade PCB Every board is being fully tested before shipped to customer Compact, powerful, flexible and robust start-up platform Suitable for hobbyists and experts Save development and soldering time No extra components required for the PIC to function All 33 I/O pins are nicely labeled to avoid missconnection by users Connector for UIC00A (low cost USB ICSP PIC Programmer) - simple and fast method to load program Fully compatible with SK40B No more frustrated work plugging PIC out and back for re-programming Perfectly fit for 40 pins 16F and PIC18F PIC With UIC00A, program can be loaded in less than 5 seconds More convenient to use and it is smaller than SK40B. Maximum current is 0.5A. Dimension: 85mm x 55mm SK40C come with additional features: 2 x Programmable switch 2 x LED indicator Turn pin for crystal. User may use others crystal provided. LCD display (optional) UART communication USB on board. And all the necessities to eliminate users difficulty in using PIC.

LABEL A B C D E F G H

FUNCTION DC Power Adapter Socket USB Connector Toggle Switch for Power Supply Power Indicator LED Connector for UIC00B Programmer LED Indicator Header Pin and Turn Pin UART Connector

LABEL I J K L M N O P

FUNCTION Programmable Push Button Reset Button LCD Contrast JP8 for LCD Backlight JP9 for USB 40 pin IC Socket for PIC MCU Turn Pin for Crystal LCD Display

A DC power adapter socket for user to plug in DC adapter. The input voltage should be range 7 15V. B USB connector for communication between devices and a host controller. This function only valid for certain model of PIC microcontroller. Please refer to SK40C User Manual. The power LED will light ON when USB cable is connected. C Toggle switch to ON/OFF the power supply from DC adapter. D Power indicator LED for on board. It will light ON

Page 6

ROBOT . HEAD to TOE SK40C + 16F887 GETTING STARTED as long as the input power is correctly connected. E 2x5 box header for UIC00A & UIC00B, USB ISCP Programmer. F 2 LEDs (connected to RB6 and RB7) as active High output for PIC MCU. These LEDs are controllable from PIC MCU. G Consist of several line of header pin and turn pin. Header pin provide connector for user to solder SK40C to prototype board and use the I/O of PIC MCU. It is fully compatible between SK40B. Turn pin offer simple way to check voltage with multi-meter probe. 40 pins of PIC MCU excerpt OSC ( connected to crystal) are extended out to there pin. There is an extra pin on top of MCLR which is labeled as Vin, is connected to the input power. H Reserved for UART communication. Tx and Rx pin of SK40C are connected to RC6 and RC7 respectively. Ensure PIC use have the correct UART pin (RC6 and RC7). I 2x Push Button connected to RB0 and RB1 of PIC MCU. This is extra input button for user. It can be programmed as input switch. J Push button with the function of reset for PIC MCU. K 5K of trimmer to set LCD contrast. L JP8 is provided for LCD backlight. LCD Display will have backlight if this pin is shorted. M JP9 is provided for USB. Connect this pin is user use USB port. N 40 pin IC socket for user to plug in any 40 pin PIC MCU(8Bit). It can be either 16F or 18F PIC. Of course the IC package should be PDIP. Please ensure the first pin is at the top side. Inside IC socket, there some electronic component, it include a 20MHz Crystal. O Turn pins is provided for crystal. 20MHz is default crystal provided in SK40C. However, the 20MHz crystal; can be removed and replace with other crystal. Just remove crystal and put other crystal on turn pin without soldering. P Reserved for 2 x 16 LCD Display. User may solder 2 x 16 LCD display at this space if want to use it2 X 16 Parallel LCD Display Connection Pin (Label P)PIN 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 NAME GND VDD CON RB4 GND RB5 RD0 RD1 RD2 RD3 RD4 RD5 RD6 RD7 VDD B/L PIN FUNCTION Ground Positive Supply for LCD Brightness Adjust Select Register, Selector Instruction or Data Register Ground Start Data Read or Write LCD Data Bus Pin LCD Data Bus Pin LCD Data Bus Pin LCD Data Bus Pin LCD Data Bus Pin LCD Data Bus Pin LCD Data Bus Pin LCD Data Bus Pin Backlight Positive input Backlight Negative Input CONNECTION GND 5V Connected to Preset to Adjust Brightness Pin RS of LCD GND Pin E of LCD Pin D0 of LCD Pin D1 of LCD Pin D2 of LCD Pin D3 of LCD Pin D4 of LCD Pin D5 of LCD Pin D6 of LCD Pin D7 of LCD VDD Connected to JP8

Turn Pin For Crystal (Label O)PIN RA6 RA7 NAME OSC2 OSC1 PIN FUNCTION Crystal Crystal CONNECTION Turn Pin (JP7) Turn Pin (JP7)

Push Button Pin (Label I)PIN RB0 RB1 NAME SW1 SW2 PIN FUNCTION Digital Input Digital Input CONNECTION SW1 Switch SW2 Switch

UART PIN (Label H)PIN RC6 RC7 NAME TX RX PINFUNCTION Transmit Data Receive Data CONNECTION TX Pin of UART RX Pin of UART

Page 7

ROBOT . HEAD to TOE SK40C + 16F887 GETTING STARTED

GETTING STARTEDIn this SK40C tutorial, we are going to use MPLAB with HI-TECH C compiler to compile and program to the PIC16F887. MPLAB can be supported to Window, MAC OS and LINUX. In here we are presume that you are using Window. Download the MPLAB IDE from download page. MPLAB IDE & HITECH C Installation Please install MPLAB IDE and HI-TECH PICC Lite according to the following steps. 1. Please double click on the Setup to install MPLAB V8.70. 2. The Install Shield Wizard will pop out.

6. In the Choose Destination Location, user is recommended to use the default destination folder, which is C:\Program Files\Microchip\ or user is also free to select the other destination folder by using the Browse button to browse to the desired destination folder. After that, click Next> for next step.

3. After all the directions shown in the welcome note (as in following diagram) is done, please click Next> for next step. 4. Please select I accept the terms of the license agreement if it is not selected in the diagram below, and click Next> after this. 5. Please select Complete for the setup type to install all program features, and click Next> to next step. 7. Please select I accept the terms of the license agreement if it is not selected in the diagram below, and click Next> after this. 8. Please select I accept the terms of the

Page 8

ROBOT . HEAD to TOE SK40C + 16F887 GETTING STARTED license agreement again for the window shown below, and click Next> after this. 9. Please click Next> for the diagram below. 10. Please wait for awhile for the installation process shown in diagram below. 11. A window for Question will pop out, please click YES to proceed with installation of HITECH C. 15. Please select Add to environment path if it is not selected in the diagram below, and click Next to proceed.

16. Please wait for the installation process to be completed. 17. Please click Finish after successfully installed the HI-TECH C. 12. Please click Next to Install HI-TECH C 13. Please select I accept the terms of the license agreement if it is not selected in the diagram below, and click Next to Install HITECH C. 18. The installation of MPLAB V8.70a is completed. Please click Finish to exit. User may need to restart the computer just after the installation or restart later. CREATE PROJECT After finished install the MPLAB IDE, we are going to show you how to create a project using it. There is just a few simple step to follow on how to create it. Please follow the step below: 1. Go to to MENU BAR and click Project > Project Wizard.

14. Please click Next> to proceed.

Page 9

ROBOT . HEAD to TOE SK40C + 16F887 GETTING STARTED PICKIT 2 SETUP In here, we want our compiler to automatically send the .HEX file into our PIC MCU after compiler. There will be just only 4 step to do it. Please follow the instruction below: 1. Go to MENU BAR. Programmer> Select Programmer> PICkit 2.

2. Again, go to Programmer> Setting.

2. Click Next> . On the Device menu, choose PIC16F887 because we are going to use this PIC MCU for coming project. 3. Choose HI-TECH Universal Toolsuite in Active Toolsuite. A HI-TECH ANSI C Compiler will show up in the Toolsuite Content.

3. For UIC00B, please tick the box below.

4. After that, choose a destination where the project you create to be save as. And also the project name. After that, click NEXT>. 5. In step 4, there are no existing file to be added so we can skip this step by just click NEXT>. After that, a project are created with the project name you added earlier.

Page 10

ROBOT . HEAD to TOE SK40C + 16F887 GETTING STARTED OPEN PROJECT

There are only few simple step to open an project. 1st Open the project file. Then, doubleclick the project file with MPLAB logo. Example like figure below.

HARDWARE SETUP

C LANGUAGE KEYWORD Below are the keyword using in standard ANSI C language. auto break bit char continue default double enum float goto int register short sizeof struct typedef unsigned volatile case const define do else extern for if long return signed static swtich union void while

To make the SK40C function, put the PIC16F887 into the IC Socket that have been provided. Make sure the PIC MCU is correctly installed in the SK40C board. The PIN 1 is just next to the little half moon shape or circle.

Next, connect the A-type USB to the computer using like picture below.

Page 11

ROBOT . HEAD to TOE SK40C + 16F887 GETTING STARTED I/O PORT I/O of microcontroller can be access through few method such as breadboard, extend jumper wire like picture below.

After that, plug in the USB(mini) to the UIC00B. The power supply indication green LED will ON. Extend I/O to another board using jumper wire.

Lastly, connect one side of programming cable to box header of UIC00B and the other side to box header of SK40C(Target device) to be program.

Plug the I/O of the Sk40C into breadboard. Then, access I/O pin through breadboard.

Page 12

ROBOT . HEAD to TOE SK40C + 16F887 GETTING STARTED

PROJECT 0My 1st PIC PROJECT, LOAD A SIMPLE LED BLINKIn this project, we are going to do a simple LEDs blinking using the LEDs on SK40C. In here, we are using both of the LEDs and blink it like a police car. Besides on learning electronic parts, you will also learn about some coding in C using HI-TECH C in MPLAB. COMPONENT NEEDED CODE OVERVIEW Configuration for PIC16F887 Figure below shows the Configuration for PIC16F887. The code above can be found at the Configuration bits (CONFIG1) in PIC16F887 data sheet.

HS WDTDIS LEDs ON SK40C CONNECTION For this project, the LEDs was already connected to the pins RB6(PORTB.6) and RB7(PORTB.7). So, you may only have to configure the I/O pins as an output in programming part. To program the PIC16F887, connect the UIC00B to the ICSP Programmer. Make sure the power connection is correct and ON for SK40C. Open MPLAB and include the Project_0.c and click to compile and build .HEX file. After Build Successful, the PICKit2 should automatically run and program the .HEX file into PIC16F887. Finally, the LED1 and LED2 will be blink like the police car. BORDIS MCLREN LVPDIS

External Crystal in High Speed Watchdog Timer Disable Brown Out Reset Disable MCLR Function Enabled Low Voltage Programming Disable

PWRTEN Power Up Timer Enable

Page 13

ROBOT . HEAD to TOE SK40C + 16F887 GETTING STARTED system.h #define RX RC7 User are not required again to define the serial comunication (UART) pin for SK40C. User only required to include the system.h file. Further detail on UART please refer to Project_13. void delay_ms(unsigned int ui_value); Function prototype is the declaration of a function that omits the function body but does specify the function's name, arity, argument types and return type. The symbol ; should put at the end of this line. MAIN PROGRAM #define _XTAL_FREQ 20000000 Besides that, same step by #define the crystal frequency(_XTAL_FREQ) according to your external crystal frequency using. In here, the default external crystal frequency using are 20000000 (20MHz). #define SW1 RB0 In here, we have already define the switch on SK40C board as RB0. User are no need to define again in the main coding. #define LED1 RB6 In figure above, using the code #define, we can replace the RB6 and RB7 as LED1 and LED2. By doing this, we can easily remember and the I/O port we are using. #define LCD_E RB5 #define LCD_RS RB4 #define LCD DATA PORTD Besides that, user are not required again to define the LCD pin for SK40C. User only required to include the system.h file. Further detail on LCD please refer to Project_2. #define TX RC6 void main (void) In figure above is the main program. 1st we have to type void main(void) at the 1st line to tell the microcontroller that this is the starting point of the program. Here, void main, tell the compiler the name of the functin which is main and it don't return any data(void). PORTA = 0; Clear the PORT to prevent from the data latch on the PIC MCU. TRISA = 0b00000000; is the Tri-STATE Register that declare the I/O ports as

Page 14

ROBOT . HEAD to TOE SK40C + 16F887 GETTING STARTED an INPUT or OUTPUT by (1=INPUT) and (0=OUTPUT). E.g. TRISB = 0b00001111 is to set the PORTB to OUTPUT and PORT as INPUT. LED1 = 0; Then, we make the LED1 off by giving it a 0 at initial state as show above. So as the LED2 too. To make it on, just change the 0 to 1. Comments are essential in the code to help you to better understanding on what going on and how the code works. Comments can be put after the command as in the next line of the program. On the other hand, you can also put comments into the block statement by using /* and */. E.g.: /* All the code within the slash and asterisks will be ignore by compiler */

while(1) To make the program loop forever, we use while(1) function. while construct consists of a block of code and a condition. The condition is evaluated, and if the condition is true, the code within the block is executed. In here, we put (1) to indicated that the condition is true. delay_ms(100); is a function call is an expression containing a simple type name and a parenthesized argument list. The argument list can contain any number of expression separated by commas. It can also be empty. In here, we put the arguments list as 100 which is 100ms.

void delay_ms(unsigned int ui_value) is an function that will be call and perform in the main function. void delay_ms, here we are telling the compiler that out function is name delay_ms and it don't return any data (void). (unsigned int ui_value) is the parameter use to insert value depending to our requirement. int mean integer which the minimum allow range are between -32767 to +32767(2 Bytes). unsigned int mean that the data we can insert is between 0 to 65535. There are many data type that we can assign and use that act as the temporary space for micro-controller to store the data. while(ui_value-- > 0) in this condition, if the ui_value is subtract by one at the time and check if the ui_value is't still greater then 0. The code within the while loop will be running for many times until the ui_value is less then 0. __delay_ms(1); This is the macro from HI-TECH complier which will generate 1ms delay base on value of _XTAL_FREQ.

Comment line // In the code, we can found the // on the right hand side of the code. The // is simply a comment in the code and is ignore by the compiler. Any code that behind the // is ignore by compiler and is just simply there for you, or everyone to reads the code.

Page 15

ROBOT . HEAD to TOE SK40C + 16F887 GETTING STARTED

PROJECT 1LED BLINKING: LED CHASERIn project 1, you are about to experience how to program a LED chaser which can be increase the running speed by just a single button pushing. While releasing it, the running speed will slowly back to normal. In here, we are going to use total of 6 LEDs and will connected with a current limiting resistor before plug into the I/O port. COMPONENT NEEDED 6x 1K RESISTOR connected to the PORTA.0 until PORTA.5. Besides that, we are going to use the switch 1 (SW1) on SK40C as our running speed controller. The 1K Resistor are function to limit the current going through it to the safe value. So that it can protect the LEDs from burn out.

BREADBAORD

JUMPER WIRES ADDITIONAL INFORMATION LIGHT EMITTING DIODE LEDs x6

CONNECTION Refer to the figure below and connect the anode (+ve) of all LEDs to 1K Resistor and end of resistor is connected to the VDD (Power Supply). On the other hand, the cathode(-ve) of all LEDs are directly

Page 16

ROBOT . HEAD to TOE SK40C + 16F887 GETTING STARTED There are few method to determine the anode and cathode side of the LED such as : 1. The flat spot on the lens/case of the LED is cathode. 2. The short lead (or leg) is cathode. 3. The flag symbol inside the lens is cathode. For further information about LED, please refer to this webpage. RESISTOR COLOUR CODE Figure above have shows that the connection of half of the breadboard. The RED and BLACK colour line are usually been connect to power supply (VDD) and ground (GND). They are connected all the way from the beginning to the end but they did not connect to each other. Besides that, the orange colour line are the part that we usually use to place the electronic component. They are connected in a straight line. There is a gap in the middle of the breadboard which are not connected to anything. This allow you to put integrated circuit across the gap and have each pin of the chip go to the different set of holes and therefore a different rial.

Above are the example of resistor with the colour code example. For more detail, refer to this webpage. BREADBOARD OVERVIEW

CODE OVERVIEW

static unsigned int time = 1000; As we can see that there is a static in front of the unsigned int. Static is a keyword which are commonly use in C programming to represent some value. Besides that, the value in static can be modified or replace. In here, we default the time

Page 17

ROBOT . HEAD to TOE SK40C + 16F887 GETTING STARTED value as 1000ms. For further information, please refer this wedpage. if (SW1 == 0) If switch 1 is press, then do the subtraction of the time. if not then go to do the addition of the time. time = time 100; In this case, it means that the default time 1000 is subtract by 100ms at each time the compiler come back to this subroutine. If (time < 50) { time = 100 } In this case, the time is check again whether the time is lesser ( 3) { mode = 1; } Check if mode is increase until 3 or more. If yes then reset back to mode 1.

References: http://www.mikroe.com/eng/chapters/view/7/chapt er-6-serial-communication-modules/

Page 37

ROBOT . HEAD to TOE SK40C + 16F887 GETTING STARTED

PROJECT 7ANALOG SENSOR: RANGE USING ANALOG DISTANCE SENSORCONNECTION Analog Distance Sensor Analog distance sensor have 3 pin which is VDD, GND and Vout. Analog distance sensor which have a function that same as the Ultrasonic sensor which is use to measure range. But for this sensor, the output value are in centimeter(cm) and also the output are only given an analog value. To get the result, an mathematics formula are required to apply into our C-code for calculation. COMPONENT NEEDED 16 x 2 LCD Display 1 Vout 2 GND 3 VCC (5V) *User must refer to analog distance sensor datasheet before connect power to sensor. Cable's color are not standard. By referring to the figure below, the YELLOW colour wire are connected to RA0, RED wire are VDD and BLACK wire are GND.

ANALOG DISTANCE SENSOR SN - GP2Y0A21

BREADBAORD

JUMPER WIRES

Page 38

ROBOT . HEAD to TOE SK40C + 16F887 GETTING STARTED

ADDITIONAL INFORMATION The linear equation is y = 20.99x + 0.19, where y is voltage output and x invert distance. Notice that the trusted output voltage are about 0.4 to 2.8 V. CODE OVERVIEW

Referring to the chart above are the voltage(V) versus distances(cm) of the distance sensor. By plotting the output versus inverse number distance (1/cm), we the linear relation such as in figure below.

output analog voltage can get

Vout = (adc_value*500000)/1024; After take the ADC output value for 100 times. The adc_value are time with 500000 and divided by 1024 to eliminate the DOT and get the bigger value. Besides that, we also get the value after the DOT. E.g : adc_value = 1022, ~ if using adc_value*5/1024, Vout = 4 ~ if using adc_value* 500000/1024 Vout = 499023

Page 39

ROBOT . HEAD to TOE SK40C + 16F887 GETTING STARTED

if ((Vout > 43945) && (Vout < 279785)) Check if Vout are between 0.4V to 2.8V. If the value is more then less then 0.4 and more then 2.8 are not valid and LCD will display OUT OF RANGE. See example below to get the Vout. E.g-1 : If using Vout = adc_value*5/1024 Vout = 2.8V 2.8V*1024 / 5 = 573. (convert to Decimal) 573*5 / 1024 = 2 (Convert to Volt) E.g-2 : If using Vout = adc_value*500000/1024 Vout = 2.8V 2.8V*1024 / 5 = 573. (convert to Decimal) 573*500000/1024 = 279785. (Convert to Volt) range_an = (Vout 19000)/2099; Refer example below on how to get the range in centimeter(cm). E.g-1 : if Vout = 279785 279785 19000 = 260785 260785 / 2099 = 124 1000 / 124 = 8

References:

http://mcuprogramming.blogspot.com/2006/09/distancemeasurement-sensor.html

Page 40

ROBOT . HEAD to TOE SK40C + 16F887 GETTING STARTED

PROJECT 8PIEZO BUZZER: MELODY BIRTHDAY SONGPiezo buzzer, generally use as a sensor and actuator which are use to measure force, pressure , tools for positioning object and etc. As for this project, we are going to use it to generate a melody by applying PWM frequency to it. COMPONENT NEEDED ADDITIONAL INFORMATION INSIDE OF PIEZO BUZZER

PIEZO BUZZER HOW PIEZO BUZZER WORKS CONNECTION For piezo buzzer, connect any of the wire to ground(GND) and the other wire to RC1.

RECOMMENDED OPERATING CIRCUIT

Page 41

ROBOT . HEAD to TOE SK40C + 16F887 GETTING STARTED CODE OVERVIEW system.h E.g: delay_period for C4_1 = (1) * 20000 = 20000

while(delay-- > 0) Loop forever if the 30000 is decrease by 1 at the time is still greater then 0. pwm_set_duty_cycle(tone); Set the duty cycle of the PWM with birthday[] value.

#define A4 78 In system.h, we firstly declare each delay period value according to the musical tone chart. MAIN PROGRAM

unsigned int birthday[] Assign an array for birthday[] so that it can increase 1 at the time by just simply give bithday[] a number. E.g: birthday [4], the delay period are N which is 132. unsigned int delay_pariod[] Assign the delay period for each tone. The delay_period much be within 0 to 65535. PR2 = (birthday[wait]/2); Assign timer 2 value (PR2) with the delay period over 2 to generate the PWM period. tone_out(birthday[wait],delay_period[wait]*20000); Call tone_out subroutine and also assign the birthday[wait] value and delay_period times with 20000.

References: 1. http://www.tdk.co.jp/tefe02/ef532_ps.pdf 2. http://www.buzzerspeaker.com/manufacturer/introduction %20of%20piezo%20buzzer.htm

Page 42

ROBOT . HEAD to TOE SK40C + 16F887 GETTING STARTED

PROJECT 9ANALOG SENSOR: LIGHT DETECTION USING LDRA photoresistor or light dependent resistor (LDR) is a resistor whose resistance decreases with increasing incident light intensity. It can also be referred to as a photoconductor. In here, we going to do a simple connection of LDR and display the ADC and Voltage value on LCD. COMPONENT NEEDED 16 x 2 LCD Display CONNECTION

There are 2 way of constructing the voltage divider which is LDR on top or LDR on bottom. LDR ON TOP Refer to the figure below, there are total 3 wires using. RED wire connect between LDR and VDD. Black wire connect between resistor and GND. Lastly, YELLOW wire are connect between RA0 and between LCD and resistor. Please refer this webpage for further detail.

LIGHT DEPENDENT RESISTOR

1K RESISTOR

BREADBOARD

JUMPER WIRES

Page 43

ROBOT . HEAD to TOE SK40C + 16F887 GETTING STARTED MEASURING RESISTANCE OF LDR To measure the LDR resistance, just probe directly +ve to one side of LDR and -ve to the other side of LDR.

LDR VOUT MEASUREMENT LDR ON BOTTOM Refer to the figure below, there are total 3 wires using. BLACK wire connect between LDR and VDD. RED wire connect between resistor and GND. Lastly, YELLOW wire are connect between RA0 and between LCD and resistor. Please refer this webpage for further detail.

In this case, we give the Vin as 5V and resistor are 1K. E.g-1: LDR on Top, Rtop = 10K Vout = 1K x 5V 1K + 10K = 0.45V E.g-2: LDR on bottom, Rbottom= 10K Vout = 10K x 5V 10K + 1K = 4.54V

ADDITIONAL INFORMATION

Page 44

ROBOT . HEAD to TOE SK40C + 16F887 GETTING STARTED CODE OVERVIEW

for (i=0 ; i 14V. VIN : Board is powered by VIN. 12V must be supplied to the VIN pin and motor power input can be 3V to 25V. SPG10 - METAL GEARMOTOR There are 3 types of metal gear motor which come with the difference gear ratio, stall torque and free run RPM. Please refer to SPG10 datasheet for detail. SIGN-MAGNITUDE PWM MODE

DIR = 1; Set the motor spinning direction. HIGH(1) to CW, LOW(0) to CCW.(Depend on motor connection) for (speed = 350;speed0;speed--) Decrease the speed by 1 at the time started from 1024 until 0. SWITCH 1 PRESS CHECK T2CKPS1 = 0; T2CKPS1 = 1; Set timer 2 prescaler to 1:4. TMR2ON = 1; HIGH(1) to Timer 2 ON, LOW(0) to Timer 2 OFF CCP2M3 = 1; CCP2M2 = 1; CCP2M1 = 0; CCP2M0 = 0; Configure the CCP2 module as PWM mode. CCPR2L = 0x00; Initialize the PWM duty cycle at 0 at the start time. PWM DUTY CYCLE

Check if switch 1 is press by continuously loop for 200 times.

CCP2CON These bits are the two LSB of the PWM duty cycle. CCPR2L = ui_duty_cycle >> 2; These are the 8 MSB of the PWM duty cycle.

Page 49

ROBOT . HEAD to TOE SK40C + 16F887 GETTING STARTED

PROJECT 117 SEGMENT DISPLAY WITH DIRECT I/ONowadays, 7 segment has become the most popular display unit on market because it is cheap, easy to program and small. On market, we can found that there was 2 types of 7 segment which is common anode and common cathode. In this project, we are going to use 7 segment with common cathode and directly interface to microcontroller. COMPONENT NEEDED CONNECTION From figure below, we can see that the 1st 7 segment are connected to PORTC.0 to PORTC.6, while 2nd 7 segment are connected to PORTD.0 to PORTD.6. Both of the 1K resistor are connected to ground(GND).

7 SEGMENT DISPLAY x2 (COMMON CATHODE)

BREADBOARD

ADDITIONAL INFORMATION 7 segment have separated to 2 types which is common cathode and common anode which show in figure below.

1K RESISTOR x2

JUMPER WIRES

Page 50

ROBOT . HEAD to TOE SK40C + 16F887 GETTING STARTED COMMON CATHODE Common cathode means that all the ground (GND) are all connected together. To ON the LED inside, we have to give a signal HIGH(1) or +5V to the input port of 7 segment. COMMON ANODE Common anode means that all the voltage in (VCC) are connected together. To turn ON the LED inside, a signal LOW(0) or 0V are required to the 7 segment input port. 1K RESISTOR By refering to the circuit. We have connected 1K resistor to each 7 segment to limit the current flowing through it so that it may prevent the 7 segment from spoil. 7 SEGMENT INPUT TO PIC MCU RC7/ RC6/ RC5/ RC4/ RC3/ RC2/ RC1/ RC0/ RD7 RD6 RD5 RD4 RD3 RD2 RD1 RD0dp 0 0 0 0 0 0 0 0 0 0 G 0 0 1 1 1 1 1 0 1 1 F 1 0 0 0 1 1 1 0 1 1 E 1 0 1 0 0 0 1 0 1 0 D 1 0 1 1 0 1 1 0 1 0 C 1 1 0 1 1 1 1 1 1 1 B 1 1 1 1 1 0 0 1 1 1 A 1 0 1 1 0 1 1 1 1 1 '0' '1' '2' '3' '4' '5' '6' '7' '8' '9'

Page 51

ROBOT . HEAD to TOE SK40C + 16F887 GETTING STARTED CODE OVERVIEW MAIN PROGRAM

unsigned int dis1[] For this part, we use ARRAY to continuously display the 7 segment character from 0 to 9 and also from 9 to 0. SEG1_dis = dis1[num]; 7 segment 1 will continuously display the number from dis1[] once the num are increase 1 at the time. system.h

References: 1. http://en.wikipedia.org/wiki/Sevensegment_display 2. http://www.see.ed.ac.uk/~gaa/DigilabNotes /Digilab/Components/node7.html

#define dis_1 0b00000110 In system.h, we have already declare the output display of 7 segment. #define SEG1_dis PORTC In here, we have selected the output port that the data will be send through.

Page 52

ROBOT . HEAD to TOE SK40C + 16F887 GETTING STARTED

PROJECT 127 SEGMENT DISPLAY WITH CD4511 ENCODERBesides on directly connected 7 segment with direct I/O port from microcontroller, we can also use the CD4511 (7 segment encoder) to help on decreasing the microcontroller I/O port and only required to send the binary data. COMPONENT NEEDED CONNECTION Referring to the figure below, RED and BLACK wire are VDD and GND and are share to both CD4511 encoder. Besides that, the LT, BI, LE pin from CD4511 are share to the same port of PIC MCU which is PORTB.0 , PORTB.1 and PORTB.2. Lastly, the LEFT CD4511 binary input (A,B,C,D) are connected to PORTC.0 until PORTC.3. While RIGHT CD4511 binary input (A,B,C,D) are connected to PORTD.0 until PORTD.3. FROM CD4511 TO 7 SEGMENT & PIC16F887CD4511 PINS 7 SEGMENT PIC16F887 PORTC B C LT BI LE D A VSS e d c b a g f VDD e d c b a g f VDD(+5V) PORTC.1 PORTC.2 PORTB.0 PORTB.1 PORTB.2 PORTC.3 PORTC.0 GND PORTD PORTD.1 PORTD.2 PORTB.0 PORTB.1 PORTB.2 PORTD.3 PORTD.0

7 SEGMENT DISPLAY x2 (Common Cathode)

BREADBOARD

PIN1 PIN2 PIN3 PIN4

1K RESISTOR x2

PIN5 PIN6 PIN7 PIN8

CD4511 7 SEGMENT ENCODER

PIN9 PIN10 PIN11 PIN12

JUMPER WIRES

PIN13 PIN14 PIN15 PIN16

Page 53

ROBOT . HEAD to TOE SK40C + 16F887 GETTING STARTED TRUTH TABLE

ADDITIONAL INFORMATION CD4511 7-SEGMENT ENCODER

X = Don't Care *Depends upon the BCD code applied during the 0 to 1 transition of LE.

Above are the truth table for CD4511 encoder to convert the input BCD to output of 7-segment number. CODE OVERVIEW system.h

Figure above is the CD4511 7 segment encoder which use to encode the binary input given and convert it into 7 segment number. This Integrated circuit input are required the BCD code and it will display the number from 0 9 in 7 segment.

#define dis_1 0b00000001 Declare the BCD number that will send to the CD4511 encoder. E.g: 0b00000001 are number 1 in 7 segment.

Page 54

ROBOT . HEAD to TOE SK40C + 16F887 GETTING STARTED #define LE RB2 #define BI RB1 #define LT RB0 In here, we have initially declare the I/O port that use to control the CD4511 such as lamp test(LT), blanking (BI), latch enable(LE) and store a BCD code. MAIN PROGRAM

LT = 0; Referring to the truth table, HIGH(1) the LT will stop the lamp test while LOW(0) the LT will on the lamp test. LE = 0; HIGH(1) to ON the latch enable and may store a BCD code. LOW(0) will OFF latch enable and will not store a BCD code. BI = 1; HIGH(1) will OFF the blanking and LOW(0) will ON the blanking.

References: http://www.play.com.br/datasheet/CD4511.pdf

Page 55

ROBOT . HEAD to TOE SK40C + 16F887 GETTING STARTED

PROJECT 13UART TO COMPUTERUniversal Asynchronous Receive/Transmit (UART) has becoming a popular item on sending and receiving data in the fast way. In here, we are going to experience on how to use UC00A to communicate with our computer/PC and SK40C. COMPONENT NEEDED 16 x 2 LCD DISPLAY CONNECTION To interface between UC00A and SK40C required only 4 wires connection which is power supply (VDD), ground(GND), transmit(TX) and receive (RX). For serial communication, the transmit and receive for UC00A and SK40C have to be connected in duplex. Please refer figure below and UC00A datasheet.

USB TO UART CONVERTER

HYPERTERMINAL INSTALL

JUMPER WIRES

Page 56

ROBOT . HEAD to TOE SK40C + 16F887 GETTING STARTED ADDITONAL INFORMATION 2. Enter a project name then click OK.

Wires in PIN1 and PIN2 are switch for the second connecter. HYPERTERMINAL INSTALLATION HyperTerminal for WINDOW VISTA & 7 please download from this webpage. For WINDOW XP please refer to UC00A datasheet. STEPS: 1. Open hypertrm.exe. Next, LOCATION INFORMATION will pop up like figure below. Then, click CANCEL until you see the CONNECTION DESCRIPTION.

3. Choose the suitable COM port by referring to the DEVICE MANAGER.

4. Setting up the COM PROPERTIES Like figure below.

Page 57

ROBOT . HEAD to TOE SK40C + 16F887 GETTING STARTED CODE OVERVIEW

uart_putstr(Ready To Receive & Transmit!!) Send a string of ASCII through UC00A and display on HyperTerminal to tell the computer that SK40C is ready.

5. Go to FILE > PROPERTIES > SETTING > ASCII SETUP and tick like figure below. After that, the HyperTerminal is ready to go.

Page 58

ROBOT . HEAD to TOE SK40C + 16F887 GETTING STARTED

uc_uart_receive(); Receive the data from computer. data = RCREG; Store the bytes of data receive in data. send_lcd_data(1,data); Send the data received to LCD which sending the 1 to telling the LCD that the data is a character. The data received from the computer are in ASCII, so there was no need any conversion.

for(i=0 ; i