PART B: Interfacing Codes

Program 9
////// "LCD DISPLAY"  //////

#include <lpc21xx.h>
#include<stdio.h>

// Function prototypes
void lcd_init(void);
void wr_cn(void);
void clr_disp(void);
void delay(unsigned int);
void lcd_com(void);
void wr_dn(void);
void lcd_data(void);

unsigned char temp1;
unsigned long int temp,r=0;
unsigned char *ptr,disp[] = "Gururaj,",disp1[] = "CSE DEPT";

int main()
{
IO0DIR = 0x000000FC; //configure o/p lines for lcd

IO0PIN = 0X00000000;
delay(3200); //delay
lcd_init(); //lcd intialisation
delay(3200); //delay
clr_disp(); //clear display
// delay(500); //delay

//........LCD DISPLAY TEST.........//
temp1 = 0x80; // Display starting address of first line 1 th pos
lcd_com();
delay(645);

ptr = disp;
while(*ptr!='\0')
{
temp1 = *ptr;
delay(3200);
lcd_data();
ptr ++;
delay(3200);
}

temp1 = 0xC0; // Display starting address of second line 4 th pos
lcd_com();
delay(800);

ptr = disp1;
while(*ptr!='\0')
{
temp1 = *ptr;
delay(3200);
lcd_data();
ptr ++;
delay(3200);
}
while(1);

} //end of main()

// lcd initialisation routine.
void lcd_init(void)
{
temp = 0x30;
wr_cn();
delay(3200);

temp = 0x30;
wr_cn();
delay(3200);

temp = 0x30;
wr_cn();
delay(3200);

temp = 0x20;
wr_cn();
delay(3200);

// load command for lcd function setting with lcd in 4 bit mode,
// 2 line and 5x7 matrix display

temp = 0x28;
lcd_com();
delay(3200);

// load a command for display on, cursor on and blinking off
temp1 = 0x0C;
lcd_com();
delay(800);

// command for cursor increment after data dump
temp1 = 0x06;
lcd_com();
delay(800);

temp1 = 0x80;
lcd_com();
delay(800);

}

void lcd_com(void)
{
temp = temp1 & 0xf0;
wr_cn();
temp = temp1 & 0x0f;
temp = temp << 4;
wr_cn();
delay(500);
}


// command nibble o/p routine
void wr_cn(void) //write command reg
{
IO0CLR = 0x000000FC; // clear the port lines.
IO0SET = temp; // Assign the value to the PORT lines
IO0CLR = 0x00000004; // clear bit RS = 0
IO0SET = 0x00000008; // E=1
delay(10);
IO0CLR = 0x00000008;

}

// data nibble o/p routine
void wr_dn(void) ////write data reg
{
IO0CLR = 0x000000FC; // clear the port lines.
IO0SET = temp; // Assign the value to the PORT lines
IO0SET = 0x00000004; // set bit RS = 1
IO0SET = 0x00000008; // E=1
delay(10);
IO0CLR = 0x00000008;
}


// data o/p routine which also outputs high nibble first
// and lower nibble next
void lcd_data(void)
{
temp = temp1 & 0xf0;
temp = temp ;//<< 6;
wr_dn();
temp= temp1 & 0x0f;
temp= temp << 4;
wr_dn();
delay(100);
}

void clr_disp(void)
{
// command to clear lcd display
temp1 = 0x01;
lcd_com();
delay(500);
}

void delay(unsigned int r1)
{
for(r=0;r<r1;r++);
}


Program 10

///////Program to test Working of DC Motor ///////// 

#include<lpc214x.h>

unsigned int j=0;

int main()
{
IO0DIR= 0X00000900;
IO0SET= 0X00000100; //P0.8 should always high.

  while(1)
  {
    //clock_wise
IO0CLR = 0x00000900; //stop motor and also turn off relay
for(j=0;j<10000;j++); //small delay to allow motor to turn off 
IO0SET = 0X00000900; //Selecting the P0.11 line for clockwise and turn on motor
    for(j=0;j<400000;j++); //delay
    //anti_clock_wise
IO0CLR = 0X00000900; //stop motor and also turn off relay
      for(j=0;j<10000;j++); //small delay to allow motor to turn off 
      IO0SET = 0X00000100; //not selecting the P0.11 line for Anti clockwise
    for(j=0;j<400000;j++); //delay
  } //End of while(1)
} //End of Main

