atmega32 problem mit if()

OP #2070413
Lesenswert?

Hallo,
1
void  start(const uint32_t steps, uint16_t speed)
2
{
3
  if (speed < max_feed)      // max_feed
4
    speed = max_feed;
5
  running = 1;
6
  myprint(0, 0, "running...");
7
  OCR1A = speed;
8

9
  uint8_t rampe = 30;
10
  uint8_t rampe_res = 10;
11
  int32_t dummy = 1;
12

13
  PORTD |= (1<<stepper_enable);
14
  TCCR1B |= (1<<CS12)/*|(1<<CS11)|(1<<CS10)*/;
15

16
  while(1)
17
  {
18
    itoa(lauf, buff0, 10);
19
    myprint(11, 0, "lauf ");
20
    myprint(19, 0, buff0);
21
    itoa(steps, buff0, 10);
22
    myprint(11, 1, "steps");
23
    myprint(19, 1, buff0);
24
    itoa(OCR1A, buff1, 10);
25
    myprint(11, 2, "OCR1A");
26
    myprint(19, 2, buff1);
27
    itoa(dummy, buff1, 10);
28
    myprint(11, 3, "dummy");
29
    myprint(19, 3, buff1);
30
    if (lauf > steps)               // hier ein problem
31
    {
32
      lcd_clrscr();
33
      //steps++;
34
    }
35
  }
36
};

ich habe ein Problem bei der if-abfrage, sie wird nur ausgeführt wenn 
ich die Variable steps durch eine festen Wert z.B. 500 ersetze 
ausgeführt. Die Variable lauf wird im Timer-ISR hochgezählt.

Compileroption "-w -W -Wall -O2 -mmcu=atmega32 -std=c99 
-fF_CPU=8000000UL

mfg rocky_j
OP #2070420
Lesenswert?

ok, der komplette Code
1
#ifndef  F_CPU
2
  #define  F_CPU 8000000L
3
#endif
4
#ifndef  XTAL
5
  #define  XTAL F_CPU
6
#endif
7

8

9
#include <avr/io.h>
10
#include <util/delay.h>
11
#include <avr/interrupt.h>
12
#include <stdio.h>
13
#include <stdlib.h>
14

15
#include "wintek2704.h"
16

17
#define stepper_port  PORTD
18
#define  stepper_clk    PD4
19
#define stepper_dir    PD5
20
#define  stepper_enable  PD6
21
#define  stepper_vh    PD7
22
#define  f        1    // vor
23
#define r        0    // rück
24

25

26
char    buff0[10];
27
char    buff1[10];
28
volatile  uint32_t    lauf = 0;
29
uint8_t    running = 0;
30
uint16_t  max_feed = 100;
31

32
// funkt protos
33
void  timer1_init();
34
void  stop();
35
void  start(const uint32_t, uint16_t);
36
void  vor();
37
void  rueck();
38
void  myprint(uint8_t, uint8_t, char *);
39
void  move(uint32_t, uint8_t, uint32_t, uint16_t);
40

41
int main(void)
42
{
43
  // erst ports auf low dann richtung
44
  PORTD &= ~( (1 << stepper_clk) | (1 << stepper_dir) | (1 << stepper_enable) | (1 << stepper_vh));
45

46
  DDRD  |= (1 << stepper_clk) | (1 << stepper_dir) | (1 << stepper_enable) | (1 << stepper_vh);
47
  _delay_ms(500);
48
  lcd_init(LCD_DISP_ON);
49
  lcd_puts("init system...\n");
50

51
  timer1_init();
52

53
  lcd_puts("TIMER1 rdy\n");
54

55
  _delay_ms(500);
56
  lcd_clrscr();
57
  _delay_ms(500);
58

59
  sei();    // interupt an
60

61
  //fakt = (vref / 1024000);
62

63
  _delay_ms(200);
64
  lcd_clrscr();
65

66
  //PORTD |= (1<< PD6);// | (1 << PD7);
67
  while(1)
68
  {/*
69
    lcd_gotoxy(0, 0);
70
    itoa(lauf, buff0, 10);
71
    lcd_puts(buff0);
72
    lcd_gotoxy(0, 1);
73
    itoa(TCNT1, buff1, 10);
74
    lcd_puts(buff1);*/
75

76

77

78
    {
79
      Move(100, f, 2000, 500);
80
      Move(100, r, 2000, 500);/*
81
      Move(5000, r, 500, 5);
82
      Move(110, r, 5000, 5);
83
      Move(1000, f, 50, 100);
84
      Move(100, r, 5000, 5);
85
      Move(150, f, 5000, 50);
86
      Move(1500, f, 200, 500);
87

88
      Move(5000, f, 500, 500);
89
      Move(30000, f, 500, 500);
90
      Move(10000, 5, 1000, 500);
91
      Move(500, f, 500, 500);*/
92

93
    }
94

95

96
  }
97
    return 0;
98
};
99

