## Generation of Square Waveform using Timer and Timer with Interrupt

### Generation of Square Waveform with Timer

#### Write a C18 Program to generate Square waveform of 50 Hz frequency continuosly using Timer0, 16-bit, No Prescaler,XTAL=10Mhz

Solution -
Given

• Timer0, 16-bit mode
• No prescaler
• XTAL i.e. fosc = 10 MHz
• Square waveform to be generated of 50Hz frequency.

Delay Generation Calculations

Time Period of Complete 1 cycle of Square waveform is given by (Tsquare) = 1 / fsquare = 1 / 50 = 0.02 Seconds
But as it is Square waveform it has equal TON = TOFF
Hence the delay to be generated using Timer is equal to either TON or TOFF
So TON = TOFF = Tsquare / 2 = 0.01 Seconds i.e. 10 milliSeconds
Crystal Freq = 10 MHz

Internally Timer Module divides this Crystal frequency by the factor of 4 to generate 1 machine cycle frequency ,
Therefor 1 machine cycle frequency = Crystal frequeny / 4
=10 MHz / 4
= 2.5 MHz
Hence T = 1 / 2.5 MHz =0.4 microSeconds

i.e. 1 machine cycle generates delay of = 0.4 microSeconds

As we have to generate  delay  of 10 milliSeconds and we are using Timer0 in 16-bit mode i.e. 16-bit Timer,
Hence it can count maximum machine cycles of 65535 + 1 machine cycle of overflow =65536
Therefor Number of Machine cycles required to generate delay of 10 milliSeconds are = 25000 Machine cycles

Initial Value to be loaded in Timer Registers = Final Value + 1 - number of Machine cycles required
= 65535 +1 - 25000
= 40536 decimal
Hence Value to be loaded in TMR0L= 58 H, TMR0H= 9E H

##### Embedded C Program
#include <p18f4520.h>
#pragma config OSC=HS
#pragma config PWRT=OFF
#pragma config WDT=OFF
#pragma config DEBUG=ON, LVP=OFF
void main (void);
void Timerdelay();
void main ()
{
TRISD = 0x00;   //Direction of PORTD as an output
PORTD=0X00;    // PORTD as LOW
Timerdelay();  // Call Delay routine using Timer
PORTD=0XFF;   //PORTD as High
Timerdelay(); // Call Delay routine using Timer
}
void Timerdelay()
{
T0CON = 0x08;              //set up timer0 - 16-bit mode, No prescaler
TMR0H = 0X9E;         //Load initial value in TMR0H
TMR0L = 0X58;      //Load initial value in TMR0L
INTCON0bits.TMR0IF=0;    //Clear TMR0IF flag initially
T0CONbits.TMR0ON = 1;    // Start Timer0
while(INTCON0bits.TMR0IF==0) ///monitor TMR0 interrupt flag
T0CONbits.TMR0ON=0;         // Stop Timer0
INTCON0bits.TMR0IF=0;      //Clear TMR0IF flag
}

#### Embedded C Program

#include <p18f4520.h>
#pragma config OSC=HS
#pragma config PWRT=OFF
#pragma config WDT=OFF
#pragma config DEBUG=ON, LVP=OFF

void main (void);
void ISR(void);
void msdelay (unsigned int itime);
unsigned int data,I;
void main ()
{
T0CON = 0x06;                    //set up timer0 - prescaler 1:128
TMR0H = 0XFF;                    //clear timer
TMR0L = 0XFF;                    //clear timer
INTCONbits.TMR0IF = 0;
INTCONbits.TMR0IE = 1;
T0CONbits.TMR0ON = 1;
INTCONbits.PEIE = 1;
INTCONbits.GIE = 1;          //enable interrupts

TRISD = 0x00;
PORTD=0X00;
while (1)
{
data=0x01;
for(I=0;I<8;I++)
{
PORTD=data;
data=data<<1;
msdelay(6);
}
}

}

#pragma code Interrupt = 0x08 //High Priority interrupt location
void Interrupt(void)
{
_asm
_endasm
}
#pragma code   //End of code

#pragma interrupt ISR     ///redirect it from address location 00008 to another rogram

void ISR()
{
if (INTCONbits.TMR0IF==1)
{   PORTD=0X00;                               //check for TMR0 overflow
INTCONbits.TMR0IF = 0;            //clear interrupt flag
PORTD=0X55;
msdelay (20);
PORTD=0XAA;
msdelay (20);

}
}

void msdelay (unsigned int itime)
{
int i,j;
for(i=0;i<itime;i++)
for(j=0;j<1275;j++);
}

#### References

• Created and Edited by Prof Sujit Wagh SKNCOE, Pune
• WikiNote Foundation