Exemple #1
0
void main(void)
{
	init();
	while (1) {
		while (usiTwiDataInReceiveBuffer())	{ // process I2C command
			processTWI();
			process_keyrepeat();
		}
	}
}
Exemple #2
0
int main()
{
  // set digits as output and disable them all
  DIG1_DDR |= _BV(DIG1_PIN);
  DIG2_DDR |= _BV(DIG2_PIN);
  DIG3_DDR |= _BV(DIG3_PIN);
  DIG4_DDR |= _BV(DIG4_PIN);
  light_digit(0);

  // setup all segments as inputs and low
  SEG_A_DDR &= ~_BV(SEG_A_PIN);
  SEG_B_DDR &= ~_BV(SEG_B_PIN);
  SEG_C_DDR &= ~_BV(SEG_C_PIN);
  SEG_D_DDR &= ~_BV(SEG_D_PIN);
  SEG_E_DDR &= ~_BV(SEG_E_PIN);
  SEG_F_DDR &= ~_BV(SEG_F_PIN);
  SEG_G_DDR &= ~_BV(SEG_G_PIN);
  
  SEG_A_PORT &= ~_BV(SEG_A_PIN);
  SEG_B_PORT &= ~_BV(SEG_B_PIN);
  SEG_C_PORT &= ~_BV(SEG_C_PIN);
  SEG_D_PORT &= ~_BV(SEG_D_PIN);
  SEG_E_PORT &= ~_BV(SEG_E_PIN);
  SEG_F_PORT &= ~_BV(SEG_F_PIN);
  SEG_G_PORT &= ~_BV(SEG_G_PIN);

  // set DP as output and to ground
  SEG_DP_DDR |= _BV(SEG_DP_PIN);
  SEG_DP_PORT &= ~_BV(SEG_DP_PIN);

  // set up timer 1 as system clock
  TIMSK = (1 << TOIE0);         // overflow interrupts
  TCCR0B = TIMER0_CLKDIV_488Hz; // default blink rate at 488Hz

  
  // init the TWI slave
  usiTwiSlaveInit(SLAVE_ADDRESS);

  // enable interrupts
  sei();

  // The infinite loop
  while (1)
  {
    // process Twi commands
    while (usiTwiDataInReceiveBuffer())
    {
      processTWI();
    }
  }

}
Exemple #3
0
int main(void)
{
	//********** Initiate variables *********
	adcArray[0] = 4;						// Device ID for Flexiforce 301A
	adcArray[1] = 0;
	adcArray[2] = 0;
	
	//********** Initialization ********** 
	
	usiTwiSlaveInit( slaveadress );			// Enable TWI I2C and set static slave address
	InitADC();								// Init ADC
	sei();									// Init global interupt
	
	//********** Main functions **********
	while(1)
	{
		
		if(!ADC_flag)
		{
		adcArray[1] = ADCL;
		adcArray[2] = ADCH;
		ADC_flag = 0;
		}

		
		while(!usiTwiDataInReceiveBuffer());		// Wait for master
		usiTwiTransmitByte(adcArray[0]);	// Send sensor ID
		
		while(!usiTwiDataInReceiveBuffer());		// Wait for master
		usiTwiTransmitByte(adcArray[1]);	// Send low part of sensordata
	
		while(!usiTwiDataInReceiveBuffer());		// Wait for master
		usiTwiTransmitByte(adcArray[2]);	// Send high part of sensordata
		
	}
	
	
	return 0;
}
uint8_t USI_TWI_S::available(){ // the bytes available that haven't been read yet
  return usiTwiDataInReceiveBuffer(); 
}
Exemple #5
0
//
// called infinitely in main() along with handle_script()
//
static void handle_i2c(void)
{
    //uint8_t tmp;
    //int8_t baddr;
    if( usiTwiDataInReceiveBuffer() ) {
        cmd  = usiTwiReceiveByte();
        myaddr = slaveAddressMatched;
        slaveAddressMatched = -1;

        switch(cmd) {
        case('@'):         // set addr to send to {'@',freemaddr,i2caddr,0}
        case('#'):         // script cmd: set ir pwm frequency & duty cycle
        case('%'):         // IR light on/off
        case('&'):         // packet_wait_millis, wait_after
            read_i2c_vals(3);  // all these take 3 args
            handle_script_cmd();
            break;
        case('$'):         // script cmd: send ir code
            read_i2c_vals(5);
            if( cmdargs[0] == 0 ) { // FIXME: 0 == sony command type 
                //uint32_t data = *(cmdargs+1);
                IRsend_sendSony( (cmdargs[1]<<8) | cmdargs[2],12 );  // FIXME
            } 
            else if( cmdargs[0] == 1 ) {  // 1 == RC5
                IRsend_sendSony( (cmdargs[1]<<8) | cmdargs[2],12 );  // FIXME
            }
            break;
        case('!'):           // send arbitrary i2c data 
            read_i2c_vals(8);  
            IRsend_sendSonyData64bit( cmdargs );
            //fanfare(3, 100 );
            break;
        case('^'):           // set colorspot {'^', 13, r,g,b }
            read_i2c_vals(4);

            cmdargs[6] = cmdargs[3]; // b
            cmdargs[5] = cmdargs[2]; // g
            cmdargs[4] = cmdargs[1]; // r
            cmdargs[3] = cmdargs[0]; // pos
            
            cmdargs[2] = 0xfe;       // 0xfe == set colorspot 
            cmdargs[1] = freem_addr;
            cmdargs[0] = 0x55;       // magic start byte

            cmdargs[7] = compute_checksum(cmdargs,7);

            //IRsend_sendSonyData64bit( cmdargs );
            ir_queue( cmdargs );

            break;
        case('*'):           // play colorspot {'*', 13, 0, 0 }
            read_i2c_vals(3);
            handle_script_cmd();
            /*
            tmp = blinkm_addr;  // FIXME: bit of a hack here
            blinkm_addr = 0xfd; // 0xfd == play colorspot 
            cmd = cmdargs[0];
            cmdargs[0] = cmdargs[1]; 
            cmdargs[1] = cmdargs[2];
            //cmdargs[2] = cmdargs[3];  // no, only have 3 args to use
            handle_script_cmd();
            blinkm_addr = tmp;
            */
            break;

            // stolen from blinkm.c
        case('a'):         // get address 
            usiTwiTransmitByte( eeprom_read_byte(&ee_i2c_addr) );
            break;
        case('A'):         // set address
            cmdargs[0] = cmdargs[1] = cmdargs[2] = cmdargs[3] = 0;
            read_i2c_vals(4);  // address, 0xD0, 0x0D, address
            if( cmdargs[0] != 0 && cmdargs[0] == cmdargs[3] && 
                cmdargs[1] == 0xD0 && cmdargs[2] == 0x0D ) {  // 
                eeprom_write_byte( &ee_i2c_addr, cmdargs[0] ); // write address
                i2c_addrs[0] = cmdargs[0];
                for( uint8_t i = 1; i<slaveAddressesCount; i++ ) {
                    i2c_addrs[i] = i2c_addrs[0] + i;
                }
                usiTwiSlaveInit( i2c_addrs );                 // re-init
                _delay_ms(5);  // wait a bit so the USI can reset
            }
            break;
        case('Z'):        // return protocol version
            usiTwiTransmitByte( BLINKM_PROTOCOL_VERSION_MAJOR );
            usiTwiTransmitByte( BLINKM_PROTOCOL_VERSION_MINOR );
            break;
        case('P'):       // play ctrlm script
            read_i2c_vals(3);
            play_script(0, cmdargs[1], cmdargs[2]);
            break;

        // blinkm cmds
        case('n'):         // script cmd: set rgb color now
        case('c'):         // script cmd: fade to rgb color
        case('C'):         // script cmd: fade to random rgb color
        case('h'):         // script cmd: fade to hsv color
        case('H'):         // script cmd: fade to random hsv color
        case('p'):         // script cmd: play script
            read_i2c_vals(3);
            handle_script_cmd();
            break;
        case('f'):
        case('t'):
            read_i2c_vals(1);
            handle_script_cmd();
        case('o'):
        case('O'):
            handle_script_cmd();
            break;
//
// new v2 commands
//
        case('l'):         // return script len & reps
            read_i2c_vals(1);  // script_id
            if( cmdargs[0] == 0 ) { // eeprom script
                usiTwiTransmitByte( eeprom_read_byte( &ee_script.len ) );
                usiTwiTransmitByte( eeprom_read_byte( &ee_script.reps ) );
            }
            else {
               //script* s=(script*)pgm_read_word(&(fl_scripts[cmdargs[1]-1]));
               //curr_script_len  = pgm_read_byte( (&s->len) );
               //curr_script_reps = pgm_read_byte( (&s->reps) );
            }
        case('i'):         // return current input values
            //usiTwiTransmitByte( myaddr ); // FIXME FIXME TEST
            usiTwiTransmitByte( timesOver ); // FIXME FIXME TEST
            //usiTwiTransmitByte( inputs[0] );
            usiTwiTransmitByte( inputs[1] );
            usiTwiTransmitByte( inputs[2] );
            usiTwiTransmitByte( inputs[3] );
            break;
        
        } // switch(cmd)
        
    } // if(usiTwi)
}
Exemple #6
0
int main(void)
{
	uint8_t data;

	uint8_t blinkDelay = BLINK_DELAY;
	bool	blinkToggle = false;

	pState	= PS_IDLE;
	mCounter = 0;

	mod_led_init();
	st_init_tmr0();
	usiTwiSlaveInit( SLAVE_ADRS );	// Initialize USI hardware for I2C Slave operation.

mod_led_toggle(4);
	
	sei();							// Enable interrupts.
	
	usitwiSlaveEnable();			// Enable the USI interface to receive data.

mod_led_toggle(3);

	// A simple loop to check for I2C Commands.
	// A state variable is needed to process multi-byte messages.
	// 01, 05 are Writes. 04 is a Read.
    while(1)
    {
#if 0
		// Heart Beat LED
		if( GPIOR0 & (1<<DEV_1MS_TIC) )
		{
			GPIOR0 &= ~(1<<DEV_1MS_TIC);
			if(--blinkDelay == 0) {
				blinkDelay = BLINK_DELAY;
				if(blinkToggle) {
					mod_led_on();
				} else {
					mod_led_off();
				}
				blinkToggle = !blinkToggle;
			}
		}
#endif
//mod_led_toggle(2);

		if( usiTwiDataInReceiveBuffer() )
		{
			data = usiTwiReceiveByte();
			switch (pState)
			{
				case PS_IDLE:
					// Process new message
					++mCounter;

					switch(data)
					{
						case 01:
							// Writing to the Control Register
							pState = PS_CMD01_0;	// next byte is Control byte
							break;

						case 04:
							// Reading counter
							usiTwiTransmitByte(mCounter);		// load up data for following read.
							break;

						case 05:
							// Writing to LED
							pState = PS_CMD05_0;	// next byte controls LED
							break;

						default:
							break;					// Ignore unknown command.
					}
					break;

				case PS_CMD01_0:
					// Process Control byte. b0=1 clears counter.
					if( (data & 0x01) == 0x01 )
					{
						mCounter = 0;
					}
					pState	= PS_IDLE;				// reset for next message
					break;

				case PS_CMD05_0:
					// Change LED state
					// If the data is 00, then turn OFF the LED. Turn it ON for any non-zero value.
					// NOTE: LED hardware is wired 'Active LOW'.
					if( data == 0)
					{
						mod_led_off();				// Turn LED OFF.
					}
					else
					{
						mod_led_on();				// Turn LED ON.
					}
					pState	= PS_IDLE;				// reset for next message
					break;

				default:
					pState	= PS_IDLE;				// ERROR, restore to know state
					break;
			}
		}
    }
}
Exemple #7
0
//----- Begin Code ------------------------------------------------------------
int main(void)
{
    static uint8_t f_i2c;
    static bool f_recieve;
    static uint8_t rbuf[8], *p, temp;
    static uint8_t state = S_NORMAL;


	// TIMER1の設定
	init_timer1();

    f_i2c = check_I2C();
    if (f_i2c)
    {
        // I2C スレーブ初期化
        usiTwiSlaveInit( TWI_slaveAddress );
    }
    else
    {
        // 2313標準UARTの初期化
        uart2313_init();
    }

    //  LCD初期化
    lcd_init(LCD_DISP_ON);
    lcdMapCustomChar(0, 0);
    lcdMapCustomChar(1, 1);
    lcdMapCustomChar(2, 2);
    lcdMapCustomChar(3, 3);
    lcdMapCustomChar(4, 4);
    lcdMapCustomChar(5, 5);
    lcdMapCustomChar(6, 6);
    lcdMapCustomChar(7, 7);
    lcd_gotoxy(0, 0);

    // any logos?
    // write something

    lcd_puts_P("I2CLCD");
    if (f_i2c)
    {             //1234567890123456
        lcd_puts_P("-I");
    }
    else
    {             //1234567890123456
        lcd_puts_P("-S");
    }

    sei();

    while (1)
    {
        if (f_i2c)
        {
            f_recieve = usiTwiDataInReceiveBuffer();
            if (f_recieve)
            {
                temp = usiTwiReceiveByte();
            }
        }
        else
        {
            f_recieve = xreadOK();
            if (f_recieve)
            {
                temp = xread();
            }

        }

        if (f_recieve)
        {
            switch (state)
            {
            case S_NORMAL:
                if (temp == ESC)
                {
                    state = S_SEQUENCE;
                }
                else
                {
                    lcd_putc(temp);
                }
                break;
            case S_SEQUENCE:
                p = rbuf;
                *p = temp;
                switch (temp)
                {
                case 'L': // L for Locate (X), (Y)
                    state = S_L1;
                    break;

                case 'H': // ホームポジションへカーソル移動
                    lcd_home();
                    state = S_NORMAL;
                    break;

                case 'C': // 画面クリア
                    lcd_clrscr();
                    state = S_NORMAL;
                    break;

                case 'S':  // S for Save Custom Character to EEPROM
                    state = S_S1;
                    break;

                case 'M': // M for Custom Character Mapping
                    state = S_M1;
                    break;

                case 'X': // 画面非表示
                    /* display off                            */
                    lcd_command(LCD_DISP_OFF);
                    state = S_NORMAL;
                    break;

                case 'N': // 画面表示・カーソル消去
                    /* display on, cursor off                 */
                    lcd_command(LCD_DISP_ON);
                    state = S_NORMAL;
                    break;

                case 'B': // 画面表示・カーソル非表示・ブリンク文字
                    /* display on, cursor off, blink char     */
                    lcd_command(LCD_DISP_ON_BLINK);
                    state = S_NORMAL;
                    break;

                case 'D': // 画面表示・カーソル表示
                    /* display on, cursor on                  */
                    lcd_command(LCD_DISP_ON_CURSOR);
                    state = S_NORMAL;
                    break;

                case 'E': // 画面表示・カーソル表示・ブリンク文字
                    /* display on, cursor on, blink char      */
                    lcd_command(LCD_DISP_ON_CURSOR_BLINK);
                    state = S_NORMAL;
                    break;

                case '-':  // カーソル左移動
                    /* move cursor left  (decrement)          */
                    lcd_command(LCD_MOVE_CURSOR_LEFT);
                    state = S_NORMAL;
                    break;
                case '+':  // カーソル右移動
                    /* move cursor right (increment)          */
                    lcd_command(LCD_MOVE_CURSOR_RIGHT);
                    state = S_NORMAL;
                    break;

                case '<': // 画面左移動
                    /* shift display left                     */
                    lcd_command(LCD_MOVE_DISP_LEFT);
                    state = S_NORMAL;
                    break;
                case '>': // 画面右移動
                    /* shift display right                    */
                    lcd_command(LCD_MOVE_DISP_RIGHT);
                    state = S_NORMAL;
                    break;

                }
                break;

            case S_L1:
                *++p = temp;
                state = S_L2;
                break;
            case S_L2:
//                *++p = temp;
                lcd_gotoxy(rbuf[1], temp);
                state = S_NORMAL;
                break;
            case S_M1:
                *++p = temp;
                state = S_M2;
                break;
            case S_M2:
//              *++p = temp;
                lcdMapCustomChar(rbuf[1],temp);
                state = S_NORMAL;
                break;

                // -----------------------------

            case S_S1:
                *++p = temp;
                state = S_S2;
                break;
            case S_S2:
                rCustom[0] = temp;
//                *++p = temp;
                state = S_S3;
                break;
            case S_S3:
                rCustom[1] = temp;
//                *++p = temp;
                state = S_S4;
                break;
            case S_S4:
                rCustom[2] = temp;
//                *++p = temp;
                state = S_S5;
                break;
            case S_S5:
                rCustom[3] = temp;
//                *++p = temp;
                state = S_S6;
                break;
            case S_S6:
                rCustom[4] = temp;
//                *++p = temp;
                state = S_S7;
                break;
            case S_S7:
                rCustom[5] = temp;
//                *++p = temp;
                state = S_S8;
                break;
            case S_S8:
                rCustom[6] = temp;
//                *++p = temp;
                state = S_S9;
                break;
            case S_S9:
                rCustom[7] = temp;
//                *++p = temp;
                lcdSaveCustomChar(rbuf[1]);
                state = S_NORMAL;
                break;
            }
        }
    }

    return 0;
}