THE MICROCONTROLLERS

Presented by: Forest Shick, WA2MZG Buyer Beware

$0.95

Is it worth it? Qualifications

Did you ever wonder Why is this person presenting? I do!! Single Board Computers

Beagleboard.org Raspberrypi.org Asus.com/us/single-board- computer/tinker-board Other Microcomputer Modules

Pjrc.com/teensy/

3.2 – 72MHz Cortex M4 3.6 – 180MHz Cortex-M4F 4.0 – 600 MHz ARM Cortex M7 NANO – 16MHz 8 bit

Teensy 3.2 Teensy 4.0

Arduino NANO Teensy 3.6 What is an Arduino? Other Arduino Modules Arduino Shields What is an Arduino good for?

• Weather station • Station control / accessories • Rotor controller • Keyer • Station 24 Hr Clock • Home automation • Use your imagination • Robot • Steppingstone to the • Light Controller and other more • Simple Security System powerful devices • Homebrew test equipment • Having fun • Game design – remember • Saying – “I DID THAT”!! Simon? Prototyping Projects – Direct Conversion Receiver

NANO controls the LCD and the DDS – the VFO in this direct conversion receiver Projects

First steps in an SSB transceiver. This uses an UNO. Projects – Morse Code Tool

The Arduino MEGA was used because the SD card required a lot of memory space – program and RAM

Details are in a previous issue of the RAG Let’s Get Started

• To be fair – there is more relevant material than we can cover in 1 hour • We will start with an overview of the NANO • We will investigate the more common / easy to use features first • Initialization • Digital IO • Analog Conversion • As time permits, we will do a 10,000 ft view of as many features as possible Comparison

UNO NANO MEGA Microcontroller ATmega328P ATmega328 ATmega2560 Digital IO 14 22 54 Analog IO 6 8 16 PWM 6 6 15 UART 1 1 4 Flash 32K 32K 256K RAM 2K 2K 8K EEPROM 1K 1K 4K Clock 16MHz 16MHz 16MHZ LED_BUILTIN 13 13 13 Pins 28 32 100 Microcontroller Block Diagram

ATmega328 ATmega2560 ATmega328 Data Sheet

BUT Arduino has done a great job of getting you started There are 294 pages in without having to depend this data “book” on this book. At some level of The Arduino web site has project complexity much to offer if you have many of these pages the time to look through it. will be important to your project TAKE THE TIME

NOTE: I have not found a hardware description – it could be there somewhere! AVR CPU Core and Sample of Instruction Set NANO / ATmega328 Features

• 32K bytes of • 8 channel, 10 bit A/D • 1K bytes of EEPROM • Internal temperature measurement • 2K bytes of RAM • USART (UART) • Two 8 bit Timer / Counter • SPI • Prescaler • I2C / IIC / TWI • Output Compare • Watchdog timer and oscillator • One 16 bit Timer / Counter • Analog Comparator • Prescaler • Interrupt • Input Capture • Output Compare • Pin Change Interrupts • Real time counter and oscillator • 23 Programmable IO lines • Six Pulse Width Modulator channels Shared Pin Functionality

As you can see there are only 32 pins, 7 of which are power and ground. Leaving only 25 for all the SHARED hardware functions See the multiple functions for each pin. Arduino UNO Schematic Arduino MEGA Schematic NANO Schematic NANO Pin Out

Notice the multiple functions assigned to each pin

POWER - red GND - black Discussed on the next slide

Be aware of the multiple numbering systems. The VIOLET numbers are the names of the digital IO. The GREEN letter number combination are the analog inputs. The GRAY numbers are the pin number of the microcontroller. Powering the NANO

• For development, when you connect the NANO to the USB port on your computer, the board is powered from the USB port – shown by the blue circles • External power may be applied to the Vin pin, red circle. It is then regulated to 5V to power the board and accessories • 5 volt power, violet circle, may also be applied to the 5V pin to power the board and accessories • The 3.3V pin, light blue is NOT power input. It is power output not to exceed 50ma. NANO Pin Functions Arduino NANO Conclusion Initial Conditions

• Most pins are tri-state during reset • Many registers provide the initial condition of the pins • Properly terminate unused pins • DDR – 0 = Input and 1 = Output • WHY? Digital IO – Chapter 13

Each of the 14 digital pins on the Nano can be used as an input or output, using pinMode(), digitalWrite(), and digitalRead() functions. They operate at 5 volts. Each pin can provide or receive a maximum of 40 mA and has an internal pull-up resistor (disconnected by default) of 20-50 kOhms. In addition, some pins have specialized functions • Circles • RED – pull up resistor • BLUE – output • GREEN - input

• Squares • RED – Pull up disable signal • BLUE – reads the state of the port output • GREEN – reads the state of the port input • NOTE – the port output and port input may not be the same Digital IO

pinMode(8, OUTPUT); // Make D8 an output • pinMode(pin, mode) • pin 0 – 22 (D0 - D22) In C • mode: INPUT, OUTPUT, INPUT_PULLUP DDRB = (1<< DDB0); // Set DDRB0 to 1 (output) • digitalRead(pin) • Returns the state of the pin digitalWrite(8, 1); // set D8 to 1 • digitalWrite(pin, value) • Value – write a 0 or 1 to the pin In C PORTB = (1 << PB0); // set PORTB0 to 1

Registers • DDxn – Data Direction, Port x, Bit n • PORTxn – Data output port • PINxn – Data input port Digital IO Examples

