<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="pl-pl">
<link rel="self" type="application/atom+xml" href="https://forum.atnel.pl/feed.php?f=30&amp;t=18184&amp;mode" />

<title>ATNEL tech-forum</title>
<link href="https://forum.atnel.pl/index.php" />
<updated>2017-04-18T11:13:39+01:00</updated>

<author><name><![CDATA[ATNEL tech-forum]]></name></author>
<id>https://forum.atnel.pl/feed.php?f=30&amp;t=18184&amp;mode</id>
<entry>
<author><name><![CDATA[resonator]]></name></author>
<updated>2017-04-18T11:13:39+01:00</updated>
<published>2017-04-18T11:13:39+01:00</published>
<id>https://forum.atnel.pl/viewtopic.php?t=18184&amp;p=187348#p187348</id>
<link href="https://forum.atnel.pl/viewtopic.php?t=18184&amp;p=187348#p187348"/>
<title type="html"><![CDATA[Re: Magnetometr hmc5883l dziwne pomiary]]></title>

<content type="html" xml:base="https://forum.atnel.pl/viewtopic.php?t=18184&amp;p=187348#p187348"><![CDATA[
Wszystkie trzy funkcje twi_writeTo() w inicjalizacji zwracają mi błąd &quot;error: address send, nack received&quot;, to znaczy że wysyłam źle dane do slave'a?<br /><br />Edit:<br />Napisałem prosty program do przeszukania adresów okazało się że moduł siedzi pod adresem 13 i teraz funkcje zwracają &quot;succes&quot;    Czas pomęczyć się z odbiorem  <img src="https://forum.atnel.pl/images/smilies/icon_e_biggrin.gif" alt=":D" title="Bardzo szczęśliwy" /><p>Statystyki: Napisane przez <a href="https://forum.atnel.pl/memberlist.php?mode=viewprofile&amp;u=8721">resonator</a> — 18 kwi 2017, o 11:13</p><hr />
]]></content>
</entry>
<entry>
<author><name><![CDATA[resonator]]></name></author>
<updated>2017-04-17T16:24:40+01:00</updated>
<published>2017-04-17T16:24:40+01:00</published>
<id>https://forum.atnel.pl/viewtopic.php?t=18184&amp;p=187318#p187318</id>
<link href="https://forum.atnel.pl/viewtopic.php?t=18184&amp;p=187318#p187318"/>
<title type="html"><![CDATA[Magnetometr hmc5883l dziwne pomiary]]></title>

