3.4.9_プログラムリスト(THERMOLOGGER_User.c)

修正箇所(自動生成ファイルに記述追加)は、以下の青色の箇所です。
#include "C:\\WIZ_C\\Ver12\\Projects\\THERMOLOGGER\\THERMOLOGGER_Auto.h"
#include "C:\\WIZ_C\\Ver12\\Projects\\THERMOLOGGER\\i2c.h"
#include "C:\\WIZ_C\\Ver12\\Projects\\THERMOLOGGER\\SHTFunc.h"

#include <stdlib.h>
#include <Strings.h>
#include <Delays.h>

#__config _CONFIG1H, 0x02
#__config _CONFIG1L, 0x00
#__config _CONFIG2H, 0x0E
#__config _CONFIG2L, 0x0E
#__config _CONFIG3H, 0x01
#__config _CONFIG3L, 0x00
#__config _CONFIG4H, 0x00
#__config _CONFIG4L, 0x81

#define CalcTMP10() (ADValTmp-4000+5)/10;        // SHT11 温度変換マクロ(10倍値)

WORD    ADValHR, ADValTmp, hr10, Temp10, hr, temp;    //SHT11 測定A-D値(Temp10:測定温度の摂氏温度値を10倍した値)
int    TimerCount1, TimerCount2, TimerCount3, TimerCount4, StrCount, cursorNo_Set;
BYTE    out_str1[17], out_str2[17], Transmit_Buf[32], Receive_Buf[32], clock_Buf[7], data_Buf[16];
BYTE    sec, min, hour, day, week, month, year, samp, time_Work1;
BYTE    sec_Set, min_Set, hour_Set, day_Set, month_Set, year_Set, samp_Set;
BYTE    bdat_H, bdat_L, PBBuf;
bool    MeasHR, MeasTmp;                //SHT11測定中フラグ
bit    LeftKey, RightKey, UpKey, DownKey, ModeKey, SetKey, Hosyu1Key, Hosyu2Key, SetMode;
bit    Flag10mSec, Flag50mSec, Flag100mSec, Flag500mSec, Flag1000mSec;
bit    write_Set;

WORD CalcHR10(void);                    //SHT11 湿度変換関数(10倍値)
void I2C_RTC_Write(BYTE *data);
void I2C_RTC_Read(BYTE *data);
void I2C_ROM_AdrsSet(WORD adrs);
void I2C_ROM_Write(WORD adrs, BYTE *data, BYTE size);
void I2C_ROM_Read(WORD adrs, BYTE *data, int size);
void TxStr(BYTE *Transmit_Buf);
void CmdProc();
void DispCursor(int cursorNo_Set);

//
// This file includes all user definable routines. It may be changed at will as
// it will not be regenerated once the application has been generated for the
// first time.
//

//*******************************************************************************
//
// Insert your interrupt handling code if required here.
// Note quick interrupts are used so code must be simple
// See the manual for details of quick interrupts.
//

void UserInterrupt()
{

    // Insert your code here
    if(PIR1 & (1<<CCP1IF))
    {
        PIR1 &= ~(1<<CCP1IF);
        Flag10mSec = 1;
        TimerCount1 = TimerCount1 + 1;
        TimerCount2 = TimerCount2 + 1;
        TimerCount3 = TimerCount3 + 1;
        TimerCount4 = TimerCount4 + 1;
        if(TimerCount1 == 5)
        {
            Flag50mSec = 1;
            TimerCount1 = 0;
        }
        if(TimerCount2 == 10)
        {
            Flag100mSec = 1;
            TimerCount2 = 0;
        }
        if(TimerCount3 == 50)
        {
            Flag500mSec = 1;
            TimerCount3 = 0;
        }
        if(TimerCount4 == 100)
        {
            Flag1000mSec = 1;
            TimerCount4 = 0;
        }
    }
#asmline goto UserIntReturn    ; PIC Assembler - go back to interrupt routine
}


//*******************************************************************************
//
// Insert your initialisation code if required here.
// Note that when this routine is called Interrupts will not be enabled - the
// Application Designer will enable them before the main loop
//