Program 11

/*------------------------------------------------------------------------
 * A stepper motor direction is controlled by shifting the voltage across 
 * the coils. Port lines : P0.12 to P0.15
 *************************************************************************/

#include <LPC21xx.H>

void clock_wise(void);
void anti_clock_wise(void);
 
unsigned long int var1,var2;
unsigned int i=0,j=0,k=0;

int main(void)
{
PINSEL0 = 0x00000000; //P0.12 to P0.15 GPIo
IO0DIR  = 0x0000F000; //P0.12 to P0.15 output

while(1)
{
for(j=0;j<50;j++)       // 50 times in Clock wise Rotation(360 degree)
  clock_wise();
for(k=0;k<65000;k++);   // Delay to show  anti_clock Rotation 
for(j=0;j<50;j++)       // 50 times in  Anti Clock wise Rotation(360 degree)
anti_clock_wise();

for(k=0;k<65000;k++);   // Delay to show clock Rotation 

} // End of while(1)

} // End of main

void clock_wise(void)
{
var1 = 0x00001000;          //For Clockwise
  for(i=0;i<=3;i++)          // for A B C D Stepping
{
IO0PIN = var1;
for(k=0;k<=30000;k++); //for step speed variation
var1 = var1<<1;        //For Clockwise
  }
}

void anti_clock_wise(void)
{
var1 = 0x00008000;      //For Anticlockwise
  for(i=0;i<=3;i++)      // for A B C D Stepping
  {
  IO0PIN = var1;
for(k=0;k<=30000;k++); //for step speed variation
var1 = var1>>1;      //For Anticlockwise        
  }
}

Program 12

//10-bit internal ADC
//AIN0 pin is selected
//you can change the channel by changing PINSEL1 and ADCR value    

#include <lpc214x.h>
#include <Stdio.h>

void lcd_init(void);
void wr_cn(void);
void clr_disp(void);
void delay(unsigned int);
void lcd_com(void);  
void wr_dn(void);
void lcd_data(void);

unsigned int data_lcd=0;
unsigned  int adc_value=0,temp_adc=0,temp1,temp2;
float temp;
char var[15],var1[15];
char *ptr,arr[]= "ADC O/P= ";
char *ptr1,dis[]="A I/P  = ";

#define vol 3.3               // Reference voltage
#define fullscale 0x3ff         // 10 bit adc

int main()
{  
        PINSEL1  = 0X00040000;       //AD0.4 pin is selected(P0.25)
        IO0DIR   = 0x000000FC;      //configure o/p lines for lcd  

delay(3200);
lcd_init(); //LCD initialization  
    delay(3200);
clr_disp(); //clear display
delay(500);

ptr = dis;
temp1 = 0x80; // Display starting address of first line 1 th pos
lcd_com();
delay(800);

    while(*ptr!='\0')
    {
    temp1 = *ptr;
     delay(3200);   //3200
lcd_data();
ptr ++;
delay(3200);
    }

ptr1 = arr;
temp1 = 0xC0; // Display starting address of second line 4 th pos
lcd_com();
delay(800);
       
while(*ptr1!='\0')
    {
      temp1 = *ptr1;
      delay(3200);
 lcd_data();
 ptr1 ++;
 delay(3200);
     }
//   while(1);
    //infinite loop
    while(1)
    {  
//CONTROL register for ADC
AD0CR = 0x09200010;         //command register for ADC-AD0.4
  delay(10);
while((temp_adc = AD0GDR) == 0x80000000); //to check the interrupt bit

adc_value = AD0GDR;           //reading the ADC value
adc_value >>=6;
adc_value &= 0x000003ff;
temp = ((float)adc_value * (float)vol)/(float)fullscale;
sprintf(var1,"%3.2fV",temp);
sprintf(var,"%x",adc_value);

delay(1200);
temp1 = 0x89;
lcd_com();
delay(1200);
ptr = var1;
                while(*ptr!='\0')
                {        
  //                data_lcd =*ptr;
   temp1=*ptr;
       delay(3200);
                  lcd_data();
                  ptr++;
       delay(3200);
                }
                 
                delay(1200);  
                temp1 = 0xc9;
                lcd_com();
                delay(1200);

                ptr1 = var;
                while(*ptr1!='\0')
                {
           //       data_lcd =*ptr1;
  temp1=*ptr1;
     delay(3200);
                  lcd_data();
                  ptr1++;
       delay(3200);
                }      
      } // end of while(1)

} //end of main()
 

