标题:
单片机C语言IO口做形参传给函数问题
[打印本页]
作者:
李冬
时间:
2022-5-12 09:43
标题:
单片机C语言IO口做形参传给函数问题
这两种方法都不对, 闪灯函数怎么写,可以灵活的控制IO...
sfr P3 = 0xb0;
sbit P30 = P3^0;
sbit P31 = P3^1;
sbit P32 = P3^2;
sbit P33 = P3^3;
sbit P34 = P3^4;
sbit P35 = P3^5;
sbit P36 = P3^6;
sbit P37 = P3^7;
void sd(uchar x ,uchar y,uchar z) // 闪灯
{
y ^=(1<<z);
delay_ms(x);
y &=~(1<<z);
delay_ms(x);
}
void sd(uchar x ,SBIT y ) // 闪灯
{
y =1;
delay_ms(x);
y =0;
delay_ms(x);
}
sd(500,P3,5);
作者:
lkc8210
时间:
2022-5-12 11:49
sfr和sbit 都不可以形参
作者:
yzwzfyz
时间:
2022-5-12 13:49
Y是什么?编译平台不知道,你也不知道。如果你知道的话,就告诉平台。
作者:
angmall
时间:
2022-5-12 15:32
这是 8051 硬件架构的一个基本限制:它无法间接寻址 SFR——包括端口。
不过你可以这样用
void setpin(uchar port_num, uchar bit_index )
{
switch( port_num)
{
case 1:
P1 |= ( 1 << bit_index );
case 2:
P2 |= ( 1 << bit_index );
case 3:
P3 |= ( 1 << bit_index );
/* case 4:
P4 |= ( 1 << bit_index );
case 5:
P5 |= ( 1 << bit_index );*/
}
}
void clrpin( uchar port_num, uchar bit_index )
{
switch (port_num)
{
case 1:
P1 &= ~( 1 << bit_index );
case 2:
P2 &= ~( 1 << bit_index );
case 3:
P3 &= ~( 1 << bit_index );
/* case 4:
P4 &=~ ( 1 << bit_index );
case 5:
P5 &= ~( 1 << bit_index );*/
}
}
void sd(uchar x , uchar n, uchar y) // 闪灯
{
setpin(n,y);
delay_ms(x);
clrpin(n,y);
delay_ms(x);
}
sd(500, 3, 5);
复制代码
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1