Search

Friday, December 26, 2014

Timer 2 For PIC 18

Program:

/***************************************/
//Author : Palak Patel
//Contact No:9173211683
//Title:Timer 2 For PIC18F4520
//Platform: PIC18f4520
//Date:5/8/2014


/*Calculation

(64us/0.25us)= 256

convert this 256 in to hex

hex=FF*/

//Fosc=16Mhz
//Timer Frequency=16Mhz/4=4Mhz
//Timer Period=1/4Mhz=0.00000025=0.25us=0.00025ms
//Max time=256*0.25us=0.000064=64us 
/***************************************/

#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 led PORTC
void delay();
void main()
{
TRISC=0x00;
  
T2CONbits.T2OUTPS3=0;          //Postscaler=1:1
T2CONbits.T2OUTPS2=0;
T2CONbits.T2OUTPS1=0;
T2CONbits.T2OUTPS0=0;
T2CONbits.T2CKPS1=0;
T2CONbits.T2CKPS0=0;            //Prescaler=1:1

while(1)
{
led=0x00;
delay();
led=0xFF;
delay();
}
}

void delay()
{
TMR2=0x00;  
PR2=0xFF;                                  //PR2=256       
T2CONbits.TMR2ON=1;            //Timer2=on
while(PIR1bits.TMR2IF==0);
PIR1bits.TMR2IF=0;                  //Timer 2 Overflow flag
T2CONbits.TMR2ON=0;           //Timer2=off
}



Proteus Simulation:


^&^&^&^&^&^&^&^&^&^&^&^&^&^&^&^&^&^&^&^&^&^&^&^&^&^&^&^&^&^&^&^&^





Timer 1 For PIC 18

Program:

/***************************************/
//Author : Palak Patel
//Contact No:9173211683
//Title:Timer 1 For PIC 18 F 4520
//Platform: PIC18f4520

/*Calculation

(1ms/0.25us)= 4000

65536-4000=61536

convert this 61536 in to hex

hex=F060*/

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



#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 led PORTC

void delay();

void main()

{

TRISC=0x00;

while(1)

{

led=0x00;

delay();

led=0xFF;

delay();

}

}

//Timer For 16ms

void delay()

{

TMR1H = 0x00;

        TMR1L = 0x00;

T1CON = 0x01;

while (PIR1bits.TMR1IF == 0); 

  PIR1bits.TMR1IF = 0;

T1CON = 0xC0;

}


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

//Timer For 0.25ms

//void delay()

/*{

TMR1H  = 0xFF;

   TMR1L  = 0xFF;

T1CON  = 0x01;

while (PIR1bits.TMR1IF == 0); // timer 1 interrupt flag

   PIR1bits.TMR1IF = 0;

T1CON  = 0xC0;

}


//Timer For 1ms

void delay()

{

TMR1H  = 0xF0;

   TMR1L  = 0x60;

T1CON  = 0x01;

while (PIR1bits.TMR1IF == 0); // timer 1 interrupt flag

   PIR1bits.TMR1IF = 0;

T1CON  = 0xC0;

}*/

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

Proteus Simulation:



^&^&^&^&^&^&^&^&^&^&^&^&^&^&^&^&^&^&^&^&^&^&^&^&^&^&^&^&^&^&^&^&^


About Blog

/***************************************/
For PIC 18F4520


1).PIC 18 f4520 Features - Here

2).LED Interface With PIC 18 f4520 -Here

3).7 SEGMENT Interface With PIC 18 f4520 - Here

4).LCD 4 Bit Interface With PIC 18 F4520 -Here

5).LCD 8 Bit Interface with PIC 18 F4520 -Here

6).DC Motor Interface With PIC 18 f4520 - Here

7).USART Transmission With PIC 18 F4520 - Here

8).USART Receiver With PIC 18 F4520 - Here

9).Timer 0 Calculation and Code PIC 18 F 4520 - Here

10).Timer 1 Calcultaion and Code PIC 18 F 4520 - Here

11).Timer 2 Calculation and CODE PIC 18 F 4520 - Here
12).PWM using PIC 18 F4520 - Here

13).ADC 20 V Using PIC 18 F4520 - Here

14).ADC using PIC 18 F4520 - Here

15).DAC Using PIC 18 F4520 - Here

16).RTC Interface Using I2C With PIC 18F 4520 -Here