//lcd initialization
void lcd_init()
{
temp2=0x30;  
wr_cn();
delay(800);

temp2=0x30;
wr_cn();
delay(800);

temp2=0x30;
wr_cn();
delay(800);

temp2=0x20;
wr_cn();
delay(800);

temp1 = 0x28;
lcd_com();
delay(800);

temp1 = 0x0c;
lcd_com();
delay(800);

temp1 = 0x06;
lcd_com();
delay(800);

temp1 = 0x80;
lcd_com();
delay(800);
}

void lcd_com(void)
{
temp2= temp1 & 0xf0;
wr_cn();
temp2 = temp1 & 0x0f;
temp2 = temp2 << 4;
wr_cn();
         delay(500);
}

// command nibble o/p routine
void wr_cn(void)                        //write command reg
{
IO0CLR  = 0x000000FC; // clear the port lines.
IO0SET = temp2; // Assign the value to the PORT lines    
IO0CLR  = 0x00000004; // clear bit  RS = 0
IO0SET = 0x00000008;   // E=1
delay(10);
IO0CLR  = 0x00000008;
 }

// data nibble o/p routine
void wr_dn(void)
{    
IO0CLR  = 0x000000FC; // clear the port lines.
IO0SET = temp2; // Assign the value to the PORT lines
IO0SET = 0x00000004;   // set bit  RS = 1
IO0SET = 0x00000008;   // E=1
delay(10);
IO0CLR = 0x00000008;
 }

// data o/p routine which also outputs high nibble first
// and lower nibble next
 void lcd_data(void)
{  
//    temp1 = data_lcd;            
    temp2 = temp1 & 0xf0;
    wr_dn();
    temp2= temp1 & 0x0f;
    temp2= temp2 << 4;
    wr_dn();
    delay(100);
}

void delay(unsigned int r1)
{
  unsigned int r;
  for(r=0;r<r1;r++);
}
void clr_disp(void)
{
temp1 = 0x01;
lcd_com();
  delay(500);
}
   Program 13

//Triangular
#include <LPC21xx.h>
unsigned long int temp=0x00000000; 
 
int main ()
{
unsigned int i=0;

  IO0DIR=0x00FF0000;
   
  while(1)
   {
    // output 0 to FE 
     for(i=0;i!=0xFF;i++)
  {
        temp=i;
        temp = temp << 16;
        IO0PIN=temp;
      }
       
// output FF to 1   
      for(i=0xFF; i!=0;i--)
      {
        temp=i;
        temp = temp << 16;
        IO0PIN=temp;
      }
}//End of while(1)
}//End of main()

//square

// program to generate square wave with DAC interface

#include <lpc21xx.h>

unsigned int delay;
  
int main ()
{
PINSEL1 = 0x00000000 ; // Configure P0.16 to P0.31 as GPIO
IO0DIR  = 0x00FF0000 ;
   
while(1)
    {
    IO0PIN = 0x00000000;
for(delay=0;delay<=950;delay++);
      IO0PIN = 0x00FF0000;
    for(delay=0;delay<=950;delay++);
    }
}   

Program 14

/*Program to demonstrate keyboard operation  Date:11/11/2011            
Takes a key from key board and displays it on LCD screen*/

#include<lpc21xx.h>
#include<stdio.h>

/******* FUNCTION PROTOTYPE*******/

void lcd_init(void);
void clr_disp(void);
void lcd_com(void);
void lcd_data(void);
void wr_cn(void);
void wr_dn(void);
void scan(void);
void get_key(void);
void display(void);
void delay(unsigned int);
void init_port(void);

/*unsigned long int scan_code[16]= {0x00110000,0x00120000,0x00140000,0x00180000,
                                  0x00210000,0x00220000,0x00240000,0x00280000,
                                  0x00410000,0x00420000,0x00440000,0x00480000,
                                  0x00810000,0x00820000,0x00840000,0x00880000}; */

