1 | /********************************************************************************************/
|
2 | /* Include-files ****************************************************************************/
|
3 | /********************************************************************************************/
|
4 |
|
5 | #include <STM32F10x.h>
|
6 |
|
7 | /********************************************************************************************/
|
8 | /* Variablen ********************************************************************************/
|
9 | /********************************************************************************************/
|
10 |
|
11 | int ADWert, AD_Val;
|
12 |
|
13 | /********************************************************************************************/
|
14 | /* Hauptprogramm Main ***********************************************************************/
|
15 | /********************************************************************************************/
|
16 |
|
17 | void init(void)
|
18 | {
|
19 |
|
20 | RCC -> APB2ENR |= 1 << 6; // Enable GPIOE clock
|
21 | RCC -> APB2ENR |= 1 << 5; // Enable GPIOD clock
|
22 | RCC -> APB2ENR |= 1 << 4; // Enable GPIOC clock
|
23 | GPIOD -> CRL &= 0x00000000;
|
24 | GPIOD -> CRL = 0x88888888;
|
25 | GPIOD -> CRH = 0x88888338;
|
26 | GPIOD -> BSRR = 0 << 10;
|
27 | GPIOD -> BSRR = 0 << 9;
|
28 | GPIOC -> CRL &= 0x00000000;
|
29 | GPIOC -> CRL = 0xBB888888;
|
30 | GPIOE -> CRH = 0x33333333; // Configure the GPIO for LEDs
|
31 |
|
32 | RCC -> APB2ENR |= 1 << 9; // Enable ADC1 clock
|
33 | ADC1 -> SQR1 = 0x00000000; // Regular channel 1 conversion
|
34 | ADC1 -> SQR2 = 0x00000000; // Clear register
|
35 | ADC1 -> SQR2 = 0x00000000; // Clear register
|
36 | ADC1 -> SQR3 = 14 << 0; // SQ1 = channel 14
|
37 | ADC1 -> SMPR1 = 5 << 12; // Channel 14 sample time is 55.5 cyc
|
38 | ADC1 -> SMPR2 = 0x00000000; // Clear register
|
39 | ADC1 -> CR1 = 1 << 8; // Scan mode on
|
40 | ADC1 -> CR2 = (1 << 20) | // Enable external trigger
|
41 | (7 << 17) | // EXTSEL = SWSTART
|
42 | (1 << 1) | // Continuous conversion
|
43 | (1 << 0) ; // ADC enable
|
44 | ADC1 -> CR2 |= 1 << 3; // Initialize calibration registers
|
45 | while (ADC1->CR2 & (1 << 3)) ; // Wait for initialization to finish
|
46 | ADC1 -> CR2 |= 1 << 2 ; // Start calibration
|
47 | while (ADC1->CR2 & (1 << 2)) ; // Wait for calibration to finish
|
48 | ADC1 -> CR2 |= 1 << 22 ; // Start first conversion
|
49 |
|
50 | // Konfiguration der GPIO für Push Buttons
|
51 |
|
52 | RCC -> CFGR = 0x001d8402;
|
53 | RCC -> CR = 0x01000081;
|
54 |
|
55 | while(RCC -> CR == (1<<1))
|
56 | {
|
57 | __nop();
|
58 | }
|
59 |
|
60 | //RCC -> APB2ENR |= 1 << 3; // Enable GPIOB clock
|
61 | RCC -> APB2ENR |= 1 << 0; // Alternate function Clock
|
62 | RCC -> APB2ENR |= 1 << 13; // Enable Timer 8 clock
|
63 |
|
64 | TIM8 -> CR1 = 0x0060; // Das Auto-Reload Register wird gepuffert; Center Aligned Mode 3, Auf- und Abzählen. Es wird bei Überlauf und Unterlauf ein Event ausgelöst.
|
65 | TIM8 -> CR2 = 0x0000; // Reset Kontrollregister 2
|
66 | TIM8 -> PSC = 0x0032; // Prescaler
|
67 | TIM8 -> ARR = 0x0FFF; // Auto-Reload Register
|
68 | TIM8 -> CCMR1 = 0x6060; // Compare Mode Register 1 Outputcompare Mode
|
69 | TIM8 -> CCMR2 = 0x0000; // Compare Mode Register 2 Output Compare 3 Mode; Output Compare 4 Mode
|
70 | TIM8 -> CCER = 0x0011; // Compare Enable Register; Compare 3 Output Enable; Compare 4 Output Enable
|
71 | TIM8 -> SMCR = 0x0000; // Slave Mode Kontroll Register
|
72 | TIM8 -> CCR1 = 0x0000; // Compare Register 1
|
73 | TIM8 -> CCR2 = 0x0000; // Compare Register 2
|
74 | TIM8 -> SR = 0x0000; // Status Register: Interrupt Flagbits
|
75 | TIM8 -> DIER = 0x0006; // Interrupt Enable Register: Interrupt Compare 3 und Compare 4 zugelassen
|
76 | TIM8 -> EGR = 0x0003; // Event Generator Register: Update generation; Compare 3 Generation / Compare 4 Generation
|
77 | TIM8 -> CR1 = 0x0061; // Timer 8 starten
|
78 |
|
79 | for (;;)
|
80 | { // Loop forever
|
81 | if (ADC1->SR & (1 << 1)) // If conversion has finished
|
82 | {
|
83 | AD_Val = ADC1->DR & 0x0FFF; // Read AD converted value
|
84 | ADC1->CR2 |= 1 << 22; // Start new conversion
|
85 | }
|
86 |
|
87 | ADWert = AD_Val << 4;
|
88 | GPIOE -> BSRR = ADWert;
|
89 | GPIOE -> BSRR |= 0xFF000000;
|
90 |
|
91 | TIM8 -> CCR1 = ADC1 -> DR; // Compare Register 3; Vergleichsregister mit dem AD-Wert laden
|
92 |
|
93 | }
|
94 |
|
95 | }
|