搜索
bottom↓
回复: 24

大家看看我们老师写的74hc595和165的头文件,我总觉得看起来不舒服,想听听大家的看法!!!

[复制链接]

出0入0汤圆

发表于 2010-10-16 18:27:54 | 显示全部楼层 |阅读模式
#ifndef _hc595_165_h_   
#define _hc595_165_h_

#define SETBIT(x,y) (x |= y)
#define CLRBIT(x,y) (x &= ~y)
#define CHKBIT(x,y) (x &  y)
#define CPLBIT(x,y) (x ^= y)

#define DeSelect595  CLRBIT(HC595_165_PORT,HC595_165_595CS)
#define Select595                CLRBIT(HC595_165_PORT,HC595_165_595CS);SETBIT(HC595_165_PORT,HC595_165_595CS);CLRBIT(HC595_165_PORT,HC595_165_595CS)
#define Select165                SETBIT(HC595_165_PORT,HC595_165_165CS)
#define DeSelect165                CLRBIT(HC595_165_PORT,HC595_165_165CS)
#define HC595_165_SetClock         SETBIT(HC595_165_PORT,HC595_165_Clock)
#define HC595_165_ClrClock        CLRBIT(HC595_165_PORT,HC595_165_Clock)
#define SetMOSI_595                SETBIT(HC595_165_PORT,HC595_165_595DataOut)
#define ClrMOSI_595                CLRBIT(HC595_165_PORT,HC595_165_595DataOut)
/*
****************************************************************************************************
HC595_165接口初始化
****************************************************************************************************
*/
void HC595_165_Init(void)
{
           SETBIT(HC595_165_DDR,HC595_165_Clock);
           #ifdef Per_HC595
                   SETBIT(HC595_165_DDR,HC595_165_595CS);
                   SETBIT(HC595_165_DDR,HC595_165_595DataOut);
                   DeSelect595;  
           #endif
           #ifdef Per_HC165
                   SETBIT(HC595_165_DDR,HC595_165_165CS);
                   CLRBIT(HC595_165_DDR,HC595_165_165DataIn);
                   DeSelect165;
           #endif       
           HC595_165_SetClock;
}
/*
****************************************************************************************************
从165读取1字节数据
****************************************************************************************************
*/
#ifdef Per_HC165
uint8 HC595_165_Read165Byte(void)
{
     uint8 i,j,indata;       
     indata=0;
     for(i=0;i<8;i++)
     {
            if(CHKBIT(HC595_165_PIN,HC595_165_165DataIn)) indata|=(0x80>>i);                                       
            HC595_165_ClrClock;
            for(j=0;j<=10;j++);                           
            HC595_165_SetClock;                       
      }       
       return indata;
}
#endif
/*
****************************************************************************************************
向595写1字节数据
****************************************************************************************************
*/
#ifdef Per_HC595
void HC595_165_Write595Byte(uint8 u8_OutData)
{
     uint8 i,j;       
     for(i=0;i<8;i++)
     {
           if(CHKBIT(u8_OutData,0x80>>i))
           {
                   SetMOSI_595;
           }
           else
           {
                 ClrMOSI_595;       
           }       
           HC595_165_ClrClock;               
           for(j=0;j<=10;j++);           
           HC595_165_SetClock;
           for(j=0;j<=10;j++);                   
     }       
     HC595_165_ClrClock;       
}
#endif
#endif

疑问:1、#define SETBIT(x,y) (x |= y)
         #define CLRBIT(x,y) (x &= ~y)
         #define CHKBIT(x,y) (x &  y)
         #define CPLBIT(x,y) (x ^= y)
         我觉得应该改为:
         #define SETBIT(x,y) (x |= (1<<y))
         #define CLRBIT(x,y) (x &= ~(1<<y))
         #define CHKBIT(x,y) (x &  (1<<y))
         #define CPLBIT(x,y) (x ^= (1<<y))
         但那样写的操作结果好像和下面的写法操作结果一样,大家都在使用这个文件,也没发现什么问题,想不通???
     2、595,165的操作中使用了过多的宏,看起来意思很不明朗(我是这么认为的),大家怎么看啊??


对齐的不是很好,大家凑合着看吧

大家有什么好的操作595的方法也可以晒晒啊,谢谢了

出0入296汤圆

发表于 2010-10-16 18:37:47 | 显示全部楼层
如果这是头文件,那确实有点问题……C语言头文件里面不应该放代码实体。

出0入0汤圆

发表于 2010-10-16 18:41:51 | 显示全部楼层
前面宏定义还挺好的,后面的函数就不要放在头文件里了~~~

出0入0汤圆

 楼主| 发表于 2010-10-16 18:46:40 | 显示全部楼层
回复【1楼】Gorgon Meducer 傻孩子
-----------------------------------------------------------------------

我们喜欢把h文件当c文件用,所以就这样写了

出0入0汤圆

 楼主| 发表于 2010-10-16 18:46:53 | 显示全部楼层
回复【1楼】Gorgon Meducer 傻孩子
-----------------------------------------------------------------------

我们喜欢把h文件当c文件用,所以就这样写了

出0入296汤圆

发表于 2010-10-18 00:11:50 | 显示全部楼层
这种方式不支持多文件编译的……

出0入0汤圆

 楼主| 发表于 2010-10-18 12:18:57 | 显示全部楼层
回复【5楼】Gorgon Meducer 傻孩子
-----------------------------------------------------------------------

