Space In HIndi

 


Pages

Popular

Advance processor

All led blinking program (Basic program)
int  main(void)

 int a, k;

 PINSEL2 = 0x00000000;     //Configure PORT1 as GPIO
 IODIR1 = 0xFFFF << 16 ;        //Configure P1.16- P1.23 as output
 IODIR0 = 0xFFFFFFFF  ;
 while(1)   
 {
                            //Initial Value
       
                IOSET1 = 0xFFFF << 16; //Turn ON LED
                IOSET0 = 0xFFFFFFFF  ;
                delay_ms (250);
                IOCLR1 = 0xFFFF << 16;    //Turn OFF LED
                IOCLR0 = 0xFFFFFFFF  ;
                delay_ms (250);
                            //Shift the value
           
 }

}


Expt. 1a.: Interfacing 16x2 LCD to LPC2148
Platform: Smart Logic LPC2148 Development Board.
       
Clock Settings:
    FOSC    >>    12MHz (onboard)
    PLL        >>    M=5, P=2
    CCLK    >>  60MHz
    PCLK    >>  15MHz
       
Hardware Setup:-
    LCD data pin :-P0.16-P0.23
    RS-P1.16
    RW-P1.17
    EN-P1.18     
      
********************************************************************************/

//Include Controller specific header file
#include <lpc214x.h>

//Function to generate software delay
//Calibrated to 1ms
void  delay_ms(unsigned char time)      

 unsigned int  i, j;
 for (j=0; j<time; j++)
  for(i=0; i<8002; i++);
}

void LCD_command(unsigned char command)
{
   
    IOCLR1=1<<16;                 // RS=0 for command
    IOCLR1=1<<17;                 // RW=0 for write
    IOCLR0 = 0xFF<<16;    // Clear LCD Data lines
    IOSET0=command<<16;    // put command on data line
    IOSET1=1<<18;            // en=1
    delay_ms(10) ;            // delay
    IOCLR1=1<<18;             // en=0
}

LCD_data(unsigned char data)
{
    IOCLR0 = 0xFF<<16;    // Clear LCD Data lines
    IOSET1=1<<16;                 // RS=1 for data
    IOCLR1=1<<17;                 // RW=0 for write
    IOSET0= data<<16;     // put command on data line
    IOSET1=(1<<18);            //en=1
    delay_ms(10) ;          //delay
    IOCLR1=(1<<18);            //en=0
 }

LCD_init()
{
    LCD_command(0x38);    //8bit mode and 5x8 dotes (function set)
    delay_ms(10) ;            // delay
    LCD_command(0x0c);    //display on, cursor off, cursor char blinking off(display on/off)
    delay_ms(10) ;            // delay
    LCD_command(0x06);  //cursor increament and display shift(entry mode set)
    delay_ms(10) ;            // delay
    LCD_command(0x01);     //clear lcd(clear command)
    delay_ms(10) ;            // delay
    LCD_command(0x80);     //set cursor to 0th location    1st lne
   
}

LCD_write_string(unsigned char *string)
{
  while(*string)            //Check for End of String
  LCD_data(*string++);     //sending data on LCD byte by byte
}

int main(void)
{
    PINSEL1 = 0x00;        //Configure PORT0 as GPIO
    PINSEL2 = 0X00;        //Configure PORT1 as GPIO
    IODIR1= 0x07<<16;    //Configure P1.18, P1.17, P1.16 as output
    IODIR0=    0xFF<<16; //Configure P0.23 - P0.16 as output
    LCD_init();                //Initialize LCD 16x2

    LCD_write_string("Smart Logic Tech");
    LCD_command(0xc0);//second line
    LCD_write_string("Embed. Processors");
    while (1);
   }


***********************************************************************************


Expt. 1b.: Interfacing 128x64 GLCD to LPC2148
Platform: Smart Logic LPC2148 Development Board.

      
Clock Settings:
    FOSC    >>    12MHz (onboard)
    PLL        >>    M=5, P=2
    CCLK    >>  60MHz
    PCLK    >>  15MHz
      
Hardware Setup:-
    LCD data pin :-P0.16-P0.23
    RS    -    P1.16
    RW    -    P1.17
    EN    -    P1.18
    CS1 - P1.19
    CS2    - P1.20
    RST - P1.21

   
********************************************************************************/

//Include Controller specific header file
#include <lpc214x.h>
#include "image.h"
#include "glcd_font_8x8.h"

//Define Macro Functions
#define GLCD_CS1_SET()     IOSET1=1<<19;
#define    GLCD_CS2_SET()    IOSET1=1<<20;
#define GLCD_RST_SET()    IOSET1=1<<21;
#define GLCD_CS1_CLR()     IOCLR1=1<<19;
#define    GLCD_CS2_CLR()    IOCLR1=1<<20;
#define GLCD_RST_CLR()    IOCLR1=1<<21;
unsigned char c=0;

//Function to generate software delay
//Calibrated to 1ms
void  delay_ms(unsigned int time)     

 unsigned int  i, j;
 for (j=0; j<time; j++)
  for(i=0; i<8002; i++);
}

void GLCD_command(unsigned char command)
{
    IOCLR0 = 0xFF<<16;    // Clear LCD Data lines
    IOCLR1=1<<16;                 // RS=0 for command
    IOCLR1=1<<17;                 // RW=0 for write
    IOSET0=command<<16;    // put command on data line
    IOSET1=(1<<18);            // en=1
    delay_ms(1) ;            // delay
    IOCLR1=(1<<18);             // en=0
}

void GLCD_data(unsigned char data)
{
    IOCLR0 = 0xFF<<16;    // Clear LCD Data lines
    IOSET1=1<<16;                 // RS=1 for data
    IOCLR1=1<<17;                 // RW=0 for write
    IOSET0= data<<16;     // put command on data line
    IOSET1=(1<<18);            //en=1
    delay_ms(1) ;          //delay
    IOCLR1=(1<<18);            //en=0
 }


void GLCD_init()
{
    GLCD_RST_SET();            //Set the Reset Pin
    GLCD_CS1_SET();            //Select Chip1
    GLCD_CS2_SET();            //Select Chip2
    GLCD_command(0x3F);    //GLCD_Display_ON_Command
    delay_ms(10) ;            //Delay
    GLCD_command(0xB8);     //Set Page Address to 0
    delay_ms(10) ;            //Delay
    GLCD_command(0x40);     //Set Coloumn Address to 0
    delay_ms(10) ;            //Delay
}

void GLCD_setpage(unsigned char page)
{
    GLCD_CS1_SET();            //Select Chip1
    GLCD_CS2_SET();            //Select Chip2
    GLCD_command(0xB8|page);    //GLCD_pageset_Command
    delay_ms(10) ;            //Delay
}

