




























































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





























































































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.
BL0114 Combo board. BL0011 programmer.
Getting more Information Flowcode http://www.matrixtsl.com/flowcode/support/ From here you can access:
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
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:
(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
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:
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:
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.
Introduction to Microcontrollors Section 1 Microcontroller Memory Microcontrollers have three separate areas of memory: