Embedded programming¶
This week we were tasked with reading through a datasheet and coding in as many platforms as possible
Reading the 412 Datasheet¶
For this week I chose to read throught the 412 chips datasheet. Before I really focused on reading through it, I spent time reading while working with port register coding. The most important sections that I found where section 16 and 16.2. In these sections located on 105, 106, and 107, all things port register are talked through. I found it really helpful becuase it list the potential commands and their explanation. In the large diagram on section 16.2, it shows how the port works. With some simple research from links I have listed below, I was able to undestand it better.
I also found sections 5 and 6 really helpful. These two diagrams which are on pages 12 and 13 show the basic design for the chip and the pinout. This was especially helpful when I was designing my board in the electronics production week. The pins on section 4 tell me which pins I would need for the port register coding. From this diagram I knew I would need pins PA6 and PA7. I was able to convert this to the bianary numbers 01000000 and 00100000. I also did some research on how to read the diagram in section 3. I found this article helpful while reading the rest of the datasheet.
The Board Im Using¶
For this week I used my blink board with a button. I described how I made in in the electronics design week but will shortly redescribe it here. In this design, I used a 412 chip, 1uf capacitor, 4.7k resistor, a botton, and an LED. I wired the led to one output pin and the button to another. Once more, I talk about exactly how I did it in the electronics design week.
Tinker Circuits¶
I started off this week by using a platform that was relatively familar to me. The first thing I had to do was create my circuit. Tinker Circuits doesnt have the 412 chip that I used on my blink board, but it has the very similar ATTiny 45/85 chip. Both have 8 pins and when I checked the data sheets for both, they had a similar port layout aswell. I referenced my schematic that I created in Week 6 and copied that into Tinker Circuits. I tested it using the code that I had used that same week to make sure I had layed everything out before I started off with the blocks. At first I struggled reading the button because I wasnt quite sure if it was on an analog or digital pin. I figured out eventaully that it was on analog pin A3. I also wasnt quite sure how to use the reading from that pin to compare it to the high value of 1. What I ended up doing was using a math operator (green) that compared the two values. I put this in the top of the If/Else statement and then follow it up by using the output features (blue) to set the led pin to high or low.
Arduino IDE¶
I didnt do to much in Arduino this week. My main focus was making sure that I had everything setup properly. The first thing that I needed to do in order to assure everything would work was to install the TinyMega Library. I used this page to get the board manager link and then followed the rest of the instructions on that page to get it setup. I then installed a few other libraries for bluetooth chips that I am using in my final project. I then plugged in my board and uploaded the JTAG.UPDI sketch to an Arduino. I will use this Arduino as a programmer since I cant get my one from the past week to work. The next step was to switch the board to the 412 chip and all of its default settings. I then used the code below to test it out again. Once I knew this was working, I started experimenting with the new Arduino 2.0 Beta. I fell in love with the dark theme and new board manager. It was far easier to switch between board because of the knew layout. I have permanently switched over to using the new Arduino because it also runs far better on my laptop.
void setup() {
pinMode(0, OUTPUT);
pinMode(1, OUTPUT); // I had to set my button as an output since I had done by button wrong
digitalWrite(1, LOW);
}
void loop() {
if(digitalRead(1)==0){
digitalWrite(0, HIGH);
}
else{
digitalWrite(0,LOW);
}
}
Platform.IO through VSC¶
Before this week, I was not familar with alternates to ArduinoIDE. I had heard of other programs like Atmel Studio but was informed by Teddy Warner that it was possible to code through Visual Studio Code using Platform.IO. From my breif research that I did before hand. I learned that you could download it alone or run it through Visual Studio Code. I obviously chose to do it through Visual Studio Code. I first made sure to install it. The proper way to do it is by searching Platform.IO in the extension search bar. It is the first one to come up and once the download button is clicked. It does everything else for you. The next step is to familirize yourself with the interface. It is very easy to navigate and shouldnt be to hard to understand. I then opened up a new project that would hold all my different files for this week. I renamed the first file ‘simple blink’ and ran this code below
Test Blink Code¶
#include <Arduino.h>
void setup() {
pinMode(0, OUTPUT);
}
void loop() {
digitalWrite(0, HIGH);
delay(1000);
digitalWrite(0, LOW);
delay(1000);
}
Once I was thought I knew enough about Platform.IO and had got my blink test working first time, I moved onto incorporating the button. I used the same code from when I did it on arduino as well to make sure everything worked. It once again worked but not after some trouble. An issue that I faced when working through this week is Platform.IO failing to compile. It would only happen once every 10 times I tried to build the code but it was enough to make me look into it. As I looked around, I couldnt find a defenite reason why it would be doing that and since I was able to build the program again, it didnt affect me to much. When I added my button, I needed to make sure that I was programming it appropriately to how I laid out my circuit. Since I wired my button wrong, I had to account for those changes by making my button an output. I was able to use the same digital read and write setup as if it was a normal button thankfully.
Blink and Button Code¶
void setup() {
pinMode(0, OUTPUT);
pinMode(1, OUTPUT); // I had to set my button as an output since I had done by button wrong
digitalWrite(1, LOW);
}
void loop() {
if(digitalRead(1)==0){
digitalWrite(0, HIGH);
}
else{
digitalWrite(0,LOW);
}
}
Port Register Coding through Visual Studio Code/Arduino¶
Before I begin talking about the actual code, I must discuss a major issue that I came across. When I was working on my code, I had been doing it in the ArduinoIDE. For some reason that is still unknown to me, my final working code that I will talk about later did not work in Arduino but did work in Platform.IO. I even went as far to ask a friend to try it to see if it was something with my laptop. It wasnt. I think it narrows down to Arduinos interpretation of the code since it is optimized for its own language but im not sure. I made this discovery after 12 hours of work.
I also spent alot of time looking through the 412 chips data sheet. It has important sections that I really focused on and helped me through this challenge. The most important section was section 16. In this section, all information in regards to the registers in held. Specifically section 16.3.2.1. I found this section especially helpful becuase it speaks to how Port Register coding works. Since I had never experienmented with it, I found this really helpful. I also found section 16.3.2.3 and 16.2 Overview very helpful. Before I began coding, I watched This Video from Sparkfun atleast 4 times aswell as many others that I will link below.
Helpful Videos¶
I started off by copying my code into an Arduino folder, remember, at this point I didnt know that Arduino wasnt going to work. I then went aspect by aspect changing each one to the Port Register code. I found this challenging at first, but since I was on spring break, I spent countless hours working through it. The first thing that I did was replace the void setup. In order to do this, I needed to know which pins I wanted to activate. I did this by referencing the datasheet for the 412 and the SparkFun. I still dont have the best understanding of it and commonly mix up different parts of it. What I do remember is that everything has to be in binary. You activate each individual port by putting a 1 in the bianary port that goes from 0-7 so in my case since I needed to activate ports PA6 and PA7. This meant putting a won in the first and second port. Orignally I had them split up but then realized I could combine them.
void setup() {
PORTA.DIR = B11000000;
}
Once I had the setup running, I really started to struggle. The main issue that came to light was the fact that I made my button wrong. I had my button as an OUTPUT when it is really an input. What I had to do in order to combat that is in the setup, tell the board that it was an output to satisfy it before using it as an input in the loop. This little hack ended up working but just like everything else it took me a while to discover. In my code I found out that I needed to do bitmasking. Out of everything that I did this week, this was by far the most foreign. I still have little clue why it was nessecary but I did find some really good articles that explain it. I have linked them below.
Helpful Articles¶
While working on the loop part, I went through atleast 50 different versions. On my final few attempts I realized that I was going to need to work on the bitmasking part. I ended up changing the signs from ands to ors and vice versa. What I found worked the best was using ors for turning on the leds and then and and statement for turning it off. This is the final combination that I ended up with so I moved onto working that actual conditional. Since I knew everything worked because I had been testing, It was back to looking at the datasheet, past examples, as well as finding new articles. Since everything was so chaotic, I didnt record any of these articles but I known there are some really strong ones out there. For the conditional I found that reading the PINA.IN value and then combining that with the and bitmask gave me the result I wanted and therefore finalized my project.
void loop() {
if (PINA.IN & B00100000){
PORTA.OUT = PORTA.OUT | B10000000;
} else {
PORTA.OUT = PORTA.OUT & B11000000;
}
}
Final Code¶
// Led = 0/6; // I included these to remind me which pins everything was on
// btn = 1/7;
void setup() {
PORTA.DIR = B11000000;
}
void loop() {
if (PINA.IN & B00100000){
PORTA.OUT = PORTA.OUT | B10000000;
} else {
PORTA.OUT = PORTA.OUT & B11000000;
}
}
Video of it working¶
Group Site¶
For this weeks work, I helped setup and test all of the MCU that we used. I had never messed around with a PICO before so setting it up and everything took some time. I was able to reference the users guide if I needed any help. Group Site