Hey guys,
I’m having some pretty weird problems crop up when using timers.
Here is an example bit of code:
//Assign and initiate threads
Thread AdcHandler = new Thread(AdcThread);
Thread GpsHandler = new Thread(GpsThreadStart);
//Thread NavHandler = new Thread(NavThread);
Thread TxHandler = new Thread(TxThread);
//Set thread priorities
AdcHandler.Priority = ThreadPriority.Lowest;
GpsHandler.Priority = ThreadPriority.Normal;
TxHandler.Priority = ThreadPriority.BelowNormal;
//Start threads
AdcHandler.Start();
//GpsHandler.Start();
//TxHandler.Start();
Timer NavTimer =
new Timer(new TimerCallback(NavThread), null, 0, 500);
Thread.Sleep(Timeout.Infinite);
//Timeout
private static void AdcThread()
{
while (true)
{
//Update battery reading and LEDs, loop every 10s
adc.AdcUpdate();
Thread.Sleep(10000);
}
}
public static void NavThread(object o)
{
Debug.Print("Nav Thread");
}
Basically, when all Thread.Start()s are commented out, this code works as expected. “Nav Thread” is debugged about every 500ms.
On the other hand, if I uncomment even a single Thread.Start(), “Nav Thread” will print just once, and never again.
What am I doing wrong?
With all threads commented:
‘Microsoft.SPOT.Debugger.CorDebug.dll’ (Managed): Loaded ‘F:\Visual Studio 2010\Projects\DaveDaveVeh\DaveDaveVeh\bin\Release\le\DaveDaveVeh.exe’, Symbols loaded.
The thread ‘’ (0x2) has exited with code 0 (0x0).
Nav Thread
Nav Thread
Nav Thread
Nav Thread
Nav Thread
Nav Thread
Nav Thread
Nav Thread
Nav Thread
Nav Thread
With just one thread uncommented, as above:
‘Microsoft.SPOT.Debugger.CorDebug.dll’ (Managed): Loaded ‘F:\Visual Studio 2010\Projects\DaveDaveVeh\DaveDaveVeh\bin\Release\le\DaveDaveVeh.exe’, Symbols loaded.
The thread ‘’ (0x2) has exited with code 0 (0x0).
Nav Thread
Just to confirm - the ADC thread, and all the other ones for that matter, work perfectly. There is no blocking involved, nothing problematic at all. Here is the code for the ADC class:
public ADC(){
battLevel = new AnalogIn((AnalogIn.Pin)FEZ_Pin.AnalogIn.An4);
//conLevel = new AnalogIn((AnalogIn.Pin)FEZ_Pin.AnalogIn.An5);
battLevel.SetLinearScale(0, 3300);
//conLevel.SetLinearScale(0, 1000);
Cpu.Pin[] battPins = new Cpu.Pin[8]{
(Cpu.Pin)FEZ_Pin.Digital.Di39,
(Cpu.Pin)FEZ_Pin.Digital.Di41,
(Cpu.Pin)FEZ_Pin.Digital.Di43,
(Cpu.Pin)FEZ_Pin.Digital.Di45,
(Cpu.Pin)FEZ_Pin.Digital.Di47,
(Cpu.Pin)FEZ_Pin.Digital.Di49,
(Cpu.Pin)FEZ_Pin.Digital.Di51,
(Cpu.Pin)FEZ_Pin.Digital.Di50
};
battPort = new ParallelPort(battPins, (Cpu.Pin)FEZ_Pin.Digital.Di48,Cpu.Pin.GPIO_NONE);
}
public void AdcUpdate()
{
mVolts = Batt2mVolt(battLevel.Read());
battPort.Write(new byte[1] { RangeBattLeds(mVolts) }, 0, 1);
}
private int Batt2mVolt(int batt)
{
return batt * 369 / 113;
}
private byte RangeBattLeds(int mVolt)
{
if (mVolt > 7800)
return 0x7F;
if (mVolt > 7500)
return 0x3F;
if (mVolt > 7200)
return 0x1F;
if (mVolt > 6900)
return 0x0F;
if (mVolt > 6600)
return 0x07;
if (mVolt > 6300)
return 0x03;
if (mVolt > 6000)
return 0x01;
else
return 0x00;
}