<?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=4&amp;t=21269&amp;mode" />

<title>ATNEL tech-forum</title>
<link href="https://forum.atnel.pl/index.php" />
<updated>2018-09-15T01:13:29+01:00</updated>

<author><name><![CDATA[ATNEL tech-forum]]></name></author>
<id>https://forum.atnel.pl/feed.php?f=4&amp;t=21269&amp;mode</id>
<entry>
<author><name><![CDATA[weles]]></name></author>
<updated>2018-09-15T01:13:29+01:00</updated>
<published>2018-09-15T01:13:29+01:00</published>
<id>https://forum.atnel.pl/viewtopic.php?t=21269&amp;p=211678#p211678</id>
<link href="https://forum.atnel.pl/viewtopic.php?t=21269&amp;p=211678#p211678"/>
<title type="html"><![CDATA[Re: Atmega 2560 , HC-SR04 Konfiguracja timera]]></title>

<content type="html" xml:base="https://forum.atnel.pl/viewtopic.php?t=21269&amp;p=211678#p211678"><![CDATA[
Dziękuję za pomoc. Dzięki przykładowi kolegi Andrews zmodyfikowałem ( i zrozumiałem) działanie własnego kodu i używając Timera5 ,umożliwiłem obsługę drugiego modułu HC.<p>Statystyki: Napisane przez <a href="https://forum.atnel.pl/memberlist.php?mode=viewprofile&amp;u=14692">weles</a> — 15 wrz 2018, o 01:13</p><hr />
]]></content>
</entry>
<entry>
<author><name><![CDATA[andrews]]></name></author>
<updated>2018-09-14T18:30:49+01:00</updated>
<published>2018-09-14T18:30:49+01:00</published>
<id>https://forum.atnel.pl/viewtopic.php?t=21269&amp;p=211675#p211675</id>
<link href="https://forum.atnel.pl/viewtopic.php?t=21269&amp;p=211675#p211675"/>
<title type="html"><![CDATA[Re: Atmega 2560 , HC-SR04 Konfiguracja timera]]></title>

