For the week 7, we have to program the board to do something, with as many different programming languages and programming environments as possible.

First Approach

First, given my lack of experience in programming, I downloaded a program already compiled (written in C) to begin to understand how it is structured and also to see if my board was working.

In the program folder that I downloaded are two files: the Makefile, which is used to automate the programming, and the file steadyled.c which is the program.

Makefile
PROJECT=steadyled 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-avrisp2: $(PROJECT).hex avrdude -p t44 -P usb -c avrisp2 -U flash:w:$(PROJECT).c.hex program-avrisp2-fuses: $(PROJECT).hex avrdude -p t44 -P usb -c avrisp2 -U lfuse:w:0x5E:m program-usbtiny: $(PROJECT).hex avrdude -p t44 -P usb -c usbtiny -U flash:w:$(PROJECT).c.hex program-usbtiny-fuses: $(PROJECT).hex avrdude -p t44 -P usb -c usbtiny -U lfuse:w:0x5E:m program-dragon: $(PROJECT).hex avrdude -p t44 -P usb -c dragon_isp -U flash:w:$(PROJECT).c.hex

steadyled.c
#include <avr/io.h> #include <inttypes.h> #include <util/delay.h> void delay_ms(uint16_t ms); void init_io(); int button_is_pressed(); void toggle_led(); #define F_CPU 20000000UL /* 20MHz crystal oscillator */ #define BUTTON_PORT PORTA /* PORTx - register for button output */ #define BUTTON_PIN PINA /* PINx - register for button input */ #define BUTTON_BIT PA3 /* bit for button input/output */ #define LED_PORT PORTA /* PORTx - register for LED output */ #define LED_BIT PA7 /* bit for button input/output */ #define LED_DDR DDRA /* LED data direction register */ #define DEBOUNCE_TIME 25 /* time to wait while "de-bouncing" button */ #define LOCK_INPUT_TIME 250 /* time to wait after a button press */ int main (void) { init_io(); while (1) { if (button_is_pressed()) { toggle_led(); } } } void delay_ms(uint16_t ms) { while ( ms ) { _delay_ms(1); ms--; } } void init_io() { /* set LED pin as digital output */ LED_DDR = _BV (LED_BIT); /* led is OFF initially (set pin high) */ LED_PORT |= _BV(LED_BIT); /* turn on internal pull-up resistor for the switch */ BUTTON_PORT |= _BV(BUTTON_BIT); } int button_is_pressed() { /* the button is pressed when BUTTON_BIT is clear */ if (bit_is_clear(BUTTON_PIN, BUTTON_BIT)) { delay_ms(DEBOUNCE_TIME); if (bit_is_clear(BUTTON_PIN, BUTTON_BIT)) return 1; } return 0; } void toggle_led() { LED_PORT ^= _BV(LED_BIT); }



Once downloaded file, I changed the number of pins where are connected the button and the LED (in the file above, pins are already modified) using the button for PA3 and PA7 for Led.

After that, I connected the FabISP and the board to the PC, and connected each other with the rainbow cable.

Responsive image

After connecting the two boards, open the terminal and enter the directory of the program "steadyled":

MacBook-Pro-di-Giulia:~ Giulia$ cd Downloads/steadyled MacBook-Pro-di-Giulia:steadyled Giulia$

After that, we have to type the command "make" in order to create the excecute the Makefile. The Makefile create by default the hexadecimal file (steadyled.c.hex).

MacBook-Pro-di-Giulia:steadyled Giulia$ make avr-gcc -mmcu=attiny44 -Wall -Os -DF_CPU=20000000 -I./ -o steadyled.out steadyled.c steadyled.c:10:0: warning: "F_CPU" redefined #define F_CPU 20000000UL /* 20MHz crystal oscillator */ ^ <command-line>:0:0: note: this is the location of the previous definition avr-objcopy -O ihex steadyled.out steadyled.c.hex;\ avr-size --mcu=attiny44 --format=avr steadyled.out AVR Memory Usage ---------------- Device: attiny44 Program: 140 bytes (3.4% Full) (.text + .data + .bootloader) Data: 0 bytes (0.0% Full) (.data + .bss + .noinit)

If you look now in the steadyled folder, you will see the new files created by the Makefile.

Responsive image

After, we have to do the "make program-usbtiny-fuses" to reset the microcontroller and prepare it to recieve the programming

MacBook-Pro-di-Giulia:steadyled Giulia$ sudo make program-usbtiny-fuses Password: avr-objcopy -O ihex steadyled.out steadyled.c.hex;\ avr-size --mcu=attiny44 --format=avr steadyled.out AVR Memory Usage ---------------- Device: attiny44 Program: 140 bytes (3.4% Full) (.text + .data + .bootloader) Data: 0 bytes (0.0% Full) (.data + .bss + .noinit) avrdude -p t44 -P usb -c usbtiny -U lfuse:w:0x5E:m avrdude: AVR device initialized and ready to accept instructions Reading | ################################################## | 100% 0.00s avrdude: Device signature = 0x1e9207 avrdude: reading input file "0x5E" avrdude: writing lfuse (1 bytes): Writing | ################################################## | 100% 0.00s avrdude: 1 bytes of lfuse written avrdude: verifying lfuse memory against 0x5E: avrdude: load data lfuse data from input file 0x5E: avrdude: input file 0x5E contains 1 bytes avrdude: reading on-chip lfuse data: Reading | ################################################## | 100% 0.00s avrdude: verifying ... avrdude: 1 bytes of lfuse verified avrdude: safemode: Fuses OK (E:FF, H:DF, L:5E) avrdude done. Thank you.

Finally we have to do the "make program-usbtiny" to program the board:

MacBook-Pro-di-Giulia:steadyled Giulia$ sudo make program-usbtiny avr-objcopy -O ihex steadyled.out steadyled.c.hex;\ avr-size --mcu=attiny44 --format=avr steadyled.out AVR Memory Usage ---------------- Device: attiny44 Program: 140 bytes (3.4% Full) (.text + .data + .bootloader) Data: 0 bytes (0.0% Full) (.data + .bss + .noinit) avrdude -p t44 -P usb -c usbtiny -U flash:w:steadyled.c.hex avrdude: AVR device initialized and ready to accept instructions Reading | ################################################## | 100% 0.01s avrdude: Device signature = 0x1e9207 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 "steadyled.c.hex" avrdude: input file steadyled.c.hex auto detected as Intel Hex avrdude: writing flash (140 bytes): Writing | ################################################## | 100% 0.26s avrdude: 140 bytes of flash written avrdude: verifying flash memory against steadyled.c.hex: avrdude: load data flash data from input file steadyled.c.hex: avrdude: input file steadyled.c.hex auto detected as Intel Hex avrdude: input file steadyled.c.hex contains 140 bytes avrdude: reading on-chip flash data: Reading | ################################################## | 100% 0.43s avrdude: verifying ... avrdude: 140 bytes of flash verified avrdude: safemode: Fuses OK (E:FF, H:DF, L:5E) avrdude done. Thank you.

After that, the LED should turn on: this means that the board is working properly and that has been programmed!

Responsive image

This was just an example, useful to see if the board is working properly and to begin to understand how is structured and how work a program written in C.

Now for the assignment of the week i have to study a "little bit" of C languages to write a personal code for my board!