0% found this document useful (0 votes)
2 views30 pages

Mc All Practical Code and Circuits

The document contains various assembly language codes for data transfer, LED blinking, LCD interfacing, relay control, square wave generation, ADC reading, PWM signal generation, and keypad interfacing using a PIC microcontroller. Each section includes code snippets and comments explaining the functionality of the code. The examples demonstrate practical applications of microcontroller programming for controlling hardware components.

Uploaded by

418 Seema Patil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views30 pages

Mc All Practical Code and Circuits

The document contains various assembly language codes for data transfer, LED blinking, LCD interfacing, relay control, square wave generation, ADC reading, PWM signal generation, and keypad interfacing using a PIC microcontroller. Each section includes code snippets and comments explaining the functionality of the code. The examples demonstrate practical applications of microcontroller programming for controlling hardware components.

Uploaded by

418 Seema Patil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

MC PRACTICAL CODE & CIRCUITS

1) DATA TRANSFER USING ASSEMBLY LANGUAGE (Direct).


CODE 
#include "p18f4520.inc"

ORG 0x00 ; Start at address 0

GOTO START

START:

CLRF 0x20 ; Clear source register (for testing)

MOVLW 0x55 ; Load WREG with test value 0x55

MOVWF 0x20 ; Move WREG to address 0x20 (source)

; Transfer from 0x20 to 0x21

MOVF 0x20, W ; Move contents of 0x20 to WREG

MOVWF 0x21 ; Move WREG to 0x21 (destination)

; Endless loop

GOTO $

END

O/P 
2) DATA TRANSFER USING ASSEMBLY LANGUAGE (Indirect).
CODE 

#include "p18f4520.inc"
COUNTER EQU 0x00
ORG 0x0000
MOVLW D'10'
MOVWF COUNTER
MOVLW 0x02
MOVWF BSR

MOVLW 0xCD
MOVWF 0x10,1
MOVWF 0x11,1
MOVLW 0xBB
MOVWF 0x12,1
MOVWF 0x13,1
MOVLW 0xDD
MOVWF 0x14,1
MOVWF 0x15,1
MOVLW 0xAA
MOVWF 0x16,1
MOVWF 0x17,1
MOVLW 0x88
MOVWF 0x18,1
MOVWF 0x19,1

LFSR 0,0x210
LFSR 1,0x220
AGAIN MOVFF INDF0,INDF1
INCF FSR0L,F,0
INCF FSR1L,F,0
DECFSZ COUNTER,F
GOTO AGAIN

GOTO $
;HERE GOTO HERE
END

O/P 
3) LED BLINK ON GPIO USING (DELAY FUNCTION).
CODE 

#include <xc.h>
#define _XTAL_FREQ 4000000

void main(void) {
OSCCON = 0XEF;
TRISBbits.TRISB0 = 0; // Set RB0 as output
LATBbits.LATB0 = 0; // Start with LED OFF (clear latch)

while (1) {
LATBbits.LATB0 = 1; // Turn LED ON
__delay_ms(500); // Delay 500ms
LATBbits.LATB0 = 0; // Turn LED OFF
__delay_ms(500); // Delay 500ms
}
}

O/P 
4) LED BLINK ON GPIO USING (SWITCH)  {PULL UP RESISTOR}.
CODE 
#include <xc.h>
#define _XTAL_FREQ 4000000
void main(void) {
TRISBbits.TRISB0 = 1; // RB0 as input (switch)
TRISDbits.TRISD0 = 0; // RD0 as output (LED)
// Optional: Clear the LED initially
LATDbits.LATD0 = 0;

while (1) {
if (PORTBbits.RB0 == 0) { // If switch is pressed (active LOW)
LATDbits.LATD0 = 1; // Turn ON LED
} else {
LATDbits.LATD0 = 0; // Turn OFF LED
}
}
}

O/P 
5) INTERFACE LCD DISPLAY ON PORTD.
CODE 
#include <xc.h>
#include <stdio.h>
#include <string.h>

#define _XTAL_FREQ 4000000

#define RS PORTCbits.RC1
#define EN PORTCbits.RC3

float data;
char array[16];