void GLCD_setcolumn(unsigned char column)
{
    if(column<64)
    {
        c=column;
        GLCD_CS1_SET();            //Select Chip1
        GLCD_CS2_CLR();            //Select Chip2
        GLCD_command(0x40|column);    //GLCD_columnset_Command
        delay_ms(10);
    }
    else
    {   
        c=column;
        GLCD_CS1_CLR();            //Select Chip1
        GLCD_CS2_SET();            //Select Chip2
        GLCD_command(0x40|column-64);  
        delay_ms(10);
    }
}


GLCD_data_array (const unsigned char *value,unsigned int limit)
{
    unsigned int i;
    for(i=0;i<limit;i++)
    {
        if(c<64)
        {
            GLCD_CS1_SET();
            GLCD_CS2_CLR();
            GLCD_data(value[i]);
            c++;
        }
        else
        {
            GLCD_setcolumn(c);
            GLCD_CS1_CLR();
            GLCD_CS2_SET();
            GLCD_data(value[i]);
            c++;
        }
        if(c>127)
        return;
    }
}

void putstr_font1(unsigned char y,unsigned char x,unsigned char *str)
{  
    unsigned char i;  
    unsigned int a;  
    GLCD_setcolumn(y);  
    GLCD_setpage(x);  
    for(i=0;str[i]!=0;i++)  
        {      
            a=(*(str+i));      
            a*=8;
            GLCD_data_array(&FONT1_8x8[a],8);  
        }
}

void GLCD_Image_Display(unsigned char *image)
{
    int i,j;
    for(i=0;i<8;i++)
    {
        GLCD_setpage(i);
        GLCD_setcolumn(0);
        for(j=0;j<64;j++)    //Send data to first half
            GLCD_data(image[(i*128)+j]);
              
        GLCD_setcolumn(64);
        for(j=64;j<128;j++)    //Send data to second half
            GLCD_data(image[(i*128)+j]);
    }
}

void GLCD_show_image(unsigned char *image)
{
    int i,j;
    int k=0; //To access element from Image Array
    for(i=0;i<8;i++)
    {
        GLCD_setpage(i);
        GLCD_setcolumn(0);
        for(j=0;j<64;j++)    //Send data to first half
            {
                GLCD_data(image[k]);
                k++;
            }
              
        GLCD_setcolumn(64);
        for(j=0;j<64;j++)    //Send data to second half
            {
                GLCD_data(image[k]);
                k++;
            }
    }
}


void GLCD_Clear_Display()
{
    int i,j;
    for(i=0;i<8;i++)
    {
        GLCD_setpage(i);
        GLCD_setcolumn(0);
        for(j=0;j<64;j++)    //Send data to first half
            GLCD_data(0x00);
              
        GLCD_setcolumn(64);
        for(j=0;j<64;j++)    //Send data to second half
            GLCD_data(0x00);
    }
}



int main(void)
{
    unsigned char str1[]="  SMART LOGIC   ";
    unsigned char str2[]="  TECHNOLOGIES  ";
    PINSEL1 = 0x00;        //Configure PORT0 as GPIO
    PINSEL2 = 0X00;        //Configure PORT1 as GPIO
    IODIR1= 0xFF<<16;    //Configure P1.23 - P1.16 as output
    IODIR0=    0xFF<<16; //Configure P0.23 - P0.16 as output
    GLCD_init();                //Initialize GLCD 128x64
    GLCD_Clear_Display();
    putstr_font1(0,1,str1);
    putstr_font1(0,2,str2);
    delay_ms(500);
    GLCD_Image_Display(SLT_IMG);
    while(1);

***********************************************************************************


Expt. 1b.: Interfacing 128x64 GLCD to LPC2148
Platform: Smart Logic LPC2148 Development Board.

       
Clock Settings:
    FOSC    >>    12MHz (onboard)
    PLL        >>    M=5, P=2
    CCLK    >>  60MHz
    PCLK    >>  15MHz
       
Hardware Setup:-
    LCD data pin :-P0.16-P0.23
    RS    -    P1.16
    RW    -    P1.17
    EN    -    P1.18
    CS1 - P1.19
    CS2    - P1.20
    RST - P1.21

   
********************************************************************************/

//Include Controller specific header file
#include <lpc214x.h>


//Define Macro Functions
#define GLCD_CS1_SET()     IOSET1=1<<19;
#define    GLCD_CS2_SET()    IOSET1=1<<20;
#define GLCD_RST_SET()    IOSET1=1<<21;
#define GLCD_CS1_CLR()     IOCLR1=1<<19;
#define    GLCD_CS2_CLR()    IOCLR1=1<<20;
#define GLCD_RST_CLR()    IOCLR1=1<<21;
unsigned char c=0;

//Function to generate software delay
//Calibrated to 1ms
void  delay_ms(unsigned int time)      

 unsigned int  i, j;
 for (j=0; j<time; j++)
  for(i=0; i<8002; i++);
}

void GLCD_command(unsigned char command)
{
    IOCLR0 = 0xFF<<16;    // Clear LCD Data lines
    IOCLR1=1<<16;                 // RS=0 for command
    IOCLR1=1<<17;                 // RW=0 for write
    IOSET0=command<<16;    // put command on data line
    IOSET1=(1<<18);            // en=1
    delay_ms(1) ;            // delay
    IOCLR1=(1<<18);             // en=0
}

void GLCD_data(unsigned char data)
{
    IOCLR0 = 0xFF<<16;    // Clear LCD Data lines
    IOSET1=1<<16;                 // RS=1 for data
    IOCLR1=1<<17;                 // RW=0 for write
    IOSET0= data<<16;     // put command on data line
    IOSET1=(1<<18);            //en=1
    delay_ms(1) ;          //delay
    IOCLR1=(1<<18);            //en=0
 }


void GLCD_init()
{
    GLCD_RST_SET();            //Set the Reset Pin
    GLCD_CS1_SET();            //Select Chip1
    GLCD_CS2_SET();            //Select Chip2
    GLCD_command(0x3F);    //GLCD_Display_ON_Command
    delay_ms(10) ;            //Delay
    GLCD_command(0xB8);     //Set Page Address to 0
    delay_ms(10) ;            //Delay
    GLCD_command(0x40);     //Set Coloumn Address to 0
    delay_ms(10) ;            //Delay
}

void GLCD_setpage(unsigned char page)
{
    GLCD_CS1_SET();            //Select Chip1
    GLCD_CS2_SET();            //Select Chip2
    GLCD_command(0xB8|page);    //GLCD_pageset_Command
    delay_ms(10) ;            //Delay
}

void GLCD_setcolumn(unsigned char column)
{
    if(column<64)
    {
        c=column;
        GLCD_CS1_SET();            //Select Chip1
        GLCD_CS2_CLR();            //Select Chip2
        GLCD_command(0x40|column);    //GLCD_columnset_Command
        delay_ms(10);
    }
    else
    {    
        c=column;
        GLCD_CS1_CLR();            //Select Chip1
        GLCD_CS2_SET();            //Select Chip2
        GLCD_command(0x40|column-64);   
        delay_ms(10);
    }
}









