Search

Tuesday, October 21, 2014

ADC using PIC18F4520

Title:Dac Using PIC18F4520


Analog to Digital Converter (ADC) is a device that converts an analog quantity (continuous voltage) to discrete digital values. This is very useful when we want to do some processing on physical quantities, which are normally analog in nature. Most of the PIC Microcontrollers have built in ADC Module. Here we are using pic18f4520 for demonstrating the working.
ADC_Symbol
ADC_Symbol
ADC of PIC Microcontrollers have 5 inputs for 28 pin devices and 8 inputs for 40/44 pin devices. It is a 10-bit ADC, ie the conversion of analog signal results in corresponding 10-bit digital number. The positive and negative reference voltage (+Vref and -Vref) of PIC ADC is software selectable, which can be VDD, VSS, voltage at RA2 or RA3. This A/D Converter module can also operate in sleep mode in which clock is derived from its internal RC oscillator. Following points may help you to understand the concept of reference voltages.
  • When the ADC input is -Vref, result will be 0000000000
  • When the ADC input is +Vref, result will be 1111111111
  • Resolution of ADC = (+Vref – -Vref)/(210 – 1), which is the minimum voltage required to change the ADC result by one bit.
We require some hardware knowledge to program PIC ADC in Hi-Tech C. If you don’t need hardware knowledge please skip this part and go to Circuit Diagram.

ADC Module in Detail

Registers

PIC ADC has 4 registers :
  • ADCON0 – ADC Control Register 0
  • ADCON1 – ADC Control Register 1
  • ADRESH – ADC Result High Register
  • ADRESL – ADC Result Low Register

 ADCON0 Register

ADCON0 Register - PIC 16F877A
ADCON0 Register – PIC 16F877A
  • ADCS1 and ADCS2 are used to select A/D Conversion Clock. It should be selected in accordance with device clock.
  • CH2CH1 and CH0 are used to select one of the analog input channel out of eight channels.
  • GO/DONE is the A/D Conversion Status bit. Setting this bit initializes A/D Conversion and will be automatically cleared when the conversion is complete.
  • ADON is used to switch on/off the ADC Module. When it is 1, the ADC Module turns ON and when it is 0, the ADC Module will be OFF.

ADCON1 Register

ADCON1 Register - PIC 16F877A
ADCON1 Register – PIC 16F877A
  • ADFM is the ADC Result Format select bit. Two 8 bit register (ADRESH and ADRESL) are provided to store the 10-bit result of A/D Conversion, thus we need’t use 6 bits. When ADFM is 1, the result will be right justified, ie Most Significant Bits of ADRESH will be read as 0. When ADFM is 0, the result will be left justified, ie Least Significant Bits of ADRESL will be read as zero.
  • ADCS2 is used to select A/D Conversion Clock in association with ADCS1 and ADC2 of ADCON0 register.
  • PCFG3 – PCFG0 are the A/D Port Configuration Control bits. Each pin amoung AN0 – AN7 is configured as analog, digital or reference voltage inputs according to the status of these configuration bits as given below.
Note : TRIS Registers of Analog inputs must be configured as input for the proper operation.

ADC Block Diagram

ADC Module Block Diagram - PIC16F877A
ADC Module Block Diagram – PIC18F4520

Selection of A/D Conversion Clock

The time for A/D Conversion per bit is defined as TAD and it requires minimum 12TAD to generate the 10-bit result. The time TAD is determined by the A/D Conversion Clock which is software selectable to following options.
  •  2 TOSC
  • 4 TOSC
  • 8 TOSC
  • 16 TOSC
  • 32 TOSC
  • 64 TOSC
  • Internal RC oscillator of ADC Module
TOSC is the time period of the device clock usually provided by the crystal oscillator.
Care should be taken while selecting the A/D Conversion Clock, such that the clock should provide the minimum TAD(1.6μS) required for the correct A/D Conversion. So refer the following table before setting the ADC clock.
ADC Clock Selection Table - PIC 16F877A
ADC Clock Selection Table – PIC18F4520

Program:


//Author : Palak Patel

//Contact No:9173211683

//Title:ADC

//Platform: PIC18F4520

//Software MPLAB



