Interrupts.c


1
/*********************************************************************
2
 *              General Procedures Libary Source Code
3
 *********************************************************************
4
 * FileName:        GenProcs.c
5
 * Dependencies:    GenProcs.h
6
 * Processor:       PIC18F4685
7
 * Complier:        Microchip C18
8
 *
9
 *
10
 * Author               Date    Comment
11
 *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
12
 * Stephan Krause       17/8/12 Version 1.0 - Initial Release
13
 *********************************************************************/
14
15
16
17
#include "Interrupts.h"
18
19
20
#pragma code high_vector=0x08
21
  void HightInterrupt(void) { _asm GOTO ISR _endasm }
22
#pragma code
23
24
#pragma code low_vector=0x18
25
  void LowInterrupt(void) { _asm GOTO ISR _endasm }
26
#pragma code
27
28
29
volatile int enc_delta=0;
30
31
static const int enc_table[16] = {    0,    /* 0000 */
32
                0,    /* 0001 */
33
                -1,    /* 0010 */
34
                0,    /* 0011 */
35
                0,    /* 0100 */
36
                0,    /* 0101 */
37
                0,    /* 0110 */
38
                0,    /* 0111 */
39
                1,    /* 1000 */
40
                0,    /* 1001 */
41
                0,    /* 1010 */
42
                0,    /* 1011 */
43
                0,    /* 1100 */
44
                0,    /* 1101 */
45
                0,    /* 1110 */
46
                0};    /* 1111 */
47
48
49
50
#pragma interruptlow ISR
51
void ISR(void) {
52
  static unsigned int counter=0;
53
  static int enc_last;
54
  
55
  if (INTCONbits.TMR0IF){  // overflow every 2,5ms
56
    // encoder einlesen
57
    
58
    enc_last = (enc_last << 2) & 0x0F;
59
    
60
    if (ENC_A) enc_last |= 0x01;
61
    if (ENC_B) enc_last |= 0x02;
62
    
63
    
64
    enc_delta += enc_table[enc_last];
65
            
66
    if(counter%10 == 0) Key_Debounce();
67
    
68
    if(counter>=200) {
69
      LED_GR ^= 0x01;  // blinken aller 200*2,5ms = 500ms
70
      counter = 0;
71
    } else {
72
      counter++;
73
    }
74
    
75
    WriteTimer0(101);
76
    INTCONbits.TMR0IF = 0;
77
  } else if (PIR1bits.TMR1IF) {
78
79
    PIR1bits.TMR1IF = 0;
80
  } else if (PIR2bits.ECCP1IF) {
81
    
82
    PIR2bits.ECCP1IF = 0;
83
  }
84
85
86
  
87
  
88
}
89
90
91
92
93
94
void InitInterrupts(void) {
95
  
96
  // enable timer0, always done in InitTimer() in GenProcs.c
97
  //INTCONbits.TMR0IE = 1;    // enable timer0 int
98
  //INTCON2bits.TMR0IP = 0;    // low prio
99
  
100
  INTCONbits.PEIE = 1;  // enable peripheral interrupts
101
  INTCONbits.GIE = 1;    // enable global interrupts
102
  
103
}