#include <stdint.h>
/* --- Periféria címek --- */
#define RCC_BASE 0x40021000UL
#define GPIOA_BASE 0x40010800UL
#define GPIOC_BASE 0x40011000UL
#define REG32(addr) (*(volatile uint32_t *)(addr))
/* --- RCC --- */
#define RCC_APB2ENR REG32(RCC_BASE + 0x18UL)
#define RCC_IOPAEN (1U << 2)
#define RCC_IOPCEN (1U << 4)
/* --- GPIOA --- */
#define GPIOA_CRL REG32(GPIOA_BASE + 0x00UL)
#define GPIOA_IDR REG32(GPIOA_BASE + 0x08UL)
#define GPIOA_ODR REG32(GPIOA_BASE + 0x0CUL)
/* --- GPIOC --- */
#define GPIOC_CRH REG32(GPIOC_BASE + 0x04UL)
#define GPIOC_BSRR REG32(GPIOC_BASE + 0x10UL)
#define GPIOC_BRR REG32(GPIOC_BASE + 0x14UL)
void gpio_init(void)
{
// Órajel engedélyezése GPIOA és GPIOC-hez
RCC_APB2ENR |= RCC_IOPAEN | RCC_IOPCEN;
// --- LED PC13-PC15, output push-pull 2MHz ---
GPIOC_CRH &= ~((0xF << ((13-8)*4)) | (0xF << ((14-8)*4)) | (0xF << ((15-8)*4)));
GPIOC_CRH |= ((0x2 << ((13-8)*4)) | (0x2 << ((14-8)*4)) | (0x2 << ((15-8)*4)));
// --- Gomb PA0-PA2, input pull-up ---
GPIOA_CRL &= ~((0xF << (0*4)) | (0xF << (1*4)) | (0xF << (2*4)));
GPIOA_CRL |= ((0x8 << (0*4)) | (0x8 << (1*4)) | (0x8 << (2*4)));
// belső pull-up engedélyezése
GPIOA_ODR |= (1<<0) | (1<<1) | (1<<2);
// LED alapállapot: kikapcsolva (aktív low)
GPIOC_BSRR = (1<<13) | (1<<14) | (1<<15);
}
int main(void)
{
gpio_init();
while(1)
{
// PA0 -> PC13
if ((GPIOA_IDR & (1<<0))) // gomb lenyomva
GPIOC_BRR = (1<<13); // LED világít
else
GPIOC_BSRR = (1<<13); // LED kikapcsol
// PA1 -> PC14
if ((GPIOA_IDR & (1<<1)))
GPIOC_BRR = (1<<14);
else
GPIOC_BSRR = (1<<14);
// PA2 -> PC15
if ((GPIOA_IDR & (1<<2)))
GPIOC_BRR = (1<<15);
else
GPIOC_BSRR = (1<<15);
}
}
Loading
stm32-bluepill
stm32-bluepill