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);
	}
}

LED blink using STM32F769I discovery board

#include "stm32f7xx.h"

void myDelay(int value)
{
	volatile int i,j;
	
	for(i =0;i< value;i++){
		j++;
	}
}

int main(void)
{
	
	//Enable the GPIO clock using AHB and RCC (PIN : PJ 3)
	RCC->AHB1ENR |= RCC_AHB1ENR_GPIOJEN;
	
	//Set mode register to OUTPUT mode for pin D7
	GPIOJ->MODER |= GPIO_MODER_MODER3_0;
	
	//SET OTYPER TO PUSH-PULL
//	GPIOJ->OTYPER &= ~(GPIO_OTYPER_OT_3);
	
	//SET OSPEEDR
	//GPIOJ->OSPEEDR |= GPIO_OSPEEDER_OSPEEDR3_0;
	
	//PUPDR
//	GPIOJ->PUPDR &= ~(GPIO_PUPDR_PUPDR3); //~(11) = NO pullup/pulldown
	
	while(1)
	{
			//turn on led 
		GPIOJ->ODR |= GPIO_ODR_ODR_3;

		//WAIT
		myDelay(40000);
		
		GPIOJ->ODR &= ~ GPIO_ODR_ODR_3;
		
		myDelay(40000);
		
	}
}

Fast PWM using atmega2560 .

#define F_CPU 16000000
#include <avr/io.h>
#include <util/delay.h>
#define PIN_OCOB PG5

void initPWM();

int main(void)
{
	
     initPWM();	
		
    while (1) 
    {
	//set the duty cycle
	OCR0B = 128;//50% duty cycle
		
	_delay_ms(10);
    }
}

void initPWM()
{
	
	//        FAST PWM                | CLEAR OCOA ON COMPARE
	TCCR0A |= (1<<WGM01) | (1<<WGM00) | (1<<COM0B1) | (0<<COM0B0);
	
	//CS = NO PRESCALE  
	TCCR0B |= (1<<CS00) ;
	
	//Enable OCOA PWM pin	
	DDRG |= 1<<PIN_OCOB;
	
}

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);

	}
}

LED blink looping in C

#include <avr/io.h>
#include <util/delay.h>

int main(void)
{
  DDRD = 0b11111111; //led test
  PORTD = 0b00000000;
  unsigned char pinNo = 0;
  while(1)
  {
    while(pinNo<8)
    {
       PORTD = 0x00;
       PORTD |= 1<<pinNo;
       if(pinNo>=7)
          pinNo=0;
       else
          pinNo++;

      _delay_ms(500);

    }
  }
}

Reading input pin in avr using Embedded C

#include <avr/io.h>
#include <util/delay.h>

int main(void)
{
  DDRD |=  0<<PD2;//PD2 is set to Input
  PORTD |= 1<<PD2;//pull-up activated.So, PD2 is high by default

  DDRB |= 1<<PB5 ;
  PORTB |= 0<<PB5 ;
  char portDVal = 0;
  while(1)
  {

      _delay_ms(100);
      portDVal = PIND;

      if( portDVal & (1<<PD2) )//if PD2 is HIGH(value read is 1)
      {
        PORTB &= ~(1<<PB5);
        _delay_ms(50);
      }
      else//if PD2 is LOW(value read is 0)
      {
        PORTB |= (1<<PB5);
        _delay_ms(50);
      }
  }
}

Fast PWM based Muli-color RGB dimming using ATMEGA328 : Induino R5 in C

/*
 * main.c
 */
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>

double dutyCycle = 0;
int colorSet = 0;

int main(void)
{
    DDRB|= 1<<PB4; //testing

    //Timer setting (Fast PWM, non-inverting)
    TCCR0A = (1 << WGM00) | (1 << WGM01) | (1 << COM0A1) | (1 << COM0B1);
    TCCR0B = (1 << CS00);//CLOCK SELECT : set prescaler to 0

    TCCR2A = (1 << WGM20) | (1 << WGM21) | (1 << COM2A1) | (1 << COM2B1);
    TCCR2B = (1 << CS20);//CLOCK SELECT : set prescaler to 0

    DDRD|= 1<<PD6;//Red (OCOA)
    DDRD|= 1<<PD3;//Green (OC2B)
    DDRD|= 1<<PD5;//Blue (OCOB)

    while(1)
    {
      //testing
        PORTB|=1<<PB4;         _delay_ms(30);         dutyCycle+=1;         if(dutyCycle > 100)
        {
            dutyCycle=0;

            //increment colorSet
            colorSet++;
            if(colorSet>6)
            {
              //reset to start color
              colorSet=0;
            }

            //testing
            if( PORTB & (1<<PB4) ) // if PB4 set
                PORTB&=~1<<PB4; //unset it      

            _delay_ms(1000);

        }

        if(colorSet==0)
          setLEDValues(255,0,0); //RED
        else
        if(colorSet==1)
          setLEDValues(0,255,0); //GREEN
        else
        if(colorSet==2)
          setLEDValues(0,0,255); //BLUE
        else
        if(colorSet==3)
          setLEDValues(255,255,255); //WHITE
        else
        if(colorSet==4)
          setLEDValues(0,255,255); //CYAN
        else
        if(colorSet==5)
          setLEDValues(255,0,255); //PINK
        else
        if(colorSet==6)
          setLEDValues(255,255,0); //YELLOW

    }
}

