I tried to control the LED on board using external switch on breadboard but it does not? The switch is connected to port d number 9, i tried to make it so that once i press a switch, the green LED comes on. But for some strange reason it does not work, i tried to fiddle around with it but no luck. How could i fix it so that it works?
Here is my code:
include <stdio.h>
include "STM32F4xx.h"
void Soft_Delay(void);
void LED_Init_1(void);
void Push_button (void);
#define ITM_Port8(n) (*((volatile unsigned char *)(0xE0000000+4*n)))
#define ITM_Port16(n) (*((volatile unsigned short*)(0xE0000000+4*n)))
#define ITM_Port32(n) (*((volatile unsigned long *)(0xE0000000+4*n)))
#define DEMCR (*((volatile unsigned long *)(0xE000EDFC)))
#define TRCENA 0x01000000
struct __FILE { int handle; };
FILE __stdout;
FILE __stdin;
int fputc(int ch, FILE *f) {
if (DEMCR & TRCENA) {
while (ITM_Port32(0) == 0);
ITM_Port8(0) = ch;
}
return(ch);
}
int main (void) {
LED_Init_1();
Push_button ();
while(1)
{
if((GPIOD->IDR &= ~ (1UL << 9))==1)
{
GPIOD->ODR |= (1UL << 12);
}
else if((GPIOD->IDR &= ~ (1UL << 9))==0)
{
GPIOD->ODR &= ~(1UL << 12);
}
}
}
void LED_Init_1 (void) {
RCC->AHB1ENR |= ((1UL << 3) );
GPIOD->MODER &= ~((3UL << 2*12));
GPIOD->MODER |= ((1UL << 2*12) );
GPIOD->OTYPER &= ~((1UL << 12) );
GPIOD->OSPEEDR &= ~((3UL << 2*12) );
GPIOD->OSPEEDR |= ((2UL << 2*12) );
GPIOD->PUPDR &= ~((3UL << 2*12) );
GPIOD->PUPDR |= ((1UL << 2*12) );
}
void Push_button (void) {
RCC->AHB1ENR |= ((1UL << 3) );
GPIOD->MODER &= ~((3UL << 9));
}
void Soft_Delay(void) {
uint32_t i=10000000;
while(i>0)
{
i--;
}
}
Thanks
Looking at this line (one of two similar) which seems to be checking the input:
if((GPIOD->IDR &= ~ (1UL << 9))==1)
Firstly I dispute why you are writing the port value back with &=
Secondly, why would an input on bit 9 equate with 1
?
Thirdly, the else
testing (apparently) for the opposite state is superfluous - a plain else
will do. I don't have your system or tools, so I am guessing this code.
if((GPIOD->IDR & (1UL << 9)) != 0)
GPIOD->ODR |= (1UL << 12);
else
GPIOD->ODR &= ~(1UL << 12);