Turn a LED on and off Read a push button switch Drive and read a 4 x 4 keypad

pinMode(4, OUTPUT); pinMode(5, OUTPUT); pinMode(6, OUTPUT); pinMode(7, OUTPUT); pinMode(8, INPUT_PULLUP); pinMode(9, INPUT_PULLUP); pinMode(10, INPUT_PULLUP); pinMode(11, INPUT_PULLUP);

PORTD = 0xf0;

val = PINB & 0x0f;

pinMode(8, OUTPUT); pinMode(5, INPUT_PULLUP); digitalWrite(8, 1); sw = digitalRead(5); A/D Converter Inputs – Chap 23

The Nano has 8 analog inputs, each of which provide 10 bits of resolution (i.e. 1024 different values). By default they measure from ground to 5 volts, though it is possible to change the upper end of their range using the analogReference() function.

Analog pins 6 and 7 cannot be used as digital pins. Additionally, some pins have specialized, alternate functionality:

I2C: A4 (SDA) and A5 (SCL). Support I2C (TWI) communication

Analog Input Resistance: 100M ohms Reference Input Resistance: 23k ohms A/D Converter

• analogReference(type) • DEFAULT – 5V power • INTERNAL – 1.1V • EXTERNAL – voltage on the AREF pin

• analogRead(pin) • A0 – A7 (ADC0 – ADC7) analogReference(DEFAULT); // optional . . int value; . . // Measure the voltage on ADC1 value = analogRead(A1); A/D Converter

Reading a potentiometer Reading a temperature sensor

int val;

int val; analogReference(EXTERNAL);

val = analogRead(A0); val = analogRead(A6); Alternate Port Functions

• Clock oscillator • Timer Oscillator • Pin change interrupt • SPI • Output Compare • Input capture • Reset • A/D Converter • IIC / I2C / TWI • Analog Comparator • USART • External clock • External Interrupt Analog Comparator – Chap 22

The Analog Comparator compares the input values on the positive pin AIN0 and negative pin AIN1. When the voltage on the positive pin AIN0 is higher than the voltage on the negative pin AIN1, the Analog Comparator output, ACO, is set. The comparator’s output can be set to trigger the Timer/Counter1 Input Capture function. In addition, the comparator can trigger a separate interrupt, Uses exclusive to the Analog • Period / frequency measurement Comparator. The user can select • Voltage threshold Interrupt triggering on comparator • Phase measurement output rise, fall or toggle. Communications

USART – Chap 19 SPI – Serial Peripheral Interface – Chap 18 • Full duplex • Full duplex • Baud rate generator • Master or slave • 5 – 9 data bit and 1 or 2 stop bits • 7 programmable bit rates • More • MISO – D12 – Master in / Slave out • MOSI – D11 – Master Out / Slave in • The NANO and UNO only have 1 • SCK – D13 – Serial Clock • It is assigned to the USB comms • SS – D10 – Slave Select

• D0 – RX • Use the SPI library • D1 - TX TWI – 2 wire Serial Interface – Chap 21 • Also known as IIC & I2C • SCL – A5 • SDA – A4

• Use the Wire library 8 Bit Timer / Counter – Chap 14 & 17

• There are 2, 8 bit timer / counters • Output Compare • PWM • Frequency Generation

OUTPUT COMPARE • A value can be set in the OC register • When the counter reaches that value a port pin will change state

PULSE WIDTH MODULATOR • Creates a repetitive waveform with a duty cycle set by an 8 bit word 16 Bit Timer / Counter – Chap 15

• One 16 bit timer counters • Output Compare • Input Capture • PWM • Frequency generation • Event counting Interrupts – Chap 11

There are 26 sources of interrupts

What is an interrupt? • An interrupt changes the normal flow of a program. • An interrupt is usually caused by an asynchronous process that requires attention • When an interrupt occurs, the current instruction is completed and control jumps to a special interrupt routine • When the interrupt routine is complete, the next instruction, in the main program, is executed

• The only interrupt that you have no control over is RESET • All other interrupts may be enabled or disabled • All interrupts are disabled at reset External Interrupts – Chap 12

• External interrupts are triggered from a source outside of the microcontroller • There are 2 external interrupts, INT0 & INT1, which are addressed using the IDE functions • External interrupts can be triggered by: • Low level • Logic change • Falling edge • Rising edge • There are 23 pin change interrupts that the IDE functions DO NOT handle : PCINT0 to PCINT23 (No PCINT15) • Pin change interrupts are triggered by: • Rising and falling edges • You don’t get to pick which one Internal Interrupts

The internal interrupts are triggered by actions of the internal peripherals – although external stimulus may be involved – receiving data on the TWI – the interrupt occurs because 8 data bits were transferred on the TWI lines

There are 20 internal interrupts

Beyond the scope of our discussion today

They are extremely useful but require studying the appropriate sections of the manual. Timer / Counter and Pin Change Interrupt Example

• D14 & 15 Use PCINT to detect key down and up – triggering a timer for dot and dash length • D11 & 12 use PCINT to detect rotation of the rotary encoder and direction • A11 uses PCINT to detection pushbutton action • A8 uses a timer for tone generation • A10 use PCINT and a timer to detect and decode incoming Morse code

• TWI is used to communicate with the LCD

• SPI is used to read and write the SD Card Resources

• Information & pictures are taken from the following: • Arduino web site • ATmega data sheet • Various advertiser in a Google search • Adafruit