PSoC-FDC1004Q
PSoC FDC1004Q Library
main.c
1 
6 #include "project.h"
7 #include "I2C_Interface.h"
8 #include "FDC1004Q_Defs.h"
9 #include "FDC1004Q.h"
10 #include "stdio.h"
11 
12 void Sensors_ProcessCapacitanceData(void);
13 
14 uint8_t capdac_values[4] = {0,0,0,0};
15 double capacitance_values[4] = {0,0,0,0};
16 
17 int main(void)
18 {
19  CyGlobalIntEnable; /* Enable global interrupts. */
20 
21  /* Place your initialization/startup code here (e.g. MyInst_Start()) */
22  I2C_Master_Start();
23  CyDelay(100);
24  UART_Start();
25  FDC_Start();
26  UART_PutString("FDC Library Test\n");
27 
28  CyDelay(1000);
29 
30  char message[60] = {'\0'};
31  uint16_t temp;
32  uint32_t cap;
33  uint8_t new_data = 0;
34  uint8_t counter = 0;
35 
36  for (uint8_t reg = 0; reg < 0x14; reg++)
37  {
38  uint8_t temp[2];
39  I2C_Peripheral_ReadRegisterMulti(0x50, reg, 2, temp);
40  sprintf(message, "0x%02X: 0x%04x\n", reg, (temp[1] << 8 | temp[0]));
41  UART_PutString(message);
42  }
43 
44 
45 
46  // Check if the sensor is connected in order to turn
47  // on the LED
48  for (int i = 0; i < 5; i++)
49  {
51  {
52  Connection_Led_Write(1);
53  break;
54  }
55  }
56 
57  // Set the sample rate to the default value
58  // of 100 Hz
59  uint8_t error = FDC_SetSampleRate(FDC_400_Hz);
60 
61 
62  error = FDC_ReadManufacturerId(&temp);
63  if (error == FDC_OK)
64  {
65  sprintf(message, "\n\nManufacturer ID: 0x%02X\r\n", temp);
66  UART_PutString(message);
67  }
68  else
69  {
70  UART_PutString("Could not read manufacturer ID\r\n");
71  }
72 
73  error = FDC_ReadDeviceId(&temp);
74  if (error == FDC_OK)
75  {
76  sprintf(message, "Device ID: 0x%02X\r\n", temp);
77  UART_PutString(message);
78  }
79 
80  // Disable all measurements
82  // Read all measurement registers
87 
92 
97 
99 
100  for (uint8_t reg = 0; reg < 0x15; reg++)
101  {
102  uint8_t temp[2];
103  I2C_Peripheral_ReadRegisterMulti(0x50, reg, 2, temp);
104  sprintf(message, "0x%02X: 0x%04x\n", reg, (temp[0] << 8 | temp[1]));
105  UART_PutString(message);
106  }
107 
108 
109 
110 
111  for(;;)
112  {
113  // Read register to determine if we have new data
114  uint8_t temp[2];
115  I2C_Peripheral_ReadRegisterMulti(0x50, 0x0C, 2, temp);
116 
117  // We have new data from all four channels
118  if ( (temp[1] & 0xF) == 0x0F)
119  {
120  // Counter to avoid sending a packet every time
121  counter ++;
122 
123  // Tune CAPDAC
124  Sensors_ProcessCapacitanceData();
125  if ( counter == 100)
126  {
127  // Print out capacitance and CAPDAC
128  for (uint8_t ch = 0; ch < 4; ch++)
129  {
130  sprintf(message, "%1d | %2d - %3d.%02d |\n", ch, capdac_values[ch],
131  (int)(capacitance_values[ch]),
132  ((int)(capacitance_values[ch]*100))%100);
133  UART_PutString(message);
134  }
135  UART_PutString("\n");
136  counter = 0;
137 
138  // Reset CAPDAC value
139  for (uint8 ch = 0; ch < 4; ch++)
140  {
141  capdac_values[ch] = 0;
142  FDC_ConfigureMeasurementInput(ch, ch, FDC_CAPDAC, capdac_values[ch]);
143 
144  }
145  }
146 
147  }
148  }
149 }
150 
151 void Sensors_ProcessCapacitanceData(void)
152 {
153  for (uint8_t ch = 0; ch < 4; ch++)
154  {
155  // Read measurement
156  FDC_ReadMeasurement(ch, &capacitance_values[ch]);
157 
158  if ( capacitance_values[ch] > (15 + ((capdac_values[ch]) * FDC_CAPDAC_FACTOR)))
159  {
160  // Increase CAPDAC
161  if (capdac_values[ch] < 31)
162  {
163  capdac_values[ch] += 1;
164  //capdac_values[ch] &= 0x1F;
165  FDC_ConfigureMeasurementInput(ch, ch, FDC_CAPDAC, capdac_values[ch]);
166  }
167  }
168 
169  }
170 }
171 
172 /* [] END OF FILE */
#define FDC_CH_1
FDC Capacitance channel 1.
#define FDC_RP_CH_4
Repeat measurement for channel 4.
#define FDC_IN_4
Capacitance channel 4.
Hardware specific I2C interface.
#define FDC_CH_2
FDC Capacitance channel 2.
Register map for the FDC1004Q component.
#define FDC_CH_4
FDC Capacitance channel 4.
#define FDC_400_Hz
400 Samples/second sample rate.
uint8_t FDC_DisableRepeatMeasurement(void)
Disable repated measurement.
Definition: FDC1004Q.c:349
Header file for the FDC1004Q driver.
#define FDC_CAPDAC
CAPDAC input.
uint8_t FDC_ReadManufacturerId(uint16_t *id)
Read the manufacturer ID from the sensor.
Definition: FDC1004Q.c:566
uint8_t FDC_ReadDeviceId(uint16_t *id)
Read the device ID from the sensor.
Definition: FDC1004Q.c:580
#define FDC_RP_CH_2
Repeat measurement for channel 2.
uint8_t FDC_StopMeasurement(uint8_t channel)
Stop measurement for given channel.
Definition: FDC1004Q.c:290
uint8_t FDC_IsDeviceConnected(void)
Check if device is connected on the I2C bus.
Definition: FDC1004Q.c:73
#define FDC_IN_3
Capacitance channel 3.
#define FDC_CAPDAC_FACTOR
CAPDAC multiplying factor.
Definition: FDC1004Q_Defs.h:14
uint8_t FDC_ReadRawMeasurement(uint8_t channel, uint32_t *capacitance)
Read capacitance measurement in raw format.
Definition: FDC1004Q.c:420
uint8_t FDC_ReadMeasurement(uint8_t channel, double *capacitance)
Read capacitance measurement in double format.
Definition: FDC1004Q.c:439
uint8_t FDC_EnableRepeatMeasurement(uint8_t channel_flags)
Enable repeated measurement.
Definition: FDC1004Q.c:321
#define FDC_OK
No error occurred.
#define FDC_RP_CH_3
Repeat measurement for channel 3.
uint8_t FDC_Start(void)
Start the FDC1004Q.
Definition: FDC1004Q.c:58
#define FDC_IN_2
Capacitance channel 2.
uint8_t FDC_SetSampleRate(uint8_t sampleRate)
Set up sample rate for continuous measurements.
Definition: FDC1004Q.c:125
#define FDC_RP_CH_1
Repeat measurement for channel 1.
#define FDC_IN_1
Capacitance channel 1.
#define FDC_CH_3
FDC Capacitance channel 3.
uint8_t FDC_ConfigureMeasurementInput(uint8_t meas_channel, uint8_t pos_channel, uint8_t neg_channel, uint8_t capdac)
Configure measurement input settings.
Definition: FDC1004Q.c:364
I2C_ErrorCode I2C_Peripheral_ReadRegisterMulti(uint8_t device_address, uint8_t register_address, uint8_t register_count, uint8_t *data)
Read multiple bytes over I2C.
Definition: I2C_Interface.c:71