CODES

 

Experiment No.1

LED FLASHING

 

#include<reg51.h>

#define Del 3000

sfr LED_PORT2=0xA0;       

 

void delay(unsigned int x)           

{

     unsigned int i,j;

     for(i=0;i<=x;i++)    

       for(j=0;j<=100;j++);    

               

}

 

void main(void)

{

     while(1)                  

           {

                LED_PORT2=0xff;           

                delay(Del);

                LED_PORT2=0x00;      

                delay(Del);

           }

}

 

Experiment No.1

//BCD Counter on PORT 1

BCD COUNTER

#include<reg51.h>

#define Del 2000

void delay(unsigned int x)            // delay function

{

     unsigned int i,j;

     for(i=0;i<x;i++)

                for(j=0;j<=100;j++); 

}

void main(void)

{

unsigned char count[10]={0xff,0xfe,0xfd,0xfc,0xfb,0xfa,0xf9,0xf8,0xf7,0xf6};

unsigned int x;

     P1=0x00;       

     while(1)  

           {

                for(x=0;x<10;x++)

                     {

                           P1=count[x];

                           delay(Del);

                     }

           }

}

 

 

Experiment No.1

Binary Counter

#include<reg51.h>

#define Del 2000

 

 

void delay(unsigned int x)            // delay function

{

     unsigned int i,j;

     for(i=0;i<x;i++)

                for(j=0;j<=100;j++); 

}

 

void main(void)

