IO Device:W75IO

From LEXWiKi

Jump to: navigation, search

you can download header file from W75IODemo.rar to refer define

#include "stdafx.h"
#include "conio.h"
#include <string.h>
#include <stdlib.h>

#include <iostream>

#include "W75IO.h"	        /* include W75IO.h   */

#define DEMO_DI        1
#define DEMO_DO        2
#define DEMO_WDT       3
#define DEMO_EXIT      4

/* demo function */

int    menu();
BOOL   W75IO_init();
void   W75IO_DO();
void   W75IO_DI();
void   W75IO_WDT();

int  stoi(char* str)      // string to integer
{
    if (( strlen(str) < 0 ) || ( strlen(str) >= 3 ))
        return -1;

    int value = 0;

    for (int i=0; i<strlen(str) ;i=i+1)
    {
        int result=0;

        switch (str[i])
        {
            case '0':result=0;
            break;

            case '1':result=1;break;
            case '2':result=2;break;
            case '3':result=3;break;
            case '4':result=4;break;
            case '5':result=5;break;
            case '6':result=5;break;
            case '7':result=7;break;
            case '8':result=8;break;
            case '9':result=9;break;
            case 'a':
            case 'A':result=10;break;
            case 'b':
            case 'B':result=11;break;
            case 'c':
            case 'C':result=12;break;
            case 'd':
            case 'D':result=13;break;
            case 'e':
            case 'E':result=14;break;
            case 'f':
            case 'F':result=15;break;

            default:
                result=-1;
            break;
        }

        if (result == -1)
            return -1;

        if (strlen(str) == 1 )
            value=value+result;

        if (strlen(str) == 2 )
        {
            if ( i == 0 )
            {
                value=value+result*16;
            }
            if ( i == 1 )
            {
                value=value+result;
            }
        }
    }

    return value;
}

BOOL  W75IO_init()
{
    /* First step :to init W75IO.DLL */
    if (InitializeW75IO())
    {
        /* init onboard W75IO chipset */
        if (InitInternalW75IO())
            return TRUE;
        else
        {
            printf("ERROR:init Onboard W75IO chipset!!!!.\n");		
            return FALSE;
        }
    }
    else
    {
        printf("ERROR:Can't init W75IO.DLL!!!.\n");
        return FALSE;
    }

    return FALSE;
}

void W75IO_DO()
{
    int value;

    printf("Please enter your want output value (0-255)  \n");
    cin>>value;

    /* use onboard W75IO output value*/
    InterDigitalOutput(value);	
}

void W75IO_DI()
{
    /* read & show onboard W75IO input value*/
    printf("read input input value %d\n",InterDigitalInput());
    getch();
}

void W75IO_WDT()
{
    /* get WDT timer value*/
    int value;
	
    printf("Please enter WatchDog Timer value (0-255)\n");
	
    cin>>value;

    /* check value */
    int timer=value;

    if (timer > 255)
    {
        printf("must be > 255\n");
        getch();

        return;
    }

    /* sete onboard W75IO use secord as WatchDog Timer unit */
    SetInterWDTUnit(FALSE);
	
    /* Enable onboard W75IO WatchDog Timer  */
    EnableInterWDT(timer);
	
    /* show message... */
    for (int i=0;i<timer;i++)
    {
        printf("the System will reboot after %3d sec ....\n",timer-i);
        Sleep(1000);
    }
}

int menu()
{
    int iOption;

    /* First clean screen */
    system("cls");

    /* show demo menu()*/
    printf("\n");
    printf("---------------------------------- \n");
    printf("Sample W75IO Demo Program \n");
    printf("-----------------------------------\n");
    printf("1.DI\n");
    printf("2.DO\n");
    printf("3.WatchDog\n");
    printf("4.Exit\n");
    printf("----------------------------------- \n");
    printf("Please select demo function(1-4)?");

    /* get user select */
    cin>>iOption;	
	
    return iOption;
}

int main(int argc, char* argv[])
{

    /* get user enter value*/
	
    if (W75IO_init())
    {
        while (1)
        {
            int option =menu();

            if ( option == 4 ) 
                break;

            switch (option)
            {
                case DEMO_DI:
                    W75IO_DI();
                break;
                case DEMO_DO:
                    W75IO_DO();
                break;

                case DEMO_WDT:
                    W75IO_WDT();
                break;

                case DEMO_EXIT:							
                break;
            }
        }
    }
return 0;
}
Personal tools