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.

Tachometer, works in an ISIS simulation but doesn't work in reality.

Status
Not open for further replies.

ItsMike

New Member
Hey guys,
For some reason this code works in an ISIS simulation but does not work in reality.

I've did some tests earlier by commenting the tachometer() call i get the leds to shift. I also used the testir() function to test the opto interrupter and it toggled all the leds perfectly.

Code:
#include	<htc.h>
//#include "font.c"
__CONFIG(HS & WDTDIS & BORDIS & LVPEN & DUNPROT & WRTEN & DEBUGDIS & UNPROTECT);
#define _XTAL_FREQ 20000000
#define s_data RD2
#define s_latch RD1
#define s_clk RD0
void output8(unsigned char data);
void output16(unsigned int data);
void tachometer();
volatile unsigned int tcount, rcount;
unsigned int RPM;
volatile bit start;
/*
void interrupt INTTEST()
{
	if(INTE && INTF)			//External Interrupt
		{
			tcount=~tcount;
			output16(tcount);
			INTF=0;
		}	
}	

void testir()
{
	TRISA=0;
	RA0=1;
 	GIE=1;
 	INTE=1;
	INTEDG=1;
	INTF=0;
	tcount=0;
	output16(0);
	while(1)
	{
		
	}			
}	
*/
void main()
{
 unsigned char i;
 TRISD=0;
 PORTD=0;
 output16(0xAAAA);
 __delay_ms(1000);
 output16(0x5555);
 __delay_ms(1000);
 while(1)
 {
	 //testir();
	 tachometer();
	 output16(0);
	 for(i=0;i<16;i++)
	 {
		output16(1<<i);
		__delay_ms(20);	 
	 }
	 
	 
 }
}

void tachometer()
{
TRISA=0;
RA0=1;	
// Timer1 Registers: 
// Prescaler=1:8; TMR1 Preset=3036; Freq=10.00Hz; Period=0.10 s
T1CKPS1 = 1;// bits 5-4  Prescaler Rate Select bits
T1CKPS0 = 1;
TMR1CS  = 0;// bit 1 Timer1 Clock Source Select bit: 0=Internal clock (FOSC/4) / 1 = External clock from pin T1CKI (on the rising edge)
TMR1ON  = 0;// bit 1 enables timer
TMR1H = 0xB;     // preset for timer1 MSB register
TMR1L = 0xDC;     // preset for timer1 LSB register

GIE=1;
TMR1IE=1;
PEIE = 1;
TMR1IF=0;

start=1;

INTE=1;
INTEDG=1;
INTF=0;

output16(0);
while(1)
 {	
	
 }
}	

void interrupt Timer1()
{
 if(TMR1IE && TMR1IF)	//Timer 1 Interrupt - every 100ms
 {
	TMR1H = 0xB;    
   TMR1L = 0xDC;  
	tcount++;
	TMR1IF=0;
 }
 if(INTE && INTF)			//External Interrupt
 {
	if(start)				//First Rotation of Massurment ?
	{
		start=0;
		tcount=0;				
		rcount=0;
   	TMR1ON=1;	
	} 
	else
	{
		rcount++;
		if(tcount==20)		//Has 2 Seconds Passed ?
		{
			TMR1ON=0;
			INTE=0;
			RPM=rcount*30;
			output16(RPM);
			tcount=0;		//Reset Counters and Timers
			rcount=0;
			TMR1H=0xB;
			TMR1L=0xDC;
			start=1;
		}	
	}	
	INTE=1;
	INTF=0; 
 }  
}	

void output8(unsigned char data)
{
	unsigned char i;
	s_latch=0;
	for(i=0;i<8;i++)
	{
		s_clk=0;
		//s_data=((data<<i)&0x80)!=0;
		s_data=(data>>i)&1;
		s_clk=1;
	}	
	s_latch=1;
}	
void output16(unsigned int data)
{
	unsigned char i;
	s_latch=0;
	for(i=0;i<16;i++)
	{
		s_clk=0;
		//s_data=((data<<i)&0x80)!=0;
		s_data=(data>>i)&1;
		s_clk=1;
	}	
	s_latch=1;
}
 

Attachments

  • tachometer.jpg
    tachometer.jpg
    295.7 KB · Views: 367
Last edited:
In ISIS all variables are set to 0 by default ( there is a option in micro properties to make the ram random.) change this option and then if it goes "&*$%" up at least you can debug.

Do you have pickit2 or any other ICD? if you do you can run direct from MPLAB to check your code...

Cheers Ian
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top