< Syntax Highlighting for File ID: 5

Download Datei: C/C_Meter_Ver1.c

/*
 * main.c
 *
 *  Created on: 17.12.2010
 *      Author: darkhedie
 */
 
#include <avr/io.h>
#include <util/delay.h>
#include <stdlib.h>
#include <avr/interrupt.h>
 
/////////////////////////////////////////////
//Definition der einzelnen Pins
/////////////////////////////////////////////
#define 	RS_0		PORTB &= ~(1<<PB2);
#define 	RS_1		PORTB |= (1<<PB2);
#define 	E_0			PORTB &= ~(1<<PB4);
#define 	E_1			PORTB |= (1<<PB4);
#define 	SR_CLK_0	PORTB &= ~(1<<PB1);
#define 	SR_CLK_1	PORTB |= (1<<PB1);
#define		SR_D_1		PORTB |= (1<<PB0);
#define 	SR_D_0		PORTB &= ~(1<<PB0);
#define		COMP_IN		PINB & (1<<PINB3)
#define		resistor	17 				//Wert in KiloOhm
 
/////////////////////////////////////////////
// Programm spezifische Definitionen
/////////////////////////////////////////////
void write_char(unsigned char ucChar);
void lcd_string( const char *data );
void write_data(unsigned char ucChar);
 
unsigned long mikrotime = 0;
unsigned long mikrotime_saved = 0;
unsigned int cap = 0;
unsigned int vorkomma = 0;
unsigned int nachkomma = 0;
 
 
ISR(PCINT0_vect)
{
	if(COMP_IN)		mikrotime = 0;	//Kondensator wurde angeschlossen!
	else
	{
		mikrotime_saved = mikrotime;
		cap = (mikrotime_saved / resistor) / 10;
		char Buffer[10]; // in diesem {} lokal
			vorkomma = cap / 10;
			nachkomma = cap % 10;
		     itoa( vorkomma, Buffer, 10 );
 
		     write_data(0x01);
		     _delay_ms(2);
		     write_data(0x02);
		     lcd_string( Buffer );
		     write_char('.');
		     itoa( nachkomma, Buffer, 10 );
		     lcd_string( Buffer );
		     write_char(0xE4);
		     write_char('F');
 
	}
	/*char Buffer[10]; // in diesem {} lokal
	 itoa( mikrotime, Buffer, 10 );
 
	 lcd_string( Buffer );*/
}
 
ISR(TIM1_COMPA_vect)
{
	mikrotime++;
}
 
void lcd_string( const char *data )
{
    while( *data != '\0' )
        write_char( *data++ );
}
 
 
void setup(void)
{
	DDRB |= (1 << DDB0) | (1 << DDB1) | (1 << DDB2) | (1 << DDB4);	//Ausgänge
	DDRB &= ~( 1 << DDB3 );	//Komparator Eingang
 
	GIMSK |= (1<<PCIE);		//Interrupt Aktivieren
	PCMSK |= (1<<PCINT3);
 
	OCR1C = 5;				//Bis auf 5 Zählen somit 10us
	TCCR1 |= (1 << CS10) | (1 << CS12) | (1 << CTC1);	//8Mhz / 16 500kHz
	TIMSK |= 1<<OCIE1A;		//Overflow Interrupt
	sei();
}
 
void clear_sr(void)
{
	unsigned char ucCounter = 0;
	SR_D_0;					//low anlegen und clocken
	while(ucCounter != 8)
	{
		SR_CLK_1;
		_delay_us(1);
		SR_CLK_0
		_delay_us(1);
		ucCounter++;
	}
}
 
void strobe_clk(void)
{
	SR_CLK_1;
	_delay_us(10);
	SR_CLK_0;
	_delay_us(10);
}
 
void sr_data(unsigned char ucData)
{
	unsigned char ucCounter = 0;
	for (ucCounter = 0; ucCounter < 8; ++ucCounter)
	{
		if((ucData & 0x80) > 0) SR_D_1
		else SR_D_0;
		ucData = ucData << 1;
		strobe_clk();
	}
}
 
void write_data(unsigned char ucData)
{
	//clear_sr();
	RS_0;
	sr_data(ucData);
	_delay_us(100);
	E_1;
	_delay_us(100);
	E_0;
	_delay_us(100);
}
 
void write_char(unsigned char ucChar)
{
	//clear_sr();
	RS_1;
	sr_data(ucChar);
	_delay_us(100);
	E_1;
	_delay_us(100);
	E_0;
	_delay_us(100);
}
void lcd_enable(void)
{
	E_1;
	_delay_us(1);
	E_0;
	_delay_us(1);
}
 
void init_lcd(void)
{
	_delay_ms(50);
	write_data(0x30);
	_delay_ms(50);
	write_data(0x30);
	_delay_ms(50);
	write_data(0x30);
	_delay_ms(50);
	write_data(0x38);
	_delay_ms(50);
	write_data(0x0F);
	_delay_ms(50);
	write_data(0x01);
	_delay_ms(50);
	write_data(0x06);
	_delay_ms(50);
	write_data(0x02);
}
 
 
int main(void)
{
	setup();
	init_lcd();
 
	while(1)
	{
 
			_delay_ms(1000 );
	}
 
}