void setLEDValues(int r,int g,int b)
{
    OCR0A = (dutyCycle/100)*r;  //RED led
    OCR2B = (dutyCycle/100)*g;  //GREEN led
    OCR0B = (dutyCycle/100)*b;  //BLUE led
}

/*
 * TCCR0A, TCCR0B - Timer/Counter Control Registers
 * OCOA  , OCOB   - Output Compare Pins
 * OCR0A , OCR0B  - Output Compare Registers
 * CS01           - Clock Select bit
 * COM0A1, COM0B1 - Compare Output Mode bits
 *
 * /
 */

Timer/Counter Registers in ATMEGA328

 

The following are the timer registers present int atmega328 microcontroller.
  TCNT0 – Timer/Counter Register (8-bit)
  OCR0A – Output Compare Register A
  OCR0B – Output Compare Register B
  TCCR0A/B – Timer/Counter Control Registers
  TIMSK0 – Timer/Counter Interrupt Mask Register
     TOV interrupt
     Compare A&B interrupts
  TIFR0 – Timer/Counter Interrupt Flag Register
     TOV interrupt
     Compare A&B interrupts




timer_atmega328

PWM on RGB led using ATMEGA328 : Induino – R5

/*
 * main.c
 */
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>

double dutyCycle = 0;

int main(void)
{
    DDRB|= 1<<PB4; //testing

    //Timer setting (Fast PWM, non-inverting)
    TCCR0A = (1 << WGM00) | (1 << WGM01) | (1 << COM0A1) | (1 << COM0B1);
    TCCR0B = (1 << CS00);//CLOCK SELECT : set prescaler to 0

    TCCR2A = (1 << WGM20) | (1 << WGM21) | (1 << COM2A1) | (1 << COM2B1);
    TCCR2B = (1 << CS20);//CLOCK SELECT : set prescaler to 0
    
    DDRD|= 1<<PD6;//Red (OCOA)
    DDRD|= 1<<PD3;//Green (OC2B)
    DDRD|= 1<<PD5;//Blue (OCOB)
    
    while(1)
    {
      //testing
        PORTB|=1<<PB4;
        _delay_ms(30);


        //PWM - RED
  
        dutyCycle+=1;
  
        if(dutyCycle > 100)
        {
          dutyCycle=0;
            
          if( PORTB & (1<<PB4) ) // if PB4 set
              PORTB&=~1<<PB4; //unset it      
              
          _delay_ms(500);
  
        }
        OCR0A = (dutyCycle/100)*255;  //RED led
        OCR0B = (dutyCycle/100)*255;  //BLUE led
        OCR2B = (dutyCycle/100)*255;  //GREEN led
    }
}

/*
 * TCCR0A, TCCR0B - Timer/Counter Control Registers
 * OCOA  , OCOB   - Output Compare Pins
 * OCR0A , OCR0B  - Output Compare Registers
 * CS01           - Clock Select bit
 * COM0A1, COM0B1 - Compare Output Mode bits
 *
 */

 

RGB LED blinking in Induino R5

/*
 * main.c
 */

#include <avr/io.h>
#include <util/delay.h>
//#define PIN 0
int main(void)
{
    DDRB|= 1<<PB4;
    DDRB|= 1<<PB5;
    DDRD|= 1<<PD3;//Green
    DDRD|= 1<<PD5;//Blue
    DDRD|= 1<<PD6;//Red

    while(1)
    {
        PORTB|=1<<PB4;
        _delay_ms(100);
        if( PORTB & (1<<PB4) ) // if PB4 set
            PORTB&=~1<<PB4; //unset it

        PORTB|=1<<PB5;
        _delay_ms(100);
        if( PORTB & (1<<PB5) )
            PORTB&=~1<<PB5;

        PORTD|=1<<PD5;
        //PORTD|=1<<PD5;
        //PORTD|=1<<PD6;

        _delay_ms(40);

        PORTD&=~1<<PD5;
        //PORTD&=~1<<PD5;
        //PORTD&=~1<<PD6;
    }
}

//PORTB&=~( (1<<3)|(1<<4)|(1<<5) );//Bitwise NOT (set the OUTPUT as LOW)