using System;
using System.Threading; // 追加
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware; // 追加、参照設定にも追加
using GHI.Pins;
using BoeBotLib;
namespace DustSensorTest
{
public class Program
{
// OutputPort LED1 = new OutputPort(Pin.PC0, false);
static DigitalOut LED1 = new DigitalOut(CPU.P22);
private static Microsoft.SPOT.Hardware.InterruptPort _interruptPort;
public static void Main()
{
// Console.setBaud();
new Program().main();
}
private void main()
{
DigitalIn trigger = new DigitalIn(CPU.P06);
I2CLCD lcd = new I2CLCD(8);
lcd.setIcon(0x0fff);
// P05
PWM LED = new PWM(Cpu.PWMChannel.PWM_1, period: 10000, duration: 320,
scale: PWM.ScaleFactor.Microseconds, invert:false);
ADcon sensor = new ADcon(CPU.P23);
LED.Start();
double coef = 3300.0 / 4096;
double v = 0,v2=0;
double alpha=0.01;
int i = 0;
while (true)
{
while (trigger.Read() == false) ;
BoeBotLib.Timer.delay(1300);//実測280μs
v = v * (1 - alpha) + alpha * sensor.Value;
if (i++ > 50)
{
String str = (v * coef).ToString();
str = str.Substring(0, 5)+" mV";
//Debug.Print(str);
lcd.home();
lcd.write(str);
if (v2 == 0) v2 = v;
v2 = v2 * (1 - alpha) + alpha * v;
str= (v2 * coef).ToString();
str = str.Substring(0, 5);
lcd.write(str.Substring(0, 5));
i = 0;
}
LED1.Write(true);
CPU.delay(1);
LED1.Write(false);
}
}
static void pwm_OnInterrupt(uint data1, uint data2, DateTime time)
{
LED1.Write(!LED1.Value);
_interruptPort.EnableInterrupt();
}
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|