Docsity

Microcontroller compete course, Exercises of Microcontrollers

Partial preview of the text

Download Microcontroller compete course and more Exercises Microcontrollers in PDF only on Docsity!

Section: Contents

  • Introduction - p.
  • Section 1 - Intro to Microcontrollers - p.
  • Section 2 - Using E-blocks - p.
  • Section 3 - Intro to Flowcode - p.
  • Section 4 - Flowcode - First program - p.
  • Section 5 - Flowcode - Examples - p.
  • Section 6 - Programming Exercises - p.
  • Appendix 1 - Arduino adjustments - p.
  • Appendix 2 - E-blocks 1 adjustments - p. - Index - p.

Before you start This course is an introduction to microcontroller programming. To get the full use out of this course we recommend you have the following: Flowcode Flowcode is a software program which allows users to quickly and easily develop complex electronic systems in a simple manner, it works with a range of microcontrollers, including Microchip’s ‘PIC’ microcontrollers (PIC MCUs), Arduino, and ARM. Flowcode 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. Hardware It is always more rewarding when learning about microcontroller programming to see the programs execute on actual hardware, therefore we recommend that you have some hardware available to send and execute your created programs onto. This course is mainly designed around the Matrix E-blocks 2 hardware platform, typically the BL0011 programmer and the BL0114 Combo board, although separate E-blocks (LCD, switches, LEDs, etc) can also be used. While most of the course is designed around the E-blocks2, we also recognise that some people may be using either an Arduino or Eblocks1 devices. So in this course, whenever there is change in the instructions for Eblocks1 or Arduino changes, they will be displayed in the following colours: Arduino users need an Arduino Uno and E-blocks Arduino Uno Shield (BL0055), as well as the Combo board. E-blocks1 users will need the EB006v9 Multiprogrammer and the EB083 Combo board plus connecting wires.

Introduction

BL0114 Combo board. BL0011 programmer.

Getting more Information Flowcode http://www.matrixtsl.com/flowcode/support/ From here you can access:

  • Flowcode—Getting Started Guide
  • Flowcode Wiki
  • A wide range of Flowcode examples E-blocks http://www.matrixtsl.com/eblocks/resources In the E-blocks section you can get the follow resources:
  • E-blocks USB Drivers
  • E-blocks example files
  • E-blocks User Guide http://www.matrixtsl.com/eblocks/boards From the boards pages:
  • Specific datasheets for the boards
  • Specific board examples Other Help http://www.matrixtsl.com/mmforums/index.php The Matrix forum provides an in-depth community of well established, long-term users of Flowcode and new Flowcode users sharing ideas and solving problems and issues encountered whilst using the software. http://www.matrixtsl.com/learning/ The Matrix ‘Learning Centre’ contains many different resources including articles, drivers, curriculum.

Introduction

The hardware: Most exercises use the BL0011 / BL0080 Multiprogrammer and BL0114 Combo board. Most of the exercises can also be completed using the Arduino Uno Shield (BL0055). However, these require different PORT settings. Hardware and software settings used to test most programs: Hardware jumper settings: Flowcode and download settings: BL0011 / BL0080 Jumper settings Microchip PIC MCU 16F Target voltage 5V J Voltage source PSU J Programming Source USB J12,13,14: USB Oscillator Selector OSC J18, Port A E-block E-blocks Combo board Port B E-block BL Port C E-block Port D E-block Port E E-block Menu Name Setting E-blocks 1 Build > Project Options... > Choose a Target Family - PIC 16F18877 16F Build > Project Options... > General Options Clock speed (Hz) 32 000 000 19 660 800 Simulation speed Normal Normal Build > Project Options... > Configure Oscillator HS Oscillator HS Oscillator Watchdog Timer Enable bit Disabled Disabled

Introduction

