PCC-304-ETC ยท PRACTICAL CODE BOOK

MC Codes

12 programs ยท Search, open and copy quickly

EXPERIMENT 01

Interfacing of LEDs โ€” Flashing, Counter, BCD, HEX (8051)

#include <reg51.h>

void delay(unsigned int msec) {
    unsigned int i, j;
    for (i = 0; i < msec; i++)
        for (j = 0; j < 1275; j++);
}

void flashingLED() {
    while (1) {
        P1 = 0xFF;

delay(500);
        P1 = 0x00;
        delay(500);
    }
}

void binaryCounter() {
    unsigned char i;
    while (1) {
        for (i = 0; i <= 0xFF; i++) {
            P1 = i;
            delay(200);
        }
    }
}

void bcdCounter() {
    unsigned char bcd, tens, ones;
    while (1) {
        for (bcd = 0; bcd <= 99; bcd++) {
            tens = bcd / 10;
            ones = bcd % 10;
            P1 = (tens << 4) | ones;   /* packed BCD on Port1 */
            delay(300);
        }
    }
}

void hexCounter() {
    unsigned char i;
    while (1) {
        for (i = 0x00; i <= 0xFF; i++) {
            P1 = i;
            delay(200);
        }
    }
}

void main() {
    /* Call the required routine to test each mode on hardware */
    flashingLED();
}
EXPERIMENT 02

Interfacing of Multiplexed 7-Segment Display (Counting Application)

#include <reg51.h>

sbit digit1 = P2^0; /* tens digit enable */
sbit digit2 = P2^1; /* units digit enable */

unsigned char code sevenSeg[10] = {
    0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F
};

void delay(unsigned int msec) {
    unsigned int i, j;
    for (i = 0; i < msec; i++)
        for (j = 0; j < 100; j++);
}

void main() {
    unsigned char count, tens, ones;
    unsigned int r;
    while (1) {
        for (count = 0; count <= 99; count++) {
            tens = count / 10;

ones = count % 10;
            for (r = 0; r < 40; r++) {           /* refresh cycles for visible period */
                P1 = sevenSeg[ones];
                digit1 = 0; digit2 = 1;            /* enable units digit */
                delay(2);
                digit2 = 0;

                P1 = sevenSeg[tens];
                digit1 = 1; digit2 = 0;            /* enable tens digit */
                delay(2);
                digit1 = 0;
            }
        }
    }
}
EXPERIMENT 03

Waveform Generation using DAC

#include <reg51.h>

void delay(unsigned int usec) {
    unsigned int i;
    for (i = 0; i < usec; i++);
}

void squareWave() {
    while (1) {
        P1 = 0xFF;
        delay(50);
        P1 = 0x00;
        delay(50);
    }
}

void sawtoothWave() {
    unsigned char i;
    while (1) {
        for (i = 0; i < 0xFF; i++) {

P1 = i;
            delay(2);
        }
    }
}

void triangularWave() {
    unsigned char i;
    while (1) {
        for (i = 0; i < 0xFF; i++) { P1 = i; delay(2); }
        for (i = 0xFF; i > 0; i--) { P1 = i; delay(2); }
    }
}

void main() {
    /* Call the required waveform-generation routine */
    sawtoothWave();
}
EXPERIMENT 04

Interfacing of Stepper Motor to 8051 (Timer-based Delay)

#include <reg51.h>

unsigned char code stepSeq[4] = {0x01, 0x02, 0x04, 0x08}; /* wired to P1.0 - P1.3 */

void timerDelay() {
    TMOD = 0x01;          /* Timer0, Mode 1, 16-bit */
    TH0 = 0x3C;
    TL0 = 0xB0;             /* illustrative delay value @ 11.0592 MHz */
    TR0 = 1;                 /* start timer */
    while (TF0 == 0);       /* wait for overflow */
    TR0 = 0;                 /* stop timer */
    TF0 = 0;                 /* clear overflow flag */
}

void main() {
    unsigned char i;
    while (1) {
        for (i = 0; i < 4; i++) {
            P1 = stepSeq[i];
            timerDelay();
        }
    }
}
EXPERIMENT 05

