MCPcopy Create free account
hub / github.com/WiringPi/WiringPi / myAnalogRead

Function myAnalogRead

wiringPi/mcp3422.c:69–106  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

67 */
68
69int myAnalogRead (struct wiringPiNodeStruct *node, int chan)
70{
71 unsigned char config ;
72 unsigned char buffer [4] ;
73 int value = 0 ;
74 int realChan = (chan & 3) - node->pinBase ;
75
76// One-shot mode, trigger plus the other configs.
77
78 config = 0x80 | (realChan << 5) | (node->data0 << 2) | (node->data1) ;
79
80 wiringPiI2CWrite (node->fd, config) ;
81
82 switch (node->data0) // Sample rate
83 {
84 case MCP3422_SR_3_75: // 18 bits
85 waitForConversion (node->fd, &buffer [0], 4) ;
86 value = ((buffer [0] & 3) << 16) | (buffer [1] << 8) | buffer [2] ;
87 break ;
88
89 case MCP3422_SR_15: // 16 bits
90 waitForConversion (node->fd, buffer, 3) ;
91 value = (buffer [0] << 8) | buffer [1] ;
92 break ;
93
94 case MCP3422_SR_60: // 14 bits
95 waitForConversion (node->fd, buffer, 3) ;
96 value = ((buffer [0] & 0x3F) << 8) | buffer [1] ;
97 break ;
98
99 case MCP3422_SR_240: // 12 bits - default
100 waitForConversion (node->fd, buffer, 3) ;
101 value = ((buffer [0] & 0x0F) << 8) | buffer [1] ;
102 break ;
103 }
104
105 return value ;
106}
107
108
109/*

Callers

nothing calls this directly

Calls 2

wiringPiI2CWriteFunction · 0.85
waitForConversionFunction · 0.85

Tested by

no test coverage detected