unsigned long int scan_code[16]= {0x00EE0000,0x00ED0000,0x00EB0000,0x00E70000,
                                  0x00DE0000,0x00DD0000,0x00DB0000,0x00D70000,
                                  0x00BE0000,0x00BD0000,0x00BB0000,0x00B70000,
                                  0x007E0000,0x007D0000,0x007B0000,0x00770000};


unsigned char ASCII_CODE[16]= {'G','1','2','3',
                               '4','5','6','7',
                               '8','9','A','B',
                               'C','D','E','F'};

unsigned char  row,col;  
unsigned char temp,flag,i,result,temp1;
unsigned int r,r1;
unsigned long int var,var1,var2,res1,temp2,temp3,temp4;
unsigned char *ptr,disp[] = "4X4 KEYPAD";
unsigned char disp0[] = "KEYPAD TESTING";
unsigned char disp1[] = "KEY = ";
int main()
{
    //   __ARMLIB_enableIRQ();
init_port();            //port intialisation
  delay(3200); //delay
lcd_init();             //lcd intialisation
delay(3200); //delay
    clr_disp(); //clear display
delay(500);             //delay

//........LCD DISPLAY TEST.........//
ptr = disp;
temp1 = 0x81; // Display starting address
lcd_com();
delay(800);
        while(*ptr!='\0')
        {
          temp1 = *ptr;
          delay(3200);
   lcd_data();
     ptr ++;
          delay(3200);
        }
  //      clr_disp();

//........KEYPAD Working.........//
  while(1)
{        
          get_key();
          display();
}
     
}  //end of main()

void get_key(void)                //get the key from the keyboard
{
unsigned int  i;
flag = 0x00;
         IO1PIN=0x000f0000;        
while(1)
{
            for(row=0X00;row<0X04;row++) //Writing one for col's
            {
                if( row == 0X00)
                {
                 temp3=0x00700000;
}
                else if(row == 0X01)
                {
                  temp3=0x00B00000;
}
else if(row == 0X02)
{
                  temp3=0x00D00000;
}
                else if(row == 0X03)
{
                  temp3=0x00E00000;
}
  var1 = temp3;
          IO1PIN = var1; // each time var1 value is put to port1
          IO1CLR =~var1; // Once again Conforming (clearing all other bits)
          scan();
          delay(100); //delay
          if(flag == 0xff)
          break;
          } // end of for

if(flag == 0xff)
break;
      }  // end of while
 
    for(i=0;i<16;i++)
    {
      if(scan_code[i] == res1)      //equate the scan_code with res1
      {
result =  ASCII_CODE[i];    //same position value of ascii code
break;                      //is assigned to result
      }
    }
}// end of get_key();

void scan(void)
{
    unsigned long int t;
    temp2 = IO1PIN;                             // status of port1
    temp2 = temp2 & 0x000F0000;                 // Verifying column key
    if(temp2 != 0x000F0000)                     // Check for Key Press or Not
    {
      delay(1000);                              //delay(100)//give debounce delay check again
      temp2 = IO1PIN;
      temp2 = temp2 & 0x000F0000;              //changed condition is same

        if(temp2 != 0x000F0000)                // store the value in res1
        {
          flag = 0xff;
          res1 = temp2;
          t = (temp3 & 0x00F00000);           //Verfying Row Write
          res1 = res1 | t;                    //final scan value is stored in res1
        }
        else
        {
          flag = 0x00;
        }
    }
}  // end of scan()
   
void display(void)
{  
//clr_disp();
//delay(800);
       
    ptr = disp0;
temp1 = 0x80; // Display starting address of first line
lcd_com();
delay(800);
   
while(*ptr!='\0')
    {
      temp1 = *ptr;
      delay(3200);
 lcd_data();
 ptr ++;
      delay(3200);
    }

    ptr = disp1;
temp1 = 0xC0; // Display starting address of second line
lcd_com();
delay(800);
       
while(*ptr!='\0')
    {
      temp1 = *ptr;
      delay(3200);
 lcd_data();
 ptr ++;
      delay(3200);
     }
        temp1 = 0xC6; //display  address for key value
lcd_com();
        delay(800);      
        temp1 = result;    
        lcd_data();
delay(800);        
}  

