; ============================================================================== ; Title: Wirless Bell Transmitter ; ; Author: Rob Jansen, Copyright (c) 2018..2018, all rights reserved. ; ; Revision: ; 2018-05-17 : Initial version. ; 2018-05-19 : Tested version. ; 2018-05-20 : Version 2. Added sequence to RF transmission for reliability. ; ; Compiler: 2.4q6 ; ; Description: Transmitter for the wireless door bell. When the bell switch ; is pressed it will activate the door bell but it will also send ; a message to the wireless door bell via an 433 MHz RF connection. ; ; Sources: ; ; ================================ Pragmas ==================================== include 12f617 ; target PICmicro ; Use internal clock and internal reset. Pragma target OSC INTOSC_NOCLKOUT ; Internal Clock Pragma target PWRTE Enabled ; Power up timer Pragma target MCLR Internal ; Reset internal Pragma target WDT Disabled ; No watchdog Pragma target IOSCFS F8MHZ ; Set internal oscillator to 8 MHz Pragma target clock 8_000_000 ; oscillator frequency 8 MHz ; ================================ Pins ======================================= enable_digital_io() ; make all pins digital I/O ; Transmitter pin for virtual wire. alias vw_tx_pin is pin_GP0 ; Pin 7. Data transmission pin. pin_GP0_direction = output alias door_bell is pin_GP1 ; Pin 6. Active high. pin_GP1_direction = output alias led is pin_GP2 ; Pin 4. Active high. pin_GP2_direction = output alias enable_switch is pin_GP3 ; Pin 4. Enabled when high. Pin_GP3_Direction = input alias door_bell_switch is Pin_GP4 ; Pin 3. Low when switch is pressed. Pin_GP4_Direction = input ; Enable weak pull up for all ports since some inputs are not connected. WPU = 0b0011_0111 ; Weak pull-up enabled for all port pins. OPTION_REG_NGPPU = FALSE ; Enable Weak Pull-Up ; ================== Constant and variable declarations ======================= ; Virtual Wire constants. const byte VW_MESSAGE_BUFFER = 3 const byte VW_ADDRESS = 1 const byte VW_COMMAND = 1 ; Sequence and repeat constants. const byte MIN_SEQUENCE = 1 const byte MAX_SEQUENCE = 100 const byte NR_OF_REPEATS = 3 ; Timer constants. const byte LED_TIME = 100 ; One second on time for the LED. const byte BELL_TIME = 50 ; Half a second activating the door bell. const byte REPEAT_TIME = 15 ; 150 ms const bit BELL_ON = TRUE const bit BELL_OFF = FALSE const bit LED_ON = TRUE const bit LED_OFF = FALSE var byte tx_buf[VW_MESSAGE_BUFFER] ; Holds the bytes to transmit. var byte led_timer var byte bell_timer var byte message_repeat var byte repeat_timer var byte sequence_number var bit switch_released ; ========================= Functions and Procedures ========================== ; None. ; ========================= Main program starts here ========================== include virtual_wire_transmitter ; Initialize the virtual wire connection and some global variables. vw_setup(VW_SPEED_1000) ; 1000 bits/second. door_bell = BELL_OFF led = LED_OFF led_timer = 0 bell_timer = 0 sequence_number = MIN_SEQUENCE message_repeat = 0 repeat_timer = 0 switch_released = TRUE forever loop ; Loop every 10 ms. _usec_delay(10_000) ; Check if somebody is ringing the bell. if !door_bell_switch then _usec_delay(50_000) ; Debounce time. ; We only do this once, switch must be released to activate again. if switch_released then switch_released = FALSE if !door_bell_switch then ; OK, somebody really pressed the switch. bell_timer = BELL_TIME ; This will activate the bell for a certain time. message_repeat = NR_OF_REPEATS ; This will start the RF transmission. end if ; !door_bell_switch end if ; switch_released end if ; !door_bell_switch ; Check for the switch being released. if door_bell_switch then ; No longer pressed. switch_released = TRUE end if ; door_bell_switch ; Check if we need to send a message but only if enabled and only if we are ; not in the pause of the repeat timer. if (message_repeat != 0) & (repeat_timer == 0) & enable_switch then ; We can only send a message if we are not already sending one. if !vw_tx_active then ; Transmitter ready, send data. tx_buf[0] = VW_ADDRESS tx_buf[1] = VW_COMMAND tx_buf[2] = sequence_number if vw_send(tx_buf, count(tx_buf)) then ; Transmission succesfully started. One less to go. repeat_timer = REPEAT_TIME ; Set the time between messages. message_repeat = message_repeat - 1 ; If we are done repeating then we need to adjust the sequence number. if (message_repeat == 0) then if (sequence_number == MAX_SEQUENCE) then sequence_number = MIN_SEQUENCE else sequence_number = sequence_number + 1 end if end if ; Set the timer for the transmission LED. led_timer = LED_TIME end if ; vw_send end if ; !vw_tx_active end if ; send_message ; Check if we need to activate the LED. if (led_timer == 0) then led = LED_OFF else led = LED_ON led_timer = led_Timer - 1 end if ; Check if we need to activate the bell. if (bell_timer == 0) then door_bell = BELL_OFF else door_bell = BELL_ON bell_timer = bell_Timer - 1 end if ; Adjust repeat time of no tranmission and if not yet done. if !vw_tx_active & (repeat_timer != 0) then repeat_timer = repeat_Timer - 1 end if end loop ; Forever