This week, I tried programming my echo hello-world board. I have had a very few experience in programming so it was challenging but I managed to make my board working.
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: experiment with other architectures
Learning outcomes
Identify relevant information in a microcontroller data sheet.
Implement programming protocols.
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
Reading a microcontroller datasheet
One of the assingments of this week was reading Atmel datasheet to work with ATTiny. It was very confusing for me as a novice in this field, and I lost so many times. Probably it will be easier to understand what is written in datasheet after I get more familiar with principle and basic idea and concept of microcontrollers and programming.
In datasheet, I mainly read Chapter 1: Pin configuration, 2: Overvew of ATTiny, 3: General information and 10: Input/ Output ports.
Here are what I found from the datasheet.
AVR is a microprocessor family developed by Atmel. It is based on modified Harvard architecture 8-bit RISC (reduced instruction set computer).
ATTiny44A is a low-power CMOS (complementary metal-oxide-semiconductor) 8-bit microcontroller based on the AVR enhanced RISC architecture. It has 32×8 general purpose working registers and 12 programmable I/O lines. By default, ATTIny44A has 8MHz internal clock.
ATTiny44A has two ports: port A and port B. Each pins on the ports has alternate function.
Port B (PB3:PB0) is 4-bit bi-directional I/O port with internal pull-up resistors. PB3 has RESET function. To use pin PB3 as an I/O pin, instead of RESET pin, program (‘0’) RSTDISBL fuse. To drive ATTiny44A with external clock, add resonator or crystal and connect it to PB0 and PB1.
Port A (PA7:PA0) is a 8-bit bi-directional I/O port with internal pull-up resistors. 8 bits means there are 256 ways of signal in binary(0:255). Port A has alternate functions as analog inputs for the ADC (analog to digital converter).
Pinout of ATtiny24A/44A/84A from Atmel datasheet p.2 Figure 1-1
Port A Pins Alternate Functions from Atmel datasheet p.60 Table 10-3
Port B Pins Alternate Functions from Atmel datasheet p.64 Table 10-7
Input/ Output ports.
The function of each I/O line (4 in port B and 8 in port A) is determined by three registers (DDRx, PINx, PORTx). In C code, they are shown like:
DDRB = DDRB | (1 << PB2)
// PB2 as output by setting bit 2 (pin 2) in register DDRB (= 0b00000100)
Register DDRx determines the direction of the signal. It decides whether the pin works as Input or Output.
When the pin is determined as Input pin, register PINx sets the voltage HIGH (1) or LOW (0) of input signal.
When the pin is determined as Output pin, register PORTx sets the voltage HIGH (1) or LOW (0) of output signal.
When the pin is determined as Input pin, register PORTx determines whether the pull-up resistor is activated or not.
After I read datasheet, I got many questions. There are many things I would like to know more/ I did not understand. For instance how a signal is processed physically in a microprocessor, how do microcontrollers differ when they have different architecture. These questions will be answered when I actually start using them, I believe.. This is Learning by Doing :)
Programming echo hello-world board
I programmed my board made in week 7. In week 7, I had a problem to program my board. This is the process I did in week 7. I followed this instruction and this documentation for programming on Mac. As AVR development toolchain, I’ve already installed Crosspack in Week 5 to program my programmer. I opened Terminal, changed directory to fts_firmware_bdm_v1 where my AVR development toolchain is.
$ cd fts_firmware_bdm_v1
I downloaded the following files and saved it in the same folder:
// compile .c file so that ATTiny understands commands.
Connect the programmer and echo hello-world board. I prepared ISP connecter. I put USBtiny to USB Hub (2.0 port) and connected with Mac. I made sure it is recognised by checking USBTiny listed in Utilities> System information> Hardware> USB. Then I connected the programmer with echo hello-world board. Make sure the orientation of the boards so that the same ISP pins on each board are connected. I connected echo hello-world board and Mac with 5V FTDI-USB-TTL-232-cable to supply power.
I typed the following command to set up the board for programming.
$ make -f hello.ftdi.44.echo.c.make program-usbtiny-fuses
// set configuration of the board for programming.
Then I ran the following command to upload the code.
$ make -f hello.ftdi.44.echo.c.make program-usbtiny
// upload code
I removed the programmer. I typed the following command to install pyserial to use term.py file which allows to communicate on the serial monitor.
$ sudo python -m pip install pyserial
// install pyserial
I ran the command to run python application. I clicked tab key after “tty.”to find the right USB port.
The problem was here. When I try to test python serial monitor, even python application window opened, but it did not do anything. The command stopped and did not finish. I realised that I didn’t have Python environment. I downloaded the latest version of Python for OSX from here. I tried again but it still did not work.
I started debugging.
First, I borrowed the board from Marta Cortés to test with her board. It worked fine. So the problem is on my board.
Second, I checked conductivity of each part with multimeter again. All the parts were fine.
I carefully looked and compared the board, schematic and the board layout of mine and Neil’s. Then I realised that I somehow changed the serial in/ out pins on ATTiny 44 from Neil’s example.
In the example:
TXD pin on FTDI which computer sends information is connected PA0 (pin13)
RXD pin on FTDI which computer receives information is connected PA1 (pin12)
But my board:
TXD pin on FTDI which computer sends information is connected PA1 (pin12)
RXD pin on FTDI which computer receives information is connected PA0 (pin13)
Neil's board layout
My board
In hello.ftdi.44.echo.c file, the pins are defined as:
Since serial in/ out pins were opposite, information was not transferred from computer to ATTiny. That was why they couldn't communicate. I referred to this website, and changed the pins as below:
I opened C file in Brackets and changed command lines. I changed the pin of the push button. In this code, the author used three LEDs but I had only one. So I deleted then commands for two LEDs. I wanted to make LED blinks when the button was pressed. I changed the pin number of the button, and turn off pull-up resister:
* First the existing code (pull-up resister activated) did not work. But it worked when I turn off pull-up resister. Later, I found the reason the code did not work when I activated the internal pull-up resistor. I had an external pull-up resistor on button (see week 7). Pull-up resister is located between input pin and VCC. When the button is not pressed (the current is not connected and doesn't go to the ground), the current (5V) is floating between input pin and VCC (HIGH). Input pin recognises 5V. So with this pull-up resister, the default value is HIGH. When the button is pressed, the circuit is connected to the ground and the current goes to the ground (LOW). When the button is not pressed, the voltage of the resistor (R5) between external resistor (R3) and internal resistor in ATTiny input pin is 5V. When the button is pressed, the voltage is divided into two resistors (digital divide). That was why the button did not work when the internal pull-up resistor was activated. So I should not activate internal pull-up resistor on this pin.
Schematic of the board
Digital divide
Without internal pull-up resistor
With internal pull-up resistor (R3)
With internal pull-up resistor (R5)
I connected programmer and board with ISP connecter and board to the computer with FTDI cable to supply power. I typed following commands on Terminal.
$ cd fts_firmware_bdm_v1
// change directory
$ make -f att44_schaad1.make
// compile .c file so that ATTiny understands commands
$ make -f att44_schaad1.make program-usbtiny-fuses
// set configuration of the board for programming
$ make -f att44_schaad1.make program-usbtiny
// upload code
Code was successfully uploaded.
When the button is pressed, LED blinks
LED blinks three times by pushing button
The code I used had many extra commands for my simple board and it was confusing to edit. So I tried simple code. I downloaded the files (blink_button.c, blink_button.make) created by Iván Sánchez Milara which only has a LED and button which is the same with my board.
I changed the pin for button from pin7 to pin3 as I have on my board.
I saved the file as blink_button1.c. I changed project name in Make file to blink_button1.
I connected programmer and board with ISP connecter and board to the computer with FTDI cable to supply power. I typed following command on Terminal.
$ cd fts_firmware_bdm_v1
// change directory
$ make -f blink_button1.make
// compile .c file so that ATTiny understands commands
$ make -f blink_button1.make program-usbtiny-fuses
// set configuration of the board for programming
Then I ran the following command to upload the code.
$ make -f blink_button1.make program-usbtiny
// upload code
Code was successfully uploaded.
Next I modified the code. I made the LED blinks three time when I press the button.
I followed the same sequence to program the board. The LED blinked three time when I press the button.
Programming with C + Arduino IDE
LED blinks by pushing button
Next I tried Arduino IDE. Arduino IDE (Integrated Development Environment), Arduino Software, has text editor, message area and libraries, where we write programs called sketches and communicate with boards including compiling and uploading programs to the board. It is possible to download Arduino Software from here. I opened the same C file in Arduino IDE. I went to Arduino> Preference> Show verbose output during, and marked Complication. To add ATTiny 44 as a board, I followed this instruction. In Preference> Additional board manager, I typed following URL
Next I tried to program with Arduino sketch in Arduino IDE. I followed the same documentation and downloaded code for Arduino to press button to turn on LEDs. I changed button pin and deleted two LED pins as I did before. I clicked Valify to compile the code and uploaded. It worked finely.
I also tried the simple code from Iván Sánchez Milara's website. I downloaded blink_button_arduino_ide sketch.
I changed the pin for button and save as blink_button_arduino_ide1. I ran the code. It worked fine.
LED blinks three times by pushing button
Next I modified the code as I did above. I made the LED blinks three times when I press the button.
It worked fine.
This week’s assignment was programming. Thanks to good documentations of previous Fab Academy students, actual programming part was completed quickly. And I found I like debugging!But when I read datasheet to understand system behind, I got more and more confused.. I know things will be clearer while I am doing more practice, but communicating with ATTiny is like I am trying to communicate with an alien somewhere on the other planet.. Well, I will keep trying :)