<content type="html" xml:base="https://forum.atnel.pl/viewtopic.php?t=21269&amp;p=211675#p211675"><![CDATA[
Przeczytaj uważnie komentarze na początku kodu:[syntax=c]// kod dla F_CPU=14745600<br />// połączenia:<br />//      ECHO1     =  PE7<br />//      TRIGGER1  =  PE4<br />//      ECHO2     =  PL0<br />//      TRIGGER2  =  PH4<br />// sygnał TRIGGER generowany sprzętowo<br />// wynik obliczany w milimetrach<br />// obliczenia uwzględniają prawidłowe<br />// zaokrąglanie wyniku<br /><br />#define FACTOR1         800000ULL<br />#define FACTOR2         8552448ULL<br /><br />#define TRIGGER1_DDR     DDRE<br />#define TRIGGER1_BIT     PE4<br /><br />#define TRIGGER2_DDR     DDRH<br />#define TRIGGER2_BIT     PH4<br /><br />volatile uint16_t echo1_ticks, echo2_ticks;<br />volatile uint8_t echo1_received, echo2_received;<br />uint16_t distance1, distance2;<br /><br />int main(void)<br />{<br />    // ustawienie pinu OC3B (PE4) jako wyjście<br />    TRIGGER1_DDR |= (1&lt;&lt;TRIGGER1_BIT);<br />    // TIMER 3 - wartość TOP generatora przebiegu<br />    OCR3A = 0xFFFF;<br />    // ustawienie czasu trwania impulsu TRIGGER1<br />    OCR3B = 10;<br />    // TIMER 3 - ustawienie trybu WGM na 'Phase Correct',<br />    // trybu pracy pinu OC3B na &quot;clear when upcounting&quot;,<br />    // włączenie taktowania z preskalerem 8,<br />    // ustawienie reakcji ICP3 na zbocze narastające<br />    TCCR3A = (1&lt;&lt;COM3B1) | (1&lt;&lt;WGM31) | (1&lt;&lt;WGM30);<br />    TCCR3B = (1&lt;&lt;ICES3) | (1&lt;&lt;WGM33) | (1&lt;&lt;CS31);<br />    // zezwolenie na przerwanie Input Capture 3<br />    TIMSK3 = (1&lt;&lt;ICIE3);<br />    <br />    // ustawienie pinu OC4B (PH4) jako wyjście<br />    TRIGGER2_DDR |= (1&lt;&lt;TRIGGER2_BIT);<br />    // przesunięcie względem poprzedniego timera,<br />    // aby pomiar nie występował w tym samym czasie<br />    TCNT4 = 0xFFF0;<br />    // TIMER 4 - wartość TOP generatora przebiegu<br />    OCR4A = 0xFFFF;<br />    // ustawienie czasu trwania impulsu TRIGGER2<br />    OCR4B = 10;<br />    // TIMER 4 - ustawienie trybu WGM na 'Phase Correct',<br />    // trybu pracy pinu OC4B na &quot;clear when upcounting&quot;,<br />    // włączenie taktowania z preskalerem 8,<br />    // ustawienie reakcji ICP4 na zbocze narastające<br />    TCCR4A = (1&lt;&lt;COM4B1) | (1&lt;&lt;WGM41) | (1&lt;&lt;WGM40);<br />    TCCR4B = (1&lt;&lt;ICES4) | (1&lt;&lt;WGM43) | (1&lt;&lt;CS41);<br />    // zezwolenie na przerwanie Input Capture 4<br />    TIMSK4 = (1&lt;&lt;ICIE4);<br />    <br />    sei();<br />    <br />    while (1) <br />    {<br />        if (echo1_received)<br />        {<br />            distance1 = (uint16_t)( (echo1_ticks * FACTOR1 + FACTOR2 / 2) / FACTOR2 );<br />            echo1_received = 0;<br />            // ewentualne wyświetlenie wyniku<br />        }<br />        PORTL = distance1;<br />        if (echo2_received)<br />        {<br />            distance2 = (uint16_t)( (echo2_ticks * FACTOR1 + FACTOR2 / 2) / FACTOR2 );<br />            echo2_received = 0;<br />            // ewentualne wyświetlenie wyniku<br />        }<br />    }<br />}<br /><br /><br />ISR(TIMER3_CAPT_vect)<br />{<br />    static uint16_t prevICR3 = 0;<br />    if ( !(TCCR3B &amp; (1&lt;&lt;ICES3)) )<br />    {<br />        echo1_ticks = ICR3 - prevICR3;<br />        echo1_received = 1;<br />    }<br />    prevICR3 = ICR3;<br />    TCCR3B ^= (1&lt;&lt;ICES3);<br />}<br /><br />ISR(TIMER4_CAPT_vect)<br />{<br />    static uint16_t prevICR4 = 0;<br />    if ( !(TCCR4B &amp; (1&lt;&lt;ICES4)) )<br />    {<br />        echo2_ticks = ICR4 - prevICR4;<br />        echo2_received = 1;<br />    }<br />    prevICR4 = ICR4;<br />    TCCR4B ^= (1&lt;&lt;ICES4);<br />}[/syntax]<p>Statystyki: Napisane przez <a href="https://forum.atnel.pl/memberlist.php?mode=viewprofile&amp;u=14165">andrews</a> — 14 wrz 2018, o 18:30</p><hr />
]]></content>
</entry>
<entry>
<author><name><![CDATA[weles]]></name></author>
<updated>2018-09-14T14:04:29+01:00</updated>
<published>2018-09-14T14:04:29+01:00</published>
<id>https://forum.atnel.pl/viewtopic.php?t=21269&amp;p=211668#p211668</id>
<link href="https://forum.atnel.pl/viewtopic.php?t=21269&amp;p=211668#p211668"/>
<title type="html"><![CDATA[Re: Atmega 2560 , HC-SR04 Konfiguracja timera]]></title>