01 Send different 8-bit codes to ports of the microcontroller. 02 Change the logic level of a one single pin. 03 Configure an output icon. 04 Use binary code. 05 Manipulate logic output levels. 06 Use LED’s to display an output. 07 Compile a program to the PIC MCU. 08 Add a delay to slow down execution of a program. 09 Change the delay interval. 10 Configure a delay icon. 11 Control the speed of a microcontroller. 12 Use an oscilloscope to time events. 13 Use Connection Points to introduce unconditional branching in a program. 14 Introduce PWM as a means of controlling the brightness of LEDs. 15 Create an infinite loop. 16 Manipulate logic output levels. 17 Use LEDs to display an output. 18 Create and use a variable. 19 Configure a calculation icon to perform arithmetic and logic calculations. 20 Create and manipulate variables. 21 Perform calculations. 22 Use LEDs with current limiting resistors. 23 Create and use a ‘running light’ program, using the ‘multiply-by-two’ method. 24 Create and use a ‘running light’ program, using the ‘shift-right’ method. 25 Create and populate an array. 26 Create a conditional loop. 27 Input data from switches. 28 Use loops to create LED sequences. 29 Configure an input icon. 30 Configure decision icons and hence add conditional branching to a program. 31 Control the frequency at which LEDs flash. 32 Use LEDs to display output logic levels. 33 Use temporary memory. 34 Create, populate and manipulate string variables. 35 Control the display of text and numbers on a LCD. 36 Use a LCD as an output device for the PIC MCU. 37 Configure a Component macro for the LCD. 38 Input text and numbers from a keypad and display messages on the LCD. 39 Use ASCII code to transmit this data. 40 Use multiplexed inputs. 41 Configure a Component macro for the keypad. 42 Create data loggers, using 8-bit and 10-bit data from the ADC. 43 Configure an analogue input. 44 Enter data via switches. 45 Enter information from light and temperature sensors. 46 Configure and use the EEPROM. 47 Scroll through EEPROM data. 48 display text and numerical data on the LCD. 49 Use the E-blocks prototype board. 50 Use software macros to simplify the structure of a program. 51 Create software macros. 52 Use closed loop control. 53 Use PWM to control the brightness of LEDs. 54 Create and use ‘single-pin’ interrupts. 55 Create and use ‘interrupt-on-change’ (IOC) interrupts. 56 Use real time operation of a PIC MCU. 57 Create and use timer interrupts. 58 Use the prescaler to create accurate time intervals. 59 Trigger the timer using the crystal or an external event. After completing this course, you will be able to:

Introduction Objectives

(Mapping to Unit 6 of the BTEC Level 3 National Extended Diploma in Engineering is given on page 9).

Mapping to BTEC Level 3 National Extended Diploma in Engineering Unit 6 B Programming Techniques and Coding B1 Programming techniques Use of a programming development environment software operation  connecting to microcontroller hardware  creating and managing program files  syntax/error checking  simulation  compiling, downloading and live testing  monitoring/debugging  safe use of computer and display (^)  Coding practices: device set-up and program initiation introductory comments  chip set-up  pin modes  Libraries  Declarations  efficient/effective code authoring  code syntax  in-line commenting  code organisation and structure  B2 Coding constructs Input/output digital – bit and port level read/write  analogue read/write, resolution, calibration  tone and sound generation  pulse and PWM  communication, including serial and I2C  Program flow and control: calling libraries (^)  subroutines and functions  control structure sequence iteration

  • if, else, switch, case, for, do, while, until and end  delays and timing  Interrupts  Logic and arithmetic variables – data types (Boolean, character, byte, integer, word, float, long, double, string)  arrays (^)  comparative operators: =, not =, <, >, < or =, > or =.  Boolean operators – AND, OR, NOT  logic using input condition
  • digital and analogue  arithmetic operations  B3 Structured program design pseudo code  flowchart  decision table  B3 Number systems bits, bytes  parallel and serial  binary to decimal conversion 

Mapping to BTEC Level 3 National Extended Diploma in Engineering Unit 6 Key C System development cycle Project C1 Development processes Project Stages of the development process. C2 Documentation Project A portfolio of evidence produced throughout the development process. Symbol Meaning  (^) Content covered by the course  (^) Content partly covered by the course  (^) Content not covered by the course Project Content addressed through project work

Introduction to Microcontrollers Section 1 Microcontrollers At their heart (or is it brain?) there is a Central Processing Unit (CPU). This processes the digital signals, does calculations and logic operations, creates time delays, and sets up sequences of signals. How does it know what to do? It is following a program of instructions, stored in Section of the memory, called the program memory , inside the microcontroller. From time to time, the CPU needs to store data, and then later retrieve it. It uses a different area of memory, called the data memory to do this. The clock synchronises the activities of the CPU. It sends a stream of voltage pulses into the CPU that controls when data is moved around the system and when the instructions in the program are carried out. The faster the clock, the quicker the microcontroller runs through the program. Typically, the clock will run at a frequency of 20MHz (twenty million voltage pulses every second). To talk to the outside world, the microcontroller has ports that input or output data in the form of binary numbers. Each port has a number of connections (often referred to as bits ). An 8-bit port handles an 8-bit (or one byte) number. Information from sensors is fed into the system through the input port(s). The microcontroller processes this data and uses it to control devices that are connected to the output port(s). The ports themselves are complex electronic circuits, not simply a bunch of terminals to hang components on.

