/*
87024.c :This demo program is for 87024 Analog Output Module 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: 87024.c                                                               
         ..\Lib\(8000.Lib, 8000E.lib)   
                                                                               
Hardware: 8000

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

int Read87024_Config(int iSlot,int *iRangeCode_Original,int *iSlewRate_Original);
int Write87K_87024AO(int iSlot,int iChannel,float fValue);
int Set87024_Config(int iSlot,int iChannel,int iRangeCode);

void main()
{
    int iRet,iSlot,iChannel;
    
    int iRangeCode,iRangeCode_Original;
    int iSlewRate,iSlewRate_Original;
    unsigned char cTemp[10];
    
    float fValue;
    
    InitLib();
    
    Print("/*******************/\n\r");
    Print("/*    87024 demo   */\n\r");
    Print("/*                 */\n\r");
    Print("/*  [8 Nov,2004]  */\n\r");
    Print("/*******************/\n\r");
    Print("\n\r");                     
    
    InstallCom_0(115200L,8,0,0);
        		
    Print("Please Input Slot Number(0 to 7) =");
    LineInput(cTemp,10);
    sscanf(cTemp,"%d",&iSlot);
    	
    iRangeCode=0x32;    // 0 to +10V
    
    //For detailed specifications, refer to 
    //CD:Napdos\DCON\IO_Module\hw_dcon_on_87kUnit
    iSlewRate=0x0;  //Immediate output 
    
    //To prevent reading EEPROM again.
    iRet=Read87024_Config(iSlot,&iRangeCode_Original,&iSlewRate_Original);
    if(iRet==NoError)
    {
        if((iRangeCode_Original==iRangeCode) && (iSlewRate_Original==iSlewRate))    
        {
        //The present configuration is the same as the input configuration. 
        //So there is no need to write into the EEPROM.           
        }
        else
            Set87024_Config(iSlot,iRangeCode,iSlewRate);
    }                                            
    else                                         
       Print("Read Configuration Error");
    
    for(;;)
    {
        Print("Please Input Channel Number(0~3) = ");
    	LineInput(cTemp,10);
        sscanf(cTemp,"%d",&iChannel);
        
        Print("Please Input Value(float) = ");
    	LineInput(cTemp,10);
        sscanf(cTemp,"%f",&fValue);
        
        iRet=Write87K_87024AO(iSlot,iChannel,fValue);
        if(iRet==NoError)
        {
            Print("Output success!!\n\r");
        }
        else
        {
            Print("AO receive response error. Error Code = %d\n\r",iRet);        
    	}
    }
    RestoreCom(0);
}
//=====================================================================
//====== The following functions are used to read 87024 module.     ===
//====== You can copy the following functions to your own program.  ===
//=====================================================================
int Read87024_Config(int iSlot,int *iRangeCode_Original,int *iSlewRate_Original)
{
    /*
    DCON protocol to read 87024 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                       
     
     SlewRate:     For detailed specifications, refer to            
                    CD:8000\Napdos\DCON\IO_Module\hw_dcon_on_87kUnit 
                    CD:8000\Napdos\7000\Manual                    
    */
    
    
    unsigned char InBufCom0[20];
    int iRet;
    
    ChangeToSlot(iSlot);//Change to specified slot  
    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]== '!')  //Valid response first character: !
        {
            *iRangeCode_Original=((ascii_to_hex(InBufCom0[3])<<4)&0xF0)
                                    +(ascii_to_hex(InBufCom0[4])&0xF);
            *iSlewRate_Original=((ascii_to_hex(InBufCom0[7]))&0x3)
                                    +((ascii_to_hex(InBufCom0[8]))&0xC);
            return NoError;
        }   
        else
            return -1;//Outpur string error.
    }
    else
        return iRet;
}

int Set87024_Config(int iSlot,int iRangeCode,int iSlewRate)
{    
    /*
    DCON protocol to write the 87024 Configuration 
     Command: %AANNTTCCFF
     Response: !AA
    
     Slot: 0 ~ 7
     RangeCode:    For detailed specifications, refer to            
                    CD:8000\Napdos\DCON\IO_Module\hw_dcon_on_87kUnit 
                    CD:8000\Napdos\7000\Manual                       
     
     SlewRate:     For detailed specifications, refer to            
                    CD:8000\Napdos\DCON\IO_Module\hw_dcon_on_87kUnit 
                    CD:8000\Napdos\7000\Manual                    
    */
    
    unsigned char InBufCom0[20],OutBufCom0[20],Slew[10];
    int SlewRate_low,SlewRate_high,SlewRate;    
    int iRet,iHex;
    
    OutBufCom0[0]=0x25; //0x25 = %
    
    //FF =2Bit+Slew rate+Checksum
    SlewRate_low=(iSlewRate << 2)&0x0F;
    SlewRate_high=iSlewRate >> 2;
    sprintf(Slew,"%01X%01X",SlewRate_high,SlewRate_low);
    sscanf(Slew,"%02X",&iHex);
    SlewRate=iHex & 0x3C;
    
    ChangeToSlot(iSlot);//Change to specified slot  
    sprintf(OutBufCom0+1,"0001%02X0A%02X",iRangeCode,SlewRate);
    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]== '!')  //Valid response first character: !
        {
            return NoError;            
        }    
        else
            return -1;//Response string error. 
    }
    else
        return iRet;    
}

int Write87K_87024AO(int iSlot,int iChannel,float fValue)
{
    /*
    DCON protocol to write the 87024 value
     Command: #AAN(data)
     Response: >
    
     Slot: 0 ~ 7
     Channel: 0 ~ 1
    */ 
    
    int iRet;
    unsigned char OutBufCom0[20],InBufCom0[20];
    
    OutBufCom0[0]=0x23;//0x23 = #
    sprintf(OutBufCom0+1,"00%01d%+06.3f",iChannel,fValue);	
    ChangeToSlot(iSlot);//Change to specified slot
    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] =='>')  //Valid response first character: >
        {
            return NoError;
        }
        else
        	return -1;  //Response string error.
    }
    else 
        return iRet;
}
                                                                                       