libpropeller
Making PropellerGCC Easier
 All Classes Files Functions Variables Enumerations Enumerator Macros Pages
mcp3208.test.h
Go to the documentation of this file.
1 #include "unity.h"
2 #include "c++-alloc.h"
3 
5 
6 #include "libpropeller/board/board_unit_tester.h"
7 
9 
10 
11 const int kChannelDac = 3;
12 const int kChannel5vOver2 = 4;
13 const int kChannel3v3Over2 = 5;
14 const int kChannelGround = 6;
15 const int kChannel3v3 = 7;
16 
17 
18 // Should be 4096 (for 3.3v)
19 // On my board, 3.3v is actually 3.27v
20 // Should be 4058 (for 3.27v)
21 const int kValue3v3 = 4058;
22 // Should be 3103 (for 5v)
23 // On my board, 5v is actually 4.71v
24 // Should be 2923 (for 4.71v)
25 const int kValue5vOver2 = 2923;
26 const int kValue3v3Over2 = kValue3v3 / 2;
27 const int kValueGround = 0;
28 
29 const int delta = 25;
30 
31 const int kMode = 0xFF;
32 
33 class UnityTests {
34 public:
35 
36  static void setUp(void) {
37  sut = new MCP3208();
38  sut->Start(Board::kPinMCP3208Data,
39  Board::kPinMCP3208Clock,
40  Board::kPinMCP3208Select, kMode,
41  Board::kPinDac);
42  waitcnt(CLKFREQ / 100 + CNT);
43  }
44 
45  static void tearDown(void) {
46  sut->Stop();
47  delete sut;
48  sut = NULL;
49  }
50 
51  static void test_3v3(void) {
52 
53  TEST_ASSERT_INT_WITHIN(delta, kValue3v3, sut->In(kChannel3v3));
54  }
55 
56  static void test_Ground(void) {
57  // Should be 0
58  TEST_ASSERT_INT_WITHIN(delta, kValueGround, sut->In(kChannelGround));
59  }
60 
61  static void test_3v3Over2(void) {
62  TEST_ASSERT_INT_WITHIN(delta, kValue3v3Over2, sut->In(kChannel3v3Over2));
63  }
64 
65  static void test_5vOver2(void) {
66  TEST_ASSERT_INT_WITHIN(delta, kValue5vOver2, sut->In(kChannel5vOver2));
67  }
68 
69  static void test_Average(void) {
70  TEST_ASSERT_INT_WITHIN(delta, kValue3v3Over2, sut->Average(kChannel3v3Over2, 16));
71 
72  }
73 
74  static void test_DAClow(void) {
75  sut->Out(0);
76  waitcnt(CLKFREQ / 100 + CNT);
77  TEST_ASSERT_INT_WITHIN(delta, kValueGround, sut->In(kChannelDac));
78  }
79 
80  static void test_DACHigh(void) {
81  sut->Out(0xFFFF);
82  waitcnt(CLKFREQ / 10 + CNT);
83  // For some reason the 3.3v from the Propeller and the 3.3v from the power
84  // rail are significantly different. Hence, the need for the explicit constant.
85  TEST_ASSERT_INT_WITHIN(delta, 4096, sut->In(kChannelDac));
86  }
87 
88  static void test_DACMiddle(void) {
89  sut->Out(0xFFFF >> 1);
90  waitcnt(CLKFREQ / 100 + CNT);
91  TEST_ASSERT_INT_WITHIN(delta, kValue3v3Over2, sut->In(kChannelDac));
92  }
93 
94 
95 
96 };
97