Search

Monday, August 25, 2014

PIC18f4520 Features

Title:PIC18f4520 Features

Features:
Wide operating voltage range :  2.0v to  5.5v
Sink/source current per  I/O : 25mA
Operating Frequency:
     External
-To external RC modes(upto 4Mhz)
-Two external clock modes(upto 40Mhz)  
Internal
-8 user selectable frequency from 31khz to 8Mhz
Resets : POR(Power on reset), BOR(Brown Out reset)
Program memory(Flash) : 32 KB (32768 bytes)
Data memory : 1536 bytes
EEPROM data Memory : 256 bytes
I/O ports : A,B,C,D,E (A-8Pins)(b-8Pins) (C-8Pins)(D-8Pins) (E-4Pins)
Timers: 4 Timers
-T0 = 8,16 bits
-T1 = 8,16 bits
-T2 = 8 bits
-T3 = 8,16 bits
Capture/Compare/PWM : 1 CCP
Interrupts : 20
Digital communication peripherals :
         -MSSP (I2C,SPI)
         -USART
Parallel communication : PSP(parallel slave port)
Analog to Digital converter : (ADC) 13 Channels , each 10 bit
Analog Comparators : 2
Instruction Set : 75 Instruction ,83 with extended Instruction set enabled
Packages : 40 PIN PDIP(plastic dual inline package)
                 44 PIN QFP(quad flat package)
                 44 PIN TQFP(thin quad flat package)

40 PIN PDIP

44PIN QFP and TQFP:
Clock Sources and Oscillator Switching:

Oscillator configuration:
1.Crystal Oscillator/Ceramic Resonators mode:
           In XT, LP, HS or HSPLL Oscillator modes, a crystal or ceramic resonator is connected to the OSC1 and OSC2 pins to establish oscillation. Figure 2-1 shows the pin connections.
2.RC oscillator mode:
  —In the RC Oscillator mode, the oscillator frequency divided by 4 is available on theOSC2pin. This signal may be used for test   purposes  or to   synchronize   other  logic.

Resets:
Power on reset:
   —A Power-on Reset pulse is generated on-chip whenever VDD rises above a certain threshold. This allows the  device  to  start  in  the initialized state when  VDD is adequate for operation.
Brown out reset:
—   The BOR threshold is set by the BORV1:BORV0 bits. If BOR is enabled (any values of BOREN1:BOREN0,except ‘00’), any drop of VDD below VBOR  for greater than TBOR will reset the device. A Reset may or may not occur if VDD falls below VBOR for less than TBOR. The chip will remain in Brown-out Reset until VDD rises above VBOR.
—   If the Power-up Timer is enabled, it will be invoked after .VDD rises above VBOR; it then will keep the chip in Reset for an additional time delay.

                                              


                                      For Basic Electronics Kindaly follow this Link:
          http://basicelectonicspalakpatel.blogspot.in/2014/08/the-mosfet-as-switch.html



DC Motor Interface With PIC18f4520

Title:DC Motor Interface With PIC18f4520 using L293D

Program:

//Author : Palak Patel(M.Tech)
//Contact No:9173211683
//Title:DC motor
//Platform: PIC18f4520
//Software:MPLAB

#include<p18f4520.h>
#define in1 PORTDbits.RD0
#define in2 PORTDbits.RD1
#define en1 PORTDbits.RD2
#define sw PORTBbits.RB0
#define sw1 PORTBbits.RB1
#define sw2 PORTBbits.RB2

#define lcd PORTC
void cmd(unsigned char);
void dat(unsigned char);
void Delay_ms(unsigned char);

