programming exercises n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Programming exercises PowerPoint Presentation
Download Presentation
Programming exercises

Loading in 2 Seconds...

play fullscreen
1 / 36

Programming exercises - PowerPoint PPT Presentation


  • 121 Views
  • Uploaded on

Programming exercises. ABE221 Automation Section. Agenda. How to develop programs in PicBasic Write your code (BasicAtom’s editor) Compile your code (using PBP and MPASM) Program your code into 18F458 Microcontroller (using MElabs programmer) Demo Good coding practices Lab exercises

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Programming exercises' - abie


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
programming exercises

Programming exercises

ABE221 Automation Section

agenda
Agenda
  • How to develop programs in PicBasic
    • Write your code (BasicAtom’s editor)
    • Compile your code (using PBP and MPASM)
    • Program your code into 18F458 Microcontroller (using MElabs programmer)
    • Demo
  • Good coding practices
  • Lab exercises
    • Serial communication
    • Control a servo (generate pulse train (PWM))
    • Interface with sonar (Pulsin/Pulsout commands)
    • Interface with infra red sensor (Analog input)
    • Remote control interface (Pulsin command)
compile your code
Compile your code
  • Start-Run: type cmd (stands for command prompt in DOS)
  • pbp –p18f458 –ampasm c:\can\canrx5.bas
upload your code in the chip
Upload your code in the chip
  • Make sure you have the right chip!
  • Make sure you have the right settings
  • 4 MHz crystal = XT
  • 20MHz crystal = HS
  • Disable
    • watchdog timer
    • Brownout reset
    • Power-up timer
  • Find out how to save these settings with the file itself
example of bad coding
Example of bad coding
  • No comments to explain what program does
  • What are TCA, TC, AT, X?
  • Why not explain what the subroutine in 700 does?
  • Where do the formulas come from? References?
  • Jumping around from 380 to 340 and from 360 to 390
  • This is called spaghetti programming
good coding practices
Good coding practices
  • Use a header block
    • Name of author
    • Function of program
    • Revision information
  • Use meaningful variable names not x,y
  • Use lots of comments !! They don’t take up space in the PIC controller.
  • Use meaningful comments
    • “Set Pin0 to 1" simply explains the syntax. Tell the user (or yourself) what it is for.
  • Make comment blocks to illustrate what sections do
  • Use indents for loops
  • Keep constants in a Constants block close to the beginning of the program
  • Do not hardcode numbers, give them a name and put them in a variable
  • Show where you got your wisdom. Put hyperlinks in the comments to papers etc.
good coding practices1
Good coding practices
  • Avoid Goto statements
  • Instead make subroutines, do not duplicate code
  • Give labels a descriptive name
how do we program the ant
How do we program the Ant?
  • RS232 serial port to Hyper Terminal
  • Sonar: Generate pulse and receive echo pulse
  • Infrared sensor: Continuous analog value
  • Servo: Output PWM (pulse train)
  • Optional
    • Zigbee through RS232
    • Remote control
slide13
f4_6

Power(VDD = 5V)

Power (VDD = 5V)

How to use the 18F458

Reset

Port B

(DIO)

Port A0-3

(Analog inputs)

CAN

Port A4-7

(Analog inputs)

Port D 4-7 (DIO)

External clock

Port C 0-3 (DIO)

Port C 4-7 (DIO)

Serial TX/RX

Port D 0,1 (DIO)

Port D 2,3 (DIO)

From Cetinkunt - Mechatronics

proposed connection scheme for ant
Proposed connection scheme for Ant

' Function Signal type Pin

'

' IR sensor left Analog input A0

' IR sensor right Analog input A1

'

' Middle servo PWM output C0

' Left servo PWM output C1

' Right servo PWM output C2

'

' RC channel 1 PWM input B7

' RC channel 2 PWM input B6

'

' Sonar echo pulse Digital In C4

' Sonar trigger pulse Digital out C5

'

' Serial out (LCD/terminal) Serial out C6

