/*
87K_AI.c :This demo program is for 87K Analog Input Modules in Com0. 
         User must to know: 
            1.The com0's baudrate is 115200. User don't modify it.
            2.User need to know the module's location in the MCU.     
    
Compiler: BC++ 3.1,                                                            
          Turbo C ++ 1.01(3.01) (free from http://community.borland.com/museum)
          MSC 6.0,                                                             
          MSVC 1.52.                                                           
                                                                               
Compile mode: large                                                            
                                                                               
Project: 87K_AI.c                                                               
         ..\Lib\(8000.Lib, 8000E.lib)   
                                                                               
Hardware: 8000

[24 May,2005] by Bill
*/
#include "..\..\lib\vp2k.h"
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>

int Set87KAI_Config(int iSlot,int iRangeCode,int iFilter,int iDataFormat);   
int Read87K_AI(int iSlot,int iChannel,float *fValue);                            
int Read87K_AI_Hex(int iSlot,int iChannel,int *iValue);                          
int Read87KAI_Config(int iSlot,int *iRangeCode,int *iDefaultFilter,int *iDataFormat_Original);           

void main()
{
    int iRet,iSlot,iChannel;
    
    int iDataFormat,iDataFormat_Original;
    int iFilter,iFilter_Original;
    int iRangeCode,iRangeCode_Original;
    unsigned char cTemp[10];
    
    float fValue;
             
    InitLib();
    
    Print("/*******************/\n\r");
    Print("/*    87K_AI demo  */\n\r");
    Print("/*                 */\n\r");
    Print("/*  [8 Nov,2004]   */\n\r");
    Print("/*******************/\n\r");
    Print("\n");
    
    InstallCom_0(115200L,8,0,0);
    
    Print("Please Input Slot Number(0~3 OR 0~7) =");
    LineInput(cTemp,10);
    sscanf(cTemp,"%d",&iSlot);
     
    //Filter Selection==> 0x0:60Hz     0x1:50Hz        
    iFilter=0x0;
    
    //Data Format Selection==>0x0: Engineering units    
    //                        0x1: % of FSR
    //                        0x2: 2's Complement Hexadecimal  
    iDataFormat=0x0;
    
    //For detailed specifications refer to,
    //CD:Napdos\DCON\IO_Module\hw_dcon_on_87kUnit
    iRangeCode=0x0F;      //87017:Range code 09(-10V to +10V)        
    
    //To prevent reading EEPROM again.
    iRet=Read87KAI_Config(iSlot,
                          &iRangeCode_Original,
                          &iFilter_Original,
                          &iDataFormat_Original);
    if(iRet==NoError)
    {
        if((iRangeCode_Original==iRangeCode) 
           && (iFilter_Original==iFilter) 
           && (iDataFormat_Original==iDataFormat))
        {
        //The present configuration is the same as the input configuration.
        //So there is no need to write into the EEPROM.                          
        }  
        else
        {    
            Set87KAI_Config(iSlot,iRangeCode,iFilter,iDataFormat);
     	    /*
            87K module needs almost 1 second to effect new settings.
            So, the program needs to delay 1 second to wait the new setting
            works. But 1 second is greater than the Watchdog timeout value (0.8 second).
            Thus we need to call "RefreshWDT" to prevent it from resetting the CPU 
            before the process can be completed.
            */
            Delay(500);
            RefreshWDT();
            Delay(500);
            RefreshWDT();
        }
    }
    else
       Print("Read Configuration Error");
    
    for(;;)
    {
        for(iChannel=0;iChannel<=7;iChannel++)
        {
            iRet=Read87K_AI(iSlot,iChannel,&fValue);                    
            if(iRet==NoError)
            {
                Print("Channel %d= %6.3f\n\r",iChannel,fValue);
				
                
            }
            else                                                             
            {
                
				Print ("Channeld %d have response error. Error code=%d\n\r",iChannel,iRet);
				//Print("AI has received a response error. Error Code =%d\n\r",iRet);
            }
            if(iChannel ==7)
            {
                Print("\n\r");
            }
        }
    }
    RestoreCom0();
}
//=====================================================================
//====== The Following functions are used to read 87K_AI modules.   ===
//====== You can copy the following functions to your own program.  ===
//=====================================================================
int Read87KAI_Config(int iSlot,
                     int *iRangeCode_Original,
                     int *iFilter_Original,
                     int *iDataFormat_Original)      
{                                                                                                 
    /*
    DCON protocol to read the AI Configuration                                                            
     Command: $AA2                                                                            
     Response: !AATTCCFF                                                                      
     
     Slot: 0 ~ 7
     RangeCode:    For detailed specifications, refer to            
                    CD:8000\Napdos\DCON\IO_Module\hw_dcon_on_87kUnit 
                    CD:8000\Napdos\7000\Manual                       
     
     Filter:    0 = 60Hz rejection 
                1 = 50Hz rejection 
                This setting is reserved for the I-87015/I-87015T and should be zero. 
     
     DataFormat:    00 = Engineer Unit Format 
                    01 = % of FSR Format 
                    10 = 2's Complement Hexdecimal Format 
                    11 = Ohms in Engineer Unit Format For I-87013 Only 
    */    
    
    unsigned char InBufCom0[20];                                                                  
    int iRet;                                                                        
                                                                                                  
    ChangeToSlot(iSlot);                                                                          
    iRet=SendCmdTo7000(0,"$002",0); //Com port=0, CheckSum=0 (0:disable 1:Enable)                                                                          
    iRet=ReceiveResponseFrom7000_ms(0,InBufCom0,100,0); //Com port=0, TimeOut=100, CheckSum=0 (0:disable 1:Enable)                                                             
                                                                                                  
    if(iRet==NoError)                                                                             
    {                                                                                             
        if(InBufCom0[0]== '!')                                                                    
        {                                                                                         
            *iRangeCode_Original= ((ascii_to_hex(InBufCom0[3])<<4)&0xF0)
                                   +(ascii_to_hex(InBufCom0[4])&0xF); 
            *iFilter_Original= (ascii_to_hex(InBufCom0[7])&0xF);                                    
            *iDataFormat_Original= (ascii_to_hex(InBufCom0[8])&0xF);                                    
                                                                                                  
            return NoError;                                                                       
        }                                                                                         
        else                                                                                      
            return -1;//Response string error.                                                        
    }                                                                                             
    else                                                                                          
        return iRet;                                                                              
}                                                                                                 

