void init(void) { unsigned char port_num = 0; #ifdef USE_OW uint16_t ram_virt_data_addr; uint16_t eeprom_perm_data_addr; #endif getFuncCode(); //read function codes from eeprom for (port_num = 0; port_num < GET_VIRT_PORT_COUNT(COUNT_IO_PINS); port_num++) { initIOport(&(io_pins[port_num])); //first pin initialisation (all configured pins) //applikation specific initialization for (int pin = 0; pin < VIRTUAL_PORT_PINCOUNT; pin++) { /* loop over all virtual IO Pins */ switch (io_pins[port_num].pins[pin].function_code) { #ifdef USE_OW case PIN_OW_POWER_PARASITE: case PIN_OW_POWER_EXTERN: { //copy rom codes from eeprom to ram I2C_MAIN_INFO("1-wire[%i;%i]: OW code eeprom -> ram\r\n",port_num,pin); ram_virt_data_addr = VIRTUAL_DATA_START + (port_num * (VIRTUAL_PORT_PINCOUNT * VIRTUAL_DATA_LENGTH)) + (pin * VIRTUAL_DATA_LENGTH); eeprom_perm_data_addr = EEPROM_DATA_START + (port_num * (VIRTUAL_PORT_PINCOUNT * EEPROM_DATA_LENGTH)) + (pin * EEPROM_DATA_LENGTH); I2C_MAIN_DEBUG("ram_virt_data_addr: 0x%x\r\n", ram_virt_data_addr); I2C_MAIN_DEBUG("eeprom_perm_data_addr: 0x%x\r\n", eeprom_perm_data_addr); eeprom_read_block (&rxbuffer[ram_virt_data_addr], eeprom_perm_data_addr, OW_ROMCODE_SIZE); } break; #endif default: break; } } } /* set Slave ID */ txbuffer[0] = SLAVE_ID; txbuffer[1] = SLAVE_ID; /* copy git version into txbuffer */ strncpy(&txbuffer[VERSION_START], VINFO_GITDESC, VERSION_LENGTH); txbuffer[VERSION_START + VERSION_LENGTH] = '\0'; I2C_MAIN_INFO("version[%i;%i]\r\n", VERSION_START, VERSION_START+VERSION_LENGTH); I2C_MAIN_INFO("version:%s\r\n", &txbuffer[VERSION_START]); /* set IO Pin count */ txbuffer[VIRTUAL_IO_COUNT] = COUNT_IO_PINS; txbuffer[VIRTUAL_IO_COUNT + 1] = COUNT_IO_PINS; //### TWI unsigned char I2C_addi = eeprom_read_byte(0); I2C_addi = I2C_addi < 128 ? I2C_addi : 0x42; init_twi_slave(I2C_addi); //TWI als Slave mit Adresse slaveadr starten I2C_MAIN_INFO("I2C_address:0x%x\r\n", I2C_addi); I2C_MAIN_INFO("init_done\r\n"); }
int main(void) { init_uart(); // UART initalisieren und einschalten. Alle n�tigen Schnittstellen-Parameter und -Funktionen werden in rs232.h definiert init_MD49data(); // set defaults for MD49 data and commands init_twi_slave(SLAVE_ADRESSE); // Init AVR as Slave with Adresse SLAVE_ADRESSE sei(); // Interrupts enabled resetEncoders(); // Reset the encoder values to 0 wdt_enable(WDTO_250MS); while (1) // mainloop { setMD49commands(); // set commands on MD49 corresponding to values stored in i2cdata(0-14) getMD49data(); // get all data from MD49 and save to corresponding values in i2cdata(15-32) wdt_reset(); } //end.mainloop } //end.mainfunction
int main( void ) { TCCR0A= (0<<WGM01) | (1<<WGM00)|(1<<COM0B1) | (0<<COM0B0);//(1<<COM0A1) | (0<<COM0A0) TCCR0B=(0<<FOC0A) | (0<<FOC0B) | (0<<WGM02) | (0<<CS02) | (1<<CS01) | (1<<CS00); // --> Siehe Datenblatt s 101 ff // Achtung PWM am MODUS 3 Setzen // Meine Frage dazu im Netz // http://www.mikrocontroller.net/topic/281776#2977371 /*Du setzt den Modus 7. Das ist Fast-PWM mit OCR0A als Top. Damit fällt OCR0A schon mal als PWM weg, und da OCR0A den gleichen Wert hat wie OCR0B, wirst du von der PWM auf dem B Kanal nicht viel sehen. */ OCR0A = 0x7f; // zum einstellen des Tastverhältnisses OCR0B = 0x7f; // zum einstellen des Tastverhältnisses DDRD = (1<<PD5)|(1<<PD7)|(1<<PD6);//PD6 und PD5 wo OC0A und OC0B anliegen (Ausgabe der PWM) // PD7 für M1 dir DDRB = (1<<PB0)|(1<<PB2)|(1<<PB7); //PB 1 ist Ausgang M1D1 --> D1 // PB2 ist für D2 DDRC |= (1<<PC0); // Laut Wahheitstabele und Schaltung muss EN -> H / D1-> L / D2 -> H / IN1-> H / IN2 -> L // I2C Initialisieren init_twi_slave(SLAVE_ADRESSE); //TWI als Slave mit Adresse slaveadr starten init_hall_counters(); sei(); i2cdata[0] = 0x13; i2cdata[1] = 0x22; i2cdata[2] = 0xFE; Check = 0; while(1){ if((hall_counter_1 > 400) | (hall_counter_2 > 400)) { TCCR0A = 0x00; TCCR0B=0x00; Check = 0; } else{} PORTD = (1<<PD7); PORTB = (1<<PB2)|(1<<PB7); // D2 - PIN //PORTB &= ~(1<<PB0); PORTC = (1<<PC0); // EN - PIN } } return 0; }
void start_twi_slave(uint8_t slave_address_in, uint8_t use_sleep, void (*data_callback_in)(uint8_t input_buffer_length, const uint8_t *input_buffer, uint8_t *output_buffer_length, uint8_t *output_buffer), void (*idle_callback_in)(void)) { uint8_t call_datacallback = 0; slave_address = slave_address_in; data_callback = data_callback_in; idle_callback = idle_callback_in; input_buffer_length = 0; output_buffer_length = 0; output_buffer_current = 0; ss_state = ss_state_idle; init_twi_slave(slave_address_in); for (;;) { if(idle_callback) { idle_callback(); } if (ss_state == ss_state_new_data) { call_datacallback = 1; } if(call_datacallback) { output_buffer_length = 0; output_buffer_current = 0; data_callback(input_buffer_length, input_buffer, &output_buffer_length, output_buffer); input_buffer_length = 0; call_datacallback = 0; ss_state = ss_state_idle; } } }
int main(void){ // 12 PB0 LASER_R ---- NICHT MEHR ---- // 11 PD7 LASER (Laser) // 10 PD6 HAPTIK OC0A Timer0 8 Bit (Haptik) // 9 PD5 LED_FRONT_B OC0B Timer0 8 Bit // 13 PB1 IR_CLOCK_- OC1A Timer1 16 Bit // 14 PB2 LED_FRONT_G OC1B Timer1 16 Bit // 15 PB3 LED_FRONT_R OC2A Timer2 8 Bit // 1 PD3 LED_FRONT_W OC2B Timer2 8 Bit // 24 PC1 LDR ADC1 // 25 PC2 V_BATT ADC2 // 2 PD4 BUTTON_3 // 7 PB6 BUTTON_2 // 8 PB7 BUTTON_1 // 12 PB0 ONOFF cli(); //set pins DDRB |= (1 << DDB1) | (1 << DDB2) | (1 << DDB3); //OUTPUT DDRD |= (1 << DDD3) | (1 << DDD5) | (1 << DDD6) | (1 << DDD7); //OUTPUT DDRB &= ~(1 << PB6) & ~(1 << PB7); //INPUT PORTB |= (1 << PB6) | (1 << PB7); //input pullup DDRD &= ~(1 << PD4); //INPUT PORTD |= (1 << PD4); //input pullup //ausschalten //DDRB |= (1 << DDB0); //OUTPUT //PORTB &= ~(1 << DDB0); //LOW //init Timers for PWM: //https://sites.google.com/site/qeewiki/books/avr-guide/pwm-on-the-atmega328 //WGM* s157: fast pwm without ctc //CS* s158: _BV(CS20) = prescaler 1 (gilt für a und b) //PWM_fequency = clock_speed / [Prescaller_value * (1 + TOP_Value) ] = 8000000/(1*(1+210)) = 37.915 kHz //Timer 0: Mode 3: Fast PWM, TOP: 0xFF, Update of OCRx: Bottom, TOV1 Flag set on: TOP, Prescaler: 1, Set OC0x on Compare Match TCCR0A = _BV(COM0B0) | _BV(COM0A0) | _BV(COM0B1) | _BV(COM0A1) | _BV(WGM00) | _BV(WGM01); TCCR0B = _BV(CS00); //TIMSK0 |= (1<<TOIE0); //Overflow Interrupt for count millis //Timer 2: Mode 3: Fast PWM, TOP: 0xFF, Update of OCRx: Bottom, TOV1 Flag set on: TOP, Prescaler: 1, Set OC2x on Compare Match TCCR2A = _BV(COM2B0) | _BV(COM2A0) | _BV(COM2B1) | _BV(COM2A1) | _BV(WGM20) | _BV(WGM21); TCCR2B = _BV(CS20); //Timer 1: Mode 14: Fast PWM, inverting mode, Top: ICR1, Update of OCR1x: Bottom, TOV1 Flag set on: TOP, Prescaler: 1 //https://docs.google.com/spreadsheets/d/1HadMDsU0MGo1LXUr1gKNFrfe4wX_Bq9kbVIwOvD3chk TCCR1A = _BV(COM1B0) | _BV(COM1A0) | _BV(COM1B1) | _BV(COM1A1) | _BV(WGM11); TCCR1B = _BV(CS10) | _BV(WGM12) | _BV(WGM13); //IR carrier frequency to 37.915 kHz: ICR1 = 210; //IR carrier frequency duty cycle to 50%: IR_CLOCK_DUTY_CYCLE = ICR1-ICR1/2; //switch off all light LED_FRONT_R=255; LED_FRONT_G=ICR1; LED_FRONT_B=255; LED_FRONT_W=255; HAPTIK=255; //haptik //PORTB |= (1 << PB0); //an //i2c init init_twi_slave(I2C_BUS_ADRESS<<1); //UART init UBRR0H = UBRR_VAL >> 8; UBRR0L = UBRR_VAL & 0xFF; UCSR0B |= _BV(TXEN0) | _BV(RXEN0) | _BV(RXCIE0); //UART RX, TX und RX-Interrupt einschalten UCSR0C = (1<<UCSZ01) | (1<<UCSZ00); //Asynchron 8N1 sei(); // enable Interrupts global uint32_t laser_duration_counter=0; uint32_t vibrate_duration_counter=0; uint32_t muzzle_flash_duration_counter=0; uint32_t muzzle_flash_duration_counter_max=0; //fürs ausfaden uint16_t ir_delay=0; uint8_t ir_count=255; // return links*256 + rechts uint16_t battt = 12345; misc_buffer.v_bat_l = battt>>8; misc_buffer.v_bat_r = battt; while(1){ /* // ---------------- blinken + buttons (gedrueckt halten) + ir shoot ---------------- LED_FRONT_R = 255-1-taster(&PINB, BUTTON_1)*100; LED_FRONT_G = ICR1-map8(2, 0, 255, 0, ICR1); //0 bis ICR1 LED_FRONT_B = 255-1-taster(&PINB, BUTTON_2)*100; LED_FRONT_W = 255-1-taster(&PIND, BUTTON_3)*100; laser(1); //laser HAPTIK = 255-50; //haptik _delay_ms(255); _delay_ms(255); _delay_ms(255); LED_FRONT_R = 255-0; LED_FRONT_G = ICR1-map8(0, 0, 255, 0, ICR1); //0 bis ICR1 LED_FRONT_B = 255-0; LED_FRONT_W = 255-0; laser(0); //laser HAPTIK = 255-0; //haptik _delay_ms(255); _delay_ms(255); _delay_ms(255); shoot_buffer.playerid = 42; shoot_buffer.damage = 123; shoot_buffer.enable = 1; uint8_t array[] = {shoot_buffer.playerid, shoot_buffer.damage}; USART_Transmit(shoot_buffer.playerid); //IR_TX: PlayerID USART_Transmit(shoot_buffer.damage); //IR_TX: Schaden USART_Transmit(crc8(array, 2)); //IR_TX: crc8 checksum */ // ---------------- pew ---------------- uint8_t brightness = 80; if(taster(&PINB, BUTTON_1)){ if(taster(&PINB, BUTTON_2) && !taster(&PIND, BUTTON_3)){ LED_FRONT_R = 255-brightness; LED_FRONT_G = ICR1-map8(0, 0, 255, 0, ICR1); LED_FRONT_B = 255-0; LED_FRONT_W = 255-0; _delay_ms(20); laser(1); }else if(!taster(&PINB, BUTTON_2) && taster(&PIND, BUTTON_3)){ LED_FRONT_R = 255-0; LED_FRONT_G = ICR1-map8(brightness, 0, 255, 0, ICR1); LED_FRONT_B = 255-0; LED_FRONT_W = 255-0; _delay_ms(20); laser(1); }else if(taster(&PINB, BUTTON_2) && taster(&PIND, BUTTON_3)){ LED_FRONT_R = 255-brightness; LED_FRONT_G = ICR1-map8(brightness, 0, 255, 0, ICR1); LED_FRONT_B = 255-brightness; LED_FRONT_W = 255-brightness; _delay_ms(20); laser(1); }else{ LED_FRONT_R = 255-0; LED_FRONT_G = ICR1-map8(0, 0, 255, 0, ICR1); LED_FRONT_B = 255-brightness; LED_FRONT_W = 255-0; _delay_ms(20); laser(1); } _delay_ms(30); LED_FRONT_R = 255-0; LED_FRONT_G = ICR1-map8(0, 0, 255, 0, ICR1); LED_FRONT_B = 255-0; LED_FRONT_W = 255-0; _delay_ms(200); laser(0); _delay_ms(255); } /* // ---- buttons ---- laser_buffer.laser = taster(&PINB, BUTTON_1); led_front_buffer.r = taster(&PINB, BUTTON_2)*255; led_front_buffer.b = taster(&PIND, BUTTON_3)*255; */ } }