|
¡û ¤Ï¤¸¤á¤Ë
ÆÍÁ³¤Ç¤¹¤¬¡¢¥Þ¥¤¥³¥ó¤ÇÍ·¤ó¤Ç¤¤¤ë¤È¸÷¥»¥ó¥µ¤äÀÖ³°Àþ¥»¥ó¥µ¡¢²Ã®ÅÙ¥»¥ó¥µ¤È¤¤¤Ã¤¿ÁõÃÖ¤ò¼è¤êÉÕ¤±¤Æ¼þ°Ï¤Î¾õ¶·¤ò¼ý½¸¤·¤Æ¤ß¤¿¤¯¤Ê¤ê¤Þ¤»¤ó¤«¡©¡Ê¤Ê¤é¤Ê¤¤¿Í¤â¡¢¤½¤¦¤¤¤¦µ¤»ý¤Á¤Ë¤Ê¤Ã¤¿¤Ä¤â¤ê¤Ë¤Ê¤Ã¤ÆÆɤó¤Ç¤¯¤À¤µ¤¤¤Í¡Ä¡Ë
¤½¤¦¤¤¤¦¥»¥ó¥µ¤¬Ä󶡤·¤Æ¤¯¤ì¤ë¥Ç¡¼¥¿¤Ï¡¢ÂçÄñ¤Î¾ì¹ç¥¢¥Ê¥í¥°ÃͤǤ¹¡£¤½¤ì¤ËÂФ·¤Æ¥Þ¥¤¥³¥ó¤ÇľÀÜ°·¤¨¤ëÃÍ¤Ï 0/1 ¤Î¥Ç¥¸¥¿¥ëÃͤǤ¹¡£¤è¤Ã¤Æ¥Þ¥¤¥³¥ó¤Ç³Æ¼ï¥»¥ó¥µ¤ò»È¤ª¤¦¤È¤¹¤ë¤È¡¢¥¢¥Ê¥í¥°Ãͤò²¿¤È¤«¤·¤Æ¥Ç¥¸¥¿¥ëÃͤËÊÑ´¹¤·¤Ê¤¤¤È¤¤¤±¤Þ¤»¤ó¡£¤³¤ì¤ò¼Â¸½¤·¤Æ¤¯¤ì¤ë¤Î¤¬ A/D ÊÑ´¹´ï¤Ç¤¹¡£
¤³¤³¤Ç¤Ï ATMega8535 ¤¬È÷¤¨¤Æ¤¤¤ë A/D ÊÑ´¹µ¡Ç½¤ò»È¤¤¡¢¥Ü¥ê¥å¡¼¥à¡Ê²ÄÊÑÄñ¹³¡Ë¤ò²ó¤·¤¿Î̤òÆɤ߹þ¤ó¤Ç¤ß¤¿¤¤¤È»×¤¤¤Þ¤¹¡£
¡û A/D ÊÑ´¹¤Ë¤Ä¤¤¤Æ
A/D ÊÑ´¹´ï(ADC) ¤Ï Analogue(Analog) to Digital Converter ¤ò°ÕÌ£¤·¤Æ¤¤¤Æ¡¢¥¢¥Ê¥í¥°Ãͤò¥Ç¥¸¥¿¥ëÃͤËÊÑ´¹¤¹¤ë¤¿¤á¤Î²óÏ©¤Ç¤¹¡£µÕ¤Ë¥Ç¥¸¥¿¥ëÃͤò¥¢¥Ê¥í¥°ÃͤËÊÑ´¹¤¹¤ë D/A ÊÑ´¹´ï (Digital to Analogue Converter) ¤âÅöÁ³À¤¤ÎÃæ¤Ë¤Ïͤê¤Þ¤¹¤¬¡¢ATMega8535 ¤¬È÷¤¨¤Æ¤¤¤ë¤Î¤Ï A/D ÊÑ´¹´ï¤Î¤ß¤Ç¤¹¡£ATmega8535 ¤Î A/D ÊÑ´¹´ï¤Î¼ç¤Ê»ÅÍͤϰʲ¼¤ÎÍͤˤʤê¤Þ¤¹¡£
- 10 ¥Ó¥Ã¥È¤Îʬ²òǽ
- ÊÑ´¹»þ´Ö 65 - 260us
- 8 ¥Á¥ã¥Í¥ë¤ÎñÆÈ¥¢¥Ê¥í¥°ÆþÎÏ
- x10¡¢x200 ¤Î¥²¥¤¥ó¤òȼ¤¦º¹Æ°ÆþÎÏ
- ¥¢¥Ê¥í¥°ÆþÎÏÅÅ°µÈÏ°Ï 0 - Vcc ¤ËÂбþ
- ÊÑ´¹´ð½àÅÅ°µ¤È¤·¤Æ³°ÉôÅÅ°µ°Ê³°¤ËÆâÉô¤Î 2.56V ÅÅ°µ¤ò»ÈÍѲÄǽ
- Ϣ³/Ã༡ÊÑ´¹Æ°ºî¤ËÂбþ
- A/D ÊÑ´¹´°Î»³ä¤ê¹þ¤ß¤ò¥µ¥Ý¡¼¥È
- ¥¹¥ê¡¼¥×Æ°ºîÃæ¤ÎÊÑ´¹¤Ë¤è¤ë¥Î¥¤¥ºÄ㸺
PORTA ¤Î 8 ¥Ô¥ó¡ÊPA0 - PA7¡Ë¤ò¥¢¥Ê¥í¥°ÆþÎÏü»Ò¤È¤·¤Æ»ÈÍѤ·¤Þ¤¹¡ÊPDIP ¥Ñ¥Ã¥±¡¼¥¸¤Î¾ì¹ç¤Ï 40 - 33 ÈÖ¥Ô¥ó¡Ë¡£¤³¤ì¤é¤Î¥Ô¥ó¤Ï¡¢¥Ç¡¼¥¿¥·¡¼¥ÈÅù¤Ç¤Ï ADC0 - ADC7 ¤È¤·¤ÆµºÜ¤·¤Æ¤¤¤ë¤³¤È¤¬Â¿¤¤ÍͤǤ¹¤¬¡¢avr-libc ¤Ç¤ÏËÜ̾¾Î¤ÏÄêµÁ¤µ¤ì¤Æ¤¤¤Ê¤¤ÍͤǤ¹¡£
ËÜ¥Ú¡¼¥¸¤Î¥µ¥ó¥×¥ë¥×¥í¥°¥é¥à¤Ç¤Ï¡¢¤³¤Î A/D ÊÑ´¹µ¡Ç½¤òÍøÍѤ·¤Æ¥Ü¥ê¥å¡¼¥à¤Î³«ÅÙ¤òÆɤ߹þ¤ó¤Ç¤ß¤Þ¤¹¡£
¢ÀÖÏÈÆ⤬¥Ü¥ê¥å¡¼¥à¤Ç¤¹¡£
¡û ´ØÏ¢¥ì¥¸¥¹¥¿
A/D ÊÑ´¹´ØÏ¢¤Î¥ì¥¸¥¹¥¿¤ò°Ê²¼¤Ë¾Ò²ð¤·¤Þ¤¹¡£¥Þ¥¯¥í¤Ë¤Ä¤¤¤Æ¤Ï¡¢avr-libc ¤Ç»ÈÍѤǤ¤ëÄêµÁ̾¤ò·ÇºÜ¤·¤Æ¤¤¤Þ¤¹¡£¤Þ¤¿¡¢ÆüËܸì¤Î¥ì¥¸¥¹¥¿Ì¾¤È¥Ó¥Ã¥È̾¤Ï¡¢±Ñ¸ì¤ÎÄêµÁ̾¾Î¤òŬÅö¤ËÌõ¤·¤Æ¤Þ¤¹¡£
- A/D ÊÑ´¹¥Á¥ã¥Í¥ëÁªÂò¥ì¥¸¥¹¥¿ (ADMUX)
¥Ó¥Ã¥ÈÈÖ¹æ 7 6 5 4 3 2 1 0 ¥Þ¥¯¥í REFS1 REFS0 ADLAR MUX4 MUX3 MUX2 MUX1 MUX0 ½é´üÃÍ 0 0 0 0 0 0 0 0 ³Æ¥Ó¥Ã¥È¤ÎÀâÌÀ¤ò°Ê²¼¤Ë½ñ¤¤Þ¤¹¡£
¥Ó¥Ã¥È̾ ÆâÍÆ REFS1-0¡Ê´ð½àÅÅ°µÁªÂò¡Ë A/D ÊÑ´¹´ï¤Î´ð½àÅÅ°µ¤òÁªÂò¤·¤Þ¤¹¡£ REFS1 REFS0 ´ð½àÅÅ°µ 0 0 AREF ¤ËÀܳ¤·¤¿³°Éô´ð½àÅÅ°µ 0 1 AVCC 1 0 ͽÌó 1 1 ÆâÉô 2.56V ´ð½àÅÅ°µ ADLAR¡ÊA/D ÊÑ´¹·ë²Ìº¸Â·¤¨¡Ë ¸å½Ò¤¹¤ë A/D ÊÑ´¹¥Ç¡¼¥¿¥ì¥¸¥¹¥¿¤Ø¤ÎÊÑ´¹·ë²Ì¤Î³ÊǼ·Á¼°¤ò»ØÄꤷ¤Þ¤¹¡£ MUX4-0¡Ê¥¢¥Ê¥í¥°ÆþÎÏÁªÂò¡Ë A/D ÊÑ´¹´ï¤ËÀܳ¤¹¤ë¥¢¥Ê¥í¥°ÆþÎϤÎÁȤ߹ç¤ï¤»¤ò»ØÄꤷ¤Þ¤¹¡£ MUX4-0 ñÆÈÆþÎÏ º¹Æ°ÆþÎÏ ÈóȿžÆþÎÏ È¿Å¾ÆþÎÏ ¥²¥¤¥ó 00000 PINA0 - - - 00001 PINA1 - - - 00010 PINA2 - - - 00011 PINA3 - - - 00100 PINA4 - - - 00101 PINA5 - - - 00110 PINA6 - - - 00111 PINA7 - - - 01000 - PINA0 PINA0 x10 01001 - PINA1 PINA0 x10 01010(PDIP ¤Ç¤ÏÊݾڳ°¤é¤·¤¤) - PINA0 PINA0 x200 01011(PDIP ¤Ç¤ÏÊݾڳ°¤é¤·¤¤) - PINA1 PINA0 x200 01100 - PINA2 PINA2 x10 01101 - PINA3 PINA2 x10 01110(PDIP ¤Ç¤ÏÊݾڳ°¤é¤·¤¤) - PINA2 PINA2 x200 01111(PDIP ¤Ç¤ÏÊݾڳ°¤é¤·¤¤) - PINA3 PINA2 x200 10000 - PINA0 PINA1 x1 10001 - PINA1 PINA1 x1 10010 - PINA2 PINA1 x1 10011 - PINA3 PINA1 x1 10100 - PINA4 PINA1 x1 10101 - PINA5 PINA1 x1 10110 - PINA6 PINA1 x1 10111 - PINA7 PINA1 x1 11000 - PINA0 PINA2 x1 11001 - PINA1 PINA2 x1 11010 - PINA2 PINA2 x1 11011 - PINA3 PINA2 x1 11100 - PINA4 PINA2 x1 11101 - PINA5 PINA2 x1 11110 1.23V - - - 11111 GND - - -
- A/D ÊÑ´¹À©¸æ/¾õÂ֥쥸¥¹¥¿ A (ADCSRA)
¥Ó¥Ã¥ÈÈÖ¹æ 7 6 5 4 3 2 1 0 ¥Þ¥¯¥í ADEN ADSC ADATE ADIF ADIE ADPS2 ADPS1 ADPS0 ½é´üÃÍ 0 0 0 0 0 0 0 0 ³Æ¥Ó¥Ã¥È¤ÎÀâÌÀ¤ò°Ê²¼¤Ë½ñ¤¤Þ¤¹¡£
¥Ó¥Ã¥È̾ ÆâÍÆ ADEN¡ÊA/D ÊÑ´¹µö²Ä¡Ë 1 ¤òÀßÄꤹ¤ë¤È A/D ÊÑ´¹´ï¤¬Í¸ú¤Ë¤Ê¤ê¤Þ¤¹¡£0 ¤òÀßÄꤹ¤ë¤È¡¢A/D ÊÑ´¹´ï¤ÎÅŸ»¤¬ÀÚ¤ì¤Þ¤¹¡£ ADSC¡ÊA/D ÊÑ´¹³«»Ï¡Ë ADATE ¤¬ 0 ¤Î¾ì¹ç¡Ê¼«Æ°ÊÑ´¹Ìµ¸ú¡Ë¤Ë¡¢ADSC ¤Ë 1 ¤òÀßÄꤹ¤ë¤³¤È¤Ç A/D ÊÑ´¹¤¬³«»Ï¤µ¤ì¤Þ¤¹¡£ADATE ¤¬ 1 ¤Î¾ì¹ç¡Ê¼«Æ°ÊÑ´¹Í¸ú¡Ë¤Ë¤â¡¢½é²ó¼Â¹Ô¤Î¤¿¤á¤Ë ADSC ¤Ë 1 ¤òÀßÄꤹ¤ëɬÍפ¬¤¢¤ê¤Þ¤¹¡£ ADATE¡ÊA/D ÊÑ´¹¼«Æ°µ¯Æ°µö²Ä¡Ë 1 ¤òÀßÄꤹ¤ë¤³¤È¤Ç A/D ÊÑ´¹¤¬¼«Æ°µ¯Æ°¤µ¤ì¤Þ¤¹¡£¼«Æ°ÊÑ´¹¤Î¼Â¹Ô¤Ï¡¢¸å½Ò¤Î SFIOR ¥ì¥¸¥¹¥¿¤Î ADTS2-0 ¥Ó¥Ã¥È¤Ç»ØÄꤷ¤¿¿®¹æ¤ÎΩ¤Á¾å¤¬¤ê¥¨¥Ã¥¸¤Ç³«»Ï¤µ¤ì¤Þ¤¹¡£ ADIF¡ÊA/D ÊÑ´¹³ä¤ê¹þ¤ß¥Õ¥é¥°¡Ë A/D ÊÑ´¹´°Î»»þ¤Ë 1 ¤¬ÀßÄꤵ¤ì¤Þ¤¹¡£Ëܥե饰¤ò¥¯¥ê¥¢¤¹¤ë¤¿¤á¤Ë¤Ï¡¢1 ¤ò½ñ¤¹þ¤àɬÍפ¬¤¢¤ë¤³¤È¤ËÃí°Õ¤·¤Æ¤¯¤À¤µ¤¤¡£¤Ê¤ª¡¢A/D ÊÑ´¹´°Î»³ä¤ê¹þ¤ß¤¬Í¸ú¤Ê¾ì¹ç¤Ï¡¢³ä¤ê¹þ¤ß¥Ï¥ó¥É¥é¼Â¹Ô¸å¤Ë¼«Æ°Åª¤Ë¥¯¥ê¥¢¤µ¤ì¤Þ¤¹¡£ ADIE¡ÊA/D ÊÑ´¹´°Î»³ä¤ê¹þ¤ßµö²Ä¡Ë 1 ¤òÀßÄꤹ¤ë¤È¡¢A/D ÊÑ´¹´°Î»³ä¤ê¹þ¤ß¤¬Í¸ú¤Ë¤Ê¤ê¤Þ¤¹¡£ ADPS2-0¡ÊA/D ÊÑ´¹¥¯¥í¥Ã¥¯ÁªÂò¡Ë A/D ÊÑ´¹¤Ë»ÈÍѤ¹¤ë¥¯¥í¥Ã¥¯¤Î¥·¥¹¥Æ¥à¥¯¥í¥Ã¥¯¤ËÂФ¹¤ëʬ¼þÈæ¤ò¡¢°Ê²¼¤ÎÁȤ߹ç¤ï¤»¤ÇÀßÄꤷ¤Þ¤¹¡£ ADPS2 ADPS1 ADPS0 ʬ¼þÈæ 0 0 0 CK/2 0 0 1 CK/2 0 1 0 CK/4 0 1 1 CK/8 1 0 0 CK/16 1 0 1 CK/32 1 1 0 CK/64 1 1 1 CK/128
- A/D ÊÑ´¹¥Ç¡¼¥¿¥ì¥¸¥¹¥¿ (ADCH¡¢ADCS)¡ÊADC ¤È¤·¤Æ 2¥Ð¥¤¥È¥¢¥¯¥»¥¹²Äǽ¡Ë
A/D ÊÑ´¹·ë²Ì¤ò³ÊǼ¤¹¤ë¥ì¥¸¥¹¥¿¤Ç¤¹¡£ADMUX ¥ì¥¸¥¹¥¿¤Î ADLAR ¥Ó¥Ã¥È¤ÎÃͤˤè¤Ã¤Æ ³ÊǼ·Á¼°¤¬ÊѤï¤ê¤Þ¤¹¡£¤Þ¤¿¡¢ADCL / ADCH ¤Î½ç¤ËÆɤàɬÍפ¬¤¢¤ê¤Þ¤¹¡£ ¤Ê¤ª¡¢ADCL ¤ÎÃͤ¬É¬Í×̵¤¤¾ì¹ç¤Ï ADCH ¤Î¤ßÆɤߤ³¤ó¤Ç¤â¹½¤¤¤Þ¤»¤ó¡£
ADC ¤È¤·¤Æ 2 ¥Ð¥¤¥È¥¢¥¯¥»¥¹¤¹¤ë¾ì¹ç¤Ï¡¢ ¥¢¥¯¥»¥¹»þ¤Ë³ä¤ê¹þ¤ß¤¬µ¯¤¤Ê¤¤Íͤˤ·¤Æ¤¯¤À¤µ¤¤¡£10 ¥Ó¥Ã¥È¤Î¤¿¤á¡¢ºÇÂçÃÍ¤Ï 0x3ff = ´ð½àÅÅ°µ - 1 LSB ¤È¤Ê¤ê¤Þ¤¹¡£
--- ADCH (ADLAR=0) ---
¥Ó¥Ã¥ÈÈÖ¹æ 7 6 5 4 3 2 1 0 ¥Þ¥¯¥í ̤»ÈÍÑ Ì¤»ÈÍÑ Ì¤»ÈÍÑ Ì¤»ÈÍÑ Ì¤»ÈÍÑ Ì¤»ÈÍÑ - - ½é´üÃÍ 0 0 0 0 0 0 0 0 --- ADCL (ADLAR=0) ---
¥Ó¥Ã¥ÈÈÖ¹æ 7 6 5 4 3 2 1 0 ¥Þ¥¯¥í - - - - - - - - ½é´üÃÍ 0 0 0 0 0 0 0 0 ADLAR=0 ¤Î¾ì¹ç¤Ï ADCL ¤ÎÁ´¥Ó¥Ã¥È¤¬ÊÑ´¹·ë²Ì¤Î²¼°Ì 8 ¥Ó¥Ã¥È¡¢ADCH ¤Î²¼°Ì 2 ¥Ó¥Ã¥È¤¬ÊÑ´¹·ë²Ì¤Î¾å°Ì 2 ¥Ó¥Ã¥È¤È¤Ê¤ê¤Þ¤¹¡£
--- ADCH (ADLAR=1) ---
¥Ó¥Ã¥ÈÈÖ¹æ 7 6 5 4 3 2 1 0 ¥Þ¥¯¥í - - - - - - - - ½é´üÃÍ 0 0 0 0 0 0 0 0 --- ADCL (ADLAR=1) ---
¥Ó¥Ã¥ÈÈÖ¹æ 7 6 5 4 3 2 1 0 ¥Þ¥¯¥í - - ̤»ÈÍÑ Ì¤»ÈÍÑ Ì¤»ÈÍÑ Ì¤»ÈÍÑ Ì¤»ÈÍÑ Ì¤»ÈÍÑ ½é´üÃÍ 0 0 0 0 0 0 0 0 ADLAR=1 ¤Î¾ì¹ç¤Ï ADCH ¤ÎÁ´¥Ó¥Ã¥È¤¬ÊÑ´¹·ë²Ì¤Î¾å°Ì 8 ¥Ó¥Ã¥È¡¢ADCH ¤Î¾å°Ì 2 ¥Ó¥Ã¥È¤¬ÊÑ´¹·ë²Ì¤Î²¼°Ì 2 ¥Ó¥Ã¥È¤È¤Ê¤ê¤Þ¤¹¡£
- ÆüìI/Oµ¡Ç½¥ì¥¸¥¹¥¿ (SFIOR)¢¨ACME / PUD / PSR2 / PSR10 ¥Þ¥¯¥í¤Çɽ¤µ¤ì¤ë¥Ó¥Ã¥È¤Ï¡¢A/D ÊÑ´¹¤È¤Ï´Ø·¸Ìµ¤¤¥Ó¥Ã¥È¤Ç¤¹¡£
¥Ó¥Ã¥ÈÈÖ¹æ 7 6 5 4 3 2 1 0 ¥Þ¥¯¥í ADTS2 ADTS1 ADTS0 ͽÌó ACME PUD PSR2 PSR10 ½é´üÃÍ 0 0 0 0 0 0 0 0 ³Æ¥Ó¥Ã¥È¤ÎÀâÌÀ¤ò°Ê²¼¤Ë½ñ¤¤Þ¤¹¡£
¥Ó¥Ã¥È̾ ÆâÍÆ ADTS2-0¡ÊA/D ÊÑ´¹¼«Æ°µ¯Æ°¸µ¡Ë ADCSRA ¥ì¥¸¥¹¥¿¤Î ADATE ¥Ó¥Ã¥È¤ò 1 ¤ËÀßÄê¡Ê¼«Æ°ÊÑ´¹Í¸ú¡Ë¤·¤¿¾ì¹ç¤Ë¡¢Ëè²ó¤Î A/D ÊÑ´¹¤òµ¯Æ°¤¹¤ë¿®¹æ¤òÁªÂò¤·¤Þ¤¹¡£ ADTS2-0 ¿®¹æ¸µ 000 Ϣ³ÊÑ´¹Æ°ºî¡ÊÊÑ´¹´°Î»¸å¡¢¤¹¤°¤Ë¼¡¤ÎÊÑ´¹³«»Ï¡£ADSC ¤Ï 1 ¤Î¤Þ¤Þ¡Ë 001 ¥¢¥Ê¥í¥°Èæ³Ó´ï 010 ³°Éô³ä¤ê¹þ¤ßÍ×µá0 011 ¥¿¥¤¥Þ/¥«¥¦¥ó¥¿0Èæ³Ó°ìÃ× 100 ¥¿¥¤¥Þ/¥«¥¦¥ó¥¿0¥ª¡¼¥Ð¡¼¥Õ¥í¡¼ 101 ¥¿¥¤¥Þ/¥«¥¦¥ó¥¿1Èæ³ÓB°ìÃ× 110 ¥¿¥¤¥Þ/¥«¥¦¥ó¥¿1¥ª¡¼¥Ð¡¼¥Õ¥í¡¼ 111 ¥¿¥¤¥Þ/¥«¥¦¥ó¥¿1Êá³ÍÍ×µá
¡û ²óÏ©¿Þ
LECTURE 1 ATmega8535 ¤ª¤è¤Ó»ÈÍѴĶ¤Ë¤Ä¤¤¤Æ ¤Ë·ÇºÜ¤·¤Æ¤¢¤ëÁ´ÂβóÏ©¿Þ¤ËÂФ·¤Æ¡¢ATMega8535 ¤Î PORTA/PA0 ¤ª¤è¤Ó¥Ü¥ê¥å¡¼¥à¡Ê²ÄÊÑÄñ¹³¡ËÉôʬ¤òÀÖÏȤǰϤó¤À¿Þ¤ò ¤³¤Á¤é ¤ËÃÖ¤¤¤Æ¤ª¤¤Þ¤¹¡£
PORTA ¤Î PA0 ¥Ô¥ó¤Ë 50KΩ ¤Î¥Ü¥ê¥å¡¼¥à¤òÀܳ¤·¤Æ¤¤¤Þ¤¹¡£Æ±»þ¤Ë PA0 ¤ò 10KΩ ¤ÎÄñ¹³¤Ë¤è¤Ã¤Æ¥×¥ë¥¢¥Ã¥×¤·¤Æ¤¤¤Þ¤¹¡£¤³¤ì¤Ë¤è¤ê¡¢¥Ü¥ê¥å¡¼¥à¤ÎÄñ¹³Ãͤ¬ 0 KΩ ¤Î¾ì¹ç¤Ï PA0 ¤Ï 0V ¤È¤Ê¤ê¡¢ºÇÂçÃͤΠ50KΩ ¤Î¾ì¹ç¤Ë Vcc x 50K/(50K + 10K) = Ìó 4.17V ¤È¤Ê¤ê¤Þ¤¹¡ÊVCC=5V ¤Î¾ì¹ç¡Ë¡£
AVCC¡Ê¥¢¥Ê¥í¥°ÅŸ»¶¡µëÉô¡Ë¤Ï VCC (5V) ¤ËÀܳ¤·¤Æ¤¤¤Þ¤¹¡£¤Þ¤¿¡¢AREF¡Ê³°Éô¥¢¥Ê¥í¥°ÊÑ´¹´ð½àÅÅ°µ¡Ë¤ò VCC Àܳ¤·¤Æ¤¤¤ë¤¿¤á¡¢A/D ÊÑ´¹·ë²Ì¤ÎºÇÂçÃÍ 0x3ff ¤Ë 1LSB ¤·¤¿Ãͤ¬ VCC(5V) ¤ËÂбþ¤·¤Þ¤¹¡£¤è¤Ã¤Æ¥Ü¥ê¥å¡¼¥à¤ò´°Á´¤ËÊĤ¸¤¿¾õÂÖ¤«¤é³«¤¤¤¿¾õÂ֤ޤDz󤹤ȡ¢A/D ÊÑ´¹·ë²Ì¤ÏºÇ¾¯ 0 ¤«¤éºÇÂç 0x3ff x 4.17V / 5V = Ìó 853.182(0x355) ÄøÅ٤ˤʤë¤ÈͽÁۤǤ¤Þ¤¹¡£
¡û ¥µ¥ó¥×¥ë¥×¥í¥°¥é¥à¤ÈÀâÌÀ
A/D ÊÑ´¹¤ò»È¤Ã¤Æ¥Ü¥ê¥å¡¼¥àÃͤòÆɤॵ¥ó¥×¥ë¥×¥í¥°¥é¥à¤ò 04volume.tar.gz ¤«¤é¥À¥¦¥ó¥í¡¼¥É¤Ç¤¤Þ¤¹¡£¤Ï¤¸¤á¤Ë ¤Î¥Ú¡¼¥¸¤«¤é¡¢Á´¤Æ¤Î¥µ¥ó¥×¥ë¥×¥í¥°¥é¥à¤ò ¸Ç¤á¤¿ tar ¥Õ¥¡¥¤¥ë¤ò¥À¥¦¥ó¥í¡¼¥É¤¹¤ë¤³¤È¤â¤Ç¤¤Þ¤¹¡£
Ãæ¤Ë¤Ï
- Makefile
- adc.h
- adc.c
- util.h
- util.c
- usart.h
- usart.c
- main-int.c
- main-noint.c
¤¬³ÊǼ¤µ¤ì¤Æ¤¤¤Þ¤¹¡£usart.h/usart.c ¤Ë¤Ä¤¤¤Æ¤Ï LECTURE 3 USART ¤Ë¤è¤ë¥·¥ê¥¢¥ëÄÌ¿® ¤ÎÆâÍƤÈƱ¤¸¤Ç Makefile ¤â¤Û¤È¤ó¤ÉÊѤï¤é¤Ê¤¤¤Î¤Ç¡¢¤½¤ì°Ê³°¤Ë¤Ä¤¤¤Æ°Ê²¼¤ËºÜ¤»¤Þ¤¹¡£¤Ê¤ª¡¢°Ê²¼¤Ë·ÇºÜ¤·¤Æ¤¤¤ë¥½¡¼¥¹¥³¡¼¥É¤Ë¤Ï½¤Àµ BSD ¥é¥¤¥»¥ó¥¹¾ò¹à¤ò´Þ¤á¤Æ¤¤¤Þ¤»¤ó¤¬¡¢¥½¡¼¥¹¥³¡¼¥ÉÇÛÉۤϽ¤Àµ BSD ¥é¥¤¥»¥ó¥¹¤Ë½¾¤¤¤Þ¤¹¡Ê¤Ï¤¸¤á¤Ë ¤Î¥Ú¡¼¥¸¤ÎÌÈÀÕ»ö¹à¤ò»²¾È¤·¤Æ¤¯¤À¤µ¤¤¡Ë¡£
¤³¤ì¤Þ¤Ç¤Ë¥Ò¥å¡¼¥º¥Ó¥Ã¥È¤ÎÀßÄê¤ò¹Ô¤Ã¤¿¤³¤È¤¬°ìÅÙ¤â̵¤¤¾ì¹ç¤Ï¡¢¤Þ¤ººÇ½é¤Ë make fuse ¤ò¼Â¹Ô¤·¤Æ¤¯¤À¤µ¤¤¡£fuse ¥¿¡¼¥²¥Ã¥È¤Ë¤Ä¤¤¤Æ¤Ï¡¢LECTURE 2 LED ¤òÅÀÌǤµ¤»¤ë ¤Î¥µ¥ó¥×¥ë¥×¥í¥°¥é¥à¤Î¹à¤ò»²¾È¤·¤Æ¤¯¤À¤µ¤¤¡£
make ¼Â¹Ô¸å¡¢³ä¤ê¹þ¤ß¤ò»ÈÍѤ·¤¿¥×¥í¥°¥é¥à¤ò¥¢¥Ã¥×¥í¡¼¥É¤¹¤ë¾ì¹ç¤Ï make uisp-int ¤ò¼Â¹Ô¤·¤Æ¤¯¤À¤µ¤¤¡£¤Þ¤¿¡¢³ä¤ê¹þ¤ß¤ò»ÈÍѤ·¤Æ¤¤¤Ê¤¤¥×¥í¥°¥é¥à¤ò¥¢¥Ã¥×¥í¡¼¥É¤¹¤ë¾ì¹ç¤Ï make uisp-noint ¤ò¼Â¹Ô¤·¤Æ¤¯¤À¤µ¤¤¡£¼ÂÁõÊýË¡¤¬°Û¤Ê¤ë¤À¤±¤ÇÆ°ºîÆâÍƤÏƱ¤¸¤Ç¤¹¡£
ËÜ¥µ¥ó¥×¥ë¥×¥í¥°¥é¥à¤Ï 500ms ¤Ë 1 ²ó¤Î³ä¹ç¤Ç¡¢¥Ü¥ê¥å¡¼¥àÃͤò A/D ÊÑ´¹µ¡Ç½¤ÇÆɤ߹þ¤ó¤À¸å¤Ë¡¢Ãͤò "PINA0 input = ..." ¤È¤¤¤¦·Á¼°¤Îʸ»úÎó¤Ë²Ã¹©¤·¤Æ¥·¥ê¥¢¥ëÄÌ¿®¤ÇÁ÷¿®¤·¤Þ¤¹¡£A/D ÊÑ´¹·ë²ÌÉôʬ¤Ï 10¿Ê¿ôɽ¼¨¤Ç¤¹¡£Ê¸»úÎó¤ò¼õ¿®¤¹¤ë¤¿¤á¤Ë¤Ï¡¢¼Â¸³ÍѲóÏ©¤È PC ¤Î¥·¥ê¥¢¥ë¥Ý¡¼¥È¡ÊD-SUB 9¥Ô¥ó¡Ë¤ò¥¹¥È¥ì¡¼¥È¥±¡¼¥Ö¥ë¤ÇÀܳ¤·¡¢Windows ¤Ç¤¢¤ì¤Ð¥Ï¥¤¥Ñ¡¼¥¿¡¼¥ß¥Ê¥ëÅù¤ÎÄÌ¿®¥½¥Õ¥È¤Ç°Ê²¼¤Î¥Ñ¥é¥á¡¼¥¿¤òÀßÄꤷ¤Æ¥Õ¥í¡¼À©¸æ̵¤·¤ÇÀܳ¤·¤Æ¤¯¤À¤µ¤¤¡£
- ÈóƱ´üÆ°ºî
- ¥Ü¡¼¥ì¡¼¥È 9600 bps
- ¥Ñ¥ê¥Æ¥£Ìµ¤·
- ¥¹¥È¥Ã¥×¥Ó¥Ã¥È 1¥Ó¥Ã¥È
- 8 ¥Ó¥Ã¥È¥Ç¡¼¥¿Å¾Á÷
"PINA0 input = ..." ¤È¤¤¤¦·Á¼°¤Îʸ»úÎ󤬡¢¥¿¡¼¥ß¥Ê¥ë¤ËϢ³Ū¤Ëɽ¼¨¤µ¤ì¤ë¤È»×¤¤¤Þ¤¹¡£¤³¤Î´Ö¤Ë¥Ü¥ê¥å¡¼¥à¤ò²ó¤·¤Æ²ÄÊÑÄñ¹³ÃͤòÊѲ½¤µ¤»¤ë¤È¡¢"PINA0 input = ..." ¤Î ... ¤ÎÉôʬ¤Ëɽ¼¨¤µ¤ì¤ë¿ô»ú¤¬ÊѲ½¤·¤Þ¤¹¡£
¢ÄÌ¿®¥½¥Õ¥È¤Ë¤Ï¤³¤ÎÍͤËɽ¼¨¤µ¤ì¤Þ¤¹¡£
- adc.h
1 #ifndef ___ADC_H_INCLUDED_ 2 #define ___ADC_H_INCLUDED_ 3 4 #include <avr/io.h> 5 #include <inttypes.h> 6 7 8 /* A/DÊÑ´¹ºÇÂçÃÍ */ 9 #define ADC_MAX 0x3ff 10 11 /* A/DÊÑ´¹¥¯¥í¥Ã¥¯ÁªÂòÍÑ¥Þ¥¯¥í */ 12 #define ADC_CK_2 (_BV(ADPS0)) 13 #define ADC_CK_4 (_BV(ADPS1)) 14 #define ADC_CK_8 (_BV(ADPS0) | _BV(ADPS1)) 15 #define ADC_CK_16 (_BV(ADPS2)) 16 #define ADC_CK_32 (_BV(ADPS0) | _BV(ADPS2)) 17 #define ADC_CK_64 (_BV(ADPS1) | _BV(ADPS2)) 18 #define ADC_CK_128 (_BV(ADPS0) | _BV(ADPS1) | _BV(ADPS2)) 19 20 /* ³ä¤ê¹þ¤ß»ÈÍÑ/Èó»ÈÍÑÁªÂò¥Þ¥¯¥í */ 21 #define ADC_NO_INT 0 22 #define ADC_USE_INT 1 23 24 25 #define adc_set_ch(ch) { ADMUX = (ch) & (_BV(MUX2) | _BV(MUX1) | _BV(MUX0)); } 26 #define adc_wait() loop_until_bit_is_set(ADCSRA, ADIF) 27 #define adc_get() ADC 28 #define adc_start() { ADCSRA |= _BV(ADIF); ADCSRA |= _BV(ADSC); } 29 30 31 void adc_set_interrupt(void (*func)(void)); 32 void adc_init(uint8_t clock, int use_interrupt); 33 34 35 #endif 36 37 /* End of adc.h */
- 11 ¡Á 18 ¹ÔÌÜ
A/D ÊÑ´¹ÍѤΥ¯¥í¥Ã¥¯»ØÄê¤ò³Ú¤Ë¤Ç¤¤ëÍͤ˥ޥ¯¥í¤òÄêµÁ¤·¤Æ¤¤¤Þ¤¹¡£ADCSRA ¥ì¥¸¥¹¥¿¤Î ADPS2-0 ¥Ó¥Ã¥È¤ËÂбþ¤¹¤ë¥Ó¥Ã¥È¤ò _BV() ¥Þ¥¯¥í¤ÇÀßÄꤷ¤Æ¤¤¤Þ¤¹¡£ËÜ¥Þ¥¯¥í¤Ï ¸å½Ò¤Î adc_init() ´Ø¿ô¼Â¹Ô»þ¤Ë»ØÄꤷ¤Þ¤¹¡£
- 25 ¡Á 28 ¹ÔÌÜ
µ½Ò¤¬³Ú¤Ë¤Ê¤ëÍͤˡ¢¤¢¤ë¤¤¤Ïʬ¤ê¤ä¤¹¤¯¤Ê¤ëÍͤˤ¤¤¯¤Ä¤«¤Î¥Þ¥¯¥í¤òÄêµÁ¤·¤Æ¤¤¤Þ¤¹¡£
adc_set_ch() ¥Þ¥¯¥í¤Ë PINA0 - PINA7¡ÊPORTA0/PA0 - PORTA7/PA7 ¤Ç¤âÎɤ¤¡Ë ¤Î¤¤¤º¤ì¤«¤ò»ØÄꤹ¤ë¤³¤È¤Ç¡¢A/D ÊÑ´¹´ï¤ËÀܳ¤¹¤ë¥¢¥Ê¥í¥°ÆþÎϤòÁªÂò¤·¤Þ¤¹¡£ adc_set_ch() ¤Ç¤Ï ADMUX ¥ì¥¸¥¹¥¿¤Î MUX2-0 ¤Ë»ØÄêÃͤòÀßÄꤷ¤Þ¤¹¡£ ¤½¤Î¤¿¤á¡¢ËÜ¥×¥í¥°¥é¥à¤Ç¥µ¥Ý¡¼¥È¤¹¤ë¥¢¥Ê¥í¥°ÆþÎϤϡ¢8 ¥Á¥ã¥Í¥ë¤ÎñÆÈÆþÎϤΤߤǤ¹¡£
adc_wait() ¥Þ¥¯¥í¤Ï¡¢¸å½Ò¤Î adc_start() ¤Ë¤è¤ë A/D ÊÑ´¹³«»Ï¸å¤Ë¡¢ÊÑ´¹¤¬½ªÎ»¤¹¤ë¤Þ¤Ç¥Ö¥í¥Ã¥¯¤·¤Þ¤¹¡£ ¼ÂºÝ¤Ë¤Ï¡¢A/D ÊÑ´¹´°Î»»þ¤Ë¤Ï ADCSRA ¥ì¥¸¥¹¥¿¤Î ADIF ¥Ó¥Ã¥È¤¬ 1 ¤È¤Ê¤ë¤¿¤á¡¢ avr-libc ¤Î loop_until_bit_is_set(sfr, bit) ¥Þ¥¯¥í¤ò»È¤Ã¤Æ ADIF ¤¬ 1 ¤È¤Ê¤ë¤Î¤ò´Æ»ë¤·¤Þ¤¹¡£
adc_get() ¥Þ¥¯¥í¤Ï¡¢Ã±¤Ë ADC ¥ì¥¸¥¹¥¿¡ÊA/D ÊÑ´¹¥Ç¡¼¥¿¥ì¥¸¥¹¥¿¡Ë¤ÎÃͤòÊÖ¤·¤Þ¤¹¡£ ËÜ¥Þ¥¯¥í»ÈÍÑ»þ¤Ë¤Ï³ä¤ê¹þ¤ß¤¬µ¯¤¤Ê¤¤Íͤˤ·¤Æ¤¯¤À¤µ¤¤¡£ ËÜ¥×¥í¥°¥é¥à¤Ç¤Ï ADMUX ¥ì¥¸¥¹¥¿¤Î ADLAR ¥Ó¥Ã¥È¤Ï¾ï¤Ë 0 ¤Ç¤¢¤ë¤¿¤á¡¢16 ¥Ó¥Ã¥È ADC ¥ì¥¸¥¹¥¿¤Î²¼°Ì 10 ¥Ó¥Ã¥È¤¬ A/D ÊÑ´¹·ë²Ì¤È¤Ê¤ê¤Þ¤¹¡£¤¿¤À¤·¡¢¾å°Ì 6 ¥Ó¥Ã¥È¤Ï½é´üÃÍ 0 ¤Î¤Ï¤º¤Ç¤¹¤Î¤Ç¡¢¤½¤Î¤Þ¤Þ»È¤Ã¤Æ¤¤¤Þ¤¹¡£
adc_start() ¥Þ¥¯¥í¤Ë¤è¤Ã¤Æ¡¢ADCSRA ¥ì¥¸¥¹¥¿¤Î ADSC ¥Ó¥Ã¥È¤ò 1 ¤Ë¤¹¤ë¤³¤È¤Ç A/D ÊÑ´¹¤ò³«»Ï¤·¤Þ¤¹¡£³«»ÏÁ°¤Ë¡¢ÊÑ´¹´°Î»´Æ»ë¤Ë»È¤¦ ADIF ¥Ó¥Ã¥È¤òÌÀ¼¨Åª¤Ë¥¯¥ê¥¢¤·¤Æ¤¤¤Þ¤¹¡£ ¤Ê¤ª¡¢ADIF ¥Ó¥Ã¥È¤Ï 1 ¤ò½ñ¤¹þ¤à¤³¤È¤Ç¥¯¥ê¥¢¤µ¤ì¤ë¤¿¤á¡¢_BV(ADIF) ¤òÀßÄꤷ¤Æ¤¤¤Þ¤¹¡£
- adc.c
1 #include <avr/interrupt.h> 2 #include <avr/io.h> 3 #include <inttypes.h> 4 5 #include "adc.h" 6 7 8 static void (*adc_interrupt_handler)(void); 9 10 11 /* 12 * A/DÊÑ´¹´°Î»»þ³ä¤ê¹þ¤ß¥Ï¥ó¥É¥é 13 * adc_init(???,ADC_USE_INT) ¤È¤·¤¿¾ì¹ç¤Ë͸ú¤È¤Ê¤ë¡£ 14 */ 15 ISR(ADC_vect) 16 { 17 adc_interrupt_handler(); 18 } 19 20 21 /* 22 * A/DÊÑ´¹´°Î»»þ³ä¤ê¹þ¤ß¥Ï¥ó¥É¥éÆâ¤Ç¼Â¹Ô¤¹¤ë 23 * ¥æ¡¼¥¶´Ø¿ô¤òÀßÄꤹ¤ë¡£ 24 */ 25 void 26 adc_set_interrupt(void (*func)(void)) 27 { 28 adc_interrupt_handler = func; 29 } 30 31 32 /* 33 * A/DÊÑ´¹´ï¤Î½é´ü²½¤ò¹Ô¤¦¡£ 34 * clock ¤Ï adc.h ¤ÇÄêµÁ¤·¤Æ¤¤¤ë¥¯¥í¥Ã¥¯ÁªÂòÍÑ¥Þ¥¯¥í¤ò»ØÄꤹ¤ë¡£ 35 * 36 * ÊÑ´¹´°Î»»þ³ä¤ê¹þ¤ß¤ò»ÈÍѤ¹¤ë¾ì¹ç¡¢ËÜ´Ø¿ô¼Â¹Ô¸å¤Ë 37 * sei() ¤ò¼Â¹Ô¤¹¤ë¤³¤È¤Ç͸ú¤È¤Ê¤ë¡£ 38 */ 39 void 40 adc_init(uint8_t clock, int use_interrupt) 41 { 42 clock &= (_BV(ADPS2) | _BV(ADPS1) | _BV(ADPS0)); 43 44 if (use_interrupt) { 45 /* A/DÊÑ´¹µö²Ä|ÊÑ´¹´°Î»»þ³ä¤ê¹þ¤ß|¥¯¥í¥Ã¥¯ÀßÄê */ 46 ADCSRA = (_BV(ADEN) | _BV(ADIE) | clock); 47 } 48 else { 49 /* A/DÊÑ´¹µö²Ä|¥¯¥í¥Ã¥¯ÀßÄê */ 50 ADCSRA = (_BV(ADEN) | clock); 51 } 52 } 53 54 /* End of adc.c */
- 11 ¡Á 18 ¹ÔÌÜ
A/D ÊÑ´¹´°Î»³ä¤ê¹þ¤ß¤ò͸ú¤Ë¤·¤¿¾ì¹ç¤Ë»ÈÍѤ¹¤ë¡¢³ä¤ê¹þ¤ß¥Ï¥ó¥É¥é¤òµ½Ò¤·¤Æ¤¤¤Þ¤¹¡£ LECTURE 3 USART ¤Ë¤è¤ë¥·¥ê¥¢¥ëÄÌ¿® ¤Ë¤ÆÀâÌÀ¤·¤¿Íͤˡ¢ avr-libc ¤ò»È¤¦¾ì¹ç¡¢³ä¤ê¹þ¤ß¥Ï¥ó¥É¥é¤Ï ISR(³ä¤ê¹þ¤ß¼ïÊ̤òɽ¤¹¥Þ¥¯¥í){ /* ¼Â½èÍý */} ¤ÎÍͤ˵½Ò¤·¤Þ¤¹¡£ATmega8535 ¤Î A/D ÊÑ´¹´°Î»¤òɽ¤¹¥Þ¥¯¥í¤Ï avr/interrupt.h ¤Ë¤Æ ADC_vect ¤ÈÄêµÁ¤µ¤ì¤Æ¤¤¤Þ¤¹¡£
¼ÂºÝ¤Ë¤Ï¡¢¸å½Ò¤¹¤ë adc_set_interrupt() ¤ÇÅÐÏ¿¤µ¤ì¤¿¥æ¡¼¥¶´Ø¿ô¤ò¼Â¹Ô¤¹¤ëÍͤ˼ÂÁõ¤·¤Æ¤¤¤Þ¤¹¡£
- 21 ¡Á 29 ¹ÔÌÜ (adc_set_interrupt())
Á°½Ò¤Î A/D ÊÑ´¹´°Î»³ä¤ê¹þ¤ß¥Ï¥ó¥É¥éÆâ¤Ç¼ÂºÝ¤Ë¼Â¹Ô¤¹¤ë¡¢¥æ¡¼¥¶´Ø¿ô¤òÅÐÏ¿¤·¤Þ¤¹¡£
- 32 ¡Á 52 ¹ÔÌÜ (adc_init())
A/D ÊÑ´¹´ï¤Î½é´ü²½¤ò¹Ô¤¤¤Þ¤¹¡£°ú¿ô¤Î clock ¤Ë¤Ï¡¢adc.h ¤ÇÄêµÁ¤·¤Æ¤¤¤ë ADC_CK_X ¥Þ¥¯¥í¤ò»ØÄꤷ¤Þ¤¹¡£¤â¤·¤â¾¤ÎÃͤòÀßÄꤵ¤ì¤¿¾ì¹ç¤Ë¡¢ADCSRA ¥ì¥¸¥¹¥¿¤Î̵´Ø·¸¤Ê¥Ó¥Ã¥È ¤ò½ñ¤ÊѤ¨¤Æ¤·¤Þ¤¦¤³¤È¤òËɤ°¤¿¤á¤Ë¡¢ADPS2-0 ¤ËÂбþ¤·¤¿¥Ó¥Ã¥È°Ê³°¤ò¥¯¥ê¥¢¤·¤Þ¤¹¡£
¤½¤Î¸å A/D ÊÑ´¹´°Î»³ä¤ê¹þ¤ß¤¬ÉÔÍפʾì¹ç¤Ï¡¢ADCSRA ¥ì¥¸¥¹¥¿¤Î ADEN ¥Ó¥Ã¥È¤ò 1 ¤ËÀßÄꤷ¤Æ A/D ÊÑ´¹´ï¤ò͸ú¤Ë¤¹¤ë¤È¶¦¤Ë A/D ÊÑ´¹ÍѤΥ¯¥í¥Ã¥¯¤òÀßÄꤷ¤Þ¤¹¡£ A/D ÊÑ´¹´°Î»³ä¤ê¹þ¤ß¤¬É¬Íפʾì¹ç¤Ï¡¢¤µ¤é¤Ë ADIE ¥Ó¥Ã¥È¤ò 1 ¤ËÀßÄꤷ¤Æ³ä¤ê¹þ¤ß¤ò͸ú¤Ë¤·¤Þ¤¹¡£
- util.h
1 #ifndef ___UTIL_H_INCLUDED_ 2 #define ___UTIL_H_INCLUDED_ 3 4 #include <avr/io.h> 5 6 7 #define pullup() {\ 8 PORTA = 0xff; \ 9 PORTB = 0xff; \ 10 PORTC = 0xff; \ 11 PORTD = 0xff; \ 12 } 13 14 void delay_250ms(void); 15 void delay_500ms(void); 16 int itoa(char *buf, int target, int base, int width, char pad); 17 18 19 #endif 20 21 /* End of util.h */
- util.c
1 #include <util/delay.h> 2 #include "util.h" 3 4 5 void 6 delay_250ms(void) 7 { 8 int i; 9 10 for (i = 0; i < 25; i ++) { 11 _delay_ms(10); 12 } 13 } 14 15 16 void 17 delay_500ms(void) 18 { 19 int i; 20 21 for (i = 0; i < 50; i ++) { 22 _delay_ms(10); 23 } 24 } 25 26 27 #define INT64_MAX_DIGIT 20 28 29 int 30 itoa(char *buf, int target, int base, int width, char pad) 31 { 32 char tmpbuf[INT64_MAX_DIGIT + 1]; 33 char *tmppos = tmpbuf; 34 char *pos = buf; 35 char *left; 36 char *right; 37 char swap; 38 char ch; 39 int length = 0; 40 int i; 41 42 do { 43 *(tmppos ++) = "0123456789abcdef"[target % base]; 44 length ++; 45 } 46 while (target /= base); 47 48 *tmppos = '\0'; 49 left = tmpbuf; 50 right = tmppos - 1; 51 52 while (left < right) { 53 swap = *left; 54 *(left ++) = *right; 55 *(right --) = swap; 56 } 57 58 for (i = 0; i < width - length; i ++) { 59 *(pos ++) = pad; 60 } 61 62 tmppos = tmpbuf; 63 64 while ((ch = *(tmppos ++))) { 65 *(pos ++) = ch; 66 } 67 68 *pos = '\0'; 69 return (width > length) ? width : length; 70 } 71 72 /* End of util.c */
- 16 ¡Á 24 ¹ÔÌÜ (delay_500ms())
500 ms ÂÔµ¡¤¹¤ë¤¿¤á¤Î delay_500ms() ¤ò¡¢avr-libc ¤Î _delay_ms() ¤ò»È¤Ã¤Æ¿·¤¿¤ËÍÑ°Õ¤·¤Þ¤·¤¿¡£
- 27 ¡Á 70 ¹ÔÌÜ (itoa())
¿ôÃͤò»ØÄꤷ¤Æʸ»úÎó¤ËÊÑ´¹¤¹¤ë¼«ºî¤Î´Ø¿ô¤Ç¤¹¡£»ØÄꤷ¤Æʸ»úÎóĹ¤Ë¤Ê¤ë¤Þ¤Ç¡¢ »ØÄꤷ¤¿Ê¸»ú¤òº¸Â¦¤«¤éµÍ¤á¤Æ¤¯¤ì¤Þ¤¹¡£Î㤨¤Ð itoa(buf, 10, 10, 5, '*') ¤È¤¹¤ë¤È¡¢ ÊÑ´¹¤Î·ë²ÌÆÀ¤é¤ì¤ëʸ»úÎó¤Ï "***10" ¤È¤Ê¤ê¤Þ¤¹¡£
int itoa(char *buf, int target, int base, int width, char pad); buf : ÊÑ´¹·ë²Ì¤Îʸ»úÎó¤ò³ÊǼ¤¹¤ë¤¿¤á¤Î¥Ð¥Ã¥Õ¥¡ target : ÊÑ´¹ÂоݤοôÃÍ base : ´ð¿ô width : ÊÑ´¹·ë²Ì¤Îʸ»úÎó¤Îʸ»úĹ pad : width ¤Ë㤹¤ë¤Þ¤Çʸ»úÎó¤Îº¸Â¦¤«¤éËä¤á¤ëʸ»ú
avr-libc ¤Îʸ»úÎóÁàºî´Ø¿ô¤ò»È¤¤¤¿¤¤½ê¤Ç¤¹¤¬¡¢Á°¤Ë¤â½ñ¤¤Þ¤·¤¿Íͤˡ¢¥ê¥ó¥¯¤¹¤ë¤È ATmega8535 ¤Î¥Õ¥é¥Ã¥·¥å¥á¥â¥êÍÆÎÌ 8KB ¤ò·Ú¤¯Ä¶¤¨¤Æ¤·¤Þ¤¦¤¿¤áɬÍ׺Ǿ®¸Â¤Î½èÍý¤ò ÍÑ°Õ¤¹¤Ù¤¯¼«ºî¤·¤Æ¤¤¤Þ¤¹¡Ä
- main-int.c¡Ê³ä¤ê¹þ¤ß»ÈÍÑ»þ¤Î main¡Ë
1 #include <avr/interrupt.h> 2 #include <avr/io.h> 3 #include <avr/sleep.h> 4 #include <inttypes.h> 5 6 #include "adc.h" 7 #include "usart.h" 8 #include "util.h" 9 10 11 static void adc_interrupt(void); 12 13 14 int 15 main(void) 16 { 17 pullup(); 18 PORTA &= ~_BV(PINA0); /* PINA0 ¥×¥ë¥¢¥Ã¥×̵¸ú */ 19 20 /* (8MHz/64) = 125KHz */ 21 adc_init(ADC_CK_64, ADC_USE_INT); 22 adc_set_interrupt(adc_interrupt); 23 adc_set_ch(PINA0); 24 usart_init(USART_NO_INT); 25 26 sei(); 27 28 while (1) { 29 sleep_mode(); 30 delay_500ms(); 31 } 32 33 return 0; /* ·Ù¹ð¤òÈò¤±¤ë¤¿¤á */ 34 } 35 36 37 static void 38 adc_interrupt(void) 39 { 40 char abuf[5]; 41 char *pos = abuf; 42 char ch; 43 char buf[32] = "PINA0 input = \r\n"; 44 int index = 14; 45 46 uint16_t data = adc_get(); 47 48 itoa(abuf, data, 10, 4, '0'); 49 50 while ((ch = *(pos ++))) { 51 buf[index ++] = ch; 52 } 53 54 usart_tx_string(buf); 55 } 56 57 /* End of main-int.c */
ËÜ¥Õ¥¡¥¤¥ë¤Ï¡¢A/D ÊÑ´¹´°Î»³ä¤ê¹þ¤ß¤ò»È¤¦¾ì¹ç¤Î main() ¤òµ½Ò¤·¤Æ¤¤¤Þ¤¹¡£
- 14 ¡Á 34 ¹ÔÌÜ (main())
- pullup() (pullup.h¡¢Á´ÆþÎϥݡ¼¥È¤ÎÆâÉô¥×¥ë¥¢¥Ã¥×¤ò͸ú²½)
- PINA0 ¥Ô¥ó¤ÏÆþÎϤȤ·¤Æ»ÈÍѤ¹¤ë¤Î¤Ç¡¢¥×¥ë¥¢¥Ã¥×¤ò̵¸ú²½
- adc_init() (adc.h¡¢A/D ÊÑ´¹´ï½é´ü²½)
- adc_set_interrupt() (adc.h¡¢A/D ÊÑ´¹´°Î»³ä¤ê¹þ¤ß»þ¤Ë¼Â¹Ô¤¹¤ë¥æ¡¼¥¶´Ø¿ô¤ÎÅÐÏ¿)
- adc_set_ch() (adc.h¡¢PINA0 ¤ò¥¢¥Ê¥í¥°ÆþÎϤȤ·¤ÆÀßÄê)
- usart_init() (usart.h¡¢USART ½é´ü²½)
¤ò¹Ô¤Ã¤¿¸å¡¢avr/interrupt.h ¤ÇÄêµÁ¤µ¤ì¤Æ¤¤¤ë sei() ¤ò¼Â¹Ô¤·¤Æ¥·¥¹¥Æ¥à¤Î³ä¤ê¹þ¤ß¤ò͸ú²½¤·¤Æ¤¤¤Þ¤¹¡£ ¤³¤ì¤Ë¤è¤ê A/D ÊÑ´¹´°Î»¤ÎÅ٤ˡ¢¸å½Ò¤¹¤ë adc_interrupt() ¤¬¼Â¹Ô¤µ¤ì¤Þ¤¹¡£
°Ê¸å 28 ¡Á 31 ¹ÔÌܤÎ̵¸Â¥ë¡¼¥×¤Ë¤Æ
- avr-libc ¤Î sleep_mode() ¤Ë¤è¤ë¥¢¥¤¥É¥ëÆ°ºî¤Ø°Ü¹Ô
- ³ä¤ê¹þ¤ßÅù¤Ë¤è¤ëÉüµ¢¸å¡¢delay_500ms() ¤Ë¤è¤Ã¤Æ 500ms ÂÔ¤Ä
¤ò·«¤êÊÖ¤·¤Æ¤¤¤Þ¤¹¡£
¤µ¤Æ¡¢ËÜ¥Õ¥¡¥¤¥ë¤Ç¤Ï A/D ÊÑ´¹¤ò³«»Ï¤¹¤ë¥³¡¼¥É¤¬¤É¤³¤Ë¤âͤê¤Þ¤»¤ó¡£ ¤³¤³¤Ç¤Ï¡¢¤Þ¤ÀÀâÌÀ¤·¤Æ¤¤¤Ê¤¤¥Î¥¤¥ºÄ㸺µ¡Ç½¤ò»È¤Ã¤Æ A/D ÊÑ´¹¤ò¼Â¹Ô¤·¤Æ¤¤¤Þ¤¹¡£ ¥Î¥¤¥ºÄ㸺µ¡Ç½¤Ï¡¢CPU ¤ÎÄä»ßÃæ¤Ë A/D ÊÑ´¹¤ò¹Ô¤¦¤³¤È¤ÇÊÑ´¹·ë²Ì¤Ë¾è¤Ã¤Æ¤¯¤ë¥Î¥¤¥º¤ò¸º¤é¤¹µ¡Ç½¤Ç¤¹¡£ °Ê²¼¤ÎÍͤˤ·¤Æ¥Î¥¤¥ºÄ㸺µ¡Ç½¤Ë¤è¤ë A/D ÊÑ´¹¤ò¼Â¹Ô¤·¤Þ¤¹¡£
- ADCSRA ¥ì¥¸¥¹¥¿¤Î ADEN ¥Ó¥Ã¥È¤ò 1 ¤È¤·¤Æ A/D ÊÑ´¹´ï¤ò͸ú¤Ë¤¹¤ë
- ADCSRA ¥ì¥¸¥¹¥¿¤Î ADSC ¥Ó¥Ã¥È¤¬ 0 ¤Ç¤¢¤ë¤³¤È¤ò³Îǧ¤¹¤ë
- ADCSRA ¥ì¥¸¥¹¥¿¤Î ADATE ¥Ó¥Ã¥È¤¬ 0 ¤Ç¤¢¤ë¤³¤È¤ò³Îǧ¤¹¤ë¡Ê¼«Æ°ÊÑ´¹Ìµ¸ú¡Ë
- ADCSRA ¥ì¥¸¥¹¥¿¤Î ADIE ¥Ó¥Ã¥È¤ò 1 ¤È¤·¤Æ A/D ÊÑ´¹´°Î»³ä¤ê¹þ¤ß¤ò͸ú¤Ë¤¹¤ë
- ¥¢¥¤¥É¥ëÆ°ºî¤Ë°Ü¹Ô¤¹¤ë¡ÊCPU Ää»ß¸å¡¢A/D ÊÑ´¹¤¬³«»Ï¤µ¤ì¤ë¡Ë
- A/D ÊÑ´¹´°Î»¤Ë¤è¤Ã¤Æ A/D ÊÑ´¹´°Î»³ä¤ê¹þ¤ß¤¬È¯À¸¤· CPU ¤¬µ¯Æ°¤¹¤ë
¾åµ¤òƧ¤Þ¤¨¤ë¤È¡¢ËÜ¥Õ¥¡¥¤¥ë¤Î main() ¤Ï sleep_mode() ¤Ë¤è¤Ã¤Æ A/D ÊÑ´¹¤ò³«»Ï¤·¡¢ ¸å½Ò¤Î adc_interrupt() ¤Ç³ä¤ê¹þ¤ß½èÍý¤ò¹Ô¤¤¡¢500ms ÂԤäƺÆÅÙ sleep_mode() ¤Ë¤è¤ê A/D ÊÑ´¹¤ò³«»Ï¤¹¤ë¤È¤¤¤¦Î®¤ì¤Ë¤Ê¤Ã¤Æ¤¤¤ë¤³¤È¤¬Ê¬¤ê¤Þ¤¹¡£
- 37 ¡Á 55 ¹ÔÌÜ (adc_interrupt())
A/D ÊÑ´¹´°Î»³ä¤ê¹þ¤ß»þ¤Ë¼Â¹Ô¤µ¤ì¤ë¥æ¡¼¥¶´Ø¿ô¤Ç¤¹¡£
¤Þ¤º adc_get() ¤Ç A/D ÊÑ´¹·ë²Ì¤ò¼èÆÀ¤·¤Þ¤¹¡£¤½¤Î¸å¡¢ÆÀ¤é¤ì¤¿ A/D ÊÑ´¹·ë²Ì¤Î¿ôÃͤò itoa() ¤Ë¤Æ 10 ¿Ê¿ô 4 ʸ»úŤÎʸ»úÎó¤ËÊÑ´¹¤·¤Æ¤¤¤Þ¤¹¡£¤½¤ÎºÝ¡¢4 ʸ»ú¤ËËþ¤¿¤Ê¤¤Ê¬¤Ï '0' ¤ÇËä¤á¤é¤ì¤Þ¤¹¡£¤½¤·¤Æ "PINA0 input = ..." ¤È¤¤¤¦Ê¸»úÎó¤òºîÀ®¤·¤¿¸å¡¢usart_tx_string() ¤Ë¤è¤Ã¤Æ USART Á÷¿®¥Ð¥Ã¥Õ¥¡¤Ëʸ»úÎó¤òÀßÄꤷ¡¢³ä¤ê¹þ¤ß½èÍý¤ò½ªÎ»¤·¤Þ¤¹¡£
- main-noint.c¡Ê³ä¤ê¹þ¤ß̤»ÈÍÑ»þ¤Î main¡Ë
1 #include <avr/io.h> 2 #include <inttypes.h> 3 4 #include "adc.h" 5 #include "usart.h" 6 #include "util.h" 7 8 9 #define INDEX_TO_INSERT 14 10 11 12 int 13 main(void) 14 { 15 char abuf[5]; 16 char *pos; 17 char ch; 18 char buf[32] = "PINA0 input = \r\n"; 19 int index; 20 uint16_t data; 21 22 pullup(); 23 PORTA &= ~_BV(PINA0); /* PINA0 ¥×¥ë¥¢¥Ã¥×̵¸ú */ 24 25 /* (8MHz/64) = 125KHz */ 26 adc_init(ADC_CK_64, ADC_NO_INT); 27 adc_set_ch(PINA0); 28 usart_init(USART_NO_INT); 29 30 while (1){ 31 adc_start(); 32 adc_wait(); 33 data = adc_get(); 34 35 itoa(abuf, data, 10, 4, '0'); 36 index = INDEX_TO_INSERT; 37 pos = abuf; 38 39 while ((ch = *(pos ++))) { 40 buf[index ++] = ch; 41 } 42 43 usart_tx_string(buf); 44 delay_500ms(); 45 } 46 47 return 0; /* ·Ù¹ð¤òÈò¤±¤ë¤¿¤á */ 48 } 49 50 /* End of main-noint.c */
ËÜ¥Õ¥¡¥¤¥ë¤Ï¡¢A/D ÊÑ´¹´°Î»³ä¤ê¹þ¤ß¤ò»È¤ï¤Ê¤¤¾ì¹ç¤Î main() ¤òµ½Ò¤·¤Æ¤¤¤Þ¤¹¡£
- 31 ¡Á 32 ¹ÔÌÜ
- pullup() (pullup.h¡¢Á´ÆþÎϥݡ¼¥È¤ÎÆâÉô¥×¥ë¥¢¥Ã¥×¤ò͸ú²½¡Ë
- PINA0 ¥Ô¥ó¤ÏÆþÎϤȤ·¤Æ»ÈÍѤ¹¤ë¤Î¤Ç¡¢¥×¥ë¥¢¥Ã¥×¤ò̵¸ú²½
- adc_init() (adc.h¡¢A/D ÊÑ´¹´ï½é´ü²½)
- adc_set_ch() (adc.h¡¢PINA0 ¤ò¥¢¥Ê¥í¥°ÆþÎϤȤ·¤ÆÀßÄê)
- usart_init() (usart.h¡¢USART ½é´ü²½)
¤ò¹Ô¤Ã¤¿¸å¡¢while ¥ë¡¼¥×¤ËÆþ¤ê¤Þ¤¹¡£¥ë¡¼¥×Æâ¤Ç¤Ï¤Þ¤º
adc_start(); adc_wait();
¤ò¼Â¹Ô¤·¤Æ¡¢A/D ÊÑ´¹³«»Ï¸å¡¢ÊÑ´¹¤¬½ªÎ»¤¹¤ë¤Þ¤ÇÂÔ¤Á¤Þ¤¹¡£adc_wait() ¤ÏÊÑ´¹¤¬½ªÎ»¤¹¤ë¤Þ¤Ç¥Ö¥í¥Ã¥¯¤·¤Þ¤¹¡£
ÊÑ´¹½ªÎ»¸å adc_get() ¤Ë¤è¤Ã¤ÆÊÑ´¹·ë²Ì¤ò¼èÆÀ¤·¡¢Ê¸»úÎó²Ã¹©¤Î¸å¤Ë usart_tx_string() ¤Ë¤è¤Ã¤Æ USART Á÷¿®¥Ð¥Ã¥Õ¥¡¤Ëʸ»úÎó¤òÀßÄꤹ¤ë½ê¤Ï¡¢A/D ÊÑ´¹´°Î»³ä¤ê¹þ¤ß»ÈÍÑ»þ¤Î main() ¤ÈƱ¤¸¤Ç¤¹¡£
¤³¤Á¤é¤Î main() ¤Ç¤Ï¤½¤Î¸å delay_500ms() ¤ò¼Â¹Ô¤·¤Æ¡¢while() ¥ë¡¼¥×¤Ë¤è¤ë¼¡¤Î A/D ÊÑ´¹³«»Ï¤Þ¤Ç 500ms ÂԤäƤ¤¤Þ¤¹¡£
°Ê¾å¤Ç²òÀâ¤Ï½ª¤ï¤ê¤Ç¤¹¡£A/D ÊÑ´¹µ¡Ç½¤¬»È¤¨¤ëÍͤˤʤì¤Ð¡¢¿§¡¹¤Ê¥»¥ó¥µÆþÎϤò AVR ¤ÇºÎ¤ê¤³¤ó¤Ç»ÈÍѤ¹¤ë¤³¤È¤¬½ÐÍè¤Þ¤¹¡£¤¼¤Ò³Ú¤·¤ó¤Ç¤ß¤Æ¤¯¤À¤µ¤¤¡ª