Interfacing of Relay and Buzzer

#include <reg51.h>

sbit relay  = P1^0;
sbit buzzer = P1^1;
sbit sensor = P3^0;   /* active-low trigger input */

void delay(unsigned int msec) {
    unsigned int i, j;
    for (i = 0; i < msec; i++)
        for (j = 0; j < 1275; j++);
}

void main() {
    while (1) {
        if (sensor == 0) {
            relay = 1;          /* energize relay -> load ON */
            buzzer = 1;          /* sound the buzzer */
            delay(1000);
            buzzer = 0;
        } else {
            relay = 0;

}
    }
}
EXPERIMENT 06

Interfacing LPC2148 with GLCD to Display an Image

#include <lpc214x.h>

#define RS  (1UL << 20)
#define RW  (1UL << 21)
#define EN  (1UL << 22)
#define CS1 (1UL << 23)
#define CS2 (1UL << 24)

extern const unsigned char image[8][128];  /* 128x64 bitmap: 8 pages x 128 columns */

void delay(unsigned int t) { unsigned int i; for (i = 0; i < t; i++); }

void glcdCommand(unsigned char cmd, unsigned long chip) {
    IO1CLR = 0x00FF0000;
    IO1SET = ((unsigned long)cmd << 16);
    IO1CLR = RS;
    IO1CLR = RW;
    IO1SET = chip;

IO1SET = EN; delay(1); IO1CLR = EN;
}

void glcdData(unsigned char data, unsigned long chip) {
    IO1CLR = 0x00FF0000;
    IO1SET = ((unsigned long)data << 16);
    IO1SET = RS;
    IO1CLR = RW;
    IO1SET = chip;
    IO1SET = EN; delay(1); IO1CLR = EN;
}

void glcdInit() {
    IO1DIR = 0x01FF0000;      /* data bus (bits 16-23) + control lines as output */
    glcdCommand(0x3F, CS1);    /* display ON, left half */
    glcdCommand(0x3F, CS2);    /* display ON, right half */
}

void displayImage() {
    unsigned char page, col;
    for (page = 0; page < 8; page++) {
        glcdCommand(0xB8 | page, CS1);
        glcdCommand(0x40, CS1);
        for (col = 0; col < 64; col++)
            glcdData(image[page][col], CS1);

        glcdCommand(0xB8 | page, CS2);
        glcdCommand(0x40, CS2);
        for (col = 64; col < 128; col++)
            glcdData(image[page][col], CS2);
    }
}

int main() {
    glcdInit();
    displayImage();
    while (1);
}
EXPERIMENT 07

Interfacing of 4x4 Matrix Keyboard with LPC2148

#include <lpc214x.h>

unsigned char keymap[4][4] = {
    {'1','2','3','A'},
    {'4','5','6','B'},
    {'7','8','9','C'},
    {'*','0','#','D'}
};

void delay(unsigned int t) { unsigned int i; for (i = 0; i < t; i++); }

unsigned char scanKeypad() {
    unsigned char row, col;
    for (row = 0; row < 4; row++) {
        IO0SET = 0x0F;                       /* rows P0.0-P0.3 high */
        IO0CLR = (1 << row);                  /* drive current row low */
        delay(1000);
        for (col = 0; col < 4; col++) {
            if ((IO0PIN & (1 << (4 + col))) == 0)   /* columns on P0.4-P0.7 */
                return keymap[row][col];
        }
    }
    return 0xFF;   /* no key pressed */
}


int main() {
    unsigned char key;
    IO0DIR |= 0x0F;      /* P0.0-P0.3 rows: output */
    IO0DIR &= ~0xF0;     /* P0.4-P0.7 columns: input */
    while (1) {
        key = scanKeypad();
        if (key != 0xFF) {
            /* display 'key' on LCD/UART as required */
        }
        delay(5000);
    }
}
EXPERIMENT 08

Built-in ADC of LPC2148 โ€” With and Without Interrupt

#include <lpc214x.h>

