标题: 单片机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——包括端口。

不过你可以这样用


  1. void setpin(uchar  port_num, uchar bit_index )
  2. {
  3.   switch( port_num)
  4.   {
  5.     case 1:
  6.        P1 |= ( 1 << bit_index );
  7.     case 2:
  8.        P2 |= ( 1 << bit_index );
  9.     case 3:
  10.        P3 |= ( 1 << bit_index );
  11. /*   case 4:
  12.        P4 |= ( 1 << bit_index );
  13.     case 5:
  14.        P5 |= ( 1 << bit_index );*/

  15.   }
  16. }

  17. void clrpin( uchar port_num, uchar bit_index )
  18. {
  19.   switch (port_num)
  20.   {
  21.     case 1:
  22.        P1 &= ~( 1 << bit_index );
  23.     case 2:
  24.        P2 &= ~( 1 << bit_index );
  25.     case 3:
  26.        P3 &= ~( 1 << bit_index );
  27. /*   case 4:
  28.        P4 &=~ ( 1 << bit_index );
  29.     case 5:
  30.        P5 &= ~( 1 << bit_index );*/

  31.   }
  32. }

  33. void sd(uchar x , uchar n,  uchar y) // 闪灯
  34. {

  35.     setpin(n,y);
  36.     delay_ms(x);

  37.     clrpin(n,y);
  38.     delay_ms(x);

  39. }

  40. sd(500, 3, 5);

复制代码





欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1