Arm Microcontroller

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 17

Expt No:1Buzzer relay-

#include <lpc17xx.h>
#define SwitchPinNumber 11
#define LedPinNumber 19
#define relayPinNumber 28
int main (void)
{
uint32_t switchStatus;
LPC_GPIO1->FIODIR = 0x1ff80000;
LPC_GPIO1->FIOCLR = 0x1ff80000;
LPC_GPIO2->FIODIR = 0x00000000;
while (1)
{
switchStatus = (LPC_GPIO2->FIOPIN>>SwitchPinNumber) & 0x01 ;
If (switchStatus == 0)
{
LPC_GPIO1->FIOPIN = (1<<buzzerPinNumber) | (1<<relayPinNumber) |
(1<<LedPinNumber);
}
else
{
LPC_GPIO1->FIOPIN = (0<<buzzerPinNumber) | (0<<relayPinNumber) |
(0<<LedPinNumber);
}
}
}

Expt No:2 segment display


#include <lpc17xx.h>
void delay_ms(unsigned int ms)
{
unsigned int i,j;
for(i=0;i<ms;i++)
for(j=0;j<40000;j++);
}
int main()
{
SystemInit();

LPC_GPIO0->FIODIR = 0x000001f7;
while (1)
{
LPC_GPIO0->FIOSET = 0x00000004;

LPC_GPIO0->FIOCLR = 0x000001f3;
LPC_GPIO0->FIOSET = 0x00000100;
delay_ms(200);

LPC_GPIO0->FIOCLR = 0x000001f3;
LPC_GPIO0->FIOSET = 0x000001e1;
delay_ms(200);

LPC_GPIO0->FIOCLR = 0x000001f3;
LPC_GPIO0->FIOSET = 0x00000090;
delay_ms(200);

LPC_GPIO0->FIOCLR = 0x000001f3;
LPC_GPIO0->FIOSET = 0x000000c0;
delay_ms(200);

LPC_GPIO0->FIOCLR = 0x000001f3;
LPC_GPIO0->FIOSET = 0x00000061;
delay_ms(200);

LPC_GPIO0->FIOCLR = 0x000001f3;
LPC_GPIO0->FIOSET = 0x00000042;
delay_ms(200);
LPC_GPIO0->FIOCLR = 0x000001f3;
LPC_GPIO0->FIOSET = 0x00000002;
delay_ms(200);
LPC_GPIO0->FIOCLR = 0x000001f3;
LPC_GPIO0->FIOSET = 0x000001e0;
delay_ms(200);

LPC_GPIO0->FIOCLR = 0x000001f3;
LPC_GPIO0->FIOSET = 0x00000000;
delay_ms(200);

LPC_GPIO0->FIOCLR = 0x000001f3;
LPC_GPIO0->FIOSET = 0x00000040;
delay_ms(200);

LPC_GPIO0->FIOCLR = 0x000001f3;
LPC_GPIO0->FIOSET = 0x00000020;
delay_ms(200);

LPC_GPIO0->FIOCLR = 0x000001f3;
LPC_GPIO0->FIOSET = 0x00000003;
delay_ms(200);

LPC_GPIO0->FIOCLR = 0x000001f3;
LPC_GPIO0->FIOSET = 0x00000112;
delay_ms(200);

LPC_GPIO0->FIOCLR = 0x000001f3;
LPC_GPIO0->FIOSET = 0x00000081;
delay_ms(200);

LPC_GPIO0->FIOCLR = 0x000001f3;
LPC_GPIO0->FIOSET = 0x00000012;
delay_ms(200);
LPC_GPIO0->FIOCLR = 0x000001f3;
LPC_GPIO0->FIOSET = 0x00000032;
delay_ms(200);

}
}

Expt No: 3 Stepper motor


#include "lpc17xx.h"
void delay();
void delay()
{
int i,j;
for(i=0;i<0xff;i++)
for(j=0;j<0x400;j++);
}
int main (void)
{
char rotate=0;
SystemInit();
LPC_GPIO0->FIODIR | = 0x00078000;
while (1)
{
if(rotate==1)
{
LPC_GPIO0->FIOPIN = 0x00008000;
delay();
LPC_GPIO0->FIOPIN = 0x00010000;
delay();
LPC_GPIO0->FIOPIN = 0x00020000;
delay();
LPC_GPIO0->FIOPIN = 0x00040000;
delay();
}
else
{
LPC_GPIO0->FIOPIN = 0x00040000 ;
delay();
LPC_GPIO0->FIOPIN = 0x00020000 ;
delay();
LPC_GPIO0->FIOPIN = 0x00010000 ;
delay();
LPC_GPIO0->FIOPIN = 0x00008000 ;
delay();
}
if(!(LPC_GPIO2->FIOPIN & 0x00000800))
{
while(!(LPC_GPIO2->FIOPIN & 0x00000800));
rotate=1;
}
else if(!(LPC_GPIO2->FIOPIN & 0x00001000))
{
while(!(LPC_GPIO2->FIOPIN & 0x00001000));
rotate=0;
}
}
}