void GLCD_Clear_Display()
{
    int i,j;
    for(i=0;i<8;i++)
    {
        GLCD_setpage(i);
        GLCD_setcolumn(0);
        for(j=0;j<64;j++)    //Send data to first half
            GLCD_data(0x00);
               
        GLCD_setcolumn(64);
        for(j=0;j<64;j++)    //Send data to second half
            GLCD_data(0x00);
    }
}



int main(void)
{
    int i,j;
   
    PINSEL1 = 0x00;        //Configure PORT0 as GPIO
    PINSEL2 = 0X00;        //Configure PORT1 as GPIO
    IODIR1= 0xFF<<16;    //Configure P1.23 - P1.16 as output
    IODIR0=    0xFF<<16; //Configure P0.23 - P0.16 as output
    GLCD_init();                //Initialize GLCD 128x64
    GLCD_Clear_Display();
   
        GLCD_setpage(4);
        GLCD_setcolumn(4);
        for(j=4;j<50;j++)   
            GLCD_data(0x03);
    GLCD_setpage(5);
        GLCD_setcolumn(4);
        for(j=4;j<50;j++)   
            GLCD_data(0x03);
           
    while(1);
}
 

**********************************************************************************
Expt. 2a: UART Communication          
Platform: Smart Logic LPC2148 Development Board.


Hardware Setup:-
    Connect a DB9 cable between PC and UART0 or UART1.
    COMPORT Settings
    Baudrate:-9600
    Databits:-8
    Parity:-None
    Stopbits:1
      

Clock Settings:
    FOSC    >>    12MHz (onboard)
    PLL        >>    M=5, P=2
    CCLK    >>  60MHz
    PCLK    >>  15MHz

********************************************************************************/
//Include controller specific header file
#include  <lpc214x.h>  

//Define Clock settings
#define PCLK            15000000          
#define BAUDRATE    9600     //Set Baud Rate here


//Function to generate software delay
//Calibrated to 1ms
void  delay_ms(unsigned char time)       //This Function is used to cause delay between LED ON and OFF events

 unsigned int  i, j;
 for (j=0; j<time; j++)
  for(i=0; i<8002; i++);
}

//Function to Initialize the UART0
void  Init_UART0(void)                    //This function setups UART0

   unsigned int Baud16;
   U0LCR = 0x83;                    // DLAB = 1
   Baud16 = (PCLK /(16*BAUDRATE)); 
   U0DLM = Baud16 >>8;                          
   U0DLL = Baud16 ;                      
   U0LCR = 0x03;
}
              
//Functin to Transmit the characters
void  UART0_SendByte(unsigned char data)       //A function to send a byte on UART0

   U0THR = data;                  
   while( (U0LSR&0x40)==0 );      
}

//Functin to Receive the characters
unsigned char  UART0_RecievedByte( )       //A function to send a byte on UART0
{    
  while((U0LSR&0x01)==0);
       return U0RBR;
      
}

//Functin to Transmit the string
void  UART0_SendStr(const unsigned char *str)     //A function to send a string on UART0

   while(1)
   { 
      if( *str == '\0' ) break;
      UART0_SendByte(*str++);      
   }
}

void  UART0_SendStr_ram(unsigned char *str)     //A function to send a string on UART0

   while(1)
   { 
      if( *str == '\0' ) break;
      UART0_SendByte(*str++);      
   }
}



int  main(void)

    unsigned char rx_data[20], i;
    PINSEL0 = 0x00000005; // Enable UART0 Rx and Tx pins
  Init_UART0();
 
    UART0_SendStr("Smart Logic Technologies\n\r ");  
  UART0_SendStr("LPC2148 DEVLOPMENT BOARD\n\r ");
    UART0_SendStr("Send 10 Characters\n\r ");
 
    for (i=0; i<10; i++)
    {  
         rx_data[i]=UART0_RecievedByte();
  }  
    rx_data [10]= 0; // To make received string as ASCIIZ string
    UART0_SendStr("\n\rTransmitted Characters are:");
    UART0_SendStr(rx_data);
    UART0_SendStr("\n\rRx Tx test complete\n\r");
    while(1);  
  
 

**********************************************************************************
Expt. 2b: GSM Module Interface for sendind SMS          
Platform: Smart Logic LPC2148 Development Board.

(UART 0)
Hardware Setup:-
    Connect a DB9 cable between GSM Module and UART0.
    COMPORT Settings
    Baudrate:-9600
    Databits:-8
    Parity:-None
    Stopbits:1
      

Clock Settings:
    FOSC    >>    12MHz (onboard)
    PLL        >>    M=5, P=2
    CCLK    >>  60MHz
    PCLK    >>  15MHz

********************************************************************************/
//Include controller specific header file
#include  <lpc214x.h>  

//Define Clock settings
#define PCLK            15000000          
#define BAUDRATE    9600     //Set Baud Rate here


//Function to generate software delay
//Calibrated to 1ms
void  delay_ms(unsigned char time)       //This Function is used to cause delay between LED ON and OFF events

 unsigned int  i, j;
 for (j=0; j<time; j++)
  for(i=0; i<8002; i++);
}

//Function to Initialize the UART0
void  Init_UART0(void)                    //This function setups UART0

   unsigned int Baud16;
   U0LCR = 0x83;                    // DLAB = 1
   Baud16 = (PCLK /(16*BAUDRATE)); 
   U0DLM = Baud16 >>8;                          
   U0DLL = Baud16 ;                      
   U0LCR = 0x03;
}
              
//Functin to Transmit the characters
void  UART0_SendByte(unsigned char data)       //A function to send a byte on UART0

   U0THR = data;                  
   while( (U0LSR&0x40)==0 );      
}
//Functin to Receive the characters
unsigned char  UART0_RecievedByte( )       //A function to send a byte on UART0
{    
  while((U0LSR&0x01)==0);
       return U0RBR;
      
}

//Functin to Transmit the string
void  UART0_SendStr(const unsigned char *str)     //A function to send a string on UART0

   while(1)
   { 
      if( *str == '\0' ) break;
      UART0_SendByte(*str++);      
   }
}

void  UART0_SendStr_ram(unsigned char *str)     //A function to send a string on UART0

   while(1)
   { 
      if( *str == '\0' ) break;
      UART0_SendByte(*str++);      
   }
}



int  main(void)

   
    PINSEL0 = 0x00050005; // Enable UART0 Rx and Tx pins
    Init_UART0();
 
UART0_SendStr("AT+CMGF=1\r");  
    delay_ms(250);
        delay_ms(250);
        delay_ms(250);
        delay_ms(250);
        delay_ms(250);
        delay_ms(250);
  UART0_SendStr("AT+CMGS=\"+919637331857\"\r");
    delay_ms(250);
        delay_ms(250);
        delay_ms(250);
        delay_ms(250);
    delay_ms(250);
    UART0_SendStr("GSM Interface");
        delay_ms(250);
    delay_ms(250);
    delay_ms(250);
    delay_ms(250);
    delay_ms(250);
    UART0_SendByte(26);
 
   
    while(1);  
  
 

