## c't-Projekte - Mailinglisten

[Voriger (Datum)] [Nächster (Datum)] [Voriger (Thread)] [Nächster (Thread)]
[Nach Datum][Nach Thread]

### AW: [ct-bot] Problem mit Pointern

Absender: Carsten Giesen
Datum: Sa, 01.04.2006 21:39:19

    Hallo Torsten,

    Danke erstmal.

    Ok, ich glaube da habe ich nicht gut genug gefragt.

    Hier definire ich ein Dataset (so heißt das glaube ich)

    void srf10_get_messure()
    {
    	char temp0[1];
    	char temp1;

    	temp1 = 0;

    	char state;
    	tx_type tx_frame[3];

    	srf10_ping(SRF10_CENTIMETERS);

    	state = SUCCESS;

    	tx_frame[0].slave_adr = SRF10_UNIT_0+W;
    	tx_frame[0].size = 1;
    	tx_frame[0].data_ptr = temp0;
    	tx_frame[0].data_ptr[0] = 2;

    	tx_frame[1].slave_adr = SRF10_UNIT_0+R;
    	tx_frame[1].size = 1;
    	tx_frame[1].data_ptr = &temp1;

    	tx_frame[2].slave_adr = OWN_ADR;

    	state = Send_to_TWI(tx_frame);

    	Close_TWI();

    	srf10_m = temp1;

    return;
    }

    Dieses wird an diese Routine übergeben:

    unsigned char Send_to_TWI(tx_type *data_pack)
    {
    	unsigned char state,i,j;

    	state = SUCCESS;

    	for(i=0;(data_pack[i].slave_adr != OWN_ADR)&&(state == SUCCESS);i++)
    	{
    		state = Send_start();				//Send START
    and repeated START
    		if (state == SUCCESS)
    			state = Send_adr(data_pack[i].slave_adr);//Send
    slave address+W/R

    		/*Dependent on the R/W in the slave address it will receive
    or
    		transmitt data.*/
    		if(!(data_pack[i].slave_adr & R))
    		{
    			/*If W, it will transmitt data and loops until all
    data have been
    			 shifted out*/
    			for(j=0;((j<data_pack[i].size)&&(state ==
    SUCCESS));j++)
    				state = Send_byte(data_pack[i].data_ptr[j]);
    		}
    		else
    //Else read from slave
    		{
    			/*If R, it will receive data and loops until all
    data have been
    			 shifted in. When j == 0x00 this is the last byte
    and a NACK
    			 will be sent to the slave to make it stop
    transmitting */
    			for(j=data_pack[i].size;((j>0)&&(state ==
    SUCCESS));j--)
    				state = Get_byte(data_pack[i].data_ptr++,j);

    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    		}
    	}
      	Send_stop();
    //Send STOP

    	return state;
    //Return SUCCESS if OK,

    //else return TWSR
    }

    Und jetzt der Teil wo ich den Sensor auslesen will:

    unsigned char Get_byte(unsigned char *rx_ptr,char last_byte)
    {
    	Wait_TWI_int();
    //Wait for TWI interrupt flag set

    	/*When receiving the last byte from the slave it will be sent a NACK
    to
    	make the slave stop transmitting, all bits before the last will get
    	a ACK*/
    	if(last_byte)
    //Not the last byte
    		//Clear int flag and enable acknowledge to receive data.
    		TWCR = ((1<<TWINT)+(1<<TWEA)+(1<<TWEN));
    	else
    //Last byte
    		/*Clear int flag to and do not enable acknowledge to tell
    the slave
    		to stop transmitting*/
    		TWCR = ((1<<TWINT)+(1<<TWEN));
    	Wait_TWI_int();
    //Wait for TWI interrupt flag set

    	*rx_ptr = TWDR;
    //Save received byte

    	/*If ACK has been transmitted or this was the last byte and NACK has
    been
    	sent -> return SUCCESS, else return TWSR*/
     	if(((TWSR == MRX_DATA_NACK)&&(last_byte == 0))||(TWSR ==
    MRX_DATA_ACK))
    		return SUCCESS;
    	return TWSR;
    }

    Jetzt ist die ganze Kette zusammen

    Zu Schull sollte in der Var "srf10_m" mein gemessener Wert stehen.
    Tut es aber nicht.

- Antworten:
  - Re: AW: [ct-bot] Problem mit Pointern
    - From: Torsten Evers
  - RE: [ct-bot] Problem mit Pointern
    - From: Timo Sandmann

- Voriger (Datum): Re: [ct-bot] Problem mit Pointern, Torsten Evers
- Nächster (Datum): Re: AW: [ct-bot] Problem mit Pointern, Torsten Evers
- Voriger (Thread): Re: [ct-bot] Problem mit Pointern, Torsten Evers
- Nächster (Thread): Re: AW: [ct-bot] Problem mit Pointern, Torsten Evers
- Indizes:
  - Datum
  - Thread