09. Embedded programming¶
This week’s assignment was to read a micro controller datasheet and programming the board with as many different languages and environments as possible to do a certain task.
Reading a Datasheet¶
What is a Datasheet of a microcontroller?¶
Datasheets are instruction manuals for electronic components. They explain exactly what a component does and how to use it.
Below is the datasheet of the ATTiny 24/44/84 microcontroller which is used in the board designed by me in the Electronics Design week.
-
The first page is usually a summary of the part’s function and features. This is where you can quickly find a description of the part’s functionality, the basic specifications (numbers that describe what a part needs and can do), and sometimes a functional block diagram that shows the internal functions of the part. This page will often give you a good first impression as to whether potential part will work for your project or not.
-
A pinout lists the part’s pins, their functions, and where they’re physically located on the part for various packages the part might be available in. Note the special marks on the part for determining where pin 1 is (this is important when you plug the part into your circuit!), and how the pins are numbered (the below parts are numbered counterclockwise). You’ll find some acronyms here: VCC is the supply voltage (commonly 5V or 3.3V), CLK is clock, CLR is clear, OE is output enable, etc. These acronyms should be spelled out later in the datasheet, but if not, try Google or Wikipedia. If a pin has a star next to it or a line over the name, that’s an indication that the pin is active low which means that you’ll pull the pin low (0V) to activate it, rather than H (VCC).
-
You’ll also see the more normal recommended operating conditions. These may include voltage and current ranges for various functions, timing information, temperature ranges, bus addresses, and other useful performance information.
-
Some parts will have one or more graphs showing the part’s performance vs. various criteria (supply voltage, temperature, etc.) Keep an eye out for “safe zones” where reliable operation is guaranteed.
-
Truth tables show how changing the inputs to a part will affect its output. Each line has all the part’s inputs set to specific states, and the resulting output of the part. “H” means that input is a logical high (usually VCC), “L” means a logical low (usually GND), “X” means the chip doesn’t care what the input is (could be H or L), and an arrow means that that you should change the state of that pin from L to H or H to L depending on the arrow direction. This is called “clocking” an input, and many chips rely on this for proper operation.
-
Timing diagrams show how data should be sent to and received from the part, and what speed it should be sent / received. These are typically laid out with various inputs and outputs as horizontal lines, showing the logic transitions that happen to those lines over time. If the trace dips down, that’s a L input or output. If the line rises higher, that’s a H input our output. Timing specifications are laid out as arrows between transitions (names are referenced back to timing numbers in the electrical specs), and vertical bars or arrows will link related transitions.
Ref: Reading a datasheet
These are the most imporatnt things that need to be read and understood at this stage.
Since this document contains more than 280 pages, it is very difficult to remember or even refer to it. Luckily, there is a summarised version with all the important and more commonly used data of about 22 pages.
Summarised Datasheet of ATTiny 24/44/48
This summarised data sheet is divided in 10 sections:
- Features
- Pin Configurations
- Overview
- General Informations
- Register Summary
- Instruction Set Summary
- Ordering Information
- Packaging Information
- Errata
- Datasheet Revision History
All this information gave me a fair idea about the kind of projects I can use ATTiny in and what are the advantages and limitations of the chip.
Programming using ATMega328p¶
- The first step is installing the latest Arduino Software. For that use this link.
(Note: Do not use the Windows app as it gives an error while programming)
-
Once the sofware is installed, you need to add the ATTiny Boards Library in the software. For that, visit this link and follow the instructions.
-
Go to File>Preferences and copy the link(http://drazzy.com/package_drazzy.com_index.json) and paste it in the Additional Boards Manager URLs section.
- Now go to Tools>Boards>Boards Manager,
and find the ATTinycore by Spence Konde and install the latest version.
-
After completeing these one-time steps, the process of programming the board can start.
-
The first step to program the board is to burn the bootloader onto the FabISP. It is a firmware that allows you to install new firmware without the need of another programmer.
-
The following equipment were used for the same:
-
Programming Device(FabISP)
-
Board you want to programme(Here, I’ve used one with ATTiny44 microcontroller)
-
Jumper Wires
-
(Note : Make sure you have the schematic of the board you’re programming on hand)
- Now it’s time to connect all the equipments to start programming. Keep the board layout and programming board Pin-out open and connect the jumper wires accordingly.
Thi board diagram can be referred from below image:
- The connections were made as shown below.
-
For using any board, you first need to burn the bootloader onto that board before uploading it on your ATTiny44 board. For that select USBTinyISP as your programmer and click on burn bootloader.
-
To start testing your board you can use a simple default code to test. Here, I’ve used a blink code. Go to file>Examples>Basics>blink. This will open the blink code in a new window.
- Go to tools>Boards and change it to ATTiny 44.
- Change the Clock to External:20MHz
- Upload the sketch using a programmer and you should see the LED on the board blinking. Here is the code and the video.
void setup() { // initialize digital pin LED_BUILTIN as an output. pinMode(LED_BUILTIN, OUTPUT); } // the loop function runs over and over again forever void loop() { digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level) delay(100); // wait for a second digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW delay(100); // wait for a second }
- You can change the speed of blinking by changing the delay time.
void setup() { // initialize digital pin LED_BUILTIN as an output. pinMode(LED_BUILTIN, OUTPUT); } // the loop function runs over and over again forever void loop() { digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level) delay(1000); // wait for a second digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW delay(1000); // wait for a second }
Programming the button¶
-
The assignment this week was to programme a button to do something.
-
I decided to make the LED blink only when the button is pressed.
Below is the code for it, with explaination for what each line does.
#include <SoftwareSerial.h> // Include the software serial library SoftwareSerial mySerial(5,6); // Defining the RX, TX Pins int led = 3; // LED Pin int bt = 2; // Button Pin int button = 0; // Button variable void setup() // Initialize digital pin { mySerial.begin(9600); pinMode(led, OUTPUT); pinMode(bt, INPUT); // Defining Input and Output } void loop() // This function starts a never ending loop of functions { button = digitalRead(bt); // Initialize the reading of input mySerial.println("BT value : "); // Prints 'BT Value' mySerial.println(button); // Prints the value of BT if (button == HIGH) // Sets the condition for output { digitalWrite(led,HIGH); // Function to perform if the condition is fulfilled delay(100); // Sets delay digitalWrite(led,LOW); // Function to perform if the condition is fulfilled delay(100); // Sets delay } }
The video¶
Progamming the button to toggle the LED On/Off¶
-
In earlier method, we programmed the LED to turn on when the button is pressed. Here, the the button will toggle the state of LED. (The LED will stay on until the button is pressed for the second time)
-
Since, this was my first time with programming, I referred to certain examples on how this can be achieved. These are the references I took:
-
Using the above references, I wrote a code to turn the LED on/off using button as toggle switch. Below is this code:
int stateNow = 0; int stateBefore = 0; int i = 0; int j = 0; void setup() { pinMode (2, INPUT); p inMode (3, OUTPUT); } void loop() { stateNow = digitalRead(2); if (stateNow != stateBefore) { if (stateNow==HIGH and i==0) { digitalWrite (3, HIGH); j=1; } else if (stateNow==LOW and j==1) { i=1; } else if (stateNow==HIGH and i==1) { digitalWrite (3, LOW); j=0; } else if (stateNow==LOW and j==0) { i=0; } } stateBefore=stateNow; }
The Video¶
Thus, we’ve programmed the button on the board. We’ll move to adding more inputs and output in the boards and adding more complex programming.
All the files for this week are attached here
Group Work¶
The Group Assignment was to compare the performance and development workflows for other architectures. The group page can be found here