Subversion Repositories MK3Mag

Rev

Rev 18 | Rev 22 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 18 Rev 19
Line 52... Line 52...
52
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
52
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
53
// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
53
// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
54
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
54
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
55
// +  POSSIBILITY OF SUCH DAMAGE. 
55
// +  POSSIBILITY OF SUCH DAMAGE.
56
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
56
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
 
57
#include <inttypes.h>
-
 
58
#include <avr/io.h>
-
 
59
#include <avr/interrupt.h>
57
#include "main.h"
60
#include "main.h"
58
volatile unsigned int CountMilliseconds = 0;
-
 
59
volatile unsigned char Timer0Overflow;
-
 
60
unsigned int I2C_Timeout = 0;
-
 
61
unsigned int SIO_Timeout = 0;
-
 
62
 
-
 
63
 
-
 
64
enum {
-
 
65
  STOP             = 0,
-
 
66
  CK               = 1,
-
 
67
  CK8              = 2,
-
 
68
  CK64             = 3,
-
 
69
  CK256            = 4,
-
 
70
  CK1024           = 5,
-
 
71
  T0_FALLING_EDGE  = 6,
-
 
72
  T0_RISING_EDGE   = 7
-
 
73
};
-
 
74
 
-
 
75
 
-
 
76
SIGNAL(SIG_OVERFLOW0)
-
 
