0
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?

More than 3 years have passed since last update.

LPC1114で温度,S-5851Aをソフトウェアシリアル出力 (ハードはichigojam)

Last updated at Posted at 2021-06-18

目的
温度をシリアルに出力したい

工夫点は、書き込み用シリアルとデータ出力用シリアルを
分ける事によつて開発の効率を上げた。
データ出力用ソフトウェアシリアルの出力先は、dp9(9PIN)
標準シリアルに出力を出したい場合は、pc.printfに変えればよい
速度は9600bps



# include "mbed.h"

//10の割り算 0から1028までは、正しい。主に0から999
# define DVI10(n) ((n*205)>>11)

//#define S5851A 0x48
# define ADDR        ((0x48)<<1)   //  address

//Serial pc(USBTX, USBRX); // tx, rx
//Serial pc(SERIAL_TX, SERIAL_RX); //767
//Serial pc(PA_2, PA_3); //010
//Serial pc(PA_9, PA_10); //010
//Serial pc(dp16, dp15); // tx, rx 1114

//I2C i2c(I2C_SDA, I2C_SCL); //767
I2C i2c(dp5, dp27); //1114
//I2C i2c(PA_10, PA_9); //010

# define UART_DELAY (96) //  1/9600

//仮想シリアルの出力ポート
//DigitalOut TX(PA_9); //010
//DigitalOut TX(PA_2); //010
//DigitalOut TX(dp16); //1114  標準シリアルポートTX
DigitalOut TX(dp9); //1114 9PIN

//DigitalOut myled(D13);    //767
//DigitalOut myled(PA_4);   //010
//DigitalOut myled(dp14); //1114

//アナログ入力の設定
//AnalogIn adc_vbat(A3); //PA_4
//AnalogIn adc_vbat(A0); //767
//AnalogIn adc_vbat(dp4); //1114


//仮想シリアルへの一文字出力 9600bps
int pc_putc(char ch) {

    TX=1;

    TX=0;//START
    wait_us(UART_DELAY);

    for(int ii=0;ii<8;ii++){
        TX=(ch>>ii)&1;
        wait_us(UART_DELAY);
    }; //for

    TX=1;//Stop
    wait_us(UART_DELAY);

    return(0);

} //pc_putc

//文字列の表示
int pc_printf(char *str1) {

    //文字の中身がゼロか
    while(*str1){

        //一文字出力
        pc_putc(*str1 ++);

    } //while

    //戻り値
    return(0);
}//pc_printf



int     tempval;        //温度
char    data_read[6];   //i2cバッファー
int     n10;            //10の桁


int main() {

    //初期化
    TX=1;wait_ms(2);    //文字分のウエート
    pc_printf("\r\n");  //ゴミの吐き出し

    //無限ループ
    while(1) { 


        //初期化
        //set address 0
        i2c.write(ADDR, "\000", 1); //addres 0

        //温度の読み込み
        // Read temperature register
        i2c.read(ADDR, data_read,1,false);

        //温度の保存
        // Calculate temperature value in Celcius
        tempval =  data_read[0];



    
        //tempval = 12; //debug
    
        //計算
        // Calculation temp
        n10 = DVI10(tempval);             //10の桁
        tempval = tempval - ( n10 * 10 ); //1の桁

        //液晶に出力
        // Display result
        data_read[0] = '0' + n10;
        data_read[1] = '0' + tempval;
        data_read[2] = '\r';
        data_read[3] = '\n';
        data_read[4] = 0;

        //温度の出力
        //pc.printf(data_read); //1114
        pc_printf(data_read); //1114

        //1秒を待つ
        wait_ms(1000);
    } //while

} //main




i2c_s5851a_1114_1.jpg

0
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
0
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?