guxingganyue 发表于 2010-10-16 18:27:54

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

#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 DeSelect595CLRBIT(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的方法也可以晒晒啊,谢谢了

Gorgon_Meducer 发表于 2010-10-16 18:37:47

如果这是头文件,那确实有点问题……C语言头文件里面不应该放代码实体。

Notting_Hill 发表于 2010-10-16 18:41:51

前面宏定义还挺好的,后面的函数就不要放在头文件里了~~~

guxingganyue 发表于 2010-10-16 18:46:40

回复【1楼】Gorgon Meducer 傻孩子
-----------------------------------------------------------------------

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

guxingganyue 发表于 2010-10-16 18:46:53

回复【1楼】Gorgon Meducer 傻孩子
-----------------------------------------------------------------------

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

Gorgon_Meducer 发表于 2010-10-18 00:11:50

这种方式不支持多文件编译的……

guxingganyue 发表于 2010-10-18 12:18:57

回复【5楼】Gorgon Meducer 傻孩子
-----------------------------------------------------------------------

这个是我改过的(只是加了开头和末尾的#ifndef ————   #endif),其实它支持多文件编译,目前有20多个,下面是哪个软件的画面
http://cache.amobbs.com/bbs_upload782111/files_34/ourdev_590737X6RDXH.jpg
(原文件名:1-1.jpg)

http://cache.amobbs.com/bbs_upload782111/files_34/ourdev_590738FMMH0E.jpg
(原文件名:2-2.jpg)

http://cache.amobbs.com/bbs_upload782111/files_34/ourdev_590739MGYAD5.jpg
(原文件名:3-3.jpg)

guxingganyue 发表于 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))
的本质上有区别吗?

ifree64 发表于 2010-10-18 12:32:52

貌似用的自己用C#开发的集成开发环境?

xl7y 发表于 2010-10-18 12:37:47

对LZ的开发环境感兴趣。。

guxingganyue 发表于 2010-10-18 13:10:09

回复【9楼】xl7y
-----------------------------------------------------------------------
回复【8楼】ifree64
-----------------------------------------------------------------------


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

guxingganyue 发表于 2010-10-18 13:15:10

这个软件使用是来很方面便,而且源代码开放的,对初学者学习单片机有很大的好处

wenjin0386 发表于 2011-4-20 20:12:56

mark

richards 发表于 2011-4-20 23:53:41

能把你的开发环境 共享下吗?

darwin187 发表于 2011-4-21 08:18:27

我说我怎么觉得这么眼熟啊

xcodes 发表于 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;
}

millwood0 发表于 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 DeSelect595CLRBIT(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.

millwood0 发表于 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.

millwood0 发表于 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.

millwood0 发表于 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.

millwood0 发表于 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.

guxingganyue 发表于 2011-4-21 12:30:55

回复【14楼】darwin187
-----------------------------------------------------------------------

ok

qxj_2011 发表于 2011-5-27 16:53:05

mark

hclin 发表于 2012-9-6 09:59:16

版主有没有算过编译出来的Code Size有多大呢?
用 asm 来写这东东, 决不超过10个BYTE.

brahen 发表于 2012-9-6 10:09:53

millwood0 发表于 2011-4-21 08:57 static/image/common/back.gif
"/刚好我也在用595 就发一个我写的595驱动
sbit CP=P2^2; "



millwood0 说话一般都不客气。不过是个好人。
页: [1]
查看完整版本: 大家看看我们老师写的74hc595和165的头文件,我总觉得看起来不舒服,想听听大家的看法!!!