Projekt

Allgemein

Profil

Example code » Historie » Version 1

Maximilian Seesslen, 27.01.2026 16:16

1 1 Maximilian Seesslen
h1. Example code
2
3
<pre><code class="cpp">
4
TSC_HandleTypeDef htsc;
5
void MX_TSC_Init(void) 
6
{ 
7
   htsc.Instance = TSC;
8
   htsc.Init.CTPulseHighLength = TSC_CTPH_2CYCLES;
9
   htsc.Init.CTPulseLowLength = TSC_CTPL_2CYCLES;
10
   htsc.Init.SpreadSpectrum = DISABLE;
11
   htsc.Init.SpreadSpectrumDeviation = 0;
12
   htsc.Init.SpreadSpectrumPrescaler = TSC_SS_PRESC_DIV1;
13
   htsc.Init.PulseGeneratorPrescaler = TSC_PG_PRESC_DIV4;
14
   htsc.Init.MaxCountValue = TSC_MCV_16383;
15
   htsc.Init.IODefaultMode = TSC_IODEF_OUT_PP_LOW;
16
   htsc.Init.SynchroPinPolarity = TSC_SYNC_POLARITY_FALLING;
17
   htsc.Init.AcquisitionMode = TSC_ACQ_MODE_NORMAL;
18
   htsc.Init.MaxCountInterrupt = DISABLE;
19
   if (HAL_TSC_Init(&htsc) != HAL_OK) 
20
   { 
21
      Error_Handler(); 
22
   } 
23
   // IOs konfigurieren
24
   TSC_IOConfigTypeDef IoConfig;
25
   IoConfig.ChannelIOs = TSC_GROUP1_IO1; // PA0
26
   IoConfig.SamplingIOs = TSC_GROUP1_IO2; // PA1
27
   IoConfig.ShieldIOs = 0;
28
   HAL_TSC_IOConfig(&htsc, &IoConfig);
29
}
30
31
32
uint16_t TSC_Read(void)
33
{ 
34
   // Start acquisition
35
   HAL_TSC_Start(&htsc);
36
   // Warten bis fertig
37
   HAL_TSC_PollForAcquisition(&htsc);
38
   // Ergebnis holen (Gruppe 1)
39
   uint16_t value = HAL_TSC_GroupGetValue(&htsc, TSC_GROUP1_IDX);
40
41
   // TSC wieder freigeben
42
   HAL_TSC_Stop(&htsc); 
43
   return value; 
44
}
45
46
47
int main(void)
48
{
49
   HAL_Init(); 
50
   SystemClock_Config();
51
   MX_GPIO_Init();
52
   MX_TSC_Init();
53
   while (1)
54
   {
55
      uint16_t val = TSC_Read();
56
      if (val < 3000)
57
      {
58
         // Taste berührt
59
      } 
60
      else
61
      {
62
         // Taste nicht berührt
63
      }
64
      HAL_Delay(10);
65
   }
66
} 
67
</code></pre>
68
69
nicht berührt:	4000–8000
70
berührt:   	500–3000