Search

Saturday, March 5, 2016

ADC Interrupt in PIC18

Program:

//Author : Palak Patel
//Contact No:9173211683
//Title:usart transmission and receiver
//Platform: PIC18f4520
//Software:MPLAB

//Configuration Bits
//**********************************************************************************
#include<p18f4520.h>
//#pragma config OSC = INTIO67
//#pragma config FCMEN = OFF
//#pragma config IESO = OFF
//#pragma config PWRT = OFF
//#pragma config BOREN = OFF
//#pragma config WDT = OFF
//#pragma config MCLRE = ON
//#pragma config PBADEN = OFF
//#pragma config STVREN = OFF
//#pragma config LVP = OFF
//Includes
//**********************************************************************************
static volatile int x,y,c=0x100,z, result;

void ADC_Conversion(unsigned int ADC_Value);

#define LCD PORTC
#define Stepper PORTD
void LCD_Ini();
void LCD_cmd(unsigned char);
void LCD_dat(unsigned char);
void LCD_Display(unsigned char lcd_pass[]);
void Delay_ms(unsigned int);
void Stepper_Motor(unsigned char);

//start ISR code
#pragma code isr = 0x08                                  // store the below code at address 0x08
#pragma interrupt isr                                       // let the compiler know that the function isr() is an interrupt handler
void isr(void)
{
if(PIR1bits.ADIF == 1)
  {
    PIR1bits.ADIF = 0;
    ADCON0bits.GO_DONE = 0;
    z = ADRESL + (ADRESH * c);
ADC_Conversion( z );
       }
}

void main(void)
{
      unsigned char name[] = "Palak Patel";
TRISA = 0xFF;                
       TRISC = 0x00;              
TRISD = 0x00;
    LCD_Ini();
ADCON1 = 0x0E;
       ADCON2 = 0xBE;    
    ADCON0 = 0x01;    
 
       LCD_cmd(0x80);
       LCD_Display(name);
    Delay_ms(2000);

RCONbits.IPEN   = 0;                 // Disable interrupt priority
    INTCONbits.GIE  = 1;                 // Global enable interrupts
INTCONbits.PEIE = 1;                // enable peripheral interrupts.
       PIR1bits.ADIF = 1;               //An A/D conversion completed (must be cleared in software)
    PIE1bits.ADIE = 1;               //Enables the A/D interrupt
 
    //ADCON0bits.GO_DONE = 1;
    while(1)
    {
 
    }
}

void ADC_Conversion(unsigned int ADC_Value)
{
unsigned char Adc_data[5];
LCD_cmd(0xc0);
  Adc_data[0] = (ADC_Value/1000) % 10;
LCD_dat(48 + Adc_data[0]);
    Adc_data[1] = (ADC_Value/100) % 10;
LCD_dat(48 + Adc_data[1]);
        Adc_data[2] = (ADC_Value/10) % 10;
LCD_dat(48 + Adc_data[2]);
         Adc_data[3] = ADC_Value % 10;
LCD_dat(48 + Adc_data[3]);
ADCON0bits.GO_DONE = 1;
}

void LCD_Ini()
{
LCD_cmd(0x01);                          //Clear Lcd
       LCD_cmd(0x02);                          //4 Bit Specification
       LCD_cmd(0x28);                          //4 Bit specification
       LCD_cmd(0x0c);
       LCD_cmd(0x80);                          //Cursor starting point
       LCD_cmd(0X06);
}

void LCD_cmd(unsigned char k)
{
        LCD &= 0x0F;
        LCD |= (k & (0xF0));
        LCD |= (2<<0);
        Delay_ms(2);
        LCD = LCD & 0xFC;
        Delay_ms(20);

        LCD &= 0x0F;
        LCD |= ((k<<4) & (0xF0));
        LCD |= (2<<0);
        Delay_ms(20);
        LCD=LCD & 0xFC;
        Delay_ms(20);
}
void LCD_dat(unsigned char k)
{
        LCD &= 0x0F;
        LCD |= (k & 0xF0);
        LCD |= (3<<0);
        Delay_ms(20);
        LCD = LCD & 0xFD;
        Delay_ms(20);

        LCD &= 0x0F;
        LCD |= ((k<<4) & (0xF0));
        LCD |= (3<<0);
        Delay_ms(20);
        LCD = LCD & 0xFD;
        Delay_ms(20);
}
void LCD_Display(unsigned char lcd_pass[])
{
unsigned char i = 0;
while(lcd_pass[i] != '\0')
{
LCD_dat(lcd_pass[i]);
Delay_ms(20);
i++;
}
}

void Delay_ms(unsigned int p)
{
unsigned int i,j;
for(i=0;i<p;i++)
{
for(j=0;j<20;j++);
}
}

void Stepper_Motor(unsigned char status_value)
{
if(status_value == 1)
{
Stepper=0x01;
Delay_ms(1000);
Stepper=0x02;
Delay_ms(1000);
Stepper=0x04;
Delay_ms(1000);
Stepper=0x08;
Delay_ms(1000);
}
else
PORTD = 0x00;
}

No comments:

Post a Comment