Tuesday 28 June 2016

Led Matrix with LPC2148

 WAP to Display 'R' in LED Matrix:

#include<lpc21xx.h>
void delay(int a);
int main()
{
PINSEL0=0x000000000;
PINSEL1=0x000000000;   
IO0DIR=0xffffffff;
    while(1)
    {
       
          IO0SET=(4063<<0);
          delay(100);
          IO0CLR=(4063<<0);
          delay(1);
          IO0SET=(4017<<0);
            delay(100);
      IO0SET=(4017<<0);
          delay(1);
          IO0CLR=(3953<<0);
          delay(100);
          IO0CLR=(3953<<0);
          delay(1);
          IO0SET=(3839<<0);
            delay(100);
          IO0CLR=(3839<<0);
           delay(1);
          IO0SET=(3572<<0);
            delay(100);
          IO0CLR=(3572<<0);
          delay(1);
          IO0SET=(3058<<0);
            delay(100);
          IO0CLR=(3058<<0);
            delay(1);
          IO0SET=(2033<<0);
            delay(100);
            IO0CLR=(2033<<0);
}
}
void delay(int a)
{
    int i;
    for(i=0;i<a;i++);
}

14 segment and16 segment using ARM

WAP to Display 'A' on 14 segment:

#include<lpc21xx.h>
//#include"delay.h"

void delay(unsigned int c)
{
    unsigned int a;
for(a=1;a<=60000;a++);
}
int main()
{ int i;
  PINSEL0=0x00000000;
    PINSEL1=0x00000000;
    IO0DIR=0xffffffff;
   
while(1)   
{
    i=0;
while(i<100)
{   
    IO0SET=0x2633;
    delay(100);
    IO0CLR=0x2633;
    delay(1);
    i++;
}
while(i<200)
{
    IO0SET=0x2237;
    delay(100);
    IO0CLR=0x2237;
    delay(1);
    i++;
}

while(i<300)
{
    IO0SET=0x0176;
    delay(100);
    IO0CLR=0x0176;
    delay(1);
    i++;
}
}
}

WAP to Display 'RAM' on 14 segment using multiplexing:

#include<lpc21xx.h>
//#include"delay.h"

void delay(unsigned int c)
{
    unsigned int a;
  for(a=1;a<=60000;a++);
}

int main()
{
  PINSEL0=0x00000000;
    PINSEL1=0x00000000;
    IO0DIR=0xffffffff;
   
while(1)   
{

    IO0SET=0x000e0000;
    IO0SET=0x2633;
    delay(100);
    IO0CLR=0x2633;
    IO0CLR=0x000e0000;


    IO0SET=0x000d0000;
    IO0SET=0x2237;
    delay(100);
    IO0CLR=0x2237;
    IO0CLR=0x000d0000;


  IO0SET=0x000b0000;
    IO0SET=0x0176;
    delay(100);
    IO0CLR=0x0176;
  IO0CLR=0x000b0000;
   
    IO0SET=0x00070000;
    IO0SET=0x2237;
    delay(100);
    IO0CLR=0x2237;
    IO0CLR=0x00070000;

}
}





Thursday 9 June 2016

I2C using AVR:

I2C using AVR:

Program for AVR(MOSI):

Master:
#include<avr/io.h>
#include<util/delay.h>
void main()
{
DDRD=0xFF;
while(1)
{
TWBR=0xFF;
TWCR=(1<<TWEA)|(1<<TWSTA)|(1<<TWEN)|(1<<TWINT);
while((TWCR&(1<<TWINT))==0);
if((TWSR&0xF8)!=0x08)
PORTD=0x01;

TWDR=0x40;
TWCR=(1<<TWINT)|(1<<TWEA)|(1<<TWEN);
while((TWCR&(1<<TWINT))==0);
if((TWSR&0xF8)!=0x18)
PORTD=0x02;

TWDR=0x55;
TWCR=(1<<TWINT)|(1<<TWEA)|(1<<TWEN);
while((TWCR&(1<<TWINT))==0);
if((TWSR&0xF8)!=0x28)
PORTD=0x03;

while(1);
}
}

Slave:

#include<avr/io.h>
#include<util/delay.h>
void main()
{
DDRD=0xFF;
DDRB=0xFF;
TWAR=0x40;
while(1)
{
TWCR=(1<<TWINT)|(1<<TWEN)|(1<<TWEA);

while(TWCR&(1<<TWINT)==0);
if(TWSR&0xF8!=0x60)
PORTD=0x04;
TWCR=(1<<TWINT)|(1<<TWEN)|(1<<TWEA);
while(TWCR&(1<<TWINT)==0);
PORTB=TWDR;
}
}


Simulation for MOSI Program:



Program for AVR(MISO):

