15.Kichik biznes
Xavfsizlik tizimlarini o'rnatish ishlarida Kichik biznesning qanday ishlatilishi quyidagicha:
Yashash joylari va kichik biznes uchun xavfsizlik tizimlari o'rnatilgan.
Ishning amaliy mazmuni
Ushbu uyni qo’riqlash tizimini ishlab chiqishda AVR mikrokontrollerini tanlaymiz.
Dastur algorimi sxemasi
/******************************************************************************************************
REV 1.07
Copyright (C) 2005 Edward Welch welcher@edjusted.com
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, or (at your option) any later version.
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.
Ushbu dastur ATmega32 uchun yozilgan
Bu bir nechta qurilmalarni ATmega8535 uController-ga ulash uchun mo'ljallangan ko'p maqsadli dastur
Interfeys TWI, UART va parallel aloqa avtobuslari yordamida amalga oshiriladi.
Qo'llab-quvvatlanadigan qurilmalar:
DS1624 Temp sensori
DS1307 RTC
HD44780 mos keladigan LCD
O'rnatilgan veb-server sayti
Tuzatish tarixi:
1.00 boshlang'ich versiyasi
1.01 qo'shilgan LCD qo'llab-quvvatlash
1.02 Siteplayer-ga qo'shilgan
1.03 qo'shilgan DS1624 qo'llab-quvvatlash
1.04 DS1307 qo'llab-quvvatladi
1.05 TWI interupt asosida amalga oshirildi
1.06 Interupt Driven 4x3 klaviaturasi amalga oshirildi
1.07 Termostatni boshqarish vositalari amalga oshirildi
LCD displeyi HD44780 bilan mos keladi
16x2 belgilar LCD
******************************************************************************************************/
#include
#include
#include
#include
#include
#include
#include
#include
#include
/*************
Ta'riflar
*************/
//LCD
#define RS 2
#define RW 3
#define E 4
#define DATA PORTA
#define COMM PORTC
//TWI Addresses
#define TEMPADDR 0x49
#define TEMPADDR_O 0x4B
#define RTCADDR 0x68
//Keypad Pins (not currently used)
#define R1 0
#define R2 1
#define R3 2
#define R4 3
#define C1 4
#define C2 5
#define C3 6
#define KEYPAD PORTB
#define KEYDIR DDRB
//Thermostat Control Pin
#define THERMPORT PORTD
#define THERMPIN PD7
/**************************
Funktsiya prototiplarini e'lon qiling
**************************/
void main(void);
void lcd_display(uint8_t);
void init(void);
void func_set(void);
void disp(void);
void clear(void);
void entry(void);
void home(void);
void pause_half_ms(uint16_t);
void output(uint8_t,uint8_t);
void twi_readtemp(void);
void start_conversion(void);
void line2(void);
void print3(uint8_t);
void print2(uint8_t);
void update_time(void);
void twi_write(uint8_t,uint8_t);
void twi_read(uint8_t,uint8_t);
void twi_initRTC(void);
void twi_gettime(void);
void nop(void);
void sendchar(char *);
void sendstring(char *,uint8_t);
void keypad(void);
void check_code(void);
void update_siteplayer(void);
void read_alarm(void);
void thermostat(void);
void load_characters(void);
void udp_send(uint8_t);
void get_thermostat_vals(void);
void input(uint8_t,uint8_t);
void update_thermostat_vals(void);
void update_thermostat_times(void);
void check_hold(void);
void twi_readtemp1(void);
uint8_t format_vals(uint8_t);
void update_thermostat_temps(void);
void move(uint8_t,uint8_t);
void cgaddress(uint8_t);
/***************
Global o'zgaruvchilar
***************/
uint8_t data,data1,slave_address,data_direction,bytes,twi_data[15],data_pointer,retry,status,trip,hours,minutes,temp_flag,alarm_status,txt_status,door_status,status_old;
uint8_t thermo_temps[7],thermo_times[12],current_temp,update_flag,temp_up_flag,o_temp,o_temp1,below_zero,heat_on,time_period,hold;
//RTC Pulse 1Hz
SIGNAL(SIG_INTERRUPT0)
{
trip = 1;
}
//Klavitura uchun ishlatiladi
SIGNAL(SIG_INTERRUPT1)
{
trip = 3;
//keypad();
}
SIGNAL(SIG_2WIRE_SERIAL)
{
switch (TW_STATUS)
{
case TW_START: //Boshlash yoki qaytadaan boshlash
case TW_REP_START:
{
if(retry > 2) // Agar 3 ta urinishdan keyin SLA + R / W NACK bekor qilingan bo'lsa
{
TWCR |= (1<
status = 0; //TWI ni sozlash
home();
pause_half_ms(5);
sendchar("A"); //Xatolik holati
//pause_half_ms(5000);
return;
//break;
}
TWDR = slave_address | data_direction; // SLA+R/W ni o’qish
TWCR &= ~(1<
break;
}
case TW_MT_SLA_NACK: // SLA + R / W NACK qabul qilindi, 3 marta takrorlang
case TW_MR_SLA_NACK:
{
retry++;
TWCR |= (1<
//char("B") ni yubor;
pause_half_ms(200);
break;
}
case TW_MT_SLA_ACK: //SLA+W ACK received
{
retry = 0;
TWDR = twi_data[data_pointer]; // Birinchi ma'lumotlar baytini yuklash
data_pointer++; //Ko’rsatkichni oshirish
TWCR |= (1<
//sendchar("C");
break;
}
case TW_MT_DATA_ACK: // ACK ma'lumotlar bayt yuborilganidan keyin qabul qilindi
{
retry = 0;
if (--bytes > 0) // Umumiy bayt ko'rsatkichidan 1-ni olib tashlang
{
TWDR = twi_data[data_pointer]; // Agar 1 bayt yoki undan ko'p bo'lsa, bayt yuklanadi
data_pointer++; //Ko’rsatkichni oshirish
TWCR |= (1<
//sendchar("D");
}
else
{
status = 0; // Agar baytlar yuborilmasa, TWI-ning band bo'lgan aniq bayrog'ini tozalash
TWCR |= (1<
//sendchar("E");
}
break;
}
case TW_MT_DATA_NACK: // Ma'lumot bayt yuborilganidan keyin NACK qabul qilindi
{
TWCR |= (1<
status = 0; // TWI band flagni Tozalash
//sendchar("F");
break;
}
case TW_MR_SLA_ACK: //ACK received after data byte received
{
//twi_data[data_pointer] = TWDR;
//data_pointer++;
if (--bytes > 0) //Subtract 1 from total bytes pointer
//if (bytes > 1)
{
TWCR |= (1<
//sendchar("1");
}
else
{
TWCR |= (1<
//sendchar("2");
}
break;
}
case TW_MR_DATA_ACK: //ACK received after data byte
{
twi_data[data_pointer] = TWDR; //Load data from TWDR
data_pointer++; //Increment pointer
if (--bytes > 0) //Subtract 1 from total bytes pointer
{
TWCR |= (1<
//sendchar("3");
}
else
{
TWCR &= ~(1<
//sendchar("4");
}
break;
}
case TW_MR_DATA_NACK: //NACK received after data byte
{
twi_data[data_pointer] = TWDR; //Load data from TWDR
TWCR |= (1<
status = 0; //Clear TWI busy flag
//sendchar("5");
break;
}
case TW_MR_ARB_LOST:
{
TWCR |= (1<
sendchar("X");
pause_half_ms(1000);
status = 0;
break;
}
case TW_NO_INFO:
{
TWCR |= (1<
status = 0;
sendchar("I");
pause_half_ms(1000);
break;
}
case TW_BUS_ERROR:
{
TWCR |= (1<
status = 0;
sendchar("E");
wdt_reset();
cli();
for(;;);
//pause_half_ms(5000);
break;
}
}
}
//UART Interupt (Not Utilized currently, more for Debug)
SIGNAL(SIG_UART_RECV)
{
uint8_t input;
input = UDR;
//clear LCD if input = 2
if (input==33)
{
//trip = 5;
//update_time();
}
else if (input==35)
{
//trip = 3;
//update_thermostat_temps();
}
else if (input==3)
{
//update_thermostat_times();
//trip = 4;
}
else
//line2();
//clear();
lcd_display(input);
}
SIGNAL(__vector_default)
{
// catch all for interupts
}
void main (void)
{
OSCCAL = 0xBF; //Calabration Byte, Change if necessary for your uC.
//Pause Function Timer Setup
TCCR0 = 0x02; // prescale timer0 ck/8
TCNT0 = 0x00; // start value of T/C0
/*********
TWI Setup
*********/
//TWSR = 0x00;
//TWBR = 40;
//status = 0;
//TWCR = (1<
//TWCR = (1<
//pause_half_ms(100);
//PORT Setup
DDRA = 0xFF; // PORTA OUTPUT
DDRC = 0x9F; // 10011111 PORTC Output
PORTC = 0xE0; // 11100000
PORTC = 0xE1; // 11100001
PORTC = 0xE3; // 11100011
pause_half_ms(5);
//pause_half_ms(1);
//PORTC = 0xE1;
//pause_half_ms(1);
//PORTC = 0xE3;
//DDRC = 0x9C;
DDRD = 0x80;
//PORTD = 0x00;
//External Interupt Setup
//MCUCR = (1<
MCUCR = (1<
//GICR = (1<
GICR = (1<
//Initialize UART
UCSRB = (1<
UCSRC = (1<
UBRRL = 12; // init transfer speed for 8 MHz 38.4kbs
//Enable Watch Dog Timer (To correct any coding bugs)
wdt_enable(WDTO_2S);
sei();
//Initialize LCD
init();
load_characters();
home();
pause_half_ms(5);
//print3(1);
//pause_half_ms(1500);
/*********
TWI Setup
*********/
TWSR = 0x00;
TWBR = 40;
status = 0;
TWCR = (1<
TWCR = (1<
pause_half_ms(100);
wdt_reset();
//home();
//pause_half_ms(5);
//print3(12);
//pause_half_ms(1000);
start_conversion();
//home();
//pause_half_ms(5);
//print3(13);
//pause_half_ms(1000);
twi_initRTC();
//home();
//pause_half_ms(5);
//print3(14);
//pause_half_ms(1500);
pause_half_ms(10);
//Init Keypad
//print3(2);
//KEYDIR = (1<
//KEYPAD = (1<
uint8_t j,read_flag;
/*******************
Initialize Variables
*******************/
status_old = 0;
alarm_status = 1;
txt_status = 1;
temp_up_flag = 1;
/*****************
Init Alarm and SP
*****************/
twi_data[1] = alarm_status;
twi_data[2] = txt_status;
twi_data[3] = 0;
output(0x1C,3);
//Siteplayer Enable UDP
uint8_t del;
outp(0x90,UDR); //send writex command to siteplayer
do //wait for UDR to clear
del = inp(UCSRA) & 0x20;
while (del != 0x20);
outp(0x20,UDR); //send address to siteplayer LB
do //wait for UDR to clear
del = inp(UCSRA) & 0x20;
while (del != 0x20);
outp(0xFF,UDR); //send address to siteplayer HB
do //wait for UDR to clear
del = inp(UCSRA) & 0x20;
while (del != 0x20);
outp(0x01,UDR); //Set UDPRCVR
do //wait for UDR to clear
del = inp(UCSRA) & 0x20;
while (del != 0x20);
udp_send(154);
//Setup Thermostat on Powerup
get_thermostat_vals();
for (j=1;j<=6;j++)
{
twi_data[j] = thermo_temps[j];
}
output(0x0A,6);
for (j=1;j<=12;j++)
{
twi_data[j] = thermo_times[j];
}
output(0x10,12);
twi_data[1] = hold;
output(0x26,1);
clear();
for(;;)
{
wdt_reset();
check_code(); //Check keypad uC to see if a code has been entered
pause_half_ms(5);
read_alarm(); //Check SP for alarm settings
if (status_old != door_status)
{
//Left Garage Door
if ((status_old & 0x01) != (door_status & 0x01))
{
if ((door_status & 0x01) == 0x01)
{
if(txt_status ==1) udp_send(10); //Left Garage Open
pause_half_ms(10);
udp_send(100);
}
else
{
if(txt_status ==1) udp_send(11); //Left Garage Closed
pause_half_ms(10);
udp_send(101);
}
}
//Right Garage Door
if ((status_old & 0x02) != (door_status & 0x02))
{
if ((door_status & 0x02) == 0x02)
{
if(txt_status ==1) udp_send(12); //Right Garage Open
pause_half_ms(10);
udp_send(102);
}
else
{
if(txt_status ==1) udp_send(13); //Right Garage Closed
pause_half_ms(10);
udp_send(103);
}
}
//Front Door
if ((status_old & 0x04) != (door_status & 0x04))
{
if ((door_status & 0x04) == 0x04)
{
if(txt_status ==1) udp_send(14); //Front Door Open
pause_half_ms(10);
udp_send(104);
}
else
{
if(txt_status ==1) udp_send(15); //Front Door Closed
pause_half_ms(10);
udp_send(105);
}
}
Do'stlaringiz bilan baham: |