Compare commits

..

4 Commits

9 changed files with 190 additions and 2 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
.pio

14
platformio.ini Normal file
View File

@ -0,0 +1,14 @@
; PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html
[env:nanoatmega328]
platform = atmelavr
board = nanoatmega328
framework = arduino

View File

@ -0,0 +1,47 @@
//
// Created by servostar on 27.10.23.
//
#include "button.h"
#ifdef BUTTON_INTERRUPT
ISR(INT0_vect)
{
PORTB = 0;
}
ISR(INT1_vect)
{
PORTB = 0xff;
}
void init() {
// set B as output
DDRB = 0xff;
PORTB = 0x00;
// set Port D to input (bit 2/3, remember bit 0/1 are for UART)
SET_INPUT(DDRD, DDD2);
SET_INPUT(DDRD, DDD3);
// put 5V on input (make it pull up resistor)
SET_BIT(PORTD, DDD2);
SET_BIT(PORTD, DDD3);
// configure interrupt mode
SET_INTERRUPT_MODE(0, FALLING);
SET_INTERRUPT_MODE(1, FALLING);
// enable interrupt 0 and 1
ENABLE_INTERRUPT(0);
ENABLE_INTERRUPT(1);
// enable interrupts globally
sei();
}
void loop() {
}
#endif

View File

@ -0,0 +1,13 @@
//
// Created by servostar on 27.10.23.
//
#ifndef ARDUINO_BUTTON_H
#define ARDUINO_BUTTON_H
#include "prelude.h"
void init();
void loop();
#endif //ARDUINO_BUTTON_H

View File

@ -0,0 +1,47 @@
//
// Created by servostar on 27.10.23.
//
#include "button.h"
// ---------------------------------------
//
// DDR set bit of port to either input or output
// PORTx set bit of port value
// PINx read voltage of bit of port D
#ifdef BUTTON_POLL
void init() {
// ---------------------------------------
// set Port B to output
// we will write through B5
SET_OUTPUT(DDRB, DDB5);
CLR_BIT(PORTB, DDB5);
// ---------------------------------------
// setup input port
// we will read through D2/D3
// set Port D to input (bit 2/3, remember bit 0/1 are for UART)
SET_INPUT(DDRD, DDD2);
SET_INPUT(DDRD, DDD3);
// put 5V on input (make it pull up resistor)
SET_BIT(PORTD, DDD2);
SET_BIT(PORTD, DDD3);
}
void loop() {
// poll voltage of pin 2 of port D
if (!GET_BIT(PIND, PIND2)) {
SET_BIT(PORTB, DDB5);
}
// poll voltage of pin 3 of port D
if (!GET_BIT(PIND, PIND3)) {
CLR_BIT(PORTB, DDB5);
}
}
#endif

13
src/button_poll/button.h Normal file
View File

@ -0,0 +1,13 @@
//
// Created by servostar on 27.10.23.
//
#ifndef ARDUINO_BUTTON_H
#define ARDUINO_BUTTON_H
#include "prelude.h"
void init();
void loop();
#endif //ARDUINO_BUTTON_H

View File

@ -2,9 +2,15 @@
extern void init(); extern void init();
extern void loop(); extern void loop();
#include "blink/blink.h" //#include "blink/blink.h"
//#include "button_poll/button.h"
//#include "button_interupt/button.h"
#include "trafficlight/trafficlight.h"
#define BLINK //#define BLINK
//#define BUTTON_POLL
//#define BUTTON_INTERRUPT
#define TRAFFIC_LIGHT
int main() { int main() {
init(); init();

View File

@ -0,0 +1,34 @@
//
// Created by servostar on 27.10.23.
//
#include "trafficlight.h"
#ifdef TRAFFIC_LIGHT
void init() {
// set Port B bit 0,1,2 to output mode
DDRB = 0b00000111;
// set port B bit 2 (red) to one
PORTB = 0b00000100;
}
void loop() {
_delay_ms(200);
// set red orange
PORTB ^= 0b00000010;
_delay_ms(200);
// set green
PORTB ^= 0b00000111;
_delay_ms(500);
// set orange
PORTB ^= 0b00000011;
_delay_ms(500);
// set to red
PORTB ^= 0b00000110;
}
#endif

View File

@ -0,0 +1,13 @@
//
// Created by servostar on 27.10.23.
//
#ifndef ARDUINO_TRAFFICLIGHT_H
#define ARDUINO_TRAFFICLIGHT_H
#include "prelude.h"
void init();
void loop();
#endif //ARDUINO_TRAFFICLIGHT_H