17).Humidity Sensor interfacing with PIC18f-Here
18).Temperature sensor interfacing with PIC 18-Here


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

For 8051


1).Led Pattern(5 Patterns using Switch) - Here

2).Led Increament Pattern - Here



Timer 0 For PIC 18


Timers are common features of most microcontroller. In simplified terms a timer is just a register whose value keeps increasing (or decreasing) by a constant rate without the help of the CPU. The CPU can read or write this register any time. It reads it find out how much time has elapsed. The Timer register can have the following bit length.

  • 8 bit timers – These can count between between 0-255
  • 16 bit timers – These can count between 0-65536
  • 32 bit timers – These can count between 0-4294967296
A timer has a clock source, for example of the clock source of 10KHz is input to a timer then one increment will take 100uS (micro second). This clock source can be obtained from the CPU clock. The CPU clock of popular MCU ranges from 1 MHz to 20Mhz, this can be sometimes too fast. To help us out their is a thing called prescaler in the MCU. The job of prescaler is to divide the CPU clock to obtain a smaller frequency. The PIC Micro that we will use in this example has the following prescaler division factors available.

  1. 256
  2. 128
  3. 64
  4. 32
  5. 16
  6. 8
  7. 4
  8. 2
  9. 1 (Prescaler by-passed)

Overflow
An overflow occurs when a timer register has already counted the maximum value it can count. At overflow the counter value become 0 again. For example when an 8 bit timer has the value 255 and receive another clock that will set it to 0 and generate an overflow. An overflow can trigger an interrupt and the ISR can handle it.


PIC18F4520 has four different timers. The simplest is the TIMER0 so we will learn how to use it first. In this example the TIMER0 is configured as follows.

  • 8 Bit Mode
  • Clock Source from Prescaler
  • Prescaler = FCPU/256 (Note: FCPU= Fosc/4)
  • Over flow INT enabled
Our FCPU=16MHz/4 (We are running from 16MHz XTAL)
=4MHz
Time Period = 0.25uS

Overflow Period =  0.25us * 256 = 64 uS (Each over flow takes 256 counts)
So we need 1/0.0131072 Overflows to count for 1 sec
= 15625 Overflows
For that we maintain a counter to keep track of overflows. When an over flow occurs the PIC jumps to ISR, where we increment the counter. And when counter becomes 15625 we toggle RB1 pin. This pin is connected to LED. Therefore we have a LED which is ON for 1 sec and Off for 1sec.


Program:

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

//Author : Palak Patel
//Contact No:9173211683
//Title:Timer0
//Platform: PIC18f4520
//Frequency:16Mhz
//Timer Frequency:16/4=4Mhz
//Timer Period:1/4Mhz=0.25us
//Timer calculation0.25us*100=0.000025=25us=0.25ms
//256-100=156
//Hex value of 156 is 9C
//for 8Bit set T0CON=0x48   //use only TMR0l
//For 16Bit set T0CON=0x08//TMR0LandTMR0H

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

#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 led PORTC

void Delay();
void main()

{
         TRISC=0x00;
        T0CON=0x08;                 //T0CON: TIMER0 CONTROL REGISTER
while(1)
{
led=0x00;
Delay();
led=0xFF;
Delay();
}
}

//delay for 25us
void Delay()
{
TMR0L=0x9C;
TMR0H=0xFF;
T0CONbits.TMR0ON=1;
while(INTCONbits.TMR0IF==0);
INTCONbits.TMR0IF=0;
T0CONbits.TMR0ON=0;
}

Proteus Simulation:



^&^&^&^&^&^&^&^&^&^&^&^&^&^&^&^&^&^&^&^&^&^&^&^&^&^&^&^&^&^&^&^&^

Thursday, December 18, 2014

LED INCREAMENT PATTERN

*///////////////////////////////////////////////////////////////////
//Author : Palak Patel
//Contact No : 9173211683
//Title : Led Pattern
//Platform : 8051
//Software : Keil 4.0
//Created: 15-05-2014
////////////////////////////////////////////////////////////////////*

In this program as you press the switch it will increament the led..

Program:

#include<reg51.h>
#define led P1
sbit sw=P2^0;

void delay();

void main()
{
unsigned char k;
led=0x00;
k=0x00;
while(1)
{
if(sw==0)
{
k=(2*k)+1;
led=k;
delay();

if(k==255)
k=0;
}
}
}

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