void UserInitialise()
{

    samp = ReadEEData(0);    //sampling cycle(1, 5, 15, 30, 60sec)
    if(!((samp >= 1) && (samp <= 60)))
    {
        samp = 15;
        WriteEEData(0,samp);
    }
    INTCON |= 1<<PEIE;
    PIE1 |= 1<<CCP1IE;
}

//*******************************************************************************
//
// Insert your main loop code if required here. This routine will be called
// as part of the main loop code
//

void UserLoop()
{
    if(Flag10mSec)
    {
        PIE1 &= ~(1<<CCP1IE);
        Flag10mSec = 0;
        PIE1 |= 1<<CCP1IE;
    }

    if(Flag50mSec)
    {
        PIE1 &= ~(1<<CCP1IE);
        Flag50mSec = 0;
        PIE1 |= 1<<CCP1IE;
    }

    if(Flag100mSec)
    {
        if(write_Set)
        {
            //clock write(RTC)
            time_Work1 = sec_Set / 10;
            clock_Buf[0] = (time_Work1 << 4) + (sec_Set % 10);
            time_Work1 = min_Set / 10;
            clock_Buf[1] = (time_Work1 << 4) + (min_Set % 10);
            time_Work1 = hour_Set / 10;
            clock_Buf[2] = (time_Work1 << 4) + (hour_Set % 10);
            time_Work1 = day_Set / 10;
            clock_Buf[3] = (time_Work1 << 4) + (day_Set % 10);
            time_Work1 = month_Set / 10;
            clock_Buf[5] = (time_Work1 << 4) + (month_Set % 10);
            time_Work1 = year_Set / 10;
            clock_Buf[6] = (time_Work1 << 4) + (year_Set % 10);
            I2C_RTC_Write(clock_Buf);
            WriteEEData(0,samp_Set);    //sampling cycle(1, 5, 15, 30, 60sec)
            sec = sec_Set;
            min = min_Set;
            hour = hour_Set;
            day = day_Set;
            month = month_Set;
            year = year_Set;
            samp = samp_Set;
            SetMode = 0;
            write_Set = 0;
        }
        else
        {
            //clock read(RTC)
            I2C_RTC_Read(clock_Buf);
            time_Work1 = clock_Buf[0];
            sec = ((time_Work1 >> 4) & 0x07) * 10 + (time_Work1 & 0x0F);
            time_Work1 = clock_Buf[1];
            min = ((time_Work1 >> 4) & 0x07) * 10 + (time_Work1 & 0x0F);
            time_Work1 = clock_Buf[2];
            hour = ((time_Work1 >> 4) & 0x03) * 10 + (time_Work1 & 0x0F);
            time_Work1 = clock_Buf[3];
            day = ((time_Work1 >> 4) & 0x03) * 10 + (time_Work1 & 0x0F);
            time_Work1 = clock_Buf[5];
            month = ((time_Work1 >> 4) & 0x01) * 10 + (time_Work1 & 0x0F);
            time_Work1 = clock_Buf[6];
            year = ((time_Work1 >> 4) & 0x00) * 10 + (time_Work1 & 0x0F);
        }
        PIE1 &= ~(1<<CCP1IE);
        Flag100mSec = 0;
        PIE1 |= 1<<CCP1IE;
    }

    if(Flag500mSec)
    {
        if(SetMode)
        {
            //LCD display
            sprintf(out_str1,"%02d/%02d/%02d Sample",year_Set, month_Set, day_Set);
            sprintf(out_str2,"%02d:%02d:%02d %02dmin ",hour_Set, min_Set, sec_Set, samp_Set);
            LCDPrintAt(0,0);
            LCDString(out_str1);
            LCDPrintAt(0,1);
            LCDString(out_str2);
            DispCursor(cursorNo_Set);
        }
        else
        {
            //LCD display
            sprintf(out_str1,"%02d/%02d/%02d %3d.%01d%cC",year,month,day, temp/10, temp%10, 0xdf);
            sprintf(out_str2,"%02d:%02d:%02d %3d.%01d%% ",hour,min,sec, hr/10, hr%10);
            LCDPrintAt(0,0);
            LCDString(out_str1);
            LCDPrintAt(0,1);
            LCDString(out_str2);
        }
        PIE1 &= ~(1<<CCP1IE);
        Flag500mSec = 0;
        PIE1 |= 1<<CCP1IE;
    }

    if(Flag1000mSec)
    {
        // SHT11湿度測定開始(12bit)
        SHTTSSeq();            // TSシーケンス開始
        SHTWrite(0x05);            // measure humidity
        MeasHR = 1;            // 湿度測定中

        // I2C-LCDへ温度、湿度を表示
        hr10 = CalcHR10();        // 湿度の工学値変換
        Temp10 = CalcTMP10();

        if((hr10 < 1000) && (Temp10 < 1000))
        {
            hr = hr10;
            temp = Temp10;
        }
        PIE1 &= ~(1<<CCP1IE);
        Flag1000mSec = 0;
        PIE1 |= 1<<CCP1IE;
    }

    // SHT11温度、湿度の測定完了チェック
    if(MeasHR)
    {
        // 湿度測定中
        if(!(SHT_PORT & (1<<bitDATA)))
        {    // 測定完了チェック
            // SHT11湿度測定終了
            MeasHR = 0;
            //湿度を読み出す
            bdat_H = SHTRead(0);    // read high byte(ACK)
            bdat_L = SHTRead(1);    // read low byte(NOACK)
            ADValHR = ((WORD)bdat_H << 8) | bdat_L;

            // SHT11温度測定開始(14bit)
            SHTTSSeq();        // TSシーケンス開始
            SHTWrite(0x03);        // measure temperature
            MeasTmp = 1;        // 温度測定中
        }
    }
    else if(MeasTmp)
    {
        // SHT11温度測定中
        if(!(SHT_PORT & (1<<bitDATA)))
        {    // 測定完了チェック
            // SHT11温度測定終了
            MeasTmp = 0;
            // 温度を読み出す
            bdat_H = SHTRead(0);    // read high byte(ACK)
            bdat_L = SHTRead(1);    // read low byte(NOACK)
            ADValTmp = ((WORD)bdat_H << 8) | bdat_L;
        }
    }

    if(ModeKey)
    {
        ModeKey = 0;
        if(SetMode)
            SetMode = 0;
        else
            SetMode = 1;
        if(SetMode)
        {
            sec_Set = sec;
            min_Set = min;
            hour_Set = hour;
            day_Set = day;
            month_Set = month;
            year_Set = year;
            samp_Set = samp;
            cursorNo_Set = 0;
        }
        LCDClear();
    }

    if(Hosyu1Key)
    {
        Hosyu1Key = 0;
    }

    if(Hosyu2Key)
    {
        Hosyu2Key = 0;
    }
    if(SetMode)
    {
        if(LeftKey)
        {
            LeftKey = 0;
            if(cursorNo_Set == 0)
                cursorNo_Set = 6;
            else
                cursorNo_Set = cursorNo_Set - 1;
        }
        if(RightKey)
        {
            RightKey = 0;
            if(cursorNo_Set == 6)
                cursorNo_Set = 0;
            else
                cursorNo_Set = cursorNo_Set + 1;
        }
        if(UpKey)
        {
            UpKey = 0;
            switch(cursorNo_Set)
            {
                case 0:
                    if(year_Set >= 99)
                        year_Set = 0;
                    else
                        year_Set = year_Set + 1;
                    break;
                case 1:
                    if(month_Set >= 12)
                        month_Set = 1;
                    else
                        month_Set = month_Set + 1;
                    break;            
                case 2:
                    if(day_Set >= 31)
                        day_Set = 1;
                    else
                        day_Set = day_Set + 1;
                    break;
                case 3:
                    if(hour_Set >= 23)
                        hour_Set = 0;
                    else
                        hour_Set = hour_Set + 1;
                    break;
                case 4:
                    if(min_Set >= 59)
                        min_Set = 0;
                    else
                        min_Set = min_Set + 1;
                    break;
                case 5:
                    if(sec_Set >= 59)
                        sec_Set = 0;
                    else
                        sec_Set = sec_Set + 1;
                    break;
                case 6:
                    switch(samp_Set)
                    {
                        case 1:
                            samp_Set = 5;
                            break;
                        case 5:
                            samp_Set = 15;
                            break;
                        case 15:
                            samp_Set = 30;
                            break;
                        case 30:
                            samp_Set = 60;
                            break;
                        case 60:
                            samp_Set = 1;
                    }
            }
        }
        if(DownKey)
        {
            DownKey = 0;
            switch(cursorNo_Set)
            {
                case 0:
                    if(year_Set == 0)
                        year_Set = 99;
                    else
                        year_Set = year_Set - 1;
                    break;
                case 1:
                    if((month_Set == 1) || (month_Set == 0))
                        month_Set = 12;
                    else
                        month_Set = month_Set - 1;
                    break;
                case 2:
                    if(day_Set == 1 || (day_Set == 0))
                        day_Set = 31;
                    else
                        day_Set = day_Set - 1;
                    break;
                case 3:
                    if(hour_Set == 0)
                        hour_Set = 23;
                    else
                        hour_Set = hour_Set - 1;
                    break;
                case 4:
                    if(min_Set == 0)
                        min_Set = 59;
                    else
                        min_Set = min_Set - 1;
                    break;
                case 5:
                    if(sec_Set == 0)
                        sec_Set = 59;
                    else
                        sec_Set = sec_Set - 1;
                    break;
                case 6:
                    switch(samp_Set)
                    {
                        case 1:
                            samp_Set = 60;
                            break;
                        case 5:
                            samp_Set = 1;
                            break;
                        case 15:
                            samp_Set = 5;
                            break;
                        case 30:
                            samp_Set = 15;
                            break;
                        case 60:
                            samp_Set = 30;
                    }
            }
        }
        if(SetKey)
        {
            SetKey = 0;
            write_Set = 1;
        }
    }
}

