Group Assignment
Understanding ATTiny 45
Microcontrollers are used in many embedded systems applications, let’s look at some consumer products like the air conditioner, the brain of it is a microcontroller that responsible for receiving sensors data like a temperature sensor that senses the current room temperature, turn that input into series of commands to go to the air conditioner compressor to work or not. Or even the microcontroller in your universal remote control translates your key presses into a precise series of pulses for an infrared LED that tells the microcontroller inside your television to change the channel or increase the volume.
| Flash (program memory) | 4096 bytes |
|---|---|
| RAM | 256 bytes |
| EEPROM | 256 bytes |
| GPIO Pins | 8 + RST + XTAL1 + XTAL2 |
| ADC Channels | 8 |
| PWM Channels | 4 |
| Clock options | Internal 1/8mhz, external crystal or clock* up to 20mhz |
DDRB = 0b00001111;
PORTB = 0xFF; will write HIGH on every pin that was set as output.
Arduino-C Code
pinMode in the void setup() section, which will be executed once, then write your code sequence in the void loop() section
and it will run repeatedly. Here are some quotes about the most important functions in the Arduino-C language.
INPUT or an OUTPUT. It is possible to enable the internal pullup resistors with the mode INPUT_PULLUP. Additionally, the INPUT mode explicitly disables the internal pullups.
pinMode(pin, state)
HIGH or a LOW value to a digital pin. digitalWrite(pin, value)
HIGH or LOW.
digitalRead(pin)
delay(ms)
int Led1 = 10
void setu() {
pinMode (Led1 ,OUTPUT);
digitalWrite (Led1 , HIGH);
}
void loop () {
digitalWrite(Led1, HIGH);
delay(1000);
digitalWrite(Led1 , LOW);
delay(1000);
}
int Led2 = 10;
int Led1 = 9;
int Led3 = 2;
int Led4 = 8;
int Led5 = 3;
int Pushbutton = 7;
void setup() {
pinMode (Led1 , OUTPUT);
pinMode (Led2 , OUTPUT);
pinMode (Led3 , OUTPUT);
pinMode (Led4 , OUTPUT);
pinMode (Led5 , OUTPUT);
pinMode (Pushbutton , INPUT);
digitalWrite(Led1 , LOW);
digitalWrite(Led2 , LOW);
digitalWrite(Led3 , LOW);
digitalWrite(Led4 , LOW);
digitalWrite(Led5 , LOW);
digitalWrite(Led5 , LOW);
digitalWrite(Pushbutton , 1);
}
void loop() {
digitalWrite(Led1 , HIGH);
delay (300);
digitalWrite(Led2 , HIGH);
delay (300);
digitalWrite(Led3 , HIGH);
delay (300);
digitalWrite(Led4 , HIGH);
delay (300);
digitalWrite(Led5 , HIGH);
delay (300);
digitalWrite(Pushbutton , HIGH);
delay (300);
digitalWrite(Led1 , LOW);
digitalWrite(Led2 , LOW);
digitalWrite(Led3 , LOW);
digitalWrite(Led4 , LOW);
digitalWrite(Led5 , LOW);
delay (300);
/* ------------------- */
digitalWrite(Led1 , HIGH);
delay (250);
digitalWrite(Led2 , HIGH);
delay (250);
digitalWrite(Led3 , HIGH);
delay (250);
digitalWrite(Led4 , HIGH);
delay (250);
digitalWrite(Led5 , HIGH);
delay (250);
digitalWrite(Pushbutton , HIGH);
delay (250);
digitalWrite(Led1 , LOW);
digitalWrite(Led2 , LOW);
digitalWrite(Led3 , LOW);
digitalWrite(Led4 , LOW);
digitalWrite(Led5 , LOW);
delay (250);
/*------------------ */
digitalWrite(Led1 , HIGH);
delay (200);
digitalWrite(Led2 , HIGH);
delay (200);
digitalWrite(Led3 , HIGH);
delay (200);
digitalWrite(Led4 , HIGH);
delay (200);
digitalWrite(Led5 , HIGH);
delay (200);
digitalWrite(Pushbutton , HIGH);
delay (200);
digitalWrite(Led1 , LOW);
digitalWrite(Led2 , LOW);
digitalWrite(Led3 , LOW);
digitalWrite(Led4 , LOW);
digitalWrite(Led5 , LOW);
delay (200);
/*--------------- */
digitalWrite(Led1 , HIGH);
delay (150);
digitalWrite(Led2 , HIGH);
delay (150);
digitalWrite(Led3 , HIGH);
delay (150);
digitalWrite(Led4 , HIGH);
delay (150);
digitalWrite(Led5 , HIGH);
delay (150);
digitalWrite(Pushbutton , HIGH);
delay (150);
digitalWrite(Led1 , LOW);
digitalWrite(Led2 , LOW);
digitalWrite(Led3 , LOW);
digitalWrite(Led4 , LOW);
digitalWrite(Led5 , LOW);
delay (150);
/*------------*/
digitalWrite(Led1 , HIGH);
delay (100);
digitalWrite(Led2 , HIGH);
delay (100);
digitalWrite(Led3 , HIGH);
delay (100);
digitalWrite(Led4 , HIGH);
delay (100);
digitalWrite(Led5 , HIGH);
delay (100);
digitalWrite(Pushbutton , HIGH);
delay (100);
digitalWrite(Led1 , LOW);
digitalWrite(Led2 , LOW);
digitalWrite(Led3 , LOW);
digitalWrite(Led4 , LOW);
digitalWrite(Led5 , LOW);
delay (100);
/* ----------------- */
digitalWrite(Led1 , HIGH);
delay (50);
digitalWrite(Led2 , HIGH);
delay (50);
digitalWrite(Led3 , HIGH);
delay (50);
digitalWrite(Led4 , HIGH);
delay (50);
digitalWrite(Led5 , HIGH);
delay (50);
digitalWrite(Pushbutton , HIGH);
delay (50);
digitalWrite(Led1 , LOW);
digitalWrite(Led2 , LOW);
digitalWrite(Led3 , LOW);
digitalWrite(Led4 , LOW);
digitalWrite(Led5 , LOW);
delay (50);
}
AVR-C Code
#define F_CPU 20000000UL
#include <avr/io.h>
#include <util/delay.h>
int main(void) function, which will be executed directly when the MCU is powered.
Inside this fucntion, we can write any sequence we want for the code. The code written inside the main is executed only for one time.
DDRB = 0b00000100; line, will configure Pin 2 at Port B as Output.
PORTA = 0b10000000; line, will Pull-up Pin 7 at Port A.
void setup() function in the Arduino-C code.
while(1)
infinite loop, where we can nest the condition and the ON/OFF sequence.
if (!(PINA & (0b10000000))). The PINA reads the whole pins byte (A).
In order to read a specifi pin, we should just ignore the rest of the pins by anding them with zeros.
When the required pin is connected to GND the expression result will be !(0) which is 1, and vice versa.
PORTB |= 0b00000100; to output logic 1 on Pin 2 at Port B; leaving the other pins in whatever state.
This is because (0) | 0 = 0 and (1) || 0 = 1
PORTB &= 0b11111011; outputs logic 0 on Pin 2 at Port B, leaving the other pins in whatever state,
because (0) & 1 = 0 and (1) & 1 = 1
_delay_ms(1250); , which is similar to the delay(ms) function in the Arduino-C.
#define F_CPU 20000000UL
#include <avr/io.h>
#include <util/delay.h>
int main(void)
{
DDRB = 0b00001100; //Configured Pin 2 at Port B as Output
PORTA = 0b10000000; //Pull-up Pin 7 at Port A.
while (1)
{
if (!(PINA & (0b10000000)))
{
// Output logic 1 on Pin 2 at Port B
PORTB |= 0b00001100;
_delay_ms(1250);
// Output logic 0 on Pin 2 at Port B
PORTB &= 0b11110011;
_delay_ms(1250);
}
}
}
Downloads