********************************************************************************** 
Expt. 2b: GSM Module Interface for sendind SMS           
Platform: Smart Logic LPC2148 Development Board.

(UART 1)

Hardware Setup:-
    Connect a DB9 cable between GSM Module and UART0.
    COMPORT Settings
    Baudrate:-9600
    Databits:-8
    Parity:-None
    Stopbits:1
       

Clock Settings:
    FOSC    >>    12MHz (onboard)
    PLL        >>    M=5, P=2
    CCLK    >>  60MHz
    PCLK    >>  15MHz

********************************************************************************/
//Include controller specific header file
#include  <lpc214x.h>   

//Define Clock settings
#define PCLK            15000000           
#define BAUDRATE    9600     //Set Baud Rate here


//Function to generate software delay
//Calibrated to 1ms
void  delay_ms(unsigned char time)       //This Function is used to cause delay between LED ON and OFF events

 unsigned int  i, j;
 for (j=0; j<time; j++)
  for(i=0; i<8002; i++);
}

//Function to Initialize the UART0
void  Init_UART1(void)                    //This function setups UART0

   unsigned int Baud16;
   U1LCR = 0x83;                    // DLAB = 1
   Baud16 = (PCLK /(16*BAUDRATE)); 
   U1DLM = Baud16 >>8;                           
   U1DLL = Baud16 ;                       
   U1LCR = 0x03;
}
               
//Functin to Transmit the characters
void  UART1_SendByte(unsigned char data)       //A function to send a byte on UART0

   U1THR = data;                   
   while( (U1LSR&0x40)==0 );       
}

//Functin to Receive the characters
unsigned char  UART1_RecievedByte( )       //A function to send a byte on UART0
{    
  while((U1LSR&0x01)==0);
       return U1RBR;
       
}

//Functin to Transmit the string
void  UART1_SendStr(const unsigned char *str)     //A function to send a string on UART0

   while(1)
   { 
      if( *str == '\0' ) break;
      UART1_SendByte(*str++);       
   }
}

void  UART1_SendStr_ram(unsigned char *str)     //A function to send a string on UART0

   while(1)
   { 
      if( *str == '\0' ) break;
      UART1_SendByte(*str++);       
   }
}



int  main(void)

   
    PINSEL0 = 0x00050005; // Enable UART0 Rx and Tx pins
  Init_UART1();
 
UART1_SendStr("AT+CMGF=1\r");     // To set TEXT Mode
    delay_ms(250);
        delay_ms(250);
        delay_ms(250);
        delay_ms(250);
        delay_ms(250);
        delay_ms(250);
  UART1_SendStr("AT+CMGS=\"+919403253051\"\r"); //send SMS command
    delay_ms(250);
        delay_ms(250);
        delay_ms(250);
        delay_ms(250);
    delay_ms(250);
    UART1_SendStr("GSM Interface");  //message
        delay_ms(250);
    delay_ms(250);
    delay_ms(250);
    delay_ms(250);
    delay_ms(250);
    UART1_SendByte(26);  //sending ctrl+ Z

    while(1);   
  

********************************************************************************* 
Expt. 2a: UART Communication           
Platform: Smart Logic LPC2148 Development Board.


Hardware Setup:-
    Connect a DB9 cable between PC and UART0 or UART1.
    COMPORT Settings
    Baudrate:-9600
    Databits:-8
    Parity:-None
    Stopbits:1
       

Clock Settings:
    FOSC    >>    12MHz (onboard)
    PLL        >>    M=5, P=2
    CCLK    >>  60MHz
    PCLK    >>  15MHz

********************************************************************************/
//Include controller specific header file
#include  <lpc214x.h>   

//Define Clock settings
#define PCLK            15000000           
#define BAUDRATE    9600     //Set Baud Rate here


//Function to generate software delay
//Calibrated to 1ms
void  delay_ms(unsigned char time)       //This Function is used to cause delay between LED ON and OFF events

 unsigned int  i, j;
 for (j=0; j<time; j++)
  for(i=0; i<8002; i++);
}

//Function to Initialize the UART0
void  Init_UART1(void)                    //This function setups UART0

   unsigned int Baud16;
   U1LCR = 0x83;                    // DLAB = 1
   Baud16 = (PCLK /(16*BAUDRATE)); 
   U1DLM = Baud16 >>8;                           
   U1DLL = Baud16 ;                       
   U1LCR = 0x03;
}
               
//Functin to Transmit the characters
void  UART1_SendByte(unsigned char data)       //A function to send a byte on UART0

   U1THR = data;                   
   while( (U1LSR&0x40)==0 );       
}

//Functin to Receive the characters
/*unsigned char  UART1_RecievedByte( )       //A function to send a byte on UART0
{    
  while((U1LSR&0x01)==0);
       return U1RBR;
       
}*/

//Functin to Transmit the string
void  UART1_SendStr(const unsigned char *str)     //A function to send a string on UART0

   while(1)
   { 
      if( *str == '\0' ) break;
      UART1_SendByte(*str++);       
   }
}

void  UART1_SendStr_ram(unsigned char *str)     //A function to send a string on UART0

   while(1)
   { 
      if( *str == '\0' ) break;
      UART1_SendByte(*str++);       
   }
}



int  main(void)

    unsigned char rx_data[20], i;
    PINSEL0 = 0x00050005; // Enable UART0 Rx and Tx pins
  Init_UART1();
 
    UART1_SendStr("Smart Logic Technologies\n\r ");   
  UART1_SendStr("LPC2148 DEVLOPMENT BOARD\n\r ");
    UART1_SendStr("Send 10 Characters\n\r ");
 
    for (i=0; i<10; i++)
    {   
         rx_data[i]=UART1_RecievedByte();
  }  
    rx_data [10]= 0; // To make received string as ASCIIZ string
    UART1_SendStr("\n\rTransmitted Characters are:");
    UART1_SendStr_ram (rx_data);
    UART1_SendStr("\n\rRx Tx test complete\n\r");
    while(1);   
  


**********************************************************************************

Expt. 2C: GPS Module Interface           
Platform: Smart Logic LPC2148 Development Board.

Hardware Setup:-
    Connect a DB9 cable between GSM Module and UART0.
    COMPORT Settings
    Baudrate:-9600
    Databits:-8
    Parity:-None
    Stopbits:1
       

Clock Settings:
    FOSC    >>    12MHz (onboard)
    PLL        >>    M=5, P=2
    CCLK    >>  60MHz
    PCLK    >>  15MHz

********************************************************************************/
//Include controller specific header file
#include  <lpc214x.h>   
#include <string.h>

