|
使用Platformio平台的libopencm3开发框架来开发STM32G0,以下使用ADC进行NTC温度采集。 / O% [* V4 S# ^* z7 F
1 新建项目建立ntc_temp项目 * k# w, s/ W% |/ ~1 S& `# g/ S4 @
在PIO的Home页面新建项目,项目名称ntc_temp,选择开发板为 MonkeyPi_STM32_G070RB,开发框架选择libopencm3;
6 `. Q" G E6 t% |1 e5 K+ x- 1upload_protocol = cmsis-dap5 G8 r9 J% q; o3 i7 b; l8 h j
- 2debug_tool = cmsis-dap
复制代码 , V$ d3 l" R9 D. \' }7 R
$ i1 ]+ k5 M, m7 o
- 1build_flags = ! K/ _9 U: D3 G+ p
- 2 -Wl,-u,_printf_float
复制代码 1 }4 f+ A X2 y
, u1 s. j1 w# y2 编写程序2.1 ADC设置 7 J# x* X$ T. M2 J
- 1void adc_setup()8 z# z8 H* V4 D! ~( ?, E% W
- 2{, E1 X' ^" d! I) k# A
- 3 rcc_periph_clock_enable(RCC_GPIOA);
6 i9 A/ \1 L9 @; x6 j. }" A - 4 rcc_periph_clock_enable(RCC_ADC);
! N& x; I# R" m ^: H. A - 5& j* T% j0 F1 n6 z7 V5 ^( @
- 6 gpio_mode_setup(GPIOA,GPIO_MODE_ANALOG,GPIO_PUPD_NONE,GPIO0);$ a! Z8 W+ d( i
- 7
+ l: d$ _; q: a# o7 H* r P - 8 adc_power_off(ADC1);
6 Y" f& Z% T, S- c% @# v - 9 adc_set_clk_prescale(ADC1,ADC_CCR_PRESC_DIV2);% F k' _' F. X9 O, G, W2 T0 z% N
- 10 adc_set_single_conversion_mode(ADC1);
' `0 y" @3 Q" Z - 11 adc_set_right_aligned(ADC1);
8 o' h( X$ n8 M6 R4 S4 ^( [ - 12 adc_set_sample_time_on_all_channels(ADC1,ADC_SMPTIME_160DOT5);
- Z T. M1 C) \6 k; o - 13 uint8_t channel = 0;- p- c# X, `! A) ]" ?; _8 P
- 14 adc_set_regular_sequence(ADC1,1,&channel);
2 l: F {; K4 ~% r1 ` - 15 adc_set_resolution(ADC1,ADC_CFGR1_RES_12_BIT);
* R- Q% O# W9 [' O4 j - 16
; X K! M v% f: x+ z - 17 adc_power_on(ADC1);
/ l' v$ e4 `" s* W. b* j4 Y - 18
; Y! h* o% r! a" I0 w4 c - 19 delay_ms(10);
! B) A6 R/ F1 E4 n - 204 Y8 ?5 e3 g. Q( O
- 21}
复制代码 % G9 ]6 r5 ~8 A, t; T; \( B0 S. o! O
`" I* W* ~) EADC设置按照之前文章介绍的方法设置PA0为ADC输入; 7 }4 V; O. N! J( @, J- Y$ c
2.2 ADC读取
7 K9 i$ I( a. Q- j- 1uint16_t adc_sample()
$ c& X4 U. O! Q. ^ - 2{
6 a2 Z9 a" p8 o# V- D - 3 adc_start_conversion_regular(ADC1);
% ?8 H7 J" ?) x8 S! h0 J1 Y - 4 return adc_read_regular(ADC1);6 `; F+ v( ]: i6 j/ D. l
- 5}
复制代码 1 c* L3 b- t( D9 O1 F
, s3 {; P) k( S2.3 ADC与NTC的温度转换这里的NTC分压电阻为10K,ADC分辨率为12位,因此ADC采集值为:(NTC电阻 x 4095) ÷ (NTC电阻 + 10K),即可得到如下所示的表格: $ C& f( m0 ^( l* b' w) [7 S' p9 l
/ Z1 I# ]% c2 e G/ x2 X* @0 ^% x# {" h: ]$ N+ k' l, i8 `
在EXCEL中计算出ADC采样数值-温度对应表,为了简单ADC进行取整,就可以得到电阻-55到125摄氏度的ADC对应表: - 1const uint16_t ntcDATA[] = {
1 A8 @, [: h8 g, K - 24040,4038,4035,4031,4027,4023,4018,4013,4007,4001,3994,3987,3980,3972,3964,3956,& u: j8 H+ y7 a2 j
- 3! |- G# d& o. w, \' F
- 43947,3937,3928,3917,3907,3896,3884,3872,3860,3847,3833,3819,3804,3789,3773,3757, I7 h/ O1 F+ N' f! ?0 m
- 5
# ^' Q0 m* | Q: j5 t/ W - 63740,3722,3703,3684,3664,3644,3622,3600,3577,3553,3528,3502,3475,3448,3419,3390,/ u" |% x% j* y0 F0 ?. o
- 7. [, u1 I5 x4 ~
- 83360,3329,3297,3264,3230,3195,3160,3123,3086,3048,3010,2970,2930,2889,2848,2806,( f8 ^4 E9 i! x l
- 9- ~2 u4 t7 X6 X$ a" ^
- 102763,2720,2676,2633,2588,2544,2499,2454,2408,2363,2317,2272,2226,2181,2136,2091,/ \8 I0 B( f5 {7 ]% [/ h7 B
- 11; |, B- } Y$ D4 k: z7 }7 ?- A
- 122048,2001,1957,1913,1869,1826,1783,1741,1699,1658,1617,1576,1537,1498,1459,1422,
/ V! [% |/ T; Q% Q. y; s - 13
/ p5 X( H$ n3 c, D) y - 141385,1348,1312,1277,1243,1209,1176,1144,1112,1081,1051,1022,993,965,937,910,( q4 u1 {( C6 g2 } ^
- 15
! S: r1 X* x! D5 @* O - 16884,859,834,810,786,764,741,720,699,679,659,639,621,602,585,568,
) H4 C2 q; S: W% r. I2 b - 17
% E3 Q* C; F8 J - 18551,535,519,504,490,475,461,448,435,422,410,398,387,376,365,355,
" _6 @8 q' x2 {" k; [ - 19( m% p/ c* h2 W6 X0 z& w
- 20345,335,325,316,307,299,290,282,274,267,259,252,245,239,232,226,
, {9 e8 m' h! t - 217 y. F3 X, H2 `
- 22220,214,208,202,197,192,187,182,177,172,168,163,159,155,151,146,
) a, ?5 {* u& F7 ^+ `8 W0 R - 23
. ?' J) b: _" X6 ` - 24143,139,135,132,129
( {7 N4 F, ~+ h" i! n5 R& S/ g - 25};
复制代码 % d( R, U+ ]9 |; b3 x& j3 _
- 1#define TEMP_HEADER_VALUE -55 //the first temp in table) u) h2 B# ~9 X# f3 `
- 2 A$ O" Y/ T. @
- 3#define ITEM_NUM(items) sizeof(items) / sizeof(items[0])
/ t1 Q! O; B6 Z5 V1 f& W1 A$ U) ~ - 42 p) L* j& D! ]: K/ b
- 5/**
) Q8 J8 T+ R3 @; S - 6 * @brief search the table, return a midium value if not found
9 I7 L6 d7 O% S, t - 7 *
8 j" M/ Q' \9 V& ` - 8 * @param table the data to search! v' [- C" r+ M$ j, q; m L% T
- 9 * @param len the table length. @! _$ p3 R/ r
- 10 * @param up if data is min to max
+ d5 `" C( _& W& {6 e - 11 * @return int32_t -1 -> if not found! @* J5 s- [4 {8 n4 r
- 12 */
) F7 h# T0 P0 b - 13int32_t bsearch_ret_mid(const uint16_t *table, uint16_t len, bool up, uint16_t key)6 q4 @( s' {0 o, y; _) T
- 14{4 ]# @0 A. E. F
- 15 uint16_t bot;; ^8 u9 _1 N0 M/ D
- 16 uint16_t mid;6 S) I8 I& h* x+ p
- 17 uint16_t check;& Q5 }% [ B' V% D% l6 L) E# M5 v
- 18 uint16_t top;& W* Z$ H8 Y* r$ G5 y, W" b3 }5 a7 l/ _
- 190 x/ W+ l5 |. N& _
- 20 if (len == 0) {4 A4 L0 w$ u; U3 ^
- 21 return -1;
2 C$ ?0 O- [/ z1 l' V8 h1 l6 U" ~6 F - 22 }: V- j8 P; x# h, t
- 235 o/ n" o# X7 f( N& n' J+ F
- 24 if (up) {
6 J( z6 ~: ]- E+ H6 i+ V - 25 //the first data will be min
2 {6 I# l7 i/ u( b4 ^7 i G# p5 u - 26 if (key < table[0]) {
$ o) {% y# s& e8 \# Q - 27 return -1;
& }+ O8 N! U6 L4 N! x0 i/ Z - 28 }
4 X R& T1 e4 D5 N/ h" n - 29, l7 ^ B6 b/ L( _! N/ Q0 O8 ^
- 30 //bigger then the last data
* G8 n6 d8 T* x9 Q A) J- i - 31 if (key > table[len - 1]) {
% D1 B: x2 f) k* |5 r - 32 return -1;
4 a$ J7 m! J8 ]/ D8 q# e - 33 }0 H1 G1 m. q. W4 q
- 34 } else {5 P. z. Y$ U+ G5 E* ]4 m2 u. D
- 35 if (key > table[0]) {
& P% _* { K, }$ J* t; b - 36 return -1;
+ ~ q% v7 w$ ]! H - 37 }$ Z9 F' r, S# n( N
- 38
1 `3 o: @0 W. I: M5 m* b - 39 if (key < table[len - 1]) {' h. Z( R5 `, K* \
- 40 return -1;
. t, g4 D; }3 V0 n - 41 }8 a" a X+ X1 S g* H# j6 o
- 42 }* b5 B5 e0 I- s
- 431 y9 Q+ |$ }+ r0 L
- 44 bot = 0;. c6 V( w1 M- X, b
- 45 top = len - 1;
( R8 S3 ]3 A$ C - 46
( ?' f" v+ s% N& V - 47 if (up) {) E. q3 D/ d; ]# n( P
- 48 while (bot < top) {9 Q( m" z( f* }/ }- K) n
- 49 mid = top - (top - bot) / 2;8 x5 O8 f$ C- V. K" }! _
- 50. [0 |$ Z, {$ B
- 51 if (key < table[mid]) {
# _, S1 U$ {9 \2 r. E+ Z - 52 top = mid - 1;) B n8 z) \( F9 V+ u D! J( S
- 53 } else {* O+ J$ V0 B* D' ]" b1 z' h6 i' M
- 54 bot = mid;
6 c# O( X+ b4 W9 W7 C$ h7 k - 55 }
( E E' S" W+ a. O8 z1 h - 56 }
$ @8 c" Y9 b2 G - 57 } else {
$ e' s8 Q! X8 e- }' v1 H - 58 while (bot < top) {5 J& _+ J( U# W- m
- 59 mid = top - (top - bot) / 2;+ M* j; }: Q9 v q
- 60
3 d+ L$ |% G- D8 r" G - 61 if (key > table[mid]) {1 r; y- h+ f8 J
- 62 top = mid - 1;4 f/ q# H* S; J
- 63 } else {) s4 c5 M0 a! C2 T# @) u& w
- 64 bot = mid;
- k! B3 _ c# M - 65 }" f- }5 A4 k, U8 ?0 R
- 66 } e4 X' l" Z# b7 H4 o/ v8 P/ y# j
- 67 }
# X; P. ^2 F7 x/ V. M! `0 Y7 r! R" H - 68
0 `; b+ m; c; H! ?! C) T - 69 if (key == table[top]) {" o0 W! m$ A, J
- 70 return top;3 g) t; ?+ }' V$ Z7 I- J) Y
- 71 }0 m; w, w7 Z9 q+ D7 @
- 72
5 H1 c" D2 P/ | - 73 //not equal the data in table' d: |2 L7 x5 x% ?
- 74 if (up) {9 r7 V/ x$ L" ?" Y
- 75 if (key > table[top]) {
/ P( `% }1 c) A3 f; m - 76 return top;
) y: c( W9 n$ A9 i- ~1 p - 77 }( [: Q& X4 q; T5 S& l" x
- 78 } else {
/ L: G2 f. n8 q - 79 if (key < table[top]) {3 Y8 m- p- v3 q" N6 V% y
- 80 return top;& X5 Z! [4 t" P3 N( B( b
- 81 }' L. g7 v# a, W( R j5 \( O( L
- 82 }
7 K5 G u5 l: H3 r+ _ - 83
' {# Y' p+ X3 |- C - 84 return -1;
2 T) e8 ?& N! |0 o0 @1 H& I - 85}
复制代码
$ `; K: u$ [2 b( h$ d$ _7 a9 A) g
然后根据索引得到温度: 6 w$ H1 q% ]0 Z3 ?2 Z9 W
- 1//get temperature , x10, M2 j2 G# h9 ]5 [, D
- 2int16_t ntc2tem(uint16_t adc)$ T/ H" e$ ?5 N( G& M9 e
- 3{
/ n2 \6 t3 h8 {& Q, x - 4 int32_t index = 0;
; K% e A/ h' S/ { - 5 int16_t temperature = 0;; X# v* @6 ?1 C, o
- 6
1 y( g3 B1 F( i; n6 a - 7 index = bsearch_ret_mid(ntcDATA, ITEM_NUM(ntcDATA),false,adc);' Q& U: C# l0 |$ D( f9 ^
- 8
' r( w t1 j3 n% `0 K, I7 Z! ^ - 9 //max, the first value6 }$ Z! Y' E0 ~4 F1 z
- 10 if(index==0){
) H# A7 t# Z4 m+ F - 11 temperature = TEMP_HEADER_VALUE*10;+ u& q$ P7 O' R0 m. @
- 12 }0 i+ z$ x/ r* L+ ]9 k- D" m+ K& m
- 13 //min, the last value0 H- C6 ^2 m2 \/ v* h4 j
- 14 else if(index>= ITEM_NUM(ntcDATA)){
5 \/ ]# M) N0 { - 15 temperature = 10*(TEMP_HEADER_VALUE + ITEM_NUM(ntcDATA));. @4 j d" Y. d- {6 {
- 16 }
. {# V% X2 ^# ?6 z( J7 B& [& t - 17 else{! U( u7 Z4 ?% J) j; b
- 18 //just get integer number7 s8 x: I7 f; D. E. l( |# y9 l3 X7 Q3 N
- 19 // temperature = TEMP_HEADER_VALUE + index;
6 J' m9 o# o- x# {' }6 l - 20- Z9 ^* f2 y/ y7 t4 Q C
- 21 //get approximation data0 T4 z/ ?- T5 X) ?7 \4 n" S* f
- 22 temperature = TEMP_HEADER_VALUE + index;8 z: E4 f1 h0 d5 }
- 23
8 y/ b* H* J9 p5 {+ Q8 Q - 24 //at middle
( Q2 u, P/ [+ F) L: t* I - 25 temperature = (temperature+1)*10 - 10.0 * (adc-ntcDATA[index+1])/(ntcDATA[index]-ntcDATA[index+1]);7 z* i4 N k2 ^' h0 F
- 26 }
0 d, c- s u9 V5 T% d - 27. h( B# i. D- p
- 28 return temperature;
8 A6 P8 w a, O* q2 a5 ~ - 29/ `+ b" c7 `. V9 c
- 30}
复制代码
% y! }. q7 L) z$ Q) s- Z: v+ }% R
, a& J1 `* Y/ z% h2 N$ ^$ B" g表格中的温度是1度进行变化的,如果需要稍微精确的,这里在1度中间的值近似看做线性进行计算; 这里为了减小计算误差,先对其扩大十倍计算,显示时候再转为小数: - 1uint16_t adc = adc_sample();
2 I: K7 U g. z: ] C! Y' b5 Q I - 2
" Y! o# y1 H1 Z k/ ?; X - 3int16_t temp_int = ntc2tem(adc);8 D k4 M3 }$ s
- 4float temp = temp_int/10.0;, a/ d& _6 K0 [( y" @6 o
- 5
8 z+ C0 A5 L" q2 e0 q; F - 6printf("adc: %d, temp: %.1f\r\n",adc, temp);
复制代码 6 c5 @! j9 ]/ F3 B- G2 B
3 连接硬件并测试
0 ?+ s! z* k. W; y" e; `# K2 S3.1 连接硬件按照如下图,将NTC和10K电阻连接到PA0脚: : j9 I! k+ `+ ~4 T. w' S
* l7 t% U" e& ^# L6 T! S9 S' }$ o7 I
3.2 测试打开串口,当手放在NTC上可以看到温度变化:
% e' J3 ^+ S/ S5 z; @2 o+ L# ~! t4 E
* T3 [ b+ \, j/ Y3 d K
|