这是一个上次写的基于STM32开发的万年历设计
单片机源程序如下:
- /* 下载完程序后要把BOOT1拔掉在重新上电即可看到效果
- 此程序最关键的是要做好io输入输出端口的配置,不能
- 使用GPIO_Mode_Out_OD模式,此不是双向IO口 */
- #include "public.h"
- #define DATA (GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_11|GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15)
- #define rs (GPIO_Pin_1)
- #define rw (GPIO_Pin_2)
- #define e (GPIO_Pin_0)
- #define io (GPIO_Pin_12)
- #define ce (GPIO_Pin_13)
- #define sck (GPIO_Pin_14)
- u8 num[]="0123456789";
- u8 time[7]={0x18, 0x19, 0x13, 0x01, 0x10, 0x06, 0x16};//秒分时日月周年
- u8 w[7]={0x80, 0x82, 0x84, 0x86, 0x88, 0x8a, 0x8c}; //写地址
- u8 r[7]={0x81, 0x83, 0x85, 0x87, 0x89, 0x8b, 0x8d}; //读地址
- u8 miao,fen,shi,zhou,ri,yue,nian;
- void GPIOINIT() //端口初始化
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- GPIO_InitStructure.GPIO_Pin=DATA|rs|rw|e;
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_Init(GPIOB,&GPIO_InitStructure);
-
- GPIO_PinRemapConfig(GPIO_Remap_SWJ_Disable,ENABLE);//把调试设置普通IO口
-
- GPIO_InitStructure.GPIO_Pin=ce|sck;
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_Init(GPIOA,&GPIO_InitStructure);
- /*
- GPIO_InitStructure.GPIO_Pin=io;
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_OD; //经过测试并不是双向IO口
- GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_Init(GPIOA,&GPIO_InitStructure);
- */
-
- }
- void IOOUTINIT() //io输出配置
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- GPIO_InitStructure.GPIO_Pin=io;
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_Init(GPIOA,&GPIO_InitStructure);
- }
- void IOININT() //io输入配置
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- GPIO_InitStructure.GPIO_Pin=io;
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
- GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_Init(GPIOA,&GPIO_InitStructure);
- }
- void RCCINIT() //系统初始化
- {
- SystemInit();
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
- //如果不加这条语句程序显示就会出错,即没有打开端口复用功能的时钟配置
- }
- u8 readbusy() //忙信号检测
- {
- u8 f;
- GPIO_ResetBits(GPIOB,rs);
- GPIO_SetBits(GPIOB,rw);
- GPIO_SetBits(GPIOB,e);
- f=((GPIO_ReadInputData(GPIOB)&0X8000));
- delayms(10);
- GPIO_ResetBits(GPIOB,e);
- return f;
- }
- void lcdwrc(u8 c)
- {
- while(readbusy());
- GPIO_ResetBits(GPIOB,rs);
- GPIO_ResetBits(GPIOB,rw);
- GPIO_ResetBits(GPIOB,e);
- delayms(1);
- GPIOB->BSRR = c<<8 & 0xf000; //将数据送到P0口
- GPIOB->BRR = ((~c)<<8) & 0xf000;
- delayms(1);
- GPIO_SetBits(GPIOB,e);
- delayms(1);
- GPIO_ResetBits(GPIOB,e);
- delayms(1);
- }
- void lcdwrc4bit(long c)
- {
- while(readbusy());
- GPIO_ResetBits(GPIOB,rs);
- GPIO_ResetBits(GPIOB,rw);
- GPIO_ResetBits(GPIOB,e);
- delayms(1);
- GPIOB->BSRR = c<<8 & 0xf000; //将数据送到P0口
- GPIOB->BRR = ((~c)<<8) & 0xf000;
- delayms(1);
- GPIO_SetBits(GPIOB,e);
- delayms(1);
- GPIO_ResetBits(GPIOB,e);
- delayms(1);
- GPIOB->BSRR = c<<12 & 0xf000; //将数据送到P0口
- GPIOB->BRR = ((~c)<<12) & 0xf000;
- delayms(1);
- GPIO_SetBits(GPIOB,e);
- delayms(1);
- GPIO_ResetBits(GPIOB,e);
- delayms(1);
- }
- void lcdwrd(long dat)
- {
- while(readbusy());
- GPIO_SetBits(GPIOB,rs);
- GPIO_ResetBits(GPIOB,rw);
- GPIO_ResetBits(GPIOB,e);
- delayms(1);
- GPIOB->BSRR = dat<<8 & 0xf000; //将数据送到P0口
- GPIOB->BRR = ((~dat)<<8) & 0xf000;
- delayms(1);
- GPIO_SetBits(GPIOB,e);
- delayms(1);
- GPIO_ResetBits(GPIOB,e);
- delayms(1);
- GPIOB->BSRR = dat<<12 & 0xf000; //将数据送到P0口
- GPIOB->BRR = ((~dat)<<12) & 0xf000;
- delayms(1);
- GPIO_SetBits(GPIOB,e);
- delayms(1);
- GPIO_ResetBits(GPIOB,e);
- delayms(1);
- GPIO_ResetBits(GPIOB,rs);
- }
- void lcdinit()
- {
- delayms(15);
- lcdwrc4bit(0x32);
- delayms(5);
- lcdwrc4bit(0x28);
- delayms(5);
- lcdwrc4bit(0x08);
- delayms(5);
- lcdwrc4bit(0x01);
- delayms(5);
- lcdwrc4bit(0x06);
- delayms(5);
- lcdwrc4bit(0x0c);
- delayms(5);
- }
- void ds1302writebyte(u8 dat) //单字节写
- {
- u8 i,value;
- GPIO_ResetBits(GPIOA,sck);
- delayus(20);
- IOOUTINIT();
- for(i=0;i<8;i++)
- {
- value=dat&0x01;
- if(value)
- GPIO_WriteBit(GPIOA,io,Bit_SET);
- else
- GPIO_WriteBit(GPIOA,io,Bit_RESET);
- dat>>=1;
- GPIO_SetBits(GPIOA,sck);
- delayus(20);
- GPIO_ResetBits(GPIOA,sck);
- delayus(20);
- }
- }
- void ds1302writebytes(u8 add,u8 dat) //多字节写
- {
- GPIO_ResetBits(GPIOA,ce);
- delayus(20);
- GPIO_SetBits(GPIOA,ce);
- delayus(20);
- ds1302writebyte(add);
- ds1302writebyte(dat);
- GPIO_ResetBits(GPIOA,ce);
- delayus(20);
- }
- u8 ds1302readbyte() //单字节读
- {
- u8 i,value;
- GPIO_ResetBits(GPIOA,sck);
- delayus(20);
- IOININT();
- for(i=0;i<8;i++)
- {
- value>>=1;
- if(GPIO_ReadInputDataBit(GPIOA,io)==1)
- {
- value|=0x80;
- }
- GPIO_SetBits(GPIOA,sck);
- delayus(20);
- GPIO_ResetBits(GPIOA,sck);
- delayus(20);
- }
- return value;
- }
- u8 ds1302readbytes(u8 add) //多字节读
- {
- u8 temp;
- GPIO_ResetBits(GPIOA,ce);
- delayus(20);
- GPIO_SetBits(GPIOA,ce);
- delayus(20);
- ds1302writebyte(add);
- temp=ds1302readbyte();
- GPIO_ResetBits(GPIOA,ce);
- delayus(20);
- GPIO_ResetBits(GPIOA,io);
- delayus(20);
- GPIO_SetBits(GPIOA,io); //释放IO
- delayus(20);
- return temp;
- }
- void settime()
- {
- u8 i;
- ds1302writebytes(0x8e,0x00);
- for(i=0;i<7;i++)
- {
- ds1302writebytes(w[i],time[i]);
- }
- ds1302writebytes(0x8e,0x80);
- }
- void readtime()
- {
- miao=ds1302readbytes(r[0]);
- fen=ds1302readbytes(r[1]);
- shi=ds1302readbytes(r[2]);
- zhou=ds1302readbytes(r[5]);
- ri=ds1302readbytes(r[3]);
- yue=ds1302readbytes(r[4]);
- nian=ds1302readbytes(r[6]);
- }
- void display()
- {
- lcdwrc4bit(0x00+0x80);
- lcdwrd(num[shi/16]);
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
DS1302万年历设计.7z
(178.13 KB, 下载次数: 229)
|