#include "stdio.h"
#include "OB6627.h"
#include "uart.h"
#include "gpio.h"
#include "qei.h"

void Delay(uint32_t nNum)
{
     while(nNum--);
}

int main(void)
{
    uint32_t nLoop, nResult;

    SystemCoreClockUpdate();

    // Open UART
    OB_GPIO2->MODE = 0x00;
    OB_GPIO2->DATA_b.GPIOXDATA3 = 0x00;
    OB_GPIO2->DATA_b.GPIOXDATA8 = 0x00;

    GPIO_SetPinMFType(OB_GPIO2, GPIO_PIN4, GPIO_MF_QEI);
    GPIO_SetPinMFType(OB_GPIO2, GPIO_PIN7, GPIO_MF_QEI);
    UART_Open(OB_UART1, 19200);

    //--------------------------------


    // Enable QEI
    QEI_SetMaxPos(OB_QEI, 1000);
    QEI_Open(OB_QEI, QEI_CON_CAP_4X);
    QEI_EnableINT(OB_QEI, QEI_INT_POS0);
    OB_QEI->CMPOS0 = 20;
    //P2.3 connect P2.4
    //P2.7 connect P2.8
    for (nLoop = 0x00; nLoop < 50; nLoop++)
    {
        OB_GPIO2->DATA_b.GPIOXDATA3 = 0x00;
        OB_GPIO2->DATA_b.GPIOXDATA8 = 0x00;
        Delay(100);
        OB_GPIO2->DATA_b.GPIOXDATA3 = 0x01;
        OB_GPIO2->DATA_b.GPIOXDATA8 = 0x00;

        Delay(100);
        OB_GPIO2->DATA_b.GPIOXDATA3 = 0x01;
        OB_GPIO2->DATA_b.GPIOXDATA8 = 0x01;

        Delay(100);
        OB_GPIO2->DATA_b.GPIOXDATA3 = 0x00;
        OB_GPIO2->DATA_b.GPIOXDATA8 = 0x01;
        Delay(100);
    }
    nResult = QEI_GetPos(OB_QEI);
    printf(" POS Value = 0x%04X \n\r", nResult);
    while(1);
}
