Also nochmal zu dem Programm.
Diesmal mit vollständigen Quellcodes:
usart_emp.h
Code:
#include <usart_bibliothek.h>
int hilf=0;
int id=1;
/*void set_id(int x) // veraltet
{
id=x;
}
*/
void set_id (void) {
DDRC=0x00; //Port C = Input
id =PINC-240; // Einlesen der PINs an Port C. Wert korrektur verbesserungswürdig
id=~id-65520;
}
int empfange (void) {
char buffer [10];
unsigned erg=0;
unsigned char data = 0;
data = USART_receive ();
if (data == '?'){
data = USART_receive ();
if (data == 'V'){
data = USART_receive ();
hilf=data;
hilf=hilf-48;
if (hilf==id ){
data = USART_receive ();
hilf=data;
hilf=hilf-48;
if (hilf<8)
{
erg=ad_port(hilf);
utoa(erg, buffer, 10); // Wandelt in String
USART_transmit('\t');
USART_transmit_string (buffer);
USART_transmit('\r');
USART_transmit('\n');
//return 0;
}
}
}
else{
if(data == 'A'){
data = USART_receive ();
hilf=data;
hilf=hilf-48;
//char buffer [10];
unsigned erg=0;
if(hilf==id){
USART_transmit('\t');
for (int i=0;i<=7;i++)
{
int erg=ad_port(i);
utoa(erg, buffer, 10); // Wandelt in String
USART_transmit_string (buffer);
USART_transmit('\t');
}
USART_transmit('\r');
USART_transmit('\n');
} } }
}
else{
if(data == '!'){
//USART_transmit_string (" 1.schleife ");
data = USART_receive ();
if(data == 'I'){
//USART_transmit_string (" 2.schleife ");
data = USART_receive ();
hilf=data;
hilf=hilf-48;
if(hilf == id){
//USART_transmit_string (" 3.schleife ");
//USART_transmit_string (" Reset ");
DDRB = 0xff;
PORTB = 0xff-0x01;
delay_ms(3000);
}
}
else{
if(data == 'T'){
int t=0;
int h=0;
int z=0;
int e=0;
int zwischen_erg=0;
data = USART_receive ();
hilf=data;
hilf=hilf-48;
if(hilf == id){
data = USART_receive ();
hilf=data;
t=hilf-48;
zwischen_erg=zwischen_erg+t*1000;
data = USART_receive ();
hilf=data;
h=hilf-48;
zwischen_erg=zwischen_erg+h*100;
data = USART_receive ();
hilf=data;
z=hilf-48;
zwischen_erg=zwischen_erg+z*10;
data = USART_receive ();
hilf=data;
e=hilf-48;
zwischen_erg=zwischen_erg+e*1;
// debug code
/*utoa(zwischen_erg, buffer, 10); // Wandelt in String
USART_transmit_string (buffer);
USART_transmit('\t');
*/
return zwischen_erg;
} }
else
{}
}
}
else
{}
}
return 9;
}
usart_bibilothek.h
Code:
/***********************************************************
Funktionsbibliothek für den USART des Atmega8
***********************************************************/
#include <avr/io.h>
#define F_CPU 4000000
#define USART_BAUD_RATE 9600
#define USART_BAUD_SELECT (F_CPU/(USART_BAUD_RATE*16L)-1)
void USART_Init(void) {
UCSRB = _BV(RXCIE) | _BV(TXCIE) | _BV(RXEN) | _BV(TXEN); // wenn einer der zustände 1 dann schreiben
UBRRL = (unsigned char) USART_BAUD_SELECT;
}
void USART_transmit (unsigned char c) {
while (!(UCSRA & (1<<UDRE))) {} //wenn speicherfrei und keine Fehler
UDR = c;
}
void USART_trans (unsigned int a)
{
while (!(UCSRA & (1<<UDRE))) {}
UDR = a;
}
unsigned char USART_receive (void) {
while(!(UCSRA & (1<<RXC))) {}
return UDR;
}
void USART_transmit_string (unsigned char *string) {
while (!(UCSRA & (1<<UDRE))) {}
while ( *string)
USART_transmit (*string++);
}
main_prg
Code:
#include <avr/io.h>
#include <stdlib.h>
#include <avr/signal.h>
#include <avr/interrupt.h>
#include <simple_delayroutinen.c>
#include <io_modul.h>
volatile unsigned int grenzwert;
int hilfs;
SIGNAL (SIG_UART_RECV) {
int x=0;
char buffer [10];
unsigned erg=0;
hilfs=empfange();
if(hilfs>10){
grenzwert=hilfs;
//debug code
/*USART_transmit('\t');
USART_transmit_string (" Im interrupt ");
utoa(grenzwert, buffer, 10);
USART_transmit_string (buffer);
USART_transmit('\t');*/
}
}
int main (void) {
grenzwert=800;
//Intialisierung
USART_Init ();
ad_init();
set_id();
v_ref(5);
USART_transmit_string ("!!! Spannungsüberwachung !!!");
USART_transmit('\n');
USART_transmit('\r');
DDRB = 0xff;
// Variablen
int x=0;
char buffer [10];
unsigned erg=0;
sei (); // Interrupt erlauben
delay_ms(8000); //Einschaltzeitverzögerung
for(;;){
PORTB = 0xff;
for (int i=0;i<=3;i++) // Überwachen der Ports 0 bis i
{
erg=ad_port(i); // Port auslesen
if(erg < grenzwert)
{
/*utoa(grenzwert, buffer, 10);
USART_transmit('\t');
USART_transmit_string (" In Hauptprg ");
USART_transmit_string (buffer);
*/
PORTB = 0xff-0x02;
delay_ms(1000); // Wenn Wert zu niedrig Pin2 von Port B einschalten
//debug text
/*utoa(i, buffer, 10);
USART_transmit_string (" Fehler bei ");
USART_transmit_string (buffer);
utoa(grenzwert, buffer, 10);
USART_transmit('\t');
USART_transmit_string (buffer);
USART_transmit('\t');
USART_transmit('\n');
USART_transmit('\r');
*/
}
}
}
return 0;
}
io_modul.h
Code:
#include <avr/io.h>
#include <stdlib.h>
#include <avr/signal.h>
#include <avr/interrupt.h>
//#include <usart_bibliothek.h>
#include <ad_read.h>
#include <usart_emp.h>
int daten (void) {
USART_Init ();
// Variablen
int x=0;
char buffer [10];
unsigned erg=0;
ad_init();
v_ref(5);
x=empfange();
if (x<8)
{
erg=ad_port(x);
utoa(erg, buffer, 10); // Wandelt in String
USART_transmit('\t');
USART_transmit_string (buffer);
USART_transmit('\r');
USART_transmit('\n');
return 0;
}
else
{
return x;
}
}
ad_read.h
Code:
#include <avr/io.h>
#include <stdlib.h>
#include <avr/signal.h>
#include <avr/interrupt.h>
//#include <usart_bibliothek.h>
unsigned int vref=3;
void v_ref(int x)
{
vref=x;
}
void ad_init(void)
{
ADCSRA = _BV(ADEN) | _BV(ADPS2); //Prescaler
}
unsigned ad_port (int x)
{
unsigned int high_bit=0;
unsigned int unten_bit=0;
unsigned erg=0;
switch(x) // Portwahl
{
case 0: break;
case 1: ADMUX |= _BV(MUX0);
break;
case 2: ADMUX |= _BV(MUX1);
break;
case 3: ADMUX = _BV(MUX0) | _BV(MUX1);
break;
case 4: ADMUX = _BV(MUX2);
break;
case 5: ADMUX = _BV(MUX2) | _BV(MUX0);
break;
case 6: ADMUX = _BV(MUX2) | _BV(MUX1);
break;
case 7: ADMUX = _BV(MUX2) | (_BV(MUX1)|_BV(MUX0));
break;
}
ADCSRA |= _BV(ADSC); // Konvertierung Starten
while (ADCSRA & _BV(ADSC)) {}
//warte, bis ADSC gelöscht wird --> Ende der Konvertierung
unten_bit = ADCL;
high_bit = ADCH;
//erg=(high_bit);
erg=((unten_bit+high_bit*256L)*vref)*1000L/1024L;
return erg;
}
id_portc.h
Code:
#include <avr/io.h>
#include <stdlib.h>
#include <avr/signal.h>
#include <avr/interrupt.h>
//#include <usart_bibliothek.h>
#include <ad_read.h>
#include <usart_emp.h>
int get_id (void) {
// Variablen
int id;
//char buffer [10];
//unsigned erg=0;
DDRC=0x00; //Port C = Input
id =PINC-240; // Einlesen der PINs an Port C. Wert korrektur verbesserungswürdig
id=~id-65520;
}
Lesezeichen