Page 1

àÃÕ¹ÃÙጠÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ MCS-51 l 1

àÃÕ¹ÃٌáÅÐÊÌҧË؋¹Â¹µÍѵâ¹ÁÑµÔ ¡ÑºäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ MCS-51 Robot experiment with MCS-51 microcontroller

¹¤Ã ÀÑ¡´ÕªÒµÔ ªÑÂÇѲ¹ ÅÔÁé ¾Ã¨ÔµÃÇÔäÅ (C) Innovative Experiment Co.,Ltd.


2 làÃÕ¹ÃÙá Œ ÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ

MCS-51

Robot experiment with MCS-51 microcontroller

àÃÕ¹ÃÙጠÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ MCS-51 (©ºÑº»ÃѺ»Ãا¤ÃÑ§é ·Õè 3) ¹¤Ã ÀÑ¡´ÕªÒµÔ ªÑÂÇѲ¹ ÅÔÁé ¾Ã¨ÔµÃÇÔäŠʧǹÅÔ¢ÊÔ·¸Ôµì ÒÁ ¾.Ã.º. ÅÔ¢ÊÔ·¸Ôì ¾.È. 2521 ˌÒÁ¡ÒÃÅÍ¡àÅÕ¹äÁ‹Çҋ ʋǹ˹֧è ʋǹ㴢ͧ˹ѧÊ×ÍàŋÁ¹Õé ¹Í¡¨Ò¡¨Ðä´ŒÃºÑ Í¹Ø­Òµ ã¤Ã¤ÇÃ㪌˹ѧÊ×ÍàŋÁ¹Õé 1. ¹Ñ¡àÃÕ¹ ¹ÔÊÔµ ¹Ñ¡ÈÖ¡ÉÒ áÅкؤ¤Å·ÑèÇä»·ÕèÁÕ¤ÇÒÁʹã¨ã¹¡ÒÃÊÌҧË؋¹Â¹µÍѵâ¹ÁѵԢ¹Ò´àÅç¡´ŒÇÂäÁâ¤Ã ¤Í¹â·ÃÅàÅÍÏ MCS-51 2. ʶҺѹ¡ÒÃÈÖ¡ÉÒ âçàÃÕ¹ ÇÔ·ÂÒÅÑ ÁËÒÇÔ·ÂÒÅÑ ·ÕÁè ¡Õ ÒÃແ´¡ÒÃàÃÕ¹¡ÒÃÊ͹ÇÔªÒÍÔàÅç¡·Ã͹ԡʏËÃ×ÍÀÒ¤ ÇÔªÒÇÔÈÇ¡ÃÃÁÍÔàÅç¡·Ã͹ԡʏáÅФÍÁ¾ÔÇàµÍÏ 3. ¤³Ò¨ÒÏ·ÁÕè ¤Õ ÇÒÁµŒÍ§¡ÒÃÈÖ¡ÉÒ áÅÐàµÃÕÂÁ¡ÒÃàÃÕ¹¡ÒÃÊ͹ÇÔªÒäÁâ¤Ãâ»Ãà«Êà«ÍÏ - äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ ã¹ÃдѺ¡ÅÒ§·Õµè ͌ §¡ÒÃ㪌§Ò¹â»Ãá¡ÃÁ¤ÍÁ¾ÔÇàµÍÏÀÒÉÒÊÙ§ 4. ˹‹Ç§ҹàÍ¡ª¹·ÕÁè ¡Õ ÒþѲ¹Ò¼ÅÔµÀѳ±´ÒŒ ¹ÍÔàÅç¡·Ã͹ԡʏáÅФÍÁ¾ÔÇàµÍÏ ´íÒà¹Ô¹¡ÒèѴ¾ÔÁ¾áÅШíÒ˹‹ÒÂâ´Â ºÃÔÉ·Ñ ÍÔ¹â¹àǵտ àÍç¡à¾ÍÃÔàÁ¹µ ¨íÒ¡Ñ´ 3133/53 «.ÊØ¢ÁØ ÇÔ· 101/2 ¶.ÊØ¢ÁØ ÇÔ· á¢Ç§ºÒ§¹Ò ࢵºÒ§¹Ò ¡ÃØ§à·¾Ï 10260 â·ÃÈѾ· 0-2747-7001-4 â·ÃÊÒà 0-2747-7005 ÃÒÂÅÐàÍÕ´·Õ»è ÃÒ¡¯ã¹Ë¹Ñ§Ê×ÍàÃÕ¹ÃÙጠÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ MCS-51 (©ºÑº»ÃѺ»Ãا ¤ÃÑ§é ·Õè 3) 䴌¼Ò‹ ¹¡ÒõÃǨ·Ò¹Í‹ҧÅÐàÍÕ´áÅжŒÇ¹¶Õè à¾×Íè ãˌÁ¤Õ ÇÒÁÊÁºÙóáÅж١µŒÍ§ÁÒ¡·ÕÊè ´Ø ÀÒÂ㵌à§×Íè ¹ä¢ áÅÐàÇÅÒ·Õ¾è §Ö Áա͋ ¹¡ÒèѴ¾ÔÁ¾à¼Âá¾Ã‹ ¤ÇÒÁàÊÕÂËÒÂÍѹÍÒ¨à¡Ô´¨Ò¡¡ÒùíÒ¢ŒÍÁÙÅã¹Ë¹Ñ§Ê×ÍàŋÁ¹Õäé »ãªŒ ºÃÔÉ·Ñ ÍÔ¹â¹àǵտ àÍç¡à¾ÍÃÔàÁ¹µ ¨íÒ¡Ñ´ ÁÔ䴌ÁÀÕ ÒÃÐ㹡ÒÃÃѺ¼Ô´ªÍºáµ‹»ÃСÒÃã´ ¤ÇÒÁ¼Ô´¾ÅÒ´¤ÅÒ´à¤Å×Íè ¹·ÕÍè Ò¨ ÁÕáÅÐä´ŒÃºÑ ¡ÒèѴ¾ÔÁ¾à¼Âá¾Ã‹ÍÍ¡ä»¹Ñ¹é ·Ò§ºÃÔÉ·Ñ Ï ¨Ð¾ÂÒÂÒÁªÕáé ¨§áÅÐᡌä¢ã¹¡ÒèѴ¾ÔÁ¾¤ÃÑ§é µ‹Íä»


àÃÕ¹ÃÙጠÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ MCS-51 l 3

ÂؤÊÁÑ¢ͧÊ×Íè ¡ÒÃàÃÕ¹ÃÙ·Œ ªÕè Í×è “Ëع‹ ¹µ” ¹Ñº¨Ò¡¹Õ¤é Í× ÂؤÊÁÑ¢ͧÊ×Íè ¡ÒÃàÃÕ¹ÃÙ·Œ ªÕè Í×è “Ëع‹ ¹µ” ໚¹àÇÅҹѺÊÔºæ »‚áŌǷÕäè Áâ¤Ã¤Í¹â·ÃÅàÅÍÏࢌÒÁÒÁÕº·ºÒ·Í‹ҧÁҡ㹡ÒþѲ¹ÒÃкº¤Çº¤ØÁÍѵâ¹ÁÑµÔ µÑ§é ᵋ¢¹Ò´àÅç¡ äÁ‹¡ÍÕè ¹Ô ¾ØµàÍÒµ¾µØ 仨¹ÃдѺ¡ÅÒ§áÅÐÃдѺ·Õµè ͌ §ãªŒË¹‹Ç¤ÇÒÁ¨íÒ¢¹Ò´ãË­‹ ª‹Í§ÊÑ­­Ò³µÔ´µ‹Í ¨íҹǹÁÒ¡ ÃÇÁ·Ñ§é ¤ÇÒÁËÅÒ¡ËÅÒ¢ͧǧ¨Ãàª×Íè Áµ‹Í ã¹¢³Ð·Õ¡è ÒÃàÃÕ¹ÃÙʌ Òí ËÃѺ¼ÙàŒ ÃÔÁè µŒ¹¨Ð໚¹ä»µÒÁÃٻẺ ͹ØÃ¡Ñ É¹ÂÔ Á¹Ñ¹è ¤×Í àÃÔÁè ¨Ò¡ÈÖ¡ÉÒʶһ˜µÂ¡ÃÃÁ ªØ´¤íÒÊÑ§è ¡ÒÃàª×Íè Áµ‹ÍÍØ»¡Ã³Í¹Ô ¾ØµàÍÒµ¾µØ Í‹ҧ§‹ÒÂæ áÅÐÁÕ¡Òà ¾Ñ²¹Òâ¤Ã§§Ò¹¢Ö¹é à¾×Íè µÃǨÊͺ¤ÇÒÁÃÙ·Œ àÕè ÃÕ¹Áҵѧé ᵋµ¹Œ «Ö§è äÁ‹ÍÒ¨»¯ÔàÊ¸Ç‹Ò Ãкº¡ÒÃàÃÕ¹ÃÙ㌠¹ÃٻẺ¹Ñ¹é ·íÒãˌ ¼ÙàŒ ÃÕ¹ÁÕÃÒ¡°Ò¹¤ÇÒÁÃÙ·Œ áÕè ¢ç§áç áÅоÌÍÁÊíÒËÃѺ¡ÒÃàµÔºâµà¾×Íè ¾Ñ²¹ÒÃкº·Õãè Ë­‹¢¹Öé «Ñº«ŒÍ¹¢Ö¹é â´Â·Õ¼è àٌ ÃÕ¹¡ç Âѧ¤§µŒÍ§ã½†Ãáٌ ÅÐáÊǧËÒͧ¤¤ÇÒÁÃÙ»Œ ÃСͺà¾ÔÁè àµÔÁµÒÁ¾Ñ²¹Ò¡Òâͧ෤â¹âÅÂÕÍ»Ø ¡Ã³ Ëع‹ ¹µÍµÑ â¹ÁÑµÔ à»š¹ÊÔ§è »ÃдÔÉ°Š·áÕè Ê´§¶Ö§ÈÑ¡ÂÀÒ¾¢Í§¡ÒþѲ¹Ò¡ÒÃàÃÕ¹Ãٌ à¹×Íè §¨Ò¡ã¹¡ÒÃÊÌҧËع‹ ¹µ Íѵâ¹ÁѵãÔ ËŒÊÒí àÃç¨Å§ä´Œ¹¹Ñé µŒÍ§ãªŒ¤ÇÒÁÃÙ·Œ ҧǧ¨Ãä¿¿‡ÒáÅÐÍÔàÅç¡·Ã͹ԡʏ ¤ÇÒÁࢌÒã¨ã¹àªÔ§¡ÅÈÒʵÏ ÇÔ·ÂÒÈÒʵÏ ¼¹Ç¡à¢ŒÒ¡Ñº¤ÇÒÁÃÙ´Œ Ҍ ¹¡ÒÃà¢Õ¹â»Ãá¡ÃÁà¾×Íè ¤Çº¤ØÁ¡Ò÷íÒ§Ò¹ ¨¹·íÒãˌ˹؋ ¹µ·Òí §Ò¹ä´Œã¹·ÕÊè ´Ø ´Ñ§¹Ñé¹ËÒ¡¨ÐÁͧËÒ¤ÇÒÁàª×èÍÁ⧢ͧ “Ë؋¹Â¹µÍѵâ¹ÁѵԔ ¡Ñº “äÁâ¤Ã¤Í¹â·ÃÅàÅÍϔ ¨Ð¾º¨Ø´µ‹Í àª×Íè ÁÁÒ¡ÁÒ ¨¹ÍÒ¨¡Å‹ÒÇ䴌Çҋ “Áѹ໚¹àÃ×Íè §à´ÕÂǡѹ” ´Ñ§¹Ñ¹é ËÒ¡ÊÒÁÒö¼ÊÁ¼ÊÒ¹¤ÇÒÁ¹‹Òʹ㨠ʹءʹҹ ·ŒÒ·Ò¨ҡ¡ÒÃÊÌҧËع‹ ¹µÅ§ä»ã¹¡ÒÃàÃÕ¹ÃÙäŒ Áâ¤Ã¤Í¹â·ÃÅàÅÍÏ ¹‹Ò¨Ð໚¹ÍÕ¡·Ò§àÅ×Í¡¢Í§¡ÒþѲ¹ÒÊ×Íè ¡Òà àÃÕ¹ÃÙ·Œ ¼Õè ÊÁ¼Êҹ͋ҧŧµÑÇ ÃÐËNjҧ¡ÒÃàÃÕ¹ÃÙ·Œ àÕè ¢ŒÁá¢ç§áººÍ¹ØÃ¡Ñ É¹ÂÔ Á¡Ñº¤ÇÒÁºÑ¹à·Ô§·ÕÁè ÊÕ ÒÃÐáÅоѲ¹Ò ¤ÇÒÁ¤Ô´ÊÌҧÊÃ䏷äÕè ´Œ¨Ò¡Ëع‹ ¹µ ¹Í¡¨Ò¡¹Ñ¹é ÂѧÁÕÍ¡Õ ÊÔ§è Ë¹Ö§è ·Õ¼è àٌ ÃÕ¹¨Ð䴌¤Í× ¡ÒþѲ¹ÒàªÔ§ÍÒÃÁ³ à¹×Íè §¨Ò¡ã¹ÃÐËNjҧ¡ÒÃàÃÕ¹ÃÙጠÅÐ ¡ÒÃÊÌҧËع‹ ¹µ ¼ÙàŒ ÃÕ¹¨Ð¾º¡Ñºº··´Êͺ·Õ·è §Ñé ¤Ò´ËÁÒÂ䴌áÅÐà˵ءÒó·äÕè Á‹ÍҨŋǧÃٌ ʋ§¼Åãˌ䴌㪌»¯ÔÀÒ³ äËǾÃÔº áÅлÃÐʺ¡Òó ¼¹Ç¡¡Ñº¤ÇÒÁáÁ‹¹ÂíÒã¹à¹×Íé ËÒ·ÄÉ®Õ à¢ŒÒÁÒËÇÁ¡Ñ¹à¾×Íè ᡌ䢻˜­ËÒáÅлŴà»Å×Íé § ¢ŒÍ¨íÒ¡Ñ´ «Ö§è ÊÔ§è àËŋҹըé Ðà¡Ô´¢Ö¹é äÁ‹ä´ŒàÅ ËÒ¡¼ÙàŒ ÃÕ¹¢Ò´ÊÁÒ¸ÔáÅФǺ¤ØÁÍÒÃÁ³äÁ‹ä´Œ ¡Ò÷íÒ§Ò¹·Õáè µ¡µ‹Ò§¡Ñ¹ ä»ã¹·Ø¡ª‹Ç§àÇÅҢͧËع‹ ¹µ Íѹà¹×Íè §ÁÒ¨Ò¡ÊÀÒ¾áǴŌÍÁ·Õ¤è Ǻ¤ØÁãˌ¤§·Õäè Á‹ä´Œ ËÃ×ÍÃдѺ¾Åѧ§Ò¹¢Í§Ëع‹ ¹µ ·ÕÅè ´Å§ËÃ×Íà»ÅÕÂè ¹á»Å§ä»µÅÍ´àÇÅÒ·Õàè ¤Å×Íè ¹·Õè ÊÔ§è àËŋҹըé Ð໚¹Íա˹֧è ˹ŒÒ¢Í§º·àÃÕ¹·Õ¼è àٌ ÃÕ¹¨ÐµŒÍ§¾ºáÅР༪ԭ˹ŒÒÍ‹ҧÁÕʵԴnj ÂÍÒÃÁ³·ÁÕè ¹Ñè ¤§ ´ŒÇ»˜¨¨Ñ·աè ŋÒÇÁҷѧé ËÁ´¹‹Ò¨Ð໚¹ÊÔ§è ·Õªè Nj ÂãˌàËç¹´ŒÇ¡ѺÇÅÕ㹺Ã÷Ѵáá¢Í§º·¹íÒ¹Õé “Ëع‹ ¹µ” ÁÔ㪋 à¾Õ§¢Í§àŋ¹ ÊÔ§è »ÃдÔÉ°Š·àÕè Á×Íè ÊÌҧàÊÃ稨ºáŌǡçáŌǡѹ “Ëع‹ ¹µ” ÂѧÁÕ¤³Ù»¡ÒÃὧã¹àªÔ§¡ÒÃ໚¹µÑÇá·¹Ê×Íè ¡Òà àÃÕ¹ÃÙ·Œ Ò§ÇÔ·ÂÒÈÒʵÏááÅÐà·¤â¹âÅÂÕ·¼Õè ¤ÙŒ ¹ã¹ÈµÇÃÃɹÕÁé ÍÔ Ò¨»¯Ôàʸ䴌 ¤ÇÒÁࢌÁ¢Œ¹¹‹Òʹã¨ÁÔ䴌ÍÂÙ·‹ ÃÕè »Ù Åѡɳ ÀÒ¹͡ ËҡᵋÁ¹Ñ ¤×Í à¹×Íé ᷌´ÒŒ ¹¡ÒþѲ¹ÒÊÁͧ ʵԻ­ ˜ ­Ò áÅÐÍÒÃÁ³ ·ÕÊè ÒÁÒöÊÑÁ¼ÑÊ䴌Í‹ҧ໚¹ÃÙ»¸ÃÃÁ ªÑÂÇѲ¹ ÅÔÁé ¾Ã¨ÔµÃÇÔäÅ


4 làÃÕ¹ÃÙá Œ ÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ

MCS-51

ÊÒúѭ º··Õè 1 á¹Ð¹íÒªÔ¹é ʋǹ/ÍØ»¡Ã³ã¹¡ÒÃÊÌҧËع‹ ¹µáÅÐ «Í¿µáÇÏÊÒí ËÃѺ¾Ñ²¹Òâ»Ãá¡ÃÁ .........................................................................7 1.1 ªÔ¹é ʋǹ¢Í§Ëع‹ ¹µ Robo-51 1.2 à¤Ã×Íè §Á×Í·Õãè ªŒã¹¡ÒÃÊÌҧËع‹ ¹µ 1.3 «Í¿µáÇÏÊÒí ËÃѺ¾Ñ²¹Òâ»Ãá¡ÃÁ¤Çº¤ØÁËع‹ ¹µ

º··Õè 2 á¹Ð¹íÒáÅÐ㪌§Ò¹ Flip «Í¿µÊÒí ËÃѺâ»Ãá¡ÃÁ¢ŒÍÁÙÅŧ㹠äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ MCS-51 ¢Í§ Atmel ã¹áºº ISP .........................................11 2.1 ¡ÒõԴµÑ§é â»Ãá¡ÃÁ 2.2 ¡ÒÃ㪌§Ò¹â»Ãá¡ÃÁ Flip

º··Õè 3 ÊÌҧËع‹ ¹µ Robo-51 ..........................................................................................19 ¡Ô¨¡ÃÃÁ·Õè 1 : ÊÌҧËع‹ ¹µ Robo-51 ¡Ô¨¡ÃÃÁ·Õè 2 : ÃÙ¨Œ ¡Ñ ¡Ñºá¼§Ç§¨Ã RBX-51AC2 plus áÅСÒÃàµÃÕÂÁ«Í¿µáÇÏÊÒí ËÃѺ ¾Ñ²¹Òâ»Ãá¡ÃÁÀÒÉÒ C 3.1 à¢Õ¹â»Ãá¡ÃÁà¾×Íè áÊ´§¼Å¡Ò÷íÒ§Ò¹¢Í§ LED ¡Ô¨¡ÃÃÁ·Õè 3 : ¢Ñº LED Í‹ҧ§‹Ò 3.2 ¡ÒáíÒà¹Ô´ÊÑ­­Ò³àÊÕ§ ¡Ô¨¡ÃÃÁ·Õè 4 : ¢ÑºÊÑ­­Ò³àÊÕ§ÍÍ¡·Ò§ÅíÒ⾧ໂÂâ« 3.3 ¡ÒÃáÊ´§¼Å´ŒÇÂâÁ´ÙÅ LCD ¡Ô¨¡ÃÃÁ·Õè 5 : áÊ´§¼Å¼‹Ò¹âÁ´ÙÅ LCD 3.4 µÔ´µ‹Í¡ÑºË¹‹Ç¤ÇÒÁ¨íÒ¢ŒÍÁÙÅÍÕÍ¾Õ ÃÍÁ ¡Ô¨¡ÃÃÁ·Õè 6 : à¢Õ¹áÅÐ͋ҹ¢ŒÍÁÙÅã¹Ë¹‹Ç¤ÇÒÁ¨íÒ¢ŒÍÁÙÅÍÕÍ¾Õ ÃÍÁÀÒÂã¹ T89C51AC2 3.5 ¢ÑºÁÍàµÍÏ俵ç ¡Ô¨¡ÃÃÁ·Õè 7 : ·´Êͺ¡ÒâѺÁÍàµÍÏàº×Íé §µŒ¹

º··Õè 4 ¢Ñºà¤Å×Íè ¹Ëع‹ ¹µ Robo-51 .................................................................................75 4.1 ä¿ÅäźÃÒÃÕËÅÑ¡·Õãè ªŒã¹¡ÒâѺà¤Å×Íè ¹Ëع‹ ¹µ Robo-51 4.2 á¹Ç·Ò§¡ÒâѺà¤Å×Íè ¹Ëع‹ ¹µ Robo-51 ¡Ô¨¡ÃÃÁ·Õè 8 : ·´Êͺ¢Ñºà¤Å×Íè ¹Ëع‹ ¹µ Robo-51 àº×Íé §µŒ¹


àÃÕ¹ÃÙጠÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ MCS-51 l 5

ÊÒúѭ º··Õè 5 ¡ÒõԴµ‹Í¡ÑºµÑǵÃǨ¨Ñºáººµ‹Ò§æ ¢Í§Ëع‹ ¹µ Robo-51 ....................................81 5.1 µÑǵÃǨ¨ÑºÊÑ­­Ò³´Ô¨µÔ ÍŨҡἧǧ¨ÃÊÇÔµª ¡Ô¨¡ÃÃÁ·Õè 9 : ÃѺ¤‹ÒÊÑ­­Ò³´Ô¨µÔ ÍŨҡἧǧ¨ÃÊÇÔµª¢Í§Ëع‹ ¹µ Robo-51 ¡Ô¨¡ÃÃÁ·Õè 10 : ÃѺ¤‹ÒÊÑ­­Ò³´Ô¨µÔ ÍŨҡἧǧ¨ÃÊÇÔµªâ´ÂãªŒÍ¹Ô àµÍÃÃ»Ñ µ 5.2 ¡ÒÃ͋ҹ¤‹ÒÊÑ­­Ò³ÍйÒÅÍ¡¢Í§Ëع‹ ¹µ Robo-51 ¡Ô¨¡ÃÃÁ·Õè 11 : ·´Êͺ¡ÒÃÃѺÊÑ­­Ò³ÍйÒÅÍ¡¢Í§Ëع‹ ¹µ Robo-51

º··Õè 6 Ëع‹ ¹µ Robo-51 ¡Ñº¡ÒõÃǨ¨ÑºÇѵ¶ØẺäÁ‹ÊÁÑ ¼ÑÊ´ŒÇÂáʧÍÔ¹¿ÃÒàô..............93 6.1 ¤Ø³ÊÁºÑµ¢Ô ͧ GP2D120 âÁ´ÙÅÇÑ´ÃÐÂзҧ´ŒÇÂáʧÍÔ¹¿ÃÒàô 6.2 ËÅÑ¡¡Ò÷íÒ§Ò¹¢Í§âÁ´ÙŵÃǨÇÑ´ÃÐÂзҧ´ŒÇÂáʧÍÔ¹¿ÃÒàô ¡Ô¨¡ÃÃÁ·Õè 12 : ·´Êͺ¡ÒÃ͋ҹ¤‹Ò¨Ò¡âÁ´ÙÅ GP2D120 ¡Ô¨¡ÃÃÁ·Õè 13 : Ëع‹ ¹µ Robo-51 ËźËÅÕ¡ÊÔ§è ¡Õ´¢ÇҧẺäÁ‹ÊÁÑ ¼ÑÊ

º··Õè 7 Ëع‹ ¹µ Robo-51 à¤Å×Íè ¹·Õµè ÒÁàʌ¹ .......................................................................101 7.1 ËÑÇã¨ÊíҤѭ¤×Í á¼§Ç§¨ÃµÃǨ¨Ñº¡ÒÃÊзŒÍ¹áʧÍÔ¹¿ÃÒàô 7.2 ¡Ò÷´Êͺ͋ҹ¤‹Ò¨Ò¡á¼§Ç§¨ÃµÃǨ¨Ñº¡ÒÃÊзŒÍ¹áʧÍÔ¹¿ÃÒàô ¡Ô¨¡ÃÃÁ·Õè 14 : ͋ҹ¤‹Ò¨Ò¡á¼§Ç§¨ÃµÃǨ¨Ñº¡ÒÃÊзŒÍ¹áʧÍÔ¹¿ÃÒàô 7.3 ¡ÒõÃǨ¨Ñºàʌ¹¢Í§Ëع‹ ¹µ Robo-51 ¡Ô¨¡ÃÃÁ·Õè 15 : Ëع‹ ¹µ Robo-51 ¤Œ¹ËÒàʌ¹ ¡Ô¨¡ÃÃÁ·Õè 16 : Ëع‹ ¹µ Robo-51 ping-pong 7.4 ¡ÒÃà¤Å×Íè ¹·Õµè ÒÁàʌ¹¢Í§Ëع‹ ¹µ Robo-51 ¡Ô¨¡ÃÃÁ·Õè 17 : ¢Ñºà¤Å×Íè ¹Ëع‹ ¹µ Robo-51 仵ÒÁàʌ¹

º··Õè 8 ¤Çº¤ØÁËع‹ ¹µ Robo-51 ´ŒÇÂÃÕâÁµ¤Í¹â·ÃÅÍÔ¹¿ÃÒàô....................................119 8.1 ER-4 ÃÕâÁµ¤Í¹â·ÃÅÍÔ¹¿ÃÒàô 4 ª‹Í§ 8.2 ἧǧ¨ÃâÁ´ÙÅÃѺáʧÍÔ¹¿ÃÒàô 38kHz 8.3 ÊÌҧäźÃÒÃÕä¿ÅÊÒí ËÃѺÃѺ¢ŒÍÁÙŨҡÃÕâÁµ¤Í¹â·ÃÅ ER-4 ¡Ô¨¡ÃÃÁ·Õè 18 :·´Êͺ¡ÒÃÃѺ¢ŒÍÁÙŨҡÃÕâÁµ¤Í¹â·ÃÅ ER-4 ¡Ô¨¡ÃÃÁ·Õè 19 : ¤Çº¤ØÁËØè¹Â¹µì Robo-51 ´ŒÇÂÃÕâÁµ¤Í¹â·ÃÅ


6 làÃÕ¹ÃÙá Œ ÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ

MCS-51


àÃÕ¹ÃÙጠÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ MCS-51 l 7

º··Õè 1 á¹Ð¹íÒªÔ¹é ʋǹ/ÍØ»¡Ã³ã¹¡ÒÃÊÌҧËع‹ ¹µ áÅЫͿµáÇÏÊÒí ËÃѺ¾Ñ²¹Òâ»Ãá¡ÃÁ 㹺·¹Õ¨é йíÒàʹͶ֧¢ŒÍÁÙÅã¹àº×Íé §µŒ¹¢Í§ªÔ¹é ʋǹµ‹Ò§æ µÅÍ´¨¹à¤Ã×Íè §Á×Í·Õ¹è Òí ÁÒ㪌㹡Òà ÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¢Ô ¹Ò´àÅç¡·Õ¤è Ǻ¤ØÁ¡Ò÷íÒ§Ò¹´ŒÇÂäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ MCS-51 «Ö§è µÑ§é ª×Íè Ç‹Ò Robo-51 â´Âẋ§Í͡໚¹ 3 ʋǹ¤×Í 1. ªÔ¹é ʋǹ¢Í§Ëع‹ ¹µ Robo-51 2. à¤Ã×Íè §Á×Í·Õãè ªŒã¹¡ÒÃÊÌҧËع‹ ¹µ 3. á¹Ð¹íÒ¡ÒÃ㪌§Ò¹«Í¿µáÇÏ·Õè㪌㹡ÒþѲ¹Òâ»Ãá¡ÃÁ¤Çº¤ØÁ¡Ò÷íÒ§Ò¹¢Í§Ë؋¹Â¹µ

1.1 ªÔ¹é ʋǹ¢Í§Ëع‹ ¹µ Robo-51 ÁÕ·§Ñé ÊÔ¹é 3 ¡ÅØÁ‹ ËÅÑ¡ ´Ñ§¹Õé

1.1.1 ªÔ¹é ʋǹ·Ò§¡Å

ªØ´à¿„ͧ¢Ñº¾ÃŒÍÁÁÍàµÍÏ

ªØ´»ÃСͺŌÍÊÒ¾ҹ ªØ´»ÃСͺ°Ò¹ »ÃСͺ´ŒÇ Ἃ ¹ °Ò¹áÅЪÔé ¹ µ‹ Í ¾ÅÒÊµÔ ¡ ẺÁØÁ©Ò¡, á¹ÇµÃ§, ÁØÁ»‡Ò¹

ªØ´¹Íµ-Ê¡ÃÙ, àÊÒÃͧ, ¢ŒÍµ‹Í áÅÐ᡹âÅËТ¹Ò´µ‹Ò§æ


8làÃÕ¹ÃÙá Œ ÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ MCS-51

1.1.2 ªÔ¹é ʋǹ·Ò§ÍÔàÅç¡·Ã͹ԡʏ

ἧǧ¨ÃÊÇÔµª âÁ´ÙÅÇÑ´ÃÐÂзҧ RBX-51AC2 ἧǧ¨Ã¤Çº¤ØÁËÅÑ¡

âÁ´ÙÅÃѺáʧÍÔ¹¿ÃÒàô 38kHz

ÃÕâÁµ¤Í¹â·ÃÅÍÔ¹¿ÃÒàô

ἧǧ¨ÃµÃǨ¨Ñº áʧÊзŒÍ¹ÍÔ¹¿ÃÒàô

1.1.3 ÍØ»¡Ã³àÊÃÔÁ (¨íÒ˹‹ÒÂᡨҡªØ´Áҵðҹ)

à«ÍÏâÇÁÍàµÍϾÌÍÁŌÍáÅÐÂÒ§

ªØ´»ÃСͺŌÍÍÔÊÃРẵàµÍÃÕè AA (ball caster) SRF-05 ἧǧ¨ÃÇÑ´ÃÐÂзҧ 4 ¡ŒÍ¹ ´ŒÇ¤Å×¹è ÍÑŵÌÒ⫹ԡ

1.2 à¤Ã×Íè §Á×Í·Õãè ªŒã¹¡ÒÃÊÌҧËع‹ ¹µ

¤ÕÁµÑ´»Í¡ÊÒÂä¿

ÁÕ´¤ÑµàµÍÏ

䢤ǧ»ÅÒÂá©¡

¤ÍÁ¾ÔÇàµÍÏ µÔ´µÑ§é Ãкº»¯ÔºµÑ ¡Ô ÒÃÇÔ¹â´Çʏ 98SE ¢Öé¹ä» ÁÕ¾Íϵ͹ءÃÁNjҧ 1 ¾Íϵ ËÃ×͵ŒÍ§ãªŒµÇÑ á»Å§ÊÑ­­Ò³¾Íϵ USB ໚¹ RS-232 㹡óÕÁàÕ ©¾ÒÐ ¾Íϵ USB


àÃÕ¹ÃÙጠÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ MCS-51 l 9

1.3 «Í¿µáÇÏÊÒí ËÃѺ¾Ñ²¹Òâ»Ãá¡ÃÁ¤Çº¤ØÁËع‹ ¹µ ã¹Ë؋¹Â¹µ Robo-51 ·Õè¨Ð·íÒ¡Ò÷´ÅͧÊÌҧã¹Ë¹Ñ§Ê×ÍàŋÁ¹Õé¤Çº¤ØÁ¡Ò÷íÒ§Ò¹´ŒÇÂäÁâ¤Ã ¤Í¹â·ÃÅàÅÍÏ MCS-51 ´Ñ§¹Ñ¹é ¨Ö§ÊÒÁÒöà¢Õ¹â»Ãá¡ÃÁ¤Çº¤ØÁ䴌´ÇŒ Ââ»Ãá¡ÃÁÀÒÉÒáÍÊà«ÁºÅÕ ÀÒÉÒàºÊÔ¡â´Â㪌«Í¿µáÇÏàºÊÔ¡¤ÍÁä¾àÅÍϪNj  áÅÐÀÒÉÒ C «Ö§è ¡çµÍŒ §¾Ö§è C ¤ÍÁä¾àÅÍÏઋ¹¡Ñ¹ ÊíÒËÃѺã¹Ë؋¹Â¹µ Robo-51 ·Õèá¹Ð¹íÒãˌÊÌҧã¹Ë¹Ñ§Ê×ÍàŋÁ¹Õé¹Ñ鹨Ð㪌â»Ãá¡ÃÁÀÒÉÒ C ໚¹ËÅÑ¡ â´Â«Í¿µáÇÏ·ãÕè ªŒÁÕ 2 ªØ´¤×Í ªØ´«Í¿µáÇÏ Rkit-51 «Ö§è ໚¹«Í¿µáÇÏ C ¤ÍÁä¾àÅÍÏ·ÁÕè Ò ¾ÃŒÍÁ¡Ñºâ»Ãá¡ÃÁª‹Ç¾Ѳ¹Òã¹ÃٻẺ IDE à¾×Íè ÊÌҧä¿Å¹ÒÁÊ¡ØÅ .hex ÊíÒËÃѺà¢Õ¹ŧã¹Ë¹‹Ç ¤ÇÒÁ¨íÒâ»Ãá¡ÃÁ¢Í§äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏã¹Ëع‹ ¹µ Robo-51 áÅЫͿµáÇÏ Flip «Ö§è 㪌㹡Òà ´ÒǹâËÅ´â»Ãá¡ÃÁŧÊÙä‹ Áâ¤Ã¤Í¹â·ÃÅàÅÍÏ T89C51AC2 Íѹ໚¹ÍØ»¡Ã³ËÅÑ¡·Õè㪌㹡ÒäǺ ¤ØÁ¡Ò÷íÒ§Ò¹¢Í§Ëع‹ ¹µ Robo-51 ÊíÒËÃѺ Rkit-51 ·Õ¹è Òí ÁÒ㪌¹¹Ñé ໚¹¼Å§Ò¹¢Í§ Raisonance ¨Ò¡»ÃÐà·È½Ãѧè àÈÊ ·Õ¹è Òí ÁÒ㪌໚¹ Ãع‹ ·´Åͧ㪌§Ò¹ËÃ×Í evaluation version «Ö§è Áբ͌ ¨íÒ¡Ñ´·Õ¢è ¹Ò´¢Í§ä¿Å .hex ËÅѧ¡ÒäÍÁä¾ÅáÅŒÇ ÊÙ§ÊØ´äÁ‹à¡Ô¹ 4 ¡ÔâÅ亵 «Ö§è à¾Õ§¾Íµ‹Í¡ÒùíÒãªŒÈ¡Ö ÉÒáÅФǺ¤ØÁ¡Ò÷íÒ§Ò¹¢Í§Ëع‹ ¹µ Robo-51 䴌Í‹ҧ¤Ãº¶ŒÇ¹Ãͺ´ŒÒ¹ ÊíÒËÃѺ¼Ù·Œ ÊÕè ¹ã¨ Rkit-51 ã¹Ãع‹ ÊÁºÙóÊÒÁÒö´ÙÃÒÂÅÐàÍÕ´à¾ÔÁè àµÔÁáÅÐ µÔ´µ‹Í䴌·Õè www.raisonance.com ÃÇÁ件֧¡ÒôÒǹâËÅ´Ã؋¹·´Åͧ㪌§Ò¹¹Õ鴌ÇÂ

ÃÙ»·Õè 1-1 ˹ŒÒµ‹Ò§ËÅÑ¡¢Í§â»Ãá¡ÃÁ RIDE «Ö§è ໚¹Ê‹Ç¹Ë¹Ö§è ¢Í§ªØ´«Í¿µáÇÏ Rkit-51 ·Õãè ªŒã‹ ¹¡ÒþѲ¹Ò â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺËع‹ ¹µ Robo-51


10làÃÕ¹ÃÙá Œ ÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ MCS-51

ÃÙ»·Õè 1-2 ˹ŒÒµ‹Ò§ËÅÑ¡¢Í§â»Ãá¡ÃÁ Flip «Ö§è 㪌´ÒǹâËÅ´â»Ãá¡Ã¤Çº¤ØÁŧã¹äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ T89C51AC2 Íѹ໚¹äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏËÅÑ¡¢Í§Ëع‹ ¹µ Robo-51 ¡ÒÃ㪌§Ò¹ªØ´«Í¿µáÇÏ Rkit-51 ÃÇÁ¶Ö§¡ÒÃà¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C àº×Íé §µŒ¹¹Ñ¹é äÁ‹ä´ŒÃÇÁ änjã¹Ë¹Ñ§Ê×ÍàŋÁ¹Õé ÊÒÁÒö͋ҹà¾ÔèÁàµÔÁ䴌¨Ò¡Ë¹Ñ§Ê×Í “¤Ù‹Á×Í¡ÒÃà¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C àº×éͧµŒ¹ ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ MCS-51” «Ö§è ¨Ñ´ÁҾÌÍÁ¡ÑºªØ´Ëع‹ ¹µ Robo-51 ¹Õáé ÅŒÇ Ê‹Ç¹«Í¿µáÇÏ Flip ໚¹«Í¿µáÇÏ·Õè㪌â»Ãá¡ÃÁäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏã¹áºº ISP (InSystem Programming) ¢Í§ Atmel «Ö§è ໚¹¼Ù¼Œ ÅÔµäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ T89C51AC2 ÊÒÁÒö´Òǹ âËÅ´àÇÍϪ¹Ñè ŋÒÊØ´·ÕÍè Ò¨ÁÕ䴌·Õè www.atmel.com ÊíÒËÃѺ¡ÒÃ㪌§Ò¹«Í¿µáÇÏ Flip ¹Ñ¹é ÁÕÃÒÂÅÐàÍÕ´ à¾ÔÁè àµÔÁ㹺··Õè 2


àÃÕ¹ÃÙጠÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ MCS-51 l 11

º··Õè 2 á¹Ð¹íÒáÅÐ㪌§Ò¹ Flip «Í¿µÊÒí ËÃѺâ»Ãá¡ÃÁ ¢ŒÍÁÙÅŧã¹äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ MCS-51 ¢Í§ Atmel ã¹áºº ISP (In-System Programming) 㹪شË؋¹Â¹µ Robo-51 㪌äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ T89C51AC2 㹡ÒäǺ¤ØÁ¡Ò÷íÒ§Ò¹ ´Ñ§¹Ñé¹ã¹¡ÒôÒǹâËÅ´â»Ãá¡ÃÁ¤Çº¤ØÁŧã¹Ë¹‹Ç¤ÇÒÁ¨íÒâ»Ãá¡ÃÁ¨Ö§µŒÍ§ãªŒ«Í¿µáÇÏ੾ÒÐ «Ö§è ã¹·Õ¹è ¤Õé Í× Flip «Ö§è ¾Ñ²¹Ò¢Ö¹é â´Â Atmel ¼Ù¼Œ ÅÔµäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ MCS-51 àºÍÏ T89C51AC2 â´Â Flip ÊÒÁÒöâ»Ãá¡ÃÁäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ MCS-51 ¢Í§ Atmel ã¹ÅѡɳРISP ËÃ×Í In-System Programming ËÃ×ÍẺâ»Ãá¡ÃÁã¹Ç§¨Ã䴌ËÅÒÂàºÍÏ ÊÒÁÒö´ÒǹâËŴ䴌·Õè www.atmel.com ÊíÒËÃѺàÇÍϪ¹Ñè ·ÕÊè ÒÁÒöâ»Ãá¡ÃÁ T89C51AC2 䴌¹¹Ñé µŒÍ§à»š¹ Flip V1.8 ¢Ö¹é ä» Flip ·íÒ˹ŒÒ·Õè´ÒǹâËÅ´ä¿Å¹ÒÁÊ¡ØÅ *.hex ŧã¹Ë¹‹Ç¤ÇÒÁ¨íÒâ»Ãá¡ÃÁ¢Í§äÁâ¤Ã ¤Í¹â·ÃÅàÅÍÏ T89C51AC2 ·Õµè ´Ô µÑ§é ÍÂÙº‹ ¹á¼§Ç§¨Ã RBX-51AC2 «Ö§è ໚¹á¼§Ç§¨Ã¤Çº¤ØÁËÅÑ¡¢Í§ Ëع‹ ¹µ Robo-51

2.1 ¡ÒõԴµÑ§é â»Ãá¡ÃÁ 1. ¨Ñ´ËÒä¿Åµ´Ô µÑ§é ¢Í§ Flip «Ö§è ÁÕÍÂÙã¹á¼‹¹«Õ´ÃÕ ÍÁ·Õ¨è ´Ñ ÁҾÌÍÁ¡ÑºªØ´Ëع‹ ¹µ Robo-51 ËÃ×Í ´ÒǹâËÅ´ÁÒ¨Ò¡ www.atmel.com (µŒÍ§àÅ×Í¡´ÒǹâËÅ´àÇÍϪ¹Ñè 2.2.4) 2. ´ÑºàºÔÅé ¤ÅÔ¡·Õäè ¿Å setup ã¹ä´àÃ硵ÍÃÕ·è àÕè ¡çºâ»Ãá¡ÃÁ Flip ¨Ð»ÃÒ¡¯Ë¹ŒÒµ‹Ò§¢Í§¡ÒõԴ µÑ§é ãˌµÍºÃѺä»Í‹ҧµ‹Íà¹×Íè § ¨¹¡ÃÐ·Ñ§è ¡ÒõԴµÑ§é àÊÃç¨ÊÔ¹é ŧ 3. ËÅѧ¨Ò¡·Õ·è Òí ¡ÒõԴµÑ§é â»Ãá¡ÃÁ Flip àÃÕºÌÍÂáÅŒÇ Åͧ·´Êͺແ´µÑÇâ»Ãá¡ÃÁâ´Âä» ·Õè Programs à Atmel à Flip 2.2.4 à Flip (µÑÇàÅ¢¢Í§àÇÍϪ¹Ñ ÍÒ¨à»ÅÕÂè ¹á»Å§ä´Œ)


12làÃÕ¹ÃÙá Œ ÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ

MCS-51

1. »ØÁ† ¤íÒÊѧè àÅ×Í¡àºÍÏäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ·µÕè ͌ §¡ÒõԴµ‹Í´ŒÇ (Select device) 2. »ØÁ† ¤íÒÊѧè àÅ×Í¡ª¹Ô´¢Í§¾Íϵ㹡ÒÃàª×Íè Áµ‹Í (Set communication) 3. »ØÁ† ¤íÒÊѧè ź¢ŒÍÁÙÅÀÒÂã¹äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ (Erase device) 4. »ØÁ† ¤íÒÊÑ§è µÃǨÊͺ¾×¹é ·ÕÇè ҋ §ÀÒÂã¹äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ (Blank check device) 5. »ØÁ† ¤íÒÊÑ§è ´ÒǹâËÅ´â»Ãá¡ÃÁŧã¹äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ (Program device) 6. »ØÁ† ¤íÒÊÑ§è µÃǨÊͺ¤ÇÒÁ¶Ù¡µŒÍ§ã¹¡ÒôÒǹâËÅ´ (Verify device) 7. »ØÁ† ¤íÒÊѧè ͋ҹ¢ŒÍÁÙŨҡäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏÁҾѡänj·ËÕè ¹‹Ç¤ÇÒÁ¨íҺѿà¿ÍϏ (Read device) 8. »ØÁ† ¤íÒÊÑ§è µÃǨÊͺáÅÐᡌ䢢ŒÍÁÙÅã¹Ë¹‹Ç¤ÇÒÁ¨íҺѿà¿ÍÏ (Edit buffer) 9. »ØÁ† ¤íÒÊѧè âËÅ´ä¿Å .hex·Õµè ͌ §¡ÒôÒǹâËÅ´â»Ãá¡ÃÁŧã¹äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ (Load HEX file) 10. »ØÁ† ¤íÒÊÑ§è ºÑ¹·Ö¡ä¿Å .hex ·Õãè ªŒ§Ò¹ã¹¢³Ð¹Ñ¹é (Save HEX file)

ÃÙ»·Õè 2-1 áÊ´§ÃÒÂÅÐàÍÕ´¢Í§â»Ãá¡ÃÁ Flip (Áյ͋ )


àÃÕ¹ÃÙጠÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ MCS-51 l 13

11. »ØÁ† ¤íÒÊѧè àÃÕ¡´Ù¤Áً Í× ¡ÒÃ㪌§Ò¹·ÕÁè ҾÌÍÁ¡Ñºâ»Ãá¡ÃÁ Flip (Help) 12. ÃÒ¡ÒäíÒÊÑ§è ·Õµè ͌ §¡ÒÃãˌà¡Ô´¡ÒáÃзíÒÍ‹ҧµ‹Íà¹×Íè §â´ÂÍѵâ¹ÁÑµÔ àÅ×͡䴌ââÂ㪌àÁÒʏ¤ÅÔ¡à¤Ã×Íè §ËÁÒ ¶Ù¡Ë¹ŒÒª‹Í§¢Í§¤íÒÊÑ§è ¹Ñ¹é æ 12.1 àÅ×Í¡¤íÒÊѧè Erase à¾×Íè Êѧè ź¢ŒÍÁÙÅÀÒÂã¹äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ 12.2 àÅ×Í¡¤íÒÊѧè Blank check à¾×Íè µÃǨÊͺ¢ŒÍÁÙÅNjҧã¹Ë¹‹Ç¤ÇÒÁ¨íҢͧäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ 12.3 àÅ×Í¡¤íÒÊѧè Program à¾×Íè ´ÒǹâËÅ´ä¿Å .hex ŧã¹äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ 12.4 àÅ×Í¡¤íÒÊѧè Verify à¾×Íè µÃǨÊͺ¤ÇÒÁ¶Ù¡µŒÍ§ã¹¡ÒôÒǹâËÅ´ ¡ÅØÁ‹ ¤íÒÊÑ§è ·Ñ§é 4 ÃÒ¡ÒÃ·Õ¶è ¡Ù àÅ×Í¡¨Ð¶Ù¡¡ÃзíÒµÒÁÅíҴѺ¨Ò¡º¹Å§Å‹Ò§ä´Œ¡µç Í× àÁ×Íè ¤ÅÔ¡·Õ»è Á؆ Run áÅÐ ÊÒÁÒöÊѧè ¡àÅÔ¡·Ø¡¤íÒÊÑ§è ¨Ò¡¡ÒäÅÔ¡·Õ»è Á؆ Clear 13. ÅÒàºÅᨌ§ª×Íè ä¿Å¹ÒÁÊ¡ØÅ .hex ·Õ¶è ¡Ù àÅ×Í¡¾ÃŒÍÁ¢¹Ò´¢Í§ä¿Å 14. á¶ºÊ¶Ò¹Ð¢Ñ¹é µÍ¹ã¹¡Ò÷íÒ§Ò¹ÃÐËNjҧâ»Ãá¡ÃÁ Flip ¡ÑºäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ 15. ªèͧáÊ´§¢Õ´ÃдѺ¤ÇÒÁ¡éÒÇ˹éÒ㹡Ò÷ӧҹ¢Í§â»Ãá¡ÃÁ㹤ÓÊÑ§è µèÒ§æ 16. ÅÒàºÅáÊ´§Ê¶Ò¹ÐºÑ¿à¿ÍϢͧäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ 17. ¡ÅØÁ‹ ¢ŒÍÁÙÅáÅÐʶҹе‹Ò§æ ¢Í§äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ·¡Õè Òí ÅѧµÔ´µ‹Í´ŒÇ »ÃСͺ´ŒÇ 17.1 ª‹Í§ÊÕàè ËÅÕÂè Áº¹ÊØ´ ໚¹¡ÒÃᨌ§àºÍϢͧäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ·µÕè ´Ô µ‹Í´ŒÇ 17.2 Signature 17.3 Device

byte

໚¹ÃËÑʢͧ¼Ù¼Œ ÅÔµÃÇÁ¡ÑºàÅ¢ËÁÒ»ÃШíÒµÑǢͧäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ

Boot IDs

17.4 Hardware

໚¹àÅ¢ËÁÒ»ÃШíÒµÑǢͧºÙµâËÅ´à´ÍÏã¹äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ

byte

17.5 Bootloader

Ver.

17.6 BLJB (Bootloader Jump Bit) ໚¹ºÔµ¤Çº¤ØÁ¡ÒÃâ»Ãá¡ÃÁẺ ISP ¼‹Ò¹ºÙµâËÅ´à´ÍÏ ÊíÒËÃѺªÔ»Ãع‹ AT89C51AC2 äÁ‹µÍŒ §¤ÅÔ¡àÅ×Í¡ ᵋ¶ÒŒ ໚¹Ãع‹ T89C51AC2 µŒÍ§àÅ×Í¡·Ø¡¤Ãѧé 17.7 X2 áÊ´§ÍѵÃÒ¡ÒÃËÒäÇÒÁ¶Õè ¶ŒÒ¶Ù¡àÅ×Í¡¨Ð໚¹ 6 ÊÑ­­Ò³¹Ò́¡Òµ‹Íä«à¡ÔŠᵋ¶ÒŒ äÁ‹àÅ×Í¡¨Ð ໚¹ 12 ÊÑ­­Ò³¹Ò́¡Òµ‹Íä«à¡ÔÅ 17.8

Level 0-2

àÅ×Í¡ºÔµ»‡Í§¡Ñ¹ã¹ÃдѺµ‹Ò§æ ¢Í§¢ŒÍÁÙÅÀÒÂã¹äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ

ÃÙ»·Õè 2-1 áÊ´§ÃÒÂÅÐàÍÕ´¢Í§â»Ãá¡ÃÁ Flip (¨º)


14làÃÕ¹ÃÙá Œ ÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ

MCS-51

2.2 ¡ÒÃ㪌§Ò¹â»Ãá¡ÃÁ Flip ã¹ÃÙ»·Õè 2-1 áÊ´§Ë¹ŒÒµ‹Ò§ËÅÑ¡¢Í§â»Ãá¡ÃÁ Flip ¾ÃŒÍÁ͸ԺÒÂʋǹ»ÃСͺµ‹Ò§æ ·ÕÊè Òí ¤Ñ­

2.2.1 ¤íÒÊѧè ÀÒÂã¹àÁ¹ÙµÒ‹ §æ·Õãè ªŒ§Ò¹º‹Í àÁ¹Ù File l Load Hex file àÅ×Í¡ä¿Å .hex ·Õµ è ͌ §¡ÒôÒǹâËŴŧã¹äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ l

Save Hex file ºÑ¹·Ö¡ä¿Å .hex·Õ¶è ¡Ù âËÅ´ã¹¢³Ð¹Ñ¹é

àÁ¹Ù Buffer l Edit µÃǨÊͺáÅÐᡌ䢢ŒÍÁÙÅ·Õµ è Òí á˹‹§Ë¹‹Ç¤ÇÒÁ¨íÒ l Option ¡íÒ˹´ÃÒÂÅÐàÍÕ´à¡ÕÂè ǡѺºÑ¿à¿ÍÏ àÁ¹Ù Device l Select àÅ×Í¡àºÍÏäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ·µ Õè ͌ §¡ÒõԴµ‹Í´ŒÇ l Erase ź¢ŒÍÁÙÅÀÒÂã¹µÑÇäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ l Blank check µÃǨÊͺ¾×¹ é ·ÕÇè ҋ §ÀÒÂã¹µÑÇäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ l Read ͋ҹ¢ŒÍÁÙŨҡäÁâ¤Ã¤ÍÃâ·ÃÅàÅÍÏÁҾѡänj·Ë Õè ¹‹Ç¤ÇÒÁ¨íÒ l Program ´ÒǹâËÅ´â»Ãá¡ÃÁŧ㹵ÑÇäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ l Verify µÃǨÊͺ¤ÇÒÁ¶Ù¡µŒÍ§ã¹¡ÒôÒǹâËÅ´ àÁ¹Ù Settings l Communication àÅ×Í¡ª¹Ô´¢Í§¾Íϵ㹡ÒÃàª×Í è Áµ‹Í l Command Window áÊ´§Ë¹ŒÒµ‹Ò§¼ÅÅѾ¸¡Ò÷íÒ¤íÒÊÑ§è µ‹Ò§æ·Õ¼è ҋ ¹ÁÒ àÁ¹Ù Help

àÃÕ¡´Ù¤Áً Í× ¡ÒÃ㪌§Ò¹·ÕÁè ҾÌÍÁ¡Ñºâ»Ãá¡ÃÁ Flip

2.2.2 ¡ÒÃ㪌«Í¿µáÇÏ Flip ´ÒǹâËÅ´â»Ãá¡ÃÁŧã¹äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ 1. ¨‹ÒÂä¿àÅÕé§áÅÐàª×èÍÁµ‹ÍÊÒ´ÒǹâËŴࢌҡѺἧǧ¨Ã·ÕèµÔ´µÑé§äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ T89C51AC2 ã¹·Õ¹è ¤Õé Í× á¼§Ç§¨Ã RBX-51AC2 2. àÅ×Í¡âËÁ´¡Ò÷íÒ§Ò¹¢Í§á¼§Ç§¨ÃãˌÍÂÙ㋠¹âËÁ´â»Ãá¡ÃÁ â´Â¡´ÊÇÔµª MODE ãËŒä¿ ÊÕá´§µíÒá˹‹§ PGM µÔ´


àÃÕ¹ÃÙጠÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ MCS-51 l 15

3. ແ´â»Ãá¡ÃÁ Flip â´Â¡ÒôѺàºÔéŤÅÔ¡·Õèäͤ͹¢Í§â»Ãá¡ÃÁ ¨Ð»ÃÒ¡¯âÅ⡌¢Í§ â»Ãá¡ÃÁ´Ñ§ÃÙ» áŌÇࢌÒÊÙˋ ¹ŒÒµ‹Ò§¢Í§â»Ãá¡ÃÁ

4. ·ÕËè ¹ŒÒµ‹Ò§â»Ãá¡ÃÁ Flip ¤ÅÔ¡·Õäè ͤ͹ Select device ËÃ×Íä»·Õàè Á¹Ù Device à Select ËÃ×Í ¡´»ØÁ† F2 ·Õ¤è ÂÕ º ÍÏ´ à¾×Íè àÅ×Í¡àºÍÏäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ·µÕè ͌ §¡ÒõԴµ‹Í´ŒÇ ´Ñ§ÃÙ»


16làÃÕ¹ÃÙá Œ ÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ

MCS-51

5. ¤ÅÔ¡»ØÁ† ¤íÒÊѧè Set communication ËÃ×Íä»·Õàè Á¹Ù Settings à communication ËÃ×Í¡´»ØÁ† F3 »ØÁ† 㪌§Ò¹·Õàè ¡ÕÂè Ç¢ŒÍ§¨Ð»ÃÒ¡¯ËÃ×ÍáÍ¡µÕ¿¢Ö¹é à¾×Íè ãˌÊÒÁÒö㪌§Ò¹ä´Œ ãˌ¤ÅÔ¡áŌÇàÅ×Í¡ËÑÇ¢ŒÍ RS232 à¾×Íè àÅ×Í¡¡ÒõԴµ‹ÍẺ¾Íϵ͹ءÃÁµÒÁÃÙ»

6. ¨Ð»ÃÒ¡¯Ë¹ŒÒµ‹Ò§ãËÁ‹ªÍ×è RS-232 ãˌ¡´»ØÁ† Connect à¾×Íè àª×Íè Áµ‹ÍÃÐËNjҧâ»Ãá¡ÃÁ Flip ¡ÑººÍÏ´ RBX-51AC2 ¶ŒÒàª×Íè Áµ‹Í¶Ù¡µŒÍ§ ˹ŒÒµ‹Ò§â»Ãá¡ÃÁ´ŒÒ¹¢ÇÒ¨ÐáÍ¡µÕ¿ ËÃ×Íà¡Ô´¡Ò÷íÒ§Ò¹ ¢Ö¹é ´Ñ§ÃÙ» ËÁÒ¤ÇÒÁNjҢ³Ð¹Õé T89C51AC2 ¾ÃŒÍÁÊíÒËÃѺ¡ÒôÒǹâËÅ´â»Ãá¡ÃÁáŌÇ

˹ŒÒµ‹Ò§´ŒÒ¹¢ÇÒáÍ¡µÕ¿¢Ö¹é àÁ×Íè ¡ÒÃàª×Íè Áµ‹Í¶Ù¡µŒÍ§


àÃÕ¹ÃÙጠÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ MCS-51 l 17

8. ¡´»ØÁ† Load Hex File

¨Ð»ÃÒ¡¯Ë¹ŒÒµ‹Ò§ãˌàÅ×Í¡ä¿Å¹ÒÁÊ¡ØÅ .hex ·Õµè ͌ §¡ÒÃ

8. ·Õè˹ŒÒµ‹Ò§´ŒÒ¹«ŒÒÂ㹪‹Í§ Operation Flow ãˌàÅ×Í¡¤íÒÊÑè§àÃÕ§ ÅíҴѺ¨Ò¡º¹Å§Å‹Ò§¤×Í Erase à Balnk Check à Program à Verify ¨Ò¡¹Ñ¹é ¡´»ØÁ† Run â»Ãá¡ÃÁ¨Ð·íÒ¡ÒÃź¢ŒÍÁÙÅã¹Ë¹‹Ç¤ÇÒÁ¨íÒ µ‹Í´ŒÇ µÃǨÊͺ¢ŒÍÁÙÅNjҧ, â»Ãá¡ÃÁ¢ŒÍÁÙÅŧã¹Ë¹‹Ç¤ÇÒÁ¨íÒâ»Ãá¡ÃÁ áÅÐ µÃǨÊͺ¢ŒÍÁÙÅã¹Ñ¢¹Œ ÊØ´·ŒÒ ·Ñ§é ËÁ´¹Õ¨é дíÒà¹Ô¹¡ÒÃÍ‹ҧµ‹Íà¹×Íè §ä»ã¹ ¤ÃÒÇà´ÕÂÇ à¾Õ§෋ҹÕéäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ¡ç¨Ð䴌ÃѺ¡ÒÃâ»Ãá¡ÃÁ¢ŒÍÁÙÅ Í‹ҧÊÁºÙó ÊÒÁÒö¹íÒä»·´Êͺ¡Ò÷íҧҹ䴌µÍ‹ ä» ¹Í¡¨Ò¡¨Ð㪌 Operation Flow áÅŒÇ ¼Ù㌠ªŒ§Ò¹ÊÒÁÒöàÅ×Í¡¡ÃзíÒ ¤íÒÊÑ§è ¡ÒÃź, µÃǨÊͺ¢ŒÍÁÙÅNjҧ, â»Ãá¡ÃÁ áÅеÃǨÊͺ¢ŒÍÁÙÅ䴌¨Ò¡ àÁ¹Ù¤Òí ÊÑ§è µÒÁ»¡µÔ䴌 â´ÂࢌÒä»·Õàè Á¹Ù Device 㹺·µ‹Í仨ҡ¹Õ¨é Ð䴌¡Å‹ÒǶ֧µÑÇÍ‹ҧ¡ÒþѲ¹Òâ»Ãá¡ÃÁÀÒÉÒ C à¾×Íè ¤Çº¤ØÁáÅÐÊÑ§è ¡Òà ÍØ»¡Ã³µÒ‹ §æ º¹á¼§Ç§¨Ã RBX-51AC2 áŌǹíÒÁÒÊÌҧ໚¹¿˜§¡ª¹Ñè ¢Í§â»Ãá¡ÃÁÀÒÉÒ C ¨Ñ´à¡çºã¹ ÃÙ»¢Í§äźÃÒÃÕà¾×èͤÇÒÁÊдǡ㹡ÒÃàÃÕ¡㪌 ¼¹Ç¡ÃÇÁࢌÒ໚¹â»Ãá¡ÃÁ¤Çº¤ØÁ¡Ò÷íÒ§Ò¹¢Í§ Ëع‹ ¹µ Robo-51 à¾×Íè ãˌÊÒÁÒö·íҧҹ䴌ã¹ÃٻẺµ‹Ò§æ µÑ§é ᵋ¢ºÑ à¤Å×Íè ¹¸ÃÃÁ´Ò ¨¹¶Ö§¢Ñºà¤Å×Íè ¹ Í‹ҧÁÕà§×è͹䢨ҡµÑǵÃǨ¨ÑºÊÑ­­Ò³áººµ‹Ò§æ


18làÃÕ¹ÃÙá Œ ÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ

MCS-51


àÃÕ¹ÃÙጠÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ MCS-51

l 19

º··Õè 3 ÊÌҧË؋¹Â¹µ Robo-51 º·¹Õàé »š¹¡ÒùíÒàʹ͡ÒÃÊÌҧËع‹ ¹µ Robo-51 Íѹ໚¹Ëع‹ ¹µÍµÑ â¹Áѵ¢Ô ¹Ò´àÅç¡·Õ¤è Ǻ¤ØÁ´ŒÇ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ MCS-51 àºÍÏ T89C51AC2 â´Â¨Ðẋ§Í͡໚¹ 2 ʋǹ¤×Í á¹Ð¹íÒ¡ÒûÃСͺ µÑÇË؋¹Â¹µã¹Ê‹Ç¹áá áÅСÒÃà¢Õ¹â»Ãá¡ÃÁà¾×èÍ·´Êͺ¡Ò÷íÒ§Ò¹¾×é¹°Ò¹¢Í§á¼§Ç§¨Ã¤Çº¤ØÁ Ëع‹ ¹µ Robo-51 㹪‹Ç§·ÕÊè ͧ «Ö§è 㹡ÒÃà¢Õ¹â»Ãá¡ÃÁ¹Ñ¹é ¨Ð㪌ÀÒÉÒ C áÅÐÁÕ¡ÒÃÊÌҧ¿˜§¡ª¹Ñè ÃÇÁ¶Ö§ ä¿ÅäźÃÒÃÕà¾×Íè ãˌÊÒÁÒö¹íÒä»ãªŒã¹¡ÒâѺà¤Å×Íè ¹Ëع‹ ¹µã¹º·µ‹Íæ ä»ä´ŒÊдǡ¢Ö¹é Robo-51 MCS-51 Microcontroller mobile robot ¢Ñºà¤Å×è͹´ŒÇÂ࿄ͧ¢ÑºÁÍàµÍϤً áÅЪشŌÍÊÒ¾ҹ

l

¤Çº¤ØÁ´ŒÇÂäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ T89C51AC2 â»Ãá¡ÃÁ䴌㹵ÑÇ

l

l ÊÒÁÒöµÔ´µÑ§é ἧǧ¨ÃµÃǨ¨Ñºáʧ

ÊзŒÍ¹ÍÔ¹¿ÃÒàôà¾×è͵ÃǨ¨Ñºàʌ¹, ἧǧ¨ÃÊÇÔµªµÃǨ¨Ñº¡Òê¹, ἧ ǧ¨ÃÃѺáʧÍÔ¹¿ÃÒàôà¾×èͤǺ¤ØÁ ¨Ò¡ÃÐÂÐä¡Å, âÁ´ÙÅÇÑ´ÃÐÂзҧẺ ÍÔ¹¿ÃÒàôà¾×èÍÇÑ´ÃÐÂзҧáÅÐËź ËÅÕ¡ÊÔ觡մ¢ÇҧẺäÁ‹ÊÑÁ¼ÑÊ

l ÁÕâÁ´ÙÅ LCD ¢¹Ò´ 16 µÑÇÍÑ¡Éà 2

ºÃ÷ѴáÅÐ LED ÊíÒËÃѺáÊ´§¼Å¡Òà ·íÒ§Ò¹

l ÁÕǧ¨Ã¢Ñº RC à«ÍÏâÇÁÍàµÍÏ 3 ª‹Í§

¨Ö § ÊÒÁÒö»ÃÑ º à»ÅÕè  ¹Ãкº¢Ñ º à¤Å×Íè ¹ áÅÐÊÒÁÒö·íÒ¡ÒõԴµÑ§é ¡Åä¡ à¤Å×è͹äËÇà¾ÔèÁàµÔÁ䴌


20 làÃÕ¹ÃÙá Œ ÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ

MCS-51

¡Ô¨¡ÃÃÁ·Õè 1

ÊÌҧËع ‹ ¹µ Robo-51 Ἃ¹°Ò¹¾ÅÒʵԡ x 1 ©Ò¡Âִ᡹ŌÍÊѹé x 2

ºÍÏ´ RBX-51 x 1

©Ò¡Âִ᡹ŌÍÂÒÇ x 2

´ØÁÅŒÍ x 6 Ō͢ѺÊÒ¾ҹ x 2 ᡹¡ÅÁ x 3 ŌͻÃФͧÊÒ¾ҹãË­‹ x 2

ÊÒ¾ҹ 30 ¢ŒÍµ‹Í x 2

ÊÒ¾ҹ 10 ¢ŒÍµ‹Í x 4

Ê¡ÃÙ 3x10 ÁÁ. x 15

ªØ´à¿„ͧ¢ÑºÁÍàµÍÏ x 2

ŌͻÃФͧÊÒ¾ҹ¡ÅÒ§ x 2 àÊÒÃͧˡàËÅÕÂè Á 30 ÁÁ. x 3

Ê¡ÃÙÁÍ× ËÁع x 3

àÊÒÃͧ¾ÅÒʵԡ 3 ÁÁ. x 2 ªÔ¹é µ‹ÍÁØÁ©Ò¡ x 3

Ê¡ÃÙà¡ÅÕÂǻŋÍ 2 ÁÁ. x 2 Ê¡ÃÙ 3x15 ÁÁ. x 1

᷋§µ‹Í 3 ÃÙ x 2

¹Íµ 3 ÁÁ. x 11

ªÔ¹é µ‹ÍÁØÁ»‡Ò¹ x 1

ªÔ¹é µ‹ÍµÃ§ x 1

ἧǧ¨ÃµÃǨ¨Ñº âÁ´ÙÅ GP2D120 x 1 ἧǧ¨ÃâÁ´ÙÅÃѺáʧ áʧÊзŒÍ¹ÍÔ¹¿ÃÒàô x 2 ÍÔ¹¿ÃÒàô x 1

ÃÙ»·Õè A1-1 áÊ´§ÍØ»¡Ã³ÊÒí ËÃѺÊÌҧŌÍÊÒ¾ҹáÅÐâ¤Ã§¢Í§Ëع‹ ¹µ Robo-51


àÃÕ¹ÃÙጠÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ MCS-51

l 21

A1.1 ¹íҪش࿄ͧ¢ÑºÁÍàµÍÃÂ´Ö à¢ŒÒ¡Ñºá¼‹¹°Ò¹¾ÅÒʵԡ â´ÂÁÍàµÍϵÇÑ ¢ÇÒÁ×ÍãËŒË¹Ñ ´ŒÒ¹·ÕÁè »Õ ¡‚ ÍÍ¡ÁÒ´ŒÒ¹¹Í¡ áŌÇ㪌ʡÃÙ 3x10 ÁÁ. ¢Ñ¹ÂÖ´¨Ò¡´ŒÒ¹Å‹Ò§ã¹µíÒá˹‹§´Ñ§áÊ´§ã¹ÃÙ» â´Âànj¹ÃÙ嫅 ´ŒÒ¹ã¹¢Í§ÁÍàµÍϵÇÑ «ŒÒÂÁ×Íänj ¡‹Í¹ ànj¹äÇŒÂ§Ñ äÁ‹Â´Ö Ê¡ÃÙ ´ŒÒ¹º¹

Ê¡ÃÙ 3x10 ÁÁ.

´ŒÒ¹Å‹Ò§

Ëѹ´ŒÒ¹·ÕÁè »Õ ¡‚ ÍÍ¡´ŒÒ¹¹Í¡ ÂÖ´´ŒÇÂÊ¡ÃÙ¨Ò¡´ŒÒ¹Å‹Ò§

Ê¡ÃÙ 3x10 ÁÁ.

A1.2 ¹íÒŌ͢ѺÊÒ¾ҹËÅÑ¡ÊÇÁࢌҡѺ᡹¢Í§ªØ´à¿„ͧ¢ÑºÁÍàµÍÏ 㪌ʡÃÙà¡ÅÕÂǻŋÍ 2 ÁÁ. ¢Ñ¹ÂÖ´ ·íÒàËÁ×͹ ¡Ñ¹·Ñ§é Êͧ´ŒÒ¹

´ŒÒ¹º¹ Ō͢ѺÊÒ¾ҹ Ê¡ÃÙà¡ÅÕÂǻŋÍ 2 ÁÁ. A1.3 ¤ÇèÒí Ἃ¹°Ò¹Å§ ¹íÒ©Ò¡Âִ᡹ŌÍÂÒÇ»ÃСºà¢ŒÒ¡Ñºá¼‹¹°Ò¹ã¹µíÒá˹‹§µÒÁÃÙ» 㪌ʡÃÙ 3x10 ÁÁ. ¢Ñ¹ÂÖ´ ÂÖ´¡ÑºÃÙ·àÕè ËÅ×ͧ͢»‚¡¢Í§ªØ´à¿„ͧ¢ÑºÁÍàµÍϨҡ¢Ñ¹é µÍ¹·Õè A1.1 ¨Ò¡¹Ñ¹é 㪌ʡÃÙ 3x10 ÁÁ. áÅй͵ 3 ÁÁ. ¢Ñ¹ÂÖ´ ©Ò¡Âִ᡹ŌÍÂÒÇÍÕ¡Ë¹Ö§è µíÒá˹‹§ â´ÂÂִࢌҷÕÃè ·Ù Õè 2 ¢Í§©Ò¡Âִ᡹ŌÍÂÒÇ ´Ñ§áÊ´§µíÒá˹‹§µÒÁÃÙ» µíÒá˹‹§ÃÙÂ´Ö Ê¡ÃÙ 3x10 ÁÁ. áÅй͵ 3 ÁÁ.

´ŒÒ¹Å‹Ò§

Ê¡ÃÙ 3x10 ÁÁ. ©Ò¡Âִ᡹ŌÍÂÒÇ


22 làÃÕ¹ÃÙá Œ ÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ

MCS-51

A1.4 ¹íÒ©Ò¡Âִ᡹ŌÍÂÒÇ·Õàè ËÅ×ÍÂִࢌҡѺἋ¹°Ò¹ áŌÇ㪌ʡÃÙ 3x10 ÁÁ. ¢Ñ¹ÂÖ´¨Ò¡´ŒÒ¹º¹ áÅÐÂÖ´´ŒÒ¹Å‹Ò§ ´ŒÇ¹͵ 3 ÁÁ. 2 µíÒá˹‹§ ´Ñ§áÊ´§ã¹ÃÙ» ¹Íµ 3 ÁÁ.

¹Íµ 3 ÁÁ. ©Ò¡Âִ᡹ŌÍÂÒÇ ´ŒÒ¹Å‹Ò§

¹Íµ 3 ÁÁ. A1.5 ¾ÅԡἋ¹°Ò¹¡ÅѺÁÒ´ŒÒ¹º¹ ÂÖ´©Ò¡Âִ᡹ŌÍÊѹé ࢌҡѺ´ŒÒ¹Ë¹ŒÒ¢Í§á¼‹¹°Ò¹ã¹µíÒá˹‹§µÒÁ·Õáè Ê´§ã¹ ÃÙ» ¢Ñ¹Ê¡ÃÙÂ´Ö à©¾ÒеíÒá˹‹§´ŒÒ¹ã¹ ÃٴҌ ¹¹Í¡ànj¹änj ¹Íµ 3 ÁÁ. ànj¹änj

´ŒÒ¹º¹

´ŒÒ¹º¹

¹Íµ 3 ÁÁ.

©Ò¡Âִ᡹ŌÍÊѹé

A1.6 µÔ´µÑ§é àÊÒâÅËÐË¡àËÅÕÂè Á·Õ´è Ҍ ¹ËÅѧ¢Í§á¼‹¹°Ò¹ ¤ÇèÒí Ἃ¹°Ò¹Å§ ¢Ñ¹Ê¡ÃÙ 3x10 ÁÁ. ࢌҷÕÃè ãÙ ¹µíÒá˹‹§ÁØÁ «ŒÒ¼‹Ò¹á¼‹¹°Ò¹áÅÐªÔ¹é µ‹ÍÁØÁ©Ò¡ à¾×Íè ¢Ñ¹ÂÖ´¡ÑºàÊÒâÅËÐË¡àËÅÕÂè Á

Ê¡ÃÙ 3x10 ÁÁ.

´ŒÒ¹Å‹Ò§

ªÔ¹é µ‹ÍÁØÁ©Ò¡ àÊÒâÅËÐË¡àËÅÕÂè Á 30 ÁÁ.

Ê¡ÃÙ 3x10 ÁÁ. ¼ÅԡἋ¹°Ò¹´ŒÒ¹Å‹Ò§¢Öé¹ ªØ´à¿„ͧ¢Ñº ÁÍàµÍÏ¢ÇÒ

Ἃ¹°Ò¹ ´ŒÒ¹º¹

àÊÒâÅËÐË¡àËÅÕèÂÁ

ÃÙ·ÕèÁØÁ«ŒÒ´ŒÒ¹ËÅѧ ¢Í§á¼‹¹°Ò¹ ªÔé¹µ‹ÍÁØÁ©Ò¡


àÃÕ¹ÃÙጠÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ MCS-51

l 23

A1.7 ·Õ´è Ҍ ¹Ë¹ŒÒãˌ·Òí ¡ÒõԴµÑ§é àÊÒâËÅÐË¡ààËÅÕÂè Áઋ¹¡Ñ¹ â´ÂÌÍÂÊ¡ÃÙ 3x10 ÁÁ. ¼‹Ò¹á·‹§µ‹Í 3 ÃÙ áÅŒÇ¢Ñ¹à¢ŒÒ ä»ã¹ÃÙ·àÕè nj¹änj¢Í§©Ò¡Âִ᡹ŌÍÊÑ¹é ¨Ò¡¢Ñ¹é µÍ¹·Õè A1.5 ¢Ñ¹¼‹Ò¹á¼‹¹°Ò¹ä»ÂִࢌҡѺàÊÒ´ÅËÐË¡àËÅÕÂè Á 30 ÁÁ. ·íÒઋ¹¹Õ·é §Ñé Êͧ¢ŒÒ§ â´Â᷋§µ‹Í·Õ¹è Òí ÁÒÂÖ´´ŒÇ¹ըé Ð㪌µ´Ô µÑ§é ἧǧ¨ÃµÃǨ¨ÑºáʧÊзŒÍ¹ÍÔ¹¿ÃÒàôµ‹Íä» á·‹§µ‹Í 3 ÃÙ ´ŒÒ¹Å‹Ò§ Ê¡ÃÙ 3x10 ÁÁ. ©Ò¡Âִ᡹ŌÍÊѹé àÊÒâÅËÐË¡àËÅÕÂè Á 30 ÁÁ. A1.8 Âѧ¤§¤ÇèÒí Ἃ¹°Ò¹ÍÂً ¹íÒ᡹âÅËСÅÁàÊÕºࢌҷթè Ò¡Âִ᡹ŌÍÂÒÇã¹µíÒá˹‹§ÃÙ·Õè 2 áÅÐ 6 ´Ñ§ÃÙ» ¡‹Í¹ ¹íÒŌͻÃФͧÊÒ¾ҹ¡ÅÒ§àÊÕºµÒÁ´ŒÇ´ØÁŌÍà¾×Íè ÂÖ´ãˌṋ¹ ¨Ò¡¹Ñ¹é ¾ÅԡἋ¹°Ò¹ãˌ¡ÅѺ仴ŒÒ¹º¹ áŌǹíÒ á¡¹âÅËСÅÁàʌ¹·Õè 3 àÊÕºࢌҷթè Ò¡Âִ᡹ŌÍÊÑ¹é µ‹Í´ŒÇ¹íÒŌͻÃФͧÊÒ¾ҹãË­‹ÊÇÁࢌÒÁÒ áŌÇÂÖ´ãˌ ṋ¹´ŒÇ´ØÁÅŒÍ á¡¹âÅËСÅÁ ´ØÁŌÍ

´ØÁŌÍ

ŌͻÃФͧÊÒ¾ҹ¡ÅÒ§

ŌͻÃФͧÊÒ¾ҹãË­‹ ´ØÁÅŒÍ á¡¹âÅËСÅÁ

ÃÙ·Õè 6

´ØÁÅŒÍ ÃÙ·Õè 2


24 làÃÕ¹ÃÙá Œ ÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ

MCS-51

A1.9 »ÃСͺŌÍÊÒ¾ҹ¨íҹǹ 2 àʌ¹ ã¹áµ‹ÅÐàʌ¹ »ÃСͺ´ŒÇ ÊÒ¾ҹ 30 ¢ŒÍµ‹Í 1 àʌ¹ ÊÒ¾ҹ 10 ¢ŒÍµ‹Í 2 àʌ¹ ¡Òõ‹ÍÊÒ¾ҹ¨ÐµŒÍ§¡ÅÑ´ÊÒ¾ҹᵋÅдŒÒ¹à¢ŒÒ´ŒÇ¡ѹÍ‹ҧÃÐÁÑ´ÃÐÇѧ

A1.10 ¹íÒÊÒ¾ҹ·Õ»è ÃСͺáŌÇÊÇÁࢌҡѺŌ͢ѺÊÒ¾ҹáÅÐŌͻÃФͧÊÒ¾ҹ ·íҷѧé Êͧ´ŒÒ¹

A1.11 ¹íÒẵàµÍÃÕ¢è ¹Ò´ AA ¨íҹǹ 4 ¡ŒÍ¹ ºÃèØ㹡кж‹Ò¹¢Í§ºÍÏ´ RBX-51 ¨Ò¡¹Ñ¹é ¹íÒºÍÏ´ RBX- 51 µÔ´µÑ§é ŧº¹ªØ´°Ò¹Ëع‹ ¹µ â´ÂµÔ´µÑ§é ãˌ´ÒŒ ¹·ÕÁè ÊÕ ÇÔµªà»´»´ÍÂÙ´‹ Ҍ ¹à´ÕÂǡѺÁÍàµÍÏ ¨Ò¡¹Ñ¹é 㪌ʡÃÙÁÍ× ËÁع 3 µÑǢѹÂÖ´ºÍÏ´¡Ñºâ¤Ã§Ëع‹ ¹µ Ê¡ÃÙÁÍ× ËÁع

Ê¡ÃÙÁÍ× ËÁع ẵàµÍÃÕè AA 4 ¡ŒÍ¹


àÃÕ¹ÃÙጠÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ MCS-51

l 25

A1.12 ¹íÒἧǧ¨ÃâÁ´ÙÅÃѺáʧÍÔ¹¿ÃÒàô ZX-IRM ÂִࢌҡѺªÔ¹é µ‹ÍÁØÁ»‡Ò¹ ¢Ñ¹ÂÖ´´ŒÇÂÊ¡ÃÙ 3x15 ÁÁ. áÅй͵ 3 ÁÁ. ¹íÒªÔ¹é µ‹ÍµÃ§ÁÒàÊÕºࢌҡѺ»ÅÒ´ŒÒ¹Ë¹Ö§è ¢Í§ªÔ¹é µ‹ÍÁØÁ»‡Ò¹ Ê¡ÃÙ 3x15 ÁÁ.

ªÔ¹é µ‹ÍÁØÁ»‡Ò¹

ÊÒÂÊÑ­­Ò³ JST3AA-8

ªÔ¹é µ‹ÍµÃ§

A1.13 µÔ´µÑ§é ªÔ¹é µ‹ÍÁØÁ©Ò¡à¢ŒÒ·ÕÃè ãÙ ¹µíÒá˹‹§¡Ö§è ¡ÅÒ§¢Í§ºÍÏ´ RBX-51 ·Ò§´ŒÒ¹ÊÇÔµªà»´»´ ËÃ×Í´ŒÒ¹ËÅѧ¢Í§â¤Ã§ Ëع‹ ¹µ Robo-51 â´Â㪌ʡÃÙ 3x10 ÁÁ. ¢Ñ¹ÂÖ´¡Ñº¹Íµ 3 ÁÁ. à¾×Íè 㪌໚¹·ÕÂè ´Ö á¼§Ç§¨ÃâÁ´ÙÅÃѺáʧÍÔ¹¿ÃÒàô ªÔ¹é µ‹ÍÁØÁ©Ò¡

Ê¡ÃÙ 3x10 ÁÁ. A1.14 ¹íÒἧǧ¨ÃâÁ´ÙÅÃѺáʧÍÔ¹¿ÃÒàô ZX-IRM ·Õµè ´Ô µÑ§é ªÔ¹é µ‹ÍÁØÁ»‡Ò¹áÅÐªÔ¹é µ‹Íá¹ÇµÃ§¨Ò¡¢Ñ¹é µÍ¹·Õè A1.11 àÊÕºࢌҡѺªÔ¹é µ‹ÍÁØÁ©Ò¡ã¹¢Ñ¹é µÍ¹·Õè A1.12 Íѹ໚¹¡ÒõԴµÑ§é ἧǧ¨Ã ZX-IRM ࢌҡѺËع‹ ¹µ Robo-51 ¨Ò¡¹Ñ¹é àÊÕºÊÒÂÊÑ­­Ò³¢Í§ ZX-IRM ࢌҷյè Òí á˹‹§ P3.2 ZX-IRM

P3.2


26 làÃÕ¹ÃÙá Œ ÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ

MCS-51

A1.15 àÊÕºÊÒÂÁÍàµÍÏࢌҡѺ¨Ø´µ‹ÍÁÍàµÍÏ â´Âµ‹ÍÁÍàµÍÏ¢ÇÒà¢ŒÒ·Õ¨è ´Ø µ‹Í M-2 ÊÕ¢ÒÇ áÅÐÁÍàµÍϫҌ ÂࢌҷÕè ¨Ø´µ‹Í M-1 ÊÕ´Òí Í‹ҧäáçµÒÁÊÒÁÒöà»ÅÕÂè ¹á»Å§ä´Œ ËÒ¡¡ÒÃà¤Å×Íè ¹·Õ¢è ͧËع‹ ¹µäÁ‹¶¡Ù µŒÍ§

ÁÍàµÍÏ M-1 ÁÍàµÍÏ M-2

A1.16 µÔ´µÑ§é ἧ¨ÃµÃǨ¨ÑºáʧÊзŒÍ¹ÍÔ¹¿ÃÒàô«Ö§è ¹íÒÁÒ㪌㹡ÒõÃǨ¨Ñºàʌ¹à¢ŒÒ·Õ´è Ҍ ¹Ë¹ŒÒ¢Í§Ëع‹ ¹µ â´Â µÔ´µÑ§é ࢌҡѺ»ÅÒ¢ͧ᷋§µ‹Í 3 Ã٠㹡ÒõԴµÑ§é à¾×Íè Âִἧǧ¨ÃµÃǨ¨ÑºáʧÊзŒÍ¹ÍÔ¹¿ÃÒàô·Ñ§é ÊͧªØ´ãˌ㪌 àÊÒÃͧ¾ÅÒʵԡ 3 ÁÁ. Ãͧ¡‹Í¹¢Ñ¹ÂÖ´´ŒÇÂÊ¡ÃÙ 3x10 ÁÁ.áÅй͵ 3 ÁÁ. µÔ´µÑ§é ·Ñ§é Êͧ´ŒÒ¹ ´Ñ§ÃÙ» ¹Íµ 3 ÁÁ. ἧǧ¨ÃµÃǨ¨ÑºáʧÊзŒÍ¹ÍÔ¹¿ÃÒàô àÊÒÃͧ 3 ÁÁ. A1.17 ¹íÒâÁ´ÙÅÇÑ´ÃÐÂзҧ GP2D120 µÔ´µÑ§é ࢌҡѺªÔ¹é µ‹ÍÁØÁ©Ò¡ã¹ÅѡɳдѧáÊ´§ã¹ÀÒ¾ â´Â㪌ʡÃÙ¢¹Ò´ 3x10 ÁÁ. áÅй͵ 3 ÁÁ. ¢Ñ¹ÂÖ´ ªÔ¹é µ‹ÍÁØÁ©Ò¡ GP2D120 ¢Ñ¹Ê¡ÃÙ 3x10 ÁÁ. áÅй͵ 3 ÁÁ.


àÃÕ¹ÃÙጠÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ MCS-51

l 27

A1.18 ·Õ´è Ҍ ¹Ë¹ŒÒ¢Í§â¤Ã§Ëع‹ ¹µ¹Òí Ê¡ÃÙ 3x10 ÁÁ. áÅй͵ 3 ÁÁ. ÌÍÂàÍÒänj ÂѧäÁ‹µÍŒ §¢Ñ¹ãˌṋ¹ ¨Ò¡¹Ñ¹é ¹íÒ â¤Ã§ÊÌҧ¢Í§ GP2D120 ·ÕÊè ÌҧäÇŒã¹¢Ñ¹é µÍ¹·Õè A1.16 ÁÒàÊÕºࢌÒä»ÃÐËNjҧ¹Íµ¡Ñº°Ò¹ áŌǢѹʡÃÙ嫅 ãˌṋ¹

¹Íµ 3 ÁÁ.

GP2D120 àÊÕºªÔ¹é ʋǹࢌÒä»ÃÐËNjҧ ¹ÍµáÅÐἋ¹°Ò¹ °Ò¹¢Í§Ëع‹ ¹µ

Ê¡ÃÙ 3x10 ÁÁ.

¢Ñ¹Ê¡ÃÙãˌṋ¹

A1.19 µ‹ÍÊÒÂÊÑ­­Ò³¢Í§âÁ´ÙÅ GP2D120 ࢌҡѺª‹Í§ AN2 áÅе‹ÍÊÒÂÊÑ­­Ò³¢Í§á¼§Ç§¨ÃµÃǨ¨Ñºáʧ ÊзŒÍ¹ÍÔ¹¿ÃÒàô (ZX-03) ´ŒÒ¹«ŒÒÂࢌҷժè ͋ § AN5 ʋǹ´ŒÒ¹¢ÇÒµ‹Í¡Ñºª‹Í§ AN4

ZX-03 ¢ÇÒ ZX--IRM GP2D120

ZX-03 «ŒÒÂ

A1.20 ¨Ñ´à¡çºÊÒÂÊÑ­­Ò³·Ñ§é ËÁ´ãˌàÃÕºÌÍ ¨Ð䴌˹؋ ¹µ Robo-51 ·Õ¾ è ÌÍÁÊíÒËÃѺ¡ÒÃà¢Õ¹â»Ãá¡ÃÁ à¾×Íè ·´Êͺ¡Ò÷íÒ§Ò¹µ‹Íä»


28làÃÕ¹ÃÙá Œ ÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ

MCS-51

¡Ô¨¡ÃÃÁ·Õè 2

ÃÙ¨Œ ¡Ñ ¡Ñºá¼§Ç§¨Ã RBX-51AC2 áÅСÒÃàµÃÕÂÁ «Í¿µáÇÏÊÒí ËÃѺ¾Ñ²¹Òâ»Ãá¡ÃÁÀÒÉÒ C ἧǧ¨Ã¤Çº¤ØÁËÅÑ¡¢Í§Ëع‹ ¹µ Robo-51 ¤×Í ºÍÏ´ RBX-51AC2 plus «Ö§è ÁÕÊNj ¹»ÃСͺ µ‹Ò§æ áÊ´§ã¹ÃÙ»·Õè A2-1 ʋǹǧ¨ÃÊÁºÙóáÊ´§ã¹ÃÙ»·Õè A2-2 ËÑÇã¨ËÅÑ¡¤×Í äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ T89C51AC2 ¨Ò¡ Atmel ·ÕÁè ¤Õ ³ Ø ÊÁºÑµ¾Ô àÔ ÈÉàËÁÒе‹Í¡ÒÃÊÌҧËع‹ ¹µ¤Í× ÁÕâÁ´ÙÅá»Å§ÊÑ­­Ò³ÍÐ¹Ò Å͡໚¹´Ô¨µÔ ÍÅ ¤ÇÁÒÅÐàÍÕ´ 10 ºÔµ ÁÒ¡¶Ö§ 8 ª‹Í§ ᵋ㹺ÍÏ´ RBX-51AC2 plus ¨Ñ´ÊÃÃänjãˌ㪌 6 ª‹Í§ ¾ÃŒÍÁ¡ÑºÇ§¨ÃÅ´·Í¹áç´Ñ¹ÍÔ¹¾Øµ à¹×èͧ¨Ò¡ÍÔ¹¾Øµ¢Í§âÁ´ÙÅá»Å§ÊÑ­­Ò³ÍйÒÅ͡໚¹ ´Ô¨µÔ ÍŹѹé ÊÒÁÒöÃѺáç´Ñ¹ÍÔ¹¾Øµä´ŒÊ§Ù ÊØ´à¾Õ§ 2.5V ã¹¢³Ð·Õµè ÇÑ µÃǨ¨ÑºÍйÒÅÍ¡·Ñ§é ËÁ´ã¹ªØ´ Ëع‹ ¹µ Robo-51 ãˌàÍÒµ¾µØ ໚¹áç´Ñ¹ÊÙ§ÊØ´ +5V ¨Ö§µ‹ÍŒ §ÁÕǧ¨ÃÁÒÅ´·Í¹áç´Ñ¹Å§ ¾ÍÏ µ ÍÔ ¹ ¾Ø µ /àÍÒµ ¾Ø µ ´Ô ¨Ô µ ÍÅ ¨Ø´µ‹ÍÃкººÑÊ I2C

(µ‹Í¢Ò P3.2, P3.3, P3.4, P4.0 áÅÐ P4.1) (µ‹Í¢Ò P0.0 : SDA áÅÐ P0.1 : SCL)

ÊÇÔµªà»´/»´

¨Ø´µ‹ÍẵàµÍÃÕè ¨Ø´µ‹Í¾Íϵ͹ءÃÁ RS-232

ÅíÒ⾧ໂÂâ« (µ‹Í¢Ò P2.7) ÍÔ¹¾ØµÍйÒÅÍ¡

äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ 6&'+#)+

¨Ø´µ‹Íà«ÍÏâÇÁÍàµÍÏ ÊÇÔµªàÅ×Í¡âËÁ´ ¨Ø´µ‹ÍâÁ´ÙÅ LCD (µ‹Í¢Ò P2.4, P2.5 áÅÐ P2.6) Ãѹ/â»Ãá¡ÃÁ ¨Ø´µ‹ÍÁÍàµÍÏ俵ç (µ‹Í¢Ò P2.0, P2.1, P1.6 áÅÐ P2.2, P2.3, P1.7)

ÊÇÔµªÃàÕ «µ

LED áÊ´§¼Å

(µ‹Í¢Ò P0.2 : RS, P0.3 : E, P0.4-P0.7 : D4-D7)

(µ‹Í¢Ò P3.5-P3.7)

¨ÑÁê à»ÍÏàÅ×͡ແ´-»´ LED ʋͧËÅѧ¢Í§âÁ´ÙÅ LCD

ÃÙ»·Õè A2-1 ʋǹ»ÃСͺ¢Í§ RBX-51AC2 plus ἧǧ¨ÃËÅÑ¡·Õãè ªŒ¤Çº¤ØÁËع‹ ¹µ Robo-51


àÃÕ¹ÃÙጠÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ MCS-51 l 29 +5V +5V

+5V C7 1/50V

4 8

+5V

R7 100k

LED1 PGM

LED2 RUN

IC3 555

6 7

C6 2.2/50V

2

R5 220

SW2 MODE

R10 1k

11

42

C19 0.1/50V

EA

5 C8 0.01/50V

1

R6 510

R9 10k

R8 150

3

1

IC5 T89C51AC2 44

SW3 RESET R11 4.7

38

43

RESET PSEN

+5V

16 1

C11

IC4 ICL232

3

TxD RxD

-

4 C12

7

10

13

8

9

12

6

LED5

P3.7

Vref

CCW. CW.

P3.6

VR1 10k V.Ref Adjust 2.5V. Nominal

P3.5

P3.7

19

GND

OUT

2

C4 ZD2 1000µF 5.6V 6.3V C5 0.01µF 50V

R37 1k R36 1k

17 P3.5

8

+5V

+5V

P1.5/AN5

P0.2

R22 47k

7

P1.4/AN4

P0.3

R21 47k

32

4

33

RS

6 +5V

RN1/8

R20 47k

6 R19 47k

P1.3/AN3

P0.7 P0.6

R18 47k

5 R17 47k

P1.2/AN2

P0.5 P0.4

DSP1 LCD 16x2 (back light option)

E

+V Vo

D7 D6 D5 D4 D3 D2 D1 D0 R/W 14 13 12 11 10 9 8 7 5

37

16

R35 47

BACK LIGHT ON

1 VR2 10k

3

BRIGHTNESS

BLK GND 15 2

36 35 35 +5V

+

R16 47k

4 R15 47k

P1.1/AN1 P2.7

+5V R14 47k

R26-R28 4.7k x3

3

R26 R27 R28

R13 47k

+5V

P1.0/AN0 P2.0

+5V R29 4.7k

R25 4.7k

21

P2.1 P4.1

P2.2

20 P4.0

P2.3 16

K11 PORT-2

15

K10 PORT-1

14

K9 PORT-0

P3.4

+5V +Vm

22

Q2 KRC102

P1.7/PWM2

P3.3

P2.6

2

28

7 2A 1

27

15

26

10

10

9

23

RN3/3 10k R32 4k7

P3.2

SDA SCL

24

30 31

P0.0

RN2/3 +Vm 10k

K17 SERVO #3

P2.4

40 C15 33pF

XTAL1 11.0592MHz

Q4 BC557 RN2/2 +Vm 10k

K16 SERVO #2

+5V

P0.1

GND

25

41

RN3/1 10k R30 4k7

12EN 4A

1Y 3 2Y

IC7 L293D

Q3 BC557 RN2/1 +Vm 10k

C16 33pF

ÃÙ»·Õè A2-2 ǧ¨ÃÊÁºÙó¢Í§á¼§Ç§¨Ã RBX-51AC2 plus

K15 SERVO #1

4Y 3Y

+ DIRECT LED6 DIR. #A

6 14

LED7 DIR. #B

11 R34 2k2

34EN

4

INVERT

+

R33 2k2

3A

13 12 5

RN3/2 10k R31 4k7

RN1/2

1A

C17 0.1/63V

8

Vcc1

+5V Q5 BC557

+5V P2.5

RN1/1

16

29

+5V

+5V

K18A-K18B MOTOR-A

SP1 PIEZO

P1.6/PWM1 9

4

K12 PORT-3

K14 I2C bus

BLA

RN1/5

+5V

+5V

JP1

RN1/3, RN1/4

+5V

K13 PORT-4

4

1

+5V

LED4

R23 47k

K3 ANALOG-0

CE

LED3

R24 47k

K4 ANALOG-1

5

Q1 FDS6680

R4 1k

R38 1k

18 P3.6

D4 1N4148

+5V

IC1 KIA7042

C3 100µF 10V

P3.1/TxD

D3 1N4148 IC6 LM336Z -2.5

K5 ANALOG-2

ZD1 5.1V

IC2 NCP1450 5.0

+5V R2 47k

P3.0/RxD

Vref 2.5V

C14 0.1/50V

+5V

D2 MBRA340T3

+5V

2

K6 ANALOG-3

L1 10µH

15

R12 1k

K7 ANALOG-4

GND 2

R3 D1 1N5819 0.47

C13

K2 RS-232 (RJ-11 female)

K8 ANALOG-5

R1 1k

RESET 3

C1 100µF 10V

+4.8~6Vdc

5

1 +V

C2 0.1/50V

K1 DC input

2

C10

LED1 LOW-BAT.

+

C9 C9-C13 10/50V

+Vm

S1 POWER

C18 0.1/63V K19A-K19B MOTOR-B

INVERT

+ + DIRECT

LED1 ON


30làÃÕ¹ÃÙá Œ ÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ

MCS-51

¡ÒôÒǹâËÅ´â»Ãá¡ÃÁ¤Çº¤ØÁ¨Ð¡ÃзíÒ¼‹Ò¹¾Íϵ͹ءÃÁâ´ÂµŒÍ§ÁÕ¡Òá´ÊÇÔµª MODE ãˌ·Òí §Ò¹ã¹âËÁ´¡ÒÃâ»Ãá¡ÃÁ (PROGRAM : PGM) àÊÕ¡‹Í¹ ¶ŒÒËÒ¡Ëع‹ ¹µ Robo-51 ÍÂÙ㋠¹âËÁ´ Ãѹ ¨Ø´µ‹Í¾Íϵ͹ءÃÁ¨Ð㪌㹡ÒÃÊ×Íè ÊÒâŒÍÁÙšѺ¤ÍÁ¾ÔÇàµÍÏ᷹

¡ÒÃàµÃÕÂÁ¡ÒÃà¾×Íè ¾Ñ²¹Òâ»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺ¤Çº¤ØÁ¡Ò÷íÒ§Ò¹¢Í§Ëع‹ ¹µ Robo-51 ¡ÒÃàµÃÕÂÁ«Í¿µáÇÏ Rkit-51 ÊíÒËÃѺ¡Ò÷´Åͧ·Ò§ÎÒÏ´áÇÏ ¡‹Í¹ÊÌҧä¿Åâ»Ãà¨ç¡µÊíÒËÃѺ¡Ò÷´Åͧ·Ò§ÎÒÏ´áÇϨеŒÍ§àµÃÕÂÁ«Í¿µáÇÏà¾×èÍãˌÊÒÁÒöÊÌҧ ä¿Åä´ŒÍ‹ҧÊдǡ ¢Ñ¹é µÍ¹â´ÂÊÃØ»ÁÕ´§Ñ ¹Õé 1. 㪌 Window Explorer ࢌÒä»·Õâè ¿Åà´ÍÏ·µÕè ´Ô µÑ§é â»Ãá¡ÃÁ RIDE â´Â»¡µÔÍÂÙ·‹ Õè C:\RIDE ¨Ò¡¹Ñ¹é ࢌÒä»·Õè â¿Åà´ÍÏ INC à 51 à Atmel áŌǤѴÅÍ¡ä¿Å c51ac2.h áÅÐ c51ac2.inc ÁÒänj·Õè C:\RIDE\INC ¢Ñ¹é µÍ¹ ¹Õàé »š¹¡ÒÃàµÃÕÂÁàÃÕ¡㪌ä¿ÅäźÃÒÃÕ·ºÕè Ãèآ͌ ÁÙŢͧÃÕ¨ÊÔ àµÍÏ·§Ñé ËÁ´·ÕÁè ãÕ ¹äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ T89C51AC2 ¢Í§ Atmel à¹×Íè §¨Ò¡Ëع‹ ¹µ Robo-51 㪌äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏàºÍϹãÕé ¹¡ÒäǺ¤ØÁ¡Ò÷íÒ§Ò¹ 2. ËÒ¡ÁÕ¡Òü¹Ç¡ä¿Åã´æ ·Õµè ͌ §¡ÒÃâ´Â㪌ä´àÃ硵տ INCLUDE ·ÕÊè Nj ¹ËÑǢͧâ»Ãá¡ÃÁà¾×Íè ¤ÇÒÁÊдǡ ¤ÇäѴÅÍ¡ÁÒÂѧâ¿Åà´ÍÏ C:\RIDE\INC

¢Ñ鹵͹¡ÒþѲ¹Òâ»Ãá¡ÃÁÊíÒËÃѺ·´Åͧ·Ò§ÎÒÏ´áÇÏ ËÅѧ¨Ò¡àµÃÕÂÁ¡ÒÃáÅŒÇ ¡ç¨ÐࢌÒÊÙ¢‹ ¹Ñé µÍ¹¡ÒþѲ¹Òâ»Ãá¡ÃÁ·´Åͧ ÁÕ¢¹Ñé µÍ¹â´ÂÊÃØ»´Ñ§¹Õé 1. ແ´â»Ãá¡ÃÁ RIDE ËÒ¡ÁÕâ»Ãà¨ç¡µã´à»´¤ŒÒ§ÍÂÙ㋠ˌ»´ ŧâ´Âä»·Õàè Á¹Ù Project à Close 2. ÊÌҧâ»Ãà¨ç¡µãËÁ‹ â´Âä»·ÕèàÁ¹Ù Project à New Project.. à New ¨Ð»ÃÒ¡¯Ë¹ŒÒµ‹Ò§ãˌµÑ駪×èÍ â»Ãà¨ç¡µ·µÕè ͌ §¡ÒÃ㹪‹Í§ Name ¡íÒ˹´ª×Íè µÒÁ·Õµè ͌ §¡Òà ¨Ò¡¹Ñ¹é àÅ×Í¡ª¹Ô´¢Í§äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ㹪‹Í§ Type ãˌàÅ×Í¡ 80C51 áŌǡ´»ØÁ† OK 3. ¨Ð»ÃÒ¡¯Ë¹ŒÒµ‹Ò§àÅ×Í¡àºÍÏäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ ä»·Õ¡è Ãͺ Device ãˌàÅ×Í¡ Atmel à T89C51AC2 ʋǹ·Õ¡è Ãͺ Properties ãˌàÅ×Í¡ Harvard Architechture áŌǡ´»ØÁ† OK 4. ä»·Õàè Á¹Ù File à New à C file ¨Ð»ÃÒ¡¯Ë¹ŒÒµ‹Ò§ÊíÒËÃѺà¢Õ¹â»Ãá¡ÃÁ 5. à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C µÒÁ·Õµè ͌ §¡Òà ¨Ò¡¹Ñ¹é ºÑ¹·Ö¡à»š¹ä¿Å¹ÒÁÊ¡ØÅ .c 㹪×Íè à´ÕÂǡѺª×Íè â»Ãà¨ç¡µ ·Õ¡è Òí ˹´änjáŌÇã¹¢Ñ¹é µÍ¹·Õè 2


àÃÕ¹ÃÙጠÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ MCS-51 l 31

6. ·íÒ¡ÒÃÃÇÁä¿Å¢Í§â»Ãá¡ÃÁÀÒÉÒ C ·Õàè ¢Õ¹¢Ö¹é ࢌÒänjã¹â»Ãà¨ç¡µ ·íÒ䴌 2 ÇÔ¸¤Õ Í× (¡) ä»·Õàè Á¹Ù Project à Add note Source/Application ËÃ×Í¡´¤Õ Alt + Insert

(¢) ä»·Õè˹ŒÒµ‹Ò§ Project «Öè§ÍÂً´ŒÒ¹Å‹Ò§·Ò§«ŒÒ áŌǤÅÔ¡àÁÒʏ¢ÇÒ·ÕèºÃ÷Ѵ«Öè§ÁÕª×èÍä¿ÅÍÂً ¨Ò¡¹Ñ¹é àÅ×Í¡ Add note Source/Application

¨Ð»ÃÒ¡¯Ë¹ŒÒµ‹Ò§àÅ×Í¡ä¿Å¹ÒÁÊ¡ØÅ .c ·Õ赌ͧ¡ÒÃà¾ÔèÁࢌÒä»ã¹â»Ãà¨ç¡µ «Ö觡ç¤×Íä¿Å¢Í§ â»Ãá¡ÃÁÀÒÉÒ C ·Õàè ¢Õ¹¢Ö¹é ¨Ò¡¢Ñ¹é µÍ¹·Õè 5 7. ä»·Õàè Á¹Ù Option Project ¨Ð»ÃÒ¡¯Ë¹ŒÒµ‹Ò§ Option áŌÇàÅ×Í¡ Directories ¨Ò¡¹Ñ¹é ä»·Õªè ͋ § Include (.h, .inc) ãˌ¡Òí ˹´à»š¹ %RIDEDIR%\Inc;%RIDEDIR%\Inc ËÃ×Í C:\RIDE\INC 8. ·íÒ¡ÒäÍÁä¾Åä¿Å·§Ñé ËÁ´ â´Â¡´¤Õ F9 ËÃ×Íä»·Õàè Á¹Ù Project à Make All ·ÕËè ¹ŒÒµ‹Ò§ Make «Ö§è ÍÂً ·Ò§¢ÇÒ´ŒÒ¹Å‹Ò§¢Í§Ë¹ŒÒµ‹Ò§ËÅÑ¡ ¨ÐáÊ´§¢ŒÍ¤ÇÒÁᨌ§¼Å¡Ò÷íÒ§Ò¹ ËÒ¡äÁ‹ÁÍÕ ÐäüԴ¾ÅÒ´ ·Õâè ¿Åà´ÍϢͧâ»Ãà¨ç¡µ ·Õ·è Òí ¡ÒÃÊÌҧ¢Ö¹é ¨Ð»ÃÒ¡¯ä¿Å¹ÒÁÊ¡ØÅ .aof, .dbi, .bak, .lst, .xrf áÅÐ .hex «Ö§è ä¿Å¹ÒÁÊ¡ØÅ .hex ¹Õàé ͧ¤×Íä¿Å·¨Õè Ð ¹íÒ¢ŒÍÁÙÅä»â»Ãá¡ÃÁŧã¹äÁâ¤Ã¤Í¹â·ÃÅàÅÍϴnj Ââ»Ãá¡ÃÁ Flip à¾×Íè ·´Åͧµ‹Íä» Robo-51


32làÃÕ¹ÃÙá Œ ÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ

MCS-51

3.1 à¢Õ¹â»Ãá¡ÃÁà¾×Íè áÊ´§¼Å¡Ò÷íÒ§Ò¹´ŒÇ LED

ã¹ËÑÇ¢ŒÍ¹Õ¡é ŋÒǶ֧¡ÒÃà¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C à¾×Íè ¤Çº¤ØÁ¡ÒõԴ´Ñº¢Í§ LED º¹á¼§Ç§¨Ã ËÅÑ¡ RBX-51AC2 plus ¢Í§Ëع‹ ¹µ Robo-51 ·Ñ§é à¾×Íè àÃÕ¹ÃÙ¡Œ ÒÃà¢Õ¹â»Ãá¡ÃÁà¾×Íè áÊ´§¼Å¡Ò÷íÒ§Ò¹ ¼‹Ò¹ LED ¹Í¡¨Ò¡¹Ñé¹Âѧ䴌àÃÕ¹Ãٌà¡ÕèÂǡѺ¡ÒÃÊÌҧ¿˜§¡ªÑè¹Ë¹‹Ç§àÇÅÒáÅШѴà¡çºà»š¹äźÃÒÃÕà¾×èÍ ¤ÇÒÁÊдǡ㹡ÒþѲ¹Òâ»Ãá¡ÃÁ㹤ÃÑ駵‹Íæä»

3.1.1 ¡ÒäǺ¤ØÁ LED ¢Í§á¼§Ç§¨Ã RBX-51AC2 plus ἧǧ¨Ã RBX-51AC2 plus ÁÕ LED áÊ´§¼Å 3 ´Ç§ â´Âàª×èÍÁµ‹Í¡Ñº¢Ò¾Íϵ P3.5-P3.7 ¢Í§äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ T89C51AC2 ´Ñ§áÊ´§Ç§¨Ã¢Í§Ê‹Ç¹¹Õãé ¹ÃÙ»·Õè 3-1 ¨Ò¡Ç§¨Ã·Õáè Ê´§µÒÁÃÙ»·Õè 3-3 ໚¹¡ÒâѺ LED Ẻ¡ÃÐáÊ«Ô§¡ (sink current) â´ÂÁÕµÇÑ µŒÒ¹ ·Ò¹ 1kΩ ·íÒ˹ŒÒ·Õè¨íÒ¡Ñ´¡ÃÐáÊ·ÕèäËż‹Ò¹ LED «Ö觡íÒ˹´änj»ÃÐÁÒ³ 5 mA µ‹Í´Ç§ ´Ñ§¹Ñ鹶ŒÒ µŒÍ§¡ÒâѺ LED ´Ç§ã´ãˌµ´Ô ·íÒ䴌â´Â¡ÒáíÒ˹´ãˌ¢Ò¾Íϵ·Õàè ª×Íè Áµ‹Í¡Ñº LED ´Ç§¹Ñ¹é Áդҋ Åͨԡ ໚¹ “0” ã¹·Ò§µÃ§¢ŒÒÁ¶ŒÒµŒÍ§¡ÒÃãˌ´ºÑ ¡ç¡Òí ˹´ãˌ໚¹ “1” ´Ñ§¹Ñ¹é ¨ÐÊѧࡵ䴌Çҋ àÁ×Íè àÃÔÁè µŒ¹¨‹ÒÂä¿ ãˌ¡ºÑ ǧ¨Ã ¢Ò¾Íϵ P3.5-P3.7 ¨ÐÁÕÊÀÒÇÐàÃÔÁè µŒ¹à»š¹ “1” ´Ñ§¹Ñ¹é LED ·Ñ§é 3 ´Ç§¨Ö§´ÑºËÁ´ +5V

R10 1k

11

42

C19 0.1/50V

EA

1

IC5 T89C51AC2

43

LED5

P3.7 P3.6 P3.5

P3.7

19 LED4

+5V R38 1k R37 1k

18 P3.6

R36 1k

17 LED3

P3.5

40 C15 33pF

XTAL1 11.0592MHz

41 C16 33pF

ÃÙ»·Õè 3-1 ǧ¨Ã¢Ñº LED º¹á¼§Ç§¨Ã RBX-51AC2 ã¹Ëع‹ ¹µ Robo-51


àÃÕ¹ÃÙጠÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ MCS-51 l 33

ã¹ÅíҴѺµ‹Í仨Ð໚¹¡ÒÃà¢Õ¹â»Ãá¡ÃÁ¤Çº¤ØÁ LED ·Ñé§ 3 ´Ç§ à¾×èÍãˌ䴌àËç¹¼ÅÅѾ¸ä´Œ Êдǡ¢Ö¹é ¨Ö§¨íÒ໚¹µŒÍ§ÁÕ¡ÒÃ˹‹Ç§àÇÅÒáÊ´§¼Å¡ÒõԴ/´Ñº¢Í§ LED ¹Ñ¹è ¤×Í àÁ×Íè Êѧè ãˌ LED µÔ´¨Ð µŒÍ§Ë¹‹Ç§àÇÅÒãˌ LED µÔ´¤ŒÒ§ªÑÇè ¢³ÐË¹Ö§è ¨Ò¡¹Ñ¹é ¨Ö§ÊÑ§è ´Ñº áŌÇ˹‹Ç§àÇÅÒÍÕ¡¤ÃÑ§é ´Ñ§¹Ñ¹é ¨Ö§µŒÍ§ÊÌҧ ¿˜§¡ª¹Ñè ˹‹Ç§àÇÅÒ¢Ö¹é ÁÒáŌǨѴà¡çºã¹ÃÙ»¢Í§äźÃÒÃÕ ÀÒÂ㵌ªÍ×è ä¿ÅäźÃÒÃÕ delay.h ¡ÒÃÊÌҧä¿ÅäźÃÒÃÕ¹Õé¤ÇáÃзíҡѺ¡Ò÷íҧҹ㹷ءẺ·ÕèÊíҤѭáÅÐÁÕ¡ÒÃàÃÕ¡㪌§Ò¹ º‹Í â´ÂµÑ§é ª×Íè ä¿ÅãˌÊÍ´¤ÅŒÍ§¡ÑºË¹ŒÒ·Õ¢è ͧ¡ÅØÁ‹ ¿˜§¡ª¹Ñè ·ÕÍè ÂÙ㋠¹ä¿ÅäźÃÒÃÕ¹¹Ñé áŌǺѹ·Ö¡à»š¹ä¿Å ¹ÒÁÊ¡ØÅ .h ·Ñ§é ¹Õàé ¾×Íè »ÃÐ⪹ã¹¡ÒþѲ¹Òâ»Ãá¡ÃÁ¤ÃÑ§é µ‹Íæ ä»

3.1.2 ¿˜§¡ª¹Ñè ˹‹Ç§àÇÅÒ

µ‹Í仹Õé໚¹¡ÒùíÒàʹ͡ÒÃÊÌҧ¿˜§¡ªÑè¹Ë¹‹Ç§àÇÅÒ·Õè¨Ð¨Ñ´à¡çºà»š¹ä¿ÅäźÃÒÃÕä¿Å·Õèª×èÍ delay.h ÁÕµÇÑ Í‹ҧâ»Ãá¡ÃÁáÊ´§ã¹â»Ãá¡ÃÁ·Õè P3-1 â´Âãˌ¾ÁÔ ¾â»Ãá¡ÃÁáÅкѹ·Ö¡à»š¹ä¿Å¹ÒÁ Ê¡ØÅ .h áŌǨѴà¡çºä¿ÅãˌÍÂÙ·‹ âÕè ¿Åà´ÍÏà´ÕÂǡѺä¿Åâ»Ãà¨ç¡µ·¡Õè Òí Åѧ¾Ñ²¹Ò«Ö§è ¨Ðª‹ÇÂãˌ¡ÒÃàª×Íè Á⧠ä¿Å (ÅÔ§¡ : link) 䴌Êдǡ¢Ö¹é ã¹¢Ñ¹é µÍ¹¢Í§¡ÒäÍÁä¾Å

â»Ãá¡ÃÁ·Õè P3-1 ä¿ÅäźÃÒÃÕ delay.h ·ÕÊè Ìҧ¢Ö¹é ÊíÒËÃѺ㪌˹‹Ç§àÇÅÒã¹Ë¹‹Ç 100 äÁâ¤ÃÇÔ¹Ò·Õ (0.1 ÁÔÅÅÔÇ¹Ô Ò·Õ) áÅÐ 1 ÁÔÅÅÔÇ¹Ô Ò·Õ /******************************************************************************/ // Program : Delay 100us and 1 ms per unit // Filename : delay.h // C compiler : RIDE 51 V6.1 /******************************************************************************/ /*************************** Delay 1 ms per unit **************************/ void delay_ms(unsigned int ms) { unsigned int x,a; // Keep for counter loop for(x=0;x<ms;x++) { for(a=0;a<878;a++); // Loop for delay 1 ms per unit } } /*********************** Delay 100 us per unit ********************************/ void delay_100us(unsigned int us) { unsigned int x,a; // Keep for counter loop for(x=0;x<us;x++) { for(a=0;a<75;a++); // Loop for delay 100 us per unit } }


34làÃÕ¹ÃÙá Œ ÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ

MCS-51

3.1.2.1 ¤íÒ͸ԺÒ¿˜§¡ª¹Ñè

(1) ¿˜§¡ª¹Ñè delay_ms : ˹‹Ç§àÇÅÒã¹Ë¹‹ÇÂÁÔÅÅÔÇ¹Ô Ò·Õ ÃٻẺ¿˜§¡ªÑè¹ void delay_ms(unsigned int ms) ¾ÒÃÒÁÔàµÍÏ ms ¡íÒ˹´¤‹ÒàÇÅÒ·Õµè ͌ §¡ÒÃ˹‹Ç§ã¹Ë¹‹ÇÂÁÔÅÅÔÇ¹Ô Ò·Õ Áդҋ µÑ§é ᵋ 0 ¶Ö§ 65,535 ¡Òä׹¤‹Ò äÁ‹Á¡Õ Òä׹¤‹Ò ÃÒÂÅÐàÍÕ´ ໚¹¿˜§¡ª¹Ñè ˹‹Ç§àÇÅÒã¹Ë¹‹ÇÂ˹‹ÇÂÁÔÅÅÔÇ¹Ô Ò·Õ 2. ¿˜§¡ª¹Ñè delay_100us : ˹‹Ç§àÇÅÒã¹Ë¹‹Ç 100 äÁâ¤ÃÇÔ¹Ò·ÕËÃ×Í 0.1 ÁÔÅÅÔÇ¹Ô Ò·Õ ÃٻẺ¿˜§¡ªÑè¹ void delay_100us(unsigned int us) ¾ÒÃÒÁÔàµÍÏ us ¡íÒ˹´¤‹ÒàÇÅÒ·Õµè ͌ §¡ÒÃ˹‹Ç§ã¹Ë¹‹Ç 100 äÁâ¤ÃÇÔ¹Ò·Õ Áդҋ 0 ¶Ö§ 65,535 ¡Òä׹¤‹Ò äÁ‹Á¡Õ Òä׹¤‹Ò ÃÒÂÅÐàÍÕ´ ໚¹¿˜§¡ª¹Ñè ˹‹Ç§àÇÅÒã¹Ë¹‹Ç 100 äÁâ¤ÃÇÔ¹Ò·Õ

µÑÇÍ‹ҧ·Õè 3-1 ¡ÒÃàÃÕ¡㪌¿§˜ ¡ª¹Ñè ÀÒÂã¹â»Ãá¡ÃÁ delay_100us(5); delay_100us(20); delay_ms(100); delay_ms(2000);

// ˹‹Ç§àÇÅÒ 500 äÁâ¤ÃÇÔ¹Ò·Õ // ˹‹Ç§àÇÅÒ 2000 äÁâ¤ÃÇÔ¹Ò·Õ ËÃ×Í 2 ÁÔÅÅÔÇ¹Ô Ò·Õ¹¹Ñè àͧ // ˹‹Ç§àÇÅÒ 100 ÁÔÅÅÔÇ¹Ô Ò·Õ // ˹‹Ç§àÇÅÒ 2 ¹Ò·Õ


àÃÕ¹ÃÙጠÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ MCS-51 l 35

¡Ô¨¡ÃÃÁ·Õè 3

¢Ñº LED Í‹ҧ§‹Ò A3.1 ແ´â»Ãá¡ÃÁ RIDE áŌÇà¢Õ¹â»Ãá¡ÃÁ·Õè A3-1 ºÑ¹·Ö¡ã¹ª×Íè ä¿Å act0301.c ·íÒ¡ÒäÍÁä¾Å áŌǴÒǹâËÅ´ ä¿ÅÊáً ¼§Ç§¨Ã RBX-51AC2 ¢Í§Ëع‹ ¹µ Robo-51 ´ŒÇÂâ»Ãá¡ÃÁ Flip A3.2 Ãѹâ»Ãá¡ÃÁ ¨ÐàËç¹ LED µÔ´äŋ໚¹´Ç§æ ¨Ò¡ LED ¢Í§¢Ò P3.5, P3.6 áÅÐ P3.7 àËÁ×͹ä¿ÇÔ§è 3 ´Ç§

â»Ãá¡ÃÁ·Õè A3-1 â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺ·´Åͧ¢Ñº LED º¹á¼§Ç§¨Ã RBX-51AC2 /*——————————————————————————————————————————————————————————————————————————*/ // Program : LED basic drive // Description : drive LED P3.5 , P3.6 and P3.7 by delay interval 2 seconds // Filename : act0301.c // C compiler : RIDE 51 V6.1 /*——————————————————————————————————————————————————————————————————————————*/ #include <C51ac2.h> // Declare T89C51AC2’s register #include <delay.h> // Declare Delay library sbit led1 = P3^5; // Define led1 to P3.5 sbit led2 = P3^6; // Define led2 to P3.6 sbit led3 = P3^7; // Define led3 to P3.7 void main() { while(1) // Endless loop { led1 = 0; // LED at P3.5 on delay_ms(2000); // Delay 2 seconds from delay function of // “delay.h” library led1 = 1; // Turn-off LED at P3.5 led2 = 0; delay_100us(20000); led2 = 1; led3 = 0; delay_100us(20000); led3 = 1;

// // // //

Turn-on LED at P3.6 Delay 2 seconds from delay_100us function from Delay library Turn-off LED at P3.6

// // // //

Turn-on LED at P3.7 Delay 2 seconds from delay_100us function from Delay library Turn-off LED at P3.7

} }

¤íÒ͸ԺÒÂà¾ÔÁè àµÔÁ â´ÂÃÇÁáŌÇâ»Ãá¡ÃÁ¹Õ¨é ФǺ¤ØÁãˌ LED µÔ´¤Ãѧé ÅÐ 1 ´Ç§àÃÕ§ÅíҴѺ¨Ò¡ LED ·Õµè ͋ ¡Ñº P3.5, P3.6 áÅÐ P3.7 áŌÇǹ¡ÅѺ LED ᵋÅдǧµÔ´¹Ò¹ 2 ÇÔ¹Ò·Õ ¨Ò¡¡ÒÃàÃÕ¡㪌¿§˜ ¡ª¹Ñè ˹‹Ç§àÇÅÒ delay_ms áÅÐ delay_100us ã¹äźÃÒÃÕ delay.h «Ö§è 䴌¡µÑÇÍ‹ҧ¡ÒÃÊÌҧáÅШѴà¡çºänj¡Í ‹ ¹Ë¹ŒÒ¹Õáé ÅŒÇ Robo-51


36làÃÕ¹ÃÙá Œ ÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ

MCS-51

3.2 ¡ÒáíÒà¹Ô´ÊÑ­­Ò³àÊÕ§ ã¹ËÑÇ¢ŒÍ¹Õ¡é ŋÒǶ֧¡ÒÃà¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C à¾×Íè ¤Çº¤ØÁãˌäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ T89C51AC2 ã¹Ëع‹ ¹µ Robo-51 ¢ÑºÊÑ­­Ò³àÊÕ§¼‹Ò¹ÅíÒ⾧ໂÂâ« ÃÇÁ·Ñ§é ¡ÒÃÊÌҧ¿˜§¡ª¹Ñè ¡íÒà¹Ô´àÊÕ§áÅШѴà¡çº ໚¹äźÃÒÃÕà¾×Íè ¤ÇÒÁÊдǡ㹡ÒþѲ¹Òâ»Ãá¡ÃÁµ‹Íä»

3.2.1 ¡ÒâѺÊÑ­­Ò³àÊÕ§´ŒÇÂÅíÒ⾧ໂÂâ« ÅíÒ⾧ໂÂâ«à»š¹ÍØ»¡Ã³·Õè·íÒ˹ŒÒ·Õèà»ÅÕè¹¾Åѧ§Ò¹ä¿¿‡Òä»à»š¹¾Åѧ§Ò¹¡Åã¹ÃÙ»¢Í§àÊÕ§ 㹧ҹ´ŒÒ¹äÁâ¤Ã¤Í¹â·ÃÅàÅÍϨѴNjÒ໚¹ÍØ»¡Ã³áÊ´§¼Å·ÕèãˌàÍÒµ¾ØµÍÍ¡ÁÒ໚¹ÊÑ­­Ò³àÊÕ§ ·íÒ˹ŒÒ·Õèᨌ§àµ×͹ʶҹе‹Ò§æ ÀÒÂã¹Ãкº ´Ñ§·Õè䴌¡Å‹ÒÇä»áŌÇÇ‹Ò ÅíÒ⾧ໂÂâ«ãˌàÍÒµ¾Øµà»š¹ ÊÑ­­Ò³àÊÕ§ ¨Ö§¨íÒ໚¹µŒÍ§ÁÕÊ­ Ñ ­Ò³ÍÔ¹¾Øµ·Ò§ä¿¿‡Ò»‡Í¹ãˌ¡ºÑ ÁѹàÊÕ¡‹Í¹ â´Â¤ÇÒÁ¶Õ¢è ͧÊÑ­­Ò³ àÊÕ§·Õè¶Ù¡¡íÒà¹Ô´ÍÍ¡ÁÒ¨Ò¡ÅíÒ⾧ໂÂ⫨Ð໚¹¤‹Òà´ÕÂǡѹ¡Ñº¤ÇÒÁ¶ÕèÊÑ­­Ò³ä¿¿‡ÒÍÔ¹¾Øµ·Õ軇͹ãˌ ᡋÁ¹Ñ ᵋ´ÇŒ ¢ŒÍ¨íҡѴ㹡ÒáíÒà¹Ô´ª‹Ç§¤ÇÒÁ¶Õàè ÊÕ§¢Í§ÅíÒ⾧ໂÂâ«áµ‹ÅЪ¹Ô´¨Ö§·íÒãˌᵡµ‹Ò§¡Ñ¹

3.2.2 ¡ÒâѺÊÑ­­Ò³àÊÕ§º¹á¼§Ç§¨Ã RBX-51AC2 º¹á¼§Ç§¨Ã RBX-51AC2 ÁÕÅÒí ⾧ໂÂâ«·ÕÊè ÒÁÒö¡íÒà¹Ô´ÊÑ­­Ò³àÊÕ§µÔ´µÑ§é ÁҾÌÍÁ㪌§Ò¹ â´Â㪌¢Ò¾Íϵ P2.7 ʋ§ÊÑ­­Ò³àÊÕ§ÍÍ¡ÁÒáŌǵ‹ÍࢌҡѺǧ¨Ã¢ÂÒÂÊÑ­­Ò³´ŒÇ·ÃÒ¹«ÔÊàµÍÏàºÍÏ BC547 à¾×Íè ¢ÑºÊÑ­­Ò³àÊÕ§ÍÍ¡·Ò§ÅíÒ⾧ໂÂâ« ´Ñ§ÃÙ»·Õè 3-2 +5V

R10 1k 11

42

EA

1

IC5 T89C51AC2

C19 0.1/50V

43

+5V

+

P2.7

40 C15 33pF

XTAL1 11.0592MHz

SP1 PIEZO

22

41

Q2 KRC102

C16 33pF

ÃÙ»·Õè 3-2 ǧ¨Ã¢Ñº LED º¹á¼§Ç§¨Ã RBX-51AC2 ã¹Ëع‹ ¹µ Robo-51


àÃÕ¹ÃÙጠÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ MCS-51 l 37

@J

time

@J 6

ÃÙ»·Õè 3-3 ÅѡɳТͧÃÙ»ÊÑ­­Ò³ ÃÙ»·Õè 3-4 áÊ´§ÃÙ»ÊÑ­­Ò³ÊÕàè ËÅÕÂè Á·Õ¡è Òí à¹Ô´ÍÍ¡ÁÒÍ‹ҧµ‹Íà¹×Íè § ÊÕàè ËÅÕÂè ÁÁҵðҹ ¡Òáí Ò à¹Ô ´ ÊÑ ­ ­Ò³àÊÕ Â §¢Í§äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ ·í Ò ä´Œ ⠴¡ÒÃà¢Õ  ¹â»Ãá¡ÃÁ¡ÅÑ º ÊÀÒÇÐÅͨԡ·Õ¢è Ò¾Íϵ P2.7 ä»ÁÒÃÐËNjҧÅͨԡÊÙ§áÅеèÒí ʋǹ¤ÇÒÁ¶Õ¢è ͧÊÑ­­Ò³àÊÕ§·Õàè ¡Ô´¢Ö¹é ¨Ð ÊÙ§ËÃ×͵èÒí ¢Ö¹é ÍÂÙ¡‹ ºÑ ª‹Ç§àÇÅÒ·Õ¶è ¡Ù Ë¹‹Ç§ÃÐËNjҧ¡‹Í¹¡ÒáÅѺÅͨԡ ´Ñ§áÊ´§Ç§¨Ãã¹ÃÙ»·Õè 3-2 ¨Ò¡ÃÙ»·Õè 3-3 áÊ´§¶Ö§ÅѡɳÐÃÙ»ÊÑ­­Ò³ÊÕàè ËÅÕÂè Á«Ö§è »ÃСͺ´ŒÇÂÊÀÒÇÐÅͨԡÊ٧໚¹àÇÅÒà·‹Ò ¡Ñº dt áÅÐÊÀÒÇÐÅͨԡµèÒí ໚¹àÇÅÒ෋ҡѺ dt ઋ¹¡Ñ¹ áÅжŒÒ¡íÒ˹´ãˌ dt ໚¹¨íҹǹàµçÁ෋ҢͧàÇÅÒ 100 äÁâ¤ÃÇÔ¹Ò·Õ ¨ÐÊÒÁÒö᷹¤‹Òã¹ÊÁ¡ÒäÇÒÁ¶ÕÊè ­ Ñ ­Ò³àÊÕ§â´Â»ÃÐÁҳ䴌´§Ñ ¹Õé f=

1 1 5000 = = −6 T 2 × 100 × 10 × dt dt

«Ö觨Ð䴌໚¹ dt =

5000 f

......................................................(3.1)

¨Ò¡ÃÙ»·Õè 3-4 áÊ´§¶Ö§ÊÑ­­Ò³àÊÕ§·Õ¶è ¡Ù ¡íÒà¹Ô´Í‹ҧµ‹Íà¹×Íè §à»š¹àÇÅÒÂÒǹҹ෋ҡѺ time â´Â ¶ŒÒ¡íÒ˹´ãˌ time ¤×ͪ‹Ç§àÇÅÒ·Õµè ͌ §¡ÒÃãˌà¡Ô´ÊÑ­­Ò³àÊÕ§ã¹Ë¹‹ÇÂÁÔÅÅÔÇ¹Ô Ò·Õ «Ö§è à¡Ô´¨Ò¡¡ÒáíÒà¹Ô´ ÊÑ­­Ò³ÃÙ»ÊÕèàËÅÕèÂÁËÅÒÂæ ÅÙ¡µÔ´µ‹Í¡Ñ¹ â´ÂÊÔ觷Õ赌ͧ¤íҹdzà¾×èÍ㪌㹡ÒÃà¢Õ¹â»Ãá¡ÃÁ¡íÒà¹Ô´ ÊÑ­­Ò³àÊÕ§㹪‹Ç§àÇÅÒ·Õ¡è Òí ˹´¤×Í ¨íҹǹÃÙ»ÊÑ­­Ò³¹Ñ¹è àͧ ´Ñ§¹Ñ¹é ¨ÐÁÕµÇÑ á»Ã dt ࢌÒÁÒà¡ÕÂè Ç¢ŒÍ§ 㹡Òäíҹdz´Ñ§¹Õé ¨íҹǹÃͺËÃ×ͨíҹǹÃÙ»¤Å×¹è =

time ×

10−3 5 × time = −6 2 × 100 × 10 × dt dt

...........(3.2)


38làÃÕ¹ÃÙá Œ ÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ

MCS-51

3.2.3 ÊÌҧ¿˜§¡ª¹Ñè ¡íÒà¹Ô´ÊÑ­­Ò³àÊÕ§ µ‹Í仹Õé໚¹¡ÒùíÒàʹ͡ÒÃÊÌҧ¿˜§¡ªÑ蹡íÒà¹Ô´ÊÑ­­Ò³àÊÕ§à¾×èͨѴà¡çºà»š¹ä¿ÅäźÃÒÃÕ·Õè ª×èÍ sound.h â´ÂÁÕÃÒÂÅÐàÍÕ´¢Í§â»Ãá¡ÃÁáÊ´§ã¹â»Ãá¡ÃÁ·Õè P3-2 â´Âãˌ¾ÁÔ ¾áÅкѹ·Ö¡à»š¹ ä¿Å¹ÒÁÊ¡ØÅ .h áŌǨѴà¡çºä¿ÅãˌÍÂÙ·‹ âÕè ¿Åà´ÍÏà´ÕÂǡѺä¿Åâ»Ãà¨ç¡µ·¡Õè Òí Åѧ¾Ñ²¹Ò à¾×Íè ª‹ÇÂãˌ¡Òà àª×èÍÁâ§ËÃ×ÍÅÔ§¡ä¿Åä´ŒÊдǡ¢Öé¹ â»Ãá¡ÃÁ·Õè P3-2 ä¿ÅäźÃÒÃÕ sound.h ·ÕÊè Ìҧ¢Ö¹é ÊíÒËÃѺ¡íÒà¹Ô´ÊÑ­­Ò³àÊÕ§ /*—————————————————————————————————————*/ // Program : Generate sound by frequency // Description : Generate sound function // Filename : sound.h // C compiler : RIDE 51 V6.1 /*—————————————————————————————————————*/ sbit s_bit = P2^7; #pragma DISABLE // for disable all interrupt // before call function delay_sound void delay_sound(unsigned int ms) { unsigned int x,a; // Keep for counter loop for(x=0;x<ms;x++) { for(a=0;a<75;a++); // Loop for delay 100 us per unit } } #pragma DISABLE

void sound(int freq,int time) { int dt=0,m=0; time dt = 5000/freq; time = (5*time)/dt; for(m=0;m<time;m++) { s_bit = 1; delay_sound(dt); s_bit = 0; delay_sound(dt); } } #pragma DISABLE void beep(void) { sound(500,100); }

// for disable all interrupt // before call function sound,beep

// Keep value and keep active logic delay // Keep counter for generating sound

// // // //

P2.7 = high Delay for sound P2.7 = low Delay for sound

// For disable all interrupt // before call function beep

// Generate sound at default frequency

ËÁÒÂà赯 ¿˜§¡ª¹Ñè ã¹äźÃÒÃÕ¹ãÕé ¹¢³Ð¶Ù¡àÃÕ¡㪌§Ò¹¨ÐäÁ‹¶¡Ù ¢Ñ´¨Ñ§ËÇШҡ¡ÒÃÍÔ¹àµÍÃÃ»Ñ µ¨Ò¡áËŋ§ã´æ à¹×Íè § ¨Ò¡¡‹Í¹à¢ŒÒ仡ÃзíÒ¤íÒÊѧè ÀÒÂ㹿˜§¡ª¹Ñè 䴌Á¡Õ ÒáíÒ˹´ä´àÃ硵տ #pragma DISABLE änj˹ŒÒ¿˜§¡ª¹Ñè


àÃÕ¹ÃÙጠÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ MCS-51 l 39

3.3.3.1 ¤íÒ͸ԺÒ¿˜§¡ª¹Ñè (1) ¿˜§¡ª¹Ñè delay_sound ÃٻẺ¿˜§¡ªÑè¹ void delay_sound(unsigned int ms) ¾ÒÃÒÁÔàµÍÏ ms ¡íÒ˹´¤‹ÒàÇÅÒ·Õµè ͌ §¡ÒÃ˹‹Ç§ã¹Ë¹‹ÇÂÁÔÅÅÔÇ¹Ô Ò·Õ Áդҋ µÑ§é ᵋ 0 ¶Ö§ 65,535 ¡Òä׹¤‹Ò äÁ‹Á¡Õ Òä׹¤‹Ò ÃÒÂÅÐàÍÕ´ ໚¹¿˜§¡ª¹Ñè ˹‹Ç§àÇÅÒã¹Ë¹‹Ç 100 äÁâ¤ÃÇÔ¹Ò·Õ (2) ¿˜§¡ª¹Ñè sound ÃٻẺ¿˜§¡ªÑè¹ ¾ÒÃÒÁÔàµÍÏ ¡Òä׹¤‹Ò ÃÒÂÅÐàÍÕ´ (3) ¿˜§¡ª¹Ñè beep ÃٻẺ¿˜§¡ªÑè¹ ¾ÒÃÒÁÔàµÍÏ ¡Òä׹¤‹Ò ÃÒÂÅÐàÍÕ´

void sound(int freq,int time)

¡íÒ˹´¤‹Ò¤ÇÒÁ¶Õ¢è ͧÊÑ­­Ò³àÊÕ§·Õ¶è ¡Ù ¡íÒà¹Ô´ ໚¹¤‹Òâ´Â»ÃÐÁÒ³ time ¡íÒ˹´ª‹Ç§àÇÅҢͧÊÑ­­Ò³àÊÕ§·Õ¶ è ¡Ù ¡íÒà¹Ô´ ໚¹¤‹Òâ´Â»ÃÐÁÒ³ äÁ‹Á¡Õ Òä׹¤‹Ò ໚¹¿˜§¡ª¹Ñè ÊíÒËÃѺ¡íÒà¹Ô´ÊÑ­­Ò³àÊÕ§ÀÒÂã¹â»Ãá¡ÃÁ freq

void beep(void) äÁ‹Á¾Õ ÒÃÒÁÔàµÍÏ äÁ‹Á¡Õ Òä׹¤‹Ò ໚¹¿˜§¡ª¹Ñè ¡íÒà¹Ô´ÊÑ­­Ò³àÊÕ§¤ÇÒÁ¶Õè 500 Hz ¹Ò¹ 0.1 ÇÔ¹Ò·Õ

µÑÇÍ‹ҧ·Õè 3-1 ¡ÒÃàÃÕ¡㪌¿§˜ ¡ª¹Ñè ÀÒÂã¹â»Ãá¡ÃÁËÅѧ¨Ò¡·Õ»è ÃСÒÈʋǹâ»Ãâµä·»Š (#include<sound.h>) àÃÕºÌÍÂáÅŒÇ sound(700,200);

// ¡íÒà¹Ô´àÊÕ§¤ÇÒÁ¶Õè 700 Hz ¹Ò¹ 200 ÁÔÅÅÔÇ¹Ô Ò·Õ

beep();

// ¡íÒà¹Ô´àÊÕ§¤ÇÒÁ¶Õè 500 Hz ¹Ò¹ 100 ÁÔÅÅÔÇ¹Ô Ò·Õ


40làÃÕ¹ÃÙá Œ ÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ

MCS-51

¡Ô¨¡ÃÃÁ·Õè 4

¢ÑºÊÑ­­Ò³àÊÕ§ÍÍ¡·Ò§ÅíÒ⾧ໂÂâ« A4.1 ແ´â»Ãá¡ÃÁ RIDE áŌÇà¢Õ¹â»Ãá¡ÃÁ·Õè A4-1 ºÑ¹·Ö¡ã¹ª×Íè ä¿Å act0401_sound.c ·íÒ¡ÒäÍÁä¾Å áÅŒÇ ´ÒǹâËÅ´ä¿ÅÊáً ¼§Ç§¨Ã RBX-51AC2 ¢Í§Ëع‹ ¹µ Robo-51 ´ŒÇÂâ»Ãá¡ÃÁ Flip

â»Ãá¡ÃÁ·Õè A4-1 â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺ¢Ñº LED áÅСíÒà¹Ô´ÊÑ­­Ò³àÊÕ§¢Í§Ëع‹ ¹µ Robo-51 (Áյ͋ ) /*—————————————————————————————————————*/ // Program : LED and sound // Description : Drive LED P3.5 , P3.6 and P3.7 by delay interval 2 second and // generate sound when change LED // Filename : act0401_sound.c // C compiler : RIDE 51 V6.1 /*—————————————————————————————————————*/ #include <C51ac2.h> // Declare T89C51AC2’s register #include <delay.h> // Declare Delay library #include <sound.h> // Declare Sound generator library sbit led1 = P3^5; // Define led1 to P3.5 sbit led2 = P3^6; // Define led2 to P3.6 sbit led3 = P3^7; // Define led3 to P3.7 void main() { while(1) { sound(1000,400); led1 = 0; delay_ms(2000);

// // // // //

Generate sound at 1000Hz for 0.4 second Turn-on LED at P3.5 Delay 2 seconds (call function from Delay library) Turn-off LED at P3.5

led2 = 1;

// // // // //

Generate sound at 800Hz for 0.2 second Turn-on LED at P3.6 Delay 2 seconds (call function from Delay library) Turn-off LED at P3.6

beep(); led3 = 0; delay_100us(20000); led3 = 1;

// // // //

Generate “beep” signal at 500Hz for 0.1s Turn-on LED at P3.7 Delay 2 seconds Turn-off LED at P3.7

led1 = 1; sound(800,200); led2 = 0; delay_100us(20000);

} }

// Endless loop


àÃÕ¹ÃÙጠÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ MCS-51 l 41

â»Ãá¡ÃÁ·Õè A4-1 â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺ¢Ñº LED áÅСíÒà¹Ô´ÊÑ­­Ò³àÊÕ§¢Í§Ëع‹ ¹µ Robo-51 (¨º) ¤íÒ͸ԺÒÂà¾ÔÁè àµÔÁ ÊíÒËÃѺâ»Ãá¡ÃÁ¹Õ´é ´Ñ á»Å§à¾ÔÁè àµÔÁ¨Ò¡µÑÇÍ‹ҧä¿Å act0301_led.c ã¹â»Ãá¡ÃÁ·Õè A3-1 â´Â¤Çº¤ØÁ ãˌ LED µÔ´¤Ãѧé ÅÐ 1 ´Ç§àÃÕ§ÅíҴѺ¨Ò¡ LED ·Õµè ͋ ¡Ñº P3.5, P3.6 áÅÐ P3.7 áŌÇǹ·íÒ§Ò¹ â´Â LED ᵋÅÐ ´Ç§¨ÐµÔ´¹Ò¹ 2 ÇÔ¹Ò·Õ Êèǹ·Õàè ¾ÔÁè àµÔÁ¤×Í â»Ãá¡ÃÁ¡Óà¹Ô´ÊÑ­­Ò³àÊÕ§·Õàè ¡Ô´¢Ö¹é ã¹¢³Ð·Õè LED µÔ´ ÊÑ­­Ò³àÊÕ§·Õ´è §Ñ ¢Ö¹é ¾ÃŒÍÁ¡Ñº¡ÒÃáÊ´§¼Å¢Í§ LED ᵋÅдǧ¨ÐäÁ‹à·‹Ò¡Ñ¹ â´Â¿˜§¡ª¹Ñè ·Õàè ÃÕ¡㪌à¾×Íè ¢ÑºÊÑ­­Ò³àÊÕ§ ã¹â»Ãá¡ÃÁÁÕ·Ñé§ sound áÅÐ beep â´Â¡ÒâѺÊÑ­­Ò³àÊÕ§¨Ò¡¿˜§¡ªÑè¹·Ñé§Êͧ¹Ñ鹨Ð㪌䴌¡çµ‹ÍàÁ×èÍÁÕ¡Òà »ÃСÒÈâ»Ãâµä·»Š¢Í§äźÃÒÃÕ sound.h àÊÕ¡‹Í¹ ÁÔઋ¹¹Ñ¹é ¨Ðà¡Ô´¤ÇÒÁ¼Ô´¾ÅÒ´ÃÐËNjҧ¤ÍÁä¾Å A4.2 Ãѹâ»Ãá¡ÃÁ ¨ÐàËç¹ LED µÔ´äŋ໚¹´Ç§æ ¨Ò¡ LED ¢Í§¢Ò P3.5, P3.6 áÅÐ P3.7 àÁ×Íè LED µÔ´¨Ðä´ŒÂ¹Ô àÊÕ§´Ñ§ÍÍ¡ ·Ò§ÅíÒ⾧ໂÂâ« â´Â¤ÇÒÁ¶Õ¢è ͧÊÑ­­Ò³àÊÕ§¨Ðà»ÅÕÂè ¹ä»µÒÁ¡ÒõԴ¢Í§ LED ¶ŒÒ LED ·Õè P3.5 µÔ´¨Ðä´ŒÂ¹Ô àÊÕ§¤ÇÒÁ¶Õè 1kHz ¶ŒÒ LED ·Õè P3.6 µÔ´¨Ðä´ŒÂ¹Ô àÊÕ§¤ÇÒÁ¶Õè 800Hz ¶ŒÒ LED ·Õè P3.7 µÔ´¨Ðä´ŒÂ¹Ô àÊÕ§¤ÇÒÁ¶Õè 500Hz

Robo-51

3.3 ¡ÒÃáÊ´§¼Å´ŒÇÂâÁ´ÙÅ LCD ã¹ËÑÇ¢ŒÍ¹ÕÍé ¸ÔºÒ¶֧¡ÒÃà¢Õ¹â»Ãá¡ÃÁà¾×Íè ¹íÒ¢ŒÍ¤ÇÒÁä»áÊ´§¼Å·Õâè Á´ÙÅ LCD º¹á¼§Ç§¨Ã RBX-51AC2 plus ¢Í§Ëع‹ ¹µ Robo-51 ÃÇÁ·Ñ§é ¡ÒÃÊÌҧ¿˜§¡ª¹Ñè áÊ´§¼Å·Õâè Á´ÙÅ LCD áŌǨѴà¡çº ໚¹äźÃÒÃÕà¾×èͤÇÒÁÊдǡ㹡ÒþѲ¹Òâ»Ãá¡ÃÁ

3.3.1 ¢Ñ¹é µÍ¹¡ÒõԴµ‹Í¡ÑºâÁ´ÙÅ LCD Ẻ 4 ºÔµ ã¹àº×Íé §µŒ¹¡‹Í¹¡ÒÃ㪌§Ò¹âÁ´ÙÅ LCD µŒÍ§Ê‹§¤íÒÊÑ§è ¡íÒ˹´ÃٻẺ¡ÒõԴµ‹Í໚¹áºº 4 ºÔµ 㹡ÒõԴµ‹ÍẺ 4 ºÔµ¢ŒÍÁÙŹÕÁé àÕ ·¤¹Ô¤·Õµè ͌ §¡ÃзíÒ¡‹Í¹àÊÁͤ×Í Ê‹§¢ŒÍÁÙÅ 3H ä»ãˌâÁ´ÙÅ LCD 2 ¤ÃÑ§é µ‹Í´ŒÇ¢ŒÍÁÙÅ 2H ä»ÍÕ¡¤Ãѧé à¾Õ§෋ҹÕâé Á´ÙÅ LCD ¡ç¾ÃŒÍÁÊíÒËÃѺ¡ÒõԴµ‹Íã¹âËÁ´ 4 ºÔµáÅŒÇ ÊÒà˵طµÕè ͌ §Ê‹§¢ŒÍÁÙÅ 3H 仡‹Í¹ 2 ¤Ãѧé à¾×Íè ໚¹¡Òâ¨Ñ´Ê¶Ò¹Ð¢Í§ÊÑ­­Ò³áÅТŒÍÁÙŤíÒÊÑ§è ·ÕÊè §‹ ä»ÂѧâÁ´ÙÅ LCD ¡‹Í¹Ë¹ŒÒ¹Õé «Ö§è ÍÒ¨ÍÂÙ㋠¹âËÁ´ 8 ºÔµ ËÅѧ¨Ò¡¹Ñ¹é ʋ§¢ŒÍÁÙÅ 2H à¾×Íè ໚¹¡Òà àµÃÕÂÁãˌâÁ´ÙÅ LCD ࢌÒÊÙ¡‹ Ò÷íÒ§Ò¹ã¹âËÁ´ 4 ºÔµ


42làÃÕ¹ÃÙá Œ ÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ

MCS-51

+5V

R10 1k

11

42

C19 0.1/50V

EA

1

IC5 T89C51AC2

43 +5V

+5V JP1

RN1/3, RN1/4

P0.2 P0.3

32

4

33

6 +5V

RN1/8

P0.7 P0.6 P0.5

37

RS E

DSP1 LCD 16x2 (back light option)

RN1/5

BLA +V Vo

D7 D6 D5 D4 D3 D2 D1 D0 R/W 14 13 12 11 10 9 8 7 5

16

R35 47

BACK LIGHT ON

2 3

BLK GND 15 1

VR2 10k

BRIGHTNESS

36 35 35

40 C15 33pF

XTAL1 11.0592MHz

41 C16 33pF

ÃÙ»·Õè 3-5 ǧ¨Ãàª×Íè Áµ‹ÍâÁ´ÙÅ LCD Ẻ 4 ºÔµ¢Í§á¼§Ç§¨Ã RBX-51AC2plus ã¹Ëع‹ ¹µ Robo-51

3.3.2 ÊÌҧä¿ÅäźÃÒÃբͧ¿˜§¡ª¹Ñè ¤Çº¤ØÁ¡ÒÃáÊ´§¼ÅâÁ´ÙÅ LCD µÑ駪×èÍä¿ÅäźÃÒÃÕ¹ÕéÇ‹Ò lcd_robo51.h ÁÕÃÒÂÅÐàÍÕ´¢Í§â»Ãá¡ÃÁáÊ´§ã¹â»Ãá¡ÃÁ·Õè P3-3 â´Âãˌ¾ÁÔ ¾áÅкѹ·Ö¡à»š¹ä¿Å¹ÒÁÊ¡ØÅ .h áŌǨѴà¡çºãˌÍÂÙ·‹ âÕè ¿Åà´ÍÏà´ÕÂǡѺä¿Åâ»Ãà¨ç¡µ·¡Õè Òí Åѧ ¾Ñ²¹ÒËÃ×Í·Õè C:\RIDE\INC


àÃÕ¹ÃÙጠÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ MCS-51 l 43

¤íÒÊÑ§è ¤Çº¤ØÁâÁ´ÙÅ LCD ẺÁҵðҹ 1. ¤íÒÊѧè à¤ÅÕÂϨÍáÊ´§¼Å Áբ͌ ÁÙŤíÒÊѧè ໚¹ $1 ໚¹¤íÒÊѧè à¢Õ¹¢ŒÍÁÙŪ‹Í§Ç‹Ò§ËÃ×Í space ࢌÒä»ã¹Ë¹‹Ç ¤ÇÒÁ¨íÒ¢ŒÍÁÙÅÊíÒËÃѺáÊ´§¼ÅËÃ×Í DDRAM ÀÒÂã¹âÁ´ÙÅ LCD àÁ×Íè à¤ÅÕÂÏ¨Í áÊ´§¼ÅáÅŒÇ ¨Ð¡íÒ˹´ãˌà¤ÍÏà«ÍÏ (cursor : ÊÑ­Åѡɳ¾àÔ ÈÉ·Õãè ªŒã¹¡ÒÃáÊ´§ µíÒá˹‹§ÊíÒËÃѺáÊ´§µÑÇÍÑ¡ÉâͧâÁ´ÙÅ LCD ºÒ§¤Ãѧé ÁÕÅ¡Ñ É³Ð໚¹ÃÙ»ÊÕàè ËÅÕÂè Á ¼×¹¼ŒÒ¢¹Ò´ 5x 7 ¨Ø´ËÃ×Í໚¹¢Õ´ ÊÒÁÒöÊѧè ãˌ¡Ð¾ÃÔºËÃ×ÍäÁ‹¡äç ´Œ) ¡ÅѺä»ÍÂÙ·‹ Õè µíÒá˹‹§«ŒÒÂÁ×ÍÊØ´¢Í§¨ÍáÊ´§¼Å 2. ¤íÒÊѧè Return Home Áդҋ ¢Í§¢ŒÍÁÙÅ෋ҡѺ $2 ËÃ×Í $3 ¡ç䴌 (á¹Ð¹íÒãˌ㪌 $2) ໚¹¡ÒáíÒ˹´ãˌ à¤ÍÏà«ÍÏä»áÊ´§ÂѧµíÒá˹‹§«ŒÒÂÊØ´¢Í§¨ÍáÊ´§¼Å â´Â¢ŒÍÁÙÅ·Õáè Ê´§ÍÂÙ㋠¹º¹ ¨ÍáÊ´§¼Å¨ÐäÁ‹Á¡Õ ÒÃà»ÅÕÂè ¹á»Å§ 3. ¤íÒÊѧè àÅ×Í¡âËÁ´¡Òû‡Í¹¢ŒÍÁÙÅ (Entry mode set) ÁÕÃٻẺ¤íÒÊÑ觴ѧ¹Õé

ºÔµ 7 ºÔµ 6 ºÔµ 5 ºÔµ 4 ºÔµ 3 ºÔµ 2 ºÔµ 1 ºÔµ 0 0

0

0

0

0

1

I/D

S

ºÔµ I/D 㪌㹡ÒáíÒ˹´Ç‹Ò àÁ×èÍà¢Õ¹ËÃ×Í͋ҹ¢ŒÍÁÙÅáÅŒÇ ·íÒãˌáÍ´à´Ãʢͧ ˹‹Ç¤ÇÒÁ¨íÒáÊ´§¼Å (DDRAM) à¾ÔÁè ËÃ×ÍŴŧ “0” áÍ´à´ÃÊŴŧ 1 áÍ´à´ÃÊ “1” áÍ´à´ÃÊà¾ÔÁè ¢Ö¹é 1 áÍ´à´ÃÊ ºÔµ S 㪌¡Òí ˹´ÅѡɳСÒÃáÊ´§¼Å “0” àÁ×Íè à¡Ô´µÑÇÍÑ¡ÉÃãËÁ‹ à¤ÍÏà«ÍÏàÅ×Íè ¹ä»·Ò§¢ÇÒÁ×Í “1” àÁ×Íè à¡Ô´µÑÇÍÑ¡ÉÃãËÁ‹ à¤ÍÏà«ÍÏÍÂÙ·‹ àÕè ´ÔÁ ÍÑ¡ÉÃàÅ×Íè ¹ä»·Ò§«ŒÒ ·Õè¹ÔÂÁ㪌ÁÒ¡·ÕèÊØ´¤×Í ¢ŒÍÁÙŤíÒÊÑè§ $6 ËÁÒ¶֧ ¡íÒ˹´ãˌàÁ×èÍà¡Ô´¢ŒÍÁÙÅãËÁ‹ à¤ÍÏà«ÍϨÐàÅ×Íè ¹ä»·Ò§¢ÇÒÁ×Í áÅÐáÍ´à´Ãʢͧ˹‹Ç¤ÇÒÁ¨íÒÊíÒËÃѺáÊ´§ ¼Å¨Ðà¾ÔÁè ¢Ö¹é 4. ¤íÒÊÑ§è ¤Çº¤ØÁ¡ÒÃáÊ´§¼Å ÁÕÃٻẺ¤íÒÊÑ觴ѧ¹Õé

5. ¤íÒÊÑ§è ¤Çº¤ØÁ¡ÒÃàÅ×Íè ¹à¤ÍÏà«ÍÏáÅеÑÇÍÑ¡Éà ÁÕÃٻẺ´Ñ§¹Õé ºÔµ 7 ºÔµ 6 ºÔµ 5 ºÔµ 4 ºÔµ 3 ºÔµ 2 ºÔµ 1 ºÔµ 0 0

0

0

1

S/C

R/L

*

*

¡ÒÃàÅ×Íè ¹à¤ÍÏà«ÍÏáÅеÑÇÍÑ¡Éâֹé ÍÂÙ¡‹ ºÑ ¡ÒáíÒ˹´ºÔµ S/C áÅÐ R/ L «Ö§è ÊÒÁÒöÊÃػ䴌´§Ñ ¹Õé S/C R/L ÅѡɳСÒÃàÅ×Íè ¹ ¢ŒÍÁÙŤíÒÊѧè 0 0 àÅ×Íè ¹à¤ÍÏà«ÍÏ价ҧ«ŒÒ $10-$13 0 1 àÅ×è͹à¤ÍÏà«ÍÏ价ҧ¢ÇÒ $14-$17 1 0 àÅ×è͹µÑÇÍÑ¡ÉÃãËÁ‹ä»·Ò§«ŒÒ $18-$1B 1 1 àÅ×è͹µÑÇÍÑ¡ÉÃãËÁ‹ä»·Ò§¢ÇÒ $1C-$1F 6. ¤íÒÊÑ§è ¡íÒ˹´¿˜§¡ª¹Ñè ¡Ò÷íÒ§Ò¹ ໚¹¤íÒÊÑ§è ·ÕÁè ¤Õ ÇÒÁÊíҤѭÁÒ¡·ÕÊè ´Ø ÍÕ¡¤íÒÊѧè ˹֧è ÁÕû٠Ẻ¢Í§¢ŒÍÁÙŤíÒÊÑ§è ´Ñ§¹Õé

ºÔµ 7 ºÔµ 6 ºÔµ 5 ºÔµ 4 ºÔµ 3 ºÔµ 2 ºÔµ 1 ºÔµ 0 0

0

1

DL

N

F

*

*

ºÔµ DL 㪌㹡ÒáíÒ˹´¨íҹǹºÔµã¹¡ÒõԴµ‹Í¡ÑºâÁ´ÙÅ LCD “0” ¡íÒ˹´ãˌ·Òí §Ò¹ã¹âËÁ´ 4 ºÔµ “1” ¡íÒ˹´ãˌ·Òí §Ò¹ã¹âËÁ´ 8 ºÔµ ºÔµ N 㪌¡Òí ˹´¨íҹǹºÃ÷Ѵ·Õµè ͌ §¡ÒÃãˌáÊ´§¼Å “0” áÊ´§¼Å 1 ºÃ÷Ѵ “1” áÊ´§¼Å 2 ºÃ÷ѴËÃ×ÍÁÒ¡¡Ç‹Ò ᵋÊíÒËÃѺâÁ´ÙÅ LCD ¢¹Ò´ 16 µÑÇÍÑ¡Éà 1 ºÃ÷Ѵ·ÕèÁÕ¨íÒ˹‹ÒÂã¹ »ÃÐà·Èä·Â µŒÍ§¡íÒ˹´ãˌºµÔ ¹Õàé »š¹ “1” à¹×Íè §¨Ò¡Ë¹‹Ç¤ÇÒÁ¨íÒÊíÒËÃѺ à¡çº¢ŒÍÁÙÅà¾×Íè ¡ÒÃáÊ´§¼ÅäÁ‹ä´ŒÍÂÙµ‹ ͋ à¹×Íè §¡Ñ¹ ¡Å‹ÒǤ×Í áº‹§à»š¹ 2 ª‹Ç§ ¤×Í ·Õáè Í´à´ÃÊ $00-$07 áÅÐ $40-$47 â´Â·Õáè Í´à´ÃÊ $40 ¨Ð໚¹áÍ´ à´ÃÊàÃÔÁè µŒ¹¢Í§Ë¹‹Ç¤ÇÒÁ¨íÒÊíÒËÃѺ¡ÒÃáÊ´§¼Å㹺Ã÷Ѵ·ÕÊè ͧ¢Í§ ºÔµ 7 ºÔµ 6 ºÔµ 5 ºÔµ 4 ºÔµ 3 ºÔµ 2 ºÔµ 1 ºÔµ 0 âÁ´ÙÅ LCD ·ÕÁè ÁÕ Ò¡¡Ç‹Ò 1 ºÃ÷Ѵ ¨Ö§·íÒãˌàÁ×Íè µŒÍ§¡ÒõԴµ‹Í¡ÑºâÁ´ÙÅ LCD 1 ºÃ÷Ѵ¨Ö§¨íÒ໚¹µŒÍ§¡íÒ˹´ãˌ໚¹ “1” 0 0 0 0 1 D C B ºÔµ F 㪌àÅ×Í¡¤ÇÒÁÅÐàÍÕ´¢Í§µÑÇÍÑ¡ÉÃ㹡ÒÃáÊ´§¼Å ºÔµ D 㪌¤Çº¤ØÁ¡ÒÃແ´»´¨ÍáÊ´§¼Å ¶ŒÒ໚¹ “0” ໚¹¡Òû´¨ÍáÊ´§ “0” áÊ´§¼ÅẺ 5x7 ¨Ø´ ¼Å ¶ŒÒ໚¹ “1” ໚¹¡ÒÃແ´¨ÍáÊ´§¼Å “1” áÊ´§¼ÅẺ 5x10 ¨Ø´ ºÔµ C 㪌¤Çº¤ØÁ¡ÒÃáÊ´§µÑÇà¤ÍÏà«ÍϺ¹¨ÍáÊ´§¼Å ¶ŒÒ໚¹ “0” ໚¹ ¡Òû´µÑÇà¤ÍÏà«ÍÏËÃ×ÍäÁ‹áÊ´§à¤ÍÏà«ÍÏ ¶ŒÒ໚¹ “1” ໚¹¡ÒÃáÊ´§µÑÇ à¤ÍÏà«ÍÏ ºÔµ B 㪌¤Çº¤ØÁ¡ÒáоÃÔº¢Í§à¤ÍÏà«ÍÏ ¶ŒÒµŒÍ§¡ÒÃãˌà¤ÍÏà«ÍÏ¡Ð ¾ÃÔº µŒÍ§¡íÒ˹´ãˌ໚¹ “1” ¤íÒÊÑ觷Õè㪌º‹Í¤×Í $0C ໚¹¡ÒÃÊÑè§ãˌແ´¨ÍáÊ´§¼Å ᵋäÁ‹áÊ´§ à¤ÍÏà«ÍÏ áÅÐ $0F ໚¹¡ÒÃÊѧè ãˌແ´¨ÍáÊ´§¼Å áÊ´§à¤ÍÏà«ÍÏ áÅÐ Êѧè ãˌà¤ÍÏà«ÍϡоÃÔº


44làÃÕ¹ÃÙá Œ ÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ

MCS-51

â»Ãá¡ÃÁ·Õè P3-3 ä¿ÅäźÃÒÃÕ lcd_robo51.h ·ÕÊè Ìҧ¢Ö¹é ÊíÒËÃѺ¤Çº¤ØÁ¡ÒÃáÊ´§¼Å¢Í§âÁ´ÙÅ LCD ¢Í§Ëع‹ ¹µ Robo-51 (Áյ͋ ) /*—————————————————————————————————————*/ // Program : Module function control LCD display // Description : Call function control LCD display group type 4 bit // Filename : lcd_robo51.h // C compiler : RIDE 51 V6.1 /*—————————————————————————————————————*/ #define line1 0x80 // Define constant #define line2 0xC0 // Define constant sbit rs = P0^2; // Bit control command for LCD sbit e = P0^3; // Bit control pulse command for LCD enum{Hex,Dec}; // Format display integer /*****************************************************************************/ /***************** Function delay 1 ms per unit ******************************/ /*****************************************************************************/ void lcd_delay(unsigned int ms) { unsigned int x,a; // Keep for counter loop for(x=0;x<ms;x++) { for(a=0;a<878;a++); // Loop for delay 1 mË per unit } } /*****************************************************************************/ /***************** Function send command to LCD format 4 Bit *****************/ /*****************************************************************************/ void lcd_command(unsigned char com) { unsigned char buff; // For Keep command send to LCD buff = com & 0xF0; // Keep 4 bit high byte before send to LCD rs = 0; // Select send command to LCD e = 1; // Start generate pulse clock LCD P0 = (P0 & 0x0F)|buff; // Send data to LCD port lcd_delay(1); // Delay 1 ms e = 0; // Stop generate pulse clock LCD lcd_delay(1); // Delay 1 ms buff = (com & 0x0F)<<4; rs = 0; e = 1; P0 = (P0 & 0x0F)|buff ; lcd_delay(1); e = 0; lcd_delay(1);

// // // // // // //

Keep 4 bit low byte before send to LCD Select send command to LCD start generate pulse clock LCD Send data to LCD port Delay 1 ms Stop generate pulse clock LCD Delay 1 ms

} /*****************************************************************************/ /*********************** Function send data to LCD format 4 Bit **************/ /*****************************************************************************/ void lcd_text(char text) { unsigned char buff; // For Keep data send to LCD buff = text & 0xF0; // Keep 4 bit high byte before send to LCD


àÃÕ¹ÃÙጠÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ MCS-51 l 45

â»Ãá¡ÃÁ·Õè P3-3 ä¿ÅäźÃÒÃÕ lcd_robo51.h ·ÕÊè Ìҧ¢Ö¹é ÊíÒËÃѺ¤Çº¤ØÁ¡ÒÃáÊ´§¼Å¢Í§âÁ´ÙÅ LCD ¢Í§Ëع‹ ¹µ Robo-51 (Áյ͋ ) rs = 1;

// Select send data to LCD

e = 1; P0 = (P0 & 0x0F)|buff ; lcd_delay(1); e = 0; lcd_delay(1);

// // // // //

Start generate pulse clock LCD Send data to LCD port Delay 1 ms Stop generate pulse clock LCD Delay 1 ms

buff = (text & 0x0F)<<4; rs = 1; e = 1; P0 = (P0 & 0x0F)|buff ; lcd_delay(1); e = 0; lcd_delay(1);

// // // // // // //

Keep 4 bit low byte before send to LCD Write Command to LCD start generate pulse clock LCD Send data to LCD port Delay 1 ms Stop generate pulse clock LCD Delay 1 ms

} /*****************************************************************************/ /***************** Function show string message ******************************/ /*****************************************************************************/ void lcd_putstr(unsigned char line,char *p) { lcd_command(0x02); // Set origin address of LCD lcd_command(line); // Set address 00H of LCD while(*p) // Check data pointer = 0? { lcd_text(*p); // Send data to LCD p++; // Increase address 1 time } } /*****************************************************************************/ /***************** Function set initial format LCD 4 Bit ******************/ /*****************************************************************************/ void lcd_init(void) { lcd_delay(500); // Delay 500 ms lcd_command(0x33); // Command set format 4 bit lcd_command(0x32); // Command set format 4 bit lcd_command(0x28); // 2 line ,8 bit display ,5*7 dot lcd_command(0x0C); // Display ON , none cursor lcd_command(0x01); // Clear screen } /*****************************************************************************/ /***************** Function set initial parameter LCD 4 Bit ******************/ /*****************************************************************************/ void inttolcd(unsigned char line_sel,int value,int format) { char index=0,buff[6]; // For keep string send to LCD switch(format) // Select format integer { case Dec : buff[0] = (value/10000) | 0x30;


46làÃÕ¹ÃÙá Œ ÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ

MCS-51

â»Ãá¡ÃÁ·Õè P3-3 ä¿ÅäźÃÒÃÕ lcd_robo51.h ·ÕÊè Ìҧ¢Ö¹é ÊíÒËÃѺ¤Çº¤ØÁ¡ÒÃáÊ´§¼Å¢Í§âÁ´ÙÅ LCD ¢Í§Ëع‹ ¹µ Robo-51 (¨º) // Convert data to ASCII 10000’th buff[1] = ((value%10000)/1000) | 0x30; // Convert data to ascii 1000’th buff[2] = (((value%10000)%1000)/100) | 0x30; // Convert data to ascii 100’th buff[3] = ((((value%10000)%1000)%100)/10) | 0x30; // Convert data to ascii 10’th buff[4] = ((((value%10000)%1000)%100)%10) | 0x30; // Convert data to ascii 1’th buff[5] = 0; // End of data break; // Out block for decimal format case case Hex : buff[0] = (value & 0xF000)>>12; // Filter 4 bit 1’th if(buff[0]<10){buff[0] |= 0x30;} else {buff[0] = (buff[0]-9) | 0x40;} // Convert data to ascii buff[1] = (value & 0x0F00)>>8; // Filter 4 bit 2’th if(buff[1]<10){buff[1] |= 0x30;} else {buff[1] = (buff[1]-9) | 0x40;} // Convert data to ascii buff[2] = (value & 0x00F0)>>4; // Filter 4 bit 3’th if(buff[2]<10){buff[2] |= 0x30;} else {buff[2] = (buff[2]-9) | 0x40;} // Convert data to ascii buff[3] = (value & 0x000F); // Filter 4 bit 4’th if(buff[3]<10){buff[3] |= 0x30;} else {buff[3] = (buff[3]-9) | 0x40;} // Convert data to ascii buff[4] = 0; // End of data break; // Out block for hexadecimal format case default : break; // Out block for default case } if(buff[0]==0x30) // if data 1’th = 0 none display {index = 1;} // Shift to display 2’th if(buff[0]==0x30 && buff[1]==0x30) // if data 1’th = 0 and 2’th = 0 none display {index = 2;} // Shift to display 3’th if(buff[0]==0x30 && buff[1]==0x30 && buff[2]==0x30) // if data 1’th = 0, 2’th = 0 and 3’th = 0 // none display {index = 3;} // Shift to display 4’th if(buff[0]==0x30 && buff[1]==0x30 && buff[2]==0x30 && buff[3]==0x30) // if data 1’th = 0, 2’th = 0, 3’th = 0 and // 4’th = 0 none display {index = 4;} // Shift to display 5’th lcd_putstr(line_sel,&buff[index]); // Send integer to LCD }


àÃÕ¹ÃÙጠÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ MCS-51 l 47

3.3.2 ¤íÒ͸ԺÒ¿˜§¡ª¹Ñè (1) ¿˜§¡ª¹Ñè lcd_init : àµÃÕÂÁ¤ÇÒÁ¾ÃŒÍÁ ÃٻẺ¿˜§¡ªÑè¹ void lcd_init(void) ¾ÒÃÒÁÔàµÍÏ äÁ‹ÁÕ ¡Òä׹¤‹Ò äÁ‹Á¡Õ Òä׹¤‹Ò ÃÒÂÅÐàÍÕ´ ·íÒ˹ŒÒ·Õàè µÃÕÂÁ¤ÇÒÁ¾ÃŒÍÁ㹡Ò÷íÒ§Ò¹¢Í§âÁ´ÙÅ LCD â´Â¡íÒ˹´âËÁ´¡Òà µÔ´µ‹Í໚¹áºº 4 ºÔµ, áÊ´§¼Å 2 ºÃ÷Ѵ, ¤ÇÒÁÅÐàÍÕ´ 5*7 ¨Ø´

µÑÇÍ‹ҧ·Õè 3-3 lcd_init();

// ÍÔ¹àÔ ªÕÂÅâÁ´ÙÅ LCD

(2) ¿˜§¡ª¹Ñè lcd_command : ÃѺ¤íÒÊÑ§è ¤Çº¤ØÁ ÃٻẺ¿˜§¡ªÑè¹ lcd_command(unsigned char com) ¾ÒÃÒÁÔàµÍÏ com ÃѺ¢ŒÍÁÙŤíÒÊÑ§è ¢¹Ò´ 1 亵 ¶‹Ò·ʹä»ÂѧâÁ´ÙÅ LCD ¡Òä׹¤‹Ò äÁ‹Á¡Õ Òä׹¤‹Ò ÃÒÂÅÐàÍÕ´ 㪌ʧ‹ ¤íÒÊÑ§è ¤Çº¤ØÁ¡ÒÃáÊ´§¼Åä»ÂѧâÁ´ÙÅ LCD

µÑÇÍ‹ҧ·Õè 3-3 lcd_command(0x01); lcd_command(0x02); lcd_command(0x80); lcd_command(0xC6);

// à¤ÅÕÂϨÍáÊ´§¼Å¢Í§âÁ´ÙÅ LCD // ¡ÅѺä»àÃÔÁè ·Õµè Òí á˹‹§µÑ§é µŒ¹ (home) // ¡ÃÐâ´´ä»ÂѧáÍ´à´ÃÊ 00H // ¡ÃÐâ´´ä»ÂѧáÍ´à´ÃÊ 46H

(3) ¿˜§¡ª¹Ñè lcd_text : ʋ§µÑÇÍÑ¡ÉÃä»áÊ´§¼Å ÃٻẺ¿˜§¡ªÑè¹ void lcd_text(char text) ¾ÒÃÒÁÔàµÍÏ text ÃѺ¢ŒÍÁÙÅÃËÑÊáÍÊ¡Õé 1 亵 ¶‹Ò·ʹä»ÂѧâÁ´ÙÅ LCD à¾×Íè áÊ´§¢ŒÍ¤ÇÒÁ ¡Òä׹¤‹Ò äÁ‹Á¡Õ Òä׹¤‹Ò ÃÒÂÅÐàÍÕ´ ·íÒ˹ŒÒ·ÕÊè §‹ ÍÑ¡¢ÃÐä»áÊ´§¼ÅÂѧâÁ´ÙÅ LCD ¹Õé

µÑÇÍ‹ҧ·Õè 3-4 lcd_text(‘A’);

// áÊ´§µÑÇÍÑ¡Éà “A” ·Õâè Á´ÙÅ LCD

lcd_text(0x30);

// áÊ´§µÑÇàÅ¢ “0” ·Õâè Á´ÙÅ LCD


48làÃÕ¹ÃÙá Œ ÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ

MCS-51

4. ¿˜§¡ª¹Ñè lcd_putstr : ʋ§ªØ´¢ŒÍ¤ÇÒÁä»áÊ´§¼Å ÃٻẺ¿˜§¡ªÑè¹ void lcd_putstr(unsigned char line,char *p) ¾ÒÃÒÁÔàµÍÏ line ÃѺ¤íÒÊѧè 1 亵 ʋ§ä»ÂѧâÁ´ÙÅ LCD à¾×Íè ¡íÒ˹´áÍ´à´ÃÊàÃÔÁè µŒ¹ãˌ¡ÅØÁ‹ ¢ŒÍ¤ÇÒÁ·Õ¨è Ðʋ§ä» p ໚¹¾Í¹àµÍϪ¢ Õé ͌ ÁÙŢͧÍÐàÏÀÒÂ㹿˜§¡ª¹Ñè à¾×Íè ¨Ð䴌¹Òí ¢ŒÍÁÙÅ·ÕÍè ÐàÏ ªÕÍé ÂÙ¹‹ ÊÕé §‹ ໚¹ÍÑ¡¢ÃдŒÇ¿˜§¡ª¹Ñè lcd_text ÍÕ¡¤Ãѧé Ë¹Ö§è ¡Òä׹¤‹Ò äÁ‹Á¡Õ Òä׹¤‹Ò ÃÒÂÅÐàÍÕ´ ·íÒ˹ŒÒ·ÕÊè §‹ ªØ´¢ŒÍ¤ÇÒÁä»áÊ´§¼ÅÂѧâÁ´ÙÅ LCD

µÑÇÍ‹ҧ·Õè 3-5 lcd_putstr(0x81,”hello world”);

// áÊ´§¢ŒÍ¤ÇÒÁ hello

world ·Õâè Á´ÙÅ LCD

5. ¿˜§¡ª¹Ñè inttolcd : ʋ§ªØ´¢ŒÍÁÙŵÑÇàÅ¢ä»áÊ´§¼Å ÃٻẺ¿˜§¡ªÑè¹ void inttolcd(unsigned char line_sel,int value,int format) ¾ÒÃÒÁÔàµÍÏ line_sel ÃѺ¢ŒÍÁÙŤíÒÊÑ§è ¢¹Ò´ 1 亵 ¶‹Ò·ʹä»ÂѧâÁ´ÙÅ LCD à¾×Íè ¡íÒ˹´ áÍ´à´ÃÊàÃÔÁè µŒ¹ãˌ¡ÅØÁ‹ ¢ŒÍ¤ÇÒÁ·Õ¨è Ðʋ§ä» value ÃѺ¢ŒÍÁÙŨíҹǹàµçÁ·Õµ è ͌ §¡ÒùíÒä»áÊ´§·Õâè Á´ÙÅ LCD format 㪌¡Òí ˹´ÃٻẺ¡ÒÃáÊ´§µÑÇàÅ¢ãˌ໚¹°Ò¹ÊÔºËÃ×Í°Ò¹ÊԺˡ ¶ŒÒ໚¹ Dec ໚¹¡ÒÃàÅ×Í¡ãˌáÊ´§à»š¹àÅ¢°Ò¹ÊÔº ¶ŒÒ¡íÒ˹´à»š¹ Hex ໚¹¡ÒÃàÅ×Í¡ãˌáÊ´§à»š¹àÅ¢°Ò¹ÊԺˡ ¡Òä׹¤‹Ò äÁ‹Á¡Õ Òä׹¤‹Ò ÃÒÂÅÐàÍÕ´ ·íÒ˹ŒÒ·ÕÊè §‹ ¢ŒÍ¤ÇÒÁµÑÇàÅ¢ä»áÊ´§¼ÅÂѧâÁ´ÙÅ LCD

µÑÇÍ‹ҧ·Õè 3-6 inttolcd(0x82,244,Dec); inttolcd(0x82,244,Hex);

// áÊ´§µÑÇàÅ¢ 244 (°Ò¹ÊÔº) ·Õâè Á´ÙÅ LCD â´ÂàÃÔÁè ·Õáè Í´à´ÃÊ 02H // áÊ´§µÑÇàÅ¢ F4 (°Ò¹ÊԺˡ) ·Õâè Á´ÙÅ LCD â´ÂàÃÔÁè ·Õáè Í´à´ÃÊ 02H

µÑÇÍ‹ҧ·Õè 3-7 int number; inttolcd(0xC5,number,Dec);

// »ÃСÒȵÑÇá»Ã number // áÊ´§¤‹Ò¢ŒÍÁÙŢͧ number ẺàÅ¢°Ò¹ÊÔº·Õâè Á´ÙÅ LCD // â´ÂàÃÔÁè ·Õáè Í´à´ÃÊ 45H


àÃÕ¹ÃÙጠÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ MCS-51 l 49

¡Ô¨¡ÃÃÁ·Õè 5

áÊ´§¼Å¼‹Ò¹âÁ´ÙÅ LCD A5.1 ແ´â»Ãá¡ÃÁ RIDE áŌÇà¢Õ¹â»Ãá¡ÃÁ·Õè A5-1 ºÑ¹·Ö¡ã¹ª×Íè ä¿Å act0501.c ·íÒ¡ÒäÍÁä¾Å áŌǴÒǹâËÅ´ ä¿ÅÊáً ¼§Ç§¨Ã RBX-51AC2 ¢Í§Ëع‹ ¹µ Robo-51 ´ŒÇÂâ»Ãá¡ÃÁ Flip A5.2 Ãѹâ»Ãá¡ÃÁ ¨ÐàËç¹ LED µÔ´äŋ໚¹´Ç§æ ¨Ò¡ LED ¢Í§¢Ò P3.5, P3.6 áÅÐ P3.7 ¾ÃŒÍÁ¡Ñ¹¹Ñ¹é ·Õâè Á´ÙÅ LCD ¨ÐáÊ´§ ¢ŒÍ¤ÇÒÁµÒÁ¡ÒõԴ´Ñº¢Í§ LED

â»Ãá¡ÃÁ·Õè A5-1 â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺ·´Åͧ¡ÒÃ㪌§Ò¹áÅÐáÊ´§¼Å¢ŒÍ¤ÇÒÁ·Õâè Á´ÙÅ LCD (Áյ͋ ) /*—————————————————————————————————————*/ // Program : LCD demonstration // Description : Send message to LCD module in 4-bit mode // Filename : act0501.c // C compiler : RIDE 51 V6.1 /*—————————————————————————————————————*/ #include <C51ac2.h> // Declare T89C51AC2’s register #include <delay.h> // Declare Delay library #include <sound.h> // Declare Sound generator library #include <lcd_robo51.h> // Declare LCD library sbit led1 = P3^5; // Define led1 to P3.5 sbit led2 = P3^6; // Define led2 to P3.6 sbit led3 = P3^7; // Define led3 to P3.7 void main() { lcd_init(); lcd_putstr(line1,” lcd_putstr(line2,”

// Hello!”); // RBX-51AC2"); // // //

delay_ms(4000); while(1) { lcd_command(1); lcd_putstr(line1,”

beep(); led1 = 0; delay_ms(2000); led1 = 1; lcd_command(1); lcd_putstr(line1,”

Initial LCD in 4-bit mode Show message “Hello” on upper line of LCD Show message “RBX-51AC2” on LCD Delay 4 seconds Endless loop

// Clear LCD LED P3.5 ON”); // Show message “LED P3.5 ON” on upper line of LCD // Once sound generator // Turn-on LED at P3.5 // Delay 2 seconds // Turn-off LED at P3.5 // Clear LCD LED P3.6 ON”); // Show message “LED P3.6 ON” on upper line of LCD


50làÃÕ¹ÃÙá Œ ÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ

MCS-51

â»Ãá¡ÃÁ·Õè A5-1 â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺ·´Åͧ¡ÒÃ㪌§Ò¹áÅÐáÊ´§¼Å¢ŒÍ¤ÇÒÁ·Õâè Á´ÙÅ LCD (¨º) beep(); led2 = 0; delay_ms(2000); led2 = 1; lcd_command(1); lcd_putstr(line1,” beep(); led3 = 0; delay_ms(2000); led3 = 1;

// // // // // LED // // // // //

Once sound generator Turn-on LED at P3.6 Delay 2 seconds Turn-off LED at P3.6 Clear LCD P3.7 ON”); Show message “ LED P3.7 ON” on upper line Generate beep sound Turn-on LED at P3.7 Delay 2 seconds Turn-off LED at P3.7

} }

Robo-51

3.4 µÔ´µ‹Í¡ÑºË¹‹Ç¤ÇÒÁ¨íÒ¢ŒÍÁÙÅÍÕÍ¾Õ ÃÍÁ

ã¹äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ T89C51AC2 «Ö§è ໚¹ÍØ»¡Ã³ËÅѡ㹡ÒäǺ¤ØÁ¡Ò÷íÒ§Ò¹¢Í§Ëع‹ ¹µ Robo-51 ÁÕ˹‹Ç¤ÇÒÁ¨íÒ¾ÔàÈÉà¾ÔÁè àµÔÁ¤×Í Ë¹‹Ç¤ÇÒÁ¨íÒ¢ŒÍÁÙÅÍÕÍ¾Õ ÃÍÁ ÁÕ¤ÇÒÁ¨Ø 2 ¡ÔâÅ亵 áÅÐ ÊÒÁÒö·íÒ¡ÒÃà¢Õ¹«éÒí Í‹ҧ¹ŒÍ 100,000 ¤Ãѧé â´Âã¹ËÑÇ¢ŒÍ¹Õ¨é Ðá¹Ð¹íÒ¶Ö§¡Ò÷íÒ§Ò¹áÅСÒÃÊÌҧ ¿˜§¡ªÑè¹à¾×èÍ͋ҹáÅÐà¢Õ¹¢ŒÍÁÙÅã¹Ë¹‹Ç¤ÇÒÁ¨íÒ¢ŒÍÁÙÅÍÕÍÕ¾ÃÍÁ¹Õéã¹ÃÙ»¢Í§ä¿ÅäźÃÒÃÕà¾×èÍàÃÕ¡ 㪌§Ò¹ã¹â»Ãá¡ÃÁÀÒÉÒ C

3.4.1 ˹‹Ç¤ÇÒÁ¨íÒÍÕÍ¾Õ ÃÍÁ¤×ÍÍÐäÃ

˹‹Ç¤ÇÒÁ¨íÒÍÕÍ¾Õ ÃÍÁ (EEPROM : Electrical Erasable Programmable Read-Only Memory) ໚¹Ë¹‹Ç¤ÇÒÁ¨íÒẺ˹Ö觷ÕèÊÒÁÒöà¡çºÃÑ¡ÉÒ¢ŒÍÁÙÅ䴌áÁŒÇ‹Ò¨ÐËÂØ´¨‹ÒÂä¿àÅÕé§ãˌ¡ÑºµÑÇÁѹËÃ×Í àÃÕÂ¡Ç‹Ò ¹Í¹-âÇÅÒä·Å (non-volatile) â´Â»¡µÔÊÒÁÒöźáÅÐà¢Õ¹«éÒí 䴌ÁÒ¡¡Ç‹Ò 100,000 Ãͺ¢Ö¹é ä» â´Â˹‹Ç¤ÇÒÁ¨íÒª¹Ô´¹ÕÁé ¡Ñ ¹íÒÁÒ㪌à¡çº¢ŒÍÁÙÅÊíҤѭµ‹Ò§æ ¢Í§Ãкº»ÃÐÁÇżÅ

3.4.2 ˹‹Ç¤ÇÒÁ¨íÒ¢ŒÍÁÙÅÍÕÍ¾Õ ÃÍÁ¢Í§ T89C51AC2 ˹‹Ç¤ÇÒÁ¨íÒ¢ŒÍÁÙÅÍÕÍ¾Õ ÃÍÁäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ T89C51AC2 ÁÕ¤ÇÒÁ¨Ø 2 ¡ÔâÅ亵 â´Â ÁÕáÍ´à´Ãʵѧé ᵋ 0000H ¶Ö§ 07FFH (0x0000 ¶Ö§ 0x07FF ã¹ÀÒÉÒ C) ä´ŒÃºÑ ¡ÒèѴÇÒ§ã¹Ê‹Ç¹¢Í§ â¤Ã§ÊÌҧ˹‹Ç¤ÇÒÁ¨íÒ·Õàè ÃÕÂ¡Ç‹Ò XRAM Íѹ໚¹Ê‹Ç¹¢ÂÒ¨ҡâ¤Ã§ÊÌҧ¢Í§äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ MCS-51 Áҵðҹ ¡ÒÃà¢Õ¹â»Ãá¡ÃÁà¾×Íè ࢌҶ֧˹‹Ç¤ÇÒÁ¨íÒ XRAM ¹Ñ¹é ÊíÒËÃѺÀÒÉÒáÍÊà«ÁºÅÕ¨Ð㪌¤Òí Êѧè MOVX ËÇÁ¡Ñº¾ÍÂàµÍÏ DPTR


àÃÕ¹ÃÙጠÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ MCS-51 l 51

ÊíÒËÃѺÀÒÉÒ C ¨ÐࢌҶ֧áÍ´à´ÃÊã´æ ¢Í§Ë¹‹Ç¤ÇÒÁ¨íÒã¹áººÍÐàϴnj ¤ÕÂà ÇÔô XBYTE[address]

â´Â

address

¤×ÍáÍ´à´ÃÊ·Õµè ͌ §¡ÒÃࢌҶ֧ Áդҋ µÑ§é ᵋ 0x0000 ¶Ö§ 0x7FFF

¤ÕÂà ÇÔô XBYTE ¶Ù¡¹ÔÂÒÁänjÀÒÂã¹äźÃÒÃÕ absacc ·Õµè ´Ô µÑ§é ÁҾÌÍÁ¡Ñº¤ÍÁä¾àÅÍÏàÃÕºÌÍ áÅŒÇ ´Ñ§¹Ñ¹é ¼Ù¾Œ ² Ñ ¹Ò¨ÐµŒÍ§¼¹Ç¡ä¿ÅäźÃÒÃÕ absacc.h änj·ÊÕè Nj ¹ËÑǢͧâ»Ãá¡ÃÁ¡‹Í¹¡ÒÃ㪌§Ò¹ã¹ ʋǹ¹Õé

3.4.3 EECON ÃÕ¨ÊÔ àµÍÏ·¤Õè Ǻ¤ØÁ¡ÒÃ͋ҹ/à¢Õ¹˹‹Ç¤ÇÒÁ¨íÒ¢ŒÍÁÙÅÍÕÍ¾Õ ÃÍÁã¹ T89C51AC2 ÁÕáÍ´à´ÃÊÍÂÙ·‹ Õè 0D2H ËÃ×Í 0xD2 (ã¹ÀÒÉÒ C) ÁÕÃÒÂÅÐàÍÕ´¢Í§ÃÕ¨ÊÔ àµÍÏ´§Ñ ¹Õé

ºÔµ 7

EEPL3

ºÔµ 6

EEPL2

ºÔµ 5

EEPL1

ºÔµ 4

EEPL0

ºÔµ 3 -

ºÔµ 2 -

ºÔµ 1 EEE

ºÔµ 0

EEBUSY

EEPL3-0 (ºÔµ 7-4)

㪌¡Òí ˹´¤íÒÊѧè à¢Õ¹¢ŒÍÁÙÅä»Âѧ˹‹Ç¤ÇÒÁ¨íÒ¢ŒÍÁÙÅÍÕÍ¾Õ ÃÍÁ «Ö§è ÁÕû٠Ẻ ੾ÒÐâ´Â¡ÒÃà¢Õ¹¢ŒÍÁÙÅ 5H (¤Ô´à©¾ÒкԵ EEPL3-EEPL0 à¾Õ§ 4 ºÔµà·‹Ò¹Ñ¹é ) áŌǵÒÁ´ŒÇ AH àÁ×Íè ¾Ô¨ÒóÒẺ 8 ºÔµ¨Ö§µŒÍ§à¢Õ¹¢ŒÍÁÙÅãˌ EECON ໚¹ 5XH áÅÐ AXH â´Â·Õè X ໚¹àÅ¢ÍÐäáç䴌 ã¹·Õ¹è ¡Õé Òí ˹´ãˌ X ໚¹ 0 ¹Ñ¹è ¤×Í àÁ×Íè µŒÍ§¡ÒÃà¢Õ¹¢ŒÍÁÙÅä»Âѧ˹‹Ç¤ÇÒÁ¨íÒ¢ŒÍÁÙÅÍÕÍ¾Õ ÃÍÁ µŒÍ§¡íÒ˹´ãˌ EECON = 0x50 áÅÐ EECON = 0xA0

ºÔµ 3 áÅÐ 2

äÁ‹ãªŒ§Ò¹ ¡íÒ˹´à»š¹ “0”

EEE (ºÔµ 1)

㪌¤Çº¤ØÁ¡ÒõԴµ‹Í¡ÑºË¹‹Ç¤ÇÒÁ¨íÒ¢ŒÍÁÙÅÍÕÍÕ¾ÃÍÁ “0” - äÁ‹µ´Ô µ‹Í¡ÑºË¹‹Ç¤ÇÒÁ¨íÒ¢ŒÍÁÙÅÍÕÍ¾Õ ÃÍÁ “1” - µÔ´µ‹Í¡ÑºË¹‹Ç¤ÇÒÁ¨íÒ¢ŒÍÁÙÅÍÕÍ¾Õ ÃÍÁ (ÊÒÁÒöࢌҶ֧¾×¹é ·ÕËè ¹‹Ç ¤ÇÒÁ¨íÒ XRAM ´ŒÇ¤íÒÊѧè MOVX ã¹ÀÒÉÒáÍÊà«ÁºÅÕ)

EEBUSY (ºÔµ 0)

໚¹ºÔµá¿Å¡áÊ´§Ê¶Ò¹Ð¡Ò÷íÒ§Ò¹¢Í§Ë¹‹Ç¤ÇÒÁ¨íÒ¢ŒÍÁÙÅÍÕÍÕ¾ÃÍÁ ¡íÒ˹´´ŒÇ¡Ãкǹ¡Ò÷ҧÎÒÏ´áÇÏ â´Â “0” - ˹‹Ç¤ÇÒÁ¨íÒ¢ŒÍÁÙÅÍÕÍ¾Õ ÃÍÁ¾ÃŒÍÁÊíÒËÃѺ¡ÒÃà¢Õ¹ËÃ×Í͋ҹ “1” - ˹‹Ç¤ÇÒÁ¨íÒ¢ŒÍÁÙÅÍÕÍ¾Õ ÃÍÁÂѧÍÂÙ㋠¹¡Ãкǹ¡ÒÃà¢Õ¹ËÃ×Í͋ҹ


52làÃÕ¹ÃÙá Œ ÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ

MCS-51

3.4.4 ¡ÒÃà¢Õ¹¢ŒÍÁÙÅä»Âѧ˹‹Ç¤ÇÒÁ¨íÒ¢ŒÍÁÙÅÍÕÍ¾Õ ÃÍÁÀÒÂã¹ T89C51AC2

ÊÒÁÒöÊÃػ໚¹¢Ñ鹵͹䴌´Ñ§¹Õé (1) µÃǨÊͺºÔµ EEBUSY ໚¹ “1” - ¢³Ð¹ÕÂé §Ñ äÁ‹¾ÃŒÍÁà¢Õ¹¢ŒÍÁÙÅŧã¹Ë¹‹Ç¤ÇÒÁ¨íÒ ãˌÃͨ¹¡Ç‹Ò¨Ð¾ÃŒÍÁ ໚¹ “0” - ¢³Ð¹ÕËé ¹‹Ç¤ÇÒÁ¨íÒ¢ŒÍÁÙÅÍÕÍ¾Õ ÃÍÁ¾ÃŒÍÁÊíÒËÃѺ¡ÒÃà¢Õ¹¢ŒÍÁÙÅáÅŒÇ (2) ૵ºÔµ EEE (ºÔµ 1 ¢Í§ÃÕ¨ÊÔ àµÍÏ EECON) (3) à¢Õ¹¢ŒÍÁÙÅŧä»ÂѧáÍ´à´ÃÊ·Õµè ͌ §¡Òôѧµ‹Í仹Õé XBYTE[áÍ´à´ÃÊ]

= ¢ŒÍÁÙÅ ;

(4) à¤ÅÕÂϺµÔ EEE (5) µÃǨÊͺºÔµ EEBUSY ÍÕ¡¤Ãѧé à¾×Íè ãˌṋã¨Ç‹ÒÍÂÙ㋠¹ÊÀÒÇоÌÍÁà¢Õ¹¢ŒÍÁÙŨÃÔ§ (6) áŵª¢ÍŒ ÁÙÅ´ŒÇ¡ÒÃà¢Õ¹¤íÒÊѧè 2 ¤íÒÊÑ§è µ‹Í仹Õé EECON = 0x50; // áŵª¢ÍŒ ÁÙÅ¢Ñ¹é µÍ¹·Õè 1 EECON = 0xA0; // áŵª¢ÍŒ ÁÙÅ¢Ñ¹é µÍ¹·Õè 2 ¨Ò¡¢Ñ鹵͹·ÕèÊÃØ»ÁÒÊÒÁÒö¹íÒà¢Õ¹໚¹¿˜§¡ªÑè¹ÊíÒËÃѺà¢Õ¹¢ŒÍÁÙÅŧã¹Ë¹‹Ç¤ÇÒÁ¨íÒ ¢ŒÍÁÙÅÍÕÍ¾Õ ÃÍÁ 1 亵 ª×Íè eeprom_write_byte â´ÂÃкØáÍ´à´ÃÊŧ㹾ÒÃÒÁÔàµÍÏ addr áÅТŒÍÁÙÅ ·Õµè ͌ §¡ÒÃà¢Õ¹ŧ㹾ÒÃÒÁÔàµÍÏ dat â´ÂÁÕÃÒÂÅÐàÍÕ´áÊ´§ã¹â»Ãá¡ÃÁ·Õè P3-4 â»Ãá¡ÃÁ·Õè P3-4 ÃÒÂÅÐàÍÕ´¢Í§¿˜§¡ª¹Ñè eeprom_write_byte ·Õãè ªŒã¹¡ÒÃà¢Õ¹¢ŒÍÁÙÅ 1 亵Å§ã¹ ˹‹Ç¤ÇÒÁ¨íÒ¢ŒÍÁÙÅÍÕÍ¾Õ ÃÍÁÀÒÂã¹äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ T89C51AC2 void eeprom_write_byte(int addr,unsigned char dat) { while((EECON & 0x01)!=0x00); EECON = 0x02; XBYTE[addr] = dat;

EECON = 0x00; while((EECON & 0x01)!=0x00); EECON = 0x50; EECON = 0xA0; }

// µÃǨÊͺNjҺԵ EEBUSY = “0” ËÃ×ÍäÁ‹ // ¶ŒÒºÔµ EEBUSY ¶Ù¡à¤ÅÕÂÏáÅŒÇ ãˌ૵ºÔµ EEE // ¹íÒ¢ŒÍÁÙÅ dat à¢Õ¹ä»Âѧ˹‹Ç¤ÇÒÁ¨íÒÍÕÍ¾Õ ÃÍÁ // ·Õáè Í´à´ÃÊ«Ö§è ¡íÒ˹´â´Â addr // à¤ÅÕÂϺµÔ EEE // µÃǨÊͺ¡ÒÃà¤ÅÕÂϺµÔ EEBUSY ÍÕ¡¤Ãѧé // áŵª¢ÍŒ ÁÙÅ¢Ñ¹é µÍ¹·Õè 1 // áŵª¢ÍŒ ÁÙÅ¢Ñ¹é µÍ¹·Õè 2


àÃÕ¹ÃÙጠÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ MCS-51 l 53

E

¡ÒùíÒ¿˜§¡ªÑè¹à¢Õ¹áÅÐ͋ҹ¢ŒÍÁÙšѺ˹‹Ç¤ÇÒÁ¨íÒ¢ŒÍÁÙÅÍÕÍÕ¾ÃÍÁÀÒÂã¹äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ T89C51AC2 ¹Õäé »ãªŒ§Ò¹¨ÃÔ§à¾×Íè ãˌ¢¹Ñé µÍ¹¡ÒÃ͋ҹáÅÐà¢Õ¹¢ŒÍÁÙšѺã¹Ë¹‹Ç¤ÇÒÁ¨íÒ໚¹ ä»Í‹ҧÊÁºÙóáÅÐṋã¨ä´ŒÇҋ ã¹ÃÐËNjҧ·ÕÍè ÂÙ㋠¹¡Ãкǹ¡ÒõŒÍ§äÁ‹¶¡Ù ¢Ñ´¨Ñ§ËÇШҡ¡Òà ÍÔ¹àµÍÃÃ»Ñ µ ¨Ö§¤ÇÃà¢Õ¹â»Ãá¡ÃÁ૵ºÔµ EA à¾×Íè ´ÔÊàÍàºÔÅ¡ÒõͺʹͧÍÔ¹àµÍÃÃ»Ñ µ ¡‹Í¹à¢ŒÒÊÙ¡‹ Ò÷íÒ§Ò¹¢Í§¿˜§¡ª¹Ñè ¹Õé â´ÂàÃÕ¡㪌ä´àÃ硵տ #pragma DISABLE

3.4.5 ¡ÒÃ͋ҹ¢ŒÍÁÙŨҡ˹‹Ç¤ÇÒÁ¨íÒ¢ŒÍÁÙÅÍÕÍ¾Õ ÃÍÁã¹ T89C51AC2 ÊÒÁÒöÊÃػ໚¹¢Ñ鹵͹䴌´Ñ§¹Õé (1) µÃǨÊͺºÔµ EEBUSY ໚¹ “1” - ¢³Ð¹ÕÂé §Ñ äÁ‹¾ÃŒÍÁ·Õ¨è Ð͋ҹ¢ŒÍÁÙŨҡ˹‹Ç¤ÇÒÁ¨íÒ ãˌÃͨ¹¡Ç‹Ò¨Ð¾ÃŒÍÁ ໚¹ “0” - áÊ´§Ç‹Ò¢³Ð¹ÕËé ¹‹Ç¤ÇÒÁ¨íÒ¢ŒÍÁÙÅÍÕÍ¾Õ ÃÍÁ¾ÃŒÍÁÊíÒËÃѺ¡ÒÃ͋ҹ¢ŒÍÁÙÅáŌÇ

(2) ૵ºÔµ EEE(ºÔµ 1 ¢Í§ÃÕ¨ÊÔ àµÍÏ EECON) (3) ͋ҹ¢ŒÍÁÙÅÁÒà¡çºänj㹵ÑÇá»Ã (¢ŒÍÁÙŤÇÃ໚¹ª¹Ô´ unsigned char) ·Õ¡è Òí ˹´¢Ö¹é ¨Ò¡áÍ´ à´ÃÊ·Õ赌ͧ¡ÒÃâ´Â¡ÒÃ͌ҧ¤íÒÊÑè§ÃٻẺ´Ñ§µ‹Í仹Õé Variable = XBYTE[addr] ;

(4) à¤ÅÕÂϺµÔ EEE ¨Ò¡¢Ñ鹵͹·ÕèÊÃØ»ÁÒÊÒÁÒö¹íÒà¢Õ¹໚¹¿˜§¡ªÑ蹡ÒÃ͋ҹ¢ŒÍÁÙŨҡ˹‹Ç¤ÇÒÁ¨íÒ¢ŒÍÁÙÅ ÍÕÍ¾Õ ÃÍÁ 1 亵 ª×Íè eeprom_read_byte â´ÂÃкØáÍ´à´ÃÊŧ㹾ÒÃÒÁÔàµÍÏ addr áÅеÑÇá»Ã ÊíÒËÃѺà¡çº¢ŒÍÁÙÅŧ㹾ÒÃÒÁÔàµÍÏ dat â´ÂÁÕÃÒÂÅÐàÍÕ´áÊ´§ã¹â»Ãá¡ÃÁ·Õè P3-5 â»Ãá¡ÃÁ·Õè P3-5 ÃÒÂÅÐàÍÕ´¢Í§¿˜§¡ª¹Ñè eeprom_read_byte ·Õãè ªŒã¹¡ÒÃà¢Õ¹¢ŒÍÁÙÅ 1 亵Å§ã¹ ˹‹Ç¤ÇÒÁ¨íÒ¢ŒÍÁÙÅÍÕÍ¾Õ ÃÍÁÀÒÂã¹äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ T89C51AC2 unsigned char eeprom_read_byte(int addr) {

// »ÃСÒȵÑÇá»Ã dat ÊíÒËÃѺÃѺ¢ŒÍÁÙÅ·ÕÍè ҋ ¹ä´Œ¨Ò¡Ë¹‹Ç¤ÇÒÁ¨íÒ while((EECON & 0x01)!=0x00); // µÃǨÊͺNjҺԵ EEBUSY ໚¹ “0” ËÃ×ÍäÁ‹ EECON = 0x02; // ¶ŒÒºÔµ EEBUSY ໚¹ “0” áÅŒÇ ·íÒ¡ÒÃ૵ºÔµ EEE dat = XBYTE[addr]; // ͋ҹ¤‹Ò¢ŒÍÁÙŨҡáÍ´à´ÃÊ·Õ¡è Òí ˹´ÁÒà¡çºÂѧµÑÇá»Ã dat EECON = 0x00; // à¤ÅÕÂϺµÔ EEE return(dat); // ¤×¹¤‹Ò¢ŒÍÁÙÅ·ÕÍè ҋ ¹ä´Œ¨Ò¡Ë¹‹Ç¤ÇÒÁ¨íÒ¢ŒÍÁÙÅÍÕÍ¾Õ ÃÍÁ unsigned char dat=0;

}


54làÃÕ¹ÃÙá Œ ÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ

MCS-51

3.4.6 ÊÌҧä¿ÅäźÃÒÃÕÊÒí ËÃѺà¢Õ¹áÅÐ͋ҹ¢ŒÍÁÙšѺ˹‹Ç¤ÇÒÁ¨íÒ¢ŒÍÁÙÅ ÍÕÍ¾Õ ÃÍÁã¹ T89C51AC2

¨Ò¡¿˜ § ¡ ªÑè ¹ ¡ÒÃà¢Õ  ¹áÅÐ͋ Ò ¹¢Œ Í ÁÙ Å ¡Ñ º ˹‹ Ç Â¤ÇÒÁ¨í Ò ¢Œ Í ÁÙ Å ÍÕ ÍÕ ¾ ÃÍÁ·Õè ä ´Œ Í ¸Ô º ÒÂä» ¡‹Í¹Ë¹ŒÒ¹Õé ÊÒÁÒö¹íÒÁÒ¼¹Ç¡à¢ŒÒ´ŒÇ¡ѹà¾×Íè ÊÌҧä¿ÅäźÃÒÃÕªÍ×è eeprom.h â´ÂÁÕÃÒÂÅÐàÍÕ´¢Í§ ä¿ÅáÊ´§ã¹â»Ãá¡ÃÁ·Õè P3-6 ¨Ñ´à¡çºä¿ÅäźÃÒÃÕ¹ÕéãˌÍÂً·Õèâ¿Åà´ÍÏà´ÕÂǡѺä¿Åâ»Ãà¨ç¡µ·Õè¡íÒÅѧ ¾Ñ²¹ÒËÃ×Í·Õè C:\RIDE\INC

â»Ãá¡ÃÁ·Õè P3-6 ä¿ÅäźÃÒÃÕ eeprom.h ·ÕÊè Ìҧ¢Ö¹é ÊíÒËÃѺà¢Õ¹áÅÐ͋ҹ¢ŒÍÁÙšѺ˹‹Ç¤ÇÒÁ¨íÒ¢ŒÍÁÙÅ ÍÕÍ¾Õ ÃÍÁã¹äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ T89C51AC2 Íѹ໚¹ÍØ»¡Ã³¤Çº¤ØÁËÅÑ¡¢Í§Ëع‹ ¹µ Robo-51 /*—————————————————————————————————————*/ // Program : write/read byte data EEPROM // Description : write/read byte data EEPROM of T89C51ac2 // Filename : eeprom.h // C compiler : RIDE 51 V6.1 /*—————————————————————————————————————*/ #include <absacc.h> // Include file for memory access #pragma DISABLE void eeprom_write_byte(unsigned int addr,unsigned char dat) { while((EECON & 0x01)!=0x00); // Check bit EEBUSY = 0? EECON = 0x02; // Set bit EEE of EECON register XBYTE[addr] = dat; // Write data to EEPROM(access to XRAM) EECON = 0x00; // Clear bit EEE while((EECON & 0x01)!=0x00); // Check bit EEBUSY = 0? again EECON = 0x50; // latch command step 1 EECON = 0xA0; // latch command step 2 } #pragma DISABLE unsigned char eeprom_read_byte(unsigned int addr) { unsigned char dat=0; // For keep data from EEPROM while((EECON & 0x01)!=0x00); // Check bit EEBUSY = 0? EECON = 0x02; // Set bit EEE of EECON register dat = XBYTE[addr]; // Read data from EEPROM(access to XRAM) EECON = 0x00; // Clear bit EEE return(dat); // Return data }


àÃÕ¹ÃÙጠÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ MCS-51 l 55

3.4.6.1 ¤íÒ͸ԺÒ¿˜§¡ª¹Ñè ã¹äźÃÒÃÕ eeprom.h (1) ¿˜§¡ª¹Ñè eeprom_write_byte ÃٻẺ¿˜§¡ªÑè¹ void eeprom_write_byte(unsigned int addr,unsigned char dat) ¾ÒÃÒÁÔàµÍÏ addr ¡íÒ˹´áÍ´à´ÃÊ·Õµè ͌ §¡ÒÃà¢Õ¹¢ŒÍÁÙÅ Áդҋ µÑ§é ᵋ 0x000 - 0x07FF dat ·íÒ˹ŒÒÃѺ¤‹Ò¢ŒÍÁÙÅ·Õµ è ͌ §¡ÒÃà¢Õ¹ŧÂѧáÍ´ÃÊ·ÕÃè кØâ´Â addr ÃÒÂÅÐàÍÕ´ ໚¹¿˜§¡ª¹Ñè à¢Õ¹¢ŒÍÁÙÅŧä»Âѧ˹‹Ç¤ÇÒÁ¨íÒ¢ŒÍÁÙÅÍÕÍ¾Õ ÃÍÁ (2) ¿˜§¡ª¹Ñè eeprom_read_byte ÃٻẺ¿˜§¡ªÑè¹ unsigned char eeprom_read_byte(unsigned int addr) ¾ÒÃÒÁÔàµÍÏ addr ¡íÒ˹´áÍ´à´ÃÊ·Õµè ͌ §¡ÒÃ͋ҹ¢ŒÍÁÙÅ Áդҋ µÑ§é ᵋ 0x000 - 0x07FF ÃÒÂÅÐàÍÕ´ ໚¹¿˜§¡ª¹Ñè ÊíÒËÃѺ͋ҹ¢ŒÍÁÙŨҡ˹‹Ç¤ÇÒÁ¨íÒ¢ŒÍÁÙÅÍÕÍ¾Õ ÃÍÁ

µÑÇÍ‹ҧ·Õè 3-8 ¡ÒÃàÃÕ¡㪌¿§˜ ¡ª¹Ñè ÀÒÂã¹â»Ãá¡ÃÁËÅѧ¨Ò¡·Õ»è ÃСÒÈʋǹâ»Ãâµä·»Š #include<eeprom.h> àÃÕºÌÍÂáÅŒÇ eeprom_write_byte(0x03A,0x25); // à¢Õ¹¢ŒÍÁÙÅ 0x25 ŧä»ÂѧáÍ´à´ÃÊ 0x03A ¢Í§ EEPROM a = eeprom_read_byte(0x145); // ͋ҹ¢ŒÍÁÙŨҡáÍ´à´ÃÊ 0x145 ÁÒà¡çºÂѧµÑÇá»Ã a

¡Ô¨¡ÃÃÁ·Õè 6

·´Êͺ¡ÒÃà¢Õ¹áÅÐ͋ҹ¢ŒÍÁÙÅã¹Ë¹‹Ç ¤ÇÒÁ¨íÒ¢ŒÍÁÙÅÍÕÍ¾Õ ÃÍÁÀÒÂã¹ T89C51AC2 A6.1 ແ´â»Ãá¡ÃÁ RIDE áŌÇà¢Õ¹â»Ãá¡ÃÁ·Õè A6-1 ºÑ¹·Ö¡ã¹ª×Íè ä¿Å act0601.c ·íÒ¡ÒäÍÁä¾Å áŌǴÒǹâËÅ´ ä¿ÅÊáً ¼§Ç§¨Ã RBX-51AC2 ¢Í§Ëع‹ ¹µ Robo-51 ´ŒÇÂâ»Ãá¡ÃÁ Flip A6.2 Ãѹâ»Ãá¡ÃÁ ·Õâè Á´ÙÅ LCD ¨ÐáÊ´§¼Å¡Ò÷íÒ§Ò¹´Ñ§¹Õé )@@HAII NN ,=J= OO â´Â¤‹Ò¢Í§ xx ·Õºè Ã÷Ѵº¹ áÅÐ yy ·Õºè Ã÷Ѵŋҧ¨ÐÁդҋ µÃ§¡Ñ¹ à¹×Íè §¨Ò¡¢ŒÍÁÙÅ·Õàè ¢Õ¹ (Data) ¡ç¤Í× ¤‹ÒáÍ´à´ÃÊ (Address) ¹Ñ¹è àͧ ÊíÒËÃѺ¡ÒÃáÊ´§¼Å¢ŒÍÁÙŢͧᵋÅÐáÍ´à´ÃʨÐáÊ´§¹Ò¹»ÃÐÁÒ³ 2 ÇÔ¹Ò·Õ ËÅѧ¨Ò¡¹Ñ¹é ¡ç¨ÐáÊ´§¼Å¤‹Ò¢ŒÍÁÙŢͧáÍ´à´ÃÊ¶Ñ´ä» â´Â¡ÒÃà¾ÔÁè ¤‹ÒáÍ´à´ÃÊ ¾ÃŒÍÁ¡ÑºÁÕàÊÕ§ÊÑ­­Ò³´Ñ§¢Ö¹é 1 ¤ÃÑ§é ¨ÐáÊ´§·Ñ§é ÊÔ¹é 256 µíÒá˹‹§¨¹¶Ö§¤‹Ò 0xFF


56làÃÕ¹ÃÙá Œ ÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ

MCS-51

â»Ãá¡ÃÁ·Õè A6-1 â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺ·´Åͧ¡ÒÃ͋ҹáÅÐà¢Õ¹¢ŒÍÁÙÅã¹Ë¹‹Ç¤ÇÒÁ¨íÒÍÕÍ¾Õ ÃÍÁ /*—————————————————————————————————————*/ // Program : write/read byte data EEPROM // Description : write/read byte data EEPROM of T89C51ac2 // Filename : act0601.c // C compiler : RIDE 51 V6.1 /*—————————————————————————————————————*/ #include <C51ac2.h> // Declare T89C51AC2’s register #include <delay.h> // Declare Delay library #include <eeprom.h> // Declare EEPROM library #include <lcd_robo51.h> // Declare LCD library #include <sound.h> // Declare Sound library void main() { int i; // Define counter variable lcd_init(); // Call initial LCD function lcd_putstr(line1,” Wait for write”); // Display message in EEPROM writing process lcd_putstr(line2,” data to EEPROM”); for(i=0;i<=0xFF;i++) // Write EEPROM data loop { eeprom_write_byte(i,i); // Write data to EEPROM by address value } lcd_clear(); // Clear LCD display after writing finish lcd_putstr(line1,” Display data”); // Show message about reading verification lcd_putstr(line1,” from EEPROM”); for(i=0;i<=0xFF;i++) // Reading loop from EEPROM address 0 to 0xFF { // Clear LCD display for preparing display address and data lcd_clear(); lcd_putstr(line1,”Address”); // Show “Address” message on line 1 of LCD // Show address value next to “Address” message inttolcd(0x89,i,Hex); lcd_putstr(line2,”Data”); // Show “Data” message on line 2 of LCD inttolcd(0xC9,eeprom_read_byte(i),Hex); // Show data value next to “Data” message delay_ms(2000); // Delay 2 seconds beep(); // Beep 1 time } while(1); // Break program }

¤íÒ͸ԺÒÂà¾ÔÁè àµÔÁ â»Ãá¡ÃÁ¹Õàé »š¹¡Ò÷´Êͺà¢Õ¹áÅÐ͋ҹ¢ŒÍÁÙšѺ˹‹Ç¤ÇÒÁ¨íÒ¢ŒÍÁÙÅÍÕÍ¾Õ ÃÍÁ¢Í§ T89C51AC2 àÃÔÁè µŒ¹´ŒÇ¡ÒÃà¢Õ¹¢ŒÍÁÙÅŧä»Âѧ˹‹Ç¤ÇÒÁ¨íÒ àÃÔÁè µÑ§é ᵋáÍ´à´ÃÊ 0x00 ¶Ö§ 0xFF â´Â¢ŒÍÁÙÅ·Õàè ¢Õ¹¨Ð໚¹¤‹Ò à´ÕÂǡѺ¤‹ÒáÍ´à´ÃÊ ËÅѧ¨Ò¡¹Ñé¹â»Ãá¡ÃÁ¡ç¨ÐǹáÊ´§¼Å¤‹ÒáÍ´à´ÃÊ·ÕèºÃ÷Ѵº¹¢Í§âÁ´ÙÅ LCD áÅФ‹Ò ¢ŒÍÁÙÅ·Õºè Ã÷Ѵŋҧ àÁ×Íè áÊ´§¼Å¤Ãº¨¹¶Ö§áÍ´à´ÃÊ 0xFF áŌǡç¨Ð¶Ù¡àºÃ¡â»Ãá¡ÃÁ´ŒÇ¤íÒÊѧè while(1); «Ö§è ໚¹¤íÒÊѧè ÊØ´·ŒÒ¢ͧâ»Ãá¡ÃÁ¹Õé Robo-51


àÃÕ¹ÃÙጠÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ MCS-51 l 57

3.5 ¢ÑºÁÍàµÍÏ俵ç ËÑÇã¨ÊíҤѭ㹡Ò÷íÒ§Ò¹¢Í§Ëع‹ ¹µ¤Í× Ãкº¢Ñºà¤Å×Íè ¹ ã¹Ëع‹ ¹µ Robo-51 㪌ÁÍàµÍÃä¿ µÃ§·ÕÁè ¡Õ Ò÷´¤ÇÒÁàÃçÇáÅÐà¾ÔÁè áç¢Ñº´ŒÇªش࿄ͧ 2 µÑÇ ¼¹Ç¡à¢ŒÒ¡ÑºÅŒÍÊÒ¾ҹ ·íÒãˌÊÒÁÒö¢Ñº à¤Å×Íè ¹Ëع‹ ¹µ Robo-51 䴌Í‹ҧÁÑ¹è ¤§áÅÐÁÕàʶÕÂÃÀÒ¾ ¡ÒâѺÁÍàµÍÏ俵çã¹Ë؋¹Â¹µ Robo-51 㪌¡ÒâѺ´ŒÇÂÊÑ­­Ò³ PWM (Pulse width modulation) ·íÒãˌÊÒÁÒö¤Çº¤ØÁ¡íÒÅѧ俿‡Ò·Õ訋ÒÂãˌ¡ÑºÁÍàµÍÏ´ŒÇ¡Ãкǹ¡Ò÷ҧ«Í¿µáÇÏ ËÃ×ÍÍÕ¡¹ÑÂ˹Ö觤×Í ¤Çº¤ØÁÍѵÃÒàÃçÇ㹡ÒÃËÁع¢Í§ÁÍàµÍϨҡâ»Ãá¡ÃÁ·Õè䴌¡íÒ˹´änj ÃÇÁ¶Ö§ ÊÒÁÒö¤Çº¤ØÁ·ÔÈ·Ò§¡ÒÃËÁع¢Í§ÁÍàµÍÏ䴌´ŒÇÂ

3.5.1 ¢ÑºÁÍàµÍÏ俵紌ÇÂÊÑ­­Ò³ PWM ¡ÒâѺÁÍàµÍϴnj ÂÊÑ­­Ò³ PWM ¤×Í ¡Òè‹ÒÂÊÑ­­Ò³¾ÑÅʏ·ÊÕè ÅѺ¡Ñ¹ÃÐËNjҧª‹Ç§àÇÅÒ ON áÅÐ OFF ¢Í§ÁÍàµÍÏ ¨Ø´»ÃÐʧ¤¡çà¾×èÍÅ´·Í¹¡íÒÅѧ俿‡Ò·Õ訋ÒÂãˌ¡ÑºµÑÇÁÍàµÍÏ áÅÐàÁ×èÍ¡íÒÅѧ ä¿¿‡Ò·ÕèÁÍàµÍÏ䴌ÃѺÁÕ¤‹Ò¹ŒÍÂŧ‹ÍÁʋ§¼ÅãˌÁÍàµÍÏËÁعªŒÒŧ仴ŒÇ ¨Ò¡ÃÙ»·Õè 3-6 áËŋ§¨‹ÒÂä¿ Vs ·íÒ˹ŒÒ·Õ¨è ҋ ÂÊÑ­­Ò³ PWM ã¹ÅѡɳзÕàè »š¹¾ÑÅʏÊàÕè ËÅÕÂè Á ·íÒãˌ ÍѵÃÒàÃçÇ㹡ÒÃËÁع¢Í§ÁÍàµÍÏ¢¹Öé ÍÂÙʋ ´Ñ ʋǹ¢Í§ª‹Ç§àÇÅÒ Ton (ª‹Ç§àÇÅÒ·ÕÁè ¡Õ ÃÐáʢѺÁÍàµÍÏãˌ ËÁع) «Ö§è ¡ç¤Í× ª‹Ç§àÇÅÒ·ÕÁè ÍàµÍÃä´ŒÃºÑ áç´Ñ¹·‹Ò¡Ñº Vm áÅжŒÒ¾Ô¨ÒóҪ‹Ç§àÇÅÒ 1 ¤Òº·ÕÁè ÍàµÍÏ䴌 ÃѺ¡ÃÐáʵÒÁÃÙ»·Õè 3-6 ¨Ð¾ºÇ‹ÒÁÍàµÍÏ·íҧҹ㹪‹Ç§ Ton ´Ñ§¹Ñ鹶ŒÒª‹Ç§àÇÅÒ Ton ÁÒ¡¡ç¨Ð·íÒãˌ ÁÍàµÍÃä´ŒÃºÑ áç´Ñ¹ÁÒ¡ ¨Ö§ËÁع䴌àÃçÇ ¤‹ÒÊѴʋǹ¢Í§ª‹Ç§àÇÅÒ Ton àÁ×Íè ¤Ô´à»š¹à»ÍÏà«ç¹µà·Õº¨Ò¡ ª‹Ç§àÇÅÒ 1 ¤Òº (T) àÃÕÂ¡Ç‹Ò ´ÔǵÕäé «à¡ÔÅ â´Âà¢Õ¹໚¹¤ÇÒÁÊÑÁ¾Ñ¹¸ä´Œ´§Ñ ¹Õé Rs

Vs

Vm

M

Vm

t Ton Toff

T

ÃÙ»·Õè 3-6 ¡Ò÷íÒ§Ò¹¢Í§ÊÑ­­Ò³ PWM ·ÕÁè µÕ Í‹ ¡ÒâѺÁÍÍàµÍÏ俵ç


58làÃÕ¹ÃÙá Œ ÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ

%´ÔǵÕäé «à¡ÔÅ =

100 ×

¤ÇÒÁ¶Õ¢è ͧ¾ÑÅʏ =

Ton Ton + Toff

MCS-51

.................................................(3.3)

1 1 = .................................................(3.4) Ton + Toff T

áç´Ñ¹à©ÅÕÂè µ¡¤Ã‹ÍÁÍàµÍÏ = áç´Ñ¹ä¿àÅÕÂé § x ¤‹Ò´ÔǵÕäé «à¡ÔÅ (%) .......(3.5)

¶Ö§áÁŒÇҋ ¤‹Ò´ÔǵÕäé «à¡ÔŢͧÊÑ­­Ò³ PWM ¨Ð໚¹µÑÇ¡íÒ˹´ÍѵÃÒàÃçÇ㹡ÒÃËÁع¢Í§ÁÍàµÍÏ ¡çµÒÁ ᵋÁÍàµÍÏ俵ç¹Ñ¹é ÁÕ¢´Õ ¨íҡѴ㹡ÒÃÃѺÊÑ­­Ò³¾ÑÅʏ·¤Õè ÇÒÁ¶ÕÊè §Ù ÊØ´ÍÂÙ¤‹ ҋ ˹֧è à·‹Ò¹Ñ¹é ¶ŒÒà¡Ô¹ ¤‹Ò´Ñ§¡Å‹ÒÇáŌǨзíÒãˌÁÍàµÍÏËÂØ´ËÁعà¹×Íè §¨Ò¡à¡Ô´¡ÒÃÍÔÁè µÑÇ ´Ñ§¹Ñ¹é ¨Ö§µŒÍ§ÃÐÇѧ㹨ش¹Õ´é nj  ã¹ÃÙ» ·Õè 3-7 áÊ´§¼Å¢Í§¡ÒÃà»ÅÕè¹á»Å§¤‹Ò´ÔǵÕéä«à¡ÔÅ·ÕèÁÕµ‹Íáç´Ñ¹µ¡¤Ã‹ÍÁÁÍàµÍÏ áÅШҡÊÑ­­Ò³ PWM µÑÇÍ‹ҧ·Õ¹è Òí ÁÒ͸ԺÒÂã¹ÃÙ»·Õè 3-7 ÁÕ¤ÒºàÇÅÒ෋ҡѺ 20 ÁÔÅÅÔÇ¹Ô Ò·Õ ´Ñ§¹Ñ¹é ¤ÇÒÁ¶Õ¢è ͧÊÑ­­Ò³ PWM ¨Ö§à·‹Ò¡Ñº 50Hz Vm (V)

(¡)

t (ms)

4 x 100% = 20% ´ÔǵÕéä«à¡ÔÅ = 20 áç´Ñ¹à©ÅÕè·ÕèÁÍàµÍÏ =4.5 x 20% = 0.9V

t (ms)

´ÔǵÕéä«à¡ÔÅ = 10 20 x 100% = 50% áç´Ñ¹à©ÅÕè·ÕèÁÍàµÍÏ = 4.5 x 50% = 2.25V

4.5

4 8 12 16 20 24 28 32 Vm (V) 10ms

(¢)

4.5

4 8 12 16 20 24 28 32 Vm (V)

(¤)

18ms 4.5

4 8 12 16 20 24 28 32

t (ms)

´ÔǵÕéä«à¡ÔÅ = 18 20 x 100% = 90% áç´Ñ¹à©ÅÕè·ÕèÁÍàµÍÏ = 4.5 x 90% = 4.05V

Vm (V)

(§)

4.5

20ms

4 8 12 16 20 24 28 32

t (ms)

´ÔǵÕéä«à¡ÔÅ = 20 20 x 100% = 100% áç´Ñ¹à©ÅÕè·ÕèÁÍàµÍÏ = 4.5 x 100% = 4.5V

ÃÙ»·Õè 3-7 áÊ´§¤ÇÒÁÊÑÁ¾Ñ¹¸ÃÐËNjҧ´ÔǵÕäé «à¡ÔŢͧÊÑ­­Ò³ PWM ¡Ñºáç´Ñ¹·ÕÁè ÍàµÍÏ䴌úÑ


àÃÕ¹ÃÙጠÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ MCS-51 l 59

´Ñ§¹Ñ¹é ¨Ö§ÊÃػ䴌Çҋ ¡ÒâѺÁÍàµÍÏâ´Â¡Òè‹ÒÂÊÑ­­Ò³ PWM µÒÁÃÙ»·Õè 3-9 (¡) ¨Ð·íÒãˌÁÍàµÍÏ ËÁع´ŒÇÂÍѵÃÒàÃçǵèÒí ÊØ´à¾ÃÒÐãˌ´ÇÔ µÕäé «à¡ÔÅ 20% áç´Ñ¹µ¡¤Ã‹ÍÁÁÍàµÍÏà¾Õ§ 0.9V ¨Ò¡ÃÐ´Ñºä¿ àÅÕÂé §ÊÙ§ÊØ´ 4.5V «Ö§è µèÒí ÊØ´àÁ×Íè à·Õº¡Ñº¡Ã³Õ͹×è æ ·Õ¤è ÇÒÁ¶Õàè ´ÕÂǡѹ ʋǹ¡Òû‡Í¹ÊÑ­­Ò³µÒÁÃÙ»·Õè 39 (§) ໚¹¡Òû‡Í¹áç´Ñ¹ä¿µÃ§¤§·Õ¨è ҋ ÂãˌᡋÁÍàµÍÏ «Ö§è ¶ŒÒà»ÃÕºà·Õº¡ÑºÊÑ­­Ò³¾ÑÅʏ·§Ñé 3 Ẻ ¨Ò¡ÃÙ»·Õè 3-9 (¡) ¶Ö§ 3-9 (¤) ¨ÐàÊÁ×Í¹Ç‹Ò µÅÍ´ª‹Ç§àÇÅÒ 20 ÁÔÅÅÔÇ¹Ô Ò·Õ¹¹Ñé ÍÂÙ㋠¹ª‹Ç§ Ton µÅÍ´ ¨Ö§Áդҋ ´ÔǵÕäé «à¡ÔÅ໚¹ 100% ʋǹ¤ÇÒÁ¶Õ¨è Ð໚¹ 0Hz à¹×Íè §¨Ò¡à»š¹ÊÑ­­Ò³ä¿µÃ§ «Ö§è ¨Ò¡·Õäè ´ŒÍ¸ÔºÒ¡Òà ¢ÑºÁÍàµÍÏ俵紌ǡÒè‹ÒÂÊÑ­­Ò³ PWM áŌǨÐàËç¹¢ŒÍ´Õ¢Í§ÇÔ¸¡Õ ÒùÕáé ŌÇÇ‹Ò à»š¹ÇÔ¸·Õ ÊÕè ÒÁÒö ¤Çº¤ØÁÍѵÃÒàÃçÇ㹡ÒÃËÁع¢Í§ÁÍàµÍÏ䴌¨Ò¡ª‹Ç§àÇÅҢͧ¡Òà ON â´ÂÊÒÁÒöÊÌҧáÅФǺ¤ØÁ ´ÔǵÕéä«à¡ÔŨҡ¡Ãкǹ¡Ò÷ҧ«Í¿µáÇÏ䴌

3.5.2 ǧ¨Ã¢ÑºÁÍàµÍÏ俵ç¢Í§á¼§Ç§¨Ã RBX-51AC2 㹡ÒâѺà¤Å×Íè ¹Ëع‹ ¹µ Robo-51 ´ŒÇÂÁÍàµÍÏ俵ç¹Ñ¹é ¡ÒäǺ¤ØÁÍѵÃÒàÃçÇ㹡ÒÃËÁعáÅÐ ·ÔÈ·Ò§¨ÐãªŒÇ¸Ô ¡Õ Òè‹ÒÂÊÑ­­Ò³ä¿àÅÕÂé §ãˌ¡ºÑ ÁÍàµÍÏẺ PWM â´Â㪌¢Ò¾Íϵ P1.6 áÅÐ P1.7 ʋ§ ÊÑ­­Ò³ PWM ·ÕÁè ¤Õ ÇÒÁÅÐàÍÕ´¢¹Ò´ 8 ºÔµ «Ö§è ÊÌҧ¢Ö¹é ¨Ò¡âÁ´ÙÅ PCA Íѹ໚¹âÁ´ÙžÔàÈÉ·ÕÁè ãÕ ¹ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ T89C51AC2 +5V

R10 1k

11

42

K18A-K18B MOTOR-A

C19 0.1/50V

EA

1

IC5 T89C51AC2

+5V +Vm

43

16 P2.0 P2.1

29

2

28

7 2A

9

1

P1.6/PWM1 P2.2 P2.3 P1.7/PWM2 40 C15 33pF

XTAL1 11.0592MHz

1A

27

15 4A

26

10

10

9

41

8

Vcc1

12EN

C17 0.1/63V

1Y 3 2Y

IC7 L293D

4Y 3Y

+ +

R33 2k2

DIRECT LED6 DIR. #A

6 14

LED7 DIR. #B

11 R34 2k2

3A 34EN

13 12 5 C16 33pF

ÃÙ»·Õè 3-8 ǧ¨Ã¢ÑºÁÍàµÍϢͧἧǧ¨Ã RBX-51AC2

4

INVERT

C18 0.1/63V K19A-K19B MOTOR-B

INVERT

+ + DIRECT


60làÃÕ¹ÃÙá Œ ÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ

MCS-51

ÊÑ­­Ò³ PWM ·Õ¶è ¡Ù ÊÌҧ¢Ö¹é ¹Õ¨é Ðʋ§µ‹Íä»Âѧǧ¨Ã¢Ñº¡ÃÐáÊ·Õãè ªŒäÍ«ÕàºÍÏ L293D ·ÕÊè ÒÁÒö ¢ÑºÁÍàµÍÏ䴌¾ÃŒÍÁ¡Ñ¹ 2 µÑÇ à¾×Íè ãˌÊÒÁÒö¨‹Ò¡ÃÐáÊᡋÁÍàµÍÏ䴌ʧ٠¢Ö¹é ·Õàè »š¹àª‹¹¹Õ¡é àç ¹×Íè §¨Ò¡ ¤ÇÒÁÊÒÁÒö㹡Òè‹Ò¡ÃÐáʨҡ¾Íϵ¢Í§äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏâ´ÂµÃ§¹Ñ¹é ¹ŒÍÂÁÒ¡ «Ö§è äÁ‹à¾Õ§ ¾Í ¨Ö§µŒÍ§ÁÕǧ¨Ã¢Ñº¡ÃÐáÊà¾ÔÁè àµÔÁࢌÒÁÒ ã¹ÃÙ»·Õè 3-8 áÊ´§Ê‹Ç¹¢Í§Ç§¨Ã¢ÑºÁÍàµÍϢͧἧǧ¨Ã RBX-51AC2 ·Õ¢è ÒàÍÒµ¾µØ ¢Í§äÍ«Õ L293D àª×Íè Áµ‹Í¡Ñº LED áÊ´§¼Å·ÔÈ·Ò§¡ÒÃËÁع¢Í§ÁÍàµÍÏ·§Ñé 2 µÑÇ â´Â LED ·Õãè ªŒ¹¹Ñé ໚¹ LED 2 ÊÕ ¤×Í ÊÕá´§áÅÐà¢ÕÂÇ ¨ÐáÊ´§à»š¹ÊÕã´¹Ñ¹é ¢Ö¹é ÍÂÙ¢‹ ÇÑé ¢Í§áç´Ñ¹·Õàè ¡Ô´ ¢Öé¹·Õè¢ÒàÍÒµ¾Øµ¢Í§ L293D «Öè§ÍÒ¨¹íÒÁÒ¡íÒ˹´·ÔÈ·Ò§¡ÒÃà¤Å×è͹·Õè¢Í§Ë؋¹Â¹µä´Œ´ŒÇ â´Âã¹·Õè¹Õé ÂÖ´ËÅÑ¡Ç‹Ò LED ¢Í§Ç§¨Ã¢ÑºÁÍàµÍϪ͋ §ã´à»š¹ÊÕà¢ÕÂÇ ËÁÒ¶֧ ÁÍàµÍÏËÁع仢ŒÒ§Ë¹ŒÒ LED ¢Í§Ç§¨Ã¢ÑºÁÍàµÍϪ͋ §ã´à»š¹ÊÕá´§ ËÁÒ¶֧ ÁÍàµÍÏËÁع仢ŒÒ§ËÅѧ ¶ŒÒËÒ¡ÁÍàµÍÏËÁعä»ã¹·Ôȵ碌ÒÁ¡Ñº¡ÒÃáÊ´§¼Å¢Í§ LED ãˌ·Òí ¡ÒÃÊÅѺ¢ÑÇé µ‹ÍÁÍàµÍÏ ἧǧ¨Ã RBX-51AC2 à¾×Íè ãˌ·ÈÔ ·Ò§¡ÒÃËÁع¢Í§ÁÍàµÍϵçµÒÁ¡ÒÃáÊ´§¼Å¢Í§ LED

3.5.3 ÃÕ¨ÊÔ àµÍÏ·àÕè ¡ÕÂè Ç¢ŒÍ§¡Ñº¡ÒáíÒà¹Ô´ÊÑ­­Ò³ PWM ¢Í§âÁ´ÙÅ PCA ã¹ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ T89C51AC2 ¡ÒâѺÁÍàµÍÏ俵ç¢Í§Ëع‹ ¹µ Robo-51 ·íÒ䴌â´Â¡Òè‹ÒÂÊÑ­­Ò³ PWM ãˌ¡ºÑ ǧ¨Ã¢Ñº â´Â ʋ§ÊÑ­­Ò³ PWM ÍÍ¡¨Ò¡¢Ò¾Íϵ P1.6 áÅÐ P1.7 «Ö§è ໚¹¢ÒÊÑ­­Ò³¢Í§âÁ´ÙÅ PCA (Programmable Counter Array) ËÃ×ÍâÁ´ÙÅǧ¨Ã¹Ñºâ»Ãá¡ÃÁ䴌 ÁÕÃ¨Õ ÊÔ àµÍÏ·àÕè ¡ÕÂè Ç¢ŒÍ§áÅкԵ¤Çº¤ØÁ´Ñ§¹Õé

3.6.3.1 CCAPM ÃÕ¨ÊÔ àµÍÏ¡Òí ˹´ÃٻẺ¡Ò÷íÒ§Ò¹¢Í§âÁ´ÙÅ PCA ºÔµ 7 ºÔµ 6 ºÔµ 5 ºÔµ 4 ºÔµ 3 ºÔµ 2 ºÔµ 1 -

ECOMn

CAPPn

CAPNn

MATn

TOGn

PWMn

ºÔµ 0

ECCFn

Áմnj ¡ѹ 5 µÑÇ෋ҡѺ¨íҹǹ¢Í§âÁ´ÙÅ PCA ·ÕÁè Õ 5 ªØ´ ÁÕáÍ´à´Ãʵѧé ᵋ 0DAH ¶Ö§ 0DEH â´Â 0DAH ¤×ÍáÍ´à´ÃʢͧÃÕ¨ÊÔ àµÍÏ CCAPM0 㪌㹡ÒÃàÅ×Í¡âËÁ´¡Ò÷íÒ§Ò¹¢Í§âÁ´ÙÅ PCA «Ö§è ÊÃØ»änjáŌÇã¹µÒÃÒ§·Õè 3-1 â´ÂÃÕ¨ÔÊàµÍÏ CCAPMn ¹ÕéäÁ‹ÊÒÁÒöࢌҶ֧䴌ã¹ÃдѺºÔµ ´Ñ§¹Ñ鹡Òà ¡íÒ˹´¤‹Ò¨Ö§µŒÍ§¡íÒ˹´à»š¹äºµá·¹ àÁ×Íè àÅ×Í¡ãˌâÁ´ÙÅ PCA ÊÌҧÊÑ­­Ò³ PWM äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ T89C51AC2 Áժ͋ §¡íÒà¹Ô´ ÊÑ­­Ò³ PWM ·Ñ§é ËÁ´ 5 ª‹Í§ ¤×Í


àÃÕ¹ÃÙጠÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ MCS-51 l 61 ºÔµ 7

ºÔµ 6

ºÔµ 5

ºÔµ 4

ºÔµ 3

ºÔµ 2

ºÔµ 1

ºÔµ 0

¡Ò÷íÒ§Ò¹¢Í§âÁ´ÙÅ

-

ECOMn

CAPPn

CAPNn

MATn

TOGn

PWMn

ECCFn

X

0

0

0

0

0

0

0

äÁ‹ÁÕ¡Ò÷íÒ§Ò¹

X

X

1

0

0

0

0

X

X

X

0

1

0

0

0

X

µÃǨ¨ÑºÊÑ­­Ò³·Õè¢Íº¢Ò¢Öé¹ µÃǨ¨ÑºÊÑ­­Ò³·Õè¢Íº¢Òŧ

X

X

1

1

0

0

0

X

µÃǨ¨Ñº¡ÒÃà»ÅÕè¹ʶҹТͧÊÑ­­Ò³

X

1

0

0

1

0

0

X

µÑǵÑé§àÇÅÒ·Ò§«Í¿µáÇÏ 16 ºÔµ

X

1

0

0

1

1

0

X

ÊÌҧÊÑ­­Ò³àÍÒµ¾Øµ¤ÇÒÁàÃçÇÊÙ§

X

1

0

0

0

0

1

0

ÊÌҧÊÑ­­Ò³ PWM ¢¹Ò´ 8 ºÔµ

X

1

0

0

1

X

0

X

Ç͵ª´çÍ¡ä·àÁÍÏ

µÒÃÒ§·Õè 3-1 ¡ÒáíÒ˹´¤‹Ò¢Í§¢ŒÍÁÙÅã¹ÃÕ¨ÊÔ àµÍÏ CCAPM ¢Í§áµ‹ÅÐâÁ´ÙÅ PCA (n ¤×ÍËÁÒÂàÅ¢ ¢Í§âÁ´ÙÅ PCA Áդҋ ෋ҡѺ 0 ¶Ö§ 4) P1.3 µÃ§¡Ñºª‹Í§¡íÒà¹Ô´ÊÑ­­Ò³ PWM 0 P1.4 µÃ§¡Ñºª‹Í§¡íÒà¹Ô´ÊÑ­­Ò³ PWM 1 P1.5 µÃ§¡Ñºª‹Í§¡íÒà¹Ô´ÊÑ­­Ò³ PWM 2 P1.6 µÃ§¡Ñºª‹Í§¡íÒà¹Ô´ÊÑ­­Ò³ PWM 3 P1.7 µÃ§¡Ñºª‹Í§¡íÒà¹Ô´ÊÑ­­Ò³ PWM 4 ÊíÒËÃѺἧǧ¨Ã RBX-51AC2 àÅ×͡㪌à¾Õ§ 2 ª‹Í§¤×Í ª‹Í§ 3 áÅÐ 4 ¨Ö§ÊÒÁÒö¢ÑºÁÍàµÍÏ ¾ÃŒÍÁ¡Ñ¹ä´Œ 2 µÑÇ ´Ñ§¹Ñ¹é 㹡ÒâѺà¤Å×Íè ¹Ëع‹ ¹µ¨ÐµŒÍ§¡íÒà¹Ô´ÊÑ­­Ò³ PWM ÍÍ¡ÁÒ·Ò§¢Ò¾Íϵ P1.6 áÅÐ P1.7 ´Ñ§¹Ñ鹨֧µŒÍ§¡íÒ˹´¤‹ÒÀÒÂã¹ÃÕ¨ÔÊàµÍÏ CCAPM3 áÅÐCCAPM4 ãˌÁÕÃٻẺ¡Òà ·íҧҹ໚¹¡ÒáíÒà¹Ô´ÊÑ­­Ò³ PWM ¹Ñ¹è ¤×͵ŒÍ§à«µºÔµ PWM áÅÐ ECOM ÀÒÂã¹ÃÕ¨ÊÔ àµÍÏ CCAPM3 áÅÐCCAPM4 ´ŒÇ¡ÒÃà¢Õ¹¤íÒÊѧè ÀÒÉÒ C ໚¹ CCAPM3 = 0x42;

// ¡íÒ˹´ÃٻẺ¡Ò÷íÒ§Ò¹ãˌâÁ´ÙÅ PCA ª‹Í§ 3 ¡íÒà¹Ô´ÊÑ­­Ò³ PWM

CCAPM4 = 0x42;

// ¡íÒ˹´ÃٻẺ¡Ò÷íÒ§Ò¹ãˌâÁ´ÙÅ PCA ª‹Í§ 4 ¡íÒà¹Ô´ÊÑ­­Ò³ PWM

3.5.3.2 CCAPn ÃÕ¨ÊÔ àµÍÏà¡çº¤‹Ò¡ÒùѺ¢Í§âÁ´ÙÅ PCA ໚¹ÃÕ¨ÊÔ àµÍÏ¢¹Ò´ 16 ºÔµ ẋ§Í͡໚¹ 2 ʋǹ¤×Í CCAPnH ÊíÒËÃѺà¡çº¢ŒÍÁÙÅ亵º¹ áÅÐ CCAPnL ÊíÒËÃѺà¡çº¢ŒÍÁÙÅ亵Åҋ § â´Â âÁ´ÙÅ PCAᵋÅЪش¨ÐÁÕ CCAPnH áÅÐCCAPnL Í‹ҧÅÐ 1 µÑÇ àª‹¹ âÁ´ÙÅ PCA ª‹Í§ 3 ¨ÐÁÕÃ¨Õ ÊÔ àµÍÏ CCAP3H áÅÐCCAP3L à¡çº¤‹Ò¡ÒùѺ ã¹¢³Ð·Õâè Á´ÙÅ PCAª‹Í§ 4 ¨ÐÁÕÃ¨Õ ÊÔ àµÍÏ CCAP4H áÅÐ CCAP4L à¡çº¤‹Òâ´ÂÃÕ¨ÊÔ àµÍÏ CCAPn ¹Õàé ͧ¨Ð㪌໚¹µÑÇ ¡íÒ˹´¤‹Ò´ÔǵÕäé «à¡ÔŢͧÊÑ­­Ò³ PWM àÁ×Íè âÁ´ÙÅ PCA ·íÒ§Ò¹ã¹âËÁ´ÊÌҧÊÑ­­Ò³ PWM


62làÃÕ¹ÃÙá Œ ÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ

MCS-51

¹Í¡¨Ò¡¹ÕéÀÒÂã¹âÁ´ÙÅ PCA ÂѧÁÕÃÕ¨ÔÊàµÍÏ¢¹Ò´ 16 ºÔµ·Õè·íÒ˹ŒÒ·Õèà¡çº¤‹Ò¡ÒùѺ «Öè§áº‹§à»š¹ÃÕ¨ÔÊàµÍÏ¢¹Ò´ 8 ºÔµ 2 µÑǤ×Í ÃÕ¨ÊÔ àµÍÏ CH áÅÐ CL àÁ×Íè ·íÒ§Ò¹ã¹âËÁ´ÊÌҧÊÑ­­Ò³ PWM ÃÕ¨ÊÔ àµÍϵÇÑ ¹Ñº CL ¨Ð¹Ñº¤‹Òà¾ÔÁè ¢Ö¹é ¤Ãѧé ÅÐ 1 ¤‹Ò ʋǹÍѵÃÒ¡ÒùѺÊÒÁÒö¡íÒ˹´ÃٻẺ䴌¨Ò¡ÃÕ¨ÊÔ àµÍÏ CMOD

3.5.3.3 CMOD ÃÕ¨ÊÔ àµÍÏ¡Òí ˹´âËÁ´¡ÒùѺ¤‹Ò¢Í§âÁ´ÙÅ PCA ºÔµ 7 ºÔµ 6 ºÔµ 5 ºÔµ 4 ºÔµ 3 ºÔµ 2 CIDL

WDTE

-

-

-

CPS1

ºÔµ 1

CPS0

ºÔµ 0 ECF

໚¹ÃÕ¨ÊÔ àµÍÏ¢¹Ò´ 8 ºÔµ ÁÕáÍ´à´ÃÊÍÂÙ·‹ Õè 0D9H ÊíÒËÃѺºÔµ·Õàè ¡ÕÂè Ç¢ŒÍ§¡Ñº¡ÒáíÒà¹Ô´ÊÑ­­Ò³ PWM 䴌ᡋ ºÔµ CPS1 áÅÐ CPS0 «Ö§è 㪌¡Òí ˹´áËŋ§¡íÒà¹Ô´ÊÑ­­Ò³¹Ò́¡Ò¢Í§âÁ´ÙÅ PCA â´ÂÁÕ ÃÒÂÅÐàÍÕ´´Ñ§¹Õé CPS1 CPS0 0 0 0 1 1 0 1 1

áËŋ§¡íÒà¹Ô´ÊÑ­­Ò³¹Ò́¡Ò ÊÑ­­Ò³¹Ò́¡Ò¤ÇÒÁ¶Õàè ·‹Ò¡Ñº 1/6 ෋Ңͧ¤ÇÒÁ¶ÕÊè ­ Ñ ­Ò³¹Ò́¡ÒËÅÑ¡ã¹âËÁ´ 6 ä«à¡ÔÅ ÊÑ­­Ò³·ÕÁè ¤Õ ÇÒÁ¶Õàè ·‹Ò¡Ñº 1/2 ෋Ңͧ¤ÇÒÁ¶ÕÊè ­ Ñ ­Ò³¹Ò́¡ÒËÅÑ¡ã¹âËÁ´ 6 ä«à¡ÔÅ ¡ÒÃâÍàÇÍÏâ¿ÅǢͧä·àÁÍÏ 0 ¨Ò¡ÀÒ¹͡¼‹Ò¹·Ò§¢Ò P1.2/ECI

3.5.3.4 CCON ÃÕ¨ÊÔ àµÍϤǺ¤ØÁ¢Í§âÁ´ÙÅ PCA

ºÔµ 7 CF

ºÔµ 6 CR

ºÔµ 5 -

ºÔµ 4

CCF4

ºÔµ 3

CCF3

ºÔµ 2

CCF2

ºÔµ 1

CCF1

ºÔµ 0

CCF0

ÁÕ¢¹Ò´ 8 ºÔµ ÍÂÙ·‹ áÕè Í´à´ÃÊ 0D8H ÊÒÁÒöࢌҶ֧䴌ã¹ÃдѺºÔµ ໚¹ÃÕ¨ÊÔ àµÍϤǺ¤ØÁµÑǹѺ áÅÐáÊ´§Ê¶Ò¹Ð¡ÒÃà¡Ô´ÍÔ¹àµÍÃÃ»Ñ µáÅÐâÍàÇÍÏâ¿ÅǢͧâÁ´ÙÅ PCA ºÔµ·Õàè ¡ÕÂè Ç¢ŒÍ§¡Ñº¡ÒáíÒà¹Ô´ ÊÑ­­Ò³ PWM ÁÕà¾Õ§ºÔµà´ÕÂǤ×Í ºÔµ CR «Ö§è ໚¹ºÔµ 6 㪌ແ´-»´¡Ò÷íÒ§Ò¹¢Í§µÑǹѺã¹âÁ´ÙÅ PCA «Ö§è ¡ç¤Í× ãªŒàÍç¹àÍàºÔÅ¡ÒáíÒà¹Ô´ÊÑ­­Ò³ PWM ¹Ñ¹è àͧ

3.5.4 ¡ÒáíÒà¹Ô´ÊÑ­­Ò³ PWM ¢Í§âÁ´ÙÅ PCA âÁ´Ùŷѧé 5 ǧ¨Ã¢Í§ PCA ÊÒÁÒöÊÌҧÊÑ­­Ò³ PWM 䴌 ä´ÍÐá¡ÃÁ¡Ò÷íÒ§Ò¹ã¹âËÁ´ ¹Õáé Ê´§ã¹ÃÙ»·Õè 3-9 ¢Ò CEX ¨Ðä´ŒÃºÑ ¡ÒáíÒ˹´ãˌ໚¹àÍÒµ¾µØ ¤ÇÒÁ¶Õ¢è ͧÊÑ­­Ò³ PWM ¨Ð¢Ö¹é ÍÂً ¡ÑºáËŋ§¡íÒà¹Ô´ÊÑ­­Ò³¹Ò́¡Ò¢Í§âÁ´ÙÅ PCA áÅФÇÒÁ¶Õ¢è ͧâÁ´Ùŷѧé ËÁ´¨Ð෋ҡѹ à¹×Íè §¨Ò¡ãªŒ áËŋ§¡íÒà¹Ô´ÊÑ­­Ò³¹Ò́¡ÒËÇÁ¡Ñ¹ ã¹¢³Ð·Õ¤è ҋ ´ÔǵÕäé «à¡ÔŢͧÊÑ­­Ò³ PWM ã¹áµ‹ÅÐâÁ´ÙŨÐᵡ µ‹Ò§¡Ñ¹µÒÁ¤‹Ò¢Í§ÃÕ¨ÊÔ àµÍÏ CCAPnL ¡ÒÃàÍç¹àÍàºÔÅãˌ·Òí §Ò¹ã¹âËÁ´¹Õ·é Òí 䴌â´Â¡ÒÃ૵ºÔµ PWM áÅÐ ECOM ã¹ÃÕ¨ÊÔ àµÍÏ CCAPM


àÃÕ¹ÃÙጠÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ MCS-51 l 63

CCAPnH

CCAPnL "0"

àÍç¹àÍàºÔÅ

¢Ò¾Íϵ¢Í§âÁ´ÙÅ PCA

CL<CCAPnL

ʋǹà»ÃÕºà·Õº ¢ŒÍÁÙÅ 8 ºÔµ

CEXn CL>=CCAPnL

"1" CL

âÍàÇÍÏâ¿ÅÇ

-

ECOMn

µÑǹѺã¹âÁ´ÙÅ PCA

CAPPn

CAPNn

MATn

TOGn

0

0

0

0

PWMn

ECCFn

CCAPMn, n=0-4 (0C2H-0C6H)

0

ÃÙ»·Õè 3-9 ä´ÍÐá¡ÃÁ¡ÒáíÒà¹Ô´ÊÑ­­Ò³ PWM ¢Í§âÁ´ÙÅ PCA ã¹äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ T89C51AC2 àÁ×Íè ¤‹Ò¢Í§ÃÕ¨ÊÔ àµÍÏ CL ¹ŒÍ¡NjҤ‹Ò¢Í§ CCAPnL ¨Ðà¡Ô´ÊÑ­­Ò³àÍÒµ¾µØ ÅͨԡµèÒí ᵋ¶ÒŒ àÁ×Íè ã´Áդҋ ෋ҡѹËÃ×ͤ‹Òã¹ CL ÊÙ§¡Ç‹Ò ¨Ð䴌ʭ Ñ ­Ò³àÍÒ¤µ¾µØ ÅͨԡÊÙ§ àÁ×Íè ¤‹Ò¢Í§ CL à¡Ô´âÍàÇÍÏâ¿ÅÇ à»ÅÕÂè ¹¨Ò¡ 0FFH ໚¹ 00H ÃÕ¨ÊÔ àµÍÏ CCAPnL ¨Ð·íÒ¡ÒÃâËÅ´¤‹ÒãËÁ‹¨Ò¡ CCAPnH ·íÒãˌʭ Ñ ­Ò³ PWM ·Õäè ´ŒµÍ‹ à¹×Íè §äÁ‹à¡Ô´¡ÒÃà»ÅÕÂè ¹á»Å§ã¹ÅѡɳСÒáÃЪҡ¢Í§ÊÑ­­Ò³ËÃ×Í·Õàè ÃÕÂ¡Ç‹Ò “¡ÅÔµª” (glitch)

3.5.5 ¡ÒÃÊÌҧ¿˜§¡ª¹Ñè ¢Í§â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺ¢Ñºà¤Å×Íè ¹Ëع‹ ¹µ ǧ¨Ã¢ÑºÁÍàµÍÏ俵ç¢Í§á¼§Ç§¨Ã RBX-51AC2 ã¹Ëع‹ ¹µ Robo-51 ÁÕ 2 ª‹Í§ ¶ŒÒ¢ÑºÁÍàµÍÏ ¾ÃŒÍÁ¡Ñ¹ 2 µÑǨеŒÍ§ãªŒºµÔ ¾Íϵ¤Çº¤ØÁ¾ÃŒÍÁ¡Ñ¹·Ñ§é ÊÔ¹é 6 ºÔµ ´Ñ§¹Õé P2.0 áÅÐ P2.1 ¶Ù¡µ‹ÍࢌҡѺ¢Ò IN1áÅÐ IN2 ¢Í§äÍ«Õ L293D µÒÁÅíҴѺ ÊíÒËÃѺ¤Çº¤ØÁ·ÔÈ ·Ò§¡ÒÃËÁع¢Í§ÁÍàµÍϵÇÑ ·Õè 1 P1.6 ¶Ù¡µ‹ÍࢌҡѺ¢Ò ENABLE1 ¢Í§äÍ«Õ L293D ÊíÒËÃѺ¡íÒà¹Ô´ÊÑ­­Ò³ PWM à¾×Íè ¤Çº¤ØÁ ÍѵÃÒàÃçǢͧÁÍàµÍϵÇÑ ·Õè 1 à¾×Íè ¤Çº¤ØÁáç´Ñ¹àÍÒµ¾µØ ·Õ¢è Ò OUT1 áÅÐ OUT2 ¢Í§ L293D


64làÃÕ¹ÃÙá Œ ÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ

MCS-51

P2.2 áÅÐ P2.3 ¶Ù¡µ‹ÍࢌҡѺ¢Ò IN3 áÅÐ IN4 ¢Í§äÍ«Õ L293D µÒÁÅíҴѺ ÊíÒËÃѺ¤Çº¤ØÁ·ÔÈ ·Ò§¡ÒÃËÁع¢Í§ÁÍàµÍϵÇÑ ·Õè 2 P1.7 ¶Ù¡µ‹ÍࢌҡѺ¢Ò ENABLE2 ¢Í§äÍ«Õ L293D ÊíÒËÃѺ¡íÒà¹Ô´ÊÑ­­Ò³ PWM à¾×Íè ¤Çº ¤ØÁÍѵÃÒàÃçǢͧÁÍàµÍϵÇÑ ·Õè 2 à¾×Íè ¤Çº¤ØÁáç´Ñ¹àÍÒµ¾µØ ·Õ¢è Ò OUT3 áÅÐ OUT4 ¢Í§ L293D ¨Ò¡â¤Ã§ÊÌҧ¡Ò÷íÒ§Ò¹¢Í§äÍ«Õ L293D ã¹ÃÙ»·Õè 3-10 ¡ÒäǺ¤ØÁ·ÔÈ·Ò§¡ÒÃËÁع¢Í§ ÁÍàµÍϪ͋ §ã´æ ·íÒ䴌â´Â¤Çº¤ØÁÊÀÒÇÐÅͨԡ¢Í§ºÔµ¤Çº¤ØÁ·Ñ§é Êͧ (IN1 ¡Ñº IN2 ÊíÒËÃѺÁÍàµÍÏ µÑÇ·Õè 1 áÅÐ IN3 ¡Ñº IN4 ÊíÒËÃѺÁÍàµÍϵÇÑ ·Õè 2) ãˌÁÊÕ ÀÒÇе碌ÒÁ¡Ñ¹¡ç¨Ð·íÒãˌÁÍàµÍϪ͋ §¹Ñ¹é ËÁعä»ã¹·Ôȷҧ˹֧è áÅÐàÁ×Íè µŒÍ§¡ÒÃãˌÁÍàµÍÏËÁع¡ÅѺ·ÔÈ·Ò§¨ÐµŒÍ§¡ÅѺÊÀÒÇÐÅͨԡ¢Í§ºÔµ ¤Çº¤ØÁ·Ñ§é ¤Ùà‹ »š¹µÃ§¢ŒÒÁ ¾ÃŒÍÁ¡Ñ¹¹Ñ¹é ª‹Í§¡íÒà¹Ô´ÊÑ­­Ò³ PWM ¢Í§ÁÍàµÍϪ´Ø ¹Ñ¹é ¨ÐµŒÍ§ÁÕ¡Òè‹Ò ÊÑ­­Ò³´ŒÇ àÁ×è͵ŒÍ§¡ÒÃãˌÁÍàµÍÏËÂØ´ËÁعªÑèÇ¢³Ð·íÒ䴌â´Â¤Çº¤ØÁÊÀÒÇÐÅͨԡ¢Í§ºÔµ¤Çº¤ØÁ ·Ñ§é ÊͧãˌÁÊÕ ÀÒÇеç¡Ñ¹ «Ö§è ÍÒ¨¨Ð໚¹Åͨԡ “1” ËÃ×Í “0” ·Ñ§é ¤Ù¡‹ äç ´Œ â´Â·ÕÂè §Ñ ÁÕ¡Òè‹ÒÂÊÑ­­Ò³ PWM ¢Í§ÁÍàµÍϪ´Ø ¹Ñ¹é ÍÂً ËÃ×ÍÍÕ¡ÇÔ¸ËÕ ¹Ö§è ¤×Í ËÂØ´¨‹ÒÂÊÑ­­Ò³ PWM ¢Í§ÁÍàµÍϪ͋ §¹Ñ¹é «Ö§è ·íÒ䴌â´Â ¡ÒÃà¤ÅÕÂϺµÔ PWM ¢Í§ª‹Í§ÊÑ­­Ò³·Õµè ͌ §¡ÒÃ

Vs

OUT1

OUT3

Vs

Vss Vs

IN1

ENABLE1

IN3

Vs

Vs

IN2

ENABLE2

IN4

OUT2

OUT4

ÃÙ»·Õè 3-10 áÊ´§â¤Ã§ÊÌҧ¡Ò÷íÒ§Ò¹ÀÒÂ㹢ͧäÍ«Õ L293D «Ö§è 㪌¢ºÑ ÁÍàµÍÏã¹Ëع‹ ¹µ Robo-51


àÃÕ¹ÃÙጠÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ MCS-51 l 65 ´ŒÒ¹Ë¹ŒÒ

Ëع‹ ¹µ Robo-51

ÁÍàµÍÏ M1

«ŒÒÂÁ×Í

ÁÍàµÍÏ M2

¢ÇÒÁ×Í

ÃÙ»·Õè 3-11 áÊ´§¡ÒáíÒ˹´µíÒá˹‹§¢Í§ÁÍàµÍÏ·ãÕè ªŒ¢ºÑ à¤Å×Íè ¹Ëع‹ ¹µ Robo-51 ¨Ò¡ËÅÑ¡¡Òôѧ¡Å‹ÒÇ¢ŒÒ§µŒ¹ÊÒÁÒö¹íÒÁÒÊÌҧ¿˜§¡ªÑè¹ã¹â»Ãá¡ÃÁÀÒÉÒ C â´Â¡íÒ˹´ãˌ ǧ¨Ã¢ÑºÁÍàµÍϪ͋ § 1 㪌¢ºÑ ÁÍàµÍϴҌ ¹«ŒÒÂÁ×Í àÁ×Íè ÁͧËع‹ ¹µ¨Ò¡´ŒÒ¹º¹ áÅÐǧ¨Ã¢ÑºÁÍàµÍÏ ª‹Í§ 2 㪌¢ºÑ ÁÍàµÍϴҌ ¹¢ÇÒÁ×Í ´Ñ§ã¹ÃÙ»·Õè 3-11

3.5.5.1 ÊÌҧ¿˜§¡ª¹Ñè ¢ÑºÁÍàµÍÏãˌ˹؋ ¹µà¤Å×Íè ¹·Õäè »¢ŒÒ§Ë¹ŒÒ ¡ÒâѺà¤Å×Íè ¹ãˌ˹؋ ¹µ Robo-51 à¤Å×Íè ¹·Õµè ç仢ŒÒ§Ë¹ŒÒ·íÒ䴌â´Â¡ÒÃÊѧè ãˌÁÍàµÍÏ 1 áÅÐ 2 ËÁع仢ŒÒ§Ë¹ŒÒ·Ñ§é ¤Ù‹ ʋǹ¡Ãкǹ¡Ò÷ҧ«Í¿µáÇϹ¹Ñé ¨ÐµŒÍ§¡íÒ˹´ãˌâÁ´ÙÅ PCA ·íÒ§Ò¹ã¹âËÁ´ ¡íÒà¹Ô´ÊÑ­­Ò³ PWM ¨‹ÒÂÊÑ­­Ò³ÍÍ¡ä»·Ò§¢Ò P1.6 áÅÐ P1.7 à¾×Íè 㪌㹡ÒäǺ¤ØÁÍѵÃÒàÃçǢͧ ÁÍàµÍÏ áÅÐàÅ×Í¡áËŋ§¡íÒà¹Ô´ÊÑ­­Ò³¹Ò́¡Ò¢Í§âÁ´ÙÅ PCA ¨Ò¡¾ÑÅʏâÍàÇÍÏâ¿ÅǢͧä·àÁÍÏ 0 ¨Ò¡¹Ñ¹é ·íÒ¡ÒÃÊÌҧ¿˜§¡ª¹Ñè motor_fd à¾×Íè 㪌¢ºÑ à¤Å×Íè ¹Ëع‹ ¹µ Robo-51 ãˌà¤Å×Íè ¹·Õäè » ¢ŒÒ§Ë¹ŒÒ â´ÂÁÕ¾ÒÃÒÁÔàµÍÏ 2 µÑÇ·ÕÃè ºÑ ¤‹Òà¾×Íè ä»»ÃÐÁÇżÅÀÒÂ㹿˜§¡ª¹Ñè ¤×Í select_fd ÊíÒËÃѺàÅ×Í¡ª‹Í§¢ÑºÁÍàµÍÏ«§ Öè Áդҋ ໚¹ “1” ËÃ×Í “2” â´Â “1” ËÁÒ¶֧ÁÍàµÍϪ͋ §·Õè 1 (M-1) “2” ËÁÒ¶֧ ÁÍàµÍϪ͋ §·Õè 2 (M-2) 㪌¡Òí ˹´ÍѵÃÒàÃçÇ㹡ÒÃËÁع¢Í§ÁÍàµÍÏ«§Öè Áդҋ µÑ§é ᵋ 0 ¶Ö§ 255 ¶ŒÒ¡íÒ˹´ ãˌ¾ÒÃÒÁÔàµÍÏ speed ãˌÁ¤Õ ҋ ÁÒ¡¡ç¨Ð·íÒãˌÁÍàµÍÏËÁعàÃçǵÒÁä»´ŒÇ speed


66làÃÕ¹ÃÙá Œ ÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ

MCS-51

ÃÒÂÅÐàÍÕ´¢Í§¿˜§¡ª¹Ñè motor_fd ÁÕ´§Ñ ¹Õé #define motor_pulse 115 sbit dir_a1 = P2^0; sbit dir_a2 = P2^1; sbit dir_b1 = P2^2;

// ¡íÒ˹´ãˌ motor_pulse á·¹µÑÇàÅ¢ 115 // ºÔµ¤Çº¤ØÁÁÍàµÍϪ͋ § 1 µ‹ÍࢌҡѺ¢Ò IN1 ¢Í§äÍ«Õ L293D // ºÔµ¤Çº¤ØÁÁÍàµÍϪ͋ § 1 µ‹ÍࢌҡѺ¢Ò IN2 ¢Í§äÍ«Õ L293D // ºÔµ¤Çº¤ØÁÁÍàµÍϪ͋ § 2 µ‹ÍࢌҡѺ¢Ò IN3 ¢Í§äÍ«Õ L293D // ºÔµ¤Çº¤ØÁÁÍàµÍϪ͋ § 2 µ‹ÍࢌҡѺ¢Ò IN4 ¢Í§äÍ«Õ L293D

sbit dir_b2 = P2^3; void motor_fd(unsigned char select_fd, unsigned char speed) { TMOD &= 0xF0; TMOD |= 0x02; TH0 = motor_pulse; TL0 = motor_pulse; TR0 = 1; CMOD = 0x04;

if(select_fd==1) { CCAPM3 = 0x42;

CCAP3L = 255-speed; CCAP3H = 255-speed; dir_a1 = 0; dir_a2 = 1;

// à¤ÅÕÂϢ͌ ÁÙÅ亵Åҋ §¢Í§ÃÕ¨ÊÔ àµÍÏ TMOD à¾×Íè àµÃÕÂÁàÅ×Í¡âËÁ´¢Í§ä·àÁÍÏ 0 // ¡íÒ˹´âËÁ´¢Í§ä·àÁÍÏ 0 ໚¹âËÁ´ 2 (8 ºÔµÃÕâËÅ´¤‹ÒÍѵâ¹Áѵ)Ô // ¡íÒ˹´¤‹ÒÃÕâËÅ´¢Í§ä·àÁÍÏ 0 ໚¹ 115 // ¡íÒ˹´¤‹ÒÃÕâËÅ´¢Í§ä·àÁÍÏ 0 àÃÔÁè µŒ¹à»š¹ 115 // ແ´¡ÒùѺ¢Í§ä·àÁÍÏ 0 // ¡íÒ˹´ãˌ CPS1=1 áÅÐ CPS0=0 à¾×Íè ¡íÒ˹´ãˌ¾ÅÑ ÊâÍàÇÍÏâ¿ÅǨҡ // ä·àÁÍÏ 0 ໚¹áËŋ§¡íÒà¹Ô´ÊÑ­­Ò³¹Ò́¡Ò¢Í§âÁ´ÙÅ PCA // ¶ŒÒàÅ×Í¡¢ÑºÁÍàµÍϪ͋ § 1 ¨Ð·íÒ¤íÒÊѧè 㹺ÅçÍ¡ // ¡íÒ˹´ãˌ PWM3=1 áÅÐ ECOM3=1 à¾×Íè ¡íÒà¹Ô´ÊÑ­­Ò³ // PWM ¨‹ÒÂÍÍ¡·Õ¢è Ò P1.6 à¾×Íè ¤Çº¤ØÁ¡ÒÃËÁع¢Í§ÁÍàµÍϪ͋ § 0 // ¡íÒ˹´ duty cycle ¢Í§ÊÑ­­Ò³ PWM ¡íÒ˹´¨Ò¡ speed // ¡íÒ˹´¤‹ÒÃÕâËÅ´ duty cycle ¢Í§ÊÑ­­Ò³ PWM ¡íÒ˹´¨Ò¡ speed // ͌ҧÍÔ§·ÔÈ¡ÒÃËÁع¢Í§ÁÍàµÍϪ͋ § 1 ËÁع仢ŒÒ§Ë¹ŒÒ // ͌ҧÍÔ§·ÔÈ¡ÒÃËÁع¢Í§ÁÍàµÍϪ͋ § 1 ËÁع仢ŒÒ§Ë¹ŒÒ

} else if(select_fd==2)

// ¶ŒÒàÅ×Í¡¢ÑºÁÍàµÍϪ͋ § 2 ¨Ð·íÒ¤íÒÊѧè 㹺ÅçÍ¡

{ CCAPM4 = 0x42;

CCAP4L = 255-speed; CCAP4H = 255-speed; dir_b1 = 0; dir_b2 = 1;

// ¡íÒ˹´ãˌ PWM4=1 áÅÐ ECOM4=1 à¾×Íè ¡íÒà¹Ô´ÊÑ­­Ò³ PWM // ¨‹ÒÂÍÍ¡·Õ¢è Ò P1.7 à¾×Íè ¤Çº¤ØÁ¡ÒÃËÁع¢Í§ÁÍàµÍϪ͋ § 1 // ¡íÒ˹´ duty cycle ¢Í§ÊÑ­­Ò³ PWM ¡íÒ˹´¨Ò¡ speed // ¡íÒ˹´¤‹ÒÃÕâËÅ´ duty cycle ¢Í§ÊÑ­­Ò³ PWM ¡íÒ˹´¨Ò¡ speed // ͌ҧÍÔ§·ÔÈ¡ÒÃËÁع¢Í§ÁÍàµÍϪ͋ § 2 ËÁع仢ŒÒ§Ë¹ŒÒ // ͌ҧÍÔ§·ÔÈ¡ÒÃËÁع¢Í§ÁÍàµÍϪ͋ § 2 ËÁع仢ŒÒ§Ë¹ŒÒ

} CCON = 0x40; }

// ૵ºÔµ CR à¾×Íè ແ´¡ÒùѺ¢Í§âÁ´ÙÅ PCA


àÃÕ¹ÃÙጠÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ MCS-51 l 67

3.5.5.2 ÊÌҧ¿˜§¡ª¹Ñè ¢ÑºÁÍàµÍÏãˌ˹؋ ¹µà¤Å×Íè ¹·Õ¶è ÍÂËÅѧ ¡ÒâѺà¤Å×Íè ¹Ëع‹ ¹µ Robo-51 ãˌà¤Å×Íè ¹·Õ¶è ÍÂËÅѧ㹷ÔÈ·Ò§µÃ§¹Ñ¹é ÁÕ¢¹Ñé µÍ¹¤ÅŒÒ¡Ѻ¡Òà à¤Å×Íè ¹·Õµè ç仢ŒÒ§Ë¹ŒÒ ·íÒ䴌â´Â¡ÒÃÊѧè ãˌÁÍàµÍÏ 0 áÅÐÁÍàµÍÏ 1 ËÁع仢ŒÒ§ËÅѧ·Ñ§é ¤Ù‹ ¿˜§¡ª¹Ñè ·ÕÊè Ìҧ¢Ö¹é à¾×Íè ·íÒ˹ŒÒ·Õ¹è ¤Õé Í× motor_bk ÁÕ¾ÒÃÒÁÔàµÍÏ 2 µÑÇÃѺ¤‹Òä»»ÃÐÁÇżÅÀÒÂ㹿˜§¡ª¹Ñè ¤×Í select_bk

ÊíÒËÃѺàÅ×Í¡ª‹Í§¢ÑºÁÍàµÍÏ«§Öè Áդҋ ໚¹ “1” ËÃ×Í “2” â´Â

“1” ËÁÒ¶֧ÁÍàµÍϪ͋ §·Õè 1(M-1) “2” ËÁÒ¶֧ ÁÍàµÍϪ͋ §·Õè 2 (M-2) 㪌¡Òí ˹´ÍѵÃÒàÃçÇ㹡ÒÃËÁع¢Í§ÁÍàµÍÏ«§Öè Áդҋ µÑ§é ᵋ 0 ¶Ö§ 255 ¶ŒÒ¡íÒ˹´ ãˌ¾ÒÃÒÁÔàµÍÏ speed ãˌÁ¤Õ ҋ ÁÒ¡¡ç¨Ð·íÒãˌÁÍàµÍÏËÁعàÃçǵÒÁä»´ŒÇ speed

ÃÒÂÅÐàÍÕ´¢Í§¿˜§¡ª¹Ñè motor_bk ÁÕ´§Ñ ¹Õé #define motor_pulse 115 sbit dir_a1 = P2^0; sbit dir_a2 = P2^1; sbit dir_b1 = P2^2;

// ¡íÒ˹´ãˌ motor_pulse á·¹µÑÇàÅ¢ 115 // ºÔµ¤Çº¤ØÁÁÍàµÍϪ͋ § 1 µ‹ÍࢌҡѺ¢Ò IN1 ¢Í§äÍ«Õ L293D // ºÔµ¤Çº¤ØÁÁÍàµÍϪ͋ § 1 µ‹ÍࢌҡѺ¢Ò IN2 ¢Í§äÍ«Õ L293D // ºÔµ¤Çº¤ØÁÁÍàµÍϪ͋ § 2 µ‹ÍࢌҡѺ¢Ò IN3 ¢Í§äÍ«Õ L293D // ºÔµ¤Çº¤ØÁÁÍàµÍϪ͋ § 2 µ‹ÍࢌҡѺ¢Ò IN4 ¢Í§äÍ«Õ L293D

sbit dir_b2 = P2^3; void motor_bk(unsigned char select_bk ,unsigned char speed) { TMOD &= 0xF0;

TMOD |= 0x02; TH0 = motor_pulse; TL0 = motor_pulse; TR0 = 1; CMOD = 0x04;

if(select_bk==1)

// à¤ÅÕÂϢ͌ ÁÙÅ亵Åҋ §¢Í§ÃÕ¨ÊÔ àµÍÏ TMODà¾×Íè àµÃÕÂÁµÑ§é âËÁ´¢Í§ // ä·àÁÍÏ 0 // ¡íÒ˹´âËÁ´¢Í§ä·àÁÍÏ 0 ໚¹âËÁ´ 2 (8 ºÔµÃÕâËÅ´¤‹ÒÍѵâ¹Áѵ)Ô // ¡íÒ˹´¤‹ÒÃÕâËÅ´¢Í§ä·àÁÍÏ 0 ໚¹ 115 // ¡íÒ˹´¤‹ÒÃÕâËÅ´¢Í§ä·àÁÍÏ 0 àÃÔÁè µŒ¹à»š¹ 115 // ແ´¡ÒùѺ¢Í§ä·àÁÍÏ 0 // ¡íÒ˹´ãˌ CPS1=1 áÅÐ CPS0=0 à¾×Íè 㪌ãˌ¾ÅÑ ÊâÍàÇÍÏâ¿ÅǨҡ // ä·àÁÍÏ 0 ໚¹áËŋ§¡íÒà¹Ô´ÊÑ­­Ò³¹Ò́¡Ò¢Í§âÁ´ÙÅ PCA // ¶ŒÒàÅ×Í¡¢ÑºÁÍàµÍϪ͋ § 1 ¨Ð·íÒ¤íÒÊѧè 㹺ÅçÍ¡

{ CCAPM3 = 0x42;

CCAP3L = 255-speed; CCAP3H = 255-speed;

// ¡íÒ˹´ãˌ PWM3=1 áÅÐ ECOM3=1 à¾×Íè ¡íÒà¹Ô´ÊÑ­­Ò³ // PWM ¨‹ÒÂÍÍ¡·Õ¢è Ò P1.6 à¾×Íè ¤Çº¤ØÁ¡ÒÃËÁع¢Í§ÁÍàµÍϪ͋ § 0 // ¡íÒ˹´ duty cycle ¢Í§ÊÑ­­Ò³ PWM ¡íÒ˹´¨Ò¡ speed // ¡íÒ˹´¤‹ÒÃÕâËÅ´ duty cycle ¢Í§ÊÑ­­Ò³ PWM ¨Ò¡ speed


68làÃÕ¹ÃÙá Œ ÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ

MCS-51

// ͌ҧÍÔ§·ÔÈ¡ÒÃËÁع¢Í§ÁÍàµÍϪ͋ § 1 ËÁع仢ŒÒ§Ë¹ŒÒ // ͌ҧÍÔ§·ÔÈ¡ÒÃËÁع¢Í§ÁÍàµÍϪ͋ § 1 ËÁع仢ŒÒ§Ë¹ŒÒ

dir_a1 = 1; dir_a2 = 0; } else if(select_bk==2)

// ¶ŒÒàÅ×Í¡¢ÑºÁÍàµÍϪ͋ § 2 ¨Ð·íÒ¤íÒÊѧè 㹺ÅçÍ¡

{ CCAPM4 = 0x42;

CCAP4L = 255-speed; CCAP4H = 255-speed; dir_b1 = 0; dir_b2 = 1;

// ¡íÒ˹´ãˌ PWM4=1 áÅÐ ECOM4=1 à¾×Íè ¡íÒà¹Ô´ÊÑ­­Ò³ PWM ¨‹Ò // ÍÍ¡·Õ¢è Ò P1.7 à¾×Íè ¤Çº¤ØÁ¡ÒÃËÁع¢Í§ÁÍàµÍϪ͋ § 1 // ¡íÒ˹´ duty cycle ¢Í§ÊÑ­­Ò³ PWM ¡íÒ˹´¨Ò¡ speed // ¡íÒ˹´¤‹ÒÃÕâËÅ´ duty cycle ¢Í§ÊÑ­­Ò³ PWM ¨Ò¡ speed // ͌ҧÍÔ§·ÔÈ¡ÒÃËÁع¢Í§ÁÍàµÍϪ͋ § 2 ËÁع仢ŒÒ§Ë¹ŒÒ // ͌ҧÍÔ§·ÔÈ¡ÒÃËÁع¢Í§ÁÍàµÍϪ͋ § 2 ËÁع仢ŒÒ§Ë¹ŒÒ

}

// ૵ºÔµ CR à¾×Íè ແ´¡ÒùѺ¢Í§âÁ´ÙÅ PCA

CCON = 0x40; }

3.5.5.3 ÊÌҧ¿˜§¡ª¹Ñè ËÂØ´¢ÑºÁÍàµÍÏ ¡ÒÃËÂØ´¢ÑºÁÍàµÍϪ͋ §ã´æ ·íÒ䴌´ÇŒ ¡ÒäǺ¤ØÁÊÀÒÇÐÅͨԡ¢Í§ºÔµ¤Çº¤ØÁ·Ñ§é ÊͧºÔµãˌ ÁÕÊÀÒÇÐàËÁ×͹¡Ñ¹ ¡Å‹ÒǤ×Í ÍÒ¨¨Ð໚¹Åͨԡ “1” ËÃ×Í “0” ·Ñ§é ¤Ù¡‹ äç ´Œ â´Â·ÕÂè §Ñ ¤§ÁÕ¡ÒÃʋ§ÊÑ­­Ò³ PWM ä»ÂѧÁÍàµÍÏÍÂً ʋǹÍÕ¡ÇÕ¸ËÕ ¹Ö§è ¤×ÍËÂØ´¨‹ÒÂÊÑ­­Ò³ PWM ¢Í§ÁÍàµÍϪ͋ §¹Ñ¹é ËÃ×Í·íҷѧé Êͧ ÇÔ¸ÕàÅ¡ç䴌«Ö觨ÐÁÕ¤ÇÒÁṋ¹Í¹ÁÒ¡¡Ç‹Ò ¿˜§¡ª¹Ñè ËÂØ´¢ÑºÁÍàµÍÏ·ÊÕè Ìҧ¢Ö¹é µ‹Í仹ÕÁé ªÕ Í×è Ç‹Ò motor_stop 㪌㹡ÒÃËÂØ´¢ÑºËÃ×ÍÍÕ¡¹ÑÂ Ë¹Ö§è ¡ç¤Í× ¡ÒÃàºÃ¡ (brake) ËÃ×ͺѧ¤Ñºá¡¹ÁÍàµÍϹ¹Ñè àͧ ÁÕ¾ÒÃÒÁÔàµÍÏ 1 µÑǤ×Í select_stop

·ÕÃè ºÑ ¤‹Òà¾×Íè àÅ×Í¡ª‹Í§¢Í§ÁÍàµÍÏ ÁÕà§×Íè ¹ä¢´Ñ§¹Õé

select_stop = 1

àÅ×Í¡àºÃ¡ª‹Í§ 1 à¾Õ§ª‹Í§à´ÕÂÇ

select_stop = 2

àÅ×Í¡àºÃ¡ª‹Í§ 2 à¾Õ§ª‹Í§à´ÕÂÇ

select_stop = 3

àÅ×Í¡àºÃ¡·Ñ§é 2 ª‹Í§

ÃÒÂÅÐàÍÕ´¢Í§¿˜§¡ª¹Ñè motor_stop ÁÕ´§Ñ ¹Õé


àÃÕ¹ÃÙጠÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ MCS-51 l 69 sbit dir_a1 = P2^0; sbit dir_a2 = P2^1; sbit dir_b1 = P2^2;

// ºÔµ¤Çº¤ØÁÁÍàµÍϪ͋ § 1 µ‹ÍࢌҡѺ¢Ò IN1 ¢Í§äÍ«Õ L293D // ºÔµ¤Çº¤ØÁÁÍàµÍϪ͋ § 1 µ‹ÍࢌҡѺ¢Ò IN2 ¢Í§äÍ«Õ L293D // ºÔµ¤Çº¤ØÁÁÍàµÍϪ͋ § 2 µ‹ÍࢌҡѺ¢Ò IN3 ¢Í§äÍ«Õ L293D // ºÔµ¤Çº¤ØÁÁÍàµÍϪ͋ § 2 µ‹ÍࢌҡѺ¢Ò IN4 ¢Í§äÍ«Õ L293D

sbit dir_b2 = P2^3; void motor_stop(char select_stop) { if(select_stop==1) { dir_a1 = 0; dir_a2 = 0; CCAPM3 &= 0xFD;

// ¶ŒÒàÅ×Í¡àºÃ¡ÁÍàµÍϪ͋ § 1 ¨Ð·íÒ¤íÒÊѧè 㹺ÅçÍ¡ // ¡íÒ˹´·ÔÈ·Ò§¢Ñº dir_a1= dir_a2=0 à¾×Íè àºÃ¡ÁÍàµÍϪ͋ § 1 // ¡íÒ˹´·ÔÈ·Ò§¢Ñº dir_a1= dir_a2=0 à¾×Íè àºÃ¡ÁÍàµÍϪ͋ § 1 // à¤ÅÕÂϺµÔ PWM3 à¾×Íè ËÂØ´¡Òè‹ÒÂÊÑ­­Ò³ PWM ÍÍ¡·Õè P1.6

} else if(select_stop==2)

// ¶ŒÒàÅ×Í¡àºÃ¡ÁÍàµÍϪ͋ § 2 ¨Ð·íÒ¤íÒÊѧè 㹺ÅçÍ¡

{ dir_b1 = 0; dir_b2 = 0; CCAPM4 &= 0xFD;

// ¡íÒ˹´·ÔÈ·Ò§¢Ñº dir_b1= dir_b2=0 à¾×Íè àºÃ¡ÁÍàµÍϪ͋ § 2 // ¡íÒ˹´·ÔÈ·Ò§¢Ñº dir_b1= dir_b2=0 à¾×Íè àºÃ¡ÁÍàµÍϪ͋ § 2 // à¤ÅÕÂϺµÔ PWM4 à¾×Íè ËÂØ´¡Òè‹ÒÂÊÑ­­Ò³ PWM ÍÍ¡·Õè P1.7

} else if(select_stop==3) { dir_a1 = 0; dir_a2 = 0; CCAPM3 &= 0xFD; dir_b1 = 0; dir_b2 = 0; CCAPM4 &= 0xFD;

// ¶ŒÒàÅ×Í¡àºÃ¡ÁÍàµÍÏ·§Ñé 2 ª‹Í§ ¨Ð·íÒ¤íÒÊѧè 㹺ÅçÍ¡ // ¡íÒ˹´·ÔÈ·Ò§¢Ñº dir_a1= dir_a2=0 à¾×Íè àºÃ¡ÁÍàµÍϪ͋ § 1 // ¡íÒ˹´·ÔÈ·Ò§¢Ñº dir_a1= dir_a2=0 à¾×Íè àºÃ¡ÁÍàµÍϪ͋ § 1 // à¤ÅÕÂϺµÔ PWM3 à¾×Íè ËÂØ´¡Òè‹ÒÂÊÑ­­Ò³ PWM ÍÍ¡·Õè P1.6 // ¡íÒ˹´·ÔÈ·Ò§¢Ñº dir_b1= dir_b2=0 à¾×Íè àºÃ¡ÁÍàµÍϪ͋ § 2 // ¡íÒ˹´·ÔÈ·Ò§¢Ñº dir_b1= dir_b2=0 à¾×Íè àºÃ¡ÁÍàµÍϪ͋ § 2 // à¤ÅÕÂϺµÔ PWM4 à¾×Íè ËÂØ´¡Òè‹ÒÂÊÑ­­Ò³ PWM ÍÍ¡·Õè P1.7

} }

3.5.5.4 ä¿ÅäźÃÒÃÕ dc_motor.h ÊíÒËÃѺ¢ÑºÁÍàµÍϢͧËع‹ ¹µ Robo-51 ¨Ò¡¿˜§¡ª¹Ñè ¡ÒâѺÁÍàµÍÏ·§Ñé 3 ¿˜§¡ª¹Ñè ·Õáè ¹Ð¹íÒãˌÊÌҧã¹ËÑÇ¢ŒÍ 3.5.5.1 ¶Ö§ 3.5.5.3 ÊÒÁÒö ¹íÒÁÒ¼¹Ç¡à¢ŒÒ´ŒÇ¡ѹà¾×Íè ÊÌҧ໚¹ä¿ÅäźÃÒÃÕ䴌 à¾×Íè ¤ÇÒÁÊдǡáÅÐÃÇ´àÃçÇ㹡ÒþѲ¹Ò㹤ÃÑ§é µ‹Íæä» â´Â¡ÒÃÊÌҧä¿ÅäźÃÒÃÕä·íÒ䴌§‹ÒÂÁÒ¡à¾Õ§à¢Õ¹â»Ãá¡ÃÁ·Õè P3-7 áŌǺѹ·Ö¡à»š¹ä¿Å dc_motor.h ¨Ñ´à¡çºä¿ÅãˌÍÂÙ·‹ âÕè ¿Åà´ÍÏà´ÕÂǡѺä¿Åâ»Ãà¨ç¡µ·¡Õè Òí Åѧ¾Ñ²¹ÒËÃ×Í·Õè C:\RIDE\INC


70làÃÕ¹ÃÙá Œ ÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ

MCS-51

â»Ãá¡ÃÁ·Õè P3-7 ä¿ÅäźÃÒÃÕ dc_motor.h ·ÕÊè Ìҧ¢Ö¹é ÊíÒËÃѺ¢ÑºÁÍàµÍÏ俵ç¢Í§Ëع‹ ¹µ Robo-51 (Áյ͋ ) /*—————————————————————————————————————————————————————————————————————————*/ // Program : Pulse width modulation by PCA module // Description : Generate pulse width modulation signal on CEX3 and CEX4 // : using PCA function // Filename : dc_motor.h // C compiler : RIDE 51 V6.1 /*—————————————————————————————————————————————————————————————————————————*/ #define ch1 1 // Define constant #define ch2 2 // Define constant #define all 3 // Define constant #define motor_pulse 115 // Define constant sbit dir_a1 = P2^0; // bit drive motor0 sbit dir_a2 = P2^1; // bit drive motor0 sbit dir_b1 = P2^2; // bit drive motor1 sbit dir_b2 = P2^3; // bit drive motor1 /*********************** Drive motor backward ********************************/ void motor_bk(unsigned char select_bk ,unsigned char speed) { TMOD &= 0xF0; // Refresh mode timer 0 TMOD |= 0x02; // Setup mode timer 0 (8 bit auto reload) TH0 = motor_pulse; // Reload value for timer 0 TL0 = motor_pulse; // Initial value for count of timer 0 TR0 = 1; // Start timer 0 // Set CMOD PCA count freq. by pulse overflow timer0 CMOD = 0x04; if(select_bk==1) { CCAPM3 = 0x42; // Set CCAP Module 3 as 8 bit PWM CCAP3L = 255-speed; // Set CCAP3L initial value by speed variable // (set duty cycle) CCAP3H = 255-speed; // Set CCAP3H reload value by speed variable // (set duty cycle) dir_a1 = 1; // set direction backward dir_a2 = 0; // set direction backward } else if(select_bk==2) { CCAPM4 = 0x42; // Set CCAP Module 3 as 8 bit PWM CCAP4L = 255-speed; // Set CCAP4L initial value by speed variable(set duty cycle) CCAP4H = 255-speed; // Set CCAP4H initial value by speed variable(set duty cycle) dir_b1 = 1; // set direction backward dir_b2 = 0; // set direction backward } CCON = 0x40; // Set PCA counter run } /*********************** Drive motor forward ********************************/ void motor_fd(unsigned char select_fd, unsigned char speed) { TMOD &= 0xF0; // Refresh mode timer 0 TMOD |= 0x02; // Setup mode timer 0 (8 bit auto reload) TH0 = motor_pulse; // Reload value for timer 0 TL0 = motor_pulse; // Initial value for count of timer 0


àÃÕ¹ÃÙጠÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ MCS-51 l 71

â»Ãá¡ÃÁ·Õè P3-7 ä¿ÅäźÃÒÃÕ dc_motor.h ·ÕÊè Ìҧ¢Ö¹é ÊíÒËÃѺ¢ÑºÁÍàµÍÏ俵ç¢Í§Ëع‹ ¹µ Robo-51 (¨º) TR0 = 1; CMOD = 0x04; if(select_fd==1) { CCAPM3 = 0x42; CCAP3L = 255-speed; CCAP3H = 255-speed; dir_a1 = 0; dir_a2 = 1; } else if(select_fd==2) { CCAPM4 = 0x42; CCAP4L = 255-speed; CCAP4H = 255-speed; dir_b1 = 0; dir_b2 = 1; } CCON = 0x40;

// Start timer 0 // Set CMOD PCA count freq. by pulse overflow timer0

// // // // // // //

Set CCAP Module 3 as 8 bit PWM Set CCAP3L initial value by speed variable (set duty cycle) Set CCAP3H initial value by speed variable (set duty cycle) Set direction forward Set direction forward

// // // // // // //

Set CCAP Module 3 as 8 bit PWM Set CCAP4L initial value by speed variable (set duty cycle) Set CCAP4H initial value by speed variable (set duty cycle) Set direction forward Set direction forward

// Set PCA counter run

} /*********************** Break motor *****************************************/ void motor_stop(char select_stop) { if(select_stop==1) { dir_a1 = 0; // Break motor channel 1 dir_a2 = 0; // Break motor channel 1 CCAPM3 &= 0xFD; // Stop generate PWM at pin P1.6 } else if(select_stop==2) { dir_b1 = 0; // break motor channel 2 dir_b2 = 0; // Break motor channel 2 CCAPM4 &= 0xFD; // Stop generate PWM at pin P1.7 } else if(select_stop==3) { dir_a1 = 0; // Break motor channel 1 dir_a2 = 0; // Break motor channel 1 CCAPM3 &= 0xFD; // Stop generate PWM at pin P1.6 dir_b1 = 0; // Break motor channel 2 dir_b2 = 0; // Break motor channel 2 CCAPM4 &= 0xFD; // Stop generate PWM at pin P1.7 } }


72làÃÕ¹ÃÙá Œ ÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ

MCS-51

3.5.5.5 ¤íÒ͸ԺÒ¿˜§¡ª¹Ñè ¢Í§äźÃÒÃÕ dc_motor.h (1) ¿˜§¡ª¹Ñè motor_bk : ¢ÑºÁÍàµÍ϶ÍÂËÅѧ ÃٻẺ¿˜§¡ªÑè¹ void motor_bk(bit select_bk ,unsigned char speed) ¾ÒÃÒÁÔàµÍÏ bk ÃѺ¤‹Ò¡ÒáíÒ˹´ª‹Í§¢ÑºÁÍàµÍÏ俵ç ¤‹Ò·Õàè »š¹ä»ä´Œ¤Í× 1 áÅÐ 2 speed ÃѺ¤‹ÒÍѵÃÒàÃçÇ㹡ÒâѺÁÍàµÍÏ俵ç Áդҋ 0 ¶Ö§ 255 ¡Òä׹¤‹Ò äÁ‹Á¡Õ Òä׹¤‹Ò ÃÒÂÅÐàÍÕ´ ·íÒ˹ŒÒ·Õ¢è ºÑ ÁÍàµÍ϶ÍÂËÅѧ (2) ¿˜§¡ª¹Ñè motor_fd : ¢ÑºÁÍàµÍÏËÁع仢ŒÒ§Ë¹ŒÒ ÃٻẺ¿˜§¡ªÑè¹ void motor_fd(bit select_fd, unsigned char speed) ¾ÒÃÒÁÔàµÍÏ fd ÃѺ¤‹Ò¡ÒáíÒ˹´ª‹Í§¢ÑºÁÍàµÍÏ俵ç ¤‹Ò·Õàè »š¹ä»ä´Œ¤Í× 1 áÅÐ 2 speed ÃѺ¤‹ÒÍѵÃÒàÃçÇ㹡ÒâѺÁÍàµÍÏ俵ç Áդҋ 0 ¶Ö§ 255 ¡Òä׹¤‹Ò äÁ‹Á¡Õ Òä׹¤‹Ò ÃÒÂÅÐàÍÕ´ ·íÒ˹ŒÒ·Õ¢è ºÑ ÁÍàµÍÏ仢ŒÒ§Ë¹ŒÒ (3) ¿˜§¡ª¹Ñè motor_stop : ËÂØ´¢ÑºÁÍàµÍÏ ÃٻẺ¿˜§¡ªÑè¹ void motor_stop(char select_stop) ¾ÒÃÒÁÔàµÍÏ select_stop ÃѺ¤‹Ò¡ÒáíÒ˹´ª‹Í§¡ÒÃËÂØ´¢ÑºÁÍàµÍÏ俵ç Áդҋ 1, 2 ËÃ×Í 3 ËÃ×Í㪌¤Òí Ç‹Ò all ¡ç䴌 ¡Òä׹¤‹Ò äÁ‹Á¡Õ Òä׹¤‹Ò ÃÒÂÅÐàÍÕ´ ·íÒ˹ŒÒ·ÕËè ÂØ´¢ÑºÁÍàµÍÏËÃ×ÍàºÃ¡ËÃ×ͺѧ¤Ñºá¡¹ÁÍàµÍÏ


àÃÕ¹ÃÙጠÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ MCS-51 l 73

¡Ô¨¡ÃÃÁ·Õè 7

·´Êͺ¡ÒâѺÁÍàµÍÏàº×éͧµŒ¹ A7.1 ແ´â»Ãá¡ÃÁ RIDE áŌÇà¢Õ¹â»Ãá¡ÃÁ·Õè A7-1 ºÑ¹·Ö¡ã¹ª×Íè ä¿Å act0701.c ·íÒ¡ÒäÍÁä¾Å áŌǴÒǹâËÅ´ ä¿ÅÊáً ¼§Ç§¨Ã RBX-51AC2 ¢Í§Ëع‹ ¹µ Robo-51 ´ŒÇÂâ»Ãá¡ÃÁ Flip A7.2 µ‹ÍÊÒÂÁÍàµÍÃà¢ŒÒ·Õ¨è ´Ø µ‹Í´ŒÒ¹ã¹ A7.3 ¡‹Í¹Ãѹâ»Ãá¡ÃÁãˌ¡Ëع‹ ¹µ¢¹Öé à˹×;׹é Ãѹâ»Ãá¡ÃÁ (LED ÊÕà¢ÕÂÇ·Õµè Òí á˹‹§ RUN µÔ´ÊNjҧ) àÃÔÁè µŒ¹¡Ò÷íÒ§Ò¹¨Ðä´ŒÂ¹Ô àÊÕ§ “µÔ´é ” ËÅѧ¨Ò¡¹Ñ¹é ᡹¢Í§ÁÍàµÍÏ·¶Õè ¡Ù µ‹ÍࢌҡѺª‹Í§ M-1 ¨ÐËÁع·Ç¹ à¢çÁ¹Ò́¡Ò¾ÃŒÍÁ¡Ñº LED áÊ´§Ê¶Ò¹Ð¢Í§ª‹Í§ M-1 à»Å‹§áʧÊÕà¢ÕÂÇ (ÍÒ¨ÁÕÊáÕ ´§»¹ÍÂÙº‹ Ҍ §) ʋǹ᡹¢Í§ÁÍàµÍÏ ·Õ¶è ¡Ù µ‹ÍࢌҡѺª‹Í§ M-2 ¨ÐËÁعµÒÁà¢çÁ¹Ò́¡Ò¾ÃŒÍÁ¡Ñº LED áÊ´§Ê¶Ò¹Ð¢Í§ª‹Í§ M-2 à»Å‹§áʧÊÕá´§ «Ö§è Á ÍàµÍÏ·§Ñé ¤Ù¨‹ ÐËÁعÍÂÙ¹‹ Ò¹»ÃÐÁÒ³ 10 ÇÔ¹Ò·Õ áŌÇËÂØ´¾ÃŒÍÁ¡ÑºÁÕÊ­ Ñ ­Ò³àÊÕ§ “µÔ´é ” ´Ñ§¢Ö¹é ÍÕ¡ 1 ¤Ãѧé

â»Ãá¡ÃÁ·Õè A7-1 â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺ·´Åͧ¢ÑºÁÍàµÍϢͧËع‹ ¹µ Robo-51 /*————————————————————————————————————————————————————————————————————————*/ // Program : Basic driving motor // Description : Drive motors on forward and backward for checking direction // Filename : act0701.c // C compiler : RIDE 51 V6.1 /*————————————————————————————————————————————————————————————————————————*/ #include <C51ac2.h> // Declare T89C51AC2’s register #include <sound.h> // Declare Sound library #include <dc_motor.h> // Declare DC motor library #include <delay.h> // Declare Delay library void main() { beep(); motor_fd(1,100); motor_bk(2,100); delay_ms(10000); motor_stop(all); beep(); while(1); }

// // // // // // //

Beep for beginning Drive motor channel 1 forward Drive motor channel 2 backward Delay 10 seconds for obseration motor turning Stop all motors Beep for finishing Endless loop


74làÃÕ¹ÃÙá Œ ÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ

MCS-51


àÃÕ¹ÃÙጠÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ MCS-51 l 75

º··Õè 4 ¢Ñºà¤Å×Íè ¹Ëع‹ ¹µ Robo-51 㹵͹·ŒÒ¢ͧº··Õè 3 ÁÕ¡ÒÃá¹Ð¹íÒáÅз´ÅͧÊÌҧ¿˜§¡ªÑè¹â»Ãá¡ÃÁÀÒÉÒ C à¾×è͢Ѻ ÁÍàµÍϢͧËع‹ ¹µ Robo-51 à¾×Íè µÃǨÊͺ·ÔÈ·Ò§¡ÒÃËÁع ¾ÃŒÍÁ¡Ñ¹¹Ñ¹é 䴌ÃǺÃÇÁ¿˜§¡ª¹Ñè ·Ñ§é ËÁ´ ÊÌҧ໚¹ä¿ÅäźÃÒÃÕà¾×Íè àµÃÕÂÁ㪌㹡ÒÃà¢Õ¹â»Ãá¡ÃÁ¢Ñºà¤Å×Íè ¹Ëع‹ ¹µ 㹺·¹Õ¨é ÐࢌÒÊÙ¡‹ ÒÃà¢Õ¹ â»Ãá¡ÃÁÀÒÉÒ C à¾×Íè ¢Ñºà¤Å×Íè ¹Ëع‹ ¹µ Robo-51 ã¹Åѡɳе‹Ò§æ ´ŒÇ¡ÒÃàÃÕ¡㪌¿§˜ ¡ª¹Ñè ¨Ò¡ä¿Å äźÃÒÃÕµ‹Ò§æ à¾×èͼ¹Ç¡¡Ñ¹à»š¹â»Ãá¡ÃÁµÒÁ¨Ø´»ÃÐʧ¤·Õ赌ͧ¡Òà â´ÂäźÃÒÃÕ·ÕèàÃÕ¡㪌§Ò¹à»š¹ ËÅÑ¡¤×Í dc_motor.h, delay.h , sound.h áÅÐ lcd_robo51.h

4.1 ä¿ÅäźÃÒÃÕËÅÑ¡·Õãè ªŒã¹¡ÒâѺà¤Å×Íè ¹Ëع‹ ¹µ Robo-51 4.1.1 äźÃÒÃÕ dc_motor.h : ¢ÑºÁÍàµÍÏ motor_fd ·íÒ˹ŒÒ·Õ¢è ºÑ ÁÍàµÍÏãˌËÁع仢ŒÒ§Ë¹ŒÒ â´Â¡ÒáíÒ˹´ª‹Í§·Õµè ͌ §¡ÒâѺ (ª‹Í§ 1 ËÃ×Í 2) áÅСíÒÅѧ¢Ñº«Ö§è ÁÕ 255 ÃдѺ â´Â¡Òû‡Í¹¤‹ÒÍСÔÇàÁ¹µ motor_bk ·íÒ˹ŒÒ·Õ¢è ºÑ ÁÍàµÍÏãˌ¶ÍÂËÅѧâ´Â¡ÒáíÒ˹´ª‹Í§·Õµè ͌ §¡ÒâѺ(ª‹Í§ 1 ËÃ×Í 2) áÅСíÒÅѧ¢Ñº«Ö§è ÁÕ 255 ÃдѺ â´Â¡Òû‡Í¹¤‹ÒÍСÔÇàÁ¹µ motor_stop ·íÒ˹ŒÒ·Õàè ºÃ¡ÁÍàµÍÏâ´Â¡íÒ˹´ª‹Í§·Õµè ͌ §¡ÒÃàºÃ¡ (ª‹Í§ 1, 2 ËÃ×Í 3)

4.1.2 äźÃÒÃÕ delay.h : ˹‹Ç§àÇÅÒ delay_100us ·íÒ˹ŒÒ·ÕËè ¹‹Ç§àÇÅÒã¹Ë¹‹Ç 100 äÁâ¤ÃÇÔ¹Ò·Õ (໚¹¤‹Òâ´Â»ÃÐÁÒ³) delay_ms ·íÒ˹ŒÒ·ÕËè ¹‹Ç§àÇÅÒã¹Ë¹‹ÇÂÁÔÅÅÔÇ¹Ô Ò·Õ (໚¹¤‹Òâ´Â»ÃÐÁÒ³)

4.1.3 äźÃÒÃÕ sound.h : ¡íÒà¹Ô´ÊÑ­­Ò³àÊÕ§ sound ·íÒ˹ŒÒ·Õè¡íÒà¹Ô´ÊÑ­­Ò³àÊÕ§â´ÂÊÒÁÒö¡íÒ˹´¤ÇÒÁ¶Õè䴌ã¹Ë¹‹Ç Hz ¾ÃŒÍÁª‹Ç§ àÇÅÒ㹡ÒáíÒà¹Ô´àÊÕ§ beep ·íÒ˹ŒÒ·Õ¡è Òí à¹Ô´ÊÑ­­Ò³àÊÕ§·Õ¤è ÇÒÁ¶Õè 500 Hz ª‹Ç§àÇÅÒ㹡ÒáíÒà¹Ô´¤×Í 0.1 ÇÔ¹Ò·Õ


76làÃÕ¹ÃÙá Œ ÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ

MCS-51

4.1.4 äźÃÒÃÕ lcd_robo51.h : áÊ´§¼Å·Õâè Á´ÙÅ LCD lcd_command ·íÒ˹ŒÒ·ÕÊè §‹ 亵¤Òí Êѧè ä»ÂѧâÁ´ÙÅ LCD lcd_text ·íÒ˹ŒÒ·ÕÊè §‹ 亵¢ÍŒ ÁÙÅÍÑ¡¢ÃÐä»ÂѧâÁ´ÙÅ LCD lcd_init ·íÒ˹ŒÒ·ÕÍè ¹Ô àÔ ªÕÂÅ¡ÒÃµÑ§é ¤‹ÒâËÁ´¡ÒõԴµ‹ÍâÁ´ÙÅ LCD Ẻ 4 ºÔµ lcd_putstr ·íÒ˹ŒÒ·ÕÊè §‹ ªØ´¢ŒÍ¤ÇÒÁ(ÊÒÂÍÑ¡¢ÃÐ)ä»áÊ´§¼ÅÂѧâÁ´ÙÅ LCD inttolcd ·íÒ˹ŒÒ·Õáè »Å§¢ŒÍÁÙŵÑÇàÅ¢áŌÇʋ§¢ŒÍ¤ÇÒÁµÑÇàÅ¢ä»áÊ´§¼ÅÂѧâÁ´ÙÅ LCD

4.2 á¹Ç·Ò§¡ÒâѺà¤Å×Íè ¹Ëع‹ ¹µ Robo-51 ÊíÒËÃѺ¡ÒâѺà¤Å×Íè ¹Ëع‹ ¹µ Robo-51 ãˌÊÁºÙó¨ÐµŒÍ§¤Çº¤ØÁãˌÁÍàµÍÏ·§Ñé 2 µÑÇ·Õµè ͋ ¡Ñº ª‹Í§ M-1 áÅÐ M-2 ·íÒ§Ò¹ÊÑÁ¾Ñ¹¸¡¹Ñ à¾×Íè ãˌ䴌¼ÅÅѾ¸¢Í§¡ÒâѺà¤Å×Íè ¹Ëع‹ ¹µã¹ÅѡɳÐà¤Å×Íè ¹ ·ÕèµÃ§ä»¢ŒÒ§Ë¹ŒÒ, ¶ÍÂËÅѧµÃ§, àÅÕéÂÇ«ŒÒÂ, àÅÕéÂÇ¢ÇÒ áÅÐàºÃ¡ â´ÂÍÒ¨¹íÒ¿˜§¡ªÑè¹ÀÒÂã¹äźÃÒÃÕ dc_motor.h ÁҾѲ¹Ò໚¹¿˜§¡ª¹Ñè ãËÁ‹à¾×Íè ¢Ñºà¤Å×Íè ¹Ëع‹ ¹µ·§Ñé 5 ÃٻẺ «Ö§è µŒÍ§¡íÒ˹´ãˌÁÍàµÍÏ ª‹Í§ M-1 ໚¹ÁÍàµÍÏ·ãÕè ªŒ¢ºÑ Ōͷҧ´ŒÒ¹«ŒÒ áÅÐ M-2 ໚¹ÁÍàµÍÏ·ãÕè ªŒ¢ºÑ Ōͷҧ¢ÇÒ

4.2.1 ¿˜§¡ª¹Ñè ¢Ñºà¤Å×Íè ¹Ëع‹ ¹µµÃ§ä»¢ŒÒ§Ë¹ŒÒ à¡Ô´¨Ò¡¡Ò÷ÕèÁÍàµÍÏ·Ñ駤ً¶Ù¡¢Ñºä»¢ŒÒ§Ë¹ŒÒ â´ÂµÑ駪×èÍ¿˜§¡ªÑè¹à»š¹ run_fd ÁÕ¾ÒÃÒÁÔàµÍÏ time_spin ÊíÒËÃѺ ÃѺ¤‹ÒàÇÅÒ·Õ赌ͧ¡ÒÃ˹‹Ç§à¾×èÍãˌË؋¹Â¹µà¤Å×è͹·ÕèµÃ§ä»¢ŒÒ§Ë¹ŒÒ໚¹ÃÐÂÐ àÇÅÒ·Õµè ͌ §¡Òà ÃÒÂÅÐàÍÕ´¢Í§¿˜§¡ª¹Ñè ÁÕ´§Ñ ¹Õé

Robo-51

#define pow 200 void run_fd(int time_spin) { motor_fd(2,pow); motor_fd(1,pow); delay_ms(time_spin);

// ¢ÑºÁÍàµÍÏ M-2 仢ŒÒ§Ë¹ŒÒ (àÃÕ¡¿˜§¡ª¹Ñè ¨Ò¡äźÃÒÃÕ dc_motor.h) // ¢ÑºÁÍàµÍÏ M-1 仢ŒÒ§Ë¹ŒÒ (àÃÕ¡¿˜§¡ª¹Ñè ¨Ò¡äźÃÒÃÕ dc_motor.h) // ˹‹Ç§àÇÅÒãˌ˹؋ ¹µà¤Å×Íè ¹·Õäè »¢ŒÒ§Ë¹ŒÒ (àÃÕ¡¿˜§¡ª¹Ñè ¨Ò¡äźÃÒÃÕ delay.h)

}

4.2.2 ¿˜§¡ª¹Ñè ¢Ñºà¤Å×Íè ¹Ëع‹ ¹µ¶ÍÂËÅѧµÃ§

à¡Ô´¨Ò¡¡Ò÷ÕèÁÍàµÍÏ·Ñ駤ً¶Ù¡¢Ñºä»´ŒÒ¹ËÅѧ â´ÂµÑ駪×èÍ¿˜§¡ªÑè¹à»š¹ run_bk ÁÕ¾ÒÃÒÁÔàµÍÏ time_spin ÊíÒËÃѺÃѺ¤‹ÒàÇÅÒ·Õµè ͌ §¡ÒÃ˹‹Ç§à¾×Íè ãˌ Ëع‹ ¹µ¶ÍÂËÅѧµÃ§à»š¹ÃÐÂÐàÇÅÒ·Õµè ͌ §¡Òà ÃÒÂÅÐàÍÕ´¢Í§¿˜§¡ª¹Ñè ÁÕ´§Ñ ¹Õé

Robo-51


àÃÕ¹ÃÙጠÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ MCS-51 l 77 #define pow 200 void run_bk(int time_spin) { motor_bk(2,pow); motor_bk(1,pow); delay_ms(time_spin);

// ¢ÑºÁÍàµÍÏ M-2 仢ŒÒ§ËÅѧ (àÃÕ¡¿˜§¡ª¹Ñè ¨Ò¡äźÃÒÃÕ dc_motor.h) // ¢ÑºÁÍàµÍÏ M-1 仢ŒÒ§ËÅѧ (àÃÕ¡¿˜§¡ª¹Ñè ¨Ò¡äźÃÒÃÕ dc_motor.h) // ˹‹Ç§àÇÅÒà¾×Íè ãˌ˹؋ ¹µ¶ÍÂËÅѧ (àÃÕ¡¿˜§¡ª¹Ñè ¨Ò¡äźÃÒÃÕ delay.h)

}

4.2.3 ¿˜§¡ª¹Ñè ¢Ñºà¤Å×Íè ¹Ëع‹ ¹µàÅÕÂé Ç«ŒÒ à¡Ô´¨Ò¡¡Ò÷ÕÁè ÍàµÍÏ·ªÕè ͋ § M-2 ¶Ù¡¢Ñºä»¢ŒÒ§Ë¹ŒÒáÅÐÁÍàµÍÏ·ªÕè ͋ § M-1 ¶Ù¡¢Ñºä»¢ŒÒ§ËÅѧ â´ÂµÑ§é ª×Íè ¿˜§¡ª¹Ñè ໚¹ turn_left ÁÕ¾ÒÃÒÁÔàµÍÏ time_spin ÊíÒËÃѺ ÃѺ¤‹ÒàÇÅÒ·Õµè ͌ §¡ÒÃ˹‹Ç§à¾×Íè ãˌ˹؋ ¹µàÅÕÂé Ç«ŒÒÂ໚¹ÃÐÂÐàÇÅÒ·Õµè ͌ §¡Òà ÃÒ ÅÐàÍÕ´¢Í§¿˜§¡ªÑè¹Áմѧ¹Õé

-51 Robo

#define pow 200 void turn_left(int time_spin) { motor_fd(2,pow); motor_bk(1,pow); delay_ms(time_spin);

// ¢ÑºÁÍàµÍÏ M-2 仢ŒÒ§Ë¹ŒÒ (àÃÕ¡¿˜§¡ª¹Ñè ¨Ò¡äźÃÒÃÕ dc_motor.h) // ¢ÑºÁÍàµÍÏ M-1 仢ŒÒ§ËÅѧ (àÃÕ¡¿˜§¡ª¹Ñè ¨Ò¡äźÃÒÃÕ dc_motor.h) // ˹‹Ç§àÇÅÒà¾×Íè ãˌ˹؋ ¹µËÁعµÑÇ (àÃÕ¡¿˜§¡ª¹Ñè ¨Ò¡äźÃÒÃÕ delay.h)

}

4.2.4 ¿˜§¡ª¹Ñè ¢Ñºà¤Å×Íè ¹Ëع‹ ¹µàÅÕÂé Ç¢ÇÒ

à¡Ô´¨Ò¡ÁÍàµÍÏ·ªÕè ͋ § M-2 ¶Ù¡¢Ñºä»¢ŒÒ§ËÅѧáÅÐÁÍàµÍÏ·ªÕè ͋ § M-1 ¶Ù¡¢Ñº 仢ŒÒ§Ë¹ŒÒ µÑ§é ª×Íè ¿˜§¡ª¹Ñè ໚¹ turn_right ÁÕ¾ÒÃÒÁÔàµÍÏ time_spin ÊíÒËÃѺ ÃѺ¤‹ÒàÇÅÒ·Õµè ͌ §¡ÒÃ˹‹Ç§à¾×Íè ãˌ˹؋ ¹µàÅÕÂé Ç¢ÇÒ໚¹ÃÐÂÐàÇÅÒ·Õµè ͌ §¡Òà ÃÒ ÅÐàÍÕ´¢Í§¿˜§¡ª¹Ñè ÁÕ´§Ñ ¹Õé

Rob o-5 1

#define pow 200 void turn_right(int time_spin) { motor_bk(2,pow); motor_fd(1,pow); delay_ms(time_spin); }

// ¢ÑºÁÍàµÍÏ M-2 仢ŒÒ§ËÅѧ (àÃÕ¡¿˜§¡ª¹Ñè ¨Ò¡äźÃÒÃÕ dc_motor.h) // ¢ÑºÁÍàµÍÏ M-1 仢ŒÒ§Ë¹ŒÒ (àÃÕ¡¿˜§¡ª¹Ñè ¨Ò¡äźÃÒÃÕ dc_motor.h) // ˹‹Ç§àÇÅÒà¾×Íè ãˌ˹؋ ¹µËÁعµÑÇ (àÃÕ¡¿˜§¡ª¹Ñè ¨Ò¡äźÃÒÃÕ delay.h)


78làÃÕ¹ÃÙá Œ ÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ

MCS-51

4.2.5 ¿˜§¡ª¹Ñè ËÂØ´Ëع‹ ¹µ Áմѧ¹Õé

à¡Ô´¨Ò¡¡Ò÷Õàè ºÃ¡ÁÍàµÍÏ·§Ñé 2 µÑÇ ·íÒãˌ˹؋ ¹µËÂØ´¡ÒÃà¤Å×Íè ¹·Õè ÃÒÂÅÐàÍÕ´¢Í§¿˜§¡ª¹Ñè void stop(void) { motor_stop(3);

// àºÃ¡ÁÍàµÍÏ·ªÕè ͋ § M-1 áÅÐ M-2 (àÃÕ¡¨Ò¡äźÃÒÃÕ dc_motor.h)

}

¨Ò¡¿˜§¡ª¹Ñè ¤Çº¤ØÁ¡ÒâѺà¤Å×Íè ¹Ëع‹ ¹µ Robo-51 àº×Íé µŒ¹ ÊÒÁÒö¨Ñ´à¡çºänj໚¹äźÃÒÃÕµÒÁ ¤ÇÒÁÊдǡ¢Í§¼ÙŒ¾Ñ²¹Òàͧ䴌

¡Ô¨¡ÃÃÁ·Õè 8

¢Ñºà¤Å×Íè ¹Ëع‹ ¹µ Robo-51 àº×Íé §µŒ¹ A8.1 ແ´â»Ãá¡ÃÁ RIDE áŌÇà¢Õ¹â»Ãá¡ÃÁ·Õè A8-1 ºÑ¹·Ö¡ã¹ª×Íè ä¿Å act0801.c ·íÒ¡ÒäÍÁä¾Å áŌǴÒǹâËÅ´ ä¿ÅÊáً ¼§Ç§¨Ã RBX-51AC2 ¢Í§Ëع‹ ¹µ Robo-51 ´ŒÇÂâ»Ãá¡ÃÁ Flip A8.2 »Å´ÊÒÂàª×Íè Áµ‹Í¤ÍÁ¾ÔÇàµÍÏÍÍ¡ áŌǷíÒ¡ÒÃÃѹâ»Ãá¡ÃÁ Ëع‹ ¹µ Robo-51 à¤Å×Íè ¹·Õäè »¢ŒÒ§Ë¹ŒÒ໚¹àÇÅÒ 2 ÇÔ¹Ò·Õ áŌǶÍÂËÅѧ 2 ÇÔ¹Ò·ÕÊÅѺä»ÁÒ ·Ø¡¤ÃÑ§é ·ÕÁè Õ à»ÅÕÂè ¹·ÔÈ¡ÒÃà¤Å×Íè ¹·Õè ¨ÐÁÕàÊÕ§ÊÑ­­Ò³´Ñ§¢Ö¹é à¾×Íè ᨌ§¡ÒÃà»ÅÕÂè ¹·ÔÈ·Ò§·Ø¡¤Ãѧé ãˌáÊ´§àÊÕ§ÊÑ­­Ò³ 1 ¤Ãѧé

ËÒ¡äÁ‹ä´ŒµÒÁ¹ÕéáÊ´§Ç‹Ò ¡Òõ‹ÍÊÒ¢ͧÁÍàµÍÏÍÒ¨äÁ‹¶Ù¡¢ÑéÇ ãˌ·íÒ¡ÒÃÊÅѺ¡Òõ‹ÍÊÒ â´ÂÊѧࡵ¨Ò¡Êբͧ LED ¡ç䴌 â´Â¶ŒÒËع‹ ¹µà¤Å×Íè ¹·Õäè »¢ŒÒ§Ë¹ŒÒ LED ¢Í§Ç§¨Ã¢ÑºÁÍàµÍÏ ·Ñ§é Êͧª‹Í§µŒÍ§à»š¹ÊÕà¢ÕÂÇ áÅÐàÁ×Íè Ëع‹ ¹µ¶ÍÂËÅѧ LED ¨Ðà»ÅÕÂè ¹à»š¹ÊÕá´§·Ñ§é ¤Ù‹

àÁ×Íè ÊÅѺÊÒ¨¹¡ÃÐ·Ñ§è ·íҧҹ䴌¶¡Ù µŒÍ§áÅŒÇ ¢ÍãËŒÂ´Ö µíÒá˹‹§¡Òõ‹Í¹Õàé ÍÒänj à¾ÃÒÐ ¨Ð㪌¡Òõ‹Í㹡Ԩ¡ÃÃÁ¹Õàé »š¹ËÅѡ㹡ÒâѺà¤Å×Íè ¹Ëع‹ ¹µ Robo-51 ·Ñ§é ËÁ´ A8.3 à¢Õ¹â»Ãá¡ÃÁ·Õè A8-2 ºÑ¹·Ö¡ã¹ª×Íè ä¿Å act0802.c ·íÒ¡ÒäÍÁä¾Å áŌǴÒǹâËÅ´ä¿ÅÊáً ¼§Ç§¨Ã RBX51AC2 plus ¢Í§Ëع‹ ¹µ Robo-51 ´ŒÇÂâ»Ãá¡ÃÁ Flip A8.5 »Å´ÊÒÂàª×Íè Áµ‹Í¤ÍÁ¾ÔÇàµÍÏÍÍ¡ áŌǷíÒ¡ÒÃÃѹâ»Ãá¡ÃÁ Ëع‹ ¹µ Robo-51 ¨Ðà¤Å×Íè ¹·Õàè »š¹ÃٻẺ´Ñ§µ‹Í仹Õé ¾ÃŒÍÁ¡ÑºáÊ´§¢ŒÍ¤ÇÒÁᨌ§ÃٻẺ¡ÒÃà¤Å×Íè ¹·Õ·è Õè âÁ´ÙÅ LCD ´ŒÇÂ


àÃÕ¹ÃÙጠÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ MCS-51 l 79

â»Ãá¡ÃÁ·Õè A8-1 â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺ·´Åͧ¢Ñºà¤Å×Íè ¹Ëع‹ ¹µ Robo-51 ¾×¹é °Ò¹ /*—————————————————————————————————————*/ // Program : Basic Move // Description : Robo-51 movement by simple fuction // Filename : act0801.c // C compiler : RIDE 51 V6.1 /*—————————————————————————————————————*/ #include <C51ac2.h> // Header include register of P89C51AC2 #include <lcd.h> // Module function LCD display #include <dc_motor.h> // Module function drive DC motor #include <sound.h> // Module function drive sound #include <delay.h> // Module function delay time #define pow 200 // Define constant for power drive DC motor void run_fd(int time_spin) { motor_fd(2,pow); // Motor channel 2 forward motor_fd(1,pow); // Motor channel 1 forward delay_ms(time_spin); // Delay time for robot driving forward } void run_bk(int time_spin) { motor_bk(2,pow); // Motor channel 2 backward motor_bk(1,pow); // Motor channel 1 backward delay_ms(time_spin); // Delay time for robot driving backward } void main() { while(1) { run_fd(2000); beep(); run_bk(2000); beep(); } }

// Infinite loop // // // //

Run forward 2 seconds Sound 1 time Run backward 2 seconds Sound 1 time

(1) à¤Å×Íè ¹·Õµè ç仢ŒÒ§Ë¹ŒÒ 2 ÇÔ¹Ò·Õ ¾ÃŒÍÁ¡ÑºáÊ´§¢ŒÍ¤ÇÒÁ Forward ·Õâè Á´ÙÅ LCD (2) ËÁعµÑÇàÅÕÂé Ç«ŒÒÂ໚¹àÇÅÒ 0.5 ÇÔ¹Ò·Õ ¾ÃŒÍÁ¡ÑºáÊ´§¢ŒÍ¤ÇÒÁ Turn

Left ·Õâè Á´ÙÅ LCD

(3) à¤Å×Íè ¹·Õµè ç仢ŒÒ§Ë¹ŒÒ 2 ÇÔ¹Ò·Õ ¾ÃŒÍÁ¡ÑºáÊ´§¢ŒÍ¤ÇÒÁ Forward ·Õâè Á´ÙÅ LCD (4) ËÁعµÑÇàÅÕÂé Ç¢ÇÒ໚¹àÇÅÒ 0.5 ÇÔ¹Ò·Õ ¾ÃŒÍÁ¡ÑºáÊ´§¢ŒÍ¤ÇÒÁ Turn Right ·Õâè Á´ÙÅ LCD (5) ǹÃͺ·íÒ¢Ñ¹é µÍ¹·Õè 1-4 ¹Õäé »µÅÍ´


80làÃÕ¹ÃÙá Œ ÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ

MCS-51

â»Ãá¡ÃÁ·Õè A8-2 â»Ãá¡ÃÁ·´Åͧ¢Ñºà¤Å×Íè ¹Ëع‹ ¹µ Robo-51 ¾ÃŒÍÁáÊ´§¡Ò÷íÒ§Ò¹¼‹Ò¹âÁ´ÙÅ LCD /*—————————————————————————————————————————————————————————————————————————*/ // Program : Basic Move -2 // Description : Robo-51 movement by simple fuction with display operation on LCD // Filename : act0802.c // C compiler : RIDE 51 V6.1 /*—————————————————————————————————————————————————————————————————————————*/ #include <C51ac2.h> // Header include register of P89C51AC2 #include <lcd_robo51.h> // Module function LCD display #include <dc_motor.h> // Module function drive DC motor #include <sound.h> // Module function drive sound #include <delay.h> // Module function delay time #define pow 200 // Define constant for power drive DC motor void run_fd(int time_spin) { motor_fd(2,pow); // motor_fd(1,pow); // delay_ms(time_spin); // } void run_bk(int time_spin) { motor_bk(2,pow); // motor_bk(1,pow); // delay_ms(time_spin); // } void turn_left(int time_spin) { motor_fd(2,pow); // motor_bk(1,pow); // delay_ms(time_spin); // } void turn_right(int time_spin) { motor_bk(2,pow); // motor_fd(1,pow); // delay_ms(time_spin); // } void main() { lcd_init(); while(1) { lcd_command(0x01); lcd_putstr(line1,” run_fd(2000);

} }

Motor channel 2 forward Motor channel 1 forward Delay time for robot drive forward

Motor channel 2 backward Motor channel 1 backward Delay time for robot drive backward

Motor channel 2 forward Motor channel 1 backward Delay time for robot spining turn left

Motor channel 2 backward Motor channel 1 forward Delay time for robot spining turn right

// Initial LCD module // Infinite loop Forward”);

// Clear display // Show message // Run forward 2 seconds

lcd_command(0x01); lcd_putstr(line1,” turn_left(500);

Turn Left”);

// Clear display // Show message // Turn left 0.5 second

lcd_command(0x01); lcd_putstr(line1,” run_fd(2000);

Forward”);

// Clear display // Show message // Run forward 2 seconds

lcd_command(0x01); lcd_putstr(line1,” turn_right(500);

Turn Right”);

// Clear display // Show message // Turn Right 0.5 second


àÃÕ¹ÃÙጠÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ MCS-51

l 81

º··Õè 5 ¡ÒõԴµ‹Í¡ÑºµÑǵÃǨ¨Ñºáººµ‹Ò§æ ¢Í§Ëع‹ ¹µ Robo-51 ʋǹ»ÃСͺ·ÕèÊíҤѭÍÕ¡»ÃСÒÃ˹Ö觢ͧË؋¹Â¹µÍѵâ¹ÁѵԤ×Í µÑǵÃǨ¨ÑºËÃ×Íૹà«ÍÏ (sensor) Ëع‹ ¹µ¨ÐÊÒÁÒöµÑ´ÊÔ¹ã¨à¤Å×Íè ¹·ÕËè Ã×Í¡ÃзíÒ¡ÒÃã´¡çµÒÁ µŒÍ§ÍÒÈÑ¢ŒÍÁÙŨҡÊÀÒ¾áÇ´ ŌÍÁ à¾×Íè ¹íÒÁÒ»ÃÐÁÇżÅáŌǵѴÊԹ㨠µÑǵÃǨ¨ÑºÁմnj ¡ѹËÅÒ¡ËÅÒÂÃٻẺ·Ñ§é µÑǵÃǨ¨Ñº¡Òê¹ áʧ àʌ¹ ÍسËÀÙÁÔ ÏÅÏ áµ‹¶ÒŒ ËÒ¡¹íÒÁҨѴ¡ÅØÁ‹ µÒÁÅѡɳТͧÊÑ­­Ò³áŌǨÐẋ§Í͡໚¹ 2 ¡ÅØÁ‹ ¤×Í µÑǵÃǨ¨ÑºÊÑ­­Ò³ÍйÒÅÍ¡ áÅеÑǵÃǨ¨ÑºÊÑ­­Ò³´Ô¨µÔ ÍÅ ÊíÒËÃѺË؋¹Â¹µ Robo-51 ÊÒÁÒöµÔ´µ‹Í¡ÑºµÑǵÃǨ¨Ñºä´Œ·Ñé§ÊͧẺ à¹×èͧ¨Ò¡ÀÒÂã¹ T89C51AC2 «Ö§è ໚¹äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏËÅÑ¡·Õãè ªŒ¤Çº¤ØÁ¡Ò÷íÒ§Ò¹¢Í§Ëع‹ ¹µ Robo-51 ÁÕâÁ´Ù Åá»Å§ÊÑ­­Ò³ÍйÒÅ͡໚¹´Ô¨ÔµÍÅ ¤ÇÒÁÅÐàÍÕ´ 10 ºÔµÍÂًÀÒÂã¹µÑÇ ¨Ö§·íÒãˌÊÒÁÒöà¢Õ¹ â»Ãá¡ÃÁà¾×Íè ÃѺÃÙʌ ­ Ñ ­Ò³ÍйÒÅ͡䴌§Ò‹  áÅÐäÁ‹µÍŒ §¡ÒÃäÍ«Õá»Å§ÊÑ­­Ò³ÍйÒÅ͡໚¹´Ô¨µÔ ÍÅ à¾ÔÁè àµÔÁ ã¹ÃÙ»·Õè 5-1 áÊ´§¢ŒÍÁÙÅâ´ÂÊÃØ»¢Í§á¼§Ç§¨ÃµÃǨ¨Ñºáººµ‹Ò§æ ·Õãè ªŒã¹Ëع‹ ¹µ Robo-51

5.1 µÃǨ¨ÑºÊÑ­­Ò³´Ô¨µÔ ÍŨҡἧǧ¨ÃÊÇÔµª ¡ÒõÃǨ¨ÑºÊÑ­­Ò³·Õ§è ҋ ·ÕÊè ´Ø ¢Í§Ëع‹ ¹µ Robo-51 ¤×Í ¡ÒõÃǨ¨ÑºÊÑ­­Ò³´Ô¨µÔ ÍÅ «Öè§ ã¹·Õ¹è ¨Õé Ð㪌ἧǧ¨ÃÊÇÔµªÁÒ¹íÒàʹÍ໚¹µÑÇÍ‹ҧ â´Âἧǧ¨ÃÊÇÔµªÊÒÁÒö¹íÒÁÒ㪌ÊÒí ËÃѺµÃǨ ¨Ñº¡Òê¹ÊÔ§è ¡Õ´¢ÇÒ§ ËÃ×Í»ÃÐÂØ¡µãªŒà»š¹ÊÇÔµªÊÒí ËÃѺÊÑ§è §Ò¹ÀÒ¹͡䴌 ǧ¨ÃÊÁºÙó¢Í§á¼§Ç§¨ÃÊÇÔµªáÊ´§ã¹ÃÙ»·Õè 5-2 ἧǧ¨ÃÊÇÔµª¨ÐãˌàÍÒµ¾µØ ÍÍ¡ÁÒ໚¹ ÊÀÒÇÐÅͨԡ “1” àÁ×Íè äÁ‹Á¡Õ Òá´ áÅÐÅͨԡ “0” àÁ×Íè ¡´ÊÇÔµª â´ÂÁÕ LED áÊ´§¼Å¡Òá´ÊÇÔµª ´Ñ§¹Ñ¹é àÁ×Íè µŒÍ§¡ÒÃ㪌§Ò¹Ã‹ÇÁ¡ÑºËع‹ ¹µ Robo-51 ¨Ö§µŒÍ§µ‹ÍࢌҡѺ¾Íϵ´Ô¨µÔ ÍŢͧºÍÏ´ RBX-51AC2 «Ö§è ÁÕãˌàÅ×Í¡ 5 ª‹Í§ ¤×Í P3.2, P3.3, P3.4, P4.0 áÅÐ P4.1 ੾ÒТҾÍϵ P3.2 áÅÐ P3.3 ÊÒÁÒöà¢Õ¹ â»Ãá¡ÃÁÃѺ¤‹ÒÊÇÔµªâ´Â㪌¡ÒõͺʹͧÍÔ¹àµÍÏÃÑ»µ¨Ò¡ÀÒ¹͡䴌


82 làÃÕ¹ÃÙá Œ ÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ

MCS-51

ZX-01 ἧǧ¨ÃÊÇÔµªµÃǨ¨Ñº¡Òê¹ (Touch sensor) l àÁ×Í è ÊÇÔµªäÁ‹¶¡Ù ¡´ ãˌ¼Å໚¹Åͨԡ “1” l àÁ×Í è ÊÇÔµª¶¡Ù ¡´ ãˌ¼Å໚¹Åͨԡ “0”

ÊÒÁÒö㪌¢Ò Data ·íÒ˹ŒÒ·Õè໚¹ÍÔ¹¾ØµÊíÒËÃѺ ÊÑ§è ¡Òà LED º¹á¼§Ç§¨Ããˌµ´Ô ´ÑºµÒÁµŒÍ§¡ÒÃ䴌 l

ER-4 ÃÕâÁµ¤Í¹â·ÃÅÍÔ¹¿ÃÒàô ZX-03 ἧǧ¨ÃµÃǨ¨ÑºáʧÊзŒÍ¹ÍÔ¹¿ÃÒàô l ÃÐÂзíÒ¡Òà 8 ¶Ö§10 àÁµÃã¹ (Infrared reflect sensor) ¾×é¹·Õèâŋ§ l ºÃèØâÁ´ÙÅ TCRT5000 «Ö§è ÃÇÁàÍÒ LED ÍÔ¹¿ÃÒàô

l ÁÕÊÇÔµª¤Çº¤ØÁ 4 µÑÇ Ê‹§¢ŒÍÁÙÅ áÅÐâ¿âµŒ·ÃÒ¹«ÔÊàµÍÏänjÀÒÂã¹ ÁÕÃÐÂеÃǨ¨Ñº¡Òà ¢Í§ÊÇÔµªà»š¹ÃËÑÊáÍÊ¡Õé ÊзŒÍ¹·Õ´è ·Õ ÊÕè ´Ø »ÃÐÁÒ³ 6 ÁÔÅÅÔàÁµÃ l 㪌ä¿àÅÕÂ é § +2.4 ¶Ö§ +3.0V ¨Ò¡ l ãˌ¼ÅÅѾ¸à»š¹áç´Ñ¹ä¿µÃ§ â´Â¤‹Ò¨ÐÁÒ¡ËÃ×Í áºµàµÍÃÕè AA 2 ¡ŒÍ¹ ¹ŒÍ¡碹Öé ÍÂÙ¡‹ ºÑ ¤ÇÒÁࢌÁ¢Í§áʧÍÔ¹¿ÃÒàô·Õäè ´ŒÃºÑ lʋ§¢ŒÍÁÙÅ͹ءÃÁ´ŒÇÂÍѵÃҺʹ 1,200 ºÔµµ‹ÍÇÔ¹Ò·Õ GP2D120 âÁ´ÙÅÇÑ´ÃÐÂзҧ´ŒÇÂáʧÍÔ¹¿ÃÒàô l 㪌§Ò¹¡Ñºá¼§Ç§¨ÃâÁ´ÙÅÃѺáʧ (Infrared distance sensor) ÍÔ¹¿ÃÒàô 38kHz l ÇÑ´ÃÐÂзҧã¹Â‹Ò¹ 4 ¶Ö§ 30 ૹµÔàÁµÃ l ãˌ¼ÅÅѾ¸à»š¹áç´Ñ¹ä¿µÃ§á»Ã¼¡¼Ñ¹¡ÑºÃÐÂÐ ·Ò§·ÕèÇѴ䴌¤×Í ãˌáç´Ñ¹ä¿µÃ§àÍÒµ¾ØµÅ´Å§àÁ×èÍ ÃÐÂзҧ·Õµè ÃǨ¨Ñº¨Ò¡Çѵ¶ØÍÂÙä‹ ¡Å

ZX-IRM ἧǧ¨ÃâÁ´ÙÅÃѺáʧ ZX-09 ἧǧ¨ÃµÑǵŒÒ¹·Ò¹»ÃѺ¤‹Ò䴌Ẻ ÍÔ¹¿ÃÒàô 38kHz â»à·¹ªÔâÍÁÔàµÍÏ (Potentiometer) (38kHz IR module)

㪌¡Òí ˹´áç´Ñ¹µÒÁ¡ÒÃËÁع᡹ l 㪌µÃǨ¨ÑºáʧÍÔ¹¿ÃÒàô·Õ¼ è ÊÁ l ãˌàÍÒµ¾µ Ø 2 Ẻ¤×Í áç´Ñ¹ÁÒ¡¢Ö¹é àÁ×Íè ËÁع ÊÑ­­Ò³¾Òˏ¤ÇÒÁ¶Õè 38 kHz ·Ç¹à¢çÁ¹Ò́¡ÒËÃ×Í µÒÁà¢çÁ¹Ò́¡Ò l

¹Í¡¨Ò¡µÑǵÃǨ¨Ñº·ÕÁè ãÕ ¹ªØ´ Robo-51 áÅŒÇ Ëع‹ ¹µ Robo-51 ÂѧÊÒÁÒöµÔ´µ‹Í¡ÑºµÑǵÃǨ¨Ñºä´ŒÍ¡Õ ËÅÒ¡ËÅÒÂÍÒ·Ô SRF05 âÁ´ÙÅÇÑ´ÃÐÂзҧ´ŒÇ¤Å×¹è ÍÑŵÌÒ⫹ԡ, CMPS03 âÁ´ÙÅà¢çÁ·ÔÈÍÔàÅç¡·Ã͹ԡʏ, SHT11 µÑǵÃǨ¨Ñº¤ÇÒÁª×¹é , MEMSIC2125 âÁ´ÙÅÇÑ´¤ÇÒÁà˧ 2 ᡹, ZX-TILT âÁ´ÙŵÃǨ¨Ñº¤ÇÒÁàÍÕ§, ZX-SOUND âÁ´ÙŵÃǨ¨ÑºàÊÕ§ ÏÅÏ ´Ù¢ÍŒ ÁÙÅà¾ÔÁè àµÔÁ·Õè www.inex.co.th

ÃÙ»·Õè 5-1 ¢ŒÍÁÙÅÍ‹ҧÊÃØ»¢Í§á¼§Ç§¨ÃµÃǨ¨Ñºáººµ‹Ò§æ ·Õãè ªŒã¹ªØ´Ëع‹ ¹µ Robo-51 áÅÐÍØ»¡Ã³ ·Õàè ¡ÕÂè Ç¢ŒÍ§


àÃÕ¹ÃÙጠÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ MCS-51

l 83

+V LED1 R1 510

R2 10k DATA

S1 GND

ÃÙ»·Õè 5-2 ǧ¨Ã¢Í§á¼§Ç§¨ÃÊÇÔµª·ãÕè ªŒã¹Ëع‹ ¹µ Robo-51

¡Ô¨¡ÃÃÁ·Õè 9

ÃѺ¤‹ÒÊÑ­­Ò³´Ô¨ÔµÍŨҡἧǧ¨Ã ÊÇÔµª¢Í§Ëع‹ ¹µ Robo-51 A9.1 ແ´â»Ãá¡ÃÁ RIDE áŌÇà¢Õ¹â»Ãá¡ÃÁ·Õè A9-1 ºÑ¹·Ö¡ã¹ª×Íè ä¿Å act0901.c ·íÒ¡ÒäÍÁä¾Å áŌǴÒǹâËÅ´ ä¿ÅÊáً ¼§Ç§¨Ã RBX-51AC2 ¢Í§Ëع‹ ¹µ Robo-51 ´ŒÇÂâ»Ãá¡ÃÁ Flip A9.2 µ‹ÍÊÒ¨ҡἧǧ¨ÃÊÇÔµªà¢ŒÒ·Õ¨è ´Ø µ‹Í P3.2 ¢Í§á¼§Ç§¨Ã¤Çº¤ØÁËع‹ ¹µ Robo-51 A9.3 áŌǷíÒ¡ÒÃÃѹâ»Ãá¡ÃÁ áŌǷ´Åͧ¡´ÊÇÔµª ·Õâè Á´ÙÅ LCD áÊ´§¢ŒÍ¤ÇÒÁ Input P3.2 Read ·Õºè Ã÷Ѵº¹ áÅÐáÊ´§Ê¶Ò¹ÐÍÔ¹¾Øµ´Ô¨µÔ ÍÅ (“0” ËÃ×Í “1”) ·Õºè Ã÷Ѵŋҧ àÁ×Íè äÁ‹Á¡Õ Òá´ÊÇÔµª ¨Ð͋ҹ¤‹Ò䴌໚¹ “1” áÅÐ໚¹ “0” àÁ×Íè ÁÕ¡Òá´ÊÇÔµª


84 làÃÕ¹ÃÙá Œ ÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ

MCS-51

â»Ãá¡ÃÁ·Õè A9-1 â»Ãá¡ÃÁ·´Êͺ¡ÒÃ͋ҹ¤‹ÒÊÑ­­Ò³´Ô¨µÔ ÍŨҡἧǧ¨ÃÊÇÔµª¢Í§Ëع‹ ¹µ Robo-51 /*—————————————————————————————————————*/ // Program : Reading digital input // Description : Reading digital signal from switch by polling method // Filename : act0901.c // C compiler : RIDE 51 V6.1 /*—————————————————————————————————————*/ #include <C51ac2.h> // Header include register of P89C51AC2 #include <lcd_robo51.h> // Module function LCD display #include <delay.h> // Module function Delay sbit sw1 = P3^2; // Define variable for P3.2 void main(void) { sw1 = 1; // Write logic “1” for setting P3.2 to input lcd_init(); // LCD initialize lcd_putstr(line1,” Input P3.2 Read”); // Show “Input P3.2 Read” on LCD while(1) // Loop { inttolcd(0xC7,sw1,Dec); // Read P3.2 data to convert and send to // LCD for displaying on lower line } }

ÃÒÂÅÐàÍÕ´à¾ÔÁè àµÔÁ¢Í§â»Ãá¡ÃÁ ÊíÒËÃѺ¡Ò÷íÒ§Ò¹¢Í§â»Ãá¡ÃÁ¹Õé¨Ð໚¹¡ÒÃ͋ҹ¤‹Ò¡Òá´ÍÔ¹¾ØµÊÇÔµª·Õ赋ÍࢌҡѺ P3.2 â´ÂǹÃͺ µÃǨÊͺËÅѧ¨Ò¡¹Ñ¹é ¨Ö§¹íÒ¤‹Ò·ÕÍè ҋ ¹ä´Œ«§Öè ໚¹µÑÇàÅ¢¨íҹǹàµçÁä»á»Å§ãˌ໚¹ÃËÑÊáÍÊ¡Õàé ¾×Íè ʋ§ä»áÊ´§¼Å¤‹Ò Åͨԡ·ÕÍè ҋ ¹ä´Œ·ËÕè ¹ŒÒ¨ÍâÁ´ÙÅ LCD â´Âª‹Ç§¡´ÊÇÔµª¨Ð͋ҹ¤‹Ò䴌໚¹Åͨԡ “0” ᵋ¶ÒŒ ã¹¢³Ð·ÕÂè §Ñ äÁ‹Á¡Õ Òá´ Robo-51 ÊÇÔµª¤Ò‹ ·ÕÍè ҋ ¹ä´Œ¨Ð໚¹Åͨԡ “1”

¡Ô¨¡ÃÃÁ·Õè 10

ÃѺ¤‹ÒÊÑ­­Ò³´Ô¨µÔ ÍŨҡἧǧ¨ÃÊÇÔµª ¢Í§Ëع‹ ¹µ Robo-51 â´Â㪌¡ÒÃÍÔ¹àµÍÃÃ»Ñ µ A10.1 ແ´â»Ãá¡ÃÁ RIDE áŌÇà¢Õ¹â»Ãá¡ÃÁ·Õè A10-1 ºÑ¹·Ö¡ã¹ª×Íè ä¿Å act1001.c ·íÒ¡ÒäÍÁä¾Å áŌǴÒǹ âËÅ´ä¿ÅÊáً ¼§Ç§¨Ã RBX-51AC2 ¢Í§Ëع‹ ¹µ Robo-51 ´ŒÇÂâ»Ãá¡ÃÁ Flip A10.2 µ‹ÍÊÒ¨ҡἧǧ¨ÃÊÇÔµªà¢ŒÒ·Õ¨è ´Ø µ‹Í P3.2 ¢Í§á¼§Ç§¨Ã¤Çº¤ØÁËع‹ ¹µ Robo-51 A10.3 áŌǷíÒ¡ÒÃÃѹâ»Ãá¡ÃÁ áŌǷ´Åͧ¡´ÊÇÔµª ·Õâè Á´ÙÅ LCD áÊ´§¤‹Ò¡ÒùѺà¾ÔÁè ¤‹Ò¢Ö¹é Í‹ҧµ‹Íà¹×Íè § áÅзء¤ÃÑ§é ·Õ¡è ´ÊÇÔµª ¤‹Ò¢Í§¡ÒùѺ¨Ð¶Ù¡à¤ÅÕÂÏ ໚¹ “0” áŌÇàÃÔÁè µŒ¹¹Ñº¤‹ÒãËÁ‹ ¡ÒÃ͋ҹ¤‹Ò¨Ò¡ÍÔ¹¾Øµ´Ô¨µÔ ÍŨÐ㪌¡ÒÃà¢Õ¹â»Ãá¡ÃÁµÍºÊ¹Í§¡ÒÃÍÔ¹àµÍÃÃ»Ñ µ ¨Ò¡ÀÒ¹᷹͡ÇÔ¸¡Õ ÒÃǹµÃǨÊͺã¹áºº·Õãè ªŒã¹¡Ô¨¡ÃÃÁ·Õè 9


àÃÕ¹ÃÙጠÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ MCS-51

l 85

â»Ãá¡ÃÁ·Õè A10-1 â»Ãá¡ÃÁÀÒÉÒ C ·´Êͺ¡ÒÃ͋ҹ¤‹ÒÊÑ­­Ò³´Ô¨µÔ ÍÅÍÔ¹¾Øµ¨Ò¡á¼§Ç§¨ÃÊÇÔµª ¢Í§Ëع‹ ¹µ Robo-51 â´Â㪌¡ÒÃÍÔ¹àµÍÃÃ»Ñ µ /*—————————————————————————————————————*/ // Program : Reading digital input // Description : Reading digital signal from switch input board by interrupt // Filename : act1001.c // C compiler : RIDE 51 V6.1 /*—————————————————————————————————————*/ #include <C51ac2.h> // Header include register of P89C51AC2 #include <lcd_robo51.h> // Module function LCD display #include <delay.h> // Module function Delay unsigned int count=0; // Declare count variable void service_external0(void) interrupt 0 // External interrupt service routine { lcd_clear(); // Clear display lcd_putstr(line1,” Count:”); // Show “Count” message count = 0; // Clear counter } void main(void) { EX0 = 1; IT0 = 1; EA = 1; lcd_init(); lcd_putstr(line1,” Count:”); while(1) { inttolcd(0x88,count,Dec); delay_ms(100); count++; }

// // // // // //

Enable external interrupt on P3.2 (EX0) Select active edge of input signal at P3.2 Enable global interrupt LCD initialize Show “ Count:” Loop

// Show counter on LCD // Delay 0.1second // Increase counter

}

ÃÒÂÅÐàÍÕ´à¾ÔÁè àµÔÁ¢Í§â»Ãá¡ÃÁ ÊíÒËÃѺ¡Ò÷íÒ§Ò¹¢Í§â»Ãá¡ÃÁ¹Õ¨é Ð໚¹¡ÒùѺ¤‹Ò¢Ö¹é ¨Ò¡ 0 ¶Ö§ 65,535 ·Ø¡æ 0.1 ÇÔ¹Ò·Õâ´Â»ÃÐÁÒ³ ¨Ò¡¹Ñ¹é à¡çº¤‹Ò¡ÒùѺänj·µÕè ÇÑ á»Ã count áŌǹíÒä»áÊ´§¼Å·Õâè Á´ÙÅ LCD ᵋ¶ÒŒ ÁÕ¡Òá´ÊÇÔµª·µÕè ͋ ࢌҡѺ P3.2 ¨Ð·íÒãˌà¡Ô´¡ÒÃÍÔ¹àµÍÃÃ»Ñ µ¨Ò¡ÀÒ¹͡ â´Â㹵͹µŒ¹¢Í§â»Ãá¡ÃÁ䴌¡Òí ˹´¤‹Òແ´ãªŒ§Ò¹ÍÔ¹àµÍÃÃ»Ñ µ¨Ò¡ ÀÒ¹͡·Õ¢è Ò P3.2 (EX0) änjáÅŒÇ «Ö§è ¨Ð·íÒãˌâ»Ãá¡ÃÁà¡Ô´¡ÒõͺʹͧÍÔ¹àµÍÃÃ»Ñ µâ´Ââ»Ãá¡ÃÁ¨Ð¡ÃÐâ´´ ä»·íÒ§Ò¹ã¹Ê‹Ç¹¢Í§â»Ãá¡ÃÁºÃÔ¡ÒÃÍÔ¹àµÍÃÃ»Ñ µ¨Ò¡ÍÔ¹¾ØµÀÒ¹͡ã¹Ê‹Ç¹¢Í§ service_external0 «Ö§è ÁÕÅÒí ´Ñº ¢Í§àÇ¡àµÍÏÍÔ¹àµÍÏÃÑ»µà»š¹ 0 (¨Ò¡¡ÒáíÒ˹´ÅíҴѺµ‹Í·ŒÒ´ŒÇ¤ÕàÇÔϴ໚¹ interrupt 0) áÅÐÀÒÂã¹ â»Ãá¡ÃÁµÍºÊ¹Í§ÍÔ¹àµÍÃÃ»Ñ µ¹¨Õé ÐàÃÔÁè µŒ¹´ŒÇ¤íÒÊÑ§è ¡ÒÃà¤ÅÕÂÏ˹ŒÒ¨ÍâÁ´ÙÅ LCD áŌÇà¢Õ¹¢ŒÍ¤ÇÒÁ Count: ãËÁ‹Í¡Õ ¤Ãѧé áŌǵÒÁ´ŒÇ¡ÒÃà¤ÅÕÂϤҋ ¡ÒùѺãˌ໚¹ 0 (count = 0) à¾×Íè àÃÔÁè µŒ¹¡ÒùѺãËÁ‹ áÅÐàÁ×Íè â»Ãá¡ÃÁ ¡ÃÐâ´´¡ÅѺ价íÒ§Ò¹·Õâè »Ãá¡ÃÁËÅÑ¡¡ç¨Ð·íÒ¡ÒùѺ¤‹Ò¢Ö¹é àËÁ×͹»¡µÔ ¨¹¡Ç‹Ò¨ÐÁÕ¡ÒÃÍÔ¹àµÍÃÃ»Ñ µ¨Ò¡ÀÒ ¹Í¡ ¨Ö§¨ÐÁÕ¡ÒõͺʹͧÍÔ¹àµÍÃÃ»Ñ µÍ¡Õ ¤Ãѧé ˹֧è ǹ·íҧҹઋ¹¹Õäé »µÅÍ´ Robo-51


86 làÃÕ¹ÃÙá Œ ÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ

MCS-51

5.2 ¡ÒÃ͋ҹ¤‹ÒÊÑ­­Ò³ÍйÒÅÍ¡¢Í§Ëع‹ ¹µ Robo-51 ã¹Ëع‹ ¹µ Robo-51 㪌äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ T89C51AC2 ໚¹µÑÇ»ÃÐÁÇżŠ«Ö§è ÀÒÂã¹ÁÕ âÁ´ÙÅá»Å§ÊÑ­­Ò³ÍйÒÅ͡໚¹´Ô¨µÔ ÍÅ ¤ÇÒÁÅÐàÍÕ´ 10 ºÔµ ¨íҹǹ 8 ª‹Í§ â´ÂÍÔ¹¾ØµÍйÒÅÍ¡ ¢Í§ T89C51AC2 ÊÒÁÒöÃѺáç´Ñ¹ä¿µÃ§ä´ŒÊاÊØ´ 2.5V ¢ŒÍÁÙÅ´Ô¨ÔµÍÅ·Õè䴌¨Ò¡¡ÒÃá»Å§¨ÐÁÕ¤‹Ò ÍÂÙ㋠¹ª‹Ç§ 0 ¶Ö§ 1,023 ÃÇÁ 1,024 ¤‹Ò (䴌¨Ò¡ 210 = 1024 ¤‹Ò) «Ö§è ¡ç¶Í× Ç‹ÒÁÕ¤ÇÒÁÅÐàÍÕ´ÁÒ¡à¾Õ§¾Í ÊíÒËÃѺËع‹ ¹µ Robo-51 Áժ͋ §ÍÔ¹¾ØµÃѺÊÑ­­Ò³ÍйÒÅÍ¡ 6 ª‹Í§ «Ö§è ᵋÅЪ‹Í§µ‹ÍµÃ§¡Ñº ¢Ò¾Íϵ¢Í§ T89C51AC2 ´Ñ§¹Õé ÍÔ¹¾ØµÍйÒÅÍ¡ª‹Í§ 0 1 2 3 4 5

¢Ò¾Íϵ P1.0 P1.1 P1.2 P1.3 P1.4 P1.5

ÍÔ¹¾ØµÍйÒÅÍ¡¢Í§Ëع‹ ¹µ Robo-51 ÊÒÁÒöÃѺáç´Ñ¹ä¿µÃ§ã¹ª‹Ç§ 0-5V à˵ؼŷÕÃè ºÑ ä´Œ ÊÙ§¶Ö§ +5V ·Ñ駷ÕèµÑÇ T89C51AC2 ÊÒÁÒöÃѺ䴌à¾Õ§ 2.5V ¤×Í ÁÕǧ¨ÃÅ´·Í¹áç´Ñ¹µ‹Íänj·Õè ÍÔ¹¾ØµÍйÒÅÍ¡·Ø¡ª‹Í§ à¾×Íè »ÃѺáç´Ñ¹¨Ò¡ÀÒ¹͡ 0-5V ÁÒ໚¹ 0-2.5V ¤ÇÒÁÊÑÁ¾Ñ¹¸ÃÐËNjҧ Vin (áç´Ñ¹ÍÔ¹¾ØµÍйÒÅÍ¡) ¡Ñº¤‹ÒÍйÒÅÍ¡·ÕÍè ҋ ¹ä´Œ¨Ò¡ª‹Í§ã´æ ÊÒÁÒö¹íÒÁÒà¢Õ¹໚¹ÊÁ¡ÒÃ䴌´§Ñ ¹Õé Vin = A ×

5 1023

㹡ÒÃ㪌§Ò¹âÁ´ÙÅá»Å§ÊÑ­­Ò³ÍйÒÅ͡໚¹´Ô¨µÔ ÍÅ ¨íÒ໚¹¨ÐµŒÍ§¡íÒ˹´¡Ò÷íÒ§Ò¹¼‹Ò¹ ÃÕ¨ÔÊàµÍÏ·Õèà¡ÕèÂÇ¢ŒÍ§ÍÂًËÅÒµÑÇ·Õè¨Ð䴌á¹Ð¹íÒã¹ËÑÇ¢ŒÍµ‹Íä»

5.2.1 ÃÕ¨ÊÔ àµÍÏ·àÕè ¡ÕÂè Ç¢ŒÍ§¡ÑºâÁ´ÙÅá»Å§ÊÑ­­Ò³ÍйÒÅ͡໚¹´Ô¨µÔ ÍŢͧ T89C51AC2 5.2.1.1 ADCF ÃÕ¨ÊÔ àµÍϤǺ¤ØÁÍÔ¹¾ØµÍйÒÅÍ¡ ÁÕáÍ´à´ÃÊÍÂÙ·‹ Õè F6H 㪌¤Çº¤ØÁ¡ÒÃແ´ãªŒ§Ò¹ª‹Í§ÍÔ¹¾ØµÍйÒÅÍ¡¢Í§âÁ´ÙÅá»Å§ÊÑ­­Ò³ ÍйÒÅ͡໚¹´Ô¨µÔ ÍŢͧ T89C51AC2 ÁÕÃÒÂÅÐàÍÕ´´Ñ§¹Õé

ºÔµ 7 CH7

ºÔµ 6 CH6

ºÔµ 5 CH5

ºÔµ 4 CH4

ºÔµ 3 CH3

ºÔµ 2 CH2

ºÔµ 1 CH1

ºÔµ 0 CH0

CH 7 - CH 0 (ºÔµ 7-0) : ໚¹ºÔµ¤Çº¤ØÁãˌແ´¡ÒÃ㪌§Ò¹¢Í§ÍÔ¹¾ØµÍйÒÅÍ¡ª‹Í§ 0 ¶Ö§ 7 ·íÒ§Ò¹·ÕÅè ͨԡ “1”


àÃÕ¹ÃÙጠÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ MCS-51

l 87

5.2.1.2 ADCON ÃÕ¨ÊÔ àµÍϤǺ¤ØÁ¡Ò÷íÒ§Ò¹¢Í§âÁ´ÙÅá»Å§ÊÑ­­Ò³ÍйÒÅ͡໚¹´Ô¨µÔ ÍÅ ¢¹Ò´ 8 ºÔµ ÁÕáÍ´à´ÃÊÍÂÙ·‹ Õè F3H ÃÒÂÅÐàÍÕ´¢Í§ºÔµµ‹Ò§æ ã¹ÃÕ¨ÊÔ àµÍÏÁ´Õ §Ñ ¹Õé

ºÔµ 7 -

ºÔµ 6

PSIDLE

ºÔµ 5

ADEN

ºÔµ 4

ADEOC

ºÔµ 3

ADSST

ºÔµ 2

SCH2

ºÔµ 1

SCH1

ºÔµ 0

SCH0

PSIDLE (ºÔµ 6) : 㪌¡Òí ˹´ãˌâÁ´ÙÅá»Å§ÊÑ­­Ò³ÍйÒÅ͡໚¹´Ô¨µÔ ÍÅãˌࢌÒÊÙʋ ÀÒÇÐNjҧËÃ×ÍäÍà´ÔÅ (idle) ·íÒ§Ò¹·ÕÅè ͨԡ “1” ADEN (ºÔµ 5) :

㪌àÍç¹àÍàºÔÅ¡Ò÷íÒ§Ò¹¢Í§âÁ´ÙÅá»Å§ÊÑ­­Ò³ÍйÒÅ͡໚¹´Ô¨µÔ ÍÅ “0” - ´ÔÊàÍàºÔÅËÃ×Í»´¡Ò÷íÒ§Ò¹¢Í§âÁ´ÙÅá»Å§ÊÑ­­Ò³ÍйÒÅ͡໚¹´Ô¨µÔ ÍÅ “1” - àÍç¹àÍàºÔÅËÃ×Íແ´¡ÒÃ㪌§Ò¹¢Í§âÁ´ÙÅá»Å§ÊÑ­­Ò³ÍйÒÅ͡໚¹´Ô¨µÔ ÍÅ

ADEOC (ºÔµ 4) : 㪌 á ¨Œ § ¤ÇÒÁ¾ÃŒ Í Á㹡ÒÃ͋ Ò ¹¤‹ Ò ÍйÒÅÍ¡¢Í§âÁ´Ù Å á»Å§ÊÑ ­ ­Ò³ÍйÒÅ͡໚ ¹ ´Ô¨µÔ ÍÅ ¶Ù¡à«µâ´ÂÎÒÏ´áÇÏ ᵋÊÒÁÒöà¤ÅÕÂÏ䴌´ÇŒ «ÍÏ¿áÇÏ “0” - ໚¹¡ÒÃᨌ§Ç‹Ò ¢³Ð¹Õâé Á´ÙÅá»Å§ÊÑ­­Ò³ÂѧäÁ‹¾ÃŒÍÁ͋ҹ¤‹Ò “1” - ໚¹¡ÒÃᨌ§Ç‹Ò ¢³Ð¹Õâé Á´ÙÅá»Å§ÊÑ­­Ò³¾ÃŒÍÁ͋ҹ¤‹Ò ºÔµ¹Õé¨Ð૵â´ÂÍѵâ¹ÁѵÔàÁ×èÍâÁ´ÙÅá»Å§ÊÑ­­Ò³ÍйÒÅ͡໚¹´Ô¨µÍžÌÍÁÊíÒËÃѺ ͋ҹ¤‹Ò àÁ×Íè µŒÍ§¡ÒÃ͋ҹ¤‹Ò·Õäè ´Œ¨Ò¡¡ÒÃá»Å§¨ÐµŒÍ§à¤ÅÕÂϺµÔ ¹Õ¡é ͋ ¹Í‹Ò¹¤‹Ò´ŒÇ ADSST(ºÔµ 3) :

໚¹ºÔµ¤Çº¤ØÁ¡ÒÃàÃÔÁè µŒ¹á»Å§ÊÑ­­Ò³¢Í§âÁ´ÙÅá»Å§ÊÑ­­Ò³ÍйÒÅ͡໚¹´Ô¨µÔ ÍÅ ·íÒ§Ò¹·ÕÅè ͨԡ “1” àÁ×Íè á»Å§ÊÑ­­Ò³àÊÃç¨ ºÔµ¹Õ¨é Ðà¤ÅÕÂÏâ´ÂÍѵâ¹ÁѵÔ

SCH2:0 (ºÔµ 2-0) : 㪌¡Òí ˹´ª‹Í§ÊÑ­­Ò³ÍÔ¹¾ØµÍйÒÅÍ¡·Õµè ͌ §¡ÒÃ͋ҹ¤‹Ò Áբ͌ ÁÙÅâ´ÂÊÃØ»´Ñ§¹Õé SCH2

SCH1

SCH0

ª‹Í§ÍÔ¹¾ØµÍйÒÅÍ¡ ·Õàè Å×Í¡á»Å§ÊÑ­­Ò³

0

0

0

AN0

0

0

1

AN1

0

1

0

AN2

0

1

1

AN3

1

0

0

AN4

1

0

1

AN5

1

1

0

AN6

1

1

1

AN7


88 làÃÕ¹ÃÙá Œ ÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ

MCS-51

5.2.1.3 IEN1 ÃÕ¨ÊÔ àµÍϤǺ¤ØÁ¡ÒÃÍÔ¹àµÍÃÃ»Ñ µã¹âÁ´ÙÅá»Å§ÊÑ­­Ò³ÍйÒÅ͡໚¹´Ô¨µÔ ÍÅ ÁÕ¢¹Ò´ 8 ºÔµ áÍ´à´ÃÊÍÂÙ·‹ Õè E8H ÁÕ㪌§Ò¹à¾Õ§ 1 ºÔµ¤×Í EADC (ºÔµ 1) ໚¹ºÔµ¤Çº¤ØÁ¡Òà µÍºÊ¹Í§ÍÔ¹àµÍÏÃÑ»µÍѹÁÒ¨Ò¡ºÔµá¿Å¡ ADEOC ૵àÁ×èÍ¡ÒÃá»Å§ÊÑ­­Ò³ÍйÒÅÍ¡ä»à»š¹ ´Ô¨ÔµÍÅàÊÃç¨ÊÔé¹Å§ ËÒ¡µŒÍ§¡ÒÃàÍç¹àÍàºÔÅ¡ÒõͺʹͧÍÔ¹àµÍÏÃÑ»µà¹×èͧ¨Ò¡âÁ´ÙÅá»Å§ÊÑ­­Ò³ÍйÒÅÍ¡ ໚¹´Ô¨ÔµÍÅ·íÒ§Ò¹àÊÃç¨ÊÔé¹Å§ µŒÍ§à«µºÔµ¹Õéãˌ໚¹ “1” ¨Ð·íÒãˌâ»Ãá¡ÃÁ¡ÃÐâ´´ä»ÂѧàÇ¡àµÍÏ ÍÔ¹àµÍÃÃ»Ñ µ·áÕè Í´à´ÃÊ 0043H àÁ×Íè ÁÕ¡ÒÃÍÔ¹àµÍÃÃ»Ñ µà¡Ô´¢Ö¹é ÁÕËÁÒÂàÅ¢ÅíҴѺÍÔ¹àµÍÃÃ»Ñ µà»š¹ 8 àÁ×Íè à¢Õ¹â»Ãá¡ÃÁµÍºÊ¹Í§ÍÔ¹àµÍÃÃ»Ñ µ´ÇŒ ÂÀÒÉÒ C

5.2.1.4 ADDH áÅÐ ADDL ÃÕ¨ÊÔ àµÍÃà¡çº¢ŒÍÁÙŨҡ¡ÒÃá»Å§ÊÑ­­Ò³ÍйÒÅ͡໚¹´Ô¨µÔ ÍÅ ÁÕáÍ´à´ÃÊ໚¹ F5H ÊíÒËÃѺ ADDH áÅÐ F4H ÊíÒËÃѺ ADDL â´Â ÃÕ¨ÊÔ àµÍÏ ADDH 㪌à¡çº¢ŒÍÁÙÅ 8 ºÔµº¹ (ºÔµ 9 ¶Ö§ 2) ·Õäè ´Œ¨Ò¡¡ÒÃá»Å§ ÃÕ¨ÊÔ àµÍÏ ADDL 㪌à¡çº¢ŒÍÁÙÅ 2 ºÔµÅ‹Ò§ (ºÔµ 1 ¶Ö§ 0) ·Õäè ´Œ¨Ò¡¡ÒÃá»Å§

5.2.2 ¡ÒÃ͋ҹ¢ŒÍÁÙÅ·Õäè ´Œ¨Ò¡¡Ò÷íÒ§Ò¹¢Í§âÁ´ÙÅá»Å§ÊÑ­­Ò³ÍйÒÅ͡໚¹´Ô¨µÔ ÍÅ 5.2.2.1 ¡ÒÃ͋ҹ¤‹ÒẺǹµÃǨÊͺá¿Å¡

à¹×èͧ¨Ò¡ÃÕ¨ÔÊàµÍÏ·Õè¤Çº¤ØÁ¡Ò÷íÒ§Ò¹¢Í§âÁ´ÙÅá»Å§ÊÑ­­Ò³ÍйÒÅ͡໚¹´Ô¨ÔµÍŹÑé¹ Ê‹Ç¹ãË­‹äÁ‹ÊÒÁÒöࢌҶ֧䴌ã¹ÃдѺºÔµ ´Ñ§¹Ñ¹é àÁ×Íè µŒÍ§¡ÒÃ૵ËÃ×Íà¤ÅÕÂϺµÔ ã´æ ¨Ö§¨íÒ໚¹µŒÍ§ãªŒ ¡ÒáÃзíÒ·Ò§Åͨԡ «Ö§è µŒÍ§ÃÐÁÑ´ÃÐÇѧ㹡ÒÃà¢Õ¹â»Ãá¡ÃÁÁÒ¡¢Ö¹é ¡ÒÃ͋ҹ¢ŒÍÁØÅ·Õè䴌¨Ò¡âÁ´ÙÅá»Å§ÊÑ­­Ò³ÍйÒÅ͡໚¹´Ô¨ÔµÍÅã¹áººÇ¹µÃǨÊͺ á¿Å¡¹Ñé¹ÊÒÁÒöÊÃػ໚¹¢Ñ鹵͹䴌´Ñ§¹Õé (1) ແ´¡ÒÃ㪌§Ò¹ª‹Í§á»Å§ÊÑ­­Ò³ÍйÒÅ͡໚¹´Ô¨µÔ ÍÅ·Õµè ͌ §¡ÒÃ㪌§Ò¹ â´Â¡ÒáíÒ˹´ ¤‹Ò·ÕÃè ¨Õ ÊÔ àµÍÏ ADCF à¾×Íè ¡íÒ˹´¤‹ÒºÔµ CH 7- CH 0 ´Ñ§µÑÇÍ‹ҧ ADCF = 0x01; ADCF = 0x03; ADCF = 0x84; ADCF = 0xFF;

// ໚¹¡ÒÃແ´ª‹Í§ AN0 ãˌÁ¡Õ ÒÃ㪌§Ò¹ä´Œ // ໚¹¡ÒÃແ´ª‹Í§ AN0 áÅÐ AN1 ãˌÁ¡Õ ÒÃ㪌§Ò¹ä´Œ // ໚¹¡ÒÃແ´ª‹Í§ AN7 áÅÐ AN2 ãˌÁ¡Õ ÒÃ㪌§Ò¹ä´Œ // ໚¹¡ÒÃແ´ª‹Í§ AN7-AN0(·Ø¡ª‹Í§)ãˌÁ¡Õ ÒÃ㪌§Ò¹ä´Œ

(2) ૵ºÔµ ADEN à¾×èÍàÍç¹àÍàºÔÅ¡ÒÃ㪌§Ò¹âÁ´ÙÅá»Å§ÊÑ­­Ò³ÍйÒÅ͡໚¹´Ô¨ÔµÍÅ â´Â ADEN ໚¹ºÔµ 5 ¢Í§ÃÕ¨ÊÔ àµÍÏ ADCON â´Âà¢Õ¹¤íÒÊѧè ÀÒÉÒ C 䴌໚¹ ADCON = 0x20;


àÃÕ¹ÃÙጠÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ MCS-51

l 89

(3) àÅ×Í¡ª‹Í§ÍÔ¹¾ØµÍйÒÅÍ¡·Õµè ͌ §¡ÒÃ͋ҹ¤‹Ò â´Â¡íÒ˹´·Õºè µÔ SCH2:SCH0 ¢Í§ÃÕ¨ÊÔ àµÍÏ ADCON ÊÒÁÒöà¢Õ¹¤íÒÊѧè ÀÒÉÒ C 䴌໚¹ ADCON &= 0xF8; ADCON |= channel;

// à¤ÅÕÂϤҋ SCH2:SCH0 ãˌ໚¹ 0 ·Ø¡ºÔµ // ¡íÒ˹´ª‹Í§â´Â¤‹Ò¢Í§ channel Áդҋ 0 ¶Ö§ 7

¶ŒÒµŒÍ§¡ÒÃàÅ×͋͡ҹ¤‹Ò¨Ò¡ÍÔ¹¾ØµÍйÒÅÍ¡ª‹Í§ AN3 ¨ÐµŒÍ§à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ´Ñ§¹Õé ADCON &= 0xF8; ADCON |= 3;

(4) ૵ºÔµ ADSST à¾×Íè ãˌâÁ´ÙÅá»Å§ÊÑ­­Ò³ÍйÒÅ͡໚¹´Ô¨µÔ ÍÅàÃÔÁè µŒ¹¡Ò÷íÒ§Ò¹ â´Â ADSST ໚¹ºÔµ 3 ¢Í§ÃÕ¨ÊÔ àµÍÏ ADCON áÅÐàÁ×Íè ¡ÒáÒÃá»Å§¤‹ÒàÊÃç¨ÊÔ¹é ºÔµ ADSST ¨Ð¶Ù¡à¤ÅÕÂÏ â´ÂÍѵâ¹ÁÑµÔ ÊÒÁÒöà¢Õ¹¤íÒÊѧè ÀÒÉÒ C 䴌໚¹ ADCON |= 0x08;

(5) àÁ×Íè ¼‹Ò¹¢Ñ¹é µÍ¹·Õè (4) ÁÒáŌÇâÁ´ÙÅá»Å§ÊÑ­­Ò³ÍйÒÅ͡໚¹´Ô¨µÔ ÍŨеŒÍ§ãªŒàÇÅÒª‹Ç§ ˹֧è 㹡Ò÷íÒ§Ò¹à¾×Íè á»Å§¤‹ÒÍйÒÅÍ¡ãˌ໚¹¤‹Ò´Ô¨µÔ ÍÅ áÅÐà¡çº¤‹Ò·Õáè »Å§ä´Œänj·ÃÕè ¨Õ ÊÔ àµÍÏ ADDH áÅÐ ADDL àÁ×Íè ¡ÒÃá»Å§ÊÑ­­Ò³àÊÃç¨ÊÔ¹é ŧ ºÔµá¿Å¡ ADEOC (ºÔµ 4 ¢Í§ÃÕ¨ÊÔ àµÍÏ ADCON) ¨Ð¶Ù¡ ૵â´ÂÍѵâ¹ÁÑµÔ ´Ñ§¹Ñ¹é ËÅѧ¨Ò¡·ÕÊè §Ñè ãˌàÃÔÁè µŒ¹¡ÒÃá»Å§ÊÑ­­Ò³ÍйÒÅ͡໚¹´Ô¨µÔ ÍÅáÅŒÇ ¤ÇÃà¢Õ¹ â»Ãá¡ÃÁµÃǨÊͺºÔµá¿Å¡ ADEOC NjҶ١૵áŌÇËÃ×ÍÂѧ ¶ŒÒ¶Ù¡à«µáÅŒÇ áÊ´§Ç‹Ò ÊÒÁÒöàÃÕ¡͋ҹ ¢ŒÍÁÙÅ´Ô¨µÔ ÍÅ·Õäè ´Œ¨Ò¡¡ÒÃá»Å§ÊÑ­­Ò³¢Í§ª‹Í§ÍÔ¹¾ØµÍйÒÅÍ¡·Õàè Å×Í¡änj䴌áÅŒÇ «Ö§è ¨ÐµŒÍ§à¤ÅÕÂÏ ºÔµá¿Å¡ ADEOC ¡‹Í¹Í‹Ò¹¤‹ÒàÊÁÍ áµ‹¶ÒŒ ºÔµ ADEOC Âѧ໚¹ “0” ¨ÐµŒÍ§Ç¹Ã͵‹Íä» ÊÒÁÒöà¢Õ¹ ¤íÒÊѧè ÀÒÉÒ C 䴌໚¹ // Ãͨ¹¡ÃÐ·Ñ§è ºÔµá¿Å¡ ADEOC ¶Ù¡à«µ // à¤ÅÕÂϺµÔ á¿Å¡ ADEOC ¡‹Í¹Í‹Ò¹¤‹ÒÍйÒÅÍ¡

while((ADCON & 0x10) != 0x10); ADCON &= 0xEF;

(6) ͋ҹ¢ŒÍÁÙÅ´Ô¨µÔ ÍÅ·Õäè ´Œ¨Ò¡¡ÒÃá»Å§ÊÑ­­Ò³ «Ö§è ÁÕ¢¹Ò´ 10 ºÔµ¨Ò¡ÃÕ¨ÊÔ àµÍÏ ADDH áÅÐ ADDL â´Â ¢ŒÍÁÙÅ·ÕÍè ÂÙ㋠¹ÃÕ¨ÊÔ àµÍÏ ADDH ໚¹¢ŒÍÁÙÅ 8 ºÔµº¹ ¹Ñ¹è ¤×ͺԵ 9 ¶Ö§ºÔµ 2 ¢ŒÍÁÙÅ·ÕÍè ÂÙ㋠¹ÃÕ¨ÊÔ àµÍÏ ADDL ¤×Í¢ŒÍÁÙÅ 2 ºÔµÅ‹Ò§ ¤×ͺԵ 1 áÅÐ 0 㹡ÒÃ͋ҹ¢ŒÍÁÙÅ ¶ŒÒµŒÍ§¡ÒÃ͋ҹ·Ñ§é 10 ºÔµ µÑÇá»Ã·Õ¹è Òí ÁÒÃѺ¤‹Ò¤ÇÃ໚¹ª¹Ô´¨íҹǹàµçÁ¢¹Ò´ 2 亵ËÃ×Í integer ¹Ñ¹è àͧ ÊÒÁÒöà¢Õ¹¤íÒÊѧè ÀÒÉÒ C 䴌໚¹ // »ÃСÒȵÑÇá»Ã·Õ¹è Òí ÁÒÃѺ¢ŒÍÁÙÅ´Ô¨µÔ ÍÅ·Õäè ´Œ¨Ò¡¡ÒÃá»Å§ÊÑ­­Ò³ (ADDH<<2) + ADDL; // ͋ҹ¤‹ÒÁÒà¡çº·Õè dat

int data; data =


90 làÃÕ¹ÃÙá Œ ÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ

MCS-51

5.2.2.2 ¡ÒÃ͋ҹ¤‹ÒẺÍÔ¹àµÍÃÃ»Ñ µ ÊÒÁÒöÊÃػ໚¹¢Ñ鹵͹䴌´Ñ§¹Õé (1) ແ´¡ÒÃ㪌§Ò¹ª‹Í§á»Å§ÊÑ­­Ò³ÍйÒÅ͡໚¹´Ô¨µÔ ÍÅ·Õµè ͌ §¡Òà â´Â¡ÒáíÒ˹´¤‹Ò·ÕÃè ¨Õ Ô ÊàµÍÏ ADCF à¾×Íè ¡íÒ˹´¤‹ÒºÔµ CH7- CH0 (2) ૵ºÔµ ADEN à¾×Íè àÍç¹àÍàºÔÅ¡ÒÃ㪌§Ò¹âÁ´ÙÅá»Å§ÊÑ­­Ò³ÍйÒÅ͡໚¹´Ô¨µÔ ÍÅ (3) àÍç¹àÍàºÔÅ¡ÒõͺʹͧÍÔ¹àµÍÃÃ»Ñ µ¢Í§ÃкºáÅШҡºÔµá¿Å¡ ADEOC àÁ×Íè ¡ÒÃá»Å§ ÊÑ­­Ò³àÊÃç¨ÊÔ¹é â´Â¡ÒÃ૵ºÔµ EA áÅÐ EADC «Ö§è ÊÒÁÒöࢌҶ֧䴌ã¹ÃдѺºÔµ ÊÒÁÒöà¢Õ¹¤íÒ Êѧè ÀÒÉÒ C 䴌໚¹ EA =1;

// àÅ×Í¡ãˌÃкºµÍºÊ¹Í§¡ÒÃÍÔ¹àµÍÃÃ»Ñ µ

EADC = 1;

// àÅ×Í¡ãˌµÍºÊ¹Í§ÍÔ¹àµÍÃÃ»Ñ µà¹×Íè §¨Ò¡ºÔµ ADEOC ¶Ù¡à«µ

(4) àÅ×Í¡ª‹Í§ÍÔ¹¾ØµÍйÒÅÍ¡·Õµè ͌ §¡Òà â´Â¡íÒ˹´·Õºè µÔ SCH2:SCH0 ¢Í§ÃÕ¨ÊÔ àµÍÏ ADCON (5) ૵ºÔµ ADSST à¾×Íè ãˌâÁ´ÙÅá»Å§ÊÑ­­Ò³ÍйÒÅ͡໚¹´Ô¨µÔ ÍÅàÃÔÁè ·íÒ§Ò¹ (6) ËÅѧ¨Ò¡¹ÕéâÁ´ÙÅá»Å§ÊÑ­­Ò³ÍйÒÅ͡໚¹´Ô¨ÔµÍŵŒÍ§ãªŒàÇÅÒª‹Ç§Ë¹Öè§ã¹¡ÒÃá»Å§ ÊÑ­­Ò³ãˌ໚¹¢ŒÍÁÙÅ´Ô¨µÔ ÍÅ áÅÐà¡çº¤‹Ò·Õäè ´Œänj·ÃÕè ¨Õ ÊÔ àµÍÏ ADDH áÅÐ ADDL àÁ×Íè ¡ÒÃá»Å§ÊÑ­­Ò³ àÊÃç¨ÊÔé¹Å§¨Ðà¡Ô´¡ÒÃÍÔ¹àµÍÏÃÑ»µà¹×èͧ¨Ò¡ºÔµá¿Å¡ ADEOC ·íÒãˌâ»Ãá¡ÃÁ¡ÃÐâ´´ä»ÂѧàÇ¡ àµÍÏÍÔ¹àµÍÏÃÑ»µ·ÕèáÍ´à´ÃÊ 0043H (¡íÒ˹´ËÁÒÂàÅ¢ÅíҴѺÍÔ¹àµÍÏÃÑ»µà»š¹ 8) 㹡ÒÃà¢Õ¹ â»Ãá¡ÃÁµÍºÊ¹Í§ÍÔ¹àµÍÃÃ»Ñ µã¹ÀÒÉÒ C ¹Ñ¹é ¨ÐµŒÍ§à¢Õ¹¿˜§¡ª¹Ñè à¾×Íè µÍºÊ¹Í§¡ÒÃÍÔ¹àµÍÃÃ»Ñ µ ¨Ò¡ºÔµ ADEOC â´Â¤íÒÊÑ觷ÕèÍÂًÀÒÂ㹨Ð໚¹¡ÒÃà¤ÅÕÂϺԵ ADEOC áÅÐ͋ҹ¤‹Ò¢ŒÍÁÙÅ·Õè䴌¨Ò¡¡Òà á»Å§ÊÑ­­Ò³ «Ö§è ÁÕ¢¹Ò´ 10 ºÔµ ´Ñ§µÑÇÍ‹ҧâ»Ãá¡ÃÁµÍºÊ¹Í§ÍÔ¹àµÍÃÃ»Ñ µµÍ‹ 仹Õé int adc_dat; void ADC_service() interrupt 8

// »ÃСÒȵÑÇá»Ã·Õ¹è Òí ÁÒÃѺ¤‹ÒÍйÒÅÍ¡·Õ¶è ¡Ù á»Å§áÅŒÇ // ËÁÒÂàÅ¢ÅíҴѺÍÔ¹àµÍÃÃ»Ñ µà»š¹ 8

{ EADC = 0;

ADCON &= 0xEF; adc_dat = (ADDH<<2)+(ADDL); ADCON |= 0x48; EADC = 1; }

// »´¡ÒÃÍÔ¹àµÍÃÃ»Ñ µà¹×Íè §¨Ò¡¤‹ÒºÔµ ADEOC ªÑÇè ¤ÃÒÇ // à¾×Íè ·íÒ§Ò¹¤íÒÊѧè Í×¹è æ // à¤ÅÕÂϺµÔ ADEOC // ͋ҹ¤‹ÒÁÒà¡çº·Õè adc_dat // ૵ºÔµ ADEN áÅÐ ADSST à¾×Íè á»Å§¤‹Ò㹤ÃÑ§é µ‹Íä» // ແ´¡ÒÃÍÔ¹àµÍÃÃ»Ñ µà¾×Íè ¡ÒÃá»Å§¤‹Ò¤ÃÑ§é µ‹Íä»


àÃÕ¹ÃÙጠÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ MCS-51

l 91

5.2.3 ¿˜§¡ª¹Ñè ͋ҹ¢ŒÍÁÙŨҡâÁ´ÙÅá»Å§ÊÑ­­Ò³ÍйÒÅ͡໚¹´Ô¨µÔ ÍÅ ¨Ò¡¢Ñ鹵͹â´ÂÊÃØ»¢Í§¡ÒÃ͋ҹ¢ŒÍÁÙŨҡâÁ´ÙÅá»Å§ÊÑ­­Ò³ÍйÒÅ͡໚¹´Ô¨ÔµÍÅ ÊÒÁÒö¹í Ò ÁÒ㪌 ã ¹¡ÒÃÊÌ Ò §¿˜ § ¡ ªÑè ¹ ã¹â»Ãá¡ÃÁÀÒÉÒ C áŌ Ç ¨Ñ ´ à¡ç º ໚ ¹ ä¿Å ä źÃÒÃÕ ·Õè ª×è Í adc_robo51.h ´Ñ§ÁÕÃÒÂÅÐàÍÕ´¢Í§â»Ãá¡ÃÁáÊ´§ã¹â»Ãá¡ÃÁ·Õè P5-1 ãˌ·Òí ¡ÒÃà¢Õ¹â»Ãá¡ÃÁâ´Â 㪌 RIDE (ã¹·Õ¹è ãÕé ªŒª´Ø «Í¿µáÇϾ² Ñ ¹Òâ»Ãá¡ÃÁÀÒÉÒ C ¢Í§äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ MCS-51 µÑǹÕ)é áŌǺѹ·Ö¡à»š¹ä¿Å¹ÒÁÊ¡ØÅ .h áŌǨѴà¡çºãˌÍÂÙ·‹ âÕè ¿Åà´ÍÏà´ÕÂǡѺä¿Åâ»Ãà¨ç¡µ·¡Õè Òí Åѧ¾Ñ²¹ÒËÃ×Í·Õè C:\RIDE\INC â»Ãá¡ÃÁ·Õè P5-1 ä¿ÅäźÃÒÃÕ adc_robo51.h ·ÕÊè Ìҧ¢Ö¹é ÊíÒËÃѺ㪌§Ò¹âÁ´ÙÅá»Å§ÊÑ­­Ò³ÍйÒÅÍ¡ ໚¹´Ô¨µÔ ÍÅã¹Ëع‹ ¹µ Robo-51 /*—————————————————————————————————————*/ // Program : Read Analog input // Description : Read Analog input function // Filename : adc_robo51.h // C compiler : RIDE 51 V6.1 /*—————————————————————————————————————*/ int analog(char channel) { int adc_dat = 0; // For keep input analog data ADCF = 0x3F; // Setup config for adc bit input convert(port1) (P1.0-P1.5) ADCON = 0x20; // Enable ADC ADCON &= 0xF8; // clear SCH bit (SCH 0:1:2 = 0) ADCON |= channel; // select convert/read by channel config. ADCON |= 0x08; while((ADCON & 0x10)!= 0x10); ADCON &= 0xEF; adc_dat = (ADDH<<2)+(ADDL); return(adc_dat);

// // // // //

start convert (set bit ADSST) wait for EOF to set clear bit ADEOC read adc data and convert Return result of ADC 10 bit

}

µÑÇÍ‹ҧ·Õè 5-1 ¡ÒÃ㪌§Ò¹¿˜§¡ª¹Ñè analog à¾×Íè ͋ҹ¢ŒÍÁÙÅ´Ô¨µÔ ÍÅ·Õäè ´Œ¨Ò¡¡ÒÃá»Å§ÊÑ­­Ò³ ÍйÒÅÍ¡ int dat;

// »ÃСÒõÑÇá»ÃẺ integer à¾×Íè ¹íÒÁÒÃѺ¤‹Ò·ÕÍè ҋ ¹¨Ò¡¿˜§¡ª¹Ñè analog

data = analog(2);

// ͋ҹ¤‹ÒÍйÒÅÍ¡ª‹Í§·Õè 2(µÃ§¡Ñº¢Ò P1.2) áŌÇà¡çºänj·µÕè ÇÑ á»Ã dat


92 làÃÕ¹ÃÙá Œ ÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ

MCS-51

¡Ô¨¡ÃÃÁ·Õè 11

·´Êͺ¡ÒÃÃѺÊÑ­­Ò³ÍйÒÅÍ¡ A11.1 ແ´â»Ãá¡ÃÁ RIDE áŌÇà¢Õ¹â»Ãá¡ÃÁ·Õè A11-1 ºÑ¹·Ö¡ã¹ª×Íè ä¿Å act1101.c ·íÒ¡ÒäÍÁä¾Å áŌǴÒǹ âËÅ´ä¿ÅÊáً ¼§Ç§¨Ã RBX-51AC2 plus ¢Í§Ëع‹ ¹µ Robo-51 ´ŒÇÂâ»Ãá¡ÃÁ Flip A11.2 µ‹ÍÊÒ¨ҡἧǧ¨ÃµÑǵŒÒ¹·Ò¹»ÃѺ¤‹Ò䴌 (ZX-POTH) à¢ŒÒ·Õ¨è ´Ø µ‹Í AN0 ¢Í§á¼§Ç§¨Ã RBX-51AC2 plus A11.3 Ãѹâ»Ãá¡ÃÁ ·´ÅͧËÁع᡹¢Í§µÑǵŒÒ¹·Ò¹»ÃѺ¤‹Ò䴌 Êѧࡵ¼Å¡Ò÷íÒ§Ò¹¼‹Ò¹âÁ´ÙÅ LCD º¹á¼§Ç§¨Ã RBX-51AC2 plus ¤‹Ò¢Í§¡ÒÃá»Å§ÊÑ­­Ò³¨Ðà»ÅÕè¹á»Å§ä»µÒÁ¡ÒÃËÁع᡹¢Í§µÑǵŒÒ¹·Ò¹»ÃѺ¤‹Ò䴌 â´ÂÁÕ¤‹ÒÍÂً ÃÐËNjҧ 0 ¶Ö§ 1,023 Í‹ҧäáçµÒÁ ¤‹ÒÊÙ§ÊØ´ÍÒ¨äÁ‹ãª‹ 1,023 ·Ñ§é ¹Õ¢é ¹Öé ÍÂÙ¡‹ ºÑ ÃдѺáç´Ñ¹¢Í§áºµàµÍÃÕãè ¹¢³Ð ·´Êͺ´ŒÇ áÅжŒÒËҡẵàµÍÃÕÁè ÃÕ Ð´Ñºáç´Ñ¹µèÒí ¨Ðʋ§¼Åãˌ¡ÒÃ͋ҹ¤‹ÒÊÑ­­Ò³ÍйÒÅÍ¡áÅСÃкǹ¡Òà á»Å§ÊÑ­­Ò³ÁÕ¤ÇÒÁ¤ÅÒ´à¤Å×è͹ÊÙ§

â»Ãá¡ÃÁ·Õè A11-1 â»Ãá¡ÃÁÀÒÉÒ C ·´Êͺ¡ÒÃ͋ҹ¤‹ÒÊÑ­­Ò³ÍйÒÅÍ¡¢Í§Ëع‹ ¹µ Robo-51 /*—————————————————————————————————————*/ // Program : Reading digital data from A/D convertion // Filename : act1101.c // C compiler : RIDE 51 V6.1 /*—————————————————————————————————————*/ #include <C51ac2.h> // Header include register of P89C51AC2 #include <lcd_robo51.h> // Module function LCD display #include <delay.h> // Module function Delay #include <adc_robo51.h> // Module function analog to digital converter void main(void) { unsigned int val; // Declare analog data variable, integer type recommended lcd_init(); // Initial LCD module while(1) // Looping { lcd_putstr(line1,” Analog CH0 Read”); // Show title message val = analog(0); // Read digital data from conversion to variable inttolcd(0xC7,val,Dec); // Convert digital data to show on LCD display delay_ms(1000); // Delay 1 second lcd_clear(); } }

ÃÒÂÅÐàÍÕ´à¾ÔÁè àµÔÁ¢Í§â»Ãá¡ÃÁ â»Ãá¡ÃÁ¹Õàé »š¹¡ÒÃ͋ҹ¤‹ÒÍйÒÅÍ¡¨Ò¡ª‹Í§ AN0 ·Õµè ͋ ࢌҡѺἧǧ¨ÃµÑǵŒÒ¹·Ò¹»ÃѺ¤‹Ò䴌 â´Â͋ҹ´ŒÇ ¿˜§¡ª¹Ñè analog â´ÂàÃÕ¡͋ҹÁÒà¡çºänj·µÕè ÇÑ á»Ã val ËÅѧ¨Ò¡¹Ñ¹é ¹íÒ¤‹Ò·ÕÍè ҋ ¹ä´Œ«§Öè ໚¹àÅ¢¨íҹǹàµçÁ int ä» á»Å§ãˌ໚¹ÃËÑÊáÍÊ¡Õàé ¾×Íè ʋ§ä»áÊ´§¼Å¢ŒÍÁÙÅ´Ô¨µÔ ÍÅ·Õäè ´Œ¨Ò¡¡ÒÃá»Å§ÊÑ­­Ò³ÍйÒÅÍ¡·Õâè Á´ÙÅ LCD


àÃÕ¹ÃÙጠÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ MCS-51 l 93

º··Õè 6 Ëع‹ ¹µ Robo-51 ¡Ñº¡ÒõÃǨ¨ÑºÇѵ¶Ø ẺäÁ‹ÊÁÑ ¼ÑÊ´ŒÇÂáʧÍÔ¹¿ÃÒàô ¨Ò¡º··Õè 7 ·Õè¾Ñ²¹Ò¤ÇÒÁÊÒÁÒö¢Í§Ë؋¹Â¹µ Robo-51 ãˌÊÒÁÒöÃѺ¢ŒÍÁÙŨҡÃÕâÁµ ¤Í¹â·ÃÅ ER-4 ·Õ¼è ÊÁÁҡѺáʧÍÔ¹¿ÃÒàôà¾×Íè ¹íÒÁÒ¡íÒ˹´ÃٻẺ¡ÒÃà¤Å×Íè ¹·Õè ÁÒ¶Ö§º·¹Õ¹é Òí àÊ¹Í ¡Ò÷íÒ§Ò¹¢Í§Ë؋¹Â¹µ Robo-51 ¡ÑºâÁ´ÙÅÇÑ´ÃÐÂзҧ´ŒÇÂáʧÍÔ¹¿ÃÒàô à¾×èÍãˌË؋¹Â¹µÊÒÁÒö µÃǨ¨ÑºÇѵ¶ØẺäÁ‹ÊÁÑ ¼ÑÊ (contact less) ÃÇÁ件֧ÊÒÁÒö¹íÒä»ãªŒã¹¡Ò䌹ËÒÇѵ¶ØËÃ×ÍÊÔ§è ¡Õ´¢ÇÒ§ 䴌 Íѹ໚¹¡ÒÃáÊ´§ãˌàË繶֧ÈÑ¡ÂÀÒ¾¢Í§Ëع‹ ¹µÍµÑ â¹Áѵ¢Ô ¹Ò´àÅç¡·Õ¤è Ǻ¤ØÁ¡Ò÷íÒ§Ò¹´ŒÇÂäÁâ¤Ã ¤Í¹â·ÃÅàÅÍÏ

6.1 ¤Ø³ÊÁºÑµ¢Ô ͧ GP2D120 âÁ´ÙÅÇÑ´ÃÐÂзҧ´ŒÇÂáʧÍÔ¹¿ÃÒàô 㪌¡ÒõÃǨ¨ÑºÃÐÂзҧ´ŒÇ¡ÒÃÊзŒÍ¹áʧÍÔ¹¿ÃÒàô l ÇÑ´ÃÐÂзҧ䴌 4 ¶Ö§ 30 ૹµÔàÁµÃ l ä¿àÅÕÂ é § 4.5 ¶Ö§ 5V ¡ÃÐáÊä¿¿‡Ò 33 mA l ãˌàÍÒµ¾µ Ø à»š¹áç´Ñ¹ã¹Â‹Ò¹ 0.4 ¶Ö§ 2.4V ·Õäè ¿àÅÕÂé § +5V GP2D120 ໚¹âÁ´ÙŵÃǨ¨ÑºÃÐÂзҧẺÍÔ¹¿ÃÒàôÁÕ¢Òµ‹Í㪌§Ò¹ 3 ¢Ò¤×Í ¢Òµ‹Íä¿àÅÕÂé § (Vcc), ¢Ò¡ÃÒÇ´ (GND) áÅТÒáç´Ñ¹àÍÒµ¾µØ (Vout) ¡ÒÃ͋ҹ¤‹Òáç´Ñ¹¨Ò¡ GP2D120 ¨ÐµŒÍ§ÃÍ ãˌ¾¹Œ ª‹Ç§àµÃÕÂÁ¤ÇÒÁ¾ÃŒÍÁ¢Í§âÁ´ÙÅ¡‹Í¹ «Ö§è 㪌àÇÅÒ»ÃÐÁÒ³ 32.7 ¶Ö§ 52.9 ÁÔÅÅÔÇ¹Ô Ò·Õ ´Ñ§¹Ñ¹é ã¹ ¡ÒÃ͋ҹ¤‹Òáç´Ñ¹¨Ö§¤ÇÃÃÍãˌ¾¹Œ ª‹Ç§àÇÅҴѧ¡Å‹ÒÇ仡‹Í¹ ´Ñ§áÊ´§¢ŒÍÁÙÅàº×Íé §µŒ¹ã¹ÃÙ»·Õè 8-1 ¤‹Òáç´Ñ¹àÍÒµ¾µØ ¢Í§ GP2D120 ·ÕÃè ÐÂзҧ 30 ૹµÔàÁµÃ·Õäè ¿àÅÕÂé § +5V ÍÂÙ㋠¹ª‹Ç§ 0.25 ¶Ö§ 0.55V â´Â¤‹Ò¡ÅÒ§¤×Í 0.4V ª‹Ç§¢Í§¡ÒÃà»ÅÕÂè ¹á»Å§áç´Ñ¹àÍÒµ¾µØ ·ÕÃè ÐÂзҧ 4 ૹµÔàÁµÃ¤×Í 2.25V ±0.3V l


94làÃÕ¹ÃÙá Œ ÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ

LED ÍÔ¹¿ÃÒàôµÑÇʋ§

MCS-51

¡ÃÒ¿áÊ´§¡Ò÷íÒ§Ò¹¢Í§ GP2D120

µÑÇÃѺáʧÍÔ¹¿ÃÒàô

áç´Ñ¹àÍÒµ¾Øµ (V) 2.8

/2 , 

2.4 Vout GND

Vcc

2.0 1.6

ä¿àÅÕÂé §

1.2 38.3±9.6

¡ÒÃÇÑ´ÃÐÂÐˋҧ áç´Ñ¹àÍÒµ¾µØ

0.8

ÁÔÅÅÔÇ¹Ô Ò·Õ

0.4

ÇÑ´¤ÃÑ駷Õè 1

ÇÑ´¤ÃÑ駷Õè 2

äÁ‹á¹‹¹Í¹

ÇÑ´¤ÃÑ§é ·Õè n

àÍÒµ¾µØ ¤ÃÑ§é ·Õè 1 àÍÒµ¾µØ ¤ÃÑ§é ·Õè 2 5 ÁÔÅÅÔÇ¹Ô Ò·Õ

0

àÍÒµ¾µØ ¤ÃÑ§é ·Õè n

0

4

8

12

16

20

24

28

ÃÐÂÐˋҧ¨Ò¡Çѵ¶Ø·ÕèµÃǨ¨Ñº (cm)

32

* 㪌¡ÃдÒÉà·Ò¢ÒÇÃع‹ R-27 ¢Í§ Kodak «Ö§è ´ŒÒ¹¢ÒÇÁÕÍѵÃÒ¡ÒÃÊзŒÍ¹áʧ 90% ໚¹ Çѵ¶ØÊíÒËÃѺÊзŒÍ¹áʧà¾×èÍÇÑ´ÃÐÂзҧ

ÃÙ»·Õè 6-1 áÊ´§¡ÒèѴ¢Ò ä´ÍÐá¡ÃÁàÇÅҨѧËÇСÒ÷íÒ§Ò¹ áÅСÃÒ¿áÊ´§¡Ò÷íÒ§Ò¹¢Í§ GP2D120

6.2 ËÅÑ¡¡Ò÷íÒ§Ò¹¢Í§âÁ´ÙŵÃǨÇÑ´ÃÐÂзҧ´ŒÇÂáʧÍÔ¹¿ÃÒàô âÁ´ÙÅÇÑ´ÃÐÂзҧ´ŒÇÂáʧÍÔ¹¿ÃÒàô ¨Ðʋ§áʧÍÔ¹¿ÃÒàô¨Ò¡µÑÇʋ§ä»¡ÃзºÇѵ¶Ø¼Ò‹ ¹àŹʏ ¹Ù¹à¾×Íè â¿¡ÑÊáʧãˌÁ¤Õ ÇÒÁࢌÁáʧä»Âѧ¨Ø´ã´¨Ø´Ë¹Ö§è àÁ×Íè áʧ仡ÃзºÇѵ¶Ø¨Ðà¡Ô´¡ÒáÃÐà¨Ô§¢Í§ áʧä»ã¹·ÔÈ·Ò§µ‹Ò§ æ áÊ§Ê‹Ç¹Ë¹Ö§è ¨Ð¡ÃÐà¨Ô§ä»ÂѧÀÒ¤ÃѺ â´ÂÁÕàŹʏÀÒ¤ÃѺ·íÒ˹ŒÒ·ÕÃè ÇÁáʧáÅÐ ¡íÒ˹´¨Ø´µ¡¡Ãзº áʧ¨Ð¶Ù¡Ê‹§¼‹Ò¹ä»Âѧâ¿âµŒ·ÃÒ¹«ÔÊàµÍϨÒí ¹Ç¹ÁÒ¡·Õµè ͋ àÃÕ§µÑǡѹ໚¹Ê‹Ç¹ ÃѺáʧ ËÃ×ÍÍÐàÃÂÃºÑ áʧ µíÒá˹‹§·Õáè ʧµ¡¡Ãзº¹ÕÊé ÒÁÒö¹íÒÁÒ¤íҹdzËÒÃÐÂзҧ (L) ¨Ò¡ÀÒ¤ ʋ§ä»ÂѧÇѵ¶Ø䴌 â´Â㪌ʵ٠ÃËÒÊÒÁàËÅÕÂè Á¤ÅŒÒ´ѧ¹Õé L F = A X

´Ñ§¹Ñ¹é ¤‹Ò L ¨ÐÁդҋ ෋ҡѺ L=

F×A X

â´Â¤‹Ò·ÕèÇÑ´ÃÐÂÐ䴌¨Ò¡â¿âµŒ·ÃÒ¹«ÔÊàµÍϨж١ʋ§ä»Âѧǧ¨Ã»ÃÐÁÇżÅÊÑ­­Ò³¡‹Í¹¨Ð à»ÅÕÂè ¹¤‹Ò໚¹ÃдѺáç´Ñ¹ «Ö§è ãˌ¼Å¡ÒÃà»ÅÕÂè ¹á»Å§áç´Ñ¹µÒÁÃÐÂзҧ·Õµè ÃǨÇѴ䴌


àÃÕ¹ÃÙጠÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ MCS-51 l 95

Çѵ¶Ø

L A

F ʋǹÃѺáʧ

LED ÍÔ¹¿ÃÒàô µÑÇʋ§ GP2D120

X

ÃÙ»·Õè 6-2 áÊ´§ÇÔ¸¡Õ ÒÃÇÑ´¤‹ÒÃÐÂзҧ´ŒÇ¡ÒÃÊзŒÍ¹áʧÍÔ¹¿ÃÒàô¢Í§âÁ´ÙÅ GP2D120

6.3 ¡ÒÃ͋ҹ¤‹Ò¢ŒÍÁÙŨҡ GP2D120 ´ŒÇÂǧ¨Ãá»Å§ÊÑ­­Ò³ÍйÒÅ͡໚¹´Ô¨µÔ ÍÅ ¤‹Òáç´Ñ¹àÍÒµ¾Øµ¨Ò¡ GP2D120 ¨Ðà»ÅÕè¹á»Å§µÒÁÃÐÂзҧ·ÕèµÃǨÇѴ䴌 ઋ¹ ÇÑ´áç´Ñ¹ Vout ෋ҡѺ 0.5V ¨Ðà·Õº໚¹ÃÐÂзҧ䴌 26 ૹµÔàÁµÃ ËÃ×Í·Õè Vout ෋ҡѺ 2V ¨Ðà·Õº໚¹ÃÐÂРˋҧ¨Ò¡Çѵ¶Ø 6 ૹµÔàÁµÃ ໚¹µŒ¹ ÊÒÁÒöÊÃØ»¢ŒÍÁÙÅ¡ÒÃà·Õº¤‹Ò Vout ¡ÑºÃÐÂзҧ㹵ÒÃÒ§·Õè 6-1 àÁ×è͹íÒâÁ´ÙÅ GP2D120 àª×èÍÁµ‹Í¡ÑºÇ§¨Ãá»Å§ÊÑ­­Ò³ÍйÒÅ͡໚¹´Ô¨ÔµÍŢͧäÁâ¤Ã ¤Í¹â·ÃÅàÅÍÏ ¶ŒÒäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏÍҋ ¹¤‹Ò䴌 307 áÊ´§Ç‹Ò GP2D120 ÍÂÙˋ ҋ §¨Ò¡Çѵ¶Ø 8 ૹµÔàÁµÃ

6.4 ä¿ÅäźÃÒÃÕÊÒí ËÃѺ㪌§Ò¹âÁ´ÙÅ GP2D120 à¹×Íè §¨Ò¡âÁ´ÙÅ GP2D120 ãˌ¼Å¡Ò÷íÒ§Ò¹ÍÍ¡ÁÒ໚¹áç´Ñ¹ä¿µÃ§ «Ö§è ÊÒÁÒöʋ§à¢ŒÒä»Âѧ ÍÔ¹¾ØµÍйÒÅÍ¡¢Í§Ëع‹ ¹µ Robo-51 䴌â´ÂµÃ§ ·Ç‹Ò ËÒ¡µŒÍ§¡Ò÷ÃÒº¶Ö§¤‹Ò·ÕÃè ÐÂзҧ·Õµè ÃǨ¨Ñº 䴌¨Ò¡âÁ´ÙÅ GP2D120 ÁÕ¤ÇÒÁ¨íÒ໚¹Í‹ҧÂÔ觷Õè¨ÐµŒÍ§ÁÕ¡Òäíҹdzà¾×èÍá»Å§¤‹Ò¨Ò¡Ç§¨Ãá»Å§ ÊÑ­­Ò³ÍйÒÅ͡໚¹´Ô¨ÔµÍÅÀÒÂã¹äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ T89C51AC2 ÁÒ໚¹¤‹ÒÃÐÂзҧ㹠˹‹ÇÂૹµÔàÁµÃ


96làÃÕ¹ÃÙá Œ ÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ

MCS-51

áç´Ñ¹àÍÒµ¾Øµ¨Ò¡ GP2D120 (V)

¤‹Ò¨Ò¡Ç§¨Ãá»Å§ÊÑ­­Ò³ ÍйÒÅ͡໚¹´Ô¨ÔµÍÅ 10 ºÔµ

ÃÐÂзҧâ´Â»ÃÐÁÒ³ (ૹµÔàÁµÃ - cm)

0.4 0.5

82 102

32 26

0.6

123

22

0.7

143

19

0.8

164

16

0.9 1.0

184 205

14 13

1.1

225

12

1.2 1.3

246 266

11 10

1.4

287

9

1.5 1.6

307 328

8 8

1.7

348

7

1.8

369

7

1.9

389

6

2.0

410

6

2.1

430

6

2.2

451

5

2.3

471

5

2.4

492

5

2.5 2.6

512 532

5 4

µÒÃÒ§·Õè 6-1 áÊ´§¤ÇÒÁÊÑÁ¾Ñ¹¸ÃÐËNjҧÃдѺáç´Ñ¹àÍÒµ¾µØ ¢Í§ GP2D120 ¡Ñº¤‹Ò¨Ò¡Ç§¨Ãá»Å§ ÊÑ­­Ò³ÍйÒÅ͡໚¹´Ô¨µÔ ÍÅáÅÐÃÐÂзҧ·ÕÇè ´Ñ ä´Œ (䴌¨Ò¡¡Òäíҹdz)


àÃÕ¹ÃÙጠÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ MCS-51 l 97

â»Ãá¡ÃÁ·Õè 6-1 ä¿ÅäźÃÒÃÕ gp2d120.h ÊíÒËÃѺ͋ҹáÅФíҹdz¤‹ÒÃÐÂзҧ¢Í§âÁ´ÙÅ GP2D120 ÊíÒËÃѺËع‹ ¹µ Robo-51 /*—————————————————————————————————————*/ // Filename : gp2d120.h // C compiler : RIDE 51 V6.1 /*————————————————-————————————————————*/ #include <adc_robo51.h>// Module function get analog from AN0-AN5 #define m 2914 #define b 5 #define k 1 /*********** Function get distance from GP2D120 sensor ***********************/ unsigned int dist(char adc_ch) { unsigned int dis; // Variable for keep distance dis = (m/(analog(adc_ch)+b)) - k; // Calculate distance return(dis); // Return distance }

6.4.1 ÃÒÂÅÐàÍÕ´«ÍÏÊâ»Ãá¡ÃÁ à¾×èͤÇÒÁÊдǡ㹡ÒÃà¢Õ¹â»Ãá¡ÃÁ¨Ö§ä´ŒÁÕ¡ÒÃÊÌҧä¿ÅäźÃÒÃÕÊíÒËÃѺ¡Òäíҹdz¤‹Ò ¢Í§âÁ´ÙÅ GP2D120 ¹Õ¢é ¹Öé â´ÂµÑ§é ª×Íè ä¿ÅäźÃÒÃÕ໚¹ gp2d120.h â´ÂÁÕÃÒÂÅÐàÍÕ´«ÍÏÊâ»Ãá¡ÃÁ áÊ´§ã¹â»Ãá¡ÃÁ·Õè 6-1 ÊÒÁÒöແ´â»Ãá¡ÃÁ RIDE à¾×Íè ÊÌҧâ»Ãá¡ÃÁáŌǨѴà¡çºà»š¹ä¿Å¹ÒÁÊ¡ØÅ .h ãˌÍÂÙ㋠¹â¿Åà´ÍÏà´ÕÂǡѺä¿Åâ»Ãà¨ç¡µ·¡Õè Òí Åѧ¾Ñ²¹Ò à¾×Íè ª‹ÇÂãˌ¡ÒÃàª×Íè Áâ§ä¿Åã¹¢Ñ¹é µÍ¹¡Òà ¤ÍÁä¾ÅÊÒÁÒö´íÒà¹Ô¹¡ÒÃ䴌ÊдǡáÅÐÅ´¤ÇÒÁ¼Ô´¾ÅҴ䴌Í‹ҧÁÒ¡

6.4.2 ¤íÒ͸ԺÒ¿˜§¡ª¹Ñè ã¹ä¿ÅäźÃÒÃÕ gp2d120.h ÁÕ¿§˜ ¡ªÑ¹è ãˌàÃÕ¡㪌§Ò¹à¾Õ§¿˜§¡ª¹Ñè à´ÕÂǤ×Í dist ´Ñ§ÁÕÃÒÂÅÐàÍÕ´ µ‹Í仹Õé ¿˜§¡ª¹Ñè dist : ·íÒ˹ŒÒ·ÕÍè ҋ ¹¤‹ÒÃÐÂзҧ㹪‹Ç§ 4-32 ૹµÔàÁµÃ ÃٻẺ¿˜§¡ª¹Ñè

unsigned int dist(char adc_ch)

¾ÒÃÒÁÔàµÍÏ

adc_ch

¡Òä׹¤‹Ò

¤×¹¤‹ÒÃÐÂзҧ·ÕÍè ÂÙ㋠¹¢Íºà¢µ¡Ò÷íÒ§Ò¹¢Í§ GP2D120 ¹Ñ¹è ¤×Í 4 ¶Ö§ 32 cm

¡íÒ˹´ª‹Í§ÍÔ¹¾ØµÍйÒÅÍ¡¢Í§ºÍÏ´ RBX-51AC2 plus ·Õµè ͋ à¢ŒÒ ¡ÑºâÁ´ÙÅ GP2D120 ÊÒÁÒöàÅ×Í¡µ‹ÍࢌҡѺ AN0 ¶Ö§ AN5


98làÃÕ¹ÃÙá Œ ÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ

MCS-51

¡Ô¨¡ÃÃÁ·Õè 12

·´Êͺ͋ҹ¤‹Ò¨Ò¡âÁ´ÙÅ GP2D120 A12.1 ແ´â»Ãá¡ÃÁ RIDE áŌÇà¢Õ¹â»Ãá¡ÃÁ·Õè A12-1 ºÑ¹·Ö¡ã¹ª×Íè ä¿Å act1201.c ·íÒ¡ÒäÍÁä¾Å áŌǴÒǹ âËÅ´ä¿ÅÊáً ¼§Ç§¨Ã RBX-51AC2 plus ¢Í§Ëع‹ ¹µ Robo-51 ´ŒÇÂâ»Ãá¡ÃÁ Flip A12.2 Ãѹâ»Ãá¡ÃÁ ·´Åͧ¹íÒÇѵ¶ØÁÒÇÒ§¢Çҧ˹ŒÒâÁ´ÙÅ GP2D120 «Ö§è µÔ´µÑ§é ÍÂÙ·‹ Ò§´ŒÒ¹Ë¹ŒÒ¢Í§Ëع‹ ¹µ Robo51 áŌǻÃѺà»ÅÕÂè ¹ÃÐÂÐˋҧ Êѧࡵ¼Å¡Ò÷íÒ§Ò¹¼‹Ò¹âÁ´ÙÅ LCD º¹á¼§Ç§¨Ã RBX-51AC2 plus àÁ×Íè àÃÔÁè ·íÒ§Ò¹·Õâè Á´ÙÅ LCD ¢Í§Ëع‹ ¹µ Robo-51 áÊ´§¢ŒÍ¤ÇÒÁ Distance: 5 cm áÊ´§Ç‹Ò ÁÕÇµÑ ¶ØÍÂÙˋ ҋ §¨Ò¡âÁ´ÙÅ GP2D120 »ÃÐÁÒ³ 5 ૹµÔàÁµÃ ᵋ¶ÒŒ ËÒ¡Çѵ¶ØÍÂÙˋ ҋ §¨Ò¡âÁ´ÙÅ GP2D120 ã¹ÃÐÂзÕãè ¡ÅŒ¡Ç‹Ò 4 ૹµÔàÁµÃËÃ×Íä¡Å¡Ç‹Ò 32 ૹµÔÁàµÃ ·Õâè Á´ÙÅ LCD ¢Í§Ëع‹ ¹µ Robo-51 áÊ´§¢ŒÍ¤ÇÒÁ Out

of

Range

â»Ãá¡ÃÁ·Õè A12-1 â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺ͋ҹ¤‹ÒÃÐÂзҧ·Õµè ÃǨ¨Ñºä´Œ¨Ò¡âÁ´ÙÅ GP2D120 /*—————————————————————————————————————*/ // Program : GP2D120 demo // Description : Get distance from GP2D120 // Filename : act1201.c /*—————————————————————————————————————*/ #include <C51ac2.h> // Header include register of P89C51AC2 #include <lcd_robo51.h> // Module function LCD display #include <delay.h> // Module function Delay #include <gp2d120.h> // Module function get distance from GP2D120 void main(void) { unsigned int val; // Declare distance data variable(cm unit), // integer type recommended lcd_init(); // Initial LCD module while(1) // Looping { // Read digital data from conversion to variable val val = dist(1); if(val>=4 && val<=32) { lcd_putstr(line1,”Distance: cm “); // Show message “Distance cm” on LCD module inttolcd(0x8A,val,Dec); // Convert digital data to show on LCD display } else { lcd_putstr(line1,”Out of Range “); // Show message on LCD module } delay_ms(1000); // Delay 0.1 second for display value lcd_clear(); // Clear display } Robo-51 }


àÃÕ¹ÃÙጠÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ MCS-51 l 99

¡Ô¨¡ÃÃÁ·Õè 13

Ëع‹ ¹µ Robo-51 ËźËÅÕ¡ÊÔ§è ¡Õ´¢ÇÒ§ ẺäÁ‹ÊÑÁ¼ÑÊ ¨Ò¡¡Ô¨¡ÃÃÁ·Õè 12 ·Õäè ´ŒÁ¡Õ ÒÃà¢Õ¹â»Ãá¡ÃÁà¾×Íè µÃǨÊͺ¡Ò÷íÒ§Ò¹¢Í§âÁ´ÙÅ GP2D120 ä»áÅŒÇ ÁÒ¶Ö§ ¡Ô¨¡ÃÃÁ¹Õ¨é йíҼšÒ÷´Êͺ·Õäè ´Œ¹¹Ñé ÁҤǺ¤ØÁ¡ÒâѺà¤Å×Íè ¹Ëع‹ ¹µ Robo-51 à¾×Íè ËźËÅÕ¡ÊÔ§è ¡Õ´¢ÇҧẺäÁ‹ ÊÑÁ¼ÑÊËÃ×ͪ¹¡ÑºÊÔ§è ¡Õ´¢ÇÒ§¹Ñ¹é A13.1 ແ´â»Ãá¡ÃÁ RIDE áŌÇà¢Õ¹â»Ãá¡ÃÁ·Õè A13-1 ºÑ¹·Ö¡ã¹ª×Íè ä¿Å act1301.c ·íÒ¡ÒäÍÁä¾Å áŌǴÒǹ âËÅ´ä¿ÅÊáً ¼§Ç§¨Ã RBX-51AC2 plus ¢Í§Ëع‹ ¹µ Robo-51 ´ŒÇÂâ»Ãá¡ÃÁ Flip »Å´ÊÒ´ÒǹâËÅ´ÍÍ¡ A13.2 ¹íÒËع‹ ¹µ Robo-51 ÁÒÇÒ§ã¹¾×¹é ·Õ·è µÕè ͌ §¡Ò÷´Êͺ «Ö§è ¨ÐÁÕʧÔè ¡Õ´¢ÇÒ§ÇÒ§ÍÂً Ãѹâ»Ãá¡ÃÁ ¡Ò÷íÒ§Ò¹¢Í§Ëع‹ ¹µ Robo-51 ໚¹´Ñ§¹Õé àÃÔÁè µŒ¹´ŒÇ¡ÒÃà¤Å×Íè ¹·Õµè ç仢ŒÒ§Ë¹ŒÒ àÁ×Íè ¾ºÊÔ§è ¡Õ´¢ÇÒ§ Ëع‹ ¹µ Robo-51 ¨Ð¢Ñºà¤Å×Íè ¹¶ÍÂËÅѧáÅŒÇ àÅÕÂé Ç¢ÇÒ à¾×Íè à»ÅÕÂè ¹àʌ¹·Ò§¡ÒÃà¤Å×Íè ¹·Õ¢è ͧËع‹ ¹µà¾×Íè äÁ‹ãˌª¹ÊÔ§è ¡Õ´¢ÇÒ§

E

ª‹Ç§¨Ñ§ËÇÐ㹡ÒÃàÅÕÂé ǢͧËع‹ ¹µ Robo-51 ¨ÐÊÁºÙóÁÒ¡ËÃ×͹ŒÍ¹ѹé ʋǹ˹֧è ÁÒ¨Ò¡ª‹Ç§àÇÅÒ㹡ÒÃ˹‹Ç§ «Ö觼ٌ¾Ñ²¹ÒµŒÍ§»ÃѺᵋ§à¾×èͤÇÒÁàËÁÒÐÊÁ


100làÃÕ¹ÃÙá Œ ÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ

MCS-51

â»Ãá¡ÃÁ·Õè A13-1 â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺ¤Çº¤ØÁ¡Ò÷íÒ§Ò¹¢Í§Ëع‹ ¹µ Robo-51 ãˌÊÒÁÒö ËźËÅÕ¡ÊÔ§è ¡Õ´¢ÇҧẺäÁ‹ÊÁÑ ¼ÑÊ´ŒÇ¡ÒÃ㪌âÁ´ÙÅ GP2D120 /*—————————————————————————————————-——————-————————-—-———————*/ // Program : Robo-51 escape object by GP2D120 sensor // Description : Robo-51 move by dc motor and detect object by GP2D120 sensor // Filename : act1301.c // C compiler : RIDE 51 V6.1 /*———————————————————————————-——————-————————-————-——————————*/ #include <C51ac2.h> // Header include register of P89C51AC2 #include <lcd_robo51.h> // Module function LCD display #include <dc_motor.h> // Module function drive DC motor #include <gp2d120.h> // Module function drive DC motor #include <delay.h> // Module function delay time #define pow 200 // Define constant for power drive DC motor /***************** Function drive robot forward ******************************/ void run_fd(int time_spin) { motor_fd(2,pow); // Drive left motor forward motor_fd(1,pow); // Drive right motor forward delay_ms(time_spin); // Delay for robot spin } /***************** Function drive robot backward ******************************/ void run_bk(int time_spin) { motor_bk(2,pow); // Drive left motor backward motor_bk(1,pow); // Drive right motor backward delay_ms(time_spin); // Delay for robot spin } /***************** Function drive robot turn left ****************************/ void turn_left(int time_spin) { motor_fd(2,pow); // Drive left motor forward motor_bk(1,pow); // Drive right motor backward delay_ms(time_spin); // Delay for robot spin } /***************** Function drive robot turn right ***************************/ void turn_right(int time_spin) { motor_bk(2,pow); // Drive left motor backward motor_fd(1,pow); // Drive right motor forward delay_ms(time_spin); // Delay for robot spin } /****************************** Main function ********************************/ void main() { unsigned int val; // Variable for keep distance while(1) // Looping { val = dist(1); // Get distance from GP2D120 if(val<15) // Distance less than 15 cm? { run_bk(1000); // Backward 1 sec turn_right(500); // Turn right 0.5 sec } else { run_fd(10); // Forward 10 ms } } }


àÃÕ¹ÃÙጠÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ MCS-51 l 101

º··Õè 7 Ëع‹ ¹µ Robo-51 à¤Å×Íè ¹·Õµè ÒÁàʌ¹ 㹡ÒþѲ¹Òâ»Ãá¡ÃÁÀÒÉÒ C à¾×Íè ¤Çº¤ØÁãˌ˹؋ ¹µ Robo-51 à¤Å×Íè ¹·Õµè ÒÁàʌ¹ä´Œ¹¹Ñé ÁÕËÅÑ¡ ¡Òä×Í Ë؋¹Â¹µ·íÒ¡ÒõÃǨÇÑ´¤‹ÒÍйÒÅÍ¡¨Ò¡á¼§Ç§¨ÃµÃǨ¨Ñº¡ÒÃÊзŒÍ¹áʧÍÔ¹¿ÃÒàô (IR reflector) «Ö§è ¹íÒÁÒ»ÃÐÂØ¡µãªŒà»š¹µÑǵÃǨ¨Ñºàʌ¹ ¤‹Ò·ÕÍè ҋ ¹ä´Œ¨Ð¹íÒä»á»Å¤ÇÒÁËÁÒÂáŌÇà»ÃÕºà·Õº ¡Ñº¤‹Ò͌ҧÍÔ§à¾×Íè µÕ¤ÇÒÁÇ‹Ò ³ ¢³Ð¹Ñ¹é µíÒá˹‹§¢Í§µÑǵÃǨ¨ÑºµÃǨ¾ºàʌ¹·Õ¡è Òí ˹´ËÃ×ÍäÁ‹ ¼Å·Õäè ´Œ¨Ò¡ ¡ÒõÃǨÊͺ¹Õ¨é ж١¹íÒä»ÊÑ§è ¡ÒÃãˌ¢ºÑ à¤Å×Íè ¹Ëع‹ ¹µä»ã¹·ÔÈ·Ò§ã´æ à¾×Íè µÍºÊ¹Í§µÒÁ¨Ø´»ÃÐʧ¤ ¢Í§¼Ù¾Œ ² Ñ ¹Ò ઋ¹ àÁ×Íè µÃǨ¾ºàʌ¹áŌÇãˌà¤Å×Íè ¹·Õäè »µÒÁàʌ¹ ËÃ×ÍËÂØ´ ËÃ×Íà¤Å×Íè ¹·Õ¶è ÍÂˋҧ¨Ò¡àʌ¹

7.1 ËÑÇã¨ÊíҤѭ¤×Í á¼§Ç§¨ÃµÃǨ¨Ñº¡ÒÃÊзŒÍ¹áʧÍÔ¹¿ÃÒàô ໚¹á¼§Ç§¨Ã·Õè㪌㹡ÒõÃǨÊͺ¡ÒÃÊзŒÍ¹¢Í§áʧÍÔ¹¿ÃÒàô¢Í§µÑǵÃǨ¨Ñºáʧ ÊзŒÍ¹ÍÔ¹¿ÃÒàô«Ö§è ÃÇÁµÑÇʋ§áÅеÑÇÃѺänj㹵ÑǶѧà´ÕÂǡѹ â´Â µÑǵÃǨ¨ÑºáʧÊзŒÍ¹ÍÔ¹¿ÃÒàô·Õè ¹íÒÁÒ㪌¤Í× TCRT5000 ã¹ÃÙ»·Õè 7-1 áÊ´§Ç§¨Ã¢Í§á¼§Ç§¨ÃµÃǨ¨ÑºáʧÊзŒÍ¹ÍÔ¹¿ÃÒàô àÁ×Íè ¨‹Ò ä¿àÅÕÂé § LED ÍÔ¹¿ÃÒàôÀÒÂã¹Ç§¨Ã¨Ðà»Å‹§áʧÍÍ¡ÁÒµÅÍ´àÇÅÒ Ê‹Ç¹µÑÇÃѺ«Ö§è ໚¹â¿âµŒ·ÃÒ¹«ÔÊàµÍÏ ¨Ðä´ŒÃºÑ áʧÍÔ¹¿ÃÒàô¨Ò¡¡ÒÃÊзŒÍ¹¡ÅѺ «Ö§è ¨ÐÁÒ¡ËÃ×͹ŒÍ¡碹Öé ÍÂÙ¡‹ ºÑ Ç‹Ò ÁÕÇµÑ ¶ØÁÒ¡Õ´¢ÇÒ§ËÃ×ÍäÁ‹ áÅÐÁÕ¤ÇÒÁÊÒÁÒö㹡ÒÃÊзŒÍ¹áʧÍÔ¹¿ÃÒàô䴌´àÕ ¾Õ§äà «Ö§è ¢Ö¹é ¡ÑºÅÑ¡É³Ð¾×¹é ¼ÔÇáÅÐÊբͧÇѵ¶Ø â´Â Çѵ¶ØÊ¢Õ ÒǼÔÇàÃÕº¨ÐÊзŒÍ¹áʧÍÔ¹¿ÃÒàô䴌´·Õ ÊÕè ´Ø ·íÒãˌµÇÑ ÃѺáʧÍÔ¹¿ÃÒàÃ´ä´ŒÃºÑ áʧÊзŒÍ¹ÁÒ¡ ʋ§¼Åãˌáç´Ñ¹·Õàè ÍÒµ¾µØ ¢Í§Ç§¨ÃÊÙ§µÒÁä»´ŒÇ 㹢³Ð·ÕÇè µÑ ¶ØÊ´Õ Òí ËÃ×ÍÊÕࢌÁ¨ÐÊзŒÍ¹áʧÍÔ¹¿ÃÒàô 䴌¹ÍŒ ·ÕÊè ´Ø ·íÒãˌµÇÑ ÃѺÍÔ¹¿ÃÒàôʋ§áç´Ñ¹ÍÍ¡ÁÒµèÒí ¨Ø´µ‹ÍÊÑ­­Ò³ +V TCRT5000

OUT GND

10k

510

µÑǵÃǨ¨ÑºáʧÊзŒÍ¹ÍÔ¹¿ÃÒàô

ÃÙ»·Õè 7-1 ÃٻËҧáÅÐǧ¨Ã¢Í§á¼§Ç§¨ÃµÃǨ¨ÑºáʧÊзŒÍ¹ÍÔ¹¿ÃÒàô·Õ¹è Òí ÁÒ㪌µÃǨ¨Ñºàʌ¹


102làÃÕ¹ÃÙá Œ ÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ

MCS-51

7.2 ¡Ò÷´Êͺ͋ҹ¤‹Ò¨Ò¡á¼§Ç§¨ÃµÃǨ¨ÑºáʧÊзŒÍ¹ÍÔ¹¿ÃÒàô¢Í§ Ëع‹ ¹µ Robo-51 ἧǧ¨ÃµÃǨ¨ÑºáʧÊзŒÍ¹ÍÔ¹¿ÃÒàô¶Ù¡µÔ´µÑ§é änj´ÒŒ ¹Å‹Ò§¢Í§Ëع‹ ¹µµ§Ñé áµ‹ã¹¢Ñ¹é µÍ¹¡Òà ÊÌҧáÅŒÇ ¨Ö§·´Êͺ䴌·¹Ñ ·Õ à¹×Íè §¨Ò¡á¼§Ç§¨ÃµÃǨ¨ÑºáʧÊзŒÍ¹ÍÔ¹¿ÃÒàôãˌ¼Å¡Ò÷íҧҹ໚¹áç ´Ñ¹ä¿µÃ§àª‹¹à´ÕÂǡѺἧǧ¨ÃµÑǵŒÒ¹·Ò¹»ÃѺ¤‹Ò䴌«§Öè 䴌¡Å‹ÒǶ֧ä»áŌÇ㹺··Õè 5 ´Ñ§¹Ñ¹é àÁ×Íè µŒÍ§¡Òà 㪌§Ò¹¡ÑºËع‹ ¹µ Robo-51 ¨Ö§µŒÍ§µ‹ÍࢌҡѺª‹Í§ÍÔ¹¾ØµÍйÒÅÍ¡¢Í§Ëع‹ ¹µ Robo-51 «Ö§è ÁÕãˌàÅ×Í¡·Ñ§é ÊÔ¹é 6 ª‹Í§¤×Í ª‹Í§ AN0 ¶Ö§ AN5 ¨Ò¡¹Ñ¹é 㪌¤ÇÒÁÃÙ¨Œ Ò¡¡ÒÃ͋ҹ¤‹ÒÊÑ­­Ò³ÍйÒÅ͡㹺··Õè 5 à¾×Íè ͋ҹ ¤‹Ò¨Ò¡á¼§Ç§¨ÃµÃǨ¨ÑºáʧÊзŒÍ¹ÍÔ¹¿ÃÒàô «Ö§è ¹íÒä»ÊÙ¡‹ ÒõÃǨ¨Ñºàʌ¹µ‹Íä»

¡Ô¨¡ÃÃÁ·Õè 14

͋ҹ¤‹Ò¨Ò¡á¼§Ç§¨ÃµÃǨ¨ÑºáʧÊзŒÍ¹ ÍÔ¹¿ÃÒàô¢Í§Ëع‹ ¹µ Robo-51 A14.1 ແ´â»Ãá¡ÃÁ RIDE áŌÇà¢Õ¹â»Ãá¡ÃÁ·Õè A12-1 ºÑ¹·Ö¡ã¹ª×Íè ä¿Å act1201.c ·íÒ¡ÒäÍÁä¾Å áŌǴÒǹ âËÅ´ä¿ÅÊáً ¼§Ç§¨Ã RBX-51AC2 plus ¢Í§Ëع‹ ¹µ Robo-51 ´ŒÇÂâ»Ãá¡ÃÁ Flip A14.2 µ‹ÍÊÒ¨ҡἧǧ¨ÃµÃǨ¨ÑºáʧÊзŒÍ¹ÍÔ¹¿ÃÒàôࢌҨشµ‹Í AN5 áÅÐ AN4 («Ö§è µ‹ÍänjáŌǵѧé ᵋ¢¹Ñé µÍ¹ ¡ÒÃÊÌҧËع‹ ¹µ Robo-51 㹺··Õè 3) A14.3 ¹íÒË؋¹Â¹µä»ÇÒ§º¹¾×é¹·ÕèÊÕ´íÒáŌǷíÒ¡ÒÃÃѹâ»Ãá¡ÃÁ ÊѧࡵáÅкѹ·Ö¡¤‹Ò·ÕèáÊ´§º¹âÁ´ÙÅ LCD ¢Í§ Ëع‹ ¹µ Robo-51 ¨Ò¡¹Ñ¹é à»ÅÕÂè ¹ä»ÇÒ§º¹¾×¹é ·ÕÊè ¢Õ ÒÇ ÊѧࡵáÅкѹ·Ö¡àª‹¹à´ÕÂǡѹ ·ÕèâÁ´ÙÅ LCD áÊ´§¤‹Ò·Õè͋ҹ䴌¨Ò¡ª‹Í§ÍÔ¹¾ØµÍйÒÅÍ¡ AN4 áÅжŒÒËÒ¡¤‹Ò·Õè͋ҹ䴌ÁÒ¡¡Ç‹Ò¤‹Ò ͌ҧÍÔ§ «Ö§è ¡íÒ˹´änj෋ҡѺ 400 ¨Ðä´ŒÂ¹Ô àÊÕ§ “µÔ´é ” ¨Ò¡ÅíÒ⾧ໂÂ⫢ͧËع‹ ¹µ Robo-51 «Ö§è ¨Ð㪌¼Å¨Ò¡¡Ô¨¡ÃÃÁ ¹Õãé ¹¡ÒõÃǨ¨Ñºàʌ¹µ‹Íä»


àÃÕ¹ÃÙጠÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ MCS-51 l 103

â»Ãá¡ÃÁ·Õè A14-1 â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺ·´Êͺ͋ҹ¤‹Ò¨Ò¡á¼§Ç§¨ÃµÃǨ¨Ñº¡ÒÃÊзŒÍ¹áʧ ÍÔ¹¿ÃÒàô¢Í§Ëع‹ ¹µ Robo-51 /*————————————————————————————————————————————————————————————————————————*/ // Program : Reading IR reflect sensor // Description : Reading white and black surface from IR reflect sensor // Filename : act1401.c // C compiler : RIDE 51 V6.1 /*————————————————————————————————————————————————————————————————————————*/ #include <C51ac2.h> // Header include register of P89C51AC2 #include <lcd_robo51.h>// Module function LCD display #include <delay.h> // Module function Delay #include <adc_robo51.h>// Module function Analog to digital converter #include <sound.h> // Module function Sound #define ref 400 // Define reference value as 400 void main(void) { unsigned int reflect_dat;

// Declare reflect_dat for keeping analog // data from IR reflector lcd_init(); // Initailize LCD module lcd_putstr(line1,"Reflect CH4"); // Show message "Reflect CH4" on LCD module while(1) // Looping { reflect_dat = analog(4); // Read analog value from IR reflect sensor //and collect data at reflect_dat inttolcd(0xC7,reflect_dat,Dec); // Display analog value on LCD module if(reflect_dat>ref) // Compare analog value with reference beep(); // Beep if reflect_dat > reference delay_ms(500); // Delay 0.5 second }

}

ÃÒÂÅÐàÍÕ´à¾ÔÁè àµÔÁ¢Í§â»Ãá¡ÃÁ ¡Ò÷íÒ§Ò¹¢Í§â»Ãá¡ÃÁ¹Õé໚¹¡ÒÃ͋ҹ¤‹ÒÍйÒÅÍ¡¨Ò¡ª‹Í§ AN4 ·Õ赋ÍࢌҡѺἧǧ¨ÃµÃǨ¨Ñº¡Òà ÊзŒÍ¹áʧÍÔ¹¿ÃÒàô â´ÂàÃÕ¡͋ҹ´ŒÇ¿˜§¡ª¹Ñè analog áŌǹíÒÁÒà¡çºänj·µÕè ÇÑ á»Ã reflect_dat áÅÐ ¹íÒ¤‹Ò·ÕÍè ҋ ¹ä´Œ«§Öè ໚¹µÑÇàÅ¢¨íҹǹàµçÁ int ä»á»Å§à»š¹ÃËÑÊáÍÊ¡Õàé ¾×Íè ʋ§ä»áÊ´§¼Å·Õâè Á´ÙÅ LCD ËÅѧ¨Ò¡¹Ñ¹é â»Ãá¡ÃÁ¨Ð¹íÒ¤‹ÒÍйÒÅÍ¡·ÕÍè ҋ ¹ä´Œä»à»ÃÕºà·Õº¡Ñº¤‹Ò ref (Áդҋ ໚¹ 400) NjÒÁÒ¡¡Ç‹ÒËÃ×ÍäÁ‹ ¶ŒÒ㪋¨Ð ʋ§àÊÕ§ “µÔ´é ” áŌǨ֧ǹ¡ÅѺä»àÃÔÁè µŒ¹Í‹Ò¹¤‹ÒãËÁ‹áÅÐǹ´íÒà¹Ô¹¡ÒõÒÁ¢Ñ¹é µÍ¹ä»µÅÍ´ Robo-51


104làÃÕ¹ÃÙá Œ ÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ

MCS-51

7.3 ¡ÒõÃǨ¨Ñºàʌ¹¢Í§Ëع‹ ¹µ Robo-51 ã¹ËÑÇ¢ŒÍ¹Õ¹é Òí àʹÍá¹Ç·Ò§ã¹¡ÒõÃǨ¨Ñºàʌ¹ÊÕ´Òí ¢Í§Ëع‹ ¹µ Robo-51 â´ÂµŒÍ§·íÒ¡ÒÃ͋ҹ ¤‹ÒÍÔ¹¾ØµÍйÒÅÍ¡¨Ò¡á¼§Ç§¨ÃµÃǨ¨ÑºáʧÊзŒÍ¹ÍÔ¹¿ÃÒàô«Ö§è ¹íÒÁÒ㪌µÃǨ¨Ñºàʌ¹ â´Â¤íҹdz ËÒ¤‹Ò¢Í§ÊÑ­­Ò³ÍйÒÅÍ¡·ÕèÊÒÁÒöᡤÇÒÁᵡµ‹Ò§ã¹¡ÒÃÊзŒÍ¹áʧ·Õèàʌ¹ÊÕ´íÒáÅкÃÔàdz ʹÒÁ·Õàè »š¹ÊÕ¢ÒÇ â´ÂÍÒ¨à¢Õ¹â»Ãá¡ÃÁ͋ҹ¤‹Ò¨Ò¡á¼§Ç§¨ÃµÃǨ¨Ñº¡ÒÃÊзŒÍ¹áʧÍÔ¹¿ÃÒàô·Ñ§é ÊͧµÑǢͧËع‹ ¹µ Robo-51 䴌´§Ñ ¹Õé µÑÇÍ‹ҧ·Õè 7-1 ¡íÒ˹´ãˌἧǧ¨ÃµÃǨ¨Ñº¡ÒÃÊзŒÍ¹áʧÍÔ¹¿ÃÒàô·Õµè ͋ ¡Ñºª‹Í§ AN4 ໚¹µÑǵÃǨ¨Ñº·Ò§´ŒÒ¹«ŒÒ áÅÐἧ ǧ¨ÃµÃǨ¨Ñº¡ÒÃÊзŒÍ¹áʧÍÔ¹¿ÃÒàô·Õµè ͋ ¡Ñºª‹Í§ AN5 ໚¹µÑǵÃǨ¨Ñº·Ò§´ŒÒ¹¢ÇÒ #include <C51ac2.h> #include <delay.h> #include <adc_robo51.h> #include <lcd_robo51.h> void main() { lcd_init(); while(1) { lcd_command(1);

// »ÃСÒÈÃÕ¨ÊÔ àµÍÏÀÒÂã¹µÑÇ T89C51AC2 ãˌ¤ÍÁä¾àÅÍÏèٌ ¡Ñ // »ÃСÒÈäźÃÒÃÕà¡ÕÂè ǡѺ¡ÒÃ˹‹Ç§àÇÅÒãˌ¤ÍÁä¾àÅÍÏèٌ ¡Ñ // »ÃСÒÈäźÃÒÃÕ¡ÒÃ͋ҹ¤‹ÒÍÔ¹¾ØµÍйÒÅÍ¡¢Í§Ëع‹ ¹µ Robo-51 // »ÃСÒÈäźÃÒÃÕ¡ÒÃáÊ´§¼Å·Õâè Á´ÙÅ LCD ¢Í§Ëع‹ ¹µ Robo-51

lcd_putstr(0x80,”L”); inttolcd(0x82,analog(5),Dec); lcd_putstr(0xC0,”R”); inttolcd(0xC2,analog(4),Dec); delay_ms(100);

// áÊ´§µÑÇÍÑ¡Éà L ¡íҡѺµÑǵÃǨ¨Ñºàʌ¹´ŒÒ¹«ŒÒ // ͋ҹ¤‹ÒÍйÒÅÍ¡¨Ò¡µÑǵÃǨ¨Ñºàʌ¹´ŒÒ¹«ŒÒ // áÊ´§µÑÇÍÑ¡Éà R ¡íҡѺµÑǵÃǨ¨Ñºàʌ¹´ŒÒ¹¢ÇÒ // ͋ҹ¤‹ÒÍйÒÅÍ¡¨Ò¡µÑǵÃǨ¨Ñºàʌ¹´ŒÒ¹¢ÇÒ // ˹‹Ç§àÇÅÒà¾×Íè áÊ´§¼Å

} }

¨Ò¡¡Ò÷´ÅͧÇÑ´¤‹Ò䴌¼ÅÅѾ¸¤Í× àÁ×Íè ¾ºàʌ¹ÊÕ´Òí ¨Ð͋ҹ¤‹Ò䴌ªÇ‹ § 30 ¶Ö§ 100 ã¹¢³Ð·Õè¾¹×é ÊÕ¢ÒÇ͋ҹ䴌 600 ¶Ö§ 800 ´Ñ§¹Ñ¹é ¨Ö§àÅ×Í¡¤‹Ò͌ҧÍԧ㹡ÒõÃǨ¨Ñºàʌ¹ÊÕ´Òí ໚¹ 400 ¡Å‹ÒǤ×Í ¶ŒÒ͋ҹ¤‹Ò¨Ò¡á¼§Ç§¨Ã µÃǨ¨Ñº¡ÒÃÊзŒÍ¹áʧÍÔ¹¿ÃÒàôµÑÇã´áŌǾºÇ‹Ò Áդҋ ¹ŒÍÂ¡Ç‹Ò 400 ãˌ¶Í× Ç‹Òἧǧ¨ÃµÃǨ¨Ñº¡ÒÃÊзŒÍ¹áʧ ÍÔ¹¿ÃÒàôµÑǹѹé ÍÂÙº‹ ÃÔàdzàʌ¹ËÃ×Í¾×¹é ·ÕÊè ´Õ Òí ᵋ¶ÒŒ ͋ҹ¤‹Ò䴌ÁÒ¡¡Ç‹Ò 400 áÊ´§Ç‹Òἧǧ¨ÃµÃǨ¨Ñº¡ÒÃÊзŒÍ¹ áʧÍÔ¹¿ÃÒàôµÑǹÑé¹ÍÂًºÃÔàdz¾×é¹·ÕèÊÕ¢ÒÇ


àÃÕ¹ÃÙጠÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ MCS-51 l 105

¡Ô¨¡ÃÃÁ·Õè 15

Ëع ‹ ¹µ Robo-51 ¤Œ¹ËÒàʌ¹ 㹡Ԩ¡ÃÃÁ¹Õ¹é Òí àʹ͡ÒäǺ¤ØÁËع‹ ¹µ Robo-51 ãˌ¤¹Œ ËÒàʌ¹ËÃ×Í¾×¹é ·ÕÊè ´Õ Òí â´Â㪌ἧǧ¨ÃµÃǨ¨Ñº áʧÊзŒÍ¹ÍÔ¹¿ÃÒàô·Ñé§ 2 µÑÇ·ÕèµÔ´µÑé§ÍÂً´ŒÒ¹ãµŒ¢Í§Ë؋¹Â¹µ â´Âµ‹ÍÊÒ¢ͧἧǧ¨ÃµÃǨ¨ÑºáʧÊзŒÍ¹ ÍÔ¹¿ÃÒàô´ŒÒ¹«ŒÒÂࢌҡѺª‹Í§ AN5 áÅÐἧǧ¨ÃµÃǨ¨Ñº¡ÒÃÊзŒÍ¹áʧÍÔ¹¿ÃÒàô´ŒÒ¹¢ÇÒµ‹Í¡Ñºª‹Í§ AN4 ¡Ò÷íÒ§Ò¹¢Í§â»Ãá¡ÃÁ¨ÐÊѧè ãˌ˹؋ ¹µà¤Å×Íè ¹·Õµè ç仢ŒÒ§Ë¹ŒÒµÅÍ´àÇÅÒáÅÐËÂØ´àÁ×Íè µÃǨ¾ºàʌ¹ËÃ×Í¾×¹é ·ÕÊè Õ ´íÒ ¾ÃŒÍÁ¡Ñºá¨Œ§¼Å¡ÒõÃǨ¾º´ŒÇÂàÊÕ§ÊÑ­­Ò³ 1 ¤Ãѧé ÅѡɳСÒ÷íÒ§Ò¹áÊ´§ä´ŒµÒÁÃÙ»·Õè A15-1

àÁ×èÍäÁ‹¾ºàʌ¹ÊÕ´íÒ

Ëع‹ ¹µà¤Å×Íè ¹·Õäè »¢ŒÒ§Ë¹ŒÒ ἧǧ¨ÃµÃǨ¨Ñº¡ÒÃÊзŒÍ¹áʧ ÍÔ¹¿ÃÒàô ·Õ¹è Òí ÁÒ㪌໚¹ ἧǧ¨ÃµÃǨ¨Ñºàʌ¹ Robo-51

ἧǧ¨ÃµÃǨ¨Ñº¡ÒÃÊзŒÍ¹áʧ ÍÔ¹¿ÃÒàô ·Õ¹è Òí ÁÒ㪌໚¹ ἧǧ¨ÃµÃǨ¨Ñºàʌ¹ Robo-51

àÁ×è;ºàʌ¹ÊÕ´íÒ

Ëع‹ ¹µËÂØ´à¤Å×Íè ¹·Õè

ἧǧ¨ÃµÃǨ¨Ñº¡ÒÃÊзŒÍ¹áʧ ÍÔ¹¿ÃÒàô ·Õ¹è Òí ÁÒ㪌໚¹ ἧǧ¨ÃµÃǨ¨Ñºàʌ¹ Robo-51

ἧǧ¨ÃµÃǨ¨Ñº¡ÒÃÊзŒÍ¹áʧ ÍÔ¹¿ÃÒàô ·Õ¹è Òí ÁÒ㪌໚¹ ἧǧ¨ÃµÃǨ¨Ñºàʌ¹ Robo-51

àÁ×èÍἧǧ¨ÃµÃǨ¨Ñºàʌ¹·Ò§«ŒÒ ¾ºàʌ¹ÊÕ´íÒ

àÁ×èÍἧǧ¨ÃµÃǨ¨Ñºàʌ¹·Ò§¢ÇÒ ¾ºàʌ¹ÊÕ´íÒ

Ëع‹ ¹µËÂØ´à¤Å×Íè ¹·Õè

Ëع‹ ¹µËÂØ´à¤Å×Íè ¹·Õè

ÃÙ»·Õè A15-1 ÃٻẺ¡Ò÷íÒ§Ò¹¢Í§Ëع‹ ¹µ Robo-51 à¾×Íè ¤Œ¹ËÒàʌ¹ÊÕ´Òí 㹡Ԩ¡ÃÃÁ¹Õé


106làÃÕ¹ÃÙá Œ ÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ

MCS-51

â»Ãá¡ÃÁ·Õè A15-1 â»Ãá¡ÃÁÀÒÉÒ C ·´Êͺ¡Ò䌹ËÒàʌ¹ÊÕ´Òí ¢Í§Ëع‹ ¹µ Robo-51 (Áյ͋ ) /*—————————————————————————————————————*/ // Program : Detect black line // Description : Drive robot for finding the black line by IR reflect sensor // Filename : act1501.c // C compiler : RIDE 51 V6.1 /*—————————————————————————————————————*/ #include <C51ac2.h> // Header include register of T89C51AC2 #include <lcd_robo51.h> // Module function LCD #include <dc_motor.h> // Module function drive DC motor #include <sound.h> // Module function drive sound #include <delay.h> // Module function delay process #include <adc_robo51.h> // Module function read input analog #define pow 100 // Define constant power drive robot #define ref 400 // Define constant reference compare track line /***************** Function drive robot forward ******************************/ void run_fd(int time_spin) { motor_fd(2,pow); // Drive left motor forward motor_fd(1,pow); // Drive right motor forward delay_ms(time_spin); // Delay for robot spin } /***************** Function drive robot backward ******************************/ void run_bk(int time_spin) { motor_bk(2,pow); // Drive left motor backward motor_bk(1,pow); // Drive right motor backward delay_ms(time_spin); // Delay for robot spin } /***************** Function drive robot void turn_left(int time_spin) { motor_fd(2,pow); // Drive motor_bk(1,pow); // Drive delay_ms(time_spin); // Delay } /***************** Function drive robot void turn_right(int time_spin) { motor_bk(2,pow); // Drive motor_fd(1,pow); // Drive delay_ms(time_spin); // Delay }

turn left ****************************/

left motor forward right motor backward for robot spin

turn right ***************************/

left motor backward right motor forward for robot spin

/****************************** Main function ********************************/ void main() { int left=0,right=0; // Define variable for keep input analog value lcd_init(); // Initial LCD module beep(); // Sound beep 1 time while(1) // Infinite loop


àÃÕ¹ÃÙጠÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ MCS-51 l 107

â»Ãá¡ÃÁ·Õè A15-1 â»Ãá¡ÃÁÀÒÉÒ C ·´Êͺ¡Ò䌹ËÒàʌ¹ÊÕ´Òí ¢Í§Ëع‹ ¹µ Robo-51 (Áյ͋ ) { left = analog(5); right = analog(4);

// Read input analog channel 5 (connect left sensor) // Read input analog channel 4 (connect right sensor)

if(left<ref || right<ref) // If any sensor detect black line { motor_stop(all); // Stop robot beep(); // Beep after any sensor detect line lcd_clear(); // Clear LCD display if(left<ref) { lcd_putstr(line1,”Left:>> Line”); // Left sensor detects line } else { lcd_putstr(line1,”Left:>> Floor”); // Left sensor detects floor } if(right<ref) { lcd_putstr(line2,”Right:>> Line”); // Right sensor detects line } else { lcd_putstr(line2,”Right:>> Floor”); // Right sensor detects floor } while(1); // Break program } else { run_fd(10); // Drive robot forward } }

}

ÃÒÂÅÐàÍÕ´à¾ÔÁè àµÔÁ¢Í§â»Ãá¡ÃÁ àÁ×Íè àÃÔÁè Ãѹâ»Ãá¡ÃÁ¨ÐÁÕÊ­ Ñ ­Ò³àÊÕ§ 1 ¤Ãѧé à¾×Íè ᨌ§¡ÒÃàÃÔÁè µŒ¹·íÒ§Ò¹ ËÅѧ¨Ò¡¹Ñ¹é Ëع‹ ¹µ¨Ðǹ͋ҹ ¤‹ÒÍйÒÅÍ¡¨Ò¡á¼§Ç§¨ÃµÃǨ¨Ñºàʌ¹·Ñ§é ÊͧµÑÇáŌǹíÒÁÒà»ÃÕºà·ÕÂºÇ‹Ò ¾ºàʌ¹ËÃ×ÍÂѧ ¤‹ÒÍйÒÅÍ¡·ÕÍè ҋ ¹ 䴌¨Ðà¡çºänj·µÕè ÇÑ á»Ã left (ÊíÒËÃѺἧǧ¨ÃµÃǨ¨Ñºàʌ¹´ŒÒ¹«ŒÒÂ«Ö§è µ‹ÍࢌҡѺª‹Í§ AN5) áÅÐ right (ÊíÒËÃѺ ἧǧ¨ÃµÃǨ¨Ñºàʌ¹´ŒÒ¹¢ÇÒ«Ö§è µ‹Í¡Ñºª‹Í§ AN4) ¨Ò¡¹Ñ¹é ¹íÒä»à»ÃÕºà·Õº¤‹Ò͌ҧÍÔ§ ref «Ö§è ¤ÇÒÁËÁÒ¢ͧ ¡ÒõÃǨ¨Ñºàʌ¹àÁ×Íè á»Å¨Ò¡à§×Íè ¹ä¢â»Ãá¡ÃÁÁÕ´§Ñ ¹Õé l ¶ŒÒà§×Í è ¹ä¢ left<ref “¨ÃÔ§” áÊ´§Ç‹Ò ἧǧ¨ÃµÃǨ¨Ñºàʌ¹´ŒÒ¹«ŒÒµÃǨ¾ºàʌ¹ÊÕ´Òí “äÁ‹¨ÃÔ§” áÊ´§Ç‹Ò ἧǧ¨ÃµÃǨ¨Ñºàʌ¹´ŒÒ¹«ŒÒµÃǨ¾º¾×¹é ÊÕ¢ÒÇ l

¶ŒÒà§×Íè ¹ä¢ right<ref “¨ÃÔ§” áÊ´§Ç‹Ò ἧǧ¨ÃµÃǨ¨Ñºàʌ¹´ŒÒ¹¢ÇÒµÃǨ¾ºàʌ¹ÊÕ´Òí “äÁ‹¨ÃÔ§” áÊ´§Ç‹Ò ἧǧ¨ÃµÃǨ¨Ñºàʌ¹´ŒÒ¹¢ÇÒµÃǨ¾º¾×¹é ÊÕ¢ÒÇ


108làÃÕ¹ÃÙá Œ ÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ

MCS-51

â»Ãá¡ÃÁ·Õè A15-1 â»Ãá¡ÃÁÀÒÉÒ C ·´Êͺ¡Ò䌹ËÒàʌ¹ÊÕ´Òí ¢Í§Ëع‹ ¹µ Robo-51 (¨º) ¨Ò¡¡ÒõÃǨÊͺà§×Íè ¹ä¢ã¹Ê‹Ç¹¢Í§ if(left<ref ËÁÒ¶֧

|| right<ref)

¡Ã³Õ·Õè 1

reft<ref “¨ÃÔ§”

ʋǹ right<ref “à·ç¨”

¡Ã³Õ·Õè 2

left<ref “à·ç¨”

ʋǹ right<ref “¨ÃÔ§”

¡Ã³Õ·Õè 3

left<ref “¨ÃÔ§”

ʋǹ right<ref “¨ÃÔ§”

¶ŒÒ¾ºÇ‹Ò໚¹”¨ÃÔ§”ÍÒ¨

Ëع‹ ¹µ Robo-51 ¨ÐËÂØ´à¤Å×Íè ¹·Õáè ÅÐʋ§ÊÑ­­Ò³àÊÕ§ÍÍ¡ÁÒ 1 ¤ÃÑ§é ¾ÃŒÍÁ¡ÑºÃÒ§ҹà˵ؼŢͧ¡Òà ËÂØ´à¤Å×è͹·ÕèNjÒà¡Ô´¨Ò¡¡Ã³Õã´¨Ò¡·Ñé§ 3 ¡Ã³Õ¢ŒÒ§µŒ¹¼‹Ò¹·Ò§âÁ´ÙÅ LCD â´Â·ÕèºÃ÷Ѵº¹¢Í§âÁ´ÙÅ LCD ¨ÐÃÒ§ҹ¼Å¨Ò¡¡Ò䌹ËÒÇ‹Ò ¾ºàʌ¹ËÃ×;׹é ʹÒÁ¨Ò¡á¼§Ç§¨ÃµÃǨ¨Ñºàʌ¹´ŒÒ¹«ŒÒ áÅкÃ÷ѴŋҧÊíÒËÃѺ ἧǧ¨ÃµÃǨ¨Ñºàʌ¹´ŒÒ¹¢ÇÒ ËÅѧ¨Ò¡¹Ñ¹é ¨Ö§àºÃ¡â»Ãá¡ÃÁ´ŒÇ¤íÒÊѧè while(1); Robo-51


àÃÕ¹ÃÙጠÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ MCS-51 l 109

¡Ô¨¡ÃÃÁ·Õè 16

Ëع ‹ ¹µ Robo-51 ping-pong 㹡Ԩ¡ÃÃÁ¹Õ¹é Òí àʹ͡ÒþѲ¹Òâ»Ãá¡ÃÁÀÒÉÒ C à¾×Íè ¤Çº¤ØÁËع‹ ¹µ Robo-51 ãˌà¤Å×Íè ¹·Õãè ¹ÅѡɳР«Ô¡á«¡ä»-ÁÒËÃ×ÍÊÅѺ¿˜¹»ÅÒ â´Â㪌àʌ¹ÊÕ´Òí ໚¹µÑÇ¡íÒ˹´¡ÒÃà»ÅÕÂè ¹·ÔÈ·Ò§¡ÒÃà¤Å×Íè ¹·Õµè ÒÁÃÙ»·Õè A14-1 â´Â Ëع‹ ¹µ Robo-51 ¨Ðà¤Å×Íè ¹·Õãè ¹·ÔÈ·Ò§µÃ§¨¹¡ÃÐ·Ñ§è ¾ºàʌ¹Ê¹ÒÁÊÕ´Òí ´ŒÒ¹ã´´ŒÒ¹Ë¹Ö§è ¡ç¨Ðà»ÅÕÂè ¹·ÔÈ·Ò§¡Òà à¤Å×Íè ¹·Õâè ´ÂËѹµÑÇàÅÕÂé ÇáŌÇà¤Å×Íè ¹·Õµè 絋Íä» àÁ×Íè ¾ºàʌ¹ÊÕ´Òí ¡ç¨Ðà»ÅÕÂè ¹·ÔÈ·Ò§¡ÒÃà¤Å×Íè ¹·ÕÍè ¡Õ ¤Ãѧé ǹ·íÒ§Ò¹ ઋ¹¹Õäé »µÅÍ´ ᵋ㹡ÒÃà¤Å×Íè ¹·Õ¢è ͧËع‹ ¹µ¨ÐÍÂÙÀ‹ ÒÂã¹ÃÐËNjҧàʌ¹ÊÕ´Òí ·Ñ§é 2 ½˜§›

Ro bo -5 1

ÃÙ»·Õè A16-1 áÊ´§àʌ¹·Ò§¡ÒÃà¤Å×Íè ¹·Õ¢è ͧËع‹ ¹µ Robo-51 Ping-Pong àÁ×Íè Áͧ¨Ò¡´ŒÒ¹º¹ á¹Ç¤Ô´ã¹¡ÒÃà¢Õ¹â»Ãá¡ÃÁ µ‹Íἧǧ¨ÃµÃǨ¨Ñº¡ÒÃÊзŒÍ¹áʧÍÔ¹¿ÃÒàô·Õ¹è Òí ÁÒ㪌໚¹µÃǨ¨Ñºàʌ¹·Ñ§é ÊͧµÑÇã¹ÅѡɳÐà´ÔÁ¤×Í á¼§Ç§¨ÃµÃǨ¨Ñºàʌ¹´ŒÒ¹«ŒÒµ‹Í¡Ñºª‹Í§ AN5 áÅÐἧǧ¨ÃµÃǨ¨Ñºàʌ¹´ŒÒ¹¢ÇÒµ‹Í¡Ñºª‹Í§ AN4 ´Ñ§¹Ñ¹é ¢Ñ¹é µÍ¹ ¡ÒÃà¢Õ¹â»Ãá¡ÃÁ໚¹´Ñ§¹Õé (1) ·´ÊͺËÒ¤‹Òà»ÃÕºà·ÕºÃÐËNjҧ¾×¹é ʹÒÁÊÕ¢ÒÇáÅÐàʌ¹ÊÕ´Òí (2) ͋ҹ¤‹ÒÍйÒÅÍ¡¨Ò¡ª‹Í§ AN4 áÅÐ AN5 áŌÇà¡çº¤‹Òà¾×Íè àµÃÕÂÁà»ÃÕºà·Õº (3) à»ÃÕºà·Õºà§×Íè ¹ä¢´Ñ§¹Õé ¡Ã³Õ·Õè 1 ¶ŒÒἧǧ¨ÃµÃǨ¨Ñºàʌ¹´ŒÒ¹«ŒÒÂáÅТÇÒ¾º¾×¹é ÊÕ¢ÒÇ µÍºÊ¹Í§â´Â¡ÒâѺãˌ˹؋ ¹µà¤Å×Íè ¹·Õµè ç仢ŒÒ§Ë¹ŒÒ ¡Ã³Õ·Õè 2 ¶ŒÒἧǧ¨ÃµÃǨ¨Ñºàʌ¹´ŒÒ¹«ŒÒ¾ºàʌ¹ÊÕ´Òí ʋǹ´ŒÒ¹¢ÇÒ¾º¾×¹é ÊÕ¢ÒÇ µÍºÊ¹Í§â´Â¡ÒÃËÁعµÑÇË؋¹Â¹µä»·Ò§´ŒÒ¹¢ÇÒã¹ÃÐÂÐàÇÅÒ·ÕèàËÁÒÐÊÁà¾×èÍãˌ ·ÔÈ·Ò§¡ÒÃà¤Å×Íè ¹·Õ¢è ͧËع‹ ¹µË¹Ñ ࢌÒÊÙà‹ ÊŒ¹ÊÕ´Òí ½˜§› µÃ§¢ŒÒÁ


110làÃÕ¹ÃÙá Œ ÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ

MCS-51

¡Ã³Õ·Õè 3 ¶ŒÒἧǧ¨ÃµÃǨ¨Ñºàʌ¹´ŒÒ¹«ŒÒ¾º¾×¹é ÊÕ¢ÒÇ Ê‹Ç¹´ŒÒ¹¢ÇÒ¾ºàʌ¹ÊÕ´Òí µÍºÊ¹Í§â´Â¡ÒÃËÁعµÑÇËع‹ ¹µä»·Ò§´ŒÒ¹«ŒÒÂ໚¹ª‹Ç§àÇÅÒÃÐÂÐàÇÅÒµÒÁ¤ÇÒÁ àËÁÒÐÊÁà¾×Íè ãˌ·ÈÔ ·Ò§¡ÒÃà¤Å×Íè ¹·Õ¢è ͧËع‹ ¹µË¹Ñ ࢌÒÊÙà‹ ÊŒ¹ÊÕ´Òí ½˜§› µÃ§¢ŒÒÁ ¡Ã³Õ·Õè 4 ¹Í¡à˹×ͨҡ¡Ã³Õ·§Ñé ÊÒÁ µÍºÊ¹Í§â´Â¡ÒâѺà¤Å×Íè ¹ãˌ˹؋ ¹µ¢ºÑ à¤Å×Íè ¹µÒÁ¨Ø´»ÃÐʧ¤¢Í§¼Ù¾Œ ² Ñ ¹Ò «Ö§è ÍÒ¨ ¨ÐãˌËÂØ´ËÃ×ͶÍÂËÅѧ¡ç䴌 (4) ¡ÅѺä»àÃÔÁè µŒ¹ã¹¢Ñ¹é µÍ¹·Õè (2) ãËÁ‹

¡ÒÃÊÌҧʹÒÁ·´Êͺ ¨Ñ´àµÃÕÂÁ¾×¹é ·Õàè ÃÕºÊÕ¢ÒÇËÃ×ÍÊÕÍ͋ ¹·ÕÊè ÒÁÒöÊзŒÍ¹áʧ䴌´Õ ¨Ò¡¹Ñ¹é ¹íÒà·»¾Ñ¹ÊÒÂä¿ÊÕ´Òí ÁÒµÔ´º¹¾×¹é 2 àʌ¹ÂÒÇ»ÃÐÁÒ³ 2 àÁµÃ¢¹Ò¹¡Ñ¹ (ÊíÒËÃѺ¤ÇÒÁÂÒÇÊÒÁÒöà»ÅÕÂè ¹ä´ŒµÒÁ¤ÇÒÁµŒÍ§¡ÒÃ) ÃÐÂÐˋҧÃÐËNjҧ àʌ¹äÁ‹¹ÍŒ Â¡Ç‹Ò 40 ૹµÔàÁµÃ à¾×Íè ãˌÁ¾Õ ¹×é ·ÕÊè Òí ËÃѺãˌ˹؋ ¹µËÁعµÑÇ䴌à¾Õ§¾Í ʋǹµíÒá˹‹§àÃÔÁè µŒ¹ÊíÒËÃѺ»Å‹ÍµÑÇËع‹ ¹µ Robo-51 ãËŒË¹Ñ Ë¹ŒÒà©Õ§·íÒÁØÁ»ÃÐÁÒ³ 45 ͧÈÒࢌÒËÒ àʌ¹´ŒÒ¹ã´´ŒÒ¹Ë¹Ö§è «Ö§è ´ÙµÇÑ Í‹ҧµíÒá˹‹§àÃÔÁè µŒ¹¢Í§Ëع‹ ¹µ´§Ñ ã¹ÃÙ»·Õè A16-1

¡Ò÷´Êͺ A16.1 ແ´â»Ãá¡ÃÁ RIDE áŌÇà¢Õ¹â»Ãá¡ÃÁ·Õè A14-1 ºÑ¹·Ö¡ã¹ª×Íè ä¿Å act1401.c ·íÒ¡ÒäÍÁä¾Å áŌǴÒǹ âËÅ´ä¿ÅÊáً ¼§Ç§¨Ã RBX-51AC2 ¢Í§Ëع‹ ¹µ Robo-51 ´ŒÇÂâ»Ãá¡ÃÁ Flip A16.2 µ‹ÍÊÒ¨ҡἧǧ¨ÃµÃǨ¨Ñº¡ÒÃÊзŒÍ¹áʧÍÔ¹¿ÃÒàôࢌҨشµ‹Í AN5 áÅÐ AN4 («Ö§è µ‹ÍänjáŌǵѧé ᵋ¢¹Ñé µÍ¹¡ÒÃÊÌҧËع‹ ¹µ Robo-51 㹺··Õè 3) A16.3 ¹íÒËع‹ ¹µä»ÇÒ§º¹¨Ø´àÃÔÁè µŒ¹ ¨Ò¡¹Ñ¹é ¡´ÊÇÔµªà¾×Íè ãˌ˹؋ ¹µ Robo-51 ·íÒ§Ò¹ Êѧࡵ¡ÒÃà¤Å×Íè ¹·Õ·è àÕè ¡Ô´ ¢Ö¹é Ç‹Ò à»š¹ä»µÒÁ·Õµè ͌ §¡ÒÃËÃ×ÍäÁ‹


àÃÕ¹ÃÙጠÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ MCS-51 l 111

â»Ãá¡ÃÁ·Õè A16-1 â»Ãá¡ÃÁÀÒÉÒ C ¤Çº¤ØÁãˌ˹؋ ¹µ Robo-51 à¤Å×Íè ¹·Õãè ¹¾×¹é ·Õ·è ¡Õè Òí ˹´ã¹ÅѡɳР»§»Í§ (Ping-Pong) (Áյ͋ ) /*————————————————————————————————————————————————————————*/ // Program : Ping-Pong Robot // Description : Drive robot by black line reflection // Filename : act1601.c // C compiler : RIDE 51 V6.1 /*————————————————————————————————————————————————————————*/ #include <C51ac2.h> // Header include register of T89C51AC2 #include <dc_motor.h> // Module function drive DC motor #include <sound.h> // Module function drive sound #include <delay.h> // Module function delay process #include <adc_robo51.h>// Module function read input analog #define pow 250 // Define constant power drive robot #define ref 400 // Define constant reference compare track line int left=0,right=0; // Define variable for keep input analog value /***************** Function drive robot forward ******************************/ void run_fd(int time_spin) { motor_fd(2,pow); // Drive left motor forward motor_fd(1,pow); // Drive right motor forward delay_ms(time_spin); // Delay for robot spin } /***************** Function drive robot backward ******************************/ void run_bk(int time_spin) { motor_bk(2,pow); // Drive left motor backward motor_bk(1,pow); // Drive right motor backward delay_ms(time_spin); // Delay for robot spin } /***************** Function drive robot void turn_left(int time_spin) { motor_fd(2,pow); // Drive motor_bk(1,pow); // Drive delay_ms(time_spin); // Delay } /***************** Function drive robot void turn_right(int time_spin) { motor_bk(2,pow); // Drive motor_fd(1,pow); // Drive delay_ms(time_spin); // Delay }

turn left ****************************/ left motor forward right motor backward for robot spin turn right ***************************/ left motor backward right motor forward for robot spin

/****************************** Main function ********************************/ void main() { beep(); // Sound beep 1 time while(1) // Infinite loop { left = analog(5); // Read input analog channel 5(connect left sensor) // Read input analog channel 4(connect right sensor) right = analog(4); if(left>ref && right>ref) // if left sensor and right sensor detect white


112làÃÕ¹ÃÙá Œ ÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ

MCS-51

â»Ãá¡ÃÁ·Õè A14-1 â»Ãá¡ÃÁÀÒÉÒ C ¤Çº¤ØÁãˌ˹؋ ¹µ Robo-51 à¤Å×Íè ¹·Õãè ¹¾×¹é ·Õ·è ¡Õè Òí ˹´ã¹ÅѡɳР»§»Í§ (Ping-Pong) (¨º) { run_fd(20); // Drive forward } else if(left<ref && right>ref) // Left sensor detect black, right sensor detect white { turn_right(1000); // Drive turn right } else if(left>ref && right<ref) // Left sensor detect white, right sensor detect black { turn_left(1000); // Turn left } else if(left<ref && right<ref) // Both sensor detect black { run_bk(20); // Move backward } }

}

ÃÒÂÅÐàÍÕ´à¾ÔÁè àµÔÁ¢Í§â»Ãá¡ÃÁ ¤íÒÊÑ§è ¤Çº¤ØÁ¡ÒÃËÁعµÑǢͧËع‹ ¹µä»·Ò§´ŒÒ¹«ŒÒÂáÅТÇÒã¹â»Ãá¡ÃÁ㪌àÇÅÒ 1 ÇÔ¹Ò·Õ ¨Ð䴌ÁÁØ φ ¤‹Ò˹֧è àÁ×Íè à·Õº¡ÑºÃÙ»·Õè A16-2 ´Ù¤ÅŒÒ¡ѺNjÒàʌ¹ÊÕ´Òí ໚¹¡íÒᾧ ´Ñ§¹Ñ¹é ÁØÁ φ ¨ÐÁÒ¡ËÃ×͹ŒÍ¨֧¢Ö¹é ÍÂÙ¡‹ ºÑ ª‹Ç§ àÇÅÒ·Õ¶è ¡Ù Ë¹‹Ç§ ¼Ù¾Œ ² Ñ ¹ÒÊÒÁÒö»ÃѺᵋ§ä´ŒµÒÁ¤ÇÒÁàËÁÒÐÊÁ ¹Ñ¹è ËÁÒ¤ÇÒÁNjҶŒÒ˹‹Ç§àÇÅÒ㹡ÒÃËÁعµÑÇÁÒ¡ ¨Ð·íÒãˌÁÁØ φ ᤺ŧ ᵋ¶ÒŒ ÁÒ¡à¡Ô¹ä»ÍÒ¨·íÒãˌ˹؋ ¹µà¤Å×Íè ¹·ÕÂè ͌ ¹¡ÅѺ·Ò§à´ÔÁ䴌 ᵋ¶ÒŒ ¹ŒÍÂà¡Ô¹ä»¡çÍÒ¨·íÒãˌ˹؋ ¹µà¤Å×Íè ¹·Õ·è ÐÅØÍÍ¡¹Í¡àʌ¹Ê¹ÒÁÊÕ´Òí ä»àÅ ËÃ×ÍÍÒ¨¨Ðà¤Å×Íè ¹·Õµè çÍÂÙÀ‹ ÒÂã¹ÃÐËNjҧàʌ¹ÊÕ´Òí ᵋäÁ‹Á¡Õ ÒáÃзº¢Íºà¢µ (ËÃ×͵ÃǨ¾ºàʌ¹) ¡ç໚¹ä´Œ

φ Ro bo -5 1

ÃÙ»·Õè A14-2 áÊ´§ÁØÁ㹡ÒÃà»ÅÕÂè ¹àʌ¹·Ò§¡ÒÃà¤Å×Íè ¹·Õ¢è ͧËع‹ ¹µ Robo-51 Ping-Pong àÁ×Íè Áͧ¨Ò¡ ´ŒÒ¹º¹ Robo-51


àÃÕ¹ÃÙጠÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ MCS-51 l 113

7.4 ¡ÒÃà¤Å×Íè ¹·Õµè ÒÁàʌ¹¢Í§Ëع‹ ¹µ Robo-51 ¡ÒâѺÁÍàµÍÏà¾×Íè ¤Çº¤ØÁãˌ˹؋ ¹µ Robo-51 à¤Å×Íè ¹·Õµè ÒÁàʌ¹ ÁÕà˵ءÒóà¡Ô´¢Ö¹é ËÅÒÂ¡Ã³Õ ÍÂً¡ÑºÊÀÒ¾áǴŌÍÁáÅÐʹÒÁ ¡Ã³Õ·Õè 1 Ëع‹ ¹µ Robo-51 ÍÂÙ㋠¹á¹Çàʌ¹¢Í§Ê¹ÒÁ ¹Ñ¹è ¤×Í á¼§Ç§¨ÃµÃǨ¨Ñºàʌ¹´ŒÒ¹«ŒÒÂ͋ҹ¤‹Ò䴌ÁÒ¡¡Ç‹Ò 400 ¨Ö§ÍÂÙº‹ ÃÔàdz¾×¹é ·ÕÊè ¢Õ ÒÇ á¼§Ç§¨ÃµÃǨ¨Ñºàʌ¹´ŒÒ¹¢ÇÒ͋ҹ¤‹Ò䴌ÁÒ¡¡Ç‹Ò 400 ¨Ö§ÍÂÙº‹ ÃÔàdz¾×¹é ·ÕÊè ¢Õ ÒÇ ¡ÒäǺ¤ØÁ : Êѧè ãˌ˹؋ ¹µ Robo-51 à¤Å×Íè ¹·Õµè 絋Íä»áŌÇ˹‹Ç§àÇÅÒ㹡ÒÃà¤Å×Íè ¹ ·ÕèµÃ§à»š¹ª‹Ç§àÇÅÒÊÑé¹æ

àÁ×Íè äÁ‹¾ºàʌ¹ÊÕ´Òí

Ëع‹ ¹µà¤Å×Íè ¹·Õäè »¢ŒÒ§Ë¹ŒÒ ἧǧ¨ÃµÃǨ¨Ñº¡ÒÃÊзŒÍ¹áʧ ÍÔ¹¿ÃÒàô ·Õ¹è Òí ÁÒ㪌໚¹ ἧǧ¨ÃµÃǨ¨Ñºàʌ¹ Robo-51

¡Ã³Õ·Õè 2 Ëع‹ ¹µ Robo-51 à¤Å×Íè ¹·Õàè ÍÕ§¨Ò¡àʌ¹ä»·Ò§¢ÇÒ ¹Ñ¹è ¤×Í á¼§Ç§¨ÃµÃǨ¨Ñºàʌ¹´ŒÒ¹«ŒÒÂ͋ҹ¤‹Ò䴌¹ÍŒ Â¡Ç‹Ò 400 ¨Ö§µÃǨ¾ºàʌ¹ÊÕ´Òí ἧǧ¨ÃµÃǨ¨Ñºàʌ¹´ŒÒ¹¢ÇÒ͋ҹ¤‹Ò䴌ÁÒ¡¡Ç‹Ò 400 ¨Ö§ÍÂÙº‹ ÃÔàdz¾×¹é ·ÕÊè ¢Õ ÒÇ ¡ÒäǺ¤ØÁ : Êѧè ãˌ˹؋ ¹µàÅÕÂé Ç«ŒÒÂáŌÇ˹‹Ç§àÇÅÒ໚¹ª‹Ç§àÇÅÒÊѹé æ

Rob o-5 1

Robo-51


114làÃÕ¹ÃÙá Œ ÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ

MCS-51

¡Ã³Õ·Õè 3 Ëع‹ ¹µ Robo-51 à¤Å×Íè ¹·Õàè ÍÕ§¨Ò¡àʌ¹ä»·Ò§«ŒÒ ¹Ñ¹è ¤×Í á¼§Ç§¨ÃµÃǨ¨Ñºàʌ¹´ŒÒ¹«ŒÒÂ͋ҹ¤‹Ò䴌ÁÒ¡¡Ç‹Ò 400 ¨Ö§ÍÂÙº‹ ÃÔàdz¾×¹é ·ÕÊè ¢Õ ÒÇ á¼§Ç§¨ÃµÃǨ¨Ñºàʌ¹´ŒÒ¹¢ÇÒ͋ҹ¤‹Ò䴌¹ÍŒ Â¡Ç‹Ò 400 ¨Ö§µÃǨ¾ºàʌ¹ÊÕ´Òí ¡ÒäǺ¤ØÁ : Êѧè ãˌ˹؋ ¹µàÅÕÂé Ç¢ÇÒáŌÇ˹‹Ç§àÇÅÒ໚¹ª‹Ç§àÇÅÒÊѹé æ

-51 Robo

Robo-51

¡Ã³Õ·Õè 4 Ëع‹ ¹µ Robo-51 ¾º·Ò§á¡ ¹Ñ¹è ¤×Í á¼§Ç§¨ÃµÃǨ¨Ñºàʌ¹´ŒÒ¹«ŒÒÂ͋ҹ¤‹Ò䴌¹ÍŒ Â¡Ç‹Ò 400 ¨Ö§µÃǨ¾ºàʌ¹ÊÕ´Òí ἧǧ¨ÃµÃǨ¨Ñºàʌ¹´ŒÒ¹¢ÇÒ͋ҹ¤‹Ò䴌¹ÍŒ Â¡Ç‹Ò 400 ¨Ö§µÃǨ¾ºàʌ¹ÊÕ´Òí ¡ÒäǺ¤ØÁ : ¼Ù¾Œ ² Ñ ¹ÒÍÒ¨àÅ×Í¡ãˌ˹؋ ¹µà¤Å×Íè ¹·Õµè ç,àÅÕÂé Ç«ŒÒ ËÃ×Í¢ÇÒ µÒÁµŒÍ§¡ÒÃ

Robo-51


àÃÕ¹ÃÙጠÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ MCS-51 l 115

¡Ô¨¡ÃÃÁ·Õè 17

¢Ñºà¤Å×Íè ¹Ëع‹ ¹µ Robo-51 仵ÒÁàʌ¹ 㹡Ԩ¡ÃÃÁ¹Õ¹é Òí àʹ͡ÒäǺ¤ØÁËع‹ ¹µ Robo-51 ãˌà¤Å×Íè ¹·Õµè ÒÁàʌ¹ÊÕ´Òí «Ö§è ໚¹Ê¹ÒÁ·´Êͺ·Õ¶è ¡Ù ÊÌҧ ¢Ö¹é â´ÂËع‹ ¹µ Robo-51 ÁÕÁÍàµÍÏ俵çáÅЪش࿄ͧ¢ÑºÅŒÍ 2 ªØ´ ÊÒÂÊÑ­­Ò³¢ÑºÁÍàµÍϪ͋ §·Õè 2 ¶Ù¡µ‹Íàª×Íè Á ࢌҷÕÁè ÍàµÍÏÅ͌ ´ŒÒ¹¢ÇÒ Ê‹Ç¹ÊÒÂÊÑ­­Ò³¢ÑºÁÍàµÍϪ͋ §·Õè 1 ¶Ù¡µ‹Íàª×Íè ÁࢌҷÕÁè ÍàµÍÏÅ͌ ´ŒÒ¹«ŒÒ ʋǹ´ŒÒ¹ 㵌¢Í§Ëع‹ ¹µ Robo-51 ÁÕἧǧ¨ÃµÃǨ¨Ñº¡ÒÃÊзŒÍ¹áʧÍÔ¹¿ÃÒàô«Ö§è ¹íÒÁÒµÃǨ¨Ñºàʌ¹µÔ´µÑ§é änj 2 µÑÇ â´Â·Õ ἧǧ¨ÃµÃǨ¨Ñºàʌ¹·Ò§«ŒÒµ‹ÍࢌҡѺÍÔ¹¾ØµÍйÒÅÍ¡ª‹Í§ AN5 ἧǧ¨ÃµÃǨ¨Ñºàʌ¹·Ò§¢ÇÒµ‹ÍࢌҡѺÍÔ¹¾ØµÍйÒÅÍ¡ª‹Í§ AN4 A17.1 ແ´â»Ãá¡ÃÁ RIDE áŌÇà¢Õ¹â»Ãá¡ÃÁ·Õè A17-1 ºÑ¹·Ö¡ã¹ª×Íè ä¿Å act1501.c ·íÒ¡ÒäÍÁä¾Å áŌǴÒǹ âËÅ´ä¿ÅÊáً ¼§Ç§¨Ã RBX-51AC2 ¢Í§Ëع‹ ¹µ Robo-51 ´ŒÇÂâ»Ãá¡ÃÁ Flip A17.2 ¹íÒËع‹ ¹µä»ÇÒ§¤Ã‹ÍÁàʌ¹ ¨Ò¡¹Ñ¹é ¡´ÊÇÔµªà¾×Íè ãˌ˹؋ ¹µ Robo-51 ·íÒ§Ò¹ Êѧࡵ¡ÒÃà¤Å×Íè ¹·Õ·è àÕè ¡Ô´¢Ö¹é Ç‹Ò à»š¹ä»µÒÁ·Õµè ͌ §¡ÒÃËÃ×ÍäÁ‹


116làÃÕ¹ÃÙá Œ ÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ

MCS-51

â»Ãá¡ÃÁ·Õè A17-1 â»Ãá¡ÃÁÀÒÉÒ C ¤Çº¤ØÁãˌ˹؋ ¹µ Robo-51 à¤Å×Íè ¹·Õµè ÒÁàʌ¹ÊÕ´Òí ẺàÅÕÂé Ç«ŒÒ àÁ×Íè ¾º·Ò§á¡ (Áյ͋ ) /*——————————————————————————————————————————————————————————————————————————*/ // Program : Tracking line robot V1 // Description : Drive robot to tracking the black line by dual IR reflect sensor // Filename : act1701.h // C compiler : RIDE 51 V6.1 /*——————————————————————————————————————————————————————————————————————————*/ #include <C51ac2.h> // Header include register of T89C51AC2 #include <dc_motor.h> // Module function drive DC motor #include <sound.h> // Module function drive sound #include <delay.h> // Module function delay process #include <adc_robo51.h> // Module function read input analog #define pow 250 // Define constant power drive robot #define ref 400 // Define constant reference compare track line int left=0,right=0; // Define variable for keep input analog value /***************** Function drive robot forward ******************************/ void run_fd(int time_spin) { motor_fd(2,pow); // Drive left motor forward motor_fd(1,pow); // Drive right motor forward delay_ms(time_spin); // Delay for robot spin } /***************** Function drive robot backward ******************************/ void run_bk(int time_spin) { motor_bk(2,pow); // Drive left motor backward motor_bk(1,pow); // Drive right motor backward delay_ms(time_spin); // Delay for robot spin } /***************** Function drive robot turn left ****************************/ void turn_left(int time_spin) { motor_fd(2,pow); // Drive left motor forward motor_bk(1,pow); // Drive right motor backward delay_ms(time_spin); // Delay for robot spin } /***************** Function drive robot turn right ***************************/ void turn_right(int time_spin) { motor_bk(2,pow); // Drive left motor backward motor_fd(1,pow); // Drive right motor forward delay_ms(time_spin); // Delay for robot spin } /****************************** Main function ********************************/ void main() { beep(); // Sound beep at once while(1) // Infinite loop { left = analog(5); // Read input analog channel 5(connect left sensor) right = analog(4); // Read input analog channel 4(connect right sensor)


àÃÕ¹ÃÙጠÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ MCS-51 l 117

â»Ãá¡ÃÁ·Õè A15-1 â»Ãá¡ÃÁÀÒÉÒ C ¤Çº¤ØÁãˌ˹؋ ¹µ Robo-51 à¤Å×Íè ¹·Õµè ÒÁàʌ¹ÊÕ´Òí ẺàÅÕÂé Ç«ŒÒ àÁ×Íè ¾º·Ò§á¡ (Áյ͋ ) if(left>ref && right>ref) // Both sensors detect white { run_fd(20); // Move forward } else if(left<ref && right>ref) // Left sensor detects black and // Right sensor detects white { turn_left(20); // Turn left } else if(left>ref && right<ref) // Left sensor detects white and // Right sensor detects black { turn_right(20); // Turn right } else if(left<ref && right<ref) // Both sensors detect black { run_fd(20); // Move forward turn_left(20); // Turn left } }

}

ÃÒÂÅÐàÍÕ´à¾ÔÁè àµÔÁ¢Í§â»Ãá¡ÃÁ ¡Ò÷íÒ§Ò¹¢Í§â»Ãá¡ÃÁË؋¹Â¹µà¤Å×è͹·ÕèµÒÁàʌ¹ÍÒÈÑ¡ÒõÃǨÊͺ»ÃÔÁÒ³¡ÒÃÃѺáʧÊзŒÍ¹¢Í§ ἧǧ¨ÃµÃǨ¨Ñºàʌ¹·Ñ§é «ŒÒÂáÅТÇÒ ¤‹Ò·ÕÍè ҋ ¹ä´Œ¨Ò¡¨Ð¶Ù¡¹íÒä»à»ÃÕºà·Õº¡Ñºà§×Íè ¹ä¢áÅеդÇÒÁà¾×Íè ãˌ䴌 ¼ÅÅѾ¸ä»¤Çº¤ØÁ¡ÒâѺà¤Å×Íè ¹Ëع‹ ¹µÍ¡Õ ¤Ãѧé ˹֧è â´Â¡‹Í¹Ë¹ŒÒ¹Õäé ´ŒÍ¸ÔºÒµÑÇÍ‹ҧ¡ÒûÃÐÁÒ³¤‹Ò͌ҧÍÔ§ ¢Í§àʌ¹ÊÕ´Òí ෋ҡѺ 400 (¤‹Ò¹ÕÍé Ò¨à»ÅÕÂè ¹á»Å§ä´Œã¹ÊÀÒ¾áǴŌÍÁ·Õáè µ¡µ‹Ò§¡Ñ¹ ÃÇÁ¶Ö§ÃٻẺ¡ÒõԴµÑ§é ἧ ǧ¨ÃµÃǨ¨Ñºàʌ¹´ŒÇÂ) ´Ñ§¹Ñ¹é ã¹â»Ãá¡ÃÁ¨Ö§¡íÒ˹´¤‹Ò͌ҧÍԧ㹡ÒÃà»ÃÕºà·Õºà¾×Íè á¡ÃÐËNjҧàʌ¹ÊÕ´Òí áÅÐ ¾×¹é ÊÕ¢ÒÇä»ÂѧµÑÇá»Ã ref ãˌà·Õº෋ҡѺ 400 ¨Ò¡¡ÒûÃСÒÈ #define ref 400 àÃÔÁè µŒ¹¡Ò÷íÒ§Ò¹¢Í§â»Ãá¡ÃÁ¨ÐÁÕÊ­ Ñ ­Ò³àÊÕÂ§Ë¹Ö§è ¤Ãѧé ËÅѧ¨Ò¡¹Ñ¹é ࢌÒÊÙ¡‹ ÒÃǹµÃǨÊͺà§×Íè ¹ä¢áÅÐ µÕ¤ÇÒÁ¨Ò¡¡ÒÃ͋ҹ¤‹ÒÍйÒÅÍ¡¢Í§á¼§Ç§¨ÃµÃǨ¨Ñºàʌ¹´ŒÒ¹«ŒÒÂáÅТÇÒÀÒÂ㵌à§×Íè ¹ä¢¢Í§ while(1){ } à¾×Íè µÃǨÊÍºÇ‹Ò á¼§Ç§¨ÃµÃǨ¨Ñºáµ‹ÅеÑǾºàʌ¹ÊÕ´Òí ËÃ×;׹é ÊÕ¢ÒÇ â´Âẋ§à§×Íè ¹ä¢´Ñ§¹Õé l

ἧǧ¨ÃµÃǨ¨Ñºàʌ¹µÑÇã´Í‹Ò¹¤‹Ò䴌¹ÍŒ Â¡Ç‹Ò 400 ¶×ÍÇ‹Ò á¼§Ç§¨ÃµÃǨ¨Ñº¹Ñ¹é ¾ºàʌ¹ÊÕ´Òí

l

ἧǧ¨ÃµÃǨ¨Ñºàʌ¹µÑÇã´Í‹Ò¹¤‹Ò䴌ÁÒ¡¡Ç‹Ò 400 ¶×ÍÇ‹Ò á¼§Ç§¨ÃµÃǨ¨Ñº¹Ñ¹é ¾º¾×¹é ÊÕ¢ÒÇ

ÊÒÁÒö͋ҹ¤‹Ò¨Ò¡á¼§Ç§¨ÃµÃǨ¨Ñºàʌ¹´ŒÒ¹«ŒÒ·յè ͋ ¡Ñºª‹Í§ AN5 áÅÐἧǧ¨ÃµÃǨ¨Ñºàʌ¹´ŒÒ¹¢ÇÒ ·Õµè ͋ ¡Ñºª‹Í§ AN4) ´ŒÇ¤íÒÊѧè left = analog(5); // ͋ҹ¤‹Ò¨Ò¡á¼§Ç§¨ÃµÃǨ¨Ñºàʌ¹´ŒÒ¹«ŒÒÂà¡çºänj·Õè left right = analog(4); // ͋ҹ¤‹Ò¨Ò¡á¼§Ç§¨ÃµÃǨ¨Ñºàʌ¹´ŒÒ¹¢ÇÒà¡çºänj·Õè right ´Ñ§¹Ñ¹é à§×Íè ¹ä¢¡ÒõÃǨÊͺ¢Í§â»Ãá¡ÃÁ¹Õ¨é §Ö ẋ§à»š¹ 4 ¡Ã³Õ ´Ñ§¹Õé


118làÃÕ¹ÃÙá Œ ÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ

MCS-51

â»Ãá¡ÃÁ·Õè A17-1 â»Ãá¡ÃÁÀÒÉÒ C ¤Çº¤ØÁãˌ˹؋ ¹µ Robo-51 à¤Å×Íè ¹·Õµè ÒÁàʌ¹ÊÕ´Òí ẺàÅÕÂé Ç«ŒÒ àÁ×Íè ¾º·Ò§á¡ (¨º) if(left>ref && right>ref)

// ¶ŒÒἧǧ¨ÃµÃǨ¨Ñºàʌ¹¾º¾×¹é ¢ÒÇ·Ñ§é ¤Ù‹

{ run_fd(20); Robo-51

// à¤Å×Íè ¹·Õµè ç仢ŒÒ§Ë¹ŒÒ 20 ÁÔÅÅÔÇ¹Ô Ò·Õ

}

else if(left<ref && right>ref)

// ¶ŒÒἧǧ¨ÃµÃǨ¨Ñºàʌ¹´ŒÒ¹«ŒÒ¾ºàʌ¹ÊÕ´Òí ᵋἧǧ¨ÃµÃǨ¨Ñºàʌ¹´ŒÒ¹¢ÇÒ¾º¾×¹é { turn_left(20);

Rob o-5 1

// àÅÕÂé Ç«ŒÒ 20 ÁÔÅÅÔÇ¹Ô Ò·Õ // à¾×Íè »ÃѺãˌἧǧ¨ÃµÃǨ¨Ñºàʌ¹·Ñ§é Êͧ¤Ã‹ÍÁàʌ¹ÊÕ´Òí

} else if(left>ref && right<ref)

// ἧǧ¨ÃµÃǨ¨Ñºàʌ¹´ŒÒ¹«ŒÒ¾º¾×¹é ¢ÒÇ áµ‹á¼§Ç§¨ÃµÃǨ¨Ñºàʌ¹´ŒÒ¹¢ÇÒ¾ºàʌ¹ÊÕ´Òí { turn_right(20); 51 Robo-

// àÅÕÂé Ç¢ÇÒ 20 ÁÔÅÅÔÇ¹Ô Ò·Õ // à¾×Íè »ÃѺãˌἧǧ¨ÃµÃǨ¨Ñºàʌ¹·Ñ§é Êͧ¤Ã‹ÍÁàʌ¹ÊÕ´Òí

} else if(left<ref && right<ref)

// ¶ŒÒἧǧ¨ÃµÃǨ¨Ñºàʌ¹·Ñ§é Êͧ¾º·Ò§á¡ËÃ×;ºàʌ¹ÊÕ´Òí ·Ñ§é ¤Ù‹ { run_fd(20); Robo-51

turn_left(20);

// à¤Å×Íè ¹·Õµè ç仢ŒÒ§Ë¹ŒÒàÅ硹ŒÍ (20 ÁÔÅÅÔÇ¹Ô Ò·Õ) // àÅÕÂé Ç«ŒÒÂàÁ×Íè ¾º·Ò§á¡

}

Robo-51


àÃÕ¹ÃÙጠÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ MCS-51 l 119

º··Õè 8 ¤Çº¤ØÁËع‹ ¹µ Robo-51 ´ŒÇÂÃÕâÁµ¤Í¹â·ÃÅÍÔ¹¿ÃÒàô Ëع‹ ¹µ Robo-51 ÊÒÁÒö·Õ¨è зíҧҹ䴌´ÇŒ µÑÇàͧ â´ÂÍÒÈÑÂἧǧ¨ÃµÃǨ¨Ñºáººµ‹Ò§æ ʋ§ ¢ŒÍÁÙÅÁÒãˌäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏËÅÑ¡µÑ´ÊԹ㨠¹Í¡¨Ò¡¹Ñ¹é ÂѧÊÒÁÒöÃѺ¢ŒÍÁÙÅà¾×Íè ¤Çº¤ØÁẺàÇÅÒ ¨ÃÔ§¨Ò¡ÀÒ¹͡´ŒÇÂÍØ»¡Ã³¤Çº¤ØÁ¨Ò¡ÃÐÂÐä¡Å¼‹Ò¹áʧÍÔ¹¿ÃÒàô䴌 㹺·¹Õé¹íÒàʹ͡Òà »ÃÐÂØ¡µË¹Ø‹ ¹µ Robo-51 ãˌÊÒÁÒö·íҧҹËÇÁ¡ÑºÍØ»¡Ã³Ê§‹ ÊÑ­­Ò³¤Çº¤ØÁÃÐÂÐä¡ÅËÃ×ÍÃÕâÁµ ¤Í¹â·ÃÅ䴌

8.1 ER-4 Easy Remote4 : ÃÕâÁµ¤Í¹â·ÃÅÍÔ¹¿ÃÒàô 4 ª‹Í§ ໚¹ÃÕâÁµ¤Í¹â·ÃÅẺÍÔ¹¿ÃÒàô·ÕèÍ͡Ẻãˌ㪌§Ò¹ä´Œà͹¡»ÃÐʧ¤ â´Â㹡ÒÃʋ§ ÊÑ­­Ò³ÍÍ¡¹Ñ¹é ¨Ð໚¹¢ŒÍÁÙÅ͹ءÃÁ·Õ¼è ÊÁ¡Ñº¤Å×¹è ¾Òˏ 38kHz â´Â¡Ãкǹ¡Ò÷ѧé ËÁ´ä´ŒÃºÑ ¡Òà ¤Çº¤ØÁ¨Ò¡äÁâ¤Ã¤Í¹â·ÃÅàÅÍϺ¹µÑÇÃÕâÁµ¤Í¹â·ÃÅà¾Õ§µÑÇà´ÕÂÇ ¤Ø³ÊÁºÑµÔ·Ò§à·¤¹Ô¤

㪌äÁâ¤Ã¤Í¹â·ÃÅàÅÍϤǺ¤ØÁ¡ÒÃÊÌҧáÅÐʋ§ÊÑ­­Ò³¢ŒÍÁÙÅ l 㪌 LED ÍÔ¹¿ÃÒàô 3 ÁÔÅÅÔàÁµÃ ÃÐÂзíÒ¡Òà 8-10 àÁµÃã¹¾×¹ é ·Õâè ŋ§ l ÁÕÊÇÔµª¤Çº¤ØÁÍÔÊÃÐ 4 ª‹Í§ l ¡Ô¹¡íÒÅѧ§Ò¹µèÒí â´ÂࢌÒÊÙ⋠ËÁ´»ÃÐËÂÑ´¾Åѧ§Ò¹·Ñ¹·ÕàÁ×Í è ʋ§¢ŒÍÁÙÅàÊÃç¨ l 㪌ä¿àÅÕÂ é §ã¹Â‹Ò¹ 2.4 ¶Ö§ 3.0V ¨Ò¡áºµàµÍÃÕè AA 2 ¡ŒÍ¹ 㪌䴌·§Ñé Ẻ ¸ÃÃÁ´ÒáÅÐẺ»ÃШØ䴌 l ʋ§¢ŒÍÁÙÅÍ͡Ẻ͹ءÃÁ´ŒÇÂÁҵðҹ RS-232 ÍѵÃҺʹ 1,200 ºÔµ µ‹ÍÇÔ¹Ò·Õ ã¹ÃٻẺ 8N1 (ºÔµ¢ŒÍÁÙÅ 8 ºÔµ äÁ‹Á¡Õ ÒõÃǨÊͺ¾ÒÃÔµÕé áÅÐ ºÔµ»´·ŒÒ 1 ºÔµ) l

ÃÙ»·Õè 8-1 ˹ŒÒµÒáÅФسÊÁºÑµ¢Ô ͧ ER-4 ÃÕâÁµ¤Í¹â·ÃÅÍÔ¹¿ÃÒàô


120làÃÕ¹ÃÙá Œ ÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ

MCS-51

+3-5V

7

SW1 "B"

GP0

V,, GP5

6

SW2 "C"

5

"D"

3

SW3

SW3

K A IR-LED

2

GP1

IC1 ER-04FW

R1 1k

Easy remote4

"A"

LED1 IR-LED

1

Q1 BC338

GP2

GP4

2-5V

, +

GND 8

ÃÙ»·Õè 8-2 ǧ¨ÃÊÁºÙó¢Í§ÃÕâÁµ¤Í¹â·ÃÅ ER-4

8.1.1 ¡Ò÷íÒ§Ò¹¢Í§Ç§¨Ã

* )

ÃÙ»·Õè 8-3 áÊ´§¢ŒÍÁÙŵÑÇÍÑ¡Éâͧ ÊÇÔµªáµ‹ÅеÑǺ¹ÃÕâÁµ¤Í¹â·ÃÅ

ǧ¨Ã¢Í§ÃÕâÁµ¤Í¹â·ÃÅ ER-4 áÊ´§ã¹ÃÙ»·Õè 8-2 ÁÕÊÇÔµª 4 µÑÇ·Õµè ͋ ࢌҡѺ¢Ò GP0, GP1, GP2 áÅÐ GP4 ¢Í§ IC1 «Ö§è 㪌äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏàºÍÏ PIC12F629 â´ÂäÁ‹¨Òí ໚¹µŒÍ§µ‹ÍµÑǵŒÒ¹·Ò¹ ¾ÙÅÍÑ»ÀÒ¹͡à¹×èͧ¨Ò¡ÊÒÁÒö¡íÒ˹´ãˌÁÕ¡ÒþÙÅÍѻ䴌¼‹Ò¹·Ò§«Í¿µáÇÏ ʋǹ¡ÒâѺ LED ÍÔ¹¿ÃÒàô (LED1) à¾×Íè ãˌÁ¡Õ Òí Åѧʋ§ÊÙ§ ÊÒÁÒöʋ§ä´Œä¡Å ¨Ö§µ‹Í¼‹Ò¹Ç§¨Ã ¢ÂÒÂâ´Â㪌·ÃÒ¹«ÔÊàµÍÏ Q1

8.1.2 ÃٻẺ¡ÒÃʋ§¢ŒÍÁÙŢͧÃÕâÁµ¤Í¹â·ÃÅ ER-4 à¾×Íè ãˌ·Ò§ÀÒ¤ÃѺÊÒÁÒö͋ҹ¤‹Ò¢ŒÍÁÙÅ¡Òá´ÊÇÔµª¨Ò¡ÃÕâÁµ¤Í¹â·ÃÅ䴌â´Â§‹Ò 㹡ÒÃʋ§ ¢ŒÍÁÙŢͧÃÕâÁµ¤Í¹â·ÃÅ ER-4 ¨Ö§ãªŒ¡ÒÃʋ§¢ŒÍÁÙÅẺ͹ءÃÁ µÒÁÁҵðҹ RS-232 ´ŒÇÂÍѵÃҺʹ 1,200 ºÔµµ‹ÍÇÔ¹Ò·Õ ã¹ÃٻẺ 8N1 ¤‹Ò¢ŒÍÁÙÅ·ÕÊè §‹ ¹Ñ¹é ໚¹µÑÇÍÑ¡Éâֹé ÍÂÙ¡‹ ºÑ ¡Òá´ÊÇÔµªµÇÑ ã´ ´Ñ§áÊ´§ µíÒá˹‹§¢Í§ÊÇÔµªã¹ÃÙ»·Õè 7-3 l

¡´ÊÇÔµª A ʋ§¢ŒÍÁÙŵÑÇÍÑ¡Éà A µÑǾÔÁ¾ãË­‹ÊÅѺµÑǾÔÁ¾àÅç¡

l

¡´ÊÇÔµª B ʋ§¢ŒÍÁÙŵÑÇÍÑ¡Éà B µÑǾÔÁ¾ãË­‹ÊÅѺµÑǾÔÁ¾àÅç¡

l

¡´ÊÇÔµª C ʋ§¢ŒÍÁÙŵÑÇÍÑ¡Éà C µÑǾÔÁ¾ãË­‹ÊÅѺµÑǾÔÁ¾àÅç¡

l

¡´ÊÇÔµª D ʋ§¢ŒÍÁÙŵÑÇÍÑ¡Éà D µÑǾÔÁ¾ãË­‹ÊÅѺµÑǾÔÁ¾àÅç¡


àÃÕ¹ÃÙጠÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ MCS-51 l 121

0.1µF 50V 1

+ S

3

2

Infrared Module  Out GND



!

! Vcc

ÃÙ»·Õè 7-4 áÊ´§ÅѡɳТͧâÁ´ÙÅÃѺáʧÍÔ¹¿Òàô, µíÒá˹‹§¢Ò áÅÐǧ¨Ã¢Í§á¼§Ç§¨ÃâÁ´ÙÅÃѺáʧ ÍÔ¹¿ÃÒàô 38kHz ÊÒà˵ط¡Õè ÒÃʋ§ÊÅѺµÑǾÔÁ¾ãË­‹¡ºÑ µÑǾÔÁ¾àÅç¡à¾×Íè ãˌµÇÑ ÃѺ·ÃÒºÇ‹Ò ¡Òá´ÊÇÔµª¹¹Ñé ໚¹¡Òà ¡´áºº¤ŒÒ§µ‹Íà¹×èͧ ËÃ×Í໚¹¡ÒᴻŋÍÂ໚¹¨Ñ§ËÇÐ «Ö觨Ðãˌ¼ÅÅѾ¸ã¹¡ÒäǺ¤ØÁ·Õèᵡµ‹Ò§¡Ñ¹ ¹Ñ¹è ¤×ͶŒÒ¡´ÊÇÔµªà»š¹¨Ñ§ËÇÐ ¤Ãѧé áá¨Ðʋ§¢ŒÍÁÙŵÑÇÍѡɳẺµÑǾÔÁ¾ãË­‹ ¶ŒÒ¡´µÑÇà´ÔÁ㹤ÃÑ§é µ‹Í ÁÒ ¢ŒÍÁÙÅ·ÕÊè §‹ ÍÍ¡¨Ð໚¹µÑÇÍÑ¡ÉÃẺµÑǾÔÁ¾àÅç¡ áµ‹¶ÒŒ ËÒ¡¡´¤ŒÒ§µ‹Íà¹×Íè § ¨Ðʋ§¢ŒÍÁÙŵÑÇÍÑ¡ÉÃẺ ŋÒÊØ´¹Ñé¹ÍÍ¡ÁÒµÅÍ´àÇÅÒ

8.2 ἧǧ¨ÃâÁ´ÙÅÃѺáʧÍÔ¹¿ÃÒàô 38kHz ÁÕǧ¨ÃáÊ´§ã¹ÃÙ»·Õè 8-4 ËÅÑ¡¡Ò÷íÒ§Ò¹¤×Í àÁ×èͨ‹ÒÂä¿ãˌᡋǧ¨ÃáÅŒÇ ÂѧäÁ‹ÁÕ¡ÒÃʋ§áʧ ÍÔ¹¿ÃÒàô·Õ¼è ÊÁÊÑ­­Ò³¾Òˏ 38kHz àÍÒµ¾µØ ¢Í§Ç§¨Ã¨Ð໚¹Åͨԡ “1” µÅÍ´àÇÅÒ ¨¹¡Ãзѧè àÁ×Íè ÁÕ¡ÒÃʋ§áʧÍÔ¹¿ÃÒàô·ÕèÁÕÊÑ­­Ò³¾Òˏ 38kHz ¼ÊÁÍÂً´ŒÇ âÁ´ÙÅÃѺáʧÍÔ¹¿ÃÒàô¨Ð·íÒ§Ò¹ ãˌ àÍÒµ¾µØ ໚¹Åͨԡ “0”

8.3 ÊÌҧäźÃÒÃÕä¿ÅÊÒí ËÃѺÃѺ¢ŒÍÁÙŨҡÃÕâÁµ¤Í¹â·ÃÅ ER-4 à¾×Íè ãˌÊÒÁÒöµÔ´µ‹Í¡ÑºÃÕâÁµ¤Í¹â·ÃÅ ER-4 ´ŒÇÂâ»Ãá¡ÃÁÀÒÉÒ C ¢Í§Ëع‹ ¹µ Robo-51 䴌 §‹ÒÂ¢Ö¹é ¨Ö§¤ÇÃÊÌҧä¿ÅäźÃÒÃնʹÃËÑʤíÒÊÑ§è ¨Ò¡ÃÕâÁµ¤Í¹â·ÃÅ ER-4 â´ÂµÑ§é ª×Íè ໚¹ remote.h à¾×Íè ¹íÒä»»ÃÐÂØ¡µãªŒà¢Õ¹â»Ãá¡ÃÁãˌ˹؋ ¹µ Robo-51 ãˌÊÒÁÒöÃѺ¤íÒÊÑ§è ¨Ò¡ÃÕâÁµ¤Í¹â·ÃÅ ER-4 à¾×Íè ·íÒ§Ò¹µ‹Íä»

8.3.1 ËÅÑ¡¡ÒÃÃѺÊÑ­­Ò³¨Ò¡ÃÕâÁµ¤Í¹â·ÃÅ ER-4 ¡ÒÃÃѺÊÑ­­Ò³¨Ò¡ÃÕâÁµ¤Í¹â·ÃÅ ER-4 ¨ÐµŒÍ§µÔ´µÑé§á¼§Ç§¨ÃâÁ´ÙÅÃѺáʧÍÔ¹¿ÃÒàô 38kHz «Ö§è ãˌàÍÒµ¾µØ ໚¹ÊÑ­­Ò³´Ô¨µÔ ÍÅ â´Âµ‹ÍࢌҡѺª‹Í§ P3.2 ¢Í§äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏËÅÑ¡¢Í§


122làÃÕ¹ÃÙá Œ ÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ

MCS-51

Ë؋¹Â¹µ Robo-51 «Öè§à»š¹ÍÔ¹¾ØµÍÔ¹àµÍÏÃÑ»µ¨Ò¡ÊÑ­­Ò³ÀÒ¹͡¢Í§äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ T89C51AC2 ´ŒÇ ·Õè¾Íϵ¹ÕéàÁ×è͵ÃǨ¨ÑºÊÑ­­Ò³ÍÔ¹¿ÃÒàô䴌 â´Â¨Ðà¡Ô´¡ÒÃÍÔ¹àµÍÏÃÑ»µàÁ×è;º ÊÑ­­Ò³¢Íº¢Òŧ·Õ¢è Ò¾Íϵ¹Õé «Ö§è µÃ§¡ÑºÊÑ­­Ò³àÃÔÁè µŒ¹¡ÒÃʋ§¢ŒÍÁÙÅËÃ×ͺԵàÃÔÁè µŒ¹ (start bit) ¹Ñ¹è àͧ áŌÇÍÒÈÑ¡ÒÃ˹‹Ç§àÇÅÒà¾×èÍãˌ䴌ÃٻẺµÃ§¡Ñº¡ÒÃÃѺ¢ŒÍÁÙÅ͹ءÃÁµÒÁÁҵðҹ RS-232 ¨¹¤Ãº 8 ºÔµ «Ö§è ¡Ãкǹ¡ÒöʹÃËÑÊ¢ŒÍÁÙŷѧé 8 ºÔµ«Ö§è ໚¹¤íÒÊÑ§è ·ÕÊè §‹ ÁÒ¹Õ¨é Ðà¡Ô´¢Ö¹é ÀÒÂ㹿˜§¡ª¹Ñè ¡ ÒõͺʹͧÍÔ¹àµÍÃÃ»Ñ µ¨Ò¡ÀÒ¹͡ª‹Í§ 0 (External interrupt 0 ËÃ×Í INT0) ¨Ò¡ËÅÑ¡¡Ò÷ѧé ËÁ´ÊÒÁÒö¹íÒÁÒÊÌҧ¿˜§¡ª¹Ñè à¾×Íè ¨Ñ´à¡çºà»š¹ä¿ÅäźÃÒÃÕ remote.h à¾×Íè àÃÕ¡ 㪌µÍ‹ ä» â´ÂÃÒÂÅÐàÍÕ´¢Í§ remote.h áÊ´§ã¹â»Ãá¡ÃÁ·Õè 8-1

8.3.2 ¤íÒ͸ԺÒ¿˜§¡ª¹Ñè ã¹ remote.h (1) ¿˜§¡ª¹Ñè get_remote : ¶Í´ÃËÑÊ¢ŒÍÁÙÅ·ÕÃè ºÑ ä´Œ¨Ò¡ÃÕâÁµ¤Í¹â·ÃÅ ER-4 ÃٻẺ¿˜§¡ªÑè¹ unsigned char get_remote(void) ¾ÒÃÒÁÔàµÍÏ äÁ‹ÁÕ ¡Òä׹¤‹Ò ¤×¹¤‹Ò¢ŒÍÁÙÅ໚¹¤íÒÊÑ§è ·Õäè ´Œ¨Ò¡¡ÒöʹÃËÑʢͧÃÕâÁµ¤Í¹â·ÃÅ ER-4 ã¹ÃٻẺ µÑÇÍÑ¡Éà “a”,”A”,”b”,”B”,”c”,”C”,”d”,”D” «Ö§è ¢Ö¹é ÍÂÙ¡‹ ºÑ ¡ÒÃàÅ×Í¡¡´»ØÁ† ¨Ò¡µÑÇʋ§ ÃÒÂÅÐàÍÕ´ ໚¹¿˜§¡ª¹Ñè ·ÕÊè §‹ ¤‹Ò¢ŒÍÁÙŢͧµÑÇá»Ã ir_command «Ö§è à¡çº¤‹Ò¤íÒÊÑ§è ¨Ò¡¡Òà ¶Í´ÃËÑʢͧÃÕâÁµ¤Í¹â·ÃÅ ER-4 (2) ¿˜§¡ª¹Ñè clear_remote : à¤ÅÕÂϤҋ à¾×Íè àÃÔÁè µŒ¹·íÒ§Ò¹ ÃٻẺ¿˜§¡ªÑè¹

void clear_remote(void)

¾ÒÃÒÁÔàµÍÏ

äÁ‹ÁÕ

¡Òä׹¤‹Ò

äÁ‹Á¡Õ Òä׹¤‹Ò

ÃÒÂÅÐàÍÕ´ à¤ÅÕÂϤҋ à¾×Íè ãˌàÃÔÁè µŒ¹

໚¹¿˜§¡ª¹Ñè à¤ÅÕÂϤҋ ¢ŒÍÁÙŢͧµÑÇá»Ã ir_command ʋǹãË­‹àÃÕ¡㪌§Ò¹à¾×Íè

ËÁÒÂàËµØ ã¹¡ÒÃàÃÕ¡㪌§Ò¹¿˜§¡ªÑè¹ã¹äźÃÒÃÕ remote.h ¹Ñ鹨еŒÍ§µÔ´µÑé§áÅе‹Íἧǧ¨ÃâÁ´ÙÅÃѺáʧ ÍÔ¹¿ÃÒàô 38kHz ࢌҷժè ͋ § P3.2


àÃÕ¹ÃÙጠÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ MCS-51 l 123

â»Ãá¡ÃÁ·Õè 8-1 ä¿ÅäźÃÒÃÕ remote.h ÊíÒËÃѺ¶Í´ÃËÑÊ¢ŒÍÁÙŨҡÃÕâÁµ¤Í¹â·ÃÅÍÔ¹¿ÃÒàô ER-4 /*———————————————————————————————————————————————————————————————————*/ // Program : Decode command from Easy remote // Description : Receive command from Easy remote to control the robot // Filename : remote.h // C compiler : RIDE 51 V6.1 /*———————————————————————————————————————————————————————————————————*/ sbit irm_data = P3^2; // Define port input remote sensor unsigned char ir_command=0; // Define for buffer command from Easy remote /****************** Function Delay for baudrate 1200 *************************/ void delay_baud1200() // Delay baudrate 1200 bps { unsigned int x,a; // Keep for counter loop for(x=0;x<1;x++) { for(a=0;a<731;a++); // Loop for delay 833 us } } /****************** Function Delay for 1200 baudrate (for start bit) **********/ void start_1200() // Delay baudrate 1200 while start bit { unsigned int x,a; // Keep for counter loop for(x=0;x<1;x++) { for(a=0;a<365;a++); // Loop for delay 416 us } } /************* Service interrupt routine for receive command ******************/ void service_ex0(void) interrupt 0 { unsigned char i; // Define for counter loop if(irm_data==0) // Check start bit true? { start_1200(); // Delay for start bit for(i=0;i<8;i++) // For loop count 8 time(for receive data 8 bit) { delay_baud1200(); // Delay for data 1 bit ir_command = ir_command>>1; // Shift data bit to right 1 time if(irm_data) // If data bit = high ir_command = ir_command | 0x80; // Config data bit = “1” } delay_baud1200(); // Delay for stop bit } } /************** Function read Command from Easy remote ***********************/ unsigned char get_remote(void) { return(ir_command); // Return command } /*********************** Function clear command ******************************/ void clear_remote() { ir_command = 0; // Clear command } /*********************** Function initial for Easy remote ********************/ void remote_init(void) { irm_data = 1; // Configuration input P3.2 EX0 = 1; // Enable External interrupt0 IT0 = 1; // Detect falling edge EA = 1; // Enable interrupt all }


124làÃÕ¹ÃÙá Œ ÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ

MCS-51

¡Ô¨¡ÃÃÁ·Õè 18

·´ÊͺÃѺ¢ŒÍÁÙÅÃÕâÁµ¤Í¹â·ÃÅ ER-4 A18.1 ແ´â»Ãá¡ÃÁ RIDE áŌÇà¢Õ¹â»Ãá¡ÃÁ·Õè A18-1 ºÑ¹·Ö¡ã¹ª×Íè ä¿Å act1801.c ·íÒ¡ÒäÍÁä¾Å áŌǴÒǹ âËÅ´ä¿ÅÊáً ¼§Ç§¨Ã RBX-51AC2 ¢Í§Ëع‹ ¹µ Robo-51 ´ŒÇÂâ»Ãá¡ÃÁ Flip A18.2 ¹íÒẵàµÍÃÕè AA ¨íҹǹ 2 ¡ŒÍ¹ãʋãˌᡋÃâÕ Áµ¤Í¹â·ÃÅ ER4 A18.3 Ãѹâ»Ãá¡ÃÁ ·´Åͧ¡´»ØÁ† µ‹Ò§æ º¹ÃÕâÁµ¤Í¹â·ÃÅ ER-4 Êѧࡵ¼ÅÅѾ¸·àÕè ¡Ô´¢Ö¹é

â»Ãá¡ÃÁ·Õè A18-1 â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺ·´Êͺ¡ÒõԴµ‹Í¡ÑºÃÕâÁµ¤Í¹â·ÃÅ ER-4 ¢Í§Ëع‹ ¹µ Robo-51 (Áյ͋ ) /*————————————————————————————————————————————————————————————————————————*/ // Program : Test Infrared receiving // Description : Control LED at P3.5 P3.6, P3.7 and sound by ER-4 Remote control // Filename : act1801.c // C compiler : RIDE 51 V6.1 /*————————————————————————————————————————————————————————————————————————*/ #include <C51ac2.h> // Header include register of P89C51AC2 #include <delay.h> // Module function Delay #include <remote.h> // Module function ER-4 Remote control #include <sound.h> // Module function Sound sbit led1 = P3^5; // Define LED on P3.5 port sbit led2 = P3^6; // Define LED on P3.6 port sbit led3 = P3^7; // Define LED on P3.7 port void main() { remote_init(); // Initialize ER-4 remote control while(1) // Looping { switch(get_remote()) // Check command from ER-4 remote control { case ‘a’ : led1 = 0; // LED P3.5 on clear_remote(); // Clear command break; // Out of from case case ‘A’ : led1 = 1; // LED P3.5 off clear_remote(); // Clear command break; // Out of from case case ‘b’ : led2 = 0; // LED P3.6 on clear_remote(); // Clear command break; // Out of from case case ‘B’ : led2 = 1; // LED P3.6 off clear_remote(); // Clear command break; // Out of from case case ‘c’ : led3 = 0; // LED P3.7 on clear_remote(); // Clear command break; // Out of from case


àÃÕ¹ÃÙጠÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ MCS-51 l 125

â»Ãá¡ÃÁ·Õè A18-1 â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺ·´Êͺ¡ÒõԴµ‹Í¡ÑºÃÕâÁµ¤Í¹â·ÃÅ ER-4 ¢Í§Ëع‹ ¹µ Robo-51 (¨º) case ‘C’ : led3 = 1; clear_remote(); break; case ‘d’ : beep(); clear_remote(); break; case ‘D’ : beep(); clear_remote(); break;

// // // // // // // // //

LED P3.7 off Clear command Out of from case Beep 1 time Clear command Out of from case Beep 1 time Clear command Out of from case

} } }

ÃÒÂÅÐàÍÕ´à¾ÔÁè àµÔÁ¢Í§â»Ãá¡ÃÁ ÊíÒËÃѺ¡Ò÷íÒ§Ò¹¢Í§â»Ãá¡ÃÁ¹Õàé »š¹¡Ò÷´Êͺ¡ÒÃÃѺ¤íÒÊÑ§è ¨Ò¡ÃÕâÁµ¤Í¹â·ÃÅ ER-4 áŌǹíÒµÑÇÍÑ¡Éà »ÃШíÒ»ØÁ† ¤íÒÊѧè ã¹áµ‹ÅФÕ· ¶Õè ¡Ù ¡´ÁҤǺ¤ØÁàÍÒµ¾µØ «Ö§è 䴌ᡋ LED ¢Í§ P3.5-P3.7 áÅСíÒà¹Ô´ÊÑ­­Ò³àÊÕ§ 1 ¤ÃÑ§é «Ö§è ÀÒÂã¹â»Ãá¡ÃÁ¨ÐÁÕ¡ÒûÃСÒÈ #include <remote.h> änjµÍ¹àÃÔÁè µŒ¹à¾×Íè ãˌÊÒÁÒöàÃÕ¡ 㪌§Ò¹¿˜§¡ª¹Ñè get_remote áÅÐ clear_remote 䴌¹¹Ñè àͧ ¹Í¡¨Ò¡¹Õãé ¹Ê‹Ç¹áá¢Í§ main ÁÕ¡ÒÃÍÔ¹àÔ ªÕÂÅ¡ÒÃ㪌§Ò¹¢Í§äźÃÒÃÕ¹´Õé nj ¿˜§¡ª¹Ñè remote_init ËÅѧ¨Ò¡¹Ñ¹é â»Ãá¡ÃÁ¡ç¨Ð͋ҹ¤íÒÊÑ§è ¨Ò¡¡Òá´»ØÁ† ÃÕâÁµ¨Ò¡¡Òä׹¤‹Ò¢Í§¿˜§¡ª¹Ñè get_remote ´ŒÇ¤íÒÊѧè switch-case ÊÑ§à¡µÇ‹Ò ÀÒÂã¹â»Ãá¡ÃÁᵋÅÐ case ¡‹Í¹¨º¡Ò÷íÒ§Ò¹´ŒÇ¤íÒÊѧè break ¨Ð·íÒ¡ÒÃà¤ÅÕÂÏ ¤íÒÊÑ§è ·Õ¤è Ҍ §ã¹ºÑ¿à¿Íϴnj ¿˜§¡ª¹Ñè clear_remote à¾×Íè ·ÕÇè ҋ ã¹ÃÐËNjҧ·ÕÂè §Ñ äÁ‹Á¡Õ Òá´»ØÁ† ¨Ò¡ÃÕâÁµ¤Í¹â·ÃÅ ER-4 â»Ãá¡ÃÁ¨Ð䴌äÁ‹Ç¹à¢ŒÒä»·íÒ§Ò¹ã¹Ê‹Ç¹à´ÔÁ ª‹ÇÂãˌ¡Ò÷íÒ§Ò¹¶Ù¡µŒÍ§ÁÒ¡¢Ö¹é ¡µÑÇÍ‹ҧ àÁ×Íè ¡´»ØÁ† D ·ÕÃè âÕ Áµ¤Í¹â·ÃÅ ER-4 ¤‹Ò·Õäè ´ŒÍҨ໚¹ “d” ËÃ×Í “D” ·íÒãˌà¡Ô´ÊÑ­­Ò³àÊÕ§ ´Ñ§¢Ö¹é 1 ¤ÃÑ§é ¶ŒÒäÁ‹Á¡Õ Òá´µ‹Í¡ç¨ÐäÁ‹à¡Ô´ÊÑ­­Ò³àÊÕ§´Ñ§¡Å‹ÒÇ Ëҡᡌâ»Ãá¡ÃÁã¹Ê‹Ç¹¢Í§¡ÒõÃǨÊͺ case “d” áÅÐ case “D” ¨Ò¡ case ‘d’ : beep(); clear_remote(); break; case ‘D’ : beep(); clear_remote();

break;

໚¹ case ‘d’ : beep(); break; case ‘D’ : beep(); break;

¨Ð¾ºÇ‹ÒàÁ×Íè ¡´»ØÁ† D ¨Ò¡ÃÕâÁµ¤Í¹â·ÃÅ ER-4 ¼Å·Õäè ´Œ¤Í× àÁ×Íè ¡´»ØÁ† º¹ÃÕâÁµ¤Í¹â·ÃÅ ER-4 à¾Õ§¤Ãѧé à´ÕÂÇáŌǻŋÍ àÊÕ§¨Ð´Ñ§¢Ö¹é µÅÍ´àÇÅÒ à¾ÃÒФíÒÊѧè à´ÔÁ·Õ¤è Ҍ §ÍÂÙ㋠¹ºÑ¿à¿ÍÃÂ§Ñ äÁ‹ä´Œ¶¡Ù à¤ÅÕÂϴnj ¡ÒÃàÃÕ¡ Robo-51 ¿˜§¡ª¹Ñè clear_remote ¹Ñ¹è àͧ


126làÃÕ¹ÃÙá Œ ÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ

MCS-51

¡Ô¨¡ÃÃÁ·Õè 19

¤Çº¤ØÁËع‹ ¹µ Robo-51 ´ŒÇÂÃÕâÁµ¤Í¹â·ÃÅ A19.1 ແ´â»Ãá¡ÃÁ RIDE áŌÇà¢Õ¹â»Ãá¡ÃÁ·Õè A19-1 ºÑ¹·Ö¡ã¹ª×Íè ä¿Å act1901.c ·íÒ¡ÒäÍÁä¾Å áŌǴÒǹ âËÅ´ä¿ÅÊáً ¼§Ç§¨Ã RBX-51AC2plus ¢Í§Ëع‹ ¹µ Robo-51 ´ŒÇÂâ»Ãá¡ÃÁ Flip

â»Ãá¡ÃÁ·Õè A19-1 â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺ¤Çº¤ØÁËع‹ ¹µ Robo-51 ´ŒÇÂÃÕâÁµ¤Í¹â·ÃÅ ER-4 (Áյ͋ ) /*—————————————————————————————————————*/ // Program : Remote control robot // Description : Robot controlled by ER-4 remote control Robot receive command // : from remote control by serial communication at baud 1200 bps // Filename : act1901.c // C compiler : RIDE 51 V6.1 /*—————————————————————————————————————*/ #include <C51ac2.h> // Header include register of T89C51AC2 #include <delay.h> // Module function delay process #include <dc_motor.h> // Module function drive DC motor #include <remote.h> // Module function for remote #include <sound.h> // Module function drive sound #include <lcd_robo51.h>// Module function LCD #define pow 200 // Define constant for power drive DC motor void run_fd(int time_spin) { motor_fd(2,pow); // Motor channel 2 forward motor_fd(1,pow); // Motor channel 1 forward delay_ms(time_spin); // Delay time for robot drive forward } void run_bk(int time_spin) { motor_bk(2,pow); // Motor channel 2 backward motor_bk(1,pow); // Motor channel 1 backward delay_ms(time_spin); // Delay time for robot drive backward } void turn_left(int time_spin) { motor_fd(2,pow); // Motor channel 2 forward motor_bk(1,pow); // Motor channel 1 backward delay_ms(time_spin); // Delay time for robot spin turn left } void turn_right(int time_spin) { motor_bk(2,pow); // Motor channel 2 backward motor_fd(1,pow); // Motor channel 1 forward delay_ms(time_spin); // Delay time for robot spin turn right } void main() { beep(); // Sound beep 1 time remote_init(); // Initial remote while(1) // Infinite loop


àÃÕ¹ÃÙጠÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ MCS-51 l 127

â»Ãá¡ÃÁ·Õè A17-1 â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺ¤Çº¤ØÁËع‹ ¹µ Robo-51 ´ŒÇÂÃÕâÁµ¤Í¹â·ÃÅ ER-4 (¨º) { switch(get_remote()) // Check command for receive { case ‘a’ : run_bk(100); // Drive robot backward when receive command “a” clear_remote(); // Clear command break; // Out from case case ‘A’ : run_bk(100);// Drive robot backward when receive command “A” clear_remote(); // Clear command break; // Out from case case ‘b’ : turn_right(100); // Turn right when receive command “b” motor_stop(all); // Robot stop clear_remote(); // Clear command break; // Out from case case ‘B’ : turn_right(100); motor_stop(all); clear_remote(); break;

// // // //

Turn right when receive command “B” Robot stop Clear command Out from case

case ‘c’ : turn_left(100); motor_stop(all); clear_remote(); break;

// // // //

Turn right when receive command “c” Robot stop Clear command Out from case

case ‘C’ : turn_left(100); motor_stop(all); clear_remote(); break;

// // // //

Turn right when receive command “C” Robot stop Clear command Out from case

case ‘d’ : run_fd(100); clear_remote(); break;

// Turn right when receive command “d” // Clear command // Out from case

case ‘D’ : run_fd(100); clear_remote(); break;

// Turn right when receive command “D” // Clear command // Out from case

default }

// Out from case

: break;

}

}

ÃÒÂÅÐàÍÕ´à¾ÔÁè àµÔÁ¢Í§â»Ãá¡ÃÁ â»Ãá¡ÃÁ¹Õàé »š¹¡ÒäǺ¤ØÁËع‹ ¹µãˌà¤Å×Íè ¹·Õµè ÒÁ¤íÒÊÑ§è ¨Ò¡¡Òá´ÊÇÔµª·ÃÕè âÕ Áµ¤Í¹â·ÃÅ ER-4 «Ö§è ã¹ ¡ÒâѺà¤Å×Íè ¹ãˌµÃ§ä»¢ŒÒ§Ë¹ŒÒ, ¶ÍÂËÅѧ, àÅÕÂé Ç«ŒÒ áÅÐàÅÕÂé Ç¢ÇÒ ¹Í¡¨Ò¡¹Ñ¹é ã¹â»Ãá¡ÃÁ¹Õ¡é ÒÃ˹‹Ç§àÇÅÒ㹡ÒÃËÁعµÑÇáÅСíÒ˹´¡íÒÅѧ¢Ñºà¤Å×Íè ¹¢Í§Ëع‹ ¹µ ÊÒÁÒö »ÃѺà»ÅÕè¹ãˌàËÁÒÐÊÁ¡ÑºÊÀÒ¾¡ÒÃà¤Å×è͹·Õè¢Í§Ë؋¹Â¹µ â´ÂÍÒ¨·´Åͧ»ÃѺà»ÅÕ蹡ÒÃ˹‹Ç§àÇÅÒ㹪‹Ç§ ¨Ñ§ËÇÐËÁعµÑÇ·Ò§«ŒÒ 90 ͧÈÒ (àÁ×Íè ¡´»ØÁ† C à¾Õ§¤Ãѧé à´ÕÂÇ) áÅÐËÁعµÑÇ·Ò§¢ÇÒ 90 ͧÈÒ (àÁ×Íè ¡´»ØÁ† B à¾Õ§¤Ãѧé à´ÕÂÇ)


128làÃÕ¹ÃÙá Œ ÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ

MCS-51

A19.2 Ãѹâ»Ãá¡ÃÁ ·´Åͧ¡´»ØÁ† µ‹Ò§æ º¹ÃÕâÁµ¤Í¹â·ÃÅ ER-4 Êѧࡵ¡Ò÷íÒ§Ò¹¢Í§ Robo-51 ¡Ò÷íÒ§Ò¹¢Í§Ëع‹ ¹µ Robo-51 ໚¹´Ñ§¹Õé l àÁ×Í è ¡´»ØÁ† D

Ëع‹ ¹µ Robo-51 à¤Å×Íè ¹·Õµè çä»Í‹ҧµ‹Íà¹×Íè §

l àÁ×Í è ¡´»ØÁ† A

Ëع‹ ¹µ Robo-51 ¶ÍÂËÅѧÍ‹ҧµ‹Íà¹×Íè §

àÁ×Íè ¡´»ØÁ† C

Ëع‹ ¹µ Robo-51 ËÁعµÑÇä»·Ò§«ŒÒ ¶ŒÒ¡´ÊÇÔµªº¹ÃÕâÁµ¤Í¹â·ÃÅ ER-4 ¤ŒÒ§ ¨ÐàÅÕÂé ÇÍ‹ҧµ‹Íà¹×Íè §¨¹¡Ç‹Ò¨Ð»Å‹Í»ØÁ†

l àÁ×Í è ¡´»ØÁ† B

Ëع‹ ¹µ Robo-51 ËÁعµÑÇä»·Ò§¢ÇÒ ¶ŒÒ¡´ÊÇÔµªº¹ÃÕâÁµ¤Í¹â·ÃÅ ER-4 ¤ŒÒ§ ¨ÐàÅÕÂé ÇÍ‹ҧµ‹Íà¹×Íè §¨¹¡Ç‹Ò¨Ð»Å‹Í»ØÁ†

l

robo-51+  

MCS-51 robotic activity book (C) Innovative Experiment Co.,Ltd.

Read more
Read more
Similar to
Popular now
Just for you