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

<title>ATNEL tech-forum</title>
<link href="https://forum.atnel.pl/index.php" />
<updated>2020-05-09T19:25:51+01:00</updated>

<author><name><![CDATA[ATNEL tech-forum]]></name></author>
<id>https://forum.atnel.pl/feed.php?f=4&amp;t=23104&amp;mode</id>
<entry>
<author><name><![CDATA[mirekk36]]></name></author>
<updated>2020-05-09T19:25:51+01:00</updated>
<published>2020-05-09T19:25:51+01:00</published>
<id>https://forum.atnel.pl/viewtopic.php?t=23104&amp;p=227085#p227085</id>
<link href="https://forum.atnel.pl/viewtopic.php?t=23104&amp;p=227085#p227085"/>
<title type="html"><![CDATA[Re: Przerwanie od UART i SPI]]></title>

<content type="html" xml:base="https://forum.atnel.pl/viewtopic.php?t=23104&amp;p=227085#p227085"><![CDATA[
<div class="quotetitle">fofex napisał(a):</div><div class="quotecontent"><br />że gdybyś te zmienne przeniósł z plików .c do .h<br /></div><br />Dlatego w języku C nie umieszcza się definicji zmiennych w plikach *.h ... tylko same deklaracje zmiennych<p>Statystyki: Napisane przez <a href="https://forum.atnel.pl/memberlist.php?mode=viewprofile&amp;u=54">mirekk36</a> — 9 maja 2020, o 19:25</p><hr />
]]></content>
</entry>
<entry>
<author><name><![CDATA[fofex]]></name></author>
<updated>2020-05-09T16:45:57+01:00</updated>
<published>2020-05-09T16:45:57+01:00</published>
<id>https://forum.atnel.pl/viewtopic.php?t=23104&amp;p=227084#p227084</id>
<link href="https://forum.atnel.pl/viewtopic.php?t=23104&amp;p=227084#p227084"/>
<title type="html"><![CDATA[Re: Przerwanie od UART i SPI]]></title>

<content type="html" xml:base="https://forum.atnel.pl/viewtopic.php?t=23104&amp;p=227084#p227084"><![CDATA[
<div class="quotetitle">MichalXY napisał(a):</div><div class="quotecontent"><br />chociaż nie powiem że jest zdziwiony że jeśli zmienne są nie zadeklarowane w pliku nagłówkowym tylko w pliku źródłowym to i tak są widoczne a<br /></div><br /><br />No to może zdziwię Cię jeszcze bardziej jak Ci powiem, że gdybyś te zmienne przeniósł z plików .c do .h to efekt byłby dokładnie taki sam. Dla kompilatora jest obojętne czy to plik nagłówkowy czy źródłowy.<p>Statystyki: Napisane przez <a href="https://forum.atnel.pl/memberlist.php?mode=viewprofile&amp;u=21072">fofex</a> — 9 maja 2020, o 16:45</p><hr />
]]></content>
</entry>
<entry>
<author><name><![CDATA[MichalXY]]></name></author>
<updated>2020-05-09T15:48:15+01:00</updated>
<published>2020-05-09T15:48:15+01:00</published>
<id>https://forum.atnel.pl/viewtopic.php?t=23104&amp;p=227083#p227083</id>
<link href="https://forum.atnel.pl/viewtopic.php?t=23104&amp;p=227083#p227083"/>
<title type="html"><![CDATA[Re: Przerwanie od UART i SPI]]></title>

