Example #1
0
void main(void)
{
    init_pins();
    init_oscillator();
    SPI1_Initialize();
    
    float temp = 0;
    // Set all pins RD1-7 to inputs, RD0 (MSB is set to output)
    //TRISD = 0b11111110;
    // RC2 is PWM pin
    TRISCbits.RC2 = 0;
    
    configure_adc();
    configure_pwm();
    int ticks = 0;
    
    while (1)
    {  
        MAX_7221_INIT();
        temp = get_temperature(0);
        set_fan_speed(temp);
        if(ticks % 125 == 0) {
            MAX_7221_WRITE_FLOAT(temp, 3);
            ticks = 0;
        }
        ticks++;
    }
}
Example #2
0
int main(void)
{
	system_clock_config(CLOCK_RESOURCE_XO_26_MHZ, CLOCK_FREQ_26_MHZ);

	//! [setup_init]
	configure_pwm();
	//! [setup_init]

	//! [main]
	while (true) {
		/* Infinite loop */
	}
	//! [main]
}
Example #3
0
int main(void)
{
		/*Status flags to indicate the re-burst for library */
	uint16_t status_flag = 0u;
	uint16_t burst_flag = 0u;

	uint8_t lft_pressed = 0;
	uint8_t rgt_pressed = 0;

	static uint8_t old_position = 0;

	uint8_t uc_char;
	uint8_t uc_flag;
	sysclk_init();
	board_init();
	configure_buttons();
	configure_hall();

	wdt_disable(WDT);
	pmc_enable_periph_clk(ID_PIOC);
	qt_reset_sensing();
	config_sensors();
	qt_init_sensing();
	/* Set the parameters like recalibration threshold, Max_On_Duration etc in this function by the user */
	qt_set_parameters();
	init_timer_isr();
	qt_filter_callback = 0;

	configure_console();
	printf(STRING_HEADER);
	
	configure_lcd();
	g_pwm_channel = configure_pwm();

	/* Cabeçalho do lcd */
	pos_lcd_x = 20;
	pos_lcd_y = 40;
	start_lcd(pos_lcd_x, pos_lcd_y, ul_duty, hall_1, hall_2, hall_3, phase);

	/* Infinite loop */
	while (1) {
		static uint8_t phase_aux;
		static uint32_t hall_1_aux, hall_2_aux, hall_3_aux, ul_duty_aux;

		/* Atualiza o display somente quando houver alteração nas variáveis que serão apresentadas */
		
		if(ul_duty_aux != ul_duty)
		{
			escreve_int_lcd("dc = ", ul_duty*100/PERIOD_VALUE, pos_lcd_x, 40);
			ul_duty_aux = ul_duty;
		}
		
		if(phase_aux != phase || hall_1_aux != hall_1 || hall_2_aux != hall_2 || hall_3_aux != hall_3)
		{
			escreve_int_lcd("hall1 = ", hall_1, pos_lcd_x, 60);
			escreve_int_lcd("hall2 = ", hall_2, pos_lcd_x, 80);
			escreve_int_lcd("hall3 = ", hall_3, pos_lcd_x, 100);
			escreve_int_lcd("phase = ", phase, pos_lcd_x, 120);

			phase_aux = phase;
			hall_1_aux = hall_1;
			hall_2_aux = hall_2;
			hall_3_aux = hall_3;
		}
		
		if(motor_run == 0 && ul_duty != 0)
			Hall_Phase();
		
		uc_char = 0;
		uc_flag = uart_read(CONSOLE_UART, &uc_char);
		if (!uc_flag) {
			if (uc_char == 't') {
				printf("   duty cicle = %lu \r\n",ul_duty*100/PERIOD_VALUE);
				printf("   hall1 = %lu \r\n", hall_1);
				printf("   hall2 = %lu \r\n", hall_2);
				printf("   hall3 = %lu \r\n", hall_3);
				printf("   phase = %u \r\n\n", phase);
			}
			if (uc_char == 'a'){				
				if(ul_duty < PERIOD_VALUE) ul_duty++;
				printf("   duty cicle = %lu \r\n",ul_duty*100/PERIOD_VALUE);
			}
			if (uc_char == 's'){
				if(ul_duty > INIT_DUTY_VALUE) ul_duty--;
				printf("   duty cicle = %lu \r\n",ul_duty*100/PERIOD_VALUE);
			}
			if (uc_char == 'd')
			{
				ensaio = 1;
				printf("   Ensaio de rampa\r\n");
				printf("   para parar pressione a letra 'P'\r\n");	
			}
			if (uc_char == 'f')
			{
				ensaio = 2;
				printf("   Ensaio de degrau\r\n");
				printf("   para parar pressione a letra 'P'\r\n");
			}
			if (uc_char == 'p')
			{
				ensaio = 0;
				ul_duty = 0;
			}
			if (uc_char == 'i')
			{
				sel_rot = !sel_rot;
				printf("   Rotacao invertida\r\n");
				printf("   para parar pressione a letra 'P'\r\n");
			}
		}
		
		if (time_to_measure_touch) {

			/* Clear flag: it's time to measure touch */
			time_to_measure_touch = 0u;

			do {
				/*  One time measure touch sensors    */
				status_flag = qt_measure_sensors(current_time_ms_touch);

				burst_flag = status_flag & QTLIB_BURST_AGAIN;

				/*Time critical host application code goes here */

			} while (burst_flag);
		}

		/*  Time Non-critical host application code goes here */


		if ((GET_SENSOR_STATE(BOARD_LEFT_KEY_ID) != 0)
		&& (lft_pressed == 0)) {
			lft_pressed = 1;
			if(ul_duty > INIT_DUTY_VALUE) ul_duty--;
			printf("  duty cicle = %lu \r\n",ul_duty*100/PERIOD_VALUE);
			} else {
			if ((GET_SENSOR_STATE(BOARD_LEFT_KEY_ID) == 0)
			&& (lft_pressed == 1)) {
				lft_pressed = 0;
			}
		}
		if ((GET_SENSOR_STATE(BOARD_RIGHT_KEY_ID) != 0)
		&& (rgt_pressed == 0)) {
			rgt_pressed = 1;
			if(ul_duty < PERIOD_VALUE) ul_duty++;
			printf("  duty cicle = %lu \r\n",ul_duty*100/PERIOD_VALUE);
			} else {
			if ((GET_SENSOR_STATE(BOARD_RIGHT_KEY_ID) == 0)
			&& (rgt_pressed == 1)) {
				rgt_pressed = 0;
			}
		}


		if (GET_ROTOR_SLIDER_POSITION(0) != old_position) {
			old_position = GET_ROTOR_SLIDER_POSITION(0);
			if (motor_run==0) flag_hab_m = 1;
			ul_duty = old_position*PERIOD_VALUE/255;
		}
	}
}
Example #4
0
int main(void)
{
	int i, spi_timeout;
	unsigned long counter;

	/* Disable JTAG port so we get our I/O pins back */
	DDPCONbits.JTAGEN = 0;
	/* Enable optimal performance */
	SYSTEMConfigPerformance(GetSystemClock());
	/* Use 1:1 CPU Core:Peripheral clocks */
	OSCSetPBDIV(OSC_PB_DIV_1);

	/* configure the core timer roll-over rate */
	OpenCoreTimer(CORE_TICK_RATE);

	/* set up the core timer interrupt */
	mConfigIntCoreTimer((CT_INT_ON | CT_INT_PRIOR_6 | CT_INT_SUB_PRIOR_0));

	/* enable multi vector interrupts */
	INTConfigureSystem(INT_SYSTEM_CONFIG_MULT_VECTOR);
	INTEnableInterrupts();

	map_peripherals();
	init_io_ports();
	configure_pwm();
	init_spi();
	init_dma();

	/* wait until tx buffer is filled up */
	while (!SPI2STATbits.SPITBF);

	reset_board();
	spi_data_ready = 0;
	spi_timeout = 0;
	counter = 0;

	/* enable watchdog */
	WDTCONSET = 0x8000;

	/* main loop */
	while (1) {
		if (spi_data_ready) {
			spi_data_ready = 0;

			/* the first element received is a command string */
			switch (rxBuf[0]) {
			case 0x5453523E:	/* >RST */
				reset_board();
				break;
			case 0x314D433E:	/* >CM1 */
				stepgen_update_input((const void *)&rxBuf[1]);
				stepgen_get_position((void *)&txBuf[1]);
				break;
			case 0x324D433E:	/* >CM2 */
				update_outputs(rxBuf[1]);
				update_pwm_duty((uint32_t *)&rxBuf[2]);
				txBuf[1] = read_inputs();
				break;
			case 0x4746433E:	/* >CFG */
				stepgen_update_stepwidth(rxBuf[1]);
				update_pwm_period(rxBuf[2]);
				stepgen_reset();
				break;
			case 0x5453543E:	/* >TST */
				for (i=0; i<BUFSIZE; i++)
					txBuf[i] = rxBuf[i] ^ ~0;
				break;
			}
		}

		/* if rx buffer is half-full, update the integrity check.
		   There isn't enough time if we wait for complete transfer */
		if (DCH0INTbits.CHDHIF) {
			DCH0INTCLR = 1<<4;		/* clear flag */
			txBuf[0] = rxBuf[0] ^ ~0;
		}

		/* if rx buffer is full, data from spi bus is ready */
		if (DCH0INTbits.CHBCIF) {
			DCH0INTCLR = 1<<3;		/* clear flag */
			spi_data_ready = 1;
			spi_timeout = SPI_TIMEOUT;
		}

		/* reset the board if there is no SPI activity */
		if (spi_timeout)
			spi_timeout--;

		if (spi_timeout == 1) {				
			DCH0ECONSET=BIT_6;	/* abort DMA transfers */
			DCH1ECONSET=BIT_6;
		
			init_spi();
			init_dma();
			reset_board();

			/* wait until tx buffer is filled up */
			while (!SPI2STATbits.SPITBF);
		}

		/* blink onboard led */
		if (!(counter++ % (spi_timeout ? 0x10000 : 0x40000))) {
			LED_TOGGLE;
		}

		/* keep alive */
		WDTCONSET = 0x01;
	}
	return 0;
}