Step Angle
#include <lpc17xx.h>
void delay(unsigned int ms)
{
unsigned int i,j;
for(i=0;i<ms;i++)
for(j=0;j<20000;j++);
}
int main()
{
int cycle;
SystemInit();
LPC_GPIO0->FIODIR | = 0x00078000;
for(cycle=0; cycle<13; cycle++)
{
LPC_GPIO0->FIOPIN = 0x00008000 ;
delay(100);
LPC_GPIO0->FIOPIN = 0x00010000 ;
delay(100);
LPC_GPIO0->FIOPIN = 0x00020000 ;
delay(100);
LPC_GPIO0->FIOPIN = 0x00040000 ;
delay(100);
}
}

Expt No 4:DAC

1.Triangular Waveform.
#include "LPC17xx.h"
uint32_t val;
int main()
{
SystemInit();
LPC_PINCON->PINSEL1 |= 0x02<<20;
while(1)
{
while(1)
{
val++;
LPC_DAC->DACR=(val<<6);
if(val>=0x3ff)
{
break;
}
}
while(1)
{
val--;
LPC_DAC->DACR=(val<<6);
if(val<=0x000)
{
break;
}
}
}

}
2.Square Waveform

#include "LPC17xx.h"
void delay(unsigned int ms) {
unsigned int i,j;
for(i=0;i<ms;i++)
for(j=0;j<2000;j++);
}
int main()
{
SystemInit();
LPC_PINCON->PINSEL1 |= 0x02<<20;
while(1)
{
LPC_DAC->DACR=0xffff;
delay(20);
LPC_DAC->DACR=0x0000;
delay(20);
}

Expt No: 5 4X4 Matrix Keypad


#include <lpc17xx.h>
#include <lcd.h>
#define col1 0
#define col2 1
#define col3 4
#define col4 8
#define row1 9
#define row2 10
#define row3 14
#define row4 15

#define colmask ((1<<col1) | (1<<col2) | (1<<col3) | (1<<col4))


#define rowmask ((1<<row1) | (1<<row2) | (1<<row3) | (1<<row4))

#define KEY_CTRL_DIR LPC_GPIO1-> FIODIR


#define KEY_CTRL_SET LPC_GPIO1-> FIOSET
#define KEY_CTRL_CLR LPC_GPIO1-> FIOCLR
#define KEY_CTRL_PIN LPC_GPIO1-> FIOPIN

void col_write(unsigned int data)


{
if(data == 0x04)
{
KEY_CTRL_CLR ;= colmask;
KEY_CTRL_SET ;= 0X00000013;
}
if(data ==0X03)
{
KEY_CTRL_CLR ;= colmask;
KEY_CTRL_SET ;= 0X00000003;
}
if(data == 0X02)
{
KEY_CTRL_CLR ;= colmask;
KEY_CTRL_SET ;= 0X00000001;
}
if(data == 0X01)
{
KEY_CTRL_CLR ;= colmask;
KEY_CTRL_SET ;= 0X00000000;
}
int main(void)
{
unsigned char key,i;
unsigned char rval[] = {0x04,0x03,0x02,0x01};
unsigned char keypadmatrix[] = {'4', '8', 'B', 'F',
'3', '7', 'A', 'E',
'2', '6', '0', 'D',
'1', '5', '9', 'C'
};
SystemInit();
init_lcd();

KEY_CTRL_DIR1=colmask;
KEY_CTRL_DIR &= ~(rowmask);

lcd_putstring16(0, "press hex keys..");


lcd_putstring16(1, "key pressed = ");
while(1)
{
key=0;
for(i=0;i<4;i++)
{
col_write(rval[i]);
if(!(KEY_CTRL_PIN & (1<<row1)))
break;
key++;

if(!(KEY_CTRL_PIN & (1<<row2)))


break;
key++;

if(!(KEY_CTRL_PIN & (1<<row3)))


break;
key++;

if(!(KEY_CTRL_PIN & (1<<row4)))


break;
key++;
}
if(key == 0x10)
{
lcd_putstring16(1, "key pressed = ");
}
else
{
lcd_gotoxy(1,14);
lcd_putchar(keypadmatrix[key]);
}
}
}
Expt No: 6 PWM

#include <lpc17xx.h>
void delay_ms(unsigned int ms)
{
unsigned int i,j;
for(i=0;i<ms;i++)
for(j=0;j<60000;j++);
}
#define SBIT_CNTEN 0
#define SBIT_PWMEN 2
#define SBIT_PWMMR0R 1
#define SBIT_PWMENA1 9
#define PWM_1 0
int main(void)
{
int dutyCycle;
SystemInit();

LPC_PINCON->PINSEL4 = (1<<PWM_1);
LPC_PWM1->TCR = (1<<SBIT_CNTEN) | (1<<SBIT_PWMEN);

LPC_PWM1->PR = 0x00;
LPC_PWM1->MCR = (1<<SBIT_PWMMR0R);

LPC_PWM1->MR0 = 100;
LPC_PWM1->PCR = (1<<SBIT_PWMENA1);
while(1)
{
for(dutyCycle=0; dutyCycle<100; dutyCycle++)
{
LPC_PWM1->MR1 = dutyCycle;
delay_ms(5);
}
for(dutyCycle=100; dutyCycle>0; dutyCycle--)
{
LPC_PWM1->MR1 = dutyCycle;
delay_ms(5);
}
}
}
Expt No: 7 DC Motor
#include <lpc17xx.h>
void delay_ms(unsigned int ms)
{
unsigned int i,j;
for(i=0;i<ms;i++)
for(j=0;j<60000;j++);
}
#define SBIT_CNTEN 0
#define SBIT_PWMEN 2
#define SBIT_PWMMR0R 1
#define SBIT_PWMENA3 11
#define PWM_3 4
int main(void)
{
int dutyCycle;
SystemInit();
LPC_PINCON->PINSEL4 = (1<<PWM_3);
LPC_PWM1->TCR = (1<<SBIT_CNTEN) | (1<<SBIT_PWMEN);

LPC_PWM1->PR = 0x00;
LPC_PWM1->MCR = (1<<SBIT_PWMMR0R);

LPC_PWM1->MR0 = 100
LPC_PWM1->PCR = (1<<SBIT_PWMENA3);
while (1)
{
{
LPC_PWM1->MR3 = dutyCycle;
delay_ms(5);
}
if(!(LPC_GPIO2->FIOPIN & 0x00000800)) //if sw 23 pressed
{
while(!(LPC_GPIO2->FIOPIN & 0x00000800));
dutyCycle-=10;
if(dutyCycle<0)
{
dutyCycle=0;
}
}
else if(!(LPC_GPIO2->FIOPIN & 0x00001000))
{
while(!(LPC_GPIO2->FIOPIN & 0x00001000));
dutyCycle+=10;
if(dutyCycle>100)
{
dutyCycle=99;
}
}

}
}
Expt No: 8 external interrupt switches toggle the led’s
#include "lpc17xx.h"
#define switch 11
#define led1 19
#define led2 20
#define led3 21
#define led4 22
#define led5 23
#define led6 24
#define led7 25
#define led8 26

int main(void)
{
uint32_t switchstatus;
LPC_GPIO1->FIODIR = 0x07F80000;
LPC_GPIO1->FIOCLR = 0x07F80000;
LPC_GPIO2->FIODIR = 0x00000000;
while(1)
{
switchstatus = (LPC_GPIO2->FIOPIN >> switch) & 0X01;
if(switchstatus == 1)
{
LPC_GPIO1->FIOPIN = (1<<led1) | (1<<led2) | (1<<led3) | (1<<led4);
}
else
{
LPC_GPIO1->FIOPIN = (1<<led5) | (1<<led6) | (1<<led7) | (1<<led8);
}
}
}
// using interrupts
#include <lpc17xx.h>
#define PINSEL_EINT1 22
#define PINSEL_EINT2 24
#define LED1 25
#define LED2 26
#define SBIT_EINT1 1
#define SBIT_EINT2 2
#define SBIT_EXTMODE1 1
#define SBIT_EXTMODE2 2
#define SBIT_EXTPOLAR1 1
#define SBIT_EXTPOLAR2 2

void EINT1_IRQHandler(void)
{
LPC_SC->EXTINT = 0x00400000;
LPC_GPIO1->FIOPIN ^= 0x02000000;
}
void EINT2_IRQHandler(void)
{
LPC_SC->EXTINT = 0x01000000;
LPC_GPIO1->FIOPIN ^= 0x04000000;
}
int main()
{
SystemInit();
LPC_SC->EXTINT = 0x00000006;
LPC_PINCON->PINSEL4 = 0x01400000;
LPC_SC->EXTMODE = 0x00000006;
LPC_SC->EXTPOLAR = 0x00000006;
LPC_GPIO1->FIODIR = 0x06000000;
LPC_GPIO1->FIOPIN = 0x00;
NVIC_EnableIRQ(EINT1_IRQn);
NVIC_EnableIRQ(EINT2_IRQn);
while(1)
{
}
}

You might also like