<content type="html" xml:base="https://forum.atnel.pl/viewtopic.php?t=23104&amp;p=227083#p227083"><![CDATA[
Przed chwilą sobie doczytałem rozdział o dzieleniu projektu na pliki w BB i tam jest napisane to co Ty piszesz żeby dołożyć static aby uniewidocznić zmienną. Tak to jest jak się przez 3 lata programuje w C++ pod arduino i wraca do C (chociaż nie powiem że jest zdziwiony że jeśli zmienne są nie zadeklarowane w pliku nagłówkowym tylko w pliku źródłowym to i tak są widoczne ale skoro tak jest to nie ma co z tym dyskutować). Jeszcze raz dzięki za pomoc!<p>Statystyki: Napisane przez <a href="https://forum.atnel.pl/memberlist.php?mode=viewprofile&amp;u=226">MichalXY</a> — 9 maja 2020, o 15:48</p><hr />
]]></content>
</entry>
<entry>
<author><name><![CDATA[fofex]]></name></author>
<updated>2020-05-09T14:44:15+01:00</updated>
<published>2020-05-09T14:44:15+01:00</published>
<id>https://forum.atnel.pl/viewtopic.php?t=23104&amp;p=227082#p227082</id>
<link href="https://forum.atnel.pl/viewtopic.php?t=23104&amp;p=227082#p227082"/>
<title type="html"><![CDATA[Re: Przerwanie od UART i SPI]]></title>

<content type="html" xml:base="https://forum.atnel.pl/viewtopic.php?t=23104&amp;p=227082#p227082"><![CDATA[
Nazwy mogłeś zostawić, za to nadać zmiennym wiązanie wewnętrzne. Wtedy było by zgodnie ze sztuką:<br /><br />[syntax=c]volatile static uint8_t tx_head;<br />volatile static uint8_t tx_tail;<br />volatile static uint8_t rx_head;<br />volatile static uint8_t rx_tail;[/syntax]<p>Statystyki: Napisane przez <a href="https://forum.atnel.pl/memberlist.php?mode=viewprofile&amp;u=21072">fofex</a> — 9 maja 2020, o 14:44</p><hr />
]]></content>
</entry>
<entry>
<author><name><![CDATA[MichalXY]]></name></author>
<updated>2020-05-09T14:25:26+01:00</updated>
<published>2020-05-09T14:25:26+01:00</published>
<id>https://forum.atnel.pl/viewtopic.php?t=23104&amp;p=227080#p227080</id>
<link href="https://forum.atnel.pl/viewtopic.php?t=23104&amp;p=227080#p227080"/>
<title type="html"><![CDATA[Re: Przerwanie od UART i SPI]]></title>

<content type="html" xml:base="https://forum.atnel.pl/viewtopic.php?t=23104&amp;p=227080#p227080"><![CDATA[
<strong>fofex</strong> - zmiana nazw zmiennych które wymieniłeś dała pozytywny rezultat. Wszystko działa jak należy. Za długo w C++ pod arduino pisałem i teraz niektóre rzeczy z C muszę odrdzewić najwyraźniej. Dzięki za pomoc!<p>Statystyki: Napisane przez <a href="https://forum.atnel.pl/memberlist.php?mode=viewprofile&amp;u=226">MichalXY</a> — 9 maja 2020, o 14:25</p><hr />
]]></content>
</entry>
<entry>
<author><name><![CDATA[fofex]]></name></author>
<updated>2020-05-09T14:05:19+01:00</updated>
<published>2020-05-09T14:05:19+01:00</published>
<id>https://forum.atnel.pl/viewtopic.php?t=23104&amp;p=227079#p227079</id>
<link href="https://forum.atnel.pl/viewtopic.php?t=23104&amp;p=227079#p227079"/>
<title type="html"><![CDATA[Re: Przerwanie od UART i SPI]]></title>

<content type="html" xml:base="https://forum.atnel.pl/viewtopic.php?t=23104&amp;p=227079#p227079"><![CDATA[
<div class="quotetitle">MichalXY napisał(a):</div><div class="quotecontent"><br />volatile uint8_t tx_head;<br />volatile uint8_t tx_tail;<br />volatile uint8_t rx_head;<br />volatile uint8_t rx_tail;<br /></div><br />Jeśli lib UART ma tak samo nazwane zmienne z widocznością zewnętrzną to następuje ich wzajemne nadpisywanie.<p>Statystyki: Napisane przez <a href="https://forum.atnel.pl/memberlist.php?mode=viewprofile&amp;u=21072">fofex</a> — 9 maja 2020, o 14:05</p><hr />
]]></content>
</entry>
<entry>
<author><name><![CDATA[MichalXY]]></name></author>
<updated>2020-05-09T12:54:03+01:00</updated>
<published>2020-05-09T12:54:03+01:00</published>
<id>https://forum.atnel.pl/viewtopic.php?t=23104&amp;p=227078#p227078</id>
<link href="https://forum.atnel.pl/viewtopic.php?t=23104&amp;p=227078#p227078"/>
<title type="html"><![CDATA[Przerwanie od UART i SPI]]></title>

