Forum: Mikrocontroller und Digitale Elektronik Lauflicht mit LPC1769


von Dennis H. (denn1s)


Angehängte Dateien:

Lesenswert?

Hallo miteinander,

ich muss mit dem LPC1769 ein Lauflicht programmieren und wollte eure 
Meinung zum Code gerne wissen, ob es so Funktionieren könnte. Vielleicht 
sind einige Fehler vorhanden und über Verbesserungsvorschläge würde ich 
mich freuen.

Das Lauflicht soll folgendermaßen funktionieren mit Taster 3 soll Start 
bzw. Stopp des lautliches hervorgerufen werden, zu beginn sollen die 
LEDs nach links laufen. Mit Taster 2 soll sich das Lauflicht nach rechts 
bewegen und mit Taster 1 dementsprechend nach Links.

Datei befindet sich im Anhang, wenn die Header gewünscht ist kann ich 
sie auch hinzufügen.

Mit freundlichen Grüßen


1
/*
2
===============================================================================
3
 Name        : Lauflicht.c
4
 Author      : $(author)
5
 Version     :
6
 Copyright   : $(copyright)
7
 Description : main definition
8
===============================================================================
9
*/
10
11
#ifdef __USE_CMSIS
12
#include "LPC17xx.h"
13
#endif
14
15
#include <cr_section_macros.h>
16
17
#include <stdio.h>
18
19
#include "gpio.h"
20
21
void initial(void);
22
void LEDReset(void);
23
uint32_t taste1 (void);
24
uint32_t taste2 (void);
25
uint32_t taste3 (void);
26
void Linkslauf(void);
27
void Rechtslauf(void);
28
29
30
void initial(void)
31
{
32
33
    GPIOSetDir(1,18, OUTPUT);
34
    GPIOSetDir(1,19, OUTPUT);
35
    GPIOSetDir(1,20, OUTPUT);
36
    GPIOSetDir(1,21, OUTPUT);
37
    GPIOSetDir(1,22, OUTPUT);
38
    GPIOSetDir(1,23, OUTPUT);
39
    GPIOSetDir(1,24, OUTPUT);
40
    GPIOSetDir(1,25, OUTPUT);
41
42
    GPIOSetDir(2,3, INPUT);
43
    GPIOSetDir(2,4, INPUT);
44
    GPIOSetDir(2,5, INPUT);
45
    GPIOSetPull(2, 3, PULLUP);
46
    GPIOSetPull(2, 4, PULLUP);
47
    GPIOSetPull(2, 5, PULLUP);
48
49
}
50
51
52
void LEDReset(void)
53
{
54
  int i;
55
56
  for(i=18; i <= 25; i++)
57
  {
58
  GPIOSetValue(1, i, LOW);
59
  }
60
}
61
62
uint32_t taste1(void)
63
{
64
  return (GPIOGetValue(2, 3));  //Lesen Taster 3 Start/Stop
65
}
66
67
uint32_t taste2(void)
68
{
69
  return (GPIOGetValue(2, 4));  //Lesen Taster 3 Start/Stop
70
}
71
72
uint32_t taste3(void)
73
{
74
  return (GPIOGetValue(2, 5));  //Lesen Taster 3 Start/Stop
75
}
76
77
void Linkslauf(void)
78
{
79
  int i;
80
81
  while(1)
82
  {
83
84
    for(i = 18; i <= 25; i++)
85
    {
86
      GPIOSetValue(1, i, HIGH);
87
      _delay_ms(1000);
88
      GPIOSetValue(1, i, LOW);
89
90
      if(taste2() == 0)
91
      {
92
        for(i = 0; i<= 70000;i++){}    // Entprellen
93
        do{
94
        }while(taste2() == 0);
95
        for(i = 0; i<= 70000;i++){}
96
97
        Rechtslauf();
98
      }
99
100
      if(taste3() == 0)
101
      {
102
        for(i = 0; i<= 70000;i++){}    // Entprellen
103
        do{
104
        }while(taste3() == 0);
105
        for(i = 0; i<= 70000;i++){}
106
107
        break;
108
      }
109
    }
110
  }
111
112
}
113
114
115
void Rechtslauf(void)
116
{
117
  int i;
118
119
  while(1)
120
  {
121
    for(i= 25; i >= 18; i--)
122
    {
123
      GPIOSetValue(1, i, HIGH);
124
      _delay_ms(1000);
125
      GPIOSetValue(1, i, LOW);
126
127
      if(taste1() == 0)
128
      {
129
        for(i = 0; i<= 70000;i++){}    // Entprellen
130
        do{
131
        }while(taste1() == 0);
132
        for(i = 0; i<= 70000;i++){}
133
134
        Linkslauf();
135
      }
136
137
      if(taste3() == 0)
138
      {
139
        for(i = 0; i<= 70000;i++){}    // Entprellen
140
        do{
141
        }while(taste3() == 0);
142
        for(i = 0; i<= 70000;i++){}
143
        break;
144
      }
145
    }
146
  }
147
}
148
149
int main(void)
150
{
151
152
  initial();
153
  LEDReset();
154
155
  while(1)
156
  {
157
158
    GPIOSetValue(1, 18, HIGH);
159
160
    if(taste3() == 0)
161
    {
162
      Linkslauf();
163
    }
164
  }
165
}

von MaWin (Gast)


Lesenswert?

Findest du den code nicht ein wenig aufwändig ?

Dann scheinst du es extern so verdrahtet zu haben, daß ein Taster einen 
Eingangspin nach Masse zieht (und ein Pull up ihn sonst nach plus). Das 
muss man im Programm nicht widerspiegeln, man kann dort durchaus taste() 
eine 1 als gedrückt zurückliefern lassen, das macht die Logik 
übersichtlicher.

Zum Entprellen gibt es fertige Methoden, da muss man das Rad nicht neu 
erfinden, und auch noch falsch.
1
int linksstoprechts=0,t1=1,t2=1,t3=1,last_t1=1,last_t2=1,last_t3=1,led=18;
2
int main(void)
3
{
4
    // initialisierung wie gehabt
5
    while(1)
6
    {
7
      for(i=0;i<100;i++)
8
      {
9
        t1=taste1();
10
        t2=taste2();
11
        t3=taste3();
12
        if(t1!=last_t1&&t1==0) linksstoprechts=1;
13
        if(t2!=last_t2&&t2==0) linksstoprechts=-1;
14
        if(t3!=kast_t3&&t3==0) linksstoprechts=(linksstoprechts==0);
15
        last_t1=t1;
16
        last_t2=t2;
17
        last_t3=t3;
18
        _delay_ms(10);
19
        if(linksstoprechts==0) break;
20
      }
21
      GPIOSetValue(1, led, HIGH);
22
      led+=linksstoprechts;
23
      if(led<18) led=25; else if(led>25) led=18;
24
      if(linksstoprechts) GPIOSetValue(1, i, LOW);
25
    }    
26
}

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.