int Set87KAI_Config(int iSlot,int iRangeCode,int iFilter,int iDataFormat)
{
    /*
    DCON protocol to write the AI Configuration
     Command: %AANNTTCCFF
     Response: !AA
     
     Slot: 0 ~ 7
     RangeCode:     For detailed specifications, refer to           
                    CD:8000\Napdos\DCON\IO_Module\hw_dcon_on_87kUnit
     
     Filter:    0 = 60Hz rejection 
                1 = 50Hz rejection 
                This setting is reserved for the I-87015/I-87015T and should be zero. 
     
     DataFormat:    00 = Engineer Unit Format 
                    01 = % of FSR Format 
                    10 = 2's Complement Hexdecimal Format 
                    11 = Ohms in Engineer Unit Format For I-87013 Only 
    */
    
    
    unsigned char OutBufCom0[20],InBufCom0[20];                
    int iRet;                                                     
    
    OutBufCom0[0]=0x25;
    sprintf(OutBufCom0+1,"0001%02X0A%01X%01X",iRangeCode,iFilter,iDataFormat);
    ChangeToSlot(iSlot);                                          
    iRet=SendCmdTo7000(0,OutBufCom0,0); //Com port=0, CheckSum=0 (0:disable 1:Enable)                                      
    iRet=ReceiveResponseFrom7000_ms(0,InBufCom0,100,0); //Com port=0, TimeOut=100, CheckSum=0 (0:disable 1:Enable)                          
                                                                  
    if(iRet==NoError)                                             
    {                                                             
        if(InBufCom0[0]== '!')                                 
        {                                                         
            return NoError;                                       
        }                                                         
        else                                                      
            return -1;//Response string error.                       
    }                                                             
    else                                                          
        return iRet;                                              
}

//Engineer Unit Format
int Read87K_AI(int iSlot,int iChannel,float *fValue)
{
    /*
    DCON protocol to read the AI value
     Command: #AAN
     Response: >(data)
     
     Slot: 0 ~ 7
     Channel: 0 ~ 8
    */
    
    unsigned char OutBufCom0[20],InBufCom0[20];
    int iRet;

    OutBufCom0[0]=0x23;
    sprintf(OutBufCom0+1,"00%01X",iChannel);
    ChangeToSlot(iSlot);    
    iRet=SendCmdTo7000(0,OutBufCom0,0); //Com port=0, CheckSum=0 (0:disable 1:Enable)
	//Delay(1000);
    iRet=ReceiveResponseFrom7000_ms(0,InBufCom0,100,0); //Com port=0, TimeOut=100, CheckSum=0 (0:disable 1:Enable)                  

	Print ("OutBufCom0 = %s    ",OutBufCom0);
    Print ("InBufCom0 = %s   \n\r",InBufCom0);
	

    if(iRet==NoError)
    {
        if(InBufCom0[0]== '>')
        {
            *fValue=atof(InBufCom0+1);
            //Delay(1000);
            return NoError;   

        }
        else
            return -999;//Response string error.
    }
    else
        return iRet;    
}

// 2's Complement Hexdecimal Format                                                   
int Read87K_AI_Hex(int iSlot,int iChannel,int *iValue)       
{                                                            
    /*
    DCON protocol to read the AI value
     Command: #AAN
     Response: >(data)
     
     Slot: 0 ~ 7
     Channel: 0 ~ 8
    */
    
    unsigned char OutBufCom0[20],InBufCom0[20];                
    int iRet,iHex;                                           
    OutBufCom0[0]=0x23;                                      
    sprintf(OutBufCom0+1,"00%01d",iChannel);                 
                                                             
    ChangeToSlot(iSlot);                                     
    iRet=SendCmdTo7000(0,OutBufCom0,0); //Com port=0, CheckSum=0 (0:disable 1:Enable)                                 
    iRet=ReceiveResponseFrom7000_ms(0,InBufCom0,100,0); //Com port=0, TimeOut=100, CheckSum=0 (0:disable 1:Enable)                  
                                                             
    if(iRet==NoError)                                        
    {                                                        
        if(InBufCom0[0]== '>')                                 
        {                                                    
            sscanf(InBufCom0+1,"%04X",&iHex);                  
            *iValue=iHex;                                    
            return NoError;                                  
        }                                                    
        else                                                 
            return -1;//Response string error.                   
    }                                                        
    else                                                     
        return iRet;                                         
}                                                            
                                                           