// Function declarations
void lcd_init(void);
void lcd_cmd(unsigned char cmd);
void lcd_data(unsigned char data);
void lcd_string(const char *str);

void lcd_string(const char *str) {


while (*str) {
lcd_data(*str++);
}
}

void lcd_cmd(unsigned char cmd) {


PORTD = cmd;
RS = 0;
__delay_ms(1);
EN = 1;
__delay_ms(1);
EN = 0;
}

void lcd_data(unsigned char data) {


PORTD = data;
RS = 1;
__delay_ms(1);
EN = 1;
__delay_ms(1);
EN = 0;
}

void lcd_init(void) {
lcd_cmd(0x01); // Clear display
__delay_ms(20);
lcd_cmd(0x38); // 8-bit mode, 2 lines, 5x8 font
lcd_cmd(0x0C); // Display ON, cursor OFF
lcd_cmd(0x06); // Entry mode: increment cursor
lcd_cmd(0x80); // Move cursor to beginning
}

void main(void) {
// Set internal oscillator to 4 MHz
OSCCON = 0xEF; // Use 4 MHz internal clock

// Configure ports
TRISB = 0x00; // All PORTB as output (unused here)
TRISC = 0x00; // Control lines (RC1=RS, RC3=EN)
TRISD = 0x00; // Data lines to LCD

// Initialize control lines


EN = 0;
RS = 0;
__delay_ms(10);

lcd_init();

// Display initial messages


lcd_cmd(0x81); // Move to 1st row, 2nd column
lcd_string("RIT IS LOVE");
lcd_cmd(0xC1); // Move to 2nd row, 2nd column
lcd_string("KARATE");
__delay_ms(1000);

// Display float value


data = 563.563;
sprintf(array, "%.2f V", data); // Format float with 2 decimal places and ' V'

lcd_cmd(0x01); // Clear display


lcd_cmd(0x80); // Move to first line
lcd_string(array); // Display formatted string

while(1); // Endless loop


}
O/P 
6) INTERFACE RELAY WITH RELAY MODEL {USING SWITCH – ACTIVE HIGH}
CODE 
#include <xc.h>
#define _XTAL_FREQ 4000000
void main(void) {
TRISBbits.TRISB0 = 1; // RB0 as input (switch)
TRISDbits.TRISD0 = 0; // RD0 as output (relay)
// Optional: Clear the RELAY initially
LATDbits.LATD0 = 0;
while (1) {
if (PORTBbits.RB0 == 1) { // If switch is pressed (active HIGH)
LATDbits.LATD0 = 1; // Turn ON RELAY
} else {
LATDbits.LATD0 = 0; // Turn OFF RELAY
}
}
}

O/P 
7) SQUARE WAVE GENERATOR WITH 1KHz using TMR0
CODE 
#include <xc.h>
#define led1 PORTDbits.RD0
void delay_500us(void);

void main(void)
{
OSCCON = 0xEF; // Internal oscillator 4 MHz
TRISDbits.TRISD0 = 0; // RD0 as output
T0CON = 0x08; // Timer0 ON, 16-bit, internal clock, 1:1 prescaler (TMR0ON set
later)

led1 = 0;
while (1)
{
led1 = ~led1; // Toggle RB0
delay_500us(); // Wait 500us → 1kHz square wave
}
}
// Timer0 delay for ~500us (at 4MHz Fosc)
void delay_500us(void)
{
TMR0H = 0xFE; // Preload high byte (500us delay)
TMR0L = 0x0C; // Preload low byte
INTCONbits.TMR0IF = 0;
T0CONbits.TMR0ON = 1;

while (INTCONbits.TMR0IF == 0); // Wait until overflow


T0CONbits.TMR0ON = 0;
INTCONbits.TMR0IF = 0;
}

O/P  (ON PORT RD0 OSCILLATOR PIN CONNECTED).


8) ADC AND RESULT DISPLAY ON LCD
CODE 
#include <xc.h>
#include <stdio.h>
#include <string.h>

#define _XTAL_FREQ 4000000

// LCD control pins


#define en PORTCbits.RC3
#define rs PORTCbits.RC1