rs232 communication
RS232 communication
  • Use DB9 connector (female)
    • 2 TX (connect to C6 (pin 25) of the 18f458)
    • 3 RX (connect to C7 (pin 26) of the 18f458)
    • 5 Gnd (connect to ground)
  • Use 9600 Baud inverted (N9600)
  • Write a program that continuously sends ‘Hello World’
  • Start hyper terminal, set to 9600 baud and connect
serial rs232 communication
Serial (RS232) communication

Define your clock (crystal)

Include library file allowing use of “N9600”, and “PORTC.6”

Set C-port data direction register (trisc)

Send ‘Hello World’ with carriage return continuously

Baud = #bits per second not bytes!

ascii table
ASCII table
  • American Standard Code for Information Interchange
rs232 communication drawbacks
RS232 communication drawbacks
  • Only defined physical layer in OSI model
    • Voltage levels, pinouts, connectors types (DB9, DB25)
  • No mechanism to deal with data collisions.
    • Instead avoid them using handshaking
  • No protocol prescribing how to communicate apart from ASCII
  • Multi-drop (meaning a connection between more than two devices) operation of an RS-232 compatible interface is not defined
  • Common signal ground limit the noise immunity and transmission distance
  • Limited bandwidths (up to 56.7kilo baud)
  • Compare
    • USB 2.0 480 Mega bits per second
    • FireWire (IEEE 1394) 400 Mega bits per second
  • Connectors are large by current standards
  • Asymmetrical definitions of the two ends of the link make the assignment of the role of a newly developed device problematic. Solutions:
    • Cables can be straight through or null-modem (switched TX and RX)
    • Gender changers
    • Often trial and error
servo control
Servo control
  • Produce pulses of 1.4 ms wide, frequency 50-60 Hz
  • 50 Hz = 20 ms cycle time
  • Put pause in of about 18ms
    • 18+1.4 = 19.4 ms cycle time, about 51.5 Hz
  • Repeat this about 10 times to give servo time to move
slide25
Generate 10ms pulse on Trigger input line
  • Measure duration of high return pulse which is proportional to travel distance
  • Calibrate to make cm out of it
pulsin command sonar remote control
Pulsin command (Sonar, Remote control)

PULSIN Pin,State,Var

Measures pulse width on Pin. If State is zero, the width of a low pulse

is measured. If State is one, the width of a high pulse is measured.

The measured width is placed in Var. If the pulse edge never happens

or the width of the pulse is too great to measure, Var is set to zero. If

an 8-bit variable is used, only the LSB of the 16-bit measurement is

returned.

Pin is automatically made an input. 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).

pulsout command sonar servos
Pulsout command (Sonar, Servos)

PULSOUT

PULSOUT Pin,Period

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. 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 10ms increments. If a 20MHz oscillator is used, Period will have a 2ms resolution. Defining an OSC value has no effect on PULSOUT. The resolution always changes with the actual oscillator speed.

‘ Send a pulse 1ms long (at 4MHz) to PORTB.5

PULSOUT PORTB.5,100

sonar
Sonar
  • Set up variables for echo, trigger, dist, dist_raw
  • word = 2 bytes
  • Conv_cm is a conversion factor constant
  • Generate puls on trigger line. 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.
  • Wait for echo and measure the time that transpired during travel of sound
analog input infrared sensors
Analog input (Infrared sensors)
  • Set up AD conversion
  • Define variables to hold infrared values
  • Make port A all inputs
  • Set port A to Analog Input (using ADCON1)
  • Get Analog values and spit them out on the serial bus
remote control
Remote control
  • Measure pulse widths of incoming PWM signals
  • Set up data direction registers for b,c port
  • Measure widths of high pulses, both channels
  • Spit them out on the serial port, catch in Hyper terminal
datasheets
Datasheets
  • PIC 18F458 controller
    • http://www.age.uiuc.edu/classes/age221/Lectures/AALectures/Lab/PIC18f458.pdf
  • Pic Basic Pro Manual
    • http://www.age.uiuc.edu/classes/age221/Lectures/AALectures/Lab/PicBasicProManual.pdf