bin2bcd & bcd2bin, 8-6-4 digits, double dabble algorithm

OP (Firma: @home) #7248274
Lesenswert?

Wer es gebrauchen kann ... frisch aus meiner Giftküche!

... bin2bcd und bcd2bin, wahlweise 8,6 oder 4 Digits ohne mul/div/mod 
mit dem double dabble Algorithmus, wenn es schnell und mit kleinem 
Footprint sein soll.

https://en.wikipedia.org/wiki/Double_dabble
Angehängte Dateien:
Gast #7248349
Lesenswert?

Kann man so machen.
Ich mache das so:
1
uint16_t convtab[4]={10,100,1000,10000};
2

3
void tobcd(volatile uint8_t *p, uint_fast16_t val){
4
  
5
  uint_fast8_t i,res;
6
  volatile uint8_t *bp;
7
  
8
  register uint8_t sreg;
9
  
10
  sreg = SREG;
11
  asm("cli");
12
  
13
  // first clear old values
14
  // keep signs and double informations
15
  bp = p;
16
  for(i=0; i < (DISP_BUF_MAX/2); i++)
17
      *bp++ &= 0xf0;
18
  
19
    
20
  i= ((DISP_BUF_MAX/2) - 1);    // Number of digits minus 1
21
  
22
  do {
23
    i--;
24
    
25
    for(res=0; val >= convtab[i]; val -= convtab[i]){
26
      res++;
27
    }
28
    *p |= res;
29
    p++;
30
    
31
    
32
  } while(i);
33
  *p |= val;
34
  
35
  clear_leading_zero();
36
  SREG = sreg;
37
}

Antwort schreiben

Bitte melde dich an, um einen Beitrag zu schreiben.

oder

Mit Google-Account einloggen

Die Registrierung ist kostenlos und dauert nur eine Minute.

Jetzt registrieren