void main()
{
        unsigned char i,name[]="Reverse Running",j,name1[]="Forward                  
                                                                                      Running",k,name2[]="MotorOff";
        TRISB=0xFF;
        TRISC=0x00;
        TRISD=0x00;
        ADCON1=0x0F;
        cmd(0x01);
        cmd(0x02);
        cmd(0x28);
        cmd(0x06);
        cmd(0x0c);

        while(1)
        {
                 if(sw==0 && sw1==1 && sw2==1)
                {
                        while(1)
                        {
                                    en1=1;
                                    in1=1;
                                    in2=0;
                                    cmd(0x01);
                                    i=0;
                                    cmd(0x80);
                                    while(name[i]!='\0')
                                    {
                                             dat(name[i]);
                                             i++;
                                     }
                                     if(sw1==0 || sw2==0)
                                     break;
                          }
            }
            
            if(sw==1 && sw1==0 && sw2==1)
            {
                       while(1)
                       {
                                   en1=1;
                                   in1=0;
                                   in2=1;
                                   cmd(0x01);
                                   i=0;
                                   cmd(0x80);
                                   while(name1[i]!='\0')
                                   {
                                            dat(name1[i]);
                                             i++;
                                    }
                                    if(sw==0 || sw2==0)
                                    break; 
                       }
            }
     
           if(sw2==0 && sw==1 && sw1==1)
          {
                     while(1)
                    {
                                   en1=0;
                                    cmd(0x01);
                                    j=0;
                                    cmd(0x80);
                                    while(name2[j]!='\0')
                                    {
                                            dat(name2[j]);
                                             j++;
                                     }
if(sw==0  ||  sw1==0)
                                    break;
}
           }   
            else
            {
while(1)
{
                                      en1=0;
                                      cmd(0x01);
                                       j=0;
                                      cmd(0x80);
                                      while(name2[j]!='\0')
                                      {
                                                   dat(name2[j]);
                                                   j++;
                                       }
                                     if(sw==0  ||  sw1==0)
                                     break;
                         }   
             }
        }
}
void cmd(unsigned char k)
{
        lcd &=0x0F;
        lcd |=(k & (0xF0));
        lcd |=(2<<0);
        Delay_ms(20);
        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 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 Delay_ms(unsigned char p)
{
unsigned char i,j;
for(i=0;i<p;i++)
{
for(j=0;j<50;j++);
}
}

Simulation:

1. Motor Off:

2.Reverse Running:
3.Forward Running:





For Basic Electronics Kindally follow this Link:


Thursday, August 21, 2014

7 SEGMENT Interface With PIC18f4520

7 SEGMENT Interface With PIC18f4520
Program:

//Author : Palak Patel(M.Tech)
//Contact No:9173211683
//Title:7seg
//Platform: PIC18f4520
//Software:MPLAB

#include<p18f4520.h>
#define seg PORTC
#define control PORTD

void Delay_ms(unsigned int);
void display(unsigned char);

void main()
{
        unsigned int i;
unsigned char a,b,c,d,j;
        TRISC=0x00;
        TRISD=0x00;
PORTD=0x00;
while(1)
       {
for(i=0;i<1000;i++)
{
a=i;
b=a%10;
a=a/10;
c=a%10;
d=a/10;
for(j=0;j<10;j++)
{
control=0x06;
display(b);
Delay_ms(10);

control=0x05;
                                      display(c);
                                      Delay_ms(15);

control=0x03;
display(d);
                                      Delay_ms(15);
}
}
}
}

void display(unsigned char n)
{
switch(n)
{
case 0:
seg=0xFC;
break;
case 1:
seg=0x60;
break;
case 2:
seg=0xDA;
break;
case 3:
seg=0xF2;
break;
case 4:
seg=0x66;
break;
case 5:
seg=0xB6;
break;
case 6:
seg=0xBE;
break;
case 7:
seg=0xE0;
break;
case 8:
seg=0xFE;
break;
case 9:
seg=0xF6;
break;
default:
seg=0x00;
break;
}
}

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

Simulation:







                                                     For Basic Electronics Kindally follow this Link:
                                  http://basicelectonicspalakpatel.blogspot.in/2014/08/the-mosfet-as-switch.html

LED Interface With PIC18f4520

Blink

           This example shows the simplest thing you can do with Pic microcontroller to see physical output: it blinks an LED.

Hardware Required

  • Pic microcontroller
  • 8-LEDS
  • 8-220 ohm res

Circuit

          To build the circuit, attach a 220-ohm resistor to port C. Then attach the long leg of an LED (the positive leg, called the anode) to the resistor. Attach the short leg (the negative leg, called the cathode) to ground. Then plug your PIC18f4520 into your computer, start the Pic program, and enter the code below.
          Most Arduino boards already have an LED attached to port c on the board itself. If you run this example with no hardware attached, you should see that LED blink.
//****************************************//

Program:

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


#include<p18f4520.h>
#define LED PORTC
void Delay();

void main()
{
TRISC=0x00;         //PORTC as a output PORT
                             //LED BLINKING
while(1)
{
LED=0x00;     //Led OFF
Delay();
LED=0xFF;    //Led ON
Delay();
}
}

void Delay()
{
unsigned int i;
for(i=0;i<2000;i++);

}
//****************************************//

Simulation:









                                                     For Basic Electronics Kindally follow this Link:
                           http://basicelectonicspalakpatel.blogspot.in/2014/08/the-mosfet-as-switch.html

Thursday, August 14, 2014

LCD 4_Bit Interface With PIC18F4520..

               
Print On LCD

    I Aim to teach you programming 16x2 LCD in simples language skills using pic18f4520.

summary on lcd:
         This second LCD-tutorial is based on 'Introduction to LCD Programming', so you should to read that first.
          In the first tutorial we have seen that character LCD's based on the HD44780 chip can be driven in 8bits mode, which requires in total 11 lines from you microcontroller. If we want (or need) to spare some lines for other purposes it is possible to drive the display in 4bits mode, which requires 7 lines. It is possible to use only 6 lines, in which case R/W is tied to GrouND. This configuration is seen many times in projects. Instead of reading the busy flag (which is somewhat trickier than it is in 8 bit modus) we have to use delay loops. These delayloops have to be recalculated when using other oscillator frequencies. In this tutorial we use the somewhat harder solution, making use of the busy flag and being independent of the oscillator frequency. The only drawback using 4 bits is that commands and data have to be sent in two nibbles (4bit parts) to the display, which take slightly more time. In many cases that won't be a problem.
          To keep things simple, I will take the examples from the first tutorial, make the necessary changes and only explain the differences.

Hardware Required

  • Pic microcontroller
  • 8-LEDS
  • 8-220 ohm res

  • Hardware configuration
  •          The only difference with the 8bit version is DB0, DB1, DB2 and DB3 on the displaymodule side. These lines are not connected to the processor. Leave those lines unconnected, DON'T SHORT THEM TO GROUND as seen in projects where R/W is tied to ground.


//****************************************//


Program:

//Author : Palak Patel
//Contact No:9173211683
//Title:lcd_4Bit
//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
void Delay_ms(unsigned char);

void cmd(unsigned char);
void dat(unsigned char);
void main()
{
         unsigned char i,name[]="PALAK PATEL";
         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);
        while(1)
       {
                i=0;
                while(name[i]!='\0')
                {
                        dat(name[i]);
                        i++;
                }
              cmd(0x01);
              cmd(0x80);
       }
}

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 Kindally follow this Link:
                           http://basicelectonicspalakpatel.blogspot.in/2014/08/the-mosfet-as-switch.html

