|
#include "stm32f4xx.h"
#include "delay.h"
#include "seg.h"
#include "timer.h"
unsigned int nian = 2020;
char yue = 7;
char ri = 16;
char shi = 9;
char fen = 31;
char miao = 30;
u8 bs=0;//存儲時間的變量
u8 date[8]={0,0,0,0,0,0,0,0};//數據
u16 address[8]={0x0101,0x0102,0x0104,0x0108,0x0110,0x0120,0x0140,0x0180};//位碼
char wendu[] = "Date:";
char shidu[] = "Time:";
#define LCD_DATA_PORT GPIOC
#define LCD_RS_Set() GPIO_SetBits(GPIOB, GPIO_Pin_8)
#define LCD_RS_Clr() GPIO_ResetBits(GPIOB, GPIO_Pin_8)
#define LCD_RW_Set() GPIO_SetBits(GPIOB, GPIO_Pin_9)
#define LCD_RW_Clr() GPIO_ResetBits(GPIOB, GPIO_Pin_9)
#define LCD_EN_Set() GPIO_SetBits(GPIOB, GPIO_Pin_10)
#define LCD_EN_Clr() GPIO_ResetBits(GPIOB, GPIO_Pin_10)
#define DATAOUT(x) GPIO_Write(GPIOC, x)
#define u8 unsigned char
void GPIO_Configuration(void);
void LCD1602_Wait_Ready(void); //判忙
void LCD1602_Write_Cmd(u8 cmd); //寫命令
void LCD1602_Write_Dat(u8 dat); //寫數據
void LCD1602_ClearScreen(void); //清屏
void LCD1602_Set_Cursor(u8 x, u8 y);
void LCD1602_Show_Str(u8 x, u8 y, char *str); //顯示
void LCD1602_Init(void); //初始化
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(LCD_DATA_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP; //上拉
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
void LCD1602_Wait_Ready(void) //檢測忙
{
int8_t sta=0;
DATAOUT(0xff); //PC全置1
LCD_RS_Clr(); //RS 0
LCD_RW_Set(); //RW 1
do
{
LCD_EN_Set(); //EN 1
delay_ms(10);
sta = GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_7); //讀取狀態字
LCD_EN_Clr(); //EN 0
}while(sta & 0x80);
}
void LCD1602_Write_Cmd(u8 cmd) //寫入指令
{
LCD1602_Wait_Ready(); //判忙
LCD_RS_Clr();
LCD_RW_Clr();
DATAOUT(cmd);
delay_us(10);
LCD_EN_Set();
LCD_EN_Clr();
}
void LCD1602_Write_Dat(u8 dat)
{
LCD1602_Wait_Ready();
LCD_RS_Set();
LCD_RW_Clr();
DATAOUT(dat);
delay_us(10);
LCD_EN_Set();
LCD_EN_Clr();
}
void LCD1602_ClearScreen(void) //清屏
{
LCD1602_Write_Cmd(0x01);
}
void LCD1602_Set_Cursor(u8 x, u8 y) //光標設置位置
{
u8 addr;
if (y == 0)
addr = 0x00 + x;
else
addr = 0x40 + x;
LCD1602_Write_Cmd(addr | 0x80);
}
void LCD1602_Show_Str(u8 x, u8 y, char *str)//字符串
{
LCD1602_Set_Cursor(x, y);
while(*str != '\0')
{
LCD1602_Write_Dat(*str++);
}
}
void LCD1602_Init(void)//初始化
{
GPIO_Configuration();
LCD1602_Write_Cmd(0x38);
LCD1602_Write_Cmd(0x0C);
LCD1602_Write_Cmd(0x06);
LCD1602_Write_Cmd(0x01);
}
int main(void)
{
TIM3_Int_Init(15-1,2100-1); //定時器3初始化
LCD1602_Init();
LCD1602_Show_Str(0,0,wendu); //日期顯示
LCD1602_Show_Str(0,2,shidu); //時間顯示
while(1)
{
get_time(); //計時
LCD1602_Write_Cmd( 0x80+5 );
LCD1602_Write_Dat( nian/1000 + 0x30 );//顯示年
LCD1602_Write_Dat( nian%1000/100 + 0x30 );
LCD1602_Write_Dat( nian%100/10 + 0x30 );
LCD1602_Write_Dat( nian%10 + 0x30 );
LCD1602_Write_Dat( '-' );
LCD1602_Write_Dat( yue/10 + 0x30 );//顯示月
LCD1602_Write_Dat( yue%10 + 0x30 );
LCD1602_Write_Dat( '-' );
LCD1602_Write_Dat( ri/10 + 0x30 );//顯示日
LCD1602_Write_Dat( ri%10 + 0x30 );
LCD1602_Write_Cmd( 0x80 +0x40+5);
LCD1602_Write_Dat( shi/10 + 0x30 ); //顯示時
LCD1602_Write_Dat( shi%10 + 0x30 );
LCD1602_Write_Dat( ':' );
LCD1602_Write_Dat( fen/10 + 0x30 );//顯示分
LCD1602_Write_Dat( fen%10 + 0x30 );
LCD1602_Write_Dat( ':' );
LCD1602_Write_Dat( miao/10 + 0x30 );//顯示秒
LCD1602_Write_Dat( miao%10 + 0x30 );
}
}
|
-
-
電子時鐘.rar
2020-7-16 22:27 上傳
點擊文件名下載附件
484.6 KB, 下載次數: 97
|