Simulation:




...................................................................................................................................................................


PIC 18 F4520 Microcontroller

1).PIC18f4520 Features - Here

2).LED Interface With PIC18f4520 -Here

3).PWM using PIC18F4520 - Here

4).ADC 20 V Using PIC18F4520 - Here

5).ADC using PIC18F4520 - Here

6).DAC Using PIC18F4520 - Here

7).USART Transmission With PIC18F4520 - Here

8).USART Receiver With PIC18F4520 - Here

9).RTC Interface Using I2C With PIC18F4520 -Here

10).DC Motor Interface With PIC18f4520 - Here

11).7 SEGMENT Interface With PIC18f4520 - Here

12).LCD 4_Bit Interface With PIC18F4520 -Here

13).LCD 8_Bit Interface with PIC18F4520 -Here


8051 Microcontroller

1).Led Pattern(5 Patterns using Switch) - Here

2).Led Increament PatternHere


...................................................................................................................................................................

LED PATTERN

*///////////////////////////////////////////////////////////////////
//Author : Palak Patel
//Contact No : 9173211683
//Title : Led Pattern(5 Patterns using Switch)
//Platform : 8051
//Software : Keil 4.0
//Created: 15-05-2014
////////////////////////////////////////////////////////////////////*


Program:

#include<reg51.h>
#define led P1

sbit sw=P2^0;
sbit sw1=P2^1;
sbit sw2=P2^2;
sbit sw3=P2^3;
sbit sw4=P2^4;

void delay();


void main()

{
        unsigned char k,k1,k2,i,j,g;
        P:while(1)
        {
if(sw==0  &&  sw1==1  && sw2==1  && sw3==1 && sw4==1)
{
while(1)
{
led=0x00;
delay();
led=0xff;
delay();
if(sw1==0 || sw2==0 || sw3==0 || sw4==0)
goto P;
}
}

else if(sw1==0 && sw==1 && sw2==1 && sw3==1 && sw4==1 )
{
while(1)
{
led=0x55;
delay();
led=0xaa;
delay();
if(sw==0 || sw2==0 || sw3==0 || sw4==0)
goto P;
}
            }

else if(sw2==0 && sw==1 && sw1==1 && sw3==1 && sw4==1)
{
while(1)
{
led=0x01;
delay();
for(g=8 ; g>=1 ; g--)
{
led=led<<1;
delay();
}
if(sw1==0 || sw1==0 || sw3==0 || sw4==0)
goto P;
}
}

else if(sw3==0 && sw==1 && sw1==1 && sw2==1 && sw4==1)

{
while(1)
{
led=0x80;
delay();
for(g=8 ; g>=1 ; g--)
{
P1=P1>>1;
delay();
}
if(sw==0 || sw1==0 || sw2==0 || sw4==0)
goto P;
}
}

else if(sw4==0 && sw==1 && sw1==1 && sw2==1 && sw3==1)
{
while(1)
{
k2=0;
for(j=4 ; j>=1 ; j--)
{
k=0x01;
k1=0x80;
for(i=1 ; i<=j ; i++)
{
led=k | k1 | k2;
delay();
k=k<<1;
k1=k1>>1;
}
k2=led;
}
if(sw==0 || sw1==0 || sw2==0 || sw3==0)
goto P;
}
}

else
{
led=0x00;
}
        }
}

void delay()

{
              unsigned int i;
              for(i=0 ; i<20000 ; i++);
}

Simulation:



...................................................................................................................................................................


PIC 18 F4520 Microcontroller

1).PIC18f4520 Features - Here

2).LED Interface With PIC18f4520 -Here

3).PWM using PIC18F4520 - Here

4).ADC 20 V Using PIC18F4520 - Here

5).ADC using PIC18F4520 - Here

6).DAC Using PIC18F4520 - Here

7).USART Transmission With PIC18F4520 - Here

8).USART Receiver With PIC18F4520 - Here

9).RTC Interface Using I2C With PIC18F4520 -Here

10).DC Motor Interface With PIC18f4520 - Here

11).7 SEGMENT Interface With PIC18f4520 - Here

12).LCD 4_Bit Interface With PIC18F4520 -Here

13).LCD 8_Bit Interface with PIC18F4520 -Here


8051 Microcontroller


1).Led Pattern(5 Patterns using Switch)Here

2).Led Increament PatternHere


...................................................................................................................................................................