//Define Clock settings
#define PCLK            15000000           
#define BAUDRATE    9600     //Set Baud Rate here


//Function to generate software delay
//Calibrated to 1ms
void  delay_ms(unsigned int time)      

 unsigned int  i, j;
 for (j=0; j<time; j++)
  for(i=0; i<8002; i++);
}

void LCD_command(unsigned char command)
{
    IOCLR0 = 0xFF<<16;    // Clear LCD Data lines
    IOCLR1=1<<16;                 // RS=0 for command
    IOCLR1=1<<17;                 // RW=0 for write
    IOSET0=command<<16;    // put command on data line
    IOSET1=(1<<18);            // en=1
    delay_ms(10) ;            // delay
    IOCLR1=(1<<18);             // en=0
}

LCD_data(unsigned char data)
{
    IOCLR0 = 0xFF<<16;    // Clear LCD Data lines
    IOSET1=1<<16;                 // RS=1 for data
    IOCLR1=1<<17;                 // RW=0 for write
    IOSET0= data<<16;     // put command on data line
    IOSET1=(1<<18);            //en=1
    delay_ms(10) ;          //delay
    IOCLR1=(1<<18);            //en=0
 }

LCD_init()
{
    LCD_command(0x38);    //8bit mode and 5x8 dotes (function set)
    delay_ms(10) ;            // delay
    LCD_command(0x0c);    //display on, cursor off, cursor char blinking off(display on/off)
    delay_ms(10) ;            // delay
    LCD_command(0x06);  //cursor increament and display shift(entry mode set)
    delay_ms(10) ;            // delay
    LCD_command(0x01);     //clear lcd(clear command)
    delay_ms(10) ;            // delay
    LCD_command(0x80);     //set cursor to 0th location    1st lne
   
}

LCD_write_string(unsigned char *string)
{
  while(*string)            //Check for End of String
  LCD_data(*string++);     //sending data on LCD byte by byte
}


//Function to Initialize the UART0
void  Init_UART1(void)                    //This function setups UART0

   unsigned int Baud16;
   U1LCR = 0x83;                    // DLAB = 1
   Baud16 = (PCLK /(16*BAUDRATE)); 
   U1DLM = Baud16 >>8;                           
   U1DLL = Baud16 ;                       
   U1LCR = 0x03;
}
               
//Functin to Transmit the characters
void  UART1_SendByte(unsigned char data)       //A function to send a byte on UART0

   U1THR = data;                   
   while( (U1LSR&0x40)==0 );       
}

//Functin to Receive the characters
unsigned char  UART1_RecievedByte( )       //A function to send a byte on UART0
{    
  while((U1LSR&0x01)==0);
       return U1RBR;
       
}

//Functin to Transmit the string
void  UART1_SendStr(const unsigned char *str)     //A function to send a string on UART0

   while(1)
   { 
      if( *str == '\0' ) break;
      UART1_SendByte(*str++);       
   }
}

void  UART1_SendStr_ram(unsigned char *str)     //A function to send a string on UART0

   while(1)
   { 
      if( *str == '\0' ) break;
      UART1_SendByte(*str++);       
   }
}



int  main(void)

    unsigned char string1[] = "GPGGA";
    unsigned char i, flag=0;
    unsigned char cmd_data[5], time_data[15], latitude[15], longitude[15];
    PINSEL0 = 0x00050005; // Enable UART0 Rx and Tx pins
  //while (strcmp(string1, string2));
    IODIR1= 0x07<<16;    //Configure P1.18, P1.17, P1.16 as output
    IODIR0=    0xFF<<16; //Configure P0.23 - P0.16 as output
    LCD_init();                //Initialize LCD 16x2
    Init_UART1();
   
    LCD_write_string("Smart Logic Tech");
    LCD_command(0xc0);//second line
    LCD_write_string("GPS_Interface");
    delay_ms(1000);
   
   
    while(1)
    {
    if(UART1_RecievedByte()=='$')
        {
            for (i=0; i<5; i++)
            {   
            flag=0;
            cmd_data[i]=UART1_RecievedByte();
            if (cmd_data[i]!= string1[i])
                {
                    flag = 1;
                    break;
                }
            }
         if (flag == 0)
         {
            for (i=0; i<12; i++)
            {   
                time_data[i]=UART1_RecievedByte();
            }
            time_data[12]='\0';
           
            for (i=0; i<12; i++)
            {   
                latitude[i]=UART1_RecievedByte();
            }
            latitude[11]='\0';
           
            for (i=0; i<12; i++)
            {   
                longitude[i]=UART1_RecievedByte();
            }
            longitude[12]='\0';
           
            LCD_command(0x01);
            LCD_write_string("GPS sent:");
            LCD_write_string(cmd_data);
            LCD_command(0xC0);
            LCD_write_string("UTC:");
            LCD_write_string(time_data);
            delay_ms(2000);
            LCD_command(0x01);
            LCD_write_string("Ln:");
            LCD_write_string(longitude);
            LCD_command(0xC0);
            LCD_write_string("Lt:");
            LCD_write_string(latitude);
            delay_ms(1000);
         }
        }
    }           
  } 

********************************************************************************** 
Expt. 3a.: On-chip ADC Programming
Platform: Smart Logic LPC2148 Development Board.

       
Clock Settings:
    FOSC    >>    12MHz (onboard)
    PLL        >>    M=5, P=2
    CCLK    >>  60MHz
    PCLK    >>  15MHz
       
Hardware Setup:-
    LCD data pin :-P0.16-P0.23
    RS-P1.16
    RW-P1.17
    EN-P1.18           

********************************************************************************/

//Include Controller specific header file
#include <lpc214x.h>

//Function to generate software delay
//Calibrated to 1ms
void  delay_ms(unsigned char time)      

 unsigned int  i, j;
 for (j=0; j<time; j++)
  for(i=0; i<8002; i++);
}

void LCD_command(unsigned char command)
{
    IOCLR0 = 0xFF<<16;    // Clear LCD Data lines
    IOCLR1=1<<16;                 // RS=0 for command
    IOCLR1=1<<17;                 // RW=0 for write
    IOSET0=command<<16;    // put command on data line
    IOSET1=(1<<18);            // en=1
    delay_ms(10) ;            // delay
    IOCLR1=(1<<18);             // en=0
}

LCD_data(unsigned char data)
{
    IOCLR0 = 0xFF<<16;    // Clear LCD Data lines
    IOSET1=1<<16;                 // RS=1 for data
    IOCLR1=1<<17;                 // RW=0 for write
    IOSET0= data<<16;     // put command on data line
    IOSET1=(1<<18);            //en=1
    delay_ms(10) ;          //delay
    IOCLR1=(1<<18);            //en=0
 }

