0 ratings0% found this document useful (0 votes) 521 views36 pagesPIC Projects With CCS PIC C Compiler - PIC16F877A
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here.
Available Formats
Download as PDF or read online on Scribd
Thursday, Sepiember 8, 2015
Real time clock with relative humidity and temperature sensing using PIC16F877A, 2004 LCD,
DS1307 RTC and DHT11
This top shows how to build areal ime clock ith relative humidity and temperature sensing sing
PIC\6F877A microcontroller, DS1g07 RTC and DHTix (RHO sensor where al dala ae displayed on 2084
{LD display. The 20x4 LCDIas 20 columns and 4 rows which good enough or this projet. The comer
used to progzam the microcontroller is CCS PICC POWHD.
To see how to interface PIC36#877A with DS1go7 take alookt the following opi:
Real time elock using PICIGF877A microcontroller and DS1307 serial RTC
Ando see how to interface PICISE4550 with DHT 22 (AM2go2) take alookat ths topic
Interfacing DHT relative humidity and temperature sensor with PICIGF877A microcontroller
The DS1g07 RTCs an & pin integrated crit uses IC commenicalion protocol to communicate with master
device which isin our eae the PICIBF877A mieroeostcoler, Ths sll dip can count seconds, minutes, ours,
day, date, month and year with lap-year upto year 2100,
‘The DHT 1s (RETo:) sensor comes in single ow 4-pin package and operates fom 9.3 to 5 5V power suppl. It
can measure temperature from 0-50 °C with an accuracy of 42°C and relative humidity ranging from 20-90%
‘with an accuracy of 5% The sensor provides fully cllrated digital outputs forthe two measurements Ithas
_ptits own proprietary 1ovire protocol, and therefore, the communication between the sensor and a
‘mirooontroller is not possible through deat interface with any oft peripherals. The protocol m
implemented nthe firmware ofthe MCU with precise timing required by the sensor
Compones
+ PICI6ES77A microcontroller
+ Dsigor RTC
(RHO) Sensor
+ 2004 100
+ g¥Coincellbatery
+ SMiivand ga.768Kitz crystal osilators
+ axzapP capacitors
+ gxs0K resistorsace PI Pc nC EC Cam RCNERIA
+ 47Kresisor
+ 10K Potentiometer
+ 2Bttone
+ +5 Power Supply
+ Protobosrd
+ Samper Wires
PICIGF877A + 2004 LCD + DSig07 RTC + DHTit sensor circuit:
bo gt
F Irpscesnesenspa oo
‘The two pushbutton for adjusting time and date as shown in the video below.
C8 € code:
“The project C code is jut combination ofthe Ccodes ofthe two previous procs
‘The reading of olasve humidity and temperature datais done every 1 second.
/ PICABFBTIA + 2004 LCD + 051307 RIC + DHTA2 Sensor CCS C code
| netps/ecspiceblogspet..con/
electronnoteggnai-con
replepctit sonei /.c0 nodule comections
fedefine LCD_RS_PIN PIN_O@
Iedefine LCO_RW_PIN PINE
Jpderine LCo_eNABLE_PIN PIN_D2
Frdefine LCo_DATAG PIN_03
Ipderine Lco_DATAS PIN_D4
FRdeine UCO_ATA? PINDE
{ena co nodule connections
frinclude <36F8778.n»
Fpruses H,NOKDT,NOPROTECT,NOLVP
hhuse delay(elock ~ #90900)
Fpinetude
Fhuse Fast_so(8)
hnuse T2c(aaster, 12c1, r4sT=108080)
Indefine OHTA1_PIN PIN,82 11 Connection pin between DHT21 and neu
ihort button_state, Tine_outs
kchar tane(] = "TINE 5
Ichar ealendar{] =" (28 "5
Linsigned ints second, seconde, minute, minuter®,
year, yearde, day, 4, 45
inar messaget{] = “Tengerature: 0.0 ¢ “;
ichar message2[] = “vuricity : 0.0 % "5
lnsigned inte T_bytet, Tbyte2, WLbytet, aH byte2, checksum, tine read ;
ose as1307_tsplay()
secondi@ = (second & x78) >> 43
Finutel® = (Rinute & 0x70) 9> 45
hourse = (hour & @x2B) >> 4;
hour = hour & OF;
dateie = (date & 038) >> 4;
date = date & OF;yearae
case 2:
case 3
ea_gotexy(18, 2);
print (ied pute, calendar);
= (nonth & ex18) >> 45
(year & 0x60) 9» 4;
year = year & er;
tine[15] = second + 48;
time[15] = seconaie + 48;
time[13] = minute + 48;
ime[22] = minueere + 48;
tine[10} = hour + 485
time[S] = hourie + af
calendar{9] = year” + 48;
calendar[8) = year1® + 48;
calendar[4]. = month + 48;
calendar[3} = onthe + 48;
calendar(1) = date + 48;
calendar(@) = daver® + 48;
ea_gotory(t, 1); 11 9 to column 1 row
peinte(led_pute, tine); 1 isplay tine
ed_gotoxyia, 2); 11 0 to colunn 1 row 2
sutten(aay
ea_pute(DATE:Sun")s breaks
ed_pute("vATE:Hon")s breaks
+ Led pute( ATE: Tue"); breaks
ed_pute(“OATE Wed"); breaks
5: Led pute(DATE:TAU")} breaks
Aed_pute( ‘DATE: Fri°)} beaks
ed_putc(‘oaTe:sat")} beeak;)
oid 451307 write(unsigned int® address, data_){
szestart(); I) stare 12
11 051387 asdress
U1 Send register address
1/ wnsta data to the selected register
s2e_stopt)s U1 Stop 126oid 4s1367_read(){
ize_start(); Uy Stace 120
s2crite(@x0e); 11 051387 acaress
s2eerite(e) U1 Send register address
s2estart(); 11 Restart 126
ize erite(ex01); U/ Initialize date read
second ~12¢_read(t); 1/ Read seconds from register @
rninute -i2e_pead(a)s 17 Read ninuts fron register 1
hour = i2e_Fead(); 17 Read hour from register 2
ay = 12¢_Fead(); U1 Read day from register 3
date = 42¢-read(3); W/ Read date fron register 4
rronth = $2¢_read(a); U/ Read month fron register $
year = £2c_read(@); 11 Read year fron register 6
i2e_stop(); 11 Stop 126
nts eaLe(snt® paraneter, intB me, snt® yy)
nie (TRUE) {
4¢(Anput(@7%L80)) button state
wht e(Hinput{ rv s1))¢
parsneter:
AFG == 1 8A paraneter > 23)
paraneter = 8
Se. == 2 88 parameter > 59)
A(L == 3 AB parameter > 31)
A¢(L == 4 && paraneter > 12)
AGL s= $ && paraneter > 99)
parameter = 0;
ed_gotory bon, D5
printt(led pute,"%@2u", paraneter):
delay_ps (280);
ed_gotony ve, 9);
ed pute(”")s
378
Wwhile((input(Prse) || button state) ab input(Pmn si) && J < 5)¢jes
aelay_e5(50)3}
ed_gotory oe, 9);
print#(1ea_pute, "2220", paraneter);
378;
‘insle((Snput(Prnge) || button state) a input(PmN Bi) ak J < 5)¢
ses
delay_ns (50)
Le(1inpat(PIN_98) && ibutton_stateyt
button_state = 1; return paraneters}
ive(OHTAS_PIN); 11 Configure connection pin a8 output
‘utput tow(OHTI1_PIN); 11 Connection pin output low
delay 5(25)3
utput_bigh(ONTt_P1N); 11 Connection pin output high
delay_1s(30);
float (oH:
Pw); 11 configure cannection pin a8 input
detay_vs(40);
#(1Amput (OHTA2_ PIN) 11 Read and test Sf connection pin is low
elay_us(80);
{(anput (ona
delay_us(50)}
?
Em) 11 Read and test 4 conection pin ts high
>
Pnstend nts eat utae
Unsigned intB 4, k, data = 8; // K As Used to count 1 bit reading duration
(Tine out)
for(t = 0; £8; SHC
ar
wwhite(1nput(oMraa_PIN))¢
oes highAF (k > 200) {Tine out = 15 breaks}
delay_us(a);}
elay_u5(30);
4 ¢(Linput(owras_pin))
bit_elear( data, (7 --); 11 Clear bit (7 = 4)
else
bit_set(_data, (7 = 19)5 11 Set bse (7 = 1)
vine input (OHTA, P2H) 4 11 wait unti pin goes Low
Af (k > 209) {Tine out = 25 breaks}
delay_us(a);)
?
)
return datas
Lroid main((
pullups(TRUE); 11 snabte PORTA pull-ups
utput_o(@)s
tris 603); 11 Configure R80 & RB 25 inputs
edn /7 anisialize UCD nodule
ed_putet V5 11 60 clear
wniecTAVED,
1¢(daput(eDi_e)) button. state = 6;
4 ¢(1Anput (PIN 89) 88 (Ioutten_state)){
button_state = 2;
11 convert BCD to decinad
ninute = minute + minute1e * 18;
hour = hour + hours * 105
11 td conversion =
hour = edst(hour, 28, 1):
iesninute = edit(sinute, 13, 1);
white (TAUE){
1#(Snput 9 80))
button state = 8;
wrste(tenpve(PIN_ 8D)
eying
Siteay > 7)
goto 6, 295 11 G to coluen 6 rom 2
susten(day)<
fase 4: Le, pute( Su
fase 2: ded pute("Pon
ase 3: Led pute("Tue"); breaks
case 4 Ted_pute("hes")3 Sneaks
case $1 Led_pute(-Tv"}s breaks
case 6: Led_pute (ri); break;
fase 71 Ned_putet"Sat")3 Breaks)
delay. (200);
)
ee, gotoxy(6, 2):
lee putec”")s
se
Soite(Cinpet(PIN_p9)|Ioutton state) 88 Input(PINB2) 88 J < 596
delay_ws(50);}
ea. gotonys, 235
suitendaay)
case AF Led, pute "Sun
case 2: Led putetHor
case 3: Ted_putet"Tue
ave 4: Ted_putet wed
cose 7: led breaks)
s(tnpue(ern_86) 8 ("button state) ){
tueeon_seate = 1
breaks)\inite((énput(PIN_ge)] [button_state) && input(PIN.62) 8B 5 « 5){
jes
elay_es(50);)
y
ina
date = eait(date, 18, 2)3
sse(nonth, 13, 25
year = edit(year, 28, 2);
11 convert decinal to 8cb
rinute = ((minute/i8) << 4) + (ninute % 18);
hour = ((hour/3@) << 4) « (hour X 20)s
date = ((date/s0) << 4) + (date % 10):
nonth = ((ronth/1@) <¢ 4) + (nonth % 10);
year = ((year/40) << a) + (year % 18)
tod conversion
4431307 wrste(2, minute);
4s1307_wrste(2, nour);
1307 write(3, day);
asi307_write(’, date):
1451307 wrste(S, nonth);
41307 rste(6, year);
as1267_arste(®, @);,
>
s1307_rea40)3 11 Read cata Fron 051307 RICE
s13@7_display(); 1/ biaplay tine and calendar
iF{((seconai® * 1ersecond)>tine_read)||{(secondi® * i8esecond)==b && tine_read)){
‘ine_read = seconei® * 18 + seconds
start_stenai(s
¢(eheekresponse(). 11 TE there 4s response fran sensor
fu pyter = Read_oata(); 11 vead RW byter
fut byte2 = Read_vata() 1/ vead Ri bye
byten = Read Oata() 1/ read T byte
Tobytez = Read bata(): 11 read T byte
Gecksun ~ Read_0ata(); 11 read checksumsF¢rine out)
ed_gotoxy(21, 2);
ed_pute(” "Tine out!
Gelay_ns(50);
vs
vs
ressage?(13] e448
ressage2[14] ye +4
ressages[16] = Toaytez / 10 + 48;
ressage2[13] = @4_sytei / 20 + 48;
ressage2[14] = 4 Byte % 18 + 48;
rressage2[16] = @4.Bytez / 30 + 48;
rressage2(17] = 2235
Ted gotoxy(2t, 1)
peinte(ted_pute, messaged);
ed gotonyi2t, 2);
printé(led_pute, sessage2);
2s
hecksum Error! "Ds
Ted_potony(2i, 2)3
ed_pute( Ds
>
d
else (
Ted gotoxy(2t, 15 11 60 to
Ted pute(" ‘No response "D3
led_gotory(24, 2); 11 (0 %0
ed_pute(” from the sensor ")3
y
>
11 1 reading takes Ione tine
11 60 to coluan 1 roe
11 co to coluan 1 row 4
V/ Clear 4th ros
Sr(CheckSun == ((RH_Byten + RH Byted + TAytel + Tpyte2) & OFF)
11 Degree sysbol
11 G0 %0 colunn 1 row 3
11 Display aessager
11 Go to colune 1 row &
11 Display sessage2
11 60 %9 colunn 1 row 3
1/ Go 0 colunn 3 row &
U1 Clear ah row
colunn 1 row 3>
Project Video:
Posted by Hletron Note at 0:35:00 AM cone
Labels: DItTsx(REFTo3),DS1307 RTC, LCD, PICIEFETZA,
Monday, Soptombor 6, 2018
Real Time Clock with Alarm Using PIC16F877A and DS1307 RTCace PI Pc nC EC Cam RCNERIA
‘This topieshows how to make a eal time dockand calendar with 2 alarms using PICIGFH77A microcontroller,
12004 LCD and DS1307 serial RTC.
“The following video shows waat Im going todo:
Terjadi kesalahan.
Ccableh menonton vdeo in
‘we youtubecom stu kta J
“To scehow to interface the DS1307 RTC chip with PICYSP877A to make areal time cockcread the following
topic:
Real time dock using PIC161877A microcontroller and DS1307 serial RTC
About DS1307 RTC IC:
replepetit noneate PI Pc mC EC Cam FRCNERIA
“The DSigo7 isan pin integrated circuit uses LaC communication protocol to communist with master device
which sin ou case the PICi6F877A microcontroller. This stall chip can count seconds, minutes, hours, day,
date, month and year with eap-year up to year 2100.
“The DS1907 receives ad transfers data (clock data and calendar data) as BCD format, so after reesving data we
have to convert these data nto decimal data, and before writing datato the D307 we have to convert this data
from decimal to BCD format. For example weave the BCD number 33, converting this number into dein)
ives
“The following mage shows the DSi907 pin configurations
PIN CONFIGURATIONS
xi Hc x ec
veal imsawiour x2] Bsaw/
sm MOsch Yau Bsc
leno msca nor] Fisoa
80150 mils) PDIP (300 mis)
‘And the following ciruits typical operating cirultfor the DS1g07 RTC:
TYPICAL OPERATING CIRCUIT
Mes
corysTaL
| 3
saviiouf —*$—+
Ds1307
_AWbatery can be connected between VBAT and GND as a backup supply input which keeps the DSig07
osllator running when the main power supply of the cruit io
The DS1307 uses an external 32. 768KIfz crystal and there sno need to add any resistors or eapacitors wit it
replespetitscnencMaal C1578ace PI Pc nC EC Cam RCNERIA
More informations in the DS1g07 RTC datasheet.
RIC with 2 alarms using PICI6F877A and DS1g07 circuit:
Project circuit schematcis shown below.
Bak, vasa
By
Required Components:
PICIGRS77A Microcontroller
Dsig07
2004 (20%4) LED
3VCell Battery
Suitizand 92,768KItzerytal oseators
‘excapPeapacitors
+ gxs0K resistors
10K Potentiometer
replepetietnorewcNaal C1847+ 4700hm resistor
+ Buttons
+ LED or Alarm)
+ Susser (or Alem 2)
+ +5Power Supply
+ Protoboard
+ Jumper Wires
Real Time Clock with 2 Alarms using DS1307 RTCC and PICIGF877A CCS C code:
“This ig the fll ode ofthis projec ..ts easy! be patint!
U1 netps/ eespice blogspot.com!
Y/ electronetedgnail.con
1 /.c0 nodule connections
iedefine
Iedefine LCO_DATAG PIN_D3
Fndetine Lco_DATAS PIND4
Frderine LCO_pATAS PIN_DS
Fhderine UCO_pATA? PIN D6
{fend 1c0 module connections
Include <16F87A.n»
infuses HS, NOMDT, NOPROTECT,NOLVP
Jruse delay(eleck = s8ee90e)
Frsnctude
frase fast_s0(8)
hhuse 12c(master, 12C4, FAST = 100000)
i neal tine clock with alara using PICISFE77A and 051307 RICC CCS ¢ code
Truse pum (PWHL, FREQUENCY = 2406H2, DUTY = 50, PAM_OFF)
{short alaraa_status, alaraz_status;hae timel] = “Tame
har ealendar{] =" / (20 5
har alarmi[] = "ALAR 2s
har adarm2[} > "ALAR 20%
insigned int second, secondie, minute, rinuter®,
laraz_minute, alare2 pour;
jd 421387 Atsplay(
secondi@ = (second § 678) >> 4;
‘second = second & OF;
inutel® = (ninute & 0x78) >> 43
hourse = (hour & @x30) >> 45
(date & 0x30) >» 4;
ronthie exe) >> 4;
ronth = nonth & 8x0
yeari® = (year & OxF8) >> 4;
ine[26] = second + 483
time(s] = seconaie + 48;
tine(13] 448;
time[12] = minurere + 48;
time[1e] = hour + 48;
tine[9}. = rourte + 48;
calendar(3) = year + 48;
calendar{8} = yeart® + 485
calendar{4). = wonth + 48;
calendar[3} = onthe + 45;
calendar[1] = date + 48;
calendar[e] = date1e + 48;
ed_gotony(t, 195
printt(led_pute, tine);
ed gotoxyi, 233
suitencaay)
11 69 t9 colunn 3 row 3
1/ Ossplay ine
U1 69 0 colunn 1 row 2case 1: Led pute(“DAre:sun ")s breaks
case 2: Led pute(“DATE:won ")} breaks
case 3: Led pute(“DATE:Tue "3 breaks
case 4: Led pute(“DATE:Wed "); breaks
case $1 Ted_pute(“DATe:Thu “3 breaks
case 6: 1ed_pute(“DATE:Fré "3 breaks
case 7: Led_pute(“OATe:sat °); beeak3}
ea_gotoxy(19, 2); U1 Go to colunn 19 rou 2
print(Led pute, calendar); 17 visplay calendar
joie alarm display()( 11 Display alarms info
ed_gotony(2t, 1); W/ 69 0 column 3 row 3
se(@larns_status)(
alarei[13] = alarst_ninute % 10 + 48;
alarai[12] = alarad_minute/t@ + 48;
alarai[10] = alaret_pourtie + «8;
alarm[9] = alarnifour/18 + 48;
printf(ied_pute, alarm);)
else led pute("ALawen: OFF“);
ed_gotoxy(22, 2);
se(@larna_status)(
alarn2[13] = alara2_minute X10 + 48;
alarn2[12] = alare2minute/t@ + 48;
alaraz{10] = alara2_hourtie + «3;
alarnz[9] = alarnzfour/i8 + 48;
ed_gotory(24, 2); 1) 60 0 colinn 3 row 4
print (led pute, alarm2);)
else Lea pute( ALAR: OFF “Ys
old ds1307_rtte(unsigned Lnt8 address, data_){
sac start); Wy stare 12¢
11 081307 acéress
11 Send register address
* U/ write data to the selected register
A2e_stort)s W1 Stop 12¢
oid ds1367_read(){s2e_start()i U1 stare 2c
ize write(@x00); 411 081307 accress
ize erite(@); U/ Send register address
126
efalize data read
sinuts fron register 4
hour fom register 2
ay fron register 3
ate fron register «
nonth fron register 5
year {ron repister 6
s2estart();
s2cerite(@x01);
16(0)5
ininute -i2e_read(3);
2e_Fead(s)i
s2e_Fead(2);
2e_read(3);
rronth = $2¢_read(a)s
((ninste & @x0F) + 3-4) * 20)) a8
(hour exar) + (hour >» 4) * 30) BA (second =~ e) af alarei_status)
high(PIN_83);
A ((alarmz_ninute == ((ninute & Ox8F) + >> 4) * 20)) 88
(alarm2_pour == ((hour & OF) + (hour >> 4) * 18)) BB (Second =+ @) 88 alare2 status)
11 Enable PORTE pull-ups
11 Configure R® & ROL a8 inp
ed_ int U1 Initialize LCD nodule
ed_pute(\F95 1 (Co clear
wnt eCTRUE){
1¢(anput(PIN_60)
11 Convert BCD to decinal
mminute = minute + minutelo *
hour = hour * nourde * 2
date = date + cater® * 10;
month = ronth + nonthio * 10;Led_gotory(2, 93
led_pute(" " inutes:
ed gotoxy(2, 2):
ed pute Ds
‘elay_n5(200);
whi eCTAUED{
(input
rinutess;
s¢(minute > 59)
led_gotory(1e, 2)3
printt(1ed_pute,"s22u", minvte);
EF(Amput(PIN_9@) == 8)
delay_#5(200);
?
ed gotoxy(7, 1)
ed_pute(” tour: ")s
ed gotoxy(10, 25
ed pute” ")s
elay_n5(200);
whi eCTRUED,
1 ¢(Anput(PIH_s2) =
hourees
(nour > 23)
hour = @;
ed_gotory(1®, 23
printf(cd pute, "220", hour):
{¢(Anput(PIN_99) = @)
elay_es(200);
ed gotoxy(9, 13
ed_pete("02y: ")s
ed gotoxy(10, 23
red pute(” “}s
elay_ns(200);
(1) = 8)
411 G0 +0 colunn 2 row 4
11 0 ¢0 column 28 row 2
11 G0 0 column 7 row 2
11 G0 %0 column 20 row 2
11 0 %0 column 28 row 2
11 60 +0 colunn 9 row 3
11 69 49 colunn 30 rom 2whideCTRUE),
¢(anput(PaN_ 81)
days
sréay > 7)
day = 15
ed gotony(6, 2)5
sudteh(day){
‘case 11 Led_pute(” Sunday“;
cease 2: ledpute(” onday °)}
case 31 ledpute(” Tuesday “4
cease 4 Led pute("wednescay")3
case 5: Led_pute("Thursday “);
cease 6: Ledpute(” Friday “3
cease 7: Led putec” saturday"):
Le(dnput(Pan_99) == @)
breaks
delay #5(200);
?
led gotoxy(9, 1)
led_pute(“Date:");
Led_gotoxy(6, 203
1ed_pute( 5
elay_ns(200);
whi e(TRUE)<
¢(Anput(PIN 81) == 8)
datenes
ardate > 31)
date = 1;
1ed_gotory (2, 2)5
print#(led pute,"to2u", date);
89)
delay. #s(280);
?
led gotoxy(8, 1)
ed putec"Honth”)s
Led_gotory(2®, 2);
11 60 0 colunn 6 row 2
breaks
breaks
breaks
breaks
breaks)
colunn 9 row 3
11 Go t0 colunn 18 row 2
11 60 40 colunn 8 row 3
11 Go $0 colunn 10 rom 2led_pute(” ");
elay_ns(200);
whie(TRUED(
(Input (PIN81) => €)
se(ponth > 12)
Led_gotoxy(t8, 2); 11 Go to column 10 rou 2
printé(1ed pute,"2e2u", month);
(input (PIN 88) == 8)
breaks
delays (280);
Led gotoxy(8, 1);
led_pute(" Year");
led_gotoxy(9, 2):
aed pute("20
elay_ns(200);
whi eCTAUED,
F(Amput(PIN_92) == 0)
Letyear > 99)
year = 6;
ted _gotoxy(1, 2);
prince(1ed pute, "se2u", year):
i Cinput(PIN_88) == 6){
11 Convert decimal to 8D
ninute = ((minute/s0) << 4) + (minute X 10);
((hour/18) << 4) + (hour % 20)3
((date/18) << 4) + (date % 18);
oath = ((eorth/18) ce 4) + (aonth % 20);
year = ((year/i) << a) + (year % 18);
11 End conversion
441307 write(a, minute);
1307 wrSte(2, hour);
as1307_wrste(3, 4ay)3
as1307_wrste(e, date);41307 write(s, month);
4s1307_wrsta(6, year);
1207 wrste(®, @);,
delay_s(20e) 5
breaks
delay_1s(200);
?
?
f(anpue(PIN.B2) == @)¢
ed_gotory(24, 13
led_pute( ALARM: "5
ledgotony(2t, 25
1ed_pute( vs
elay_15(200);
ns Le(TRUEY(
4s1287_read();
451307 ¢isplay();
AFCanput(PIN_B1) = 0)
slarm_statusies
se(alarna_status)(
led gotory (32, 2);
ed_pute("oN ");)
else(
Led_gotony(38, 2);
ed_pute("0**")5)
AFCEnpat(PIN_82) == 0)
breaks
delay_ns(200);)
s¢(alarat_status)(
Led_gotoxy(21, 1);
ed_pute(” —“ALawe winutes:");
ed_gotony(24, 2)5
1ed_putec %
elay_es (280);
whi LeCTRUED,
4s1387_read();41307 display);
(anpit(P1N 61)
A¢(alarmi_ninute > 59)
slarm_pinute = 0;
1ed_gotony(3®, 2)5
print#(ted_pute,"1o20", alarat_minute)s
4¢(Anput(PIN_82) == 0)
breaks
delay. es(280);
led_gotory(2t, 193
ed_putec” "ALARA Hours “)s
1ed_gotoxy(21, 2);
1ea_pute( »
elay_es(200);
hs e(TRUED(
4s1307_read();
41307 ¢8splay();
S¢(Snpot(PIN_81) == 0)
se(alarnd_tour > 23)
ed_gotoxy(3e, 2);
princé(1ed pute, "se2u", alarad hur);
i Cinput(PIN_82) == @)
breaks
delay. (200);
»
ed_gotoy(21, 1);
Acd_pute( name: "5
Led_gotoxy(21, 2);
cd_pute vs
elay_es(280);
hi LeCTRUE
451387 read();
4s1307a8splay0)54(anpt(P2n 81)
s¢(alarnd_status)(
ed gotony(38, 25
led_pute("0N ")5)
eset
Led_gotory(38, 2)3
ed_pute("0°F");)
s¢(anput(PIn 82)
breaks
elay_ns(20e);)
ir(atarma_status){
ted_gotony(2h, 13
ed_pute(” “ALARM? Minutes:—)s
1ed_gotoxy(21, 2);
ed_pute(” ”
elay_es(200);
hi e(TRUED(
4s1307_read();
451307 display);
s¢(Smpat (PIN.81) == @)
As(alarmz_ninute > 59)
slarm_einute = @;
ed_gotory(3, 2);
printed pute, "#620", alara2 minute):
A CEnput(PIN 82) == 8)
breaks
delay_5(200);
Led _gotoxy(21, 1);
ed_pute(” "ALAR Hours");
ed_gotony(24, 2)5
ed_putec »
delay_es (280);
whi LeCTRUED,
4s1387_read();41307 display);
(anpit (PIN 61)
slarm2_poures;
A¢(atarm®2 four > 23)
slarm2_pour = @5
1ed_gotory(3®, 2)5,
print#(ted_pute,"to2u", alaraz hour);
4¢(Anput(PIN_82) == 0)
breaks
delay es(280);
»
delay_es (280);
}
i(input(IN_p1) == 0)¢
utput_low(PIN_65);
pum of (3
?
4s2307_re200)3 11 Read data From 031307 ATCC
alare_check(); U1 Check 3 there 4s an alaon
451307_¢isplayOs 11 Osaplay ine and calendar
alone a8splay()s 11 Ossplay alares
delay ns(50)3
>
Posted by Electron Note a 10:97:00 PM ous
labels: BS1307 RTC, LCD, LED, PICIGFS77A, PWM.
‘Saturday, Seplomber 3, 2016
DHT11 VS DHT22
Comparison between DHT (RHTox) and DHT22 (AM23o2 - RHTo3) sensorsDHT: (RHTos) and DHT2a (AMago2-RHTog) ate relative humidity and temperature sensors. These sensors
ae smal, cheap end easy to use. They use only one wire to communicate with mew
The following wo topie shows how to interface PIC:6F877A with DHT 11 and DHT22 sensors respectively
Interfacing DHT relative humidity and temperature sensor with PICIGF877A mierocontroller
Interfacing PICIOF877A with DHT22(AM2302-RHTog) sensor using CCS PICC
The following able shows the DHT sensor characterises (rom DHT datasheet)PI Pc nC EC Cam RCNERIA
[conditions
Repeatability
‘ecuracy we
0500 =
Interchangeabity | Fully interchangeable
Memsurement [OC so Soa
Range Fi ORR ‘RH
soc OR aOR
Response Time | 1/e(63K)250, | 68 105 35S
{Seconds} am/s he
Hysteresis wR
Tong-Term | Tyna EIR year
stability
Tempersiure|
Resolution ¥ ie ie
ear co ea
Repeal zi
‘ecoraey He Frid
Measurement oe so
Range
Response Tine | Ue(63H) 6S 305
{Seconds)
‘And the following table shows DIfTa2 (AM2go2) tehnial specification from AM2g02 datashoc)
replepetit noneace PI Pc nC EC Cam RCNERIA
Model AMIS02
Power supply 33.8.5V DC
Ontpat sig digital signal via wire bus
‘Operting a
Acourney Tbumsidity -296RI Max TORII), _temperanare =-05Celsius
Revolution or sensitivity | humidity 0-1°0RH: Temperanare O:1Celsins
Repeatability Tamil 196 reaperature ~02Celsivs
Hnmidity hysteresis | 1-0.30RH
Lougetenm Sibiity | 1-0.S%Roar
Inotehangenbsity Tally interchangeable
“The diferences between the two sensors are summarized inthe following table
Data DiTas
[Relative Humidity Operating Range| 20 ~ 90% = 100%
Relative Humidity Accuracy | =5%RH__(22%RH (Max 5%)
‘Humidity Resolution 1%5RHL ‘OaRHL
Humidity Repeatability =URIT SRI
“Humidity Hysteresis INRA 0.3%RH
Long-term Stability ([EMRH/year| _20.54RH/year
“Temperature Operating Range _|_0~ 50°C =40 = 80°C
“Temperature Accuracy. 22°C 405°C
Res
ion re ox°C
Temper
DHTu: and DHT22 interfacing with PICIGF877A microcontroller:
“The following cruit schematic shows the comparison between DITT11 and DHT22 sensors connection iret
replepetit noneace PI Pc nC EC Cam RCNERIA
FRETS WERE
‘The microcontroller used to make the comparison between DIT and DHT22 is PICIGF877A which runs with
ceystalosllatr at MHz
The sensor DIT aswell as DITT22 has 4 ps which are
Ver: Posve power supply (58)
Data :Sensor data input and output
NC: Not connected termina
Gnd Ground (0.
pull-up resistor must be added berween the data pin ofeach sensor and VOC (45V) pin as shown inthe dca
schematic 4.7K ~ 108)
2004 (20x4) LCD display i used to display temperature and humidity results for both sensors,
(CCS C code for DHT vs DHT 22 project:
/ ATI VS DaT22 CCS C code
1 netps/ eesplce blogspot.con/
7 electronnotedgnai.con
1.60 nodule conections
define LCO_RS_PIN PIN_O8
repleepetit screwLCO_DATAS PLN_O4
LD_PATRG PINDS
/Ené CO nodule conned
include <26F877A.n>
har messagea[] = "ovT3i Temp =
har messagea{] * "DHTIL RH =
har messagea[] * “o#T22 Temp =
har messagea{] = "DHT22 RH =
mnsigned Ants Teno, tH
delay (25);
utput_pigh(OHTat) ;
delay_35(30)3
foutput_#2oae(onTt2);
hort de23_eheck_response()(
delay_vs(4e);
A (Linput (0431) )
Gelay_us(80);
LFCanput (onraay)<
ely s(50);
uw
"
1
1
fon pin between ONT22 and meu
configure connaction pin 3s output
Connection pin output low
Connection pin output high
ead and test AF connection pin $5 low>
>
Lisigned int# ahta3_Reas_oata()<
Unsigned intB 4, ky data = 05 Wks
s¢(Tine_out)
for(i = 8; 1 € 8; HDC
kee;
wwhse(HInput(ant33))¢
bees
se(k > 10004
>
felay_us(a)s
?
Gelay_us(30);
F(a imput ants)
bit_elear(data, (7 - 9)3
else
bit_set(_data, (7 - 19);
nt e¢anpue(ahena))(
Fk > 200)¢
y
fetay_us(a);}
?
>
return data;
Froid aht22_start_signal(¢
‘utput_orsve(OHT22);
output low(o4T22);
11 Wait For OHT22 pin to go high
11 Caear bit (7 = 4)
Uy set tt (7-1)
11 wait for OHT22 pin vo go low
11 Configure connection pin 2s output
11 Connection pin output. Low[ detay.ns(25);
‘utput_nagn(onT22);
delay_15(38);
tput_float(OHT22);,
Lnort dnt2z_check_response(){
‘delay_vs (0);
(Langue (0aT22))
elay_us(se);
SF Canput (0722)
delay_us(80);
return 1;
lunsignes ante 4, ky
3 (Tine_out)
for(h = 85 18; 144)
winite(input_state(aht22) 4
A(& > 10894
Tine out = 2;
break
d
feloy_vs(a)s
?
elay_us(30);
4 ¢(44nput_stave(dne22))
bit_clear(_data, (7 - ));
else
bit_set(_data, (7 - 4))5
winLle¢input_state¢aht22)){
i(k > 200)¢
11 Connection pin output high
11 Configure connection pin as Snput
11 Read and test 4¢ connection pin 4s Low
1 Rasa and test 4f conection pin Ss high
11 & 4s used to count 2 bit reading curat}on
11 tai for 0422 pin to go high
1) caear bit (7 = 4)
U1 set it (7-1)
11 ost for OAT22 pin £0 go LowTine out
break;
)
delay usta);
?
>
ed_putet\0)5
detay_ns(2000);
wnine(TAUE)
antdi_start_signal(s
4 F{dnt21_check_nesponse())<
Ry pytan = ofta_tead_oata();
fvCbyted = dread Read oata();
Bytel = dnt23_Fead_bata();
Tlpyte2 = dntsa_ Read Data();
checksum = dnt4_Read_Data()s
S¢(Tine out)
ed gotoxy(2,
ed pute(” OWT: Tine out! "5
Led_gotoxy(2, 2):
ed_pute( Dv:
y
else
rnessaget(13] = Tlayte1/i0 + 48
ressagel{1é] = Taytermia + 43;
rnessagel[16] = T_Sytez/10 + 48;
rnessagez[13] = AM Bytet/30 + 43;
rmessage2(i4] = RH Bytettio + 43;
Inessage2(16) ~ RH Byte2/20 + 48;
get[17] = 223;
Ted gotoxy(t, 195
(Checksum == ((RH_Byten + RH_ByteD + T Byker + TLoyte2) & OxFFD)G
11 tnitéatize cD module
1 Woo clear
there 1s 2 sesponse fron dheat senso
1/ read Ri byeen
1) read RH byred
11 v0ad T byte
U1 read T byte
17 read checksum
11 TF reading tates long tine
11 Go to coluan 1 row 1
11 0 0 colunn 1 row
W/ Clear second row
11 egree synbo}
11 60 9 coluan 1 row 1print#(1ca_pute, sessaget);
Led_gotory(, 2)5
print(1ce pute, nessage2);
)
else(
led gotory(t, 195
ed_pute("DHT11 Checksum Eenor")s
Led_gotoxy(2, 2);
ed_pute("™ .
?
)
d
ise {
ed gotoxy(t, 105
led_pute(” "no response ")s
Led_gotoxy(, 2);
Aed_pute(” fron DuTa1 sensor
?
Tine_out = 8;
ant22_Start_signal(s
{¢(anez2_eheek response()
Ri_pyter = dbt22_Read_oata()s
ibyted = ant22_Read_oata();
checksum = dnt23_Read_oata()
Af(Tive_out){
ed gotoxy (2, 1)
led_putet” 01122 Tine out! °)3
tee gotoxy(2a, 2);
ted_pute( ds
>
ease
RA = RL bytet;
f= (RH ce 8) | RA_byte2s
11 visplay messages
11 60 to colunn 1 row 2
11 Display message
11 60 0 coluan 1 row 2
11 Go to coluan 1 row 2
sar second row
1-1F dne24 sensor don't respond
11 60 0 coluan 1 row 1
11 co to coluan 1 row 2
11, TF there is 2 response from sen
11 read RW byter
11 r0ad Ri byte
read T byte
1 read T byte
W/ read checksum
11 1F reading takes long tine
11 60 0 coluan 1 row
U1 Go to coluan 1 row 4
1 Clear fou
“(Checksum == ((RH_Bytel + RH Byte2 + TBytet + Tbyte2) & &xFF))(eee
2¢(Temp > ex8000)
essages[12] = '-'s
fmp = Tenp & GXIFFF; }
nessages(32] = "5
rnessage3[13] = (Temp / 200) % 10 + a
rnessages[M4] = (Temp / 20) % 10 + 48;
inessages[16] = Teng X10 + 45;
rmessagec(i3] = (RH / 100) % 10 + 48;
Inessages[ié] ~ (RH / 18) X10 + 48;
inessages{16] = RH ¥ 10 + 485
rnessage3(17] = 2233
1ed_gotory(21, 2)5
print#(1ea_pute, messages);
led_gotoy(2, 2);
print(1ea pute, messages);
>
else(
ed_gotony(24, 43
Led-pute(“OHT22 Checksum Error“):
1ed_gotory(21, 2);
red_pute( os
y
?
else {
ed_gotoxy(22, 13
Led_pute(” "No response ")s
otoxy(2t, 2);
Aed_pute(” fron D422 sensor *)s
delay_ns(2000);
)
11 End of progran
DIT V8 DITT22 Video:
11 0@ree symbo
11 60 9 coluan 1 row 3
11 Display messages
11 co to coluan 1 row 4
11 oisplay messages
11 60 to coluan 1 row 3
11 a to coluan 1 roe 4
1 Clear fourth row
1/16 8822 sensor don’t respond
1/69 to colunn 1 row 3
11 a to coluan 1 row 4The following video shows the comparison between the two sensors results.