Flanken abfragen in C; MSP430 Olimex

Gast #1509166
Lesenswert?

Hallo Community,

ich habe ein Olimexboard mit einem MSP430F449 mCPU.
Darauf sind vier Taster.
Ich will mit einen Taster wie die Tabulatortaste verwenden, sprich bei 
jedem Druck soll ein anderer Menupunkt angezeigt werden.
Dies habe ich bis jetzt so realisiert:
__________________________________________________________________
void Button1 ()
{ konst++;


  if (konst ==1)
  {Menu();
  }

  if (konst ==2)
  {Clock();
  }
  if (konst ==3)
  {Config();
  }
  if (konst ==4)
  {Timer();
  }
  if (konst ==5)
  {Sensor();
  }

  if (konst > 5)
  {konst=0;
  }
_____________________________________________________________________

Problem ist jetzt nur, dass konst so lange hochzählt wie ich den Taster 
halte. Sprich es ist zufall welcher Menupunkt angezeit wird....
Wie kann ich das Problem Softwareseitig beheben?

Danke für eure Hilfe

MfG
Styx
(Firma: OCL) #1509176
Lesenswert?

Dein Code für die Flankenabfrage wäre wie gesagt interessant.
Außerdem solltest du davon ausgehen, das die Taster prellen und sich 
damit für einige Millisekunden in einem unbestimmten Zustand befinden.
Genaueresläßt sich aber nur mit deinem Code sagen.
Gast #1509183
Lesenswert?

Hi,

also da ich sehr wenig Ahnung habe und trotzdem das für die Uni 
programmieren muss, hab ich die Beispielprogramme von Olimex und TI 
angeschaut und mir daraus mein eigenes zusammen gestrickt.

Ich hab noch kein Code für die Flankenabfragung geschrieben...
Habe leider keine Idee wie sowas aussehne soll...
Muss ich da die Baud-rate beachten?

Der Code sieht bis jetzt so aus:
1
#include  <msp430x44x.h>
2
//#include  <io430x44x.h>
3

4
#define          B1                 P3IN & BIT4       //B1 - P4.4
5
#define          B2                 P3IN & BIT5       //B2 - P4.5
6
#define          B3                 P3IN & BIT6       //B3 - P4.6
7
#define          B4                 P3IN & BIT7       //B4 - P4.7
8
#define          STATUS_LED         BIT3              //STATUS_LED - P1.3
9
#define          time               200
10
#define          time_s             200
11
#define          BUZ1_ON            P1OUT |= BIT0     //P4.2
12
#define          BUZ1_OFF           P1OUT &= ~BIT0    //P4.2
13
#define          BUZ2_ON            P1OUT |= BIT2     //P4.3
14
#define          BUZ2_OFF           P1OUT &= ~BIT2    //P4.3
15
#define          DALLAS             P2IN & BIT7       //P2.7 - DALLAS
16
#define          CR                 0x0d
17
#define          LF                 0x0a
18
#define          LED_OFF            P1OUT |= BIT3
19
#define          LED_ON             P1OUT &= ~BIT3
20

21

22
//LCD digit segments
23

24

25
unsigned char i,time_segment,TXData;
26
unsigned int j;
27
unsigned int konst=0;
28

29
const unsigned char UART_Message [] = "http://www.olimex.com/dev";
30

31

32
void Delay (unsigned int a);
33
void DelayX(unsigned int b);
34
void Clear_LCD(void);
35
void LCD_all(void);
36
void BUZZER (void);
37
void UART_transmit (unsigned char Transmit_Data);
38
void Menu(void);
39
void OK (void);
40
void Timer (void);
41
void An (void);
42
void Aus (void);
43
void Button1 (void);
44

45
void main(void)
46
{
47
//++++++++++++++++++++++++++++++
48
  WDTCTL = WDTPW | WDTHOLD;                      // stop watchdog timer
49

50
  FLL_CTL0 &= ~XTS_FLL;                           // XT1 as low-frequency
51
  _BIC_SR(OSCOFF);                               // turn on XT1 oscillator
52

53
  do                                             // wait in loop until crystal is stable
54
    IFG1 &= ~OFIFG;
55
  while (IFG1 & OFIFG);
56

57
  FLL_CTL1 &= ~FLL_DIV0;                              // ACLK = XT1
58
  FLL_CTL1 &= ~FLL_DIV1;
59

60

61
  IFG1 &= ~OFIFG;                                // clear osc. fault int. flag
62
  FLL_CTL1 &= ~SELM0;                             // set DCO as MCLK
63
  FLL_CTL1 &= ~SELM1;
64

65

66
//++++++++++++++++++++++++++++++
67

68

69

70
//  SVSCTL = 0x68;                                    //brown out
71

72
//  WDTCTL = WDTPW + WDTHOLD;                           // Stop watchdog timer
73

74
//  SCFI0 = BIT2 | BIT3 | BIT4 | BIT5 | BIT6 | BIT7;  
75
//  SCFI0 |= FN_4;                            // x2 DCO frequency, 8MHz nominal DCO
76
//  SCFQCTL = SCFQ_1M;      
77

78
//  FLL_CTL0 = /*DCOPLUS + */XCAP10PF;                  //Set load Cap. for Cristal = 10pF                                  // x2 DCO frequency, 8MHz nominal DCO  
79

80
//  FLL_CTL1 |= SELM_XT2;                             //Select XT2 for CPU MCLK
81
//  SCFI1 = 0;
82
//  SCFQCTL = SCFQ_M | SCFQ_1M;                       //M=1, 1Mhz
83
//  _BIS_SR(SCG0+SCG1);                               //disable DCO in status_reg
84
//  DelayX(200);
85

86

87
//UART ini
88
  U1TCTL = SSEL0;                                     //UCLOCK = ACLK
89
  U1BR0 = 0x03;                                       //BAUD RATE = 9600
90
  U1BR1 = 0x00;
91
  U1MCTL = 0x4a;                                      //with modulation
92
  U1CTL = CHAR;                                       //Start 8bit 1stop, N
93
  ME2 = UTXE1 | URXE1;                                //enable RX and TX
94
  P4SEL |= BIT0 | BIT1;                               //select UART pins
95
  P4DIR |= BIT0;                                      //port direction for TXD0
96
  P4DIR &= ~BIT1;                                     //port direction for RXD0
97
//  IE1 |= URXIE0;                                    // Enable USART0 RX interrupt
98
//  _EINT();                                          // Enable interrupts
99
//  U1CTL &= ~SWRST;                                  //reset UART logic
100

101

102

103
//LCD ini
104
  Clear_LCD();
105
  TACTL = TASSEL0 | TACLR;                                //set timer_A
106
  LCDCTL = LCDON + LCD4MUX + LCDP0 + LCDP1 + LCDP2;       // STK LCD 4Mux, S0-S39
107
  BTCTL = BT_fLCD_DIV64;                                  // STK LCD freq ACLK/64
108
  P5SEL = 0xFC;                                           // Common and Rxx all selected
109

110
//IO port ini
111

112
  P1SEL=BIT5;                                             //p1.5 is 32768Hz
113
  P1DIR=BIT5 | BIT3 | BIT0 | BIT2;                        //BUZ,LED are outputs
114

115
  LCD_all();                                              //light all LCD segments
116

117
  for (i=0; i!=15; i++)
118
    {
119
      LED_ON;
120
      DelayX(60);
121
      LED_OFF;
122
      DelayX(60);
123
    }
124

125
  UART_transmit (CR);                                     //send message by RS232
126
  UART_transmit (LF);
127
  for (i=0; i!=26; i++)  UART_transmit (UART_Message[i]);
128
  UART_transmit (CR);
129
  UART_transmit (LF);
130

131
//---------------MAIN LOOP-----------------------------------
132

133
  while (1)
134
    {
135

136
     if ((IFG2 & URXIFG1) != 0) UART_transmit (U1RXBUF+1);      //test RS 232 (echo+1)
137

138
     while ((DALLAS) == 0) BUZZER();                            //test DALLAS button
139

140
L1:  if ((B1) == 0) Clear_LCD();
141
     if ((B2) == 0) Button1();
142

143
     if ((B3) == 0)
144
       {
145
        time_segment=100;
146
        for (i=0; i!= 20; i++)
147
         {
148
           for (j=0; j!=9; j++)
149
            {
150
              LCDMEM[i] = (BIT0 << j);
151
              if ((IFG2 & URXIFG1) != 0) UART_transmit (U1RXBUF+1);      //test RS 232 (echo+1)
152
              DelayX(time_segment);
153
              if((B1) == 0 ) goto L1;
154
              if((B2) == 0 ) goto L1;
155
              if((B4) == 0 )
156
                {
157
                  LCD_all();
158
                  goto L1;
159
                }
160
            }
161
         }
162
       }
163
      if((B4) == 0)
164
        {
165
           BUZZER();
166
           P1OUT &= ~STATUS_LED;                 //switch on status_led
167
        }
168
      else  P1OUT |= STATUS_LED;            //switch off status led
169
    }
170
}
171

172

173
void Delay (unsigned int a)                           //9+a*12 cycles
174
{
175
  unsigned int l;
176
  for (l=0 ; l != a; l++);
177
}
178

179
void Clear_LCD(void)
180
{
181
for (i=0; i!= 20; i++) LCDMEM[i]=0x00;                  //clear Sxx
182
}
183

184
void LCD_all(void)
185
{
186
for (i=0; i!= 20; i++) LCDMEM[i]=0xff;                  //turnon all LCD segments
187
}
188

189
void DelayX(unsigned int b)
190
{
191
unsigned int m;
192
for(m=0;m!=b;m++) Delay(255);
193
}
194

195
void BUZZER (void)
196
{
197
  BUZ1_OFF;
198
  BUZ2_ON;
199
  Delay(40);
200
  BUZ2_OFF;
201
  BUZ1_ON;
202
  Delay(40);
203
}
204

205
void UART_transmit (unsigned char Transmit_Data)              //UART1 Transmit Subroutine
206
{
207
  while ((IFG2 & UTXIFG1) == 0);                              //Wait for ready U1TXBUF
208
  U1TXBUF = Transmit_Data;                                    //send data
209
}
210

211
void Menu (void)
212
{
213
 Clear_LCD(); 
214
 LCDMEM[19]=0x68;  //M
215
 LCDMEM[18]=0x64;
216
 LCDMEM[17]=0x64;  //E
217
 LCDMEM[16]=0x90;
218
 LCDMEM[15]=0x68;  //N
219
 LCDMEM[14]=0x61;
220
 LCDMEM[13]=0x60;  //U
221
 LCDMEM[12]=0x70;
222
}
223

224
void OK (void)
225
{
226
  
227
 Clear_LCD(); 
228
 LCDMEM[19]=0x60;  // O
229
 LCDMEM[18]=0xF0;  // O
230
 LCDMEM[17]=0x64;  // K
231
 LCDMEM[16]=0x05;  // K
232

233
}
234

235
void Timer()
236
{
237
 Clear_LCD(); 
238
 LCDMEM[19]=0x01; //T
239
 LCDMEM[18]=0x88;
240
 LCDMEM[17]=0x01; //I
241
 LCDMEM[16]=0x08;
242
 LCDMEM[15]=0x68; //M
243
 LCDMEM[14]=0x64;  
244
 LCDMEM[13]=0x64; //E
245
 LCDMEM[12]=0x90;
246
 LCDMEM[11]=0x64; //R
247
 LCDMEM[10]=0xC3;
248
}
249

250
void An()
251
{
252
 Clear_LCD(); 
253
 LCDMEM[19]=0x64; //A
254
 LCDMEM[18]=0xE2;
255
 LCDMEM[17]=0x68; //N
256
 LCDMEM[16]=0x61; 
257
}
258

259
void Aus()
260
{
261
 Clear_LCD(); 
262
 LCDMEM[19]=0x64; //A
263
 LCDMEM[18]=0xE2;
264
 LCDMEM[17]=0x60; //U
265
 LCDMEM[16]=0x70; 
266
 LCDMEM[15]=0x44; //S
267
 LCDMEM[14]=0xB2;
268
}
269

270

271

272
void Button1 ()
273
{ konst++;
274
  
275
    
276
  if (konst ==1)
277
  {Menu();
278
  }
279

280
  if (konst ==2)
281
  {Aus();
282
  }
283
  if (konst ==3)
284
  {An();
285
  }
286
  if (konst ==4)
287
  {Timer();
288
  }
289
  if (konst ==5)
290
  {OK();
291
  }
292
  
293
  if (konst > 5)
294
  {konst=0;
295
  }
296
}

Letztendlich soll das eine Menuführung werden...

MfG
Styx
(Firma: OCL) #1509194
Lesenswert?

Flanken Abfrage:

Geh davon aus, dass der Button ungedrückt auf Null-Pegel ist.
Diesen Zusatnd speicherst du in einer Variable.
Beim nächsten Schleifendurchgang guckst du dir den Porteingang an. Wenn 
der Wert am Eingang nicht deinem gespeicherten Wert entspricht hast du 
ne Flanke. In diesem Fall speicherst du den neuen Wert / Zustand. 
Ansonsten lässt du die Variable wie sie ist.


Was an deinem Code ein bischen aufstösst sind die 'Goto'-Anweisungen. 
Das macht man eigentlich nicht mehr. Aber versuch erstmal die Flanken zu 
erfassen.
Gast #1510175
Lesenswert?

So also ich hab jetzt ne Flankenabfrage eingebaut.

Jetzt würde mich doch aber des mit den Interupts interessieren.
1
#include  <msp430x44x.h>
2
//#include  <io430x44x.h>
3

4
#define          B1                 P3IN & BIT4       //B1 - P4.4
5
#define          B2                 P3IN & BIT5       //B2 - P4.5
6
#define          B3                 P3IN & BIT6       //B3 - P4.6
7
#define          B4                 P3IN & BIT7       //B4 - P4.7
8
#define          STATUS_LED         BIT3              //STATUS_LED - P1.3
9
#define          time               200
10
#define          time_s             200
11
#define          BUZ1_ON            P1OUT |= BIT0     //P4.2
12
#define          BUZ1_OFF           P1OUT &= ~BIT0    //P4.2
13
#define          BUZ2_ON            P1OUT |= BIT2     //P4.3
14
#define          BUZ2_OFF           P1OUT &= ~BIT2    //P4.3
15
#define          DALLAS             P2IN & BIT7       //P2.7 - DALLAS
16
#define          CR                 0x0d
17
#define          LF                 0x0a
18
#define          LED_OFF            P1OUT |= BIT3
19
#define          LED_ON             P1OUT &= ~BIT3
20

21

22
// LCD Segmente
23
#define     lcd_a      (0x80)    // definitions for LCD seegments on the Olimex LCD. 4-Mux operation is assumed
24
#define     lcd_b      (0x40)    // For more details on 4-Mux operation, gather your LCD datasheet, 
25
#define     lcd_c      (0x20)    // TI's MSP430F449 User Guide (look for LCD Controller, then 4-Mux),
26
#define     lcd_d      (0x01)    // and MSP-449STK-2 schematic. You will need ALL these 3 when defining
27
#define     lcd_e      (0x02)    // each number or character. Remember, the Olimex LCD doesn't use a LCD driver!
28
#define     lcd_f      (0x08)    // You tell the LCD what characters to display. It's very time consuming!!
29
#define     lcd_g      (0x04)
30
#define     lcd_h      (0x10)
31

32
//LCD digit segments
33
char *LCD = LCDMEM;          // pointer to LCD Memory Segments. Pretty cool, got this idea from TI-website
34
//
35
unsigned char i,time_segment,TXData;
36
unsigned int j;
37
unsigned int konst=0;
38
unsigned int konst_2=0;
39
unsigned int Button= 0;
40
unsigned int Button_1= 0;
41
unsigned int zaehler= 0;
42
unsigned int Flanke_1= 0;
43
unsigned int Flanke_2= 0;
44
const unsigned char UART_Message [] = "http://www.olimex.com/dev";
45

46

47
void Delay (unsigned int a);
48
void DelayX(unsigned int b);
49
void Clear_LCD(void);
50
void LCD_all(void);
51
void BUZZER (void);
52
void UART_transmit (unsigned char Transmit_Data);
53
void Menu(void);
54
void OK (void);
55
void Timer (void);
56
void An (void);
57
void Aus (void);
58
void Button1 (void);
59
void Button2 (void);
60
void writeLetter(int position,char letter);     // displays a single character on the LCD
61

62
/// Tests
63
void Hallo(void);
64
void du(void);
65
void da(void);
66
void wie(void);
67

68
void main(void)
69
{
70
//++++++++++++++++++++++++++++++
71
  WDTCTL = WDTPW | WDTHOLD;                      // stop watchdog timer
72

73
  FLL_CTL0 &= ~XTS_FLL;                           // XT1 as low-frequency
74
  _BIC_SR(OSCOFF);                               // turn on XT1 oscillator
75

76
  do                                             // wait in loop until crystal is stable
77
    IFG1 &= ~OFIFG;
78
  while (IFG1 & OFIFG);
79

80
  FLL_CTL1 &= ~FLL_DIV0;                              // ACLK = XT1
81
  FLL_CTL1 &= ~FLL_DIV1;
82

83

84
  IFG1 &= ~OFIFG;                                // clear osc. fault int. flag
85
  FLL_CTL1 &= ~SELM0;                             // set DCO as MCLK
86
  FLL_CTL1 &= ~SELM1;
87

88

89
//++++++++++++++++++++++++++++++
90

91

92

93
//  SVSCTL = 0x68;                                    //brown out
94

95
//  WDTCTL = WDTPW + WDTHOLD;                           // Stop watchdog timer
96

97
//  SCFI0 = BIT2 | BIT3 | BIT4 | BIT5 | BIT6 | BIT7;  
98
//  SCFI0 |= FN_4;                            // x2 DCO frequency, 8MHz nominal DCO
99
//  SCFQCTL = SCFQ_1M;      
100

101
//  FLL_CTL0 = /*DCOPLUS + */XCAP10PF;                  //Set load Cap. for Cristal = 10pF                                  // x2 DCO frequency, 8MHz nominal DCO  
102

103
//  FLL_CTL1 |= SELM_XT2;                             //Select XT2 for CPU MCLK
104
//  SCFI1 = 0;
105
//  SCFQCTL = SCFQ_M | SCFQ_1M;                       //M=1, 1Mhz
106
//  _BIS_SR(SCG0+SCG1);                               //disable DCO in status_reg
107
//  DelayX(200);
108

109

110
//UART ini
111
  U1TCTL = SSEL0;                                     //UCLOCK = ACLK
112
  U1BR0 = 0x03;                                       //BAUD RATE = 9600
113
  U1BR1 = 0x00;
114
  U1MCTL = 0x4a;                                      //with modulation
115
  U1CTL = CHAR;                                       //Start 8bit 1stop, N
116
  ME2 = UTXE1 | URXE1;                                //enable RX and TX
117
  P4SEL |= BIT0 | BIT1;                               //select UART pins
118
  P4DIR |= BIT0;                                      //port direction for TXD0
119
  P4DIR &= ~BIT1;                                     //port direction for RXD0
120
//  IE1 |= URXIE0;                                    // Enable USART0 RX interrupt
121
//  _EINT();                                          // Enable interrupts
122
//  U1CTL &= ~SWRST;                                  //reset UART logic
123

124

125

126
//LCD ini
127
  Clear_LCD();
128
  TACTL = TASSEL0 | TACLR;                                //set timer_A
129
  LCDCTL = LCDON + LCD4MUX + LCDP0 + LCDP1 + LCDP2;       // STK LCD 4Mux, S0-S39
130
  BTCTL = BT_fLCD_DIV64;                                  // STK LCD freq ACLK/64
131
  P5SEL = 0xFC;                                           // Common and Rxx all selected
132

133
//IO port ini
134

135
  P1SEL=BIT5;                                             //p1.5 is 32768Hz
136
  P1DIR=BIT5 | BIT3 | BIT0 | BIT2;                        //BUZ,LED are outputs
137

138
  LCD_all();                                              //light all LCD segments
139

140
  for (i=0; i!=15; i++)
141
    {
142
      LED_ON;
143
      DelayX(60);
144
      LED_OFF;
145
      DelayX(60);
146
    }
147

148
  UART_transmit (CR);                                     //send message by RS232
149
  UART_transmit (LF);
150
  for (i=0; i!=26; i++)  UART_transmit (UART_Message[i]);
151
  UART_transmit (CR);
152
  UART_transmit (LF);
153

154
//---------------MAIN LOOP-----------------------------------
155
Button_1 = B2;
156
Flanke_1=1;
157
Flanke_2=1;
158

159
    while (1)
160
    {
161
     
162
      
163
     if ((IFG2 & URXIFG1) != 0) UART_transmit (U1RXBUF+1);      //test RS 232 (echo+1)
164

165
     while ((DALLAS) == 0) BUZZER();                            //test DALLAS button
166

167
L1:  if ((B2) == 0) //Clear_LCD();
168
    { 
169
      if (Flanke_2 == 1 )
170
       {
171
         zaehler = 0;
172
         Button2();
173
         Flanke_2 = 0;
174
       }
175
    }  
176
        else 
177
          {
178
            Flanke_2 = 1;
179
           }
180
    
181
     if ((B1) == 0) 
182
     {
183
       if (Flanke_1 == 1 )
184
       {
185
         zaehler = 0;
186
         konst_2 = 0;
187
         Button1();
188
         Flanke_1 = 0;
189
       }
190
       else
191
       { zaehler++;
192
       
193
         if (zaehler >= 30000)
194
         {
195
          zaehler = 0;
196
          konst =0;
197
          Clear_LCD();
198
          for (i=0; i!= 20; i++) BUZZER();
199
         }
200
       }
201
     }
202
     else 
203
     {
204
     Flanke_1 = 1;
205
     }
206
     if ((B3) == 0)
207
       {
208
        time_segment=100;
209
        for (i=0; i!= 20; i++)
210
         {
211
           for (j=0; j!=9; j++)
212
            {
213
              LCDMEM[i] = (BIT0 << j);
214
              if ((IFG2 & URXIFG1) != 0) UART_transmit (U1RXBUF+1);      //test RS 232 (echo+1)
215
              DelayX(time_segment);
216
              if((B1) == 0 ) goto L1;
217
              if((B2) == 0 ) goto L1;
218
              if((B4) == 0 )
219
                {
220
                  LCD_all();
221
                  goto L1;
222
                }
223
            }
224
         }
225
       }
226
      if((B4) == 0)
227
        {
228
           BUZZER();
229
           Hallo();
230
           P1OUT &= ~STATUS_LED;                 //switch on status_led
231
        }
232
      else  P1OUT |= STATUS_LED;            //switch off status led
233
    }
234
}
235

236

237
void Delay (unsigned int a)                           //9+a*12 cycles
238
{
239
  unsigned int l;
240
  for (l=0 ; l != a; l++);
241
}
242

243
void Clear_LCD(void)
244
{
245
for (i=0; i!= 20; i++) LCDMEM[i]=0x00;                  //clear Sxx
246
}
247

248
void LCD_all(void)
249
{
250
for (i=0; i!= 20; i++) LCDMEM[i]=0xff;                  //turnon all LCD segments
251
}
252

253
void DelayX(unsigned int b)
254
{
255
unsigned int m;
256
for(m=0;m!=b;m++) Delay(255);
257
}
258

259
void BUZZER (void)
260
{
261
  BUZ1_OFF;
262
  BUZ2_ON;
263
  Delay(40);
264
  BUZ2_OFF;
265
  BUZ1_ON;
266
  Delay(40);
267
}
268

269
void UART_transmit (unsigned char Transmit_Data)              //UART1 Transmit Subroutine
270
{
271
  while ((IFG2 & UTXIFG1) == 0);                              //Wait for ready U1TXBUF
272
  U1TXBUF = Transmit_Data;                                    //send data
273
}
274

275
void writeLetter(int position,char letter) // writes a single character on the LCD. User can specify position as well
276
{
277
    // DO NOT PLAY WITH THE CODE BELOW ------------------------------------------------------------------------------
278
    if (position == 1)  {  position = position + 6; } // this is position adjustment for compatibility.
279
    else if (position == 2 || position == 3 || position == 4 || position == 5 || position == 6 || position == 7)
280
    {  position = ((position * 2) - 1) + 6; }  // adjust position
281
    // --------------------------------------------------------------------------------------------------------------
282

283
    switch(letter)                                  
284
    {
285
       // letter  // LCDM7                           // LCDM8                          // End
286
       case 'A':  LCD[position-1] = lcd_a + lcd_b + lcd_c + lcd_e;          LCD[position] = lcd_b + lcd_c + lcd_g;        break;                                                                        
287
       case 'B':  LCD[position-1] = lcd_c + lcd_h + lcd_e;                  LCD[position] = lcd_b + lcd_c + lcd_g;        break;  
288
       case 'C':  LCD[position-1] = lcd_a + lcd_h;                          LCD[position] = lcd_b + lcd_c;            break;  
289
       case 'D':  LCD[position-1] = lcd_b + lcd_c + lcd_h + lcd_e;          LCD[position] = lcd_c + lcd_g;            break;     
290
       case 'E':  LCD[position-1] = lcd_a + lcd_h + lcd_e;                  LCD[position] = lcd_b + lcd_c + lcd_g;        break;  
291
       case 'F':  LCD[position-1] = lcd_a;                                  LCD[position] = lcd_b + lcd_c + lcd_g;        break;  
292
       case 'G':  LCD[position-1] = lcd_a + lcd_c + lcd_h + lcd_e;          LCD[position] = lcd_b + lcd_c;            break; 
293
       case 'H':  LCD[position-1] = lcd_b + lcd_c + lcd_e;                  LCD[position] = lcd_b + lcd_c + lcd_g;        break;  
294
       case 'I':  LCD[position-1] = lcd_a + lcd_h + lcd_f;                  LCD[position] = lcd_d;                break;  
295
       case 'J':  LCD[position-1] = lcd_b + lcd_h + lcd_c;                  LCD[position] = lcd_c;                break;  
296
       case 'K':  LCD[position-1] = lcd_d + lcd_g;                          LCD[position] = lcd_b + lcd_c + lcd_g;        break;  
297
       case 'L':  LCD[position-1] = lcd_h;                                  LCD[position] = lcd_b + lcd_c ;           break;    
298
       case 'M':  LCD[position-1] = lcd_b + lcd_c + lcd_g;                  LCD[position] = lcd_b + lcd_c + lcd_f;        break;    
299
       case 'N':  LCD[position-1] = lcd_b + lcd_c + lcd_d;                  LCD[position] = lcd_b + lcd_c + lcd_f;        break;
300
       case 'O':  LCD[position-1] = lcd_a + lcd_b + lcd_c + lcd_h;          LCD[position] = lcd_b + lcd_c;            break;
301
       case 'P':  LCD[position-1] = lcd_a + lcd_b + lcd_e;                  LCD[position] = lcd_b + lcd_c + lcd_g;        break;
302
       case 'Q':  LCD[position-1] = lcd_a + lcd_b + lcd_c + lcd_h + lcd_d;  LCD[position] = lcd_b + lcd_c;            break;
303
       case 'R':  LCD[position-1] = lcd_a + lcd_b + lcd_d + lcd_e;          LCD[position] = lcd_b + lcd_c + lcd_g;        break;
304
       case 'S':  LCD[position-1] = lcd_a + lcd_c + lcd_h + lcd_e;          LCD[position] = lcd_b + lcd_g;            break;
305
       case 'T':  LCD[position-1] = lcd_a + lcd_f + lcd_b;                  LCD[position] = lcd_d + lcd_b;            break;
306
       case 'U':  LCD[position-1] = lcd_b + lcd_c + lcd_h;                  LCD[position] = lcd_b + lcd_c;            break;
307
       case 'V':  LCD[position-1] = lcd_g;                                  LCD[position] = lcd_b + lcd_c + lcd_e;        break;
308
       case 'W':  LCD[position-1] = lcd_b + lcd_c + lcd_d;                  LCD[position] = lcd_b + lcd_c + lcd_e;        break;
309
       case 'X':  LCD[position-1] = lcd_d + lcd_g;                          LCD[position] = lcd_e + lcd_f;            break;
310
       case 'Y':  LCD[position-1] = lcd_b + lcd_c + lcd_h + lcd_e;          LCD[position] = lcd_f;                break;
311
       case 'Z':  LCD[position-1] = lcd_a + lcd_h + lcd_g;                  LCD[position] = lcd_e;                break;      
312
       
313
       // number  // LCDM7                              // LCDM8                          // END
314
       case '0':  LCD[position-1] = lcd_a + lcd_b + lcd_c + lcd_h;          LCD[position] = lcd_b + lcd_c;            break;                            
315
       case '1':  LCD[position-1] = lcd_b + lcd_c;                                                                    break; 
316
       case '2':  LCD[position-1] = lcd_a + lcd_b + lcd_e + lcd_h;              LCD[position] = lcd_c + lcd_g;            break; 
317
       case '3':  LCD[position-1] = lcd_a + lcd_b + lcd_c + lcd_e + lcd_h;  LCD[position] = lcd_g;                    break; 
318
       case '4':  LCD[position-1] = lcd_b + lcd_c + lcd_e;                  LCD[position] = lcd_b + lcd_g;            break; 
319
       case '5':  LCD[position-1] = lcd_a + lcd_c + lcd_h + lcd_e;          LCD[position] = lcd_b + lcd_g;            break; 
320
       case '6':  LCD[position-1] = lcd_a + lcd_c + lcd_h + lcd_e;          LCD[position] = lcd_b + lcd_c + lcd_g;    break; 
321
       case '7':  LCD[position-1] = lcd_a + lcd_b + lcd_c;                                                            break;
322
       case '8':  LCD[position-1] = lcd_a + lcd_b + lcd_c + lcd_e + lcd_h;  LCD[position] = lcd_b + lcd_c + lcd_g;    break; 
323
       case '9':  LCD[position-1] = lcd_a + lcd_b + lcd_c + lcd_e ;         LCD[position] = lcd_b + lcd_g;            break; 
324
       
325
       // others
326
       case '.':                                        LCD[position] = lcd_h;                break;  // decimal point
327
       case '^':                                        LCDM2 = lcd_c;                        break;  // top arrow
328
       case '!':                                        LCDM2 = lcd_a;                        break;  // bottom arrow
329
       case '>':                                        LCDM2 = lcd_b;                        break;  // right arrow
330
       case '<':                                        LCDM2 = lcd_h;                        break;  // left arrow
331
       case '+':                                        LCDM20= lcd_a;                        break;  // plus sign
332
       case '-':                                        LCDM20= lcd_h;                        break;  // minus sign
333
       case '&':                                        LCDM2 = lcd_d;                        break;  // zero battery
334
       case '*':                                        LCDM2 = lcd_d + lcd_f;                    break;  // low battery
335
       case '(':                                        LCDM2 = lcd_d + lcd_f + lcd_g;                break;  // medium battery
336
       case ')':                                        LCDM2 = lcd_d + lcd_e + lcd_f + lcd_g;            break;  // full battery */
337
    }
338
}
339

340
void Menu (void)
341
{
342
 Clear_LCD(); 
343
 LCDMEM[19]=0x68;  //M
344
 LCDMEM[18]=0x64;
345
 LCDMEM[17]=0x64;  //E
346
 LCDMEM[16]=0x90;
347
 LCDMEM[15]=0x68;  //N
348
 LCDMEM[14]=0x61;
349
 LCDMEM[13]=0x60;  //U
350
 LCDMEM[12]=0x70;
351
}
352

353
void OK (void)
354
{
355
  
356
 Clear_LCD(); 
357
 LCDMEM[19]=0x60;  // O
358
 LCDMEM[18]=0xF0;  // O
359
 LCDMEM[17]=0x64;  // K
360
 LCDMEM[16]=0x05;  // K
361

362
}
363

364
void Timer()
365
{
366
 Clear_LCD(); 
367
 LCDMEM[19]=0x01; //T
368
 LCDMEM[18]=0x88;
369
 LCDMEM[17]=0x01; //I
370
 LCDMEM[16]=0x08;
371
 LCDMEM[15]=0x68; //M
372
 LCDMEM[14]=0x64;  
373
 LCDMEM[13]=0x64; //E
374
 LCDMEM[12]=0x90;
375
 LCDMEM[11]=0x64; //R
376
 LCDMEM[10]=0xC3;
377
}
378

379
void An()
380
{
381
 Clear_LCD(); 
382
 LCDMEM[19]=0x64; //A
383
 LCDMEM[18]=0xE2;
384
 LCDMEM[17]=0x68; //N
385
 LCDMEM[16]=0x61; 
386
}
387

388
void Aus()
389
{
390
 Clear_LCD(); 
391
 LCDMEM[19]=0x64; //A
392
 LCDMEM[18]=0xE2;
393
 LCDMEM[17]=0x60; //U
394
 LCDMEM[16]=0x70; 
395
 LCDMEM[15]=0x44; //S
396
 LCDMEM[14]=0xB2;
397
}
398

399
void Hallo()
400
{
401
  Clear_LCD(); 
402
  writeLetter(7,'H');
403
  writeLetter(6,'A');
404
  writeLetter(5,'L');
405
  writeLetter(4,'L');
406
  writeLetter(3,'O');
407
  writeLetter(1,'^');
408
  
409
}
410

411
void du()
412
{
413
  Clear_LCD(); 
414
  writeLetter(7,'D');
415
  writeLetter(6,'U');
416
    
417
}
418

419
void da()
420
{
421
  Clear_LCD(); 
422
  writeLetter(7,'D');
423
  writeLetter(6,'A');
424
  }
425

426
void wie()
427
{
428
  Clear_LCD(); 
429
  writeLetter(7,'W');
430
  writeLetter(6,'I');
431
 
432
}
433

434
void Button1 ()
435
{ konst++;
436
  
437
    
438
  if (konst ==1)
439
  {Menu();
440
  }
441

442
  if (konst ==2)
443
  {Aus();
444
  }
445
  if (konst ==3)
446
  {An();
447
  }
448
  if (konst ==4)
449
  {Timer();
450
  }
451
  if (konst ==5)
452
  {OK();
453
  konst=0;
454
  }
455
  
456
}
457

458
void Button2 ()
459
{  
460
  konst_2++;
461
  
462
  if (konst ==1)
463
  {
464
    if (konst_2 ==1)
465
    {
466
    Hallo();
467
    }
468
    
469
    if (konst_2==2)
470
    {
471
      du();
472
      
473
      konst_2 =0;
474
    }
475
  }
476

477
  if (konst ==2)
478
  {
479
    if (konst_2 ==1)
480
    {
481
    da();
482
    }
483
    
484
    if (konst_2==2)
485
    {
486
      wie();
487
      
488
      konst_2 =0;
489
    }
490
  }
491
  if (konst ==3)
492
  {An();
493
  }
494
  if (konst ==4)
495
  {Timer();
496
  }
497
  if (konst ==5)
498
  {OK();
499
  konst=0;
500
  }
501
  
502
}

Will noch n Timer einbauen der dann ein Interupt ausgibt wenn er 
abgelaufen ist und dann eine ISR aktiviert.
#1510181
Lesenswert?

Ehe du das tust, solltest du den Code erst mal aufräumen.
Selten so etwas unnötig unübersichtlich komplexes gesehen.

Fang damit an, dir Ausgabefunktionen für Strings zu bauen (sowohl LCD 
als auch UART). Gut wäre es auch, wenn du die Sammlung an LCD-Funktionen 
bzw. UART Funktionen in jeweils ein eigenes *.c File auslagerst, damit 
du sie beim Editieren deines eigentlichen Source Codes aus dem Weg hast. 
Sowas kommt der Übersicht immer zugute, wenn man funktionierenden Code 
thematisch geordnet in einzelne *.c Files auslagert. Dann beschäftigt 
sich 1 *.c File mit 1 Thema und man wird nicht ständig abgelenkt.
1
void writeString( int Position, char* String )
2
{
3
  while( *String )
4
    writeLetter( Position--, *String++ );
5
}

Mit dieser kleinen Hilfsfunktion, vereinfacht sich zb dieser Code
1
void Hallo()
2
{
3
  Clear_LCD(); 
4
  writeLetter(7,'H');
5
  writeLetter(6,'A');
6
  writeLetter(5,'L');
7
  writeLetter(4,'L');
8
  writeLetter(3,'O');
9
  writeLetter(1,'^');
10
  
11
}
zu
1
void Hallo()
2
{
3
  Clear_LCD(); 
4
  writeString( 7, "Hallo^" );
5
}

oder dieser hier
1
void Timer()
2
{
3
 Clear_LCD(); 
4
 LCDMEM[19]=0x01; //T
5
 LCDMEM[18]=0x88;
6
 LCDMEM[17]=0x01; //I
7
 LCDMEM[16]=0x08;
8
 LCDMEM[15]=0x68; //M
9
 LCDMEM[14]=0x64;  
10
 LCDMEM[13]=0x64; //E
11
 LCDMEM[12]=0x90;
12
 LCDMEM[11]=0x64; //R
13
 LCDMEM[10]=0xC3;
14
}
zu
1
void Timer()
2
{
3
 Clear_LCD(); 
4
 writeString( 7, "Timer" );
5
}

und das ist dann schon viel einfacher zu verfolgen, als die 
Originalversion, bei der irgendwelche Konstanten, von denen keiner weiß, 
was sie bedeuten, in irgendwelche Arrayplätze geschrieben werden, die 
interessanterweise auch noch Indexmässig abnehmen und nicht, wie man das 
eigentlich meistens erwarten würde, zunehmen.
Gast #1510444
Lesenswert?

Hey,

Okay danke für den Tipp.
Hab alles zu LCD jetzt ausgelagert und das mit der Wortfunktion habe ich 
auch schon versucht nur ich wusste nicht wie ich ein Teil des Stings 
auswähle bzw. Abschneide.

Hab den Vorschlag eingefügt, aber er zeigt nur den 1. Buchstaben an.

Weiß aber nicht woran des liegt. Glaub er nimmt einfach den nächsten 
Buchstaben nicht an.

Hier der Teil aus dem LCD.c File
1
void writeString( int Position, char* String )
2
{
3
  while( *String)
4
    writeLetter( Position--, *String++ );
5
}


Hier der Teil aus dem Main.
1
void Button1 ()
2
{ konst++;
3
  
4
    
5
  if (konst ==1)
6
  {
7
    Clear_LCD();
8
    writeString(7, "Menu");
9
  }
10

11
  if (konst ==2)
12
  {Aus();
13
  }
14
  if (konst ==3)
15
  {An();
16
  }
17
  if (konst ==4)
18
  {Timer();
19
  }
20
  if (konst ==5)
21
  {OK();
22
  konst=0;
23
  }
24
  
25
}

MfG

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