/*
87K_DO.c :This demo program is for 87K Digital 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: 87K_DO.c                                                               
         ..\Lib\(8000.Lib, 8000E.lib)   
                                                                               
Hardware: 8000

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

int Write87K_DO(int Slot,int iTotalChannel,unsigned long Value);
int Read87K_DO(int Slot,int iTotalChannel,unsigned long *Value);

void main()
{
    int iRet,iSlot,iTotalChannel,data;
    unsigned long DOdata;
    unsigned long int RB_value;
    unsigned char cTemp[10];
    
    InitLib();
    
    Print("/*******************/\n\r");
    Print("/*    87K_DO demo  */\n\r");
    Print("/*                 */\n\r");
    Print("/*  [8 Nov,2004]   */\n\r");
    Print("/*******************/\n\r");
    Print("\n");                       

    InstallCom_0(115200L,8,0,0);//Open com0(baudrate,data,parity,stop)
              
    Print("Please Input Slot Number(0~3 or 0~7) =");
    LineInput(cTemp,10);
    sscanf(cTemp,"%d",&iSlot);
    	
    Print("Please Input Total DO Channel Number(8,16,32) = ");
    LineInput(cTemp,10);
    sscanf(cTemp,"%d",&iTotalChannel);
    
    for(;;)
    {
        Print("Please Input DO Data(Hex) = ");
    	LineInput(cTemp,10);
        sscanf(cTemp,"%08lX",&DOdata);
	
        iRet=Write87K_DO(iSlot,iTotalChannel,DOdata);
            
        if(iRet==NoError)
        {
            iRet=Read87K_DO(iSlot,iTotalChannel,&RB_value);
            if(iRet==NoError)
            {
            	Print("DO output success.The output value is %-08lX \n\r",RB_value);
            }
            else
            {
                Print("Reading output value is error\n\r ");
            }
        }
        else 
        {
            Print("DO receive response error. Error Code = %d\n\r",iRet);
        }
    }
    RestoreCom(0);
}

//=====================================================================
//====== Following functions are used to write 87K DO modules      ===
//====== You can copy the functions to your own program.            ===
//=====================================================================
int Write87K_DO(int Slot,int iTotalChannel,unsigned long DOdata)
{
    //DCON protocol to write DO value
    // Command: @AA(Data)
    // Response: >
        
    int iRet;
    unsigned char OutBufCom0[20],InBufCom0[20];
 
    switch(iTotalChannel)
    {
        case 8://8 bit
        	sprintf(OutBufCom0,"@00%02X",DOdata&0xFF);	
        	//Transfer command string
        	break;
        
        case 16://16 bit
        	sprintf(OutBufCom0,"@00%04X",DOdata&0xFFFF);	
        	//Transfer command string
        	break;
    	case 32:
    		sprintf(OutBufCom0,"@00%08lX",DOdata&0xFFFFFFFF);	
        	//Transfer command string
        	break;
    }
    
    ChangeToSlot(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] =='>')
        {
            return NoError;
        }
        else
        	return -1;  //Response string error.
    }
    else 
        return iRet;
}

int Read87K_DO(int Slot,int iTotalChannel,unsigned long *Value)
{
   /*
    DCON protocol to read DI value
     Command: @AA
     Response: >(DO Data)(DI Data)
     
     Slot: 0 ~ 8
     iTotalChannel: 0 ~ 32
    */
    
    int iRet;
    unsigned char InBufCom0[20];
    
    Delay(10);  //Because writting value to the module need a little time, 
                //"delay 10 ms" in order to avoid to catch previous DO value. 
    
    ChangeToSlot(Slot);		
    SendCmdTo7000(0,"@00",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] == '>')
        {
            switch(iTotalChannel)                                                                                                               
            {                                                                                                                          
                case 8: // 8 bit  
                    *Value=((ascii_to_hex(InBufCom0[1])<<4)&0xF0)
            	           +(ascii_to_hex(InBufCom0[2])&0xF);                     
            	    return NoError;
            	    
            	case 16://16 bit                                                                                                       
                    *Value=(((unsigned)(ascii_to_hex(InBufCom0[1]))<<12)&0xF000)
                           +((ascii_to_hex(InBufCom0[2])<<8)&0xF00)
                       +((ascii_to_hex(InBufCom0[3])<<4)&0xF0)
                       +(ascii_to_hex(InBufCom0[4])&0xF);                     
                    return NoError;
                case 32:
                    *Value=(((unsigned long)(ascii_to_hex(InBufCom0[1]))<<28)&0xF0000000)
            		       +(((unsigned long)(ascii_to_hex(InBufCom0[2]))<<24)&0xF000000)
                           +(((unsigned long)(ascii_to_hex(InBufCom0[3]))<<20)&0xF00000)
                           +(((unsigned long)(ascii_to_hex(InBufCom0[4]))<<16)&0xF0000)
                           +((unsigned long)(ascii_to_hex(InBufCom0[5])<<12)&0xF000)
                           +((unsigned long)(ascii_to_hex(InBufCom0[6])<<8)&0xF00)
                           +((unsigned long)(ascii_to_hex(InBufCom0[7])<<4)&0xF0)
                           +((unsigned long)(ascii_to_hex(InBufCom0[8]))&0xF);                    
		    return NoError;    
                default:
                    return NoError;
            }
        }
        else    
        {
           return -1;  //Response string error.
        } 
    }      
    else   
        return iRet;
}   
