Search

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: