led.c

来自「Welding Robot controller C Embedded Prog」· C语言 代码 · 共 75 行

C
75
字号
/******************************************************************************
* welding mobile robot control programme
* led display processing module
* @author: thai nguyen nhut dien
* @date: may 2006
******************************************************************************/

#define Pin_E Pin_B3
#define Pin_D Pin_B2
#define Pin_C Pin_B1

void displayBCD(int bcd) {
  int i;
  for (i=0; i<4; i++) {
  output_high(Pin_C);                        // data send start
  output_bit(Pin_D, (bcd & 0x08) != 0);      // send data
  output_low(Pin_C);                         // data send end
  bcd = bcd << 1;                            // shift left one bit
  }
}

void display(int32 num) {
  int thousand, hundred, ten, unit;

  thousand  = num / 1000;
  hundred   = (num % 1000) / 100;
  ten       = (num % 100) /10;
  unit      = num % 10;

  // trim leading zero
  ten       = (thousand | hundred | ten) != 0 ? ten : 255;
  hundred   = (thousand | hundred) != 0 ? hundred : 255;
  thousand  = thousand != 0 ? thousand : 255;

  // enable leds
  output_low(Pin_E);

  displayBCD(0);
  displayBCD(thousand);
  displayBCD(hundred);
  displayBCD(ten);
  displayBCD(unit);

  // disable leds
  output_high(Pin_E);
}

void displayFloat(float fnum) {
  int32 num;
  int thousand, hundred, ten, unit;

  num = fnum * 10;

  thousand  = num / 1000;
  hundred   = (num % 1000) / 100;
  ten       = (num % 100) /10;
  unit      = num % 10;

  // trim leading zero
  ten       = (thousand | hundred | ten) != 0 ? ten : 255;
  hundred   = (thousand | hundred) != 0 ? hundred : 255;
  thousand  = thousand != 0 ? thousand : 255;

  // enable leds
  output_low(Pin_E);

  displayBCD(2);
  displayBCD(thousand);
  displayBCD(hundred);
  displayBCD(ten);
  displayBCD(unit);

  // disable leds
  output_high(Pin_E);
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?