0% found this document useful (0 votes)
42 views13 pages

PIRCODE

The document contains code for a microcontroller program that controls an LCD display and water pump. It includes function definitions for initializing the LCD display via I2C, writing strings and characters to the display, and turning the water pump on and off based on sensor input. The program continuously checks an IR sensor, coin sensor and capacitive sensor, and displays status messages on the LCD depending on the sensor states, while controlling the water pump.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
42 views13 pages

PIRCODE

The document contains code for a microcontroller program that controls an LCD display and water pump. It includes function definitions for initializing the LCD display via I2C, writing strings and characters to the display, and turning the water pump on and off based on sensor input. The program continuously checks an IR sensor, coin sensor and capacitive sensor, and displays status messages on the LCD depending on the sensor states, while controlling the water pump.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 13

CODE.

#include <xc.h>

#include <htc.h>

#include "CONFIG.h"

#include "LCD_HEADER.h"

#define delay for(i=0;i<1000;i++)

int i;

#define IR RD0 //IR Output is connected at PORTD.0

#define MP RB0 // WATERPumt

#define CN RB7 // coinsensor

#define CS RB6//CAPACITIVE SENSOR

void main()

TRISD = 0xff;

TRISB=TRISC0=TRISC1=TRISC2=0;

PORTB= 0x00;

//Port D act as Input

I2C_Master_Init();

LCD_Init(0x4E);

while(1) {

if(IR == 1 && CN == 0 && CS == 0 ) {

LCD_Clear();

LCD_Set_Cursor (1,4);
LCD_Write_String("APPROACHING:");

LCD_Set_Cursor (2,6);

LCD_Write_String("TAO");

MP = 1;

__delay_ms(7000);

} else {

LCD_Clear();

LCD_Set_Cursor (1, 5);

LCD_Write_String("NO SIGNS OF TAO");

MP = 0;

while(2) {

if(IR == 0 && CN == 1 && CS == 0) {

LCD_Clear();

LCD_Set_Cursor (1,4);

LCD_Write_String("APPROACHING:");

LCD_Set_Cursor (2,6);

LCD_Write_String("TAO");

MP = 1;

__delay_ms(7000);

} else {

LCD_Clear();

LCD_Set_Cursor (1, 5);

LCD_Write_String("NO SIGNS OF TAO");

MP = 0;

}
}

}
LCD.HEADER.H

#define _XTAL_FREQ 16000000

#define I2C_BaudRate 100000

#define SCL_D TRISC3

#define SDA_D TRISC4

#define LCD_BACKLIGHT 0x08

#define LCD_NOBACKLIGHT 0x00

#define LCD_FIRST_ROW 0x80

#define LCD_SECOND_ROW 0xC0

#define LCD_THIRD_ROW 0x94

#define LCD_FOURTH_ROW 0xD4

#define LCD_CLEAR 0x01

#define LCD_RETURN_HOME 0x02

#define LCD_ENTRY_MODE_SET 0x04

#define LCD_CURSOR_OFF 0x0C

#define LCD_UNDERLINE_ON 0x0E

#define LCD_BLINK_CURSOR_ON 0x0F

#define LCD_MOVE_CURSOR_LEFT 0x10

#define LCD_MOVE_CURSOR_RIGHT 0x14

#define LCD_TURN_ON 0x0C

#define LCD_TURN_OFF 0x08

#define LCD_SHIFT_LEFT 0x18

#define LCD_SHIFT_RIGHT 0x1E

#define LCD_TYPE 2 // 0 -> 5x7 | 1 -> 5x10 | 2 -> 2 lines

//-----------[ Functions' Prototypes ]--------------


//---[ I2C Routines ]---

void I2C_Master_Init();

void I2C_Master_Wait();

void I2C_Master_Start();

void I2C_Master_RepeatedStart();

void I2C_Master_Stop();

void I2C_ACK();

void I2C_NACK();

unsigned char I2C_Master_Write(unsigned char data);

unsigned char I2C_Read_Byte(void);

//---[ LCD Routines ]---

void LCD_Init(unsigned char I2C_Add);

void IO_Expander_Write(unsigned char Data);

void LCD_Write_4Bit(unsigned char Nibble);

void LCD_CMD(unsigned char CMD);

void LCD_Set_Cursor(unsigned char ROW, unsigned char COL);

void LCD_Write_Char(char);

void LCD_Write_String(char*);

void Backlight();

void noBacklight();

void LCD_SR();

void LCD_SL();

void LCD_Clear();

unsigned char RS, i2c_add, BackLight_State = LCD_BACKLIGHT;


//---------------[ I2C Routines ]-------------------

//--------------------------------------------------

void I2C_Master_Init()

SSPCON = 0x28;

SSPCON2 = 0x00;

SSPSTAT = 0x00;

SSPADD = ((_XTAL_FREQ/4)/I2C_BaudRate) - 1;

SCL_D = 1;

SDA_D = 1;

void I2C_Master_Wait()

while ((SSPSTAT & 0x04) || (SSPCON2 & 0x1F));

void I2C_Master_Start()

I2C_Master_Wait();

SEN = 1;

void I2C_Master_RepeatedStart()

I2C_Master_Wait();

RSEN = 1;

}
void I2C_Master_Stop()

