Interrupts.c


1
//=========================================================
2
// src/Interrupts.c: generated by Hardware Configurator
3
//
4
// This file will be regenerated when saving a document.
5
// leave the sections inside the "$[...]" comment tags alone
6
// or they will be overwritten!
7
//=========================================================
8
9
// USER INCLUDES
10
#include <IREF_UART_main.h>
11
#include <SI_EFM8SB1_Register_Enums.h>
12
#include "retargetserial.h"
13
#include "SI_EFM8SB1_Defs.h"
14
SI_SBIT(PA,SFR_P0,2);
15
SI_SBIT(PB,SFR_P0,3);
16
SI_SBIT (LED0, SFR_P1, 1); // '1' means ON, '0' means OFF
17
#ifdef SDCC
18
#include <sdcc_stdio.h>
19
#endif
20
21
char *GETS(char *, uint16_t);
22
23
#ifdef __C51__
24
#define GETS gets
25
#endif
26
27
#ifdef SDCC
28
#include <sdcc_stdio.c>
29
#endif
30
31
//------------------------------------------------------------------------
32
//Global Constants
33
34
#define UART_BUFFERSIZE        64
35
36
//------------------------------------------------------------------------
37
//Global variables
38
uint8_t UART_Buffer[UART_BUFFERSIZE];
39
uint8_t UART_Input_First = 0;
40
uint8_t UART_Output_First = 0;
41
42
//-----------------------------------------------------------------------------
43
// INT0_ISR
44
//-----------------------------------------------------------------------------
45
//
46
// INT0 ISR Content goes here. Remember to clear flag bits:
47
// TCON::IE0 (External Interrupt 0)
48
//
49
//-----------------------------------------------------------------------------
50
SI_INTERRUPT (INT0_ISR, INT0_IRQn)
51
{
52
53
  IE|= IE_EX0__DISABLED; // Disable EX0 interrupts
54
55
  if(PB)//if Pin B is high -> turn clockwise
56
  {
57
58
    if((IREF0CN0 & 0x3F) < 0x3F )
59
    {
60
61
      // increase current
62
      rising = 1;
63
64
    }
65
    if(rising)
66
    {
67
      // Increment the output current
68
      IREF0CN0++;
69
      virtPos++;
70
71
      // If we have reached the maximum value
72
      if((IREF0CN0 & 0x3F) >= 0x3F)
73
      {
74
        LED0=0; //Toggle LED
75
76
        // keep current as-is
77
        rising = 0;
78
        virtPos=0;
79
80
      }
81
      Wait_MS(100);                  // Wait [input] milliseconds
82
83
    }
84
    else
85
    {
86
      //keep current as-is
87
      virtPos=0;
88
      IREF0CN0=MAX_VALUE;
89
    }
90
  }
91
  else if(!PB)  // if Pin B is low -> turn anti-clockwise
92
  {
93
    if((IREF0CN0 & 0x3F) > 0x00)     //decrease current
94
    {
95
      decreasing =1;
96
    }
97
    // Decrement the output current
98
    if (decreasing)
99
    {
100
      IREF0CN0--;
101
      virtPos--;
102
103
      // If we have reached the minimum value
104
      if((IREF0CN0 & MAX_VALUE) == MIN_VALUE)
105
      {
106
        //keep current as-is
107
        virtPos=100;
108
        decreasing = 0;
109
      }
110
111
      Wait_MS(0);                  // Wait [input] milliseconds
112
113
    }
114
115
  }
116
  //Wait_MS();
117
  IE|=IE_EX0__ENABLED;//enable EX0 interrupts
118
119
}
120
121
SI_INTERRUPT (TIMER2_ISR, TIMER2_IRQn)
122
{
123
  //current mode
124
  if (mode==1)
125
  {
126
    IREF0CN0|=IREF0CN0_MDSEL__HIGH_CURRENT;
127
  }
128
  else
129
  {
130
    IREF0CN0|=IREF0CN0_MDSEL__LOW_POWER;
131
  }
132
133
  //disable timer interrupts, initialize values
134
  IE|=IE_ET2__DISABLED;
135
  rising=0;
136
  decreasing=0;
137
  if(PA!=PAStat)
138
  {
139
    // debounce
140
    Wait_MS(20);
141
    PAStat=PA;
142
    if(PB) //if Pin B is high -> turn clockwise
143
    {
144
      if((IREF0CN0 & 0x3F) < 0x3F)
145
      {
146
        // increase current
147
        rising = 1;
148
149
      }
150
      if(rising)
151
      {
152
        // Increment the output current
153
        IREF0CN0++;
154
        virtPos++;
155
156
        // If we have reached the maximum value
157
        if((IREF0CN0 & 0x3F) >= 0x3F)
158
        {
159
          // keep current as-is
160
          rising = 0;
161
          virtPos=0;
162
163
        }
164
165
      }
166
      else
167
      {
168
        //keep current as-is
169
        virtPos=0;
170
        IREF0CN0=0x3F;
171
      }
172
    }
173
    else if(!PB)  // if Pin B is low -> turn anti-clockwise
174
    {
175
176
177
      if((IREF0CN0 & 0x3F) > 0x00) //decrease current
178
      {
179
        decreasing =1;
180
      }
181
      // Decrement the output current
182
      if (decreasing)
183
      {
184
        IREF0CN0--;
185
        virtPos--;
186
187
        // If we have reached the minimum value
188
        if((IREF0CN0 & 0x3F) == 0x00)
189
        {
190
          //keep current as-is
191
          virtPos=100;
192
          decreasing = 0;
193
        }
194
195
196
      }
197
198
    }
199
    //set transmit flag
200
    SCON0_TI=1;
201
  }
202
  IE|=IE_ET2__ENABLED;
203
204
}
205
//-----------------------------------------------------------------------------
206
// UART0_ISR
207
//-----------------------------------------------------------------------------
208
//
209
// UART0 ISR Content goes here. Remember to clear flag bits:
210
// SCON0::RI (Receive Interrupt Flag)
211
// SCON0::TI (Transmit Interrupt Flag)
212
//
213
//-----------------------------------------------------------------------------
214
SI_INTERRUPT (UART0_ISR, UART0_IRQn)
215
{
216
  if(SCON0_TI==1)
217
  {
218
219
    LED0=0;
220
    IE|=IE_ES0__DISABLED; //disable UART interrupts
221
    SCON0_TI=0;//clear transmit flag
222
223
    SBUF0= IREF0CN0;
224
225
    IE|=IE_ES0__ENABLED;//enable UART interrupts
226
227
  }
228
}
229
230
//-----------------------------------------------------------------------------
231
// TIMER0_ISR
232
//-----------------------------------------------------------------------------
233
//
234
// TIMER0 ISR Content goes here. Remember to clear flag bits:
235
// TCON::TF0 (Timer 0 Overflow Flag)
236
//
237
//-----------------------------------------------------------------------------
238
//-----------------------------------------------------------------------------
239
// TIMER0_ISR
240
//-----------------------------------------------------------------------------
241
//
242
// TIMER0 ISR Content goes here. Remember to clear flag bits:
243
// TCON::TF0 (Timer 0 Overflow Flag)
244
//
245
//-----------------------------------------------------------------------------