100

101
void  timer1_init()
102
{
103
//TCCR1A |= (1 << WGM11) | (1 << WGM10);
104
TCCR1B |= (1 << WGM12);// | (1 << WGM13);//  | (1<<CS10)|(1<<CS11);
105
//ICR1 = 10000;
106
//OCR1A = 5000;
107
TIMSK |= /*(1<<TOIE1) |*/ (1<<OCF1A);
108

109
};
110

111
void  myprint(uint8_t x, uint8_t y, char *txt)
112
{
113
  //lcd_clrscr();
114
  lcd_gotoxy(x, y);
115
  lcd_puts(txt);
116
};
117

118
void  start(const uint32_t steps, uint16_t speed)
119
{
120
  if (speed < max_feed)      // max_feed
121
    speed = max_feed;
122
  running = 1;
123
  myprint(0, 0, "running...");
124
  OCR1A = speed;
125

126
  uint8_t rampe = 30;
127
  uint8_t rampe_res = 10;
128
  int32_t dummy = 1;
129

130
  PORTD |= (1<<stepper_enable);
131
  TCCR1B |= (1<<CS12)/*|(1<<CS11)|(1<<CS10)*/;
132

133
  while(1)
134
  {
135
    itoa(lauf, buff0, 10);
136
    myprint(11, 0, "lauf ");
137
    myprint(19, 0, buff0);
138
    itoa(steps, buff0, 10);
139
    myprint(11, 1, "steps");
140
    myprint(19, 1, buff0);
141
    itoa(OCR1A, buff1, 10);
142
    myprint(11, 2, "OCR1A");
143
    myprint(19, 2, buff1);
144
    itoa(dummy, buff1, 10);
145
    myprint(11, 3, "dummy");
146
    myprint(19, 3, buff1);
147
    if (lauf > steps)
148
    {
149
      lcd_clrscr();
150
      //steps++;
151
    }
152
  }
153

154

155
/*  while(1)//(dummy >= 0))
156
  {
157
    //if (lauf <= rampe)
158
    //  OCR1A = speed + ((rampe_res * rampe) - rampe_res * lauf);
159
*/
160

161

162

163
};
164

165
void  stop()
166
{
167
  PORTD &= ~(1<<stepper_enable);
168
  TCCR1B &= ~((1<<CS10)|(1<<CS11)|(1<<CS12));
169
  running = 0;
170
  myprint(0, 0, "stop......");
171
}
172
void  rueck()
173
{
174
  PORTD |= (1<<stepper_dir);
175
  myprint(0, 1, "ruek");
176
};
177

178
void  vor()
179
{
180
  PORTD &= ~(1<<stepper_dir);
181
  myprint(0, 1, "vor ");
182
}
183

184

185
ISR(TIMER1_OVF_vect)
186
{
187
  PORTD ^= (1 << stepper_clk);
188
  _delay_us(5);
189
  PORTD &= ~(1 << stepper_clk);
190
  lauf++;
191
};
192

193
ISR(TIMER1_COMPA_vect)
194
{
195
  PORTD ^= (1 << stepper_clk);
196
  _delay_us(5);
197
  PORTD &= ~(1 << stepper_clk);
198
  lauf++;
199
}
200

201
void  Move(uint16_t speed, uint8_t dir, uint32_t steps, uint16_t wait)
202
{
203
  //OCR1A = speed;
204
  if (dir == f)
205
    vor();
206
  if (dir == r)
207
    rueck();
208
  start(steps, speed);
209
  _delay_ms(wait + 1);
210
};

mfh rocky_j
#2070429
Lesenswert?

Als Anhang wärs besser gewesen.
Wie wird denn die Fuktion start(), welche von Move() aufgerufen wird, 
beendet? Ich sehe keine Möglichkeit der Terminierung der while-Schleife.
Weiters greifst du nicht atomar auf lauf zu, was man jedoch machen 
sollte wenn der Datentyp größer als 8 Bit ist und er auch in einer ISR 
verändert wird.

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