示例#1
0
// function to initialize UART
void uart_init(uint32_t Desired_Baudrate) {
    // If its not already set, calculate the baud rate dynamically,
    // based on current microcontroller speed and user specified
    // desired baudrate.
#ifndef UBBR
#define UBBR ((F_CPU)/(Desired_Baudrate*8UL)-1)
#endif

    // Set to double speed mode.
    SS_UCSRnA |= (1 << SS_U2Xn);

    // Set baud rate.
    SS_UBRRnH = (uint8_t)(UBBR >> 8);
    SS_UBRRnL = (uint8_t)UBBR;
    // Enable receiver, transmitter, and RxComplete interrupt
    SS_UCSRnB = (1 << SS_RXENn) | (1 << SS_TXENn) | (1 << SS_RXCIEn);
    // Set frame format: 8data, 1 stop bit
    SS_UCSRnC = (1 << SS_USBSn) | (1 << SS_UCSZn0) | (1 << SS_UCSZn1);
    // Enable the Global Interrupt Enbl flag to process interrupts

    DIGITAL_SET_OUT(SS_UART_TXE);  // Set nRX enable and TX enable as outputs
    DIGITAL_SET_OUT(SS_UART_nRXE);
    DIGITAL_SET_LOW(SS_UART_TXE);  // Set nRX enable and TX enable to 0
    DIGITAL_SET_LOW(SS_UART_nRXE);  // Enable RX and disable TX

    DIGITAL_PULLUP_ON(SS_UART_RX);

    sei();  // Enable interrupts
}
示例#2
0
// Public functions called from main.c
void initDigital() {
  DIGITAL_PULLUP_ON(IN0);
  DIGITAL_SET_HIGH(IN1);
  DIGITAL_SET_LOW(IN2);
  DIGITAL_SET_LOW(IN3);

  // TODO(tobinsarah): allow configuration as input/output for each relevant pin
  DIGITAL_SET_IN(IN0);
  DIGITAL_SET_OUT(IN1);
  DIGITAL_SET_OUT(IN2);
  DIGITAL_SET_OUT(IN3);
}
示例#3
0
// Public functions called from main.c
void initBuzzer() {
    // Does everything to set up the analog stuffs.
    // Turn on pin PC1 (which maps to IN0)
    ADMUX |= (1 << MUX3) | (1 << MUX1);
#if F_CPU != 8000000
#error Clock speed not correct
#endif
    // Enable the ADC and set the division factor between
    // the system clock frequency and the input clock to the ADC.
    // Division factor: 111 = 128
    ADCSRA |= (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0) | (1 << ADEN);

    // Set PA6 pin as output in order to see PWM signal
    // Set pulse width to 1/2 of entire duration.
    OCR1BL = 0x7F;
    DIGITAL_SET_OUT(PWM0);
    // DIGITAL_SET is also ANALOG_SET apparently...
    DIGITAL_SET_IN(IN1);
    DIGITAL_SET_IN(IN2);
    DIGITAL_SET_IN(IN3);

    // Testing
    DIGITAL_SET_OUT(PWM1);
    DIGITAL_SET_HIGH(PWM1);
    // End Testing

    // Set PWM to Fast PWM Mode Operation, with non-inverting PWM
    // Clear OC1B on Compare Match
    // Clock prescaler = 1/64
    TCCR1A = (1 << COM1B1) | (1 << WGM10);
    TCCR1B = (1 << WGM12) | (1 << CS11) | (1 << CS10);

    // Enable Timer/Counter1 Overflow Interrupt
    TIMSK = (1 << TOIE1);

    sei();
}