Week 8's assignment
I begun to read it at the very end of the week, when I had failed to achieve anything else ;) So I did not go very far, but I took down some notes of what I understood :
- The operating voltagle is between 1.8 to 5.5v. This means that I can use a 5v power source which always good to know.
- "I/O" means Input/Output : if you don't know it, you won't understand much of what is in this datasheet !
- I understood that the pull-up resistors are important notions that allow a specific pin to behave as a stable source of current when the resistor is activated. It prevents from having a floating current. Type A and B pins both all include pull-up resistors. To activate the pull up resistor I will have to specify the High state of the pin I my program. It will be necessary for the button pin.
- The "overview" paragraph and especially the one on page5 is an interesting and important start because everthing the ATtiny44 does or can do is summarized there.
- This data sheet includes code examples, which could be very usefull in the future
- Although for the moment my konwledge does not allow me to understand the details, the Clock section (p25) of the date sheet will certainly be usefull for my final project. I will need to have a very precise clock system, so it is better if I use an external clock.
- The table 10.3 on page 60 is important to know what are the alternate functions of each pin. I connected the LED to PA7 (or pin6), so I know that it can be used, like all pins, as an ADC input.
- Three of the pins are output for the PWM mode function : PA6, PA7 and PB2. PWM, or Pulse Width Modulation rapidly turns the output pin high and low over a fixed period of time. This acts as if the voltage was changing. The data sheet provides formulas to calculate what should be the PMW frequency.
int switchState = 0;
void setup() {
// put your setup code here, to run once:
pinMode(3, OUTPUT);
pinMode(2, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
switchState = digitalRead(2);
if (switchState == LOW) {
digitalWrite(3, LOW);
}
else {
delay(500);
digitalWrite(3, HIGH);
delay(500);
digitalWrite(3, LOW);
delay(500);
digitalWrite(3, HIGH);
delay(500);
digitalWrite(3, LOW);
delay(500);
digitalWrite(3, HIGH);
delay(500);
}
}
Here is what it does when loaded into the Arduino UNO :Week8 - Code Test from Thomas Feminier on Vimeo.
int switchState = 0;
//morse code mapping
static const struct {const char letter, *code;} MorseMap[] =
{
{ 'A', ".-" },
{ 'B', "-..." },
{ 'C', "-.-." },
{ 'D', "-.." },
{ 'E', "." },
{ 'F', "..-." },
{ 'G', "--." },
{ 'H', "...." },
{ 'I', ".." },
{ 'J', ".---" },
{ 'K', ".-.-" },
{ 'L', ".-.." },
{ 'M', "--" },
{ 'N', "-." },
{ 'O', "---" },
{ 'P', ".--." },
{ 'Q', "--.-" },
{ 'R', ".-." },
{ 'S', "..." },
{ 'T', "-" },
{ 'U', "..-" },
{ 'V', "...-" },
{ 'W', ".--" },
{ 'X', "-..-" },
{ 'Y', "-.--" },
{ 'Z', "--.." },
{ ' ', " " }, //Gap between word, seven units
{ '1', ".----" },
{ '2', "..---" },
{ '3', "...--" },
{ '4', "....-" },
{ '5', "....." },
{ '6', "-...." },
{ '7', "--..." },
{ '8', "---.." },
{ '9', "----." },
{ '0', "-----" },
{ '.', "·–·–·–" },
{ ',', "--..--" },
{ '?', "..--.." },
{ '!', "-.-.--" },
{ ':', "---..." },
{ ';', "-.-.-." },
{ '(', "-.--." },
{ ')', "-.--.-" },
{ '"', ".-..-." },
{ '@', ".--.-." },
{ '&', ".-..." },
};
void setup() {
// put your setup code here, to run once:
pinMode(3, OUTPUT);
pinMode(2, INPUT);
// to activate the pull-up resistor :
digitalWrite(2, HIGH);
}
void loop() {
// put your main code here, to run repeatedly:
switchState = digitalRead(2);
if (switchState == LOW) {
digitalWrite(3, LOW);
}
else {
String morseWord = encode( "HELLO " );
for(int i=0; i<=morseWord.length(); i++)
{
switch( morseWord[i] )
{
case '.': //dit
digitalWrite(3, HIGH );
delay(250);
digitalWrite(3, LOW );
delay(250);
break;
case '-': //dah
digitalWrite(3, HIGH );
delay(250*3);
digitalWrite(3, LOW );
delay(250);
break;
case ' ': //gap
delay(500);
}
}
}
}
String encode(const char *string)
{
size_t i, j;
String morseWord = "";
for( i = 0; string[i]; ++i )
{
for( j = 0; j < sizeof MorseMap / sizeof *MorseMap; ++j )
{
if( toupper(string[i]) == MorseMap[j].letter )
{
morseWord += MorseMap[j].code;
break;
}
}
morseWord += " "; //Add tailing space to seperate the chars
}
return morseWord;
}
Week8 - Code test2 from Thomas Feminier on Vimeo.
void setup() {
// put your setup code here, to run once:
pinMode(PA7, OUTPUT);
pinMode(PA3, INPUT);
}
Then my main concern was to understand how I could program by Hello board directly without the FabISP which was not yet workind, using and Arduino UNO as an ISP. So first I uploaded the Arduino ISP sketch to the Arduino UNO and connected some LED's to the breadboard, as instructed in the sketch :
#include
#include
#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif
int main(void)
{
sbi(PORTA, PA7); // Set PA7 for output
for(;;){
sbi(PORTA, PA7); // LED is ON
_delay_ms(100);
cbi(PORTA, PA7); // LED is OFF
_delay_ms(100);
}
return 0;
}
Using XcodeI created a new project with parameters corresponding to my board (identifying the righ microcontroller, frequency, etc.) and then I simply had to hit the "build" button to send the program to the board. It worked immediately so I was relieved because at first, I was afraid something was wrong with the circuit itself... Here is a video of the result :Week8 - Blink from Thomas Feminier on Vimeo.
#include
#include
#include
#include
#include
#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif
int switchState = 0;
//morse code mapping
static const struct {const char letter, *code;} MorseMap[] =
{
{ 'A', ".-" },
{ 'B', "-..." },
{ 'C', "-.-." },
{ 'D', "-.." },
{ 'E', "." },
{ 'F', "..-." },
{ 'G', "--." },
{ 'H', "...." },
{ 'I', ".." },
{ 'J', ".---" },
{ 'K', ".-.-" },
{ 'L', ".-.." },
{ 'M', "--" },
{ 'N', "-." },
{ 'O', "---" },
{ 'P', ".--." },
{ 'Q', "--.-" },
{ 'R', ".-." },
{ 'S', "..." },
{ 'T', "-" },
{ 'U', "..-" },
{ 'V', "...-" },
{ 'W', ".--" },
{ 'X', "-..-" },
{ 'Y', "-.--" },
{ 'Z', "--.." },
{ ' ', " " }, //Gap between word, seven units
{ '1', ".----" },
{ '2', "..---" },
{ '3', "...--" },
{ '4', "....-" },
{ '5', "....." },
{ '6', "-...." },
{ '7', "--..." },
{ '8', "---.." },
{ '9', "----." },
{ '0', "-----" },
{ '.', "·–·–·–" },
{ ',', "--..--" },
{ '?', "..--.." },
{ '!', "-.-.--" },
{ ':', "---..." },
{ ';', "-.-.-." },
{ '(', "-.--." },
{ ')', "-.--.-" },
{ '"', ".-..-." },
{ '@', ".--.-." },
{ '&', ".-..." },
};
//char encode(const char *string)
char encode(char string[])
{
size_t i, j;
//char morseWord[]="";
char toto[]="";
//for( i = 0; string[i]; ++i )
for( i = 0; strlen(string); ++i )
{
for( j = 0; j < sizeof MorseMap / sizeof *MorseMap; ++j )
{
if( toupper(string[i]) == MorseMap[j].letter )
{
strcat(toto, MorseMap[j].code);
break;
}
}
strcat(toto," "); //Add tailing space to seperate the chars
}
return *string;
// return *morseWord;
}
int main(void)
{
sbi(PORTA, PA7); // Set PA7 for output (sbi = set bit)
cbi(DDRA, PA3); // Set PA3 for input (cbi = clear bit)
sbi(PORTA, PA3); // Pull-up resistor activated on PA3
sbi(PORTA, PA7);
_delay_ms(100);
cbi(PORTA, PA7);
_delay_ms(100);
for(;;){
// switchState = digitalRead(PA3);
if (switchState == 0) {
cbi(PORTA, PA7);
} else {
//char morseWord[] = "";
//char morseWord[] = encode( "hello " );
char toto[] = encode( "HELLO " );
//char morseWord[] = "--.";
for(int i=0; i<=strlen(toto); i++)
{
char Zozo = toto[i];
switch( Zozo )
{
case '.': //court
sbi(PORTA, PA7);
_delay_ms(250);
cbi(PORTA, PA7);
_delay_ms(250);
break;
case '-': //long
sbi(PORTA, PA7);
_delay_ms(250*3);
cbi(PORTA, PA7);
_delay_ms(250);
break;
case ' ': //blanc
_delay_ms(500);
}
}
}
}
return 1;
}