MCESPrograms PartB
MCESPrograms PartB
#include <lpc214x.h>
int contUP = 0;
int contDN = 99;
unsigned int rightSFT = 1U<<7;
unsigned int leftSFT = 1;
void reset(int x)
{
if(x!=0)
contUP=0;
if(x!=1)
contDN=99;
if(x!=2)
rightSFT=1U<<7;
if(x!=3)
leftSFT=1;
}
int main()
{
IO0DIR = 0xFF<<16;
IO0SET = 0xff<<16;
while(1)
{
if(!SW2)
{
reset(0);
IO0SET = 0xff<<16;
IO0CLR = ((contUP/10)<<4 | (contUP%10))<<16;
contUP++;
if(contUP>99) contUP=0;
}
else if(!SW3)
{
reset(1);
IO0SET = 0xff<<16;
IO0CLR = ((contDN/10)<<4 | (contDN%10))<<16;
contDN--;
if(contDN<0) contUP=99;
}
else if(!SW4)
{
reset(2);
IO0SET = 0xff<<16;
IO0CLR = rightSFT<<16;
rightSFT>>=1;
if(rightSFT < 1) rightSFT=1U<<7;
}
else if(!SW5)
{
reset(3);
IO0SET = 0xff<<16;
IO0CLR = leftSFT<<16;
leftSFT<<=1;
if(leftSFT> 1U<<7) leftSFT=1;
}
delay(300);
}
}
-----------------------------------------------------------------------------------
------------
//Seven Segment Display Program:
//P0.19 Data pin of 1st shift register
//P0.20 Clock pin of shift registers, make 1 to 0
//P0.30 Strobe pin of shift registers: 1 to 0
#include <lpc214x.h>
int main()
{
IO0DIR |= 1U << 31 | 1U << 19 | 1U << 20 | 1U << 30 ; // to set as o/ps
LED_ON; // make D7 Led on .. just indicate the program is running
while(1)
{
alphadisp7SEG("fire ");
delay_ms(500);
alphadisp7SEG("help ");
delay_ms(500);
}
}
-----------------------------------------------------------------------------------
---------------
//Program 3b
#include <lpc214x.h>
-----------------------------------------------------------------------------------
------------------
//Matrix 4 x 4 Keyboard
//Columns & Rows are pulled to +5v,if dont press key, we receive '1' on columns
//Method: Sending '0' to a selected row, checking for '0' on each column
//ROWS - ROW0-ROW3 -> P0.16,P0.17,P0.18,P0.19
//COLS - COL0-COL3 -> P1.19,P1.18,P1.17,P1.16
#include <lpc214x.h>
int main( )
{
uart_init();//initialize UART0 port
IO0DIR |= 1U << 31 | 0x00FF0000; // to set P0.16 to P0.23 as o/ps
//make D7 Led on off for testing
LED_ON; delay_ms(500);LED_OFF;delay_ms(500);
do
{
while(1)
{
//check for keypress in row0,make row0 '0',row1=row2=row3='1'
rowsel=0;IO0SET = 0X000F0000;IO0CLR = 1 << 16;
if(COL0==0){colsel=0;break;};if(COL1==0){colsel=1;break;};
if(COL2==0){colsel=2;break;};if(COL3==0){colsel=3;break;};
//check for keypress in row1,make row1 '0'
rowsel=1;IO0SET = 0X000F0000;IO0CLR = 1 << 17;
if(COL0==0){colsel=0;break;};if(COL1==0){colsel=1;break;};
if(COL2==0){colsel=2;break;};if(COL3==0){colsel=3;break;};
//check for keypress in row2,make row2 '0'
rowsel=2;IO0SET = 0X000F0000;IO0CLR = 1 << 18;//make row2 '0'
if(COL0==0){colsel=0;break;};if(COL1==0){colsel=1;break;};
if(COL2==0){colsel=2;break;};if(COL3==0){colsel=3;break;};
//check for keypress in row3,make row3 '0'
rowsel=3;IO0SET = 0X000F0000;IO0CLR = 1 << 19;//make row3 '0'
if(COL0==0){colsel=0;break;};if(COL1==0){colsel=1;break;};
if(COL2==0){colsel=2;break;};if(COL3==0){colsel=3;break;};
};
delay_ms(50); //allow for key debouncing
while(COL0==0 || COL1==0 || COL2==0 || COL3==0);//wait for key
release
delay_ms(50); //allow for key debouncing
IO0SET = 0X000F0000; //disable all the rows
U0THR = lookup_table[rowsel][colsel]; //send to serial port(check on the
terminal)
}while(1);
}
void uart_init(void)
{
//configurations to use serial port
PINSEL0 |= 0x00000005; // P0.0 & P0.1 ARE CONFIGURED AS TXD0 & RXD0
U0LCR = 0x83; /* 8 bits, no Parity, 1 Stop bit */
U0DLM = 0; U0DLL = 8; // 115200 baud rate
U0LCR = 0x03; /* DLAB = 0 */
U0FCR = 0x07; /* Enable and reset TX and RX FIFO. */
}
void delay_ms(unsigned int j)
{
unsigned int x,i;
for(i=0;i<j;i++)
{
for(x=0; x<10000; x++);
}
}
-----------------------------------------------------------------------------------
----------------
//DC Motor Speed Control
//P0.28 - used for direction control
//P0.9 - used for speed,generated by PWM6
//duty cycle - 0 to 100 controlled by PWM, fed from Potentiameter connected to ADC
#include <lpc214x.h>
int main()
{
int dig_val;
IO0DIR |= 1U << 31 | 0x00FF0000 | 1U << 30; // to set P0.16 to P0.23 as o/ps
LED_ON; delay_ms(500);LED_OFF; // make D7 Led on / off for program checking
do{
dig_val = adc(1,3)/10; //use LDR for analog input
if(dig_val > 100) dig_val =100;
runDCMotor(2,dig_val); // run at 10% duty cycle
}while(1);
}
-----------------------------------------------------------------------------------
----------------------------
//Alpha-numeric LCD Interface (4Lines,20characters)
//Connected in 4bit nibble mode
//LCD handshaking:RS->P0.20,EN->P0.25 ,R/W -Gnd
//LCD data:D4,D5,D6,D7 -> P0.16,P0.17,P0.18,P0.19
#include <lpc214x.h>
-----------------------------------------------------------------------------------
---------------