Blinky code for STM32F429I-DISC1

#include "stm32f4xx.h"
			
void myDelay(int value)
{
	while(value>0)
		value--;
}

int main(void)
{
	RCC->AHB1ENR |= RCC_AHB1ENR_GPIOCEN;

	//pc2 output mode
	GPIOC->MODER |= GPIO_MODER_MODER2_0;

	while(1)
	{
		GPIOC->ODR |=  GPIO_ODR_ODR_2;

		myDelay(500000);

		GPIOC->ODR &= ~GPIO_ODR_ODR_2;

		myDelay(500000);
	}
}

Conditional Check of #define values in C macros

This code shows condition checking of #define values and also token combining using ‘##’

#include <stdio.h>
#define REQMOTHERBOARD 11
#define BOARD_USERBOARD1 11
#define BOARD_USERBOARD2 12
#define MB(board) (REQMOTHERBOARD==BOARD_##board)

int main(void) {

   printf("Testing defines\n");

   #if MB(USERBOARD1)
      printf("Board available!");
   #else
      printf("Board not available");
   #endif

   return 0;
}

LED blink in STM32F4XX using embedded C.

#include "stm32f4xx.h"
void myDelay(int value)
{
	volatile int i,j;

	for(i =0;i< value;i++){ 
		j++; 	
        } 
} 
int main(void) 
{
 	//Enable the GPIO clock for PORT C using AHB and RCC
 	RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN;

	//Set mode register
	GPIOA->MODER |= GPIO_MODER_MODE6_0;

	//SET OTYPER TO PUSH-PULL
	GPIOA->OTYPER &= ~(GPIO_OTYPER_OT_6);

	//SET OSPEEDR
	GPIOA->OSPEEDR |= GPIO_OSPEEDER_OSPEEDR6;

	//PUPDR
	GPIOA->PUPDR &= ~(GPIO_PUPDR_PUPDR6); //~(11) = NO pullup/pulldown

	while(1)
	{
		//turn on led
		GPIOA->ODR |= GPIO_ODR_OD6;

		//WAIT
		myDelay(50000);

		GPIOA->ODR &= ~GPIO_ODR_OD6;

		myDelay(50000);

	}
}

Function Pointers inside Struct in C

#include <stdio.h>

typedef struct{
	int x;
	int y;
	int (*addXPoints) (int x,int y);
	int (*subXPoints) (int x,int y);
}Point;

int addPoints_x(int p1, int p2)
{
		return (p1+p2);
}	

int subtracteXPoints(int p1,int p2)
{
		return (p1-p2);
}

void printPointDetails(Point* temp)
{
		printf("Point [x,y] : %d , %d \n", temp->x, temp->y);
}

int main()
{
	Point p1={30,30};
	
	//another way to initialize
	//p1.x=10; 
	//p1.y=20;
	p1.addXPoints=addPoints_x;
	p1.subXPoints=subtracteXPoints;
	
	printPointDetails(&p1);
	
	printf("Total X Value of p1,p2 : %d\n",p1.addXPoints(p1.x,p1.x));
	printf("Total X Value of p1,p2 : %d\n",p1.subXPoints(p1.x,p1.x));
	

	return 0;
}

Constant pointers and Pointers to constant in C

#include <stdio.h>
int main()
{
	int val = 250;
	int val2 =300;

        // ptrInt is a pointer to integer constant
        const int* ptrInt = &val; //Pointer to Constant

        // ptrInt1 is a constant pointer to integer
        int* const ptrInt1 = &val; //Constant Pointer

        *ptrInt = 500; //pointer to constant wont allow

	ptrInt1 = &val2; //constant pointer wont allow

	return 0;
}