这个是我改过的(只是加了开头和末尾的#ifndef ————   #endif),其实它支持多文件编译,目前有20多个,下面是哪个软件的画面

(原文件名:1-1.jpg)


(原文件名:2-2.jpg)


(原文件名:3-3.jpg)

出0入0汤圆

 楼主| 发表于 2010-10-18 12:26:16 | 显示全部楼层
回复【5楼】Gorgon Meducer 傻孩子
-----------------------------------------------------------------------

请问傻孩子,这几个宏定义为什么正确呢
#define SETBIT(x,y) (x |= y)
#define CLRBIT(x,y) (x &= ~y)
#define CHKBIT(x,y) (x &  y)
#define CPLBIT(x,y) (x ^= y)

他和     #define SETBIT(x,y) (x |= (1<<y))
         #define CLRBIT(x,y) (x &= ~(1<<y))
         #define CHKBIT(x,y) (x &  (1<<y))
         #define CPLBIT(x,y) (x ^= (1<<y))
的本质上有区别吗?

出0入0汤圆

发表于 2010-10-18 12:32:52 | 显示全部楼层
貌似用的自己用C#开发的集成开发环境?

出0入0汤圆

发表于 2010-10-18 12:37:47 | 显示全部楼层
对LZ的开发环境感兴趣。。

出0入0汤圆

 楼主| 发表于 2010-10-18 13:10:09 | 显示全部楼层
回复【9楼】xl7y
-----------------------------------------------------------------------
回复【8楼】ifree64
-----------------------------------------------------------------------


这是我们老师写的,编译器为 avr-gcc,基于windows.net ,用vb写的

出0入0汤圆

 楼主| 发表于 2010-10-18 13:15:10 | 显示全部楼层
这个软件使用是来很方面便,而且源代码开放的,对初学者学习单片机有很大的好处

出0入0汤圆

发表于 2011-4-20 20:12:56 | 显示全部楼层
mark

出0入0汤圆

发表于 2011-4-20 23:53:41 | 显示全部楼层
能把你的开发环境 共享下吗?

出0入0汤圆

发表于 2011-4-21 08:18:27 | 显示全部楼层
我说我怎么觉得这么眼熟啊

出0入0汤圆

发表于 2011-4-21 08:27:55 | 显示全部楼层
//刚好我也在用595 就发一个我写的595驱动
sbit CP=P2^2;
sbit DS=P2^0;
sbit OE=P2^1;

void LED_OutPut(uchar Dat)
{
        char Cnt;
        for(Cnt=0;Cnt<8;Cnt++)
        {
                DS=0;
                if(Dat&0x80)
                DS=1;
                _nop_();
                CP=0;
                _nop_();
                CP=1;
                Dat<<=1;
        }
        OE=0;
        _nop_();
        _nop_();
        OE=1;
}

出0入0汤圆

发表于 2011-4-21 08:47:55 | 显示全部楼层
it is a very good framework, as the port manipulation portion of it is entirely portable - the same code works on 8-bit, 16-bit and 32-bit platforms without any modification.

I use an almost identical system, personally and commercially.

some potentially fatal errors:

"#define SETBIT(x,y) (x |= y) "

this should really be
"#define SETBIT(x,y) (x |= (y))"

this allows for cases where you want to set multiple bits in x, like "SETBIT(var, (1<<1) | (1<<0) | (1<<7));"

and

"#define DeSelect595  CLRBIT(HC595_165_PORT,HC595_165_595CS) "

should be
"#define DeSelect595()  {CLRBIT(HC595_165_PORT,HC595_165_595CS);}"

the naming convention can also be improved but that's minor. the basic framework is very good.

出0入0汤圆

发表于 2011-4-21 08:49:32 | 显示全部楼层
"         #define SETBIT(x,y) (x |= (y)) "

我觉得应该改为:
         #define SETBIT(x,y) (x |= (1<<y)) "

the original definition allows setting multiple bits in one statement and yours cannot do that.

出0入0汤圆

发表于 2011-4-21 08:52:41 | 显示全部楼层
"这是我们老师写的"

s/he is very good. the framework is very modular: notice the definitions for adc, timers, spi, ints, and ports, etc..

the guy/gal understood what c is all about and is taking the right approach to it. hopefully you can learn from him/her.

出0入0汤圆

发表于 2011-4-21 08:56:34 | 显示全部楼层
if s/he had real life experience running large projects, using this very approach on more sophisticated chips (Renesas, Freestyle or ARM), s/he can easily be a project architect and make tons of money - I would put a figure to $500K+ easily.

s/he is wasting his/her time teaching in a school.

出0入0汤圆

发表于 2011-4-21 08:57:38 | 显示全部楼层
"/刚好我也在用595 就发一个我写的595驱动
sbit CP=P2^2; "

with that approach, you have zero hope of finding a job at any decent firm.

出0入0汤圆

 楼主| 发表于 2011-4-21 12:30:55 | 显示全部楼层
回复【14楼】darwin187
-----------------------------------------------------------------------

ok

出0入0汤圆

发表于 2011-5-27 16:53:05 | 显示全部楼层
mark

出0入0汤圆

发表于 2012-9-6 09:59:16 | 显示全部楼层
版主有没有算过编译出来的Code Size有多大呢?
用 asm 来写这东东, 决不超过10个BYTE.

出0入0汤圆

发表于 2012-9-6 10:09:53 | 显示全部楼层
millwood0 发表于 2011-4-21 08:57
"/刚好我也在用595 就发一个我写的595驱动
sbit CP=P2^2; "

millwood0 说话一般都不客气。不过是个好人。
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-5-2 19:40

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

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