/* ---------- Polling-based ADC read ---------- */
unsigned int readADC_Polling(unsigned char channel) {
    unsigned int result;
    AD0CR = (1 << channel) | (4 << 8) | (1 << 21) | (1 << 24); /* channel, clkdiv, PDN, start */
    while (!(AD0GDR & 0x80000000));    /* wait for DONE bit */
    result = (AD0GDR >> 6) & 0x3FF;     /* extract 10-bit result */
    return result;
}

/* ---------- Interrupt-based ADC read ---------- */
volatile unsigned int adcResult;
volatile unsigned char dataReady = 0;

void ADC_ISR(void) __irq {
    adcResult = (AD0GDR >> 6) & 0x3FF;
    dataReady = 1;
    AD0CR &= ~(1 << 24);
    VICVectAddr = 0;                     /* acknowledge interrupt */
}

void ADC_InitInterrupt() {
    PINSEL1 |= (1 << 18);                 /* select AD0.1 function on the pin */
    AD0CR = (1 << 1) | (4 << 8) | (1 << 21);
    VICVectAddr8 = (unsigned int)ADC_ISR;
    VICVectCntl8 = 0x20 | 18;             /* ADC interrupt channel */
    VICIntEnable = (1 << 18);
}

int main() {
    unsigned int pollValue;
    PINSEL1 |= (1 << 18);
    ADC_InitInterrupt();
    while (1) {
        pollValue = readADC_Polling(1);    /* poll-based read */

        AD0CR |= (1 << 24);                 /* interrupt-based read */
        while (!dataReady);
        dataReady = 0;
        /* use 'pollValue' and 'adcResult' as required (e.g., display on LCD/UART) */
    }
}
EXPERIMENT 09

Interfacing GPS with LPC2148 (Latitude/Longitude)

#include <lpc214x.h>
#include <string.h>

char gpsBuffer[100];
unsigned char idx = 0;

void UART0_Init() {
    PINSEL0 |= 0x00000005;   /* P0.0 = TXD0, P0.1 = RXD0 */
    U0LCR = 0x83;              /* 8N1, DLAB = 1 */
    U0DLL = 97;                 /* 9600 baud @ 15 MHz PCLK (illustrative) */
    U0DLM = 0;
    U0LCR = 0x03;               /* DLAB = 0 */
}

char UART0_Getch() {
    while (!(U0LSR & 0x01));
    return U0RBR;
}

void readGPSLine() {
    char c;
    idx = 0;

do {
        c = UART0_Getch();
        gpsBuffer[idx++] = c;
    } while (c != '\n' && idx < 99);
    gpsBuffer[idx] = '\0';
}

void extractLatLong(char *sentence, char *lat, char *lon) {
    char *token;
    unsigned char field = 0;
    token = strtok(sentence, ",");
    while (token != NULL) {
        if (field == 2) strcpy(lat, token);   /* latitude field */
        if (field == 4) strcpy(lon, token);   /* longitude field */
        token = strtok(NULL, ",");
        field++;
    }
}

int main() {
    char latitude[15], longitude[15];
    UART0_Init();
    while (1) {
        readGPSLine();
        if (strncmp(gpsBuffer, "$GPGGA", 6) == 0) {
            extractLatLong(gpsBuffer, latitude, longitude);
            /* display 'latitude' and 'longitude' on LCD/second UART */
        }
    }
}
EXPERIMENT 10

Interfacing and Programming of On-Chip RTC

#include <lpc214x.h>

void RTC_Init() {
    CCR = 0x01;         /* enable RTC clock */
    ILR = 0x03;          /* clear pending RTC interrupt flags */

    SEC = 0;
    MIN = 30;
    HOUR = 10;
    DOM = 4;
    MONTH = 9;
    YEAR = 2026;
}

void displayTime() {
    unsigned int hh = HOUR, mm = MIN, ss = SEC;
    /* format hh:mm:ss and display on LCD/UART */
}

int main() {
    RTC_Init();
    while (1) {
        displayTime();
    }
}
EXPERIMENT 11

Interfacing EEPROM to LPC2148 using I2C Protocol

#include <lpc214x.h>