//
// 湿度の工学値変換(計算結果の単位は X10%)
//
WORD CalcHR10(void)
{
    WORD ad, x1, x2, x3, x4, cd10, b10;

    // 桁ごとの数値を取り出す
    ad = ADValHR;
    x1 = ad / 1000;
    ad = ad - 1000 * x1;
    x2 = ad / 100;
    ad = ad - 100 * x2;
    x3 = ad / 10;
    x4 = ad - 10 * x3;

    // 桁ごとにA-D値を掛け算する
    x1 = ADValHR * x1;
    x2 = (ADValHR * x2 + 5) / 10;        // 小数点以下四捨五入
    x3 = (ADValHR * x3 + 50) / 100;        // 小数点以下四捨五入
    x4 = (ADValHR * x4 + 500) / 1000;    // 小数点以下四捨五入

    x1 = x1 + x2 + x3 + x4;            // 計算結果を合成
    x2 = (x1 + 5) / 10;            // 1/10する. 小数点以下四捨五入

    cd10 = (28 * x2 + 50) / 100;

    x1 = (ADValHR * 4 + 5) / 10;
    x2 = (ADValHR * 5 + 500) / 1000;
    b10 = x1 + x2;

    return -40 + b10 - cd10;        // 湿度値を10倍した整数値
}

