完成ds1302时间的设置与读取
This commit is contained in:
parent
531f06616b
commit
f65b593f80
11 changed files with 672 additions and 488 deletions
File diff suppressed because it is too large
Load diff
File diff suppressed because one or more lines are too long
|
|
@ -9,7 +9,7 @@ TriggerName=main
|
|||
LimitSize=0
|
||||
ByteLimit=50
|
||||
[DebugChecksum]
|
||||
Checksum=2065568720
|
||||
Checksum=-203845490
|
||||
[CodeCoverage]
|
||||
Enabled=_ 0
|
||||
[Exceptions]
|
||||
|
|
@ -48,6 +48,14 @@ CStepIntDis=_ 0
|
|||
TraceBufferSize=0x00010000
|
||||
TraceStallIfFIFOFull=0x00000000
|
||||
TracePortSize=0x00000004
|
||||
[Trace1]
|
||||
Enabled=0
|
||||
ShowSource=1
|
||||
[Disassemble mode]
|
||||
mode=0
|
||||
[Breakpoints2]
|
||||
Bp0=_ 1 "EMUL_CODE" "{$PROJ_DIR$\..\src\app\main.c}.28.5" 0 0 1 "" 0 "" 0
|
||||
Count=1
|
||||
[Log file]
|
||||
LoggingEnabled=_ 0
|
||||
LogFile=_ ""
|
||||
|
|
@ -55,6 +63,9 @@ Category=_ 0
|
|||
[TermIOLog]
|
||||
LoggingEnabled=_ 0
|
||||
LogFile=_ ""
|
||||
[Aliases]
|
||||
Count=0
|
||||
SuppressDialog=0
|
||||
[CallStackLog]
|
||||
Enabled=0
|
||||
[DriverProfiling]
|
||||
|
|
@ -63,11 +74,3 @@ Mode=1
|
|||
Graph=0
|
||||
Symbiont=0
|
||||
Exclusions=
|
||||
[Disassemble mode]
|
||||
mode=0
|
||||
[Breakpoints2]
|
||||
Bp0=_ 0 "EMUL_CODE" "{$PROJ_DIR$\..\src\app\main.c}.81.13" 0 0 1 "" 0 "" 0
|
||||
Count=1
|
||||
[Aliases]
|
||||
Count=0
|
||||
SuppressDialog=0
|
||||
|
|
|
|||
|
|
@ -47,6 +47,12 @@
|
|||
#define TFT_UARTX UART2 // PTD2,PTD3
|
||||
|
||||
|
||||
/*
|
||||
* 时间模块
|
||||
*/
|
||||
#define DS1302_CE_PINX PTB2
|
||||
#define DS1302_CLK_PINX PTB1
|
||||
#define DS1302_IO_PINX PTB0
|
||||
|
||||
|
||||
#endif // CONFIG_H
|
||||
|
|
|
|||
|
|
@ -4,8 +4,25 @@
|
|||
|
||||
#include <stdint.h>
|
||||
|
||||
typedef struct time_info_
|
||||
{
|
||||
uint16_t year;
|
||||
uint8_t month;
|
||||
uint8_t week;
|
||||
uint8_t day;
|
||||
uint8_t hour;
|
||||
uint8_t minute;
|
||||
uint8_t sec;
|
||||
} time_info;
|
||||
|
||||
|
||||
|
||||
uint8_t is_leapyear(uint16_t year);
|
||||
|
||||
|
||||
|
||||
uint8_t get_month_days(uint16_t year, uint8_t month);
|
||||
|
||||
void ds1302_init(void);
|
||||
|
||||
void ds1302_set_time(time_info time);
|
||||
|
||||
void ds1302_read_time(time_info *time);
|
||||
|
|
|
|||
|
|
@ -7,17 +7,26 @@
|
|||
#include "include/key.h"
|
||||
#include "include/knob.h"
|
||||
#include "include/tft.h"
|
||||
#include "include/config.h"
|
||||
#include "include/time.h"
|
||||
|
||||
|
||||
void main(void)
|
||||
{
|
||||
int32_t log = 0, knob_v;
|
||||
time_info time = {
|
||||
2016, 4, 1, 18, 14, 9, 0
|
||||
};
|
||||
|
||||
pm_init();
|
||||
|
||||
st_init(0, COMPARE, 10);
|
||||
st_init(1, COMPARE, 1);
|
||||
|
||||
|
||||
st_init(2, COMPARE, 3);
|
||||
ds1302_set_time(time);
|
||||
|
||||
knob_enable();
|
||||
|
||||
while (1)
|
||||
|
|
@ -31,6 +40,15 @@ void main(void)
|
|||
log = knob_v;
|
||||
}
|
||||
}
|
||||
if (st_tcf(2) == 1)
|
||||
{
|
||||
enter_critical();
|
||||
ds1302_read_time(&time);
|
||||
exit_critical();
|
||||
printf("%d 年 %d 月 %d 星期 %d 日 %d 时 %d 分 %d 秒\n",
|
||||
time.year,time.month, time.week, time.day,
|
||||
time.hour, time.minute, time.sec);
|
||||
}
|
||||
|
||||
|
||||
switch (get_key_mean(UP_KEY))
|
||||
|
|
|
|||
|
|
@ -7,6 +7,8 @@
|
|||
#include "include/knob.h"
|
||||
#include "include/debug.h"
|
||||
#include "include/tft.h"
|
||||
#include "include/config.h"
|
||||
#include "include/time.h"
|
||||
|
||||
|
||||
/*
|
||||
|
|
@ -19,5 +21,6 @@ void pm_init(void)
|
|||
knob_init();
|
||||
st_base_init();
|
||||
tft_init();
|
||||
ds1302_init();
|
||||
return;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,8 +2,10 @@
|
|||
* time.c - 时间处理
|
||||
*/
|
||||
|
||||
#include "include/time.h"
|
||||
#include "gpio.h"
|
||||
|
||||
#include "include/time.h"
|
||||
#include "include/config.h"
|
||||
|
||||
|
||||
uint8_t is_leapyear(uint16_t year)
|
||||
|
|
@ -29,3 +31,148 @@ uint8_t get_month_days(uint16_t year, uint8_t month)
|
|||
return 31;
|
||||
}
|
||||
}
|
||||
|
||||
void ds1302_byte_write(uint8_t data)
|
||||
{
|
||||
gpio_init(DS1302_IO_PINX, 1, 0);
|
||||
for (uint8_t mask = 0x01; mask != 0; mask <<= 1)
|
||||
{
|
||||
gpio_set(DS1302_CLK_PINX, 0);
|
||||
if (data & mask)
|
||||
{
|
||||
gpio_set(DS1302_IO_PINX, 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
gpio_set(DS1302_IO_PINX, 0);
|
||||
}
|
||||
// tdc,200ns,数据建立时间,tcl,1000ns,sclk低电平保持时间
|
||||
for (uint8_t i = 0; i < 50; i++);
|
||||
gpio_set(DS1302_CLK_PINX, 1);
|
||||
// tcdh,280ns,数据采集时间,tch,1000ns,sclk高电平保持时间
|
||||
for (uint8_t i = 0; i < 50; i++);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
uint8_t ds1302_byte_read(void)
|
||||
{
|
||||
uint8_t data = 0;
|
||||
|
||||
//gpio_init(DS1302_IO_PINX, 0, 1);
|
||||
gpio_Interrupt_init(DS1302_IO_PINX, GPI_UP_PF, GPI_DISAB);
|
||||
gpio_set(DS1302_CLK_PINX, 1);
|
||||
// tccz,280ns,sclk到高阻态
|
||||
for (uint8_t i = 0; i < 50; i++);
|
||||
for (uint8_t mask = 0x01; mask != 0; mask <<= 1)
|
||||
{
|
||||
gpio_set(DS1302_CLK_PINX, 0); // 产生下降沿
|
||||
// tcdd,800ns,数据输出延迟
|
||||
for (uint8_t i = 0; i < 50; i++);
|
||||
for (uint8_t i = 0; i < 50; i++);
|
||||
if (gpio_get(DS1302_IO_PINX))
|
||||
{
|
||||
data |= mask;
|
||||
}
|
||||
gpio_set(DS1302_CLK_PINX, 1);
|
||||
// tccz,280ns,sclk到高阻态
|
||||
for (uint8_t i = 0; i < 50; i++);
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
uint8_t ds1302_single_read(uint8_t addr)
|
||||
{
|
||||
uint8_t cmd = 0,
|
||||
data = 0;
|
||||
|
||||
cmd = (1 << 7) | (addr << 1) | 1;
|
||||
/*
|
||||
* 初始化ce和clk状态
|
||||
*/
|
||||
gpio_set(DS1302_CE_PINX, 0);
|
||||
for (uint8_t i = 0; i < 50; i++);
|
||||
gpio_set(DS1302_CLK_PINX, 0);
|
||||
for (uint8_t i = 0; i < 50; i++);
|
||||
|
||||
gpio_set(DS1302_CE_PINX, 1);
|
||||
// tcc,ce到时钟建立时间,4us
|
||||
for (uint8_t i = 0; i < 200; i++);
|
||||
ds1302_byte_write(cmd);
|
||||
data = ds1302_byte_read();
|
||||
gpio_set(DS1302_CE_PINX, 0);
|
||||
// tcdz,ce到高阻态时间,280ns
|
||||
for (uint8_t i = 0; i < 20; i++);
|
||||
return data;
|
||||
}
|
||||
|
||||
void ds1302_single_write(uint8_t addr, uint8_t data)
|
||||
{
|
||||
uint8_t cmd = 0;
|
||||
|
||||
cmd = (1 << 7) | (addr << 1);
|
||||
/*
|
||||
* 初始化ce和clk状态
|
||||
*/
|
||||
gpio_set(DS1302_CE_PINX, 0);
|
||||
for (uint8_t i = 0; i < 50; i++);
|
||||
gpio_set(DS1302_CLK_PINX, 0);
|
||||
for (uint8_t i = 0; i < 50; i++);
|
||||
|
||||
gpio_set(DS1302_CE_PINX, 1);
|
||||
// tcc,ce到时钟建立时间,4us
|
||||
for (uint8_t i = 0; i < 200; i++);
|
||||
ds1302_byte_write(cmd);
|
||||
ds1302_byte_write(data);
|
||||
gpio_set(DS1302_CE_PINX, 0);
|
||||
return;
|
||||
}
|
||||
|
||||
void ds1302_set_time(time_info time)
|
||||
{
|
||||
ds1302_single_write(7, 0x00);
|
||||
ds1302_single_write(0, time.sec % 10 + (time.sec / 10 << 4));
|
||||
ds1302_single_write(1, time.minute % 10 + (time.minute / 10 << 4));
|
||||
ds1302_single_write(2, time.hour % 10 + (time.hour / 10 << 4));
|
||||
ds1302_single_write(3, time.day % 10 + (time.day / 10 << 4));
|
||||
ds1302_single_write(4, time.month % 10 + (time.month / 10 << 4));
|
||||
ds1302_single_write(5, time.week);
|
||||
ds1302_single_write(6, (time.year - 2000) % 10 + ((time.year - 2000) / 10 << 4));
|
||||
return;
|
||||
}
|
||||
|
||||
void ds1302_read_time(time_info *time)
|
||||
{
|
||||
uint8_t rval;
|
||||
|
||||
rval = ds1302_single_read(0);
|
||||
(*time).sec = (rval & 0x0f) + ((rval & 0x70) >> 4) * 10;
|
||||
rval = ds1302_single_read(1);
|
||||
(*time).minute = (rval & 0x0f) + ((rval & 0x70) >> 4) * 10;
|
||||
rval = ds1302_single_read(2);
|
||||
(*time).hour = (rval & 0x0f) + ((rval & 0x30) >> 4) * 10;
|
||||
rval = ds1302_single_read(3);
|
||||
(*time).day = (rval & 0x0f) + ((rval & 0x10) >> 4) * 10;
|
||||
rval = ds1302_single_read(4);
|
||||
(*time).month = (rval & 0x0f) + ((rval & 0x10) >> 4) * 10;
|
||||
rval = ds1302_single_read(5);
|
||||
(*time).week = rval & 0x07;
|
||||
rval = ds1302_single_read(6);
|
||||
(*time).year = (rval & 0x0f) + ((rval & 0xf0) >> 4) * 10 + 2000;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void ds1302_init(void)
|
||||
{
|
||||
//gpio_Interrupt_init(DS1302_CE_PINX, GPO, GPI_DISAB);
|
||||
//gpio_Interrupt_init(DS1302_CLK_PINX, GPO, GPI_DISAB);
|
||||
gpio_init(DS1302_CE_PINX, 1, 0);
|
||||
gpio_init(DS1302_CLK_PINX, 1, 0);
|
||||
gpio_Interrupt_init(DS1302_IO_PINX, GPI_UP_PF, GPI_DISAB);
|
||||
ds1302_single_write(7, 0x00);
|
||||
return;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@
|
|||
|
||||
#include "common.h"
|
||||
#include <stdarg.h>
|
||||
#include <string.h>
|
||||
|
||||
/********************************************************************/
|
||||
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@
|
|||
|
||||
#include "common.h"
|
||||
#include "uif.h"
|
||||
#include <string.h>
|
||||
|
||||
/********************************************************************/
|
||||
/*
|
||||
|
|
|
|||
File diff suppressed because one or more lines are too long
Loading…
Add table
Add a link
Reference in a new issue