uni

University stuff
git clone git://git.margiolis.net/uni.git
Log | Files | Refs | README | LICENSE

commit 48e4ee26bb002b269a49a90c81dd94ba30ede76c
parent 57c9c092e7cebd82c55d3e204cfc087a0d2d61e2
Author: Christos Margiolis <christos@margiolis.net>
Date:   Wed,  2 Feb 2022 19:52:11 +0200

done

Diffstat:
Mc_embedded/doc/doc.pdf | 0
Mc_embedded/doc/doc.tex | 69++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------
Ac_embedded/doc/doc.toc | 9+++++++++
Ac_embedded/doc/res/button.jpg | 0
Ac_embedded/doc/res/main.jpg | 0
Ac_embedded/doc/res/pickit.jpg | 0
Ac_embedded/doc/res/side.jpg | 0
Dc_embedded/schem/pic.kicad_pcb | 1-
Mc_embedded/schem/pic.pro | 247++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------
Mc_embedded/schem/pic.sch | 16++++++++--------
Mc_embedded/schem/pic.sch-bak | 8++++++++
Mc_embedded/src/Makefile | 2+-
Mc_embedded/src/bme280.c | 50+++++++++++++++++++++++++++-----------------------
Mc_embedded/src/bme280.h | 6+++---
Mc_embedded/src/i2c.c | 2+-
Mc_embedded/src/i2c.h | 1+
Mc_embedded/src/main.c | 26++++++++++++++++++--------
Mc_embedded/src/tmr0.c | 8++++----
Mc_embedded/src/tmr0.h | 4++--
19 files changed, 362 insertions(+), 87 deletions(-)

