Programming PIC Microcontrollers in PicBasic Pro Servos Cornerstone Electronics Technology and Robotics II 4 Hour Class

Similar documents
Programming PIC Microcontrollers in PicBasic Pro Lesson 1 Cornerstone Electronics Technology and Robotics II

Servo Info and Centering

Chapter 4: Pulse Width Modulation

MODULE BOUSSOLE ÉLECTRONIQUE CMPS03 Référence :

An Introduction to MPLAB Integrated Development Environment

ezsystem elab16m Project 1F: Alarm System (Full Project description)

CHAPTER 11: Flip Flops

Servo Motors (SensorDAQ only) Evaluation copy. Vernier Digital Control Unit (DCU) LabQuest or LabPro power supply

Eric Mitchell April 2, 2012 Application Note: Control of a 180 Servo Motor with Arduino UNO Development Board

INTRODUCTION TO SERIAL ARM

Cornerstone Electronics Technology and Robotics I Week 15 Voltage Comparators Tutorial

Using the Motor Controller

Microcontroller Code Example Explanation and Words of Wisdom For Senior Design

[F/T] [5] [KHz] [AMP] [3] [V] 4 ) To set DC offset to -2.5V press the following keys [OFS] [+/-] [2] [.] [5] [V]

PHYS 2P32 Project: MIDI for Arduino/ 8 Note Keyboard

Ocean Controls RC Servo Motor Controller

SD21-21 Channel Servo Driver Module Power Servo's

