Пример #1
0
int main(void)
{
    uint8_t escaped = 0;
    leds_init();

    DDR_CONFIG_OUT(RS485_TX_PIN);
    DDR_CONFIG_OUT(RS485_NRX_PIN);

    PIN_CLEAR(RS485_TX_PIN);
    PIN_CLEAR(RS485_NRX_PIN);

    UBRR0 = UART_BAUD_SELECT(RS485_BITRATE,F_CPU);
    UCSR0B |= (1<<TXEN0) | (1<<RXEN0);

    UBRR1 = UART_BAUD_SELECT(RS485_BITRATE,F_CPU);
    UCSR1B |= (1<<TXEN1) | (1<<RXEN1);
    
    wdt_reset();
    PIN_SET(RS485_TX_PIN);
    PIN_SET(RS485_NRX_PIN);

    while(1){
        wdt_reset();
        if( (UCSR1A & (1<<RXC1)) ){
            uint8_t data = UDR1;
            if( escaped ){
                escaped = 0;
                if( data == 'R' ){
                    wdt_enable(WDTO_2S);
                    while(1);
                }
            }else{
               if( data == '\\' ){
                    escaped = 1;
                    continue;
                }
            }
            UDR0 = data;
            PIN_SET(RS485_TX_PIN);
            PIN_SET(RS485_NRX_PIN);
            //_delay_us(4);
            leds_tx();
            while( !(UCSR0A & (1<<TXC0)) );
            UCSR0A |= (1<<TXC0);
            leds_txend();
            //_delay_us(4);
            PIN_CLEAR(RS485_TX_PIN);
            PIN_CLEAR(RS485_NRX_PIN);
        }
        if( (UCSR0A & (1<<RXC0)) ){
            UDR1 = UDR0;
            leds_rx();
            while( !(UCSR1A & (1<<TXC1)) );
            UCSR1A |= (1<<TXC1);
            leds_rxend();
        }

    }
}
Пример #2
0
void leds_init(void)
{
    DDR_CONFIG_OUT(LED1); 
    DDR_CONFIG_OUT(LED2); 
    DDR_CONFIG_OUT(LED3); 
    PIN_CLEAR(LED1);
    PIN_CLEAR(LED2);
    PIN_CLEAR(LED3);
}
Пример #3
0
void
ps2_send_byte(uint8_t byte)
{
  uint8_t sreg = SREG; cli();
  uint8_t i = 11;
  DDR_CONFIG_OUT(PS2_DATA);
  DDR_CONFIG_OUT(PS2_CLOCK);
  /* > 100 us clock low */
  PIN_CLEAR(PS2_CLOCK);
  while (i--)
    _delay_us(10);

  /* data low */
  PIN_CLEAR(PS2_DATA);

  /* clock high */
  PIN_SET(PS2_CLOCK);

  /* Wait until clock is low again */
  DDR_CONFIG_IN(PS2_CLOCK);

  while (PIN_HIGH(PS2_CLOCK));
  uint8_t parity = 1;
  bitcount = 0;
  while(bitcount < 9) {
    /* Parity */
    if (bitcount == 8) {
      PIN_CLEAR(PS2_DATA);
      if (parity)
        PIN_SET(PS2_DATA);
    }
    /* data */
    else {
      PIN_CLEAR(PS2_DATA);
      if (byte & 0x01) {
        PIN_SET(PS2_DATA);
        parity ^= 1;
      }
      byte >>= 1;
    }
    /* The keyboard generates the clock */
    while (!PIN_HIGH(PS2_CLOCK));
    while (PIN_HIGH(PS2_CLOCK));
    bitcount++;
  }
  DDR_CONFIG_IN(PS2_DATA);
  PIN_CLEAR(PS2_DATA);
  PIN_CLEAR(PS2_CLOCK);

  /* Wait for the ack from the keyboard */
  while (PIN_HIGH(PS2_DATA));
  while (PIN_HIGH(PS2_CLOCK));

  SREG = sreg;
}
Пример #4
0
void
zbus_core_init(void)
{
    /* Initialize the usart module */
    usart_init();
    /* Enable RX/TX Swtich as Output */
    DDR_CONFIG_OUT(RS485_TX_PIN);
    DDR_CONFIG_OUT(RS485_NRX_PIN);
    usart(UCSR,B) = _BV(usart(RXCIE)) | _BV(usart(RXEN));
    /* Default is reciever enabled*/
    PIN_CLEAR(RS485_TX_PIN);
    PIN_CLEAR(RS485_NRX_PIN);
}
Пример #5
0
// init DDR, waveform and timer
void
pwm_init(){
  TC1_COUNTER_CURRENT=0x00FF; //set the timer counter
#ifdef CH_A_PWM_GENERAL_SUPPORT
  DDR_CONFIG_OUT(CHANNEL_A_PWM); 		// PWM OUTPUT
  TC1_COUNTER_COMPARE=channelAval;
  TCCR1A|=_BV(COM1A1)|_BV(COM1A0); 		// Set OCnA on compare match
#endif /* CH_A_PWM_GENERAL_SUPPORT */
#ifdef CH_B_PWM_GENERAL_SUPPORT
  DDR_CONFIG_OUT(CHANNEL_B_PWM); 		// PWM OUTPUT
  OCR1B=channelBval;
  TCCR1A|=_BV(COM1B1)|_BV(COM1B0); 		// Set OCnB on compare match
#endif /* CH_B_PWM_GENERAL_SUPPORT */
#ifdef CH_C_PWM_GENERAL_SUPPORT
  DDR_CONFIG_OUT(CHANNEL_C_PWM); 		// PWM OUTPUT
  OCR1C=channelCval;
  TCCR1A|=_BV(COM1C1)|_BV(COM1C0); 		// Set OCnC on compare match
#endif /* CH_C_PWM_GENERAL_SUPPORT */

  TCCR1A|=_BV(WGM10);  					// PWM, Phase Correct, 8-bit

  TCCR1B|=_BV(WGM12); 					// waveform generation mode: CTC, 
  TCCR1B|=_BV(CS10); 					// clockselect: clkI/O/1 (No prescaling)

  // activate PWM outports OC1C
#ifdef CH_A_PWM_GENERAL_SUPPORT
  #if defined(_ATMEGA128)
  TCCR1C|=1<<FOC1A;					// with atmega128
  #else
  TCCR1A|=1<<FOC1A;  					// with atmega32
  #endif
#endif /* CH_A_PWM_GENERAL_SUPPORT */
#ifdef CH_B_PWM_GENERAL_SUPPORT
  #if defined(_ATMEGA128)
  TCCR1C|=1<<FOC1B;					// with atmega128
  #else
  TCCR1A|=1<<FOC1B;						// with atmega 32
  #endif
#endif /* CH_B_PWM_GENERAL_SUPPORT */
#ifdef CH_C_PWM_GENERAL_SUPPORT
  #if defined(_ATMEGA128)
  TCCR1C|=1<<FOC1C;  					// with atmega128
  #else
  TCCR1A|=1<<FOC1C; 					// with atmega 32
  #endif
#endif /* CH_C_PWM_GENERAL_SUPPORT */

}
Пример #6
0
void rc5_init(void)
{

    /* configure send pin as output, set low */
    DDR_CONFIG_OUT(RC5_SEND);
    PIN_CLEAR(RC5_SEND);

    /* enable timer0/timer2, set prescaler and enable overflow interrupt */
#ifdef RC5_USE_TIMER2
    TCCR2 = _BV(CS22) | _BV(CS21) | _BV(CS20);
#else
    TCCR0A = 0;
    TCCR0B = _BV(CS02) | _BV(CS00);
#endif

    /* configure int0 to fire at any logical change */
    EICRA |= _BV(RC5_ISC0);
    EICRA &= ~_BV(RC5_ISC1);

    /* clear any old interrupts and enable int0 interrupt */
    EIFR = _BV(RC5_INT_PIN);
    EIMSK |= _BV(RC5_INT_PIN);

    /* reset everything to zero */
    memset((void *)&rc5_global, 0, sizeof(rc5_global));

    /* enable rc5 receive, init variables */
    rc5_global.enabled = 1;
}
Пример #7
0
void door_init(void)
{
    door_doorstate = 0;

    DDR_CONFIG_IN(DOOR_REED_CONTACT);
    PIN_SET(DOOR_REED_CONTACT);

    DDR_CONFIG_IN(DOOR_LOCK_BRIDGE);
    PIN_SET(DOOR_LOCK_BRIDGE);

    DDR_CONFIG_IN(DOOR_LOCK_UNLOCKED_CONTACT);
    PIN_SET(DOOR_LOCK_UNLOCKED_CONTACT);

    DDR_CONFIG_IN(DOOR_LOCK_LOCKED_CONTACT);
    PIN_SET(DOOR_LOCK_LOCKED_CONTACT);

    DDR_CONFIG_IN(DOOR_HANDLE_CONTACT);
    PIN_SET(DOOR_HANDLE_CONTACT);

    DDR_CONFIG_IN(DOOR_DOOR_OPEN_CONTACT);
    PIN_SET(DOOR_DOOR_OPEN_CONTACT);

    PIN_CLEAR(DOOR_LOCK);
    DDR_CONFIG_OUT(DOOR_LOCK);
}
Пример #8
0
void leds_init(void)
{
    DDR_CONFIG_OUT(LED_OPEN_PIN);
    PIN_SET(LED_OPEN_PIN);
    leds_open_state = LED_OFF;
    leds_counter = 0;
}
Пример #9
0
uint8_t 
spi_send(uint8_t outdata)
{
  DDR_CONFIG_IN(SOFT_SPI_MISO);

  uint8_t j, indata = indata;
  for(j = 0; j < 8; j++)
  {
    if(outdata & 0x80)
      PIN_SET(SOFT_SPI_MOSI);
    else
      PIN_CLEAR(SOFT_SPI_MOSI);

    PIN_SET(SOFT_SPI_SCK);
    indata <<= 1;

    if(PIN_HIGH(SOFT_SPI_MISO))
      indata |= 1;

    PIN_CLEAR(SOFT_SPI_SCK);

    outdata <<= 1;
  }

  DDR_CONFIG_OUT(SOFT_SPI_MISO);
  return indata;
}
Пример #10
0
void
irmp_init(void)
{
#ifdef IRMP_RX_SUPPORT
  /* configure TSOP input, disable pullup */
  DDR_CONFIG_IN(IRMP_RX);
  PIN_CLEAR(IRMP_RX);
#endif

#ifdef STATUSLED_IRMP_RX_SUPPORT
  DDR_CONFIG_OUT(STATUSLED_IRMP_RX);
  IRMP_RX_LED_OFF;
#endif

#ifdef STATUSLED_IRMP_TX_SUPPORT
  DDR_CONFIG_OUT(STATUSLED_IRMP_TX);
  IRMP_TX_LED_OFF;
#endif

  /* init timer0/2 to expire after 1000/IRMP_HZ ms */
#ifdef IRMP_USE_TIMER2
  SET_HW_PRESCALER;
  TC2_COUNTER_COMPARE = SW_PRESCALER - 1;
  TC2_COUNTER_CURRENT = 0;
  TC2_INT_COMPARE_ON;           /* enable interrupt */
#else
  SET_HW_PRESCALER;
  TC0_COUNTER_COMPARE = SW_PRESCALER - 1;
  TC0_COUNTER_CURRENT = 0;
  TC0_INT_COMPARE_ON;           /* enable interrupt */
#endif

#ifdef IRMP_TX_SUPPORT
  PIN_CLEAR(IRMP_TX);
  DDR_CONFIG_OUT(IRMP_TX);
#ifndef IRMP_EXTERNAL_MODULATOR
#ifdef IRMP_USE_TIMER2
  TC0_MODE_CTC;
  TC0_PRESCALER_1;
#else
  TC2_MODE_CTC;
  TC2_PRESCALER_1;
#endif
#endif
  irmp_tx_set_freq(IRSND_FREQ_36_KHZ);  /* default frequency */
#endif
}
Пример #11
0
void leds_init(void)
{
    DDR_CONFIG_OUT(LED_0);
    PIN_CLEAR(LED_0);
    leds_set_pin(LED_0, false);

    DDR_CONFIG_OUT(LED_1);
    leds_set_pin(LED_1, false);

    DDR_CONFIG_OUT(LED_2);
    leds_set_pin(LED_2, false);

    uint8_t i;
    for(i=0; i<LEDS_COUNT; i++){
        leds_state[i] = LED_OFF;
    }

}
Пример #12
0
void door_init(void)
{
    door_doorstate = 0;

    PIN_CLEAR(DOOR_HBRIDGE_ENABLE);
    PIN_CLEAR(DOOR_HBRIDGE_IN1);
    PIN_CLEAR(DOOR_HBRIDGE_IN2);
   
    DDR_CONFIG_OUT(DOOR_HBRIDGE_ENABLE);
    DDR_CONFIG_OUT(DOOR_HBRIDGE_IN1);
    DDR_CONFIG_OUT(DOOR_HBRIDGE_IN2);

    locking = false;
    unlocking = false;
#if 0    
    int i;
    while(1){
        door_turnLeft();
        for(i=0; i<25; i++) _delay_ms(1);
        while(1){
            for(i=0; i<5; i++) _delay_ms(1);
            if(adc_getChannel(7) > 1000) {
                break;
            }
        }
        door_stop();
        for(i=0; i<1000; i++) _delay_ms(1);
        door_turnRight();
        for(i=0; i<25; i++) _delay_ms(1);
        while(1){
            for(i=0; i<5; i++) _delay_ms(1);
            if(adc_getChannel(7) > 1000) {
                break;
            }
        }
        door_stop();
        for(i=0; i<1000; i++) _delay_ms(1);
    }
#endif
}
Пример #13
0
void door_init(void)
{
    door_doorstate = 0;
    DDR_CONFIG_IN(DOOR_REED_CONTACT);
    PIN_SET(DOOR_REED_CONTACT);

    DDR_CONFIG_IN(DOOR_LOCK_BRIDGE);
    PIN_SET(DOOR_LOCK_BRIDGE);

    DDR_CONFIG_IN(DOOR_LOCK_UNLOCKED_CONTACT);
    PIN_SET(DOOR_LOCK_UNLOCKED_CONTACT);

    DDR_CONFIG_IN(DOOR_LOCK_LOCKED_CONTACT);
    PIN_SET(DOOR_LOCK_LOCKED_CONTACT);

    DDR_CONFIG_IN(DOOR_HANDLE_CONTACT);
    PIN_SET(DOOR_HANDLE_CONTACT);

    DDR_CONFIG_IN(DOOR_DOOR_OPEN_CONTACT);
    PIN_SET(DOOR_DOOR_OPEN_CONTACT);

    PIN_SET(DOOR_HBRIDGE_ENABLE);
    PIN_CLEAR(DOOR_HBRIDGE_IN1);
    PIN_CLEAR(DOOR_HBRIDGE_IN2);
   
    DDR_CONFIG_OUT(DOOR_HBRIDGE_ENABLE);
    DDR_CONFIG_OUT(DOOR_HBRIDGE_IN1);
    DDR_CONFIG_OUT(DOOR_HBRIDGE_IN2);
 
    DDR_CONFIG_IN(DOOR_LOCK_CONTACT);
    PIN_SET(DOOR_LOCK_CONTACT);
    
    //PIN_CLEAR(R1);
    //DDR_CONFIG_OUT(R1);

    door_desiredState = DOOR_LOCK_LOCKED;
    door_barState = BAR_UNKNOWN;
    door_state = DOOR_LOCK;
}
Пример #14
0
void
zbus_core_init(void)
{
    /* Initialize the usart module */
    usart_init();

    /* Enable RX/TX Swtich as Output */
    DDR_CONFIG_OUT(ZBUS_RXTX_PIN);

#ifdef HAVE_ZBUS_RX_PIN
    DDR_CONFIG_OUT(ZBUS_RX_PIN);
#endif
#ifdef HAVE_ZBUS_TX_PIN
    DDR_CONFIG_OUT(ZBUS_TX_PIN);
#endif

    /* clear the buffers */
    zbus_txlen = 0;
    zbus_rxlen = 0;
    zbus_index = 0;

    zbus_rxstart ();
}
Пример #15
0
void
irmp_init (void)
{
    /* configure TSOP input, disable pullup */
    DDR_CONFIG_IN (IRMP_RX);
    PIN_CLEAR (IRMP_RX);

#ifdef IRMP_RX_LED
    DDR_CONFIG_OUT (STATUSLED_RX);
    IRMP_RX_LED_OFF;
#endif

    /* init timer0/2 to expire after 1000/IRMP_HZ ms */
#ifdef IRMP_USE_TIMER2
    _TCCR2_PRESCALE = HW_PRESCALER_MASK;
    _OUTPUT_COMPARE_REG2 = SW_PRESCALER - 1;
    _TCNT2 = 0;
    _TIMSK_TIMER2 |= _BV (_OUTPUT_COMPARE_IE2);	/* enable interrupt */
#else
    _TCCR0_PRESCALE = HW_PRESCALER_MASK;
    _OUTPUT_COMPARE_REG0 = SW_PRESCALER - 1;
    _TCNT0 = 0;
    _TIMSK_TIMER0 |= _BV (_OUTPUT_COMPARE_IE0);	/* enable interrupt */
#endif

#ifdef IRSND_SUPPORT
    PIN_CLEAR (IRMP_TX);
    DDR_CONFIG_OUT (IRMP_TX);
#ifdef IRMP_USE_TIMER2
    _TCCR0_PRESCALE = _BV (_WGM01) | _BV (_CS00);	/* CTC mode, 0x01, start Timer 0, no prescaling */
#else
    _TCCR2_PRESCALE = _BV (_WGM21) | _BV (_CS20);	/* CTC mode, 0x01, start Timer 2, no prescaling */
#endif
    irmp_tx_set_freq (IRSND_FREQ_36_KHZ);	/* default frequency */
#endif
}
Пример #16
0
void fs20_init(void)
{
    /* default: enabled */
    fs20_global.enable = 1;

#ifdef FS20_SEND_SUPPORT
    /* configure port pin for sending */
    DDR_CONFIG_OUT(FS20_SEND);
    PIN_CLEAR(FS20_SEND);
#endif

#ifdef FS20_RECEIVE_SUPPORT
    /* reset global data structures */
    //fs20_global.fs20.raw = 0;
    memset((void *)&fs20_global.fs20.datagram, 0, sizeof(fs20_global.fs20.datagram));

    /* configure port pin for use as input to the analoge comparator */
    DDR_CONFIG_IN(FS20_RECV);
    PIN_CLEAR(FS20_RECV);

    /* enable analog comparator,
     * use fixed voltage reference (1V, connected to AIN0)
     * reset interrupt flag (ACI)
     * enable interrupt
     */
    ACSR = _BV(ACBG) | _BV(ACI) | _BV(ACIE);

    /* configure timer2 for receiving fs20,
     * prescaler 128
     * overflow interrupt enabled */
    TC2_COUNTER_CURRENT = 0;
    TC2_PRESCALER_128;
    TC2_MODE_OFF;
    TC2_OUTPUT_COMPARE_NONE;
    TC2_INT_OVERFLOW_ON;

#ifdef FS20_RECEIVE_WS300_SUPPORT

    /* reset everything to zero */
    memset((void *)&fs20_global.ws300, 0, sizeof(fs20_global.ws300));
#endif
#endif

#ifdef FS20_RECV_PROFILE
    fs20_global.int_counter = 0;
    fs20_global.ovf_counter = 0;
#endif
}
Пример #17
0
void fs20_init(void)
{
    /* default: enabled */
    fs20_global.enable = 1;

#ifdef FS20_SEND_SUPPORT
    /* configure port pin for sending */
    DDR_CONFIG_OUT(FS20_SEND);
    PIN_CLEAR(FS20_SEND);
#endif

#ifdef FS20_RECEIVE_SUPPORT
    /* reset global data structures */
    memset((void *)&fs20_global.fs20.datagram, 0, sizeof(fs20_global.fs20));

    /* configure port pin for use as input to the analoge comparator */
    DDR_CONFIG_IN(FS20_RECV);
    PIN_CLEAR(FS20_RECV);

    /* enable analog comparator,
     * use fixed voltage reference (1V, connected to AIN0)
     * reset interrupt flag (ACI)
     * enable interrupt
     */
    ACSR = _BV(ACBG) | _BV(ACI) | _BV(ACIE);

    /* configure timer2 for receiving fs20,
     * prescaler 128
     * overflow interrupt enabled */
    TCNT2 = 0;
    TCCR2A = 0;
    TCCR2B = _BV(CS20) | _BV(CS22);
    _TIMSK_TIMER2 = _BV(TOIE2);

#ifdef FS20_RECEIVE_WS300_SUPPORT

    /* reset everything to zero */
    memset((void *)&fs20_global.ws300, 0, sizeof(fs20_global.ws300));
#endif
#endif

#ifdef FS20_RECV_PROFILE
    fs20_global.int_counter = 0;
    fs20_global.ovf_counter = 0;
#endif
}
Пример #18
0
void
modbus_init(void)
{
    /* Initialize the usart module */
    usart_init();

    /* Enable RX/TX Swtich as Output */
    DDR_CONFIG_OUT(MODBUS_TX);
    PIN_CLEAR(MODBUS_TX);

    modbus_data.len = 0;
    modbus_data.sent = 0;
    modbus_data.crc_len = 0;

#ifdef MODBUS_CLIENT_SUPPORT
    modbus_client_state.len = 0;
#endif

}
Пример #19
0
void matemat_init()
{
    DDR_CONFIG_OUT(MATEMAT_COOLER);
    PIN_CLEAR(MATEMAT_COOLER);
    DDRC &= ~((1<<PC4)|(1<<PC3));
    DDRC |= ((1<<PC5)|(1<<PC6));
    PORTC |= (1<<PC4)|(1<<PC3);
    PORTC &= ~((1<<PC5)|(1<<PC6));

    i2c_init();
    lm75_init();
    matemat_setupdisp();
    usart_init();
  //  usart(UCSR,B) &= ~_BV(usart(RXCIE));

    matemat_global.mode = MODE_IDLE;
    matemat_global.temps[TEMP_START] = temp(TEMP_START_VAL);
    matemat_global.temps[TEMP_STOP] = temp(TEMP_STOP_VAL);

    TCCR0B = 1<<CS01;       //div by 8
    TIMSK0 |= 1<<TOIE0;
}
Пример #20
0
void spi_init(void)
/* {{{ */ {

    /* configure MOSI, SCK, lines as outputs */
    DDR_CONFIG_OUT(SPI_MOSI);
    DDR_CONFIG_OUT(SPI_SCK);
    DDR_CONFIG_IN(SPI_MISO);

#ifdef DATAFLASH_SUPPORT
    DDR_CONFIG_OUT(SPI_CS_DF);
    PIN_SET(SPI_CS_DF);
#endif

#ifdef ENC28J60_SUPPORT
    /* set all CS high (output) */
    DDR_CONFIG_OUT(SPI_CS_NET);
    PIN_SET(SPI_CS_NET);
#endif

#ifdef RFM12_SUPPORT
    /* initialize spi link to rfm12 module */
    DDR_CONFIG_OUT(SPI_CS_RFM12);
    /* Enable the pullup */
    PIN_SET(SPI_CS_RFM12);
#endif

#ifdef DATAFLASH_SUPPORT
    DDR_CONFIG_OUT(SPI_CS_DF);
    PIN_SET(SPI_CS_DF);
#endif

    /* enable spi, set master and clock modes (f/2) */
    _SPCR0 = _BV(_SPE0) | _BV(_MSTR0);
    _SPSR0 = _BV(_SPI2X0);

} /* }}} */
Пример #21
0
static void
dht_start(void)
{
  DDR_CONFIG_OUT(DHT);
  PIN_CLEAR(DHT);
}