Space In HIndi

 


Pages

Popular

Pic Microcontroller programming in c using mplab or using keil software


EXPT.. write a program for interfacing button, LED, relay & buzzer with PIC18F as follows A. when button 1 is pressed relay and buzzer is turned ON and LED’s start chasing from left to right. B. when button 2 is pressed relay and buzzer is turned OFF and LED’s start chasing from right to left.
//////////////////////////////////////////////////////PROGRAM //////////////////////////////////////////////////////////////
#include <p18f4550.h>
#pragma config FOSC = HS         // High-speed oscillator;20MHz
#pragma config  WDT = OFF        // Watchdog Timer disabled
#pragma config LVP = OFF        // Low-voltage Programming disabled
#pragma config PBADEN = OFF
#define LEDS PORTD
#define BUZZER PORTAbits.RA3
#define BUTTON1 PORTBbits.RB0   // SW1 Switch
#define BUTTON2 PORTBbits.RB1  // SW2 Switch
void msdelay (unsigned int itime);
unsigned int flag=0;
void main()
{ TRISB=0XFF; //input port
TRISA=0X00; // output port or (TRISAbits.TRISA3=0;)
  TRISD = 0x00; // output port
PORTD=0xFF; // all LEDs off
while(1)
{ if(BUTTON1==0)//checking if the button is pressed or not
flag=1;
if(BUTTON2==0)
flag=0;
if(flag==1)
{ BUZZER=1; // buzzer on
                                                  LEDS=0x00; //leds ON
msdelay(550);
}
      if (flag==0)
{ BUZZER=0;
    LEDS=0xFF; // leds off
                msdelay(550);
          }
}
}
void msdelay (unsigned int itime)
{ int i, j;
for(i=0; i< itime; i++)
for(j=0;j<135;j++);
}



EXPT.. Generate square wave using timer with interrupt using PIC18F.
//////////////////////////////////////////////////////PROGRAM//////////////////////////
//Expt.No.4: Generation of Square wave using timer interrupt
//Includes
#include <p18f4550.h>
//Configration bits setting/
#pragma config FOSC = HS //Oscillator Selection
#pragma config WDT = OFF //Disable Watchdog timer
#pragma config LVP = OFF //Disable Low Voltage Programming
#pragma config PBADEN = OFF //Disable PORTB Analog inputs


//Timer ISR Function Prototype
void timer_isr(void);

// Timer ISR Definition
#pragma interrupt timer_isr
void timer_isr(void)
{
TMR0H = 0X6D;          //Reload the timer values after overflow
TMR0L = 0X82;
PORTBbits.RB0 = ~PORTBbits.RB0; //Toggle the RB0
  INTCONbits.TMR0IF = 0; //Reset the timer overflow interrupt flag
}

//Interrupt Vector for Timer0 Interrupt
#pragma code _HIGH_INTERRUPT_VECTOR = 0x0008
void high_ISR (void)
{
_asm 
goto timer_isr //The program is relocated to execute the ISR
_endasm    
}
#pragma code

// Start of main Program
void main()
{
ADCON1 = 0x0F;        //Configuring the PORTE pins as digital I/O 
TRISBbits.TRISB0 = 0; //Configruing the RB0 as output
PORTBbits.RB0 = 0;    //Setting the initial value
T0CON = 0x02;        //Set the timer to 16-bit,Fosc/4,1:16 prescaler
  TMR0H = 0x85;         //load timer value to genearate delay 50ms
  TMR0L = 0xEE;
    INTCONbits.TMR0IF = 0;// Clear Timer0 overflow flag
INTCONbits.TMR0IE = 1;// TMR0 interrupt enabled
  T0CONbits.TMR0ON = 1; // Start timer0
INTCONbits.GIE = 1;   // Global interrupt enabled

while(1);             //Program execution stays here until the timer overflow interrupt is generated
}

EXPT.. Interfacing serial port with PC using PIC18F.
//////////////////////////////////////////////////////PROGRAM////////////////////
//Expt.No.5 - Serial Communication with PC

#include <p18f4550.h>

//Function Prorotypes
void TXbyte(); //To transmit single character
void msdelay (unsigned int itime);
//Start of Main Program
void main()
{
 TRISCbits.TRISC6=0; // TXD line as output
 SPBRG = 0x07;
 SPBRGH = 0x02;      // 0x04E1 for 9600 baud 
 TXSTA = 0x24;       // TX enable BRGH=1
 RCSTA = 0x90;       // continuous RX
 BAUDCON = 0x08;     // BRG16 = 1
 TXbyte();
 while(1); //loop forever
}

void TXbyte()
{   unsigned int i=0; 
    while(TXSTAbits.TRMT==0); //wait till transmit buffer is not empty
    for (i=0;i<2;i++)
{
    TXREG = 'S';
    msdelay(550);
TXREG = 'R';
    msdelay(550);
// Transmit Data
}
}
void msdelay (unsigned int itime)
{ int i, j;
for(i=0; i< itime; i++)
for(j=0;j<135;j++);
}


EXPT:: Interfacing DC Motor with PIC18F to Generate PWM signal.
//**************************************************************
// Program for PWM Generation using PIC18F4550. 
// PWM output  on:       RC2
//**************************************************************
#include <p18f4550.h>

#pragma config FOSC = HS //Oscillator Selection
#pragma config WDT = OFF //Disable Watchdog timer
#pragma config LVP = OFF //Disable Low Voltage Programming
#pragma config PBADEN = OFF //Disable PORTB Analog inputs

void MsDelay (unsigned int time)  // Definition of delay subroutine
{
unsigned int i, j;
for (i = 0; i < time; i++)// Loop for itime
for (j = 0; j < 710; j++); // Calibrated for a 1 ms delay in MPLAB
}
void main()
TRISCbits.TRISC2 = 0 ;// Set PORTC, RC2 as output (CCP1)
TRISCbits.TRISC6 = 0 ;// Set PORTC, RC6 as output (DCM IN3)
TRISCbits.TRISC7 = 0 ;// Set PORTC, RC6 as output (DCM IN4)
PR2 = 0x4E;             // set PWM Frequency 4KHz  
CCP1CON = 0x0C;         // Configure CCP1CON as PWM mode.
T2CON = 0x07; //Start timer 2 with prescaler 1:16
PORTCbits.RC6 = 1; // Turn ON the Motor
PORTCbits.RC7 = 0; 
  while(1)// Endless Loop
{
-------------//-------Duty Cycle 80%-------//----
CCP1CONbits.DC1B0 = 0;
CCP1CONbits.DC1B1 = 1;
CCPR1L = 0x3E;
MsDelay(2000);
///--------Duty Cycle 60%------/////-----
CCP1CONbits.DC1B0 = 1;
CCP1CONbits.DC1B1 = 1;
CCPR1L = 0x2E;
MsDelay(2000);
/////// ----------Duty Cycle 40%-----/////------
CCP1CONbits.DC1B0 = 1;
CCP1CONbits.DC1B1 = 0;
CCPR1L = 0x1F;
MsDelay(2000);
//---------Duty Cycle 20%----//-------
CCP1CONbits.DC1B0 = 0;
CCP1CONbits.DC1B1 = 1;
CCPR1L = 0x0F;
MsDelay(2000);
}   
}

No comments: