chipKIT® Development Platform

Inspired by Arduino™

Howto switch off timers, etc for max performance?

Created Mon, 09 Mar 2015 13:42:52 +0000 by Magnum


Magnum

Mon, 09 Mar 2015 13:42:52 +0000

Hello,

I am working on a small project where I want to "listen" to an I2C communication between two boards. As I don't want to disturb the communication I can't use the built in I2C from the chipkit. I found an I2C-sniffer example and adapted the code for what I need. It is working quite well, but sometimes I get errors. I am using the uC32 and Max32, so I think processor speed should be sufficient. But I don't know if timers, interrupts or whatever work in the background and are a cause for the errors I get. I used T1CON = 0x0, T2CON = 0x0 and T3CON = 0x0 to switch off the timers. Is there more I can do?

Thanks,

Magnus


Gary Freegard

Thu, 14 May 2015 15:58:47 +0000

Hi Magnus

The core timer and its interrupt are used by the delay method and by default is on, to turn off the interrupt you use

mCTIntEnable(0);//1 to enable

This will mean that the delay method will no longer work, if delays are required then you could use this

void delayMS(unsigned int time)
{
	for(int count = 0; count < time; count+= 1){
		writeCoreTimer(0);
		while(readCoreTimer()  < 24000);// 24MHz/24000 =1000 (1ms). 
	}
}

This is for a device running at 48MHz, the core timer runs at half the processor speed (24MHz).

If a delay is needed that is less than a millisecond then try this

void delayUS(unsigned int time)
{
	for(int count = 0; count < time; count+= 1){
		writeCoreTimer(0);
		while(readCoreTimer()  < 24);// 24MHz/24 =1000000 (1us). 
	}
}

In both cases the value in the while loop may need to be 'calibrated' to improve accuracy.

Hope this helps

Gary