|
¡û ¤Ï¤¸¤á¤Ë
ATMega8535 ¤Ï¡¢USART (Universal Synchronous and Asynchronous Receiver and Transmitter) ¤È¸Æ¤Ð¤ì¤ë¡¢¥·¥ê¥¢¥ëÄÌ¿®¤ò¹Ô¤¦¤¿¤á¤ËɬÍפʵ¡Ç½¤òÈ÷¤¨¤Æ¤¤¤Þ¤¹¡£¤³¤Îµ¡Ç½¤ò»È¤¦¤È PC Åù¤È¥·¥ê¥¢¥ë¥Ý¡¼¥È·Ðͳ¤Ç¥Ç¡¼¥¿ÄÌ¿®¤ò¹Ô¤¦¤³¤È¤¬¤Ç¤¤Þ¤¹¡£¤³¤³¤Ç¤Ï¼ÂºÝ¤Ë¡¢PC ¤Î¥·¥ê¥¢¥ë¥Ý¡¼¥È (D-SUB 9¥Ô¥ó) ¤ÈÀܳ¤·¤Æ¥Ç¡¼¥¿ÄÌ¿®¤ò¹Ô¤¤¤Þ¤¹¡£
¡û USART ¤Ë¤Ä¤¤¤Æ
USART ¤È¤Ï Universal Synchronous and Asynchronous Receiver and Transmitter ¤Îά¤Ç¡¢¥·¥ê¥¢¥ë¿®¹æ¤È¥Ñ¥é¥ì¥ë¿®¹æ´Ö¤ÎÊÑ´¹¤ò¹Ô¤¦¤¿¤á¤Î½¸ÀѲóÏ©¤Î¤³¤È¤òɽ¤¹ÍͤǤ¹¡ÊWikipedia ¤Ë¤è¤ë¤È¡Ë¡£ATmega8535 ¤Î USART ¤Î¼ç¤Ê»ÅÍͤϰʲ¼¤ÎÍͤˤʤê¤Þ¤¹¡£
- Á´Æó½ÅÆ°ºî¡ÊÁ÷¿®¤È¼õ¿®¤òƱ»þ¤Ë¹Ô¤¦¤³¤È¤¬¤Ç¤¤ë¡Ë
- Ʊ´ü¡¢ÈóƱ´üÆ°ºî
- 5/6/7/8/9 ¥Ó¥Ã¥È¥Ç¡¼¥¿¤ÎÁ÷¼õ¿®
- ¥¹¥È¥Ã¥×¥Ó¥Ã¥È¤Ï 1/2 ¥Ó¥Ã¥È¤ÎξÊý¤ò¥µ¥Ý¡¼¥È
- ´ñ¿ô/¶ö¿ô¥Ñ¥ê¥Æ¥£¥Ó¥Ã¥È¤ÎÀ¸À®¤ª¤è¤Ó¸¡ºº¤¬²Äǽ
- ¥Ç¡¼¥¿¥ª¡¼¥Ð¥é¥ó¡ÊÅþÃå¥Ç¡¼¥¿¤òÆɤàÁ°¤Ë¿·¤·¤¤¥Ç¡¼¥¿¤¬ÅþÃ夷¤Æ¤·¤Þ¤¤¡¢Ì¤Æɥǡ¼¥¿¤ò¼º¤Ã¤¿¡Ë¸¡½Ð²Äǽ
- ¥Õ¥ì¡¼¥ß¥ó¥°¥¨¥é¡¼¡Ê¥¹¥È¥Ã¥×¥Ó¥Ã¥È¸¡½Ð¼ºÇԡ˸¡½Ð²Äǽ
- ¼õ¿®´°Î»/Á÷¿®´°Î»/Á÷¿®¥Ç¡¼¥¿¥ì¥¸¥¹¥¿¶õ¤ ¤òÄÌÃΤ¹¤ë³ä¤ê¹þ¤ß¤ò¥µ¥Ý¡¼¥È
ATmega8535 ¤Ç¤Ï RXD ¥Ô¥ó¡ÊPDIP ¥Ñ¥Ã¥±¡¼¥¸¤Ç¤Ï 14¥Ô¥ó¡Ë¤ò¥Ç¡¼¥¿¼õ¿®¤Ë¡¢TXD ¥Ô¥ó¡ÊPDIP ¥Ñ¥Ã¥±¡¼¥¸¤Ç¤Ï 15¥Ô¥ó¡Ë¤ò¥Ç¡¼¥¿Á÷¿®¤Ë»ÈÍѤ·¤Þ¤¹¡£Íý¶þ¤È¤·¤Æ¤Ï¡¢¤³¤ÎÆó¤Ä¤Î¥Ô¥ó¤ò¤½¤ì¤¾¤ì PC ¤Î¥·¥ê¥¢¥ë¥Ý¡¼¥È¤Î TXD/RXD ¤ËÀܳ¤¹¤ì¤ÐÎɤ¤¤ï¤±¤Ç¤¹¡£
¤·¤«¤·¡¢PC ¤Ç¥µ¥Ý¡¼¥È¤·¤Æ¤¤¤ë RS-232C¡ÊRS-232C ¤Ï 25¥Ô¥ó¤Îµ¬³Ê¤ÎÀΤθƾΤǤ¢¤ê¡¢Àµ¼°µ¬³Ê¤Ï 25¥Ô¥ó¤¬ EIA-232... ¤Ç 9¥Ô¥ó¤¬ EIA-574... ¤È¤¤¤¦Ì¾¾Î¤é¤·¤¤¤Ç¤¹¡Ë¤Î¿®¹æ¥ì¥Ù¥ë¤¬ -15V ¤«¤é 15V ÄøÅ٤Ǥ¢¤ë¤Î¤ËÂФ·¡¢ATmega8535 ¤ÎÍͤʥޥ¤¥³¥ó¤Î¿®¹æ¥ì¥Ù¥ë¤Ï 0 - 5V ¤È¤º¤ì¤Æ¤¤¤Þ¤¹¡£¤½¤Î¤¿¤á¼ÂºÝ¤Ë¤Ï¡¢ATmega8535 ¤È PC ¤Î¥·¥ê¥¢¥ë¥Ý¡¼¥È¤Î´Ö¤ËÅÅ°µÊÑ´¹ÍѤΠIC¡Ê¤â¤·¤¯¤Ï¥È¥é¥ó¥¸¥¹¥¿Åù¤Ç¼«ºî¤·¤¿²óÏ©¡Ë¤òÆþ¤ì¤ëɬÍפ¬¤¢¤ê¤Þ¤¹¡£Ëܼ¸³²óÏ©¤Ç¤Ï¡¢ADM3202 ¤È¤¤¤¦ RS-232¥é¥¤¥ó¡¦¥É¥é¥¤¥Ð/¥ì¥·¡¼¥Ð¤ò»ÈÍѤ·¤Æ¤¤¤Þ¤¹¡£½©·îÅÅ»Ò ¤Ç 200±ß¤¯¤é¤¤¤À¤Ã¤¿¡Ä¤«¤Ê¡©
¢ÀÖÏÈÆ⤬ ADM3202 ¤Ç¤¹¡£
¡û ´ØÏ¢¥ì¥¸¥¹¥¿
USART ´ØÏ¢¤Î¥ì¥¸¥¹¥¿¤ò°Ê²¼¤Ë¾Ò²ð¤·¤Þ¤¹¡£¥Þ¥¯¥í¤Ë¤Ä¤¤¤Æ¤Ï¡¢avr-libc ¤Ç»ÈÍѤǤ¤ëÄêµÁ̾¤ò·ÇºÜ¤·¤Æ¤¤¤Þ¤¹¡£¤Þ¤¿¡¢ÆüËܸì¤Î¥ì¥¸¥¹¥¿Ì¾¤È¥Ó¥Ã¥È̾¤Ï¡¢±Ñ¸ì¤ÎÄêµÁ̾¾Î¤òŬÅö¤ËÌõ¤·¤Æ¤Þ¤¹¡£
- USART ¥Ç¡¼¥¿¥ì¥¸¥¹¥¿ (UDR)
¥Ó¥Ã¥ÈÈÖ¹æ 7 6 5 4 3 2 1 0 ¥Þ¥¯¥í - - - - - - - - ½é´üÃÍ 0 0 0 0 0 0 0 0 Á÷¼õ¿®¥Ç¡¼¥¿¤ò³ÊǼ¤¹¤ë¥Ç¡¼¥¿¥ì¥¸¥¹¥¿¤Ç¤¹¡£Ëܥ쥸¥¹¥¿¤òÆɤळ¤È¤Ç¡¢¼õ¿®¥Ç¡¼¥¿¥Ð¥Ã¥Õ¥¡Æâ¤Î¥Ç¡¼¥¿¤òÆɤ߹þ¤à¤³¤È¤¬¤Ç¤¤Þ¤¹¡£¤Þ¤¿¡¢Ëܥ쥸¥¹¥¿¤Ë½ñ¤¹þ¤à¤³¤È¤ÇÁ÷¿®¥Ð¥Ã¥Õ¥¡¤Ë¥Ç¡¼¥¿¤¬½ñ¤¹þ¤Þ¤ì¤Þ¤¹¡£
Á÷¿®¥Ð¥Ã¥Õ¥¡¤Ë½ñ¤¹þ¤Þ¤ì¤¿¥Ç¡¼¥¿¤Ï¡¢Á÷¿®¥·¥Õ¥È¥ì¥¸¥¹¥¿¤È¤¤¤¦¥ì¥¸¥¹¥¿¤¬¶õ¤¯¤È¤½¤³¤ËÁ÷¤é¤ì¡¢¤½¤Î¸å TXD ¥Ô¥ó¤Ë½ÐÎϤµ¤ì¤Þ¤¹¡£Á÷¿®¥Ð¥Ã¥Õ¥¡¤Ø¤Î½ñ¤¹þ¤ß¤Ï¡¢¸ý½Ò¤¹¤ë UCSRA ¥ì¥¸¥¹¥¿¤Î UDRE ¥Õ¥é¥°¤Ë 1 ¤¬ÀßÄꤵ¤ì¤Æ¤¤¤ë»þ¤Î¤ß²Äǽ¤Ç¤¹¡£¤½¤ì°Ê³°¤Ï½ñ¤¹þ¤ó¤À¥Ç¡¼¥¿¤Ï̵»ë¤µ¤ì¤Þ¤¹¡£
Ëܥ쥸¥¹¥¿¤Ï 8 ¥Ó¥Ã¥ÈÉý¤Ç¤¢¤ë¤¿¤á¡¢9 ¥Ó¥Ã¥È¥Ç¡¼¥¿¤òÁ÷¼õ¿®¤¹¤ë¾ì¹ç¤Ë¤Ï¾å°Ì 1 ¥Ó¥Ã¥È¤ò¸å½Ò¤¹¤ë UCSRB ¥ì¥¸¥¹¥¿Æâ¤Î¥Ó¥Ã¥È¤È¤ä¤ê¤È¤ê¤·¤Ê¤±¤ì¤Ð¤Ê¤ê¤Þ¤»¤ó¡£
- USART À©¸æ/¾õÂ֥쥸¥¹¥¿A (UCSRA)
¥Ó¥Ã¥ÈÈÖ¹æ 7 6 5 4 3 2 1 0 ¥Þ¥¯¥í RXC TXC UDRE FE DOR PE U2X MPCM ½é´üÃÍ 0 0 1 0 0 0 0 0 ³Æ¥Ó¥Ã¥È¤ÎÀâÌÀ¤ò°Ê²¼¤Ë½ñ¤¤Þ¤¹¡£
¥Ó¥Ã¥È̾ ÆâÍÆ RXC¡Ê¼õ¿®´°Î»¥Õ¥é¥°¡Ë ¼õ¿®¥Ð¥Ã¥Õ¥¡Æâ¤Ë¤Þ¤ÀÆɤó¤Ç¤¤¤Ê¤¤¥Ç¡¼¥¿¤¬¤¢¤ë¾ì¹ç¤Ë 1 ¤¬ÀßÄꤵ¤ì¡¢UDR ¤òÆɤ߹þ¤à¤³¤È¤Ç¶õ¤Ë¤Ê¤ë¤È 0 ¤Ë¤Ê¤ê¤Þ¤¹¡£ TXC¡ÊÁ÷¿®´°Î»¥Õ¥é¥°¡Ë Á÷¿®¥Ð¥Ã¥Õ¥¡Æâ¤Ë¥Ç¡¼¥¿¤¬Ìµ¤¤¾ì¹ç¤Ë 1 ¤¬ÀßÄꤵ¤ì¤Þ¤¹¡£Á÷¿®´°Î»³ä¤ê¹þ¤ß»þ¤Ë¼«Æ°Åª¤Ë 0 ¤È¤Ê¤ë¾¡¢1 ¤ò½ñ¤¹þ¤à¤³¤È¤Ç 0 ¤È¤Ê¤ê¤Þ¤¹¡£ UDRE¡ÊÁ÷¿®¥Ç¡¼¥¿¥ì¥¸¥¹¥¿¶õ¤¥Õ¥é¥°¡Ë Á÷¿®¥Ð¥Ã¥Õ¥¡¤¬¶õ¤Ç¥Ç¡¼¥¿¼õ¼è½àÈ÷¤¬´°Î»¤·¤Æ¤¤¤ë¾ì¹ç¤Ë 1 ¤¬ÀßÄꤵ¤ì¤Þ¤¹¡£ FE¡Ê¥Õ¥ì¡¼¥ß¥ó¥°¥¨¥é¡¼¥Õ¥é¥°¡Ë ¥Õ¥ì¡¼¥ß¥ó¥°¥¨¥é¡¼È¯À¸»þ¤Ë 1 ¤¬ÀßÄꤵ¤ì¤Þ¤¹¡£UDR ¤òÆɤà¤È¥¯¥ê¥¢¤µ¤ì¤Æ¤·¤Þ¤¦¤¿¤á¡¢UDR ¤òÆɤàÁ°¤Ë¥¨¥é¡¼È½Ä꤬ɬÍפǤ¹¡£¤Þ¤¿¡¢UCSRA ¤Ø¤Î½ñ¤¹þ¤ß¤ò¹Ô¤¦¾ì¹ç¤Ï¡¢ËܥӥåȤò¤¢¤é¤«¤¸¤á 0 ¤ËÀßÄꤹ¤ëɬÍפ¬¤¢¤ê¤Þ¤¹¡£ DOR¡Ê¥Ç¡¼¥¿¥ª¡¼¥Ð¥é¥ó¥Õ¥é¥°¡Ë ¥Ç¡¼¥¿¥ª¡¼¥Ð¥é¥óȯÀ¸»þ¤Ë 1 ¤¬ÀßÄꤵ¤ì¤Þ¤¹¡£FE ƱÍÍ UDR ¤òÆɤà¤È¥¯¥ê¥¢¤µ¤ì¤Þ¤¹¡£¤Þ¤¿¡¢UCSRA ¤Ë½ñ¤¹þ¤ß¤ò¹Ô¤¦¾ì¹ç¤ÏËܥӥåȤò¤¢¤é¤«¤¸¤á 0 ¤ËÀßÄꤷ¤Æ¤¯¤À¤µ¤¤¡£ PE¡Ê¥Ñ¥ê¥Æ¥£¥¨¥é¡¼¥Õ¥é¥°¡Ë ¥Ñ¥ê¥Æ¥£¥¨¥é¡¼È¯À¸»þ¤Ë 1 ¤¬ÀßÄꤵ¤ì¤Þ¤¹¡£FE ƱÍÍ UDR ¤òÆɤà¤È¥¯¥ê¥¢¤µ¤ì¤Þ¤¹¡£¤Þ¤¿¡¢UCSRA ¤Ë½ñ¤¹þ¤ß¤ò¹Ô¤¦¾ì¹ç¤ÏËܥӥåȤò¤¢¤é¤«¤¸¤á 0 ¤ËÀßÄꤷ¤Æ¤¯¤À¤µ¤¤¡£ U2X¡ÊÇÜ®µö²Ä¡Ë 1 ¤ò½ñ¤¹þ¤à¤È¡¢ÈóƱ´üÆ°ºî»þ¤Ë¥Ü¡¼¥ì¡¼¥È¤¬¼«Æ°Åª¤Ë 2 ÇܤËÀßÄꤵ¤ì¡¢ÈóƱ´üÄÌ¿®»þ¤Î¥Ç¡¼¥¿Å¾Á÷®ÅÙ¤¬Çܤˤʤê¤Þ¤¹¡£Æ±´üÄÌ¿®»þ¤Ï 0 ¤ËÀßÄꤷ¤Þ¤¹¡£ MPCM¡Ê¥Þ¥ë¥Á¥×¥í¥»¥Ã¥µÄÌ¿®Æ°ºî¡Ë 1 ¤ò½ñ¤¹þ¤à¤È¡¢¥Þ¥ë¥Á¥×¥í¥»¥Ã¥µÄÌ¿®Æ°ºî¤¬Í¸ú¤È¤Ê¤ê¤Þ¤¹¡£¥Þ¥ë¥Á¥×¥í¥»¥Ã¥µÄÌ¿®Æ°ºî¤È¤Ï¡¢Ê£¿ô¤Î ATmega8535 ¤Î USART ¤ò¥·¥ê¥¢¥ë¥Ð¥¹¤ÇÀܳ¤·¤¿¾ì¹ç¤Ë¡¢¥¢¥É¥ì¥¹¾ðÊó¤ò¥Õ¥ì¡¼¥à¤Ë´Þ¤á¤ë¤³¤È¤ÇÁê¼ê¤ò»ØÄꤷ¤ÆÄÌ¿®¤¹¤ë¤³¤È¤¬¤Ç¤¤ëÆ°ºî¤Ç¤¹¡£¤³¤Î¾ì¹ç¡¢¥¢¥É¥ì¥¹¾ðÊó¤ò´Þ¤Þ¤Ê¤¤¥Õ¥ì¡¼¥à¤ÏÇË´þ¤µ¤ì¤Þ¤¹¡£
- USART À©¸æ/¾õÂ֥쥸¥¹¥¿B (UCSRB)
¥Ó¥Ã¥ÈÈÖ¹æ 7 6 5 4 3 2 1 0 ¥Þ¥¯¥í RXCIE TXCIE UDRIE RXEN TXEN UCSZ2 RXB8 TXB8 ½é´üÃÍ 0 0 0 0 0 0 0 0 ³Æ¥Ó¥Ã¥È¤ÎÀâÌÀ¤ò°Ê²¼¤Ë½ñ¤¤Þ¤¹¡£
¥Ó¥Ã¥È̾ ÆâÍÆ RXCIE¡Ê¼õ¿®´°Î»³ä¤ê¹þ¤ßµö²Ä¡Ë 1 ¤òÀßÄꤹ¤ë¤È¡¢¼õ¿®´°Î»¤òÃΤ餻¤ë³ä¤ê¹þ¤ß¤¬Í¸ú¤Ë¤Ê¤ê¤Þ¤¹¡£ TXCIE¡ÊÁ÷¿®´°Î»³ä¤ê¹þ¤ßµö²Ä¡Ë 1 ¤òÀßÄꤹ¤ë¤È¡¢Á÷¿®´°Î»¤òÃΤ餻¤ë³ä¤ê¹þ¤ß¤¬Í¸ú¤Ë¤Ê¤ê¤Þ¤¹¡£ UDRIE¡ÊÁ÷¿®¥Ç¡¼¥¿¥ì¥¸¥¹¥¿¶õ¤³ä¤ê¹þ¤ßµö²Ä¡Ë 1 ¤òÀßÄꤹ¤ë¤È¡¢Á÷¿®¥Ç¡¼¥¿¥ì¥¸¥¹¥¿¶õ¤¤òÃΤ餻¤ë³ä¤ê¹þ¤ß¤¬Í¸ú¤Ë¤Ê¤ê¤Þ¤¹¡£ RXEN¡Ê¼õ¿®µö²Ä¡Ë 1 ¤òÀßÄꤹ¤ë¤³¤È¤Ç¡¢USART ¤Ë¤è¤ë¥Ç¡¼¥¿¼õ¿®¤¬Í¸ú¤Ë¤Ê¤ê¤Þ¤¹¡£RXD ¥Ô¥ó(14ÈÖ¥Ô¥ó) ¤ÏÆþ½ÐÎÏ¥Ô¥ó PORTD0 ¤È¤Î·óÍÑ¥Ô¥ó¤Ç¤¹¤¬¡¢RXEN ¤Ë 1 ¤òÀßÄꤹ¤ë¤È PORTD0 ¤ÎÆ°ºî¤¬Ìµ¸ú¤È¤Ê¤ê¤Þ¤¹¡£ TXEN¡ÊÁ÷¿®µö²Ä¡Ë 1 ¤òÀßÄꤹ¤ë¤³¤È¤Ç¡¢USART ¤Ë¤è¤ë¥Ç¡¼¥¿Á÷¿®¤¬Í¸ú¤Ë¤Ê¤ê¤Þ¤¹¡£TXD ¥Ô¥ó(15ÈÖ¥Ô¥ó) ¤ÏÆþ½ÐÎÏ¥Ô¥ó PORTD1 ¤È¤Î·óÍÑ¥Ô¥ó¤Ç¤¹¤¬¡¢TXEN ¤Ë 1 ¤òÀßÄꤹ¤ë¤È PORTD1 ¤ÎÆ°ºî¤¬Ìµ¸ú¤È¤Ê¤ê¤Þ¤¹¡£ UCSZ2¡Ê¥Ç¡¼¥¿¥Ó¥Ã¥ÈĹÁªÂò2¡Ë ¸å½Ò¤¹¤ë UCSRC ¥ì¥¸¥¹¥¿¤Î UCSZ1/UCSZ0 ¥Ó¥Ã¥È¤È¹ç¤»¤¿ 3 ¥Ó¥Ã¥È¤Ç¡¢Á÷¼õ¿®¥Ç¡¼¥¿¥Ó¥Ã¥È¿ô¤ò·èÄꤷ¤Þ¤¹¡£ RXB8¡Ê¼õ¿®¥Ç¡¼¥¿¥Ó¥Ã¥È8¡Ë Á÷¼õ¿®¥Ç¡¼¥¿¥Ó¥Ã¥È¤¬ 9 ¤Î¾ì¹ç¤Ë¡¢¼õ¿®¥Ç¡¼¥¿¤Î¾å°Ì 1 ¥Ó¥Ã¥È¤¬¤³¤³¤Ë³ÊǼ¤µ¤ì¤Þ¤¹¡£ËܥӥåȤòÆɤó¤À¸å¤Ë¡¢UDR ¤«¤é²¼°Ì 8 ¥Ó¥Ã¥È¤òÆɤó¤Ç¤¯¤À¤µ¤¤¡£ TXB8¡ÊÁ÷¿®¥Ç¡¼¥¿¥Ó¥Ã¥È8¡Ë Á÷¼õ¿®¥Ç¡¼¥¿¥Ó¥Ã¥È¤¬ 9 ¤Î¾ì¹ç¤Ë¡¢Á÷¿®¥Ç¡¼¥¿¤Î¾å°Ì 1 ¥Ó¥Ã¥È¤ò¤³¤³¤Ë³ÊǼ¤·¤Þ¤¹¡£UDR ¤Ë²¼°Ì 8 ¥Ó¥Ã¥È¤ò½ñ¤¹þ¤àÁ°¤ËËܥӥåȤ˽ñ¤¹þ¤ó¤Ç¤¯¤À¤µ¤¤¡£
- USART À©¸æ/¾õÂ֥쥸¥¹¥¿C (UCSRC)
¥Ó¥Ã¥ÈÈÖ¹æ 7 6 5 4 3 2 1 0 ¥Þ¥¯¥í URSEL UMSEL UPM1 UPM0 USBS UCSZ1 UCSZ0 UCPOL ½é´üÃÍ 1 0 0 0 0 1 1 0 UCSRC ¥ì¥¸¥¹¥¿¤Ï¸å½Ò¤¹¤ë UBRHH ¥ì¥¸¥¹¥¿¤È¥¢¥É¥ì¥¹¤¬½Å¤Ê¤ëÍͤǤ¹¡£UCSRC ¤Ë½ñ¤¹þ¤ß¤ò¹Ô¤¦¾ì¹ç¡¢ºÇ¾å°Ì¥Ó¥Ã¥È(URSEL)¤Ï 1 ¤È¤·¤Ê¤±¤ì¤Ð¤Ê¤ê¤Þ¤»¤ó¡£URSEL ¤¬ 0 ¤È¤Ê¤ëÃͤò UCSRC ¤Ë½ñ¤¹þ¤â¤¦¤È¤·¤Æ¤â¡¢¼ÂºÝ¤Ë¤Ï UBRHH ¤Ë½ñ¤¹þ¤Þ¤ì¤Æ¤·¤Þ¤¤¤Þ¤¹¡£¤Þ¤¿ UCSRC ¤ÎÆâÍƤòÆɤ߹þ¤à¾ì¹ç¤Ï¡¢¤Þ¤º UBRRH ¤òÆɤ߹þ¤ß¡¢¼¡¤Î¥¯¥í¥Ã¥¯¥µ¥¤¥¯¥ë¤Ç UCSRC ¤òÆɤ߹þ¤Þ¤Ê¤¤¤È¤¤¤±¤Ê¤¤ÍͤǤ¹¡£UBRHH ¤È UCSRC ¤ÎÆɤ߹þ¤ß¤ÏϢ³¤·¤Æ¤¤¤Ê¤±¤ì¤Ð¤¤¤±¤Ê¤¤¤¿¤á¡¢ÅÓÃæ¤Ç³ä¤ê¹þ¤ßÅù¤¬µ¯¤¤Ê¤¤Íͤˤ·¤Ê¤±¤ì¤Ð¤Ê¤ê¤Þ¤»¤ó¡£
³Æ¥Ó¥Ã¥È¤ÎÀâÌÀ¤ò°Ê²¼¤Ë½ñ¤¤Þ¤¹¡£
¥Ó¥Ã¥È̾ ÆâÍÆ URSEL¡Ê¥ì¥¸¥¹¥¿ÁªÂò¡Ë UCSRC ½ñ¤¹þ¤ß»þ¤Ë 1 ¤È¤¹¤ë¤³¤È¤Ç¡¢UCSRC ¤Ø¤Î½ñ¤¹þ¤ß¤ò»ØÄꤷ¤Þ¤¹¡£¤Þ¤¿¡¢UCSRC ¤ÎÆɤ߹þ¤ß»þ¤Ë¤Ï 1 ¤È¤Ê¤Ã¤Æ¤¤¤Þ¤¹¡£ UMSEL¡ÊÆ°ºî¥â¡¼¥ÉÁªÂò¡Ë 0 ¤òÀßÄꤹ¤ë¤È USART ÈóƱ´üÆ°ºî¡¢1 ¤òÀßÄꤹ¤ë¤È USART Ʊ´üÆ°ºî¤¬ÁªÂò¤µ¤ì¤Þ¤¹¡£ UPM1/UPM0¡Ê¥Ñ¥ê¥Æ¥£Æ°ºîÁªÂò¡Ë ÁȤ߹ç¤ï¤»¤Ë¤è¤Ã¤Æ¥Ñ¥ê¥Æ¥£Æ°ºî¤ò·èÄꤷ¤Þ¤¹¡£ UPM1 UPM0 Æ°ºîÆâÍÆ 0 0 ̵¸ú 0 1 ¾ÍèÍѤÎͽÌóÃÍ 1 0 ¶ö¿ô¥Ñ¥ê¥Æ¥£ 1 1 ´ñ¿ô¥Ñ¥ê¥Æ¥£ USBS¡Ê¥¹¥È¥Ã¥×¥Ó¥Ã¥È¿ôÁªÂò¡Ë 0 ¤òÀßÄꤹ¤ë¤È¥¹¥È¥Ã¥×¥Ó¥Ã¥È¿ô¤¬ 1 ¥Ó¥Ã¥È¤Ë¡¢1 ¤òÀßÄꤹ¤ë¤È¥¹¥È¥Ã¥×¥Ó¥Ã¥È¿ô¤¬ 2 ¥Ó¥Ã¥È¤Ë¤Ê¤ê¤Þ¤¹¡£ UCSZ1/UCSZ0¡Ê¥Ç¡¼¥¿¥Ó¥Ã¥ÈĹÁªÂò¡Ë UCSRB ¥ì¥¸¥¹¥¿¤Î UCSZ2 ¥Ó¥Ã¥È¤È¹ç¤»¤Æ¡¢¥Ç¡¼¥¿¥Ó¥Ã¥ÈŤò·èÄꤷ¤Þ¤¹¡£ UCSZ2,1,0 ¥Ç¡¼¥¿¥Ó¥Ã¥ÈĹ 0,0,0 5¥Ó¥Ã¥È 0,0,1 6¥Ó¥Ã¥È 0,1,0 7¥Ó¥Ã¥È 0,1,1 8¥Ó¥Ã¥È 1,0,0/1,0,1/1,1,0 ¾ÍèÍѤÎͽÌó¹àÌÜ 1,1,1 9¥Ó¥Ã¥È UCPOL¡Ê¥¯¥í¥Ã¥¯¶ËÀÁªÂò¡Ë Ʊ´üÆ°ºî»þ¤ËŬÍѤµ¤ì¡¢Æ±´ü¥¯¥í¥Ã¥¯¡ÊXCK¡¢1ÈÖ¥Ô¥ó¡ÊPORTB0 ¤È¤Î·óÍÑ¥Ô¥ó¡Ë¡Ë¤È¥Ç¡¼¥¿Á÷¼õ¿®Æ°ºî¤Î´Ø·¸¤òÀßÄꤷ¤Þ¤¹¡£ UCPOL Á÷¿®¥Ç¡¼¥¿Á÷½Ð ¼õ¿®¥Ç¡¼¥¿¼èÆÀ 0 XCK ¤ÎΩ¤Á¾å¤¬¤ê¥¨¥Ã¥¸ XCK ¤ÎΩ¤Á²¼¤¬¤ê¥¨¥Ã¥¸ 1 XCK ¤ÎΩ¤Á²¼¤¬¤ê¥¨¥Ã¥¸ XCK ¤ÎΩ¤Á¾å¤¬¤ê¥¨¥Ã¥¸
- USART ¥Ü¡¼¥ì¡¼¥È¥ì¥¸¥¹¥¿ (UBRRH¡¢UBRRL)
UBRR ¥ì¥¸¥¹¥¿¤Ï¡¢USART ¤Î¥Ü¡¼¥ì¡¼¥È¡ÊžÁ÷®ÅÙ¡¢¥Ó¥Ã¥È/sec¡Ë¤ò´ð¤Ë·×»»¤·¤¿Ã͡ʥܡ¼¥ì¡¼¥Èʬ¼þÃͤȸƤ֤餷¤¤¡©¡Ë¤òÀßÄꤹ¤ë¤¿¤á¤Î¥ì¥¸¥¹¥¿¤Ç¤¹¡£ÃÍ¤Ï 12 ¥Ó¥Ã¥È¤Çɽ¸½¤µ¤ì¡¢¾å°Ì 4 ¥Ó¥Ã¥È¤ò UBRRH ¥ì¥¸¥¹¥¿¤Ë¡¢²¼°Ì 8 ¥Ó¥Ã¥È¤ò UBRRL ¥ì¥¸¥¹¥¿¤Ë½ñ¤¹þ¤ß¤Þ¤¹¡£¥Ü¡¼¥ì¡¼¥Èʬ¼þÃͤϡ¢°Ê²¼¤ÎÍͤˤ·¤Æ·×»»¤·¤Þ¤¹¡£
Æ°ºî¼ïÊÌ ¥Ü¡¼¥ì¡¼¥Èʬ¼þÃÍ ÈóƱ´üÆ°ºî¡Êɸ½à®ÅÙ¡Ë (¥·¥¹¥Æ¥à¥¯¥í¥Ã¥¯¼þÇÈ¿ô/(16 x ¥Ü¡¼¥ì¡¼¥È)) - 1 ÈóƱ´üÆ°ºî¡ÊÇÜ® U2X=1¡Ë (¥·¥¹¥Æ¥à¥¯¥í¥Ã¥¯¼þÇÈ¿ô/(8 x ¥Ü¡¼¥ì¡¼¥È)) - 1 Ʊ´üÆ°ºî (¥·¥¹¥Æ¥à¥¯¥í¥Ã¥¯¼þÇÈ¿ô/(2 x ¥Ü¡¼¥ì¡¼¥È)) - 1 --- UBRRH ---
¥Ó¥Ã¥ÈÈÖ¹æ 7 6 5 4 3 2 1 0 ¥Þ¥¯¥í URSEL ͽÌó ͽÌó ͽÌó - - - - ½é´üÃÍ 0 0 0 0 0 0 0 0 UCSRC ¤Î½ê¤ÇÀâÌÀ¤·¤¿Íͤˡ¢UBRRH ¥ì¥¸¥¹¥¿¤Ï UCSRC ¤È¥¢¥É¥ì¥¹¤¬½Å¤Ê¤Ã¤Æ¤¤¤Þ¤¹¡£UBRRH ¤Ë½ñ¤¹þ¤ß¤ò¹Ô¤¦¾ì¹ç¤Ï¡¢ºÇ¾å°Ì¥Ó¥Ã¥È(URSEL) ¤Ï 0 ¤È¤·¤Ê¤±¤ì¤Ð¤Ê¤ê¤Þ¤»¤ó¡£URSEL ¤¬ 1 ¤È¤Ê¤ëÃͤò UBRRH ¤Ë½ñ¤¹þ¤â¤¦¤È¤·¤Æ¤â¡¢¼ÂºÝ¤Ë¤Ï UCSRC ¤Ë½ñ¤¹þ¤Þ¤ì¤Æ¤·¤Þ¤¤¤Þ¤¹¡£
³Æ¥Ó¥Ã¥È¤ÎÀâÌÀ¤ò°Ê²¼¤Ë½ñ¤¤Þ¤¹¡£
¥Ó¥Ã¥È̾ ÆâÍÆ URSEL¡Ê¥ì¥¸¥¹¥¿ÁªÂò¡Ë UCSRC ½ñ¤¹þ¤ß»þ¤Ë 1 ¤È¤¹¤ë¤³¤È¤Ç¡¢UCSRC ¤Ø¤Î½ñ¤¹þ¤ß¤ò»ØÄꤷ¤Þ¤¹¡£¤Þ¤¿¡¢UCSRC ¤ÎÆɤ߹þ¤ß»þ¤Ë¤Ï 1 ¤È¤Ê¤Ã¤Æ¤¤¤Þ¤¹¡£ ¥Ó¥Ã¥È3 - ¥Ó¥Ã¥È0¡Ê¥Ü¡¼¥ì¡¼¥Èʬ¼þÃÍ¡Ë ¥Ü¡¼¥ì¡¼¥Èʬ¼þÃͤξå°Ì 4 ¥Ó¥Ã¥È¤òÀßÄꤷ¤Þ¤¹¡£ --- UBRRL ---
¥Ó¥Ã¥ÈÈÖ¹æ 7 6 5 4 3 2 1 0 ¥Þ¥¯¥í - - - - - - - - ½é´üÃÍ 0 0 0 0 0 0 0 0 ³Æ¥Ó¥Ã¥È¤ÎÀâÌÀ¤ò°Ê²¼¤Ë½ñ¤¤Þ¤¹¡£
¥Ó¥Ã¥È̾ ÆâÍÆ ¥Ó¥Ã¥È7 - ¥Ó¥Ã¥È 0¡Ê¥Ü¡¼¥ì¡¼¥Èʬ¼þÃÍ¡Ë ¥Ü¡¼¥ì¡¼¥Èʬ¼þÃͤβ¼°Ì 8 ¥Ó¥Ã¥È¤òÀßÄꤷ¤Þ¤¹¡£
¡û ²óÏ©¿Þ
LECTURE 1 ATmega8535 ¤ª¤è¤Ó»ÈÍѴĶ¤Ë¤Ä¤¤¤Æ ¤Ë·ÇºÜ¤·¤Æ¤¢¤ëÁ´ÂβóÏ©¿Þ¤ËÂФ·¤Æ¡¢ATMega8535 ¤Î RXD/TXD ¥Ô¥ó¡¢ADM3202(RS-232¥é¥¤¥ó¡¦¥É¥é¥¤¥Ð/¥ì¥·¡¼¥Ð) ¤ª¤è¤Ó D-SUB 9 ¥Ô¥ó¥³¥Í¥¯¥¿Éôʬ¤òÀÖÏȤǰϤó¤À¿Þ¤ò¤³¤Á¤é¤ËÃÖ¤¤¤Æ¤ª¤¤Þ¤¹¡£
ATmega8535 ¤Î RXD/TXD ¤ò¤½¤ì¤¾¤ì ADM3202 ¤Î R1OUT/T1IN ¤ËÀܳ¤·¡¢ADM3202 ¤Î R1IN/T1OUT ¤ò D-SUB 9 ¥Ô¥ó¥³¥Í¥¯¥¿¤ËÀܳ¤·¤Æ¤¤¤Þ¤¹¡£¤³¤ì¤Ë¤è¤ê¡¢PC ¤«¤é¤Î¥Ç¡¼¥¿¤Ï ADM3202 ¤Î R1IN -> ATmega8535 ¤Î RXD ¤ÈÆþ¤ê¡¢ATmega8535 ¤«¤é¤Î¥Ç¡¼¥¿¤Ï ATMega8535 ¤Î TXD -> ADM3202 ¤Î T1IN -> ADM3202 ¤Î T1OUT -> PC ¤Èή¤ì¤Þ¤¹¡£
¡û ¥µ¥ó¥×¥ë¥×¥í¥°¥é¥à¤ÈÀâÌÀ
USART ¤ò»È¤Ã¤Æ¥·¥ê¥¢¥ëÄÌ¿®¤ò¹Ô¤¦¥µ¥ó¥×¥ë¥×¥í¥°¥é¥à¤ò 03usart.tar.gz ¤«¤é¥À¥¦¥ó¥í¡¼¥É¤Ç¤¤Þ¤¹¡£¤Ï¤¸¤á¤Ë ¤Î¥Ú¡¼¥¸¤«¤é¡¢Á´¤Æ¤Î¥µ¥ó¥×¥ë¥×¥í¥°¥é¥à¤ò ¸Ç¤á¤¿ tar ¥Õ¥¡¥¤¥ë¤ò¥À¥¦¥ó¥í¡¼¥É¤¹¤ë¤³¤È¤â¤Ç¤¤Þ¤¹¡£
Ãæ¤Ë¤Ï
- Makefile
- util.h
- util.c
- usart.h
- usart.c
- main-int.c ¡Ê¼õ¿®´°Î»³ä¤ê¹þ¤ß»ÈÍÑ»þ¤Î main¡Ë
- main-noint.c ¡Ê¼õ¿®´°Î»³ä¤ê¹þ¤ß̤»ÈÍÑ»þ¤Î main¡Ë
¤¬³ÊǼ¤µ¤ì¤Æ¤¤¤Þ¤¹¡£util.h/util.c ¤Ë¤Ä¤¤¤Æ¤Ï LECTURE 2 LED ¤òÅÀÌǤµ¤»¤ë ¤ÎÆâÍƤÈƱ¤¸¤Ç¡¢Makefile ¤Ï¥¿¡¼¥²¥Ã¥È¤¬Áý¤¨¤Æ¤¤¤Þ¤¹¤¬¤â¤Û¤È¤ó¤ÉÊѤï¤é¤Ê¤¤¤Î¤Ç¡¢¤½¤ì°Ê³°¤Ë¤Ä¤¤¤Æ°Ê²¼¤ËºÜ¤»¤Þ¤¹¡£¤Ê¤ª¡¢°Ê²¼¤Ë·ÇºÜ¤·¤Æ¤¤¤ë¥½¡¼¥¹¥³¡¼¥É¤Ë¤Ï½¤Àµ BSD ¥é¥¤¥»¥ó¥¹¾ò¹à¤ò´Þ¤á¤Æ¤¤¤Þ¤»¤ó¤¬¡¢¥½¡¼¥¹¥³¡¼¥ÉÇÛÉۤϽ¤Àµ BSD ¥é¥¤¥»¥ó¥¹¤Ë½¾¤¤¤Þ¤¹¡Ê¤Ï¤¸¤á¤Ë ¤Î¥Ú¡¼¥¸¤ÎÌÈÀÕ»ö¹à¤ò»²¾È¤·¤Æ¤¯¤À¤µ¤¤¡Ë¡£
¤³¤ì¤Þ¤Ç¤Ë¥Ò¥å¡¼¥º¥Ó¥Ã¥È¤ÎÀßÄê¤ò¹Ô¤Ã¤¿¤³¤È¤¬°ìÅÙ¤â̵¤¤¾ì¹ç¤Ï¡¢¤Þ¤ººÇ½é¤Ë make fuse ¤ò¼Â¹Ô¤·¤Æ¤¯¤À¤µ¤¤¡£fuse ¥¿¡¼¥²¥Ã¥È¤Ë¤Ä¤¤¤Æ¤Ï¡¢LECTURE 2 LED ¤òÅÀÌǤµ¤»¤ë ¤Î¥µ¥ó¥×¥ë¥×¥í¥°¥é¥à¤Î¹à¤ò»²¾È¤·¤Æ¤¯¤À¤µ¤¤¡£
make ¼Â¹Ô¸å¡¢³ä¤ê¹þ¤ß¤ò»ÈÍѤ·¤¿¥×¥í¥°¥é¥à¤ò¥¢¥Ã¥×¥í¡¼¥É¤¹¤ë¾ì¹ç¤Ï make uisp-int ¤ò¼Â¹Ô¤·¤Æ¤¯¤À¤µ¤¤¡£¤Þ¤¿¡¢³ä¤ê¹þ¤ß¤ò»ÈÍѤ·¤Æ¤¤¤Ê¤¤¥×¥í¥°¥é¥à¤ò¥¢¥Ã¥×¥í¡¼¥É¤¹¤ë¾ì¹ç¤Ï make uisp-noint ¤ò¼Â¹Ô¤·¤Æ¤¯¤À¤µ¤¤¡£¼ÂÁõÊýË¡¤¬°Û¤Ê¤ë¤À¤±¤ÇÆ°ºîÆâÍƤÏƱ¤¸¤Ç¤¹¡£
ËÜ¥µ¥ó¥×¥ë¥×¥í¥°¥é¥à¤Ï¡¢°Ê²¼¤Î¥Ñ¥é¥á¡¼¥¿¤ÇÄÌ¿®¤ò¹Ô¤¤¤Þ¤¹¡£
- ÈóƱ´üÆ°ºî
- ¥Ü¡¼¥ì¡¼¥È 9600 bps
- ¥Ñ¥ê¥Æ¥£Ìµ¤·
- ¥¹¥È¥Ã¥×¥Ó¥Ã¥È 1¥Ó¥Ã¥È
- 8 ¥Ó¥Ã¥È¥Ç¡¼¥¿Å¾Á÷
¼Â¸³ÍѲóÏ©¤È PC ¤Î¥·¥ê¥¢¥ë¥Ý¡¼¥È¡ÊD-SUB 9¥Ô¥ó¡Ë¤ò¥¹¥È¥ì¡¼¥È¥±¡¼¥Ö¥ë¤ÇÀܳ¤·¤Æ¤¯¤À¤µ¤¤¡£Windows ¤Ç¤¢¤ì¤Ð¥Ï¥¤¥Ñ¡¼¥¿¡¼¥ß¥Ê¥ëÅù¤ÎÄÌ¿®¥½¥Õ¥È¤Ç¾åµ¥Ñ¥é¥á¡¼¥¿¤òÀßÄꤷ¡¢¥Õ¥í¡¼À©¸æ̵¤·¤ÇÀܳ¤·¤Þ¤¹¡£PC ¦¤Ç²¿¤«¥¡¼¤ò²¡¤¹¤È¡¢"Received data = " ¤È¤¤¤¦É½¼¨¤Ë³¤¤¤Æ²¡¤·¤¿¥¡¼¤Îʸ»ú¤¬É½¼¨¤µ¤ì¤Þ¤¹¡£É½¼¨Ê¸»ú¤¬Ìµ¤¤¥¡¼¡ÊTab Åù¡Ë¤ò²¡¤¹¤Èʸ»úÉôʬ¤Ë¤Ï²¿¤âɽ¼¨¤µ¤ì¤Þ¤»¤ó¡£
¢¼Â¸³ÍÑ´ðÈĤȥΡ¼¥ÈPC ¤ò¥·¥ê¥¢¥ë¥±¡¼¥Ö¥ë¤ÇÀܳ¤·¤Æ¤¤¤ë½ê
¢ÄÌ¿®¥½¥Õ¥È¤Ë¤Ï¤³¤ÎÍͤËɽ¼¨¤µ¤ì¤Þ¤¹¡£
- usart.h
1 #ifndef ___USART_H_INCLUDED_ 2 #define ___USART_H_INCLUDED_ 3 4 #include <inttypes.h> 5 6 7 /* ³ä¤ê¹þ¤ß»ÈÍÑ/Èó»ÈÍÑÁªÂò¥Þ¥¯¥í */ 8 #define USART_NO_INT 0 9 #define USART_USE_INT 1 10 11 12 void usart_set_rx_interrupt(void (*func)(void)); 13 void usart_init(int use_interrupt); 14 int8_t usart_rx_char(void); 15 void usart_tx_char(int8_t c); 16 void usart_tx_string(char *string); 17 18 19 #endif 20 21 /* End of usart.h */
- 4 ¹ÔÌÜ
avr-libc ¤Î¥Ø¥Ã¥À¥Õ¥¡¥¤¥ë inttypes.h ¤ò¥¤¥ó¥¯¥ë¡¼¥É¤·¤Æ¤¤¤Þ¤¹¡£int8_t Åù¤Î¥µ¥¤¥º»ØÄê·¿¤ò»ÈÍѤ¹¤ë¤¿¤á¤ËɬÍפǤ¹¡£
- 7 ¡Á 9 ¹ÔÌÜ
usart_init() ¤Ë¤Æ¡¢¼õ¿®´°Î»ÄÌÃγä¤ê¹þ¤ß¤ò»ÈÍѤ¹¤ë¤«¤É¤¦¤«¤ò»ØÄꤹ¤ë¤¿¤á¤Î¥Þ¥¯¥íÄêµÁ¤Ç¤¹¡£
- usart.c
1 #include <avr/interrupt.h> 2 #include <avr/io.h> 3 #include <inttypes.h> 4 5 #include "usart.h" 6 7 8 /* ¥Ü¡¼¥ì¡¼¥È */ 9 #define USART_BAUD 9600 10 11 12 static void (*rx_interrupt_handler)(void); 13 14 15 /* 16 * ¼õ¿®´°Î»»þ³ä¤ê¹þ¤ß¥Ï¥ó¥É¥é 17 * usart_init(USART_USE_INT) ¤È¤·¤¿¾ì¹ç¤Ë͸ú¤È¤Ê¤ë¡£ 18 */ 19 ISR(USART_RX_vect) 20 { 21 rx_interrupt_handler(); 22 } 23 24 25 /* 26 * ¼õ¿®´°Î»»þ³ä¤ê¹þ¤ß¥Ï¥ó¥É¥éÆâ¤Ç¼Â¹Ô¤¹¤ë 27 * ¥æ¡¼¥¶´Ø¿ô¤òÀßÄꤹ¤ë¡£ 28 */ 29 void 30 usart_set_rx_interrupt(void (*func)(void)) 31 { 32 rx_interrupt_handler = func; 33 } 34 35 36 /* 37 * USART ÄÌ¿®¥Ñ¥é¥á¡¼¥¿¤ò 9600bps/8N1 ¤ËÀßÄꤹ¤ë¡£ 38 * 39 * ¼õ¿®´°Î»»þ³ä¤ê¹þ¤ß¤ò»ÈÍѤ¹¤ë¾ì¹ç¡¢ËÜ´Ø¿ô¼Â¹Ô¸å¤Ë 40 * sei() ¤ò¼Â¹Ô¤¹¤ë¤³¤È¤Ç͸ú¤È¤Ê¤ë¡£ 41 */ 42 void 43 usart_init(int use_interrupt) 44 { 45 /* ¥Ü¡¼¥ì¡¼¥ÈÀßÄê( (F_CPU/(16*BAUD)) - 1 ¤Ç·×»»¤¹¤ë ) */ 46 uint16_t baud = (F_CPU / (16UL * USART_BAUD)) - 1; 47 48 UBRRH = (uint8_t)(baud >> 8); 49 UBRRL = (uint8_t)baud; 50 51 if (use_interrupt) { 52 /* Tx/Rx/¼õ¿®´°Î»»þ³ä¤ê¹þ¤ß¤ò͸ú²½ */ 53 UCSRB = _BV(TXEN) | _BV(RXEN) | _BV(RXCIE); 54 } 55 else { 56 /* Tx/Rx ¤ò͸ú²½ */ 57 UCSRB = _BV(TXEN) | _BV(RXEN); 58 } 59 } 60 61 62 /* 63 * 1 ʸ»ú¼õ¿®½èÍý 64 * ¥Õ¥ì¡¼¥à¥¨¥é¡¼¤ª¤è¤Ó¥ª¡¼¥Ð¥é¥óȯÀ¸»þ¤Ï -1 ¤òÊÖ¤¹¡£ 65 */ 66 int8_t 67 usart_rx_char(void) 68 { 69 loop_until_bit_is_set(UCSRA, RXC); /* 1 ¤Ë¤Ê¤Ã¤¿¤é¼õ¿®´°Î» */ 70 71 if ((UCSRA & _BV(FE)) || (UCSRA & _BV(DOR))) { 72 return -1; 73 } 74 75 return UDR; 76 } 77 78 79 /* 80 * 1 ʸ»úÁ÷¿®½èÍý 81 */ 82 void 83 usart_tx_char(int8_t c) 84 { 85 loop_until_bit_is_set(UCSRA, UDRE); /* 1 ¤Ë¤Ê¤Ã¤¿¤éÁ÷¿®²Äǽ */ 86 UDR = c; 87 } 88 89 90 /* 91 * ʸ»úÎóÁ÷¿®½èÍý 92 */ 93 void 94 usart_tx_string(char *string) 95 { 96 while (*string != '\0') { 97 usart_tx_char(*string); 98 string ++; 99 } 100 } 101 102 103 /* End of usart.c */
- 1 ¹ÔÌÜ
¤³¤³¤Ç¥¤¥ó¥¯¥ë¡¼¥É¤·¤Æ¤¤¤ë avr/interrupt.h ¤Ï¡¢19 ¹ÔÌܤǻÈÍѤ·¤Æ¤¤¤ë³ä¤ê¹þ¤ß¥Ï¥ó¥É¥éÄêµÁ¤Î¤¿¤á¤ËɬÍפǤ¹¡£
- 8 ¡Á 9 ¹ÔÌÜ
ËÜ¥×¥í¥°¥é¥à¤¬»ÈÍѤ¹¤ë¥Ü¡¼¥ì¡¼¥È¤ò 9600 bps ¸ÇÄê¤È¤·¤ÆÄêµÁ¤·¤Æ¤¤¤Þ¤¹¡£
- 15 ¡Á 22 ¹ÔÌÜ
¼õ¿®´°Î»ÄÌÃγä¤ê¹þ¤ß¤ò͸ú¤Ë¤·¤¿¾ì¹ç¤Ë»ÈÍѤ¹¤ë¡¢³ä¤ê¹þ¤ß¥Ï¥ó¥É¥é¤òµ½Ò¤·¤Æ¤¤¤Þ¤¹¡£ avr-libc ¤ò»È¤¦¾ì¹ç¡¢³ä¤ê¹þ¤ß¥Ï¥ó¥É¥é¤Ï°Ê²¼¤ÎÍͤËÀë¸À¤·¤Þ¤¹¡£
ISR(³ä¤ê¹þ¤ß¼ïÊ̤òɽ¤¹¥Þ¥¯¥í) { /* ³ä¤ê¹þ¤ß¥Ï¥ó¥É¥é¤Î¼Â½èÍý¤ò¤³¤³¤Ëµ½Ò¤¹¤ë */ }
³ä¤ê¹þ¤ß¼ïÊ̤òɽ¤¹¥Þ¥¯¥í¤Ï¡¢avr/interrupt.h ¤ËÄêµÁ¤µ¤ì¤Æ¤¤¤Þ¤¹¡£¤³¤³¤Ç¤Ï¡¢ATmega8535 ¤Î USART ¼õ¿®´°Î»³ä¤ê¹þ¤ß¤òɽ¤¹ USART_RX_vect ¤ò»ØÄꤷ¤Æ¤¤¤Þ¤¹¡£¼ÂºÝ¤Ë¤Ï¸å½Ò¤¹¤ë usart_set_rx_interrupt() ¤ÇÅÐÏ¿¤µ¤ì¤¿¥æ¡¼¥¶´Ø¿ô¤ò¼Â¹Ô¤¹¤ëÍͤ˼ÂÁõ¤·¤Æ¤¤¤Þ¤¹¡£
- 25 ¡Á 33 ¹ÔÌÜ (usart_set_rx_interrupt())
Á°½Ò¤Î USART ¼õ¿®´°Î»³ä¤ê¹þ¤ß¥Ï¥ó¥É¥éÆâ¤Ç¼ÂºÝ¤Ë¼Â¹Ô¤¹¤ë¡¢¥æ¡¼¥¶´Ø¿ô¤òÅÐÏ¿¤·¤Þ¤¹¡£
- 36 ¡Á 59 ¹ÔÌÜ (usart_init())
USART µ¡Ç½¤Î½é´ü²½¤ò¹Ô¤¤¤Þ¤¹¡£ºÇ½é¤Ë ´ØÏ¢¥ì¥¸¥¹¥¿ ¤Ë¤ÆÀâÌÀ¤·¤Æ¤¤¤ë¼°¤ò»È¤Ã¤Æ¥Ü¡¼¥ì¡¼¥È¤ËÂбþ¤·¤¿ UBRR Ãͤò·×»»¤·¡¢UBRRH / UBRRL ¤ËÀßÄꤷ¤Þ¤¹¡£ ¾¤ÎÀßÄêÍѥ쥸¥¹¥¿¤Ë¤Ä¤¤¤Æ¤Ï¥Ç¥Õ¥©¥ë¥È¤Ç
- ÈóƱ´üÆ°ºî
- ¥Ñ¥ê¥Æ¥£Ìµ¤·
- ¥¹¥È¥Ã¥×¥Ó¥Ã¥È 1 ¥Ó¥Ã¥È
- 8 ¥Ó¥Ã¥È¥Ç¡¼¥¿Å¾Á÷
¤Ç¤¢¤ë¤¿¤á¡¢UCSRB ¥ì¥¸¥¹¥¿¤Î TXEN / RXEN ¥Ó¥Ã¥È¤ò 1 ¤È¤¹¤ë¤³¤È¤Ç USART Á÷¼õ¿®¤ò͸ú¤Ë¤·¤Æ¤¤¤Þ¤¹¡£ ¼õ¿®´°Î»ÄÌÃγä¤ê¹þ¤ß¤ò͸ú¤Ë¤¹¤ë¾ì¹ç¤Ï¡¢Ê»¤»¤Æ RXCIE ¤ò 1 ¤È¤¹¤ë¤³¤È¤Ç͸ú¤Ë¤·¤Æ¤¤¤Þ¤¹¡£ ¼ÂºÝ¤Ë¤Ï¤³¤ì¤À¤±¤Ç¤Ï¼õ¿®´°Î»ÄÌÃγä¤ê¹þ¤ß¤ÏÁ÷¿®¤µ¤ì¤º¡¢avr/interrupt.h ¤ÇÄêµÁ¤µ¤ì¤Æ¤¤¤ë sei() ´Ø¿ô¤ò¼Â¹Ô¤¹¤ë¤³¤È¤Ç¡¢³ä¤ê¹þ¤ß¤½¤Î¤â¤Î¤ò͸ú¤È¤¹¤ëɬÍפ¬¤¢¤ê¤Þ¤¹¡£
- 62 ¡Á 76 ¹ÔÌÜ (usart_rx_char())
¼õ¿®¥Ð¥Ã¥Õ¥¡¤«¤é 1 ¥Ç¡¼¥¿¤ò¼èÆÀ¤·¤Þ¤¹¡£ ¤Þ¤º UCSRA ¥ì¥¸¥¹¥¿¤Î RXC ¥Ó¥Ã¥È¤¬ 1¡Ê¼õ¿®¥Ð¥Ã¥Õ¥¡¤Ë̤Æɥǡ¼¥¿Í¤ê¡Ë¤Ë¤Ê¤ë¤Þ¤ÇÂÔ¤Á¤Þ¤¹¡£ loop_until_bit_is_set(sfr, bit) ¤Ï¡¢sfr ¥ì¥¸¥¹¥¿¤Î bit È֥ӥåȤ¬ 1 ¤È¤Ê¤ë¤Þ¤Ç¥ë¡¼¥×¤·¤ÆÂԤĥޥ¯¥í¤Ç¤¹¡£ avr/io.h ¤ò¥¤¥ó¥¯¥ë¡¼¥É¤¹¤ì¤Ð»È¤¨¤Þ¤¹¡£
¼õ¿®´°Î»¤ò¸¡½Ð¤·¤¿¤é¡¢¥Ç¡¼¥¿¤òÆɤߤÀ¤¹Á°¤Ë UCSRA ¥ì¥¸¥¹¥¿¤Î FE ¤ª¤è¤Ó DOR ¥Ó¥Ã¥È¤òÄ´¤Ù¤Æ ¥Ç¡¼¥¿¥ª¡¼¥Ð¥é¥ó¤ä¥Õ¥ì¡¼¥ß¥ó¥°¥¨¥é¡¼¤¬È¯À¸¤·¤Æ¤¤¤Ê¤¤¤«¤É¤¦¤«³Îǧ¤·¤Æ¤¤¤Þ¤¹¡£ ¤¤¤º¤ì¤«¤ò¸¡½Ð¤·¤¿¾ì¹ç¤Ï -1 ¤òÊÖ¤·¤Æ¤¤¤Þ¤¹¡£ËÜ´Ø¿ô¤Ç¤Ï¥¨¥é¡¼¼ïÊ̤϶èÊ̤·¤Æ¤¤¤Þ¤»¤ó¡£
¥¨¥é¡¼¤¬È¯À¸¤·¤Æ¤¤¤Ê¤±¤ì¤Ð¡¢UDR ¥ì¥¸¥¹¥¿¤ÎÃͤòÆɤळ¤È¤Ç¼õ¿®¥Ð¥Ã¥Õ¥¡¤«¤é¥Ç¡¼¥¿¤ò¼èÆÀ¤·¤Æ½ªÎ»¤·¤Þ¤¹¡£
- 79 ¡Á 87 ¹ÔÌÜ (usart_tx_char())
Á÷¿®¥Ð¥Ã¥Õ¥¡¤ËÂФ·¤Æ 1 ¥Ç¡¼¥¿¤òÀßÄꤷ¤Þ¤¹¡£¤Þ¤º UCSRA ¥ì¥¸¥¹¥¿¤Î UDRE ¥Ó¥Ã¥È¤¬ 1 ¡ÊÁ÷¿®¥Ð¥Ã¥Õ¥¡¤¬¶õ¡Ë¤Ë¤Ê¤ë¤Þ¤ÇÂÔ¤Á¤Þ¤¹¡£Á÷¿®¥Ð¥Ã¥Õ¥¡¤¬¶õ¤Ë¤Ê¤Ã¤¿¤Î¤ò¸¡½Ð¤·¤¿¤é¡¢UDR ¤ËÃͤò½ñ¤¹þ¤à¤³¤È¤ÇÁ÷¿®¥Ð¥Ã¥Õ¥¡¤Ë¥Ç¡¼¥¿¤òÀßÄꤷ¤Æ½ªÎ»¤·¤Þ¤¹¡£
- 90 ¡Á 100 ¹ÔÌÜ (usart_tx_string())
ʸ»úÎóÁ÷¿®ÍѤδؿô¤Ç¤¹¡£1 ʸ»ú¤º¤Ä usart_tx_char() ¤ò»È¤Ã¤ÆÁ÷¿®¤·¤Æ¤¤¤ë¤À¤±¤Ç¤¹¡£
- main-int.c¡Ê¼õ¿®´°Î»³ä¤ê¹þ¤ß»ÈÍÑ»þ¤Î main¡Ë
1 #include <avr/interrupt.h> 2 #include <avr/sleep.h> 3 #include <inttypes.h> 4 5 #include "usart.h" 6 #include "util.h" 7 8 9 static void rx_interrupt(void); 10 11 12 int 13 main(void) 14 { 15 pullup(); 16 usart_init(USART_USE_INT); 17 usart_set_rx_interrupt(rx_interrupt); 18 sei(); 19 20 while (1) { 21 sleep_mode(); 22 } 23 24 return 0; /* ·Ù¹ð¤òÈò¤±¤ë¤¿¤á */ 25 } 26 27 28 static void 29 rx_interrupt(void) 30 { 31 char buf[20] = "Received data = \r\n"; 32 int index = 16; 33 34 int8_t recv_data = usart_rx_char(); 35 36 if ( recv_data == -1 ) { 37 usart_tx_string("Unknown data...\r\n"); 38 return; 39 } 40 41 buf[index] = recv_data; 42 usart_tx_string(buf); 43 } 44 45 /* End of main-int.c */
ËÜ¥Õ¥¡¥¤¥ë¤Ï¡¢¼õ¿®´°Î»³ä¤ê¹þ¤ß¤ò»È¤Ã¤Æ¼õ¿®½èÍý¤ò¹Ô¤¦¾ì¹ç¤Î main() ¤òµ½Ò¤·¤Æ¤¤¤Þ¤¹¡£
- 12 ¡Á 25 ¹ÔÌÜ (main())
- pullup() (pullup.h¡¢Á´ÆþÎϥݡ¼¥È¤ÎÆâÉô¥×¥ë¥¢¥Ã¥×¤ò͸ú²½)
- usart_init() (usart.h¡¢USART ½é´ü²½)
- usart_set_rx_interrupt() (usart.h¡¢¼õ¿®´°Î»³ä¤ê¹þ¤ß»þ¤Ë¼Â¹Ô¤¹¤ë¥æ¡¼¥¶´Ø¿ô¤ÎÅÐÏ¿)
¤ò¹Ô¤Ã¤¿¸å¡¢avr/interrupt.h ¤ÇÄêµÁ¤µ¤ì¤Æ¤¤¤ë sei() ¤ò¼Â¹Ô¤·¤Æ¥·¥¹¥Æ¥à¤Î³ä¤ê¹þ¤ß¤ò͸ú²½¤·¤Æ¤¤¤Þ¤¹¡£ ¤³¤ì¤Ë¤è¤ê USART Éô¤¬¥Ç¡¼¥¿¤ò¼õ¿®¤¹¤ëÅ٤ˡ¢¸å½Ò¤¹¤ë rx_interrupt() ¤¬¼Â¹Ô¤µ¤ì¤Þ¤¹¡£
°Ê¸å¡¢USART ¤ÎÁ÷¼õ¿®½èÍý¤Ï rx_interrupt() ¤ÎÃæ¤Ç¹Ô¤¦¤¿¤á¡¢20 ¡Á 22 ¹ÔÌܤÏ̵¸Â¥ë¡¼¥×¤È¤Ê¤Ã¤Æ¤¤¤Þ¤¹¡£ 21 ¹ÔÌܤΠsleep_mode() ¤Ï¡¢AVR ¤ò¥¹¥ê¡¼¥×¾õÂ֤˰ܹԤ¹¤ë¤¿¤á¤Î´Ø¿ô¤Ç¤¹¡£avr/sleep.h ¤ò¥¤¥ó¥¯¥ë¡¼¥É¤¹¤ë¤È»È¤¨¤Þ¤¹¡£¥¹¥ê¡¼¥×¾õÂ֤ˤϿô¼ïÎàͤꡢɬÍפʤé¤Ð set_sleep_mode() ¤ÇÁ°¤â¤Ã¤Æ»ØÄꤷ¤Æ¤ª¤¯É¬Íפ¬¤¢¤ê¤Þ¤¹¡£
#include <inttypes.h> #include <avr/sleep.h> void set_sleep_mode(uint8_t mode); void sleep_mode(void);
set_sleep_mode() ¤Ë»ØÄê¤Ç¤¤ëÃͤϰʲ¼¤Î¤¤¤º¤ì¤«¤Ç¤¹¡£
ÃÍ ¥¹¥ê¡¼¥×Æ°ºî¼ïÊÌ SLEEP_MODE_IDLE ¥¢¥¤¥É¥ëÆ°ºî SLEEP_MODE_ADC A/D ÊÑ´¹¥Î¥¤¥ºÄ㸺ưºî SLEEP_MODE_PWR_DOWN ¥Ñ¥ï¡¼¥À¥¦¥óÆ°ºî SLEEP_MODE_PWR_SAVE ¥Ñ¥ï¡¼¥»¡¼¥ÖÆ°ºî SLEEP_MODE_STANDBY ¥¹¥¿¥ó¥Ð¥¤Æ°ºî SLEEP_MODE_EXT_STANDBY ³ÈÄ¥¥¹¥¿¥ó¥Ð¥¤Æ°ºî ¤½¤ì¤¾¤ì¤ÎÆ°ºî¼ïÊ̤ξܺ٤ˤĤ¤¤Æ¤Ï¥Ç¡¼¥¿¥·¡¼¥ÈÅù¤ò¤´Í÷¤¯¤À¤µ¤¤¡£ ËÜ¥µ¥ó¥×¥ë¤Ç¤Ï²¿¤â»ØÄꤷ¤Æ¤¤¤Ê¤¤¤¿¤á¡¢SLEEP_MODE_IDLE ¤ò»ØÄꤷ¤¿¤Î¤ÈƱ¤¸¾õÂ֤ˤʤê¤Þ¤¹¡£
- 28 ¡Á 43 ¹ÔÌÜ (rx_interrupt())
¤Þ¤º usart_rx_char() ¤Ç¼õ¿®¥Ð¥Ã¥Õ¥¡¤«¤é¥Ç¡¼¥¿¤ò¼èÆÀ¤·¤Þ¤¹¡£¼õ¿®´°Î»³ä¤ê¹þ¤ß¥Ï¥ó¥É¥éÆâ¤Ç¤¢¤ê UCSRA ¥ì¥¸¥¹¥¿¤Î RXC ¥Ó¥Ã¥È¤¬ÀßÄꤵ¤ì¤Æ¤¤¤ë¤¿¤á¡¢usart_rx_char() ¤Ï¥Ö¥í¥Ã¥¯¤»¤º¤Ë¤¹¤°Ìá¤ê¤Þ¤¹¡£ ¥Ç¡¼¥¿¥ª¡¼¥Ð¥é¥ó¤ä¥Õ¥ì¡¼¥ß¥ó¥°¥¨¥é¡¼È¯À¸»þ¤Ë¤Ï "Unknown data..." ¤È¤¤¤¦Ê¸»úÎó¤ò usart_tx_string() ¤ÇÁ÷¿®¥Ð¥Ã¥Õ¥¡¤ËÀßÄꤷ¤Æ½ªÎ»¤·¤Þ¤¹¡£Í¸ú¤Ê¼õ¿®¥Ç¡¼¥¿¤¬¤¢¤ë¾ì¹ç¤Ï¡¢ "Received data = ..." ¤È¤¤¤¦Ê¸»úÎó¤òºîÀ®¤·¤Æ usart_tx_string() ¤ÇÁ÷¿®¥Ð¥Ã¥Õ¥¡¤ËÀßÄꤷ¤Æ¤¤¤Þ¤¹¡£
¤È¤³¤í¤Ç avr-libc ¤Ë¤Ï sprintf() / snprintf() Åù¤¬¤¢¤ë¤Î¤Ç¤¹¤¬¡¢ËÜ¥×¥í¥°¥é¥à¤Ç¤Ï ¼õ¿®¥Ç¡¼¥¿¤òľÀÜʸ»ú¥Ð¥Ã¥Õ¥¡¤Ë³ÊǼ¤·¤Æʸ»úÎó¤òºîÀ®¤·¤Æ¤¤¤Þ¤¹¡£ sprintf() Åù¤ò»È¤¤¤¿¤«¤Ã¤¿¤Î¤Ç¤¹¤¬¡¢¤¤¤¶»È¤Ã¤Æ avr-libc ¤È¥ê¥ó¥¯¤·¤Æ¤ß¤¿¤é¡¢ ¥¢¥Ã¥×¥í¡¼¥ÉÍѤΥХ¤¥Ê¥ê¥Õ¥¡¥¤¥ë¤¬ ATmega8535 ¤Î¥Õ¥é¥Ã¥·¥å¥á¥â¥ê¥µ¥¤¥º¤Ç¤¢¤ë 8 KB ¤ò¤¢¤Ã¤µ¤êĶ¤¨¤Æ¤·¤Þ¤¤¤Þ¤·¤¿¡ÊºÇŬ²½Í¤ê¤Ç¤â¡Ë¡£¤Î¤Ç»È¤Ã¤Æ¤Þ¤»¤ó¡£ ¼«Ê¬¤¬´ª°ã¤¤Åù¤·¤Æ¤¤¤Æ¡¢¼Â¤Ï 8 KB °ÊÆâ¤Ë¼ý¤á¤ëÊýË¡¤¬¤¢¤ë¤Î¤«¤â¤·¤ì¤Þ¤»¤ó¡£ ¤ªµ¤¤Å¤¤ÎÊý¤Ï¤¼¤Ò¶µ¤¨¤Æ¤¯¤À¤µ¤¤¡Ä
- main-noint.c¡Ê¼õ¿®´°Î»³ä¤ê¹þ¤ß̤»ÈÍÑ»þ¤Î main¡Ë
1 #include <inttypes.h> 2 3 #include "usart.h" 4 #include "util.h" 5 6 7 int 8 main(void) 9 { 10 char buf[20] = "Received data = \r\n"; 11 int index = 16; 12 13 pullup(); 14 usart_init(USART_NO_INT); 15 16 while (1){ 17 int8_t recv_data = usart_rx_char(); 18 19 if (recv_data == -1) { 20 usart_tx_string("Unknown data...\r\n"); 21 continue; 22 } 23 24 buf[index] = recv_data; 25 usart_tx_string(buf); 26 } 27 28 return 0; /* ·Ù¹ð¤òÈò¤±¤ë¤¿¤á */ 29 } 30 31 /* End of main-noint.c */
ËÜ¥Õ¥¡¥¤¥ë¤Ï¡¢¼õ¿®´°Î»³ä¤ê¹þ¤ß¤ò»È¤ï¤º¤Ë¼õ¿®½èÍý¤ò¹Ô¤¦¾ì¹ç¤Î main() ¤òµ½Ò¤·¤Æ¤¤¤Þ¤¹¡£
- 7 ¡Á 29 ¹ÔÌÜ (main())
- pullup() (pullup.h¡¢Á´ÆþÎϥݡ¼¥È¤ÎÆâÉô¥×¥ë¥¢¥Ã¥×¤ò͸ú²½)
- usart_init() (usart.h¡¢USART ½é´ü²½)
¤ò¹Ô¤Ã¤¿¸å¡¢while ¥ë¡¼¥×¤ËÆþ¤ê¤Þ¤¹¡£¥ë¡¼¥×Æâ¤Î½èÍý¤Ï¡¢main-int.c ¤Î rx_interrupt() ¤Ë¤ª¤±¤ë½èÍý¤ÈƱ¤¸¤Ç¤¹¡£¤¿¤À¤·¡¢main-noint.c ¤Î¾ì¹ç¤Ï¡¢usart_rx_char() ¤Ï¼õ¿®¥Ð¥Ã¥Õ¥¡¤Ë¥Ç¡¼¥¿¤¬³ÊǼ¤µ¤ì¤ë¤Þ¤Ç¥Ö¥í¥Ã¥¯¤·¤Þ¤¹¡£
°Ê¾å¤Ç²òÀâ¤Ï½ª¤ï¤ê¤Ç¤¹¡£PC ¤«¤é¼«Ê¬¤¬ºî¤Ã¤¿´ðÈĤËÂФ·¤ÆÄÌ¿®¤ò»î¤ß¤Æ¤¦¤Þ¤¯ÄÌ¿®¤Ç¤¤ë¤È¡¢·ë¹½³Ú¤·¤¤¤Ç¤¹¤è¡Ä