diff --git a/c_embedded/doc/doc.pdf b/c_embedded/doc/doc.pdf Binary files differ. diff --git a/c_embedded/doc/doc.tex b/c_embedded/doc/doc.tex @@ -104,30 +104,69 @@ εύρος θερμοκρασίας λειτουργίας, το σύστημα είναι ασφαλές να λειτουργήσει στους $\SI{-20}{\celsius} \sim \SI{70}{\celsius}$. -\subsection{Διάρκεια ζωής} - -<++> - \section{Ανάπτυξη συστήματος} -\subsection{Εργαλεία} -Η ανάπτυξη του συστήματος έγινε σε FreeBSD 13.0 με την χρήση του -\lstinline{sdcc} C compiler, και το \lstinline{pk2cmd} για την -επικοινωνία του μικροελεγκτή με το PICKit2 -- τον προγραμματιστή. -'Εχω αναλύσει την διαδικασία αυτή σε μορφή οδηγού στην ιστοσελίδα -μου και στο FreeBSD Wiki: https://wiki.freebsd.org/Microcontrollers/PIC + +Στο directory \lstinline{schem/} βρίσκονται όλα τα αρχεία +σχετικά με το σχηματικό (αρχεία KiCAD και PDF). \subsection{Σχηματικό} \begin{center} \includegraphics[width=\linewidth]{./res/schem.png} \end{center} -\subsection{PCB} -<++> - \subsection{Κώδικας} -<++> +Η ανάπτυξη του κώδικα έγινε σε λειτουργικό FreeBSD 13.0 με την χρήση του +\lstinline{sdcc} C compiler, και το \lstinline{pk2cmd} για την +επικοινωνία του μικροελεγκτή με το PICKit2 -- τον προγραμματιστή. +'Εχω αναλύσει την διαδικασία αυτή σε μορφή οδηγού στην ιστοσελίδα +μου και στο FreeBSD Wiki: https://wiki.freebsd.org/Microcontrollers/PIC. + +Στο directory \lstinline{src/} βρίσκεται όλος ο κώδικας του συστήματος. +Το \lstinline{Makefile} είναι υπεύθυνο για την μεταγλώττιση και φόρτωση +του προγράμματος στο PIC. Η επιλογές που μπορούν να δωθούν στο \lstinline{Makefile} +είναι οι εξής: + +\begin{itemize} + \item Καμία επιλογή: Μεταγλωττίζει το πρόγραμμα. + \item \lstinline{clean}: Καθαρίζει το directory από τα αρχεία + που παράγει ο compiler. + \item \lstinline{erase}: Διαγράφει την μνήμη του PIC. + \item \lstinline{flash}: Φορτώνει το πρόγραμμα στο PIC. +\end{itemize} + +Στον κώδικα έχει υλοποιηθεί ο Timer0 του PIC, driver για την οθόνη LCD, +υλοποιήση του προτοκώλλου I$^2$C και driver για τον σένσορα BME280. 'Εχει +υλοποιηθεί μόνο ό,τι είναι απολύτως απαραίτητο για το σύστημα. + +Αν και στην εκφώνηση αναφέρεται ότι ο σένσορας θερμοκρασίας και υγρασίας +πρέπει να είναι αναλογικός, χρησιμοποίησα ψηφιακό, λόγω του ότι στο μαγαζί +όπου προμηθεύτηκα τα υλικά δεν υπήρχε αναλογικός σένσορας σε απόθεμα. \subsection{Εικόνες} -<++> + +\begin{figure}[H] + \centering + \rotatebox{270}{\includegraphics[width=\linewidth]{res/main.jpg}} + \caption{Βασική λειτουργία.} +\end{figure} + + +\begin{figure}[H] + \centering + \includegraphics[width=\linewidth]{res/side.jpg} + \caption{Πλάγια λήψη.} +\end{figure} + +\begin{figure}[H] + \centering + \rotatebox{270}{\includegraphics[width=\linewidth]{res/button.jpg}} + \caption{Λειτουργία κατά την πίεση του κουμπιού.} +\end{figure} + +\begin{figure}[H] + \centering + \rotatebox{270}{\includegraphics[width=\linewidth]{res/pickit.jpg}} + \caption{Προγραμματισμός του συστήματος με το PICKit2.} +\end{figure} \end{document} diff --git a/c_embedded/doc/doc.toc b/c_embedded/doc/doc.toc @@ -0,0 +1,9 @@ +\select@language {english} +\contentsline {section}{\numberline {1}\IeC {\TextOrMath {\textPi }{\mathPi }}\IeC {\TextOrMath {\textlambda }{\mathlambda }}\IeC {\TextOrMath {\texteta }{\matheta }}\IeC {\TextOrMath {\textrho }{\mathrho }}\IeC {\textomicron }\IeC {\TextOrMath {\textphi }{\mathphi }}\IeC {\textomicron }\IeC {\TextOrMath {\textrho }{\mathrho }}\IeC {\acctonos \textiota }\IeC {\TextOrMath {\textepsilon }{\mathepsilon }}\IeC {\TextOrMath {\textvarsigma }{\mathvarsigma }}}{2} +\contentsline {subsection}{\numberline {1.1}\IeC {\TextOrMath {\textLambda }{\mathLambda }}\IeC {\acctonos \textiota }\IeC {\TextOrMath {\textsigma }{\mathsigma }}\IeC {\TextOrMath {\texttau }{\mathtau }}\IeC {\TextOrMath {\textalpha }{\mathalpha }} \IeC {\TextOrMath {\textupsilon }{\mathupsilon }}\IeC {\TextOrMath {\textlambda }{\mathlambda }}\IeC {\TextOrMath {\textiota }{\mathiota }}\IeC {\TextOrMath {\textkappa }{\mathkappa }}\IeC {\acctonos \textomega }\IeC {\TextOrMath {\textnu }{\mathnu }} -- BOM}{2} +\contentsline {subsection}{\numberline {1.2}\IeC {\textKappa }\IeC {\acctonos \textomicron }\IeC {\TextOrMath {\textsigma }{\mathsigma }}\IeC {\TextOrMath {\texttau }{\mathtau }}\IeC {\textomicron }\IeC {\TextOrMath {\textvarsigma }{\mathvarsigma }} \IeC {\TextOrMath {\textpi }{\mathpi }}\IeC {\TextOrMath {\textalpha }{\mathalpha }}\IeC {\TextOrMath {\textrho }{\mathrho }}\IeC {\TextOrMath {\textalpha }{\mathalpha }}\IeC {\TextOrMath {\textgamma }{\mathgamma }}\IeC {\TextOrMath {\textomega }{\mathomega }}\IeC {\TextOrMath {\textgamma }{\mathgamma }}\IeC {\acctonos \texteta }\IeC {\TextOrMath {\textvarsigma }{\mathvarsigma }}}{2} +\contentsline {subsection}{\numberline {1.3}\IeC {\TextOrMath {\textTheta }{\mathTheta }}\IeC {\TextOrMath {\textepsilon }{\mathepsilon }}\IeC {\TextOrMath {\textrho }{\mathrho }}\IeC {\TextOrMath {\textmu }{\mathmu }}\IeC {\textomicron }\IeC {\TextOrMath {\textkappa }{\mathkappa }}\IeC {\TextOrMath {\textrho }{\mathrho }}\IeC {\TextOrMath {\textalpha }{\mathalpha }}\IeC {\TextOrMath {\textsigma }{\mathsigma }}\IeC {\acctonos \textiota }\IeC {\TextOrMath {\textepsilon }{\mathepsilon }}\IeC {\TextOrMath {\textvarsigma }{\mathvarsigma }} \IeC {\TextOrMath {\textlambda }{\mathlambda }}\IeC {\TextOrMath {\textepsilon }{\mathepsilon }}\IeC {\TextOrMath {\textiota }{\mathiota }}\IeC {\TextOrMath {\texttau }{\mathtau }}\IeC {\textomicron }\IeC {\TextOrMath {\textupsilon }{\mathupsilon }}\IeC {\TextOrMath {\textrho }{\mathrho }}\IeC {\TextOrMath {\textgamma }{\mathgamma }}\IeC {\acctonos \textiota }\IeC {\TextOrMath {\textalpha }{\mathalpha }}\IeC {\TextOrMath {\textvarsigma }{\mathvarsigma }}}{3} +\contentsline {section}{\numberline {2}\IeC {\textAlpha }\IeC {\TextOrMath {\textnu }{\mathnu }}\IeC {\acctonos \textalpha }\IeC {\TextOrMath {\textpi }{\mathpi }}\IeC {\TextOrMath {\texttau }{\mathtau }}\IeC {\TextOrMath {\textupsilon }{\mathupsilon }}\IeC {\TextOrMath {\textxi }{\mathxi }}\IeC {\TextOrMath {\texteta }{\matheta }} \IeC {\TextOrMath {\textsigma }{\mathsigma }}\IeC {\TextOrMath {\textupsilon }{\mathupsilon }}\IeC {\TextOrMath {\textsigma }{\mathsigma }}\IeC {\TextOrMath {\texttau }{\mathtau }}\IeC {\acctonos \texteta }\IeC {\TextOrMath {\textmu }{\mathmu }}\IeC {\TextOrMath {\textalpha }{\mathalpha }}\IeC {\TextOrMath {\texttau }{\mathtau }}\IeC {\textomicron }\IeC {\TextOrMath {\textvarsigma }{\mathvarsigma }}}{3} +\contentsline {subsection}{\numberline {2.1}\IeC {\TextOrMath {\textSigma }{\mathSigma }}\IeC {\TextOrMath {\textchi }{\mathchi }}\IeC {\TextOrMath {\texteta }{\matheta }}\IeC {\TextOrMath {\textmu }{\mathmu }}\IeC {\TextOrMath {\textalpha }{\mathalpha }}\IeC {\TextOrMath {\texttau }{\mathtau }}\IeC {\TextOrMath {\textiota }{\mathiota }}\IeC {\TextOrMath {\textkappa }{\mathkappa }}\IeC {\acctonos \textomicron }}{3} +\contentsline {subsection}{\numberline {2.2}\IeC {\textKappa }\IeC {\acctonos \textomega }\IeC {\TextOrMath {\textdelta }{\mathdelta }}\IeC {\TextOrMath {\textiota }{\mathiota }}\IeC {\TextOrMath {\textkappa }{\mathkappa }}\IeC {\TextOrMath {\textalpha }{\mathalpha }}\IeC {\TextOrMath {\textvarsigma }{\mathvarsigma }}}{3} +\contentsline {subsection}{\numberline {2.3}\IeC {\textEpsilon }\IeC {\TextOrMath {\textiota }{\mathiota }}\IeC {\TextOrMath {\textkappa }{\mathkappa }}\IeC {\acctonos \textomicron }\IeC {\TextOrMath {\textnu }{\mathnu }}\IeC {\TextOrMath {\textepsilon }{\mathepsilon }}\IeC {\TextOrMath {\textvarsigma }{\mathvarsigma }}}{5} diff --git a/c_embedded/doc/res/button.jpg b/c_embedded/doc/res/button.jpg Binary files differ. diff --git a/c_embedded/doc/res/main.jpg b/c_embedded/doc/res/main.jpg Binary files differ. diff --git a/c_embedded/doc/res/pickit.jpg b/c_embedded/doc/res/pickit.jpg Binary files differ. diff --git a/c_embedded/doc/res/side.jpg b/c_embedded/doc/res/side.jpg Binary files differ. diff --git a/c_embedded/schem/pic.kicad_pcb b/c_embedded/schem/pic.kicad_pcb @@ -1 +0,0 @@ -(kicad_pcb (version 4) (host kicad "dummy file") ) diff --git a/c_embedded/schem/pic.pro b/c_embedded/schem/pic.pro @@ -1,29 +1,10 @@ -update=Mon Jan 31 23:37:10 2022 +update=Wed Feb 2 15:50:17 2022 version=1 last_client=kicad [general] version=1 RootSch= BoardNm= -[pcbnew] -version=1 -LastNetListRead= -UseCmpFile=1 -PadDrill=0.600000000000 -PadDrillOvalY=0.600000000000 -PadSizeH=1.500000000000 -PadSizeV=1.500000000000 -PcbTextSizeV=1.500000000000 -PcbTextSizeH=1.500000000000 -PcbTextThickness=0.300000000000 -ModuleTextSizeV=1.000000000000 -ModuleTextSizeH=1.000000000000 -ModuleTextSizeThickness=0.150000000000 -SolderMaskClearance=0.000000000000 -SolderMaskMinWidth=0.000000000000 -DrawSegmentWidth=0.200000000000 -BoardOutlineThickness=0.100000000000 -ModuleOutlineThickness=0.150000000000 [cvpcb] version=1 NetIExt=net @@ -37,7 +18,231 @@ PageLayoutDescrFile=layout.kicad_wks PlotDirectoryName=../doc/ SubpartIdSeparator=0 SubpartFirstId=65 -NetFmtName= +NetFmtName=Pcbnew SpiceAjustPassiveValues=0 LabSize=50 ERC_TestSimilarLabels=1 +[pcbnew] +version=1 +PageLayoutDescrFile= +LastNetListRead=pic.net +CopperLayerCount=2 +BoardThickness=1.6 +AllowMicroVias=0 +AllowBlindVias=0 +RequireCourtyardDefinitions=0 +ProhibitOverlappingCourtyards=1 +MinTrackWidth=0.2 +MinViaDiameter=0.4 +MinViaDrill=0.3 +MinMicroViaDiameter=0.2 +MinMicroViaDrill=0.09999999999999999 +MinHoleToHole=0.25 +TrackWidth1=0.25 +ViaDiameter1=0.8 +ViaDrill1=0.4 +dPairWidth1=0.2 +dPairGap1=0.25 +dPairViaGap1=0.25 +SilkLineWidth=0.12 +SilkTextSizeV=1 +SilkTextSizeH=1 +SilkTextSizeThickness=0.15 +SilkTextItalic=0 +SilkTextUpright=1 +CopperLineWidth=0.2 +CopperTextSizeV=1.5 +CopperTextSizeH=1.5 +CopperTextThickness=0.3 +CopperTextItalic=0 +CopperTextUpright=1 +EdgeCutLineWidth=0.05 +CourtyardLineWidth=0.05 +OthersLineWidth=0.15 +OthersTextSizeV=1 +OthersTextSizeH=1 +OthersTextSizeThickness=0.15 +OthersTextItalic=0 +OthersTextUpright=1 +SolderMaskClearance=0 +SolderMaskMinWidth=0 +SolderPasteClearance=0 +SolderPasteRatio=0 +[pcbnew/Layer.F.Cu] +Name=F.Cu +Type=0 +Enabled=1 +[pcbnew/Layer.In1.Cu] +Name=In1.Cu +Type=0 +Enabled=0 +[pcbnew/Layer.In2.Cu] +Name=In2.Cu +Type=0 +Enabled=0 +[pcbnew/Layer.In3.Cu] +Name=In3.Cu +Type=0 +Enabled=0 +[pcbnew/Layer.In4.Cu] +Name=In4.Cu +Type=0 +Enabled=0 +[pcbnew/Layer.In5.Cu] +Name=In5.Cu +Type=0 +Enabled=0 +[pcbnew/Layer.In6.Cu] +Name=In6.Cu +Type=0 +Enabled=0 +[pcbnew/Layer.In7.Cu] +Name=In7.Cu +Type=0 +Enabled=0 +[pcbnew/Layer.In8.Cu] +Name=In8.Cu +Type=0 +Enabled=0 +[pcbnew/Layer.In9.Cu] +Name=In9.Cu +Type=0 +Enabled=0 +[pcbnew/Layer.In10.Cu] +Name=In10.Cu +Type=0 +Enabled=0 +[pcbnew/Layer.In11.Cu] +Name=In11.Cu +Type=0 +Enabled=0 +[pcbnew/Layer.In12.Cu] +Name=In12.Cu +Type=0 +Enabled=0 +[pcbnew/Layer.In13.Cu] +Name=In13.Cu +Type=0 +Enabled=0 +[pcbnew/Layer.In14.Cu] +Name=In14.Cu +Type=0 +Enabled=0 +[pcbnew/Layer.In15.Cu] +Name=In15.Cu +Type=0 +Enabled=0 +[pcbnew/Layer.In16.Cu] +Name=In16.Cu +Type=0 +Enabled=0 +[pcbnew/Layer.In17.Cu] +Name=In17.Cu +Type=0 +Enabled=0 +[pcbnew/Layer.In18.Cu] +Name=In18.Cu +Type=0 +Enabled=0 +[pcbnew/Layer.In19.Cu] +Name=In19.Cu +Type=0 +Enabled=0 +[pcbnew/Layer.In20.Cu] +Name=In20.Cu +Type=0 +Enabled=0 +[pcbnew/Layer.In21.Cu] +Name=In21.Cu +Type=0 +Enabled=0 +[pcbnew/Layer.In22.Cu] +Name=In22.Cu +Type=0 +Enabled=0 +[pcbnew/Layer.In23.Cu] +Name=In23.Cu +Type=0 +Enabled=0 +[pcbnew/Layer.In24.Cu] +Name=In24.Cu +Type=0 +Enabled=0 +[pcbnew/Layer.In25.Cu] +Name=In25.Cu +Type=0 +Enabled=0 +[pcbnew/Layer.In26.Cu] +Name=In26.Cu +Type=0 +Enabled=0 +[pcbnew/Layer.In27.Cu] +Name=In27.Cu +Type=0 +Enabled=0 +[pcbnew/Layer.In28.Cu] +Name=In28.Cu +Type=0 +Enabled=0 +[pcbnew/Layer.In29.Cu] +Name=In29.Cu +Type=0 +Enabled=0 +[pcbnew/Layer.In30.Cu] +Name=In30.Cu +Type=0 +Enabled=0 +[pcbnew/Layer.B.Cu] +Name=B.Cu +Type=0 +Enabled=1 +[pcbnew/Layer.B.Adhes] +Enabled=1 +[pcbnew/Layer.F.Adhes] +Enabled=1 +[pcbnew/Layer.B.Paste] +Enabled=1 +[pcbnew/Layer.F.Paste] +Enabled=1 +[pcbnew/Layer.B.SilkS] +Enabled=1 +[pcbnew/Layer.F.SilkS] +Enabled=1 +[pcbnew/Layer.B.Mask] +Enabled=1 +[pcbnew/Layer.F.Mask] +Enabled=1 +[pcbnew/Layer.Dwgs.User] +Enabled=1 +[pcbnew/Layer.Cmts.User] +Enabled=1 +[pcbnew/Layer.Eco1.User] +Enabled=1 +[pcbnew/Layer.Eco2.User] +Enabled=1 +[pcbnew/Layer.Edge.Cuts] +Enabled=1 +[pcbnew/Layer.Margin] +Enabled=1 +[pcbnew/Layer.B.CrtYd] +Enabled=1 +[pcbnew/Layer.F.CrtYd] +Enabled=1 +[pcbnew/Layer.B.Fab] +Enabled=1 +[pcbnew/Layer.F.Fab] +Enabled=1 +[pcbnew/Layer.Rescue] +Enabled=1 +[pcbnew/Netclasses] +[pcbnew/Netclasses/Default] +Name=Default +Clearance=0.2 +TrackWidth=0.25 +ViaDiameter=0.8 +ViaDrill=0.4 +uViaDiameter=0.3 +uViaDrill=0.1 +dPairWidth=0.2 +dPairGap=0.25 +dPairViaGap=0.25 diff --git a/c_embedded/schem/pic.sch b/c_embedded/schem/pic.sch @@ -25,10 +25,10 @@ F 3 "~" H 5300 3000 50 0001 C CNN 0 -1 -1 0 $EndComp $Comp -L power:GNDREF GND +L power:GNDREF #GND0101 U 1 1 61EB279A P 5450 2150 -F 0 "GND" H 5538 2113 50 0000 L CNN +F 0 "#GND0101" H 5538 2113 50 0000 L CNN F 1 "GNDREF" H 5455 1977 50 0001 C CNN F 2 "" H 5450 2150 50 0001 C CNN F 3 "" H 5450 2150 50 0001 C CNN @@ -72,10 +72,10 @@ F 6 "dc(1)" H 6130 1459 50 0001 L CNN "Spice_Model" -1 0 0 1 $EndComp $Comp -L power:GNDREF GND +L power:GNDREF #GND0102 U 1 1 61F5552B P 6550 6300 -F 0 "GND" H 6638 6263 50 0000 L CNN +F 0 "#GND0102" H 6638 6263 50 0000 L CNN F 1 "GNDREF" H 6555 6127 50 0001 C CNN F 2 "" H 6550 6300 50 0001 C CNN F 3 "" H 6550 6300 50 0001 C CNN @@ -134,10 +134,10 @@ $EndComp Wire Wire Line 6650 2700 6650 2450 $Comp -L Display_Character:NHD-0420H1Z LCD +L Display_Character:NHD-0420H1Z LCD1 U 1 1 61E937A6 P 4400 5200 -F 0 "LCD" V 4400 4356 50 0000 R CNN +F 0 "LCD1" V 4400 4356 50 0000 R CNN F 1 "NHD-0420H1Z" H 4400 4220 50 0001 C CNN F 2 "Display:NHD-0420H1Z" H 4400 4300 50 0001 C CNN F 3 "http://www.newhavendisplay.com/specs/NHD-0420H1Z-FSW-GBW-33V3.pdf" H 4500 5100 50 0001 C CNN @@ -283,10 +283,10 @@ F 3 "~" H 4200 3000 50 0001 C CNN 0 -1 -1 0 $EndComp $Comp -L power:GNDREF GND +L power:GNDREF #GND0103 U 1 1 61EE8AF0 P 3800 3000 -F 0 "GND" H 3888 2963 50 0000 L CNN +F 0 "#GND0103" H 3888 2963 50 0000 L CNN F 1 "GNDREF" H 3805 2827 50 0001 C CNN F 2 "" H 3800 3000 50 0001 C CNN F 3 "" H 3800 3000 50 0001 C CNN diff --git a/c_embedded/schem/pic.sch-bak b/c_embedded/schem/pic.sch-bak @@ -462,4 +462,12 @@ Wire Wire Line 7850 5800 7850 4800 Wire Wire Line 7850 4800 7450 4800 +Entry Bus Bus + 2200 2000 2300 2100 +Entry Bus Bus + 2200 2000 2300 2100 +Entry Bus Bus + 2200 2000 2300 2100 +Entry Bus Bus + 2200 2000 2300 2100 $EndSCHEMATC diff --git a/c_embedded/src/Makefile b/c_embedded/src/Makefile @@ -37,4 +37,4 @@ erase: all clean: rm -f *.hex *.asm *.cod *.hex *.lst *.o *.core -.PHONY: all options clean flash clean +.PHONY: all options clean erase flash diff --git a/c_embedded/src/bme280.c b/c_embedded/src/bme280.c @@ -12,6 +12,7 @@ #define BME280_FILTER_OFF 0x00 #define BME280_STANDBY_0_5 0x00 #define BME280_MODE_NORMAL 0x03 +#define BME280_RESET_POWERON 0xb6 #define BME280_ADDR 0xee #define BME280_CHIPID 0x60 @@ -125,8 +126,8 @@ bme280_update(void) r.a[2] = 0x00; r.a[1] = i2c_read(1); - r.a[0] = i2c_read(1); - adc_h = (r.v >> 4) & 0xffff; + r.a[0] = i2c_read(0); + adc_h = r.v & 0xffff; i2c_stop(); } @@ -136,7 +137,7 @@ bme280_init(void) { if (bme280_read8(BME280_REG_CHIPID) != BME280_CHIPID) return (-1); - bme280_write(BME280_REG_SOFTRESET, 0xb6); + bme280_write(BME280_REG_SOFTRESET, BME280_RESET_POWERON); tmr0_delay_ms(BME280_DELAY); while ((bme280_read8(BME280_REG_STATUS) & 0x01) == 0x01) tmr0_delay_ms(BME280_DELAY); @@ -163,21 +164,24 @@ bme280_init(void) (BME280_FILTER_OFF << 2)) & 0xfc); bme280_write(BME280_REG_CTL, ((BME280_SAMPLE_X1 << 5) | (BME280_SAMPLE_SKIP << 2)) | BME280_MODE_NORMAL); - + + tmr0_delay_ms(BME280_DELAY); + return (0); } +/* Black magic taken (stolen) from BME280's datasheet. */ int32_t bme280_read_temp(void) { int32_t v1, v2; bme280_update(); - v1 = ((((adc_t / 8) - ((int32_t)cal.dig_t1 * 2))) * - ((int32_t)cal.dig_t2)) / 2048; - v2 = (((((adc_t / 16) - ((int32_t)cal.dig_t1)) * - ((adc_t / 16) - ((int32_t)cal.dig_t1))) / 4096) * - ((int32_t)cal.dig_t3)) / 16384; + v1 = ((((adc_t >> 3) - ((int32_t)cal.dig_t1 << 1))) * + ((int32_t)cal.dig_t2)) >> 11; + v2 = (((((adc_t >> 4) - ((int32_t)cal.dig_t1)) * + ((adc_t >> 4) - ((int32_t)cal.dig_t1))) >> 12) * + ((int32_t)cal.dig_t3)) >> 14; t_fine = v1 + v2; @@ -187,20 +191,20 @@ bme280_read_temp(void) uint32_t bme280_read_humid(void) { - return (0); - /*int32_t v;*/ - - /*v = (t_fine - ((int32_t)76800));*/ + int32_t v = 0; - /*v = (((((adc_h * 16384) - (((int32_t)cal.dig_h4) * 1048576) -*/ - /*(((int32_t)cal.dig_h5) * v)) + ((int32_t)16384)) / 32768) **/ - /*(((((((v * ((int32_t)cal.dig_h6)) / 1024) **/ - /*(((v * ((int32_t)cal.dig_h3)) / 2048) + ((int32_t)32768))) / 1024) +*/ - /*((int32_t)2097152)) * ((int32_t)cal.dig_h2) + 8192) / 16384));*/ - - /*v = (v - (((((v / 32768) * (v / 32768)) / 128) * ((int32_t)cal.dig_h1)) / 16));*/ - /*v = (v < 0 ? 0 : v);*/ - /*v = (v > 419430400 ? 419430400 : v);*/ + bme280_update(); + v = (t_fine - ((int32_t)76800)); + + v = (((((adc_h << 14) - (((int32_t)cal.dig_h4) << 20) - + (((int32_t)cal.dig_h5) * v)) + ((int32_t)16384)) >> 15) * + (((((((v * ((int32_t)cal.dig_h6)) >> 10) * + (((v * ((int32_t)cal.dig_h3)) >> 11) + ((int32_t)32768))) >> 10) + + ((int32_t)2097152)) * ((int32_t)cal.dig_h2) + 8192) >> 14)); + + v = (v - (((((v >> 15) * (v >> 15)) >> 7) * ((int32_t)cal.dig_h1)) >> 4)); + v = (v < 0 ? 0 : v); + v = (v > 419430400 ? 419430400 : v); - /*return ((uint32_t)(v / 4096));*/ + return ((uint32_t)(v >> 12)); } diff --git a/c_embedded/src/bme280.h b/c_embedded/src/bme280.h @@ -3,8 +3,8 @@ #include <stdint.h> -int bme280_init(void); -int32_t bme280_read_temp(void); -uint32_t bme280_read_humid(void); +int bme280_init(void); +int32_t bme280_read_temp(void); +uint32_t bme280_read_humid(void); #endif /* _BME280_H_ */ diff --git a/c_embedded/src/i2c.c b/c_embedded/src/i2c.c @@ -13,7 +13,7 @@ i2c_init(uint8_t mode, uint8_t slew, uint32_t freq) SSPCON2 = 0; SSPADD = (_XTAL_FREQ / (4 * freq)) - 1; /* Bus clock speed */ SSPSTAT &= 0x3f; - SSPSTAT |= slew; + SSPSTAT = slew; } void diff --git a/c_embedded/src/i2c.h b/c_embedded/src/i2c.h @@ -6,6 +6,7 @@ #define I2C_MASTER 0x08 #define I2C_SLEW_OFF 0x80 #define I2C_SLEW_ON 0x00 +#define I2C_CLK_1MHZ 1000000 void i2c_init(uint8_t, uint8_t, uint32_t); void i2c_hold(void); diff --git a/c_embedded/src/main.c b/c_embedded/src/main.c @@ -9,17 +9,21 @@ static __code uint16_t __at (_CONFIG) __configword = _FOSC_HS & _WDTE_OFF & _PWRTE_ON & _LVP_OFF & _WRT_OFF & _BOREN_ON & _CPD_OFF & _CP_OFF; +/* + * TODO: turn off unneeded modules (e.g tmr1..) to minimize consumption. + */ + static void ctx_main(void); static void ctx_uptime_maxtp(void); static void print_tp(int32_t); static void led_blink(void); - static void button_debounce(void); + static uint32_t timecnt = 0; /* Seconds passed since start */ static int f_ctx = 0; /* Change context */ static uint32_t humid; /* Current humidity */ static int32_t tp; /* Current temperature */ -static int32_t maxtp = -100; /* Max temperature */ +static int32_t maxtp = -99999; /* Max temperature */ static char buf[BUFSIZ+1] = {0}; /* Generic buffer */ #define LCD_PUTS_INT(buf, v) do { \ @@ -30,12 +34,15 @@ static char buf[BUFSIZ+1] = {0}; /* Generic buffer */ static void ctx_main(void) { - lcd_cmd(LCD_CURS_ROW1); - lcd_puts("19390133"); + /*lcd_cmd(LCD_CURS_ROW1);*/ + /*lcd_puts("ID: 19390133");*/ - lcd_cmd(LCD_CURS_ROW2); + lcd_cmd(LCD_CURS_ROW1); + lcd_puts("Temp: "); print_tp(tp); + lcd_cmd(LCD_CURS_ROW2); + lcd_puts("Humid: "); LCD_PUTS_INT(buf, humid / 1024); lcd_putc('.'); LCD_PUTS_INT(buf, ((humid * 100) / 1024) % 100); @@ -57,7 +64,10 @@ ctx_uptime_maxtp(void) static void print_tp(int32_t tp) { - /* TODO: handle negative */ + if (tp < 0) { + tp = -tp; + lcd_putc('-'); + } LCD_PUTS_INT(buf, tp / 100); lcd_putc('.'); LCD_PUTS_INT(buf, tp % 100); @@ -80,7 +90,7 @@ led_blink(void) static void button_debounce(void) { - static uint16_t cnt = 0; + static uint8_t cnt = 0; /* Button is pressed */ if (BTN_PORT == 0) { @@ -103,7 +113,7 @@ main(void) tmr0_set_event(&led_blink, 1000); tmr0_set_event(&button_debounce, 1); lcd_init(); - i2c_init(I2C_MASTER, I2C_SLEW_OFF, 1000000); + i2c_init(I2C_MASTER, I2C_SLEW_OFF, I2C_CLK_1MHZ); if (bme280_init() < 0) { lcd_puts("BME280 error"); for (;;); /* Hang */ diff --git a/c_embedded/src/tmr0.c b/c_embedded/src/tmr0.c @@ -5,8 +5,8 @@ struct timer_req { ev_handler handler; /* ISR callback */ - uint32_t rate; /* Interval */ - uint32_t cnt; /* Current time */ + uint16_t rate; /* Interval */ + uint16_t cnt; /* Current time */ }; static void tmr0_isr(void) __interrupt; @@ -47,7 +47,7 @@ tmr0_init(void) } void -tmr0_delay_ms(uint32_t t) +tmr0_delay_ms(uint16_t t) { while (t--) { while (INTCONbits.TMR0IF == 0) @@ -58,7 +58,7 @@ tmr0_delay_ms(uint32_t t) } int -tmr0_set_event(ev_handler handler, uint32_t rate) +tmr0_set_event(ev_handler handler, uint16_t rate) { struct timer_req *r; uint8_t i; diff --git a/c_embedded/src/tmr0.h b/c_embedded/src/tmr0.h @@ -24,7 +24,7 @@ typedef void (*ev_handler)(void); void tmr0_init(void); -void tmr0_delay_ms(uint32_t); -int tmr0_set_event(ev_handler, uint32_t); +void tmr0_delay_ms(uint16_t); +int tmr0_set_event(ev_handler, uint16_t); #endif /* _TMR0_H_ */