I2C_Master_Wait();

PEN = 1;

void I2C_ACK(void)

ACKDT = 0; // 0 -> ACK

I2C_Master_Wait();

ACKEN = 1; // Send ACK

void I2C_NACK(void)

ACKDT = 1; // 1 -> NACK

I2C_Master_Wait();

ACKEN = 1; // Send NACK

unsigned char I2C_Master_Write(unsigned char data)

I2C_Master_Wait();

SSPBUF = data;

while(!SSPIF); // Wait Until Completion

SSPIF = 0;

return ACKSTAT;

}
unsigned char I2C_Read_Byte(void)

//---[ Receive & Return A Byte ]---

I2C_Master_Wait();

RCEN = 1; // Enable & Start Reception

while(!SSPIF); // Wait Until Completion

SSPIF = 0; // Clear The Interrupt Flag Bit

I2C_Master_Wait();

return SSPBUF; // Return The Received Byte

//======================================================

//---------------[ LCD Routines ]----------------

//-----------------------------------------------

void LCD_Init(unsigned char I2C_Add)

i2c_add = I2C_Add;

IO_Expander_Write(0x00);

__delay_ms(30);

LCD_CMD(0x03);

__delay_ms(5);

LCD_CMD(0x03);

__delay_ms(5);

LCD_CMD(0x03);

__delay_ms(5);

LCD_CMD(LCD_RETURN_HOME);

__delay_ms(5);
LCD_CMD(0x20 | (LCD_TYPE << 2));

__delay_ms(50);

LCD_CMD(LCD_TURN_ON);

__delay_ms(50);

LCD_CMD(LCD_CLEAR);

__delay_ms(50);

LCD_CMD(LCD_ENTRY_MODE_SET | LCD_RETURN_HOME);

__delay_ms(50);

void IO_Expander_Write(unsigned char Data)

I2C_Master_Start();

I2C_Master_Write(i2c_add);

I2C_Master_Write(Data | BackLight_State);

I2C_Master_Stop();

void LCD_Write_4Bit(unsigned char Nibble)

// Get The RS Value To LSB OF Data

Nibble |= RS;

IO_Expander_Write(Nibble | 0x04);

IO_Expander_Write(Nibble & 0xFB);

__delay_us(50);

void LCD_CMD(unsigned char CMD)

{
RS = 0; // Command Register Select

LCD_Write_4Bit(CMD & 0xF0);

LCD_Write_4Bit((CMD << 4) & 0xF0);

void LCD_Write_Char(char Data)

RS = 1; // Data Register Select

LCD_Write_4Bit(Data & 0xF0);

LCD_Write_4Bit((Data << 4) & 0xF0);

void LCD_Write_String(char* Str)

for(int i=0; Str[i]!='\0'; i++)

LCD_Write_Char(Str[i]);

void LCD_Set_Cursor(unsigned char ROW, unsigned char COL)

switch(ROW)

case 2:

LCD_CMD(0xC0 + COL-1);

break;

case 3:

LCD_CMD(0x94 + COL-1);

break;

case 4:
LCD_CMD(0xD4 + COL-1);

break;

// Case 1

default:

LCD_CMD(0x80 + COL-1);

void Backlight()

BackLight_State = LCD_BACKLIGHT;

IO_Expander_Write(0);

void noBacklight()

BackLight_State = LCD_NOBACKLIGHT;

IO_Expander_Write(0);

void LCD_SL()

LCD_CMD(0x18);

__delay_us(40);

void LCD_SR()

LCD_CMD(0x1C);
__delay_us(40);

void LCD_Clear()

LCD_CMD(0x01);

__delay_us(40);

}
CONFIG.H

// PIC16F877A Configuration Bit Settings

// 'C' source line config statements

// CONFIG

#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)

#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)

#pragma config PWRTE = ON // Power-up Timer Enable bit (PWRT enabled)

#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)

#pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit
(RB3 is digital I/O, HV on MCLR must be used for programming)

#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code
protection off)

#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all
program memory may be written to by EECON control)

#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)

// #pragma config statements should precede project file includes.

// Use project enums instead of #define for ON and OFF.

#include <xc.h>

You might also like