矿石收音机论坛

 找回密码
 加入会员

QQ登录

只需一步,快速开始

搜索
楼主: Edward

我的51初学故事+初学作品:有功能菜单的DS1302+1602表

  [复制链接]
发表于 2011-11-15 05:59:46 | 显示全部楼层
本帖最后由 millwood 于 2011-11-15 06:01 编辑

here is an example as to how I would have structured it.

1) I would have split the files into a lcd module (lcd_4bit) and a ds1302 module (ds1302), plus a user module.
2) in the user module, I would simply call the functions defined by lcd_4bit.h/ds1302.h, with the confidence that they would work.
3) the lcd_4bit/ds1302 modules are written so that the user has the ability to customize the pins. recompile and the code will work.

here is an example, 100% simulated.
clock lcd1602 ds1302.PNG
notice that the lcd control pins and the ds1302 pins differ from your implementation.
回复 支持 反对

使用道具 举报

发表于 2011-11-15 06:02:13 | 显示全部楼层
to make it complete, here is the lcd_4bit module:

=============lcd_4bit.h================

//lcd1602 4bit interface

//hardware configuration
#define LCD_PORT                P0                //lcd data bits on p0
#define lcd_delay(n)        {NOP(); NOP(); NOP(); NOP();}
sbit LCD_EN=P3^6;                                //1602 LCDLCD_EN?,1602??????P0?
sbit LCD_RS=P3^4;                                //1602 LCD_RS?
sbit LCD_RW=P3^5;                                //1602 R/W?
//end hardware configuration

#ifndef unchar
  #define unchar unsigned char
#endif
#ifndef NOP
  #define NOP()                        _nop_()
#endif


bit lcdread(void);//LCD?????

void lcdwrite_cmd(unchar a);//LCD????

void lcdwrite_dat(unchar a);//LCD????

void lcdinit(void);


====================lcd_4bit.c=============

#include <reg52.h>                                //we use keil c51
#include <intrins.h>                        //we use _nop_()
#include "lcd_4bit.h"                        //we use 4-bit lcd1602 routines

bit lcdread(void)//LCD?????
{
        unchar t;
        LCD_PORT=0xff;
        LCD_RS=0;
        LCD_RW=1;
        LCD_EN=1;
        t/*ACC*/=LCD_PORT;
        LCD_EN=0;
        return (t&0x80)?1:0/*A7*/;
}

void lcdwrite_cmd(unchar a)//LCD????
{
        bit e=1;
        while(e)e=lcdread();
        LCD_RS=0;
        LCD_RW=0;
        LCD_PORT=a;
        LCD_EN=1;
        //_nop_();
        //_nop_();
        //_nop_();
        //_nop_();
        //_nop_();
        lcd_delay(0);
        LCD_EN=0;
}

void lcdwrite_dat(unchar a)//LCD????
{
        bit e=1;
        while(e)e=lcdread();
        LCD_RS=1;
        LCD_RW=0;
        LCD_PORT=a;
        LCD_EN=1;
        //_nop_();
        //_nop_();
        //_nop_();
        //_nop_();
        //_nop_();
        lcd_delay(0);
        LCD_EN=0;
}


void lcdinit(void) {
        LCD_PORT = 0;                                //clear the lcd data line
        LCD_EN = 0, LCD_RS = 0, LCD_RW = 0;        //clear the lcd control bits
        lcdwrite_cmd(0x38);//??????
        lcdwrite_cmd(0x18);//??????
        lcdwrite_cmd(0x0c);//??????
        lcdwrite_cmd(0x06);//??????
        lcdwrite_cmd(0x02);//??????
        lcdwrite_cmd(0x01);//??
        lcdwrite_cmd(0x84);//????
        //lcdwritecgram(1);           //????CGRAM
}
回复 支持 反对

使用道具 举报

发表于 2011-11-15 06:02:57 | 显示全部楼层
here is the ds1302 module

============ds1302.h==============

//ds1302 interface
/*
DS1302+1602LCD??????

?Edward??,??????????,????????????

??????:
STC89C52?1602?????????4??????

??????:
?????????????;
???12/24???;
??????;
2???,???1??;
???????;
????????;
???????????????(??2??????30?31?);
??S0?Set/OK,S1???,S2??????,S3???;

?Keil μVision2?Keil μVision4???????;

2011?8???
*/

#ifndef unchar
  #define unchar unsigned char
#endif

#ifndef NOP
  #define NOP()                        _nop_()
#endif

