0% found this document useful (0 votes)
11 views28 pages

Colored Code Output

The document contains code for interfacing an LCD with an LPC2148 microcontroller using the I2C protocol, demonstrating master-slave communication with a PCF8574 I2C to LCD converter. It includes functions for initializing I2C, sending commands and data to the LCD, and displaying strings. Additionally, there is a section for interfacing a 4x4 keypad, although the code for that part is incomplete.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views28 pages

Colored Code Output

The document contains code for interfacing an LCD with an LPC2148 microcontroller using the I2C protocol, demonstrating master-slave communication with a PCF8574 I2C to LCD converter. It includes functions for initializing I2C, sending commands and data to the LCD, and displaying strings. Additionally, there is a section for interfacing a 4x4 keypad, although the code for that part is incomplete.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 28

Formatted Code with Highlights

====== Final Combined Code with Headlines ======

====== LCD I2C Interface with LPC2148 (Copy) ======


(File: lcd_i2c_final - Copy.c)

//////////////////////////////////////////////////////
// This program demonstrates a Master and slave
// communication using I2C interface of LPC2148
// In this example Master is LPC2148 device
// and slave is a I2CtoLCD converter PCF8574
// Author: www.tmisystems.in
//////////////////////////////////////////////////////

#include
#include

////////////////////////
#define SLAVE_ADDR 78
#define MAX 12
#define AA 2
#define SI 3
#define STO 4
#define STA 5
#define I2EN 6

