|
¡û ¤Ï¤¸¤á¤Ë
Á°²ó ¤Ï¡¢¥¿¥¤¥Þ/¥«¥¦¥ó¥¿1 ¤Î¹â® PWM µ¡Ç½¤ò»È¤Ã¤Æ¥µ¡¼¥Ü¥â¡¼¥¿¤òÆ°¤«¤·¤Æ¤ß¤Þ¤·¤¿¡£PWM µ¡Ç½¤Ï¡¢¤¢¤ë¥Ô¥ó¤ËÂФ·¤Æ¼«Æ°Åª¤Ë PWM ¿®¹æ¤ò½ÐÎϤ·¤Æ¤¯¤ì¤ë¤¿¤áÈó¾ï¤ËÊØÍø¤Ç¤¹¡£ATmega8535 ¤Î¾ì¹ç¡¢¥¿¥¤¥Þ/¥«¥¦¥ó¥¿0¡Á2 Á´¤Æ¤Î PWM µ¡Ç½¤ò»È¤¦¤È OC0/OC1A/OC1B/OC2 ¥Ô¥ó¤ÎºÇÂç 4 ËܤΠPWM ½ÐÎϤòÀ¸À®¤Ç¤¤Þ¤¹¡£
º£²ó¤Ï¡¢¥¿¥¤¥Þ/¥«¥¦¥ó¥¿0 ¤ÎÈæ³Ó°ìÃ׳ä¤ê¹þ¤ß¤ò»È¤Ã¤Æ¼«ÎÏ¤Ç PWM ½ÐÎϤòºîÀ®¤·¡¢¥µ¡¼¥Ü¥â¡¼¥¿¤òÆ°¤«¤·¤Æ¤ß¤¿¤¤¤È»×¤¤¤Þ¤¹¡£¼«ÎÏ¤Ç PWM ½ÐÎϤòºîÀ®¤¹¤ë¤È¡¢4 ËܤȤ¤¤ï¤º¹¥¤¤ÊËÜ¿ô¤Î PWM ½ÐÎϤòÀ¸À®¤¹¤ë¤³¤È¤â²Äǽ¤Ë¤Ê¤ê¤Þ¤¹¡ÊÆþ½ÐÎϥݡ¼¥È¿ô¤ä¡¢¼ÂºÝ¤Ë¤Ï¼Â¹Ô®ÅÙÅù¤ÎÀ©¸Â¤ò¼õ¤±¤ë¤È»×¤¤¤Þ¤¹¤¬¡Ä¡Ë¡£
²óÏ©¿Þ¤Ï Á°²ó ¤ÈƱ¤¸¤Ê¤Î¤Ç¡¢¤¤¤¤Ê¤ê¥µ¥ó¥×¥ë¥×¥í¥°¥é¥à¤ÈÀâÌÀ¤Ë¤¤¤¤¿¤¤¤È»×¤¤¤Þ¤¹¡£
¡û ¥µ¥ó¥×¥ë¥×¥í¥°¥é¥à¤ÈÀâÌÀ
¥¿¥¤¥Þ/¥«¥¦¥ó¥¿0¤ÎÈæ³Ó°ìÃ׳ä¤ê¹þ¤ß¤ò»È¤Ã¤Æ¼«ÎϤǥµ¡¼¥Ü¥â¡¼¥¿¤òÆ°¤«¤¹¥µ¥ó¥×¥ë¥×¥í¥°¥é¥à¤ò 10pwm2.tar.gz ¤«¤é¥À¥¦¥ó¥í¡¼¥É¤Ç¤¤Þ¤¹¡£¤Ï¤¸¤á¤Ë ¤Î¥Ú¡¼¥¸¤«¤é¡¢Á´¤Æ¤Î¥µ¥ó¥×¥ë¥×¥í¥°¥é¥à¤ò ¸Ç¤á¤¿ tar ¥Õ¥¡¥¤¥ë¤ò¥À¥¦¥ó¥í¡¼¥É¤¹¤ë¤³¤È¤â¤Ç¤¤Þ¤¹¡£
Ãæ¤Ë¤Ï
- Makefile
- util.h
- util.c
- timer0.h
- timer0.c
- main.c
¤¬³ÊǼ¤µ¤ì¤Æ¤¤¤Þ¤¹¡£util.h/util.c ¤Ë¤Ä¤¤¤Æ¤Ï LECTURE 4 A/D ÊÑ´¹¤Ç¥Ü¥ê¥å¡¼¥à¡Ê²ÄÊÑÄñ¹³¡ËÃͤòÆÉ¤à ¤ÎÆâÍƤÈƱ¤¸¡¢timer0.h/timer0.c ¤Ë¤Ä¤¤¤Æ¤Ï LECTURE 7 ¥¿¥¤¥Þ/¥«¥¦¥ó¥¿0¤ò»È¤¦ ¤ÎÆâÍƤÈƱ¤¸¤Ç Makefile ¤â¤Û¤È¤ó¤ÉÊѤï¤é¤Ê¤¤¤Î¤Ç¡¢main.c ¤Ë¤Ä¤¤¤Æ°Ê²¼¤ËºÜ¤»¤Þ¤¹¡£¤Ê¤ª¡¢°Ê²¼¤Ë·ÇºÜ¤·¤Æ¤¤¤ë¥½¡¼¥¹¥³¡¼¥É¤Ë¤Ï½¤Àµ BSD ¥é¥¤¥»¥ó¥¹¾ò¹à¤ò´Þ¤á¤Æ¤¤¤Þ¤»¤ó¤¬¡¢¥½¡¼¥¹¥³¡¼¥ÉÇÛÉۤϽ¤Àµ BSD ¥é¥¤¥»¥ó¥¹¤Ë½¾¤¤¤Þ¤¹¡Ê¤Ï¤¸¤á¤Ë ¤Î¥Ú¡¼¥¸¤ÎÌÈÀÕ»ö¹à¤ò»²¾È¤·¤Æ¤¯¤À¤µ¤¤¡Ë¡£
¤³¤ì¤Þ¤Ç¤Ë¥Ò¥å¡¼¥º¥Ó¥Ã¥È¤ÎÀßÄê¤ò¹Ô¤Ã¤¿¤³¤È¤¬°ìÅÙ¤â̵¤¤¾ì¹ç¤Ï¡¢¤Þ¤ººÇ½é¤Ë make fuse ¤ò¼Â¹Ô¤·¤Æ¤¯¤À¤µ¤¤¡£fuse ¥¿¡¼¥²¥Ã¥È¤Ë¤Ä¤¤¤Æ¤Ï¡¢LECTURE 2 LED ¤òÅÀÌǤµ¤»¤ë ¤Î¥µ¥ó¥×¥ë¥×¥í¥°¥é¥à¤Î¹à¤ò»²¾È¤·¤Æ¤¯¤À¤µ¤¤¡£
make ¼Â¹Ô¸å¡¢¥×¥í¥°¥é¥à¤ò¥¢¥Ã¥×¥í¡¼¥É¤¹¤ë¾ì¹ç¤Ï make uisp ¤ò¼Â¹Ô¤·¤Æ¤¯¤À¤µ¤¤¡£
¥µ¡¼¥Ü¥â¡¼¥¿¤¬Ìó 1 ÉÃËè¤ËÈ¿»þ·×²ó¤ê60¡ë¢ªÃæ¿´¢ª»þ·×²ó¤ê60¡ë¡Ä¤Î½ç¤Ë²óž³Ñ¤òÊѤ¨¤Þ¤¹¡£Á°²ó ¤ÈÆ°ºîÆâÍƤ¬Æ±¤¸¤Ê¤Î¤Ç¡¢Æ°²è¤ÏºÜ¤»¤Þ¤»¤ó¡£
- main.c
1 #include <avr/interrupt.h> 2 #include <avr/io.h> 3 #include <avr/sleep.h> 4 5 #include "timer0.h" 6 #include "util.h" 7 8 9 #define OCR0_AS_TOP 99 10 11 static void timer0_comp_interrupt(void); 12 13 14 int 15 main(void) 16 { 17 DDRD |= _BV(DDD5); /* PD5 ¥Ô¥ó¤ò½ÐÎϤËÀßÄê */ 18 pullup(); 19 20 timer0_comp_enable(); 21 timer0_comp_set_interrupt(timer0_comp_interrupt); 22 timer0_set_wave_mode(WGM0_CTC); 23 timer0_set_count(0); 24 timer0_set_ocr0(OCR0_AS_TOP); 25 26 sei(); 27 28 /* (8MHz/8)/100 = 10KHz */ 29 timer0_start(TIMER0_CK_8); 30 31 while (1){ 32 sleep_mode(); 33 } 34 35 return 0; /* ·Ù¹ð¤òÈò¤±¤ë¤¿¤á */ 36 } 37 38 39 static void 40 timer0_comp_interrupt(void) 41 { 42 static int duty[3][2] = { 43 {21, 179}, 44 {15, 185}, 45 { 9, 191} 46 }; 47 48 static int dir = 0; /* Êý¸þ */ 49 static int state = 0; /* H/L */ 50 static int count = 0; 51 static int pulse_count = 0; 52 53 /* 1ÉÃËè¤ËÊý¸þ¤òÊѤ¨¤ë */ 54 if (++ count == 10000) { 55 dir = (dir + 1) % 3; 56 state = count = pulse_count = 0; 57 PORTD |= _BV(PORTD5); 58 return; 59 } 60 61 /* PWM À©¸æ */ 62 if (++ pulse_count == duty[dir][state]) { 63 state = (state + 1) % 2; 64 pulse_count = 0; 65 PORTD ^= _BV(PORTD5); 66 } 67 } 68 69 /* End of main.c */
- 14 ¡Á 36 ¹ÔÌÜ (main())
- pullup() Á°¤Ë PORTD5 ¤Î¤ß½ÐÎÏÊý¸þ¤ËÀßÄê
- pullup() (pullup.h¡¢PORTD5 °Ê³°¤ÎÁ´ÆþÎϥݡ¼¥È¤ÎÆâÉô¥×¥ë¥¢¥Ã¥×¤ò͸ú²½)
- timer0_comp_enable() (timer0.h¡¢Èæ³Ó°ìÃ׳ä¤ê¹þ¤ß͸ú²½)
- timer0_comp_set_interrupt() (timer0.h¡¢Èæ³Ó°ìÃ׳ä¤ê¹þ¤ß»þ¤Ë¼Â¹Ô¤¹¤ë¥æ¡¼¥¶´Ø¿ô¤ÎÅÐÏ¿)
- timer0_set_wave_mode() (timer0.h¡¢ÇÈ·ÁÀ¸À®¼ïÊ̤òÀßÄê)
- timer0_set_count() (timer0.h¡¢TCNT0 ¤Î½é´üÃͤòÀßÄê)
- timer0_set_ocr0() (timer0.h¡¢OCR0 ¤ÎÃͤòÀßÄê)
¤ò¹Ô¤Ã¤¿¸å¡¢sei() (avr/interrupt.h) ¤ò¼Â¹Ô¤·¤Æ¥·¥¹¥Æ¥à¤Î³ä¤ê¹þ¤ß¤ò͸ú²½¤·¤Æ¤¤¤Þ¤¹¡£
¤³¤Î½é´ü²½¥Õ¥§¡¼¥º¤Ç¡¢timer0_set_wave_mode(WGM0_CTC) ¤Ë¤è¤Ã¤ÆÇÈ·ÁÀ¸À®¼ïÊ̤ò "Èæ³Ó°ìÃ×¥¿¥¤¥Þ/¥«¥¦¥ó¥¿¥¯¥ê¥¢(CTC)"¤ËÀßÄꤷ¤Æ¤¤¤Þ¤¹¡£¤³¤ì¤Ë¤è¤ê TCNT0 ¤È OCR0 ¤¬°ìÃפ·¤¿»þÅÀ¤Ç TCNT0 ¤ò¥¯¥ê¥¢¤·¡¢0 ¤«¤é¥«¥¦¥ó¥È¤òºÆ³«¤¹¤ëÆ°ºî¤òÁªÂò¤·¤Æ¤¤¤Þ¤¹¡£
¤½¤Î¸å¤Ë timer0_start() ¤ò¼Â¹Ô¤·¤Æ¥¿¥¤¥Þ/¥«¥¦¥ó¥¿0 ¤ÎÆ°ºî¤ò³«»Ï¤·¤Þ¤¹¡£ timer0_start() ¤Ë TIMER0_CK_8 ¤ò»ØÄꤷ¤Æ¤ª¤ê¡¢¤«¤Ä OCR0 ¤ÎÃͤˤè¤ê¥«¥¦¥ó¥¿ºÇÂçÃÍ¤Ï 99 ¤È¤Ê¤ë¤¿¤á¡¢Èæ³Ó°ìÃ׳ä¤ê¹þ¤ß¤Î¼þ´ü¤ÏÌó 10KHz ((8MHz/8)/100)¤È¤Ê¤ê¤Þ¤¹¡£
°Ê¸å sleep_mode() ¤Ë¤Æ¥¢¥¤¥É¥ëÆ°ºî¤Ø°Ü¹Ô¤·¡¢LED ¤ÎÅÀÌǽèÍý¤ÏÈæ³Ó°ìÃ׳ä¤ê¹þ¤ß¥Ï¥ó¥É¥é¤ËǤ¤»¤Þ¤¹¡£
- 39 ¡Á 67 ¹ÔÌÜ (timer0_comp_interrupt())
Èæ³Ó°ìÃ׳ä¤ê¹þ¤ß»þ¤Ë¼Â¹Ô¤µ¤ì¤ë¥æ¡¼¥¶´Ø¿ô¤Ç¤¹¡£
42 ¡Á 45 ¹ÔÌܤˤơ¢¼«ÎÏ¤Ç PWM À©¸æ¤¹¤ëºÝ¤Ë PD5 ¥Ô¥ó¤Î H/L ¤ò°Ý»ý¤¹¤ë³ä¤ê¹þ¤ß²ó¿ô¤òÄêµÁ¤·¤Æ¤¤¤Þ¤¹¡£³ä¤ê¹þ¤ß¼þ´ü¤¬ 10KHz ¤Ê¤Î¤Ç¡¢³ä¤ê¹þ¤ß 1 ²ó¤¬ 0.1ms ¤È¤Ê¤ê¤Þ¤¹¡£¤½¤Î¤¿¤á
static int duty[3][2] = { {21, 179}, /* H:2.1ms¡¢L:17.9ms È¿»þ·×²ó¤ê¤Ë 60¡ë*/ {15, 185}, /* H:1.5ms¡¢L:18.5ms Ãæ¿´°ÌÃÖ */ { 9, 191} /* H:0.9ms¡¢L:19.1ms »þ·×²ó¤ê¤Ë 60¡ë*/ };
¤È¤Ê¤ê¤Þ¤¹¡£
53 ¡Á 59 ¹ÔÌܤˤơ¢static ÊÑ¿ô¤Ç¤¢¤ë count ¤Ç³ä¤ê¹þ¤ß²ó¿ô¤ò¿ô¤¨¡¢10000 ²ó¤Ë°ìÅÙ ¡Ê0.1ms x 10000 = 1s¡Ë¥µ¡¼¥Ü¥â¡¼¥¿¤Î²óž³ÑÅÙ¤òÊѤ¨¤ë¤Ù¤¯¡¢static ÊÑ¿ô¤Ç¤¢¤ë dir ¤ÎÃͤòÊѹ¹¤·¤Þ¤¹¡£PWM À©¸æ¤ò¹Ô¤¦ºÝ¤Ë¡¢H/L ¤ò°Ý»ý¤¹¤ë³ä¤ê¹þ¤ß²ó¿ô¤ÎÁȤ߹ç¤ï¤»¤ò duty[dir] ¤È¤·¤Æ»²¾È¤¹¤ë¤¿¤á¡¢1s Ëè¤Ë H/L ¤ò°Ý»ý¤¹¤ë³ä¤ê¹þ¤ß²ó¿ô¤òÊѤ¨¤ë¤³¤È¤Ë¤Ê¤ê¤Þ¤¹¡£
61 ¡Á 66 ¹ÔÌܤǼºݤ˥ԥó¤Î½ÐÎϤòÊѤ¨¤ë¤³¤È¤Ç PWM À©¸æ¤ò¹Ô¤¤¤Þ¤¹¡£ 62 ¹ÔÌܤˤơ¢¤Þ¤º static ÊÑ¿ô¤Ç¤¢¤ë pulse ¤ÎÃͤ¬ H/L ¤ò°Ý»ý¤¹¤Ù¤³ä¤ê¹þ¤ß²ó¿ô ¤ËÅþ㤷¤¿¤«¤É¤¦¤«¤ò³Îǧ¤·¤Æ¤¤¤Þ¤¹¡£ H/L ¤ò°Ý»ý¤¹¤Ù¤³ä¤ê¹þ¤ß²ó¿ô¤Ï duty[dir][state] ¤È¤·¤Æ»²¾È¤·¤Þ¤¹¡£ state = 0 ¤Î¾ì¹ç¤Ï H¡¢state = 1 ¤Î¾ì¹ç¤Ï L ¤ò°Ý»ý¤·¤Þ¤¹¡£
°Ý»ý¤¹¤Ù¤³ä¤ê¹þ¤ß²ó¿ô¤ËÅþ㤷¤¿¾ì¹ç¤Ï¡¢63 ¡Á 65 ¹ÔÌܤˤưʲ¼¤ò¹Ô¤¤¤Þ¤¹¡£
- ¼¡¤Ë°Ý»ý¤¹¤Ù¤¾õÂÖ¤ò»²¾È¤¹¤ëÍÍ¤Ë state ¤ò¹¹¿·
- ¥«¥¦¥ó¥È¤·¤Ê¤ª¤¹¤¿¤á¤Ë pulse ¤ò¥¯¥ê¥¢
- PD5 ¥Ô¥ó¤Î½ÐÎϤòȿž
¤³¤ì¤Ë¤è¤ê¡¢°Õ¿Þ¤·¤¿»þ´ÖÉý¤Î¥Ñ¥ë¥¹¤òÀ¸À®¤¹¤ë¤³¤È¤¬½ÐÍè¤Þ¤¹¡£
°Ê¾å¤Ç²òÀâ¤Ï½ª¤ï¤ê¤Ç¤¹¡£¥µ¡¼¥Ü¥â¡¼¥¿¤òÆ°¤«¤¹¤³¤È¤Ë´·¤ì¤Æ¤¤¿½ê¤Ç¡¢º£Å٤ϥܥê¥å¡¼¥à¤ò²ó¤·¤Æ¥µ¡¼¥Ü¥â¡¼¥¿¤ò²óž¤µ¤»¤Æ¤ß¤è¤¦¤È»×¤¤¤Þ¤¹¡£