/** Configures the board hardware and chip peripherals for the demo's functionality. */ static void SetupHardware(void) { /* Disable watchdog if enabled by bootloader/fuses */ MCUSR &= ~(1 << WDRF); wdt_disable(); /* Disable clock division */ clock_prescale_set(clock_div_1); /* Relocate the interrupt vector table to the bootloader section */ MCUCR = (1 << IVCE); MCUCR = (1 << IVSEL); /* Hardware Initialization */ LEDs_Init(); USB_Init(); /* Bootloader active LED toggle timer initialization */ TIMSK1 = (1 << TOIE1); TCCR1B = ((1 << CS11) | (1 << CS10)); }
/** Configures the board hardware and chip peripherals for the demo's functionality. */ void SetupHardware(void) { #if (ARCH == ARCH_AVR8) /* Disable watchdog if enabled by bootloader/fuses */ MCUSR &= ~(1 << WDRF); wdt_disable(); /* Disable clock division */ clock_prescale_set(clock_div_1); #endif /* Hardware Initialization */ Serial_Init(9600, false); LEDs_Init(); Buttons_Init(); Joystick_Init(); USB_Init(); /* Create a stdio stream for the serial port for stdin and stdout */ Serial_CreateStream(NULL); }
/** Configures the board hardware and chip peripherals for the demo's functionality. */ void SetupHardware(void) { /* Disable watchdog if enabled by bootloader/fuses */ MCUSR &= ~(1 << WDRF); wdt_disable(); /* Disable clock division */ clock_prescale_set(clock_div_1); /* Configure all button pins to use internal pullup */ PORTD |= (1<<7) | (1<<4) | (1<<2) | (1<<0) | (1<<6) | (1<<1); PORTE |= (1<<2); PORTB |= (1<<0) | (1<<4) | (1<<5) | (1<<7); /* Subsystem Initialization */ EncoderInit(); DebounceInit(); LedInit(); USB_Init(); }
/** Configures the board hardware and chip peripherals for the application's functionality. */ void SetupHardware(void) { #if (ARCH == ARCH_AVR8) /* Disable watchdog if enabled by bootloader/fuses */ MCUSR &= ~(1 << WDRF); wdt_disable(); /* Disable clock division */ clock_prescale_set(clock_div_1); #endif /* Hardware Initialization */ USB_Init(); /* Power up the HD44780 Interface */ HD44780_Initialize(); HD44780_WriteCommand(CMD_DISPLAY_ON); /* Start the flush timer so that overflows occur rapidly to push received bytes to the USB interface */ TCCR0B = (1 << CS02); }
/** Configures the board hardware and chip peripherals for the demo's functionality. */ void SetupHardware(void) { MCUSR = 0; /* Disable watchdog if enabled by bootloader/fuses */ wdt_disable(); /* Disable clock division */ clock_prescale_set(clock_div_1); serial_init(); GlobalInterruptEnable(); /* Hardware Initialization */ LEDs_Init(); while(!started); USB_Init(); }
/** Configures the board hardware and chip peripherals for the demo's functionality. */ void SetupHardware(void) { #if (ARCH == ARCH_AVR8) /* Disable watchdog if enabled by bootloader/fuses */ MCUSR &= ~(1 << WDRF); wdt_disable(); /* Disable clock division */ clock_prescale_set(clock_div_1); #endif /* Hardware Initialization */ LEDs_Init(); USB_Init(); /* Timer Initialization */ OCR0A = 100; TCCR0A = (1 << WGM01); TCCR0B = (1 << CS00); TIMSK0 = (1 << OCIE0A); }
/** Main program entry point. This routine configures the hardware required by the application, then * starts the scheduler to run the USB management task. */ int main(void) { /* Disable watchdog if enabled by bootloader/fuses */ MCUSR &= ~(1 << WDRF); wdt_disable(); /* Disable clock division */ clock_prescale_set(clock_div_1); /* Indicate USB not ready */ UpdateStatus(Status_USBNotReady); /* Initialize Scheduler so that it can be used */ Scheduler_Init(); /* Initialize USB Subsystem */ USB_Init(); /* Scheduling - routine never returns, so put this last in the main function */ Scheduler_Start(); }
/** Configures the board hardware and chip peripherals for the demo's functionality. */ void SetupHardware(void) { /* Disable watchdog if enabled by bootloader/fuses */ MCUSR &= ~(1 << WDRF); wdt_disable(); /* Disable clock division */ clock_prescale_set(clock_div_1); /* Hardware Initialization */ //LEDs_Init(); USB_Init(); TWI_Init(); /* PCICR |= (1 << PCIE0); // enable PCMSK0 scan on PCIE0 PCMSK0 |= (1 << PCINT7); // PCINT7 (PB7) to trigger interrupt on pin change sei(); */ }
/** Main program entry point. This routine configures the hardware required by the application, then * starts the scheduler to run the USB management task. */ int main(void) { /* After reset start bootloader? */ if ((AVR_IS_WDT_RESET()) && (boot_key == DFU_BOOT_KEY_VAL)) { boot_key = 0; Bootloader(); } /* Disable watchdog if enabled by bootloader/fuses */ MCUSR &= ~(1 << WDRF); wdt_disable(); /* Disable clock division */ clock_prescale_set(clock_div_1); /* Hardware Initialization */ LEDs_Init(); /* Indicate USB not ready */ UpdateStatus(Status_USBNotReady); /* Initialize Scheduler so that it can be used */ Scheduler_Init(); /* Initialize USB Subsystem */ USB_Init(); /* Initialize I/O lines */ //IO_Init(); /* Initialize Timers */ Timer_Init(); /* Initialize Motors */ Motor_Init(); /* Scheduling - routine never returns, so put this last in the main function */ Scheduler_Start(); }
int main(void) { clock_prescale_set(clock_div_16); uint8_t brightness = 0; int8_t direction = 1; LED_DDR = 0xff; while (1) { // Bright and dim if (brightness == 0) { direction = 1; } if (brightness == 255) { direction = -1; } brightness += direction; pwmAllPins(brightness); } return (0); }
/** Configures the board hardware and chip peripherals for the demo's functionality. */ static void SetupHardware(void) { /* Disable watchdog if enabled by bootloader/fuses */ MCUSR &= ~(1 << WDRF); wdt_disable(); /* Disable clock division */ clock_prescale_set(clock_div_1); /* Relocate the interrupt vector table to the bootloader section */ MCUCR = (1 << IVCE); MCUCR = (1 << IVSEL); /* Hardware Initialization */ LEDs_Init(); USB_Init(); /* configure opendeck led */ DDRE |= 0b01000000; //led output PORTE |= 0b01000000; //led high }
/** Configures the board hardware and chip peripherals for the demo's functionality. */ void SetupHardware(void) { #if (ARCH == ARCH_AVR8) /* Disable watchdog if enabled by bootloader/fuses */ MCUSR &= ~(1 << WDRF); wdt_disable(); /* Disable clock division */ clock_prescale_set(clock_div_1); #endif /* Hardware Initialization */ LEDs_Init(); USB_Init(); /* Start the flush timer so that overflows occur rapidly to push received bytes to the USB interface */ TCCR0B = (1 << CS02); /* Pull target /RESET line high */ AVR_RESET_LINE_PORT |= AVR_RESET_LINE_MASK; AVR_RESET_LINE_DDR |= AVR_RESET_LINE_MASK; }
int main(void) { uint8_t tempHighByte, tempLowByte; // -------- Inits --------- // clock_prescale_set(clock_div_1); /* 8MHz */ initUSART(); printString("\r\n==== i2c Thermometer ====\r\n"); initI2C(); // ------ Event loop ------ // while (1) { /* To set register, address LM75 in write mode */ i2cStart(); i2cSend(LM75_ADDRESS_W); i2cSend(LM75_TEMP_REGISTER); i2cStart(); /* restart, just send start again */ /* Setup and send address, with read bit */ i2cSend(LM75_ADDRESS_R); /* Now receive two bytes of temperature */ tempHighByte = i2cReadAck(); tempLowByte = i2cReadNoAck(); i2cStop(); // Print it out nicely over serial for now... printByte(tempHighByte); if (tempLowByte & _BV(7)) { printString(".5\r\n"); } else { printString(".0\r\n"); } /* Once per second */ _delay_ms(1000); } /* End event loop */ return 0; /* This line is never reached */ }
/** Configures the board hardware and chip peripherals for the demo's functionality. */ void SetupHardware(void) { #if (ARCH == ARCH_AVR8) /* Disable watchdog if enabled by bootloader/fuses */ MCUSR &= ~(1 << WDRF); wdt_disable(); /* Disable clock division */ clock_prescale_set(clock_div_1); #elif (ARCH == ARCH_XMEGA) /* Start the PLL to multiply the 2MHz RC oscillator to 32MHz and switch the CPU core to run from it */ XMEGACLK_StartPLL(CLOCK_SRC_INT_RC2MHZ, 2000000, F_CPU); XMEGACLK_SetCPUClockSource(CLOCK_SRC_PLL); /* Start the 32MHz internal RC oscillator and start the DFLL to increase it to 48MHz using the USB SOF as a reference */ XMEGACLK_StartInternalOscillator(CLOCK_SRC_INT_RC32MHZ); XMEGACLK_StartDFLL(CLOCK_SRC_INT_RC32MHZ, DFLL_REF_INT_USBSOF, F_USB); PMIC.CTRL = PMIC_LOLVLEN_bm | PMIC_MEDLVLEN_bm | PMIC_HILVLEN_bm; #endif /* Hardware Initialization */ LEDs_Init(); Joystick_Init(); Buttons_Init(); Dataflash_Init(); USB_Init(); /* Check if the Dataflash is working, abort if not */ if (!(DataflashManager_CheckDataflashOperation())) { LEDs_SetAllLEDs(LEDMASK_USB_ERROR); for(;;); } /* Clear Dataflash sector protections, if enabled */ DataflashManager_ResetDataflashProtections(); }
/** Configures the board hardware and chip peripherals for the demo's functionality. */ void SetupHardware(void) { #if (ARCH == ARCH_AVR8) /* Disable watchdog if enabled by bootloader/fuses */ MCUSR &= ~(1 << WDRF); wdt_disable(); /* Disable clock division */ clock_prescale_set(clock_div_1); #elif (ARCH == ARCH_XMEGA) /* Start the PLL to multiply the 2MHz RC oscillator to 32MHz and switch the CPU core to run from it */ XMEGACLK_StartPLL(CLOCK_SRC_INT_RC2MHZ, 2000000, F_CPU); XMEGACLK_SetCPUClockSource(CLOCK_SRC_PLL); /* Start the 32MHz internal RC oscillator and start the DFLL to increase it to 48MHz using the USB SOF as a reference */ XMEGACLK_StartInternalOscillator(CLOCK_SRC_INT_RC32MHZ); XMEGACLK_StartDFLL(CLOCK_SRC_INT_RC32MHZ, DFLL_REF_INT_USBSOF, F_USB); PMIC.CTRL = PMIC_LOLVLEN_bm | PMIC_MEDLVLEN_bm | PMIC_HILVLEN_bm; #endif LEDs_Init(); USB_Init(); }
// Get the Leonardo ready for USB communication void setupHardware(void) { // Disable the watch-dog timer if enabled by boot-loader or fuses MCUSR &= ~(1 << WDRF); wdt_disable(); // Disable clock division clock_prescale_set(clock_div_1); // Initialise the hardware USART module #ifdef USART_DEBUG Serial_Init(115200, true); Serial_CreateStream(NULL); printf("USART Debug enabled\r\n"); #endif // Initialise the EEPROM settings eepromInit(); // Initialise the LUFA board driver and USB library LEDs_Init(); USB_Init(); }
/** Initializes all of the hardware. */ void initialize(void){ /* Disable watchdog if enabled by bootloader/fuses */ MCUSR &= ~(1 << WDRF); wdt_disable(); /* Disable clock division */ clock_prescale_set(clock_div_1); /* Init SPI */ spi_init(SPIMODE2); /* Init AD9833 */ ad9833_init(); /* LUFA USB related inits */ USB_Init(); CDC_Device_CreateBlockingStream (&VirtualSerial_CDC_Interface, &USBSerialStream); /* enable interrupts*/ sei(); }
/** Configures the board hardware and chip peripherals for the demo's functionality. */ void SetupHardware(void) { /* Disable watchdog if enabled by bootloader/fuses */ MCUSR &= ~(1 << WDRF); wdt_disable(); /* Disable clock division */ clock_prescale_set(clock_div_1); /* Hardware Initialization */ Serial_Init(9600, false); Buttons_Init(); ADC_Init(ADC_FREE_RUNNING | ADC_PRESCALE_32); ADC_SetupChannel(MIC_IN_ADC_CHANNEL); LEDs_Init(); USB_Init(); /* Create a stdio stream for the serial port for stdin and stdout */ Serial_CreateStream(NULL); /* Start the ADC conversion in free running mode */ ADC_StartReading(ADC_REFERENCE_AVCC | ADC_RIGHT_ADJUSTED | ADC_GET_CHANNEL_MASK(MIC_IN_ADC_CHANNEL)); }
/** Initializes all of the hardware. */ void initialize(void){ /* Disable watchdog if enabled by bootloader/fuses */ MCUSR &= ~(1 << WDRF); wdt_disable(); /* Disable clock division */ clock_prescale_set(clock_div_1); /** LUFA USB related inits */ USB_Init(); /* Enable numpad pullups */ PORTB |= PB_ALL_MASK; PORTC |= PC_ALL_MASK; PORTD |= PD_ALL_MASK; //keypad_init(); /* Capslock LED */ CAPL_LED_DDR |= (1<<CAPL_LED_BIT); /** enable interrupts*/ sei(); }
/** Configures the board hardware and chip peripherals for the demo's functionality. */ inline static void SetupHardware(void) { /* Disable watchdog if enabled by bootloader/fuses */ MCUSR &= ~(1 << WDRF); wdt_disable(); /* Disable clock division */ clock_prescale_set(clock_div_1); /* Hardware Initialization */ LEDs_Init(); Buttons_Init(); //button HWB Joystick_Init(); //gate driving output - PORTC.6 / PWM-A Timer3 DDRC |= (1<<PC6); PORTC &= ~(1<<PC6); Init_PWM_timer(); /* conversion mode (free running or single) and prescaler masks * ACDSRA: start ADC, clock prescaler, interrupt, trigger enable, start single conv */ ADC_Init( ADC_SINGLE_CONVERSION | ADC_PRESCALE_128); /* Must setup the ADC channel to read beforehand */ ADC_SetupChannel(3); ADC_SetupChannel(4); /* Start the ADC conversion in free running mode * ADMUX and Vref, adjust, sel mux channel with gain * */ //ADC_StartReading(ADC_REFERENCE_INT2560MV | ADC_RIGHT_ADJUSTED | ADC_1100MV_BANDGAP); //Temperature_Init(); //Dataflash_Init(); SerialStream_Init(9600, false); }
/** Configures the board hardware and chip peripherals for the demo's functionality. */ void SetupHardware(void) { /* Disable watchdog if enabled by bootloader/fuses */ MCUSR &= ~(1 << WDRF); wdt_disable(); /* Disable clock division */ clock_prescale_set(clock_div_1); /* Hardware Initialization */ #if defined(USB_CAN_BE_BOTH) USB_Init(USB_MODE_UID); #else USB_Init(); #endif LEDs_Init(); SPI_Init(SPI_SPEED_FCPU_DIV_2 | SPI_SCK_LEAD_FALLING | SPI_SAMPLE_TRAILING | SPI_MODE_MASTER); Dataflash_Init(); Buttons_Init(); Serial_Init(9600, true); /* Create a stdio stream for the serial port for stdin and stdout */ Serial_CreateStream(NULL); #if defined(USB_CAN_BE_DEVICE) /* Check if the Dataflash is working, abort if not */ if (!(DataflashManager_CheckDataflashOperation())) { LEDs_SetAllLEDs(LEDMASK_USB_ERROR); for(;;); } /* Clear Dataflash sector protections, if enabled */ DataflashManager_ResetDataflashProtections(); #endif }
/** * @brief Initialize the system clock to 16 MHz * * Ensure that CKDIV8 fuse does not affect the system clock prescaler * */ void mcu_clock_init(void) { uint8_t low_fuse, prescaler; /* * Determine which oscillator the CPU is configured for, and * adjust the clock prescaler appropriately. For the internal RC * oscillator, the special prescaler value 0x0F must be used to * adjust the RC oscillator for full 16 MHz operation. In all * other cases, value 0x0 is used to use a prescaler of 1. This * makes the operation independent of the CKDIV8 fuse setting. */ ENTER_CRITICAL_REGION(); low_fuse = read_avr_fuse(0); LEAVE_CRITICAL_REGION(); if ((low_fuse & 0x0F) == 0x02) { /* Internal RC oscillator used. */ #if (F_CPU == 16000000) prescaler = 0x0F; #endif #if (F_CPU == 8000000) prescaler = clock_div_1; #endif #if (F_CPU == 4000000) prescaler = clock_div_2; #endif } else { /* Any other clock source, usually the 16 MHz crystal oscillator. */ prescaler = clock_div_1; } clock_prescale_set(prescaler); }
static void SetupHardware(void) { /* Disable watchdog if enabled by bootloader/fuses */ MCUSR &= ~(1 << WDRF); wdt_disable(); /* Disable clock division */ clock_prescale_set(clock_div_1); // Leonardo needs. Without this USB device is not recognized. USB_Disable(); USB_Init(); // for Console_Task USB_Device_EnableSOFEvents(); print_set_sendchar(sendchar_func); // SUART PD0:output, PD1:input DDRD |= (1<<0); PORTD |= (1<<0); DDRD &= ~(1<<1); PORTD |= (1<<1); }
void SetupHardware(void) { /* Disable watchdog if enabled by bootloader/fuses */ MCUSR &= ~(1 << WDRF); wdt_disable(); /* Disable clock division */ clock_prescale_set(clock_div_1); status.is_neo_geo_stick = 1; /* Hardware Initialization */ /* Port B all pins are input with pull=up resistor enabled */ DDRB = 0; PORTB = 0xFF; /* Port C we only use pin 2 */ DDRC &= ~(1<<DDC2); PORTC |= (1<<PORTC2); /* Port D we use ping 0-1, 4-5 */ DDRD = 0xFF; PORTD = 0x33; SerialDebug_init(); USB_Init(); }
int main(void) { /* Disable watchdog if enabled by bootloader/fuses */ MCUSR &= ~(1 << WDRF); wdt_disable(); /* Disable clock division */ clock_prescale_set(clock_div_1); /* Hardware Initialization */ DDRB = 0xFF; DDRD = 0xFF; DDRC = 0xF0; /* Initialize Scheduler so that it can be used */ Scheduler_Init(); /* Initialize USB Subsystem */ USB_Init(); /* Scheduling - routine never returns, so put this last in the main function */ Scheduler_Start(); }
/** Configures the board hardware and chip peripherals for the demo's functionality. */ void SetupHardware(void) { #if (ARCH == ARCH_AVR8) /* Disable watchdog if enabled by bootloader/fuses */ MCUSR &= ~(1 << WDRF); wdt_disable(); /* Disable clock division */ clock_prescale_set(clock_div_1); #endif /* Disable JTAG debugging */ MCUCR |= (1 << JTD); MCUCR |= (1 << JTD); /* Enable pull-up on the JTAG TDI pin so we can use it to select the mode */ PORTF |= (1 << 7); Delay_MS(10); /* Select the firmware mode based on the JTD pin's value */ CurrentFirmwareMode = (PINF & (1 << 7)) ? MODE_USART_BRIDGE : MODE_PDI_PROGRAMMER; /* Re-enable JTAG debugging */ MCUCR &= ~(1 << JTD); MCUCR &= ~(1 << JTD); /* Hardware Initialization */ SoftUART_Init(); LEDs_Init(); #if defined(RESET_TOGGLES_LIBUSB_COMPAT) UpdateCurrentCompatibilityMode(); #endif /* USB Stack Initialization */ USB_Init(); }
int main(void) { wdt_disable(); #ifdef HAS_16MHZ_CLOCK /* set clock to 16MHz/2 = 8Mhz */ clock_prescale_set(clock_div_2); #endif // LED_ON_DDR |= _BV( LED_ON_PIN ); // LED_ON_PORT |= _BV( LED_ON_PIN ); led_init(); LED_ON(); spi_init(); // init_adcw(); //eeprom_factory_reset("xx"); eeprom_init(); // Setup the timers. Are needed for watchdog-reset OCR0A = 249; // Timer0: 0.008s = 8MHz/256/250 == 125Hz TCCR0B = _BV(CS02); TCCR0A = _BV(WGM01); TIMSK0 = _BV(OCIE0A); TCCR1A = 0; TCCR1B = _BV(CS11) | _BV(WGM12); // Timer1: 1us = 8MHz/8 MCUSR &= ~(1 << WDRF); // Enable the watchdog wdt_enable(WDTO_2S); #ifdef HAS_16MHZ_CLOCK uart_init( UART_BAUD_SELECT(UART_BAUD_RATE,F_CPU) ); #else uart_init( UART_BAUD_SELECT_DOUBLE_SPEED(UART_BAUD_RATE,F_CPU) ); #endif fht_init(); tx_init(); input_handle_func = analyze_ttydata; display_channel = DISPLAY_USB; #ifdef HAS_RF_ROUTER rf_router_init(); display_channel |= DISPLAY_RFROUTER; #endif LED_OFF(); sei(); for(;;) { uart_task(); RfAnalyze_Task(); Minute_Task(); #ifdef HAS_FASTRF FastRF_Task(); #endif #ifdef HAS_RF_ROUTER rf_router_task(); #endif #ifdef HAS_ASKSIN rf_asksin_task(); #endif #ifdef HAS_MORITZ rf_moritz_task(); #endif #ifdef HAS_RWE rf_rwe_task(); #endif #ifdef HAS_MBUS rf_mbus_task(); #endif } }
int main(void) { unsigned char tmp; wdt_disable(); /* Disable watchdog if enabled by bootloader/fuses */ power_usb_disable() ; power_usart1_disable(); power_spi_disable(); Buttons_Init(); LEDs_Init(); clock_prescale_set(clock_div_1); // run at x-tal frequency 16Mhz eeprom_read_block( &EE_data,0,sizeof(EE_data)); if (EE_data.bright_level == 0xff && EE_data.dim_level == 0xff) { EE_data.bright_level = LED_BRIGHT; EE_data.dim_level = LED_DIMM; eeprom_write_block(&EE_data,0,sizeof(EE_data)); eeprom_busy_wait(); } OCR0A = EE_data.bright_level; OCR0B = EE_data.dim_level; // Timer 0 setup for simple count mode, used as timebase LED PWM TCCR0A = 0; // simple count more TCCR0B = 4; // system Clock 16Mhz/256 = 16us per counter tick //TCCR0B = 3; // system Clock 16Mhz/64 = 4us per counter tick //TCCR0B = 5; // system Clock 16Mhz/1024 = 64us per counter tick //TCCR0B = 2; // system Clock 1Mhz/8 = 8us per counter tick TIMSK0 = 0x7; // Enable OverFLow , OCR0A and OCR0B interrupt enables MCUSR =0; //MCU status register clear // Timer 1 setup for fast PWM mode for servo pulse generation 1 to 2ms TIMSK1 = 0x2; // Enable OCR1A interrupt TCCR1A = 0x00; // No pin toggles on compare -- CTC (normal) mode TCCR1B = 0x0D; // CTC mode, F_CPU 16mhz/1024 clock ( 64us) //TCCR1B = 0x0B; // CTC mode, F_CPU 1Mhz/64 clock ( 64us) OCR1A = 15625-1; // counting 15625 counts of 64 us == 1 second sei(); while(1) // for ever { if (IsButtonPressed(BUTTON1) ) { // de-bounce -- 10 consecutive reads of switch open for (tmp =0; tmp<=10; tmp++) { _delay_ms(2); if (IsButtonPressed(BUTTON1)) tmp = 0; } if ( mode < 3) mode++; else { eeprom_write_block(&EE_data,0,sizeof(EE_data)); eeprom_busy_wait(); mode = 0; // enter running mode } } if (IsButtonPressed(BUTTON2) ) // increases Minutes and decrease brightness { // de-bounce -- 10 consecutive reads of switch open for (tmp =0; tmp<=10; tmp++) { _delay_ms(2); if (IsButtonPressed(BUTTON2)) tmp = 0; } switch (mode) { case 1: Minutes++; ripple(); break; case 2: if (OCR0B < 0xff ) // dim level { OCR0B++; EE_data.dim_level = OCR0B; } break; case 3: // bright level if (OCR0A < OCR0B-10 ) // make sure that bright is at least 10 counts brighter than dim { OCR0A +=10; // step in increments of 10 EE_data.bright_level =OCR0A; } break; } } if (IsButtonPressed(BUTTON3) ) // Increases Hours and increase brightness { // de-bounce -- 10 consecutive reads of switch open for (tmp =0; tmp<=10; tmp++) { _delay_ms(2); if (IsButtonPressed(BUTTON3)) tmp = 0; } switch (mode) { case 1: Hours++; ripple(); break; case 2: if (OCR0B > OCR0A+10) // dim level { OCR0B--; EE_data.dim_level = OCR0B; } break; case 3: // bright level if (OCR0A > 20) { OCR0A -=10; //step in increments of 10 EE_data.bright_level =OCR0A; } break; } } } // end of for-ever }
void main() { // power management setup clock_prescale_set(clock_div_16); // 500 kHz clock ADCSRA = 0; // DIE, ADC! DIE!!! power_adc_disable(); power_usi_disable(); power_timer1_disable(); ACSR = _BV(ACD); set_sleep_mode(SLEEP_MODE_PWR_DOWN); // millisecond timer setup TCCR0A = _BV(WGM01); // mode 2 - CTC TCCR0B = _BV(CS01) | _BV(CS00); // prescale = 64 TIMSK0 = _BV(OCIE0A); // OCR0A interrupt only. OCR0A = IRQ_CYCLE_LENGTH + 1; millis_counter = 0; irq_cycle_pos = 0; sei(); // I/O port setup DDRA = _BV(PA0) | _BV(PA1) | _BV(PA2) | _BV(PA3) | _BV(PA7); DDRB = _BV(PB0) | _BV(PB1) | _BV(PB2); PORTA = _BV(PA5); // enable the pull-up on the button pin PORTB = 0; unsigned char pattern = eeprom_read_byte(EE_PATTERN_NUM); if (pattern >= PATTERN_COUNT) pattern = 0; place_in_pattern = -1; milli_of_next_change = 0; button_debounce_time = 0; button_press_time = 0; ignoring_button = 0; while(1) { unsigned long now = millis(); // poll for button events unsigned int event = checkEvent(); switch(event) { case EVENT_LONG_PUSH: sleepNow(); continue; case EVENT_SHORT_PUSH: if (++pattern >= PATTERN_COUNT) pattern = 0; place_in_pattern = -1; milli_of_next_change = 0; eeprom_write_byte(EE_PATTERN_NUM, pattern); continue; } // If it's time to move to the next step in a pattern, pull out its // mask and clear all of the LEDs. // Note that now > milli_of_next_change is wrong here because our // time wraps. now - milli_of_next_change > 0 *looks* the same, // but handles cases where the sign differs. if (milli_of_next_change == 0 || ((long)(now - milli_of_next_change)) >= 0) { // It's time to go to the next step in the pattern place_in_pattern++; const struct pattern_entry *our_pattern = (struct pattern_entry *)pgm_read_ptr(&(patterns[pattern])); struct pattern_entry entry; do { memcpy_P(&entry, (void*)(&(our_pattern[place_in_pattern])), sizeof(struct pattern_entry)); // A duration of 0 is the end of the pattern. if (entry.duration != 0) break; place_in_pattern = 0; } while(1); // This means a pattern of a single entry with 0 duration is kryptonite. current_mask = entry.mask; // Turn out all the lights for(int i = 0; i < LED_COUNT; i++) { *((unsigned char *)pgm_read_ptr(&(LED_PORT[i]))) &= ~_BV(pgm_read_byte(&(LED_PIN[i]))); } milli_of_next_change = now + entry.duration; current_led = 99; //invalid } // Now multiplex the LEDSs. Rotate through all of the set bits in the // current mask and turn the next one on every pass through this loop. if (current_mask != 0) { // if it IS zero, then all the lights are out anyway. unsigned char next_led = current_led; while(1) { // we know it's not zero, so there's at least one bit set if (++next_led >= LED_COUNT) next_led = 0; if (current_mask & (1 << next_led)) break; // found one! } if (next_led != current_led) { // turn the old one off and the new one on. if (current_led < LED_COUNT) // is it safe? *((unsigned char *)pgm_read_ptr(&(LED_PORT[current_led]))) &= ~_BV(pgm_read_byte(&(LED_PIN[current_led]))); *((unsigned char *)pgm_read_ptr(&(LED_PORT[next_led]))) |= _BV(pgm_read_byte(&(LED_PIN[next_led]))); current_led = next_led; } } } }
int main(void) { wdt_disable(); // un-reset ethernet ENC28J60_RESET_DDR |= _BV( ENC28J60_RESET_BIT ); ENC28J60_RESET_PORT |= _BV( ENC28J60_RESET_BIT ); led_init(); LED_ON(); spi_init(); eeprom_init(); // if we had been restarted by watchdog check the REQ BootLoader byte in the // EEPROM ... if(bit_is_set(MCUSR,WDRF) && eeprom_read_byte(EE_REQBL)) { eeprom_write_byte( EE_REQBL, 0 ); // clear flag // TBD: This is useless as button needs to be pressed - needs moving into bootloader directly // start_bootloader(); } // Setup OneWire and make a full search at the beginning (takes some time) #ifdef HAS_ONEWIRE i2c_init(); onewire_Init(); onewire_FullSearch(); #endif // Setup the timers. Are needed for watchdog-reset #if defined (HAS_IRRX) || defined (HAS_IRTX) ir_init(); // IR uses highspeed TIMER0 for sampling OCR0A = 1; // Timer0: 0.008s = 8MHz/256/2 == 15625Hz #else OCR0A = 249; // Timer0: 0.008s = 8MHz/256/250 == 125Hz #endif TCCR0B = _BV(CS02); TCCR0A = _BV(WGM01); TIMSK0 = _BV(OCIE0A); TCCR1A = 0; TCCR1B = _BV(CS11) | _BV(WGM12); // Timer1: 1us = 8MHz/8 clock_prescale_set(clock_div_1); MCUSR &= ~(1 << WDRF); // Enable the watchdog wdt_enable(WDTO_2S); uart_init( UART_BAUD_SELECT_DOUBLE_SPEED(UART_BAUD_RATE,F_CPU) ); fht_init(); tx_init(); input_handle_func = analyze_ttydata; #ifdef HAS_RF_ROUTER rf_router_init(); display_channel = (DISPLAY_USB|DISPLAY_RFROUTER); #else display_channel = DISPLAY_USB; #endif ethernet_init(); LED_OFF(); sei(); for(;;) { uart_task(); RfAnalyze_Task(); Minute_Task(); #ifdef HAS_FASTRF FastRF_Task(); #endif #ifdef HAS_RF_ROUTER rf_router_task(); #endif #ifdef HAS_ASKSIN rf_asksin_task(); #endif #ifdef HAS_IRRX ir_task(); #endif #ifdef HAS_ETHERNET Ethernet_Task(); #endif } }