x MCP9701-E/TO 販売コード 103199
STM8で温度計を作る(MCP9701)(7セグ)
x 過去ログを見よ
目的
SPIのテスト
//SPI_7SEG_9701_STM8_1
//******* ****** ***** *****
// * * * *
// * * ***** * ***
// * **** * * *
// * * * * *
// * ***** ***** *****
//インクルド
#include <SPI.h>
//定義
#define DW digitalWrite
char ss[] = {
"#9#9#9#8#7#7#6#5#5#4#3#3"
"#2#1#1#0-9-9-8-7-7-6-5-5"
"-4-3-3-2-2-1-1 0 1 1 2 2"
" 3 4 4 5 6 6 7 8 8 91010"
"111212131414151616171818"
"192020212222232424252626"
"272828293030313232333434"
"353636373838394040414242"
"434444454546474748494950"
"50#0#0#0#0#0#0#0#0#0#0#0"
"#0#0#0#0#0#0#0#0"
};
char seg[32] = {
0x00 , //0 @ -> ' '
0x77 , //1 A o
0x7c , //2 B combined use "6"
0x39 , //3 C
0x5e , //4 D
0x79 , //5 E o
0x71 , //6 F
0x3d , //7 G
0x76 , //8 H o
0x06 , //9 I combined use "1"
0x1e , //10 J
0x75 , //11 K
0x38 , //12 L o
0x15 , //13 M
0x37 , //14 N o
0x3f , //15 O o combined use "0"
0x73 , //16 P
0x67 , //17 Q combined use "9"
0x50 , //18 R
0x6d , //19 S combined use "5"
0x78 , //20 T
0x3e , //21 U
0x1c , //22 V
0x2a , //23 W o
0x64 , //24 X
0x6e , //25 Y
0x5b , //26 Z combined use "2"
0x4f , //27 [ --> "3"
0x66 , //28 \ --> "4"
0x27 , //29 ] --> "7"
0x7f , //30 ^ --> "8"
0x40 //31 _ --> "-"
};
char nn[] = {'O', 'I', 'Z', '[', '\\', 'S', 'B', ']', '^', 'Q'};
int comma1 = 0; // コンマ(小数点)の有効フラグ 1で有効 0無効
//7segの一文字出力 nana_seg
int ns_putc(char ch) {
if (ch == ' ') {
ch = '@';
} else if (ch == '.') {
return (0);
} else if (ch >= '0' && ch <= '9' ) {
ch = nn[ch - '0'];
}
if ( comma1 != 0 ) {
comma1 = (0x80);
}
//7SEGに一文字送る
SPI_transfer(seg[ch - 64] | comma1);
DW(PE5, 1); DW(PE5, 0); //ラッチの操作
delay(200);//debug
comma1 = 0;
//戻り値
return (0);
}//ns_putc
//文字列の表示 nana_seg
int ns_printf(char *str1) {
//文字の中身がゼロか
while (*str1) {
//コンマの処理
comma1 = 0;
if ( (*(str1 + 1) ) == '.' ) {
comma1 = 1;
}
//一文字出力
ns_putc(*str1 ++);
} //while
//戻り値
return (0);
}//ns_printf
//初期化
void setup() {
//SSポートの初期化
pinMode(PE5, OUTPUT); DW(PE5, 0);
//SPIの開始
SPI_begin();
// SPIの初期化
SPI_beginTransaction( SPISettings( 8000000, MSBFIRST, SPI_MODE0) );
} //setup
//メインループ
void loop() {
uint16_t s;//センサーの値
char str1[16]; //温度の文字列
//ADCの読み込み
s = analogRead(PB0);
//s = 250; //" 20C" debug
//s = 0; //"-19C" debug
//温度の変換 テーブルから読み出す
str1[3] = ss[((s >> 1) & (0x7f << 1))]; //'#'か
str1[0] = (str1[3] == '#') ? '_' : ' ';
str1[1] = (str1[3] == '#') ? '1' :
ss[((s >> 1) & (0x7f << 1))] ; //10の桁
str1[2] = ss[((s >> 1) & (0x7f << 1)) + 1] ; // 1の桁
str1[3] = '.';
str1[4] = 'C';
str1[5] = 0;
//温度の表示
ns_printf(str1);
delay(2500);//1秒待つ
} //loop