1 / 36

Programming exercises

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

abie
Download Presentation

Programming exercises

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming exercises ABE221 Automation Section

  2. 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)

  3. Compile your code • Start-Run: type cmd (stands for command prompt in DOS) • pbp –p18f458 –ampasm c:\can\canrx5.bas

  4. Write yourself a batch file

  5. 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

  6. List output with machine code

  7. Demo

  8. 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

  9. 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.

  10. Good coding practices • Avoid Goto statements • Instead make subroutines, do not duplicate code • Give labels a descriptive name

  11. 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

  12. 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

  13. 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

  14. Lab exercise 1, Serial communication

  15. 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

  16. 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!

  17. ASCII table • American Standard Code for Information Interchange

  18. 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

  19. Some microcontrollers have USB on board • Microchip 18F87J50

  20. Lab exercise 2, Servo control

  21. 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

  22. Lab exercise 3, Sonar interface

  23. Sonar SRF04 connections

  24. 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

  25. 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).

  26. 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

  27. 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

  28. Lab exercise 4, Infrared sensor interface

  29. 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

  30. From PIC 18F458 datasheet

  31. Lab exercise 4, Remote Control

  32. 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

  33. 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

  34. The End

More Related