// Function Prototypes
void lcd_string(const char *str);
void lcd_init(void);
void lcd_cmd(unsigned char cmd);
void lcd_data(unsigned char data);
unsigned int read_adc(void);

// LCD string function


void lcd_string(const char *str) {
while(*str) {
lcd_data(*str++);
}
}

// LCD command function


void lcd_cmd(unsigned char cmd) {
PORTD = cmd;
rs = 0;
__delay_ms(1);
en = 1;
__delay_ms(1);
en = 0;
}

// LCD data write function


void lcd_data(unsigned char data) {
PORTD = data;
rs = 1;
__delay_ms(1);
en = 1;
__delay_ms(1);
en = 0;
}

// LCD initialization
void lcd_init(void) {
lcd_cmd(0x01); // Clear display
__delay_ms(100);
lcd_cmd(0x38); // 8-bit mode, 2 lines
__delay_ms(50);
lcd_cmd(0x0E); // Display on, cursor on
__delay_ms(50);
lcd_cmd(0x06); // Entry mode
__delay_ms(50);
lcd_cmd(0x80); // Set cursor to first line
}
// ADC read function
unsigned int read_adc(void) {
unsigned int adc_val;

ADCON0bits.GO = 1; // Start conversion


while (ADCON0bits.GO); // Wait for completion

adc_val = (ADRESH << 8) | ADRESL; // Combine high and low bytes


return adc_val;
}

// Main function
void main(void) {
char lcd_buffer[16];
unsigned int adc_value;

// Oscillator: Internal 4 MHz


OSCCON = 0xEF;

// ADC configuration
ADCON0 = 0x01; // Enable ADC, channel 0 (AN0)
ADCON1 = 0x0E; // Configure AN0 as analog, rest as digital
ADCON2 = 0xA5; // Right justified, 8TAD, Fosc/16

TRISAbits.TRISA0 = 1; // RA0/AN0 as input


TRISC = 0x00; // LCD control port
TRISD = 0x00; // LCD data port
__delay_ms(400);

lcd_init();
lcd_string("I LOVE RIT");
__delay_ms(2000);
lcd_cmd(0x01); // Clear screen
lcd_cmd(0x80);
lcd_string("ADC Result");

while (1) {
adc_value = read_adc(); // Read ADC value
sprintf(lcd_buffer, "Val: %04u", adc_value); // Format with 4 digits
lcd_cmd(0xC0);
__delay_ms(500);
lcd_string(lcd_buffer); // Display value
__delay_ms(1000);
}
}

O/P 
9) PWM SIGNAL GENERATOR USING TMR2 {FOR DC MOTOR}.
CODE 
#include <xc.h>
#define _XTAL_FREQ 4000000
void atod(void); // Function prototype
void main(void)
{
OSCCON = 0xEF; // Set internal oscillator to 4 MHz
// ADC configuration
ADCON0 = 0x01; // Channel AN0 selected, ADC enabled
ADCON1 = 0x0E; // AN0 analog, others digital
ADCON2 = 0x11; // left justify, 2 TAD, Fosc/8

TRISAbits.TRISA0 = 1; // Set RA0/AN0 as input


TRISC = 0x00; // PORTC as output (RC2 = CCP1)

// PWM configuration
CCP1CON = 0x0C; // PWM mode on CCP1
PR2 = 124; // PWM period for ~1kHz at 4MHz
T2CON = 0x03; // Prescaler = 16
TMR2ON=1; // Timer 2 ON
while (!PIR1bits.TMR2IF); // Wait for Timer2 to overflow once
unsigned char tmp;
while (1)
{
atod(); // Start and wait for ADC conversion
CCPR1L = ADRESH; // Load upper 8 bits into PWM duty register

tmp = ADRESL; // Get lower 2 bits


tmp = tmp >> 2; // Align to DC1B1:DC1B0 position (bits 6:5)
CCP1CON = (CCP1CON & 0xCF) | (tmp << 4); // Mask and set DC1B bits

__delay_ms(1000); // Conversion delay


}
}
// ADC Conversion
void atod(void)
{
ADCON0bits.GO = 1; // Start ADC conversion
while (ADCON0bits.GO); // Wait for conversion to complete
}

