当前位置: 代码迷 >> 单片机 >> 能否把端口当做函数参数
  详细解决方案

能否把端口当做函数参数

热度:35   发布时间:2016-04-28 15:42:35.0
可不可以把端口当做函数参数?
多个端口要完成同样的功能,
可不可以把端口当做函数参数?
比如把下面的两个函数合并为一个函数

void offStep_0()
{
char j = 0;
unsigned char state= 0x0; // 全亮
unsigned char  t = 0x01;  //
for(j = 0; j < 8; ++j)
{
state = state | t;
P0 = state;
delay(TimeDelay);
t <<= 1;
}
}
void offStep_1()
{
char j = 0;
unsigned char state= 0x0; // 全亮
unsigned char  t = 0x01;  //
for(j = 0; j < 8; ++j)
{
state = state | t;
P1 = state;
delay(TimeDelay);
t <<= 1;
}
}

------最佳解决方案--------------------
可以,最简单的方法是typedef一个枚举类型,然后函数里switch就行了
------其他解决方案--------------------
可以,最简单的方法是typedef一个枚举类型,然后函数里switch就行
------其他解决方案--------------------
引用:
C/C++ code12345678910111213141516void offStep(char n) {     char j = 0;     unsigned char state= 0x0; // 全亮     unsigned char  t = 0x01;  //     for(j = 0; j < 8; ++j)     {         state……

这个和switch case一样
------其他解决方案--------------------
引用:
可以,最简单的方法是typedef一个枚举类型,然后函数里switch就行了

嗯 有理
------其他解决方案--------------------
void offStep_01()
{
    char j = 0;
    unsigned char state= 0x0; // 全亮
    unsigned char  t = 0x01;  //
    for(j = 0; j < 8; ++j)
    {
        state = state 
------其他解决方案--------------------
 t;
        P0 = state;
        P1 = state;
        delay(TimeDelay);
        t <<= 1;
    }
}

        P0 = state;
        P1 = state;
------其他解决方案--------------------
P0 , P1 是 寄存器地址。
------其他解决方案--------------------
引用:
C/C++ code1234567891011121314void offStep_01(){    char j = 0;    unsigned char state= 0x0; // 全亮    unsigned char  t = 0x01;  //    for(j = 0; j < 8; ++j)    {        state = state 
  相关解决方案