示例#1
0
/* The initialize function configures the PLL to set the internal clock
 * frequency. It also configures the digital IO and calls the initialization
 * functions for each of the modules. A light sequence signals the end of
 * initialization.
 */
void initialize(void){
    /* Configure Phase Lock Loop for  the system clock reference at 40MHz */
    // Fosc (Clock frequency) is set at 80MHz
    // Fin is 7.37 MHz from internal FRC oscillator
    // FPLLI = Fin/N1 = 3.685 MHz
    CLKDIVbits.PLLPRE = 0;   // N1 = 2
    // FVCO = FPLLI*M1 = 162.14MHz
    PLLFBDbits.PLLDIV = 42;  // M = 44
    // FPLLO = FVCO/N2 = 81.07 MHz
    // FOSC ~= 80MHz, FCY ~= 40MHz
    CLKDIVbits.PLLPOST = 0;  // N2 = 2

    /* Initiate Clock Switch */
    //The __builtin macro handles unlocking the OSCCON register
    __builtin_write_OSCCONH(1); //New oscillator is FRC with PLL
    __builtin_write_OSCCONL(OSCCON | 0x01); //Enable clock switch

    while (OSCCONbits.COSC!= 1); //Wait for FRC with PLL to be clock source
    while (OSCCONbits.LOCK!= 1); //Wait for PLL to lock

    /* Configure IO*/
    TRISDbits.TRISD10 = 1;   //USER input
    //LED outputs
    ANSELBbits.ANSB13 = 0;  //Disable Analog on B13
    TRISBbits.TRISB13 = 0;  //LED1
    ANSELBbits.ANSB12 = 0;  //Disable Analog on B12
    TRISBbits.TRISB12 = 0;  //LED2
    TRISDbits.TRISD11 = 0;  //LED3
    TRISDbits.TRISD0 = 0;   //LED4
    //Magnet Control
    TRISBbits.TRISB14 = 0;   //Top Magnet

    //Store bits indicating reason for reset
    resetStat = RCON;
    //Clear reset buffer so next reset reading is correct
    RCON = 0;

    /* Initialize peripherals*/
    initialize_PWM();
    initialize_CN();
    initialize_ADC();
    initialize_QEI();
    initialize_UART();
    initialize_UART2();
    //initialize_I2C_Master();
    lights();
    __delay32(10000000);
    //initialize_MPU();
    initialize_encoder_values(1600,1700,1800);
}
示例#2
0
文件: main.c 项目: Lone-L/MicroP_Labs
int main(void)
{
	uint32_t v_sense;
	float tempinvolts, temperature;
	int counter = 0;
	HAL_StatusTypeDef rc;
	KalmanState kstate = {0.01, 0.3, 0.0, 0.1, 0.0};
		
  /* MCU Configuration----------------------------------------------------------*/
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();
	
  /* Configure the system clock */
  SystemClock_Config();
	
	/* Initialize the ADC and GPIO and LCD display */
	initialize_ADC();
	initialize_GPIO();
	initialize_LCD();
	
	/* then call start */
	if (HAL_ADC_Start(&ADC1_handle) != HAL_OK)
		Error_Handler(ADC_INIT_FAIL);
	
	while (1) {
		if (SYSTICK_READ_TEMP_FLAG == 1) {
			if ((rc = HAL_ADC_PollForConversion(&ADC1_handle, 10000)) != HAL_OK)
				printf("Error: %d\n", rc);
			
			v_sense = HAL_ADC_GetValue(&ADC1_handle);	/* Read register from the ADC */
			tempinvolts = v_sense * (V_REF / 4096);	/* Scale the reading into V (resolution is 12bits with VREF+ = 3.3V) */
			temperature = (tempinvolts - V_25) / AVG_SLOPE + TEMP_REF + FUDGE_FACTOR;	/* Formula for temperature from doc_05 p.230 */
			
			/* Grind through the Kalman filter */
			if (Kalmanfilter_asm(&temperature, &filtered_temp, 1, &kstate) != 0)
				printf("Overflow error\n");
			
			unfiltered_temp = temperature;
//			printf("%d %f\n", counter, filtered_temp);
			
			/* Display only once out of this many times the 7-segment display */
			if (counter % SEGMENT_DISPLAY_PERIOD == 0) {
				displayed_segment_value = filtered_temp;
				display_LCD_num(filtered_temp);
			}
			
			if (ALARM_TRIGGERED_FLAG == 1) {
				/* Avoid spurious noise by waiting for a lower threshold before turning off alarm */
				if (filtered_temp < LOWER_THRESHOLD_TEMP) {
					ALARM_TRIGGERED_FLAG = 0;
					turn_off_LEDs();
				}
			} else {
				if (filtered_temp > THRESHOLD_TEMP)
					ALARM_TRIGGERED_FLAG = 1;
			}

			if (ALARM_TRIGGERED_FLAG == 1) {
				if (counter % ALARM_LED_TOGGLE_PERIOD == 0)
					toggle_LEDs();
			}
			
			printf("%f, %f\n", unfiltered_temp, filtered_temp);
			++counter;
			SYSTICK_READ_TEMP_FLAG = 0;	/* Reset the flag */
		}
		
		/* Systick_Handler triggers the flag once every 50ms */
		if (SYSTICK_DISPLAY_SEGMENT_FLAG == 1) {
			display_segment_val();
			SYSTICK_DISPLAY_SEGMENT_FLAG = 0;
		}
	}
}