Saturday, December 16, 2017
11. Measure Ambient temperature using a sensor and SPI ADC IC
PartB
Theory:
Explanation:
10. Interface a simple Switch
PartB
Theory:
Explanation:
9. Seven-segment LED interface
PartB
delay in between.
Table: hexadecimal equivalent values for each digit to be displayed
fig:interfacing diagram of LPC1768 to seven segment dislay
Explanation:
code:
#include <LPC17xx.h>
unsigned int delay, count=0, Switchcount=0,j;
unsigned int Disp[16]={0x000003f0, 0x00000060,
0x000005b0, 0x000004f0,
0x00000660,0x000006d0,
0x000007d0, 0x00000070,
0x000007f0, 0x000006f0,
0x00000770,0x000007c0,
0x00000390, 0x000005e0,
0x00000790, 0x00000710 };
#define ALLDISP 0x00180000 //Select all display
#define DATAPORT 0x00000ff0 //P0.4 to P0.11 : Data lines connected to drive Seven Segments
int main (void)
{
LPC_PINCON->PINSEL0 = 0x00000000;
LPC_PINCON->PINSEL1 = 0x00000000;
LPC_GPIO0->FIODIR = 0x00180ff0;
while(1)
{
LPC_GPIO0->FIOSET |= ALLDISP;
LPC_GPIO0->FIOCLR = 0x00000ff0; // clear the data lines to 7-segment displays
LPC_GPIO0->FIOSET = Disp[Switchcount]; // get the 7-segment display value from the array
for(j=0;j<3;j++)
for(delay=0;delay<30000;delay++); // 1s delay
Switchcount++;
if(Switchcount == 0x10) // 0 to F has been displayed ? go back to 0
{
Switchcount = 0;
LPC_GPIO0->FIOCLR = 0x00180ff0;
}
}
}
#include <LPC17xx.h>
unsigned int delay, count=0, Switchcount=0,j;
unsigned int Disp[16]={0x000003f0, 0x00000060,
0x000005b0, 0x000004f0,
0x00000660,0x000006d0,
0x000007d0, 0x00000070,
0x000007f0, 0x000006f0,
0x00000770,0x000007c0,
0x00000390, 0x000005e0,
0x00000790, 0x00000710 };
#define ALLDISP 0x00180000 //Select all display
#define DATAPORT 0x00000ff0 //P0.4 to P0.11 : Data lines connected to drive Seven Segments
int main (void)
{
LPC_PINCON->PINSEL0 = 0x00000000;
LPC_PINCON->PINSEL1 = 0x00000000;
LPC_GPIO0->FIODIR = 0x00180ff0;
while(1)
{
LPC_GPIO0->FIOSET |= ALLDISP;
LPC_GPIO0->FIOCLR = 0x00000ff0; // clear the data lines to 7-segment displays
LPC_GPIO0->FIOSET = Disp[Switchcount]; // get the 7-segment display value from the array
for(j=0;j<3;j++)
for(delay=0;delay<30000;delay++); // 1s delay
Switchcount++;
if(Switchcount == 0x10) // 0 to F has been displayed ? go back to 0
{
Switchcount = 0;
LPC_GPIO0->FIOCLR = 0x00180ff0;
}
}
}
*********************************************************************************
implementation of same code using for loop
#include <LPC17xx.h>
unsigned int delay, count=0, Switchcount,j;
unsigned int Disp[16]={0x000003f0, 0x00000060,
0x000005b0, 0x000004f0,
0x00000660,0x000006d0,
0x000007d0, 0x00000070,
0x000007f0, 0x000006f0,
0x00000770,0x000007c0,
0x00000390, 0x000005e0,
0x00000790, 0x00000710 };
#define ALLDISP 0x00180000 //Select all display
#define DATAPORT 0x00000ff0 //P0.4 to P0.11 : Data lines connected to drive Seven Segments
int main (void)
{
LPC_PINCON->PINSEL0 = 0x00000000;
LPC_PINCON->PINSEL1 = 0x00000000;
LPC_GPIO0->FIODIR = 0x00180ff0;
while(1)
{
for(Switchcount=0;Switchcount<16;Switchcount++)
{
LPC_GPIO0->FIOSET |= ALLDISP;
LPC_GPIO0->FIOCLR = 0x00000ff0; // clear the data lines to 7-segment displays
LPC_GPIO0->FIOSET = Disp[Switchcount]; // get the 7-segment display value from the array
for(j=0;j<3;j++)
for(delay=0;delay<30000;delay++); // 1s delay
}
}
#include <LPC17xx.h>
unsigned int delay, count=0, Switchcount,j;
unsigned int Disp[16]={0x000003f0, 0x00000060,
0x000005b0, 0x000004f0,
0x00000660,0x000006d0,
0x000007d0, 0x00000070,
0x000007f0, 0x000006f0,
0x00000770,0x000007c0,
0x00000390, 0x000005e0,
0x00000790, 0x00000710 };
#define ALLDISP 0x00180000 //Select all display
#define DATAPORT 0x00000ff0 //P0.4 to P0.11 : Data lines connected to drive Seven Segments
int main (void)
{
LPC_PINCON->PINSEL0 = 0x00000000;
LPC_PINCON->PINSEL1 = 0x00000000;
LPC_GPIO0->FIODIR = 0x00180ff0;
while(1)
{
for(Switchcount=0;Switchcount<16;Switchcount++)
{
LPC_GPIO0->FIOSET |= ALLDISP;
LPC_GPIO0->FIOCLR = 0x00000ff0; // clear the data lines to 7-segment displays
LPC_GPIO0->FIOSET = Disp[Switchcount]; // get the 7-segment display value from the array
for(j=0;j<3;j++)
for(delay=0;delay<30000;delay++); // 1s delay
}
}
}
*********************************************************************************
Download:7 segment LED to display 0 to F*********************************************************************************
implementation of count code using for loop 0 to F and F to 0
#include <LPC17xx.h>
unsigned int delay, count=0, Switchcount,j;
unsigned int Disp[16]={0x000003f0, 0x00000060,
0x000005b0, 0x000004f0,
0x00000660,0x000006d0,
0x000007d0, 0x00000070,
0x000007f0, 0x000006f0,
0x00000770,0x000007c0,
0x00000390, 0x000005e0,
0x00000790, 0x00000710 };
#define ALLDISP 0x00180000 //Select all display
#define DATAPORT 0x00000ff0 //P0.4 to P0.11 : Data lines connected to drive Seven Segments
int main (void)
{
LPC_PINCON->PINSEL0 = 0x00000000;
LPC_PINCON->PINSEL1 = 0x00000000;
LPC_GPIO0->FIODIR = 0x00180ff0;
while(1)
{
for(Switchcount=0;Switchcount<16;Switchcount++)
{
LPC_GPIO0->FIOSET |= ALLDISP;
LPC_GPIO0->FIOCLR = 0x00000ff0; // clear the data lines to 7-segment displays
LPC_GPIO0->FIOSET = Disp[Switchcount]; // get the 7-segment display value from the array
for(j=0;j<3;j++)
for(delay=0;delay<30000;delay++); // 1s delay
}
for(Switchcount=15;Switchcount>=0;Switchcount--)
{
LPC_GPIO0->FIOSET |= ALLDISP;
LPC_GPIO0->FIOCLR = 0x00000ff0; // clear the data lines to 7-segment displays
LPC_GPIO0->FIOSET = Disp[Switchcount]; // get the 7-segment display value from the array
for(j=0;j<3;j++)
for(delay=0;delay<30000;delay++); // 1s delay
}
}
#include <LPC17xx.h>
unsigned int delay, count=0, Switchcount,j;
unsigned int Disp[16]={0x000003f0, 0x00000060,
0x000005b0, 0x000004f0,
0x00000660,0x000006d0,
0x000007d0, 0x00000070,
0x000007f0, 0x000006f0,
0x00000770,0x000007c0,
0x00000390, 0x000005e0,
0x00000790, 0x00000710 };
#define ALLDISP 0x00180000 //Select all display
#define DATAPORT 0x00000ff0 //P0.4 to P0.11 : Data lines connected to drive Seven Segments
int main (void)
{
LPC_PINCON->PINSEL0 = 0x00000000;
LPC_PINCON->PINSEL1 = 0x00000000;
LPC_GPIO0->FIODIR = 0x00180ff0;
while(1)
{
for(Switchcount=0;Switchcount<16;Switchcount++)
{
LPC_GPIO0->FIOSET |= ALLDISP;
LPC_GPIO0->FIOCLR = 0x00000ff0; // clear the data lines to 7-segment displays
LPC_GPIO0->FIOSET = Disp[Switchcount]; // get the 7-segment display value from the array
for(j=0;j<3;j++)
for(delay=0;delay<30000;delay++); // 1s delay
}
for(Switchcount=15;Switchcount>=0;Switchcount--)
{
LPC_GPIO0->FIOSET |= ALLDISP;
LPC_GPIO0->FIOCLR = 0x00000ff0; // clear the data lines to 7-segment displays
LPC_GPIO0->FIOSET = Disp[Switchcount]; // get the 7-segment display value from the array
for(j=0;j<3;j++)
for(delay=0;delay<30000;delay++); // 1s delay
}
}
*********************************************************************************
to control each display independently written by Adarsh N D 6th sem TCE, JNNCE
#include<LPC17xx.h>
unsigned int delay,count=0,Switchcount=0,j,i,k;
unsigned int disp[16]={0x000003f0,0x00000060,0x000005b0,0x000004f0,
0x00000660,0x000006d0,0x000007d0,0x00000070,
0x000007f0,0x000006f0,0x00000770,0x000007c0,
0x00000390,0x000005e0,0x00000790,0x00000710};
void dispone(void);
void disptwo(void);
#define ALLDISP 0x00180000
#define DATAPORT 0x00000ff0
int main(void)
{
LPC_PINCON ->PINSEL0=0x00000000;
LPC_PINCON ->PINSEL1=0x00000000;
while(1)
{
for (i=0;i<16;i++)
dispone(); //function call dispone
for(k=0;k<16;k++)
disptwo(); //function call disptwo
}
}
//function for display1
void dispone(void)
{
LPC_GPIO0->FIODIR=0x00080ff0;
LPC_GPIO0 ->FIOSET =ALLDISP;
LPC_GPIO0 ->FIOCLR =0x00000ff0;
LPC_GPIO0 ->FIOSET=disp[Switchcount];
for(j=0;j<3;j++)
for(delay=0;delay<2500000;delay++);
Switchcount++;
if(Switchcount==0x10)
{
Switchcount=0;
LPC_GPIO0 ->FIOCLR=0x00180ff0;
}
}
//function for display2
void disptwo(void)
{
LPC_GPIO0->FIODIR=0x00100ff0;
LPC_GPIO0 ->FIOSET =ALLDISP;
LPC_GPIO0 ->FIOCLR =0x00000ff0;
LPC_GPIO0 ->FIOSET=disp[Switchcount];
for(j=0;j<3;j++)
for(delay=0;delay<2500000;delay++);
Switchcount++;
if(Switchcount==0x10)
{
Switchcount=0;
LPC_GPIO0 ->FIOCLR=0x00180ff0;
}
}
*********************************************************************************
to control each display independently written by Adarsh N D 6th sem TCE, JNNCE
#include<LPC17xx.h>
unsigned int delay,count=0,Switchcount=0,j,i,k;
unsigned int disp[16]={0x000003f0,0x00000060,0x000005b0,0x000004f0,
0x00000660,0x000006d0,0x000007d0,0x00000070,
0x000007f0,0x000006f0,0x00000770,0x000007c0,
0x00000390,0x000005e0,0x00000790,0x00000710};
void dispone(void);
void disptwo(void);
#define ALLDISP 0x00180000
#define DATAPORT 0x00000ff0
int main(void)
{
LPC_PINCON ->PINSEL0=0x00000000;
LPC_PINCON ->PINSEL1=0x00000000;
while(1)
{
for (i=0;i<16;i++)
dispone(); //function call dispone
for(k=0;k<16;k++)
disptwo(); //function call disptwo
}
}
//function for display1
void dispone(void)
{
LPC_GPIO0->FIODIR=0x00080ff0;
LPC_GPIO0 ->FIOSET =ALLDISP;
LPC_GPIO0 ->FIOCLR =0x00000ff0;
LPC_GPIO0 ->FIOSET=disp[Switchcount];
for(j=0;j<3;j++)
for(delay=0;delay<2500000;delay++);
Switchcount++;
if(Switchcount==0x10)
{
Switchcount=0;
LPC_GPIO0 ->FIOCLR=0x00180ff0;
}
}
//function for display2
void disptwo(void)
{
LPC_GPIO0->FIODIR=0x00100ff0;
LPC_GPIO0 ->FIOSET =ALLDISP;
LPC_GPIO0 ->FIOCLR =0x00000ff0;
LPC_GPIO0 ->FIOSET=disp[Switchcount];
for(j=0;j<3;j++)
for(delay=0;delay<2500000;delay++);
Switchcount++;
if(Switchcount==0x10)
{
Switchcount=0;
LPC_GPIO0 ->FIOCLR=0x00180ff0;
}
}
8. External Interrupt
PartB
Theory:
Explanation:
7. Generate PWM
PartB
7.Using the Internal PWM module of ARM controller generate PWM and vary its duty cycle.
*********************************************************************************
//program to fade an LED or to demonstrate PWM on CRO
#include <lpc17xx.h>
void pwm_init(void);
void PWM1_IRQHandler(void);
unsigned long int i,j;
unsigned char flag,flag1;
int main(void)
{
SystemInit();
SystemCoreClockUpdate();
pwm_init();
while(1)
{
for(i=0;i<=1000;i++); // delay
}
} //end of main
void pwm_init(void)
{
LPC_SC->PCONP |= (1<<6); //PWM1 is powered
LPC_PINCON->PINSEL7 |= 0x000c00000; //pwm1.2 is selected for the pin P3.25
LPC_PWM1->PR = 0x00000000; //Count frequency : Fpclk
LPC_PWM1->PCR = 0x00000400; //select PWM5 single edge and PWM5 output is enabled
LPC_PWM1->MCR = 0x00000003; //Reset and interrupt on PWMMR0
LPC_PWM1->MR0 = 0X00007530; //setup match register 0[MR0] count to cycle time
LPC_PWM1->MR2 = 0x00000064; //setup match register 2[MR2] coount to duty cycle
LPC_PWM1->LER = 0x000000FF; //enable shadow copy register
LPC_PWM1->TCR = 0x00000002; //RESET COUNTER AND PRESCALER
LPC_PWM1->TCR = 0x00000009; //enable PWM and counter
NVIC_EnableIRQ(PWM1_IRQn);
return;
}
void PWM1_IRQHandler(void)
{
LPC_PWM1->IR = 0xff; //clear the interrupts
if(flag == 0x00) // to increase the Ton period by 64 for every iteration till MR5 is less than 6978
{
LPC_PWM1->MR2 += 0X00000064;//incrementation of MR2 count value by 64 for every iteration
LPC_PWM1->LER = 0x000000FF;
if(LPC_PWM1->MR2 >= 0X00004E20)
{
flag1 = 0xff;
flag = 0xff;
LPC_PWM1->LER = 0x000000FF;
}
for(i=0;i<8000;i++);
}
else if(flag1 == 0xff) // to increase the Toff period or to decrease Ton period by 64 for every iteration till MR2 is greater than BB
{
LPC_PWM1->MR2 -= 0X00000064; //decrementation of MR2 count value by 64 for every iteration
LPC_PWM1->LER = 0x000000FF;
if(LPC_PWM1->MR2 <= 0x00000500)
{
flag = 0x00;
flag1 = 0x00;
LPC_PWM1->LER = 0X000000FF;
}
for(i=0;i<8000;i++);
}
}
#include <lpc17xx.h>
void pwm_init(void);
void PWM1_IRQHandler(void);
unsigned long int i,j;
unsigned char flag,flag1;
int main(void)
{
SystemInit();
SystemCoreClockUpdate();
pwm_init();
while(1)
{
for(i=0;i<=1000;i++); // delay
}
} //end of main
void pwm_init(void)
{
LPC_SC->PCONP |= (1<<6); //PWM1 is powered
LPC_PINCON->PINSEL7 |= 0x000c00000; //pwm1.2 is selected for the pin P3.25
LPC_PWM1->PR = 0x00000000; //Count frequency : Fpclk
LPC_PWM1->PCR = 0x00000400; //select PWM5 single edge and PWM5 output is enabled
LPC_PWM1->MCR = 0x00000003; //Reset and interrupt on PWMMR0
LPC_PWM1->MR0 = 0X00007530; //setup match register 0[MR0] count to cycle time
LPC_PWM1->MR2 = 0x00000064; //setup match register 2[MR2] coount to duty cycle
LPC_PWM1->LER = 0x000000FF; //enable shadow copy register
LPC_PWM1->TCR = 0x00000002; //RESET COUNTER AND PRESCALER
LPC_PWM1->TCR = 0x00000009; //enable PWM and counter
NVIC_EnableIRQ(PWM1_IRQn);
return;
}
void PWM1_IRQHandler(void)
{
LPC_PWM1->IR = 0xff; //clear the interrupts
if(flag == 0x00) // to increase the Ton period by 64 for every iteration till MR5 is less than 6978
{
LPC_PWM1->MR2 += 0X00000064;//incrementation of MR2 count value by 64 for every iteration
if(LPC_PWM1->MR2 >= 0X00004E20)
{
flag1 = 0xff;
flag = 0xff;
LPC_PWM1->LER = 0x000000FF;
}
for(i=0;i<8000;i++);
}
else if(flag1 == 0xff) // to increase the Toff period or to decrease Ton period by 64 for every iteration till MR2 is greater than BB
{
LPC_PWM1->MR2 -= 0X00000064; //decrementation of MR2 count value by 64 for every iteration
LPC_PWM1->LER = 0x000000FF;
if(LPC_PWM1->MR2 <= 0x00000500)
{
flag = 0x00;
flag1 = 0x00;
LPC_PWM1->LER = 0X000000FF;
}
for(i=0;i<8000;i++);
}
}
6. 4x4 keypad interface
PartB
6a.Interface an LCD with LPC1768 .
Theory:
In this tutorial we are going to see how to interface a 2x16 LCD with LPC1768 in 4-bit mode. As per the name the 2x16 has 2 lines with 16 chars on each lines. It supports all the ascii chars and is basically used for displaying the alpha numeric characters. Here each character is displayed in a matrix of 5x7 pixels. Apart from alpha numeric chars it also provides the provision to display the custom characters by creating the pattern.
LCD UNIT
Let us look at a pin diagram of a commercially available LCD like JHD162 which uses a HD44780 controller and then describe its operation.
5 X 7 Dots
Pin Number | Symbol | Pin Function |
1 | VSS | Ground |
2 | VCC | +5v |
3 | VEE | Contrast adjustment (VO) |
4 | RS | Register Select. 0:Command, 1: Data |
5 | R/W | Read/Write, R/W=0: Write & R/W=1: Read |
6 | EN | Enable. Falling edge triggered |
7 | D0 | Data Bit 0 (Not used in 4-bit operation) |
8 | D1 | Data Bit 1 (Not used in 4-bit operation) |
9 | D2 | Data Bit 2 (Not used in 4-bit operation) |
10 | D3 | Data Bit 3 (Not used in 4-bit operation) |
11 | D4 | Data Bit 4 |
12 | D5 | Data Bit 5 |
13 | D6 | Data Bit 6 |
14 | D7 | Data Bit 7/Busy Flag |
15 | A/LED+ | Back-light Anode(+) |
16 | K/LED- | Back-Light Cathode(-) |
Apart from the voltage supply connections the important pins from the programming perspective are the data lines(8-bit Data bus), Register select, Read/Write and Enable pin.
Data Bus: As shown in the above figure and table, an alpha numeric lcd has a 8-bit data bus referenced as D0-D7. As it is a 8-bit data bus, we can send the data/cmd to LCD in bytes. It also provides the provision to send the the data/cmd in chunks of 4-bit, which is used when there are limited number of GPIO lines on the microcontroller.
Register Select(RS): The LCD has two register namely a Data register and Command register. Any data that needs to be displayed on the LCD has to be written to the data register of LCD. Command can be issued to LCD by writing it to Command register of LCD. This signal is used to differentiate the data/cmd received by the LCD.
If the RS signal is LOW then the LCD interprets the 8-bit info as Command and writes it Command register and performs the action as per the command.
If the RS signal is HIGH then the LCD interprets the 8-bit info as data and copies it to data register. After that the LCD decodes the data for generating the 5x7 pattern and finally displays on the LCD.
Read/Write(RW): This signal is used to write the data/cmd to LCD and reads the busy flag of LCD. For write operation the RW should be LOW and for read operation the R/W should be HIGH.
Enable(EN): This pin is used to send the enable trigger to LCD. After sending the data/cmd, Selecting the data/cmd register, Selecting the Write operation. A HIGH-to-LOW pulse has to be send on this enable pin which will latch the info into the LCD register and triggers the LCD to act accordingly.
Data Bus: As shown in the above figure and table, an alpha numeric lcd has a 8-bit data bus referenced as D0-D7. As it is a 8-bit data bus, we can send the data/cmd to LCD in bytes. It also provides the provision to send the the data/cmd in chunks of 4-bit, which is used when there are limited number of GPIO lines on the microcontroller.
Register Select(RS): The LCD has two register namely a Data register and Command register. Any data that needs to be displayed on the LCD has to be written to the data register of LCD. Command can be issued to LCD by writing it to Command register of LCD. This signal is used to differentiate the data/cmd received by the LCD.
If the RS signal is LOW then the LCD interprets the 8-bit info as Command and writes it Command register and performs the action as per the command.
If the RS signal is HIGH then the LCD interprets the 8-bit info as data and copies it to data register. After that the LCD decodes the data for generating the 5x7 pattern and finally displays on the LCD.
Read/Write(RW): This signal is used to write the data/cmd to LCD and reads the busy flag of LCD. For write operation the RW should be LOW and for read operation the R/W should be HIGH.
Enable(EN): This pin is used to send the enable trigger to LCD. After sending the data/cmd, Selecting the data/cmd register, Selecting the Write operation. A HIGH-to-LOW pulse has to be send on this enable pin which will latch the info into the LCD register and triggers the LCD to act accordingly.
LCD Operation
In this section we are going to see how to send the data/cmd to the LCD along with the timing diagrams. First lets see the timing diagram for sending the data and the command signals(RS,RW,EN), accordingly we write the algorithm and finally the code.
Timing Diagram
The below image shows the timing diagram for sending the data to the LCD.
As shown in the timing diagram the data is written after sending the RS and RW signals.
As shown in the timing diagram the data is written after sending the RS and RW signals.
The only important thing is the data should be available on the databus before generating the High-to-Low pulse on EN pin.
Steps for Sending Command:
- Send the I/P command to LCD.
- Select the Control Register by making RS low.
- Select Write operation making RW low.
- Send a High-to-Low pulse on Enable PIN with some delay_us.
Steps for Sending Data:
- Send the character to LCD.
- Select the Data Register by making RS high.
- Select Write operation making RW low.
- Send a High-to-Low pulse on Enable PIN with some delay_us.
The timings are similar as above only change is that RS is made high for selecting Data register.
Explanation:
// program to display on LCD
If lcd_data(‘A’) or lcd_data(0x41) then lcd_data(0x04) to send higher nibble first by shift operation
void lcd_cmd1(unsigned char pcmd)
if( pcmd & 0x02) // check d1 set or not
if( pcmd & 0x04) // check d2 set or not
if( pcmd & 0x08) // check d3 set or not
if( pdata & 0x01) // check d0 set or not
if( pdata & 0x04) // check d2 set or not
if( pdata & 0x08) // check d3 set or not
// SEND ENABLE PULSE
/* LCD 4 bit mode
SFRs required RS=(0/1)(cmd/data) P2.4;EN short pulse of 25 count value after checking task P2.5; R/W by default in ALS kit its 1 need not to worry as its internally connected,
Data = (MSB)d3=P2.9,d2=P2.8,d1=P2.7,d0=P2.6(LSB)
Data = (MSB)d3=P2.9,d2=P2.8,d1=P2.7,d0=P2.6(LSB)
Start writing 2 functions
F1: void_cmd1(unsigned char pcmd);
F2: void_data1(unsigned char pdata);
Problem: our data, as well as data to be sent, is of byte length (8 bits) but we are using 4-bit mode programming
Note: As its 4bit mode programming, we should pass higher nibble of a byte first by shift right operation
lcd_data(0x01) anyhow lower nibble passed later
for LCD initialization, the sequence is important
0x30
30000
0x30
30000
0x30
30000
0x20
30000
0x28 4 bit 2 lines 5*7 dots
30000
0x0c display on cursor
30000
0x01 clear display screen
30000
circuit diagram
#include <LPC17xx.h>
fig: LCD interfacing diagram
#include <LPC17xx.h>
void lcd_cmd1(unsigned char pcmd); //functions where masking technique is used to check //whether d3d2d1d0 are set or reset and if set then force //corresponding pin(9876) to set or clr
void lcd_data1(unsigned char pdata);
void lcd_cmd(unsigned char pcmd); //functions to split the byte data to higher and lower nibble
void lcd_data(unsigned char pdata); //functions to split the byte data to higher and lower nibble
void delay(unsigned int count);
void lcd_init(void);
unsigned int i, n=25,pcmd,pdata,count; // n=25 to generate short pulse of EN
int main( void )
{ //unsigned char tdata,tcmd,temp1,temp2;
SystemInit(); // System initialization
SystemCoreClockUpdate(); // Clock initialization
// Function Selection
LPC_PINCON->PINSEL4 = 0X00000000; // PORT 2, 12th bit as zero, P2.12 as GPIO
// set Direction
LPC_GPIO2->FIODIR = 0x0000FFFF; // 0-> input , 1->output (P2.9;8;7;6)
lcd_init(); //initialization function calling statement
lcd_data('C'); //fuction calling statement
while(1);
}
void lcd_init(void)
{
lcd_cmd(0x30);//function call
delay(30000);
lcd_cmd(0x30); //function call
delay(30000);
lcd_cmd(0x30); //function call
delay(30000);
lcd_cmd(0x20); //function call
delay(30000);
lcd_cmd(0x28); //function call
delay(30000);
lcd_cmd(0x0c); //function call
delay(30000);
lcd_cmd(0x06); //function call
lcd_cmd(0x01); //function call
delay(30000);
lcd_cmd(0x80); //function call
delay(30000);
}
void lcd_cmd(unsigned char pcmd)
{
lcd_cmd1(pcmd>>4);//function call
lcd_cmd1(pcmd);
}
void lcd_data(unsigned char pdata)
{
lcd_data1(pdata>>4);//function call
lcd_data1(pdata);//
}
void lcd_cmd1(unsigned char pcmd)
{
if( pcmd & 0x01) // check d0 set or not
{
LPC_GPIO2->FIOSET = 0X0040; //set pin 6
}
else
{
LPC_GPIO2->FIOCLR = 0X0040; //clr pin 6
}
if( pcmd & 0x02) // check d1 set or not
{
LPC_GPIO2->FIOSET = 0X0080; //set pin 7
}
else
{
LPC_GPIO2->FIOCLR = 0X0080; //clr pin 7
}
if( pcmd & 0x04) // check d2 set or not
{
LPC_GPIO2->FIOSET = 0X0100; //set pin 8
}
else
{
LPC_GPIO2->FIOCLR = 0X0100; //clr pin 8
}
if( pcmd & 0x08) // check d3 set or not
{
LPC_GPIO2->FIOSET = 0X0200; //set pin 9
}
else
{
LPC_GPIO2->FIOCLR = 0X0200; //clr pin 9
}
LPC_GPIO2->FIOCLR = 0X0010; // clear 4th bit ( RS) P2.4 select command register
// SEND ENABLE PULSE on P2.5 high to low transition
LPC_GPIO2->FIOSET = 0X0020;
for( i = 0; i < n; i++ );
LPC_GPIO2->FIOCLR = 0X0020;
}
void lcd_data1(unsigned char pdata)
{
if( pdata & 0x01) // check d0 set or not
{
LPC_GPIO2->FIOSET = 0X0040; //set pin 6
}
else
{
LPC_GPIO2->FIOCLR = 0X0040; //clr pin 6
}
if( pdata & 0x02) // check d1 set or not
{
LPC_GPIO2->FIOSET = 0X0080; //set pin 7
}
else
{
LPC_GPIO2->FIOCLR = 0X0080; //clr pin 7
}
if( pdata & 0x04) // check d2 set or not
{
LPC_GPIO2->FIOSET = 0X0100; //set pin 8
}
else
{
LPC_GPIO2->FIOCLR = 0X0100; //clr pin 8
}
if( pdata & 0x08) // check d3 set or not
{
LPC_GPIO2->FIOSET = 0X0200; //set pin 9
}
else
{
LPC_GPIO2->FIOCLR = 0X0200; //clr pin 9
}
LPC_GPIO2->FIOSET = 0x0010; // SET 4th bit ( RS) select data register P2.4
// SEND ENABLE PULSE P2.5 high to low transition
LPC_GPIO2->FIOSET = 0X0020;
for( i = 0; i < n; i++ );
LPC_GPIO2->FIOCLR = 0X0020;
}
void delay(unsigned int count)
{
for(i=0;i<count;i++);
}
*********************************************************************************
//To display a message on LCD
#include <LPC17xx.h>
void lcd_cmd1(unsigned char pcmd);
void lcd_data1(unsigned char pdata);
void lcd_cmd(unsigned char pcmd);
void lcd_data(unsigned char pdata);
void delay(unsigned int count);
void lcd_init(void);
unsigned int i, n=25,pcmd,pdata,count;
int main( void )
{ //unsigned char tdata,tcmd,temp1,temp2;
int j;
unsigned char msg[16]={"*JNNCE forever*"};
unsigned char msg1[16]={"**and ever**"};
SystemInit(); // System initialization
SystemCoreClockUpdate(); // Clock initialization
// Function Selection
LPC_PINCON->PINSEL4 = 0X00; // PORT 2, 12th bit as zero, P2.12 as GPIO
// set Direction
LPC_GPIO2->FIODIR = 0xFFFF; // 0-> input , 1->output
lcd_init();
lcd_cmd(0x80);//function call
delay(30000);
for (j=0;j<16;j++)
{
lcd_data(msg[j]); //function calling statement
delay(30000);
}
lcd_cmd(0xc0);//function call
delay(30000);
for (j=0;j<16;j++)
{
lcd_data(msg1[j]);//
delay(30000);
}
delay(10000);
while(1);
}
void lcd_init(void)
{
lcd_cmd(0x30);//function call
delay(30000);
lcd_cmd(0x30);//function call
delay(30000);
lcd_cmd(0x30);//function call
delay(30000);
lcd_cmd(0x20);//function call
delay(30000);
lcd_cmd(0x28);//function call
delay(30000);
lcd_cmd(0x0c);//function call
delay(30000);
lcd_cmd(0x06);//function call
lcd_cmd(0x01);//function call
delay(30000);
}
void lcd_cmd(unsigned char pcmd)
{
lcd_cmd1(pcmd>>4);//function call
lcd_cmd1(pcmd);
}
void lcd_data(unsigned char pdata)
{
lcd_data1(pdata>>4);//
lcd_data1(pdata);//
}
void lcd_cmd1(unsigned char pcmd)
{
if( pcmd & 0x01) // check d0 set or not
{
LPC_GPIO2->FIOSET = 0X0040;
}
else
{
LPC_GPIO2->FIOCLR = 0X0040;
}
if( pcmd & 0x02) // check d1 set or not
{
LPC_GPIO2->FIOSET = 0X0080;
}
else
{
LPC_GPIO2->FIOCLR = 0X0080;
}
if( pcmd & 0x04) // check d2 set or not
{
LPC_GPIO2->FIOSET = 0X0100;
}
else
{
LPC_GPIO2->FIOCLR = 0X0100;
}
if( pcmd & 0x08) // check d3 set or not
{
LPC_GPIO2->FIOSET = 0X0200;
}
else
{
LPC_GPIO2->FIOCLR = 0X0200;
}
LPC_GPIO2->FIOCLR = 0X0010; // clear 4th bit ( RS) select command register
// SEND ENABLE PULSE
LPC_GPIO2->FIOSET = 0X0020;
for( i = 0; i < n; i++ );
LPC_GPIO2->FIOCLR = 0X0020;
}
void lcd_data1(unsigned char pdata)
{
if( pdata & 0x01) // check d0 set or not
{
LPC_GPIO2->FIOSET = 0X0040;
}
else
{
LPC_GPIO2->FIOCLR = 0X0040;
}
if( pdata & 0x02) // check d1 set or not
{
LPC_GPIO2->FIOSET = 0X0080;
}
else
{
LPC_GPIO2->FIOCLR = 0X0080;
}
if( pdata & 0x04) // check d2 set or not
{
LPC_GPIO2->FIOSET = 0X0100;
}
else
{
LPC_GPIO2->FIOCLR = 0X0100;
}
if( pdata & 0x08) // check d3 set or not
{
LPC_GPIO2->FIOSET = 0X0200;
}
else
{
LPC_GPIO2->FIOCLR = 0X0200;
}
LPC_GPIO2->FIOSET = 0x0010; // SET 4th bit ( RS) select data register
// SEND ENABLE PULSE
LPC_GPIO2->FIOSET = 0X0020;
for( i = 0; i < n; i++ );
LPC_GPIO2->FIOCLR = 0X0020;
}
void delay(unsigned int count)
{
for(i=0;i<count;i++);
}
*****************************************************************************************
// to Flash msg
#include <LPC17xx.h>
void lcd_cmd1(unsigned char pcmd);
void lcd_data1(unsigned char pdata);
void lcd_cmd(unsigned char pcmd);
void lcd_data(unsigned char pdata);
void delay(unsigned int count);
void lcd_init(void);
unsigned int i, n=25,pcmd,pdata,count;
int main( void )
{ //unsigned char tdata,tcmd,temp1,temp2;
int j;
unsigned char msg]={"*JNNCE forever*"};
unsigned char msg1[]={"**and ever**"};
SystemInit(); // System initialization
SystemCoreClockUpdate(); // Clock initialization
// Function Selection
LPC_PINCON->PINSEL4 = 0X00; // PORT 2, 12th bit as zero, P2.12 as GPIO
// set Direction
LPC_GPIO2->FIODIR = 0xFFFF; // 0-> input , 1->output
lcd_init();
while(1)
{
lcd_cmd(0x80);//function call
delay(30000);
for (j=0;j<16;j++)
{
lcd_data(msg[j]);//
delay(100);
}
lcd_cmd(0xc0);//function call
delay(1000);
for (j=0;j<16;j++)
{
lcd_data(msg1[j]);//
delay(100);
}
delay(30000);
delay(30000);
lcd_cmd(0x01);//function call to clear display screen
delay(30000);
}
}
void lcd_init(void)
{
lcd_cmd(0x30);//function call
delay(30000);
lcd_cmd(0x30);//function call
delay(30000);
lcd_cmd(0x30);//function call
delay(30000);
lcd_cmd(0x20);//function call
delay(30000);
lcd_cmd(0x28);//function call
delay(30000);
lcd_cmd(0x0c);//function call
delay(30000);
lcd_cmd(0x06);//function call
lcd_cmd(0x01);//function call
delay(30000);
}
void lcd_cmd(unsigned char pcmd)
{
lcd_cmd1(pcmd>>4);//function call
lcd_cmd1(pcmd);
}
void lcd_data(unsigned char pdata)
{
lcd_data1(pdata>>4);//
lcd_data1(pdata);//
}
void lcd_cmd1(unsigned char pcmd)
{
// LPC_GPIO2->FIOPIN =pcmd; //doubt
if( pcmd & 0x01) // check d0 set or not
{
LPC_GPIO2->FIOSET = 0X0040;
}
else
{
LPC_GPIO2->FIOCLR = 0X0040;
}
if( pcmd & 0x02) // check d1 set or not
{
LPC_GPIO2->FIOSET = 0X0080;
}
else
{
LPC_GPIO2->FIOCLR = 0X0080;
}
if( pcmd & 0x04) // check d2 set or not
{
LPC_GPIO2->FIOSET = 0X0100;
}
else
{
LPC_GPIO2->FIOCLR = 0X0100;
}
if( pcmd & 0x08) // check d3 set or not
{
LPC_GPIO2->FIOSET = 0X0200;
}
else
{
LPC_GPIO2->FIOCLR = 0X0200;
}
LPC_GPIO2->FIOCLR = 0X0010; // clear 4th bit ( RS) selectc command register
//LPC_GPIO2->FIOCLR = 0X0020;// SEND ENABLE PULSE
LPC_GPIO2->FIOSET = 0X0020;
for( i = 0; i < n; i++ );
LPC_GPIO2->FIOCLR = 0X0020;
}
void lcd_data1(unsigned char pdata)
{
// LPC_GPIO2->FIOPIN =pdata; //doubt
if( pdata & 0x01) // check d0 set or not
{
LPC_GPIO2->FIOSET = 0X0040;
}
else
{
LPC_GPIO2->FIOCLR = 0X0040;
}
if( pdata & 0x02) // check d1 set or not
{
LPC_GPIO2->FIOSET = 0X0080;
}
else
{
LPC_GPIO2->FIOCLR = 0X0080;
}
if( pdata & 0x04) // check d2 set or not
{
LPC_GPIO2->FIOSET = 0X0100;
}
else
{
LPC_GPIO2->FIOCLR = 0X0100;
}
if( pdata & 0x08) // check d3 set or not
{
LPC_GPIO2->FIOSET = 0X0200;
}
else
{
LPC_GPIO2->FIOCLR = 0X0200;
}
LPC_GPIO2->FIOSET = 0x0010; // SET 4th bit ( RS) select data register
// SEND ENABLE PULSE
LPC_GPIO2->FIOSET = 0X0020;
for( i = 0; i < n; i++ );
LPC_GPIO2->FIOCLR = 0X0020;
}
void delay(unsigned int count)
{
for(i=0;i<count;i++);
6b.Interface a 4x4 keyboard and display the key code on an LCD.
if we were to program each push buttons(16 in no.) would consume 16 port pins
- As we are using a 4×4 matrix keypad, a total of 8 input-output pins of the micro-controller will be required for interfacing.
- *****************************************************************************************************************
- ------------------------------------------------------prototypes.h--------------------------------------------------------------
- #include<LPC17xx.h>
- void lcd_cmd1(unsigned char pcmd);
- void lcd_data1(unsigned char pdata);
- void lcd_cmd(unsigned char pcmd);
- void lcd_data(unsigned char pdata);
- void delay(unsigned int count);
- void lcd_init(void);
- void scan(void);
- unsigned int n=25,pcmd,pdata,count;
- unsigned char col,row,var,flag,key;
- unsigned long int i,j,var1,temp,temp3;
- ---------------------------------------------------keypad.c---------------------------------------------------------
- #include "prototypes.h"
- unsigned char msg1[16]=" jnnce ";
- unsigned char msg2[16]=" keypressed= ";
- unsigned char SCAN_CODE[16] = {0x1E,0x1D,0x1B,0x17,
- 0x2E,0x2D,0x2B,0x27,
- 0x4E,0x4D,0x4B,0x47,
- 0x8E,0x8D,0x8B,0x87};
- unsigned char ASCII_CODE[16] = {'0','1','2','3',
- '4','5','6','7',
- '8','9','A','B',
- 'C','D','E','F'};
- int main()
- {
- SystemInit();
- SystemCoreClockUpdate();
- LPC_PINCON->PINSEL3 &= 0x00000000; //P1.20 to P1.23 MADE GPIO
- LPC_PINCON->PINSEL0 &= 0x00000000; //P0.15 as GPIO
- LPC_PINCON->PINSEL1 &= 0x00000000; //P0.16 t0 P0.18 made GPIO
- LPC_GPIO0->FIODIR &= ~0x00078000; //made INput P0.15 to P0.18 (cols)
- LPC_GPIO1->FIODIR |= 0x00F00000; //made output P1.20 to P1.23 (rows)
- LPC_GPIO1->FIOSET = 0x00F00000;
- LPC_PINCON->PINSEL4=0X00;
- LPC_GPIO2->FIODIR=0XFFFF;
- lcd_init();
- lcd_cmd(0x80);
- delay(30000);
- for(j=0;j<16;j++)
- {
- lcd_data(msg1[j]);
- delay(100);
- }
- lcd_cmd(0xc0);
- delay(30000);
- for(j=0;j<16;j++)
- {
- lcd_data(msg2[j]);
- delay(100);
- }
- delay(30000);
- while(1)
- {
- while(1)
- {
- for(row=1;row<5;row++)
- {
- if(row == 1)
- var1 = 0x00100000;
- else if(row == 2)
- var1 = 0x00200000;
- else if(row == 3)
- var1 = 0x00400000;
- else if(row == 4)
- var1 = 0x00800000;
- temp = var1;
- LPC_GPIO1->FIOSET = 0x00F00000;
- LPC_GPIO1->FIOCLR = var1; //row got selected
- flag = 0;
- scan();
- if(flag == 1)
- break;
- } //end for(row=1;row<5;row++)
- if(flag == 1)
- break;
- } //2nd while(1)
- for(i=0;i<16;i++)
- {
- if(key == SCAN_CODE[i])
- {
- key = ASCII_CODE[i];
- break;
- } //end if(key == SCAN_CODE[i])
- } //end for(i=0;i<16;i++)
- lcd_cmd(0xce);
- lcd_data(key);
- } //end while 1
- } //end main
- void scan(void)
- {
- unsigned long temp3;
- temp3 = LPC_GPIO0->FIOPIN;
- temp3 &= 0x00078000;
- if(temp3 != 0x00078000)
- {
- for(i=0;i<500;i++);
- temp3 = LPC_GPIO0->FIOPIN;
- temp3 &= 0x00078000;
- if(temp3 != 0x00078000)
- {
- flag = 1;
- temp3 >>= 15; //Shifted to come at LN of byte
- temp >>= 16; //shifted to come at HN of byte
- key = temp3|temp;
- } //2nd if(temp3 != 0x00000000)
- } //1st if(temp3 != 0x00000000)
- } //end scan
- void lcd_init(void)
- {
- lcd_cmd(0X30);
- delay(30000);
- lcd_cmd(0X30);
- delay(30000);
- lcd_cmd(0X30);
- delay(30000);
- lcd_cmd(0X20);
- delay(30000);
- lcd_cmd(0X28);
- delay(30000);
- lcd_cmd(0X0c);
- delay(30000);
- lcd_cmd(0X06);
- lcd_cmd(0X01);
- delay(30000);
- }
- void lcd_cmd(unsigned char pcmd)
- {
- lcd_cmd1(pcmd>>4);
- lcd_cmd1(pcmd);
- }
- void lcd_data(unsigned char pdata)
- {
- lcd_data1(pdata>>4);
- lcd_data1(pdata);
- }
- void lcd_cmd1(unsigned char pcmd)
- {
- if(pcmd & 0X01)
- {
- LPC_GPIO2->FIOSET=0X0040;
- }
- else
- {
- LPC_GPIO2->FIOCLR=0X0040;
- }
- if(pcmd & 0X02)
- {
- LPC_GPIO2->FIOSET=0X0080;
- }
- else
- {
- LPC_GPIO2->FIOCLR=0X0080;
- }
- if(pcmd & 0X04)
- {
- LPC_GPIO2->FIOSET=0X0100;
- }
- else
- {
- LPC_GPIO2->FIOCLR=0X0100;
- }
- if(pcmd & 0X08)
- {
- LPC_GPIO2->FIOSET=0X0200;
- }
- else
- {
- LPC_GPIO2->FIOCLR=0X0200;
- }
- LPC_GPIO2->FIOCLR=0X0010;
- LPC_GPIO2->FIOSET=0X0020;
- for(i=0;i<n;i++)
- LPC_GPIO2->FIOCLR=0X0020;
- }
- void lcd_data1(unsigned char pdata)
- {
- if(pdata & 0X01)
- {
- LPC_GPIO2->FIOSET=0X0040;
- }
- else
- {
- LPC_GPIO2->FIOCLR=0X0040;
- }
- if(pdata & 0X02)
- {
- LPC_GPIO2->FIOSET=0X0080;
- }
- else
- {
- LPC_GPIO2->FIOCLR=0X0080;
- }
- if(pdata & 0X04)
- {
- LPC_GPIO2->FIOSET=0X0100;
- }
- else
- {
- LPC_GPIO2->FIOCLR=0X0100;
- }
- if(pdata & 0X08)
- {
- LPC_GPIO2->FIOSET=0X0200;
- }
- else
- {
- LPC_GPIO2->FIOCLR=0X0200;
- }
- LPC_GPIO2->FIOSET=0X0010;
- LPC_GPIO2->FIOSET=0X0020;
- for(i=0;i<n;i++);
- LPC_GPIO2->FIOCLR=0X0020;
- }
- void delay(unsigned int count)
- {
- for(i=20;i<count;i++);
- }
- *****************************************************************************************************************
Subscribe to:
Posts (Atom)
lab record
TECHNICAL SPECIFICATIONS of LPC1768 (15-16 of ARMCTXM3) ******************************************************************************...