LCD_init()
{
    LCD_command(0x38);    //8bit mode and 5x8 dotes (function set)
    delay_ms(10) ;            // delay
    LCD_command(0x80);     //set cursor to 0th location    1st lne
    delay_ms(10) ;            // delay
    LCD_command(0x01);     //clear lcd(clear command)
    delay_ms(10) ;            // delay
    LCD_command(0x06);  //cursor increament and display shift(entry mode set)
    delay_ms(10) ;            // delay
    LCD_command(0x0c);    //display on, cursor off, cursor char blinking off(display on/off)
}

LCD_write_string(unsigned char *string)
{
  while(*string)            //Check for End of String
  LCD_data(*string++);     //sending data on LCD byte by byte
}

int main(void)
{
    unsigned int ADC_Result=0;
    unsigned char i, Thousands,Hundreds,Tens,Ones;
    PINSEL1 = 0x04000000;
    PINSEL2 = 0X00;        //Configure PORT1 as GPIO
    IODIR1= 0x07<<16;    //Configure P1.18, P1.17, P1.16 as output
    IODIR0=    0xFF<<16; //Configure P0.23 - P0.16 as output
    LCD_init();                //Initialize LCD 16x2
    LCD_write_string("Smart Logic Tech");
    LCD_command(0xc0);//second line
    LCD_write_string("AD0.2 O/P=");
   
    /* Configure ADC0 for following
    ADC Channel = AD0.2
    ADC Clock = 3 MHz
    Clock Selection = 11 Clock Cycles/10bit
    Start Condition = No start
    Power Down = 1, EDGE = 0, BURST = 0 */
   
    AD0CR = 0x01200404;
   
    while (1)
    {
       
        AD0CR |= 1<<24;    //Start ADC
        //Wait for the conversion to be completed
        while((AD0DR2&0x80000000)==0);              
        ADC_Result = AD0DR2; //Store converted data
        ADC_Result = (ADC_Result>>6) & 0x3FF;
        LCD_command (0xCA);            //Goto 10th place on second line of LCD
     
        i = ADC_Result/1000 ;        //Get the thousands place
      Thousands = i + 0x30;        // Convert it to ASCII
      LCD_data (Thousands);    // Display thousands place
     
        i = (ADC_Result%1000)/100;    //Get the Hundreds place
      Hundreds = i + 0x30;        // Convert it to ASCII
      LCD_data (Hundreds);        //Display Hundreds place
 
        i = ((ADC_Result%1000)%100)/10; //Get the Tens place
      Tens = i + 0x30;            // Convert it to ASCII
      LCD_data (Tens);            //Display Tens place
     
        i = ADC_Result%10 ;            //Get the Ones place
      Ones = i + 0x30;             // Convert it to ASCII
      LCD_data (Ones);        //Display Ones place

      delay_ms(250);        //Delay between two conversions
    }

********************************************************************************** 
Expt. 3b.: On-chip ADC Programming using Interrupt
Platform: Smart Logic LPC2148 Development Board.
       

Clock Settings:
    FOSC    >>    12MHz (onboard)
    PLL        >>    M=5, P=2
    CCLK    >>  60MHz
    PCLK    >>  15MHz
       
Hardware Setup:-
    LCD data pin :-P0.16-P0.23
    RS-P1.16
    RW-P1.17
    EN-P1.18           

********************************************************************************/

//Include Controller specific header file
#include <lpc214x.h>

//Function to generate software delay
//Calibrated to 1ms
void  delay_ms(unsigned char time)      

 unsigned int  i, j;
 for (j=0; j<time; j++)
  for(i=0; i<8002; i++);
}

void LCD_command(unsigned char command)
{
    IOCLR0 = 0xFF<<16;    // Clear LCD Data lines
    IOCLR1=1<<16;                 // RS=0 for command
    IOCLR1=1<<17;                 // RW=0 for write
    IOSET0=command<<16;    // put command on data line
    IOSET1=(1<<18);            // en=1
    delay_ms(10) ;            // delay
    IOCLR1=(1<<18);             // en=0
}

LCD_data(unsigned char data)
{
    IOCLR0 = 0xFF<<16;    // Clear LCD Data lines
    IOSET1=1<<16;                 // RS=1 for data
    IOCLR1=1<<17;                 // RW=0 for write
    IOSET0= data<<16;     // put command on data line
    IOSET1=(1<<18);            //en=1
    delay_ms(10) ;          //delay
    IOCLR1=(1<<18);            //en=0
 }

LCD_init()
{
    LCD_command(0x38);    //8bit mode and 5x8 dotes (function set)
    delay_ms(10) ;            // delay
    LCD_command(0x80);     //set cursor to 0th location    1st lne
    delay_ms(10) ;            // delay
    LCD_command(0x01);     //clear lcd(clear command)
    delay_ms(10) ;            // delay
    LCD_command(0x06);  //cursor increament and display shift(entry mode set)
    delay_ms(10) ;            // delay
    LCD_command(0x0c);    //display on, cursor off, cursor char blinking off(display on/off)
}

LCD_write_string(unsigned char *string)
{
  while(*string)            //Check for End of String
  LCD_data(*string++);     //sending data on LCD byte by byte
}

void  __irq ADC0_ISR(void)
{
        unsigned int ADC_Result=0;
        unsigned char i;
        ADC_Result = AD0DR2; //Store converted data
        ADC_Result = (ADC_Result>>6) & 0x3FF;
        LCD_command (0xCA);            //Goto 10th place on second line of LCD
     
        i = (ADC_Result/1000)+0x30 ;        //Get the thousands place
      LCD_data (i);    // Display thousands place
     
        i = ((ADC_Result%1000)/100)+0x30;    //Get the Hundreds place
      LCD_data (i);        //Display Hundreds place
 
        i = (((ADC_Result%1000)%100)/10)+0x30; //Get the Tens place
      LCD_data (i);            //Display Tens place
     
        i = (ADC_Result%10)+0x30 ;            //Get the Ones place
      LCD_data (i);        //Display Ones place
       
        VICVectAddr = 0x00;           //Acknowledge Interrupt
}   