Master:

#include<avr/io.h>
#include<util/delay.h>
void main()
{
DDRD=0xFF;
DDRB=0xFF;
while(1)
{
TWBR=0xFF;

TWCR=((1<<TWEA)|(1<<TWSTA)|(1<<TWEN)|(1<<TWINT));
while((TWCR&(1<<TWINT))==0);
if((TWSR&0xF8)!=0x08)
PORTD=0x04;

TWCR=((1<<TWINT)|(1<<TWEA)|(1<<TWEN));
TWDR=0x41;
while((TWCR&(1<<TWINT))==0);
if((TWSR&0xF8)!=0x40)
PORTD=0x05;

TWCR=((1<<TWINT)|(1<<TWEA)|(1<<TWEN));
while((TWCR&(1<<TWINT))==0);
PORTB=TWDR;
if((TWSR&0xF8)!=0x50)
PORTD=0x06;

TWCR=((1<<TWSTO)|(1<<TWEN)|(1<<TWINT));
while((TWCR&(1<<TWINT))==0);
}
}


Slave:


#include<avr/io.h>
#include<util/delay.h>
void main()
{
DDRD=0xFF;
DDRB=0xFF;
while(1)
{
TWCR=(1<<TWINT)|(1<<TWEN)|(1<<TWEA);
TWAR=0x41;
while((TWCR&(1<<TWINT))==0);
if((TWSR&0xF8)!=0xA8)
PORTD=0x07;
TWCR=(1<<TWINT)|(1<<TWEN)|(1<<TWEA);
TWDR=0X44;
while((TWCR&(1<<TWINT))==0);
if((TWSR&0xF8)!=0xB8)
PORTD=0x08;
}
}

SPI using AVR:

SPI using AVR:

Program for SPI (MOSI):

Master:


#include<avr/io.h>
#include<util/delay.h>
void main()
{
DDRB=0xAC;
SPCR=0x70;
while(1)
{
SPDR=0xf0;
while((SPSR&0x80)==0);


}
}


Slave:


#include<avr/io.h>
#include<util/delay.h>
void main()
{
DDRD=0xFF;
//DDRB=0X40;
SPCR=0x70;
while(1)
{
while((SPSR&0x80)==0);
PORTD=SPDR;
}
}

Simulation for MOSI:




Program for SPI (MISO):

Master:

 #include<avr/io.h>
#include<util/delay.h>
void main()
{
while(1)
{
DDRB=0x2C;
SPCR=0x70;
SPDR=0x55;
while((SPSR&0x80)==0);
DDRB=0x34;
DDRD=0xFF;
SPCR=0x50;
while((SPSR&0x80)==0);
PORTD=SPDR;
while(1);
}
}


Slave:


 #include<avr/io.h>
#include<util/delay.h>
void main()
{
while(1)
{
DDRD=0xFF;
SPCR=0x40;
while((SPSR&0x80)==0);
PORTD=SPDR;
DDRB=0x10;
SPCR=0x60;
SPDR=0x55;
while((SPSR&0x80)==0);
while(1);
}
}














INTERRUPT using AVR:

Interrupts:

  • An interrupt is an external or internal event that interrupts the microcontroller to inform it that a device needs its service.
  • Interrupts vs. Polling • A single microcontroller can serve several devices. •
  • There are two ways to do that: – interrupts – Polling. •
  • The program which is associated with the interrupt is called the interrupt service routine (ISR) or interrupt handler.

Steps in Executing an Interrupt:

  • Finish current instruction and saves the PC on stack.
  • Jumps to a fixed location in memory depend on type of interrupt
  • Starts to execute the interrupt service routine until RETI (return from interrupt)
  • Upon executing the RETI the microcontroller returns to the place where it was interrupted. Get pop PC from stack

Types of Interrupt


Block Diagram of Interrupt



Program for External interrupt0 (INT0):

#include<avr/io.h>
#include<util/delay.h>
#include<avr/interrupt.h>
 ISR(INT0_vect)
{
PORTC=0x55;
}
void main()
{
sei();
MCUCR=0x02;
GICR=0x40;
DDRB=0xFF;
DDRC=0xFF;
while(1);
}

TIMER using AVR

TIMER using AVR:

AVR have 3 types of Timer.
1) Timer 0= 8 bit
2)  Timer1= 16 bit
3) Timer 2= 8 bit

Program: 1 sec delay using Timer 0:

#include<avr/io.h>
#include<util/delay.h>
void main()
{
int i;
DDRD=0xFF;
TCCR0=0x01;
while(1)
{
for(i=0;i<5000;i++)
{
TCNT0=0x38;
while((TIFR&0x01)==0);
TIFR=0x01;
}
PORTD=~PORTD;
}
}