1 | /***********************************************************************
|
2 | Used µC : ATmega32U4
|
3 | Compiler : AVR Studio 5.1
|
4 | Configuration : External oscillator with 16 MHz. ************************************************************************
|
5 |
|
6 | /***********************************************************************************************Adjusted Fuse-Bits************************************
|
7 | **************************************************************************/
|
8 |
|
9 | /*
|
10 | Notice: [x] means programmed (enabled or 0), while [] mean unprogrammed (diasabled or 1)!
|
11 |
|
12 | BODLEVEL [4V3]
|
13 | HWBE [x]
|
14 | OCDEN []
|
15 | JTAGEN []...need to be disabled because of the ADC pins
|
16 | SPIEN []
|
17 | WDTON []
|
18 | EESAVE []
|
19 | BOOTSZ [2048W_3800]
|
20 | BOOTRST []
|
21 | CKDIV8 []
|
22 | CKOUT []
|
23 | SUT_CKSEL [EXTXOSC_8MHz_XX_258CK_65MS]
|
24 | */
|
25 |
|
26 | /**************************************************************************
|
27 | **************************** Header-files *********************************
|
28 | **************************************************************************/
|
29 |
|
30 | // <avr/io.h> integrates all the necessary PORT and register names.
|
31 | #include <avr/io.h>
|
32 |
|
33 | //<stdint.h> Integer types with predefined width such as uint8_t or int16_t.
|
34 | #include <stdint.h>
|
35 |
|
36 | //<stdbool.h> the use of Boolean. The type is bool.
|
37 | #include <stdbool.h>
|
38 |
|
39 | /***********************************************************************************************Variable Definition Global *********************
|
40 | **************************************************************************/
|
41 |
|
42 | #define F_CPU 16000000UL /* External quartz with 16 MHz */
|
43 |
|
44 | volatile uint32_t duty_cycle = 127;
|
45 |
|
46 | /**************************************************************************
|
47 | ************************ Declaration of functions *************************
|
48 | **************************************************************************/
|
49 |
|
50 | void ADC_Initialization(void);
|
51 |
|
52 | uint16_t ADC_Read_Value(uint8_t pin_channel);
|
53 |
|
54 | /**************************************************************************
|
55 | ******************************* Main program ******************************
|
56 | **************************************************************************/
|
57 | {
|
58 | bool sampling_end = false;
|
59 |
|
60 | uint8_t pin_channel = 0x04;
|
61 |
|
62 | // set Pin F4-7 as Input (ADC Input Channels).
|
63 | DDRF &= ~((1<<DDF4)|(1<<DDF5)|(1<<DDF6)|(1<<DDF7));
|
64 | // deactivate Pull-Up Resistor on Pin F4-7.
|
65 | PORTF &= ~((1<<PF4)|(1<<PF5)|(1<<PF6)|(1<<PF7));
|
66 |
|
67 | /**************************************************************************
|
68 | ****************************** Timer/Counter ******************************
|
69 | ****************************** PWM-generator ******************************
|
70 | **************************************************************************/
|
71 | TCCR0A|=(1<<WGM01)|(1<<WGM00); // Fast PWM-Mode
|
72 | TCCR0A|=(1<<COM0A1); // OC0A become low at a match and high at top.
|
73 |
|
74 |
|
75 | // PWM-frequency = timer clock/(Prescaler * 256)= 16 MHz/(1*256) = 62.5 kHz
|
76 | TCCR0B|=(1<<CS00) // Prescaler = 1.
|
77 |
|
78 | TCCR0B&=~(1<<WGM02); // TCNT0 run continuous after match until TOP reaches.
|
79 |
|
80 | OCR0A = duty_cycle; // Clock ratio/ trigger the PWM.
|
81 |
|
82 | DDRB|=(1<<PB7); // Set Pin P7 for OC0A as output.
|
83 |
|
84 | /**************************************************************************
|
85 | ******************************* Main Routine ******************************
|
86 | **************************************************************************/
|
87 | while(1)
|
88 | {
|
89 | // Sampling the current and voltage values.
|
90 | while(!sampling_end) // while sampling_end is not satisfied.
|
91 | {
|
92 | switch(pin_channel)
|
93 | {
|
94 | // First measurement of the input voltage at pin PF4.
|
95 | case 0x04: ADC_Initialization();
|
96 | u_in = ADC_Read_Value(pin_channel);
|
97 | pin_channel = 0x05;
|
98 | break;
|
99 | // Second measuring input current at pin PF5.
|
100 | case 0x05: ADC_Initialization();
|
101 | i_in = ADC_Read_Value(pin_channel);
|
102 | pin_channel = 0x06;
|
103 | break;
|
104 | // Third measurement output current at pin PF6.
|
105 | case 0x06: ADC_Initialization();
|
106 | i_out = ADC_Read_Value(pin_channel);
|
107 | pin_channel = 0x07;
|
108 | break;
|
109 | //Fourth measurement output voltage on pin PF7.
|
110 | case 0x07: ADC_Initialization();
|
111 | u_out = ADC_Read_Value(pin_channel);
|
112 | pin_channel = 0x04 sampling_end = true;
|
113 | break;
|
114 | default:
|
115 | break;
|
116 | }
|
117 | }
|
118 |
|
119 | sampling_end = false; // For the next measuring cycle.
|
120 |
|
121 | /**************************************************************************
|
122 | ********************************* Algorithm *******************************
|
123 | **************************************************************************/
|
124 | ( Hier wir das duty_cycle ermittelt
|
125 |
|
126 | } // end main routine
|
127 |
|
128 | )
|
129 |
|
130 | /**************************************************************************
|
131 | ************************** Definition of the functions ********************
|
132 | **************************************************************************/
|
133 |
|
134 | void ADC_Initialization(void)
|
135 | {
|
136 | ADMUX|=(1<<REFS0); // Set reference voltage to AVcc.
|
137 |
|
138 | ADCSRA|=(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0); // Set prescaler to 128.
|
139 |
|
140 | ADCSRA|=(1<<ADEN); // Activate converter.
|
141 |
|
142 | ADCSRA|=(1<<ADSC); //AD conversion is started
|
143 |
|
144 | while (ADCSRA & (1<<ADSC) )// Wait for completion of the conversion.
|
145 | {
|
146 | } /* ADCW must be read once, otherwise the result of the next conversion is not taken. */
|
147 |
|
148 | (void) ADCW;
|
149 | }
|
150 |
|
151 | uint16_t ADC_Read_Value(uint8_t pin_channel)
|
152 | {
|
153 | /* ADMUX &= ~(0x1F) means: delete only the first 5 bit position and then put them accordingly to pin_channel.*/
|
154 |
|
155 | ADMUX = (ADMUX & ~(0x1F)) | (pin_channel & 0x1F);
|
156 |
|
157 | ADCSRA |= (1<<ADSC); // one Conversion "single conversion"
|
158 |
|
159 | while (ADCSRA & (1<<ADSC) )
|
160 | { // Wait for completion of the conversion
|
161 | }
|
162 | return ADCW;
|
163 | }
|