LCD 8_Bit Interface with PIC18F4520..

Print On LCD


I Aim to teach you prgramming 16x2 LCD in simples language skills using pic18f4520.

Summary on LCD:
LCD means Liquid Crystal Display. Liquid Crystal Polarizes on potential drop across it hence blocking light passing through it.(black pixel). When no potential is dropped the LCD passes light hence white pixel. 

16x2 means 16 columns and 2 rows of alphanumeric ASCII blocks on the LCD.

Has three control signals and one databus. 

R/W: When this signal is '1' = Reads data from the LCD RAM. When this signal is '0' = Writes data on LCD RAM.

EN: Is basically a Latch signal. You have to send '1' first and then '0' signal with a particular delay to latch the data.

RS: Is a Register Select Control signal. When this signal is '1' = It accepts data to be displayed. When this signal is '0' = It accepts instructions for the LCD like setting font, cursor position etc.

D0 to D7: Is 8-bit Databus. It is used to send both data as well as Instructions to the LCD based upon control signals.

Backlight + and Backlight GND: Turns on Backlight of that LCD so that you can see the words correctly.
VEE: is a contrast voltage. Using a trim pot you can adjust the contrast of the LCD. More voltage more the contrast and vice versa(voltage should never exceed VCC = +5 volts).

This LCD has 2 lines and can write maximum 16 letter word per line.
That is all you have to know till now.

Next you should know how to connect a LCD to any microcontroller. For our demonstration I'm using pic18f4520. Check the circuit diagram below for details. Construct this circuit diagram. (Beware wrong connections on LCD or microcontroller can permanently damage the devices).

Hardware required: 

1. JHD 162A 16x2 LCD                             
2. Pic18F4520  Microcontroller                  
3. 40 Pin ZIP Socket for microcontroller.   
4. Tactile Switch                                         
5. 41k ohms 1/4Watts resistor

//****************************************//
     
Program:

//Author : Palak Patel(M.Tech)
//Contact No:9173211683
//Title:lcd_8bit
//Platform: PIC18f4520
//Software:Microchip PIC

#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 = OFF
#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

void cmd(unsigned char);

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

void main()

{
        unsigned char P[ ]="Hello World",i;
        TRISD=0x00;
        TRISC = 0x00;

        cmd(0x01);

        cmd(0x80);
        cmd(0x0C);
        cmd(0x06);
        cmd(0x38);
        while(1)
        {
                 while(P[i]!='\0')
                 {
                       dat(P[i]);
                       i++;
                       Delay_ms(100);
                  }
        cmd(0x01);
        i=0;
        }
}

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++);
}
}
//****************************************//

Simulation:






                                                For Basic Electronics Kindaly follow this Link:
                           http://basicelectonicspalakpatel.blogspot.in/2014/08/the-mosfet-as-switch.html