//POINT_COLOR=BLACK;
LCD_ShowString(30,40,210,24,24,"Today's Mission:");
LCD_ShowString(30,70,300,16,16,"1.Compelete keyboard entry program");
LCD_ShowString(30,100,400,16,16,"2.Try to use the interrupt method to do it");
LCD_ShowString(30,130,300,16,16,"3.Fill in application information"); //显示LCD ID
Time_Display();
key=KEY_Scan(0);
if(key)
{
switch(key)
{
case WKUP_PRES: //控制蜂鸣器
LCD_ShowString(30,160,210,24,24,"WKUP_PRES");
break;
case KEY1_PRES: //控制LED1翻转
LCD_ShowString(30,160,210,24,24,"KEY1_PRES");
break;
case KEY0_PRES: //同时控制LED0,LED1翻转
LCD_ShowString(30,160,210,24,24,"KEY0_PRES");
break;
}
}
#include "DS1302.h"
#define NOP() __NOP
#define DS1302_CLK_H() (GPIOE->BSRR=GPIO_Pin_0)
#define DS1302_CLK_L() (GPIOE->BRR=GPIO_Pin_0)
#define DS1302_RST_H() (GPIOE->BSRR=GPIO_Pin_6)
#define DS1302_RST_L() (GPIOE->BRR=GPIO_Pin_6)
#define DS1302_OUT_H() (GPIOE->BSRR=GPIO_Pin_5)
#define DS1302_OUT_L() (GPIOE->BRR=GPIO_Pin_5)
#define DS1302_IN_X (GPIOE->IDR&GPIO_Pin_5)
#define Time_24_Hour 0x00 //24时制控制
#define Time_Start 0x00 //开始走时
#define DS1302_SECOND 0x80 //DS1302各寄存器操作命令定义
#define DS1302_MINUTE 0x82
#define DS1302_HOUR 0x84
#define DS1302_DAY 0x86
#define DS1302_MONTH 0x88
#define DS1302_WEEK 0x8A
#define DS1302_YEAR 0x8C
#define DS1302_WRITE 0x8E
#define DS1302_POWER 0x90
extern unsigned char time[15];
static GPIO_InitTypeDef GPIO_InitStructure;
void DS1302_Configuration(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE, ENABLE);
/* PE4,5,6输出 */
GPIO_ResetBits(GPIOE,GPIO_Pin_0|GPIO_Pin_5|GPIO_Pin_6);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_5|GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //50M时钟速度
GPIO_Init(GPIOE, &GPIO_InitStructure);
}
static void DelayNOP(u32 count)
{
while(count--) NOP();
}
static void DS1302_OUT(void)
{
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOE, &GPIO_InitStructure);
}
static void DS1302_IN(void)
{
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOE, &GPIO_InitStructure);
}
void DS1302SendByte(u8 byte)
{
u8 i;
for(i=0x01;i;i<<=1)
{
if(byte&i) DS1302_OUT_H();
else DS1302_OUT_L();
DS1302_CLK_H();
DelayNOP(50); //加延时
DS1302_CLK_L();
}
}
u8 DS1302ReceiveByte(void)
{
u8 i,byte=0;
for(i=0x01;i;i<<=1)
{
if(DS1302_IN_X) byte |= i;
DS1302_CLK_H();
DelayNOP(50); //加延时
DS1302_CLK_L();
}
return(byte);
}
void Write1302(u8 addr,u8 data)
{
DS1302_OUT();
DS1302_RST_L();
DS1302_CLK_L();
DS1302_RST_H();
DelayNOP(100);
DS1302SendByte(addr);
DS1302SendByte(data);
DelayNOP(100);
DS1302_RST_L();
}
u8 Read1302(u8 addr)
{
u8 data=0;
DS1302_OUT();
DS1302_RST_L();
DS1302_CLK_L();
DS1302_RST_H();
DelayNOP(100);
DS1302SendByte(addr|0x01);
DS1302_IN();
data = DS1302ReceiveByte();
DelayNOP(100);
DS1302_RST_L();
return(data);
}
//读取时间函数
void DS1302_GetTime(u8 *time)
{
//u8 tmp;
time[0] = Read1302(DS1302_YEAR);
time[1] = Read1302(DS1302_WEEK);
time[2] = Read1302(DS1302_MONTH);
time[3] = Read1302(DS1302_DAY);
time[4] = Read1302(DS1302_HOUR);
time[5] = Read1302(DS1302_MINUTE);
time[6] = Read1302(DS1302_SECOND);
}
/*
读取DS1302中的RAM
addr:地址,从0到30,共31个字节的空间
返回为所读取的数据
*/
u8 ReadDS1302Ram(u8 addr)
{
u8 tmp,res;
tmp = (addr<<1)|0xc0;
res = Read1302(tmp);
return(res);
}
/*
写DS1302中的RAM
addr:地址,从0到30,共31个字节的空间
data:要写的数据
*/
void WriteDS1302Ram(u8 addr,u8 data)
{
u8 tmp;
Write1302(DS1302_WRITE,0x00); //关闭写保护
tmp = (addr<<1)|0xc0;
Write1302(tmp,data);
Write1302(DS1302_WRITE,0x80); //打开写保护
}
void ReadDSRam(u8 *p,u8 add,u8 cnt)
{
u8 i;
if(cnt>30) return;
for(i=0;i<cnt;i++)
{
*p = ReadDS1302Ram(add+1+i);
p++;
}
}
void WriteDSRam(u8 *p,u8 add,u8 cnt)
{
u8 i;
if(cnt>30) return;
for(i=0;i<cnt;i++)
{
WriteDS1302Ram(add+1+i,*p++);
}
}
/*
读时间函数,顺序为:年周月日时分秒
*/
void ReadDS1302Clock(u8 *p)
{
DS1302_OUT();
DS1302_RST_L();
DS1302_CLK_L();
DS1302_RST_H();
DelayNOP(100);
DS1302SendByte(0xbf); //突发模式
DS1302_IN();
time[5] = DS1302ReceiveByte(); //秒
time[4] = DS1302ReceiveByte(); //分
time[3] = DS1302ReceiveByte(); //时
time[2] = DS1302ReceiveByte(); //日
time[1] = DS1302ReceiveByte(); //月
DS1302ReceiveByte(); //周
time[0] = DS1302ReceiveByte(); //年
DS1302ReceiveByte(); //保护标志字节
DelayNOP(100);
DS1302_RST_L();
}
/*
写时间函数,顺序为:年周月日时分秒
*/
void WriteDS1302Clock(u8 *p)
{
Write1302(DS1302_WRITE,0x00); //关闭写保护
DS1302_OUT();
DS1302_RST_L();
DS1302_CLK_L();
DS1302_RST_H();
DelayNOP(100);
DS1302SendByte(0xbe); //突发模式
DS1302SendByte(p[5]); //秒
DS1302SendByte(p[4]); //分
DS1302SendByte(p[3]); //时
DS1302SendByte(p[2]); //日
DS1302SendByte(p[1]); //月
DS1302SendByte(0x01); //周,设置成周一,没有使用
DS1302SendByte(p[0]); //年
DS1302SendByte(0x80); //保护标志字节
DelayNOP(100);
DS1302_RST_L();
}
void InitClock(void)
{
u8 tmp;
DS1302_Configuration();
tmp = ReadDS1302Ram(0);
if(tmp^0xa5)
{
WriteDS1302Ram(0,0xa5);
Write1302(DS1302_WRITE,0x00); //关闭写保护
Write1302(0x90,0x03); //禁止涓流充电
Write1302(DS1302_HOUR,0x00); //设置成24小时制
Write1302(DS1302_SECOND,0x00); //使能时钟运行
Write1302(DS1302_WRITE,0x80); //打开写保护
}
}
/*
void TestDS1302(void)
{
u8 i,tt[7],dd1[30],dd2[30];
DS1302_Configuration();
InitClock();
tt[0] = 0x13;
tt[1] = 0x05;
tt[2] = 0x23;
tt[3] = 0x09;
tt[4] = 0x25;
tt[5] = 0x00;
WriteDS1302Clock(tt);
for(i=0;i<30;i++)
{
dd1[i] = i;
dd2[i] = 0;
}
WriteDSRam(dd1,0,30);
ReadDSRam(dd2,0,30);
while(1)
{
ReadDS1302Clock(tt);
}
}
*/
//end
这是ds1302的.c文件
#include "led.h"
#include "delay.h"
#include "key.h"
#include "sys.h"
#include "lcd.h"
#include "usart.h"
#include "DS1302.h"
#include "stdio.h"
unsigned char time[15];
//static u8 TIMET[8];
//static uint8_t shi[]={0,0,0,0,0,0,0};
/************************************************
ALIENTEK精英STM32开发板实验13
TFTLCD显示实验
技术支持:www.openedv.com
淘宝店铺:http://eboard.taobao.com
关注微信公众平台微信号:"正点原子",免费获取STM32资料。
广州市星翼电子科技有限公司
作者:正点原子 @ALIENTEK
************************************************/
void Time_Display()
{
ReadDS1302Clock(time);
if(time[2])
{
switch(time[2])
{
case 0x00CD_ShowString(30,190,300,16,16,"Nothing ");break;
case 0x01CD_ShowString(30,190,300,16,16,"Monday ");break;
case 0x02CD_ShowString(30,190,300,16,16,"Tuesday ");break;
case 0x30CD_ShowString(30,190,300,16,16,"Wednesday");break;
case 0x04CD_ShowString(30,190,300,16,16,"Thursday ");break;
case 0x05CD_ShowString(30,190,300,16,16,"Friday ");break;
case 0x06CD_ShowString(30,190,300,16,16,"Saturday ");break;
case 0x07CD_ShowString(30,190,300,16,16,"Sunday");break;
}
LCD_ShowxNum(30,190,time[2],5,16,0);
}
if(time[5])
{
LCD_ShowxNum(30,210,time[5],5,16,0);
}
//DS1302_ReadTime(0x89,TIMET);
if(time[4])
{
LCD_ShowxNum(30,230,time[4],5,16,0);
}
if(time[6])
{
LCD_ShowString(30,250,300,16,16,"SomethingT3");
}
}
int main(void)
{
u8 key=0;
u8 lcd_id[12]; //存放LCD ID字符串
delay_init(); //延时函数初始化
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级
uart_init(115200); //串口初始化为115200
LED_Init(); //LED端口初始化
LCD_Init();
KEY_Init();
POINT_COLOR=BLACK;
InitClock();
//BACK_COLOR=0X7D7C;
sprintf((char*)lcd_id,"LCD ID:%04X",lcddev.id);//将LCD ID打印到lcd_id数组。
while(1)
{
//POINT_COLOR=BLACK;
LCD_ShowString(30,40,210,24,24,"Today's Mission:");
LCD_ShowString(30,70,300,16,16,"1.Compelete keyboard entry program");
LCD_ShowString(30,100,400,16,16,"2.Try to use the interrupt method to do it");
LCD_ShowString(30,130,300,16,16,"3.Fill in application information"); //显示LCD ID
Time_Display();
key=KEY_Scan(0);
if(key)
{
switch(key)
{
case WKUP_PRES: //控制蜂鸣器
LCD_ShowString(30,160,210,24,24,"WKUP_PRES");
break;
case KEY1_PRES: //控制LED1翻转
LCD_ShowString(30,160,210,24,24,"KEY1_PRES");
break;
case KEY0_PRES: //同时控制LED0,LED1翻转
LCD_ShowString(30,160,210,24,24,"KEY0_PRES");
break;
}
}
//LED0=!LED0;
//delay_ms(1300);
//LCD_Clear(WHITE);
}
}
站长老大。。。
一周热门 更多>