Masterprogramm zur zyklischen Abfrage des Klima-Daten-Slave
Dieses Programm kommuniziert mit dem MEGA8-Slave üner eine serielle
Schnittstelle. Die Messwerte werden anschließend auf einem Display angezeigt
(siehe Foto).
Es ist auch möglich die im Slave eingelesen ROM-Konstanten der Sensoren
abzufragen und anzuzeigen. Da es nur einmal erforderlich ist, die ROM-Konstanten
einzulesen, kann man sich diese Konstanten notieren und dann als Konstanten im
Programm-Text einsetzen - so ist es möglich das Programm kürzer zu halten - vor
allem, wenn man einen MEGA8 einsetzt.
{------------------------------------------------------------------------------}
// Master-Program for request and display the Sensor-data
//
// S. Taborek
// 20.12.2010
//
// Developmentsystem: BIGAVR2
//
program sensoren_req;
{------------------------------------------------------------------------------}
// MEGA128
{ Declarations section }
// Lcd module connections
var LCD_RS : sbit at PORTC2_bit;
var LCD_EN : sbit at PORTC3_bit;
var LCD_D4 : sbit at PORTC4_bit;
var LCD_D5 : sbit at PORTC5_bit;
var LCD_D6 : sbit at PORTC6_bit;
var LCD_D7 : sbit at PORTC7_bit;
var LCD_RS_Direction : sbit at DDC2_bit;
var LCD_EN_Direction : sbit at DDC3_bit;
var LCD_D4_Direction : sbit at DDC4_bit;
var LCD_D5_Direction : sbit at DDC5_bit;
var LCD_D6_Direction : sbit at DDC6_bit;
var LCD_D7_Direction : sbit at DDC7_bit;
// End Lcd module connections
var GLCD_DataPort : byte at PORTA;
GLCD_DataPort_Direction : byte at DDRA;
var GLCD_CS1 : sbit at PORTE.B2;
GLCD_CS2 : sbit at PORTE.B3;
GLCD_RS : sbit at PORTE.B4;
GLCD_RW : sbit at PORTE.B5;
GLCD_EN : sbit at PORTE.B6;
GLCD_RST : sbit at PORTE.B7;
var GLCD_CS1_Direction : sbit at DDRE.B2;
GLCD_CS2_Direction : sbit at DDRE.B3;
GLCD_RS_Direction : sbit at DDRE.B4;
GLCD_RW_Direction : sbit at DDRE.B5;
GLCD_EN_Direction : sbit at DDRE.B6;
GLCD_RST_Direction : sbit at DDRE.B7;
// End Glcd module connections------------------
{------------------------------------------------------------------------------}
var f_mess_lo, f_mess_hi : word;
f_sens_lo, f_sens_hi : word;
lcd_str : String[28];
f_mess, f_sens : word;
f_mess_str : String[8];
h_r : real;
t1 : byte;
humidity : real;
pressure : real;
temperatur : real;
h_b, cnt : byte;
rec : boolean;
rec_txt : string[90];
hu_str : string[18];
te_str : string[18];
pr_str : string[18];
glcd_cnt : byte;
var i : byte;
barray : array[1..4] of byte;
Pbyte : ^byte;
{------------------------------------------------------------------------------}
function ReceiveByte(var _b: byte): boolean;
{------------------------------------------------------------------------------}
begin
result := false;
_b := 255;
repeat
inc(cnt);
delay_us(10);
if (UART1_Data_Ready() = 1) then begin
result := true;
_b := UART1_Read();
end;
until (result=true) or (cnt > 250);
if result = false then PortB.4 := 1;
end;
{------------------------------------------------------------------------------}
function ReceiveStr(): boolean;
{------------------------------------------------------------------------------}
var b, l : word;
begin
result := false;
b := 0;
repeat
delay_ms(1);
if (UART1_Data_Ready() = 1) then begin
result := true;
end;
inc(b);
until (result = true)or(b > 400); // wait for send from client
if result = true then begin
UART1_Read_Text(rec_txt, # , 255);
UART1_Write(0xA0);
rec_txt := rec_txt + #0;
end;
end;
{------------------------------------------------------------------------------}
procedure Show_ROM_Constants;
{------------------------------------------------------------------------------}
var b, i, l, zl : byte;
c : char;
s : string[21];
begin
l := length(rec_txt);
zl := 0;
s := ;
for i := 0 to l do begin
c := rec_txt[i];
if (c = / )or(i = l) then begin
Glcd_Write_Text( , 1, zl,2);
Glcd_Write_Text(s+ , 1, zl, 1); // Write string
inc(zl);
s := ;
end
else begin
s := s + c;
end;
end;
inc(glcd_cnt);
ByteToStr(glcd_cnt, s);
Glcd_Write_Text(s, 90, 7, 1); // Write string
end;
{------------------------------------------------------------------------------}
begin
{ Main program ATMEGA128}
UART1_Init(9600);
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
Lcd_Out(1, 3, Taste B7 );
//---------------------------------------------------
Glcd_Init(); // Initialize GLCD
Glcd_Fill(0x00); // Clear GLCD
Glcd_Set_Font(@Character8x7, 8, 7, 32); // Choose font "Character8x7"
Glcd_Write_Text( Start , 1, 0, 2); // Write string
//---------------------------------------------------
DDRB.4 := 0;
DDRB.5 := 0;
DDRB.6 := 0;
DDRB.7 := 0;
//
t1 := 0;
DDRB.0 := 1;
PORTB.0 := 1;
DDRB.1 := 1;
PORTB.1 := 1;
DDRB.2 := 1;
PORTB.2 := 1;
DDRB.3 := 1;
PORTB.3 := 0;
DDRB.4 := 1;
PORTB.4 := 0;
delay_ms(1400);
Lcd_Cmd(_LCD_CLEAR); // Clear display
//
while true do begin
if (t1 > 120) then begin
PortB.0 := 1;
t1 := 0;
while (UART1_Data_Ready() = 1) do begin // Puffer leeren
h_b := UART1_Read();
delay_ms(3);
end;
//
PORTB.2 := 1;
PortB.1 := 0;
//
UART1_Write(0xA1); // request for data 1
if ReceiveStr() then begin //
Show_ROM_Constants;
end;
//
delay_ms(100);
PortB.2 := 0;
PortB.0 := 0;
end{if };
if (PINB.7 = 1) then begin
Glcd_Fill(0x00); // Clear GLCD
UART1_Write(0xA8); // request for data ROM
if ReceiveStr() then begin //
Show_ROM_Constants;
end;
repeat
until (PINB.7 = 0);
Glcd_Fill(0x00); // Clear GLCD
end;
if (PINB.5 = 1) then begin
Glcd_Fill(0x00); // Clear GLCD
UART1_Write(0xA9); // request for data ROM
if ReceiveStr() then begin //
Show_ROM_Constants;
end;
repeat
until (PINB.5 = 0);
Glcd_Fill(0x00); // Clear GLCD
end;
if (PINB.4 = 1) then begin
Glcd_Fill(0x00); // Clear GLCD
UART1_Write(0xAA); // request for data ROM
if ReceiveStr() then begin //
Show_ROM_Constants;
end;
repeat
until (PINB.4 = 0);
Glcd_Fill(0x00); // Clear GLCD
end;
delay_ms(25);
PortB.4 := 0;
inc(t1);
end{loop};
end.