void DispCursor(int cursorNo_Set)
{
    int    x, y;
    switch(cursorNo_Set)
    {
        case 0:
            x = 1;
            y = 0;
            break;
        case 1:
            x = 4;
            y = 0;
            break;
        case 2:
            x = 7;
            y = 0;
            break;
        case 3:
            x = 1;
            y = 1;
            break;
        case 4:
            x = 4;
            y = 1;
            break;
        case 5:
            x = 7;
            y = 1;
            break;
        case 6:
            x = 12;
            y = 1;
    }
    LCDPrintAt(x, y);
    LCDOnOff(1, 0, 1);
}

//
// User occurrence code
//

//
// Occurrence - A key has been pressed (or is repeating)
//

void KeyPress()
{
    switch(KP4Value)
    {
        case 0:UpKey = 1;break;
        case 1:DownKey = 1;break;
        case 2:LeftKey = 1;break;
        case 3:RightKey = 1;break;
        case 4:ModeKey = 1;break;
        case 5:SetKey = 1;break;
        case 6:Hosyu1Key = 1;break;
        case 7:Hosyu2Key = 1;break;
    }
}

//
// Occurrence - Byte received on serial interface
//

void GetRX()
{
    BYTE ch;
    if(GetRxSize() > 0)
    {
        ch = WaitRx();
        if(ch == '\r')                //'\r'(0x0D)
        {
            Receive_Buf[StrCount] = 0;    //NULL
            StrCount = 0;
            CmdProc();
        }
        if(ch == '\n')
        {
            strcpy(Receive_Buf,"");
            StrCount = 0;
        }
        else
        {
            Receive_Buf[StrCount] = ch;
            StrCount = StrCount + 1;
        }
    }
}

