STM32G0を使ってみる3 -I2Cライブラリの拡張そしてAHT20-
●AHT20だ!
今回は前回ご紹介したAOSUNGの温湿度センサAHT20を動作させてみます。
AHT20はAOSUNG(ブランド名はASAIR)の新世代の温湿度センサです。これを搭載し、
さらにLDOと信号レベル変換FETを搭載したモジュールをアマゾン経由で入手しました。
そしてつい最近秋月よりDIPモジュール版のAHT25も登場してもう入手されて方もいる
かと思いますが過去世代のAM23xx系よりもさらに使いやすくなっているようですね。
●I2Cドライバを10年ぶりくらいに改造する
さて、私が過去に作成したI2Cライブラリは送信受信ともバイトorワード単位でしか
やり取りすることができず、2バイトコマンドが存在するI2Cデバイスのコントロール
すらできないガバガバっぷりです☠(RaspiのSMBusも同じだし私は悪くなi)
そしてAHT20は温度取得トリガーコマンドが2バイトどころか3バイトもあった…
そんなわけで重い腰を上げてSTM32G0のI2Cレジスタをたたいてマルチバイトを
送受信できる関数を整備してみました。
※受信※
/**************************************************************************/
/*!
Read RAW MultiByteData Middlelevel.
*/
/**************************************************************************/
uint8_t I2CDev_ReadBytesRaw(uint8_t sla,uint8_t *data,uint8_t count)
{
/* Wait Bus Busy */
I2CDevTimer = I2CDEV_SHORT_TIMEOUT;
while((I2CDEV_PORT->ISR & I2C_ISR_BUSY))
{
if((I2CDevTimer--) == 0) return I2CDev_Timeout();
}
/* Send Slave Address and Memory Address */
/* Configure slave address, nbytes, NOLELOAD,NOAUTOSTOP */
I2C_TransferHandling(I2CDEV_PORT, sla|I2CDEV_SD_READ, count, 0, (uint32_t)(I2C_CR2_START | I2C_CR2_RD_WRN));
for(int i = 0;i<(count-1);i++){
*data = I2CDev_RecvData(I2CDEV_ACK);
data++;
}
*data = I2CDev_RecvData(I2CDEV_NACK); /* Last data MUST be NACK */
/* Send Stop Condition */
I2CDev_StopCondition();
/* Wait until STOPF flag is reset */
I2CDevTimer = I2CDEV_SHORT_TIMEOUT;
while(!(I2CDEV_PORT->ISR & I2C_FLAG_STOPF))
{
if(I2CDev_ReadNAck() ==I2CDEV_NACK) return I2CDev_Timeout();
if((I2CDevTimer--) == 0) return I2CDev_Timeout();
}
/* Clear STOP Flag */
I2CDEV_PORT->ICR =I2C_ISR_STOPF;
/* Clear Configuration Register 2 */
I2CDEV_PORT->CR2 &= (uint32_t)~((uint32_t)(I2C_CR2_SADD | I2C_CR2_HEAD10R | I2C_CR2_NBYTES | I2C_CR2_RELOAD | I2C_CR2_RD_WRN));
return 1;
}
※送信※
/**************************************************************************/
/*!
Write RAW MultiBytes Middlelevel.
*/
/**************************************************************************/
uint8_t I2CDev_WriteBytesRaw(uint8_t sla,uint8_t *data,uint8_t count)
{
/* Wait Bus Busy */
I2CDevTimer = I2CDEV_SHORT_TIMEOUT;
while((I2CDEV_PORT->ISR & I2C_ISR_BUSY))
{
if((I2CDevTimer--) == 0) return I2CDev_Timeout();
}
/* Send Slave Address and Memory Address */
/* Configure slave address, nbytes, RELOAD,AUTOSTOP,AUTOSTART */
I2C_TransferHandling(I2CDEV_PORT, sla|I2CDEV_SD_WRITE, count, I2C_CR2_AUTOEND|I2C_CR2_RELOAD, I2C_CR2_START);
/* Send multibytes datas */
for(int i = 0; i < count ; i++){
/* Wait until TXIS flag is set */
I2CDevTimer = I2CDEV_SHORT_TIMEOUT;
while(!(I2CDEV_PORT->ISR & I2C_ISR_TXIS))
{
if(I2CDev_ReadNAck() == I2CDEV_NACK){
/* Clear STOP Flag */
I2CDEV_PORT->ICR =I2C_ISR_NACKF;
/* Clear STOP Flag */
I2CDEV_PORT->ICR =I2C_ISR_STOPF;
/* Clear TXE Flag */
I2CDEV_PORT->ISR |=I2C_ISR_TXE;
/* Clear Configuration Register 2 */
I2CDEV_PORT->CR2 &= (uint32_t)~((uint32_t)(I2C_CR2_SADD | I2C_CR2_HEAD10R | I2C_CR2_NBYTES | I2C_CR2_RELOAD | I2C_CR2_RD_WRN));
return 0;
}
if((I2CDevTimer--) == 0) return I2CDev_Timeout();
}
I2CDev_SendData(*data);
data++;
//I2C_WaitFlag(I2C_ISR_TCR); /* Notneeded in auto STOP */
}
/* Send Stop Condition */
//I2CDev_StopCondition(); /* Notneeded in auto STOP */
/* Wait until STOPF flag is reset */
I2CDevTimer = I2CDEV_SHORT_TIMEOUT;
while(!(I2CDEV_PORT->ISR & I2C_FLAG_STOPF))
{
if(I2CDev_ReadNAck() ==I2CDEV_NACK) return I2CDev_Timeout();
if((I2CDevTimer--) == 0) return I2CDev_Timeout();
}
/* Clear STOP Flag */
I2CDEV_PORT->ICR |=I2C_ISR_STOPF;
/* Clear Configuration Register 2 */
I2CDEV_PORT->CR2 &= (uint32_t)~((uint32_t)(I2C_CR2_SADD | I2C_CR2_HEAD10R | I2C_CR2_NBYTES | I2C_CR2_RELOAD | I2C_CR2_RD_WRN));
return 1;
}
STM32F0以降のI2Cブロックはハードウエアで細かい設定ができる分
ちゃんと仕組みを知っておかないとうまく使いこなせないのでご注意を…!
![](https://dl.dropboxusercontent.com/s/0eq36iidfdi5qay/20211031_004.jpg)
とりあえずサクッと動きました☆
値段も安くて精度もそこそこなので使い倒せるセンサですね〜
次はオシロで波形を観測してみます。
温湿度測定トリガコマンド送信時の波形は以下のようになります。
![](https://dl.dropboxusercontent.com/s/hujen7i05573i28/20211031_005.png)
温度/湿度の受信パケットは以下の波形です。
![](https://dl.dropboxusercontent.com/s/mq1egxvz0sfwq4z/20211031_006.png)
画像の説明にもある通りCRCチェックが必要なければ6バイトで受信を打ち切って
しまうこともできます。私のプログラムはまだCRCチェックに対応してないので
6バイトめで打ち切りにしています。
20230929追:
現在おきぱで公開しているソースはCRC値チェックを入れております!
20230929追:
といううわけで10年越しでやっとこI2Cライブラリに拡張性持たせた
改造を行うことができました。これを応用して買いためたままの他の
温湿度センサもガンガン使って行こうと思います!!!
今回の内容を盛り込んだSTM32G0のプログラムも更新しましたのでよろしく!!!
![](https://dl.dropboxusercontent.com/s/s04ic2zovaeby2h/20211031_007.jpg)
次のネタですが実はもうソースコード見たら実装済みでわかるかと思いますが
1Wireなデバイスたちをオシロの波形と照らし合わせながら動かしてみます。
免責・連絡先は↑のリンクを
↓SNSもやってます↓
powered by まめわざ- ARM/STM32 (116)
- OpenOCD (27)
- ARM/NxP (34)
- ARM/Cypress (5)
- ARM/Others (3)
- ARM/Raspi (1)
- AVR (13)
- FPGA (4)
- GPS/GNSS (19)
- MISC (80)
- STM8 (2)
- Wirelessなアレ (16)
- ブラウザベンチマーク (28)
- 日本の自然歩道 (25)
- STM32U0はぢめました
⇒ ねむい (08/07) - STM32U0はぢめました
⇒ ひかわ (07/28) - STM32H5を使ってみる3 -待ち受ける初見殺しの罠たち-
⇒ ねむい (05/17) - STM32H5を使ってみる3 -待ち受ける初見殺しの罠たち-
⇒ どじょりん (05/16) - STM32H5を使ってみる3 -待ち受ける初見殺しの罠たち-
⇒ どじょりん (05/16) - いろいろ試す61(と今年の反省会)
⇒ ねむい (01/02) - いろいろ試す61(と今年の反省会)
⇒ ひかわ (01/02) - いろいろ試す61(と今年の反省会)
⇒ ひかわ (01/01) - STM32H5を使ってみる3 -待ち受ける初見殺しの罠たち-
⇒ ねむい (12/31) - STM32H5を使ってみる3 -待ち受ける初見殺しの罠たち-
⇒ ひかわ (12/31)
- August 2024 (1)
- July 2024 (1)
- June 2024 (1)
- May 2024 (1)
- April 2024 (1)
- March 2024 (1)
- February 2024 (2)
- January 2024 (1)
- December 2023 (4)
- November 2023 (2)
- October 2023 (2)
- September 2023 (1)
- August 2023 (2)
- July 2023 (1)
- June 2023 (2)
- May 2023 (3)
- April 2023 (1)
- March 2023 (1)
- February 2023 (1)
- January 2023 (1)
- December 2022 (2)
- November 2022 (1)
- October 2022 (1)
- September 2022 (1)
- August 2022 (1)
- July 2022 (1)
- June 2022 (1)
- May 2022 (1)
- April 2022 (1)
- March 2022 (1)
- February 2022 (1)
- January 2022 (1)
- December 2021 (2)
- November 2021 (2)
- October 2021 (1)
- September 2021 (1)
- August 2021 (1)
- July 2021 (1)
- June 2021 (1)
- May 2021 (1)
- April 2021 (1)
- March 2021 (1)
- February 2021 (1)
- January 2021 (1)
- December 2020 (3)
- November 2020 (1)
- October 2020 (1)
- September 2020 (1)
- August 2020 (1)
- July 2020 (1)
- June 2020 (2)
- May 2020 (1)
- April 2020 (1)
- March 2020 (1)
- February 2020 (1)
- January 2020 (1)
- December 2019 (3)
- November 2019 (1)
- October 2019 (1)
- September 2019 (2)
- August 2019 (1)
- July 2019 (1)
- June 2019 (1)
- May 2019 (1)
- April 2019 (1)
- March 2019 (1)
- February 2019 (1)
- January 2019 (1)
- December 2018 (3)
- November 2018 (2)
- October 2018 (1)
- September 2018 (1)
- August 2018 (1)
- July 2018 (1)
- June 2018 (1)
- May 2018 (1)
- April 2018 (2)
- March 2018 (1)
- February 2018 (1)
- January 2018 (1)
- December 2017 (2)
- November 2017 (2)
- October 2017 (1)
- September 2017 (1)
- August 2017 (1)
- July 2017 (1)
- June 2017 (1)
- May 2017 (1)
- April 2017 (1)
- March 2017 (2)
- February 2017 (2)
- January 2017 (2)
- December 2016 (7)
- November 2016 (2)
- October 2016 (2)
- September 2016 (1)
- August 2016 (1)
- July 2016 (1)
- June 2016 (1)
- May 2016 (2)
- April 2016 (1)
- March 2016 (2)
- February 2016 (1)
- January 2016 (1)
- December 2015 (3)
- November 2015 (1)
- October 2015 (3)
- September 2015 (2)
- August 2015 (2)
- July 2015 (3)
- June 2015 (3)
- May 2015 (4)
- April 2015 (2)
- March 2015 (4)
- February 2015 (1)
- January 2015 (3)
- December 2014 (3)
- November 2014 (2)
- October 2014 (1)
- September 2014 (2)
- August 2014 (2)
- July 2014 (3)
- June 2014 (2)
- May 2014 (1)
- April 2014 (1)
- March 2014 (4)
- February 2014 (4)
- January 2014 (3)
- December 2013 (5)
- November 2013 (4)
- October 2013 (3)
- September 2013 (2)
- August 2013 (2)
- July 2013 (2)
- June 2013 (3)
- May 2013 (2)
- April 2013 (2)
- March 2013 (2)
- February 2013 (2)
- January 2013 (3)
- December 2012 (4)
- November 2012 (2)
- October 2012 (2)
- September 2012 (4)
- August 2012 (1)
- July 2012 (3)
- June 2012 (2)
- May 2012 (3)
- April 2012 (3)
- March 2012 (2)
- February 2012 (3)
- January 2012 (3)
- December 2011 (5)
- November 2011 (3)
- October 2011 (2)
- September 2011 (2)
- August 2011 (2)
- July 2011 (2)
- June 2011 (2)
- May 2011 (2)
- April 2011 (2)
- March 2011 (2)
- February 2011 (2)
- January 2011 (3)
- December 2010 (7)
- November 2010 (1)
- October 2010 (1)
- September 2010 (1)
- August 2010 (3)
- July 2010 (4)
- May 2010 (1)
- April 2010 (2)
- March 2010 (2)
- February 2010 (2)
- January 2010 (3)
- December 2009 (3)
- November 2009 (8)
- October 2009 (7)
- September 2009 (5)
- August 2009 (4)
- July 2009 (6)
- June 2009 (7)
- May 2009 (14)
- January 1970 (1)
Copyright(C) B-Blog project All rights reserved.