void lcd_init (void)
{

temp = 0x30;  
wr_cn();
delay(3200); //delay(800);  

temp = 0x30;
wr_cn();
delay(3200); //delay(800);  

temp = 0x30;
wr_cn();
delay(3200); //delay(800);  

temp = 0x20;
wr_cn();
delay(3200); //delay(800);  

// load command for lcd function setting with lcd in 4 bit mode,
// 2 line and 5x7 matrix display

temp = 0x28;
lcd_com();
delay(3200); //delay(800);  

// load a command for display on, cursor on and blinking off
temp1 = 0x0C;
lcd_com();
delay(800);  

// command for cursor increment after data dump
temp1 = 0x06;
lcd_com();
delay(800);  

temp1 = 0x80;
lcd_com();
delay(800);  

}

void lcd_data(void)
{          
    temp = temp1 & 0xf0;
 //   temp = temp ; //<< 6;
    wr_dn();
    temp= temp1 & 0x0f;
    temp= temp << 4;
    wr_dn();
    delay(100);
}

void wr_dn(void) ////write data reg
{    
IO0CLR  = 0x000000FC; // clear the port lines.
IO0SET = temp; // Assign the value to the PORT lines
IO0SET = 0x00000004;   // set bit  RS = 1
// IO0CLR = 0x00010000;   // R/W=0
IO0SET = 0x00000008;   // E=1
delay(10);
IO0CLR = 0x00000008;
 }


void lcd_com(void)
{
    temp = temp1 & 0xf0;
    wr_cn();
    temp = temp1 & 0x0f;
    temp = temp << 4;
    wr_cn();
    delay(500);
}

void wr_cn(void)                //write command reg
{
IO0CLR  = 0x000000FC; // clear the port lines.
IO0SET = temp; // Assign the value to the PORT lines    
IO0CLR  = 0x00000004; // clear bit  RS = 0
  IO0SET = 0x00000008;   // E=1
delay(10);
IO0CLR  = 0x00000008;
 }

void clr_disp(void)
{
// command to clear lcd display
    temp1 = 0x01;
    lcd_com();
    delay(500);
}  

void delay(unsigned int r1)
{
for(r=0;r<r1;r++);
}
     
void init_port()
{    
        IO0DIR   = 0x000000FC; //configure o/p lines for lcd
IO1DIR   = 0XFFF0FFFF;
}
Program 15

// The program demonstrates About the External interrupt to the Controller 

#include<lpc214x.h>

void Extint0_Isr(void) __irq; //declaration of ISR

unsigned char int_flag = 0, flag = 0;

int main(void)
{
    IO1DIR  = 0X02000000;    
    IO1SET  = 0X02000000;
    PINSEL1 =0X00000001;          //Setup P0.16 to alternate function EINT0

    EXTMODE =0x01;                //edge i.e falling egge trigger and active low
    EXTPOLAR= 0X00;
    VICVectAddr0 = (unsigned long) Extint0_Isr;   //Assign the EINT0 ISR function 
    VICVectCntl0 = 0x20 | 14;      //Assign the VIC channel EINT0 to interrupt priority 0
    VICIntEnable |= 0x00004000;    //Enable the EINT0 interrupt

while(1)                            //waiting for interrupt to occur
    {
    if(int_flag == 0x01)
{
if(flag == 0)
{
IO1CLR = 0X02000000;
flag = 1;
}
else if(flag == 1)
{
IO1SET = 0x02000000;
flag = 0;
}
int_flag = 0x00;
}   
  }
}

void Extint0_Isr(void)__irq 
{           //whenever there is a low level on EINT0
  EXTINT |= 0x01;                  //Clear interrupt
int_flag = 0x01;
VICVectAddr = 0; //Acknowledge Interrupt
}

Program 16

#include <LPC21xx.h>

unsigned int delay, count=0, Switchcount=0;

/*cmmon cathode seven segment data for 0 to F*/
unsigned int Disp[16]={0x003F0000, 0x00060000, 0x005B0000, 0x004F0000, 0x00660000,0x006D0000,
   0x007D0000, 0x00070000, 0x007F0000, 0x006F0000, 0x00770000,0x007C0000,
   0x00390000, 0x005E0000, 0x00790000, 0x00710000 };