#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 PBADEN = OFF
//#pragma config LPT1OSC = OFF
//#pragma config MCLRE = ON
//#pragma config STVREN = OFF
//#pragma config LVP = OFF
//#pragma config XINST = OFF

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

void main()
{
unsigned char temp[] = "TEMP",tdata[4],i,k;

unsigned int x=0,y,c=0x100,z,x1;
float p,tmpr,f;
TRISA=0x01;
TRISC=0x00;
cmd(0x01);                          //Clear Lcd
         cmd(0x02);                          //4 Bit Specification
         cmd(0x28);                          //4 Bit specification
         cmd(0x0c);
         cmd(0x80);                          //Cursor starting point
         cmd(0X06);

i=0;
while(temp[i]!='\0')
{
                 dat(temp[i]);
                  i++;
}
cmd(0xC1); //Position second line 2nd character
ADCON1=0x0E;
ADCON0=0x00;
ADCON2=0x9C;
ADCON0bits.ADON = 1;
while(1)
{
                 ADCON0bits.GO=1;
while(ADCON0bits.GO==1);
                 x=ADRESH;
                 y=ADRESL;
z=(x*c)+y;
f=(z*(float)4.8875);
                x1=(int)f;
tdata[3] = (x1/1000)%10 + 48;
tdata[2] =(x1/100)%10 + 48;
tdata[1] = (x1/10)%10 + 48;
tdata[0] = x1%10 + 48;
cmd(0xC3);
dat(tdata[3]);
dat(tdata[2]);
dat(tdata[1]);
dat(tdata[0]);
}
}

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

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

        PORTC &=0x0F;
        PORTC |=((k<<4) & (0xF0));
        PORTC |=(3<<0);
        Delay_ms(20);
        PORTC=PORTC & 0xFD;
        Delay_ms(20);
}

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

Simulation:






For Basic Electronics Kindaly follow this Link:

Wednesday, September 3, 2014

DAC Using PIC18F4520

Title:Dac Using PIC18F4520

Program:

//Author : Palak Patel
//Contact No:9173211683
//Title:dac
//Platform: PIC18f4520
//Software:MPLAB

#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

#define sw0 PORTCbits.RC0
#define sw1 PORTCbits.RC1

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

#define dac PORTD
void delay()
{
unsigned int p;
for(p=0;p<1000;p++);
}

void main()
{
unsigned int value,value1,i,Temp,x[25],j,a,b,c,d,p,e,f,g,h;
unsigned char name[]="Voltage Difference=";
TRISB=0x00;
TRISD=0x00;
TRISAbits.TRISA0=1;
TRISAbits.TRISA2=1;
TRISCbits.TRISC0=1;
TRISCbits.TRISC2=1;
ADCON=0x0F;

  cmd(0x01);                          //Clear Lcd
          cmd(0x02);                          //4 Bit Specification
          cmd(0x28);                          //4 Bit specification
          cmd(0x0c);
          cmd(0x80);                          //Cursor starting point
          cmd(0X06);
          i=0;
          while(name[i]!='\0')
          {
                     dat(name[i]);
                     i++;
          }

cmd(0xc0);

i=0x00;
dac=0x00;
while(1)
{
if(sw0 == 0)
{
delay();
delay();
if(sw0 == 0)
{
i=i+1;
dac=i;
delay();
}
}
if(sw1 == 0)
{
delay();
delay();
if(sw1 == 0)
{
i=i-1;
dac=i;
delay();
}
}
dac=i;
delay();
}
}

Simulation:







For Basic Electronics Kindaly follow this Link:

USART Transmission With PIC18F4520

Title:USART Transmission With PIC18F4520

Program:


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

#include<p18f4520.h>
void delay();

void main()
{
                unsigned char palak[]="Palak Patel";
                unsigned int i=0;
                TRISC=0x00;

                SPBRG=31;         
                TXSTAbits.CSRC=1;                 //Master Mode
                TXSTAbits.TX9=0;                   //8 Bit
                TXSTAbits.TXEN=1;                //Transmit enabled
                RCSTAbits.SPEN=1;
                TXSTAbits.SYNC=0;                //Asynchronous Mode
                TXSTAbits.SENDB=0;              //Sync Break transmission completed
                TXSTAbits.BRGH=0;                //High Speed
                while(palak[i]!='\0')
                {
                               
                                while(PIR1bits.TXIF==0);// TMRT Empty
                                TXREG=palak[i];       //Transmit buffer
                                i++;
                                delay();
                }
while(1);
}

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

}
Simulation:




