|
(原创)51单片机高精度计时器电路原理图。采用AT89S52加DP8573,实现两个功能:带掉电保持功能的日历时钟、由外部开关信号触发的高精度计时,pcb文件与原理图配套,完全对应,可直接加工制作.
以上所有文件打包下载:
源码pcb等资料.rar
(206.35 KB, 下载次数: 12)
- //********************************************************************
- // Author : Bing Wang
- // Date : Mar. 2017
- // File : msTIMER.c
- // Hardware : 89S51/89S52
- //********************************************************************
- #include <AT89X52.h> // SFR definition file.
- #include <stdio.h>
- #include <absacc.h> //
- #include <ctype.h> //
- #include <stdlib.h> //
- #include <math.h>
- // Interface address definition
- // DP8573AN RTC
- xdata char DP8573SEC _at_ 0x0600;
- xdata char DP8573MIN _at_ 0x0700;
- xdata char DP8573HOUR _at_ 0x0800;
- xdata char DP8573WEEK _at_ 0x0E00;
- xdata char DP8573DAY _at_ 0x0900;
- xdata char DP8573MONTH _at_ 0x0A00;
- xdata char DP8573YEAR _at_ 0x0B00;
- xdata char DP8573SECs _at_ 0x1900; // Second Save
- xdata char DP8573MINs _at_ 0x1A00; // Minute Save
- xdata char DP8573HOURs _at_ 0x1B00; // Hour Save
- xdata char DP8573DAYs _at_ 0x1C00; // Day Save
- xdata char DP8573MONs _at_ 0x1D00; // Month Save
- xdata char DP8573MSR _at_ 0x0000; // Main Status Register
- xdata char DP8573PFR _at_ 0x0400; // Periodic Flag Register
- xdata char DP8573TSCR _at_ 0x0400; // Time Save Control Register
- xdata char DP8573RTMR _at_ 0x0100; // Real Time Mode Register
- xdata char DP8573OMR _at_ 0x0200; // Output Mode Register
- xdata char DP8573ICR0 _at_ 0x0300; // Interrupt Control Register 0
- xdata char DP8573ICR1 _at_ 0x0400; // Interrupt Control Register 1
- unsigned char msCNT, Tsecond;
- unsigned char SecRTC, MinRTC, HourRTC, WeekRTC, DayRTC, MonthRTC, YearRTC;
- unsigned char CmdOld, KeyCmd;
- unsigned char second, secondOld, minute, minuteOld, hour, hourOld;
- unsigned char day, dayOld, month, monthOld, year, yearOld, timeH, timeL;
- void UART_Init(void)
- {
- SCON = 0x50; // Setup serial port ctrl reg
- // Mode 1: 8-bit uart var. baud rate
- // REN: enable receiver
- PCON &= 0x7F; // Clear SMOD bit in power ctrl reg
- // This bit doubles the baud rate
- TMOD &= 0xCF; // Clear M1 and M0 for timer 1
- TMOD |= 0x20; // Set M1 for 8-bit autoreload timer
- // TMOD: Timer 1 Mode 2: 8-bit auto-reload timer
- TH1 = 213; // 1200 baud with 20MHz xtal
- TR1 = 1; // Start timer 1
- TI = 1; // Set TI to indicate ready to xmit
- }
- void DP8573A_Init(void)
- {
- DP8573MSR = 0x40; // RS=1
- DP8573ICR0 = 0x04; // Interrupt every 1sec
- DP8573RTMR = 0x00; // 24 hour mode + stop timer
- DP8573MSR = 0x00; // RS=0
- DP8573PFR = 0x00; // Clear INT flags + battery backup
- DP8573TSCR = 0x80; // Time save enable
- DP8573SECs = 0x00; // Clear saved second
- DP8573MINs = 0x00; // Clear saved minute
- DP8573HOURs = 0x00; // Clear saved hour
- DP8573DAYs = 0x00; // Clear saved day
- DP8573MONs = 0x00; // Clear saved month
- DP8573MSR = 0x44; // RS=1, clear INT flag
- DP8573RTMR = 0x08; // Clock start
- }
- void delay1s(void)
- {
- unsigned int k;
- for (k=0;k<40000;k++);
- }
- void delay100ms(void)
- {
- unsigned int k;
- for (k=0;k<2000;k++);
- }
- unsigned char KeyScan(void)
- {
- unsigned char keypad;
- keypad = P1;
- keypad &= 0x1D;
- switch (keypad)
- {
- case 0x1C:
- keypad = 1;
- break;
- case 0x19:
- keypad = 2;
- break;
- case 0x15:
- keypad = 3;
- break;
- case 0x0D:
- keypad = 4;
- break;
- default:
- keypad = 0;
- break;
- }
- return keypad;
- }
- void AdjustRTC(void)
- {
- unsigned char EndCMD, Para, ParaOld, ParaValue;
- SecRTC = DP8573SEC;
- SecRTC = (SecRTC>>4)*10 + (SecRTC%16);
- MinRTC = DP8573MIN;
- MinRTC = (MinRTC>>4)*10 + (MinRTC%16);
- HourRTC = DP8573HOUR;
- HourRTC = (HourRTC>>4)*10 + (HourRTC%16);
- DayRTC = DP8573DAY;
- DayRTC = (DayRTC>>4)*10 + (DayRTC%16);
- MonthRTC = DP8573MONTH;
- MonthRTC = (MonthRTC>>4)*10 + (MonthRTC%16);
- YearRTC = DP8573YEAR;
- YearRTC = (YearRTC>>4)*10 + (YearRTC%16);
- EndCMD = 0;
- Para = ParaOld = 0;
- CmdOld = 0;
- while (!EndCMD) {
- if (KeyCmd != CmdOld) {
- switch (KeyCmd) {
- case 1: // Setting
- Para += 1;
- if (Para >= 7) { Para = 0; EndCMD = 1; }
- break;
- case 2: // +
- switch(Para) {
- case 1: // Year
- if (YearRTC >= 99) YearRTC = 0;
- else YearRTC++;
- putchar(16); // Set cursor to the last 2 chars
- putchar(14);
- putchar((YearRTC/10)+48);
- putchar((YearRTC%10)+48);
- break;
- case 2: // Month
- if (MonthRTC >= 12) MonthRTC = 1;
- else MonthRTC++;
- putchar(16); // Set cursor to the last 2 chars
- putchar(14);
- putchar((MonthRTC/10)+48);
- putchar((MonthRTC%10)+48);
- break;
- case 3: // Day
- if (DayRTC >= 31) DayRTC = 1;
- else DayRTC++;
- putchar(16); // Set cursor to the last 2 chars
- putchar(14);
- putchar((DayRTC/10)+48);
- putchar((DayRTC%10)+48);
- break;
- case 4: // Hour
- if (HourRTC >= 24) HourRTC = 0;
- else HourRTC++;
- putchar(16); // Set cursor to the last 2 chars
- putchar(14);
- putchar((HourRTC/10)+48);
- putchar((HourRTC%10)+48);
- break;
- case 5: // Minute
- if (MinRTC >= 59) MinRTC = 0;
- else MinRTC++;
- putchar(16); // Set cursor to the last 2 chars
- putchar(14);
- putchar((MinRTC/10)+48);
- putchar((MinRTC%10)+48);
- break;
- case 6: // Second
- if (SecRTC >= 59) SecRTC = 0;
- else SecRTC++;
- putchar(16); // Set cursor to the last 2 chars
- putchar(14);
- putchar((SecRTC/10)+48);
- putchar((SecRTC%10)+48);
- break;
- default:
- break;
- }
- break;
- case 3: // -
- switch(Para) {
- case 1: // Year
- if (YearRTC == 0) YearRTC = 99;
- else YearRTC--;
- putchar(16); // Set cursor to the last 2 chars
- putchar(14);
- putchar((YearRTC/10)+48);
- putchar((YearRTC%10)+48);
- break;
- case 2: // Month
- if (MonthRTC == 1) MonthRTC = 12;
- else MonthRTC--;
- putchar(16); // Set cursor to the last 2 chars
- putchar(14);
- putchar((MonthRTC/10)+48);
- putchar((MonthRTC%10)+48);
- break;
- case 3: // Day
- if (DayRTC == 1) DayRTC = 31;
- else DayRTC--;
- putchar(16); // Set cursor to the last 2 chars
- putchar(14);
- putchar((DayRTC/10)+48);
- putchar((DayRTC%10)+48);
- break;
- case 4: // Hour
- if (HourRTC == 0) HourRTC = 24;
- else HourRTC--;
- putchar(16); // Set cursor to the last 2 chars
- putchar(14);
- putchar((HourRTC/10)+48);
- putchar((HourRTC%10)+48);
- break;
- case 5: // Minute
- if (MinRTC == 0) MinRTC = 59;
- else MinRTC--;
- putchar(16); // Set cursor to the last 2 chars
- putchar(14);
- putchar((MinRTC/10)+48);
- putchar((MinRTC%10)+48);
- break;
- case 6: // Second
- if (SecRTC == 0) SecRTC = 59;
- else SecRTC--;
- putchar(16); // Set cursor to the last 2 chars
- putchar(14);
- putchar((SecRTC/10)+48);
- putchar((SecRTC%10)+48);
- break;
- default:
- break;
- }
- break;
- case 4: // Enter
- ParaValue = ((SecRTC/10)*16) + (SecRTC%10);
- SecRTC = ParaValue;
- ParaValue = ((MinRTC/10)*16) + (MinRTC%10);
- MinRTC = ParaValue;
- ParaValue = ((HourRTC/10)*16) + (HourRTC%10);
- HourRTC = ParaValue;
- ParaValue = ((DayRTC/10)*16) + (DayRTC%10);
- DayRTC = ParaValue;
- ParaValue = ((MonthRTC/10)*16) + (MonthRTC%10);
- MonthRTC = ParaValue;
- ParaValue = ((YearRTC/10)*16) + (YearRTC%10);
- YearRTC = ParaValue;
-
- DP8573MSR = 0x40; // RS=1
- DP8573RTMR = 0x00; // 24 hour mode + stop timer
- DP8573SEC = SecRTC;
- DP8573MIN = MinRTC;
- DP8573HOUR = HourRTC;
- DP8573DAY = DayRTC;
- DP8573MONTH = MonthRTC;
- DP8573YEAR = YearRTC;
- DP8573MSR = 0x40; // RS=1
- DP8573RTMR = 0x08; // 24 hour mode + run timer
- EndCMD = 1;
- Para = 0;
- break;
- default:
- break;
- }
- // Display setting
- if (ParaOld != Para) {
- putchar(13);
- ParaOld = Para;
- switch(Para) {
- case 1: // Year
- printf("Year:");
- putchar(16); // Set cursor to the last 2 chars
- putchar(14);
- putchar((YearRTC/10)+48);
- putchar((YearRTC%10)+48);
- break;
- case 2: // Month
- printf("Month:");
- putchar(16); // Set cursor to the last 2 chars
- putchar(14);
- putchar((MonthRTC/10)+48);
- putchar((MonthRTC%10)+48);
- break;
- case 3: // Day
- printf("Day:");
- putchar(16); // Set cursor to the last 2 chars
- putchar(14);
- putchar((DayRTC/10)+48);
- putchar((DayRTC%10)+48);
- break;
- case 4: // Hour
- printf("Hour:");
- putchar(16); // Set cursor to the last 2 chars
- putchar(14);
- putchar((HourRTC/10)+48);
- putchar((HourRTC%10)+48);
- break;
- case 5: // Minute
- printf("Minute:");
- putchar(16); // Set cursor to the last 2 chars
- putchar(14);
- putchar((MinRTC/10)+48);
- putchar((MinRTC%10)+48);
- break;
- case 6: // Second
- printf("Second:");
- putchar(16); // Set cursor to the last 2 chars
- putchar(14);
- putchar((SecRTC/10)+48);
- putchar((SecRTC%10)+48);
- break;
- default:
- break;
- } // End SWITCH
- } // End if(ParaOld != Para)
- } // End if(KeyCmd != CmdOld)
- CmdOld = KeyCmd;
- KeyCmd = KeyScan();
- } // End while(!EndCMD)
- // CmdOld = KeyCmd;
- // putchar(7); // Cursor back 1 char
- // putchar(23); // Cursor blinking
- }
- void DisplayRTC(void)
- {
- putchar(13);
- hour = DP8573HOUR; // Hour
- timeH = hour >> 4;
- putchar(timeH+48);
- timeL = hour % 16;
- putchar(timeL+48);
- putchar(':');
-
- minute = DP8573MIN; // Minute
- timeH = minute >> 4;
- putchar(timeH+48);
- timeL = minute % 16;
- putchar(timeL+48);
- putchar(':');
- second = DP8573SEC; // Second
- timeH = second >> 4;
- putchar(timeH+48);
- timeL = second % 16;
- putchar(timeL+48);
- putchar(' ');
- putchar(' ');
- year = DP8573YEAR; // Year
- timeH = year >> 4;
- putchar(timeH+48);
- putchar(25); // .
- putchar(3);
- timeL = year % 16;
- putchar(timeL+48);
- month = DP8573MONTH; // Month
- timeH = month >> 4;
- putchar(timeH+48);
- putchar(25); // .
- putchar(3);
- timeL = month % 16;
- putchar(timeL+48);
- day = DP8573DAY; // Day
- timeH = day >> 4;
- putchar(timeH+48);
- timeL = day % 16;
- putchar(timeL+48);
- secondOld = second;
- minuteOld = minute;
- hourOld = hour;
- yearOld = year;
- monthOld = month;
- dayOld = day;
- }
- void msTimer(void) interrupt 0 using 0 {
- if (++msCNT == 1) { /* 1s */
- Tsecond++; /* second counter */
- msCNT = 0; /* clear int counter */
- }
- }
- main()
- {
- UART_Init();
- delay1s();
- delay100ms();
- putchar(15); // Set VFD brightness
- delay100ms();
- putchar(0x20); // Lowest brightness to reduce power consumption
- // user-defined char #1: \05 \01; show with \01
- putchar(5); putchar(1);
- // SHI: 02 39 17 EE 45 A2 B1 C8 04 06 00
- putchar(0x02);
- putchar(0x39);
- putchar(0x17);
- putchar(0xEE);
- putchar(0x45);
- putchar(0xA2);
- putchar(0xB1);
- putchar(0xC8);
- putchar(0x04);
- putchar(0x06);
- putchar(0x00);
- // user-defined char #2: \05 \02; show with \02
- putchar(5); putchar(2);
- // JIAN: 40 17 90 4B A5 52 E9 54 BA 43 00
- putchar(0x40);
- putchar(0x17);
- putchar(0x90);
- putchar(0x4B);
- putchar(0xA5);
- putchar(0x52);
- putchar(0xE9);
- putchar(0x54);
- putchar(0xBA);
- putchar(0x43);
- putchar(0x00);
- // user-defined char #3: \05 \03; show with \03
- putchar(5); putchar(3);
- // RI: 00 1F 08 84 43 E1 10 88 7C 00 00
- putchar(0x00);
- putchar(0x1F);
- putchar(0x08);
- putchar(0x84);
- putchar(0x43);
- putchar(0xE1);
- putchar(0x10);
- putchar(0x88);
- putchar(0x7C);
- putchar(0x00);
- putchar(0x00);
- // user-defined char #4: \05 \04; show with \04
- putchar(5); putchar(4);
- // QI: 50 7F 95 4E E7 52 BB F4 AA 55 01 =or= 50 7D D4 AE 77 2A 9F EA A5 95 80
- putchar(0x50);
- putchar(0x7F);
- putchar(0x95);
- putchar(0x4E);
- putchar(0xE7);
- putchar(0x52);
- putchar(0xBB);
- putchar(0xF4);
- putchar(0xAA);
- putchar(0x55);
- putchar(0x01);
- putchar(22); // Hide cursor
- putchar(13); // Reset display
- putchar(1);
- putchar(2);
- putchar(':');
- putchar(16); // Set cursor position
- putchar(11);
- putchar(3);
- putchar(4);
- putchar('-');
- delay1s();
- DP8573A_Init();
- P1 = 0x7F; // P1: 1DO + 7DI
- delay1s();
- // Used to set RTC
- /* DP8573MSR = 0x40;
- DP8573RTMR = 0x00;
- DP8573MIN = 54;
- DP8573HOUR = 24;
- DP8573DAY = 24;
- DP8573MONTH = 3;
- DP8573YEAR = 7;
- DP8573MSR = 0x40;
- DP8573RTMR = 0x08;
- */
- DisplayRTC();
- delay1s();
- delay1s();
- CmdOld = KeyScan();
- while(1)
- {
- hour = DP8573HOUR; // Hour
- if (hour != hourOld) {
- putchar(16); // Set cursor position
- putchar(0);
- timeH = hour >> 4;
- putchar(timeH+48);
- timeL = hour % 16;
- putchar(timeL+48);
- putchar(':');
- hourOld = hour;
- }
- minute = DP8573MIN; // Minute
- if (minute != minuteOld) {
- putchar(16); // Set cursor position
- putchar(3);
- timeH = minute >> 4;
- putchar(timeH+48);
- timeL = minute % 16;
- putchar(timeL+48);
- putchar(':');
- minuteOld = minute;
- }
- second = DP8573SEC; // Second
- if (second != secondOld) {
- putchar(16); // Set cursor position
- putchar(6);
- timeH = second >> 4;
- putchar(timeH+48);
- timeL = second % 16;
- putchar(timeL+48);
- secondOld = second;
- }
- year = DP8573YEAR;
- if (year != yearOld) {
- putchar(16); // Set cursor position
- putchar(10);
- timeH = year >> 4;
- putchar(timeH+48);
- timeL = year % 16;
- putchar(timeL+48);
- yearOld = year;
- }
- month = DP8573MONTH;
- if (month != monthOld) {
- putchar(16); // Set cursor position
- putchar(12);
- timeH = month >> 4;
- putchar(timeH+48);
- timeL = month % 16;
- putchar(timeL+48);
- yearOld = year;
- }
- day = DP8573DAY;
- if (day != dayOld) {
- putchar(16); // Set cursor position
- putchar(14);
- timeH = day >> 4;
- putchar(timeH+48);
- timeL = day % 16;
- putchar(timeL+48);
- dayOld = day;
- }
- KeyCmd = KeyScan();
- if (KeyCmd == 1) { // RTC adjustment
- AdjustRTC();
- DisplayRTC();
- }
- }
- }
复制代码
|
|