int main (void)
{
PINSEL1 = 0x00000000;
IO0DIR  = 0x10FF0000;
while(1)
{
//Display values on Seven Segment
IO0SET = 0x10000000;
IO0CLR  = 0x00FF0000;
IO0SET = Disp[Switchcount];   // display the values 0 to F one after the other
for(delay=0;delay<=300000;delay++);
Switchcount++;
if(Switchcount == 16)   // after F go back to 0
{
Switchcount = 0;
}
}
}

1 comment:

  1. ////// "LCD DISPLAY" To display the predefined data Date:21/01/2012 //////

    #include
    #include

    // Function prototypes
    void lcd_init(void);
    void wr_cn(void);
    void clr_disp(void);
    void delay(unsigned int);
    void lcd_com(void);
    void wr_dn(void);
    void lcd_data(void);

    unsigned char temp1;
    unsigned long int temp,r=0;
    unsigned char *ptr,disp[] = "Gururaj,",disp1[] = "CSE DEPT";

    int main()
    {
    IO0DIR = 0x000000FC; //configure o/p lines for lcd

    IO0PIN = 0X00000000;
    delay(3200); //delay
    lcd_init(); //lcd intialisation
    delay(3200); //delay
    clr_disp(); //clear display
    // delay(500); //delay

    //........LCD DISPLAY TEST.........//
    temp1 = 0x80; // Display starting address of first line 1 th pos
    lcd_com();
    delay(645);

    ptr = disp;
    while(*ptr!='\0')
    {
    temp1 = *ptr;
    delay(3200);
    lcd_data();
    ptr ++;
    delay(3200);
    }

    temp1 = 0xC0; // Display starting address of second line 4 th pos
    lcd_com();
    delay(800);

    ptr = disp1;
    while(*ptr!='\0')
    {
    temp1 = *ptr;
    delay(3200);
    lcd_data();
    ptr ++;
    delay(3200);
    }
    while(1);

    } //end of main()

    // lcd initialisation routine.
    void lcd_init(void)
    {
    temp = 0x30;
    wr_cn();
    delay(3200);

    temp = 0x30;
    wr_cn();
    delay(3200);

    temp = 0x30;
    wr_cn();
    delay(3200);

    temp = 0x20;
    wr_cn();
    delay(3200);

    // load command for lcd function setting with lcd in 4 bit mode,
    // 2 line and 5x7 matrix display

    temp = 0x28;
    lcd_com();
    delay(3200);

    // load a command for display on, cursor on and blinking off
    temp1 = 0x0C;
    lcd_com();
    delay(800);

    // command for cursor increment after data dump
    temp1 = 0x06;
    lcd_com();
    delay(800);

    temp1 = 0x80;
    lcd_com();
    delay(800);

    }

    void lcd_com(void)
    {
    temp = temp1 & 0xf0;
    wr_cn();
    temp = temp1 & 0x0f;
    temp = temp << 4;
    wr_cn();
    delay(500);
    }


    // command nibble o/p routine
    void wr_cn(void) //write command reg
    {
    IO0CLR = 0x000000FC; // clear the port lines.
    IO0SET = temp; // Assign the value to the PORT lines
    IO0CLR = 0x00000004; // clear bit RS = 0
    IO0SET = 0x00000008; // E=1
    delay(10);
    IO0CLR = 0x00000008;

    }

    // data nibble o/p routine
    void wr_dn(void) ////write data reg
    {
    IO0CLR = 0x000000FC; // clear the port lines.
    IO0SET = temp; // Assign the value to the PORT lines
    IO0SET = 0x00000004; // set bit RS = 1
    IO0SET = 0x00000008; // E=1
    delay(10);
    IO0CLR = 0x00000008;
    }


    // data o/p routine which also outputs high nibble first
    // and lower nibble next
    void lcd_data(void)
    {
    temp = temp1 & 0xf0;
    temp = temp ;//<< 6;
    wr_dn();
    temp= temp1 & 0x0f;
    temp= temp << 4;
    wr_dn();
    delay(100);
    }

    void clr_disp(void)
    {
    // command to clear lcd display
    temp1 = 0x01;
    lcd_com();
    delay(500);
    }

    void delay(unsigned int r1)
    {
    for(r=0;r<r1;r++);
    }

    ReplyDelete