<content type="html" xml:base="https://forum.atnel.pl/viewtopic.php?t=18184&amp;p=187318#p187318"><![CDATA[
Witajcie,<br />mam problem z odczytaniem jakichkolwiek sensownych wartości z tego układu. Niby odczytuje x,z,y jednak latają one jak szalone. Z tego co wywnioskowałem z datesheet powinny być to liczby z zakresu -2048 do 2047, zależne od pochylenia wzdłuż każdej osi, na filmiku widać jak pomiary pływają mimo tego że układ był cały czas nieruchomy. <br /><br />Konfiguracja:<br />[syntax=c]//average 8 samples per measurement output<br />//data output rate 15Hz<br />array&#91;0&#93; = 0x00;<br />array&#91;1&#93; = 0x70;<br />twi_writeTo(0x3C, array, 2);<br /><br />//default gain 1.3Ga<br />array&#91;0&#93; = 0x01;<br />array&#91;1&#93; = 0x20;<br />twi_writeTo(0x3C, array, 2);<br /><br />//continuous-measurement mode<br />array&#91;0&#93; = 0x02;<br />array&#91;1&#93; = 0x00;<br />twi_writeTo(0x3C, array, 2);[/syntax]<br /><br />Pobranie odczytów:<br />[syntax=c]int16_t raw_x = 0;<br />int16_t raw_y = 0;<br />int16_t raw_z = 0;<br /><br />void getHeading(void){<br /><br />uint8_t array&#91;1&#93;;<br />array&#91;0&#93; = 0x03;<br />twi_writeTo(0x3C, array, 1);<br /><br />uint8_t data&#91;6&#93;;<br />twi_readFrom(0x3D, data, 6);<br /><br />raw_x = data&#91;0&#93; &lt;&lt; 8;<br />raw_x |= data&#91;1&#93;;<br /><br />raw_z = data&#91;2&#93; &lt;&lt; 8;<br />raw_z |= data&#91;3&#93;;<br /><br />raw_y = data&#91;4&#93; &lt;&lt; 8;<br />raw_y |= data&#91;5&#93;;<br /><br />        //sendCommand odpowiada za wysyłanie do wyświetlacza<br />sendCommand(10, raw_x);<br />sendCommand(11, raw_z);<br />sendCommand(12, raw_y);<br />}[/syntax]<br /><br />getHeading() wywołuje 10 razy na sekundę.<br /><br />Biblioteka:<br />[syntax=c]/*<br />  twi.c - TWI/I2C library for Wiring &amp; Arduino<br />  Copyright (c) 2006 Nicholas Zambetti.  All right reserved.<br /><br />  This library is free software; you can redistribute it and/or<br />  modify it under the terms of the GNU Lesser General Public<br />  License as published by the Free Software Foundation; either<br />  version 2.1 of the License, or (at your option) any later version.<br /><br />  This library is distributed in the hope that it will be useful,<br />  but WITHOUT ANY WARRANTY; without even the implied warranty of<br />  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU<br />  Lesser General Public License for more details.<br /><br />  You should have received a copy of the GNU Lesser General Public<br />  License along with this library; if not, write to the Free Software<br />  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA<br />*/<br /><br />#include &lt;avr/io.h&gt;<br />#include &lt;avr/interrupt.h&gt;<br />#include &lt;util/twi.h&gt;<br /><br />#include &quot;twi.h&quot;<br /><br />static volatile uint8_t twi_state;<br />static uint8_t twi_slarw;<br /><br />static void (*twi_onSlaveTransmit)(void);<br />static void (*twi_onSlaveReceive)(uint8_t*, int);<br /><br />static uint8_t twi_masterBuffer&#91;TWI_BUFFER_LENGTH&#93;;<br />static volatile uint8_t twi_masterBufferIndex;<br />static uint8_t twi_masterBufferLength;<br /><br />static uint8_t twi_txBuffer&#91;TWI_BUFFER_LENGTH&#93;;<br />static volatile uint8_t twi_txBufferIndex;<br />static volatile uint8_t twi_txBufferLength;<br /><br />static uint8_t twi_rxBuffer&#91;TWI_BUFFER_LENGTH&#93;;<br />static volatile uint8_t twi_rxBufferIndex;<br /><br />static volatile uint8_t twi_error;<br /><br />/*<br /> * Function twi_init<br /> * Desc     readys twi pins and sets twi bitrate<br /> * Input    none<br /> * Output   none<br /> */<br />void twi_init(void)<br />{<br />  // initialize state<br />  twi_state = TWI_READY;<br /><br />  // initialize twi prescaler and bit rate<br />  TWSR &amp;= ~(_BV(TWPS0) | _BV(TWPS1));<br />  TWBR = (uint8_t)((F_CPU / TWI_FREQ) - 16) / 2;<br /><br />  /* twi bit rate formula from atmega128 manual pg 204<br />  SCL Frequency = CPU Clock Frequency / (16 + (2 * TWBR))<br />  note: TWBR should be 10 or higher for master mode<br />  It is 72 for a 16mhz Wiring board with 100kHz TWI */<br /><br />  // enable twi module, acks, and twi interrupt<br />  TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA);<br /><br />  sei();<br />}<br /><br />/*<br /> * Function twi_slaveInit<br /> * Desc     sets slave address and enables interrupt<br /> * Input    none<br /> * Output   none<br /> */<br />void twi_setAddress(uint8_t address)<br />{<br />  // set twi slave address (skip over TWGCE bit)<br />  TWAR = address &lt;&lt; 1;<br />}<br /><br />/*<br /> * Function twi_readFrom<br /> * Desc     attempts to become twi bus master and read a<br /> *          series of bytes from a device on the bus<br /> * Input    address: 7bit i2c device address<br /> *          data: pointer to byte array<br /> *          length: number of bytes to read into array<br /> * Output   number of bytes read<br /> */<br />uint8_t twi_readFrom(uint8_t address, uint8_t* data, uint8_t length)<br />{<br />  uint8_t i;<br /><br />  // ensure data will fit into buffer<br />  if(TWI_BUFFER_LENGTH &lt; length) {<br />    return 0;<br />  }<br /><br />  // wait until twi is ready, become master receiver<br />  while(TWI_READY != twi_state);<br /><br />  twi_state = TWI_MRX;<br /><br />  // reset error state (0xFF.. no error occured)<br />  twi_error = 0xFF;<br /><br />  // initialize buffer iteration vars<br />  twi_masterBufferIndex = 0;<br />  twi_masterBufferLength = length-1;  // This is not intuitive, read on...<br />  // On receive, the previously configured ACK/NACK setting is transmitted in<br />  // response to the received byte before the interrupt is signalled.<br />  // Therefor we must actually set NACK when the _next_ to last byte is<br />  // received, causing that NACK to be sent in response to receiving the last<br />  // expected byte of data.<br /><br />  // build sla+w, slave device address + w bit<br />  twi_slarw = TW_READ;<br />  twi_slarw |= address &lt;&lt; 1;<br /><br />  // send start condition<br />  TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT) | _BV(TWSTA);<br /><br />  // wait for read operation to complete<br />  while(TWI_MRX == twi_state);<br /><br />  if (twi_masterBufferIndex &lt; length)<br />    length = twi_masterBufferIndex;<br /><br />  // copy twi buffer to data<br />  for(i = 0; i &lt; length; ++i) {<br />    data&#91;i&#93; = twi_masterBuffer&#91;i&#93;;<br />  }<br /><br />  return length;<br />}<br /><br />/*<br /> * Function twi_writeTo<br /> * Desc     attempts to become twi bus master and write a<br /> *          series of bytes to a device on the bus<br /> * Input    address: 7bit i2c device address<br /> *          data: pointer to byte array<br /> *          length: number of bytes in array<br /> *          wait: boolean indicating to wait for write or not<br /> * Output   0 .. success<br /> *          1 .. length to long for buffer<br /> *          2 .. address send, NACK received<br /> *          3 .. data send, NACK received<br /> *          4 .. other twi error (lost bus arbitration, bus error, ..)<br /> */<br />uint8_t twi_writeTo(uint8_t address, uint8_t* data, uint8_t length)<br />{<br />  uint8_t i;<br /><br />  // ensure data will fit into buffer<br />  if(TWI_BUFFER_LENGTH &lt; length) {<br />    return 1;<br />  }<br /><br />  // wait until twi is ready, become master transmitter<br />  while(TWI_READY != twi_state);<br /><br />  twi_state = TWI_MTX;<br /><br />  // reset error state (0xFF.. no error occured)<br />  twi_error = 0xFF;<br /><br />  // initialize buffer iteration vars<br />  twi_masterBufferIndex = 0;<br />  twi_masterBufferLength = length;<br /><br />  // copy data to twi buffer<br />  for(i = 0; i &lt; length; ++i) {<br />    twi_masterBuffer&#91;i&#93; = data&#91;i&#93;;<br />  }<br /><br />  // build sla+w, slave device address + w bit<br />  twi_slarw = TW_WRITE;<br />  twi_slarw |= address &lt;&lt; 1;<br /><br />  // send start condition<br />  TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT) | _BV(TWSTA);<br /><br />  // wait for write operation to complete<br />  while(TWI_MTX == twi_state);<br /><br />  if (twi_error == 0xFF)<br />    return 0;   // success<br />  else if (twi_error == TW_MT_SLA_NACK)<br />    return 2;   // error: address send, nack received<br />  else if (twi_error == TW_MT_DATA_NACK)<br />    return 3;   // error: data send, nack received<br />  else<br />    return 4;   // other twi error<br />}<br /><br />/*<br /> * Function twi_transmit<br /> * Desc     fills slave tx buffer with data<br /> *          must be called in slave tx event callback<br /> * Input    data: pointer to byte array<br /> *          length: number of bytes in array<br /> * Output   1 length too long for buffer<br /> *          2 not slave transmitter<br /> *          0 ok<br /> */<br />uint8_t twi_transmit(uint8_t* data, uint8_t length)<br />{<br />  uint8_t i;<br /><br />  // ensure data will fit into buffer<br />  if(TWI_BUFFER_LENGTH &lt; length) {<br />    return 1;<br />  }<br /><br />  // ensure we are currently a slave transmitter<br />  if(TWI_STX != twi_state) {<br />    return 2;<br />  }<br /><br />  // set length and copy data into tx buffer<br />  twi_txBufferLength = length;<br />  for(i = 0; i &lt; length; ++i) {<br />    twi_txBuffer&#91;i&#93; = data&#91;i&#93;;<br />  }<br /><br />  return 0;<br />}<br /><br />/*<br /> * Function twi_attachSlaveRxEvent<br /> * Desc     sets function called before a slave read operation<br /> * Input    function: callback function to use<br /> * Output   none<br /> */<br />void twi_attachSlaveRxEvent( void (*function)(uint8_t*, int) )<br />{<br />  twi_onSlaveReceive = function;<br />}<br /><br />/*<br /> * Function twi_attachSlaveTxEvent<br /> * Desc     sets function called before a slave write operation<br /> * Input    function: callback function to use<br /> * Output   none<br /> */<br />void twi_attachSlaveTxEvent( void (*function)(void) )<br />{<br />  twi_onSlaveTransmit = function;<br />}<br /><br />/*<br /> * Function twi_reply<br /> * Desc     sends byte or readys receive line<br /> * Input    ack: byte indicating to ack or to nack<br /> * Output   none<br /> */<br />void twi_reply(uint8_t ack)<br />{<br />  // transmit master read ready signal, with or without ack<br />  if(ack) {<br />    TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWINT) | _BV(TWEA);<br />  } else {<br />    TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWINT);<br />  }<br />}<br /><br />/*<br /> * Function twi_stop<br /> * Desc     relinquishes bus master status<br /> * Input    none<br /> * Output   none<br /> */<br />void twi_stop(void)<br />{<br />  // send stop condition<br />  TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT) | _BV(TWSTO);<br /><br />  // wait for stop condition to be exectued on bus<br />  // TWINT is not set after a stop condition!<br />  while(TWCR &amp; _BV(TWSTO));<br /><br />  // update twi state<br />  twi_state = TWI_READY;<br />}<br /><br />/*<br /> * Function twi_releaseBus<br /> * Desc     releases bus control<br /> * Input    none<br /> * Output   none<br /> */<br />void twi_releaseBus(void)<br />{<br />  // release bus<br />  TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT);<br /><br />  // update twi state<br />  twi_state = TWI_READY;<br />}<br /><br />SIGNAL(TWI_vect)<br />{<br />  switch(TW_STATUS){<br />//================= All Master =================//<br />    case TW_START:     // sent start condition<br />    case TW_REP_START: // sent repeated start condition<br />      // copy device address and r/w bit to output register and ack<br />      TWDR = twi_slarw;<br />      twi_reply(ACK);<br />      break;<br /><br />//================= Master Transmitter =================//<br />    case TW_MT_SLA_ACK:  // slave receiver acked address<br />    case TW_MT_DATA_ACK: // slave receiver acked data<br />      // if there is data to send, send it, otherwise stop<br />      if(twi_masterBufferIndex &lt; twi_masterBufferLength) {<br />        // copy data to output register and ack<br />        TWDR = twi_masterBuffer&#91;twi_masterBufferIndex++&#93;;<br />        twi_reply(ACK);<br />      } else {<br />        twi_stop();<br />      }<br />      break;<br /><br />    case TW_MT_SLA_NACK:  // address sent, nack received<br />      twi_error = TW_MT_SLA_NACK;<br />      twi_stop();<br />      break;<br /><br />    case TW_MT_DATA_NACK: // data sent, nack received<br />      twi_error = TW_MT_DATA_NACK;<br />      twi_stop();<br />      break;<br /><br />    case TW_MT_ARB_LOST: // lost bus arbitration<br />      twi_error = TW_MT_ARB_LOST;<br />      twi_releaseBus();<br />      break;<br /><br />//================= Master Receiver =================//<br />    case TW_MR_DATA_ACK: // data received, ack sent<br />      // put byte into buffer<br />      twi_masterBuffer&#91;twi_masterBufferIndex++&#93; = TWDR;<br />      /* no break */<br />    case TW_MR_SLA_ACK:  // address sent, ack received<br />      // ack if more bytes are expected, otherwise nack<br />      if(twi_masterBufferIndex &lt; twi_masterBufferLength){<br />        twi_reply(ACK);<br />      }else{<br />        twi_reply(NOACK);<br />      }<br />      break;<br /><br />    case TW_MR_DATA_NACK: // data received, nack sent<br />      // put final byte into buffer<br />      twi_masterBuffer&#91;twi_masterBufferIndex++&#93; = TWDR;<br />      /* no break */<br />    case TW_MR_SLA_NACK: // address sent, nack received<br />      twi_stop();<br />      break;<br />    // TW_MR_ARB_LOST handled by TW_MT_ARB_LOST case<br /><br />//================= Slave Receiver =================//<br />    case TW_SR_SLA_ACK:   // addressed, returned ack<br />    case TW_SR_GCALL_ACK: // addressed generally, returned ack<br />    case TW_SR_ARB_LOST_SLA_ACK:   // lost arbitration, returned ack<br />    case TW_SR_ARB_LOST_GCALL_ACK: // lost arbitration, returned ack<br />      // enter slave receiver mode<br />      twi_state = TWI_SRX;<br />      // indicate that rx buffer can be overwritten and ack<br />      twi_rxBufferIndex = 0;<br />      twi_reply(ACK);<br />      break;<br /><br />    case TW_SR_DATA_ACK:       // data received, returned ack<br />    case TW_SR_GCALL_DATA_ACK: // data received generally, returned ack<br />      // if there is still room in the rx buffer<br />      if(twi_rxBufferIndex &lt; TWI_BUFFER_LENGTH) {<br />        // put byte in buffer and ack<br />        twi_rxBuffer&#91;twi_rxBufferIndex++&#93; = TWDR;<br />        twi_reply(ACK);<br />      } else {<br />        // otherwise nack<br />        twi_reply(NOACK);<br />      }<br />      break;<br /><br />    case TW_SR_STOP: // stop or repeated start condition received<br />      // put a null char after data if there's room<br />      if(twi_rxBufferIndex &lt; TWI_BUFFER_LENGTH){<br />        twi_rxBuffer&#91;twi_rxBufferIndex&#93; = '\0';<br />      }<br />      // sends ack and stops interface for clock stretching<br />      twi_stop();<br />      // callback to user defined callback<br />      twi_onSlaveReceive(twi_rxBuffer, twi_rxBufferIndex);<br />      // since we submit rx buffer to &quot;wire&quot; library, we can reset it<br />      twi_rxBufferIndex = 0;<br />      // ack future responses and leave slave receiver state<br />      twi_releaseBus();<br />      break;<br /><br />    case TW_SR_DATA_NACK:       // data received, returned nack<br />    case TW_SR_GCALL_DATA_NACK: // data received generally, returned nack<br />      // nack back at master<br />      twi_reply(NOACK);<br />      break;<br /><br />//================= Slave Transmitter =================//<br />    case TW_ST_SLA_ACK:          // addressed, returned ack<br />    case TW_ST_ARB_LOST_SLA_ACK: // arbitration lost, returned ack<br />      // enter slave transmitter mode<br />      twi_state = TWI_STX;<br />      // ready the tx buffer index for iteration<br />      twi_txBufferIndex = 0;<br />      // set tx buffer length to be zero, to verify if user changes it<br />      twi_txBufferLength = 0;<br />      // request for txBuffer to be filled and length to be set<br />      // note: user must call twi_transmit(bytes, length) to do this<br />      twi_onSlaveTransmit();<br />      // if they didn't change buffer &amp; length, initialize it<br />      if(0 == twi_txBufferLength) {<br />        twi_txBufferLength = 1;<br />        twi_txBuffer&#91;0&#93; = 0x00;<br />      }<br />      // transmit first byte from buffer, fall<br />      /* no break */<br />    case TW_ST_DATA_ACK: // byte sent, ack returned<br />      // copy data to output register<br />      TWDR = twi_txBuffer&#91;twi_txBufferIndex++&#93;;<br />      // if there is more to send, ack, otherwise nack<br />      if(twi_txBufferIndex &lt; twi_txBufferLength){<br />        twi_reply(ACK);<br />      }else{<br />        twi_reply(NOACK);<br />      }<br />      break;<br /><br />    case TW_ST_DATA_NACK: // received nack, we are done<br />    case TW_ST_LAST_DATA: // received ack, but we are done already!<br />      // ack future responses<br />      twi_reply(ACK);<br />      // leave slave receiver state<br />      twi_state = TWI_READY;<br />      break;<br /><br />//================= All =================//<br />    case TW_NO_INFO:   // no state information<br />      break;<br /><br />    case TW_BUS_ERROR: // bus error, illegal stop/start<br />      twi_error = TW_BUS_ERROR;<br />      twi_stop();<br />      break;<br />  }<br />}[/syntax]<br />[syntax=c]/*<br />  twi.h - TWI/I2C library for Wiring &amp; Arduino<br />  Copyright (c) 2006 Nicholas Zambetti.  All right reserved.<br /><br />  This library is free software; you can redistribute it and/or<br />  modify it under the terms of the GNU Lesser General Public<br />  License as published by the Free Software Foundation; either<br />  version 2.1 of the License, or (at your option) any later version.<br /><br />  This library is distributed in the hope that it will be useful,<br />  but WITHOUT ANY WARRANTY; without even the implied warranty of<br />  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU<br />  Lesser General Public License for more details.<br /><br />  You should have received a copy of the GNU Lesser General Public<br />  License along with this library; if not, write to the Free Software<br />  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA<br />*/<br /><br />#ifndef TWI_H_<br />#define TWI_H_<br /><br />#include &lt;inttypes.h&gt;<br /><br />#ifdef __cplusplus<br />extern &quot;C&quot; {<br />#endif<br /><br />#define TWI_FREQ 100000UL<br /><br />#define TWI_BUFFER_LENGTH 32<br /><br />#define TWI_READY 0<br />#define TWI_MRX   1<br />#define TWI_MTX   2<br />#define TWI_SRX   3<br />#define TWI_STX   4<br /><br />#define ACK     1<br />#define NOACK   0<br /><br />void twi_init(void);<br />void twi_setAddress(uint8_t);<br />uint8_t twi_readFrom(uint8_t, uint8_t*, uint8_t);<br />uint8_t twi_writeTo(uint8_t, uint8_t*, uint8_t);<br />uint8_t twi_transmit(uint8_t*, uint8_t);<br />void twi_attachSlaveRxEvent( void (*)(uint8_t*, int) );<br />void twi_attachSlaveTxEvent( void (*)(void) );<br />void twi_reply(uint8_t);<br />void twi_stop(void);<br />void twi_releaseBus(void);<br /><br />#ifdef __cplusplus<br />}<br />#endif<br /><br />#endif[/syntax]<br /><br /><!-- m --><a class="postlink" href="https://drive.google.com/file/d/0B0GMIRRogS7aU0QwYXRaYkhLTGM/view?usp=sharing" >https://drive.google.com/file/d/0B0GMIR ... sp=sharing</a><!-- m --><p>Statystyki: Napisane przez <a href="https://forum.atnel.pl/memberlist.php?mode=viewprofile&amp;u=8721">resonator</a> — 17 kwi 2017, o 16:24</p><hr />
]]></content>
</entry>
</feed>