0

I have an STM32f103c8 with 8Mhz SystemClock i configured My Systick to make delay every 1 seconde , i have a Button Connected to PA1 (Input Pull up) and Two Leds PB8 and PB9 i want when i press first time Button it starts blinking LEDs every 1 seconde for ever but when i press the button another time it turn off both LEDs i made a code and i want to see if it works because i don't really have the carte now ,

#include "stm32f10x.h"
int main(void){
    int S=0;
    RCC->APB2ENR|=0x0000000C;
    GPIOA->CRL=0;
    GPIOB->CRH=0;
    GPIOA->CRL|=0x00000880;
    GPIOA->ODR=1;
    GPIOB->CRH|=0x00000011;
    SysTick->LOAD=8000000;
    SysTick->VAL=0;
    SysTick->CTRL=5;
    while(1){
        if(!(GPIOA->IDR&(1<<1))){
            while(1){
        if((SysTick->CTRL&(1<<16))){
            if(S==0){
            GPIOB->BSRR=(1<<8);
            GPIOB->BSRR=(1<<25);
                S=1;
            }
                else{
                GPIOB->BSRR=(1<<24);
            GPIOB->BSRR=(1<<9);
                    S=0;
                }
            }
        if(!(GPIOA->IDR&0x02)){
        break;
        }
        }
                GPIOB->BSRR=(1<<24);
        GPIOB->BSRR=(1<<25);
        }
    }
}

I tried to use it in Proteus but the button doesn't work there i don't know why so i came here to see if my code is correct or poteus is not good

Issylin
  • 353
  • 2
  • 3
  • 11
Tchalago
  • 1
  • 1
  • If you expect people to read your code, it would serve you well to make it easy and indent it conventionally and consistently. – Clifford Jun 24 '23 at 19:11
  • If it is the button that does not work, why it the title about LEDs and SysTick? Very strange way to use SysTick. You would normally set it to reload at say 1ms intervals, then increment a counter in the SysTick interrupt handler, then poll the counter. That will give you a much more flexible time base for other code. – Clifford Jun 24 '23 at 19:23
  • Your code is very difficult to read. Please, format it well, there are too many curly braces. Also, from programming manual, Systick CTRL register: Bit 16 COUNTFLAG: Returns 1 if timer counted to 0 **since last time this was read**. I would make a dummy read to it in the beginning just not to contradict the doc. GPIOs configured correctly? Can you blink them without SysTick? – Ilya Jun 24 '23 at 19:49
  • I can understand why you would not use the HAL as it contains several checks in the functions, however, the LL precisely exists to give readable code and doing this kind of stuff you want without checks (or at least without expensive ones). Furthermore, it's safer to use than directly writing into registers – Issylin Jul 03 '23 at 18:07
  • The entire loop logic seems broken to me. You check, if the button is not pressed, if not, you enter while(1), where if the same button is not pressed, it exits. The way it seems to me, you enter the internal while(1) loop, and then do nothing inside it because no IFs are met, and immediately exit it without doing anything. A mess of curly braces makes it extremely difficult to read. Also, for whatever reason you configure PA2 alongside PA1, but never use it. – Ilya Jul 20 '23 at 12:11

0 Answers0