O/P 
10) 4X3 KEYPAD INTERFACE AND DISPLAY KEYPRESSED ON LCD – {ACTIVE LOW}
CODE 
#include <xc.h>
#include <string.h>

#define _XTAL_FREQ 4000000

// Define keypad column and row pins


#define C1 PORTBbits.RB0
#define C2 PORTBbits.RB1
#define C3 PORTBbits.RB2

#define R1 PORTBbits.RB4
#define R2 PORTBbits.RB5
#define R3 PORTBbits.RB6
#define R4 PORTBbits.RB7

// LCD control pins


#define en PORTCbits.RC3
#define rs PORTCbits.RC1

// Function declarations
void lcd_string(unsigned char *S);
void lcdinit(void);
void lcdcmd(unsigned char data);
void lcddata(unsigned char data);

//////////////////////////////////////////////////////////////////////
void lcd_string(unsigned char *S)
{
while(*S)
{
lcddata(*S++);
}
}

////////////////////////////////////////////////////////////////
void lcdcmd(unsigned char command)
{
PORTD = command;
rs = 0;
en = 1;
__delay_ms(1);
en = 0;
}

////////////////////////////////////////////////////////////////
void lcddata(unsigned char data)
{
PORTD = data;
rs = 1;
en = 1;
__delay_ms(1);
en = 0;
}

////////////////////////////////////////////////////////////////
void lcdinit(void)
{
lcdcmd(0x01);
__delay_ms(80);
lcdcmd(0x38);
__delay_ms(10);
lcdcmd(0x0C);
__delay_ms(10);
lcdcmd(0x06);
__delay_ms(10);
lcdcmd(0x80);
}

////////////////////////////////////////////////////////////////
void main(void)
{
INTCON2bits.RBPU = 0; // Internal PULL UP Resistor (when no external pull up
resistor are connected)
OSCCON = 0xEF; // Internal oscillator 4 MHz
ADCON1 = 0x0F; // Configure all pins as digital
TRISD = 0x00; // LCD data port as output
TRISB = 0x0F; // RB0–RB3 inputs (C1–C3), RB4–RB7 outputs (R1–R4)
TRISC = 0x00; // LCD control port as output

PORTD = 0x00;
PORTC = 0x00;

lcdinit();
lcdcmd(0x80);
lcd_string("I LOVE RIT");

while(1)
{
// scan row1
R1 = 0; R2 = R3 = R4 = 1;
if(C1 == 0) { lcdcmd(0xC0); lcddata('1'); }
if(C2 == 0) { lcdcmd(0xC0); lcddata('2'); }
if(C3 == 0) { lcdcmd(0xC0); lcddata('3'); }

// scan row2
R2 = 0; R1 = R3 = R4 = 1;
if(C1 == 0) { lcdcmd(0xC0); lcddata('4'); }
if(C2 == 0) { lcdcmd(0xC0); lcddata('5'); }
if(C3 == 0) { lcdcmd(0xC0); lcddata('6'); }

// scan row3
R3 = 0; R1 = R2 = R4 = 1;
if(C1 == 0) { lcdcmd(0xC0); lcddata('7'); }
if(C2 == 0) { lcdcmd(0xC0); lcddata('8'); }
if(C3 == 0) { lcdcmd(0xC0); lcddata('9'); }

// scan row4
R4 = 0; R1 = R2 = R3 = 1;
if(C1 == 0) { lcdcmd(0xC0); lcddata('*'); }
if(C2 == 0) { lcdcmd(0xC0); lcddata('0'); }
if(C3 == 0) { lcdcmd(0xC0); lcddata('#'); }

__delay_ms(300); // Simple debounce


}
}
O/P 
11) STEPPER MOTOR INTERFACE USING {ULN2003 CURRENT DRIVER}
CODE  (FULL STEP – {CLOCK & ANTI-CLOCK})
#include <xc.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define IN1 PORTBbits.RB0


#define IN2 PORTBbits.RB1
#define IN3 PORTBbits.RB2
#define IN4 PORTBbits.RB3

#define _XTAL_FREQ 4000000

// Function prototypes
void rotate_CW(unsigned int steps);
void rotate_CCW(unsigned int steps);