//hardware configuration
sbit DS1302_CE=P2^5;        //DS1302 DS1302_CE(LCD_RST)?
sbit DS1302_IO=P2^7;        //DS1302 I/O?
sbit DS1302_CL=P2^6;        //DS1302 SDS1302_CLK?
#define ds1302_delay(n)        {NOP(); NOP(); NOP(); NOP();}
//end hardware configuration

void ds1302write(unchar cmd,dat);//DS1302??

unchar ds1302read(unchar cmd);//DS1302??

unchar ds1302readbyte(void);//DS1302??1???,???????

void ds1302burstread(unchar *s);//DS1302????

void ds1302init(void);














============ds1302.c=============

/*
DS1302+1602LCD??????

?Edward??,??????????,????????????

??????:
STC89C52?1602?????????4??????

??????:
?????????????;
???12/24???;
??????;
2???,???1??;
???????;
????????;
???????????????(??2??????30?31?);
??S0?Set/OK,S1???,S2??????,S3???;

?Keil μVision2?Keil μVision4???????;

2011?8???
*/
#include<reg52.h>
#include<intrins.h>
#include "ds1302.h"                                        //we use ds1302

//#define unchar unsigned char
//#define ds1302_delay(n)        {NOP(); NOP(); NOP(); NOP();}
//sbit DS1302_CE=P2^5;        //DS1302 DS1302_CE(LCD_RST)?
//sbit DS1302_IO=P2^7;        //DS1302 I/O?
//sbit DS1302_CL=P2^6;        //DS1302 SDS1302_CLK?

void ds1302write(unchar cmd,dat)//DS1302??
{
        unchar i,t;
        DS1302_CE=0;
        DS1302_CL=0;
        _nop_();
        DS1302_CE=1;
        for(i=8;i>0;i--)//????
        {
                DS1302_CL=0;
                t=cmd;
                DS1302_IO=(bit)(t&0x01);
                cmd>>=1;
                DS1302_CL=1;
                //_nop_();
                //_nop_();
                //_nop_();
                ds1302_delay(0);
        }
        for(i=8;i>0;i--)//????
        {
                DS1302_CL=0;
                t=dat;
                DS1302_IO=(bit)(t&0x01);
                dat>>=1;
                DS1302_CL=1;
                //_nop_();
                //_nop_();
                //_nop_();
                ds1302_delay(0);
        }
        DS1302_CE=0;
        DS1302_CL=0;
}

unchar ds1302read(unchar cmd)//DS1302??
{
        unchar i,t;
        DS1302_IO=1;
        DS1302_CE=0;
        DS1302_CL=0;
        _nop_();
        DS1302_CE=1;
        for(i=8;i>0;i--)//????
        {
                t=cmd;
                DS1302_IO=(bit)(t&0x01);
                cmd>>=1;
                DS1302_CL=1;
                //_nop_();
                //_nop_();
                //_nop_();
                //_nop_();
                //_nop_();
                ds1302_delay(0);
                DS1302_CL=0;
                //_nop_();
        }
        //_nop_();//?????????????????
        NOP();
        t=0;                                        //reset the temp variable
        for(i=8;i>0;i--)//????
        {
                //ACC>>=1;
                t>>=1;
                //A7=DS1302_IO;
                if (DS1302_IO) t |= 0x80;
                else t |= 0x00;
                DS1302_CL=1;
                //_nop_();
                //_nop_();
                //_nop_();
                //_nop_();
                ds1302_delay(0);
                DS1302_CL=0;
                //_nop_();
                //_nop_();
                //_nop_();
                ds1302_delay(0);
        }
        DS1302_CE=0;
        return(t/*ACC*/);
}

unchar ds1302readbyte()//DS1302??1???,???????
{
        unchar i, t=0;
        for(i=8;i>0;i--)//????
        {
                //ACC=ACC>>1;
                t >>= 1;
                //A7=DS1302_IO;
                if (DS1302_IO) t |= 0x80;
                else t |= 0x00;
                DS1302_CL=1;
                //_nop_();
                //_nop_();
                //_nop_();
                //_nop_();
                ds1302_delay(0);
                DS1302_CL=0;
                //_nop_();
                //_nop_();
                //_nop_();
                ds1302_delay(0);
        }
        return(t/*ACC*/);
}

