1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

(小ネタ)3.3Vと5.0Vの電圧変換(マジックナンバー39.0625と15.625)

Last updated at Posted at 2025-01-09

参考

x 過去ログを見よ!!!
x 主に8ビット系でintのサイズが16ビット

目的
ADCから電圧の変換を
●軽量
●高速
●読みやすさ重視
で行う

3.3V系

15.625 = 2.5 x 2.5 x 2.5

1024 * 3 = 3072

3072 * 15.625 = 48000

48000 / 16 = 3000

3000 /10 + 3000 = 3300


  int s; //センサーの値
  unsigned int vo; //電圧

  s = 1024; //センサーの値を1024

  //センサーの値から電圧に変換(x3 x15.625/16 1/10)
  vo = s;
  vo = vo + vo + vo;
  vo = ((vo * 15U) + (vo >> 1) + (vo >> 3)) >> 4;
  vo = vo + (vo / 10);

5V系

39.0625 = 2.5 x 2.5 x 2.5 x 2.5

1024 * 39.0625 = 40000

40000 / 8 = 5000


  int s; //センサーの値
  unsigned int vo; //電圧

  s = 1024; //センサーの値を1024

  //センサーの値から電圧に変換(x39.0625/8)
  vo = s;
  vo = ((vo * 39U) + (vo >> 4)) >> 3;

おまけ

39.0625 * 256 = 10000

なぜかそうなる

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?