For Basic Electronics Kindaly follow this Link:

USART Receiver With PIC18F4520

Title:USART Receiver With PIC18F4520

Program:

//Author : Palak Patel
//Contact No:9173211683
//Title: USART Receiver
//Platform: PIC18f4520
//Software: MPLAB

#include<p18f4520.h>
void cmd(unsigned char);
void dat(unsigned char);
void delay();
void main()
{
         unsigned char i,lcddata;
         TRISD=0x00;
         TRISB=0x00;        
         cmd(0x01);                          //Clear Lcd
         cmd(0x02);                          //4 Bit Specification
         cmd(0x28);                          //4 Bit specification
         cmd(0x0c);
         cmd(0x80);                          //Cursor starting point
               
         RCSTA=0x90;
         SPBRG=31;
         i=0;
         dat('I');
          while(1)
          {
                                while(PIR1bits.RCIF==0);
                                lcddata=RCREG;
                                PORTB=RCREG;
                                dat(lcddata);
                                //i++;
           }
}
void cmd(unsigned char k)
{
        PORTD &=0x0F;
        PORTD |=(k & (0xF0));
        PORTD |=(2<<0);
        delay();
        PORTD=PORTD & 0xFC;
        delay();

        PORTD &=0x0F;
        PORTD |=((k<<4) & (0xF0));
        PORTD |=(2<<0);
        delay();
        PORTD=PORTD & 0xFC;
        delay();
}
void dat(unsigned char k)
{
        PORTD &=0x0F;
        PORTD |=(k & 0xF0);
        PORTD |=(3<<0);
        delay();
        PORTD=PORTD & 0xFD;
        delay();

        PORTD &=0x0F;
        PORTD |=((k<<4) & (0xF0));
        PORTD |=(3<<0);
        delay();
        PORTD=PORTD & 0xFD;
        delay();
}

void delay()
{
                unsigned int i;
                for(i=0;i<40;i++);
}

Simulation:







For Basic Electronics Kindaly follow this Link:


RTC Interface Using I2C With PIC18F4520

Title:I2C Interface With PIC18F4520

Program:

//Author : Palak Patel
//Contact No:9173211683
//Title:i2c RTC
//Platform: PIC18f4520
//Software:MPLAB

#include<p18f4520.h>
#include<stdio.h>
#include <i2c.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 = OFF
#pragma config PBADEN = OFF
#pragma config STVREN = OFF
#pragma config LVP = OFF

#define lcd PORTD
#define sda TRISCbits.TRISC4
#define scl TRISCbits.TRISC3

#if defined (I2C_V1)
#undef StartI2C

#if defined (I2C_V1)
#undef RestartI2C
#if defined (I2C_V1)
#undef StopI2C

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

void Delay_ms();
void StartI2C();
unsigned char Write(unsigned char);
void RestartI2C();
unsigned char ReadI2C();
void StopI2C();

