EEE305 Microcontroller Systems Lecture 9B: GPIO on the Arduino Teaching resources are at www.eej.ulst.ac.uk My office 5B18, telephone 028 90 366364 My email IJ.McCrum@ulster.ac.uk What is an Arduino? • A small circuit board containing an ATMEL AVR microprocessor. AVR chips are similar to PICs • Typically with a specific physical size and form factor, with the I/o pins arranged in a specific way to support many add on boards (called shields) • Typically with a board loader using RS232 or USB so a programmer is not needed • Typically capable of running Arduino Software, from a nice simple IDE, in a sanitised language called sketch (based on C and a tiny bit of C++) • Everything is opensource and free (boards are cheap). Designed for designers and artists… Arduino boards Now several variants, often with Italian names reflecting their Origin. See Arduino.cc for info. There are also bigger MEGA boards, and boards with ARM chips, Galileo PC compatible boards and even a PIC version that carries a PIC32 – a very powerful chip. The top two above are the more normal types – Buy from Farnell.co.uk or Maplin Hardware of basic Arduino The pins are labelled as shown, irrespective of what actual pins on the physical chip are – that depends on the package, surface mount, Dual in Line (DIL) and whether a 28 pin or 40 pin device. The special software environment uses the connection pins – labelled here beside the black connectors so the software “abstracts” you away from the actual chip. Arduino is meant to be used by non-specialists, artists, hobbists, and children. Nonetheless it is very convenient – even for heavyweight embedded systems. Various power options…you program them by holding down the reset button and releasing as you you download code from the PC – a bootloader The original Connection system was based on a ATMEGA168 from ATMEL but we now typically use different more modern chips. The real pin names are on the chip datasheet but users never read that – the arduino way of doing things is give the users a simple way of achieving rapid results. – even of they may not understand how things works. This works fine usually, and there is a massive online community to help if you are doing something tricky Standardised hardware was easier to teach with. Also the All (99%) Arduinos already have an LED on pin 13 So a user can get the board to do something at once Software tools were stripped Two buttons on the TOOLBAR, one to down to compile and one to download. Also a something simple serial monitor on the PC allowed use of printf – using the very simple same hardware as the bootloader. Also even the language was made simple – based on C and C++ but all the tricky stuff was abstracted out into a powerful library system. This meant that beginners could get a lot done quickly. Libraries came with examples, there are dozen of examples found under the (simple) menu system. Also every arduino program has a setup section, run once and a loop section, this continually reruns once power is applied, you don’t need a do… while(1). Resembles C but you don’t need to tell it abou the standard libraries. Note you MUST have a setup() and a loop() function but don’t need a main() pinMode( 13, OUTPUT); // sets up PIN 13 digitalWrite(13,HIGH); // puts +5 out on pin 13 You tell the IDE which board and what clock frequency, it works out the right time delay Note that the loop function runs and runs automatically… These programs are called “sketches” and have a .ino extension. There are many libraries – you just ensure they are installed in the right place and they become available You access library functions using a dot ‘.’ Each library has different member functions. This resembles object orientated programming. You access library functions using a dot ‘.’ Most people grab an examlple program, guess what things mean and just modify (hack) the code to want they want. There are 5 tabs on the IDE menu – the Help has “getting started” and “reference” sections Actually the Arduino IDE is built on top of a complex set of software tools given away free by ATMEL. Known as AVRStudio they are analogous to Microchip’s MPLAB used to program PIC microprocessors. You can write normal C in the Arduino environment and it will work, you may need to #include <avr/io.h> and other libraries to make it function correctly. Equally valid is to use the AVRStudio direcly, it is based on the industry standard Eclipse IDE and is easily customisable. It containes a compiler, linker and a make utility which helps automate the building and programming of a hex file. Or you can use a good programming editor such as “The Programmers Notepad”. This can be customised to call the compiler and downloader directly. If you do not use a downloader you can get an AVR hardware programmer for under £20. These use a 3x2 connector which you can see on the photograph of the Arduinos in slide 3 and 4. Interrupts in sketches You might need to read the AVR/ATMEL datasheet carefully to use the timers, but broadly speaking they are similar to the PIC. An Interrupt function in the sketch language initialises the peripheral, uses an “attachinterrupt” function and other similar functions. Note that timer0 is used by the internal delay functions so avoid using it … http://www.instructables.com/id/Arduino-Timer-Interrupts/step1/Prescalers-and-the-CompareMatch-Register/ There are three timers. Timer0 and timer2 are 8 bit timers, meaning they can store a maximum counter value of 255. Timer1 is a 16 bit timer, meaning it can store a maximum counter value of 65535. Once a counter reaches its maximum, it will tick back to zero (this is called overflow). This means at 16MHz, even if we set the compare match register to the max counter value, interrupts will occur every 256/16,000,000 seconds (~16us) for the 8 bit counters, and every 65,536/16,000,000 (~4 ms) seconds for the 16 bit counter. The prescaler can equal 1, 8, 64, 256, and 1024. interrupt frequency (Hz) = (Arduino clock speed 16,000,000Hz) / (prescaler * (compare match register + 1)) Rather than continue with digging up the data, reading the datasheets carefully and using “googled code” we will avoid reinventing the wheel – there is a library we can use. This is the arduino way to get things done but you learn less… look at http://playground.arduino.cc/Main/MsTimer2#.U0HjPfldWSo MsTimer2 is a small and very easy to use library to interface Timer2 with humans. It's called MsTimer2 because it "hardcodes" a resolution of 1 millisecond on timer2. (After downloading the MsTimer2 library from arduino.cc) Unzip it into the libraries folder and restart the IDE This function is treated as an ISR, in our miniproject it can output a multiplex pattern to the digit select lines of the 7 segment displays and poll the columns of the keypad We will want a 5millisecond interval, not a 500 millisecond interval…