Help with pushbutton/LED needed

Post here first, or if you can't find a relevant section!
ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: Help with pushbutton/LED needed

Post by ag123 »

on stm32f4, there are things pretty 'advanced'
https://www.st.com/resource/en/referenc ... 096844.pdf
there is something called SYSCFG registers, you could try playing with them if you want
something like

SYSCFG->MEMRMP = 1;
NVIC_SystemReset();

not tested - speculative, u'd need to review ref manual and dig through the headers about it.

too complicated? get the st-link v2 it does the job
btw, you can't really do much stm32 without a good review in the ref manual, keep that handy
TurboTimmy
Posts: 22
Joined: Wed Dec 22, 2021 9:43 pm

Re: Help with pushbutton/LED needed

Post by TurboTimmy »

ag123 wrote: Fri Dec 24, 2021 6:14 pm on stm32f4, there are things pretty 'advanced'
https://www.st.com/resource/en/referenc ... 096844.pdf
there is something called SYSCFG registers, you could try playing with them if you want
something like

SYSCFG->MEMRMP = 1;
NVIC_SystemReset();

not tested - speculative, u'd need to review ref manual and dig through the headers about it.

too complicated? get the st-link v2 it does the job
btw, you can't really do much stm32 without a good review in the ref manual, keep that handy
Thank you. At the moment I am still very much a beginner. I am learning alot as I go along. I tend to learn best by actually building different projects (found online) and learning what each thing does as I progress though the projects. I like a simple life and am very much a "plug and play" kind of person, so the more simple something can be, the better.

Most of my projects involve button press, Keyboard.press and sometimes some displays are involved. I will consentrate on those and try my best to get a grip of the basics for STM32.

On that note, I have found that the default Adafruit libraries seem to work OK with my board and both the ST7735 and ILI9431. I don't know if an adapted library will work better for these or not.

Lots to learn, and I am very much enjoying my journey so far :D
GonzoG
Posts: 403
Joined: Wed Jan 15, 2020 11:30 am
Answers: 27
Location: Prudnik, Poland

Re: Help with pushbutton/LED needed

Post by GonzoG »

TurboTimmy wrote: Fri Dec 24, 2021 6:09 pm Thanks for that. I have managed to do that since day one, but I cannot for the life of me get it to download a sketch via the USB socket (on the board). I always have to use UART. It was claimed that I could use the USB socket, but this doesnt seem to be the case. So, to use the board I have to:

1: Connect via UART to gnd, 3.3v, A9, A10
2: Download sketch
3: Dissconnect UART
4: connect USB-C to run the program (I use alot ok serial read/write)

I would like to do away with having to switch between UART and USB, and be able to download via the USB socket (in the same we I do with the Arduino boards)

On a side note, I cannot download a new sketch to the board until I have used STM32 CubeProgrammer to earse everything first. Not sure what's going on there.
If you boot into DFU then you can upload sketch via USB.
Select "STM32CubeProgrammer (DFU)" as an upload method, boot MCU into DFU (should be seen in Windows device manager as "STM32 BOOTLOADER"), hit "upload".
It's been working for me for nearly 2 years.
TurboTimmy
Posts: 22
Joined: Wed Dec 22, 2021 9:43 pm

Re: Help with pushbutton/LED needed

Post by TurboTimmy »

GonzoG wrote: Fri Dec 24, 2021 6:50 pm
TurboTimmy wrote: Fri Dec 24, 2021 6:09 pm Thanks for that. I have managed to do that since day one, but I cannot for the life of me get it to download a sketch via the USB socket (on the board). I always have to use UART. It was claimed that I could use the USB socket, but this doesnt seem to be the case. So, to use the board I have to:

1: Connect via UART to gnd, 3.3v, A9, A10
2: Download sketch
3: Dissconnect UART
4: connect USB-C to run the program (I use alot ok serial read/write)

I would like to do away with having to switch between UART and USB, and be able to download via the USB socket (in the same we I do with the Arduino boards)

