Главная страница Случайная страница КАТЕГОРИИ: АвтомобилиАстрономияБиологияГеографияДом и садДругие языкиДругоеИнформатикаИсторияКультураЛитератураЛогикаМатематикаМедицинаМеталлургияМеханикаОбразованиеОхрана трудаПедагогикаПолитикаПравоПсихологияРелигияРиторикаСоциологияСпортСтроительствоТехнологияТуризмФизикаФилософияФинансыХимияЧерчениеЭкологияЭкономикаЭлектроника |
Додаток Б. # #define SENSOR_INPUT1 PIN_RA0 ⇐ ПредыдущаяСтр 3 из 3
#include < 16F873a.h> # #define SENSOR_INPUT1 PIN_RA0 #define SENSOR_INPUT2 PIN_ RA1 #define SENSOR_ INPUT2 PIN_ RA 5 #define OUT_First PIN_RB0 #define OUT_Second PIN_ RB 1 #define OUT_Third PIN_ RB 2 #define OUT_Light1 PIN_RC5 #define OUT_ Light2 PIN_RC4 #define OUT_ Light3 PIN_ RC3 #define DTMF_ROW1 0x0E #define DTMF_ROW2 0x0F #define DTMF_ROW3 0x11 #define DTMF_ROW4 0x13 #define DTMF_COLA 0x18 #define DTMF_COLB 0x1B #define DTMF_COLC 0x1E
#define VERSION " 1.0.0" #define PHONENUM_SIZE 10 #define WAVETABLE_SIZE 200
int16 min_timeout_counter; int8 input_value, numpos, tpos, min_counter, alarm_timeout; int8 index1, index2, inc1, inc2, phone_hold_time; int8 phone_number[PHONENUM_SIZE]; int1 is_dial_number, is_alarm_on, is_light_on, is_beep_on;
CONST unsigned int8 SINE_WAVE[WAVETABLE_SIZE] = { 128, 132, 136, 139, 143, 147, 150, 154, 158, 161, 165, 169, 172, 176, 179, 182, 186, 189, 192, 195, 199, 202, 204, 207, 210, 213, 215, 218, 220, 223, 225, 227, 229, 231, 233, 235, 237, 238, 240, 241, 242, 243, 244, 245, 246, 247, 247, 247, 248, 248, 248, 248, 248, 247, 247, 247, 246, 245, 244, 243, 242, 241, 240, 238, 237, 235, 233, 231, 229, 227, 225, 223, 220, 218, 215, 213, 210, 207, 204, 202, 199, 195, 192, 189, 186, 182, 179, 176, 172, 169, 165, 161, 158, 154, 150, 147, 143, 139, 136, 132, 128, 124, 120, 117, 113, 109, 106, 102, 98, 95, 91, 87, 84, 80, 77, 74, 70, 67, 64, 61, 57, 54, 52, 49, 46, 43, 41, 38, 36, 33, 31, 29, 27, 25, 23, 21, 19, 18, 16, 15, 14, 13, 12, 11, 10, 9, 9, 9, 8, 8, 8, 8, 8, 9, 9, 9, 10, 11, 12, 13, 14, 15, 16, 18, 19, 21, 23, 25, 27, 29, 31, 33, 36, 38, 41, 43, 46, 49, 52, 54, 57, 61, 64, 67, 70, 74, 77, 80, 84, 87, 91, 95, 98, 102, 106, 109, 113, 117, 120, 124};
void setup_mcu_reg() { set_tris_a(0x1); set_tris_b(0x9); set_tris_d(0); set_tris_e(0x7); output_b(0); setup_adc_ports(NO_ANALOGS); }
void save_config() { write_eeprom(0x0, is_dial_number); write_eeprom(0x1, is_alarm_on); write_eeprom(0x2, is_light_on); write_eeprom(0x3, is_beep_on); write_eeprom(0x4, alarm_timeout); write_eeprom(0x5, phone_hold_time); for(tpos = 0x0; tpos < PHONENUM_SIZE; tpos++) write_eeprom((tpos+0x6), phone_number[tpos]); }
void load_config() { is_dial_number = read_eeprom(0x0); is_alarm_on = read_eeprom(0x1); is_light_on = read_eeprom(0x2); is_beep_on = read_eeprom(0x3); alarm_timeout = read_eeprom(0x4); phone_hold_time = read_eeprom(0x5); for(tpos = 0x0; tpos < PHONENUM_SIZE; tpos++) phone_number[tpos] = read_eeprom(tpos+0x6); }
void generate_dtmf_tone(int8 keypad) { index1=0; index2=0; inc1=0; inc2=0; if((keypad == 0x1)||(keypad == 0x2)||(keypad == 0x3)) inc1 = DTMF_ROW1; else if((keypad == 0x4)||(keypad == 0x5)||(keypad == 0x6)) inc1 = DTMF_ROW2; else if((keypad == 0x7)||(keypad== 0x8)||(keypad == 0x9)) inc1 = DTMF_ROW3; else if(keypad == 0) inc1 = DTMF_ROW4;
if((keypad == 0x1)||(keypad == 0x4)||(keypad == 0x7)) inc2 = DTMF_COLA; else if((keypad == 0x2)||(keypad == 0x5)||(keypad == 0x8)||(keypad == 0)) inc2 = DTMF_COLB; else if((keypad == 0x3)||(keypad == 0x6)||(keypad == 0x9)) inc2 = DTMF_COLC;
setup_counters(RTCC_INTERNAL, RTCC_DIV_2); enable_interrupts(INT_RTCC); enable_interrupts(GLOBAL); delay_ms(100); disable_interrupts(INT_RTCC); output_d(0); }
void enable_alarm_light() { if(is_light_on & & input(SENSOR_ENV)) output_high(OUT_LIGHT); }
void beep() { if(is_beep_on) { int8 beep_loop; for(beep_loop = 0; beep_loop < 0x7D; beep_loop++) { output_high(OUT_BEEPER); delay_us(500); output_low(OUT_BEEPER); delay_us(500); } output_low(OUT_BEEPER); } }
void set_master_alarm(int1 state) { if(is_alarm_on) { if(state) output_high(OUT_ALARM); else output_low(OUT_ALARM); } }
void clear_phone_number() { for(tpos = 0; tpos < PHONENUM_SIZE; tpos++) phone_number[tpos]=0xA; }
void dial_phone_number() { if(phone_number[0]! =0xA) { numpos = 0; while((numpos < PHONENUM_SIZE)& & (phone_number[numpos]! =0xA)) { generate_dtmf_tone(phone_number[numpos]); delay_ms(50); numpos++; } } }
void call_to_phone() { if(is_dial_number) { min_counter = 0; output_high(OUT_PHONE_SELECT); delay_ms(500); dial_phone_number(); delay_ms(300); while(min_counter < phone_hold_time) { min_timeout_counter = 0; while(++min_timeout_counter < 0x1E) delay_ms(1000); min_counter++; } output_low(OUT_PHONE_SELECT); } }
void show_phone_number() { if(phone_number[0]==0xA) printf(" < NOT SPECIFIED> "); else { numpos = 0; while((numpos < PHONENUM_SIZE)& & (phone_number[numpos]! =0xA)) { printf(" %d", phone_number[numpos]); numpos++; } } }
void _pause() { printf(" \nPress any key to coutinue..."); getch(); printf(" \n"); }
int1 is_select_yes() { do input_value = getch(); while(! ((input_value == 0x59)||(input_value == 0x79)||(input_value == 0x4E)||(input_value == 0x6E))); return((input_value == 0x59)||(input_value == 0x79)); }
int8 console_get_int() { do input_value = getch(); while(! ((input_value > 0x30) & & (input_value < 0x3A))); return(input_value - 0x30); }
void echo_state(int1 param) { if(param) printf(" Enable"); else printf(" Disable"); }
void do_sys_param_setup() { printf(" \n\nParameter Setup"); printf(" \nDial the phone number [Y/N]: "); is_dial_number = is_select_yes(); printf(" \nActivate Master Alarm [Y/N]: "); is_alarm_on = is_select_yes(); printf(" \nActivate Master Lamp [Y/N]: "); is_light_on = is_select_yes(); printf(" \nEnable the System Beeper [Y/N]: "); is_beep_on = is_select_yes(); if(is_alarm_on) { printf(" \nAlarm Timeout [1-9]min: "); alarm_timeout = console_get_int(); } else alarm_timeout = 0; if(is_dial_number) { printf(" \nPhone Hold Time [1-9]x0.5min: "); phone_hold_time = console_get_int(); } else phone_hold_time = 0; printf(" \n"); }
void show_system_report() { printf(" \n\nSystem Configuration"); printf(" \n- Phone Number: "); show_phone_number(); printf(" \n- Phone number dialing: "); echo_state(is_dial_number); printf(" \n- Master Alarm: "); echo_state(is_alarm_on); printf(" \n- Master Lamp: "); echo_state(is_light_on); printf(" \n- System Beeper: "); echo_state(is_beep_on); printf(" \n- Alarm Timeout: %dmin.", alarm_timeout); printf(" \n- Phone Hold Time: %1.1fmin.", (phone_hold_time*0.5)); printf(" \n\nAutomatic Home Security System. Version %s (%s).", VERSION, __DATE__); }
void do_self_test() { int8 routine_id = 0; int1 sen1 = 0, sen2 = 0; printf(" \n\nSelf Test"); beep(); delay_ms(200); beep(); printf(" \nOperating Mode: "); if(input(SENSOR_ENV)) printf(" Night Mode"); else printf(" Day Mode"); while(routine_id < 0x4) { switch(routine_id) { case 0x0: printf(" \n\nSensor Checking..."); printf(" \nPlease cut the light beam(s)..."); do { if(! sen1) { sen1 = input(SENSOR_INPUT1); if(sen1) printf(" \n - Sensor 1: Detected"); } if(! sen2) { sen2 = input(SENSOR_INPUT2); if(sen2) printf(" \n - Sensor 2: Detected"); } } while(! (sen1 & & sen2)); printf(" \nSensor Check"); break; case 0x1: printf(" \nDialing the number "); show_phone_number(); printf(" \nWaiting for Hold Time: %1.1fmin.", (phone_hold_time*0.5)); call_to_phone(); printf(" \nPhone Dialing"); break; case 0x2: printf(" \nMaster Alarm ("); echo_state(is_alarm_on); printf(" by the user)"); set_master_alarm(true); delay_ms(5000); set_master_alarm(false); break; case 0x3: printf(" \nMaster Lamp ("); echo_state(is_light_on); printf(" by the user)"); enable_alarm_light(); delay_ms(3000); output_low(OUT_LIGHT); break; } routine_id++; printf(": DONE\n"); delay_ms(50); } printf(" \nSystem testing completed"); beep(); _pause(); }
void system_setup_mode() { reinit_menu: printf(" \nAutomatic Home Security System\n\n System Setup Options: "); repaint_menu: printf(" \n\n [1] Setup Phone Number\n [2] Parameter Setup\n"); printf(" [3] Self Test\n [4] Show Configuration\n [5] Exit"); printf(" \n\n Selection: "); input_value = getch(); if((input_value > 0x35) || (input_value < 0x31)) { beep(); goto repaint_menu; } if(input_value == 0x31) { printf(" \n\nSetup Phone Number"); numpos = 0; clear_phone_number(); printf(" \nEnter 10 digit phone number which is ended with the # key: "); while(numpos < PHONENUM_SIZE) { num_error: input_value = getch(); if ((input_value > 0x2F)& & (input_value < 0x3A)) phone_number[numpos] = (input_value - 0x30); else if (input_value == 0x23) break; else { beep(); goto num_error; } numpos++; } printf(" \nNew Phone Number: "); show_phone_number(); _pause(); } else if(input_value == 0x32) do_sys_param_setup(); else if(input_value == 0x34) show_system_report(); else if(input_value == 0x33) do_self_test(); else if(input_value == 0x35) { save_config(); return; } printf(" \n"); goto reinit_menu; }
void set_status_output(int1 run_mode) { output_bit(OUT_STATUS, run_mode); output_bit(OUT_STATUS_INV, (! run_mode)); }
void flash_output() { for(tpos = 0; tpos < 0x4; tpos++) { set_status_output(true); delay_ms(125); set_status_output(false); delay_ms(125); } set_status_output(true); }
#INT_RTCC void wave_generator() { int8 wave = 0; set_rtcc(0x19); wave = ((long)SINE_WAVE[index1]+(long)SINE_WAVE[index2])/0x2; output_d(wave); index1 += inc1; index2 += inc2; if(index1 > = WAVETABLE_SIZE) index1 -= WAVETABLE_SIZE; if(index2 > = WAVETABLE_SIZE) index2 -= WAVETABLE_SIZE; }
void main() { setup_mcu_reg(); output_low(OUT_STATUS); output_low(OUT_STATUS_INV); load_config(); beep(); while(TRUE) { if(input(SW_MODE_SELECT)) { set_status_output(false); system_setup_mode(); } else { printf(" \nWorking\n"); set_status_output(true); while(TRUE) { if(input(SENSOR_INPUT1) || input(SENSOR_INPUT2)) { printf(" \Switch\n"); flash_output(); if(input(SW_NO_CALL)) call_to_phone(); set_master_alarm(true); enable_alarm_light(); min_timeout_counter = 0; min_counter = 0; while(! (input(SW_F1_KEY) || (timeout == 0))) { min_timeout_counter++; delay_ms(100); if(min_timeout_counter > = 0x258) { min_timeout_counter = 0; min_counter++; if(min_counter > = timeout) break; } } set_master_alarm(false); output_low(OUT_Light3); flash_output(); printf(" \Control microclimate indoor \n"); } } } } }
|