Überprüfung meines Teilcodes

OP #3430494
Lesenswert?

Hallo,

ich versuche gerade eine PWM für einen DC-DC Wandler zu erzeugen. Dazu 
erfasse ich die benötigten Größen über einen ADC, berechne die benötigte 
PWM und gib diese raus.
Den Teilcode mit der ADC Erfassung und der PWM Einstellung habe ich euch 
mal hier rein kopiert. Nun da ich noch ein ziemlicher Anfänger auf 
diesem Gebiet bin, bitte ich euch mal über meinen Teilcode rüber zu 
sehen und mir eure Meinung mit zu teilen. Kann das so, wie ich es 
geschrieben habe auch funktionieren?

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
}
#3430502
Lesenswert?

1
    case 0x04:  ADC_Initialization();
2
    u_in = ADC_Read_Value(pin_channel);

du brauchst doch den ADC nicht dauernd immer wieder neu zu 
initialisieren.
Initialisierungen macht man einmal am Programmanfang und gut ists.


Warum so kompliziert
1
 // Sampling the current and voltage values.
2
while(!sampling_end) // while sampling_end is not satisfied.
3
 {  
4
   switch(pin_channel)
5
   {  
6
      // First measurement of the input voltage at pin PF4.
7
    case 0x04:  ADC_Initialization();
8
    u_in = ADC_Read_Value(pin_channel); 
9
    pin_channel = 0x05;
10
    break;
11
     // Second measuring input current at pin PF5.
12
    case 0x05:  ADC_Initialization();
13
    i_in = ADC_Read_Value(pin_channel);
14
           pin_channel = 0x06;
15
           break;
16
     // Third measurement output current at pin PF6.
17
    case 0x06:    ADC_Initialization();
18
    i_out = ADC_Read_Value(pin_channel);
19
    pin_channel = 0x07;
20
    break;
21
     //Fourth measurement output on pin PF7.
22
    case 0x07:  ADC_Initialization();
23
    u_out = ADC_Read_Value(pin_channel);
24
    pin_channel = 0x04                   sampling_end = true;
25
    break;    
26
    default:
27
    break;
28
   }
29
 }

wenn ich mir deine Variablen und deren Veränderung ansehe (und mir das 
Initialize vorgezogen denke), dann steht da nichts anderes als
1
    u_in = ADC_Read_Value( 4 );
2
    i_in = ADC_Read_Value( 5 );
3
    i_out = ADC_Read_Value( 6 ); 
4
    u_out = ADC_Read_Value( 7 );

und das ist dann doch ein ganzes Stück einfacher als deine komplizierte 
Version mit switch/case und Schleife rundherum.
#3430513
Lesenswert?

Abgesehen davon sind da noch ein paar andere Syntax Fehler. Aber die 
sagt dir dann schon der Compiler.


Generell: schreib nicht haufenweise Code in einem Zug!
ALles was du damit tust ist: du stehst dann mit einem Haufen Code da, 
der voller Fehler ist und du hast keine Ahnung wo du mit fixen anfangen 
sollst.
#3430600
Lesenswert?

Robert Malle schrieb:
> Syntaxfehler hab ich in meinem Code nicht, zumindest konnte ich es
> compilieren ohne das der Compiler gemeckert hat.

Das glaub ich nicht.
Wo zb ist denn die main() Funktion? Also der Funktionskopf der main() 
Funktione?

 Die fehlenden
> Simikolons hier verschwanden sicherlich, als ich meinen Code hier hin
> kopiert und "zurecht gerückt" hab.

Mag sein.
Was sagt uns das?
Poste Code als Anhang und tippe ihn nicht ab.

Antwort schreiben

Bitte melde dich an, um einen Beitrag zu schreiben.

oder

Mit Google-Account einloggen

Die Registrierung ist kostenlos und dauert nur eine Minute.

Jetzt registrieren