ServoPAL (#28824): Servo Pulser and Timer

B Robo Claw 2 Channel 5A Motor Controller Data Sheet

Bluetooth + USB 16 Servo Controller [RKI-1005 & RKI-1205]

Key Words Student Paper, School of Professional Studies

ETEC Digital Controls PIC Lab 10 Pulse Width Modulation

Special Lecture. Basic Stamp 2 Programming. (Presented on popular demand)

SPI. Overview and Use of the PICmicro Serial Peripheral Interface. Getting Started: SPI

Robot Board Sub-System Testing. Abstract. Introduction and Theory. Equipment. Procedures. EE 101 Spring 2006 Date: Lab Section # Lab #6

PID Control. Proportional Integral Derivative (PID) Control. Matrix Multimedia 2011 MX009 - PID Control. by Ben Rowland, April 2011

Software Manual RS232 Laser Merge Module. Document # SU Rev A

Six-servo Robot Arm. DAGU Hi-Tech Electronic Co., LTD Six-servo Robot Arm

cs281: Introduction to Computer Systems Lab08 Interrupt Handling and Stepper Motor Controller

ECE 495 Project 3: Shocker Actuator Subsystem and Website Design. Group 1: One Awesome Engineering

Chapter 13. PIC Family Microcontroller

Using Arduino Microcontrollers to Sense DC Motor Speed and Position

EXERCISE 3: String Variables and ASCII Code

PIC Programming in Assembly. (

The Programming Interface

Decimal Number (base 10) Binary Number (base 2)

Objectives: Part 1: Build a simple power supply. CS99S Laboratory 1

How To Program A Microcontroller Board (Eb064) With A Psp Microcontroller (B064-74) With An Ios 2.5V (Power) And A Ppt (Power Control) (Power Supply) (

AUTOMATIC NIGHT LAMP WITH MORNING ALARM USING MICROPROCESSOR

Your Multimeter. The Arduino Uno 10/1/2012. Using Your Arduino, Breadboard and Multimeter. EAS 199A Fall Work in teams of two!

RCDC1 Radio Controlled Device Controller- 1 Channel

Digital Systems Based on Principles and Applications of Electrical Engineering/Rizzoni (McGraw Hill

Accurate Measurement of the Mains Electricity Frequency

Using your LED Plus keypad

Wireless Security Camera

AEO Head Movement Tracker X-GYRO 1000 USER MANUAL(V1.1bata )

Introduction to Electronic Signals

c0003 A Simple PIC Application CHAPTER 3

PROJECT PRESENTATION ON CELLPHONE OPERATED ROBOTIC ASSISTANT

revolution Contents: Introduction Power 28-pin Project Board with input/output cables

STEPPER MOTOR SPEED AND POSITION CONTROL

Adding Heart to Your Technology

Analog-to-Digital Converters

Glolab Talking Phone Dial Monitor

Talon and Talon SR User Manual

Develop a Dallas 1-Wire Master Using the Z8F1680 Series of MCUs

Having read this workbook you should be able to: recognise the arrangement of NAND gates used to form an S-R flip-flop.

Pulse Width Modulated (PWM) Controller for 12 Volt Motors

Table 1 Comparison of DC, Uni-Polar and Bi-polar Stepper Motors

Switch board datasheet EB

ECEN 1400, Introduction to Analog and Digital Electronics

I 2 C Master Mode Overview and Use of the PICmicro MSSP I 2 C Interface with a 24xx01x EEPROM

GTS-4E Hardware User Manual. Version: V1.1.0 Date:

PICmicro tm Development Board

PACKAGE OUTLINE DALLAS DS2434 DS2434 GND. PR 35 PACKAGE See Mech. Drawings Section

PIC Application Notes

Radio Control System

Radio Control System

A Practical Guide to Free Energy Devices

Advanced LED Controller (LED Chaser)

Digital Electronics Detailed Outline

Arduino Lesson 14. Servo Motors

Theory and Practice of Tangible User Interfaces. Thursday Week 2: Digital Input and Output. week. Digital Input and Output. RGB LEDs fade with PWM

AVR Butterfly Training. Atmel Norway, AVR Applications Group

Pulse Width Modulation (PWM) LED Dimmer Circuit. Using a 555 Timer Chip

Experiment 8 : Pulse Width Modulation

PLL frequency synthesizer

- 35mA Standby, mA Speaking pre-defined phrases with up to 1925 total characters.

Pulse Width Modulation Applications

Micro-Step Driving for Stepper Motors: A Case Study

Whale 3. User Manual and Installation Guide. DC Servo drive. Contents. 1. Safety, policy and warranty Safety notes Policy Warranty.

1. Learn about the 555 timer integrated circuit and applications 2. Apply the 555 timer to build an infrared (IR) transmitter and receiver

Data Sheet. Adaptive Design ltd. Arduino Dual L6470 Stepper Motor Shield V th November L6470 Stepper Motor Shield

Permanent Magnet Motor Kit, Magnetic Reed Type. (SKY-ReedMotorKit) Instructions

LAB4: Audio Synthesizer

MANUAL FOR RX700 LR and NR

DESIGN OF OPEN ARCHITECTURE SHIP ALARM, MONITORING AND CONTROL SYSTEM

Lab 3 Rectifier Circuits

AMZ-FX Guitar effects. (2007) Mosfet Body Diodes. Accessed 22/12/09.

Conversion Between Analog and Digital Signals

EasyC. Programming Tips

Lecture 7: Programming for the Arduino

MidiStream. UHF Wireless MIDI System Operating Manual

Programming Logic controllers

Making Basic Measurements. Publication Number August Training Kit for the Agilent Technologies Series Logic Analysis System

PICNet 1. PICNet 1 PIC18 Network & SD/MMC Development Board. Features. Applications. Description

Implementing SPI Master and Slave Functionality Using the Z8 Encore! F083A

Lab 3 - DC Circuits and Ohm s Law

EasyPIC4 User s Manual

Microcomputer Based Electronics: Using the C Stamp in Pre- Engineering, Technology, and Engineering Programs

Transcription:

Programming PIC Microcontrollers in PicBasic Pro Servos Cornerstone Electronics Technology and Robotics II 4 Hour Class Administration: o Prayer o Turn in Quiz 4 PicBasic Pro Programs Used in This Lesson: o General PicBasic Pro Program Listing: http://www.cornerstonerobotics.org/picbasic.php o Lab 1 switch1 as.pdf: http://www.cornerstonerobotics.org/code/switch1.pdf o Lab 2 master_slave_master1 as pdf: http://www.cornerstonerobotics.org/code/master_slave_master1.pdf o Lab 2 master_slave_slave1 as pdf: http://www.cornerstonerobotics.org/code/master_slave_slave1.pdf o Lab 3 servo1 as.pdf file: http://www.cornerstonerobotics.org/code/servo1.pdf o Lab 3 servo2 as.pdf file: http://www.cornerstonerobotics.org/code/servo2.pdf o Lab 3 servo3 as.pdf file: http://www.cornerstonerobotics.org/code/servo3.pdf o Lab3 servo4 as.pdf file: http://www.cornerstonerobotics.org/code/servo4.pdf Programming PIC Microcontrollers, PicBasic Pro Basics: o Comments continued: Use lots of comments. Even though it may be perfectly obvious to you what the code is doing as you write it, someone else looking at the program may not have any idea of what you were trying to achieve. While comments take up space (memory) in your Basic source file, they do not take up any additional space in the PICmicro MCU, so use them freely. Make the comments tell you something useful about what the program is doing. For example, Turn on the red battery low LED might be more useful than Set pin 0 to 1. A block of comments at the beginning of the program and before each section of code can describe what is about to happen in more detail than just the space remaining after each statement. But don t include a comment block instead of individual line comments use both. Specifying what each pin is connected to can be helpful in remembering what hardware this particular program is designed to run on. o Constants: Named constants may be created in a similar manner to variables. It can be more convenient to use a constant name instead of a constant number. The standard format for declaring a constant is: Name of constant con Constant value 1

An example is: x con 3 o Pin, Variable, and Label Names: Give pins names that will help you or someone else decipher the program; judges in competitions are more able to follow the program logic if the pins and variables are coherent. For example, if the right motor is connected to PORTB.1, use the VAR keyword to assign a proper name: right_motor VAR PORTB.1 Also, give variables logical names. For example, if you are counting the number of times a switch1 is pressed, assign a variable name such as the one below: count_switch1 VAR Byte Finally, assign coherent names to the program labels. If the section of code is to turn the robotic car to the left, assign the label name of left_turn. Identifiers (variable names and labels) may be up to 31 characters long, but must not start with a digit. o Ports continued: As we set pins in PORTB for input or output (e.g. TRISB = %11111110), we can also set pins to high or low using PORT. For example: PORTB = %00000000 sets all PORTB pins to low (0 volts). PORTB = %11111111 sets all PORTB pins to high (+5 volts). Perform PIC Microcontrollers Programming 3 Lab 1 Blink 3 Ways. New PicBasic Pro Commands: o See the PicBasic Pro Compiler Manual by microengineering Labs, Inc. for a detailed explanation of these commands. o IF THEN: Formats: IF Comparison(s) THEN Label IF Comparison(s) THEN Explanation: The IF THEN statement judges the comparison to whether it is true or false. If the comparison is true (any other value than 0), it will execute the THEN portion of the statement. If the comparison is false (0), it will execute the statement following the IF THEN command. 2

Example: IF PORTB.0 = 1 THEN led2 'If the switch on PORTB.0 is pushed, PORTB.0 becomes high (+5V) and the comparison is true, so the program jumps to label led2 Summary of Formats for IF THEN and IF THEN ELSE Conditional s: IF Comparison(s) THEN IF Comparison(s) THEN ENDIF IF Comparison(s) THEN ELSE ENDIF o Perform PIC Microcontrollers Programming 3 Lab 2 switch1.pbp o Perform PIC Microcontrollers Programming 3 Lab 3 master_slave1 3

o GOSUB: Format: GOSUB Label Explanation: Jump to the subroutine at Label saving its return address on the stack. Unlike GOTO, when a RETURN statement is reached, execution resumes with the statement following the last executed GOSUB statement. An unlimited number of subroutines may be used in a program. Subroutines may also be nested. In other words, it is possible for a subroutine to call another subroutine. Such subroutine nesting must be restricted to no more than four levels deep (12 levels for 7Cxxx and 27 levels for 18Xxxx). Example: GOSUB beep Execute subroutine named beep (More PicBasic Pro program code) beep: HIGH 0 SOUND 1,[80,10] RETURN Turn on LED connected to RB0 Sends tone to RB1 Go back to the next line in the main routine after the GOSUB command o PULSOUT: Format: PULSOUT Pin, Period Explanation: Generates a pulse on Pin of specified Period. The pulse is generated by toggling the pin twice, thus the initial state of the pin determines the polarity of the pulse. If you want HIGH pulses, initialize the pin to LOW (For example, PORTB.0 = 0). Pin is automatically made an output. Pin may be a constant, 0-15, or a variable that contains a number 0-15 (e.g. B0) or a pin name (e.g. PORTA.0). The resolution of PULSOUT is dependent upon the oscillator frequency. If a 4MHz oscillator is used, the Period of the generated pulse will be in 10us increments. If a 20MHz oscillator is used, Period will have a 2us resolution. Defining an OSC value has no effect on PULSOUT. The resolution always changes with the actual oscillator speed. 4

Examples: PULSOUT PORTB.5,100 Send a pulse 1 msec long (at 4MHz) to RB5 PULSOUT 2,200 'Send a pulse 2 msec long to RB2. Power Supplies for Digital Circuits: o Digital circuits require power supplies that provide a stable source of power to all circuit components. o Even though our circuits are powered from a voltage regulator, motors and servos can disrupt the proper operation of the PIC microcontroller. o Make certain that the servo power supply is separate from the PIC power supply, i.e., have two +5V regulated power supplies. Otherwise, if the servo spikes the single power line supplying power to both the servo and the PIC, the 16F88 may reset. Introduction to Servos: o A servo is a special motor used to provide control for a desired rotational position through the use of feedback. o Most servos have a position range of 180 degrees. o A hobby servo is typically used to provide actuation for various mechanical systems such as the steering of a RC car, the flaps on a RC plane, or the rudder of a RC boat. Servo Connections: Black = Ground Red = +5 to +6 dc volts White/Orange = Control wire (Futaba uses a white wire) 5

Servo Pulse Signal Widths and Period: o Pulse width for counterclockwise position: 1.0 ms Pulse Width and Corresponding Servo Position o Pulse width for center position: 1.5 ms Pulse Width and Corresponding Servo Position o Pulse width for clockwise position: 2.0 ms Pulse Width and Corresponding Servo Position o Perform LAB 4 servo1, servo2, servo3, and servo4. 6

Cornerstone Electronics Technology and Robotics II Programming PIC Microcontrollers in PicBasic Pro Servos LAB 1 Blink 3 Ways Purpose: The purpose of this lab is to reinforce the three different ways to blink an LED. Apparatus and Materials: 1 Breadboard or Analog/Digital Trainer 1 PIC16F88 1 4.7K Resistor 1 150 Ohm Resistor 1 LED Procedure: o Wire the circuit blink1 below on a breadboard. o Turn on and off the LED using the following three sets of commands: PORTB.0 = 1 & PORTB.0 = 0 HIGH 0 & LOW 0 PORTB = %00000001 & PORTB = %00000000 Challenge: o Wire an LED and a current limiting resistor to each pin in PORTB. o Program the PIC16F88 to display binary counting from 0 to 255 using a FOR NEXT loop and a variable x set up in the following manner: PORTB = x PORTB = %00000000 may be written as PORTB = 0 since %00000000 in binary is equal to 0 in decimal. PORTB = %11111111 may be written as PORTB = 255 since %11111111 in binary is equal to 255 in decimal. 7

Cornerstone Electronics Technology and Robotics II Programming PIC Microcontrollers in PicBasic Pro Servos LAB 2 switch1.pbp Purpose: The purpose of this lab is to acquaint the student with the PicBasic Pro command IF THEN and their first input device into a PIC MCU. Apparatus and Materials: 1 Breadboard or Analog/Digital Trainer 1 PIC16F88 1 1K Resistor 1 10K Resistor 2 150 Ohm Resistors 2 LEDs 1 NO Momentary Switch Brief Discussion of Pull-Down Resistor, R2: o Pull-down resistor (R2) is used to hold the input to a zero (low) value when no other component is driving the input, i.e., the switch is open. If nothing is connected to pin RB0, the value of the input is considered to be floating. R2 will allow the pin to keep a steady state at zero until the switch is closed. Procedure: o Wire the circuit switch1 & switch2 below on a breadboard and program the 16F88 with switch1.pbp (NOT 16F877A_switch1.pbp). o Remember, the switch connected to RB0 is considered an input device. Input devices will allow your robotic car to interact with its environment. This is the first input device discussed to date. o Demonstrate the program using the circuit switch1. Challenge: o Design a circuit and program such that a momentary switch connected to the PIC turns on and off a dc motor. Save the program as switch10.pbp. 8

Cornerstone Electronics Technology and Robotics II Programming PIC Microcontrollers in PicBasic Pro Servos LAB 3 - master_slave1 Purpose: The purpose of this lab is to acquaint the student with connecting two PIC microcontrollers such that first MCU controls the timing of the second MCU. Apparatus and Materials: 1 Breadboard or Analog/Digital Trainer 2 PIC16F88 2 1K Resistor 2 150 Ohm Resistors 2 LEDs Procedure: o Open master_slave_master1.pbp and download to master MCU chip. Open master_slave_slave1.pbp and download to slave MCU chip. o Wire your breadboard for master_slave1 as shown below. Challenge: o Design a program and circuit using two PIC 16F88s where a switch connected to the first PIC16F88 program turns on/off a dc motor connected to the second PIC16F88. Save the master PIC program as msm10.pbp and the slave program as mss10.pbp. 9

Cornerstone Electronics Technology and Robotics II Programming PIC Microcontrollers in PicBasic Pro Servos LAB 4 - servo1.pbp, servo2.pbp, servo3.pbp, and servo4.pbp Purpose: The purpose of this lab is to acquaint the student with: o PicBasic Pro commands GOSUB and PULSOUT. o The basic operation of a hobby servo. Apparatus and Materials: 1 Breadboard or Analog/Digital Trainer 1 Oscilloscope 1 PIC16F88 1 1K Resistor 3 10K Resistors 3 NO Momentary Switches 1 Futaba 3003 Servomotor Procedure: o Wire your breadboard for servo1 shown below. Program the 16F88 with servo1.pbp (NOT 16F877A_servo1.pbp). o Make certain that the servo power supply is separate from the PIC power supply, i.e., have two +5V power supplies. Otherwise, if the servo spikes the single power line supplying power to both the servo and the PIC, the 16F88 may reset. o Relate the program code to the observed servo motions. o Open servo2.pbp and download to your chip. Use the same schematic as for servo1.pbp above. 10

o Observe the servo behavior. This servo action is suitable for panning sensor devices such as sonar sensors. o Open servo3.pbp and download to your chip. Use the same schematic as for servo1.pbp above. o Observe the waveforms on the oscilloscope. Verify that the waveforms are consistent with the program code. o Open servo4.pbp and download to your chip. Wire your breadboard for servo4 shown below. o Observe the waveforms on the oscilloscope. Verify that the waveforms are consistent with the program code. Challenge: o Write a program that slows the panning motion of servo3.pbp. Save the program as pan1.pbp. o Design and build brackets to mount a servo and SRF04 sonar module onto your robotic car. The sonar must be mounted atop the servo horn. Remember when mounting the sonar that it is not accurate at ranges closer than 3 cm. o If you mount the SRF04 sonar module lower than 12 above the floor, point it slightly upwards to avoid reflections from the flooring material. 11

Conditional Summary: Conditional statements allow programs to branch to another part of the program when a conditional comparison is true. Formats: IF Comparison(s) THEN Examples: IF PORTB.0 = 1 THEN HIGH 2 IF Comparison(s) THEN ENDIF IF PORTB.0 = 1THEN HIGH 2 LOW 3 HIGH 6 ENDIF IF Comparison(s) THEN ELSE ENDIF IF PORTB.0 = 1THEN HIGH 2 LOW 3 HIGH 6 ELSE LOW 2 LOW 5 LOW 7 ENDIF SELECT CASE Variable CASE Value CASE Another Value CASE IS Comparison CASE ELSE END SELECT SELECT CASE x CASE 1 LCDOUT $FE, 1, x =, DEC x x = x + 1 CASE 2 LCDOUT $FE, 1, x =, DEC x x = x + 2 CASE IS > 10 LCDOUT $FE, 1, x > 10 x = 0 CASE ELSE LCDOUT $FE, 1, Problem number x = 0 END SELECT 12

13