int main(void)
{
    PINSEL1 = 0x04000000;
    PINSEL2 = 0X00;        //Configure PORT1 as GPIO
    IODIR1= 0x07<<16;    //Configure P1.18, P1.17, P1.16 as output
    IODIR0=    0xFF<<16; //Configure P0.23 - P0.16 as output
    LCD_init();                //Initialize LCD 16x2
    LCD_write_string("Smart Logic Tech");
    LCD_command(0xc0);//second line
    LCD_write_string("AD0.2 O/P=");
   
    VICIntSelect = 0x00000000;        // Setting all interrupts as IRQ(Vectored)
    VICVectCntl0 = 0x20 | 18;        // Assigning Highest Priority Slot to ADC0 and enabling this slot
    VICVectAddr0 = (unsigned long)ADC0_ISR; // Storing vector address of ADC0
    VICIntEnable = (1<<18);       //Enable AD0 Interrupt
   
    /* Configure ADC0 for following
    ADC Channel = AD0.2
    ADC Clock = 3 MHz
    Clock Selection = 11 Clock Cycles/10bit
    Start Condition = No start
    Power Down = 1, EDGE = 0, BURST = 1 */
   
    AD0CR = 0x00210404;
   
    while (1);
   

********************************************************************************** 
Expt4_SD_Card_Interface
//Include controller specific header file
#include  <lpc214x.h>   
#include "UART0.H"
#include "SPI.H"
#include "SDCARD.H"

int  main(void)

   
    unsigned char read_data[512];
    unsigned char write_data[512]= {"\n\rSmart Logic Techologies\n\rSD Card write and read\n\r"};
 
    Init_UART0();
    SPI_Init();
 
    UART0_SendStr("Smart Logic Technologies\n\r");   
  UART0_SendStr("SD_Card_Testing\n\r ");
   
  UART0_SendStr("sending commands for card init\n\r ");
   
  if ( sdcard_init()!= 0)
    {
        UART0_SendStr("Init Failed\n\r");
        while ( 1 ); /* Very bad happened */
    }
   
    UART0_SendStr("sending commands for writing\n\r ");
    if ( sdcard_write_block(0, write_data) == 0 )
    {
        UART0_SendStr("Write Complete\n\r");
    }
    else
    {
        UART0_SendStr("Write Failed\n\r");
        while(1);
    }
   
    if(sdcard_read_block(0,read_data)==0)
    {   
        UART0_SendStr(read_data);
  }
    else
    {
        UART0_SendStr("Read Failed\n\r");
        while(1);
    }
   
    UART0_SendStr("SD_Card_Tested Successfully\n\r ");
    while(1);   
  

 *********************************************************************************
 Expt. 5a.: Interface I2C EEPROM to LPC2148
Platform: Smart Logic LPC2148 Development Board.

      
Clock Settings:
    FOSC    >>    12MHz (onboard)
    PLL        >>    M=5, P=2
    CCLK    >>  60MHz
    PCLK    >>  15MHz
       
Hardware Setup:-
    LCD data pin :-P0.16-P0.23
    RS-P1.16
    RW-P1.17
    EN-P1.18     
      
********************************************************************************/

//Include Controller specific header file
#include <lpc214x.h>

//Function to generate software delay
//Calibrated to 1ms
void  delay_ms(unsigned char time)      

 unsigned int  i, j;
 for (j=0; j<time; j++)
  for(i=0; i<8002; i++);
}

void LCD_command(unsigned char command)
{
    IOCLR0 = 0xFF<<16;    // Clear LCD Data lines
    IOCLR1=1<<16;                 // RS=0 for command
    IOCLR1=1<<17;                 // RW=0 for write
    IOSET0=command<<16;    // put command on data line
    IOSET1=(1<<18);            // en=1
    delay_ms(10) ;            // delay
    IOCLR1=(1<<18);             // en=0
}

LCD_data(unsigned char data)
{
    IOCLR0 = 0xFF<<16;    // Clear LCD Data lines
    IOSET1=1<<16;                 // RS=1 for data
    IOCLR1=1<<17;                 // RW=0 for write
    IOSET0= data<<16;     // put command on data line
    IOSET1=(1<<18);            //en=1
    delay_ms(10) ;          //delay
    IOCLR1=(1<<18);            //en=0
 }

LCD_init()
{
    LCD_command(0x38);    //8bit mode and 5x8 dotes (function set)
    delay_ms(10) ;            // delay
    LCD_command(0x80);     //set cursor to 0th location    1st lne
    delay_ms(10) ;            // delay
    LCD_command(0x01);     //clear lcd(clear command)
    delay_ms(10) ;            // delay
    LCD_command(0x06);  //cursor increament and display shift(entry mode set)
    delay_ms(10) ;            // delay
    LCD_command(0x0c);    //display on, cursor off, cursor char blinking off(display on/off)
}

LCD_write_string(unsigned char *string)
{
  while(*string)            //Check for End of String
  LCD_data(*string++);     //sending data on LCD byte by byte
}


void I2C_Init()
{

 I2C0CONCLR=0xFF;    //Clear All bits
 I2C0CONSET=0x40;    //Set I2C Enable
 I2C0SCLH=75;            //Set I2C_Clock = 100Khz
 I2C0SCLL=75;            //with 50% duty cycle
}

void Send_Start()
{
 I2C0CONSET=0x20;
}


void Send_Stop()
{
 I2C0CONSET=0x10;
}

unsigned char I2C_Status(unsigned char status_code)
{
    unsigned char status, SI_Flag;
    while(!((SI_Flag=I2C0CONSET)& 0x08));
    status=I2C0STAT;                //Read Status byte
    I2C0CONCLR=0x28;                //Clear SI bit
    if(status!=status_code)
    {
        return 1;
    }
    else
    {
  return 0;
    }
}

unsigned char byte_write(unsigned char device,unsigned char address,unsigned char data)
{
    I2C0CONCLR=0xFF;    //Clear All bits
    I2C0CONSET=0x40;    //Set I2C Enable
    Send_Start();
    if(I2C_Status(0x08))    //Start has been transmitted
    { return 1;    }

    I2C0DAT= device&0xFE;          //Sending Device Address
    if(I2C_Status(0x20))                    //Device address, block num and write has been transmitted
    {  return 1; }

    I2C0DAT= address;                 //Sending Memory Location
    if(I2C_Status(0x30))    //Block address has been transmitted
    {    return 1; }

    I2C0DAT= data;                 //Sending data
    if(I2C_Status(0x30))    //Block address has been transmitted
    {    return 1; }
  delay_ms(10);
    Send_Stop();
    delay_ms(10);
    return 0;
}


unsigned char byte_read(unsigned char device,unsigned char address)
{
    unsigned char data;
    I2C0CONCLR=0xFF;    //Clear All bits
    I2C0CONSET=0x40;    //Set I2C Enable   
    Send_Start();
    if(I2C_Status(0x08))    //Start has been transmitted
    {  return 1; }

    I2C0DAT= device&0xFE;  //Selecting device in write mode
    if(I2C_Status(0x20))    //Device address, block num and write has been transmitted
    {  return 1; }

    I2C0DAT= address;        //Sending Memory Location
    if(I2C_Status(0x30))    //Block address has been transmitted
    {  return 1; }

    Send_Start();             // Repeat Start
    if(I2C_Status(0x10))    //Repeated Start has been transmitted
    {  return 1; }

    I2C0DAT= device | 0x01;            //Device address, block num and read has been transmitted
    if(I2C_Status(0x48))    //
    {  return 1; }

    I2C0CONCLR=0x04;         //set hardware to send nack
    if(I2C_Status(0x58))    //last byte has been received and NACK has been returned
    {    return 1;   }
    data=I2C0DAT;

    Send_Stop();
    return data;
}



