右側のほうのセンサLPC1114FN28 fumiya版

Dependencies:   IRM2121_2 Ping mbed HMC6352 Watchdog

Fork of CatPot_SensorRight by CatPot 2015-2016

main.cpp

Committer:
lilac0112_1
Date:
2015-02-27
Revision:
8:52f8023cdb8f
Parent:
6:f64989ad4746
Child:
9:c992dfaa77ce

File content as of revision 8:52f8023cdb8f:

/*
 *メインと通信するLPC1114のプログラム()
 *
 *現在は両方がmainと通信します.
 * 
 */

#include "mbed.h"


#define SYSAHBCLKDIV_Val      0x00000001        // Reset: 0x001
#define CLOCK_SETUP           1                  // デフォルト=1
#define SYSPLLCTRL_Val        0x00000023        // デフォルト=23 Reset: 0x000
#define SYSPLLCLKSEL_Val      0x00000001        // デフォルト=0  Reset: 0x000
#define MAINCLKSEL_Val        0x00000003        // デフォルト=3  Reset: 0x000

#include "IRM2121.h"
#include "Ping.h"
#include "WatchDogTimer.h"

#define PING_COUNT 5 //5回に1回にPing処理
#define IR_LEVEL 400 //irのしきい値
#define I2C_ADDRESS 0xAA+2 //



IRM2121 Ir[6] = {dp17,dp18,dp24,dp25,dp26,dp28};

Ping Ping1(dp1,dp2);
Ping Ping2(dp4,dp6);
DigitalOut Led(LED1);

I2CSlave Mbed(dp5,dp27);//sda,scl
Serial pc(USBTX,USBRX);
 




int main() {
    
    /*begin SetUp*/
    //default Mbed.frequency();
    Mbed.address(I2C_ADDRESS);
    Watchdog g;
    g.kick(1.0f);//1秒でWDT発生
    /*end   Setup*/
    
    /*Count*/
    uint8_t PingCk = 0;
    int i;
    
    /*Data*/
    uint8_t PingData[2] = {0};
    unsigned int ir__data[6] = {0};
    
    /*State(operate_value etc...)*/
    int I2CState;
    
    /* I2Cdata*/
    char SendData[6]={0};
    char AddressData[1] = {0};
    

    while(1) {
        
        /*Ping(圧縮1/1)*/
        /*if(PingCk >= PING_COUNT){
            
            Ping1.Send();
            wait_ms(30);
            PingData[0] = Ping1.Read_cm();
            
            if(I2C_ADDRESS==0xAA){
                Ping2.Send();    
                wait_ms(30);
                PingData[1] = Ping2.Read_cm();
            }
            
            for(i=0; i < 2; i++){
                
                if(PingData[i] > 125) PingData[i] = 125;
                
            }
            
            PingCk = 0;
        }*/
        
        /*Ir(圧縮1/10)*/
        for(i=0; i < 6; i++){
            
            ir__data[i] = Ir[i].Read();
            
            if(ir__data[i] > 1000) ir__data[i] = 1000;
            ir__data[i] = ir__data[i] / 10;
        }
        
        I2CState = Mbed.receive();
        
        switch(I2CState){
            
            case I2CSlave::ReadAddressed:
            
            if(AddressData[0]){
                switch(AddressData[0]){
                    case 0x01://ping
                        SendData[0] = (char)PingData[0];
                        SendData[1] = (char)PingData[1];
                        Mbed.write(SendData, 2);
                        break;
                    case 0x00:
                    case 0x02://IR
                        for(i=0; i < 6; i++) SendData[i] = (char)ir__data[i];
                        
                        /*for(i=0; i<6; i++){
                            pc.printf("%d ", (char)SendData[i]);
                        }
                        pc.printf("\n");*/
                        
                        Mbed.write(SendData, 6);
                        
                        break;
                    default:
                        break;
                }
            }
                 
                 AddressData[0] = 0x00;
                 break;
            case I2CSlave::WriteGeneral:
                 Mbed.read(AddressData,1);
                 break;
            case I2CSlave::WriteAddressed:
                 Mbed.read(AddressData,1);
                 break;
        
        }
        Led = 0;
        
        PingCk ++;
        
    }
}