#define EEPROM_ADDR 0xA0

void I2C_Init() {
    PINSEL0 |= 0x50;    /* P0.2 = SCL0, P0.3 = SDA0 */
    I20SCLH = 150;
    I20SCLL = 150;
    I20CONSET = 0x40;    /* enable I2C */
}

void I2C_Start() { I20CONSET = 0x20; while (!(I20CONSET & 0x08)); I20CONCLR = 0x20; }
void I2C_Stop()  { I20CONSET = 0x10; }

void I2C_Write(unsigned char data) {
    I20DAT = data;
    I20CONCLR = 0x08;
    while (!(I20CONSET & 0x08));
}

void EEPROM_WriteByte(unsigned int memAddr, unsigned char data) {
    I2C_Start();

I2C_Write(EEPROM_ADDR);
    I2C_Write((memAddr >> 8) & 0xFF);
    I2C_Write(memAddr & 0xFF);
    I2C_Write(data);
    I2C_Stop();
}

unsigned char EEPROM_ReadByte(unsigned int memAddr) {
    unsigned char data;
    I2C_Start();
    I2C_Write(EEPROM_ADDR);
    I2C_Write((memAddr >> 8) & 0xFF);
    I2C_Write(memAddr & 0xFF);
    I2C_Start();
    I2C_Write(EEPROM_ADDR | 0x01);
    I20CONSET = 0x04;                 /* enable ACK for the incoming byte */
    while (!(I20CONSET & 0x08));
    data = I20DAT;
    I2C_Stop();
    return data;
}

int main() {
    unsigned char value;
    I2C_Init();
    EEPROM_WriteByte(0x0010, 0x55);
    while (1) {
        value = EEPROM_ReadByte(0x0010);
        /* display 'value' โ€” expected 0x55 */
    }
}
EXPERIMENT 12

Interfacing SD Card to LPC2148 using SPI

#include <lpc214x.h>

#define CS_LOW()  (IO0CLR = (1 << 20))
#define CS_HIGH() (IO0SET = (1 << 20))

void SPI_Init() {
    PINSEL0 |= 0x00002A00;   /* SCK0, MISO0, MOSI0 function select */
    IO0DIR |= (1 << 20);      /* CS pin as output */
    S0SPCR = 0x20;             /* SPI master mode */
    S0SPCCR = 8;                /* clock divider */
}

unsigned char SPI_Transfer(unsigned char data) {
    S0SPDR = data;
    while (!(S0SPSR & 0x80));
    return S0SPDR;
}

unsigned char SD_SendCmd(unsigned char cmd, unsigned long arg, unsigned char crc) {
    unsigned char response;
    unsigned char retry = 0;
    SPI_Transfer(0x40 | cmd);
    SPI_Transfer((arg >> 24) & 0xFF);
    SPI_Transfer((arg >> 16) & 0xFF);
    SPI_Transfer((arg >> 8) & 0xFF);
    SPI_Transfer(arg & 0xFF);
    SPI_Transfer(crc);
    do {
        response = SPI_Transfer(0xFF);
    } while ((response & 0x80) && (retry++ < 8));
    return response;
}

unsigned char SD_Init() {
    unsigned char i, r;
    CS_HIGH();
    for (i = 0; i < 10; i++) SPI_Transfer(0xFF);   /* >= 74 clock pulses */
    CS_LOW();
    r = SD_SendCmd(0, 0, 0x95);         /* CMD0: go idle */
    r = SD_SendCmd(8, 0x1AA, 0x87);     /* CMD8: check interface condition */
    do {
        SD_SendCmd(55, 0, 0xFF);          /* CMD55: application command prefix */
        r = SD_SendCmd(41, 0x40000000, 0xFF);   /* ACMD41: initialize */
    } while (r != 0);
    CS_HIGH();
    return r;
}

int main() {
    unsigned char status;
    SPI_Init();
    status = SD_Init();
    while (1) {
        /* status == 0 indicates the card is ready;
           use SD_SendCmd(17, blockAddr, 0xFF) to read / CMD24 to write a block */
    }
}
No matching experiment found.