8. Embedded programming¶
Weekly Summary¶
- Programming microbit
- Read ATtiny 1614 datasheet
Assignments¶
group assignment:
- compare the performance and development workflows for other architectures
individual assignment:
- read the data sheet for your microcontroller
- use your programmer to program your board to do something
Group Assignment Link¶
Kamakura Group Assignment Week08
Individual Assignment¶
Reading data sheet of ATtiny 1614
Pin Names and pin functions
Arduino IDE pins to physical pin mapping for ATtiny 16x4
8-bit CPU
All AVR devices use the AVR 8-bit CPU. The CPU is able to access memories, perform calculations, control peripherals, and execute instructions in the program memory. Interrupt handling is described in a separate section.
System Clock
If there is no ongoing write operation, the NVMCTRL will enter a sleep mode when the system enters a sleep mode. If a write operation is ongoing when the system enters a sleep mode, the NVM block, the NVM Controller, and the system clock will remain ON until the write is finished. This is valid for all sleep modes, including Power-Down sleep mode. The EEPROM Ready interrupt will wake up the device only from Idle sleep mode. The page buffer is cleared when waking up from sleep.
Voltage range
During periods of low VDD, the Flash program or EEPROM data can be corrupted if the supply voltage is too low for the CPU and the Flash/EEPROM to operate properly. These issues are the same on-board level systems using Flash/EEPROM, and the same design solutions may be applied. A Flash/EEPROM corruption can be caused by two situations when the voltage is too low:
- A regular write sequence to the Flash, which requires a minimum voltage to operate correctly.
- The CPU itself can execute instructions incorrectly when the supply voltage is too low. See the Electrical Characteristics chapter for Maximum Frequency vs. VDD.
Flash, EEPROM, SRAM
Flash
The ATtiny1614/1616/1617 contains 16 KB on-chip in-system reprogrammable Flash memory for program storage. Since all AVR instructions are 16 or 32-bit wide, the Flash is organized as 4K x 16. For write protection, the Flash program memory space can be divided into three sections (see the illustration below): Bootloader section, Application code section, and Application data section, with restricted access rights among them.
SRAM
The 2 KB SRAM is used for data storage and stack.
EEPROM
The ATtiny1614/1616/1617 has 256 bytes of EEPROM data memory, see section 6.2 Memory Map. The EEPROM memory supports single-byte read and write. The EEPROM is controlled by the Nonvolatile Memory Controller (NVMCTRL).
UPDI (Unified Program and Debug Interface)
The Unified Program and Debug Interface (UPDI) is a proprietary interface for external programming and OCD of a device. The UPDI supports programming of Nonvolatile Memory (NVM) space, Flash, EEPROM, fuses, lock bits, and the user row. Some memory-mapped registers are accessible only with the correct access privilege enabled (key, lock bits) and only in the OCD Stopped mode or certain Programming modes. These modes are unlocked by sending the correct key to the UPDI. See the NVMCTRL - Nonvolatile Memory Controller section for programming via the NVM controller and executing NVM controller commands.
The UPDI is partitioned into three separate protocol layers: the UPDI Physical (PHY) Layer, the UPDI Data Link (DL) Layer and the UPDI Access (ACC) Layer. The default PHY layer handles bidirectional UART communication over the UPDI pin line towards a connected programmer/debugger and provides data recovery and clock recovery on an incoming data frame in the One-Wire Communication mode. Received instructions and corresponding data are handled by the DL layer, which sets up the communication with the ACC layer based on the decoded instruction. Access to the system bus and memory-mapped registers is granted through the ACC layer. Programming and debugging are done through the PHY layer, which is a one-wire UART based on a half-duplex interface using the RESET pin for data reception and transmission. The clocking of the PHY layer is done by a dedicated internal oscillator. The ACC layer is the interface between the UPDI and the connected bus matrix. This layer grants access via the UPDI interface to the bus matrix with memory-mapped access to system blocks such as memories, NVM, and peripherals. The Asynchronous System Interface (ASI) provides direct interface access to select features in the OCD, NVM, and System Management systems. This gives the debugger direct access to system information without requesting bus access.
USART (Universal Synchronous and Asynchronous Receiver and Transmitter)
The Universal Synchronous and Asynchronous serial Receiver and Transmitter (USART) is a fast and flexible serial communication peripheral. The USART supports a number of different modes of operation that can accommodate multiple types of applications and communication devices. For example, the One-Wire Half-Duplex mode is useful when low pin count applications are desired. The communication is frame-based, and the frame format can be customized to support a wide range of standards. The USART is buffered in both directions, enabling continued data transmission without any delay between frames. Separate interrupts for receive and transmit completion allow fully interrupt-driven communication. The transmitter consists of a single-write buffer, a Shift register, and control logic for different frame formats. The receiver consists of a two-level receive buffer and a Shift register. The status information of the received data is available for error checking. Data and clock recovery units ensure robust synchronization and noise filtering during asynchronous data reception.
SPI (Serial Peripheral Interface)
The Serial Peripheral Interface (SPI) is a high-speed synchronous data transfer interface using three or four pins. It allows full duplex communication between an AVR® device and peripheral devices, or between several microcontrollers. The SPI peripheral can be configured as either master or slave. The master initiates and controls all data transactions. The interconnection between master and slave devices with SPI is shown in the block diagram. The system consists of two shift registers and a master clock generator. The SPI master initiates the communication cycle by pulling the desired slave’s Slave Select (SS) signal low. The master and slave prepare the data to be sent to their respective shift registers, and the master generates the required clock pulses on the SCK line to exchange data. Data are always shifted from master to slave on the master output, slave input (MOSI) line, and from slave to master on the master input, slave output (MISO) line.
Peripheral
The address map shows the base address for each peripheral. For complete register description and summary for each peripheral, refer to the respective sections.
General Purpose I/O Pin
Peripherals such as USARTs, ADCs and timers may be connected to I/O pins. Such peripherals will usually have a primary and, optionally, one or more alternate I/O pin connections, selectable by PORTMUX or a multiplexer inside the peripheral. By configuring and enabling such peripherals, the general purpose I/O pin behavior normally controlled by PORT will be overridden in a peripheral dependent way. Some peripherals may not override all the PORT registers, leaving the PORT module to control some aspects of the I/O pin operation. Refer to the description of each peripheral for information on the peripheral override. Any pin in a PORT that is not overridden by a peripheral will continue to operate as a general purpose I/O pin.
Program with my programmer¶
Using my FT230 with ATtiny1614 board
Blink
void setup() {
pinMode(2, OUTPUT);
}
void loop() {
digitalWrite(2, HIGH);
delay(1000);
digitalWrite(2, LOW);
delay(1000);
}
Blink and Button
#define LED 2
#define Button 3
void setup() {
pinMode(LED,OUTPUT);
}
void loop() {
if (digitalRead(Button) == LOW) {
digitalWrite(LED, HIGH);
delay(1000);
digitalWrite(LED, LOW);
delay(1000);
} else {
digitalWrite(LED, LOW);
}
}
It was opposite solution…
Light is off when push the button.
Research little bit about Pull up and Pull down resistor from here
Button is connected with Ground. So I should add the 10K Ohm for Pull up resistor.
So I found the spot where I can connect with signal and VCC.
Work property.
I tried my code!!
Morse code right “Hello”
#define LED 2
#define Button 3
void setup() {
pinMode(LED,OUTPUT);
}
void loop() {
if (digitalRead(Button) == LOW) {
//H
digitalWrite(LED, HIGH);
delay(300);
digitalWrite(LED, LOW);
delay(300);
digitalWrite(LED, HIGH);
delay(300);
digitalWrite(LED, LOW);
delay(300);
digitalWrite(LED, HIGH);
delay(300);
digitalWrite(LED, LOW);
delay(300);
digitalWrite(LED, HIGH);
delay(300);
digitalWrite(LED, LOW);
delay(800);
//E
digitalWrite(LED, HIGH);
delay(300);
digitalWrite(LED, LOW);
delay(800);
//L
digitalWrite(LED, HIGH);
delay(300);
digitalWrite(LED, LOW);
delay(300);
digitalWrite(LED, HIGH);
delay(800);
digitalWrite(LED, LOW);
delay(300);
digitalWrite(LED, HIGH);
delay(800);
digitalWrite(LED, LOW);
delay(300);
digitalWrite(LED, HIGH);
delay(300);
digitalWrite(LED, LOW);
delay(300);
digitalWrite(LED, HIGH);
delay(300);
digitalWrite(LED, LOW);
delay(800);
//L
digitalWrite(LED, HIGH);
delay(300);
digitalWrite(LED, LOW);
delay(300);
digitalWrite(LED, HIGH);
delay(800);
digitalWrite(LED, LOW);
delay(300);
digitalWrite(LED, HIGH);
delay(800);
digitalWrite(LED, LOW);
delay(300);
digitalWrite(LED, HIGH);
delay(300);
digitalWrite(LED, LOW);
delay(300);
digitalWrite(LED, HIGH);
delay(300);
digitalWrite(LED, LOW);
delay(800);
//O
digitalWrite(LED, HIGH);
delay(800);
digitalWrite(LED, LOW);
delay(300);
digitalWrite(LED, HIGH);
delay(800);
digitalWrite(LED, LOW);
delay(300);
digitalWrite(LED, HIGH);
delay(800);
digitalWrite(LED, LOW);
delay(3000);
} else {
digitalWrite(LED, LOW);
}
}
Organized dot()
and dash()
#define LED 2
#define Button 3
int shortDelay = 300;
int longDelay = 800;
void dot(){
digitalWrite(LED, HIGH);
delay(shortDelay);
digitalWrite(LED, LOW);
delay(shortDelay);
}
void dash(){
digitalWrite(LED, HIGH);
delay(longDelay);
digitalWrite(LED, LOW);
delay(shortDelay);
}
void H(){
dot();
dot();
dot();
dot();
digitalWrite(LED, LOW);
delay(500);
}
void E(){
dot();
digitalWrite(LED, LOW);
delay(500);
}
void L(){
dot();
dash();
dash();
dot();
dot();
digitalWrite(LED, LOW);
delay(500);
}
void O(){
dash();
dash();
dash();
digitalWrite(LED, LOW);
delay(500);
}
void setup() {
pinMode(LED,OUTPUT);
}
void loop() {
if (digitalRead(Button) == LOW) {
H();
E();
L();
L();
O();
} else {
digitalWrite(LED, LOW);
}
}
Final result!! Add pause()
.
#define LED 2
#define Button 3
int shortDelay = 300;
int longDelay = 800;
void dot(){
digitalWrite(LED, HIGH);
delay(shortDelay);
digitalWrite(LED, LOW);
delay(shortDelay);
}
void dash(){
digitalWrite(LED, HIGH);
delay(longDelay);
digitalWrite(LED, LOW);
delay(shortDelay);
}
void pause(){
digitalWrite(LED, LOW);
delay(500);
}
void H(){
dot();
dot();
dot();
dot();
pause();
}
void E(){
dot();
pause();
}
void L(){
dot();
dash();
dash();
dot();
dot();
pause();
}
void O(){
dash();
dash();
dash();
pause();
}
void setup() {
pinMode(LED,OUTPUT);
}
void loop() {
if (digitalRead(Button) == LOW) {
H();
E();
L();
L();
O();
} else {
digitalWrite(LED, LOW);
}
}
I change the way to connect.
I took the 5V from Serial-UPDI_3-pin to FTDI VCC.
It is simple and looking good.
Appendix¶
Pull up and Pull down resistor
What I learned¶
Data sheet is important to understand what you use. I still take a time to understand. But it’s good to know there is a guid-line.
It is better to combine the programming at the begging. It looks better and easy to use again and different word or somthing.