{"id":157,"date":"2018-10-10T13:18:58","date_gmt":"2018-10-10T13:18:58","guid":{"rendered":"http:\/\/marcjuneau.ca\/?p=157"},"modified":"2018-10-10T13:18:58","modified_gmt":"2018-10-10T13:18:58","slug":"avr-spi-avec-un-arduino-uno","status":"publish","type":"post","link":"https:\/\/marcjuneau.ca\/?p=157","title":{"rendered":"AVR SPI avec un Arduino UNO"},"content":{"rendered":"<p>Cet article explique comment utiliser le port SPI pr\u00e9sent sur le Atmega328p du Arduino UNO.<\/p>\n<p>Dans cet article, nous utiliserons une boucle pour l&rsquo;\u00e9criture. Cette m\u00e9thode est simple mais elle limite les performances du microcontr\u00f4leur. Je ferai un autre article qui expliquera comment faire cette op\u00e9ration en interruption, ce qui est recommand\u00e9 afin de lib\u00e9rer le programme principal.<\/p>\n<p><strong>\u00c9tape 1 : Initialisation du port SPI<\/strong><\/p>\n<p>Le port SPI utilise 3 liens pour transmettre et recevoir les donn\u00e9es en plus d&rsquo;un signal pour s\u00e9lectionner l&rsquo;esclave vis\u00e9.<\/p>\n<table style=\"border-collapse: collapse; width: 98.4997%;\" border=\"1\">\n<tbody>\n<tr>\n<td style=\"width: 21.1111%; text-align: center; background-color: #000000;\"><span style=\"color: #ffffff; font-family: arial, helvetica, sans-serif;\"><strong>Signal<\/strong><\/span><\/td>\n<td style=\"width: 24.8611%; text-align: center; background-color: #000000;\"><span style=\"color: #ffffff; font-family: arial, helvetica, sans-serif;\"><strong>Broche (port)<\/strong><\/span><\/td>\n<td style=\"width: 52.6489%; text-align: center; background-color: #000000;\"><span style=\"color: #ffffff; font-family: arial, helvetica, sans-serif;\"><strong>Description<\/strong><\/span><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 21.1111%; text-align: center;\"><span style=\"font-family: arial, helvetica, sans-serif;\">SCLK<\/span><\/td>\n<td style=\"width: 24.8611%; text-align: center;\"><span style=\"font-family: arial, helvetica, sans-serif;\">13 (PB5)<\/span><\/td>\n<td style=\"width: 52.6489%; text-align: center;\"><span style=\"font-family: arial, helvetica, sans-serif;\">Horloge<\/span><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 21.1111%; text-align: center;\">MISO<\/td>\n<td style=\"width: 24.8611%; text-align: center;\">12 (PB4)<\/td>\n<td style=\"width: 52.6489%; text-align: center;\">Donn\u00e9es de l&rsquo;esclave vers le ma\u00eetre<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 21.1111%; text-align: center;\">MOSI<\/td>\n<td style=\"width: 24.8611%; text-align: center;\">11 (PB3)<\/td>\n<td style=\"width: 52.6489%; text-align: center;\">Donn\u00e9es du ma\u00eetre vers l&rsquo;esclave<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 21.1111%; text-align: center;\">CS<\/td>\n<td style=\"width: 24.8611%; text-align: center;\">10 (PB2)<\/td>\n<td style=\"width: 52.6489%; text-align: center;\">S\u00e9lection de l&rsquo;esclave<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Les signaux SCLK, MOSI, CS sont donc des sorties pour l&rsquo;Arduino et le signal MISO est une entr\u00e9e. La ligne suivante configure les broches requises en sorties.<\/p>\n<pre>DDRB |= (1&lt;&lt;2) | (1&lt;&lt;3) | (1&lt;&lt;5) ;<\/pre>\n<p>La configuration par d\u00e9faut correspond \u00e0 la majorit\u00e9 des situations.\u00a0 L&rsquo;horloge principale est divis\u00e9e par 4 (ce qui donne 4Mhz pour le Arduino UNO), le signal SCLK est \u00e0 0 lorsque le SPI est inactif, les donn\u00e9es d\u00e9butent par le MSB. Le registre de contr\u00f4le est le SPCR.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-159\" src=\"http:\/\/marcjuneau.ca\/wp-content\/uploads\/2018\/10\/spi1.png\" alt=\"\" width=\"688\" height=\"187\" srcset=\"https:\/\/marcjuneau.ca\/wp-content\/uploads\/2018\/10\/spi1.png 976w, https:\/\/marcjuneau.ca\/wp-content\/uploads\/2018\/10\/spi1-300x81.png 300w, https:\/\/marcjuneau.ca\/wp-content\/uploads\/2018\/10\/spi1-768x209.png 768w\" sizes=\"auto, (max-width: 688px) 100vw, 688px\" \/><\/p>\n<p>La ligne suivante active le SPI et place le SPI en mode ma\u00eetre :<\/p>\n<pre>SPCR = (1&lt;&lt;SPE) | (1&lt;&lt;MSTR);<\/pre>\n<p><strong>\u00c9tape 2 : Utilisation du port SPI\u00a0<\/strong><\/p>\n<p>Apr\u00e8s son initialisation, le port SPI est pr\u00eat pour envoyer des donn\u00e9es. Avant toute communication, il faut s\u00e9lectionner l&rsquo;esclave en abaissant le signal CS correspondant. Il suffit ensuite d&rsquo;\u00e9crire dans le registre\u00a0SPDR l&rsquo;octet \u00e0 transmettre. Apr\u00e8s chaque \u00e9criture, il faut lire le bit d&rsquo;\u00e9tat SPIF du registre d&rsquo;\u00e9tat SPSR. Ce dernier devient 1 lorsque la transmission est termin\u00e9e. Voici une fonction d&rsquo;\u00e9criture qui regroupe ces lignes :<\/p>\n<pre><span style=\"font-family: 'courier new', courier, monospace;\">uint8_t spiWrite(uint8_t data)<\/span>\r\n<span style=\"font-family: 'courier new', courier, monospace;\">{<\/span>\r\n<span style=\"font-family: 'courier new', courier, monospace;\">  SPDR = data;<\/span>\r\n<span style=\"font-family: 'courier new', courier, monospace;\">  while(!(SPSR &amp; (1&lt;&lt;SPIF)));<\/span>\r\n<span style=\"font-family: 'courier new', courier, monospace;\">  return SPDR;<\/span>\r\n<span style=\"font-family: 'courier new', courier, monospace;\">}<\/span><\/pre>\n<p>La fonction retourne le contenu de SPDR, qui contient alors l&rsquo;octet re\u00e7u via le lien MISO simultan\u00e9ment \u00e0 l&rsquo;\u00e9criture sur MOSI.<\/p>\n<p>Voici un exemple de code qui initialise le port SPI puis envoie 0x40 suivi de 0x05 vers un esclave.<\/p>\n<pre><span style=\"font-family: 'courier new', courier, monospace;\">\/\/ Dans cet exemple, CS est sur le bit 2 du port B\r\n\/\/ Initialisation du SPI\r\nDDRB |= (1&lt;&lt;2) | (1&lt;&lt;3) | (1&lt;&lt;5) ; \r\nSPCR = (1&lt;&lt;SPE) | (1&lt;&lt;MSTR);\r\n\/\/ Transmission vers un esclave\r\nPORTB &amp;= ~(1&lt;&lt;2);\u00a0 \u00a0\/\/ mise \u00e0 z\u00e9ro de CS<\/span>\r\n<span style=\"font-family: 'courier new', courier, monospace;\">spiWrite(0x40);<\/span>\r\n<span style=\"font-family: 'courier new', courier, monospace;\">spiWrite(0x05);<\/span>\r\n<span style=\"font-family: 'courier new', courier, monospace;\">PORTB |= (1&lt;&lt;2);\u00a0 \u00a0\/\/ mise \u00e0 un de CS<\/span><\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Cet article explique comment utiliser le port SPI pr\u00e9sent sur le Atmega328p du Arduino UNO. Dans cet article, nous utiliserons une boucle pour l&rsquo;\u00e9criture. Cette m\u00e9thode est simple mais elle limite les performances du microcontr\u00f4leur. Je ferai un autre article qui expliquera comment faire cette op\u00e9ration en interruption, ce qui<span class=\"more-link\"><a href=\"https:\/\/marcjuneau.ca\/?p=157\">Continue Reading<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,11,2],"tags":[],"class_list":["entry","author-mjuneau","post-157","post","type-post","status-publish","format-standard","category-arduino","category-avrstudio","category-cc"],"_links":{"self":[{"href":"https:\/\/marcjuneau.ca\/index.php?rest_route=\/wp\/v2\/posts\/157","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/marcjuneau.ca\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/marcjuneau.ca\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/marcjuneau.ca\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/marcjuneau.ca\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=157"}],"version-history":[{"count":4,"href":"https:\/\/marcjuneau.ca\/index.php?rest_route=\/wp\/v2\/posts\/157\/revisions"}],"predecessor-version":[{"id":162,"href":"https:\/\/marcjuneau.ca\/index.php?rest_route=\/wp\/v2\/posts\/157\/revisions\/162"}],"wp:attachment":[{"href":"https:\/\/marcjuneau.ca\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=157"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/marcjuneau.ca\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=157"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/marcjuneau.ca\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=157"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}