{

unsigned char count[16]={0xff,0xfe,0xfd,0xfc,0xfb,0xfa,0xf9,0xf8,0xf7,0xf6,0xf5,0xf4,0xf3,0xf2,0xf1,0xf0};

    

unsigned int x;

    

     P1=0x00;        // Make P1 as output port

    

     while(1)   // do it continuosly

           {

                for(x=0;x<10;x++)

                     {

                           P1=count[x];

                           delay(Del);

                     }

           }

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Experiment No.2

Seven Segment Display

 

#include<reg51.h>

#define del 500

 

void delay(unsigned int x)           

{

     unsigned int i,j;

     for(i=0;i<x;i++)

          for(j=0;j<=100;j++); 

}

 

void main(void)

{

while(1)                    

          

{

     P2=0xEB;

     P3=0x7F;

     delay(del);          

 

     P2=0x4C;

     P3=0xBF;

     delay(del);          

     P2=0x49;

     P3=0xDF;

     delay(del);          

     P2=0x2B;

     P3=0xEF;

     delay(del);          

     }

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Experiment No.3

 

//Squarewave using DAC with timer delay

#include <reg51.h>

void delay();

 

 

void main()

{

while(1)

{

     P2=0x00;

     delay();

     P2=0xFF;

     delay();

}

}

 

void delay()      //20ms delay

{

TMOD=0x01; // timer 0 ,mode 1(16 bit)

TL0=0x3E;// load TL0

TH0=0xB8;

TR0=1;

while(TF0==0);// wait for TF0 to roll over

TR0=0;

TF0=0;

}

 

 

// Triangular waveform generation

#include< reg51.h>

 

unsigned char d;

void main(void)

{

while(1)

    {

for(d=0; d<255; d++)

        {            P2 = d;

        }

for(d=255; d>0; d--)

        {

            P2 = d;

        }

    }

}

 

 

 

 

Experiment No.4

//Stepper motor Anticlockwise

#include <reg51.h>

void delay(unsigned char del);

void main()

{

     while(1)

     {

     P0=0x0a;

     delay(10);

     P0=0x09;

     delay(10);

     P0=0x05;

     delay(10);

     P0=0x06;

     delay(10);

    

     }

}

void delay(unsigned char del)

{

     unsigned int i,j;

     for(i=0;i<1275;i++)

      for(j=0;j<del;j++); 

}

 

Experiment No.4

//Stepper motor Clockwise

#include <reg51.h>

void delay(unsigned char del);

void main()

{

     while(1)

     {

     P0=0x06;

     delay(10);

     P0=0x05;

     delay(10);

     P0=0x09;

     delay(10);

     P0=0x0a;

     delay(10);

    

     }

}

void delay(unsigned char del)

{

     unsigned int i,j;

     for(i=0;i<1275;i++)

      for(j=0;j<del;j++);

}

 

Experiment No.5

 

RAM-RAM

 

           ORG 0000H

    

           MOV R0, #20H

           MOV R1, #40H

           MOV R2, #05H

 

BACK: MOV A, @R0

           MOV @R1, A

           INC R0

           INC R1

           DJNZ R2, BACK

           END

          

 

RAM-EX

     ORG 0000H

    

           MOV R0, #20H

           MOV DPTR, #040H

           MOV R2, #05H

 

BACK: MOV A, @R0

           MOVX @DPTR, A

           INC R0

           INC DPTR

           DJNZ R2, BACK

           END

 

ROM-RAM

 

     ORG 0000H

    

           MOV R0, #20H

           MOV R1, #05H

           MOV DPTR, #0234H

          

BACK: CLR A

           MOVC A, @A+DPTR

           MOV @R0, A

           INC R0

           INC DPTR

           DJNZ R1, BACK

          

           ORG 0234H

           DB 2H, 4H, 6H, 8H, 10H

           END

 

 

Exp 6 :

Led buzzer relay interfacing

#include <stdio.h>

#include <stdlib.h>

#include<PIC18F4550.h>

void delay()

{

    unsigned int j;

for(j=0; j<30000; j++)

  {

    }

}

void main()

 

{

   unsigned int key, i;

key=0;

 TRISB=0x00;

 TRISDbits.RD2=0;

 TRISAbits.RA4=0; 

 TRISDbits.RD0=1;

 TRISDbits.RD1=1;

   while(1)

  {   

if(PORTDbits.RD0==0)    

 {

key=0;

    }

     if(PORTDbits.RD1==0)

  {

            key=1;

        }

        if(key==0)

        {

            LATDbits.LATD2=0;

            LATAbits.LATA4=0;

            for(i=0;i<8;i++)

            {

                LATB=0x01<<i;

                delay();

            }

        }

        if(key==1)

        {

            LATDbits.LATD2=1;

            LATAbits.LATA4=1;

            for(i=0;i<8;i++)

            {

                LATB=0x80>>i;

                delay();

            }

        }

    }

 

Exp 7 
LCD 8 bit
#include <stdio.h>
#include <stdlib.h>
#include<pic18f4550.h>
#define RS LATCbits.LATC0
#define E LATCbits.LATC1
#define LCDPORT LATB
void delay()
{
    for(int i=0; i<30000; i++)
    {
    }
}
void sendCommand(unsigned char command)
{
    LCDPORT=command;
    RS=0;
    delay();
    E=1;
    delay();
    E=0;
    delay();
}
void sendData(unsigned char data)
{
    LCDPORT=data;
    RS=1;
    delay();
    E=1;
    delay();
    E=0;
    delay();
}
 
void main()
{
    TRISB=0x00;
    TRISCbits.RC0=0;
    TRISCbits.RC1=0;
    sendCommand(0x38);
    sendCommand(0x01);
    sendCommand(0x0F);
    sendCommand(0x06);
    sendCommand(0x80);
    sendData('E');
    sendData('&');
    sendData('T');
    sendData('C');
    sendData(' ');
    sendData('S');
    sendData('C');
    sendData('O');
    sendData('E');
    sendCommand(0xC0);
    sendData('P');
    sendData('U');
    sendData('N');
    sendData('E');
    sendData('-');
    sendData('4');
    sendData('1');
}

 

LCD wit Keypad

#include <stdio.h>
#include <stdlib.h>
#include<pic18f4550.h>
#define RS LATCbits.LATC0
#define E LATCbits.LATC1
#define LCDPORT LATB
#define R1 LATDbits.LATD0
#define R2 LATDbits.LATD1
#define R3 LATDbits.LATD2
#define R4 LATDbits.LATD3
#define C1 PORTDbits.RD4
#define C2 PORTDbits.RD5
#define C3 PORTDbits.RD6
#define C4 PORTDbits.RD7
void delay()
{
   for(int i=0; i<30000; i++)
   {   }
}
void sendCommand(unsigned char command)
{
LCDPORT=command;
RS=0;
delay(); 
 E=1;   
 delay();   
 E=0;    
delay();
}
void sendData(unsigned char data)
{  
  LCDPORT=data;    
RS=1;   
 delay();  
  E=1;   
 delay();  
  E=0;   
 delay();
 
}
unsigned char keypad_read(void)
{
        
R1 = 0; 
R2 = 1; 
R3 = 1; 
R4 = 1; 
delay();       
if (C1 == 0) return '1';             
if (C2 == 0) return '2';                     
if (C3 == 0) return '3';                     
if (C4 == 0) return 'A';                     
R1 = 1;                
R2 = 0; 
R3 = 1; 
R4 = 1; 
delay();       
if (C1 == 0) return '4';     
if (C2 == 0) return '5';     
if (C3 == 0) return '6';
if (C4 == 0) return 'B';
R1 = 1;        
R2 = 1;
R3 = 0;
R4 = 1;
        delay();
if (C1 == 0) return '7';
if (C2 == 0) return '8';             
if (C3 == 0) return '9';             
if (C4 == 0) return 'C';             
        R1 = 1;       
R2 = 1;
        R3 = 1
        R4 = 0;
delay();
if (C1 == 0) return '0';             
if (C2 == 0) return 'D';             
if (C3 == 0) return 'E';             
if (C4 == 0) return 'F';             
return 0xFF;                          
}
unsigned char keypad_wait()
{ 
unsigned char key_press = 0xFF       
do       
{       
        key_press = keypad_read();
        }
        while (key_press == 0xFF);
       while (keypad_read() != 0xFF);
        return key_press;
}
void main()
{
TRISB=0x00;
TRISD=0xF0;   
TRISCbits.RC0=0;
    
TRISCbits.RC1=0;
unsigned char key_get;
sendCommand(0x38); 
sendCommand(0x01);  
sendCommand(0x0F); 
sendCommand(0x06);
sendCommand(0x80);
  while(1)
  {  
   key_get = keypad_wait();
     sendData(key_get);
   }
}

 


Timer interupt
#include<p18f4550.h>
void timerInit(void)
{
    T0CON = 0b00000111;      
    TMR0H = 0xED;
    TMR0L = 0xB0;
}
void Interrupt_Init(void)
{
    IPEN = 1;
    INTCON = 0b11100000;     
    TMR0IP = 0;
}
void interrupt low_priority timerinterrupt(void)
{
    if(TMR0IF == 1)          
    {
      TMR0ON = 0;            
      TMR0IF = 0;            
      LATB =~LATB;           
      TMR0H = 0xED;          
      TMR0L = 0xB0;
      TMR0ON = 1;                           
    }
}
void main(void)
{
   TRISB = 0x00;
   LATB = 0xFF;
   Interrupt_Init();
   timerInit();
   TMR0ON = 1;                            
   while(1);                              
}

 


UART

 

#include<p18F4550.h>
#include<stdio.h>
void InitUART()
{
  TRISCbits.RC6 = 0;
  TRISCbits.RC7 = 1;                        
  SPBRG = 77;
  TXSTA = 0b00100000;                       
  RCSTA = 0b10010000;
}
void SendChar(unsigned char data)
{
    while(TXIF == 0);             
    TXREG = data;                 
}
unsigned char GetChar(void)
{
    while(!RCIF);                 
    return RCREG;                 
}
void main(void)
{
    unsigned char key;
    InitUART();
    while(1)
    {
        key=GetChar();
        SendChar(key);
    }
}

 

 

ADC

#include<stdio.h>
#include <p18f4550.h>
#define RS LATCbits.LATC0
#define E LATCbits.LATC1
#define LCDPORT LATB
void delay()
{
    unsigned int i;
    for(i=0;i<30000;i++)
    {
    }
}
void sendCommand(unsigned char command)
{
    LCDPORT=command;
    delay();
    RS=0;
    delay();
    E=1;
    delay();
    E=0;
    delay();
}
void sendData(unsigned char data)
{
    LCDPORT=data;
    delay();
    RS=1;
    delay();
    E=1;
    delay();
    E=0;
    delay();
}
void InitLCD(void)
{
    sendCommand(0x38);
    sendCommand(0x01);
    sendCommand(0x0F);
    sendCommand(0x06);
}
void ADCInit()
{
    TRISEbits.RE2 = 1;  
        ADCON0 = 0b00011101;  
    ADCON1 = 0b00000111;
    ADCON2 = 0b10101110;
}
unsigned short Read_ADC()
{ 
    GODONE = 1;         
    while(GODONE == 1 );
    return ADRES;       
}
void DisplayResult(unsigned short ADCVal)
{
    unsigned char i,text[16];
    unsigned short tempv;
    tempv = ADCVal;
 
    ADCVal = (5500/1024)*tempv;            
    sprintf(text,"%04dmv",ADCVal);         
    sendCommand(0x80);
    for(i=0;i<6;i++)                       
    {
       sendData(text[i]);
    }
    sendCommand(0xC0);                     
    for(i=0;i<10;i++)
    {
        if(tempv & 0x200)
        {
            sendData('1');
        }
        else
        {
            sendData('0');
        }
        tempv=tempv<<1;
    }
}
void main()
{
    unsigned short Ch_result;
    TRISB = 0x00; 
    TRISCbits.RC0 = 0;
    TRISCbits.RC1 = 0;
    ADCInit();
    InitLCD();   
    while(1)
    {        
        Ch_result = Read_ADC();
        DisplayResult(Ch_result);
        delay();
        delay();
    }
}

 

Comments