int main(void) {
	uint16_t values[2] = {0, 0};
	
	/* Init system clock for maximum system speed */
	TM_RCC_InitSystem();
	
	/* Init HAL layer */
	HAL_Init();
	
	/* Init leds */
	TM_DISCO_LedInit();
	
	/* Init button */
	TM_DISCO_ButtonInit();
	
	/* Init DAC channel 1 = PA4 */
	TM_DAC_Init(TM_DAC_Channel_1);
	
	/* Init DAC channel 2 = PA5 */
	TM_DAC_Init(TM_DAC_Channel_2);
	
	while (1) {
		/* Toggle ALL leds */
		if (TM_DELAY_Time() > 200) {	
			/* Toggle leds */
			TM_DISCO_LedToggle(LED_ALL);
		}
		
		/* Increase channel 1 value */
		values[0]++;
		
		/* Decrease channel 2 value */
		values[1]--;
		
		/* Check if channel 1 is overflowed 12 bit and set it to zero */
		if (values[0] > 0x0FFF) {
			values[0] = 0;
		}
		
		/* Check if channel 2 is less than zero (overflow to 0xFFFF) and set to to max 12 bit value */
		if (values[1] > 0x0FFF) {
			values[1] = 0x0FFF;
		}
		
		/* Set DAC channel 1 = PA4 */
		TM_DAC_SetValue(TM_DAC_Channel_1, values[0]);
		
		/* Set DAC channel 2 = PA5 */
		TM_DAC_SetValue(TM_DAC_Channel_2, values[1]);
		
		/* Delay 1ms */
		Delayms(1);
	}
}
/**
**===========================================================================
**
**  Abstract: main program
**
**===========================================================================
*/
int main(void)
{
    uint8_t buffer[5] = {0,0,0,0,0};//buffer for received data
    uint8_t buffer_size;//size of buffer
    uint8_t *buffer_ptr = &buffer[0];
    uint8_t buffer_ptr_inc = 0;//buffer pointer incrementer
    uint16_t read_voltage;//voltage value read from MATlab*1000
    uint16_t DAC_input;//input voltage to DAC function

    /*Start up system parameters*/
    SystemInit();
    /**
    *  IMPORTANT NOTE!
    *  The symbol VECT_TAB_SRAM needs to be defined when building the project
    *  if code has been located to RAM and interrupts are used.
    *  Otherwise the interrupt table located in flash will be used.
    *  See also the <system_*.c> file and how the SystemInit() function updates
    *  SCB->VTOR register.
    *  E.g.  SCB->VTOR = 0x20000000;
    */



    /* Initialize DAC channel 1, pin PA4 */
    TM_DAC_Init(TM_DAC1);

    main_LED_init();

    /*Start USB VCP*/
    TM_USB_VCP_Init();

    /* Infinite loop */
    while (1) {
        /* USB configured OK, drivers OK */
        if (TM_USB_VCP_GetStatus() == TM_USB_VCP_CONNECTED) {
            /* Turn on GREEN led */
            STM_EVAL_LEDOn(LED3);

            /* If something arrived at VCP */
            if (TM_USB_VCP_Getc(buffer_ptr+buffer_ptr_inc*sizeof(uint8_t)) == TM_USB_VCP_DATA_OK) {
                /* data received over port */
                STM_EVAL_LEDOn(LED4);
                /*Check to make sure no bad indexing*/
                if(buffer_ptr_inc == 0)
                {
                    buffer_size = ascii_to_decimal(buffer_ptr, 1);//
                    buffer_ptr_inc++;
                }
                else if(buffer_ptr_inc < (buffer_size))
                {
                    buffer_ptr_inc++;
                }
                else
                {
                    buffer_ptr_inc=0;
                    read_voltage = ascii_to_decimal(buffer_ptr+sizeof(uint8_t), buffer_size);/*end of capturing decimal data, convert string to decimal*/
                    DAC_input = (uint16_t)read_voltage*(4.096/2.633);
                    /* Set 12bit analog value of 1500/4096 * 2.633V */
                    TM_DAC_SetValue(TM_DAC1, DAC_input);
                    TM_USB_VCP_Putc(1);//send data okay to MATlab
                }
            }

        } else {
            /* USB not OK */
            STM_EVAL_LEDOff(LED3);
            STM_EVAL_LEDOff(LED4);
        }
    }//end of while
    return 0;
}