Ср окт 12, 2022 13:17:37
Ср окт 12, 2022 17:25:36
Ср окт 12, 2022 19:00:57
Ср окт 12, 2022 20:04:43
Ср окт 12, 2022 20:09:53
Ср окт 12, 2022 20:22:00
Ср окт 12, 2022 21:26:35
Ср окт 12, 2022 23:28:58
Пт окт 14, 2022 11:28:48
struct GPIO_TypeDef
{
volatile uint8_t PIN;
volatile uint8_t DDR;
volatile uint8_t PORT;
};
template<uint8_t pGPIO, uint8_t Pin, bool PinToggle=false>
struct TPin final
{
static inline void set()
{
base()->PORT |= pin_mask;
}
static inline void clear()
{
base()->PORT &= ~pin_mask;
}
static inline void toggle()
{
if constexpr (PinToggle) base()->PIN |= pin_mask;
else
{
if (base()->PORT & pin_mask) clear(); else set();
}
}
static inline void write(bool data)
{
if (data) set(); else clear();
}
template<bool data>
static inline void write()
{
if constexpr (data) set(); else clear();
}
static inline bool read()
{
return base()->PIN & pin_mask;
}
inline TPin& operator=(const TPin& pin)
{
write(pin.read());
return *this;
}
inline TPin& operator=(bool value)
{
write(value);
return *this;
}
inline operator bool() const { return read(); }
private:
static constexpr uint8_t pin_mask = 1 << Pin;
static constexpr auto base() { return (GPIO_TypeDef*)pGPIO; }
};
#include <avr/io.h>
#include <avr/interrupt.h>
#include "AVR_simple_pins.hpp"
#include "key.hpp"
#include "led.hpp"
void AVR_Init();
// Подключить светодиоды к контроллеру
using LED1 = TLed<GPIO::PB_0>;
using LED2 = TLed<GPIO::PB_1>;
using LED3 = TLed<GPIO::PB_2>;
// Создать объекты мигающих светодиодов
TBlinkingLed<LED1, 12> bl_led1;
TBlinkingLed<LED2, 8> bl_led2;
TBlinkingLed<LED3, 4> bl_led3;
// Создать объекты кнопок
TKey<GPIO::PC_0> key1;
TKey<GPIO::PC_1> key2;
TKey<GPIO::PC_2> key3;
int main()
{
AVR_Init();
for (;;)
{
if(key1.isKeyPressed())
{
bl_led1.Toggle();
bl_led2.Off();
bl_led3.Off();
}
if (key2.isKeyPressed())
{
bl_led1.Off();
bl_led2.Toggle();
bl_led3.Off();
}
if (key3.isKeyPressed())
{
bl_led1.Off();
bl_led2.Off();
bl_led3.Toggle();
}
}
}
// 30 Гц
ISR(TIMER0_OVF_vect)
{
key1.Scan();
key2.Scan();
key3.Scan();
bl_led1.tic();
bl_led2.tic();
bl_led3.tic();
}
void AVR_Init()
{
DDRB = (1 << PB0) | (1 << PB1) | (1 << PB2); // PB0-PB2 - на выход
PORTC = (1 << PC0) | (1 << PC1) | (1 << PC2); // PС0-PС2 - pullup
TCCR0 = (1 << CS02) | (0 << CS01) | (1 << CS00);
TIMSK = (1 << TOIE0);
sei();
}
Пт окт 14, 2022 11:49:23
Пт окт 14, 2022 11:59:58
for (;;)
{
if(key_board.isKeyPressed())
{
switch(key_board.GetKey())
{
case KEY_1:
bl_led1.Toggle();
bl_led2.Off();
bl_led3.Off();
break;
case KEY_2:
bl_led1.Off();
bl_led2.Toggle();
bl_led3.Off();
break;
case KEY_3:
bl_led1.Off();
bl_led2.Off();
bl_led3.Toggle();
break;
}
}
}
Пт окт 14, 2022 12:46:03
Пт окт 14, 2022 13:01:36
Пт окт 14, 2022 13:15:54
Пт окт 14, 2022 16:55:53
объектно ориентированный способ
Пт окт 14, 2022 18:25:26
Пт окт 14, 2022 21:39:18
Пт окт 14, 2022 21:54:43
Пт окт 14, 2022 22:04:08
Пт окт 14, 2022 22:30:04