<content type="html" xml:base="https://forum.atnel.pl/viewtopic.php?t=21269&amp;p=211668#p211668"><![CDATA[
Na prawdę nikt,nic? Może ktoś z kolegów dysponuje jakimś przykładowym kodem na tą atmegę?<p>Statystyki: Napisane przez <a href="https://forum.atnel.pl/memberlist.php?mode=viewprofile&amp;u=14692">weles</a> — 14 wrz 2018, o 14:04</p><hr />
]]></content>
</entry>
<entry>
<author><name><![CDATA[weles]]></name></author>
<updated>2018-09-13T20:13:48+01:00</updated>
<published>2018-09-13T20:13:48+01:00</published>
<id>https://forum.atnel.pl/viewtopic.php?t=21269&amp;p=211654#p211654</id>
<link href="https://forum.atnel.pl/viewtopic.php?t=21269&amp;p=211654#p211654"/>
<title type="html"><![CDATA[Re: Atmega 2560 , HC-SR04 Konfiguracja timera]]></title>

<content type="html" xml:base="https://forum.atnel.pl/viewtopic.php?t=21269&amp;p=211654#p211654"><![CDATA[
No dobrze. Udało mi się odpowiednio skonfigurować wszystko aby odległość mierzona była poprawna. <br />Wygląda to tak:<br /><br />[syntax=c]void init_timer4(void)<br />{<br /><br />  TCCR4A |= (1 &lt;&lt; WGM42)|(1 &lt;&lt; WGM43);<br /><br />  TCCR4B |= 1 &lt;&lt; CS42 ; // prescaler 256<br /><br />  TIMSK4 |= 1 &lt;&lt; OCIE4A | 1 &lt;&lt; ICIE4;<br /><br />  TCCR4B |= 1 &lt;&lt; ICES4; // Input capture on rising edge<br /><br />   OCR4A = 4031;// PRESCALER 256 - 70ms = 4031  80ms = 4607<br /><br /><br />  // 70ms cycle: 14745600 / 256 = 57600 counts/second =&gt; 57600/10 = 5760 counts/100ms =&gt; 5760/100*70 = 4032 counts / 70ms<br /><br /><br />  //us_per_count = 164; // 14745600/256 = 57600 counts/second =&gt; 14745600/57600<br />  us_per_count =164 ;<br />}[/syntax]<br /><br /><br />[syntax=c]ISR (TIMER4_CAPT_vect)<br />{<br /><br />  if (TCCR4B &amp; (1&lt;&lt;ICES4)) // On rising edge<br />  {<br />    TCCR4B &amp;= ~(1&lt;&lt;ICES4); // Next time detect falling edge<br />    rising = ICR4; // Save current count<br />  }<br />  else // On falling edge<br />  {<br />    TCCR4B |= (1&lt;&lt;ICES4); // Next time detect falling edge<br />    falling = ICR4; // Save current count<br />    counts = (uint32_t)falling - (uint32_t)rising;<br />    dist = (uint32_t)us_per_count * counts / 58; // useconds / 58 to get distance in cm<br />  }<br /><br />}//ISR (TIMER4_CAPT_vect)<br /><br /><br /><br /><br />ISR (TIMER4_COMPA_vect)<br />{<br />TRIG1_PORT |= HC_TRIG1;<br />TRIG2_PORT |= HC_TRIG2;<br />_delay_us(10);<br />TRIG1_PORT &amp;= ~HC_TRIG1;<br />TRIG2_PORT &amp;= ~HC_TRIG2;<br />}[/syntax]<br /><br />Tylko teraz muszę jeszcze obsłużyć drugi HC-SR05.<br />Pierwszy czujnik obsługuję z pinu ICP4 (pin 49). Drugi mam podpięte pod ICP5.<br />Proszę o jakąś poradę jak tego dokonać nie rozbudowując niepotrzebnie zbytnio kodu.<br />Z resztą już sobie poradzę.<p>Statystyki: Napisane przez <a href="https://forum.atnel.pl/memberlist.php?mode=viewprofile&amp;u=14692">weles</a> — 13 wrz 2018, o 20:13</p><hr />
]]></content>
</entry>
<entry>
<author><name><![CDATA[weles]]></name></author>
<updated>2018-09-11T22:31:39+01:00</updated>
<published>2018-09-11T22:31:39+01:00</published>
<id>https://forum.atnel.pl/viewtopic.php?t=21269&amp;p=211584#p211584</id>
<link href="https://forum.atnel.pl/viewtopic.php?t=21269&amp;p=211584#p211584"/>
<title type="html"><![CDATA[Re: Atmega 2560 , HC-SR04 Konfiguracja timera]]></title>

<content type="html" xml:base="https://forum.atnel.pl/viewtopic.php?t=21269&amp;p=211584#p211584"><![CDATA[
No właśnie przy takim ustawieniu timera procesor się cyklicznie restartuje.<br /><br />A przy tym kodzie pomiar na jednym czujniku działa ale nie zgadza się odległość. Niby ma być wynik w CM a jest w milimetrach. Ale i tak nieco się różni od rzeczywistego.<br /><br />[syntax=c]void init_timer4()<br />{<br />  /**<br />   * https://s30.postimg.org/ahb62cb01/Wavefrom_Generation_Mode_Bit_Description.jpg<br />   */<br />  TCCR4A |= (1 &lt;&lt; WGM41);<br />   /**<br />   * https://s27.postimg.org/k2tu225pf/Clock_Select_Bit_Description.jpg<br />   **/<br /><br />  TCCR4B |= 1 &lt;&lt; CS42 ; // prescaler 256<br /><br />  TIMSK4 |= 1 &lt;&lt; OCIE4A | 1 &lt;&lt; ICIE4;<br />  /**<br />   *  ICIE: Input Capture Interrupt Enable<br />      When this bit is written to '1', and the I-flag in the Status Register is set (interrupts globally enabled), the<br />      Timer/Counter1 Input Capture interrupt is enabled. The corresponding Interrupt Vector is executed when<br />      the ICF Flag, located in TIFR1, is set.<br /><br />      When a capture is triggered according to the ICESn setting, the counter value is copied into the Input Capture Register<br />      (ICRn). The event will also set the Input Capture Flag (ICFn), and this can be used to cause an Input Capture<br />      Interrupt, if this interrupt is enabled.<br /><br />      When the ICRn is used as TOP value (see description of the WGMn3:0 bits located in the TCCRnA and the<br />      TCCRnB Register), the ICPn is disconnected and consequently the input capture function is disabled.<br />   */<br /><br />  /* ICP4 (Input Capture Interrupt) , DIGITAL PIN 49<br />   *   ISR(TIMER4_CAPT_vect) when the signal on the ICP pin goes from 0 to 1 (raising edge).<br />   *   The method contains a if stament to see if it was called during a raising edge or falling edge.<br />   *   On a raising edge it switches the ICP to falling edge detection and then stores the start time.<br />   *   On a falling edge it switches the ICP to raising edge detection and gets the end time to calculate the distance in millimeters.<br />   */<br />  TCCR4B |= 1 &lt;&lt; ICES4; // Input capture on rising edge<br /><br />  /**<br />   * This bit selects which edge on the Input Capture pin (ICP1) that is used to trigger a capture event. When<br />    the ICES1 bit is written to zero, a falling (negative) edge is used as trigger, and when the ICES1 bit is<br />    written to '1', a rising (positive) edge will trigger the capture.<br />    When a capture is triggered according to the ICES1 setting, the counter value is copied into the Input<br />    Capture Register (ICR1). The event will also set the Input Capture Flag (ICF1), and this can be used to<br />    cause an Input Capture Interrupt, if this interrupt is enabled.<br />    When the ICR1 is used as TOP value (see description of the WGM1&#91;3:0&#93; bits located in the TCCR1A and<br />    the TCCR1B Register), the ICP1 is disconnected and consequently the Input Capture function is<br />    disabled.<br />   */<br /> // OCR4A = 8750;<br />  OCR4A = 4608;<br />  // &quot;we suggest to use over 60ms measurement cycle, in order to prevent trigger signal to the echo signal.&quot;<br />  // source: http://www.robosoftsystems.co.in/wikidocs/index.php?title=Ultrasonic_Sensor_(HC-SR04)<br />  // 70ms cycle: 8MHz/64 = 125000 counts/second =&gt; 125000/10 = 12500 counts/100ms =&gt; 12500/100*70 = 8750 counts / 70ms<br /><br />  // 80ms cycle: 14745600/256 = 57600 counts/second =&gt; 57600/10 = 5760 counts/100ms =&gt; 5760/100*80 = 4608 counts / 80ms<br />  sei();<br />  //us_per_count = 4; // 8MHz / 64 = 125000 counts/second =&gt; 1000000/125000<br />  //us_per_count = 164; // 14745600/256 = 57600 counts/second =&gt; 14745600/57600<br />  us_per_count = 128;<br />}[/syntax]<br /><br /><br />[syntax=c]ISR (TIMER4_CAPT_vect)<br />{<br />  /*  Input Capture Unit<br />   * https://s23.postimg.org/gumgo0sln/Input_Capture_Unit.jpg<br />   */<br />  if (TCCR4B &amp; (1&lt;&lt;ICES4)) // On rising edge<br />  {<br />    TCCR4B &amp;= ~(1&lt;&lt;ICES4); // Next time detect falling edge<br />    rising = ICR4; // Save current count<br />  }<br />  else // On falling edge<br />  {<br />    TCCR4B |= (1&lt;&lt;ICES4); // Next time detect falling edge<br />    falling = ICR4; // Save current count<br />    counts = (uint32_t)falling - (uint32_t)rising;<br />    dist = (uint32_t)us_per_count * counts / 58; // useconds / 58 to get distance in cm<br /><br />  }<br /><br />}//ISR (TIMER4_CAPT_vect)<br /><br /><br /><br /><br />ISR (TIMER4_COMPA_vect)<br />{<br />TRIG1_PORT |= HC_TRIG1;<br />_delay_us(12);<br />TRIG1_PORT &amp;= ~HC_TRIG1;<br />}[/syntax]<br /><br /><br />Według metody prób i błędów problem w dużej mierze tkwi tutaj:<br /><br />[syntax=c]OCR4A = 4608;[/syntax]<br /><br />Liczba 4608 wychodzi mi z obliczenia cyklu 80ms dla odczytu.<br /><br />     80ms cycle: 14745600/256 = 57600 counts/second =&gt; 57600/10 = 5760 counts/100ms =&gt; 5760/100*80 = 4608 counts / 80ms<br /><br />Producent zaleca powyżej 60ms. <br /><br />Dodatkowo kolejny błąd tkwi tutaj. Ma to kluczowe znaczenie przy końcowym wyliczeniu odległości.<br />Teoretycznie powinno to wyglądać tak:<br /><br />   us_per_count = 128; // 14745600/256 = 57600 counts/second =&gt; 14745600/57600<br /><br />Ale w miarę zbliżony do prawdziwego wyniku pomiaru odległości muszę wprowadzić wartość<br /><br />   us_per_count = 164;<p>Statystyki: Napisane przez <a href="https://forum.atnel.pl/memberlist.php?mode=viewprofile&amp;u=14692">weles</a> — 11 wrz 2018, o 22:31</p><hr />
]]></content>
</entry>
<entry>
<author><name><![CDATA[mczarny]]></name></author>
<updated>2018-09-11T22:17:58+01:00</updated>
<published>2018-09-11T22:17:58+01:00</published>
<id>https://forum.atnel.pl/viewtopic.php?t=21269&amp;p=211583#p211583</id>
<link href="https://forum.atnel.pl/viewtopic.php?t=21269&amp;p=211583#p211583"/>
<title type="html"><![CDATA[Re: Atmega 2560 , HC-SR04 Konfiguracja timera]]></title>

<content type="html" xml:base="https://forum.atnel.pl/viewtopic.php?t=21269&amp;p=211583#p211583"><![CDATA[
Strasznie mieszasz Kolego<br />nie wiem skąd wziąłeś to<br />[syntax=c]TCCR4B |= 1 &lt;&lt; ICES5;[/syntax]<br />mieszasz rejestry i numery timerów<br />dla timera4 powinno być<br />[syntax=c]TCCR4B |=(1&lt;&lt;ICES4);<br />TCCR4B |=(1&lt;&lt;CS41);<br />TIMSK4 |=(1&lt;&lt;TOIE4);[/syntax]<br />i spróbuj z jednym czujnikiem a jak zadziała to pójdziemy dalej<p>Statystyki: Napisane przez <a href="https://forum.atnel.pl/memberlist.php?mode=viewprofile&amp;u=4651">mczarny</a> — 11 wrz 2018, o 22:17</p><hr />
]]></content>
</entry>
<entry>
<author><name><![CDATA[weles]]></name></author>
<updated>2018-09-11T20:19:24+01:00</updated>
<published>2018-09-11T20:19:24+01:00</published>
<id>https://forum.atnel.pl/viewtopic.php?t=21269&amp;p=211580#p211580</id>
<link href="https://forum.atnel.pl/viewtopic.php?t=21269&amp;p=211580#p211580"/>
<title type="html"><![CDATA[Re: Atmega 2560 , HC-SR04 Konfiguracja timera]]></title>

<content type="html" xml:base="https://forum.atnel.pl/viewtopic.php?t=21269&amp;p=211580#p211580"><![CDATA[
A więc tak.<br />Ja potrzebuję mierzyć czas w mikrosekundach poprzez wejścia timera ICP<br />Zacznijmy od tego że potrzebuję przerwania od ICP.<br /><br />Czyli (jeśli chodzi o kod z Atmega 32) powinno to wyglądać tak:<br />[syntax=c]TIMSK |= (1&lt;&lt;TICIE1);[/syntax]<br /><br />ale ja używam M2560.<br /><br /><br />Znalazłem przykład dla M2560 i Timera4 na Avrfreaks (z tym że dla kwarcu 1MHz)<br /><br />[syntax=c]void init_timer4()<br />{<br /><br />  /**<br />   * https://s27.postimg.org/k2tu225pf/Clock_Select_Bit_Description.jpg<br />   **/<br />  TCCR4A |= (1 &lt;&lt; WGM41);<br />  TCCR4B |= 1 &lt;&lt; CS41; // prescaler 8<br /><br />  TIMSK4 |= 1 &lt;&lt; OCIE4A | 1 &lt;&lt; ICIE4;<br />  /**<br />   *  ICIE: Input Capture Interrupt Enable<br />      When this bit is written to '1', and the I-flag in the Status Register is set (interrupts globally enabled), the<br />      Timer/Counter1 Input Capture interrupt is enabled. The corresponding Interrupt Vector is executed when<br />      the ICF Flag, located in TIFR1, is set.<br /><br />      When a capture is triggered according to the ICESn setting, the counter value is copied into the Input Capture Register<br />      (ICRn). The event will also set the Input Capture Flag (ICFn), and this can be used to cause an Input Capture<br />      Interrupt, if this interrupt is enabled.<br /><br />      When the ICRn is used as TOP value (see description of the WGMn3:0 bits located in the TCCRnA and the<br />      TCCRnB Register), the ICPn is disconnected and consequently the input capture function is disabled.<br />   */<br /><br />  /* ICP4 (Input Capture Interrupt) , DIGITAL PIN 49<br />   *   ISR(TIMER4_CAPT_vect) when the signal on the ICP pin goes from 0 to 1 (raising edge).<br />   *   The method contains a if stament to see if it was called during a raising edge or falling edge.<br />   *   On a raising edge it switches the ICP to falling edge detection and then stores the start time.<br />   *   On a falling edge it switches the ICP to raising edge detection and gets the end time to calculate the distance in millimeters.<br />   */<br />  TCCR4B |= 1 &lt;&lt; ICES4; // Input capture on rising edge<br />  /**<br />   * This bit selects which edge on the Input Capture pin (ICP1) that is used to trigger a capture event. When<br />    the ICES1 bit is written to zero, a falling (negative) edge is used as trigger, and when the ICES1 bit is<br />    written to '1', a rising (positive) edge will trigger the capture.<br />    When a capture is triggered according to the ICES1 setting, the counter value is copied into the Input<br />    Capture Register (ICR1). The event will also set the Input Capture Flag (ICF1), and this can be used to<br />    cause an Input Capture Interrupt, if this interrupt is enabled.<br />    When the ICR1 is used as TOP value (see description of the WGM1&#91;3:0&#93; bits located in the TCCR1A and<br />    the TCCR1B Register), the ICP1 is disconnected and consequently the Input Capture function is<br />    disabled.<br />   */<br />  OCR4A = 8750;<br />  // &quot;we suggest to use over 60ms measurement cycle, in order to prevent trigger signal to the echo signal.&quot;<br />  // source: http://www.robosoftsystems.co.in/wikidocs/index.php?title=Ultrasonic_Sensor_(HC-SR04)<br />  // 70ms cycle: 1MHz/8 = 125000 counts/second =&gt; 125000/10 = 12500 counts/100ms =&gt; 12500/100*70 = 8750 counts / 70ms<br />  sei();<br />  us_per_count = 8; // 1MHz / 8 = 125000 counts/second =&gt; 1000000/125000<br /><br />}[/syntax]<br /><br />W zasadzie mogę użyć Timera4 ale muszę dopasować go do mojego kwarcu czyli 14745600.<br />Dodatkowo nie do końca rozumiem jak mam obsłużyć dwa moduły HC-SR04 odczytywane z pinów ICP4 i ICP5.<br /><br />Czy chodzi o to:<br /><br />[syntax=c]TCCR4B |= 1 &lt;&lt; ICES4; // Input capture on rising edge[/syntax]  <br /><br />i dla drugiego pinu ICP powinienem ustawić to w taki sposób ?<br /><br />[syntax=c]TCCR4B |= 1 &lt;&lt; ICES5; // Input capture on rising edge[/syntax]<br /><br /><br />Jeśli to dobrze rozumiem to odczyt dla ICP4 powinien wyglądać tak:<br /><br />[syntax=c]ISR (TIMER4_CAPT_vect)<br />{<br />  /*  Input Capture Unit<br />   */<br />  if (TCCR4B &amp; (1&lt;&lt;ICES4)) // On rising edge<br />  {<br />    TCCR4B &amp;= ~(1&lt;&lt;ICES1); // Next time detect falling edge<br />    rising = ICR1; // Save current count<br />  }<br />  else // On falling edge<br />  {<br />    TCCR4B |= (1&lt;&lt;ICES4); // Next time detect falling edge<br />    falling = ICR4; // Save current count<br />    counts = (uint32_t)falling - (uint32_t)rising;<br />    dist = (uint32_t)us_per_count * counts/ 58; // useconds / 58 to get distance in cm<br />    ready_to_print = 1;<br />  }<br />}[/syntax]<p>Statystyki: Napisane przez <a href="https://forum.atnel.pl/memberlist.php?mode=viewprofile&amp;u=14692">weles</a> — 11 wrz 2018, o 20:19</p><hr />
]]></content>
</entry>
<entry>
<author><name><![CDATA[mczarny]]></name></author>
<updated>2018-09-10T06:45:52+01:00</updated>
<published>2018-09-10T06:45:52+01:00</published>
<id>https://forum.atnel.pl/viewtopic.php?t=21269&amp;p=211520#p211520</id>
<link href="https://forum.atnel.pl/viewtopic.php?t=21269&amp;p=211520#p211520"/>
<title type="html"><![CDATA[Re: Atmega 2560 , HC-SR04 Konfiguracja timera]]></title>

<content type="html" xml:base="https://forum.atnel.pl/viewtopic.php?t=21269&amp;p=211520#p211520"><![CDATA[
cześć<br />ta linijka jest błądna<br />[syntax=c]TIMSK3 |= (1&lt;&lt;ICIE3 );[/syntax]<br />powinno być<br />[syntax=c]TIMSK3 |=(1&lt;&lt;OCIE3A);[/syntax]<br />No i nie masz ustawionego trybu pracy timera<br /><a href="https://obrazkiforum.atnel.pl/4651/003ad3b061501b872f0cf4854adc8448.jpg"  class="postlink"><img src="https://obrazkiforum.atnel.pl/thumb/4651/003ad3b061501b872f0cf4854adc8448.jpg" alt="Obrazek" /></a><br />jeżeli chcesz tryb CTC to bit WGM32 w rejestrze TCCR3B<br />Zapoznaj się z notą kontrolera tam masz wszystko<br />[syntax=c]TCCR3B |= (1&lt;&lt;WGM32);<br />TCCR3B |= (1&lt;&lt;CS31);<br />OCR0A = 100;<br />TIMSK3 |= (1&lt;&lt;OCIE3A);[/syntax]<br />przykładowo można tak zależy co ile chcesz przerwanie<p>Statystyki: Napisane przez <a href="https://forum.atnel.pl/memberlist.php?mode=viewprofile&amp;u=4651">mczarny</a> — 10 wrz 2018, o 06:45</p><hr />
]]></content>
</entry>
<entry>
<author><name><![CDATA[weles]]></name></author>
<updated>2018-09-09T21:36:31+01:00</updated>
<published>2018-09-09T21:36:31+01:00</published>
<id>https://forum.atnel.pl/viewtopic.php?t=21269&amp;p=211518#p211518</id>
<link href="https://forum.atnel.pl/viewtopic.php?t=21269&amp;p=211518#p211518"/>
<title type="html"><![CDATA[Atmega 2560 , HC-SR04 Konfiguracja timera]]></title>

<content type="html" xml:base="https://forum.atnel.pl/viewtopic.php?t=21269&amp;p=211518#p211518"><![CDATA[
Witam<br />Nadeszła w moim projekcie pora na uruchomienie pomiarów z dwóch modułów HC-SR04.<br />Kiedy wcześniej testowałem działanie na Atmega32 i zastosowaniu kodu z poradnika Mirka wszystko działało poprawnie.<br />Ale teraz używam Atmega 2560 i nie jest już tak prosto. Nie dość że to inny mikrokontroler to do tego muszę użyć dwóch modułów HC.<br /><br />Piny jakich używam do modułów HC to :<br /><br />[syntax=c]//L0-HC1<br />#define HC_ECHO1(1&lt;&lt;PL0)<br />#define ECHO1_PORTPORTL<br />#define ECHO1_DIRDDRL<br />//L1HC2<br />#define HC_ECHO2(1&lt;&lt;PL1)<br />#define ECHO2_PORTPORTL<br />#define ECHO2_DIRDDRL<br />//L2-HC1<br />#define HC_TRIG1(1&lt;&lt;PL2)<br />#define TRIG1_PORTPORTL<br />#define TRIG1_DIRDDRL<br />//L3-HC2<br />#define HC_TRIG2(1&lt;&lt;PL3)<br />#define TRIG2_PORTPORTL[/syntax]<br /><br /><br />[syntax=c]//L0- <br />ECHO1_PORT |= HC_ECHO1;<br />//L1- <br />ECHO2_PORT |= HC_ECHO2;<br />//L2- <br />TRIG1_DIR |= HC_TRIG1;                    <br />//L3- <br />TRIG2_DIR |= HC_TRIG2;[/syntax]<br /><br />Gdzie piny mikrokontrolera L0 i L1 to ICP.<br /><br />Wydaje mi się że problemem jest tu konfiguracja timera.<br />Do dyspozycji mam sprzętowy Timer3.<br />Robię to tak:<br /><br />[syntax=c]//7.***** TIMER3 - TRYB ICP - DALMIERZ  *******<br />TCCR3B |= (1&lt;&lt;ICES3);//zbocze narastające<br />TCCR3B |= (1&lt;&lt;CS31);// ustawienie prescalera na 8<br />TIMSK3 |= (1&lt;&lt;ICIE3 );// odblokowanie przerwania[/syntax]<br /><br />I zapewne pierwszy błąd znajduje się właśnie tu. Bo o ile preskaler i zbocze potrafię ustawić to problemem jest odblokowanie przerwania.<br />Zwyczajnie nie wiem jak powinienem zabrać się za obsługę dwóch modułów. W sumie na tym mikrokontrolerze nawet jednego nie potrafię uruchomić :/<br /><br />Może znajdzie się wśród kolegów i koleżanek ktoś kto zechciałby nieco mi pomóc w tej kwestii.<br />Chciałbym w pierwszej kolejności poprawnie ustawić timer i dokonać odczytu z jednego modułu HC. Potem przyjdzie pora na kolejny.<p>Statystyki: Napisane przez <a href="https://forum.atnel.pl/memberlist.php?mode=viewprofile&amp;u=14692">weles</a> — 9 wrz 2018, o 21:36</p><hr />
]]></content>
</entry>
</feed>