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;
}

PIC 18F UART Receiver and Transmitter

Program:

//Author : Palak Patel

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

#include<p18f4520.h>

#pragma config OSC = HS
#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

#define LCD PORTD

#define RS PORTCbits.RC0
#define RW PORTCbits.RC1
#define EN PORTCbits.RC2

// 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 delay();

void cmd(unsigned char);
void dat(unsigned char);
void Delay_ms(unsigned char);

void isr(void)

{
   unsigned char lcddata;
   if(PIR1bits.RCIF == 1)                              // if the USART receive interrupt flag has been set
   {
       if(PIR1bits.TXIF == 1)                    // check if the TXREG is empty
       {
            lcddata=RCREG;
dat(lcddata);
          }
   }    
}

void main()

{
              unsigned char palak[]="Palak Patel";
              unsigned int i=0;

TRISCbits.TRISC0=0;
TRISCbits.TRISC1=0;
TRISCbits.TRISC2=0;
             TRISCbits.TRISC6 = 0;                 // make the TX pin a digital output
    TRISCbits.TRISC7 = 1;                 // make the RX pin a digital input
TRISD=0x00;

cmd(0x01);                          //Clear Lcd

          cmd(0x02);                          //4 Bit Specification
          cmd(0x38);                          //4 Bit specification
          cmd(0x0c);
          cmd(0x80);                          //Cursor starting point
cmd(0x06);
           
          TXSTAbits.CSRC=1;               //Master Mode
          TXSTAbits.TX9=0;                   //8 Bit
          TXSTAbits.TXEN=1;                //Transmit enabled
          TXSTAbits.SYNC=0;                //Asynchronous Mode
          TXSTAbits.SENDB=0;             //Sync Break transmission completed
          TXSTAbits.BRGH=1;               //High Speed
      RCSTA=0x90;
SPBRG=100;   


RCONbits.IPEN   = 0;                 // disable interrupt priority
    INTCONbits.GIE  = 1;                 // enable interrupts
    INTCONbits.PEIE = 1;                // enable peripheral interrupts.
    PIE1bits.RCIE   = 1;                    // enable USART receive interrupt

                while(palak[i]!='\0')

                {
                                while(PIR1bits.TXIF==0);// TMRT Empty
                                TXREG=palak[i];       //Transmit buffer
                                i++;
                }
while(1);
}

void delay()

{
                unsigned int j;
                for(j=0;j<100;j++);
}

void cmd(unsigned char k)

{
        LCD=k;
        RS=0;
        RW=0;
        EN=1;
        Delay_ms(20);
        EN=0;
}

void dat(unsigned char k)

{
        LCD=k;
        RS=1;
        RW=0;
        EN=1;
        Delay_ms(20);
        EN=0;
}

void Delay_ms(unsigned char p)

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