Code:
Include "modedefs.bas"
DEFINE OSC 20
'LCD CONFIGURATION
DEFINE LCD_DREG PORTB 'Define PIC port used for LCD Data lines
DEFINE LCD_DBIT 4 'Define first pin of portb connected to LCD DB4
DEFINE LCD_RSREG PORTB 'Define PIC port used for RS line of LCD
DEFINE LCD_RSBIT 3 'Define Portb pin used for RS connection Communication
DEFINE LCD_EREG PORTB 'Define PIC prot used for E line of LCD
DEFINE LCD_EBIT 0 'Define PortB pin used for E connection
DEFINE LCD_BITS 4 'Define the 4 bit communication mode to LCD
DEFINE LCD_LINES 2 'Define using a 2 line LCD
DEFINE LCD_COMMANDUS 2000 'Define delay between sending LCD commands
DEFINE LCD_DATAUS 50 'Define delay time between data sent.
'PWM CONFIGURATION
DEFINE CCP1 REG PORTC 'Port to be used by HPWM1
DEFINE CCP1_BIT 2 'Pin to be used by Hpwm1
DEFINE CCP2 REG PORIIC 'Port to be used by HPWM2
DEFINE CCP2_BIT 1 'Pin to be used by hwmp2
'ADC CONFIGURATION
Define ADC_BITS 10 '10 bit result
define ADC_clock 3 '8mhz external clock
define ADC_Samplus 50
trisA= 255 'all PORTA pins are inputs
adcon1= %10000010 ' Set PORTA analog and right justify
trisc=0
portc=0
Temp var word 'Temp out of Lm35
Vp var word 'Voltage out of Pressure Sensor
Vl var word 'Voltage out of Level Sensor
char var byte
SCL VAR PORTC.3 ' Clock pin
SDA VAR PORTC.4 ' Data pin
B0 VAR BYTE
B1 VAR BYTE
B2 var byte
B3 var byte
address VAR WORD
address=17
main:
Serin portc.7,2,char 'Receive data sent from the transmitting pic
if (char == "A" ) then 'if data is character A
pause 10
Adcin 0, Temp ' Read channel 0 to Temp (0-1023)
Temp = (Temp */ 500)>>2 ' Equates to: (Temp * 500)/1024
lcdout $fe,$80,"Temp= ",dec2 Temp 'Display Temp On The First Line
Adcin 1, Vp 'Read channel 1 to Vp (0-1023)
Vp = (Vp */ 500)>>2 'Equates to: (Vp * 500)/1024
LCDOut $fe,$c0 ,"Pressure = ",DEC (Vp/100),".", DEC2 Vp 'Display Voltage out of pressure sensor on second line
Adcin 2, Vl 'Read channel 2 to Vl (0-1023)
Vl = (Vl */ 500)>>2 'Equates to: (Vl * 500)/1024
LCDOut $fe,$94 ,"Level= ",DEC (Vl/100),".", DEC2 Vl 'Display Voltage out of Level sensor on third line
i2cwrite SDA,SCL,%10100000,address,[B0,B1] 'Writes two data to i2c
pause 10
I2CRead SDA,SCL,%10100001,address,[B2,B3] 'Read 2 locations in a row
pause 10
LCDOut $fe,$D4,#address,":",#B1,"%P ",#B0,"%S "
if portd.0==0 then 'Soil Moisture is lower than preset value
if Vp<300 then 'Voltage From Pressure Sensor is Less than preset value (3 volt)
hpwm 2,179,5000 'Solenoid is on with 70% duty cycle at 5khz
B0=70 'Data (70%duty cycle of the soloneid)
if Vl<300 then 'If voltage of Level sensor is Less than preset value (3 volt)
hpwm 1,179,5000 'Pump is on with 70% duty cycle at 5 khz
B1=70 'Data (70%duty cycle of the Pump)
else
hpwm 1,77,5000 'Pump is on with 30% duty cycle at 5 khz
B1=30 'Data (30%duty cycle of the Pump)
endif
else
hpwm 2,77,5000
B0=30
if Vl<300 then
hpwm 1,179,5000
B1=70
else
hpwm 1,77,5000
B1=30
endif
endif
else 'Soil Moisture is high or equal to preset value
hpwm 1,0,5000 'Turn off Pump
hpwm 2,0,5000 'Turn off Solenoid
B0=0
B1=0
endif
else
lcdout $fe,1 'Clear LCD
hpwm 1,0,5000 'Turn off Pump
hpwm 2,0,5000 'Turn off Solenoid
Serin portc.7,2,char 'Receive data sent from the transmitting pic
ENDIF
Goto main 'Do it forever
End
Sender:
Bookmarks