77
{
-
 
78
 static unsigned char cnt;
-
 
79
 static unsigned int cmps_cnt;
-
 
80
 TCNT0 -= 101;  // reload
-
 
81
 Timer0Overflow++;
-
 
Line -... Line 61...
-
 
61
 
-
 
62
volatile uint16_t CountMilliseconds = 0;
-
 
63
volatile uint16_t I2C_Timeout = 0;
-
 
64
 
-
 
65
 
-
 
66
/*****************************************************/
-
 
67
/*              Initialize Timer 0                   */
82
 
68
/*****************************************************/
-
 
69
void TIMER0_Init(void)
-
 
70
{
-
 
71
        uint8_t sreg = SREG;
-
 
72
 
-
 
73
        // disable all interrupts before reconfiguration
-
 
74
        cli();
-
 
75
 
-
 
76
        // set PB2 as output for the PWM used to signal compass heading
-
 
77
        DDRB |= (1<<DDB2);
-
 
78
        PORTB &= ~(1<<PORTB2);
-
 
79
 
-
 
80
 
-
 
81
        // Timer/Counter 0 Control Register A
-
 
82
 
-
 
83
        // Normal Timer Counter Mode (Bits WGM02 = 0, WGM01 = 0, WGM00 = 0)
-
 
84
    // OC0A disconnected (Bits COM0A1 = 0, COM0A0 = 0)
-
 
85
    // OC0B disconnected (Bits COM0B1 = 0, COM0B0 = 0)
-
 
86
    TCCR0A &= ~((1<<COM0A1)|(1<<COM0A0)|(1<<COM0B1)|(1<<COM0B0)|(1<<WGM01)|(1<<WGM00));
-
 
87
 
-
 
88
        // Timer/Counter 0 Control Register B
-
 
89
 
-
 
90
        // set clock devider for timer 0 to SYSKLOCK/8 = 8MHz / 8 = 1MHz
-
 
91
        // i.e. the timer increments from 0x00 to 0xFF with an update rate of 2.5 MHz
-
 
92
        // hence the timer overflow interrupt frequency is 2.5 MHz / 256 = 9.765 kHz
-
 
93
 
-
 
94
        // divider 8 (Bits CS02 = 0, CS01 = 1, CS00 = 0)
-
 
95
        TCCR0B &= ~((1<<FOC0A)|(1<<FOC0B)|(1<<WGM02)|(1<<CS02));
-
 
96
    TCCR0B = (1<<CS01)|(0<<CS00);
-
 
97
 
-
 
98
        // init Timer/Counter 0 Register
-
 
99
    TCNT0 = 0;
-
 
100
 
-
 
101
        // Timer/Counter 0 Interrupt Mask Register
-
 
102
        // enable timer overflow interrupt only
-
 
103
        TIMSK0 &= ~((1<<OCIE0B)|(1<<OCIE0A));
-
 
104
        TIMSK0 |= (1<<TOIE0);
-
 
105
 
-
 
106
        SREG = sreg;
-
 
107
}
-
 
108
 
-
 
109
 
-
 
110
// -----------------------------------------------------------------------
83
 if(++cmps_cnt == 380)
111
ISR(TIMER0_OVF_vect)
-
 
112
{
-
 
113
        static uint8_t cnt;
-
 
114
        static uint16_t cmps_cnt;
-
 
115
 
-
 
116
        // reload timer register so that overflow occurs after 100 increments at 1 MHz
-
 
117
        // resulting in a calling rate of this ISR of 10kHz or 0.1 ms.
-
 
118
        TCNT0 -= 101;
-
 
119
 
-
 
120
        // disable PWM when bad compass heading value
-
 
121
        if(Heading < 0)
84
 {
122
        {
85
  PORTB |= 0x04;
123
                PORTB &= ~(PORTB2);
86
  cmps_cnt = 0;
124
                cmps_cnt = 0;
87
 }
125
        }
88
 else
-
 
89
 if(cmps_cnt == PwmHeading)
126
        else
-
 
127
        {
-
 
128
                // if a periode of 38.0 ms is over
-
 
129
                if(++cmps_cnt >= 380)
-
 
130
                {
90
 {
131
                        // set PWM out to high
-
 
132
                        PORTB |= PORTB2;
-
 
133
                        // reset periode counter
-
 
134
                        cmps_cnt = 0;
-
 
135
                }
-
 
136
                // if the delay in 0.1 ms is equal to Heading + 10
-
 
137
                else if(cmps_cnt >= (Heading + 10))
-
 
138
                {
-
 
139
                        // set PWM out to low
-
 
140
                        PORTB &= ~(PORTB2);
91
  PORTB &= ~0x04;
141
                }
Line 92... Line 142...
92
 }
142
        }
93
 
143
 
-
 
144
        if(!--cnt)
94
 if(!--cnt)
145
        {
95
  {
146
                // every 10th run (1kHz or 1ms)
96
   cnt = 10;
147
                cnt = 10;
97
   CountMilliseconds += 1;
-
 
98
   if(I2C_Timeout) I2C_Timeout--;
148
                CountMilliseconds++;
99
   if(SIO_Timeout) SIO_Timeout--;
149
                if(I2C_Timeout) I2C_Timeout--;
Line 100... Line -...
100
  }
-
 
101
}
-
 
102
 
150
        }
103
 
-
 
104
void Timer0_Init(void)
-
 
105
{
-
 
106
 TCCR0B = TIMER_TEILER;  // Starten des Timers
-
 
107
// TCNT0 = 100;  // reload
-
 
108
 TIM0_START;
-
 
109
 TIMER2_INT_ENABLE;
151
}
110
}
152
 
111
 
153
 
112
 
154
// -----------------------------------------------------------------------
Line -... Line 155...
-
 
155
uint16_t SetDelay (uint16_t t)
113
unsigned int SetDelay(unsigned int t)
156
{
114
{
157
  return(CountMilliseconds + t - 1);
115
  return(CountMilliseconds + t - 1);                                            
158
}
116
}
159
 
Line -... Line 160...
-
 
160
// -----------------------------------------------------------------------
117
 
161
int8_t CheckDelay(uint16_t t)
118
char CheckDelay (unsigned int t)
162
{
119
{
163
        return(((t - CountMilliseconds) & 0x8000) >> 8); // check sign bit
120
  return(((t - CountMilliseconds) & 0x8000) >> 8);
164
}
121
}
165
 
122
 
166
// -----------------------------------------------------------------------
-
 
167
void Delay_ms(uint16_t wait)
-
 
168
{
-
 
169
        uint16_t t_stop;
-
 
170
        t_stop = SetDelay(wait);