void main(void)
{
OSCCON = 0xEF; // Internal 4 MHz
ADCON1 = 0x0F; // All digital
TRISB = 0x00;
PORTB = 0x00;

unsigned int steps_cw = 100; // X degrees = 100 * 1.8 = 180°


unsigned int steps_ccw = 50; // Y degrees = 50 * 1.8 = 90°

rotate_CW(steps_cw); // Rotate CW by X degrees


__delay_ms(1000); // Wait
rotate_CCW(steps_ccw); // Rotate CCW by Y degrees

while(1); // Infinite loop


}

// Full-step clockwise sequence


void rotate_CW(unsigned int steps) {
for(unsigned int i = 0; i < steps; i++) {
IN1 = 1; IN2 = 0; IN3 = 0; IN4 = 1; __delay_ms(1000);
IN1 = 1; IN2 = 1; IN3 = 0; IN4 = 0; __delay_ms(1000);
IN1 = 0; IN2 = 1; IN3 = 1; IN4 = 0; __delay_ms(1000);
IN1 = 0; IN2 = 0; IN3 = 1; IN4 = 1; __delay_ms(1000);
}
}

// Full-step counterclockwise sequence


void rotate_CCW(unsigned int steps) {
for(unsigned int i = 0; i < steps; i++) {
IN1 = 0; IN2 = 0; IN3 = 1; IN4 = 1; __delay_ms(1000);
IN1 = 0; IN2 = 1; IN3 = 1; IN4 = 0; __delay_ms(1000);
IN1 = 1; IN2 = 1; IN3 = 0; IN4 = 0; __delay_ms(1000);
IN1 = 1; IN2 = 0; IN3 = 0; IN4 = 1; __delay_ms(1000);
}
}

IMP 
THIS 1.8 DEG IS FOR ONLY PROTEUS STEPPER MOTOR !!!

FOR {28BJY-48} STEPPER MOTOR [Stride angle: 5.625º/64], (360º/5.625º = 64


steps)
The 28BYJ-48 stepper motor comprises four coils in total. These coils are
linked at one end to the motor’s red wire, connected to a 12V source. The
opposite ends of these coils correspond to wires tinted in blue, pink, yellow,
and orange. Activating the coils sequentially generates a step-by-step motion
of the motor in either direction.
THIS CALCULATION FOR 45 Deg. WHEN YOU WHAT 180 OR 360 SIMPLY PUT VALUE
O/P 

12) FOR HALF STEP SEQUENCE CODE ONLY CLOCK-WISE


CODE 
#include <xc.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define IN1 PORTBbits.RB0


#define IN2 PORTBbits.RB1
#define IN3 PORTBbits.RB2
#define IN4 PORTBbits.RB3

#define _XTAL_FREQ 4000000


void main(void)
{
OSCCON = 0xEF; // 4 MHz internal oscillator
ADCON1 = 0x0F; // All pins digital
TRISB = 0x00; // PORTB as output
PORTB = 0x00; // Clear PORTB

unsigned char j;

while(1)
{
for(j = 0; j < 330; j++) // Half-step anticlockwise
{
// Step 1
IN1=1; IN2=1; IN3=0; IN4=0;
__delay_ms(500);

// Step 2
IN1=1; IN2=0; IN3=0; IN4=0;
__delay_ms(500);

// Step 3
IN1=0; IN2=1; IN3=1; IN4=0;
__delay_ms(500);

// Step 4
IN1=0; IN2=1; IN3=0; IN4=0;
__delay_ms(500);

// Step 5
IN1=0; IN2=0; IN3=1; IN4=1;
__delay_ms(500);

// Step 6
IN1=0; IN2=0; IN3=1; IN4=0;
__delay_ms(500);

// Step 7
IN1=1; IN2=; IN3=0; IN4=1;
__delay_ms(500);

// Step 8
IN1=0; IN2=0; IN3=0; IN4=1;
__delay_ms(500);
}
}
}

THIS ALL INFORMATION ARE COLLECTED FROM  [DEEPSEEK], [CHATGPT],


[GOOGLE], [MICROCHIP (Datasheet)] & [60% HUMAN BRAIN].

THANKS FOR READING…

You might also like