////////////////////////////
// Delay Function
//
wait(int count)
{
while(count-- );
}
//////////////////////////
// Initialize I2c
//
//
//
void I2C_Init (void)
{
VPBDIV = 0x02; // sets FOSC = 60MHHZ
PINSEL0 = 0x00000050; // set po.2,p0.3 to sda scl
I2C0SCLH = 150; //50%duty,I2CFreq->100KHz,PCLK=30MHz
I2C0SCLL = 150;
I2C0CONSET = (1<
//PINSEL1 = 0x00000000 ; // Configure P0.16 to P0.31 as GPIO
// IO0DIR = 0x000F0000 ;
// IO0CLR = 0x000F0000;
}
//////////////////////////
// Enter Master
// transmitter Mode
//
int I2C_Start ()
{
I2C0CONCLR = 1 << STO;
I2C0CONCLR = 1 << SI;
I2C0CONSET = 1 << STA;
return 0;
}

//////////////////////////////////////
// Delay function
//
//
void delay_ms(int count)
{
int j=0,i=0;

for(j=0;j
{
/* At 60Mhz, the below loop introduces
delay of 10 us */
for(i=0;i<35;i++);
}
}
///////////////////////////////////////////////////////
// Function to send data from Master to slave I2C
// device
//

// Function to send a byte of char data


// EX:if data = 0x12 higher nibble
// is sent first in this case 0x1 and then lower
// nibble.whyy? Because slave is set to 4bit mode.
// The Format shud be :7 6 5 4 3 2 1 0
// 0 E RW RS|D A T A
//

void senddata(char data){


while(!(I2C0CONSET & 0x08));
I2C0DAT = data;
I2C0CONCLR = 1 << SI;
delay_ms(200);
}
void LCD_Command (char cmnd) /* LCD16x2 command funtion */
{
char data;
data = (cmnd & 0xF0)|0x04|0x08;/* Send upper nibble */ // 0X08 NEED TO
CHK
//rs=0; rw=0; en=1; 0100 = 0x04 LSB
senddata(data);
delay_ms(100);
//en=0;
data = (cmnd & 0xF0)|0x08;
senddata(data);
delay_ms(100);

data = (cmnd << 4)|0x04|0x08;/* Send lower nibble */


senddata(data);
delay_ms(100);
//en=0;
data = (cmnd << 4)|0x08;
senddata(data);
delay_ms(100);
}
void LCD_Char (char char_data) /* LCD data write function */
{
char data;
//rs=1; rw=0; en=1;
data =(char_data & 0xF0)|0x08|0x05;/* Send upper nibble */
senddata(data);
delay_ms(1);
//rs=1; rw =0 ; en=0;
data =(char_data & 0xF0)|0x08|0x01;
senddata(data);
delay_ms(2);

////rs=1; rw =0 ; en=1;
data = (char_data << 4)|0x08|0x05;/* Send lower nibble */
senddata(data);
delay_ms(1);
////rs=1; rw =0 ; en=0;
data = (char_data << 4)|0x08|0x01;
senddata(data);
delay_ms(5);
senddata(0x08);
}
void LCD_String (char *str) /* Send string to LCD function */
{
int i;
for(i=0;str[i]!=0;i++) /* Send each char of string till the NULL */
{
LCD_Char (str[i]); /* Call LCD data write */
}
}

//void LCD_String_xy (char row, char pos, char *str) /* Send string to LCD
function */
//{
//if (row == 0)
//LCD_Command((pos & 0x0F)|0x80);
//else if (row == 1)
//LCD_Command((pos & 0x0F)|0xC0);
// LCD_String(str); /* Call LCD string function */
//}
int main(){

char code = SLAVE_ADDR;// 0x6e;


char str[] = "ROSU";
int i =0;
I2C_Init(); //Initialize I2C moodule
I2C_Start(); //start I2C module
wait(4000);

while(!(I2C0CONSET & 0x08)); //wait till SI bit set


//IO0SET = (1<<21);
I2C0CONCLR = 1 << STO;
I2C0CONCLR = 1 << STA;
I2C0CONSET = 1 << AA;
I2C0DAT = code;

I2C0CONCLR = 1 << SI;


//wait(4000);
while(!(I2C0CONSET & 0x08));
//IO0SET = 0x00010000;

if(I2C0STAT == 0x18) //SLA+W has been


transmitted; ACK has been received
{
// IO0SET = (1<<23);
I2C0CONSET = 1 << AA;
I2C0DAT = 0x00;//x00;//Buff[index];
I2C0CONCLR = 1 << SI;
// IO0SET = 0x00020000;
while(!(I2C0CONSET & 0x08));
for(i=0; i < 2000;i++)wait(800);
if(I2C0STAT == 0x28) //Data byte in I2DAT has been
transmitted; ACK has been received
{
// IO0SET = 0x00030000;

LCD_Command(0x02);
LCD_Command (0x28); /* Initialization of
16X2 LCD in 4bit mode */
LCD_Command (0x0C); /* Display ON Cursor OFF
*/
LCD_Command (0x06); /* Auto Increment cursor
*/
LCD_Command (0x01); /* Clear display */
LCD_Command (0x80); /* Cursor at home
position */

LCD_String(str);
//LCD_String_xy(1,0,"HELLO WORLD");

// while(1){
// IO0CLR = 0x000F0000;
// for(i=0; i < 2000;i++)wait(800);
// IO0SET = 0x000F0000;
// for(i=0; i < 2000;i++)wait(800);
// } ;

}
}
}//END OF MAIN
====== LCD I2C Interface with LPC2148 ======
(File: lcd_i2c_final.c)

//////////////////////////////////////////////////////
// This program demonstrates a Master and slave
// communication using I2C interface of LPC2148
// In this example Master is LPC2148 device
// and slave is a I2CtoLCD converter PCF8574
// Author: www.tmisystems.in
//////////////////////////////////////////////////////

#include
#include

////////////////////////
#define SLAVE_ADDR 78
#define MAX 12
#define AA 2
#define SI 3
#define STO 4
#define STA 5
#define I2EN 6

////////////////////////////
// Delay Function
//
wait(int count)
{
while(count-- );
}
//////////////////////////
// Initialize I2c
//
//
//
void I2C_Init (void)
{
VPBDIV = 0x02; // sets FOSC = 60MHHZ
PINSEL0 = 0x00000050; // set po.2,p0.3 to sda scl
I2C0SCLH = 150; //50%duty,I2CFreq->100KHz,PCLK=30MHz
I2C0SCLL = 150;
I2C0CONSET = (1<
//PINSEL1 = 0x00000000 ; // Configure P0.16 to P0.31 as GPIO
// IO0DIR = 0x000F0000 ;
// IO0CLR = 0x000F0000;
}
//////////////////////////
// Enter Master
// transmitter Mode
//
int I2C_Start ()
{
I2C0CONCLR = 1 << STO;
I2C0CONCLR = 1 << SI;
I2C0CONSET = 1 << STA;
return 0;
}

//////////////////////////////////////
// Delay function
//
//
void delay_ms(int count)
{
int j=0,i=0;

for(j=0;j
{
/* At 60Mhz, the below loop introduces
delay of 10 us */
for(i=0;i<35;i++);
}
}
///////////////////////////////////////////////////////
// Function to send data from Master to slave I2C
// device
//

// Function to send a byte of char data


// EX:if data = 0x12 higher nibble
// is sent first in this case 0x1 and then lower
// nibble.whyy? Because slave is set to 4bit mode.
// The Format shud be :7 6 5 4 3 2 1 0
// 0 E RW RS|D A T A
//

void senddata(char data){


while(!(I2C0CONSET & 0x08));
I2C0DAT = data;
I2C0CONCLR = 1 << SI;
delay_ms(200);
}
void LCD_Command (char cmnd) /* LCD16x2 command funtion */
{
char data;
data = (cmnd & 0xF0)|0x04|0x08;/* Send upper nibble */ // 0X08 NEED TO
CHK
//rs=0; rw=0; en=1; 0100 = 0x04 LSB
senddata(data);
delay_ms(100);
//en=0;
data = (cmnd & 0xF0)|0x08;
senddata(data);
delay_ms(100);

data = (cmnd << 4)|0x04|0x08;/* Send lower nibble */


senddata(data);
delay_ms(100);
//en=0;
data = (cmnd << 4)|0x08;
senddata(data);
delay_ms(100);
}
void LCD_Char (char char_data) /* LCD data write function */
{
char data;
//rs=1; rw=0; en=1;
data =(char_data & 0xF0)|0x08|0x05;/* Send upper nibble */
senddata(data);
delay_ms(1);
//rs=1; rw =0 ; en=0;
data =(char_data & 0xF0)|0x08|0x01;
senddata(data);
delay_ms(2);

////rs=1; rw =0 ; en=1;
data = (char_data << 4)|0x08|0x05;/* Send lower nibble */
senddata(data);
delay_ms(1);
////rs=1; rw =0 ; en=0;
data = (char_data << 4)|0x08|0x01;
senddata(data);
delay_ms(5);
senddata(0x08);
}
void LCD_String (char *str) /* Send string to LCD function */
{
int i;
for(i=0;str[i]!=0;i++) /* Send each char of string till the NULL */
{
LCD_Char (str[i]); /* Call LCD data write */
}
}

//void LCD_String_xy (char row, char pos, char *str) /* Send string to LCD
function */
//{
//if (row == 0)
//LCD_Command((pos & 0x0F)|0x80);
//else if (row == 1)
//LCD_Command((pos & 0x0F)|0xC0);
// LCD_String(str); /* Call LCD string function */
//}
int main(){

char code = SLAVE_ADDR;// 0x6e;

char str[] = "ROSU";


int i =0;
I2C_Init(); //Initialize I2C moodule
I2C_Start(); //start I2C module
wait(4000);

while(!(I2C0CONSET & 0x08)); //wait till SI bit set


//IO0SET = (1<<21);
I2C0CONCLR = 1 << STO;
I2C0CONCLR = 1 << STA;
I2C0CONSET = 1 << AA;
I2C0DAT = code;

I2C0CONCLR = 1 << SI;


//wait(4000);
while(!(I2C0CONSET & 0x08));
//IO0SET = 0x00010000;

if(I2C0STAT == 0x18) //SLA+W has been


transmitted; ACK has been received
{
// IO0SET = (1<<23);
I2C0CONSET = 1 << AA;
I2C0DAT = 0x00;//x00;//Buff[index];
I2C0CONCLR = 1 << SI;
// IO0SET = 0x00020000;
while(!(I2C0CONSET & 0x08));
for(i=0; i < 2000;i++)wait(800);
if(I2C0STAT == 0x28) //Data byte in I2DAT has been
transmitted; ACK has been received
{
// IO0SET = 0x00030000;

LCD_Command(0x02);
LCD_Command (0x28); /* Initialization of
16X2 LCD in 4bit mode */
LCD_Command (0x0C); /* Display ON Cursor OFF
*/
LCD_Command (0x06); /* Auto Increment cursor
*/
LCD_Command (0x01); /* Clear display */
LCD_Command (0x80); /* Cursor at home
position */

LCD_String(str);
//LCD_String_xy(1,0,"HELLO WORLD");

// while(1){
// IO0CLR = 0x000F0000;
// for(i=0; i < 2000;i++)wait(800);
// IO0SET = 0x000F0000;
// for(i=0; i < 2000;i++)wait(800);
// } ;

}
}
}//END OF MAIN

====== 4x4 Keypad Interfacing with LPC2148 (Version 1) ======


(File: keypad.c)

#include
void delay(unsigned int);
void disp(unsigned int);

//Main function
int main()
{
unsigned long int value,i;
unsigned int row0[4]={ 0x00ee0000,0x00ed0000,0x00eb0000,0x00e70000};
unsigned int row1[4]={ 0x00de0000,0x00dd0000,0x00db0000,0x00d70000};
unsigned int row2[4]={ 0x00be000,0x00bd0000,0x00bb0000,0x00b70000};
unsigned int row3[4]={ 0x007e0000,0x007d0000,0x007b0000,0x00770000};
IO1DIR = 0XFFF0FFFF; //set rows as output and colomn as input
PINSEL1=0x00000000;
IODIR0=0xf0ff0000; // making po.16 to p0.23 and p0.28 to p0.31 output lines for
disp
IOSET0=0XF0000000;
while(1)
{
IO1PIN=0x00ff0000; //initialize rows and colomns with one
IOCLR1=0x00100000; //enable row0
value=IOPIN1;
delay(50000);
value=value & 0x00ff0000;
for(i=0; i<4;i++)
{
if(value==row0[i])
{
disp(i);
delay(65000);
delay(65000);
//delay(65000);delay(65000);delay(65000);
}
}
IO1PIN=0x00ff0000; //initialize rows and colomns with one
IOCLR1=0x00200000; //enable row1
value=IOPIN1;
delay(50000);delay(50000);
value=value & 0x00ff0000;
for(i=0; i<4;i++)
{
if(value==row1[i])
{
disp(i+4);
delay(65000);delay(65000);//delay(65000);
//delay(65000);delay(65000);
}
}
IO1PIN=0x00ff0000; //initialize rows and colomns with one
IOCLR1=0x00400000; //enable row2
value=IOPIN1;
delay(65000);delay(65000);delay(65000);
delay(65000);delay(65000);
value=value & 0x00ff0000;
for(i=0; i<4;i++)
{
if(value==row2[i])
{
disp(i+8);
delay(50000);

}
}
IO1PIN=0x00ff0000; //initialize rows and colomns with one
IOCLR1=0x00800000; //enable row3
value=IOPIN1;
delay(65000); delay(65000);//delay(65000);delay(65000);delay(65000);
value=value & 0x00ff0000;
for(i=0;i<4;i++)
{
if(value==row3[i])
{
disp(i+12);
delay(65000);delay(65000);//delay(65000);
//delay(65000);delay(65000);
}
}
}
}
//Display function
void disp(unsigned int temp)
{
unsigned int i;
unsigned int da[16]={0xf03F0000, 0xf0060000, 0x305B0000, 0x304F0000,
0x00660000,0x006D0000,
0x007D0000, 0x00070000, 0x007F0000, 0x006F0000,
0x00770000,0x007C0000,
0x00390000, 0x005E0000, 0x00790000, 0x00710000 };

IOCLR0=0x00ff0000; //clear disp


IOSET0=0x10000000;
i=temp;
IOSET0|=da[i];
delay(65000);delay(65000);delay(65000);
delay(65000);delay(65000);
//IOCLR0=0X00FF0000;
}

// Delay function
void delay(unsigned int del)
{ unsigned int k;
for(k=0;k
}

====== ADC Interfacing Using LPC2148 (Hex Display) ======


(File: ADC-2148.txt)

//working 10-bit internal ADC


//AIN0 pin is selected
//you can change the channel by changing PINSEL1 and ADCR value

#include
#include

unsigned int i;
void lcd_init(void);
void delay(unsigned long int);
void cmd(unsigned int );
void hascii(int *);
unsigned int adc_value=0,temp_adc=0;
void data(unsigned int );
int temp;
int var[10];
char val[10];
int main()
{ PINSEL1 = 0X00040000; //AD0.4 pin is selected(P0.18)
IO0DIR = 0x000000FC; //configure o/p lines for lcd
lcd_init();
while(1)
{
//CONTROL register for ADC
AD0CR = 0x09200010; //command register
for ADC-AD0.4
delay(100);
while((temp_adc = AD0GDR) == 0x80000000); //to check the interrupt bit
adc_value = AD0GDR; //reading the ADC value
adc_value >>=6;
adc_value &= 0x000003ff;
temp=adc_value & 0x00000f00;
var[0]= temp>>8 ;
temp=adc_value & 0x000000f0;
var[1]= temp>>4;
temp=adc_value & 0x0000000f;
var[2]=temp;

hascii(var);

for(i=0;i<3;i++)
{
delay(32000);

data(val[i]);
delay(32000); delay(32000); delay(32000);
}
cmd(0x01); delay(120000); delay(120000);
}
}
void lcd_init(void)

{
unsigned int i;
unsigned int c[]={0x30,0x20,0x28,0x01,0x06,0x0E,0x80};
IODIR0=0x000000fc;
IOCLR0=0x000000fc;
for(i=0;i<7;i++)

{
cmd(c[i]);
delay(10000);
}
}
void cmd(unsigned int value)
{
unsigned int y;
y=value;
y=y & 0xf0;
IOCLR0=0x000000fc;
IOCLR0=0X00000004;
IOSET0=y;

IOSET0=0x00000008;
delay(10);
IOCLR0=0x00000008;
y=value;
y=y & 0x0f;
y=y<<4;
IOCLR0=0x000000fc;
IOCLR0=0X00000004;
IOSET0=y;

IOSET0=0x00000008;
delay(10);
IOCLR0=0x00000008;

}
void data(unsigned int dat)
{
unsigned int y;
y=dat;
y=y & 0xf0;
IOCLR0=0x000000fc;
IOSET0=0X00000004;
IOSET0=y;

IOSET0=0x00000008;
delay(10);
IOCLR0=0x00000008;

y=dat;
y=y & 0x0f;
y=y<<4;
IOCLR0=0x000000fc;
IOSET0=0X00000004;
IOSET0=y;

IOSET0=0x00000008;
delay(10);
IOCLR0=0x00000008;
}

void delay(unsigned long int r1)


{
unsigned long int r;
for(r=0;r
}
void hascii(int var[])
{
char asc[20]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};

for (i=0;i<3;i++)
val[i]=asc[var[i]];
}

====== 4x4 Keypad Interfacing with LPC2148 (Version 2) ======


(File: keypad (1).c)

#include
void delay(unsigned int);
void disp(unsigned int);
int main()
{

unsigned long int value,i;


unsigned int row0[4]={ 0x00ee0000,0x00ed0000,0x00eb0000,0x00e70000};

unsigned int row1[4]={ 0x00de0000,0x00dd0000,0x00db0000,0x00d70000};

unsigned int row2[4]={ 0x00db0000,0x00bd0000,0x00bb0000,0x00b70000};

unsigned int row3[4]={ 0x007e0000,0x007d0000,0x007b0000,0x00770000};

IO1DIR = 0XFFF0FFFF; //set rows as output and colomn as input

PINSEL1=0x00000000;
IODIR0=0xf0ff0000; // making po.16 to p0.23 and p0.28 to p0.31 output lines for
disp
IOSET0=0XF0000000;
while(1)
{
IO1PIN=0x00ff0000; //initialize rows and colomns with one
IOCLR1=0x00100000; //enable row0
value=IOPIN1;
delay(50000);

value=value & 0x00ff0000;


for(i=0; i<4;i++)
{
if(value==row0[i])
{
disp(i);
delay(65000); delay(65000);delay(65000);delay(65000);delay(65000);

}
}
IO1PIN=0x00ff0000; //initialize rows and colomns with one
IOCLR1=0x00200000; //enable row0
value=IOPIN1;
delay(50000); delay(50000);

value=value & 0x00ff0000;


for(i=0; i<4;i++)
{
if(value==row1[i])
{
disp(i+4);
delay(65000); delay(65000);delay(65000);delay(65000);delay(65000);
}
}

IO1PIN=0x00ff0000; //initialize rows and colomns with one


IOCLR1=0x00400000; //enable row0
value=IOPIN1;
delay(65000); delay(65000);delay(65000);delay(65000);delay(65000);
value=value & 0x00ff0000;
for(i=0; i<4;i++)
{
if(value==row2[i])
{
disp(i+8);
delay(50000);

}
}

IO1PIN=0x00ff0000; //initialize rows and colomns with one


IOCLR1=0x00800000; //enable row0
value=IOPIN1;
delay(65000); delay(65000);delay(65000);delay(65000);delay(65000);
value=value & 0x00ff0000;
for(i=0;i<4;i++)
{
if(value==row3[i])
{
disp(i+12);
delay(65000); delay(65000);delay(65000);delay(65000);delay(65000);
}
}
}

}
void disp(unsigned int temp)
{
unsigned int i;
unsigned int da[16]={0x003F0000, 0x00060000, 0x005B0000, 0x004F0000,
0x00660000,0x006D0000,
0x007D0000, 0x00070000, 0x007F0000, 0x006F0000,
0x00770000,0x007C0000,
0x00390000, 0x005E0000, 0x00790000, 0x00710000 };

IOCLR0=0x00ff0000; //clear disp


i=temp;

IOSET0=da[i];
delay(65000); delay(65000);delay(65000);delay(65000);delay(65000);
IOCLR0=0X00FF0000;
}

void delay(unsigned int del)


{ unsigned int k;
for(k=0;k
}

====== ADC Interfacing Using LPC2148 (Decimal Display) ======


(File: adc_sup_new.c)

//working 10-bit internal ADC


//AIN0 pin is selected
//you can change the channel by changing PINSEL1 and ADCR value

#include
#include

unsigned int i;
void lcd_init(void);
void delay(unsigned long int);
void cmd(unsigned int );
//void hascii(int *);
unsigned int adc_value=0,temp_adc=0;
void data(unsigned int );
int temp;
//int var[10];
char arr[20];
int main()
{ PINSEL1 = 0X00040000; //AD0.4 pin is selected(P0.25)
IO0DIR = 0x000000FC; //configure o/p lines for lcd
lcd_init();
while(1)
{
//CONTROL register for ADC
AD0CR = 0x09200010; //command register
for ADC-AD0.4
delay(100);
while((temp_adc = AD0GDR) == 0x80000000); //to check the interrupt bit
adc_value = AD0GDR; //reading the ADC value
adc_value >>=6;
adc_value &= 0x000003ff;
sprintf(arr,"adc value=%x",adc_value);

for(i=0;i<13;i++)
//while(arr[i]!=)
{
delay(32000);

data(arr[i]);
delay(65000); delay(65000); delay(65000);
// i++;
}

delay(65000); delay(65000); delay(65000); delay(65000); delay(65000); delay(65000);

cmd(0x01); delay(120000); delay(120000);


}
}
void lcd_init(void)

{
unsigned int i;
unsigned int c[]={0x30,0x20,0x28,0x01,0x06,0x0E,0x80};
IODIR0=0x000000fc;
IOCLR0=0x000000fc;
for(i=0;i<7;i++)

{
cmd(c[i]);
delay(10000);
}
}
void cmd(unsigned int value)
{
unsigned int y;
y=value;
y=y & 0xf0;
IOCLR0=0x000000fc;
IOCLR0=0X00000004;
IOSET0=y;

IOSET0=0x00000008;
delay(10);
IOCLR0=0x00000008;

y=value;
y=y & 0x0f;
y=y<<4;
IOCLR0=0x000000fc;
IOCLR0=0X00000004;
IOSET0=y;

IOSET0=0x00000008;
delay(10);
IOCLR0=0x00000008;

}
void data(unsigned int dat)
{
unsigned int y;
y=dat;
y=y & 0xf0;
IOCLR0=0x000000fc;
IOSET0=0X00000004;
IOSET0=y;

IOSET0=0x00000008;
delay(10);
IOCLR0=0x00000008;

y=dat;
y=y & 0x0f;
y=y<<4;
IOCLR0=0x000000fc;
IOSET0=0X00000004;
IOSET0=y;

IOSET0=0x00000008;
delay(10);
IOCLR0=0x00000008;
}

void delay(unsigned long int r1)


{
unsigned long int r;
for(r=0;r
}

====== Stepper Motor Control with LPC2148 ======


(File: STPM.c)

// STEPPER MOTOR INTERFACING


//--------------------------------------------------------------
// CONTROLLER : LPC-2148
// DATE : December - 2015
// Developed By : Advanced Electronic Systems Bangalore,India
//----------------------------------------------------------------
//-------------------------------------------------------------------
// A stepper motor direction is controlled by shifting the voltage across
// the coils. Port lines : P0.28 to P0.31
//-------------------------------------------------------------------

#include

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)
{
PINSEL1 = 0x00FFFFFF ; //P0.28 to P0.31 GPIO
IO0DIR |= 0xF0000000 ; //P0.28 to P0.31 made as output

while(1)
{
for( j = 0 ; j < 50 ; j++ ) // 20 times in Clock wise Rotation
clock_wise() ;

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


Rotation

for( j=0 ; j < 50 ; j++ ) // 20 times in Anti Clock wise


Rotation
anti_clock_wise() ;

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

}// End of main

void clock_wise(void)
{
var1 = 0x08000000; //For Clockwise
for( i = 0 ; i <= 3 ; i++ ) // for A B C D Stepping
{
var1 <<= 1 ; //For Clockwise

IO0CLR =0xF0000000 ; //clearing all 4 bits

IO0SET = var1 ; // setting perticular bit

for( k = 0 ; k < 3000 ; k++ ); //for step speed variation


}

void anti_clock_wise(void)
{
var1 = 0x80000000 ; //For Anticlockwise
IO0CLR =0xF0000000 ; //clearing all 4 bits

IO0SET = var1 ; // setting perticular


bit

for( k = 0 ; k < 3000 ; k++ ) ; //for step speed variation

for( i = 0 ; i < 3 ; i++ ) // for A B C D Stepping


{
var1 >>= 1 ; //For Anticlockwise

IO0CLR =0xF0000000 ;

IO0SET = var1 ;

for( k = 0 ; k < 3000 ; k++ ) ; //for step speed variation


}
}

====== Basic LCD Display Interfacing with LPC2148 ======


(File: lcd.c)

#include
void cmd(unsigned int);
void data(unsigned int);
void delay(unsigned int);
int main()
{
unsigned char msg[]={"welcome KLETECH"};
unsigned int c[]={0x30,0x30,0x20,0x20,0x28,0x01,0x06,0x0e,0x80};
unsigned char i,j;
PINSEL0=0x00000000;
IODIR0=0x000000fc;
IOCLR0=0x000000fc;
for(i=0;i<9;i++)
{
cmd(c[i]);
delay(10000);
}
while(1)
{
cmd(0x80);
delay(10000);
for(j=0;j<15;j++)
{
data(msg[j]);
delay(500);
}
delay(65000);
delay(65000);
delay(65000);
cmd(0x01);
delay(65000);
delay(65000);
delay(65000);
delay(65000);
}
}
void cmd(unsigned int value)
{
unsigned int y;
y=value;
y=y & 0xf0;
IOCLR0=0x000000fc;
IOCLR0=0X00000004; //rs=0
IOSET0=y;
IOSET0=0x00000008; //en=1
delay(10);
IOCLR0=0x00000008; //en=0
y=value;
y=y & 0x0f;
y=y<<4;
IOCLR0=0x000000fc;
IOCLR0=0X00000004; //rs=0
IOSET0=y;
IOSET0=0x00000008;
delay(10);
IOCLR0=0x00000008;
}

void data(unsigned int dat)


{
unsigned int y;
y=dat;
y=y & 0xf0;
IOCLR0=0x000000fc;
IOSET0=0X00000004; //rs=1
IOSET0=y;
IOSET0=0x00000008; //en=1
delay(10);
IOCLR0=0x00000008; //en=0
y=dat;
y=y & 0x0f;
y=y<<4;
IOCLR0=0x000000fc;
IOSET0=0X00000004;
IOSET0=y;
IOSET0=0x00000008;
delay(10);
IOCLR0=0x00000008;
}

void delay(unsigned int x)


{
unsigned int del;
for(del=0;del
}

====== External Interrupt (EINT0) Handling on LPC2148 ======


(File: extint_sup.c)

#include
#include
void delay(unsigned int);
void extint0_ISR(void)__irq;
void delay(unsigned int );
int main(void)
{
PINSEL1=0x00000001;
EXTMODE=0x00000001;
VICVectAddr0=(unsigned long)extint0_ISR;
VICVectCntl0=0x20|14;
VICIntEnable|=0x00004000;
while(1);
}
void extint0_ISR(void)__irq
{
PINSEL0=0x00000000;
IODIR0=0x000f0000;
IOCLR0=0X00040000; // ON LEDS
delay(100000);
IOSET0=0X00040000; // OFF LEDS
delay(100000);
EXTINT|=0X00000001; //Clear interrupt flag
VICVectAddr=0; // End of interrupt execution
}
void delay(unsigned int x)
{
unsigned int i;
for(i=0;i
}

====== Additional ARM Programs ======

1 // code to blink 4 leds with some delay conneced on p0.16-p0.19


2 #include
3 int main()
4 {
5 PINSEL0 = 0X00000000; // port as GPIO
6 IODIR1 = 0X00FF0000; // set as output port
7 while (1)
8 {
9 IOCLR1 = 0X000F0000; // clear to turn on leds as common anode connection
10 delay (); // wait
11 IOSET1 = 0X000F0000;// set to turn off leds as common anode connection
12 delay ();
13 }
14 }
15 delay ()
16 {
17 int I;
18 for (I=0; I<100000; I++);
19 for (I=0; I<100000; I++);
20 }
21
22
23 // SWITCH CONTROLLED LED
24 #include
25 void delay(void);
26 unsigned char i;
27 int main()
28 {
29 PINSEL1 = 0X00000000;
30 IODIR0 = 0X000F0000;
31 IOSET0 = 0X000F0000;
32 while (1)
33 {
34 if ((IOPIN1&0X00100000)==0X00100000)
35 {
36 IOCLR0 = 0X000F0000;
37 delay ();
38 IOSET0 = 0X000F0000;
39 delay ();
40 }
41 else
42 {
43 IOSET0 = 0X000F0000;
44 }
45 }
46 }
47 void delay (void)
48 {
49 unsigned long int j;
50 for (j=0; j<500000; j++);
51 }
52
53
54 // seven segment code depending switch status if off count from 0-5 else count
from 5-0
55
56 #include
57 void delay(void);
58 unsigned char i;
59 unsigned int Disp[16]={0x003F0000, 0x00060000, 0x005B0000, 0x004F0000,
0x00660000,
0x006D0000,
60 0x007D0000, 0x00070000, 0x007F0000, 0x006F0000, 0x00770000,
0x007C0000,
61 0x00390000, 0x005E0000, 0x00790000, 0x00710000 };
62 int main()
63 {
64 PINSEL2=0x00000000;
65 IODIR0=0xf0ff0000; // making po.16 to p0.23 and p0.28 to p0.31 output lines
66 IOSET0=0xf0000000; // enabling both segemnts
67 IODIR1=0x00000000; // port 1 as input port
68 IOCLR0=0x00ff0000; // clearing the seven segments initially
69 while(1)
70 {
71 if((IOPIN1&0x00100000)==0) // if switch is in off position
72 {
73 for(i=0;i<5;i++)
74 {
75 IOSET0=Disp[i];
76 delay();delay();delay();
77 delay();delay();delay();delay();
78 IOCLR0=0x00ff0000;
79 }
80 }
81 else
82 {
83 for(i=4;i>0;i--)
84 {
85 IOSET0=Disp[i];
86 delay();delay();delay();delay();
87 delay();delay();delay();
88 IOCLR0=0x00ff0000;
89 }
90 }
91 }
92 }
93 void delay(void)
94 { unsigned long int j;
95 for(j=0;j<650000;j++);
96 }
97 // keypad displaying on seven segment
98
99 #include
100 void delay(unsigned int);
101 void disp(unsigned int);
102 int main()
103 {
104 unsigned long int value,i;
105 unsigned int row0[4]={ 0x00ee0000,0x00ed0000,0x00eb0000,0x00e70000};
106 unsigned int row1[4]={ 0x00de0000,0x00dd0000,0x00db0000,0x00d70000};
107 unsigned int row2[4]={ 0x00db0000,0x00bd0000,0x00bb0000,0x00b70000};
108 unsigned int row3[4]={ 0x007e0000,0x007d0000,0x007b0000,0x00770000};
109 IO1DIR = 0XFFF0FFFF; //set rows as output and colomn as input
110 PINSEL1=0x00000000;
111 IODIR0=0xf0ff0000; // making po.16 to p0.23 and p0.28 to p0.31 output lines for
disp
112 IOSET0=0XF0000000;
113 while(1)
114 {
115 IO1PIN=0x00ff0000; //initialize rows and colomns with one
116 IOCLR1=0x00100000; //enable row0
117 value=IOPIN1; // read keypad status
118 delay(50000);
119 value=value & 0x00ff0000;
120 for(i=0; i<4;i++)
121 {
122 if(value==row0[i])
123 {
124 disp(i);
125 delay(65000); delay(65000);delay(65000);delay(65000);delay(65000);
126 }
127 }
128 IO1PIN=0x00ff0000; //initialize rows and colomns with one
129 IOCLR1=0x00200000; //enable row1
130 value=IOPIN1; // read keypad status
131 delay(50000); delay(50000);
132 value=value & 0x00ff0000;
133 for(i=0; i<4;i++)
134 {
135 if(value==row1[i])
136 {
137 disp(i+4);
138 delay(65000); delay(65000);delay(65000);delay(65000);delay(65000);
139 }
140 }
141 IO1PIN=0x00ff0000; //initialize rows and colomns with one
142 IOCLR1=0x00400000; //enable row2
143 value=IOPIN1; // read keypad status
144 delay(65000); delay(65000);delay(65000);
145 value=value & 0x00ff0000;
146 for(i=0; i<4;i++)
147 {
148 if(value==row2[i])
149 {
150 disp(i+8);
151 delay(50000);
152 }
153 }
154 IO1PIN=0x00ff0000; //initialize rows and colomns with one
155 IOCLR1=0x00800000; //enable row3
156 value=IOPIN1;
157 delay(65000); delay(65000);delay(65000);delay(65000);delay(65000);
158 value=value & 0x00ff0000;
159 for(i=0;i<4;i++)
160 {
161 if(value==row3[i])
162 {
163 disp(i+12);
164 delay(65000); delay(65000);delay(65000);delay(65000);delay(65000);
165 }
166 }
167 }
168
169 }
170 void disp(unsigned int temp)
171 {
172 unsigned int i;
173 unsigned int da[16]={0x003F0000, 0x00060000, 0x005B0000, 0x004F0000,
0x00660000,
0x006D0000,
174 0x007D0000, 0x00070000, 0x007F0000, 0x006F0000, 0x00770000,
0x007C0000,
175 0x00390000, 0x005E0000, 0x00790000, 0x00710000 };
176 IOCLR0=0x00ff0000; //clear disp
177 i=temp; // key read data
178 IOSET0=da[i]; // send data to seven segment
179 delay(65000); delay(65000);delay(65000);delay(65000);delay(65000);
180 IOCLR0=0X00FF0000; // clear segments after some delay
181 }
182 void delay(unsigned int del)
183 { unsigned int k;
184 for(k=0;k
185 }
186
187 // code to run dc motor in clockwise and anticlockwise direction connected to
p0.8 and 11
188
189 #include
190 void clock_wise(void);
191 void anticlock_wise(void);
192 unsigned int j=0;
193 int main()
194 {
195 IO0DIR = 0X00000900;
196 IO0SET = 0X00000100;
197 while (1)
198 {
199 clock_wise ();
200 for (j=0; j<400000; j++);
201 anticlock_wise ();
202 for (j=0; j<400000; j++);
203 }
204 }
205 void clock_wise(void)
206 {
207 IO0CLR = 0X00000900;
208 for (j=0; j<10000; j++);
209 IO0SET = 0X00000900;
210 }
211 void anticlock_wise(void)
212 {
213 IO0CLR = 0X00000900;
214 for (j=0; j<10000; j++);
215 IO0SET = 0X00000100;
216 }
217
218 // stepper motor code to turn clockwise and anticlockwise.connected on P0.12-
P0.15
219 #include
220 void clock_wise(void) ;
221 void anti_clock_wise(void) ;
222 unsigned long int var1 ;
223 unsigned int i = 0 , j = 0 , k = 0 ;
224 int main(void)
225 {
226 PINSEL1 = 0x00FFFFFF ; //P0.28 to P0.31 GPIO
227 IO0DIR |= 0x0000F000 ; //P0.28 to P0.31 made as output
228 while(1)
229 {
230 for( j = 0 ; j < 1000 ; j++ ) // 20 times in Clock wise Rotation
231 clock_wise() ;
232 for( k = 0 ; k < 65000 ; k++ ) ; // Delay to show anti_clock Rotation
233 for( j=0 ; j < 1000 ; j++ ) // 20 times in Anti Clock wise Rotation
234 anti_clock_wise() ;
235 for( k = 0 ; k < 65000 ; k++ ) ; // Delay to show clock Rotation
236 }
237 }// End of main
238 void clock_wise(void)
239 {
240 var1 = 0x00000800; //For Clockwise
241 for( i = 0 ; i <= 3 ; i++ ) // for A B C D Stepping
242 {
243 var1 <<= 1 ; //For Clockwise 1,2,4,8,
244 IO0CLR =0x0000F000 ; //clearing all 4 bits
245 IO0SET = var1 ; // setting perticular bit
246 for( k = 0 ; k < 3000 ; k++ ); //for step speed variation
247 }
248 }
249 void anti_clock_wise(void)
250 {
251 var1 = 0x00008000 ; //For Anticlockwise
252 IO0CLR =0x0000F000 ; //clearing all 4 bits
253 IO0SET = var1 ; // setting perticular bit
254 for( k = 0 ; k < 300 ; k++ ) ; //for step speed variation
255 for( i = 0 ; i < 3 ; i++ ) // for A B C D Stepping 8,4,2,1
256 {
257 var1 >>= 1 ; //For Anticlockwise
258 IO0CLR =0x0000F000 ;
259 IO0SET = var1 ;
260 for( k = 0 ; k < 300 ; k++ ) ; //for step speed variation
261 }
262 }
263
264 // example program to show TIMER0
265 //basetimevalue=1ms
266 // count(PR)=(pclk*basetimevalue)-1
267 // =(12MHz *0.001 sec)-1
268 // = 11999
269 //DESIRED_COUNT = requiredtime *basetimevalue = 1000* 0.001= 1sec
270
271 #include
272 #define DESIRED_COUNT 1000 // for 1sec
273 #define PRESCALER 11999
274 void InitTimer0(void);
275 void InitTimer0(void)
276 {
277 T0PR=PRESCALER;
278 T0MR0=DESIRED_COUNT; //interrupt every 1 sec for interval = 1000
279 T0MCR=3; //interrupt and reset when counter=match
280 T0TCR=2; // reset timer
281 }
282 int main (void)
283 {
284 PINSEL0 = 0X00000000 ; // Configure P0.16-p0.23 as GPIO
285 IODIR0=0X000F0000;
286 InitTimer0(); // initialise timer0 -
287 T0TCR = 0x01; // start timer
288 while(1)
289 {
290 while(!( T0IR==0x01)); // wait for overflow
291 T0IR=0x01; // clear flag
292 IOCLR0=0X000F0000; // turn on led
293 while(!( T0IR==0x01));
294 IOSET0=0X000F0000; // turn off led
295 T0IR=0x01; // turn off flag
296 }
297 }
298
299 // Code to send the "BVB" CHARATECTER TO UART , THEN Accept a single character
from user
and send the received character back to uart
300 #include
301 void delay(void);
302 void serial(void);
303 unsigned char mg;
304 int main()
305 {
306 unsigned int i;
307 unsigned char msg[]={"BVB"};
308 serial();
309 while(1)
310 {
311 for(i=0;i<3;i++) // loop to send "BVB" TO UART
312 {
313 while(!(U0LSR & 0x20)); // wait till uothr is empty
314 U0THR = msg[i]; // send one char at a time
315 }
316
317 while(!(U0LSR & 0x01)); // wait until a valid character in U0RBR
318 mg=U0RBR; // Read the received char in variable mg
319 U0THR=mg; // send it to uart
320 }
321 }
322 void serial()
323 {
324 PINSEL0 = 0x00000005; // txd0 and rxd0 as special pins on p0.0 and p0.1
325 U0LCR = 0x83; // select 8 bit, and dlab=1
326 U0DLL = 0x61; // set baudrate of 9600
327 U0LCR = 0x03;
328 }
329 // code to generate the square waveform using dac
330 #include
331 void delay( long int x)
332 {
333 unsigned int i;
334 for(i=0;i
335 }
336 int main (void)
337 {
338 int value;
339 PINSEL1 = 0x00080000; /* P0.25 as DAC output */
340 while(1)
341 {
342 value = 1023;
343 DACR = ( (1<<16) | (value<<6) ); // send high
344 delay(100000);
345 value = 0;
346 DACR = ( (1<<16) | (value<<6) ); // send low
347 delay(100000);
348 }
349 }
350
351 //working 10-bit internal ADC results printed on uart
352 //AIN0 pin is selected AD0.4 CHANNEL
353 //you can change the channel by changing PINSEL1 and ADCR value
354 #include
355 #include
356 unsigned int i;
357 void delay(unsigned long int r1)
358 {
359 unsigned long int r;
360 for(r=0;r
361 }
362 unsigned int adc_value=0,temp_adc=0;
363 char val[30];
364 void serial()
365 {
366 PINSEL0=0X0000005; //select TXD0 and RXD0 lines
367 U0LCR = 0X00000083; //enable baud rate divisor loading and //select the data
format
368 U0DLM = 0X00;
369 U0DLL = 0x61; //select baud rate 9600 bps
370 U0LCR = 0X00000003;
371 }
372 int main()
373 { PINSEL1 = 0X00040000; //AD0.4 pin is selected(P0.25)
374 serial();
375 while(1)
376 {
377 AD0CR = 0x09200010; //command register for ADC-AD0.4
378 delay(100);
379 while(!(AD0GDR) == 0x80000000); //to check the interrupt bit
380 adc_value = AD0GDR; //reading the ADC value
381 adc_value >>=6; // shift right to get it in lsb position
382 adc_value &= 0x000003ff;
383 sprintf(val,"The digital value is %x",adc_value); // to convert hex to string
384 while (val[i] != '\0')
385 {
386 while (!(U0LSR & 0x20));
387 U0THR = val[i];
388 i++;
389 }
390 delay(20000);
391 U0THR='\t'; // inserting tab character to send next sring
392 i=0;
393 }
394 }
395
396 // code to demo external interrupt 0 to blink led when an interrupt is
generated
397 #include
398 #include
399 void delay(unsigned int);
400 void extint0_ISR(void)__irq;
401 void delay(unsigned int );
402 int main(void)
403 {
404 PINSEL1=0x00000001;
405 EXTMODE=0x00000001;
406 VICVectAddr0=(unsigned long)extint0_ISR; // loads address of isr
407 VICVectCntl0=0x20|14; // enable irq slot for eint0
408 VICIntEnable|=0x00004000; // enable eint0
409 while(1);
410 }
411 void extint0_ISR(void)__irq
412 {
413 IODIR0=0x000f0000;
414 IOCLR0=0X000f0000; // ON LEDS
415 delay(100000);
416 IOSET0=0X000f0000; // OFF LEDS
417 delay(100000);
418 EXTINT|=0X00000001; //Clear interrupt flag
419 VICVectAddr=0; // End of interrupt execution
420 }
421 void delay(unsigned int x)
422 {
423 unsigned int i;
424 for(i=0;i
425 }
426
427
428
429
430

You might also like