C-Programm für ein HUD
// ***********************************************************
// Project: HUD
// Author: Holger
// Module description: Main
// ***********************************************************
#define F_CPU 8000000
#define ADV_Ref 2.5 // A/D Wandler referenz
#include <avr\io.h> // Most basic include files
#include <stdlib.h>
#include <stdio.h>
#include <avr\interrupt.h> // Add the necessary ones
#include <compat/deprecated.h>
#include “lcd-4×16.h”
#define KEY_DDR DDRC
#define KEY_PORT PORTC
#define KEY_PIN PINC
#define KEY0 0
#define KEY1 1
#define KEY2 2
#define ALL_KEYS (1<<KEY0 | 1<<KEY1 | 1<<KEY2)
#define REPEAT_MASK (1<<KEY1 | 1<<KEY2) // repeat: key1, key2
#define REPEAT_START 50 // after 500ms
#define REPEAT_NEXT 20 // every 200ms
// Define here the global static variables
//
int My_global;
volatile uint8_t key_state; // debounced and inverted key state:
// bit = 1: key pressed
volatile uint8_t key_press; // key press detect
volatile uint8_t key_rpt; // key long press and repeat
// Interrupt handler example for INT0
//
//SIGNAL(SIG_INTERRUPT0) {
//
//}
ISR( TIMER0_OVF_vect ) // every 10ms
{
static uint8_t ct0, ct1, rpt;
uint8_t i;
TCNT0 = (uint8_t)(int16_t)-(F_CPU / 1024 * 10e-3 + 0.5); // preload for 10ms
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( (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;
}
}
///////////////////////////////////////////////////////////////////
// READ A/D Wandler
//
// It is recommended to use this coding style to
// follow better the mixed C-assembly code in the
// Program Memory window
//
uint16_t ReadADC(uint8_t mux)
{
uint8_t i;
uint16_t result;
ADMUX = mux; // Kanal waehlen
ADMUX |= (1<<REFS1) | (1<<REFS0); // interne Referenzspannung nutzen
ADCSRA = (1<<ADEN) | (1<<ADPS1) | (1<<ADPS0); // Frequenzvorteiler
// setzen auf 8 (1) und ADC aktivieren (1)
/* nach Aktivieren des ADC wird ein “Dummy-Readout” empfohlen, man liest
also einen Wert und verwirft diesen, um den ADC “warmlaufen zu lassen” */
ADCSRA |= (1<<ADSC); // eine ADC-Wandlung
while ( ADCSRA & (1<<ADSC) ) {
; // auf Abschluss der Konvertierung warten
}
result = ADCW; // ADCW muss einmal gelesen werden,
// sonst wird Ergebnis der nächsten Wandlung
// nicht übernommen.
/* Eigentliche Messung – Mittelwert aus 4 aufeinanderfolgenden Wandlungen */
result = 0;
for( i=0; i<4; i++ )
{
ADCSRA |= (1<<ADSC); // eine Wandlung “single conversion”
while ( ADCSRA & (1<<ADSC) ) {
; // auf Abschluss der Konvertierung warten
}
result += ADCW; // Wandlungsergebnisse aufaddieren
}
ADCSRA &= ~(1<<ADEN); // ADC deaktivieren (2)
result /= 4; // Summe durch vier teilen = arithm. Mittelwert
return result;
}
///////////////////////////////////////////////////////////////////
//
// check if a key has been pressed. Each pressed key is reported
// only once
//
uint8_t get_key_press( uint8_t key_mask )
{
cli(); // read and clear atomic !
key_mask &= key_press; // read key(s)
key_press ^= key_mask; // clear key(s)
sei();
return key_mask;
}
///////////////////////////////////////////////////////////////////
//
// check if a key has been pressed long enough such that the
// key repeat functionality kicks in. After a small setup delay
// the key is reported beeing pressed in subsequent calls
// to this function. This simulates the user repeatedly
// pressing and releasing the key.
//
uint8_t get_key_rpt( uint8_t key_mask )
{
cli(); // read and clear atomic !
key_mask &= key_rpt; // read key(s)
key_rpt ^= key_mask; // clear key(s)
sei();
return key_mask;
}
///////////////////////////////////////////////////////////////////
//
uint8_t get_key_short( uint8_t key_mask )
{
cli(); // read key state and key press atomic !
return get_key_press( ~key_state & key_mask );
}
///////////////////////////////////////////////////////////////////
//
uint8_t get_key_long( uint8_t key_mask )
{
return get_key_press( get_key_rpt( key_mask ));
}
// ***********************************************************
// Main program
//
int main(void)
{
uint16_t adc0;
float Bias, Calair, Po2;
KEY_DDR &= ~ALL_KEYS; // konfigure key port for input
KEY_PORT |= ALL_KEYS; // and turn on pull up resistors
DDRC = DDRC | 0xF0; // Led oberes halbbyte und Taster unteres halbbyte
PORTC = PORTC | 0xF0; // LEDs an VDD, daher erst mal alle Outports High
TCCR0 = (1<<CS02)|(1<<CS00); // divide by 1024
TIMSK = 1<<TOIE0; // enable timer interrupt
lcd_init();
Calair=12.0;
Bias=Calair * 20.0;
Bias=Bias / ADV_Ref;
set_cursor(0,1);
lcd_string(“Po2”);
while(1)
{
adc0=ReadADC(0);
Po2= adc0 / Bias;
Po2= Po2 * 0.21;
set_cursor(0,2);
lcd_putf(Po2,3,2);
if ( Po2 >= 0.9 && Po2 <= 1.3 )
{
cbi(PORTC, 4);
sbi(PORTC, 5);
}
if ( Po2 > 1.3 )
{
sbi(PORTC, 4);
cbi(PORTC, 5);
}
if ( Po2 < 0.9 )
{
cbi(PORTC, 4);
cbi(PORTC, 5);
}
// single press
if( debounce(&PORTC, 0)== 1 ) {
adc0=ReadADC(0);
Calair = adc0 * ADV_Ref;
Calair = Calair / 20.0;
Bias = Calair * 20.0; // gemessener Cal-Wert * Verstärkung (20 fach)
Bias = Bias / ADV_Ref; // Errechne Digits (1 Digit entspricht 2.5 mV Vref/1024)
}
return 0;
}
Kategorie: , Aktualisiert am 3. November 2016 von tauchcomputer | Log in