9. Embedded programming¶
Assignments¶
Individual assignment: ~~read a microcontroller data sheet~~ ~~program your board to do something, with as many different programming languages and programming environments as possible~~
Group assignment: ~~compare the performance and development workflows for other architectures~~
Have you: ~~Documented what you learned from reading a microcontroller datasheet.~~ What questions do you have? What would you like to learn more about? ~~Programmed your board~~ ~~Described the programming process/es you used~~ ~~Included your code~~
Review a datasheet¶
The ATtiny44 is a popular microcontroller. Its datasheet is 289 pages long.
The need-to-know Features of the ATTiny44:
The ATTiny44 is a high Performance, low Power 8-bit microcontroller with advanced RISC architecture. Features: 1. 4K Bytes of Flash Program Memory 2. 256 Bytes of Programmable EEPROM 3. 256 bytes SRAM 4. 12 general purpose i/o lines 5. 32 general purpose working registers and 8-channel 10-bit ADC. 6. Internal calibrated oscillator 7. Operating voltage supply between 1.8 ~ 5.5V 8. Two kinds of ports: Port A pins are 8-bit bi-directional I/O port with internal pull-up resistors (pin names are from PA0 to PA7) and Port B pins are 4-bit bi-directional I/O port with internal pull-up resistors(pin names are from PB0 to PB3).
Pin configuration: Need to know for building the board
VCC: voltage in
RESET: This pin makes the program to start again
INT: interruption
MOSI: (Master Output Slave Input): Master data output and data input to the Slave
MISO: (Master Input Slave Output): Slave Data Output and input to Master
SCLK: it’s the pulse that establishes the synchronization. With each of this pulse of the clock, it reads or sends a bit.
ADC: Analog to Digital convertor for entrance pins.
OC: it’s the same as PWM (Analog simulation for exit pins)
PORTA: Port A is a 8-bit bi-directional I/O port with internal pull-up resistors. The pullup resistors are resistors inside the microcontroller that you can activate.
Embedded Programming Week Work¶
For this week, I started with Neil’s HelloFTDI_echo board. I added two LEDs and a button to it. If all goes well, they will be a duet of LEDs.
Resistor Research¶
To determine how much resistor I need for my LEDs, I use Ohm’s law. R=V/c
The data sheet tells me that the forward voltage on the blue LED is 3.2 and the continuous forward current is 30mA. That comes out to 106ohms. I have a 100 ohm resistor that will work.
The [data sheet for the green LED] (https://media.digikey.com/pdf/Data%20Sheets/Everlight%20PDFs/QTLP650(C,D)-2,3,4,7,B.pdf) says that the forward voltage is 2.8 and forward current is 30mA and that comes out to 96ohms, so that 100 ohm resistor should work for that, too.
I have a 499 with a blue LED I have on another board but I have noticed that Neil uses a 1k with his LEDs.
Design in Eagle Software¶
For my board design, I start with the HelloFTDI_echo parts list:
- ATtiny44
- 1uF capacitor
- 10k resistor
- XTAL resonator
- 2x3 pins for ISP
- FTDI connection
I add my:
- 2 LEDs
- 2 499 resistors
- 1 button
I put all of the parts into my schematic.
The fab library has many of the parts we need for making our boards in this course. They constructed some interesting things to satisfy Eagle’s requirements. The AVRISP part is an example where someone who created this library “named” this part “VALUE” and made the “value” of this part AVRISPSMD (which stands for AVR (the type of microchips we use) ISP (the pins we will use for programming) and SMD (a surface-mounted part). Because the “value” is filled in, ERC will not complain that this part has no designated value. It’s pretty clever.
I change the name to J1=ISP on my board. Then I turn off “show” for the value.
Capacitor¶
The capacitor is always connected to ground and vcc. His job is to store an electrical charge. He has, on either ends, two conductors (known as plates) and an insulator in between them (called the dielectric). The two plates inside a capacitor are wired to two electrical connections on the outside called terminals. We solder them onto two different pads - one connected to VCC and the other to GND. The amount of electrical energy a capacitor can store depends on its capacitance. The size of a capacitor is measured in units called farads (F) (named after Michael Faraday, a 19th century inventor. A farad is a very large number so we talk about capacitance in terms of microfarads. A millionth of a farad is written uF. A nanofarad (thousand-millionths of a farad) is written nF. A million-millionth of a farad is written pF and is referred to as a pico-farad. In my work, I frequently use a 1 uF capacitor. I don’t particularly like working with them because, unlike a resistor which is flat, capacitors are brick-shaped and tend to roll around when I’m trying to solder them.
On this board, the capacitor is in-between where voltage comes into the board from the FTDI and where it goes into the VCC on the micro controller. That 10k resistor is also in the path to protect the micro controller from too much of the 5 volts coming from the FTDI.
ATTiny44’s data sheet says that its operating voltage is 1.8 to 5.5V¶
I need a 10k resistor between VCC and the RESET pin (pull up resistor for the reset pin), to prevent too much current from flowing into the RESET pin and burning out the IC. This resistor through to the reset pin is advised if you are using debugWIRE through JTAG, which I am through my ATMEL ICE programmer.
Resonator (crystal)¶
20MHz resonator – provides an external clock signal to the microcontroller.
Connections and Pins¶
I’m going to put the button on the right side of the board, so I connect it to pin PA2 on the right in the middle. Back to the data sheet for the tiny44 to check if I can use that pin for the button.
Port A Pins Alternate Functions
PA2
- ADC2: ADC Input Channel 2
- Analog to Digital Converter, Channel 2
- AIN1: Analog Comparator, Negative Input
- PCINT2: Pin Change Interrupt 0, Source 2
So I can use that pin for input from the button.
I’m going to put the LEDs and their resistors on the left side of the tiny44, so I’m assigning them to output duty at PA7 and PB2, next to each other.
PA7
- ADC7: ACD Input Channel 7: Analog to Digital Converter, Channel 7
- PCINT7: Pin Change Interrupt 0, Source 7
- ICP: Time Counter 1 Input Capture Pin
- OC0B: Timer/Counter0 Compare Match B Output
PB2
- INTO: Exterminal Interrupt 0 Input
- OC0A: Timer/Counter0 Compare Match output
- CKOUT: System Clock Output
- PCINT10: Pin Change Interrupt 0, Source 10
I can use these two pins for LED output.
I ask ERC if he sees any problems so far.
The report is clear.
BRD View¶
When I was finished setting up the board in the schematic, I pushed it over to the BRD view.
I put the 6pin below and a bit to the right of the tiny44. The button and the FTDI go on the right of the board.
That’s when I remembered to change my GRID setting to 2 (instead of the default 50). Then I could move my components precisely.
The capacitor and the 10K resistor go on top, lined up for easy access to the pins they need on the tiny44. The XTAL crystal always goes by PB0 and PB1 on the tiny. I put the extra LEDs and their 499 resistors below the XTAL.
After the components are in their correct positions, I set my Design Rules.
I want the Clearances at 16. (Not the default 6.) This keeps the traces sufficiently far away from the pads and other traces so that they don’t get merged together in the milling.
I set the width of the traces to no less than 12. That way they are not too fragile and likely to break.
I set the traces to 12 in the primary BRD screen
and start by drawing the obvious connections between the LEDs and their resistors.
I have to rotate them to get them to the right places. I also had to move them away from each other a bit, so I use “Move Group”. I think that this is not an intuitive set of keystrokes. First I have to use the “group” command to grab the two components I’m going to move. Then right click on the group to access the “move group” option.
Now they are in the right places.
I drew a universal GND trace going to the left of the board and a universal VCC on the right.
Because I knew where everything had to go, I didn’t have much trouble with the traces.
To prepare for milling, I used “Hide Layers” and then un-hid the top layer. I exported at 500 dpi, monochrome checked. To export the interior, I hid the Top layer and just showed Dimensions. Export at 500 dpi, monochrome.
Inkscape¶
In Inkscape, I:
- Object —> Transform —> Scale to reduce the size to 52%.
- Use the “resize page to drawing” command to make the box the same size as my image
- I didn’t like what the words looked like (afraid they will be too fragile in milling) so I covered over them and wrote EMBED PROG in bold, larger type.
- Export the file as 1000 dpi as a png file
Then I did the same reduce and resize process with the “interior” file and exported that, as well.
Milling¶
I put the two png 1000 dpi files in “mods” and downloaded them as rml files.
Here’s what the rml rendering looks like.
I used the Roland SRM-20 to mill the board. It looks pretty good! The letters came out very clear.
Soldering¶
I made this video of soldering the components onto the board. I typically start with the microcontroller and then the crystal. I put the 6 pin and FTDI on last. I check that the connections are good with the multimeter along the way. If something is not connected well, I want to know that before I move on to another component.
Writing Code in C¶
Using Neil’s code as a sample, I wrote this tiny code called turnLEDon using Brackets software:
I modified one of Neil’s make files so that it looks like this. Notice the PROJECT= I can keep using this same make file over and over just changing the name of the project.
PROJECT=turnLEDon
SOURCES=$(PROJECT).c MMCU=attiny44 F_CPU = 20000000 CFLAGS=-mmcu=$(MMCU) -Wall -Os -DF_CPU=$(F_CPU) $(PROJECT).hex: $(PROJECT).out avr-objcopy -O ihex $(PROJECT).out $(PROJECT).c.hex;\ avr-size --mcu=$(MMCU) --format=avr $(PROJECT).out $(PROJECT).out: $(SOURCES) avr-gcc $(CFLAGS) -I./ -o $(PROJECT).out $(SOURCES) program-bsd: $(PROJECT).hex avrdude -p t44 -c bsd -U flash:w:$(PROJECT).c.hex program-dasa: $(PROJECT).hex avrdude -p t44 -P /dev/ttyUSB0 -c dasa -U flash:w:$(PROJECT).c.hex program-ice: $(PROJECT).hex avrdude -p t44 -P usb -c atmelice_isp -U flash:w:$(PROJECT).c.hex
Putting the Code in the ATtiny44¶
In Parallels…Ubuntu…Terminal, I create a folder called turnLEDon and put both the make and the c file in there. I open that folder in Terminal and use sudo make -f turnLEDon.make
to create the .hex and .out files.
Then I use sudo make -f turnLEDon.make program-ice to program the tiny44 with my tiny LED program.
Terminal reported back:
parallels@parallels-Parallels-Virtual-Platform:~/Desktop/turnLEDon$ sudo make -f turnLEDon.make program-ice [sudo] password for parallels: avr-objcopy -O ihex turnLEDon.out turnLEDon.c.hex;\ avr-size --mcu=attiny44 --format=avr turnLEDon.out AVR Memory Usage Device: attiny44 Program: 66 bytes (1.6% Full) (.text + .data + .bootloader) Data: 0 bytes (0.0% Full) (.data + .bss + .noinit) avrdude -p t44 -P usb -c atmelice_isp -U flash:w:turnLEDon.c.hex avrdude: usbdev_open(): WARNING: failed to set configuration 1: could not set config 1: Device or resource busy avrdude: AVR device initialized and ready to accept instructions Reading | ################################################## | 100% 0.02s avrdude: Device signature = 0x1e9207 (probably t44) avrdude: NOTE: "flash" memory has been specified, an erase cycle will be performed To disable this feature, specify the -D option. avrdude: erasing chip avrdude: reading input file "turnLEDon.c.hex" avrdude: input file turnLEDon.c.hex auto detected as Intel Hex avrdude: writing flash (66 bytes): Writing | ################################################## | 100% 0.29s avrdude: 66 bytes of flash written avrdude: verifying flash memory against turnLEDon.c.hex: avrdude: load data flash data from input file turnLEDon.c.hex: avrdude: input file turnLEDon.c.hex auto detected as Intel Hex avrdude: input file turnLEDon.c.hex contains 66 bytes avrdude: reading on-chip flash data: Reading | ################################################## | 100% 0.54s avrdude: verifying ... avrdude: 66 bytes of flash verified avrdude: safemode: Fuses OK (E:FF, H:DF, L:62) avrdude done. Thank you.
Yes! The green LED at PB2 turned on.
Blink¶
I changed the code to make the light blink.
This creates a delay for turning the LED on and off. The LED is still at pin PB2.
I made a new folder and put the makeLEDblink.c file and make file in it. I went through the same process of making the .hex and .out files and then programmed the tiny44 using:
sudo make -f makeLEDblink.make program-ice
Here’s a little video of that LED blinking.
I wanted to be sure that the second LED worked, too. I had a lot of trouble with the blue LED so I changed it to orange. I had to change the code because the orange LED is connected to a different pin on the tiny44. I changed the code to PA7 where it is connected. Then I changed DDRB to DDRA and changed PORTB to PORTA.
I went through the same process of making a new folder for the changed c and make files.
Then I made new .hex and .out files and sent them to the tiny44 with the same sudo make command as above.
Yes! It turns on.
So, I went through the same commands to make it blink, as well. Here’s a little video of it blinking.
Turn Both Lights On At Once¶
To turn on both LEDs at the same time, I had to change the code to specify two different LEDs
Here’s a picture of the two of them on at the same time.
Make Both Blink¶
To make them both blink, I wrote this, using what I learned from the code I just wrote and experimenting with where the brackets go.
Here’s a little video of the duet blinking.