3x7 Matrix (Code Verbesserungen?)

OP #1915900
Lesenswert?

Hallo Zusammen,

Ich habs endlich geschafft, meine Tastatur Matrix zu beschalten, 
auszulesen und auszuwerten.

Ich möchte euch gerne meinen Code hier vorstellen, vielleicht gibt es an 
der ein oder anderen Stelle sogar noch was zu verbessern?

(Ich frage daher, da mein AVR Studio mir nach dem Kompilieren mitteilt, 
das .data schon zu 94,7% Full ist)
1
#include <stdlib.h>
2
#include <avr/io.h>
3
#include <avr/interrupt.h>
4
#include "Diagnose-Terminal.h"
5
#include "lcd_lib.h"
6
#include "uart.h"
7
#include <util/delay.h>
8

9
#define UART_BAUD_RATE      57400
10
#define NOP();  asm volatile("nop"::);
11
volatile KEY_PRESSED = 0;
12

13
ISR( TIMER0_OVF_vect )  
14
{
15
  int col =0;
16
  col = key_scan();
17
  if(col>0);
18
  {
19
    PORTB = 0b00000111; // PB0-2 "high"
20
    DDRB &= ~ ((1<<DDB0) | (1<<DDB1) | (1<<DDB2)); // PB0-2 Eingang
21

22
    PORTC = 0b00000000; // PC0-7 "low"
23
    DDRC |= ((1<<DDC0) | (1<<DDC1) | (1<<DDC2) | (1<<DDC3) | (1<<DDC4) | (1<<DDC5) | (1<<DDC6) | (1<<DDC7)); // PC0-7 Ausgang 
24
  
25
    NOP();
26
  }
27

28
  if(col>0)
29
  {
30
    _delay_ms(50);  //debouncing der Keys
31
    KEY_PRESSED = col;
32
  }  
33
}
34

35
int key_scan(void)
36
{
37
  int col=0,row=0;
38

39
  TCNT0 = (uint8_t)(int16_t)-(F_CPU / 1024 * 10e-3 + 0.5); // 10ms Preloading
40
  
41
  NOP();
42
  if(!(PINB & (1<< PINB0)))
43
    col = 3;
44
  if(!(PINB & (1<< PINB1)))
45
    col = 2;
46
  if(!(PINB & (1<< PINB2)))
47
    col = 1;
48

49
  if(col)
50
  {
51
    PORTB = 0b00000000; // PB0-2 "low"
52
    DDRB |= (1<<DDB0) | (1<<DDB1) | (1<<DDB2); // PB0-2 Ausgang
53

54
    PORTC = 0b11111111; // PC0-7 "high"
55
    DDRC &= ~ ((1<<DDC0) | (1<<DDC1) | (1<<DDC2) | (1<<DDC3) | (1<<DDC4) | (1<<DDC5) | (1<<DDC6) | (1<<DDC7)); // PC0-7 Eingang!
56

57
    NOP();
58
    _delay_ms(50); // nochmal ein Debounce
59
    
60
      if(!(PINC & (1<<PINC0)))
61
        row = 4;
62
      if(!(PINC & (1<<PINC1)))
63
        row = 7;
64
      if(!(PINC & (1<<PINC2)))
65
        row = 10;
66
      if(!(PINC & (1<<PINC3)))
67
        row = 22;
68
      if(!(PINC & (1<<PINC4)))
69
        row = 22;
70
      if(!(PINC & (1<<PINC5)))
71
        row = 19;
72
      if(!(PINC & (1<<PINC6)))
73
        row = 16;
74
      if(!(PINC & (1<<PINC7)))
75
        row = 13;
76
  }
77
  return (row-col);
78
}
79

80
int main(void)
81
{
82
  PORTB = 0b00000111; // PB0-2 high
83
  DDRB &= ~ (1<<DDB0) | (1<<DDB1) | (1<<DDB2); // PB0-2 Eingang
84

85
  PORTC = 0b00000000; // PC0-7 low
86
  DDRC = 0xff; // PC0-7 Ausgang
87

88
  
89
  TCCR0 = (1<<CS02) | (1<<CS00); // Teilen durch 1024 / ~10ms
90
  TIMSK |= 1<<TOIE0;        // Timer aktivieren
91

92
  lcd_init(LCD_DISP_ON); // LCD Init
93
  lcd_clrscr(); // LCD Leeren
94
  initialize(); // Init 2 ( Nicht wichtig für euch)
95
  uart_init( UART_BAUD_SELECT(UART_BAUD_RATE,F_CPU) ); // UART An
96

97
  sei();
98

99
  while(1)
100
  {
101
    if(KEY_PRESSED) // Zum Test paar Abfragen der Tasten
102
    {
103
      if(KEY_PRESSED == KEY_ARROW_DOWN)
104
        lcd_puts("down pressed");
105
      else if(KEY_PRESSED == KEY_ENTER)
106
        lcd_puts("enter pressed");
107
      else if(KEY_PRESSED == KEY_ARROW_LEFT)
108
        lcd_puts("left pressed");
109
      else if(KEY_PRESSED == KEY_ARROW_UP)
110
        lcd_puts("up pressed");
111
      else if(KEY_PRESSED == KEY_0)
112
        lcd_puts("0 pressed");
113

114
      KEY_PRESSED = 0;
115
                        lcd_home();
116
    }
117
    
118
  }
119
}

Würde mich über Kritiken freuen.
Die Einzelnen Tasten sind in der Diagnose-Terminal.h einfach mittels
1
#define KEY_ENTER 6

definiert.

Danke :)

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