<content type="html" xml:base="https://forum.atnel.pl/viewtopic.php?t=23104&amp;p=227078#p227078"><![CDATA[
Witajcie, <br />W chwili obecnej pracuję nad projektem który wykorzystuje SPI oraz UART. Haczyk polega na tym że biblioteka do SPI z której korzystam wykorzystuje przerwania od SPI. Problem polega na tym że osobno te dwie rzeczy działają idealnie a w momencie gdy chcę połączyć je tj. odczytać coś z SPI i wysłać tą odczytaną wartość na UART to na terminalu dostaje jakieś śmieci - łańcuchy tekstowe które wysyłam na uart dostaję w terminalu w niekompletnej postaci (np. zamiast słowa Status dostaje samo St bez  żadnego znaku nowej linit itp.). Na moje oko wygląda to tak jakby przerwania od UARTa gryzły się z przerwaniem od SPI przy czym jak przeglądałem kod z BB z Projektem Wielozdaniowym to tam co prawda nie ma przerwań od SPI ale są przerwania zewnętrzne od INT (RTC),  ICP (do podczerwieni) i obsługa UARTa itd. i tam takich problemów nie ma więc pewnie ja coś namotałem i nie wiem jak to ugryźć teraz. <br />Poniżej jest schemat płytki testowej którą wykorzystuje (tak jest tam arduino nano ale to dlatego że jest małe i dość poręczne) do testów. <br /><br /><a href="https://obrazkiforum.atnel.pl/226/0e0b76bfc39d678cda030c32ff7fb661.png"  class="postlink"><img src="https://obrazkiforum.atnel.pl/thumb/226/0e0b76bfc39d678cda030c32ff7fb661.png" alt="Obrazek" /></a><br /><br />Poniżej znajduje się kod pliku main.c, spi.c, spi.h, timeout.c i timeout.h<br /><br />main.c<br />[syntax=c]/*<br /> * main.c<br /> *<br /> *  Created on: 6 maj 2020<br /> *      Author: MIKE<br /> */<br /><br />#include &lt;avr/io.h&gt;<br />#include &lt;util/delay.h&gt;<br />#include &quot;spi/spi.h&quot;<br />#include &quot;uart/uart.h&quot;<br />#include &quot;timeout/timeout.h&quot;<br /><br />unsigned long previus_millis = 0;<br /><br />int main(void) {<br />DDRD |= (1&lt;&lt;7);<br />PORTD &amp;= ~(1&lt;&lt;7);<br />uint8_t status = 0xFF;<br />uart_init(UBRR_VAL);<br />spi_init(SPI_MODE0, SPI_CLOCK_DIV64);<br />timeout_init();<br />sei();<br />uart_putsn(&quot;SPI TEST&quot;);<br />while (1) {<br />unsigned long current_millis = timeout_millis();<br />if (current_millis - previus_millis &gt;= 1000) {<br />previus_millis = current_millis;<br />spi_putc(0xFF, 1);<br />uart_puts(&quot;Status: &quot;);<br />uart_putint(status, 16);<br />uart_putsn(&quot;&quot;);<br />}<br />}<br />}[/syntax]<br />spi.c<br />[syntax=c]/*<br /> * spi.c<br /> *<br /> *  Created on: 6 maj 2020<br /> *      Author: MIKE<br /> *      based on SPI library by Julien Delvaux<br /> */<br /><br />#include &quot;spi.h&quot;<br /><br />volatile uint8_t tx_buff&#91;TX_BUFF_SIZE&#93;;<br />volatile uint8_t tx_buff_mask;<br />volatile uint8_t rx_buff&#91;RX_BUFF_SIZE&#93;;<br />volatile uint8_t rx_buff_mask;<br />volatile uint8_t cts;<br />volatile uint8_t bytes_request;<br />volatile uint8_t tx_head;<br />volatile uint8_t tx_tail;<br />volatile uint8_t rx_head;<br />volatile uint8_t rx_tail;<br />void (*tx_callback)();<br />void (*rx_callback)();<br />volatile uint8_t tx_flag = 0;<br />volatile uint8_t rx_flag = 0;<br /><br /><br />void spi_init(uint8_t mode, uint8_t clock) {<br />// Pin Configuration<br />SPI_DDR |= (1&lt;&lt;SPI_PIN_SS);<br />SPI_PORT|= (1&lt;&lt;SPI_PIN_SS);<br /><br />cts = SPI_INACTIVE;<br />// Set MOSI and SCK output, all others input<br />SPI_DDR |= (1&lt;&lt;SPI_PIN_MOSI)|(1&lt;&lt;SPI_PIN_SCK);<br />// Enable SPI, Master, set clock rate<br />SPCR = (1&lt;&lt;SPIE)|(1&lt;&lt;SPE)|(1&lt;&lt;MSTR)|(mode&lt;&lt;CPHA)|(clock&lt;&lt;SPR0);<br />}<br /><br />void spi_send_bytes(const char *str) {<br />uint16_t tmptail=0;<br /><br />// Stores datas in buffer<br />while (*str) {<br />spi_putc(*str++, 0);<br />}<br /><br />// Checks if ready to send and proceed<br />if(cts==SPI_INACTIVE){<br />cts=SPI_ACTIVE;<br />SPI_PORT &amp;= ~(1&lt;&lt;SPI_PIN_SS); // Pull-down the line<br />if ( tx_head != tx_tail) {<br />tmptail = (tx_tail + 1) &amp; TX_BUFF_MASK;<br />tx_tail = tmptail;<br />/* get one byte from buffer and write it to UART */<br />SPDR = tx_buff&#91;tmptail&#93;;  /* start transmission */<br />}<br />}<br />}<br /><br />void spi_read_data(uint8_t bytes_count) {<br />bytes_request = bytes_count;<br />if (cts == SPI_INACTIVE) {<br />cts = SPI_ACTIVE;<br />SPI_PORT &amp;= ~(1&lt;&lt;SPI_PIN_SS);<br />SPDR = 0x00;<br />}<br />}<br /><br />void spi_close(void) {<br />spi_flush();<br />SPCR = (0x00);<br />SPI_DDR &amp;= ~(1&lt;&lt;SPI_PIN_SS);<br />SPI_PORT &amp;= ~(1&lt;&lt;SPI_PIN_SS);<br />}<br /><br />uint8_t spi_getc() {<br />uint16_t temp_tail;<br />uint8_t data;<br /><br />if (rx_head == rx_tail) {<br /><br />}<br />temp_tail = (rx_tail + 1) &amp; RX_BUFF_MASK;<br />rx_tail = temp_tail;<br />data = rx_buff&#91;temp_tail&#93;;<br />return data;<br />}<br /><br /><br /><br />void spi_putc(uint8_t data, uint8_t mode) {<br />uint16_t temp_head;<br />if (mode == 0) {<br />temp_head = (tx_head + 1) &amp; TX_BUFF_MASK;<br />if (temp_head != tx_tail) {<br />tx_buff&#91;temp_head&#93; = data;<br />tx_head = temp_head;<br />}<br />} else {<br />uint16_t tmptail=0;<br />temp_head = (tx_head + 1) &amp; TX_BUFF_MASK;<br />if (temp_head != tx_tail) {<br />tx_buff&#91;temp_head&#93; = data;<br />tx_head = temp_head;<br />}<br />if(cts==SPI_INACTIVE){<br />cts=SPI_ACTIVE;<br />SPI_PORT &amp;= ~(1&lt;&lt;SPI_PIN_SS); // Pull-down the line<br />if ( tx_head != tx_tail) {<br />tmptail = (tx_tail + 1) &amp; TX_BUFF_MASK;<br />tx_tail = tmptail;<br />/* get one byte from buffer and write it to UART */<br />SPDR = tx_buff&#91;tmptail&#93;;  /* start transmission */<br />}<br />}<br /><br />}<br />}<br /><br />uint8_t spi_transfer(uint8_t data) {<br />spi_putc(data, 1);<br />return spi_getc();<br />}<br /><br />void spi_puts(const char *str) {<br />while (*str) {<br />spi_putc(*str++, 0);<br />}<br />}<br /><br />void spi_flush(void) {<br />rx_head = rx_tail;<br />}<br /><br />uint8_t spi_available(void) {<br />return (RX_BUFF_SIZE + rx_head - rx_tail) &amp; rx_buff_mask;<br />}<br /><br /><br />ISR(SPI_STC_vect)<br />/*************************************************************************<br />Function: SPI interrupt<br />Purpose:  called when the SS pin has been put low<br /> **************************************************************************/<br />{<br />uint16_t temp_head=0;<br />uint16_t temp_tail=0;<br /><br />//RECEIVE<br />// calculate buffer index<br />temp_head = ( rx_head + 1) &amp; RX_BUFF_MASK;<br />if ( temp_head == rx_tail ) {<br />// error: receive buffer overflow<br /><br />} else {<br />// store new index<br />rx_head = temp_head;<br />// store received data in buffer<br />rx_buff&#91;temp_head&#93; = SPDR;<br />rx_flag = 1;<br />}<br /><br />// SEND<br />if ( tx_head != tx_tail) {<br />// calculate and store new buffer index<br />temp_tail = (tx_tail + 1) &amp; TX_BUFF_MASK;<br />tx_tail = temp_tail;<br />// get one byte from buffer and write it to UART<br />SPDR = tx_buff&#91;temp_tail&#93;;  // start transmission<br />tx_flag = 1;<br />}<br />else if(bytes_request &gt; 0){<br />bytes_request--;<br />SPDR = 0x00;<br />}<br />else {<br />// tx buffer empty, STOP the transmission<br />SPI_PORT|= (1&lt;&lt;SPI_PIN_SS);<br />cts = SPI_INACTIVE;<br />}<br />}<br /><br />void spi_set_on_writed_data_callback(void (*callback)()) {<br />tx_callback = callback;<br />tx_flag = 0;<br />}<br /><br />void spi_set_on_readed_data_callback(void (*callback)()) {<br />rx_callback = callback;<br />rx_flag = 0;<br />}<br /><br />void spi_on_writed_data() {<br />(*tx_callback)();<br />}<br />void spi_on_readed_data() {<br />(*rx_callback)();<br />}[/syntax]<br /><br />spi.h<br />[syntax=c]/*<br /><br /> * spi.h<br /> *<br /> *  Created on: 6 maj 2020<br /> *      Author: MIKE<br /> *      based on SPI library by Julien Delvaux<br /> */<br /><br />#ifndef SPI_SPI_H_<br />#define SPI_SPI_H_<br /><br />#include &lt;avr/io.h&gt;<br />#include &lt;avr/interrupt.h&gt;<br /><br />#define MOSI PB5//   &lt;---- A (SER IN)<br />#define SCK PB7//   &lt;---- SHIFT CLOCK (SC)<br />#define CS PB4// &lt;---- LATCH CLOCK (LC)<br /><br />#define TX_BUFF_SIZE 64<br />#define TX_BUFF_MASK (TX_BUFF_SIZE - 1)<br />#define RX_BUFF_SIZE 64<br />#define RX_BUFF_MASK (RX_BUFF_SIZE - 1)<br /><br /><br />#define SPI_ACTIVE0 // SS Pin put Low<br />#define SPI_INACTIVE1 // SS Pin put High<br /><br />#define SPI_PIN_SS2//SS PIN<br />#define SPI_PIN_MOSI3//MOSI PIN<br />#define SPI_PIN_MISO4//MISO PIN<br />#define SPI_PIN_SCK5//SCK PIN<br />#define SPI_DDRDDRB//SPI on PORTB<br />#define SPI_PORTPORTB//SPI on PORTB<br /><br />#define SPI_CLOCK_DIV40x00<br />#define SPI_CLOCK_DIV160x01<br />#define SPI_CLOCK_DIV640x02<br />#define SPI_CLOCK_DIV1280x03<br />#define SPI_CLOCK_DIV20x04<br />#define SPI_CLOCK_DIV80x05<br />#define SPI_CLOCK_DIV320x06<br /><br />#define SPI_MODE0 0x00<br />#define SPI_MODE1 0x04<br />#define SPI_MODE2 0x08<br />#define SPI_MODE3 0x0C<br /><br />void spi_init(uint8_t mode, uint8_t clock);<br />void spi_read_data(uint8_t bytes_count);<br />void spi_send_bytes(const char *str);<br />void spi_close(void);<br />uint8_t spi_getc(void);<br />void spi_putc(uint8_t data, uint8_t mode);<br />void spi_puts(const char *str);<br />uint8_t spi_transfer(uint8_t data);<br /><br />void spi_on_writed_data();<br />void spi_on_readed_data();<br />void spi_set_on_writed_data_callback(void (*callback)());<br />void spi_set_on_readed_data_callback(void (*callback)());<br /><br />void spi_flush(void);<br />uint8_t spi_available(void);<br /><br /><br />#endif /* SPI_SPI_H_ */[/syntax]<br /><br />timeout.c<br />[syntax=c]/*<br /><br /> * timeout.c<br /> *<br /> *  Created on: 9 maj 2020<br /> *      Author: MIKE<br /> */<br /><br />#include &lt;avr/io.h&gt;<br />#include &lt;avr/interrupt.h&gt;<br />#include &quot;timeout.h&quot;<br /><br />volatile unsigned long millis;<br /><br />void timeout_init(void) {<br />// 8-bit TimerX config<br />TCCR0B |= (1&lt;&lt;CS00)|(1&lt;&lt;CS01);  // set prescaler = 64 ---&gt; please check proper values in PDF;<br />TCCR0A |= (1&lt;&lt;WGM01); // set CTC mode ---&gt; please check proper values in PDF<br />OCR0A  = 249;  // every  &#91; 1 ms &#93;<br />TIMSK0  |= (1&lt;&lt;OCIE0A);     // enable CompareX interrupt<br />}<br /><br />unsigned long timeout_millis() {<br />return millis;<br />}<br /><br />ISR( TIMER0_COMPA_vect ) {<br />millis++;<br />}[/syntax]<br />timeout.h<br />[syntax=c]/*<br /> * timeout.h<br /> *<br /> *  Created on: 9 maj 2020<br /> *      Author: MIKE<br /> */<br /><br />#ifndef TIMEOUT_TIMEOUT_H_<br />#define TIMEOUT_TIMEOUT_H_<br /><br />void timeout_init();<br />unsigned long timeout_millis();<br /><br /><br />#endif /* TIMEOUT_TIMEOUT_H_ */[/syntax]<br /><br />Kodów źródłowych do uarta nie będę publikował bo jest to przerobiona wersja z bb (dostosowane rejestry do ATmegi328p, zmieniona konwencja nazewnictwa zmiennych, wycięta obsługa RS485 i dodany odbiór łańcuchów tekstowych) - chyba że Mirek się zgodzi to wtedy źródła do uarta udostępnię.<br />Z góry dziękuję za każda pomoc w namierzeniu problemu  <img src="https://forum.atnel.pl/images/smilies/icon_e_smile.gif" alt=":)" title="Szczęśliwy" />. <br /><br />Pozdrawiam!<p>Statystyki: Napisane przez <a href="https://forum.atnel.pl/memberlist.php?mode=viewprofile&amp;u=226">MichalXY</a> — 9 maja 2020, o 12:54</p><hr />
]]></content>
</entry>
</feed>