Project Đồng hồ số để bàn kết hợp hiển thị nhiệt độ
![]() |
Đồng hồ số kết hợp hiển thị nhiệt độ |
// VARIABLES
char second = 0; char second_old = 0;
char minute = 0; char minute_ten = 0; char minute_one = 0;
char hour = 0; char hour_ten = 0; char hour_one = 0;
char counter_1=0; char counter_2=0;
unsigned temp = 0; char temp_ten=0; char temp_one=0; char temp_neg=0;
char blink=0;
char i=0;
void send_to_max7219(char address,char data) {
PORTC.F6 = 0; // LOAD(CS) LINE LOW
Spi_Write(address);
Spi_Write(data);
PORTC.F6 = 1; // LOAD(CS) LINE HIGH
}
void init_MAX7219() {
send_to_max7219(0x09,0xFF);// BCD Mode Code B
send_to_max7219(0x0A,0x0F);// INTENSITY MAX
send_to_max7219(0x0B,0x03);// SCAN ONLY DIGITS 0,1,2,3
send_to_max7219(0x0C,0x01);// TURN ON
send_to_max7219(0x00,0x0F);// TEST OFF
}
void show_temp() {
// READ AN0 INPUT 50 TIMES CUMULATIVELY
temp=0;
for (i=0;i<=49;i++) {
temp = temp+ Adc_Read(0);
delay_ms(1);
}
temp=temp/50; //CALCULATE THE AVERAGE
temp = temp*4.57;//CALCULATE THE SENSOR OUTPUT in mV
// TEMPERATURE POSITIVE OR NEGATIVE?
if (temp>=2730) {
temp = temp-2730; temp_neg=0;}
else {temp=2730-temp;temp_neg=1;}
// CONVERT TEMPERATURE VALUE INTO BCD FORMAT
temp=Dec2Bcd16(temp);
// MAX7219 NO DECODE MODE
send_to_max7219(0x09,0x00);
// DISPLAY THE MINUS SIGN IF TEMP IS NEGATIVE
if (temp_neg) {
send_to_max7219(0x01,0x01); }
else {
send_to_max7219(0x01,0x00); }
// SEPERATE THE TEMPERATURE DIGITS
temp_one=(temp>>4)&0b00001111;
temp_ten=(temp>>8)&0b00001111;
// DISPLAY THE TEMPERATURE DIGITS
if (temp_one==0x00) {send_to_max7219(0x03,0x7E);}
else if (temp_one==0x01) {send_to_max7219(0x03,0x30);}
else if (temp_one==0x02) {send_to_max7219(0x03,0x6D);}
else if (temp_one==0x03) {send_to_max7219(0x03,0x79);}
else if (temp_one==0x04) {send_to_max7219(0x03,0x33);}
else if (temp_one==0x05) {send_to_max7219(0x03,0x5B);}
else if (temp_one==0x06) {send_to_max7219(0x03,0x5F);}
else if (temp_one==0x07) {send_to_max7219(0x03,0x70);}
else if (temp_one==0x08) {send_to_max7219(0x03,0x7F);}
else if (temp_one==0x09) {send_to_max7219(0x03,0x7B);}
if (temp_ten==0x00) {send_to_max7219(0x03,0x7E);}
else if (temp_ten==0x01) {send_to_max7219(0x02,0x30);}
else if (temp_ten==0x02) {send_to_max7219(0x02,0x6D);}
else if (temp_ten==0x03) {send_to_max7219(0x02,0x79);}
else if (temp_ten==0x04) {send_to_max7219(0x02,0x33);}
else if (temp_ten==0x05) {send_to_max7219(0x02,0x5B);}
else if (temp_ten==0x06) {send_to_max7219(0x02,0x5F);}
else if (temp_ten==0x07) {send_to_max7219(0x02,0x70);}
else if (temp_ten==0x08) {send_to_max7219(0x02,0x7F);}
else if (temp_ten==0x09) {send_to_max7219(0x02,0x7B);}
// DISPLAY THE DEGREE SIGN
send_to_max7219(0x04,0x62);
}
void main() {
// INITIALIZE SPECIAL FUNCTION REGISTERS
ADCON1=0b00001110; // SELECT THE ANALOG INPUTS
TRISC = 0b00010000; // ASSIGN DIRECTIONS FOR PORTC
TRISB = 0b11111111; // ASSIGN DIRECTIONS FOR PORTB
TRISA = 0b00000001; // ASSIGN DIRECTIONS FOR PORTA
PORTC.F6 = 1; //LOAD(CS) LINE IS INITIALLY LOW
// INIT HW SPI
Spi_Init_Advanced(MASTER_OSC_DIV16,DATA_SAMPLE_MIDDLE,CLK_IDLE_LOW,LOW_2_HIGH);
// INIT SW I2C
Soft_I2C_Config(&PORTB, 1, 0);
while(1) {
init_MAX7219();
// READ SECOND, MINUTE and HOUR INFO FROM DS1307
Soft_I2C_Start();
Soft_I2C_Write(0xD0);
Soft_I2C_Write(0x00);
Soft_I2C_Start();
Soft_I2C_Write(0xD1);
second = Soft_I2C_Read(1);
minute = Soft_I2C_Read(1);
hour = Soft_I2C_Read(0);
Soft_I2C_Stop();
// SEPERATE THE HOUR AND MINUTE DIGITS
hour_one = 0b00001111 & hour;
hour_ten = (hour >> 4) & 0b00001111;
minute_one = 0b00001111 & minute;
minute_ten = (minute >> 4) & 0b00001111;
// BLINKING AND TEMP. DISPLAY PERIOD
if (second != second_old) { // DETECT A SECOND
counter_1++;
counter_2++;
second_old=second;
if (counter_1==1) { // BLINK EVERY 1 SECOND
counter_1=0;
blink=~blink;
}
if (counter_2==15) { // DISPLAY TEMP EVERY 15 SECONDS
counter_2=0;
show_temp();
delay_ms(3000); // DISPLAY FOR THREE SECONDS
send_to_max7219(0x09,0xFF); // SET BCD CODE B
}
}
// MANUALLY SHOW TEMP IF SW2 IS PRESSED
if (PORTB.F3==0) {
counter_2=0;
show_temp();
delay_ms(3000);
send_to_max7219(0x09,0xFF); // SET BCD CODE B
}
// DISPLAY THE HOUR DIGITS
if (hour_ten == 0x00) { // IF THE DECIMAL DIGIT IS 0
send_to_max7219(0x01,0x0F);} // THEN DISPLAY NOTHING IN FIRST DIGIT
else {
send_to_max7219(0x01,hour_ten);}
if (blink==0xFF) {
hour_one|=0b10000000;} // DETERMINE THE DOT STATUS OF THE 2nd DIGIT
else {
hour&=0b01111111;}
send_to_max7219(0x02,hour_one);
// DISPLAY THE MINUTE DIGITS
if (blink==0xFF) {
minute_ten|=0b10000000;} // DETERMINE THE DOT STATUS OF THE 3th DIGIT
else {
minute_ten&=0b01111111;}
send_to_max7219(0x03,minute_ten);
send_to_max7219(0x04,minute_one);
// INCREASE THE HOUR IF SW IS PRESSED
hour_one&=0b01111111;
if (PORTB.F2==0) {
if (hour_one<=0x08) {
if ((hour_ten==0x02) && (hour_one==0x03)) {
hour_one=0;
hour_ten=0; }
else {
hour_one++;}
}
else {hour_one=0x00;
if (hour_ten<=0x1) {
hour_ten++;}
else { hour_ten=0x00;}
}
// WRITE THE NEW HOUR VALUE TO DS1307
hour_ten = (hour_ten<<4)&0b11110000;
hour = hour_one|hour_ten;
Soft_I2C_Start();
Soft_I2C_Write(0xD0);
Soft_I2C_Write(0x02);
Soft_I2C_Write(hour);
Soft_I2C_Stop();
do {} while (PORTB.F2==0) ; // SW DEBOUNCE
}
// INCREASE MINUTE IF SW3 IS PRESSED
minute_ten&=0b01111111;
if (PORTB.F4==0) {
if (minute_one<=0x08) {
minute_one++;
}
else {
if ((minute_ten==0x05) && (minute_one==0x09)) {
minute_one=0x00;
minute_ten=0x00; }
else if (minute_ten<=0x05) {
minute_ten++;
minute_one=0x00;}
else {
minute_ten=0x00;
minute_one=0x00;}
}
// WRITE THE NEW MINUTE VALUE TO DS1307
minute_ten = (minute_ten<<4)&0b11110000;
minute = minute_one|minute_ten;
Soft_I2C_Start();
Soft_I2C_Write(0xD0);
Soft_I2C_Write(0x00);
Soft_I2C_Write(0x00);
Soft_I2C_Write(minute);
Soft_I2C_Stop();
do {} while (PORTB.F4==0); // SW DEBOUNCE
}
}
}
* Sau đây là file hex, bạn copy vào notepad và save lại với đuôi ".hex".
:1000000004EF00F0FFFFFFFF92EC05F00E0EC16E53
:10001000100E946EFF0E936E010E926E828C010E86
:10002000276E286A296A010E2A6E24EC06F0810EDA
:10003000276E0F0E286E3AEC06F0D3EC05F04DEC6F
:1000400006F0D00E276E5BEC04F0276A5BEC04F040
:100050004DEC06F0D10E276E5BEC04F0010E276E1E
:10006000BBEC04F000C01CF0010E276EBBEC04F0EA
:1000700000C01EF0276ABBEC04F000C021F0F1ECD8
:1000800005F00F0E2114236E21C022F02232229E91
:100090002232229E2232229E2232229E0F0E2216CF
:1000A0000F0E1E14206E1EC01FF01F321F9E1F3227
:1000B0001F9E1F321F9E1F321F9E0F0E1F161C50A9
:1000C0001D1834E0242A252A1CC01DF02450010AE2
:1000D00002E1246A261E25500F0A28E1256A83ECD6
:1000E00001F0270E0C6EFF0E0B6EFF0E0A6E0C2E2B
:1000F00001D007D00B2E01D003D00A2EFED7FAD79D
:10010000F6D73F0E0B6EFF0E0A6E0B2E01D003D0FA
:100110000A2EFED7FAD7850E0A6E0A2EFED70000E9
:10012000090E2B6EFF0E2C6E97EC06F0016A81B65D
:10013000012A0150000A28E1256A83EC01F0270E0C
:100140000C6EFF0E0B6EFF0E0A6E0C2E01D007D048
:100150000B2E01D003D00A2EFED7FAD7F6D73F0ECA
:100160000B6EFF0E0A6E0B2E01D003D00A2EFED7A7
:10017000FAD7850E0A6E0A2EFED70000090E2B6EE6
:10018000FF0E2C6E97EC06F02250000A07E1010EDC
:100190002B6E0F0E2C6E97EC06F006D0010E2B6E18
:1001A00022C02CF097EC06F02650FF0A02E1238EC5
:1001B00002D07F0E2116020E2B6E23C02CF097EC7E
:1001C00006F02650FF0A02E11F8E02D07F0E1F1696
:1001D000030E2B6E1FC02CF097EC06F0040E2B6E56
:1001E00020C02CF097EC06F07F0E2316016A81B434
:1001F000012A0150000A3EE1235008080CE3225076
:10020000020A07E12350030A04E1236A226A0AEF83
:1002100001F0232A08D0236A2250010803E3222A8E
:1002200013EF01F0226A22C000F000360090003681
:1002300000900036009000360090F00E001600C0CE
:1002400022F000502310216E4DEC06F0D00E276EE8
:100250005BEC04F0020E276E5BEC04F021C027F08B
:100260005BEC04F0F1EC05F0016A81B4012A015065
:10027000000AFAE07F0E1F16016A81B8012A0150B8
:10028000000A3FE12050080802E3202A11D01F5045
:10029000050A06E12050090A03E1206A1F6A08D016
:1002A0001F50050803E31F2A206A02D01F6A206A34
:1002B0001FC000F00036009000360090003600901D
:1002C00000360090F00E001600C01FF00050201005
:1002D0001E6E4DEC06F0D00E276E5BEC04F0276A24
:1002E0005BEC04F0276A5BEC04F01EC027F05BECCB
:1002F00004F0F1EC05F0016A81B8012A0150000A0E
:10030000FAE09BD6FFD7166A176A186A18503108A8
:1003100018E32B6A6AEC05F00050162601501722EC
:10032000040E0B6EFF0E0A6E0B2E01D003D00A2EA8
:10033000FED7FAD73B0E0A6E0A2EFED70000182A07
:10034000E5D7320E046E000E056E16C000F017C021
:1003500001F0B5EC05F000C016F001C017F00EEC8E
:1003600006F0710E046E3D0E056E120E066E810EC5
:10037000076EDEEC02F0ECEC03F000C016F001C0FA
:1003800017F00A0E015C02E1AA0E005C000106E310
:10039000AA0E165E0A0E175A196A0AD000011650E4
:1003A000AA08166E17C017F00A0E1756010E196E1E
:1003B000000116C02BF017C02CF072EC03F000C047
:1003C00016F001C017F0090E2B6E2C6A97EC06F0A0
:1003D000195207E0010E2B6E010E2C6E97EC06F001
:1003E00005D0010E2B6E2C6A97EC06F016C000F0BB
:1003F00017C001F001320032019E01320032019E2D
:1004000001320032019E01320032019E0F0E0014B3
:10041000056E05C01AF017C000F0016A0F0E001437
:100420001B6E0550000A07E1030E2B6E7E0E2C6E2C
:1004300097EC06F059D01A50010A07E1030E2B6E13
:10044000300E2C6E97EC06F04FD01A50020A07E1DE
:10045000030E2B6E6D0E2C6E97EC06F045D01A50E5
:10046000030A07E1030E2B6E790E2C6E97EC06F053
:100470003BD01A50040A07E1030E2B6E330E2C6E8C
:1004800097EC06F031D01A50050A07E1030E2B6EE7
:100490005B0E2C6E97EC06F027D01A50060A07E187
:1004A000030E2B6E5F0E2C6E97EC06F01DD01A50CB
:1004B000070A07E1030E2B6E700E2C6E97EC06F008
:1004C00013D01A50080A07E1030E2B6E7F0E2C6E14
:1004D00097EC06F009D01A50090A06E1030E2B6EBC
:1004E0007B0E2C6E97EC06F01B50000A07E1030E02
:1004F0002B6E7E0E2C6E97EC06F059D01B50010A25
:1005000007E1020E2B6E300E2C6E97EC06F04FD0EA
:100510001B50020A07E1020E2B6E6D0E2C6E97EC3B
:1005200006F045D01B50030A07E1020E2B6E790E30
:100530002C6E97EC06F03BD01B50040A07E1020E2C
:100540002B6E330E2C6E97EC06F031D01B50050A43
:1005500007E1020E2B6E5B0E2C6E97EC06F027D097
:100560001B50060A07E1020E2B6E5F0E2C6E97ECF5
:1005700006F01DD01B50070A07E1020E2B6E700E0D
:100580002C6E97EC06F013D01B50080A07E1020E00
:100590002B6E7F0E2C6E97EC06F009D01B50090ACB
:1005A00006E1020E2B6E7B0E2C6E97EC06F0040E0D
:1005B0002B6E620E2C6E97EC06F012000001086A9A
:1005C000096A000E0762036464EF03F00250061824
:1005D0000A6E075003267E0ED8A0F5EF02F0035EE8
:1005E000D8B069EF03F0F9EF02F0035ED8A06EEF28
:1005F00003F0028E068E02C00EF001C00BF000C0A8
:100600000DF000500402F4CF0CF001500502F4CFBD
:1006100000F0F3CF08F00402F3500C26F450082247
:10062000000E00220D500502F3500C26F450082253
:10063000000E002202500402F3500826F45000225B
:1006400002500502016A000E0122F3500026F45008
:1006500001220D500602F3500826F4500022026ACF
:10066000000E012202220B500602F3500026F45025
:100670000122000E02220E500602F3500126F45011
:10068000022202BE4CEF03F0083600360136023675
:100690000306D8B46EEF03F05FEF03F0D880800E4E
:1006A00008640030000E002201220222D8A05FEF71
:1006B00003F0023201320032034A69EF03F00AAE5E
:1006C000029E000E71EF03F0010EAAEC06F071EF2E
:1006D00003F0020E8AEC06F071EF03F0040E6FECEB
:1006E00006F01200E80E046E030E056E2BC000F03B
:1006F0002CC001F0B5EC05F00C0E026E00C02DF020
:1007000001C02EF00250000105E02D362D902E364E
:10071000FF0FF9D7E80E046E030E056E41EC05F0ED
:1007200000502B5C2F6E01502C58306E2FC02BF0D8
:1007300030C02CF0640E046E000E056E2FC000F069
:1007400030C001F0B5EC05F000C003F0026A0250C1
:100750002D1203502E12640E046E000E056E41EC35
:1007600005F000502F5E0150305A2FC02BF030C0E2
:100770002CF00A0E046E000E056E2FC000F030C083
:1007800001F0B5EC05F000C002F001C003F0023644
:100790000290033602360290033602360290033688
:1007A00002360290033602502D1203502E120A0E0A
:1007B000046E000E056E41EC05F000502F5C006EDB
:1007C00001503058016E00C02BF001C02CF02D50AC
:1007D00000122E500112120000010B6A0C6A0A6A04
:1007E0000350D8B437EF04F0028E8E0E035E03BEC2
:1007F000FFEF03F0D8B4FFEF03F03BEF04F0036D1D
:10080000080E035CD8A016EF04F0036E013602C098
:1008100001F0026A080E035CD8A016EF04F001365E
:10082000016A0350D8A4D89020EF04F00350D8B444
:1008300020EF04F0D89002320132032E1AEF04F0B8
:100840000BBC01A02DEF04F0D8A02DEF04F0012A7D
:10085000D8B4022A02BE4AEF04F00AAE50EF04F008
:10086000021E011E012AD8B4022A50EF04F0026AC7
:10087000016A50EF04F00B800BAE54EF04F0026AF3
:100880000AAE54EF04F0FF0E026E016E0A36023219
:1008900054EF04F0010E0C6E60EC06F056EF04F01D
:1008A000000E0C6E56EF04F0FF0E0C6E0150006E41
:1008B0000250016E1200010E296E080E286E15C03E
:1008C000E9FF0F0EEA6E285222E0BCEC06F0BCEC09
:1008D00006F0A2EC06F0EF90BCEC06F0275200E028
:1008E000A2EC06F02736D8A079EF04F0EF827AEF79
:1008F00004F0EF920000BCEC06F0EF80120EE95E0F
:10090000EFA080EF04F0120EE9262806DCD7296A52
:10091000BCEC06F0A2EC06F00000EF90BCEC06F098
:10092000EF82BCEC06F0BCEC06F0295200E0EF8050
:10093000120EE95EEFA09AEF04F0296ABCEC06F013
:10094000EFB22980BCEC06F0BCEC06F0BCEC06F083
:10095000BCEC06F0BCEC06F0BCEC06F0BCEC06F01F
:10096000BCEC06F0A2EC06F0120EE926EF90EF9236
:1009700029C000F01200286A080E296E15C0E9FF90
:100980000F0EEA6E29521CE0BCEC06F00000EF826C
:10099000BCEC06F0285200E0EF80120EE95EEFA0FA
:1009A000CFEF04F0EFB2D880EFA2D8902836BCEC9D
:1009B00006F0A2EC06F0120EE926EF902906E2D727
:1009C000EF82BCEC06F0275202E0A2EC06F02750C2
:1009D000D8B4ECEF04F0EF920000BCEC06F0EF802E
:1009E000120EE95EEFA0F2EF04F0120EE926BCEC65
:1009F00006F0A2EC06F0EF90BCEC06F0A2EC06F0DC
:100A0000EF9228C000F0120000010C6A0E6A02503A
:100A1000D8A41EEF05F001C002F000C001F0006A8A
:100A20000E860250D8A41EEF05F001C002F0016A44
:100A30000E960E880250D8B430EF05F00E50035ECB
:100A4000D8A4D8A036EF05F0D89002BE3CEF05F050
:100A5000003601360236032E25EF05F036EF05F09D
:100A6000010E0C6EAAEC06F040EF05F0020E0C6EC3
:100A70006FEC06F040EF05F0040E0C6EB7EC06F0DC
:100A8000120000010B6A0A6A096A00500402F4CFDE
:100A900009F0F3CF08F001500502F4CF0BF0F3CFCB
:100AA0000AF000500502F3500926F4500A22E86AC1
:100AB0000B2201500402F3500926F4500A22E86A7E
:100AC0000B2208C000F009C001F00AC002F00BC000
:100AD00003F01200C26AC00EC212C18CC18E2BC0BC
:100AE00000F0003600900036009000360090005074
:100AF000C212C280B2EC06F0C284C2B4FED7080EA5
:100B0000026EC4CF00F0000E016E025005E0003608
:100B100000900136FF0FFAD7C3500012000E0112E9
:100B2000C2901200000E1C6E000E1D6E000E1E6E96
:100B3000000E1F6E000E206E000E216E000E226E43
:100B4000000E236E000E246E000E256E000E166E33
:100B5000000E176E000E1B6E000E1A6E000E196E40
:100B6000000E266E000E186E12000001086A096A57
:100B7000100E0C6E0134083609360450085E05501C
:100B8000D8A0053C095ED8B0CDEF05F0045008268A
:100B90000550D8B0053C0926D890003601360C2EF9
:100BA000BAEF05F01200090E2B6EFF0E2C6E97ECBB
:100BB00006F00A0E2B6E0F0E2C6E97EC06F00B0E45
:100BC0002B6E030E2C6E97EC06F00C0E2B6E010EA6
:100BD0002C6E97EC06F02B6A0F0E2C6E97EC06F03D
:100BE000120015C0E9FF0F0EEA6EA2EC06F0EF92BC
:100BF000BCEC06F0EF80120EE95EEFA0FDEF05F011
:100C0000120EE926BCEC06F0BCEC06F0BCEC06F0DB
:100C1000BCEC06F0EF82BCEC06F0120000010050C4
:100C2000086E0150006E016A026A036A0B6A0C6A60
:100C3000960E036E0A6A00C001F008C000F0086A50
:100C4000026A04EC05F01200949A94969488C66A9D
:100C50002750C6122950C6122850C712295205E043
:100C60002A50000AD8B4C78C03D02A52D8A4C78C03
:100C7000C68A120027C0E9FF28C0EAFF120E272407
:100C8000006E00C015F000C0E9FFEF82EF80120E89
:100C9000E95EEFA049EF06F0120015C0E9FF0F0E64
:100CA000EA6EEF82BCEC06F0EF80BCEC06F0A2EC42
:100CB00006F0EF92BCEC06F0A2EC06F0EF9012000A
:100CC00000010B800BAE6DEF06F0026A0AAEFF0E5C
:100CD000026E016E006E0A360232FF0E1200000133
:100CE0000B840BAE7BEF06F0010E036E026A016A05
:100CF000006A0A360232FF0E120031C0C9FF010E2F
:100D0000C714016E0150000A02E10000F8D7C9CFF4
:100D100032F0120000010B820BAE95EF06F0036873
:100D20000268016800680A360232FF0E1200829CD7
:100D30002BC031F07DEC06F02CC031F07DEC06F0DC
:100D4000828C1200090E155CE16E0F0EE26EE792C6
:100D5000E79012000001026A016A006A086A036AE9
:100D6000000E1200120E0A6E0A2EFED712000001AB
:100D70000AAE029E000E1200000000001200FFFFEB
:020000040030CA
:0E000000FFF9FFFFFFFFFFFFFFFFFFFFFFFF06
:00000001FF
0 nhận xét: