...
 
Commits (4)
......@@ -135,7 +135,10 @@
<span class="image main"><img src="../images/final/solar-pcb.png" alt="solar-pcb" /></span>
<p>It turned out that one ATtiny44 could do all the job, but I realised this too late. My system has
4 inputs (analog readings from LDRs) and 2 pairs of outputs for two DC motors. I thought
driving DC motors require analog pins but in reality it needs PWMs. </p>
driving DC motors require analog pins but in reality it needs PWMs. It has two ATtiny44
microcontrollers one for measuring LDR inputs and the other one for driving DC motors. The first
ATTiny44's PA0 and PA1 are connected to other ATtiny44's PA0 and PA1 respectively with 500ohm
resistors between them. This is done in order to drive DC motors based on LDRs data.</p>
<span class="image main"><img src="../images/final/sensor-pcb.png" alt="sensor-pcb" /></span>
<p>Here are the Kicad PCB files:
<br><a href="../cad files/final/solar_v1 kicad.zip">solar_v1 kicad.zip</a>
......
This diff is collapsed.
......@@ -41,40 +41,87 @@
<!-- <a href=""></a> -->
<h2>Introduction</h2>
<p>For this week we had to:
<br>read a microcontroller data sheet program your board to do something, with as many different programming languages
and programming environments as possible (individual assignment)
<br>read a microcontroller data sheet program your board to do something, with as many different
programming languages
and programming environments as possible (individual assignment)
<br> compare the performance and development workflows
for other architectures (group assignment)</p>
My
My
<h2>Datasheet</h2>
<p>Here's the <a
href="http://fab.cba.mit.edu/classes/863.09/people/ryan/week5/ATtiny44%20Data%20Sheet.pdf">ATtiny44a datasheet</a>
The first thing I did was to figure out whar AVR means, and in the book
<a href="https://cyber.rms.moe/books/02%20-%20Electronic%20Engineering/Practical%20AVR%20Microcontrollers%20-%20Alan%20Trevennor.pdf">Practical AVR Microcontrollers by Alan Trevennor</a>
I learned that nobody at Atmel wants to tell what the AVR stand for. The inventors of AVR Alf Egil Bogen and Vegard Wollan even made a teasing video <a href="https://youtu.be/HrydNwAxbcY">The Story of AVR</a>.
<br>This week was very stressful for me since I felt depleted and neither of my soldered microcontrollers worked.
While trying to understand the datasheet I learned this concepts:
<br>In System Programming is achieved via <a href="https://en.wikipedia.org/wiki/Serial_Peripheral_Interface">SPI port</a> (Serial Peripheral Interface).
Here are the 4 wire names of SPI:
<br>SCLK: Serial Clock (output from master)
<br>MOSI-Master Out Slave In (data output from master)
<br>MISO-Master In Slave Out (data output from slave)
<br>SS: Slave Select (output from master) This is used for communicating with several microcontrollers.
<br>SPI is A <a href="https://en.wikipedia.org/wiki/Duplex_(telecommunications)#Full-duplex">full-duplex</a> (FDX) system, or sometimes called double-duplex, which allows communication in both directions,
and, unlike half-duplex, allows this to happen simultaneously.
<br>If some pins are unused, it is recommended to ensure that these pins have a defined level. <b>Page 55</b> </p>
href="http://fab.cba.mit.edu/classes/863.09/people/ryan/week5/ATtiny44%20Data%20Sheet.pdf">ATtiny44a
datasheet</a>
The first thing I did was to figure out whar AVR means, and in the book
<a
href="https://cyber.rms.moe/books/02%20-%20Electronic%20Engineering/Practical%20AVR%20Microcontrollers%20-%20Alan%20Trevennor.pdf">Practical
AVR Microcontrollers by Alan Trevennor</a>
I learned that nobody at Atmel wants to tell what the AVR stand for. The inventors of AVR Alf
Egil Bogen and Vegard Wollan even made a teasing video <a
href="https://youtu.be/HrydNwAxbcY">The Story of AVR</a>.
<br>This week was very stressful for me since I felt depleted and neither of my soldered
microcontrollers worked.
While trying to understand the datasheet I learned this concepts:
<br>In System Programming is achieved via <a
href="https://en.wikipedia.org/wiki/Serial_Peripheral_Interface">SPI port</a> (Serial
Peripheral Interface).
Here are the 4 wire names of SPI:
<br>SCLK: Serial Clock (output from master)
<br>MOSI-Master Out Slave In (data output from master)
<br>MISO-Master In Slave Out (data output from slave)
<br>SS: Slave Select (output from master) This is used for communicating with several
microcontrollers.
<br>SPI is A <a
href="https://en.wikipedia.org/wiki/Duplex_(telecommunications)#Full-duplex">full-duplex</a>
(FDX) system, or sometimes called double-duplex, which allows communication in both directions,
and, unlike half-duplex, allows this to happen simultaneously.
<br>If some pins are unused, it is recommended to ensure that these pins have a defined level.
<b>Page 55</b> </p>
<h2>Prepairing the board</h2>
<p>In <a href="../assignments/week7.html">week7</a> we redesigned echo hello-world board. This week we had to program it with the FabISP we made on <a href="../assignments/week5.html">week5</a>.
I decided to improve some drawbacks of my design such as narrow holes and small footprints, and mismanaged the time to spend more on programming.
Here is my final board.</p>
<span class="image main"><img src="../images/week9/final_guitar.jpg" alt="final_guitar_board" /></span>
<p>In order to program echo hello you will need:
USB to FTDI cable or you can make your own <a href="https://ppatil.pages.cba.mit.edu/FabFTDI/">FabFTDI</a>
The FTDI is a USB to Serial <a href="https://en.wikipedia.org/wiki/Transistor–transistor_logic">(TTL level)</a> converter which allows for a simple way to connect TTL interface devices to USB.
Unfortunatelly I was not that attentive during Neils lecture and missed the part where he was talking about getting the right FTDI cable and discovered this at the end of the week and didn't manage
to make it or get one, so instead I just powered my board from Arduino 5v pin.
FabISP </p>
<p>In <a href="../assignments/week7.html">week7</a> we redesigned echo hello-world board. This week
we had to program it with the FabISP we made on <a href="../assignments/week5.html">week5</a>.
I decided to improve some drawbacks of my design such as narrow holes and small footprints, and
mismanaged the time to spend more on programming.
<br>In order to program echo hello you will need:
USB to FTDI cable or you can make your own <a
href="https://ppatil.pages.cba.mit.edu/FabFTDI/">FabFTDI</a>
The FTDI is a USB to Serial <a
href="https://en.wikipedia.org/wiki/Transistor–transistor_logic">(TTL level)</a> converter
which allows for a simple way to connect TTL interface devices to USB.
For this assignment I will use my Ubuntu computer, FabFTDI and the guitar board I made during <a
href="./week7.html">week7</a>. I will use c code and make file from Neil's keynotes.</p>
<h2>Programming Solar Tracking board (Final Project)</h2>
<p>My final project has two different programs that work on different ATtiny44 microcontrollers. The
first one <a href="../cad files/final/sun.44.c">sun.44.c</a> is measuring voltage from 4 LDR's
<a href="https://www.kitronik.co.uk/blog/how-an-ldr-light-dependent-resistor-works/">(Light
Dependent Resistor)</a>
and the second one for getting commands from the first microcontroller and drives the DC motors
based on this commands.
</p>
<p>Let's begin from the <a href="../cad files/final/sun.44.c">sun.44.c</a>.
<br><code>#include <avr/io.h></code> This is the library for <a
href="https://www.microchip.com/webdoc/AVRLibcReferenceManual/group__avr__io.html">AVR
device-specific Input Output definitions</a>
<br><code>#include <util/delay.h></code> This is the library of <a
href="https://www.microchip.com/webdoc/AVRLibcReferenceManual/group__util__delay.html">functions
for busy-wait delay loops</a>
<br>Next come the definition of macros: <a
href="https://www.techonthenet.com/c_language/constants/create_define.php">#define</a>
directive allows the definition of macros
<br><code>#define output(directions,pin) (directions |= pin) </code> // set port direction for
output
<br><code>#define set(port,pin) (port |= pin)</code> // set port pin
<br><code>#define clear(port,pin) (port &= (~pin))</code> // clear port pin
<br><code>#define pin_test(pins,pin) (pins & pin)</code> // test for port pin
<br><code>#define bit_test(byte,bit) (byte & (1 << bit))</code> // test for bit set
<br><code>#define PWM_delay() _delay_us(500)</code> // PWM delay
<br>
<br><code>#define led_port PORTB</code>
<br><code>#define led_direction DDRB</code>
<br><code>#define red (1 << PB1)</code>
<br><code>#define blue (1 << PB0)</code>
</p>
......
// dc.44.c
//
// solar tracker motor drive
//
// Babken Chugaszyan
// 19/06/2019
//
// (c)This work may be reproduced, modified, distributed, performed,
// and displayed for any purpose. No warranty is provided, and users accept all
// liability.
//
#include <avr/io.h>
#include <util/delay.h>
#define output(directions,pin) (directions |= pin) // set port direction for output
#define input(directions,pin) (directions &= (~pin)) // set port direction for input
#define set(port,pin) (port |= pin) // set port pin
#define clear(port,pin) (port &= (~pin)) // clear port pin
#define pin_test(pins,pin) (pins & pin) // test for port pin
#define bit_test(byte,bit) (byte & (1 << bit)) // test for bit set
#define on_delay() _delay_us(3) // PWM on time
#define fast_off_delay() _delay_us(0) // PWM fast off time
#define medium_off_delay() _delay_us(3) // PWM medium off time
#define slow_off_delay() _delay_us(5) // PWM slow off time
#define PWM_count 20000 // number of PWM cycles
#define cycle_count 5 // number of speed cycles
#define input_port PORTB
#define input_direction DDRB
#define input_pino (1 << PB0)//button
#define input_pini (1 << PB1)//button
#define input_pins PINB
#define bridge_port PORTA // H-bridge port
#define bridge_direction DDRA // H-bridge direction
#define IN1 (1 << PA0) // IN1
#define IN2 (1 << PA1) // IN2
#define IN3 (1 << PA3) // IN3
#define IN4 (1 << PA4) // IN4
int main(void) {
//
// main
//
//
// set clock divider to /1
//
CLKPR = (1 << CLKPCE);
CLKPR = (0 << CLKPS3) | (0 << CLKPS2) | (0 << CLKPS1) | (0 << CLKPS0);
//
// initialize H-bridge pins
//
clear(bridge_port, IN1);
output(bridge_direction, IN1);
clear(bridge_port, IN2);
output(bridge_direction, IN2);
set(input_port, input_pino); // turn on pull-up
set(input_port, input_pini);
input(input_direction, input_pino);
input(input_direction, input_pini);
//
// main loop
//
while (1) {
if (0 != pin_test(input_pins,input_pino))
{
clear(bridge_port, IN1);//left
set(bridge_port, IN2);
}
else {
clear(bridge_port, IN2);//right
set(bridge_port, IN1);
}
if (0 != pin_test(input_pins,input_pini))
{
clear(bridge_port, IN3);//up
set(bridge_port, IN4);
}
else {
clear(bridge_port, IN4);//down
set(bridge_port, IN3);
}
}
}
PROJECT=dc.44.DC
SOURCES=$(PROJECT).c
MMCU=attiny44
F_CPU = 8000000
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-usbtiny: $(PROJECT).hex
avrdude -p t44 -P usb -c usbtiny -U flash:w:$(PROJECT).c.hex
program-dragon: $(PROJECT).hex
avrdude -p t44 -P usb -c dragon_isp -U flash:w:$(PROJECT).c.hex
program-ice: $(PROJECT).hex
avrdude -p t44 -P usb -c atmelice_isp -U flash:w:$(PROJECT).c.hex
//
//
// sun.44.c
//
// solar tracker
//
// Babken Chugaszyan
// 19/06/2019
//
// (c)This work may be reproduced, modified, distributed, performed,
// and displayed for any purpose. No warranty is provided, and users accept all
// liability.
//
#include <avr/io.h>
#include <util/delay.h>
#define output(directions,pin) (directions |= pin) // set port direction for output
#define set(port,pin) (port |= pin) // set port pin
#define clear(port,pin) (port &= (~pin)) // clear port pin
#define pin_test(pins,pin) (pins & pin) // test for port pin
#define bit_test(byte,bit) (byte & (1 << bit)) // test for bit set
#define PWM_delay() _delay_us(500) // PWM delay
#define led_port PORTB
#define led_direction DDRB
#define red (1 << PB1)
#define blue (1 << PB0)
// 8-bit resolution
// set ADLAR to 1 to enable the Left-shift result (only bits ADC9..ADC2 are available)
// then, only reading ADCH is sufficient for 8-bit results (256 values)
void initADCul()
{
ADMUX =
(0 << ADLAR) | // left shift result
(0 << REFS1) | // Sets ref. voltage to VCC, bit 1
(0 << REFS0) | // Sets ref. voltage to VCC, bit 0
(0 << MUX3) | // use ADC1 for input (PA0), MUX bit 3
(0 << MUX2) |
(0 << MUX1) |
(0 << MUX0);
ADCSRA =
(1 << ADEN) | // Enable ADC
(1 << ADPS2) | // set prescaler to 64, bit 2
(1 << ADPS1) | // set prescaler to 64, bit 1
(0 << ADPS0); // set prescaler to 64, bit 0
}
void initADCur()
{
ADMUX =
(0 << ADLAR) | // left shift result
(0 << REFS1) | // Sets ref. voltage to VCC, bit 1
(0 << REFS0) | // Sets ref. voltage to VCC, bit 0
(0 << MUX3) | // use ADC2 for input (PA1), MUX bit 3
(0 << MUX2) |
(0 << MUX1) |
(1 << MUX0);
ADCSRA =
(1 << ADEN) | // Enable ADC
(1 << ADPS2) | // set prescaler to 64, bit 2
(1 << ADPS1) | // set prescaler to 64, bit 1
(0 << ADPS0); // set prescaler to 64, bit 0
}
void initADCdl()
{
ADMUX =
(0 << ADLAR) | // left shift result
(0 << REFS1) | // Sets ref. voltage to VCC, bit 1
(0 << REFS0) | // Sets ref. voltage to VCC, bit 0
(0 << MUX3) | // use ADC2 for input (PA3), MUX bit 3
(0 << MUX2) |
(1 << MUX1) |
(1 << MUX0);
ADCSRA =
(1 << ADEN) | // Enable ADC
(1 << ADPS2) | // set prescaler to 64, bit 2
(1 << ADPS1) | // set prescaler to 64, bit 1
(0 << ADPS0); // set prescaler to 64, bit 0
}
void initADCdr()
{
ADMUX =
(0 << ADLAR) | // left shift result
(0 << REFS1) | // Sets ref. voltage to VCC, bit 1
(0 << REFS0) | // Sets ref. voltage to VCC, bit 0
(0 << MUX3) | // use ADC2 for input (PA4), MUX bit 3
(1 << MUX2) |
(0 << MUX1) |
(0 << MUX0);
ADCSRA =
(1 << ADEN) | // Enable ADC
(1 << ADPS2) | // set prescaler to 64, bit 2
(1 << ADPS1) | // set prescaler to 64, bit 1
(0 << ADPS0); // set prescaler to 64, bit 0
}
int main(void)
{
int ul = 0;//up left
int ur = 0;//up right
int dl = 0;//down left
int dr = 0;//down right
int au = 0;//average up
int ad = 0;//average down
int al = 0;//average left
int ar = 0;//average right
//
// initialize output pins
//
output(led_direction, red);
output(led_direction, blue);
while(1)
{
initADCur();
ADCSRA |= (1 << ADSC); // start ADC measurement
while (ADCSRA & (1 << ADSC) ); // wait till conversion complete
ur = ADCW;
initADCul();
ADCSRA |= (1 << ADSC); // start ADC measurement
while (ADCSRA & (1 << ADSC) ); // wait till conversion complete
ul = ADCW;
initADCdl();
ADCSRA |= (1 << ADSC); // start ADC measurement
while (ADCSRA & (1 << ADSC) ); // wait till conversion complete
dl = ADCW;
initADCdr();
ADCSRA |= (1 << ADSC); // start ADC measurement
while (ADCSRA & (1 << ADSC) ); // wait till conversion complete
dr = ADCW;
au = (ul + ur)/2;
ad = (dl + dr)/2;
al = (ul + dl)/2;
ar = (ur + dr)/2;
if (au > ad) {
set(led_port,red);
}
else if (au < ad) {
clear(led_port,red);
}
if (al > ar) {
set(led_port,blue);
}
else if (al < ar) {
clear(led_port,blue);
}
}
return 0;
}
PROJECT=sun.44
SOURCES=$(PROJECT).c
MMCU=attiny44
F_CPU = 8000000
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-usbtiny: $(PROJECT).hex
avrdude -p t44 -P usb -c usbtiny -U flash:w:$(PROJECT).c.hex
program-dragon: $(PROJECT).hex
avrdude -p t44 -P usb -c dragon_isp -U flash:w:$(PROJECT).c.hex
program-ice: $(PROJECT).hex
avrdude -p t44 -P usb -c atmelice_isp -U flash:w:$(PROJECT).c.hex