void CmdProc()
{
    if(strcmp(Receive_Buf,"TEST01") == 0)
    {
        TxStr("TEST01abcdefgh\r\n");
    }
    if(strcmp(Receive_Buf,"TEST02") == 0)
    {
        TxStr("TEST02abcdefgh\r\n");
    }

    strcpy(Receive_Buf,"");
}

void TxStr(BYTE *Transmit_Buf)
{
    int i;
    for(i = 0; i < 16; i++)            //16byte送信
        AddTx(Transmit_Buf[i]);
}

//
// I2C-RTC DATA WRITE (7byte Write: sec, min, hour, day, week, month, year)
//
void I2C_RTC_Write(BYTE *data)
{
    int i;
    I2CStart(I2C_START_NORM);
    I2CWrite(0xA2);                // control byte (write)
    I2CWrite(0x02);                // Address set (sec)
    for(i = 0; i < 7; i++)
    {
        I2CWrite(*data);
        data++;
    }
    I2CStop();
}

//
// I2C-RTC DATA READ (7byte Read: sec, min, hour, day, week, month, year)
//
void I2C_RTC_Read(BYTE *data)
{
    int i;
    I2CStart(I2C_START_NORM);
    I2CWrite(0xA2);                // control byte (write)
    I2CWrite(0x02);                // Address set (sec)
    I2CStart(I2C_START_CONT);
    I2CWrite(0xA3);                // control byte (read)
    for(i = 0; i < 7; i++)
    {
        if(i != 6)
            *data = I2CRead(I2C_READ_ACK);
        else
            *data = I2CRead(I2C_READ_NOACK);
        data++;
    }
    I2CStop();
}

//
// I2C-EEPROM DATA WRITE
//
void I2C_ROM_Write(WORD adrs, BYTE *data, BYTE size)
{
    int i;
    I2C_ROM_AdrsSet(adrs);
    for(i = 0; i < size; i++)
    {
        I2CWrite(*data);
        data++;
    }
    I2CStop();
}

//
// I2C-EEPROM DATA READ
//
void I2C_ROM_Read(WORD adrs, BYTE *data, int size)
{
    int i;
    I2C_ROM_AdrsSet(adrs);
    I2CStart(I2C_START_CONT);
    I2CWrite(0xA1);                // control byte (read)
    for(i = 0; i < size; i++)
    {
        if(i != (size - 1))
            *data = I2CRead(I2C_READ_ACK);
        else
            *data = I2CRead(I2C_READ_NOACK);
        data++;
    }
    I2CStop();
}

//
// I2C-EEPROM DATA READ/WRITE ADDRES SET
//
void I2C_ROM_AdrsSet(WORD adrs)
{
    I2CStart(I2C_START_NORM);
    I2CWrite(0xA0);                // control byte (write)
    I2CWrite(adrs>>8);            // EEPROM Address(H)
    I2CWrite(adrs);                // EEPROM Address(L)
}




トップページヘもどる