diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..3aecb35 --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,19 @@ +{ + "configurations": [ + { + "name": "Linux", + "includePath": [ + "${workspaceFolder}/**", + "/usr/lib/avr/include/" + ], + "defines": [ + "__AVR_ATmega16__" + ], + "compilerPath": "/usr/bin/gcc", + "cStandard": "gnu18", + "cppStandard": "gnu++14", + "intelliSenseMode": "gcc-x64" + } + ], + "version": 4 +} \ No newline at end of file diff --git a/bin/main.bin b/bin/main.bin new file mode 100755 index 0000000..6df4843 Binary files /dev/null and b/bin/main.bin differ diff --git a/bin/main.elf b/bin/main.elf new file mode 100755 index 0000000..6df4843 Binary files /dev/null and b/bin/main.elf differ diff --git a/bin/main.hex b/bin/main.hex new file mode 100644 index 0000000..13a215e --- /dev/null +++ b/bin/main.hex @@ -0,0 +1,291 @@ +:100000000C942A000C943F000C943F000C943F0089 +:100010000C943F000C943F000C943F000C943F0064 +:100020000C943F000C943F000C943F000C943F0054 +:100030000C943F000C943F000C943F000C943F0044 +:100040000C943F000C943F000C943F000C943F0034 +:100050000C943F0011241FBECFE5D4E0DEBFCDBF1E +:1000600010E0A0E6B0E0E4EFF1E102C005900D92EF +:10007000A237B107D9F70E94BF040C94F8080C947A +:1000800000000F931F93CF93DF93CDB7DEB7289770 +:100090000FB6F894DEBF0FBECDBF9A8389836B8302 +:1000A0004C833E832D8318870F8389819A812B810E +:1000B000FC01208389819A812C81FC0121832F817D +:1000C000388589819A81FC013287218789819A81CB +:1000D0002D813E81FC01348323838D819E819C018F +:1000E0002150310989819A81FC01368325838D81D4 +:1000F0009E819C012250310989819A81FC013087BF +:10010000278389819A810E94A30589819A810E940F +:10011000570689819A810E94CE0589819A810E9421 +:100120008206000028960FB6F894DEBF0FBECDBF42 +:10013000DF91CF911F910F910895CF93DF9300D05E +:1001400000D0CDB7DEB79A8389836B834C838B81D4 +:10015000982F97708C81882311F088E001C080E08F +:10016000892B282F89819A81FC01228300000F901E +:100170000F900F900F90DF91CF910895CF93DF9361 +:10018000CDB7DEB727970FB6F894DEBF0FBECDBF51 +:100190009B838A837D836C834E832F838A819B819B +:1001A0000E94B9038A819B8160E40E94F3038A81E3 +:1001B0009B810E94CE038A819B810E94B9038F811B +:1001C000837020EC280F8A819B81622F0E94F303A9 +:1001D000198299818E81981790F48981882F90E0F7 +:1001E0002C813D81820F931FFC0120818A819B819C +:1001F000622F0E94F30389818F5F8983EACF8A810E +:100200009B810E94CE038A819B810E94B9038A81CF +:100210009B81FC0182818F7020E8280F8A819B815D +:10022000622F0E94F3038A819B810E94CE0300000B +:1002300027960FB6F894DEBF0FBECDBFDF91CF91EA +:100240000895CF93DF9300D000D000D0CDB7DEB7B4 +:100250009E838D8319821A821B821C828D819E81CE +:1002600020E044E0BE016F5F7F4F0E94BE000000AF +:1002700026960FB6F894DEBF0FBECDBFDF91CF91AB +:100280000895EF920F93CF93DF93CDB7DEB7279703 +:100290000FB6F894DEBF0FBECDBF9A8389837C83EF +:1002A0006B834D832E830F836B817C8189819A813F +:1002B000EF800E812D8140E00E946A0100002796A8 +:1002C0000FB6F894DEBF0FBECDBFDF91CF910F9177 +:1002D000EF900895CF92EF920F93CF93DF93CDB726 +:1002E000DEB728970FB6F894DEBF0FBECDBF9A8356 +:1002F00089837C836B834D832E830F83E8868B8178 +:100300009C8199231CF4919581959109AC018B8175 +:100310009C81992314F436EF01C03AE089819A81D7 +:10032000C884EF800E812D81632F0E94D1010000CF +:1003300028960FB6F894DEBF0FBECDBFDF91CF91E8 +:100340000F91EF90CF900895CF92EF920F93CF93AC +:10035000DF93CDB7DEB728970FB6F894DEBF0FBE98 +:10036000CDBF9A8389837C836B834D832E830F83D8 +:10037000E8864B815C8189819A81C884EF800E81F7 +:100380002D8160E10E94D101000028960FB6F894FB +:10039000DEBF0FBECDBFDF91CF910F91EF90CF9019 +:1003A0000895CF92EF920F931F93CF93DF93CDB722 +:1003B000DEB762970FB6F894DEBF0FBECDBF9B8746 +:1003C0008A876C875E874D872F87088BE98ACA8A60 +:1003D00019828C8588232CF48C8581958C8781E00B +:1003E00089838D859E85892B89F5988981E0892768 +:1003F000882361F11A828A81282F30E08989882F29 +:1004000090E001972817390774F48A81882F90E0CB +:100410009E012A5F3F4F820F931FFC0110828A8149 +:100420008F5F8A83E8CF8989882F90E08C01015003 +:1004300011098A859B8560E00E94A704282FCE01C0 +:100440000696800F911FFC0120836DC08989882F3B +:1004500090E001979C838B838B819C8199230CF482 +:1004600056C08C85282F880F330B8D859E85B9014A +:100470000E94AD068D838D81882399F48D859E859C +:10048000892B79F4988981E08927882351F09E018E +:100490002A5F3F4F8B819C81820F931FFC0110824A +:1004A0000FC08A859B856D810E94A704482F9E01FD +:1004B0002A5F3F4F8B819C81820F931FFC014083F9 +:1004C0008D81882391F48D859E85892B71F4898196 +:1004D000882359F09E012A5F3F4F8B819C81820FB8 +:1004E000931F20E4FC01208319828C85282F880F1C +:1004F000330B8D859E85B9010E94AD06CB019E8789 +:100500008D878B819C8101979C838B83A5CF8F8561 +:10051000882349F09E012A5F3F4F8A859B85A90168 +:100520006F850E946B04BE016A5F7F4F8A859B8541 +:100530002A8949890E94BE00000062960FB6F8948D +:10054000DEBF0FBECDBFDF91CF911F910F91EF9016 +:10055000CF900895CF93DF93CDB7DEB7A2970FB6B4 +:10056000F894DEBF0FBECDBF9AA389A389A19AA13B +:10057000FC0181859285CC01A0E0B0E0BC01CD01F9 +:100580000E94D407DC01CB0189839A83AB83BC83AF +:100590002BEA3AEA4AE250E469817A818B819C81B4 +:1005A0000E948B08DC01CB018D839E83AF83B887CB +:1005B00089819A81AB81BC819C01AD01BC01CD01D7 +:1005C0000E94C206DC01CB0189879A87AB87BC8772 +:1005D00020E030E040E85FE36D817E818F81988587 +:1005E0000E942E0788231CF481E08D87B2C020E092 +:1005F0003FEF4FE757E469857A858B859C850E949C +:10060000860818160CF07BC020E030E04AE754E47E +:1006100069817A818B819C810E943307DC01CB0147 +:100620008E879F87A88BB98B20E030E04AEF54E497 +:100630006E857F85888999890E948B08DC01CB01B2 +:100640008A8B9B8BAC8BBD8B20E030E040E85FE376 +:100650006A897B898C899D890E942E0788232CF4C6 +:1006600081E090E09F8B8E8B3FC020E03FEF4FE713 +:1006700057E46A897B898C899D890E9486081816AF +:100680004CF520E030E040E251E46E857F858889BA +:1006900099890E948B08DC01CB01BC01CD010E942D +:1006A000A507DC01CB019F8B8E8B8E899F89892BBF +:1006B00009F44FC088EC90E0998F888F888D998DD0 +:1006C0000197F1F7998F888F8E899F8901979F8B6A +:1006D0008E8BEBCF6A897B898C899D890E94A507C7 +:1006E000DC01CB019F8B8E8B8E899F899B8F8A8F9C +:1006F0008A8D9B8D0197F1F79B8F8A8F2AC020E00E +:1007000030E04FE753E46D817E818F8198850E94B0 +:1007100086081816ACF469857A858B859C850E94BD +:10072000A507DC01CB019D8F8C8F8C8D9D8D9F8FBC +:100730008E8F8E8D9F8D0197F1F79F8F8E8F0FC0BB +:100740006D817E818F8198850E94A507DC01CB0198 +:100750008D878D8588A388A18A95F1F788A30000ED +:10076000A2960FB6F894DEBF0FBECDBFDF91CF913A +:100770000895CF93DF9300D0CDB7DEB79A838983F6 +:1007800089819A810E94790589819A810E94AA02B1 +:1007900000000F900F90DF91CF910895CF93DF93DA +:1007A00000D0CDB7DEB79A83898389819A810E9470 +:1007B000790589819A810E94AA0289819A810E9481 +:1007C000570689819A810E94AA0289819A810E9492 +:1007D000A30589819A810E94AA0200000F900F90C0 +:1007E000DF91CF910895CF93DF9300D000D000D058 +:1007F000CDB7DEB79D838C836E838E8189831A8209 +:100800008A81883070F58C819D810E942D068C81B3 +:100810009D810E94AA028981882F90E0817099278A +:10082000892B29F08C819D810E94A30504C08C81B5 +:100830009D810E9479058C819D810E94AA028C81F4 +:100840009D810E9457068C819D810E94AA02898108 +:10085000882F90E09595879589838A818F5F8A8319 +:10086000CFCF8C819D810E942D068C819D810E941D +:10087000A3058C819D810E94AA028C819D810E948A +:1008800057068C819D810E94AA028C819D810E94C5 +:10089000F9058B838B81882321F48C819D810E94B3 +:1008A00079058C819D810E94AA028C819D810E9484 +:1008B0002D068C819D810E94AA0281E09B81992353 +:1008C00009F480E026960FB6F894DEBF0FBECDBFC8 +:1008D000DF91CF910895CF93DF93CDB7DEB7279700 +:1008E0000FB6F894DEBF0FBECDBF9C838B836D83A4 +:1008F0005F834E831A82198289819A810497ECF46E +:1009000089819A812E813F81820F931F29813A81AB +:100910004E815F81240F351FF9012081322F2D81F7 +:100920002078232BFC0120838D81880F8D83898182 +:100930009A8101969A838983DFCF000027960FB6AC +:10094000F894DEBF0FBECDBFDF91CF910895CF9356 +:10095000DF9300D01F92CDB7DEB79A8389836B8374 +:100960008B81882F90E08F709927805A9F4FFC01D0 +:1009700080810F900F900F90DF91CF9108950F938A +:100980001F93CF93DF93CDB7DEB76F970FB6F89471 +:10099000DEBF0FBECDBF8AE390E02AE330E0F9016D +:1009A00020812160FC01208381E090E09C838B8387 +:1009B0001E821D82CE01459604E610E025E330E05C +:1009C00040E061E00E9441001A821982CE01459602 +:1009D00041E06FEF0E949D0089819A819C012F5F09 +:1009E0003F4F3A832983FE01759600E024E040E002 +:1009F000BC01CF010E9441018BE390E02BE330E08A +:100A0000F901308121E02327FC01208380E090E080 +:100A1000AAE7B4E48F839887A987BA8720E030E0FB +:100A20004AEF54E46F81788589859A850E948B0806 +:100A3000DC01CB018B879C87AD87BE8720E030E04F +:100A400040E85FE36B857C858D859E850E942E073F +:100A500088232CF481E090E0988B8F873FC020E0C2 +:100A60003FEF4FE757E46B857C858D859E850E941F +:100A7000860818164CF520E030E040E251E46F8122 +:100A8000788589859A850E948B08DC01CB01BC01A1 +:100A9000CD010E94A507DC01CB01988B8F878F8544 +:100AA0009889892B09F498CF88EC90E09A8B898BF0 +:100AB00089899A890197F1F79A8B898B8F85988913 +:100AC0000197988B8F87EBCF6B857C858D859E8575 +:100AD0000E94A507DC01CB01988B8F878F859889B1 +:100AE0009C8B8B8B8B899C890197F1F79C8B8B8BD3 +:100AF00073CFCF93DF9300D0CDB7DEB79A838983CE +:100B000089819A81FC018581968129813A81F90147 +:100B100025813681F9012081622F29813A81F901ED +:100B20002181422F50E021E030E002C0220F331F2C +:100B30004A95E2F7262BFC01208300000F900F90CE +:100B4000DF91CF910895CF93DF9300D0CDB7DEB77B +:100B50009A83898389819A81FC0185819681298183 +:100B60003A81F90125813681F9012081622F29819D +:100B70003A81F9012181422F50E021E030E002C0AA +:100B8000220F331F4A95E2F720952623FC0120838C +:100B900000000F900F90DF91CF910895CF93DF93D6 +:100BA00000D0CDB7DEB79A83898389819A81FC0111 +:100BB0008381948129813A81F90123813481F9016A +:100BC0002081622F29813A81F9012181422F50E051 +:100BD00021E030E002C0220F331F4A95E2F7209552 +:100BE0002623FC01208300000F900F90DF91CF910E +:100BF0000895CF93DF9300D0CDB7DEB79A83898372 +:100C000089819A81FC0187819085FC018081282F50 +:100C100030E089819A81FC018181482F50E081E098 +:100C200090E002C0880F991F4A95E2F749815A81E6 +:100C3000FA014181442F50E002C0959587954A956D +:100C4000E2F78223932321E0892B09F420E0822F0D +:100C50000F900F90DF91CF910895CF93DF9300D045 +:100C6000CDB7DEB79A83898389819A81FC0185811A +:100C7000968129813A81F90125813681F901208106 +:100C8000622F29813A81F9012081422F50E021E031 +:100C900030E002C0220F331F4A95E2F7262BFC01F9 +:100CA000208300000F900F90DF91CF910895CF9394 +:100CB000DF9300D0CDB7DEB79A83898389819A818B +:100CC000FC018581968129813A81F901258136814E +:100CD000F9012081622F29813A81F9012081422F77 +:100CE00050E021E030E002C0220F331F4A95E2F7C6 +:100CF00020952623FC01208300000F900F90DF91A8 +:100D0000CF910895CF93DF9300D0CDB7DEB79A830C +:100D1000898389819A81FC018381948129813A8127 +:100D2000F90123813481F9012081622F29813A81DF +:100D3000F9012081422F50E021E030E002C0220F73 +:100D4000331F4A95E2F720952623FC0120830000FB +:100D50000F900F90DF91CF910895AA1BBB1B51E11B +:100D600007C0AA1FBB1FA617B70710F0A61BB70B1B +:100D7000881F991F5A95A9F780959095BC01CD01C0 +:100D800008955058BB27AA270E94D9060C944C08F6 +:100D90000E943E0838F00E94450820F039F49F3F39 +:100DA00019F426F40C943B080EF4E095E7FB0C9440 +:100DB0003508E92F0E945D0858F3BA1762077307D8 +:100DC0008407950720F079F4A6F50C947F080EF4BB +:100DD000E0950B2EBA2FA02D0B01B90190010C014B +:100DE000CA01A0011124FF27591B99F0593F50F463 +:100DF000503E68F11A16F040A22F232F342F4427BB +:100E0000585FF3CF469537952795A795F0405395B2 +:100E1000C9F77EF41F16BA0B620B730B840BBAF082 +:100E20009150A1F0FF0FBB1F661F771F881FC2F7ED +:100E30000EC0BA0F621F731F841F48F48795779501 +:100E40006795B795F7959E3F08F0B0CF9395880FBB +:100E500008F09927EE0F9795879508950E9411083D +:100E600008F481E008950E9447070C944C080E9402 +:100E7000450858F00E943E0840F029F45F3F29F0F1 +:100E80000C94350851110C9480080C943B080E9476 +:100E90005D0868F39923B1F3552391F3951B550B26 +:100EA000BB27AA2762177307840738F09F5F5F4F3D +:100EB000220F331F441FAA1FA9F335D00E2E3AF07C +:100EC000E0E832D091505040E695001CCAF72BD094 +:100ED000FE2F29D0660F771F881FBB1F26173707E5 +:100EE0004807AB07B0E809F0BB0B802DBF01FF2717 +:100EF00093585F4F3AF09E3F510578F00C943508B7 +:100F00000C9480085F3FE4F3983ED4F38695779580 +:100F10006795B795F7959F5FC9F7880F911D9695CF +:100F2000879597F90895E1E0660F771F881FBB1F2B +:100F3000621773078407BA0720F0621B730B840BD8 +:100F4000BA0BEE1F88F7E09508950E94650888F0B7 +:100F50009F5798F0B92F9927B751B0F0E1F0660F7D +:100F6000771F881F991F1AF0BA95C9F714C0B130BE +:100F700091F00E947F08B1E008950C947F08672FDC +:100F8000782F8827B85F39F0B93FCCF386957795ED +:100F90006795B395D9F73EF49095809570956195D6 +:100FA0007F4F8F4F9F4F0895E89409C097FB3EF401 +:100FB00090958095709561957F4F8F4F9F4F9923A6 +:100FC000A9F0F92F96E9BB279395F6958795779524 +:100FD0006795B795F111F8CFFAF4BB0F11F460FFE4 +:100FE0001BC06F5F7F4F8F4F9F4F16C0882311F03C +:100FF00096E911C0772321F09EE8872F762F05C050 +:10100000662371F096E8862F70E060E02AF09A95EA +:10101000660F771F881FDAF7880F9695879597F9DF +:101020000895990F0008550FAA0BE0E8FEEF161679 +:101030001706E807F907C0F012161306E407F507CC +:1010400098F0621B730B840B950B39F40A2661F040 +:10105000232B242B252B21F408950A2609F4A140E3 +:10106000A6958FEF811D811D089597F99F6780E8F0 +:1010700070E060E008959FEF80EC089500240A94EA +:101080001616170618060906089500240A94121663 +:101090001306140605060895092E0394000C11F496 +:1010A000882352F0BB0F40F4BF2B11F460FF04C043 +:1010B0006F5F7F4F8F4F9F4F089557FD9058440F9C +:1010C000551F59F05F3F71F04795880F97FB991FA7 +:1010D00061F09F3F79F08795089512161306140664 +:1010E000551FF2CF4695F1DF08C0161617061806F1 +:1010F000991FF1CF86957105610508940895E894CC +:10110000BB2766277727CB0197F908950E9411081E +:1011100008F48FEF08950E949E080C944C080E94DA +:101120003E0838F00E94450820F0952311F00C94F9 +:1011300035080C943B0811240C9480080E945D082B +:1011400070F3959FC1F3950F50E0551F629FF0011A +:10115000729FBB27F00DB11D639FAA27F00DB11D33 +:10116000AA1F649F6627B00DA11D661F829F2227BC +:10117000B00DA11D621F739FB00DA11D621F839F43 +:10118000A00D611D221F749F3327A00D611D231F19 +:10119000849F600D211D822F762F6A2F11249F5767 +:1011A00050409AF0F1F088234AF0EE0FFF1FBB1F6A +:1011B000661F771F881F91505040A9F79E3F510529 +:1011C00080F00C9435080C9480085F3FE4F3983E5F +:1011D000D4F3869577956795B795F795E7959F5FD3 +:1011E000C1F7FE2B880F911D9695879597F9089565 +:0411F000F894FFCFA1 +:1011F4003F065B4F666D7D077F6F777C395E797143 +:021204004000A8 +:00000001FF diff --git a/makefile b/makefile new file mode 100644 index 0000000..a262ec9 --- /dev/null +++ b/makefile @@ -0,0 +1,29 @@ +MCU=atmega16 +F_CPU=8000000 +PROG=dragon_jtag +DEBUGGER = dragon +PORT=/dev/ttyUSB0 + +CC=avr-g++ +OBJCOPY=avr-objcopy +CFLAGS=-Wall -g -mmcu=${MCU} -DF_CPU=${F_CPU} -I. +TARGET=main +SRCS=src/*.cpp + +default: build flash + +build: + ${CC} ${CFLAGS} -o bin/${TARGET}.bin ${SRCS} + ${CC} ${CFLAGS} -o bin/${TARGET}.elf ${SRCS} + ${OBJCOPY} -j .text -j .data -O ihex bin/${TARGET}.bin bin/${TARGET}.hex + +flash: + avrdude -p ${MCU} -c ${PROG} -U flash:w:bin/main.hex + +clean: + rm -f bin/* + +debug: flash + sleep 1 + avarice --program --file bin/main.elf --part ${MCU} --${DEBUGGER} :4242 & + avr-gdb -ex "target remote :4242" bin/main.elf diff --git a/src/TM1637Display.cpp b/src/TM1637Display.cpp new file mode 100644 index 0000000..5dddb16 --- /dev/null +++ b/src/TM1637Display.cpp @@ -0,0 +1,296 @@ + +// Author: avishorp@gmail.com +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +extern "C" { + #include + #include + #include +} + +#include "TM1637Display.h" +#include +#include + +#define TM1637_I2C_COMM1 0x40 +#define TM1637_I2C_COMM2 0xC0 +#define TM1637_I2C_COMM3 0x80 + +// +// A +// --- +// F | | B +// -G- +// E | | C +// --- +// D +const uint8_t digitToSegment[] = { + // XGFEDCBA + 0b00111111, // 0 + 0b00000110, // 1 + 0b01011011, // 2 + 0b01001111, // 3 + 0b01100110, // 4 + 0b01101101, // 5 + 0b01111101, // 6 + 0b00000111, // 7 + 0b01111111, // 8 + 0b01101111, // 9 + 0b01110111, // A + 0b01111100, // b + 0b00111001, // C + 0b01011110, // d + 0b01111001, // E + 0b01110001 // F + }; + +static const uint8_t minusSegments = 0b01000000; + + + + +TM1637Display::TM1637Display(uint8_t pinClk, uint8_t pinDIO, volatile uint8_t *port, int bitDelay) +{ + // Copy the pin numbers + m_pinClk = pinClk; + m_pinDIO = pinDIO; + m_bitDelay = bitDelay; + m_port = port; + m_ddr = port-1; + m_pin = port-2; + + // Set the pin direction and default value. + // Both pins are set as inputs, allowing the pull-up resistors to pull them up + //CLK_Input(); + //pinMode(m_pinDIO,INPUT); + DIO_Input(); + CLK_Input(); + //digitalWrite(m_pinClk, LOW); + //digitalWrite(m_pinDIO, LOW); + DIO_Low(); + CLK_Low(); +} + +void TM1637Display::setBrightness(uint8_t brightness, bool on) +{ + m_brightness = (brightness & 0x7) | (on? 0x08 : 0x00); +} + +void TM1637Display::setSegments(const uint8_t segments[], uint8_t length, uint8_t pos) +{ + // Write COMM1 + start(); + writeByte(TM1637_I2C_COMM1); + stop(); + + // Write COMM2 + first digit address + start(); + writeByte(TM1637_I2C_COMM2 + (pos & 0x03)); + + // Write the data bytes + for (uint8_t k=0; k < length; k++) + writeByte(segments[k]); + + stop(); + + // Write COMM3 + brightness + start(); + writeByte(TM1637_I2C_COMM3 + (m_brightness & 0x0f)); + stop(); +} + +void TM1637Display::clear() +{ + uint8_t data[] = { 0, 0, 0, 0 }; + setSegments(data); +} + +void TM1637Display::showNumberDec(int num, bool leading_zero, uint8_t length, uint8_t pos) +{ + showNumberDecEx(num, 0, leading_zero, length, pos); +} + +void TM1637Display::showNumberDecEx(int num, uint8_t dots, bool leading_zero, + uint8_t length, uint8_t pos) +{ + showNumberBaseEx(num < 0? -10 : 10, num < 0? -num : num, dots, leading_zero, length, pos); +} + +void TM1637Display::showNumberHexEx(uint16_t num, uint8_t dots, bool leading_zero, + uint8_t length, uint8_t pos) +{ + showNumberBaseEx(16, num, dots, leading_zero, length, pos); +} + +void TM1637Display::showNumberBaseEx(int8_t base, uint16_t num, uint8_t dots, bool leading_zero, + uint8_t length, uint8_t pos) +{ + bool negative = false; + if (base < 0) { + base = -base; + negative = true; + } + + + uint8_t digits[4]; + + if (num == 0 && !leading_zero) { + // Singular case - take care separately + for(uint8_t i = 0; i < (length-1); i++) + digits[i] = 0; + digits[length-1] = encodeDigit(0); + } + else { + //uint8_t i = length-1; + //if (negative) { + // // Negative number, show the minus sign + // digits[i] = minusSegments; + // i--; + //} + + for(int i = length-1; i >= 0; --i) + { + uint8_t digit = num % base; + + if (digit == 0 && num == 0 && leading_zero == false) + // Leading zero is blank + digits[i] = 0; + else + digits[i] = encodeDigit(digit); + + if (digit == 0 && num == 0 && negative) { + digits[i] = minusSegments; + negative = false; + } + + num /= base; + } + + if(dots != 0) + { + showDots(dots, digits); + } + } + setSegments(digits, length, pos); +} + +void TM1637Display::bitDelay() +{ + _delay_us(m_bitDelay); +} + +void TM1637Display::start() +{ + DIO_Output(); + bitDelay(); +} + +void TM1637Display::stop() +{ + DIO_Output(); + bitDelay(); + CLK_Input(); + bitDelay(); + DIO_Input(); + bitDelay(); +} + +bool TM1637Display::writeByte(uint8_t b) +{ + uint8_t data = b; + + // 8 Data Bits + for(uint8_t i = 0; i < 8; i++) { + // CLK low + CLK_Output(); + bitDelay(); + + // Set data bit + if (data & 0x01) + DIO_Input(); + else + DIO_Output(); + + bitDelay(); + + // CLK high + CLK_Input(); + bitDelay(); + data = data >> 1; + } + + // Wait for acknowledge + // CLK to zero + CLK_Output(); + DIO_Input(); + bitDelay(); + + // CLK to high + CLK_Input(); + bitDelay(); + uint8_t ack = DIO_Read(); + if (ack == 0) + DIO_Output(); + + + bitDelay(); + CLK_Output(); + bitDelay(); + + return ack; +} + +void TM1637Display::showDots(uint8_t dots, uint8_t* digits) +{ + for(int i = 0; i < 4; ++i) + { + digits[i] |= (dots & 0x80); + dots <<= 1; + } +} + +uint8_t TM1637Display::encodeDigit(uint8_t digit) +{ + return digitToSegment[digit & 0x0f]; +} + +inline void TM1637Display::DIO_Output() { + *(m_ddr) |= (1<> m_pinDIO; + } + + inline void TM1637Display::CLK_Output() { + *(m_ddr) |= (1< +#include + +#define SEG_A 0b00000001 +#define SEG_B 0b00000010 +#define SEG_C 0b00000100 +#define SEG_D 0b00001000 +#define SEG_E 0b00010000 +#define SEG_F 0b00100000 +#define SEG_G 0b01000000 + +#define DEFAULT_BIT_DELAY 100 + +class TM1637Display { + +public: + //! Initialize a TM1637Display object, setting the clock and + //! data pins. + //! + //! @param pinClk - The number of the digital pin connected to the clock pin of the module + //! @param pinDIO - The number of the digital pin connected to the DIO pin of the module + //! @param bitDelay - The delay, in microseconds, between bit transition on the serial + //! bus connected to the display + TM1637Display(uint8_t pinClk, uint8_t pinDIO, volatile uint8_t *port, int bitDelay = DEFAULT_BIT_DELAY); + + //! Sets the brightness of the display. + //! + //! The setting takes effect when a command is given to change the data being + //! displayed. + //! + //! @param brightness A number from 0 (lowes brightness) to 7 (highest brightness) + //! @param on Turn display on or off + void setBrightness(uint8_t brightness, bool on = true); + + //! Display arbitrary data on the module + //! + //! This function receives raw segment values as input and displays them. The segment data + //! is given as a byte array, each byte corresponding to a single digit. Within each byte, + //! bit 0 is segment A, bit 1 is segment B etc. + //! The function may either set the entire display or any desirable part on its own. The first + //! digit is given by the @ref pos argument with 0 being the leftmost digit. The @ref length + //! argument is the number of digits to be set. Other digits are not affected. + //! + //! @param segments An array of size @ref length containing the raw segment values + //! @param length The number of digits to be modified + //! @param pos The position from which to start the modification (0 - leftmost, 3 - rightmost) + void setSegments(const uint8_t segments[], uint8_t length = 4, uint8_t pos = 0); + + //! Clear the display + void clear(); + + //! Display a decimal number + //! + //! Dispaly the given argument as a decimal number. + //! + //! @param num The number to be shown + //! @param leading_zero When true, leading zeros are displayed. Otherwise unnecessary digits are + //! blank. NOTE: leading zero is not supported with negative numbers. + //! @param length The number of digits to set. The user must ensure that the number to be shown + //! fits to the number of digits requested (for example, if two digits are to be displayed, + //! the number must be between 0 to 99) + //! @param pos The position of the most significant digit (0 - leftmost, 3 - rightmost) + void showNumberDec(int num, bool leading_zero = false, uint8_t length = 4, uint8_t pos = 0); + + //! Display a decimal number, with dot control + //! + //! Dispaly the given argument as a decimal number. The dots between the digits (or colon) + //! can be individually controlled. + //! + //! @param num The number to be shown + //! @param dots Dot/Colon enable. The argument is a bitmask, with each bit corresponding to a dot + //! between the digits (or colon mark, as implemented by each module). i.e. + //! For displays with dots between each digit: + //! * 0.000 (0b10000000) + //! * 00.00 (0b01000000) + //! * 000.0 (0b00100000) + //! * 0.0.0.0 (0b11100000) + //! For displays with just a colon: + //! * 00:00 (0b01000000) + //! For displays with dots and colons colon: + //! * 0.0:0.0 (0b11100000) + //! @param leading_zero When true, leading zeros are displayed. Otherwise unnecessary digits are + //! blank. NOTE: leading zero is not supported with negative numbers. + //! @param length The number of digits to set. The user must ensure that the number to be shown + //! fits to the number of digits requested (for example, if two digits are to be displayed, + //! the number must be between 0 to 99) + //! @param pos The position of the most significant digit (0 - leftmost, 3 - rightmost) + void showNumberDecEx(int num, uint8_t dots = 0, bool leading_zero = false, uint8_t length = 4, uint8_t pos = 0); + + //! Display a hexadecimal number, with dot control + //! + //! Dispaly the given argument as a hexadecimal number. The dots between the digits (or colon) + //! can be individually controlled. + //! + //! @param num The number to be shown + //! @param dots Dot/Colon enable. The argument is a bitmask, with each bit corresponding to a dot + //! between the digits (or colon mark, as implemented by each module). i.e. + //! For displays with dots between each digit: + //! * 0.000 (0b10000000) + //! * 00.00 (0b01000000) + //! * 000.0 (0b00100000) + //! * 0.0.0.0 (0b11100000) + //! For displays with just a colon: + //! * 00:00 (0b01000000) + //! For displays with dots and colons colon: + //! * 0.0:0.0 (0b11100000) + //! @param leading_zero When true, leading zeros are displayed. Otherwise unnecessary digits are + //! blank + //! @param length The number of digits to set. The user must ensure that the number to be shown + //! fits to the number of digits requested (for example, if two digits are to be displayed, + //! the number must be between 0 to 99) + //! @param pos The position of the most significant digit (0 - leftmost, 3 - rightmost) + void showNumberHexEx(uint16_t num, uint8_t dots = 0, bool leading_zero = false, uint8_t length = 4, uint8_t pos = 0); + + //! Translate a single digit into 7 segment code + //! + //! The method accepts a number between 0 - 15 and converts it to the + //! code required to display the number on a 7 segment display. + //! Numbers between 10-15 are converted to hexadecimal digits (A-F) + //! + //! @param digit A number between 0 to 15 + //! @return A code representing the 7 segment image of the digit (LSB - segment A; + //! bit 6 - segment G; bit 7 - always zero) + uint8_t encodeDigit(uint8_t digit); + +protected: + void bitDelay(); + + void start(); + + void stop(); + + bool writeByte(uint8_t b); + + void showDots(uint8_t dots, uint8_t* digits); + + void showNumberBaseEx(int8_t base, uint16_t num, uint8_t dots = 0, bool leading_zero = false, uint8_t length = 4, uint8_t pos = 0); + + +private: + uint8_t m_pinClk; + uint8_t m_pinDIO; + uint8_t m_brightness; + volatile uint8_t *m_port; + volatile uint8_t *m_ddr; + volatile uint8_t *m_pin; + unsigned int m_bitDelay; + inline void DIO_Output(); + inline void DIO_Input(); + inline void DIO_Low(); + inline bool DIO_Read(); + inline void CLK_Output(); + inline void CLK_Input(); + inline void CLK_Low(); +}; + +#endif // __TM1637DISPLAY__ diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..47f5880 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,29 @@ +/* + * Blink.cpp + * + * Created: 18/12/2018 5:11:34 PM + * Author : jimmy + */ +#define F_CPU 8000000 +#include +#include +#include +#include "TM1637Display.h" + +int main(void) { + *(&PORTA-1) |= (1<