int main(void)
{
    unsigned char read_data;
    PINSEL0 = 0x00000055;
    PINSEL1 = 0x00;        //Configure PORT0 as GPIO
    PINSEL2 = 0X00;        //Configure PORT1 as GPIO
    IODIR1= 0x07<<16;    //Configure P1.18, P1.17, P1.16 as output
    IODIR0=    0xFF<<16; //Configure P0.23 - P0.16 as output
    LCD_init();                //Initialize LCD 16x2
    I2C_Init();
    LCD_write_string("Smart Logic Tech");
    LCD_command(0xc0);//second line
    LCD_write_string("Embed. Processors");
   
    byte_write (0xA0,0x20,0x41);
    delay_ms (100);
    read_data = byte_read (0xA0,0x20);
    LCD_command (0x01);
    LCD_data (read_data);
    while (1);
   
}

********************************************************************************* 
Expt. 5b.: Interface I2C EEPROM to LPC2148
Display the contents on UART
Platform: Smart Logic LPC2148 Development Board.

       
Clock Settings:
    FOSC    >>    12MHz (onboard)
    PLL        >>    M=5, P=2
    CCLK    >>  60MHz
    PCLK    >>  15MHz
       
Hardware Setup:-
    Connect a DB9 cable between PC and UART0 or UART1.
    COMPORT Settings
    Baudrate:-9600
    Databits:-8
    Parity:-None
    Stopbits:1    

********************************************************************************/

//Include Controller specific header file
#include <lpc214x.h>

//Define Clock settings
#define PCLK            15000000           
#define BAUDRATE    9600     //Set Baud Rate here


//Function to generate software delay
//Calibrated to 1ms
void  delay_ms(unsigned int time)      

 unsigned int  i, j;
 for (j=0; j<time; j++)
  for(i=0; i<8002; i++);
}

//Function to Initialize the UART0
void  Init_UART0(void)                    //This function setups UART0

   unsigned int Baud16;
   U0LCR = 0x83;                    // DLAB = 1
   Baud16 = (PCLK /(16*BAUDRATE)); 
   U0DLM = Baud16 >>8;                           
   U0DLL = Baud16 ;                       
   U0LCR = 0x03;
}
               
//Functin to Transmit the characters
void  UART0_SendByte(unsigned char data)       //A function to send a byte on UART0

   U0THR = data;                   
   while( (U0LSR&0x40)==0 );       
}

//Functin to Receive the characters
unsigned char  UART0_RecievedByte( )       //A function to send a byte on UART0
{    
  while((U0LSR&0x01)==0);
       return U0RBR;
       
}

//Functin to Transmit the string
void  UART0_SendStr(const unsigned char *str)     //A function to send a string on UART0

   while(1)
   { 
      if( *str == '\0' ) break;
      UART0_SendByte(*str++);       
   }
}



void I2C_Init()
{

 I2C0CONCLR=0xFF;    //Clear All bits
 I2C0CONSET=0x40;    //Set I2C Enable
 I2C0SCLH=75;            //Set I2C_Clock = 100Khz
 I2C0SCLL=75;            //with 50% duty cycle
}

void Send_Start()
{
 I2C0CONSET=0x20;
}


void Send_Stop()
{
 I2C0CONSET=0x10;
}


unsigned char byte_write(unsigned char device,unsigned char address,unsigned char data)
{
   
    I2C0CONCLR=0xFF;    //Clear All bits
    I2C0CONSET=0x40;    //Set I2C Enable   
   
    Send_Start();            //Send Start Condition
    while(!(I2C0CONSET& 0x08)); //Check SI Flag
    I2C0CONCLR=0x28;    //Clear SI flag
   
    I2C0DAT= device&0xFE; //Selecting device in write mode
    while(!(I2C0CONSET& 0x08)); //Check SI Flag
    I2C0CONCLR=0x08;    //Clear SI flag
   
    I2C0DAT= address;  //Sending Memory Location
    while(!(I2C0CONSET& 0x08)); //Check SI Flag
    I2C0CONCLR=0x08;    //Clear SI flag
   
    I2C0DAT= data;                 //Sending data
    while(!(I2C0CONSET& 0x08)); //Check SI Flag
  I2C0CONCLR=0x08;   
   
    Send_Stop();        //send stop condition
   
    return 0;
}


unsigned char byte_read(unsigned char device,unsigned char address)
{
    unsigned char data;

    I2C0CONCLR=0xFF;    //Clear All bits
    I2C0CONSET=0x40;    //Set I2C Enable   
   
    Send_Start();            //Send Start Condition
    while(!(I2C0CONSET& 0x08)); //Check SI Flag
    I2C0CONCLR=0x28;    //Clear SI flag
   
    I2C0DAT= device&0xFE; //Selecting device in write mode
    while(!(I2C0CONSET& 0x08)); //Check SI Flag
    I2C0CONCLR=0x28;    //Clear SI flag
   
    I2C0DAT= address;        //Sending Memory Location
    while(!(I2C0CONSET& 0x08)); //Check SI Flag
    I2C0CONCLR=0x28;    //Clear SI flag
   
    Send_Start();           // Repeat Start
    while(!(I2C0CONSET& 0x08)); //Check SI Flag
    I2C0CONCLR=0x28;    //Clear SI flag
   
    I2C0DAT= device | 0x01;    //Selecting device in read mode
    while(!(I2C0CONSET& 0x08)); //Check SI Flag
    I2C0CONCLR=0x28;    //Clear SI flag
    I2C0CONCLR=0x04;         //set hardware to send nack
   
    while(!(I2C0CONSET& 0x08)); //Check SI Flag
    I2C0CONCLR=0x28;    //Clear SI flag
    data=I2C0DAT;   
   
    Send_Stop();        //send stop condition
    return data;
}



int main(void)
{
    unsigned char read_data, i;
    unsigned char write_data[]= "EEPROM I2C TEST ";
    PINSEL0 = 0x00000055; //Configure Port pin as I2C & UART pin
    PINSEL1 = 0x00;        //Configure PORT0 as GPIO
    PINSEL2 = 0X00;        //Configure PORT1 as GPIO
    IODIR1= 0x07<<16;    //Configure P1.18, P1.17, P1.16 as output
    IODIR0=    0xFF<<16; //Configure P0.23 - P0.16 as output
   
    I2C_Init();
    Init_UART0();
 
    UART0_SendStr("Smart Logic Technologies\n\r ");   
  UART0_SendStr("Embedded Processors\n\r ");
    UART0_SendStr("Writing to I2C: ");
   
    for (i=0;i<16; i++)
    {   
    byte_write (0xA0,i, write_data[i]);
    UART0_SendByte(write_data[i]);   
    delay_ms (50);
    }
    delay_ms (500);
    UART0_SendStr("\n\rReading from I2C: ");
    for (i=0;i<16; i++)
    {   
    read_data = byte_read (0xA0,i);
    UART0_SendByte(read_data);       
    delay_ms (50);
    }
   
    while (1);
   
}
 

***********************************************************************************

No comments: