Nowadays, most people have a car for their daily commute, thanks to the ease of financing and various payment options. With the explosive growth of sales in the automobile sector, traffic management and parking are now two major problems, especially in metropolitan cities. Leaving aside the traffic problem, parking has become a major problem, whether in offices or at home. It is not always possible to find a parking space and you may need to park your car anywhere available. This led to another car theft problem. The car remains safe in a parking lot, but when parked in a distant location, it is vulnerable to being damaged or stolen by malicious elements. The central locking system comes, but it only protects the precious vehicle against theft and not against intentional damage.
In this project, an Anti-Theft and Car Security System based on ATMega8 was designed that can be installed together with Central Locking Systems. The embedded system developed in this project provides the user with a remote monitoring device for the car that by default works like a digital clock and is capable of alerting the user if any malicious element tries to damage the car, tries to break into doors or is otherwise capable of illegally starting the car yourself.
The system involves two on-board devices, one that must be installed in the car and the other a digital clock that is at the same time a car safety alert system. Both devices are built on the ATMega8 microcontroller. The installable system was designed through the interface of the GY-61 Accelerometer module, which can detect any bumps in the car, and the HC-12 Bluetooth module, which enables the communication of alert messages to the remote digital clock and the car's security system . The remote monitor system is built by interfacing between RTC-DS1307 and 16×2 character LCD to display alert messages otherwise current date and time. The monitoring device also communicates with the installed embedded system via the HC-12 Bluetooth module.
Figure 1: Car-embedded device prototype for car theft and protection
Both devices are designed on AVR ATMega8 microcontrollers and are programmed using embedded C. The code was written, compiled and recorded on the respective systems using AVR Studio.
Fig. 2: Car status remote monitoring prototype and real-time digital clock device
Required component –
Block diagram –
Figure 3: Block diagram of car anti-theft and security system based on AVR ATmega8
Prerequisite skills –
Before attempting this project, the developer must have the following prerequisites:
1) The Developer must have basic knowledge of the AVR microcontroller.
2) The developer must be familiar with programming in AVR Studio 4.
3) The developer must know how to interface the 16×2 LCD display with the AVR.
4) The developer must know how to interface the RTC with the AVR.
Circuit Connections –
This system involves two embedded devices – one, in-car installation device and the second, remote monitoring device. The car-mounted device is equipped with sensors to detect any car breakdown incident and continuously updates real-time alert messages to the remote monitoring device every 500 milliseconds via Bluetooth. The remote monitoring device is designed as a digital clock that continuously reminds the car installation device to update the status and if there is any alert, it flashes the alert on its screen.
Fig. 4: Image of the car anti-theft system on a breadboard
The in-car installation device is designed by making the following circuit connections –
1) AVR ATmega8 – Atmega8 is an 8-bit AVR microcontroller with 23 I/O pins. It interfaces with the GY-61 accelerometer sensor and the HC-12 Bluetooth module in the circuit of this project.
2) GY-61 Accelerometer Module (ADXL335) – GY-61 is a 3-axis accelerometer module. It interfaces with the controller to detect any vibrations in the car. The module comes with 5 terminals – VCC, GND, The X pin of the GY-61 is connected to the 23rd pin of the ATmega8, which is the ADC channel 0 of the microcontroller. The Y pin of the GY-61 is connected to the 24th pin of the ATmega8, which is the ADC channel 1 of the microcontroller, and the Z pin of the GY-61 is connected to the 25th pin of the ATmega8, which is the ADC channel 2 of the microcontroller.
3) HC-12 Module – HC-12 is a wireless Bluetooth serial port communication module. It is used to transmit the data from the in-car installation device to the remote monitoring device. The module has 5 terminals – VCC, GND, Rx, Tx and SET. The HC-12's VCC pin is connected to 5V DC and the HC-12's GND pin is connected to common ground. The TX pin of the HC-12 is connected to the 2nd pin of the ATmega8, the RX pin of the HC-12 is connected to the 3rd pin of the ATmega8, and the SET pin of the HC-12 module is connected to the 4th pin of the ATmega8.
4) Optional connections – In addition to these connections, the car installation device can be connected to use three more ADC channels of the ATMega8 microcontroller. Already three ADC channels are in use to read data from the GY-61 accelerometer sensor. An ADC channel can be used to record changes in voltage due to the car automatically starting. Two ADC channels can optionally be used to connect the ignition key switch/relay and the door lock system.
5) Power source – The car installation device can be powered by the car battery. The battery voltage will need to be regulated to 5V DC using a voltage regulator IC such as the 7805.
The remote monitoring device is designed by making the following circuit connections –
1) AVR ATmega8 – The same AVR ATMega8 microcontroller is used in the remote monitoring device. In the remote monitoring device, the controller interfaces with 16×2 character LCD, HC-12 Bluetooth module and RTC DS1307 in the circuit of this project.
2) 16×2 LCD Display – A character LCD interfaces with the controller to display the current date and time and vehicle status. The VSS/GND and LED pins of the LCD module are connected to ground and the VCC and LED+ pins of the LCD are connected to 5V DC. The VEE pin of the LCD is connected to a 5k trimpot. The EN pin of the LCD is connected to pin 16 of the ATmega8 and the RS pin is connected to pin 15 of the ATmega8. The LCD's RW pin is connected to ground. The LCD interfaces in 4-bit mode. Thus, data pins D4, D5, D6 and D7 are connected to pins 26, 25, 24 and 23 of the ATmega8 respectively.
3) RTC DS1307 – RTC DS1307 is a real-time clock that interfaces with the AVR ATmega8 to show the current time and date by default on the display. The 8th pin of DS1307 is connected to 5V DC and the 4th pin is connected to ground. The first and second pins are connected to the external crystal oscillator. The 3rd pin of the DS1307 is connected to the positive terminal of a 3V battery and the 4th pin is connected to the negative terminal of a 3V battery. The 7th pin of DS1307 is connected to the negative terminal of an LED, while the positive terminal of the same LED is connected to 5V DC via a pull-up resistor. The 5th and 6th pins of the DS1307 are connected to the 27th and 28th pins of the ATmega8 microcontroller.
4) Transistor BC548 – BC548 is an NPN transistor which is used to trigger the buzzer on the signal from the AVR microcontroller. The base terminal of the transistor is connected to the 14th pin of the ATmega8. The collector terminal is connected to the buzzer and the sender terminal is grounded.
5) HC-12 Module – The same wireless Bluetooth serial port communication module is used in the remote monitoring device to communicate alert messages with the in-car installation device. The HC-12's VCC pin is connected to 5V DC and the HC-12's GND pin is connected to ground. The TX pin of the HC-12 is connected to the 2nd pin of the ATmega8, the RX pin of the HC-12 is connected to the 3rd pin of the ATmega8 and the SET pin of the HC-12 module is connected to the 4th pin of the ATmega8 microcontroller.
How the circuit works –
The operation of this system can be understood by what both embedded wireless communication devices do. The function and working of both the devices have been described below –
Car Installation Device – When the car device controller is powered by a 5VDC source, the red and green LEDs start to glow for a while and stop. Finally, only the red LED glows for a while, which is an indication that the device is checking the HC-12 module and reading analog voltages from the accelerometer module on the ADC pins (ADC0 to ADC5). After checking that the Bluetooth module is working well and that the analog data from the accelerometer sensor has been received correctly, the red LED stops glowing. The green LED blinks continuously, which indicates that the microcontroller is checking the status of all ADC channels every 500 milliseconds. If the red LED lights up continuously, it indicates that there is a communication problem with the HC-12 module. If the red LED starts blinking, this indicates that one or more ADC channels are reading changes in voltage due to vibration or fluctuations in the power supply. Once the status request is received from the remote monitoring device, the red LED stops. Thus, the Red LED is used as an Error indicator and the Green LED is used as a flasher.
Remote Monitoring Device – The remote monitoring device, by default, acts as a real-time digital clock while flashing any alert message and starting the buzzer when it receives the appropriate message from the car device. When the controller on the remote monitoring device is powered by a 5V DC source, initially all the LEDs connected to it glow for a while and the buzzer generates a small beep. After that, the LCD displays a welcome message and verifies the HC-12 module communication and connection with the DS1307 RTC module. If there is an error in communication via the HC-12 or DS1307 module, an error message will be displayed on the LCD display. If all connections are OK, it will start displaying the current time and date on the LCD. The device keeps sending “?” character to the car device as a status request every 500 milliseconds and waits for the response. If the response from the car device contains any alert message, the buzzer will receive a PWM signal to beep and the LCD display will flash the alert message. To stop the beeping, the user needs to press the S1 switch, which sets the microcontroller for the next monitoring cycle. If the RTC DS1307 is not connected, it must be shorted via Jumper J1, which bypasses the RTC and displays a formal “Have a nice day” message on the LCD.
The time and date on the remote monitoring device can be updated using the S2 and S3 buttons. When the S2 button is pressed, the device enters edit mode. The S3 button can be used to change the value of the selected parameter, after which the S2 button can be used to select the parameter that needs to be changed. Once the time and date are updated, the user must leave the device for 5 seconds. After 5 seconds, the time and date are saved in the DS1307 RTC and the current date/time is displayed on the LCD.
The in-vehicle device may communicate the following alert messages to the remote monitoring device –
1) “NO SIGNAL. CHECK VEHICLE AND BOARDING” – This message means that the vehicle is out of signal range (theft) or that the car device is not responding due to a lack of power. Therefore, the user needs to immediately check the vehicle and battery power.
2) “JERKS/VIBRATIONS/MOTOR/KEY USED” – This message means that the ADC inputs of the car device have observed some changes in their signal voltages, whether from GY-61 or engine starting or door open, etc.
3) “WEAK SIGNAL OR LONG DISTANCE” – This message means that the coded signal was received by the car device, but has incorrect values. Therefore, the user needs to park the vehicle a little closer to get rid of bad signals and communicate status messages properly.
4) “DATE and TIME” or display “HAVE A NICE DAY” – These messages mean that no errors were observed and that the vehicle is in a safe condition.
Fig. 5: Date and time image displayed on the car anti-theft system
Fig. 6: Alert message displayed on the car's anti-theft system
Precautions –
Installing the device in the car essentially involves the following precautions –
1) The device installed in the vehicle must be installed in a location that remains protected against engine overheating, water spillage, etc. .
2) The user must have the help of a specialized mechanic to connect Signal Inputs 1 and 2, which are optional sensor connections for the open door indicator and ignition key indicator. These connections can only be made by an experienced mechanic.
Programming guide –
Both circuits are based on the AVR ATmega8. The AVR is programmed in embedded C using AVR Studio 4 or can be programmed through other programming tools like Atmel Studio or Notepad++ which can also be used to write and compile the code. First of all, the user needs to add the required library files attached below in a zip file and then copy and paste the code into AVR Studio 4 to generate the hex file. In the automotive device, the ATmega8 is used to detect vibration via GY-61 and the data detected in the ADC is sent to the remote monitoring device via the HC-12 Bluetooth module. The remote monitoring device by default displays the current date and time, otherwise it displays any warning message from the in-car installation on a character LCD.
The AVR code of the car device has the following division –
Constants used in the code: –
#define F_CPU 1000000UL :- Constant used to define the clock frequency of the MCU
#define FCPU_BY_100 F_CPU/100 :-Frequency divided by 100 to calculate the transmission rate
#define ADCAVGDIFF 100: – used to determine ADC value change
#define ADCEXTREMEDIFF 50: – used to determine ADC value change
#define BLINKER_LED 0b00100000 :- Blinking LED connected to pin 5 of Port D
#define ERROR_LED 0b00100000 :- Error LED connected to pin 5 of Port B
#define HC12_SET 0b00000100 :-The SET pin of the HC-12 is connected to pin 2 of port D
#define ON 1: – To turn on the LED
#define OFF 0: -To turn off the LED
#define TOGGLE 2: -To blink the LED
#define USART_MAX_RECV_BUF_SIZE 10: – Maximum size of received data
Variable used in code: –
uint8_t isHC12ERROR = 0: – check error in HC-12 communication
uint8_t USART_RECD_DATA_POS=0: – the position of the buffer array
uint8_t isUSARTQUERY=0: – if RX requested for query
uint8_t USART_RXD = 0: – To store received data
int ADCHIVALUE(6)={0 , 0 , 0 , 0 , 0 , 0} : – To set the default value of the ADC
int ADCLOVALUE(6)={1024,1024,1024,1024,1024,1024}: – To set the default ADC value
int ADCVALUE (6): – To store ADC values
int ADCAVGVALUE(6): – the average value of the ADC
uint8_t ADC_ERROR_CODE = 0: -Used to send code to RX if there is a change in ADC value
Header file and libraries used in the code: –
#include
: – Standard AVR header for input/output
#include :-Standard AVR header to provide time delays
#include
:-Standard AVR header for interrupts
Function used in code: –
initPorts: - initializes the port as input and output
initAdc: -ADC initialized
readAdc: -Reads the value of the channels
USART_init: - initializes UART communication
HC12_sendChar: – sends character to HC-12
HC12_sendString: -sends string to HC-12
HC12_sendCommand: - sends commands to HC-12
HC12_start: – check HC-12 communication
setBlinker: -controls the blinker LED
setError: -controls the error LED
sendStatus: - sends the status of ADC channels
readAdcValues: -Reads the ADC value of all channels
setAdcValues: -set default ADC values
checkAdcValues: - checks if there is a change in the ADC value
I know :- enable interrupts
Algorithm: –
1) Initially, in the main program, the port is initialized for input and output, then the flashing LED and the error LED light up for a while and both are turned off. After this initialized UART to start communication.
_delay_ms(100);
initialports;
_delay_ms(10);
setBlinker(ON);
setError(ON);
_delay_ms(500);
setBlinker(OFF);
setError(OFF);
USART_init ;
_delay_ms(100);
2) The ADC is initialized and the error LED is lit, then a function is used to set the ADC default values. After that, the error LED turns off. The “know” function is used to enable interrupts. After that, communication via HC-12 is verified and started.
initAdc;
_delay_ms(100);
setError(ON);
setAdcValues ;
_delay_ms(200);
setError(OFF);
know ;
HC12_start;
3) The program enters a while loop, where the loop variable is used to blink the Error LED when there is an error in the ADC values. In the first 10 cycles it checks for errors and matches for different conditions.
if (loop>10)
{
setBlinker(TOGGLE);
if (isHC12ERROR)
setError(ON);
else if (ADC_ERROR_CODE)
setError(TOGGLE);
other
setError(OFF);
cycle=0;
}
4) The “readAdcValues” function is used to read the ADC values of all channels and the “checkAdcValues” function is used to check the values for errors.
readAdcValues ;
checkAdcValues;
5) If the remote monitoring device sends a status request, it will be sent via the following code snippet.
if (isUSARTQUERY==1)
{
sendStatus ;
isUSARTQUERY = 0;
ADC_ERROR_CODE=0;
}
The AVR code of the remote monitoring device has the following division –
Constants used in the code: –
#define F_CPU 1000000UL :- Constant used to define the MCU clock frequency
#define FCPU_BY_100 F_CPU/100 :- Frequency divided by 100 to calculate the transmission rate
#define ON 1: – To turn on the LED
#define OFF 0: – To turn off the LED
#define TOGGLE 2: – To blink the LED
#define ERROR 1: – used when there is an error
#define BUTTONS_PORT PIND: – button port defined as D port
#define BUTTON_UP 0b10000000 :- S3 is connected to pin 7 of port D
#define BUTTON_NEXT 0b01000000 :- S2 is connected to pin 6 of port D
#define BUTTON_MUTE 0b00100000 :- S1 is connected to pin 5 of port D
#define BUTTON_SETTING 0b01000000 :- S2 is connected to pin 6 of port D
#define RTC_BIT 0b00010000 :- jumper J1 connected to the 4th pin of Port D
#define HC12_SET 0b00000100 :-The set pin of HC-12 is connected to pin 2 of PORT D
#define BLINKER_LED 0b00100000 :- Blinking LED connected to pin 5 of PORT B
#define BUZZER_BIT 0b00000001 :-buzzer is connected to pin 0 of port B
#define USART_MAX_RECV_BUF_SIZE 40: – the maximum size of the received buffer value
Variables used in the code: –
char USART_RECV_BUF(USART_MAX_RECV_BUF_SIZE+1): – used to store received buffer
char TEMP_STR(20): – to store the time value
uint8_t CURRENT_PROCESS = 0: – Process clock
uint8_t HC12error = 0: – used to check for error on HC-12
uint8_t RTCerror = 0: - used to check error in RTC
uint8_t isUSART_DATA_READY=0: -checks data received from Tx
uint8_t USART_RECD_DATA_POS=0: – the position of the buffer array
uint8_t USART_RXD = 0: – To store received data
uint8_t errorloop = 0: -control buzzer indication
Header file and libraries used in the code: –
#include
: – Standard AVR header for input/output
#include :-Standard AVR header to provide time delays
#include
:-Standard AVR header for interrupts
#include :-used to perform string operation
#include “FHN_LCD4_LIB.C”: – LCD Library
#include “FHN_TWI_LIB.C”: -library for two-wire interface
#include “fhn_rtc_ds1307_lib.C”: -RTC Library
Functions used in the code: –
initPorts: - initializes the port as input and output
initAdc: -ADC initialized
readAdc: -Reads the value of the channels
USART_init: - initializes UART communication
HC12_sendChar: – sends character to HC-12
HC12_sendString: - sends string to HC-12
HC12_sendCommand: - sends commands to HC-12
HC12_start: – check HC-12 communication
setBlinker: -controls the blinker LED
setBuzzer: – controls the buzzer
showDateTime: - shows the time on the LCD
sendMessage: - sends message via HC-12
editDateTime: -edit time for RTC
clearUsartRx: – Clears data received on the UART
I know :- enable interrupts
Algorithm: –
1) Initially, the “errorloop” variable is used to turn off the buzzer when there is an error. The port is initialized for input and output. After that, the blinker LED lights up for a while and goes out. After that, the buzzer makes a short beep.
error loop uint8_t = 0;
_delay_ms(100);
initPorts;
_delay_ms(500);
set Flasher (ON);
_delay_ms(500);
set Flasher (OFF);
setBuzzer(ON);
_delay_ms(200);
setBuzzer(OFF);
2) The LCD is initialized and then cleared, which clears any useless values. At first, the LCD shows the message “Parked vehicle monitoring –fnn/..” which indicates that the LCD is working properly. Then the blinker LED turns on for a while and then turns off.
LCD_start ( );
_delay_ms(100);
LCD_clrscr;
LCD_writeString(“VEHICLE PARKED”);
LCD_gotoRowCol(2, 1);
LCD_writeString(“MONITOR -fhn/..”);
set Flasher (ON);
_delay_ms(500);
set Flasher (OFF);
_delay_ms(100);
3) The “know” function is used to enable interrupts. Then, the UART is initialized for Bluetooth communication. Again the blinker LED stays on for a while and then goes off. Communication via HC-12 is started and it is checked whether there are any alert messages via HC-12 communication. Again, the blinker LED stays on for a while and then turns off. The LCD is clean.
know ;
USART_init ;
set Flasher (ON);
_delay_ms(500);
set Flasher (OFF);
_delay_ms(500);
Error HC12 = HC12_start ( );
set Flasher (ON);
_delay_ms(500);
set Flasher (OFF);
_delay_ms(500);
//RTC_Start;
_delay_ms(100);
LCD_clrscr;
4) The code checks if Jumper J1 is connected and then bypasses the RTC. If J1 is not connected, it reads the RTC value. If there is an error reading the RTC data, the message “NOT connected” will be displayed. Then the LCD is turned off.
if ((PIND & RTC_BIT) == 0)
{
LCD_writeString(“BY-PASS CHECK”);
LCD_gotoRowCol(2, 1);
LCD_writeString(”RTC:DS1307″);
RTCerror = 5;
}
other
{
LCD_writeString(“Check RTC:DS1307”);
LCD_gotoRowCol(2, 1);
RTCerror=RTC_getTime ;
strcpy (TEMP_STR, TIMESTR);
if (RTCerror) //error
LCD_writeString(“ERROR”);
else if (strcmp ( TIMESTR, “HH:MM:SS”)==0)
{
LCD_writeString(“NOT CONNECTED”);
RTCerror = 2;
}
}
_delay_ms(1500);
LCD_clrscr;
5) The program enters a while loop. First, it sets the LED to blink every cycle. It clears the UART. If switch S1 is pressed, the buzzer will be turned off. If switch S2 is pressed, it will enter time edit mode. It sends a status request to the car device via Bluetooth.
setBlinker(TOGGLE);
clearUsartRx ;
if ((BUTTONS_PORT & BUTTON_MUTE) == 0)
{
error loop=0;
}
if ((BUTTONS_PORT & BUTTON_SETTING) == 0)
{
editDateTime ;
}
send Message ( "?" );
6) If jumper J1 is connected, the message “HAVE A NICE DAY VEHICLE MONITOR” will be displayed on the LCD. Otherwise, if J1 is not connected, the current RTC time and date will be displayed.
if (erroloop==0)
{
if (RTCerror)
{
LCD_gotoRowCol(1,1);
LCD_writeString (“HAVE A NICE DAY“);
LCD_gotoRowCol(2,1);
LCD_writeString (“VEHICLE MONITOR “);
_delay_ms(100);
}
other
{
RTC_getData ;
_delay_ms(100);
LCD_gotoRowCol(1,1);
LCD_writeString (“DATE: “);
LCD_writeString (DATESTR);
LCD_gotoRowCol(2,1);
LCD_writeString (“TIME: “);
RTC_getTime ;
LCD_writeString (TIMESTR);
LCD_writeString(””);
}
}
7) If the data received from UART has some incorrect values, the message “WEAK SIGNAL OR LONG DISTANCE” will be displayed on the LCD. If the received data contains any warning messages, the message “JEARK/VIBRATION/ENGINE/KEY USED” will be displayed on the LCD. If there is no communication between the two HC-12 modules, the message “NO SIGNAL CHECK VEHICLE & BOARD” will be displayed on the LCD.
if (isUSART_DATA_READY==1)
{
if (USART_RECV_BUF(0) != '<' USART_RECV_BUF(3) != '>' (USART_RECV_BUF(1)!=USART_RECV_BUF(2)) )
{
error loop++;
LCD_gotoRowCol(1,1);
LCD_writeString (“WEAK SIGNAL OR “);
LCD_gotoRowCol(2,1);
LCD_writeString (“LONG DISTANCE”);
}
else if ( USART_RECV_BUF(1) != 0x80 )
{
error loop++;
if ( USART_RECV_BUF(1) & 0x07 )
{
LCD_gotoRowCol(1,1);
LCD_writeString (“pushes/vibration/”);
}
if ( USART_RECV_BUF(1) & 0x38 )
{
LCD_gotoRowCol(2,1);
LCD_writeString (“MOTOR/KEY USED “);
}
}
}
else // signal is out of coverage
{
error loop++;
LCD_gotoRowCol(1,1);
LCD_writeString (“NO SIGNAL. CHECK”);
LCD_gotoRowCol(2,1);
LCD_writeString (“VEHICLE AND PLATE PLATE.”);
}
8) If there is any vibration detected by the car device, it turns on the buzzer to beep.
if (error loop > 0)
{
_delay_ms(250);
setBuzzer(ON);
error loop = 1;
}
Check out the full code and quickly start building this exciting project. Please note that the system designed here is intended for cars and other four-wheeled vehicles. However, it can be installed on bicycles and scooters where only three ADC channels for detecting vibrations and one ADC channel for detecting ignition or automatic start can be used.
DIY challenge –
How exciting it would have been if, instead of receiving the alert messages on an integrated remote monitoring device, the user could receive the same messages on their smartphone. Try to develop the same project with a mobile application where the in-car installation device connects to a mobile application and the mobile application displays and emits warning beeps.
Buy this project now –
Required quantity of component
ATmega8(A)2 Microcontrollers
16×2 LCD display 1
HC-12 2 Modules
GY-61 Module 1
DS 1307 CI 1
7805 CI2
BC548 Transistor 1
3V battery (C2032) 1
Piezo Doorbell 1
5K Potentiometer 1
Capacitor 470uf 25V 2
Capacitor 47uf 16V 2
0.1uf 8 disk capacitor
1K 2 resistor
Resistors 3.3K 11
10K Resistor 7
33K Resistor 3
47K Resistor 1
1N4007 4 Diodes
3mm LEDs 6
32.758 KHz Crystal 1
1 3V battery holder
Base 2 28-pin IC
8-pin IC base 1
2-pin tactile button 5
Press to turn on switches 2
AVR 1 Programmer
PCB/breadboard as required
Connecting wire as required
Project source code
//Program to
/ *
filename: ParkedVehicleRx.C
author: fhn
date: 02-mar-2019
mcu: atmega8 (A)
usage: to receive data through HC-12 and set alarm accordingly. else display date and time only
*/
#define F_CPU 1000000UL
#define FCPU_BY_100 F_CPU/100
#define ON 1
#define OFF 0
#define TOGGLE 2
#define ERROR 1
#define OK
#define BUTTONS_PORT PIND
#define BUTTON_UP 0b10000000
#define BUTTON_NEXT 0b01000000
#define BUTTON_MUTE 0b00100000
#define BUTTON_SETTING 0b01000000
#define RTC_BIT 0b00010000 //portD
#define HC12_SET 0b00000100 //portD
#define BLINKER_LED 0b00100000 //portB
#define BUZZER_BIT 0b00000001 //portB
#define USART_MAX_RECV_BUF_SIZE 40
char USART_RECV_BUF(USART_MAX_RECV_BUF_SIZE+1);
char TEMP_STR(20);
#include
#include
#include
#include
uint8_t CURRENT_PROCESS = 0;
uint8_t HC12error=0, RTCerror=0;
uint8_t isUSART_DATA_READY=0;
uint8_t USART_RECD_DATA_POS=0;
uint8_t USART_RXD = 0;
#include "FHN_LCD4_LIB.C"
#include "FHN_TWI_LIB.C"
#include "fhn_rtc_ds1307_lib.C"
//================================================ ============
void initPorts ( )
{
DDRB=0;
DDRC=0;
DDRD=0;
LCD_DATA_REG = ( LCD_DATA_BITS );
LCD_CTRL_REG = ( LCD_ENABLE LCD_RS );
DDRD = 0b00000110;
DDRB = (BLINKER_LED BUZZER_BIT);
cli; //all interrupts disabled
}
//================================================ ============
int USART_init ( )
{
// INITIATES 9500-8-N-1 FORMAT
UBRRL = 12;
UBRRH = 0;
UCSRA = (1< '9') ch="0";
if ( ch>'3' && pos==0 )
ch="0";
if ( ch>'1' && (pos==3 pos==7) )
ch="0";
if ( ch>'1' && DATESTR(0)=='3' && pos==1 )
ch="0";
if ( ch>'2' && DATESTR(3)=='1' && pos==4 )
ch="0";
DATESTR(pos%10) = ch;
LCD_gotoRowCol(1,1);
LCD_writeString ( DATESTR );
}
else
{
ch = TIMESTR(pos%10);
ch++; if (ch>'9') ch="0";
if ( ch>'5' && (pos==13 pos==16) )
ch="0";
if ( ch>'2' && pos==10 )
ch="0";
if ( ch>'3' && TIMESTR(0)=='2' && pos==11 )
ch="0";
TIMESTR(pos%10) = ch;
LCD_gotoRowCol(2,1);
LCD_writeString ( TIMESTR );
}
}
else if ( (BUTTONS_PORT & BUTTON_NEXT) == 0 ) // MOVES TO NEXT POSITION
{
//isEdited=1;
eloop=0;
pos++;
if (pos>=18)
pos=0;
if (pos==2 pos==5 pos==12 pos==15 )
pos++;
if (pos==6) // for year 2000 series
pos++;
if (pos==7) // for year 2000 series
pos++;
}
else
{
loop++;
}
}
// set for foolproof except Feb 29 days in leap year (alway 28 days max.)
if (isEdited==1)
{
if ( DATESTR(0)=='3' && DATESTR(1)>'1')
{ DATESTR(1)='1'; }
if ( DATESTR(3)=='1' && DATESTR(4)>'2')
{ DATESTR(4)='2'; }
if ( DATESTR(0)=='3' && DATESTR(3)=='0' && DATESTR(4)=='2')
{ DATESTR(0)='2'; DATESTR(1)='8'; }
if ( DATESTR(0)=='3' && DATESTR(1)>'0' && DATESTR(3)=='0' &&(DATESTR(4)=='4' DATESTR(4)=='6' DATESTR(4)=='9' ))
{ DATESTR(1)='0'; }
if ( DATESTR(0)=='3' && DATESTR(1)>'0' && DATESTR(3)=='1' && DATESTR(4)=='1')
{ DATESTR(1)='0'; }
RTC_WriteDate ( DATESTR );
RTC_WriteTime ( TIMESTR );
LCD_gotoRowCol(2,11);
LCD_writeString("SAVED");
_delay_ms ( 2000 );
}
LCD_writeCommand ( 0x0C );
}
//================================================ ============
void clearUsartRx ( )
{
USART_RECD_DATA_POS=0;
isUSART_DATA_READY=0;
USART_RECV_BUF(0)=0;
USART_RECV_BUF(1)=0;
USART_RECV_BUF(2)=0;
USART_RECV_BUF(3)=0;
}
//================================================ ============
int main()
{
uint8_t errorloop = 0;
_delay_ms ( 100 );
initPorts;
_delay_ms ( 500 );
setBlinker(ON);
_delay_ms ( 500 );
setBlinker(OFF);
setBuzzer ( ON );
_delay_ms ( 200 );
setBuzzer ( OFF );
LCD_start ( );
_delay_ms ( 100 );
LCD_clrscr;
LCD_writeString("PARKED VEHICLE");
LCD_gotoRowCol(2, 1);
LCD_writeString("MONITOR -fhn/..");
setBlinker(ON);
_delay_ms ( 500 );
setBlinker(OFF);
_delay_ms ( 100 );
know ;
USART_init ( );
setBlinker(ON);
_delay_ms ( 500 );
setBlinker(OFF);
_delay_ms ( 500 );
HC12error = HC12_start ( );
setBlinker(ON);
_delay_ms ( 500 );
setBlinker(OFF);
_delay_ms ( 500 );
//RTC_Start ( );
_delay_ms ( 100 );
LCD_clrscr;
if ((PIND & RTC_BIT) == 0)
{
LCD_writeString("BY-PASS CHECKING");
LCD_gotoRowCol(2, 1);
LCD_writeString("RTC:DS1307");
RTCerror = 5;
}
else
{
LCD_writeString("Check RTC:DS1307");
LCD_gotoRowCol(2, 1);
RTCerror=RTC_getTime ( );
strcpy (TEMP_STR, TIMESTR );
if (RTCerror) //error
LCD_writeString( "ERROR" );
else if (strcmp ( TIMESTR, "HH:MM:SS")==0)
{
LCD_writeString( "NOT CONNECTED" );
RTCerror = 2;
}
}
_delay_ms ( 1500 );
LCD_clrscr;
while(1)
{
setBlinker(TOGGLE);
clearUsartRx ( );
if ((BUTTONS_PORT & BUTTON_MUTE) == 0)
{
errorloop=0;
}
if ((BUTTONS_PORT & BUTTON_SETTING) == 0)
{
editDateTime ;
}
sendMessage ( "?" );
if (errorloop==0)
{
if (RTCerror)
{
LCD_gotoRowCol(1,1);
LCD_writeString ("HAVE A NICE DAY ");
LCD_gotoRowCol(2,1);
LCD_writeString ("VEHICLE MONITOR ");
_delay_ms(100);
}
else
{
RTC_getDate ( );
_delay_ms(100);
LCD_gotoRowCol(1,1);
LCD_writeString("DATE: ");
LCD_writeString (DATESTR);
LCD_gotoRowCol(2,1);
LCD_writeString("TIME: ");
RTC_getTime ( );
LCD_writeString (TIMESTR);
LCD_writeString (" " );
}
}
_delay_ms(500);
if ( isUSART_DATA_READY==1 )
{
if ( USART_RECV_BUF(0) != '<' USART_RECV_BUF(3) != '>' (USART_RECV_BUF(1)!=USART_RECV_BUF(2)) )
{
errorloop++;
LCD_gotoRowCol(1,1);
LCD_writeString ("WEAK SIGNAL OR ");
LCD_gotoRowCol(2,1);
LCD_writeString ("LONG DISTANCE");
}
else if ( USART_RECV_BUF(1) != 0x80 )
{
errorloop++;
if ( USART_RECV_BUF(1) & 0x07 )
{
LCD_gotoRowCol(1,1);
LCD_writeString ("JERKS/VIBRATION/");
}
if ( USART_RECV_BUF(1) & 0x38 )
{
LCD_gotoRowCol(2,1);
LCD_writeString ("ENGINE/KEY USED ");
}
}
}
else // signal is out of coverage
{
errorloop++;
LCD_gotoRowCol(1,1);
LCD_writeString ("NO SIGNAL. CHECK");
LCD_gotoRowCol(2,1);
LCD_writeString ("VEHICLE & BOARD." );
}
setBuzzer(OFF);
if (errorloop>0)
{
_delay_ms(250);
setBuzzer ( ON );
errorloop=1;
}
}
}
//================================================ ============
ISR (USART_RXC_vect)
{
USART_RXD = UDR;
if ( USART_RECD_DATA_POS < USART_MAX_RECV_BUF_SIZE && USART_RXD>0)
USART_RECV_BUF(USART_RECD_DATA_POS++) = USART_RXD;
if ( USART_RECD_DATA_POS>=4 ) //( USART_RXD == '>' )
isUSART_DATA_READY=1;
}
//================================================ ============
Circuit diagrams
RX |
|
Texas |
|
Project Technical Sheet
https://www.engineersgarage.com/wp-content/uploads/2019/10/library_attechment.zip
Project video