Subversion Repositories Projects

Compare Revisions

Ignore whitespace Rev 1052 → Rev 1053

/Transportables_Koptertool/tags/V3.2/timer.c
0,0 → 1,293
/*****************************************************************************
* Copyright (C) 2009 Peter "woggle" Mack, mac@denich.net *
* based on the key handling by Peter Dannegger *
* see www.mikrocontroller.net *
* Copyright (C) 2011 Christian Brandtner brandtner@brandtner.net *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
* *
* 22.03.2011 Zeitgesteuerte Displaybeleuchtung C.B. *
* *
*****************************************************************************/
 
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <string.h>
 
 
#include "main.h"
#include "timer.h"
#include "eeprom.h"
#include "lcd.h"
 
volatile uint16_t timer;
volatile uint16_t abo_timer;
volatile uint8_t pwm1 = 255;
 
#define FRAME_LENGTH 20
uint16_t icrval = (F_CPU / 64) * FRAME_LENGTH / 1000 ;
uint16_t minocr_a = (F_CPU / 64) * 1 / 1000;// 312
uint16_t maxocr_a = (F_CPU / 64) * 2 / 1000;// 624
uint16_t minocr_b = (F_CPU / 64) * 1 / 1000;// 312
uint16_t maxocr_b = (F_CPU / 64) * 2 / 1000;// 624
 
 
uint8_t key_state = 0; // debounced and inverted key state:
// bit = 1: key pressed
uint8_t key_press = 0; // key press detect
uint8_t key_rpt; // key long press and repeat
uint16_t DisplayTime = 0; // Leuchtdauer
volatile uint8_t Display_on;// Flag Display on/off
 
 
 
//*****************************************************************************
//
 
 
#if defined (__AVR_ATmega32__)
ISR(TIMER0_COMP_vect) // Timer-Interrupt (100 Hz)
#else
ISR(TIMER0_COMPA_vect) // Timer-Interrupt (100 Hz)
#endif
{
static uint8_t ct0 = 0;
static uint8_t ct1 = 0;
static uint8_t rpt = 0;
uint8_t i;
// Key handling by Peter Dannegger
// see www.mikrocontroller.net
i = key_state ^ ~KEY_PIN; // key changed ?
ct0 = ~(ct0 & i); // reset or count ct0
ct1 = ct0 ^ (ct1 & i); // reset or count ct1
i &= (ct0 & ct1); // count until roll over ?
key_state ^= i; // then toggle debounced state
key_press |= (key_state & i); // 0->1: key press detect
 
if (i!=0)
{ // Displaylicht einschalten, und bzw. Timeoutzählerreset wenn Taste gedrückt wurde
if (Display_on ==0)
{ // einschalten
// PORTD &= ~(1<<PORTD7);
#ifdef HWVERSION1_2
PORTC &= ~(1<<PORTC0);
PORTC &= ~(1<<PORTC1);
PORTD &= ~(1<<PORTD7);
#endif
#ifdef HWVERSION1_3
PORTD &= ~(1<<PORTD6);
PORTC &= ~(1<<PORTC2);
PORTD &= ~(1<<PORTD7);
#endif
#ifdef HWVERSION3_1
set_D_LIGHT();
#endif
 
} // if Display_on ==0
Display_on =1; // Flag Display on
DisplayTime = 0; // Timer Reset
 
}
 
 
if ((key_state & REPEAT_MASK) == 0) // check repeat function
{
rpt = REPEAT_START; // start delay
}
if (--rpt == 0)
{
rpt = REPEAT_NEXT; // repeat delay
key_rpt |= (key_state & REPEAT_MASK);
}
 
if (timer > 0)
{
timer --;
}
 
if (abo_timer > 0)
{
abo_timer --;
}
 
if (DisplayTimeout > 0)
 
{
if (Display_on==1)
{
DisplayTime++;
if ((DisplayTime/100) == DisplayTimeout) //ISR läuft mit 100Hz
 
{ //Displaylicht ausschalten
// PORTD |= (1<<PORTD7);
#ifdef HWVERSION1_2
PORTC |= (1<<PORTC0);
PORTC |= (1<<PORTC1);
PORTD |= (1<<PORTD7);
#endif
#ifdef HWVERSION1_3
PORTD |= (1<<PORTD6);
PORTD |= (1<<PORTD7);
PORTC |= (1<<PORTC2);
#endif
#ifdef HWVERSION3_1
clr_D_LIGHT();
#endif
Display_on = 0; // Flag Display off
}
}
 
}
 
 
}
 
 
void TIMER1_Init (void)
{
 
DDRD |= (1<<PORTD4)|(1<<PORTD5);
TCCR1A |= (1<<COM1B1) |(1<<COM1A1) | (1<<WGM11);
TCCR1B |= (1<<CS11)|(1<<CS10)| (1<<WGM12)| (1<<WGM13);
ICR1 = icrval;
OCR1A = minocr_a + ((maxocr_a-minocr_a)/2);
OCR1B = minocr_b + ((maxocr_b-minocr_b)/2);
 
// OCR1A = minocr_a;
// OCR1B = maxocr_b;
 
}
 
void set_pwm_a(uint16_t value)
{
uint16_t setv = ((value * 9) / 10 ) + minocr_a;
if((setv > 311)&&(setv < 637))
{
OCR1A = setv;
}
}
void set_pwm_b(uint8_t value)
{
uint16_t setv = ((value * 4 * 5 ) / 6 ) + minocr_b;
if((setv > 200)&&(setv < 750))
{
OCR1B = setv;
}
}
 
 
//*****************************************************************************
//
void TIMER0_Init (void)
{
timer = 0;
#if defined (__AVR_ATmega32__)
TCCR0 = (1 << CS02) | (1 << CS00) | (1 << WGM01); // Prescaler 1024
OCR0 = (F_CPU / (100L * 1024L)) ;
 
TIMSK |= (1 << OCIE0); // enable interrupt for OCR
#else
TCCR0A = (1 << WGM01);
TCCR0B = (1 << CS02) | (1 << CS00);
OCR0A = (F_CPU / (100L * 1024L)) ;
 
TIMSK0 |= (1 << OCIE0A); // enable interrupt for OCR
#endif
}
 
 
//*****************************************************************************
//
uint8_t get_key_press (uint8_t key_mask)
{
uint8_t sreg = SREG;
// disable all interrupts
cli();
key_mask &= key_press; // read key(s)
key_press ^= key_mask; // clear key(s)
SREG = sreg; // restore status register
return key_mask;
}
 
 
//*****************************************************************************
//
uint8_t get_key_rpt (uint8_t key_mask)
{
uint8_t sreg = SREG;
 
// disable all interrupts
cli();
key_mask &= key_rpt; // read key(s)
key_rpt ^= key_mask; // clear key(s)
SREG = sreg; // restore status register
return key_mask;
}
 
 
//*****************************************************************************
//
uint8_t get_key_short (uint8_t key_mask)
{
uint8_t ret;
uint8_t sreg = SREG;
// disable all interrupts
cli();
ret = get_key_press (~key_state & key_mask);
SREG = sreg; // restore status register
return ret;
}
 
 
//*****************************************************************************
//
uint8_t get_key_long (uint8_t key_mask)
{
return get_key_press (get_key_rpt (key_mask));
}
 
 
//*****************************************************************************
//
uint8_t get_key_long2 (uint8_t key_mask)
{
return get_key_press (get_key_rpt (key_press^key_mask));
}
 
 
//*****************************************************************************
//
uint8_t get_key_long_rpt (uint8_t key_mask)
{
return get_key_rpt (~key_press^key_mask);
}