< back to home

Input device

Goals

For this week I decided not to mill any other board, for my old board (the one used during the embedded programming assignment) has 2 exposed pins and a button, which would serve just right for measuring a couple of inputs.

Button

Since my board already came with a button, all I had to do was to download Neil's code and make some minor changes to it, in order to use the right pins; so I modified some #defines to look like this

            
#define input_port PORTA
#define input_direction DDRA
#define input_pin (1 << PA7)
#define input_pins PINA
#define serial_port PORTA
#define serial_direction DDRA
#define serial_pin_out (1 << PA1)
            
          

I then downloaded the Makefile and modified it in order to make it work with my attiny44 (I had to change the MMCU, F_CPU variables and the value for the "-p" flag).

            
PROJECT=button
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-avrisp2: $(PROJECT).hex
	avrdude -p t45 -P usb -c avrisp2 -U flash:w:$(PROJECT).c.hex

program-usbtiny: $(PROJECT).hex
	avrdude -p t44 -P usb -c usbtiny -U flash:w:$(PROJECT).c.hex
            
          

To read the output from my board I used both the rx.py and the term.py scripts (I had to dowload the pyserial module first, a simple aptitude install python-serial on debian GNU/Linux was enough)

            
sgrc@t420:~/fabacademy2017/doc/week13-input/code/button01$ chmod +x rx.py
sgrc@t420:~/fabacademy2017/doc/week13-input/code/button01$ ./rx.py 
command line: rx.py serial_port port_speed
sgrc@t420:~/fabacademy2017/doc/week13-input/code/button01$ ./rx.py /dev/ttyUSB0 9600
1: d  (dec 100  hex 64)
2: u  (dec 117  hex 75)
3: d  (dec 100  hex 64)
4: u  (dec 117  hex 75)
5: d  (dec 100  hex 64)
6: u  (dec 117  hex 75)
7: d  (dec 100  hex 64)
8: u  (dec 117  hex 75)
            
          

Sonar

I then tried to connect a sonar module to my board; I made the right connections using 4 jumper wires and modified the supplied code as follows:

            
#define serial_port PORTA
#define serial_direction DDRA
#define serial_pin_out (1 << PA1)
#define trigger_port PORTA
#define trigger_direction DDRA
#define trigger_pin (1 << PA2)
#define echo_pins PINA
#define echo_direction DDRA
#define echo_pin (1 << PA3)
            
          

The code reflects the connection I made between the sensor and my board: pin2 is the trigger pin, and pin3 is the echo one. I also connectend ground and vcc:

Alas, when I tried to upload the code I got this error:

            
sonar.c: In function ‘main’:
sonar.c:145:15: error: ‘TIFR’ undeclared (first use in this function)
          if ((TIFR & (1 << TOV0)) != 0) { // check for counter overflow
               ^
sonar.c:145:15: note: each undeclared identifier is reported only once for each function it appears in
Makefile:13: recipe for target 'sonar.out' failed
make: *** [sonar.out] Error 1
            
          

A quick glance at the datasheet (par 11.9.7, page 85) gave me the solution: I simply had to rename each instance of TIFR in TIFR0 and I was able to upload the code to my board.
I then downloaded the python script, added a shebang at the beginning, out of habit, and launched it. Everything worked, but I got some bizarre readings from the sensor:

Looking at the code I realized I forgot to change the value for my board clock (20 instead of 8).

Links