/***************************(abc897_18325TE.c)***
 *  I2C接続温度センサとEEPROMメモリ読み書き
 ************************************************/
#include "stdlib.h"
#include "string.h"
#include "mcc_generated_files/mcc.h"
#include "i2cLCD_ST7032i.h"
#define  ADT_ADR  0x48      // ADT I2Cアドレス
#define  DT_START 0x10      // データ格納開始アドレス
//---- グローバル変数 ----------
char     SBuf[BFSIZE];      // 文字列操作バッファー
char     RBuf[BFSIZE];      // シリアル受信バッファー
uint8_t  SFlg;              // シリアル受信フラグ
uint16_t TpNum = 0;         // 所得温度データ数
uint16_t TmInt = 1;         // 測定間隔秒数
//---- EEPRONMから1ワード読み出す ----
uint16_t EE_ReadWd(uint8_t adr){
    uint16_t tp;
    tp  = (uint16_t)DATAEE_ReadByte(0xF000 + adr);
    tp |= (uint16_t)DATAEE_ReadByte(0xF001 + adr)<<8;
    return tp;
}
//---- EEPROMに1ワード書き出す ----
void EE_WriteWd(uint8_t adr, uint16_t dt){
    DATAEE_WriteByte(0xF000 + adr, (uint8_t)dt);
    DATAEE_WriteByte(0xF001 + adr, (uint8_t)(dt >> 8));
}
//---- EEPROMの内容をダンプリスト表示 ----
void EE_dump(void){
    uint16_t a;
    uint8_t  b;
    LCD_clr(); LCD_str("*Dump");
    puts("\n\r*Dump");
    for(a = 0; a < 256; a++){
        if((a % 16)==0) printf("\n\r");
        b = DATAEE_ReadByte(0xF000 + a);
        printf("%02X ",b);
    }
}
//---- EEPROMの全てに指定されたデータを書き込む --
void EE_setNum(char *str){
    uint16_t a;
    uint8_t  b;
    b = (uint8_t)atoi(str);
    if((b > 0) || (str[0] == '0')){
        for(a = 0; a < 256; a++){
            if((a % 16)==0) printf("\n\r");
            DATAEE_WriteByte(0xF000 + a, b);
            printf(".");
        }
    } 
}
//---- 温度データの表示 ----
void prt_Temp(uint16_t n, uint16_t tDat){
    uint16_t  tp;
    sprintf(SBuf,"%3d: %04X ",n,tDat);
    printf(SBuf);
    LCD_cursor(0,1); LCD_str(SBuf);
    if(tDat & 0x8000){                    	// 負数なら
        tp = ((8192 - (tDat >> 3)) * 10) / 16;
        sprintf(SBuf,"-%2d.%1dC\r", tp / 10, tp % 10);
    }else{                               	// 正数
        tp = ((tDat >> 3) * 10) / 16;
        sprintf(SBuf,"%3d.%1dC\r", tp / 10, tp % 10);
    }
    puts(SBuf);
    LCD_cursor(10,1); LCD_str(SBuf);
}
//---- 温度データを読み出す -----
void readTemp(void){
    uint8_t  a,n;
    uint16_t   tp;
    for(n = 0; n < TpNum; n++){
        a = n * 2 + DT_START;
        tp = EE_ReadWd(a);
        prt_Temp(n,tp);
    }
}
//---- タイムインターバルの設定 ----
void setTmInt(char *str){
    uint16_t t;
    if(strlen(str) > 1){
        t = (uint16_t)atoi(&str[1]);
        if((t > 0) && (t < 0xFFFF)){
            TmInt = t;
            EE_WriteWd(0x0C,TmInt);
        }
    }
    printf("\n\rInterval: %d sec\n\r",TmInt);
}
/*-----------------------------------
 *       Main application
 *-----------------------------------*/
void main(void){
    uint8_t eAdr;               // EEPROMアドレス
    uint16_t tct;               // インターバルカウンタ
    uint8_t  md = 0;            // 0:測定停止、1:測定中
    uint8_t  cmd = 0;           // 指示コマンド
    uint16_t temp;              // 測定温度データ
    SYSTEM_Initialize();
    LCD_init();
    INTERRUPT_GlobalInterruptEnable();
    INTERRUPT_PeripheralInterruptEnable();
   
    tct = TmInt;
    eAdr = DT_START;
    sprintf(SBuf,"ready\n\r>");
    printf(SBuf);
    LCD_str(SBuf);
    
    while (1)    {
        if(SFlg){
            cmd = RBuf[0];
            LCD_clr(); LCD_str(RBuf);
        }
        //--- コマンド解析
        switch (cmd){
            case 'G': md = 1; puts("*start\r");
                        eAdr = DT_START; TpNum = 0; break;
            case 'X': md = 0; puts("*stop\r"); break;
            case 'N': case 'n':
                        printf(" Num: %d\n\r",TpNum);  break;
            case 'T': setTmInt(RBuf);   break;
            case 'R': case 'r':
                        readTemp();   break;
            case 'U': case 'u': 
                        EE_dump();   break;
            case 'S': EE_setNum(RBuf + 1); break;
            default :   break;    
        }
        //--- 測定動作
        if(md){
            I2C1_ReadNBytes(ADT_ADR,(uint8_t *)&temp,2); // 温度データ読込
            temp = (temp<<8) | ((uint8_t)(temp>>8));     // 上下バイトデータ入替
            if(tct == 1){
                prt_Temp(TpNum, temp);
                if(eAdr < 255){
                    EE_WriteWd(eAdr,temp); 
                    eAdr += 2;
                    TpNum++;
                    EE_WriteWd(0x0E,TpNum);
                }
                tct = TmInt;
            }else{
                tct--;
            }
        }
        if(cmd && (md == 0)) printf("\n\r>");
        cmd = 0; SFlg = 0;
        IO_RA4_SetHigh();
        __delay_ms(400);
        IO_RA4_Toggle();
        __delay_ms(550);            // ほぼ1秒確保
    }    
}
/******  End of File  ******/
 
 |