void main()
{
                unsigned int sec,sec1,sec2,x[25],min,min1,min2,hour,hour1,hour2;
                unsigned char i,j;
                TRISD=0x00;
                I2C_SCL = 1;
                 I2C_SDA = 1;
                cmd(0x01);
                cmd(0x02);
                cmd(0x28);
                cmd(0x0C);
                cmd(0x06);
               
                ADCON1=0x0F;
                SSPSTAT &=0x3F;
               
                SSPSTATbits.SMP=0;          //Slew Rate Control bit
                SSPSTATbits.CKE=0;           //SMBus Select bit
                 
                SSPCON1bits.WCOL=0;     //Write Collision
                SSPCON1bits.SSPEN=1;     //Synchronous Serial Port Enable bit
                SSPCON1bits.SSPM3=1;    //1000 = I2C Firmware Controlled Master mode
                SSPCON1bits.SSPM2=0;
                SSPCON1bits.SSPM1=1;
                SSPCON1bits.SSPM0=1;

                SSPCON2bits.ACKDT=0;     //Acknowledge Data bit
                SSPCON2bits.ACKEN=0;     //Acknowledge Sequence Enable bit
                SSPCON2bits.RCEN=0;       //Receive Enable bit
                SSPCON2bits.PEN=0;         //Stop Condition Enable bit
                SSPCON2bits.RSEN=0;      //Repeated Start Condition Enable bit
                SSPCON2bits.SEN=0;       //Start Condition Enable/Stretch Enable bit
                //SSPCON2=0x00;
                StartI2C();
                WriteI2C(0xD0);
                WriteI2C(0x00);
                WriteI2C(0x40);
                StopI2C();
                StartI2C();
                WriteI2C(0xD0);
                WriteI2C(0x01);
                WriteI2C(0x40);
                StopI2C();
                StartI2C();
                WriteI2C(0xD0);
                WriteI2C(0x02);
                WriteI2C(0x10);
                StopI2C();
                while(1)
                {
                                //Read second
                                StartI2C();
                                WriteI2C(0xD0);
                                WriteI2C(0x00);
                                RestartI2C();
                                WriteI2C(0xD1);
                                sec=ReadI2C();
                                StopI2C();

                                //Read minute
                                StartI2C();
                                WriteI2C(0xD0);
                                WriteI2C(0x01);
                                RestartI2C();
                                WriteI2C(0xD1);
                                min=ReadI2C();
                                StopI2C();
                               
                                //Read hour
                                StartI2C();
                                WriteI2C(0xD0);
                                WriteI2C(0x02);
                                RestartI2C();
                                WriteI2C(0xD1);
                                hour=ReadI2C();
                                StopI2C();

                                hour1=(hour>>4);
                                hour2=(hour & 0x0F);

                                min1=(min>>4);
                                min2=(min & 0x0F);

                                sec1=(sec>>4);
                                sec2=(sec & 0x0F);

                                sprintf(x,"%d %d :",hour1,hour2);
                                cmd(0x83);
                                i=0;
                                while(x[i]!='\0')
                                {
                                                dat(x[i]);
                                                i++;
                                }

                                sprintf(x,"%d %d ",min1,min2);
                                cmd(0x86);
                                i=0;
                                while(x[i]!='\0')
                                {
                                                dat(x[i]);
                                                i++;
                                }

                                sprintf(x,"%d %d :",sec1,sec2);
                                cmd(0x80);
                                i=0;
                                while(x[i]!='\0')
                                {
                                                dat(x[i]);
                                                i++;
                                }             
                }
}

void StartI2C()
{
    SSPCON2bits.SEN = 1;                           // initiate bus start condition
}

unsigned char WriteI2C( unsigned char data_out )
{
            SSPBUF = data_out;                       // write single byte to SSPBUF
            if (SSPCON1bits.WCOL==1)          // test if write collision occurred
                       return ( -1 );                       // if WCOL bit is set return negative #
            else
            {
                       while( SSPSTATbits.BF );  // wait until write cycle is complete  
               
                       if ( PIR1bits.SSPIF )          // test for ACK condition received
                                return ( -2 );
                       else
                                return ( 0 );              // if WCOL bit is not set return non-negative #
            }
}

void RestartI2C()
{
          SPCON2bits.RSEN = 1;                    // initiate bus restart condition
}

unsigned char ReadI2C()
{
          if( ((SSPCON1&0x0F)==0x08) || ((SSPCON1&0x0F)==0x0B) )       //master mode only
                SSPCON2bits.RCEN = 1;          // enable master for 1 byte reception
          while ( !SSPSTATbits.BF );             // wait until byte received 
          return ( SSPBUF );                         // return with read byte
}

void StopI2C()
{
       SSPCON2bits.PEN = 1;                     // initiate bus stop condition
}             

void cmd(unsigned char k)
{
        PORTD &=0x0F;
        PORTD |=(k & (0xF0));
        PORTD |=(2<<0);
        Delay_ms();
        PORTD=PORTD & 0xFC;
        Delay_ms();

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

        PORTD &=0x0F;
        PORTD |=((k<<4) & (0xF0));
        PORTD |=(3<<0);
        Delay_ms();
        PORTD=PORTD & 0xFD;
        Delay_ms();
}

void Delay_ms()
{
         unsigned int i;
         for(i=0;i<40;i++);
}

Simulation: