Saturday, 8 July 2017

Programming Microcontrollers

20171120.1

Introduction

In the past, microcontrollers were studied in academic institutions, requiring expensive and sometimes sophisticated equipment. Now you can study a microcontroller as a hobby at home, spending less than 100 Singapore dollars, assuming that you already have a computer. Besides you may not have to study all the technical details. This article may not be very well-organized, but it does not mean that it is not as good as the course in the educational institutions. Due to various constraints, it is unlikely that the approach here can be used in the educational institutions.
The Arm-based 32-bit microcontrollers are a little bit hard to learn, especially for beginners. But it is rare that you get a chance to study a 32-bit advanced microcontroller by just paying a few Singapore dollars. In any case, the flexibilities and the complexities provided by such high-end microcontrollers will soon become normal for 16-bit and 8-bit microcontrollers. Thus, even for beginners, it is more efficient to start with these 32-bit microcontrollers. 

Aim
To study a microcontroller as a hobby.

Prerequisites
            Able to download materials from the internet.
            Able to write programs in C language.



What is a microcontroller ?

A microcontroller is a ‘stored-program, high-speed, digital IC with on-chip functions’.

IC
A microcontroller is an IC(integrated circuits), now usually in LQFP (low profile quad flat package). It is quite difficult to solder it on the PCB(printed circuit board) without proper training. Fortunately, on many development kits, the pins are brought out at a pitch of 0.1-inch, suitable for wire connection. 
                       
Digital
A microcontroller is an electronic digital device. Its operation involves logic states of ‘1’/‘set’ and ‘0’/’clear’ only. Usually groups of 8 bits, 16 bits or 32 bits are operated on. As C language does not support binary numbers, hexadecimal notations are often used. Logic operations such as ‘AND’, ‘OR’ and shift’s are particularly useful.

High Speed
A microcontroller usually runs with a clock signal in the MHz (megaHertz) range. Higher speed is usually not required. Even at the frequency of 1 MHz, one cycle has a period of 1 us (microsecond) which is very fast for a human being. Thus, in many cases, the microcontroller has to be programmed to wait a while for the user to see the changing LED’s or to hear the beep sound. For such purposes, software delays are usually used in the early stage. At the later stage, timer interrupts may be introduced to enable events to occur at regular intervals. Beginners should get used to talking in terms of us (microseconds) and ms (milliseconds).

Stored-Program
The operation of a microcontroller is controlled by a program stored in its memory. As a program can be very flexible, the applications for a microcontroller cover a wide area. The performance of a microcontroller-based project depends mainly on the program. Program development takes a lot of time and effort.

The program codes are placed in the microcontroller’s memory through a programmer. Recently, these programmers/debuggers have become rather low cost, which makes it much easier for the hobbyists.
By the way, the size of the on-chip program memory is usually large that we seldom talk about efficient usage of the program memory.
 
On-chip Functions
A microcontroller differs from a microprocessor in that it contains memories and on-chip functions. Such on-chip functions reduce external devices, connections and simplify applications.

   
Bit Operations

Other than flags, binary notations are not supported by C language. So binary numbers have to be written in hexadecimal notations, such as 0x53 for 0b01010011 (b for binary).
Bit operations are then carried out using logic operations.
To set particular bits, use the ‘|’ (OR) operation.
To clear particular bits, use the ‘&’ (AND) operation.

It will become obvious that bit shifting and the use of terms appropriately defined in a header file are very helpful in writing a comprehensive program.

For example:

1.         RCC_AHBENR_GPIOBEN = 0x00000002 is defined in a header file
RCC->AHBENR   |=  RCC_AHBENR_GPIOBEN ; then sets bit 2 of AHBENR

2.         1<<6 (shift left 6 bits) is actually 00000000 01000000
                  GPIOB->ODR    |=    1<<6 ;  then sets bit6 of GPIOB->ODR

3.         To configure bit 6 as an output, two bits (‘01’) have to be written at bits 13 and 12 of the mode register. But the mode register is zero on reset, so we just set bit 12 and leave bit 13 as ‘0’.
          0x01 = 00000000 00000000 00000000 00000001 (bit0 = 1)
                0x01<<(2*6) = 00000000 00000000 00010000 00000000 (bit12 = 1)
GPIOB->MODER         |=    0x01<<(2*6) ;  // set bit 12, leave bit 13 as ‘0’


Program Development

The process of program development involves the following steps:
  1        Understand the operation of the microcontroller and its peripherals
  2        Write a straight-forward program to test a specific function
  3        Compile the program into machine (hexadecimal) codes
  4        Download the codes into the microcontroller’s memory
  5        Execute the program (in standalone mode)
  6        Verify that the operation follows the program

Steps 2 and 3 require a PC with an IDE and the C compiler. These software may be freely downloaded from the internet. But because a program may need some supporting files such as the start-up file and header files, the compilation may not always be straight forward. In addition, free compilers usually come with limitations.

Step 4 requires a programmer or a debugger or in older times an expensive emulator. In some cases, the programmer/debugger shares the same PCB as the microcontroller. As the programmer works with the IDE on the PC through the USB, the link may be a problem due to rapid development of the versions, on the IDE and on the programmer. In one particular case, all other USB devices (except the mouse) have to be disconnected before a link worked.

Steps 5 and 6 sound simple. But things may still go wrong. Sometimes, a project can only work with the emulator connected. Even today, it may still be  possible to have a program running as expected immediately after downloading and executing from the IDE. But if the IDE is closed and the board is powered through the USB again or through a separate power supply, the program fails.

Thus, if an LED blinks as programmed, it is quite a big step.



Wire Wrapping

Wire wrapping is a reliable and flexible method for circuit connection. It is also relatively cheap, except for the initial cost. The wire-wrapping tool, WSU 30M for example, costs about 30 Singapore dollars. The pins are expensive, but they can be recycled, if you avoid cutting them into short strips. The consumable wires are also expensive and are less likely to be recycled. So you should buy long 1000-feet coils and not small coils. You can buy a piece of good-quality wire-wrapping board for about 10 Singapore dollars or one low-cost version for about 3 Singapore dollars.

Wire wrapping is a skill which improves with good practice.

Wire wrapping is an art, some can do it better than the others.

As the electronic parts can be purchased online at a relative lower cost, wire-wrapping may not be the more attractive alternative anymore.


                   

Function Registers

Developing a microcontroller application usually involves three area of work:
            Read those technical manuals
            Write C programs
            Do some arithmetic and logic operations

In most cases, you read the technical manual when you want to use a particular function such as the SPI. The manual most properly does not describe the function in detail, assuming that you already know that function. It usually describes the features of the function provided by this microcontroller. You will then find that the selections of the options and the operation of the function are carried out through many registers. These registers are sometimes called special function registers or just function registers or just registers for that function. The registers can generally be classified into three types: data registers, control registers and status registers. Occasionally, there are very few status bits  and these bits are just included in one of the control registers. So most of the work involves the setting or clearing of a bit or a few bits in a particular register. These bit operations use the ‘AND’ and ‘OR’ operations heavily.

A header file for the various control and status bits would be very useful in writing the codes for the initial configuration and the bit operations for the data registers. Most codes would then be an exercise to copy the terms in the header file into the C statements. Such practice will be more comprehensive and less error prone. The older method of writing the bits in binary, convert it into a hexadecimal number and assign it to a function register is not efficient, hard-to-understand and allows mistakes to be easily made.

End.

No comments:

Post a Comment