Introduction to Microcontrollors Section 1 Microcontrollers—PIC and AVR The name PIC refers to a popular group of microcontrollers, produced by Arizona Microchip. AVR is another group of microcontrollers, also made by Microchip. The microcontroller on an Arduino board is actually an AVR chip. When we use a microcontroller, we have to specify how we want the ports to behave. The ports are bi-directional, meaning that they can act as either input ports or output ports. When we write a program for a microcontroller, we start by configuring the ports, telling them whether they are to behave as input ports or output ports. The input port can receive data (information) in one of two forms, as an analogue signal, or as a digital signal. It is important that we understand clearly the difference between these. The Digital World Much of our everyday information is described in numerical format. For example:

  • "It is 2 o'clock."
  • "The temperature outside is 21 degrees C."
  • "The car was travelling at 48 kilometres per hour." It is easy to understand data in this form. For example, the table shows how the speed of a car changes over a period of time. However, you might wonder what happened at time 35 seconds. Was the car moving faster or slower than 25 km/h at that moment? The Analogue World Now the information is given in the form of an analogy. In other words, we use something that behaves in a similar way. For example:
  1. The hour glass egg timer: The greater the time elapsed, the deeper the sand in the bottom of the egg timer.
  2. The mercury-in-glass thermometer The hotter it gets, the further the mercury moves up the tube.
  3. The car speedometer The higher the speed, the further the pointer moves around the dial. The problem with analogue data is that you have to do some work to extract it. For the speedometer, and thermometer, you have to work out where the pointer sits on the scale. On the other hand, it is easy to judge how the temperature of a body or speed of a car is changing. We see the mercury moving along the tube or the pointer moving around the dial. Time in seconds Speed in kilometres per hour 0 0 10 15 20 21 30 25 40 22 50 20 60 16

Introduction to Microcontrollors Section 1 Analogue to Digital Conversion Much of our 'real world' data is analogue , but computers (including microcontrollers), only process digital data. Fortunately microcontrollers often contain a subsystem that can convert information from analogue format to digital format. This is called an Analogue-to-Digital Converter (usually shortened to ' ADC ' or ' A/D '). The ADC inside a microcontroller divides the range of possible analogue voltages into equal steps. The lowest step is given the number ‘0’, and the highest step is given the highest number that the A/D converter can handle. This highest number is determined by the resolution of the ADC, which, in turn, depends on number of bits the internal circuitry of the ADC can handle. Typical resolution of microcontroller ADCs is 8, 10 or 12 bit. For example, if the biggest analogue voltage is 5V, and a microcontroller has an 8-bit ADC:

  • the highest 8-bit number is 1111 1111 (= 255 in decimal).
  • the first step is 0000 0000 (= 0 in decimal).
  • meaning that there are 256 voltage levels.
  • so stepping from one level to the next involves a voltage jump of 5V/256, or about 20mV. When this microcontroller processes an analogue signal , it first divides it by 20mV, to find out how many steps the signal includes. This gives the digital equivalent of the analogue signal. The next graph illustrates this process. In our example, the converter outputs 0000 0000 for any analogue signal up to 20mV, outputs 0000 0001 for analogue signals between 20 and 40mV, and so on. The analogue signal shown in the graph produces an output of 0000 0011.

Introduction to Microcontrollors Section 1 Inputting data into a Microcontroller A microcontroller is a digital device, but data can be entered in both analogue and digital forms. Programmers choose whether pins on the microcontroller are used as analogue inputs, digital in- puts or digital outputs. This flexibility leads to complex labelling. The diagram shows the pinout for a typical 40pin PIC microcontroller, in this case it is the 16F1937 chip. It has five ports, known as A, B, C, D and E. The pins on port A are labelled RA0 to RA7; pins on port B are la- belled RB0 to RB7 etc. Ports A, B, C and D have eight pins but port E has only four. For example, up to eight digital sensors can be connected to port A of the 16F1937. Pin 2 is marked as ' RA 0/ AN 0', meaning that it can be used as bit 0 of port A ( R egister A bit 0) or as AN alogue input 0. The function of each input / output pin is determined by setting the contents of in- ternal registers, called 'data-direction' reg- isters inside the microcontroller. Pins RA6 and RA7 are also labelled as ‘OSC1’ and ‘OSC2’. They can be connected to an external os- cillator circuit or be used for digital input /output. Analogue sensors must be attached to the pins labelled with an 'ANx' (ANalogue) label. These, found on ports A, B and E, can handle analogue signals between VDD (5V) and VSS (Gnd). Most pins have alternative functions. For example pin 25 is labelled as 'RC6/TX/CK', meaning that it can be Register C bit 6, or the transmit (TX) pin of the internal serial interface, or the C loc K pin of the internal serial interface. Fortunately Flowcode takes care of the internal settings that dictate pin functionality for you. Outputting data The microcontroller is a digital device - we have said that several times already. It outputs a digital signal. In most cases, we use this to turn something on and off - '0' = 'off' and '1' = '0n'. For example: Suppose that we set up port B as the output port (or let Flowcode do it for us). There are eight pins on port B, so we can switch eight devices on and off. It is important to plan how we connect these devices, as otherwise they might work the opposite way round.