On a side note, I cannot download a new sketch to the board until I have used STM32 CubeProgrammer to earse everything first. Not sure what's going on there.
If you boot into DFU then you can upload sketch via USB.
Select "STM32CubeProgrammer (DFU)" as an upload method, boot MCU into DFU (should be seen in Windows device manager as "STM32 BOOTLOADER"), hit "upload".
It's been working for me for nearly 2 years.
I have tried doing just that, but I cannot get it to work at all. I have tried using different USB cables, different USB ports, and even different computers. I think there must be something wrong with the board
TurboTimmy
Posts: 22
Joined: Wed Dec 22, 2021 9:43 pm

Re: Help with pushbutton/LED needed

Post by TurboTimmy »

Just a quick update on the whole USB problem. I tried another board and it worked right away with no problems. Looks like I had a duff board that wasnt allowing the USB to work. It's a shame that I had this problem at the very start of my STM32 journey, it was starting to put me off a little. At least it is now worked out and I can start to learn as much as I can. :D
pwrgreg007
Posts: 13
Joined: Sun Jan 09, 2022 2:56 am
Location: LaGrange, GA

Re: Help with pushbutton/LED needed

Post by pwrgreg007 »

It appears in your code that you're reading the value incorrectly (as already pointed out), and you don't output a value to the LED if the switch is released (up).
Try this code. It reads the switch on PB2 and outputs the opposite value to PC13. The while loops are window dressing, to keep it from repeatedly outputting to the LED if there is no change in button state. It really isn't necessary. And this code doesn't use any switch debounce logic.

Code: Select all

void setup() {
  pinMode(PC13, OUTPUT);
  pinMode(PB2, INPUT_PULLUP);
}

void loop() {
  // is switch depressed?
  if (digitalRead(PB2) == LOW)
  {
    // yes - light LED
    digitalWrite(PC13, LOW);		// use HIGH here if LED is on with HIGH
    // wait for switch to be released
    while(digitalRead(PB2) == LOW)
    { }
  }
  else
  {
    // no - clear LED
    digitalWrite(PC13, HIGH);		// use LOW here if LED is off with LOW
    // wait for switch to be depressed
    while(digitalRead(PB2) == HIGH)
    { }
  } 
}
User avatar
Mabruntan
Posts: 1
Joined: Tue Feb 15, 2022 8:08 am

Re: Help with pushbutton/LED needed

Post by Mabruntan »

Hi! Did you manage to make your code work? Now I'm learning the basics of programming, and everything related to the code interests me. Tell me, what textbooks for a beginner can you recommend to me? Or there may be useful courses in programming languages. I am currently studying HTML and CSS because I understand that this is a base in web programming. And I also managed to set up the code of my smart light bulbs so that my favorite song plays when I enter the apartment. It turned out to be very simple. And so far, this is all my progress in learning.
lewiscamron72
Posts: 1
Joined: Thu Mar 03, 2022 5:22 pm

Re: Help with pushbutton/LED needed

Post by lewiscamron72 »

I see your point.

Thank you for explaining it.

Managed to get it downloaded to the board and working, although the Serial.print values seem to be backward.

Anyway, now that I have a working button press, it is time to move onto the next code snippet.

I am trying to put together small examples of different things that I will ultimately put together.

So we have button press sorted, now to move onto ST7735 display.

Thank you all fro your help

I have no doubt there will be more posts to come.
GonzoG
Posts: 403
Joined: Wed Jan 15, 2020 11:30 am
Answers: 27
Location: Prudnik, Poland

Re: Help with pushbutton/LED needed

Post by GonzoG »

lewiscamron72 wrote: Thu Mar 03, 2022 5:29 pm Managed to get it downloaded to the board and working, although the Serial.print values seem to be backward.
They are. You've used pulled-up input, so default state (button depressed) is 1 (true).
When you press button, you short pin to ground, so it reads 0 (false).
To reverse it you can use pull-down resistors (INPUT_PULLDOWN) or simply negate read value:

Code: Select all

 uint8_t button_state = !digitalRead(PB2);
Post Reply

Return to “General discussion”