Сб фев 05, 2022 21:28:43
Сб фев 05, 2022 21:46:31
Сб фев 05, 2022 23:07:30
Вс фев 06, 2022 17:08:09
Вс фев 06, 2022 17:13:39
Вс фев 06, 2022 17:16:22
Вс фев 06, 2022 18:08:06
Вс фев 06, 2022 18:34:21
template <typename TPin, bool invert=false>
struct TKey final
{
bool pressed() { return key_pressed?key_pressed=false,true:false; }
inline void Scan()
{
if (bool state=TPin::read(); prev_state!=state)
{
if(!state==invert) key_pressed = true;
prev_state = state;
}
};
private:
volatile bool key_pressed = false;
volatile bool prev_state = invert;
};
using LED = TLed<GPIO::PA5>;
using Key = TKey <GPIO::PC13>;
Key key;
int main()
{
for(;;)
{
if(key.pressed())
LED::Toggle();
}
}
void SysTick_Handler()
{
key.Scan();
}
template <typename TPin, bool invert=false>
struct TLed final
{
static inline void On() { invert ? TPin::clear() : TPin::set(); }
static inline void Off() { invert ? TPin::set() : TPin::clear(); }
static inline void Toggle() { TPin::toggle(); }
static inline bool Read() { return invert ? !TPin::read() : TPin::read(); }
inline TLed& operator=(bool state)
{
if(state) On(); else Off();
return *this;
}
inline operator bool() const { return Read(); }
};
namespace SimpleSTM32Pin
{
template<uint32_t pGPIO, uint32_t Pin>
struct TPin final
{
static inline void set() { base()->BSRR = pin_mask; }
#if (defined(GPIO_BRR_BR0) && (GPIO_BRR_BR0!=GPIO_BSRR_BR0)) // Есть ли BRR?
static inline void clear()
{
base()->BRR = pin_mask;
}
#else
static inline void clear()
{
*((volatile uint16_t*)&base()->BSRR + 1) = pin_mask;
}
#endif
static inline void toggle(){ if (base()->ODR & pin_mask) clear(); else set(); }
static inline void write(bool data)
{
base()->BSRR = (pin_mask << 16) | (data ? pin_mask : 0);
}
static inline bool read() { return base()->IDR & 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 uint32_t pin_mask = 1UL << Pin;
static constexpr auto base() { return (GPIO_TypeDef*)pGPIO; }
};
} //namespace SimpleSTM32Pin
#define MAKE_16PINS(PORT_NAME) \
using P##PORT_NAME##0 = SimpleSTM32Pin::TPin<(uint32_t)GPIO##PORT_NAME##_BASE,0>; \
using P##PORT_NAME##1 = SimpleSTM32Pin::TPin<(uint32_t)GPIO##PORT_NAME##_BASE,1>; \
using P##PORT_NAME##2 = SimpleSTM32Pin::TPin<(uint32_t)GPIO##PORT_NAME##_BASE,2>; \
using P##PORT_NAME##3 = SimpleSTM32Pin::TPin<(uint32_t)GPIO##PORT_NAME##_BASE,3>; \
using P##PORT_NAME##4 = SimpleSTM32Pin::TPin<(uint32_t)GPIO##PORT_NAME##_BASE,4>; \
using P##PORT_NAME##5 = SimpleSTM32Pin::TPin<(uint32_t)GPIO##PORT_NAME##_BASE,5>; \
using P##PORT_NAME##6 = SimpleSTM32Pin::TPin<(uint32_t)GPIO##PORT_NAME##_BASE,6>; \
using P##PORT_NAME##7 = SimpleSTM32Pin::TPin<(uint32_t)GPIO##PORT_NAME##_BASE,7>; \
using P##PORT_NAME##8 = SimpleSTM32Pin::TPin<(uint32_t)GPIO##PORT_NAME##_BASE,8>; \
using P##PORT_NAME##9 = SimpleSTM32Pin::TPin<(uint32_t)GPIO##PORT_NAME##_BASE,9>; \
using P##PORT_NAME##10 = SimpleSTM32Pin::TPin<(uint32_t)GPIO##PORT_NAME##_BASE,10>; \
using P##PORT_NAME##11 = SimpleSTM32Pin::TPin<(uint32_t)GPIO##PORT_NAME##_BASE,11>; \
using P##PORT_NAME##12 = SimpleSTM32Pin::TPin<(uint32_t)GPIO##PORT_NAME##_BASE,12>; \
using P##PORT_NAME##13 = SimpleSTM32Pin::TPin<(uint32_t)GPIO##PORT_NAME##_BASE,13>; \
using P##PORT_NAME##14 = SimpleSTM32Pin::TPin<(uint32_t)GPIO##PORT_NAME##_BASE,14>; \
using P##PORT_NAME##15 = SimpleSTM32Pin::TPin<(uint32_t)GPIO##PORT_NAME##_BASE,15>;
namespace GPIO
{
#ifdef GPIOA
MAKE_16PINS(A)
#endif
#ifdef GPIOB
MAKE_16PINS(B)
#endif
#ifdef GPIOC
MAKE_16PINS(C)
#endif
#ifdef GPIOD
MAKE_16PINS(D)
#endif
#ifdef GPIOE
MAKE_16PINS(E)
#endif
#ifdef GPIOF
MAKE_16PINS(F)
#endif
#ifdef GPIOG
MAKE_16PINS(G)
#endif
#ifdef GPIOH
MAKE_16PINS(H)
#endif
#ifdef GPIOI
MAKE_16PINS(I)
#endif
#ifdef GPIOK
MAKE_16PINS(K)
#endif
} //namespace GPIO
Вс фев 06, 2022 18:41:00
Вс фев 06, 2022 18:45:11
Вс фев 06, 2022 18:45:46
Вс фев 06, 2022 18:47:36
Вс фев 06, 2022 18:48:44
Вс фев 06, 2022 18:50:54
Вс фев 06, 2022 18:56:19
Вс фев 06, 2022 19:01:33
Вс фев 06, 2022 19:03:08
Протеус 8.10 SP3 чип F103, флаг USART_SR_TC не так обрабатывается как в железе, прерывание IDLEIE вообще отсутствует, ну ни как не хочет срабатывать, а в железе срабатывает.tonyk писал(а):Давно в Proteus есть.
Вс фев 06, 2022 19:26:42
Вс фев 06, 2022 19:42:35
Вс фев 06, 2022 20:00:31