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

16×2 LCD(1602A) interfacing with ATMEGA328p 4-bit mode

The code is not tested for output.
Blocks are shown instead of chars.

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

#define LCD_DATA_DDR DDRD
#define LCD_CTRL_DDR DDRB
#define LCD_DataBus PORTD  //Using PD4 to PD7
#define LCD_CTRL_PORT PORTB

//function prototypes
void LCD_Write(unsigned char cmd, unsigned char reg_select);
void LCD_WriteData(char data);
void LCD_WriteCmd(char cmd);
void LCD_Init();

void LCD_Write(unsigned char cmd, unsigned char reg_select)
{
  char  tempCmd;
  if(reg_select == 1)
    LCD_CTRL_PORT |= (1<<PB0);   //Select Data Register
  else
    LCD_CTRL_PORT &= ~(1<<PB0);   //Select Command Register

  //Mask data/cmd
  tempCmd = cmd;
  tempCmd &= 0xf0;// mask Lower 4 bits

  //Set data/cmd
  LCD_DataBus &= 0xf0;           //Set PORTD LSB to HIGH since we use PD4 to PD7
  LCD_DataBus |= tempCmd;        //Set bits in PORTD

  //Send data/cmd
  LCD_CTRL_PORT |= (1<<PB1); // Send a High-to-Low pulse at Enable pin
  _delay_ms(400);
  LCD_CTRL_PORT &= ~(1<<PB1); // Send a High-to-Low pulse at Enable pin

  //******Send next 4bits

  //mask data/cmd
  tempCmd = cmd << 4; //shift left 4 times so the LSB bit can be sent.
  tempCmd &= 0xf0; //mask Lower 4 bits

  //Set data/cmd in bus
  LCD_DataBus &= 0xf0;
  LCD_DataBus |= tempCmd;

  //Send data/cmd
  LCD_CTRL_PORT |= (1<<PB1); // Send a High-to-Low pulse at Enable pin
  _delay_ms(400);
  LCD_CTRL_PORT &= ~(1<<PB1); // Send a High-to-Low pulse at Enable pin

  return ;
}

//Send data by setting RS=1.
void LCD_WriteData(char data)
{
  LCD_Write(data, 1);
}

//Send cmd by setting RS=0.
void LCD_WriteCmd(char cmd)
{
  LCD_Write(cmd, 0);
}

void LCD_Init()
{
    LCD_WriteCmd(0x02); // to initialize LCD in 4-bit mode.
    _delay_ms(1);
    LCD_WriteCmd(0x28); //to initialize LCD in 2 lines, 5X7 dots and 4bit mode.
    _delay_ms(1);
    LCD_WriteCmd(0x01); // clear LCD
    _delay_ms(1);
    LCD_WriteCmd(0x0E); // cursor ON
    _delay_ms(1);
    LCD_WriteCmd(0x80); // —8 go to first line and –0 is for 0th position
    _delay_ms(1);
    return;
}

int main(void)
{
    LCD_DATA_DDR |= 0xf0;  //set the LCD DDRD to output
    LCD_CTRL_DDR |= 0b00000011; //For RS, E

    LCD_Init();
    _delay_ms(30);
    LCD_WriteData('a');
    LCD_WriteData('2');
    while(1)
    {

    }//while
}

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

    }
  }
}

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)

A simple LED blink using Pure C in Arduino

Worked with ATmega8 using Eclipse.

Command used: avrdude    -pm8 -carduino -P/dev/ttyUSB0 -b19200 -v
            -Uflash:w:TestBlink1.hex:a

/*
* main.c
*
*      Author: praveenmax
*/

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

#define BLINK_DELAY_MS 100

int main(void)
{

DDRB |=_BV(DDB5);

while(1)
{

 PORTB |= _BV(PORTB5);
_delay_ms(BLINK_DELAY_MS);

 PORTB &= ~ _BV(PORTB5);
_delay_ms(BLINK_DELAY_MS);
}

}