void ds1302burstread(unchar *s)//DS1302????
{
        unchar i, t;
        DS1302_CE=0;
        DS1302_CL=0;
        _nop_();
        DS1302_CE=1;
        //ACC=0xbf;
        t=0xbf;
        for(i=8;i>0;i--)//????
        {
                DS1302_IO=t & 0x01/*A0*/;
                DS1302_CL=1;
                //_nop_();
                //_nop_();
                //_nop_();
                //_nop_();
                //_nop_();
                ds1302_delay(0);
                DS1302_CL=0;
                //_nop_();
                //_nop_();
                t/*ACC*/>>=1;
        }
        for(i=8;i>0;i--)
        {
                *s=ds1302readbyte();
                s++;
        }
        DS1302_CE=0;
}

void ds1302init(void) {
        ds1302write(0x8e,0x00);//?1302???
        ds1302write(0x80,0x00);//???
        ds1302write(0x82,0x30);//???
        ds1302write(0x84,0x12);//???,????
        ds1302write(0x86,0x17);//???
        ds1302write(0x88,0x08);//???
        ds1302write(0x8a,0x03);//????
        ds1302write(0x8c,0x11);//???
        ds1302write(0x8e,0x80);//??1302??
}
回复 支持 反对

使用道具 举报

发表于 2011-11-15 06:04:23 | 显示全部楼层
本帖最后由 millwood 于 2011-11-15 06:06 编辑

here is the user module.
main.rar (4.76 KB, 下载次数: 159)
notice how it utilizes the lcd_4bit/ds1302 modules.
回复 支持 反对

使用道具 举报

发表于 2011-11-15 06:08:23 | 显示全部楼层
a couple quick suggestions:

1) your code could have been structured much better: many of the display functions are too tangled together.
2) you should reduce the use of global variables.
回复 支持 反对

使用道具 举报

发表于 2011-11-15 06:09:02 | 显示全部楼层
one more suggestion:

always, always, comment on your code.
回复 支持 反对

使用道具 举报

发表于 2011-11-15 06:10:46 | 显示全部楼层
about those two modules (lcd_4bit/ds1302).

you will notice that in the .h files, there is a section called "hardware configuration".

if you ever change the connections, you can simply redefine those pins there, and recompile the code. it will work, without any changes to the corresponding .c files.
回复 支持 反对

使用道具 举报

     
 楼主| 发表于 2011-11-16 21:30:58 | 显示全部楼层
about those two modules (lcd_4bit/ds1302).
you will notice that in the .h files, there is a secti ...
millwood 发表于 2011-11-15 06:10


非常感谢!最近有些忙,需要一些时间来研读一下,不能及时回复了。
回复 支持 反对

使用道具 举报

发表于 2011-11-17 23:36:57 | 显示全部楼层
本帖最后由 dghadu 于 2011-11-17 23:38 编辑
about those two modules (lcd_4bit/ds1302).

you will notice that in the .h files, there is a secti ...
millwood 发表于 2011-11-15 06:10



    这英语牛的,这里没人能看得懂!恕我直言,你是来和中国人探讨问题还是来显摆的???
回复 支持 反对

使用道具 举报

发表于 2011-11-17 23:54:26 | 显示全部楼层
回复 40# dghadu


    应该是一个能看懂中文,不会写中文的热心的对技术很执着的技术牛人!
回复 支持 反对

使用道具 举报

发表于 2011-11-18 00:07:50 | 显示全部楼层
了解了一下,也是中国音响DIY的朋友,在那边也全是英文发贴,那果真是看得懂中文却不会打中文,失敬,失敬!
回复 支持 反对

使用道具 举报

发表于 2011-11-18 00:22:47 | 显示全部楼层
回复 42# dghadu


    确实值得尊敬!特别是对技术执着的牛人!
回复 支持 反对

使用道具 举报

发表于 2011-11-18 00:36:23 | 显示全部楼层
本帖最后由 dghadu 于 2011-11-18 00:38 编辑

回复 43# 风之澜


    呵呵,只可惜看不懂英文,对他的技术水准还是很钦佩的。
回复 支持 反对

使用道具 举报

     
发表于 2011-11-18 11:57:24 | 显示全部楼层
好东西,先收藏。。。
回复 支持 反对

使用道具 举报

     
 楼主| 发表于 2011-11-18 12:48:52 | 显示全部楼层
这里没人能看得懂!
dghadu 发表于 2011-11-17 23:36


这个英语我觉得问题还是不大的,只要有高中英语水平,借助谷歌翻译,还是基本没问题的。主要是得耐心的去看。我这几天挺忙的,还没来得及研读,就是看了个大概意思。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 加入会员

本版积分规则

小黑屋|手机版|矿石收音机 ( 蒙ICP备05000029号-1 )

蒙公网安备 15040402000005号

GMT+8, 2024-5-5 11:05

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表