Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "STM32L1xx.h"
- #define PA1 0x02 // Second bit
- #define PA2 0x04 // Fourth bit
- #define SWITCH1 PA1
- #define SWITCH2 PA2
- #define PC0 0x01
- #define PC1 0x02
- #define PC2 0x04
- #define PC3 0x08
- #define OUTPUT_PINS (PC0 | PC1 | PC2 | PC3)
- void switch_setup() {
- RCC->AHBENR |= 0x01; // Enable GPIOA
- GPIOA->MODER &= ~(GPIO_MODER_MODER1 | GPIO_MODER_MODER2); // set PA[1:2] to inputs
- }
- void output_setup() {
- RCC->AHBENR |= 0x04;
- GPIOC->MODER &= ~(GPIO_MODER_MODER0 | GPIO_MODER_MODER1 | GPIO_MODER_MODER2 | GPIO_MODER_MODER3); // clear PC[0:3] modes
- GPIOC->MODER |= GPIO_MODER_MODER0_0 | GPIO_MODER_MODER1_0 | GPIO_MODER_MODER2_0 | GPIO_MODER_MODER3_0; // set PC[0:3] to outputs
- }
- void set_output(uint8_t count) {
- GPIOC->ODR = (GPIOC->ODR & ~OUTPUT_PINS) | (count & OUTPUT_PINS); // Set PC[0:3] to count
- }
- uint8_t read_switch1() {
- return GPIOA->IDR & SWITCH1;
- }
- uint8_t read_switch2() {
- return GPIOA->IDR & SWITCH2;
- }
- void delay() {
- // delay for roughly 500 ms
- for (int i = 0; i < 20; i++) {
- for (int j = 0; j < 10000; j++) {
- __nop();
- }
- }
- }
- static uint8_t count = 0;
- int main() {
- switch_setup();
- output_setup();
- count = 0;
- while (1) {
- if (read_switch1()) {
- // counter is enabled
- if (read_switch2()) {
- // counting down if value is 1
- count--;
- } else {
- // counting up otherwise
- count++;
- }
- }
- set_output(count);
- delay();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement