//LED Blinking using IAR Embedded Workbench
// connect LED in pin Port B 0 to 3.
#include "stm32f4xx.h"
#include "stm32f4xx_rcc.h"
#include "stdio.h"
void LED_init();
void delay(long int a)
{
long int b,c;
for(b=0;b<a;b++)
for(c=0;c<a;c++);
}
void main()
{
LED_init();
while(1)
{
GPIOB->ODR=0X0000000F; // Set specific pins high
delay(3000);
GPIOB->ODR=0X00000000; // Set all pins low
delay(3000);
}
}
void LED_init()
{
RCC->AHB1ENR |= (1 << 1); // Bit 2 enables GPIOB
GPIOB->MODER=0X55555555; // Set all pins to output mode
GPIOB->OTYPER=0X00000000; // Set pins to push-pull
GPIOB->PUPDR=0X00000000; // No pull-up/pull-down
GPIOB->OSPEEDR=0XAAAAAAAA; // High-speed mode
}
// relay and LED
//connect LED and relay in pin Port B 0 to 7.
#include "stm32f4xx.h"
#include "stm32f4xx_rcc.h"
#include "stdio.h"
void delay() // Simple delay
{
for(int i=0; i<0x3FFF; i++);
for(int j=0; j<0x3FFF; j++);
}
void mx_pinout_config()
{
RCC->AHB1ENR |= (1 << 1); // Bit 0 enables GPIOA
GPIOB->MODER=0X55555555; // Set all pins to output mode
GPIOB->OTYPER=0X00000000; // Set pins to push-pull
GPIOB->PUPDR=0X00000000; // No pull-up/pull-down
GPIOB->OSPEEDR=0XFFFFFFFF; // High-speed mode
GPIOB->ODR=0X00000000; // Initialize output as low
}
int main()
{
mx_pinout_config();
while(1)
{
GPIOB->ODR=0X000000FF; // Set specific pins high
delay();
GPIOB->ODR=0X00000000; // Set all pins low
delay();
}
}
//buzzer at portC 3rd pin (PC3 – pin diagram)
#include "stm32f4xx.h"
#include "stm32f4xx_rcc.h"
#include "stdio.h"
#define BUZZER_ON ((GPIOC->ODR=0X08))
#define BUZZER_OFF ((GPIOC->ODR=0X00))
void buzzer_init()
{
RCC->AHB1ENR |= (1 << 2); // Bit 2 enables GPIOC
GPIOC->MODER=0X55555555; // Set all pins to output mode
GPIOC->OTYPER=0X00000000; // Set pins to push-pull
GPIOC->PUPDR=0X00000000; // No pull-up/pull-down
GPIOC->OSPEEDR=0XAAAAAAAA; // High-speed mode
}
void delay()
{
for(int i=0; i<0x3FFF; i++);
for(int j=0; j<0x3FFF; j++);
}
void main()
{
buzzer_init();
while(1)
{
BUZZER_ON; // Set specific pins high
delay();
BUZZER_OFF; // Set all pins low
delay();
}
}