MICROCONTROLLER AND EMBEDDED SYSTEMS LABORATORY Part B
Program 9 Display “Hello World” message using Internal UART.
#include<lpc21xx.h>
void Uart0Init(void) //initialize serial interface
{
PINSEL0=0X00000005; // enable RXD0 (P0.1) & TXD0 (P0.0)
U0LCR = 0X83; // 8 bits, no parity, 1 stop bit
U0DLL = 97; // 9600 Baud rate @ 15MHz PCLK
U0LCR = 0X03; // DLAB = 0
}
void Uart0PutCh(unsigned char ch) //write char to serial port
{
U0THR = ch; // Transmission hold register
while(!(U0LSR & 0x20));
//checking whether the Char is completely transmitted,
// TX= 0 or 1
// if tx bit =1 , complete transmission of a char to PC has been done
}
void uart_print(char *a)
{
int i;
for(i=0;a[i]!='\0';i++)
Uart0PutCh(a[i]);
//one char at a time is transmitted using this function
}
int main()
{
Uart0Init(); // function call to initialize LPC2148
uart_print("Hello World"); // function call to transmit the data to PC
}
Program explanation and Demo: UART
Program 10 Interface and Control a DC Motor.
//#define MOTOR 0x00000060 // P0.5-P0.6
//#define KEY_CTRL_PIN IO1PIN
#include <LPC214x.H> /* LPC214x definitions */
#define MCW 0X00010000 // OR (1 << 16)//KEY1 P1.16
#define MCCW 0X00020000 // OR (1 << 17)//KEY2 P1.17
#define MOFF 0X00040000 // OR (1 << 18) //KEY3 P1.18
////////////////////// MAIN /////////////////////////////////////
int main (void)
{
IO1DIR = 0X0007; // OR ~( MOFF | MCW |MCCW );
IO0DIR = 0X00000060; // OR (MOTOR );
while(1)
{
if(!(IO1PIN & MCW)) //Clock wise key pressed
{
IO0PIN = 0X000000A0 ;
}
if (!(IO1PIN & MOFF)) //OFF key pressed
{
IO0PIN = 0X00000000 ;
}
if (!(IO1PIN & MCCW)) // Anticlock wise
IO0PIN = 0X000000C0 ;
}
}
Program explanation and Demo: DC Motor Working of DC motor
Program 11 Interface a Stepper motor and rotate it in clockwise and anti-clockwise direction.
#include <LPC214x.H> /* LPC214x definitions */
#define CLK 0x00010000 // or (1 << 16) //KEY1 P1.16
#define ACLK 0x00020000 // or (1 << 17) //KEY5 P1.17
void delay(unsigned int count)
{
int j=0,i=0;
for(j=0;j<count;j++)
{
for(i=0;i<500;i++);
}
}
int main (void)
{
unsigned char run=1;
IO1DIR = 0x0F000000; //make Port P1.31 to P1.24 as output
PINSEL2 = 0x0;
// to ensure RTCK/P1.23 behaves like GPIO, no accidental JTAG
{
while(1)
{
if (!(IO1PIN & CLK) )
run = 1;
if (!(IO1PIN & ACLK))
run = 0;
if (run==1)
{
IO1PIN = 0X01000000;
delay(100);
IO1PIN = 0X02000000;
delay(100);
IO1PIN = 0X04000000;
delay(100);
IO1PIN = 0X08000000;
delay(100);
}
else
{
IO1PIN = 0X08000000;
delay(100);
IO1PIN = 0X04000000;
delay(100);
IO1PIN = 0X02000000;
delay(100);
IO1PIN = 0X01000000;
delay(100);
}
}
}
}
Program 12 Determine Digital output for a given Analog input using Internal ADC of ARM controller.
#include <LPC214x.H> /* LPC214x definitions */
#include <stdio.h>
#include "lcd.h"
////////// Init ADC0 /////////////////
Init_ADC()
{
// Convert Port pin 0.28 to function as AD0.2
PINSEL1 = 0X01000000;
}
////////// READ ADC0 CH:2 /////////////////
unsigned int Read_ADC()
{
unsigned int i=0;
AD0CR = 0x00200D02; //0x00200D02;
AD0CR |= 0x01000000; // Start A/D Conversion
do
{
i = AD0GDR; // Read A/D Data Register
} while ((i & 0x80000000) == 0); // Wait for end of A/D Conversion
return (i >> 6) & 0x03FF; // bit 6:15 is 10 bit AD value
}
////////// DISPLAY ADC VALUE /////////////////
Display_ADC()
{
unsigned int adc_value = 0;
char buf[4];
adc_value = Read_ADC();
sprintf((char *)buf, "%3d", adc_value);
lcd_putstring16(0,"ADC VAL = 000 ");
lcd_gotoxy(0,10);
lcd_putstring(buf);
}
////////// MAIN /////////////////
int main (void)
{
init_lcd();
Init_ADC();
while(1)
{
Display_ADC();
delay(50000);
}
}
Program explanation and Demo: ADC
Program 13 Interface a DAC and generate Triangular and Square waveforms.
#include <LPC214x.H> /* LPC214x definitions */
////////// Init DAC /////////////////
Init_DAC()
{
// Convert Port pin 0.25 to function as DAC
PINSEL1 = 0X00080000;
DACR = 0;
}
////////// Write DAC /////////////////
Write_DAC(unsigned int dacval)
{
DACR = dacval << 6;
}
////////// MAIN /////////////////
int main (void)
{
unsigned int i;
Init_DAC();
while(1)
{
for(i=0;i<1024;i++)
Write_DAC(i);
for(i=1023;i>0;i--)
Write_DAC(i);
}
}
#include <LPC214x.H> /* LPC214x definitions */
////////// Init DAC /////////////////
Init_DAC()
{
// Convert Port pin 0.25 to function as DAC
PINSEL1 = 0X00080000;
DACR = 0;
}
////////// Write DAC /////////////////
Write_DAC(unsigned int dacval)
{
DACR = dacval << 6;
}
void delay(unsigned int count)
{
int j=0,i=0;
for(j=0;j<count;j++)
{
for(i=0;i<120;i++);
}
}
////////// MAIN /////////////////
int main (void)
{
Init_DAC();
while(1)
{
Write_DAC(00);
delay(100); //change this value to change Frequency
Write_DAC(1023); //change this value to change Amplitude
delay(100); //change this value to change Frequency
}
}
Program 14 Interface a 4x4 keyboard and display the key code on an LCD.
include <LPC214x.H> /* LPC214x definitions */
#include "lcd.h"
/////////////////////////////////////////////
#define COL1 0X00010000 // OR (1 << 16)
#define COL2 0X00020000 // OR (1 << 17)
#define COL3 0X00040000 // OR (1 << 18)
#define COL4 0X00080000 // OR (1 << 19)
#define ROW1 0X00100000 // OR (1 << 20)
#define ROW2 0X00200000 // OR (1 << 21)
#define ROW3 0X00400000 // OR (1 << 22)
#define ROW4 0X00800000 // OR (1 << 23)
/////////////// COLUMN WRITE /////////////////////
void col_write( unsigned char data )
{
unsigned int temp=0;
temp=(data << 16);
IO1CLR = (COL1 | COL2 | COL3 | COL4);
IO1SET = temp;
}
///////////////////////////////// MAIN ///////////////////////////////////////
int main (void)
{
unsigned char key, i;
unsigned char rval[] = {0x7,0xB,0xD,0xE,0x0};
unsigned char keyPadMatrix[] =
{
'C','8','4','0',
'D','9','5','1',
'E','A','6','2',
'F','B','7','3'
};
init_lcd();
IO1DIR |= 0X000F0000; //Set COLs as Outputs and Set ROW lines as Inputs
while (1)
{
key = 0;
for( i = 0; i < 4; i++ )
{
// turn on COL output one by one
col_write(rval[i]);
// read rows - break when key press detected
if (!(IO1PIN & ROW1))
break;
key++;
if (!(IO1PIN & ROW2))
break;
key++;
if (!(IO1PIN & ROW3))
break;
key++;
if (!(IO1PIN & ROW4))
break;
key++;
}
if (key == 0x10)
{
key==0;
}
else
{
lcd_gotoxy(0,2);
lcd_putchar(keyPadMatrix[key]);
}
}
}
Program 15 Demonstrate the use of an external interrupt to toggle an LED On/Off.
#include <LPC214x.H>
void delay(int count);
void init_ext_interrupt(void);
__irq void Ext_ISR(void);
int main (void)
{
init_ext_interrupt(); // initialize the external interrupt
while (1)
{
}
}
void init_ext_interrupt() //Initialize Interrupt
{
EXTMODE = 0x4; //Edge sensitive mode on EINT2
EXTPOLAR &= ~(0x4); //Falling Edge Sensitive
PINSEL0 = 0x0000C000; //Select Pin function P0.15 as EINT2
/* initialize the interrupt vector */
VICIntSelect &= ~ (1<<16); // EINT2 selected as IRQ 16
VICVectAddr5 = (unsigned int)Ext_ISR; // address of the ISR
VICVectCntl5 = (1<<5) | 16; //
VICIntEnable = (1<<16); // EINT2 interrupt enabled
EXTINT &= ~(0x4);
}
__irq void Ext_ISR(void) // Interrupt Service Routine-ISR
{
IO1DIR |=0xFF000000;//make Port P1.31 to P1.24 as output
IO1PIN ^= 0x01000000; // Turn ON Buzzer
//delay(10);
//IO1PIN |= 0x00000000;//(1<<25); // Turn OFF Buzzer
EXTINT |= 0x4; //clear interrupt
VICVectAddr = 0; // End of interrupt execution
}
Program 16 Display the Hex digits 0 to F on a 7-segment LED interface, with an appropriate delay in between
#include <LPC214x.H> /* LPC214x definitions */
#define DIG2 (1 << 11)
//#define LED_DATA_MASK 0x007F8000
////////// digits are created using bit patterns corresponding to the segments ////////////////////
unsigned char dig[] =
{ 0x88,0xeb,0x4c,0x49,0x2b,0x19,0x18,0xcb,0x8,0x9,0xa,0x38,0x9c,0x68,0x1c,0x1e};
///////////////////////////////// MAIN ///////////////////////////////////////
int main (void)
{
unsigned char count=0;
unsigned short j=0;
unsigned short i=;0
unsigned char data ;
IO0DIR |= (DIG2 ); //Set Digit control lines as Outputs
IO0CLR = ( DIG2 ); //Clear Digit control lines
while(1)
{
count++;
if(count ==16) count = 0;
for (i=0; i < 800; i++) //change to inc/dec speed of count
{
IO0CLR = 0x007F8000;
IO0SET = (dig[count] << 15);
IO0DIR |= 0x007F8000;
IO0SET = DIG2;
for (j=0;j<2000;j++); //change to inc/dec brightness of display
IO0CLR = DIG2;
}
}
}
Program explanation and Demo:
Comments
Post a Comment