int main () { nrk_setup_ports (); nrk_setup_uart (UART_BAUDRATE_115K2); // nrk_setup_uart (UART_BAUDRATE_230K4); nrk_setup_uart_gps(UART_BAUDRATE_115K2); TWI_Master_Initialise(); grideye_addr = 0x68; printf ("Starting up...\r\n"); nrk_init (); nrk_led_clr (ORANGE_LED); nrk_led_clr (BLUE_LED); nrk_led_clr (GREEN_LED); nrk_led_clr (RED_LED); nrk_time_set (0, 0); nrk_create_taskset (); nrk_start (); return 0; }
int main () { nrk_setup_ports(); nrk_setup_uart(UART_BAUDRATE_115K2); TWI_Master_Initialise(); sei(); nrk_led_set(RED_LED); /* initialize the adxl345 */ init_adxl345(); init_itg3200(); init_hmc5843(); nrk_init(); nrk_led_clr(ORANGE_LED); nrk_led_clr(BLUE_LED); nrk_led_clr(GREEN_LED); nrk_led_clr(RED_LED); nrk_time_set(0,0); nrk_create_taskset (); nrk_start(); return 0; }
void motor_init (void) { MOTOR_DDR |= (1 << MOTOR_OUTPUT_ENABLE) | (1 << MOTOR_RESET) | (1 << MOTOR_SELECT_BYTE) | (1 << MOTOR_ENABLE) | (1 << MOTOR_DIR); MOTOR_PORT |= (1 << MOTOR_ENABLE) | (1 << MOTOR_RESET); /* Compare match at 10625 gives a timer freq of 100hz */ OCR3A = 10400; /* Enable CTC */ TCCR3A |= (1 << COM3A0); /* Prescale = 8 */ TCCR3B = (1 << CS31) | (1 << WGM32); /* Enable compare match A interrupt */ ETIMSK |= (1 << OCIE3A); /* Set TWI pins to output and initialize */ DDRD |= (1 << PD0) | (1 << PD1); /* Address */ messageBuf[0] = 0x50; /* Command */ messageBuf[1] = 0x00; TWI_Master_Initialise (); motor_reset (); motor_write (0); return; }
Light::Light() { // Configure I2C // TWI_Master_Initialise(); lastSeconds = 0; initialized = 0; integrationActive = false; lockedSlope = 0.0; }
uint8_t init_twi(uint8_t priority) { uint8_t num_tasks = 0; LOG("init: prio "); LOGP("%u\r\n", priority); TWI_Master_Initialise(); ASSERT(num_tasks == NUM_TASKS_TWI); return num_tasks; }
void pca9634_init(uint8_t address) { unsigned char msgBuf[5]; TWI_Master_Initialise(); pca9634_address = address; /* init, MODE1.SLEEP shall be set to normal mode */ uint8_t data = PCA9634_MODE1_SLEEP|PCA9634_MODE1_SUB1|PCA9634_MODE1_SUB2|PCA9634_MODE1_SUB3|PCA9634_MODE1_ALL; msgBuf[0] = (pca9634_address<<TWI_ADR_BITS) | (FALSE<<TWI_READ_BIT); msgBuf[1] = PCA9634_REG_MODE1; msgBuf[2] = data; TWI_Start_Read_Write( msgBuf, 3 ); while ( TWI_Transceiver_Busy() ); }
void twi_init(void) { #if TWILIB == AVR315 || TWILIB == AVR315_SYNC || TWILIB == AVR315_QUEUED TWI_Master_Initialise(); #elif TWILIB == BUFFTW i2cInit(); i2cSetBitrate(400); #else twi_init(); #endif }
void mainInit() { USART_Init(MYUBRR); fdevopen(USART_Transmit, USART_Receive); SPI_Init(); CAN_init(); SERVO_init(); TWI_Master_Initialise(); SOLENOID_init(); MOTOR_init(); IR_init(); sei(); }
void main (void) { uint16_t sensorRead; unsigned char msgBuf[2]; uint16_t statusCode; sei(); myInit(); TWI_Master_Initialise(); //pwmInit(100); lcd_set_font(FONT_FIXED_8,NORMAL); lcd_put_string_P(FONT_FIXED_8, NORMAL, PSTR("Init ... done!")); while(1) { sensorRead+=1; //_delay_ms(200); //TWEN: twi module enable //TWSTA: twi start condition -> generates a start contition on the bus (it it is free) // if it is not free, it waits until a stop condition is // detected //TWIE: the interrupt request for twi will be activated as long as the twint flag is high //TWINT: reset twint flag (by writing a one) -> starts the operation of the twi module TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);// | (1<<TWIE); //TWSR should now have the status code, that the START has been send successfully statusCode = TWSR; lcd_moveto_xy (1, 0); lcd_put_uint(statusCode); //after the START has been send, the TWINT flag is set 0 (wait until it is 0) //while (!(TWCR & (1<<TWINT))); //write the bmp addressWrite into the TWI data register TWDR = 0xEE; //start transmission by reseting the TWINT (by writing a one) TWCR = (1<<TWINT) | (1<<TWEN); //while (!(TWCR & (1<<TWINT))); TWDR = 0xF4; TWCR = (1<<TWINT) | (1<<TWEN); //while (!(TWCR & (1<<TWINT))); TWDR = 0xF4; TWCR = (1<<TWINT) | (1<<TWEN); //send STOP condition TWCR = (1<<TWINT) | (1<<TWEN)| (1<<TWSTO); //TWSR should now have the status code, that the slave has accepted the data package statusCode = TWSR; lcd_moveto_xy (2, 0); lcd_put_uint(statusCode); //readInternalRegister(0xAA); //sensorRead = readInternalRegister(0xAA); //lcd_moveto_xy (2, 0); //lcd_put_string_P(FONT_FIXED_8, NORMAL, PSTR("Task1")); //lcd_put_uint(sensorRead); _delay_ms(100); } }
void main( void ) { unsigned char messageBuf[4]; unsigned char TWI_targetSlaveAddress, temp, TWI_operation=0, pressedButton, myCounter=0; //LED feedback port - connect port B to the STK500 LEDS DDRB = 0xFF; PORTB = myCounter; //Switch port - connect portD to the STK500 switches DDRD = 0x00; TWI_Master_Initialise(); __enable_interrupt(); TWI_targetSlaveAddress = 0x10; // This example is made to work together with the AVR311 TWI Slave application note and stk500. // In adition to connecting the TWI pins, also connect PORTB to the LEDS and PORTD to the switches. // The code reads the pins to trigger the action you request. There is an example sending a general call, // address call with Master Read and Master Write. The first byte in the transmission is used to send // commands to the TWI slave. // This is a stk500 demo example. The buttons on PORTD are used to control different TWI operations. for(;;) { pressedButton = ~PIND; if (pressedButton) // Check if any button is pressed { do{temp = ~PIND;} // Wait until key released while (temp); switch ( pressedButton ) { // Send a Generall Call case (1<<PD0): messageBuf[0] = TWI_GEN_CALL; // The first byte must always consit of General Call code or the TWI slave address. messageBuf[1] = 0xAA; // The command or data to be included in the general call. TWI_Start_Transceiver_With_Data( messageBuf, 2 ); break; // Send a Address Call, sending a command and data to the Slave case (1<<PD1): messageBuf[0] = (TWI_targetSlaveAddress<<TWI_ADR_BITS) | (FALSE<<TWI_READ_BIT); // The first byte must always consit of General Call code or the TWI slave address. messageBuf[1] = TWI_CMD_MASTER_WRITE; // The first byte is used for commands. messageBuf[2] = myCounter; // The second byte is used for the data. TWI_Start_Transceiver_With_Data( messageBuf, 3 ); break; // Send a Address Call, sending a request, followed by a resceive case (1<<PD2): // Send the request-for-data command to the Slave messageBuf[0] = (TWI_targetSlaveAddress<<TWI_ADR_BITS) | (FALSE<<TWI_READ_BIT); // The first byte must always consit of General Call code or the TWI slave address. messageBuf[1] = TWI_CMD_MASTER_READ; // The first byte is used for commands. TWI_Start_Transceiver_With_Data( messageBuf, 2 ); TWI_operation = REQUEST_DATA; // To release resources to other operations while waiting for the TWI to complete, // we set a operation mode and continue this command sequence in a "followup" // section further down in the code. // Get status from Transceiver and put it on PORTB case (1<<PD5): PORTB = TWI_Get_State_Info(); break; // Increment myCounter and put it on PORTB case (1<<PD6): PORTB = ++myCounter; break; // Reset myCounter and put it on PORTB case (1<<PD7): PORTB = myCounter = 0; break; } } if ( ! TWI_Transceiver_Busy() ) { // Check if the last operation was successful if ( TWI_statusReg.lastTransOK ) { if ( TWI_operation ) // Section for follow-up operations. { // Determine what action to take now if (TWI_operation == REQUEST_DATA) { // Request/collect the data from the Slave messageBuf[0] = (TWI_targetSlaveAddress<<TWI_ADR_BITS) | (TRUE<<TWI_READ_BIT); // The first byte must always consit of General Call code or the TWI slave address. TWI_Start_Transceiver_With_Data( messageBuf, 2 ); TWI_operation = READ_DATA_FROM_BUFFER; // Set next operation } else if (TWI_operation == READ_DATA_FROM_BUFFER) { // Get the received data from the transceiver buffer TWI_Get_Data_From_Transceiver( messageBuf, 2 ); PORTB = messageBuf[1]; // Store data on PORTB. TWI_operation = FALSE; // Set next operation } } } else // Got an error during the last transmission { // Use TWI status information to detemine cause of failure and take appropriate actions. TWI_Act_On_Failure_In_Last_Transmission( TWI_Get_State_Info( ) ); } } // Do something else while waiting for TWI operation to complete and/or a switch to be pressed __no_operation(); // Put own code here. } }
Light::Light() { // Configure I2C // TWI_Master_Initialise(); lastSeconds = 0; }
void i2c_init() { TWI_Master_Initialise(); TWI_targetSlaveAddress = TMP100; TWI_operation = SEND_DATA; }
main(void) { // {{{ uchar sensor_probe_counter = 0; uchar timer_overflow = 0; #if ENABLE_IDLE_RATE int idle_counter = 0; #endif cli(); hardware_init(); #if ENABLE_KEYBOARD init_keyboard_emulation(); init_ui_system(); #endif #if ENABLE_MOUSE init_mouse_emulation(); #endif TWI_Master_Initialise(); usbInit(); init_int_eeprom(); init_button_state(); wdt_reset(); sei(); // Sensor initialization must be done with interrupts enabled! // It uses I2C (TWI) to configure the sensor. sensor_init_configuration(); LED_TURN_ON(GREEN_LED); for (;;) { // main event loop wdt_reset(); usbPoll(); if (TIFR & (1<<TOV0)) { timer_overflow = 1; // Resetting the Timer0 // Setting this bit to one will clear it. TIFR = 1<<TOV0; } else { timer_overflow = 0; } update_button_state(timer_overflow); // Red LED lights up if there is any kind of error in I2C communication if ( TWI_statusReg.lastTransOK ) { LED_TURN_OFF(RED_LED); } else { LED_TURN_ON(RED_LED); } // Handling the state change of the main switch if (ON_KEY_UP(BUTTON_SWITCH)) { // Upon releasing the switch, stop the continuous reading. sensor_stop_continuous_reading(); #if ENABLE_KEYBOARD // And also reset the menu system. init_ui_system(); #endif } else if (ON_KEY_DOWN(BUTTON_SWITCH)) { // Upon pressing the switch, start the continuous reading for // mouse emulation code. sensor_start_continuous_reading(); } // Continuous reading of sensor data if (sensor.continuous_reading) { // {{{ // Timer is set to 1.365ms if (timer_overflow) { // The sensor is configured for 75Hz measurements. // I'm using this timer to read the values twice that rate. // 5 * 1.365ms = 6.827ms ~= 146Hz if (sensor_probe_counter > 0){ // Waiting... sensor_probe_counter--; } } if (sensor_probe_counter == 0) { // Time for reading new data! uchar return_code; return_code = sensor_read_data_registers(); if (return_code == SENSOR_FUNC_DONE || return_code == SENSOR_FUNC_ERROR) { // Restart the counter+timer sensor_probe_counter = 5; } } } // }}} #if ENABLE_IDLE_RATE // Timer is set to 1.365ms if (timer_overflow) { // {{{ // Implementing the idle rate... if (idle_rate != 0) { if (idle_counter > 0){ idle_counter--; } else { // idle_counter counts how many Timer0 overflows are // required before sending another report. // The exact formula is: // idle_counter = (idle_rate * 4)/1.365; // But it's better to avoid floating point math. // 4/1.365 = 2.93, so let's just multiply it by 3. idle_counter = idle_rate * 3; //keyDidChange = 1; LED_TOGGLE(YELLOW_LED); // TODO: Actually implement idle rate... Should re-send // the current status. } } } // }}} #endif // MAIN code. Code that emulates the mouse or implements the menu // system. if (button.state & BUTTON_SWITCH) { // Code for when the switch is held down // Should read data and do things #if ENABLE_MOUSE // nothing here #endif } else { // Code for when the switch is "off" // Basically, this is the menu system (implemented as keyboard) #if ENABLE_KEYBOARD ui_main_code(); #endif } // Sending USB Interrupt-in report if(usbInterruptIsReady()) { if (0) { // This useless "if" is here to make all the following // conditionals an "else if", and thus making it a lot // easier to add/remove them using preprocessor directives. } #if ENABLE_KEYBOARD else if(string_output_pointer != NULL){ // Automatically send keyboard report if there is something // in the buffer send_next_char(); usbSetInterrupt((void*) &keyboard_report, sizeof(keyboard_report)); } #endif #if ENABLE_MOUSE else if (button.state & BUTTON_SWITCH) { if (mouse_prepare_next_report()) { usbSetInterrupt((void*) &mouse_report, sizeof(mouse_report)); } } #endif } } } // }}}