Introduction to Microcontroller Programming
Total Page:16
File Type:pdf, Size:1020Kb
Page 2 Welcome to the course! The course contains a series of programming exercises, (part 1) supported by background information for reference, (parts 2 to 6). Part 1 - Programming Exercises - p.4 Part 2 - Using E-Blocks - p.29 Part 3 - Introduction to Flowcode 7 - p.37 Part 4 - Flowcode 7 - Your first program - p.48 Part 5 - Flowcode 7 - Worked Examples - p.55 Part 6 - Introduction to Microcontrollers - p.84 Part 7 - Course Rationale - p.106 Your route through it: Broadly speaking, there are two types of student: The experienced programmer: probably begins with the ‘Programming Exercises’, (part 1), referring back to the information in the other sections, as and when needed. After all, programmers only read the manual when they get stuck! Nevertheless, before starting an exercise, you should familiarise yourself with the reference section(s) so that you know where to look for help should you get stuck! The less confident user: concentrates first on the reference material and the worked examples and then attempts the exercises. Copyright © 2016 Matrix Technology Solutions Limited www.matrixtsl.com Page 3 Before you start: ‘Flowcode 7’ works with a range of microcontrollers, including PIC, Arduino and ARM. For simplicity, this course focuses on PIC microcontrollers. It also includes information for the Arduino user. ‘Flowcode 7’ itself is microcontroller neutral - it presents virtually the same user interface regardless of the microcontroller used. The differences are in the hardware and the way the program is downloaded and tested. For PIC users, most of the exercises use the E-blocks EB006 v9 Multiprogrammer and the EB083 Combo board. These are shown in the following images. Arduino users need an Arduino Uno and E-blocks Arduino Uno Shield (EB093), shown below, as well as the Combo board. For more information: On the Matrix website, www.matrixtsl.com, in the ‘Products’ menu: click on ‘Flowcode’ - open the ‘Support Resources’ menu and access: ‘Flowcode 7’ - Getting Started Guide; ‘Flowcode 7’ Wiki; a range of ‘Flowcode 7’ examples. click on ‘E-blocks’ - access the ‘E-blocks User Guide’; open the ‘Boards’ menu and click on any E-blocks board to locate its datasheet, under the ‘Resources’ tab. search for ‘Arduino’ - access the ‘Flowcode Arduino Beginners Guide’ under ‘Resources’. search for ‘HPACT (the Actuator training panel) - under the ‘Resources’ tab, access the ‘Actuators panel worksheets’. Copyright © 2016 Matrix Technology Solutions Limited www.matrixtsl.com Page 4 Part 1 Programming Exercises Copyright © 2016 Matrix Technology Solutions Limited www.matrixtsl.com Page 5 Part 1 Contents The hardware: ............................................................................................................................................. 7 Hardware jumper settings: ......................................................................................................................... 7 Flowcode 7 and download settings: ........................................................................................................... 7 Exercise 1 - Creating Outputs ..................................................................................................................... 8 Exercise 2 - Using Delays ............................................................................................................................. 9 Exercise 3 - Using Connection Points ........................................................................................................ 10 Exercise 4 - Performing Calculations ........................................................................................................ 11 Exercise 5 - Using Loops ............................................................................................................................ 12 Exercise 6 - Inputting Data ........................................................................................................................ 14 Exercise 7 - Making Decisions ................................................................................................................... 15 Exercise 8 - Programming LCDs ................................................................................................................ 17 Exercise 9 - Using the Keypad ................................................................................................................... 19 Exercise 10 - Analogue Inputs and the EEPROM ...................................................................................... 21 Exercise 11 - Using Software Macros ........................................................................................................ 23 Exercise 12 - Using External Interrupts ..................................................................................................... 24 Exercise 13 - Using Timer Interrupts......................................................................................................... 26 Additional Challenges: .............................................................................................................................. 28 Copyright © 2016 Matrix Technology Solutions Limited www.matrixtsl.com Page 6 Programming Exercises for you to try: The aim of the exercises is to develop experience in using ‘Flowcode 7’ and, in the process, develop understanding of the programming terminology and techniques it embraces. At all stages, further experimentation is always to be encouraged. “What happens if…” is the best way to learn! Experienced programmers may wish to start the course here and use the reference material as and when needed. Most exercises assume that the programmer is using a PIC microcontroller though they are equally applicable to other microcontrollers. The next page describes the hardware and settings used in these exercises. Occasionally, they may differ from this standard, in which case the details are con- tained in the instructions for the exercise. For some exercises, it is sufficient to test programs by simulating them on ‘Flowcode 7’. Others should be downloaded to a microcontroller and tested on hardware. The instructions make the recommended option clear. The section ends with some challenges. These are fairly open-ended and contain only a brief speci- fication. Part 2 contains information about using E-Blocks to create and test control systems. Copyright © 2016 Matrix Technology Solutions Limited www.matrixtsl.com Page 7 The hardware: Most exercises use the EB006 Multiprogrammer and EB083 Combo board, shown below. (They can be supplied together as HP4832 - E-Blocks Development Board.) The tables list the hardware and software settings used to test most programs. Any changes to these are indicated in the instructions for the exercise. Hardware jumper settings: EB006 Jumper settings version 9 PIC device 16F1937 Target voltage 5V J15 Voltage source PSU J11 Programming Source USB J12,13,14: USB Oscillator Selector OSC J18,19 Port A E-block E-blocks Combo board Port B E-block EB083 Port C E-block Port D E-block Port E E-block Flowcode 7 and download settings: Menu Name Setting Build > Project Options... > Choose a Target Family - PIC 16F1937 Clock speed (Hz) 19 660 800 Build > Project Options... > General Options Simulation speed Normal Oscillator HS Oscillator Build > Project Options... > Configure Watchdog Timer Enable bit Disabled Copyright © 2016 Matrix Technology Solutions Limited www.matrixtsl.com Exercise 1 Page 8 Creating Outputs This exercise configures ‘Flowcode 7’ to output specific digital signals to the LED array. Objectives: to send different 8-bit codes to port B of the microcontroller; to change the logic level of a one single pin of port B; to configure an output icon; to use binary code; to manipulate logic output levels; to use LEDs to display an output; to compile a program to a microcontroller. Relevant course content: Part 6 - ‘Introduction to microcontrollers’; Part 2 - ‘Using E-blocks’; Adding digital outputs - “Light the LED”; Flowcode Wiki - ‘Using masks’; Instructions Program 1: create ‘Flowcode 7’ flowcharts that: add a single output icon, configured to light all port B LEDs and run the simulation; then alter the parameters to light only the odd-numbered LEDs and run the simulation; do the same but for only the even-numbered LEDs; finally do the same but for only the high ‘nibble’ bits (4 to 7) of port B. modify this program by: repeating these four steps using hexadecimal rather than decimal numbering; lighting only the LED on bit 7, by sending an 8-bit value to the port; lighting only the LED on bit 7, using the 'single bit' output method; lighting only the LED on bit 7, using the 'masking' output method. Program 2: write a program that uses at least twenty outputs icons to write different values to port B, one after the other: use all four methods in this exercise - hexadecimal, decimal, single bit and masking; simulate the program and review the results; save the program and download it to the microcontroller; restart the program a number of times by pressing the Reset button on the multipro- grammer board. Copyright © 2016 Matrix Technology Solutions Limited www.matrixtsl.com Exercise 2 Page 9 Using Delays Introduction: In this exercise, you learn how delays are used to slow down program execution. Microcontrollers work extremely quickly - a PIC can execute about 5,000,000 assembly instructions, every second. A human can detect and understand only around three stable images per second. To allow a high-speed microcontroller