Timer 1 interrupt and Timer Configuration PIC18f252, 20 MHZ

Microcontroller Topics
Post Reply
Eyenix
Corporal
Corporal
Posts: 5
Joined: Tue Jun 14, 2011 6:17 am

Timer 1 interrupt and Timer Configuration PIC18f252, 20 MHZ

Post by Eyenix » Thu Oct 27, 2011 7:41 pm

Code: Select all

/*
Copyright : Chathura Dhananjaya Yapa Bandara

		Eyenix Technologies
		137/4C, Main Street ,Rajamalwaththa, Baththaramulla

Compiler : Mikroc Pro (mikroelektronika)
Microcontroller : PIC18f252
Crystal:20Mhz
Objective : 16 bit Timer 1 interrupt handling & Testing the Timer capabilities
interrupt period : 0.1 s
Timer value : 1Second can be changed by changing the value of timeval variable
		when timeval =10 , timer period =1 Second

Description : Timer 1 interrupt will occur every 0.1 s. So the the time period 
can be handled by changing the timeval variable. here I have assigned it 10 which
is equivalent to 1 second. So by default the code will toggle portb every 1 second
*/



unsigned short count = 0;
unsigned short timeval = 10; // 10 x interrupt value (1Sec)


void main() {

	trisb=0;
	// Prescaler=1:8; TMR1 Preset=3036; Freq=20.00Hz; Period=.1
	T1CKPS1_bit = 1;// bits 5-4 Prescaler Rate Select bits
	T1CKPS0_bit = 1;//
	T1OSCEN_bit = 0;// bit 3 Timer1 Oscillator Enable Control: bit 1=on
	T1SYNC_bit = 1;// bit 2 Timer1 External Clock Input Synchronization Control bit: 1=Do not synchronize external clock input
	TMR1CS_bit = 0;// bit 1 Timer1 Clock Source Select bit: 0=Internal clock (FOSC/4) / 1 = External clock from pin T13CKI (on the rising edge)
	TMR1ON_bit = 1;// bit 0 enables timer : off to be enabled in the burst charge mode
	TMR1H = 0xB; // preset for timer1 MSB register
	TMR1L = 0xDC; // preset for timer1 LSB register
	PEIE_bit = 1; // pheripheral interrupt enable for

	TMR1IE_bit = 1; // Timer 1 interrupt enable.
	GIE_bit = 1; // Global interrupt enable
	portb = 0x00;
	while(1);
}

void interrupt()
{
	if (TMR1IF_bit)
	{
		TMR1IF_bit = 0;
		TMR1H = 0xB; // preset for timer1 MSB register
		TMR1L = 0xDC; // preset for timer1 LSB register
		count++;
		if(count==timeval)
		{

			// the work to be done which is toggle portb as an example

			count=0;
			portb=~portb;
		}
	}
}
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: Timer 1 interrupt and Timer Configuration PIC18f252, 20 MHZ

Post by Neo » Fri Oct 28, 2011 2:43 am

Hi Eyenix,

First of all welcome to ROBOT.LK. Our only aim is to help people with technical matters as much as possible without any type of financial gain. I just formatted your code with proper indents, while(1) {} is replaced with while(1); for indefinite looping, removed unnecessary spaces within code (to avoid a syntax highlighting bug) and kept your code under syntax=c tags for syntax highlighting. In overall it is a nice code. Well done.

Please put a brief introduction under Member Area -> Introductions and update your User Profile through User Control Panel (especially put a nice image) to identify your posts quickly. I highly value talented individuals from our country. Please continue to contribute to ROBOT.LK as much as you can.

Neo
User avatar
Nipuna
Moderator
Moderator
Posts: 2729
Joined: Mon Jan 04, 2010 8:02 pm
Location: Deraniyagala,SRI LANKA

Re: Timer 1 interrupt and Timer Configuration PIC18f252, 20 MHZ

Post by Nipuna » Fri Oct 28, 2011 8:09 am

Hi.

Welcome to robot.lk :)
Eyenix
Corporal
Corporal
Posts: 5
Joined: Tue Jun 14, 2011 6:17 am

Re: Timer 1 interrupt and Timer Configuration PIC18f252, 20 MHZ

Post by Eyenix » Fri Oct 28, 2011 10:40 am

Thanks guys .. There will be many codes in the near future....
nukleng
Corporal
Corporal
Posts: 3
Joined: Thu Jan 31, 2013 11:34 am

Re: Timer 1 interrupt and Timer Configuration PIC18f252, 20 MHZ

Post by nukleng » Thu Jan 31, 2013 12:03 pm

thx
Post Reply

Return to “Microcontrollers”