Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

getting timer1 to work (pic16f628a)

Status
Not open for further replies.

normad

Member
hey guys im trying to use timer1 to measure time.. Ive enabled interrupts set up timer 1 in timer mode and initialized it to 53036 so that it'll give a 0.1 second interrupt. Im running on an external 4Mhz crystal.

i dont know whats wrong :( its a very simple code but the interrupt isnt occuring :( please help

my displays are working fine.. but its only displaying 123456 its not incrementing
Code:
#include <htc.h>

__CONFIG(MCLREN & UNPROTECT & WDTDIS & XT & PWRTDIS & LVPDIS & PWRTDIS & BORDIS);

#define _XTAL_FREQ 4000000

unsigned char a = 1, b = 2,c = 3,d = 4,e = 5,f = 6;
unsigned int len_thrs = 60 , time = 0 , count = 0;

void init_mpu(void);
void disp(void);
void disp_thrs(void);

void main(void) {

	init_mpu();	
	disp();
	while(1) {
	
	}
}



void init_mpu(void) {
	
	TRISA = 0b00010000; // 00010000
	TRISB = 0b11000001; // 11000001

	PORTA = 0x00;
	PORTB = 0x00;

	ei(); //enable global interrupts

	TMR1CS = 0;
	T1CKPS0 = 1;
	T1CKPS0 = 1;
	TMR1IF = 0;
	TMR1ON = 0;
	T1OSCEN = 0;
	TMR1L = 0b11100100; 
	TMR1H = 0b00101101;
	TMR1ON = 1;

	TMR1IE = 1; //enable timer 1 interrupt


}

void disp(void) {


	PORTA = f;
	RB1 = 0;
	RB2 = 0;
	RB3 = 0;
	RB4 = 1;
	__delay_us(10);
	RB4 = 0;

	PORTA = e;
	RB1 = 1;
	RB2 = 0;
	RB3 = 0;
	RB4 = 1;
	__delay_us(10);
	RB4 = 0;

	PORTA = d;
	RB1 = 0;
	RB2 = 1;
	RB3 = 0;
	RB4 = 1;
	__delay_us(10);
	RB4 = 0;

	PORTA = c;
	RB1 = 1;
	RB2 = 1;
	RB3 = 0;
	RB4 = 1;
	__delay_us(10);
	RB4 = 0;

	PORTA = b;
	RB1 = 0;
	RB2 = 0;
	RB3 = 1;
	RB4 = 1;
	__delay_us(10);
	RB4 = 0;

	PORTA = a;
	RB1 = 1;
	RB2 = 0;
	RB3 = 1;
	RB4 = 1;
	__delay_us(10);
	RB4 = 0;

}

static void interrupt int_handler(void)  {

	if(TMR1IF) {

		TMR1IF = 0;
		TMR1ON = 0;
		TMR1L = 0b11100100; 
		TMR1H = 0b00101101;
		TMR1ON = 1;
		time++;

		if(time>10) {
			time = 0;
			a++;
			b++;
			c++;
			d++;
			e++;
			f++;
			disp();
		}
	
	}

}
 
You never actually turn on the interrupts.
 
im sorry this is my first pic project :) what do you mean turn on interrupts? im enabling the global interrupts and the timer1 interrupt but how do you turn on an interrupt?
 
You also have to set PEIE (peripheral interrupt enable) which is bit 6 of intcon.

Mike.
 
Last edited:
Status
Not open for further replies.

Latest threads

Back
Top