读书人

能否把端口当做函数参数

发布时间: 2012-12-26 14:39:29 作者: rapoo

可不可以把端口当做函数参数?
多个端口要完成同样的功能,
可不可以把端口当做函数参数?
比如把下面的两个函数合并为一个函数


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


[其他解释]
t; ……


谢谢,但是这样不能满足需求。不能同时给P0 P1赋值。P0 和P1是单独的。
[其他解释]
P0 = state;P1 = state;这就是同时的。

再不行,你就把两个外设接到一起。
[其他解释]
引用:
P0 = state;P1 = state;这就是同时的。

再不行,你就把两个外设接到一起。

这样描述吧
我先要对端口P0执行offStep_0()的操作。然后对端口P1也执行相同的操作。
我的实现是写了个offStep_1().我觉得有重复的代码,因此想着能不能复用代码。这是我的想法……
[其他解释]
void offStep(char n)
{
char j = 0;
unsigned char state= 0x0; // 全亮
unsigned char t = 0x01; //
for(j = 0; j < 8; ++j)
{
state = state
[其他解释]
t;
if(n==0)
P0 = state;
else
P1 = state;
delay(TimeDelay);
t <<= 1;
}
}

每天回帖即可获得10分可用分
[其他解释]
char port;
port = P0;
//function(&port);
port = P1;
//function(&port);
[其他解释]
引用:
char port;
port = P0;
//function(&port);
port = P1;
//function(&port);

你这个方法不行吧
举个例子
int P0 = 0;
char port = P0;
port = 1;
此时 P0还是0


[其他解释]
当然要写P0 = xx;
P0 是IO口的寄存器地址,port(所声明的变量)只是一个有值的变量而已。
不清楚你单片机学到什么程度了,感觉是你硬件、程序没区分太清楚。

这个问题可以先搁置,能实现功能就行了。
等你用多了之后,再回头想这个问题吧。

读书人网 >单片机

热点推荐