Introduction to Microcontrollors Section 1 Electronic systems only understand binary numbers. This very small memory has eight locations (notice that numbering normally starts at 0). It needs a 3-bit binary number to create unique addresses for each location. We can store items of data that are eight bits long (one byte - 1B). Our example memory could be called a 8 x 1B memory. Memory systems used in computers are much larger. Data is often stored as 32 bit numbers, allowing the use of much larger numbers. There are many more locations, too. A typical computer memory now has millions of memory locations. Types of Memory There are several types of electronic memory, each with a slightly different job to do. We can divide them into two main groups, ROM and RAM. Read Only Memory (ROM) These devices are normally only read (i.e. the contents are accessed but not changed / written,) during the running of a program.

  • The contents are not volatile (the data remains stored even when the power supply is switched off).
  • They are often used to store the basic programs, known as operating systems , needed by computers.
  • The group includes:
    • PROM (Programmable Read Only Memory)
    • EPROM (Erasable Programmable Read Only Memory)
    • EEPROM (Electrically Erasable Programmable Read Only Memory) A PROM is a one-shot device, which arrives blank, ready to receive data. Data can then be 'burned' into it, but only once. After that it behaves like a ROM chip that can be read many times but not altered. With an EPROM , shining ultraviolet light through a window in the top of the chip erases the contents. New data can then be 'burned' into the memory. Some older microcontrollers operate in this way. The EEPROM devices work in a similar way to an EPROM , except that the contents are erased by sending in a special sequence of electrical signals to selected pins. Fl ash memory is a form of EEPROM , widely used as the storage medium in digital cameras (the memory stick) and in home video games consoles. Random Access Memory (RAM)
  • RAM allows both read and write operations during the running of a program.
  • The contents are volatile and disappear as soon as the power supply is removed. (The exception is NVRAM , Non-Volatile RAM , where the memory device may include a battery to retain the contents, or may include an EEPROM chip as Section of the memory to store the contents during power loss).
  • They are often used for the temporary storage of data or application programs.

Introduction to Microcontrollors Section 1 Microcontroller Memory Microcontrollers have three separate areas of memory:

  • program memory (Flash)
  • user variable memory (RAM)
  • EEPROM The names give strong hints as to the purpose of the areas. For the eighteen pin PIC16F84 the graphic illustrates the organisation of the memory. Program memory is used to store the program. In most microcontrollers, such as the 16F1937, this uses Flash technology, meaning that it can be programmed and cleared many times. Older PIC MCUs use PROM for the program memory so that many of these can be programmed only once. Data memory is used to store data. Section of this uses RAM and Section uses EEPROM. The EEPROM allows us to preserve important data even if the power supply to the system is switched off. For example, suppose that the microcontroller is used as a temperature controller that keeps an incubator at a set temperature. It might make sense to store the target temperature value in EEPROM so that we do not have to enter it into the system every time we switch the incubator on. Programming Microcontrollers are programmable devices. They do exactly what they are told to do by the program, and nothing else. A program is a list of instructions, along with any data needed to carry them out. The only thing microprocessors understand is numbers. There's a problem because we don't speak in numbers, and they don't understand English. There are two solutions, and both need some form of translator:
  • Write the program in English, or something close to it, and then have the result translated into numbers.
  • We can think through the program design in English and then translate it ourselves into a language that is similar to numbers, known as assembler. From there, it is a swift and simple step to convert into the numerical code that the microcontroller understands. These two extremes are known as programming in a high-level language (something close to English) or in a low-level language (assembler). The first is usually quicker and easier for the programmer, but takes longer to run the program, because of the need to translate it for the microcontroller. The second is much slower for the programmer, but ends up running very quickly on the microcontroller.