Ejemplo n.º 1
0
// Fonction d'initialisation
void init(void)
{
	unsigned char i;
	for(i=0;i<=5;i++) Data_Received[i] = 0x00;

	cli();							// Désactiver toutes les interruptions

	DDRD = 0b10000110;				// config pour BP1 2 3 et 4 (BP = bouton poussoir)
	sbiBF (PORTD, PORTD3);			// BP0
	sbiBF (PORTD, PORTD4);			// BP1
	sbiBF (PORTD, PORTD5);			// BP2
	sbiBF (PORTD, PORTD6);			// BP3
	sDDR(DDRD,1);					// mettre port TX en sortie
	sbiBF(PORTD,0); 				// mettre pull-up sur RX
	sbiBF(PORTC,0); 				// mettre pull-up sur RX
	sDDR(DDRC,1); 					// mettre port en sortie
	DDRB = 0b00111111;  			// PORTB.6et7 en entrée (quartz)
	sbiBF (PORTB, PINB0);				// communication usart à 600baud non modulé

	USART0_Init_9600();				// initialise le port série 
	Init_Buffers_USART ();			// initialise les buffers de la communication série

	init_pcint();					// initialise les interruptions externes

	TIMER0_INIT();					// initialise le timer 0
	TIMER1_INIT ();					// initialiser le timer 1
	TIMER2_INIT ();					// initialiser le timer 2

	sei();
}
Ejemplo n.º 2
0
int main()
{
    int ANGLE;
    ADCstart(); // initialize the ADC port
    DDRB = (1<< PORTB1); // Set PORTB1 pin as output

    ICR1 = 40000;// 50Hz PWN at 16Mhz, TOP = ICR1
    OCR1A = 1000;// 1000 = 0 degrees, 4600 = 180 degrees
    TIMER1_INIT();
    //Brown = Ground
    // Red = Power
    // Orange = Signal
    while(1)
    {
        ADCvalue = 0;

        ADCSRA |= (1<<ADSC);// write one to ADSC bit to start conversion
        while(ADCSRA & (1<<ADSC))// wait until conversion is complete
        {
        }
        ADCvalue = ADC;// save voltage
        ADCvalue = (ADCvalue*5.0)/1024;// Calculate Vin
        ADCvalue = ((ADCvalue/5.0)*3600); // Calculate how far the angle is above 0 degrees
        ANGLE = ADCvalue + 1000; // Add value for 0 degree
        OCR1A = ANGLE; // Save value for PWN duty cycle

    }
    return 0;
}
Ejemplo n.º 3
0
int main(void){

    /*
     * Initializations
     */
    cli();

    // Make sure interrupts belong to us
    MCUCR = (1<<IVCE);
    MCUCR = 0;

    // Initialize global variables
    g_ext_ref_present = false;
    g_gps_present = false;
    g_switch_pos = PREFER_INTERNAL;
    g_ref = NO_REF;

    // Atmega128
    setup_atmel_io_ports();

    // Reset ClkDist chip
    reset_TI_CDCE18005();

    // GPSDO communication
    usart_init();

    // Ethernet stack
    network_init();
    register_udp_listener(OCTOCLOCK_UDP_CTRL_PORT,  handle_udp_ctrl_packet);
    register_udp_listener(OCTOCLOCK_UDP_GPSDO_PORT, handle_udp_gpsdo_packet);

    // Timers
    TIMER1_INIT(); // Network
    TIMER3_INIT(); // External reference check

    // Debug (does nothing when not in debug mode)
    DEBUG_INIT();
    DEBUG_LOG(" "); // Force a newline between runs

    leds_off();

    sei();

    // Check if GPS present (should only need to happen once)
    g_gps_present = (PIND & (1<<DDD4));

    // State of previous iteration
    prev_ref           = NO_REF;
    prev_switch_pos    = PREFER_EXTERNAL;
    cli();
    prev_ICR3          = ICR3;
    sei();
    prev_num_overflows = 0;

    /*
     * Main loop
     */
    while(true){
        // Check switch position
        g_switch_pos = (PINC & (1<<DDC1)) ? PREFER_EXTERNAL : PREFER_INTERNAL;

        /*
         * Check input capture pin for external reference detection.
         *
         * 16-bit reads could be corrupted during an interrupt, so
         * disable interrupts for safety.
         */
        cli();
        current_ICR3          = ICR3;
        current_num_overflows = num_overflows;
        sei();

        // Signal detected, reset timer
        if(current_ICR3 != prev_ICR3){
            cli();
            TCNT3 = 0;
            num_overflows = 0;
            sei();
            g_ext_ref_present = true;
        }

        // Timeout, no external reference detected
        if(current_num_overflows >= EXT_REF_TIMEOUT){
            g_ext_ref_present = false;
        }

        // Determine and set reference based on state
        if(!g_gps_present && !g_ext_ref_present)     g_ref = NO_REF;
        else if(g_gps_present && !g_ext_ref_present) g_ref = INTERNAL;
        else if(!g_gps_present && g_ext_ref_present) g_ref = EXTERNAL;
        else g_ref = (g_switch_pos == PREFER_INTERNAL) ? INTERNAL : EXTERNAL;

        if((g_ref != prev_ref) || (g_switch_pos != prev_switch_pos)){
            if(g_switch_pos == PREFER_INTERNAL) prefer_internal();
            else prefer_external();
        }

        // Record this iteration's state
        prev_ref           = g_ref;
        prev_switch_pos    = g_switch_pos;
        prev_ICR3          = current_ICR3;
        prev_num_overflows = current_num_overflows;

        // Handle incoming Ethernet packets
        network_check();
    }
}