Issuu on Google+

à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹ l 1

à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC ¤ÍÁä¾àÅÍÏ áÅСÒ÷´Åͧàº×éͧµŒ¹ Getting started C programming for PIC microcontroller with mikro C compiler

¡ÄÉ´Ò ã¨àÂç¹ ªÑÂÇѲ¹ ÅÔÁé ¾Ã¨ÔµÃÇÔäÅ (C) Innovative Experiment Co.,Ltd.

INNOVATIVE EXPERIMENT


2 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

Getting started C programming for PIC microcontroller with mikro C compiler

à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹ ISBN 978-974-05-3099-2 ¡ÄÉ´Ò ã¨àÂç¹ ªÑÂÇѲ¹ ÅÔÁé ¾Ã¨ÔµÃÇÔäŠʧǹÅÔ¢ÊÔ·¸Ôµì ÒÁ ¾.Ã.º. ÅÔ¢ÊÔ·¸Ôì ¾.È. 2537 ˌÒÁ¡ÒÃÅÍ¡àÅÕ¹äÁ‹Çҋ ʋǹ˹֧è ʋǹ㴢ͧ˹ѧÊ×ÍàŋÁ¹Õé ¹Í¡¨Ò¡¨Ðä´ŒÃºÑ Í¹Ø­Òµ ã¤Ã¤ÇÃ㪌˹ѧÊ×ÍàŋÁ¹Õé 1. ¹Ñ¡àÃÕ¹ ¹ÔÊµÔ ¹Ñ¡ÈÖ¡ÉÒ áÅкؤ¤Å·ÑÇè ä»·ÕÁè ¤Õ ÇÒÁʹã¨ã¹¡ÒÃà¢Õ¹â»Ãá¡ÃÁ¤Çº¤ØÁäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇÂÀÒÉÒ C 2. ʶҺѹ¡ÒÃÈÖ¡ÉÒ âçàÃÕ¹ ÇÔ·ÂÒÅÑ ÁËÒÇÔ·ÂÒÅÑ ·ÕÁè ¡Õ ÒÃແ´¡ÒÃàÃÕ¹¡ÒÃÊ͹ÇÔªÒÍÔàÅç¡·Ã͹ԡʏËÃ×ÍÀÒ¤ÇÔªÒ ÇÔÈÇ¡ÃÃÁÍÔàÅç¡·Ã͹ԡʏáÅФÍÁ¾ÔÇàµÍÏ 3. ¤³Ò¨ÒÏ·ÁÕè ¤Õ ÇÒÁµŒÍ§¡ÒÃÈÖ¡ÉÒ áÅÐàµÃÕÂÁ¡ÒÃàÃÕ¹¡ÒÃÊ͹ÇÔªÒäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ ã¹ÃдѺÍÒªÕÇÈÖ¡ÉÒáÅÐ »ÃÔ­­ÒµÃÕ

´íÒà¹Ô¹¡ÒèѴ¾ÔÁ¾áÅШíÒ˹‹ÒÂâ´Â ºÃÔÉ·Ñ ÍÔ¹â¹àǵտ àÍç¡à¾ÍÃÔàÁ¹µ ¨íÒ¡Ñ´ 3133/53 «.ÊØ¢ÁØ ÇÔ· 101/2 ¶.ÊØ¢ÁØ ÇÔ· á¢Ç§ºÒ§¹Ò ࢵºÒ§¹Ò ¡ÃØ§à·¾Ï 10260 â·ÃÈѾ· 0-2747-7001-4 â·ÃÊÒà 0-2747-7005 ÃÒÂÅÐàÍÕ´·Õ»è ÃÒ¡¯ã¹Ë¹Ñ§Ê×ÍàŋÁ¹Õäé ´Œ¼Ò‹ ¹¡ÒõÃǨ·Ò¹Í‹ҧÅÐàÍÕ´áÅжŒÇ¹¶Õè à¾×Íè ãˌÁ¤Õ ÇÒÁÊÁºÙóáÅж١ µŒÍ§ÁÒ¡·ÕÊè ´Ø ÀÒÂ㵌à§×Íè ¹ä¢áÅÐàÇÅÒ·Õ¾è §Ö Áա͋ ¹¡ÒèѴ¾ÔÁ¾à¼Âá¾Ã‹ ¤ÇÒÁàÊÕÂËÒÂÍѹÍÒ¨à¡Ô´¨Ò¡ ¡ÒùíÒ¢ŒÍÁÙÅ ã¹Ë¹Ñ§Ê×ÍàŋÁ¹Õäé »ãªŒ ·Ò§ºÃÔÉ·Ñ ÍÔ¹â¹àǵտ àÍç¡à¾ÍÃÔàÁ¹µ ¨íÒ¡Ñ´ ÁÔ䴌ÁÀÕ ÒÃÐ㹡ÒÃÃѺ¼Ô´ªÍºáµ‹»ÃСÒÃã´ ¤ÇÒÁ¼Ô´¾ÅÒ´¤ÅÒ´à¤Å×Íè ¹·ÕÍè Ò¨ÁÕáÅÐä´ŒÃºÑ ¡ÒèѴ¾ÔÁ¾à¼Âá¾Ã‹ÍÍ¡ä»¹Ñ¹é ·Ò§ºÃÔÉ·Ñ Ï ¨Ð¾ÂÒÂÒÁªÕáé ¨§áÅРᡌä¢ã¹¡ÒèѴ¾ÔÁ¾¤ÃÑ§é µ‹Íä»


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹ l 3

¤íÒ¹íÒ ¡ÒþѲ¹Òâ»Ãá¡ÃÁà¾×Íè ¤Ç¤ºØÁäÁâ¤Ã¤Í¹â·ÃÅàÅÍϴnj ÂÀÒÉÒÃдѺÊÙ§ ઋ¹ ÀÒÉÒàºÊÔ¡ ËÃ×Í C ÊÔ§è Ë¹Ö§è ·ÕÁè ¤Õ ÇÒÁÊíҤѭÍ‹ҧÂÔ§è ¤×Í «Í¿µáÇÏ·àÕè ÃÕÂ¡Ç‹Ò ¤ÍÁä¾àÅÍÏ (compiler) ËÃ×͵ÑÇá»Åâ»Ãá¡ÃÁ Áѹ¨Ð·íÒ˹ŒÒ·Õãè ¹ ¡ÒÃá»ÅÀÒÉÒÊÙ§¹Ñé¹ÁÒ໚¹ÀÒÉÒáÍÊà«ÁºÅÕ áŌǨҡ¹Ñ鹨ÐÁÕ«Í¿µáÇÏÍÕ¡µÑÇ˹Ö觷ÕàÃÕÂ¡Ç‹Ò áÍÊà«ÁàºÅÍÏ (assembler) ·íÒ˹ŒÒ·Õãè ¹¡ÒÃá»ÅÀÒÉÒáÍÊà«ÁºÅÕ໚¹ÀÒÉÒà¤Ã×Íè §ËÃ×ÍáÁªÕ¹â¤Œ´ÍÕ¡µ‹Í˹֧è â´Â·ÑÇè ä»áŌÇ㹪ش ¢Í§¤ÍÁä¾àÅÍÏÁ¡Ñ ¨Ð¼¹Ç¡àÍÒáÍÊà«ÁàºÅÍÏࢌÒänj´ÇŒ ¡ѹ à¾×Íè ãˌ¡ÒþѲ¹Òâ»Ãá¡ÃÁ໚¹ä»Í‹ҧÊдǡ áÅÐÅ´ ¢Ñ¹é µÍ¹ã¹¡Ò÷íҧҹŧ mikroC ¤ÍÁä¾àÅÍÏ໚¹Ë¹Ö§è 㹤ÍÁä¾àÅÍÏ·äÕè ´ŒÃºÑ ¤ÇÒÁʹã¨ã¹áǴǧ¡ÒþѲ¹Òâ»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ¡ÒÃ㪌§Ò¹·Õ§è ҋ  áÅÐÁÕà¤Ã×Íè §Á×ÍʹѺʹع㹡Ò÷íÒ§Ò¹ÁÒ¡ÁÒ ÃÇÁä»·Ñ§é µÑǪ‹ÇÂÍ‹ҧä¿ÅäźÃÒÃÕ (library) ·ÕÁè ÍÕ ÂÙà‹ »š¹¨íҹǹÁÒ¡ ÃͧÃѺ¡ÑºÍØ»¡Ã³µÍ‹ ¾‹Ç§ËÅÒ¡ËÅÒ áÅдŒÇÂʋǹ¢Í§ «Í¿µáÇÏ·ãÕè ªŒã¹¡ÒþѲ¹Òâ»Ãá¡ÃÁ·Õàè ÃÕÂ¡Ç‹Ò mikro IDE ¹Ñ¹é ÁÕ¤ÇÒÁÊÁºÙóáÅкÃèØà¤Ã×Íè §Á×Í·Õªè Nj Â㹡ÒÃà¢Õ¹ â»Ãá¡ÃÁänj¾ÃŒÍÁ ¨Ñ´ä´ŒÇҋ ໚¹à¤Ã×Íè §Á×;Ѳ¹Òâ»Ãá¡ÃÁ·ÕÊè ÒÁÒöàÃÔÁè µŒ¹ä´Œ§Ò‹ ÂÁÒ¡µÑÇ˹֧è ÊíÒËÃѺã¹Ë¹Ñ§Ê×ÍàŋÁ¹Õ¨é йíÒàʹͶ֧ ¡ÒÃ㪌§Ò¹áÅÐÇÔ¸¡Õ ÒþѲ¹Òâ»Ãá¡ÃÁÀÒÉÒ C ´ŒÇ mikroC ¤ÍÁ ä¾àÅÍÏÊÒí ËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC â´Â์¹ä»·Õàè ºÍÏ PIC16F877 áÅÐ PIC16F887 ÁÕ¤Òí ͸ԺÒ¡ÒÃ㪌§Ò¹ ¿˜§¡ª¹Ñè áÅÐäźÃÒÃÕ·ÊÕè Òí ¤Ñ­ ÃÇÁ¶Ö§¡Ò÷´Åͧ·Ò§ÎÒÏ´áÇÏ·ÊÕè ÒÁÒöáÊ´§ãˌàË繶֧¼Å¡Ò÷íҧҹ䴌Í‹ҧªÑ´à¨¹ áÅШѺµŒÍ§ä´Œ â´Âã¹áµ‹ÅСÒ÷´Åͧ¨ÐÁÕ¤Òí ͸ԺÒ·ÄÉ®Õ Ç§¨Ã µÑÇÍ‹ҧâ»Ãá¡ÃÁ ¢Ñ¹é µÍ¹¡Ò÷´Åͧ áÅмŠ·Õàè ¡Ô´¢Ö¹é à¾×Íè ãˌ¼àٌ ÃÕ¹ÊÒÁÒö·íÒ¤ÇÒÁࢌÒ㨠áÅеÃǨÊͺ¼Å¡Ò÷íÒ§Ò¹à»ÃÕºà·Õº䴌 mikroC ໚¹«Í¿µáÇÏ·Ò§¾Ò³ÔªÂ ¨ÐµŒÍ§ÁÕ¡ÒèѴ«×éÍÁÒÍ‹ҧ¶Ù¡µŒÍ§¨Ò¡à¨ŒÒ¢Í§ÅÔ¢ÊÔ·¸Ôì¹Ñ蹤×Í Mikroelektronika ÊÒÁÒöÊÑ§è «×Íé 䴌·Õè www.mikroe.com Í‹ҧäáçµÒÁ·Ò§ Mikroelektronika ¡ç䴌ແ´âÍ¡ÒÊãˌ ¼Ùʌ ¹ã¨ÊÒÁÒö´ÒǹâËÅ´ mikroC Ãع‹ 2K ä»·´Åͧ㪌§Ò¹ä´Œ¿ÃÕ â´ÂäÁ‹¨Òí ¡Ñ´àÇÅÒ ÊÒÁÒö㪌§Ò¹à¤Ã×Íè §Á×Í¾×¹é °Ò¹ä´Œ¤Ãº 㪌¡ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC 䴌·¡Ø àºÍÏ·§Ñé 12Fxxx, 16Fxxx áÅÐ 18Fxxxx Ëҡᵋ¨Òí ¡Ñ´¢¹Ò´ ¢Í§ä¿Åänj·Õè 2 ¡ÔâÅàÇÔô áÅÐàÁ×Íè ÁÕ¡ÒèѴ«×Íé Ãع‹ ÊÁºÙ󏡨ç ÐÊÒÁÒö㪌§Ò¹ä´ŒÍ‹ҧäÁ‹Á¢Õ ͌ ¨íÒ¡Ñ´ µÑÇÍ‹ҧ¡Ò÷´Åͧ·Õ¹è Òí àʹÍã¹Ë¹Ñ§Ê×ÍàŋÁ¹Õ·é §Ñé ËÁ´ ÊÒÁÒö·íÒ§Ò¹ÀÒÂ㵌 mikroC Ãع‹ 2K 䴌Í‹ҧ ÊÁºÙó ¢Í¢Íº¤Ø³ Mikroelektronika ÊíÒËÃѺ¡ÒÃà¼Âá¾Ã‹ mikroC Ãع‹ 2K à¾×Íè ãˌ¹¡Ñ ·´Åͧ䴌ÁâÕ Í¡ÒÊàÃÔÁè µŒ¹ ¡ÒþѲ¹Òâ»Ãá¡ÃÁäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇÂÀÒÉÒ C ÊíÒËÃѺ¼Ù㌠ªŒ§Ò¹·Õµè ͌ §¡ÒÃ㪌ù؋ ÊÁºÙóÊÒÁÒöʹѺ ʹع¼Ù¾Œ ² Ñ ¹Ò䴌´ÇŒ ¡ÒèѴ«×Íé ¨Ò¡ Mikroelektronika ·Õè www.mikroe.com ¡ÒÃàÃÕ¹ÃÙäŒ Áâ¤Ã¤Í¹â·ÃÅàÅÍÃÇ¹Ñ ¹Õé ÀÒÉÒ C ໚¹»˜¨¨ÑÂË¹Ö§è ·Õ¹è ¡Ñ ¾Ñ²¹Òâ»Ãá¡ÃÁäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏÊÁÑ ãËÁ‹µÍŒ §ãˌ¤ÇÒÁʹ㨠à¾ÃÒй͡¨Ò¡¨Ðª‹ÇÂÅ´¤ÇÒÁ«Ñº«ŒÍ¹áÅÐÂا‹ Âҡ㹡ÒþѲ¹Òâ»Ãá¡ÃÁáÅŒÇ Âѧª‹ÇÂãˌ¡Òà µ‹ÍÂÍ´áÅСÒûÃѺà»ÅÕÂè ¹â»Ãá¡ÃÁã¹Í¹Ò¤µÊÒÁÒö·íÒ䴌§Ò‹ Â¢Ö¹é ¡ÄÉ´Ò ã¨àÂç¹ ªÑÂÇѲ¹ ÅÔÁé ¾Ã¨ÔµÃÇÔäÅ


4 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

ÊÒúѭ º··Õè 1 á¹Ð¹íÒ mikroC..............................................................................................................7 º··Õè 2 ¡ÒÃà¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C àº×Íé §µŒ¹ÊíÒËÃѺ mikroC ¤ÍÁä¾àÅÍÏ.....................35 º··Õè 3 ¡ÒÃà¢Õ¹â»Ãá¡ÃÁà¾×è͵Դµ‹Í¾Íϵ¢Í§äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC...................................................................................................................55 »¯ÔºµÑ ¡Ô Ò÷Õè 1 ¢Ñº LED ·Õ¾è Íϵ C....................................................................61 »¯ÔºµÑ ¡Ô Ò÷Õè 2 ¡ÒÃ㪌§Ò¹¾Íϵ A áÅÐ E ໚¹àÍÒµ¾µØ ........................................65 »¯ÔºµÑ ¡Ô Ò÷Õè 3 ¡ÒÃ㪌§Ò¹¾Íϵ A áÅÐ B ໚¹àÍÒµ¾µØ ........................................67 »¯ÔºµÑ ¡Ô Ò÷Õè 4 ·´Êͺ͋ҹ¤‹Ò¨Ò¡ÊÇÔµª............................................................69

º··Õè 4 ¡íÒà¹Ô´àÊÕ§´ŒÇ¿˜§¡ª¹Ñè Sound.................................................................................73 »¯ÔºµÑ ¡Ô Ò÷Õè 5 ¢ÑºàÊÕ§´ŒÇ¿˜§¡ª¹Ñè Sound.........................................................75

º··Õè 5 ÍÔ¹àµÍÃÃ»Ñ µã¹äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ¡Ñº mikroC ¤ÍÁä¾àÅÍÏ................77 »¯ÔºµÑ ¡Ô Ò÷Õè 6 ÍÔ¹àµÍÃÃ»Ñ µ·¢Õè Ò RB0/INT...........................................................85

º··Õè 6 ¡ÒõԴµ‹Í¡ÑºâÁ´ÙÅ LCD ´ŒÇ¿˜§¡ª¹Ñè ¢Í§ mikroC ¤ÍÁä¾àÅÍÏ...........................87 »¯ÔºµÑ ¡Ô Ò÷Õè 7 ʋ§¢ŒÍÁÙÅáÊ´§¼Å·Õâè Á´ÙÅ LCD â´Â㪌¿§˜ ¡ª¹Ñè Lcd.........................95 »¯ÔºµÑ ¡Ô Ò÷Õè 8 ʋ§¢ŒÍÁÙÅáÊ´§¼Å·Õâè Á´ÙÅ LCD â´Â㪌¿§˜ ¡ª¹Ñè Lcd8.....................104 »¯ÔºµÑ ¡Ô Ò÷Õè 9 ʋ§¢ŒÍÁÙÅáÊ´§¼Å·Õâè Á´ÙÅ LCD â´Â㪌¿§˜ ¡ª¹Ñè Lcd_Custom.........112

º··Õè 7 ¿˜§¡ª¹Ñè µÔ´µ‹Í¡ÑºË¹‹Ç¤ÇÒÁ¨íÒ¢ŒÍÁÙÅÍÕÍ¾Õ ÃÍÁ¢Í§äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC.....115 »¯ÔºµÑ ¡Ô Ò÷Õè 10 ·´Êͺ¡ÒÃà¢Õ¹áÅÐ͋ҹ˹‹Ç¤ÇÒÁ¨íÒ¢ŒÍÁÙÅÍÕÍ¾Õ ÃÍÁ................117

º··Õè 8 ¿˜§¡ª¹Ñè ͋ҹ¤‹Ò¨Ò¡âÁ´ÙÅá»Å§ÊÑ­­Ò³ÍйÒÅ͡໚¹´Ô¨ÔµÍÅ ¢- ͧäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC...................................................................................119 »¯ÔºµÑ ¡Ô Ò÷Õè 11 ¡ÒÃ㪌§Ò¹¿˜§¡ª¹Ñè Adc_Read ¡Ñº PIC16F877..........................125 »¯ÔºµÑ ¡Ô Ò÷Õè 12 ͋ҹ¤‹Ò¨Ò¡âÁ´ÙÅ ADC ẺµÔ´µ‹Íâ´ÂµÃ§.................................131 »¯ÔºµÑ ¡Ô Ò÷Õè 13 ͋ҹ¤‹Ò¨Ò¡âÁ´ÙÅ ADC áÊ´§¼Å·Õâè Á´ÙÅ LCD............................133


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹ l 5

º··Õè 9 㪌§Ò¹¿˜§¡ª¹Ñè PWM ¢Í§ mikroC............................................................................137 »¯ÔºµÑ ¡Ô Ò÷Õè 9 ¤Çº¤ØÁ¤ÇÒÁàÃçǢͧÁÍàµÍϴnj ¿˜§¡ª¹Ñè PWM.............................140

º··Õè 10 ¡ÒõԴµ‹Í USART âÁ´ÙÅÊ×Íè ÊÒâŒÍÁÙžÍϵ͹ءÃÁ¢Í§ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ¿˜§¡ª¹Ñè ¢Í§ mikroC ¤ÍÁä¾àÅÍÏ....................143 »¯ÔºµÑ ¡Ô Ò÷Õè 15 ¡ÒÃÃѺʋ§¢ŒÍÁÙÅÃÐËNjҧäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏáÅФÍÁ¾ÔÇàµÍÏ.......146 »¯ÔºµÑ ¡Ô Ò÷Õè 16 Ê×Íè ÊÒâŒÍÁÙÅ͹ءÃÁâ´Â㪌¡Ãкǹ¡Ò÷ҧ«Í¿µáÇÏ..................152 »¯ÔºµÑ ¡Ô Ò÷Õè 17 ¡ÒÃʋ§¢ŒÍÁÙÅÍЫԧâ¤Ã¹ÑÊ´ŒÇ¡ÒáíÒ˹´¤‹ÒÃÕ¨ÊÔ àµÍÏâ´ÂµÃ§........164 »¯ÔºµÑ ¡Ô Ò÷Õè 18 ¡ÒÃÃѺ¢ŒÍÁÙÅÍЫԧâ¤Ã¹ÑÊ´ŒÇ¡ÒáíÒ˹´¤‹ÒÃÕ¨ÊÔ àµÍÏâ´ÂµÃ§........166 »¯ÔºµÑ ¡Ô Ò÷Õè 19 ¡ÒÃÃѺ¢ŒÍÁÙÅÍЫԧâ¤Ã¹ÑÊâ´ÂãªŒÍ¹Ô àµÍÃÃ»Ñ µ................................168

º··Õè 11 ¡ÒõԴµ‹ÍÍØ»¡Ã³ÃкººÑÊ I2C ´ŒÇ¿˜§¡ª¹Ñè ¢Í§ mikroC ¤ÍÁä¾àÅÍÏ..............171 »¯ÔºµÑ ¡Ô Ò÷Õè 20 µÔ´µ‹ÍºÍÏ´¢ÑºÃÕàŏ 16 ª‹Í§ ZX-RELAY16 ´ŒÇ¿˜§¡ª¹Ñè I2C........177 »¯ÔºµÑ ¡Ô Ò÷Õè 21 µÔ´µ‹Í¡ÑºâÁ´ÙÅà¢çÁ·ÔÈ´Ô¨µÔ ÍÅ CMPS03 ´ŒÇ¿˜§¡ª¹Ñè I2C............183 »¯ÔºµÑ ¡Ô Ò÷Õè 22 µÔ´µ‹ÍºÍÏ´¢ÑºÃÕàŏ 16 ª‹Í§ ZX-RELAY16 ´ŒÇ¿˜§¡ªÑè¹ Soft_I2C........................................................................192

º··Õè 12 ¡ÒõԴµ‹ÍÍØ»¡Ã³áºº SPI ¢Í§äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ¿˜§¡ª¹Ñè ¢Í§ mikroC ¤ÍÁä¾àÅÍÏ.....................................................................197 »¯ÔºµÑ ¡Ô Ò÷Õè 23 µÔ´µ‹Í¡Ñº MCP4922 äÍ«Õá»Å§ÊÑ­­Ò³´Ô¨µÔ ÍÅ໚¹ÍйÒÅÍ¡....204

º··Õè 13 ¡ÒõԴµ‹ÍÍØ»¡Ã³ÃкººÑÊ˹֧è ÊÒ´ŒÇ¿˜§¡ª¹Ñè ¢Í§ mikroC ¤ÍÁä¾àÅÍÏ......209 »¯ÔºµÑ ¡Ô Ò÷Õè 24 ͋ҹ¤‹ÒÍسËÀÙÁ¨Ô Ò¡äÍ«Õ DS18B20 ´ŒÇ¿˜§¡ª¹Ñè onewire............217

ÀÒ¤¼¹Ç¡ : á¹Ð¹íÒà¤Ã×Íè §Á×Í㹡Ò÷´Åͧ·Ò§ÎÒÏ´áÇÏ.....................................................223


6 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 7

º··Õè 1

á¹Ð¹íÒ mikroC

mikroC ໚¹ªØ´«Í¿µáÇϾѲ¹Òâ»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ¼Ù¾Œ ² Ñ ¹Ò¤×Í mikroElektronika (mikroE : www.mikroe.com) mikroC 䴌ºÃèØà¤Ã×Íè §Á×Íà¾×Íè ÍíҹǠ¤ÇÒÁÊдǡ㹡ÒþѲ¹Òâ»Ãá¡ÃÁänjÁÒ¡ÁÒ µÑÇá»Åâ»Ãá¡ÃÁÀÒÉÒ C ËÃ×Í C compiler ÁÕû٠Ẻ ¡Ò÷íÒ§Ò¹·Õàè ¢ŒÒ¡Ñ¹ä´Œ¡ºÑ ANSI C Áҵðҹ ÁÕäźÃÒÃÕÃͧÃѺ¡ÒõԴµ‹Í¡ÑºÍØ»¡Ã³ÀÒ¹͡¨íҹǹ ÁÒ¡ ¾ÃŒÍÁ·Ñ§é µÑÇÍ‹ҧâ»Ãá¡ÃÁ·Õªè Nj ÂÅ´àÇÅÒ㹡ÒþѲ¹Òâ»Ãá¡ÃÁ ¼Ù¾Œ ² Ñ ¹Òâ»Ãá¡ÃÁÊÒÁÒöµÃǨÊͺ·ÃѾÂҡâͧäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ䴌µÅÍ´àÇÅÒ «Ö§è ໚¹ »ÃÐ⪹Áҡ㹡óշÕè㪌Ã؋¹ÊÒ¸Ôµ ·Ñ駹Õéà¾×èͪ‹Ç»‡Í§¡Ñ¹äÁ‹ãˌà¢Õ¹â»Ãá¡ÃÁÂÒÇà¡Ô¹¡Ç‹Ò¢ŒÍ¨íÒ¡Ñ´ mikroC ÊÒÁÒöãˌä¿Å¼ÅÅѾ¸ .hex ´ŒÇ¡ÒäÅÔ¡à¾Õ§»ØÁ† à´ÕÂÇ ·íÒãˌŴ¢Ñ¹é µÍ¹ã¹¡Ò÷íҧҹŧ䴌Í‹ҧ ÁÒ¡ áÅÐä¿Å .hex ÊÒÁÒö¹íÒä»ãªŒ¡ºÑ à¤Ã×Íè §â»Ãá¡ÃÁäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC Áҵðҹ䴌·¡Ø Ãع‹ ÊíÒËÃѺ㹡ÒÃ͸ԺÒ¶֧¡ÒÃ㪌§Ò¹áÅÐÃÒÂÅÐàÍÕ´¢Í§â»Ãá¡ÃÁ·´Åͧ¨Ð͌ҧ¶Ö§ mikroC ã¹Ãع‹ 2K ÍѹÁբ͌ ¨íÒ¡Ñ´´ŒÒ¹¢¹Ò´¢Í§ä¿ÅËÅѧ¨Ò¡¤ÍÁä¾ÅËÃ×Íá»Å§à»š¹ÃËÑÊà¤Ã×Íè § (ä¿Å¹ÒÁÊ¡ØÅ .hex) µŒÍ§äÁ‹à¡Ô¹ 2 ¡ÔâÅàÇÔô ᵋ¡Áç Ò¡à¾Õ§¾ÍÊíÒËÃѺ¡ÒþѲ¹Òâ»Ãá¡ÃÁÊíÒËÃѺ¡Ò÷´Åͧ㪌§Ò¹ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC àº×Íé §µŒ¹ ÃÇÁ·Ñ§é â»Ãá¡ÃÁÊíÒËÃѺ¤Çº¤ØÁ¡Ò÷íÒ§Ò¹¢Í§Ëع‹ ¹µÍµÑ â¹ÁÑµÔ ¢¹Ò´àÅç¡ä´Œà»š¹Í‹ҧ´Õ ÊíÒËÃѺ¼ÙŒãªŒ§Ò¹·Õè·´Åͧ㪌 mikroC áŌÇÁÕ¤ÇÒÁʹ㨵ŒÍ§¡ÒÃÃ؋¹ÊÁºÙóÊÒÁÒöµÔ´µ‹ÍÊÑè§ «×éÍ䴌·ÕèàǺ䫵¢Í§¼ÙŒ¼ÅÔµ http://www.mikroe.com

1.1 ¡ÒõԴµÑ§é mikroC ¤ÍÁä¾àÅÍÏ Ãع‹ 2K ¡ÒÃ䴌ÁÒ«Ö§è ä¿Åµ´Ô µÑ§é ¢Í§ mikroC ã¹Ãع‹ ÊÒ¸Ôµ¹Õé ÁÕ 2 ·Ò§¤×Í 1. ¨Ò¡á¼‹¹«Õ´ÃÕ ÍÁ·Õ¨è ´Ñ ÁÒ㹪ش·´ÅͧäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ËÃ×ͪش»ÃСͺ Ëع‹ ¹µ Robo-PICA ¢Í§ inex 2. ´ÒǹâËÅ´àÇÍϪ¹Ñè ãËÁ‹Åҋ ÊØ´·ÕÍè Ò¨ÁÕ·Õè www.mikroe.com ¨Ò¡¹Ñé¹·íÒ¡ÒõԴµÑé§àËÁ×͹¡Ñºâ»Ãá¡ÃÁ»ÃÐÂØ¡µ¢Í§ÇÔ¹â´Çʏ


8 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

1.2 mikroC IDE à¤Ã×Íè §Á×ÍÊíÒËÃѺ¾Ñ²¹Òâ»Ãá¡ÃÁ IDE ÁÒ¨Ò¡ Integrated Development Environment ËÁÒ¶֧ «Í¿µáÇÏ·ÃÕè ǺÃÇÁà¤Ã×Íè §Á×Í áÅÐÊÀÒÇÐáǴŌÍÁ㹡ÒþѲ¹Òâ»Ãá¡ÃÁàÍÒänj㹷Õàè ´ÕÂǡѹ â´ÂÀÒÂã¹ MikroC IDE »ÃÐ¡Íºä» ´ŒÇ à¤Ã×Íè §Á×Íà¤Ã×Íè §ãªŒ·ÍÕè Òí ¹Ç¤ÇÒÁÊдǡ´Ñ§¹Õé 1. ⤌´àÍ´ÔàµÍÏ (Code editor) ໚¹â»Ãá¡ÃÁàÍ´ÔàµÍÏÊÒí ËÃѺà¢Õ¹áÅÐᡌ䢫ÍÃÊ â»Ãá¡ÃÁÀÒÉÒ C â´Â⤌´àÍ´ÔàµÍϨзíÒ¡ÒÃá¡áÂЪش¤íÒÊѧè , µÑÇá»Ã, ¢ŒÍÁÙŵÑÇàÅ¢, ÍÑ¡¢ÃÐ áÅÐ ¤íÒ͸ԺÒ ´ŒÇÂÊÕ (color syntax) ÃÇÁ·Ñ§é ÃٻẺµÑÇÍÑ¡Éà ¼Ù㌠ªŒ§Ò¹ÊÒÁÒöàÅ×Í¡»ÃѺà»ÅÕÂè ¹ÃٻẺ áÅÐÊÕÊѹ¢Í§¢ŒÍÁÙÅᵋÅЪ¹Ô´ä´Œàͧ »ØÁ† ¤ÍÁä¾Åâ»Ãá¡ÃÁ

˹Œ Ò µ‹ Ò §´Ù ¤‹ Ò ÃÕ ¨Ô Ê àµÍÏ (Watch window)

⤌´àÍ´ÔàµÍÏ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ·ãÕè ªŒ

ª‹Í§¡íÒ˹´¤ÇÒÁ¶ÕÊè ­ Ñ ­Ò³¹Ò́¡Ò ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ

˹ŒÒµ‹Ò§¼ÅÅѾ¸

ÃÙ»·Õè 1-1 áÊ´§Ë¹ŒÒµ‹Ò§ËÅÑ¡¢Í§ mikroC IDE áÅÐʋǹ»ÃСͺ·ÕÊè Òí ¤Ñ­


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 9

2. äźÃÒÃÕÊÒí àÃç¨ÃÙ» ã¹ mikroC 䴌àµÃÕÂÁäźÃÒÃբͧâ»Ãá¡ÃÁÀÒÉÒ C änj¨Òí ¹Ç¹ ÁÒ¡ à¾×èͪ‹ÇÂãˌ¡ÒþѲ¹Òâ»Ãá¡ÃÁÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ÊÒÁÒö·íÒ䴌Í‹ҧÃÇ´àÃçÇ äÁ‹Ç‹Ò¨Ð໚¹äźÃÒÃÕà¡ÕèÂǡѺ¡ÒõԴµ‹Í˹‹Ç¤ÇÒÁ¨íÒ ¡ÒÃáÊ´§¼Å¢ŒÍÁÙÅ ¡ÒÃÊ×èÍÊÒâŒÍÁÙÅ ¡ÒáÒà ¨Ñ´à¡çº¢ŒÍÁÙŵ‹Ò§æ áÅÐäºÃÒÃÕÊíÒËÃѺ¡ÒõԴµ‹ÍÍØ»¡Ã³ÀÒ¹͡ 3. à¤Ã×Íè §Á×ͪ‹ÇÂà¢Õ¹â»Ãá¡ÃÁ ·Õªè Nj ÂáÊ´§¼Åâ¤Ã§ÊÌҧ¢Í§â»Ãá¡ÃÁ µÑÇá»Ã áÅÐ ¿˜§¡ª¹Ñè µ‹Ò§ æ ÀÒÂã¹â»Ãá¡ÃÁ 4. C ¤ÍÁä¾àÅÍÏ 㪌㹡ÒÃá»Åâ»Ãá¡ÃÁÀÒÉÒ C ໚¹ÀÒÉÒáÍÊà«ÁºÅÕ áÅм¹Ç¡ ¡Ñºâ»Ãá¡ÃÁáÍÊà«ÁàºÅÍÏà¾×Íè á»Åâ»Ãá¡ÃÁ໚¹áÁªÕ¹â¤Œ´¨Ñ´à¡çºã¹ÃٻẺä¿Å Hex Áҵðҹ ÊÒÁÒö㪌¡ºÑ à¤Ã×Íè §â»Ãá¡ÃÁ PIC Áҵðҹ䴌·§Ñé ËÁ´ 5. «ÔÁàÙ ÅàµÍÏ ໚¹à¤Ã×Íè §Á×ÍʹѺʹع¡ÒèíÒÅͧ¡Ò÷íÒ§Ò¹¢Í§â»Ãá¡ÃÁ 㪌´¼Ù Å¡Òà ·íÒ§Ò¹¢Í§â»Ãá¡ÃÁ·ÕÅÐÊàµç» áÅÐÂѧ㪌ªÇ‹ ¨ѺàÇÅÒ¡Ò÷íÒ§Ò¹¢Í§â»Ãá¡ÃÁ·ÕÅЪ‹Ç§ ÃÇÁ¶Ö§´Ù¡Òà à»ÅÕÂè ¹á»Å§¢Í§ÃÕ¨ÊÔ àµÍϵҋ § æ 䴌Í‹ҧ§‹Ò´Ò 6. àÍ¡ÊÒáÒÃ㪌§Ò¹¤Ø³ÀÒ¾ÊÙ§ ¹Í¡¨Ò¡¡ÒÃ㪌§Ò¹â»Ãá¡ÃÁ·Õ觋ÒÂáÅŒÇ mikroC Âѧ ʹѺʹع¢ŒÍÁÙÅàÍ¡ÊÒáÒÃ㪌§Ò¹â»Ãá¡ÃÁ äźÃÒÃÕµ‹Ò§æ áÅеÑÇÍ‹ҧâ»Ãá¡ÃÁänjÍ‹ҧÁÒ¡ÁÒ ª‹ÇÂãˌ¼àٌ ÃÔÁè µŒ¹ÊÒÁÒöàÃÕ¹ÃÙ¡Œ ÒÃà¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ¨Ò¡µÑÇÍ‹ҧ䴌§Ò‹ ¢ֹé

1.2.1 ÃÒÂÅÐàÍÕ´¢Í§â¤Œ´àÍ´ÔàµÍÏ (Code Editor) ⤌´àÍ´ÔàµÍϢͧ mikroC ໚¹àÍ´ÔàµÍÏ·ÁÕè ҾÌÍÁà¤Ã×Íè §Á×ͪ‹ÇÂ㹡ÒÃà¢Õ¹â»Ãá¡ÃÁ ·íÒãˌ ¡ÒÃà¢Õ¹·íÒ䴌§Ò‹ Â¢Ö¹é ´ŒÇ¤سÊÁºÑµµÔ ҋ § æ ´Ñ§¹Õé l

»ÃѺᵋ§ÃٻẺÊբͧµÑÇÍÑ¡Éõ‹Ò§ æ 䴌

l àÅ×Í¡áÊ´§ªØ´¤íÒÊѧè ÍÍ¡ÁÒ â´Â¡´»ØÁ † CTRL + Space bar l

´ÙÃÒÂÅÐàÍÕ´¤‹Ò¾ÒÃÒÁÔàµÍϢͧªØ´¤íÒÊÑ§è µ‹Ò§æ 䴌

l

ÃͧÃѺ¡ÒÃ㪌§Ò¹à·Áà¾Åµ (template) ËÃ×Í¡ÒÃÊÌҧÃٻẺ⤌´Íѵâ¹ÁѵäÔ ´Œ

l

µÃǨÊͺáÅÐᡌä¢â¤Œ´·Õàè ¢Õ¹¼Ô´ä´Œâ´ÂÍѵâ¹ÁѵÔ

l

¡íÒ˹´µíÒá˹‹§·Õµè ͌ §¡ÒáÃÐâ´´ä»ÂѧºÃ÷Ѵ·Õµè ͌ §¡ÒÃ䴌

ÊÒÁÒö»ÃѺᵋ§¡ÒÃ㪌§Ò¹ä´Œ´ÇŒ ¡ÒÃàÅ×Í¡·Õàè Á¹Ù Tools à Option ËÃ×ͤÅÔ¡»ØÁ† ·Õè˹ŒÒµ‹Ò§ËÅÑ¡¢Í§â»Ãá¡ÃÁ¡ç䴌 l


10 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

¡ÒÃáÊ´§ªØ´¤íÒÊѧè : Code Assistant [CTRL+SPACE] à¾×Íè Êдǡ㹡ÒÃà¢Õ¹â»Ãá¡ÃÁËÃ×Í㹺ҧ¤ÃÑ§é ¨íҪش¤íÒÊѧè äÁ‹ä´Œ ãˌ¡´¤ÕÂˏ ÅÑ¡¢Í§ª×Íè ¤íÒÊѧè áŌǡ´»ØÁ† CTRL+ SPACE (ËÁÒ¶֧¡´»ØÁ† Ctrl ¤ŒÒ§änjáŌǵÒÁ´ŒÇ¡Òá´»ØÁ† Space bar) ¤íÒÊÑ§è ·ÕÁè Õ ¤Õµ ÇÑ ÍÑ¡É÷Õàè Å×Í¡änjÍÂÙ´‹ Ҍ ¹Ë¹ŒÒ¨Ð»ÃÒ¡¯ÍÍ¡Áҷѧé ËÁ´ à¾Õ§àÅ×Íè ¹à¤ÍÏà«ÍÏä»Âѧ¤íÒÊÑ§è ·Õµè ͌ §¡Òà áŌǡ´ Enter ¤íÒÊÑ§è ¹Ñ¹é ¨Ð¶Ù¡àÃÕ¡¢Ö¹é ÁÒ㪌§Ò¹

¨Ò¡µÑÇÍ‹ҧ໚¹¡ÒÃàÅ×Í¡¾ÔÁ¾¤Òí Êѧè Lcd áŌǡ´¤Õ CTRL áÅÐ SPACE ¨Ð»ÃÒ¡¯Ë¹ŒÒµ‹Ò§ ¢Í§¤íÒÊÑ§è ·Õ¢è ¹Öé µŒ¹´ŒÇ¤íÒÇ‹Ò Lcd ¢Ö¹é ÁÒãˌàÅ×Í¡

¡Òê‹ÇÂáÊ´§¾ÒÃÒÁÔàµÍÏ : Parameter Assistant [CTRL+SHIFT+SPACE] ÊíÒËÃѺ¤íÒÊÑè§ËÃ×Í¿˜§¡ªÑè¹ã¹ä¿ÅäźÃÒÃÕÁҵðҹ¢Í§ mikroC àÁ×èÍ¡íÒ˹´ª×èÍ¿˜§¡ªÑè¹ËÃ×Í ¤íÒÊѧè áŌǵÒÁ´ŒÇÂà¤Ã×Íè §ËÁÒÂǧàÅçºà»´ ( ËÃ×Í¡´¤Õ CTRL+SHIFT+SPACE (¡´¤Õ Ctrl µÒÁ´ŒÇ Shift áÅÐ Space bar) ¤‹Ò¾ÒÃÒÁÔàµÍϢͧªØ´¤íÒÊÑ§è ¹Ñ¹é æ ¨Ð¶Ù¡áÊ´§¢Ö¹é ÁÒâ´ÂÍѵâ¹ÁѵãÔ ËŒãªŒ§Ò¹ä´Œ ·Ñ¹·Õ

¨Ò¡µÑÇÍ‹ҧ໚¹¡ÒÃàÅ×Í¡¾ÔÁ¾¤íÒÊÑè§ Lcd_Out_CP( áŌǡ´¤Õ CTRL+SHIFT+SPACE ¨Ð»ÃÒ¡¯¾ÒÃÒÁÔàµÍÏ unsigned char * text ¢Ö¹é Áҷѹ·Õ à¾×Íè ª‹ÇÂãˌ¼àٌ ¢Õ¹â»Ãá¡ÃÁ·ÃÒºÃÙ» Ẻ¡ÒáíÒ˹´¤‹Ò¾ÒÃÒÁÔàµÍϢͧ¤íÒÊÑ觹ÕéÍ‹ҧ¶Ù¡µŒÍ§


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 11

µÃǨÊͺáÅÐᡌ䢤íÒ¼Ô´Íѵâ¹ÁÑµÔ : Auto Correct ໚¹¤Ø³ÊÁºÑµ¡Ô ÒõÃǨÊͺ¤íÒ¼Ô´â´ÂÍѵâ¹ÁÑµÔ ÁÕ¡Ò÷íÒ§Ò¹¤ÅŒÒ¡Ѻ Microsoft Word áÅÐ Âѧª‹ÇÂᡌ䢡ÒþÔÁ¾¤Òí ÊÑ§è ·Õäè Á‹¶¡Ù µŒÍ§¢Í§¼Ù㌠ªŒ§Ò¹ â´Â¤íÒÊÑ§è ·ÕÁè ¡Ñ ¾ÔÁ¾¼´Ô º‹ÍÂæ ¹Ñ¹é ¼Ù㌠ªŒÊÒÁÒöà¾ÔÁè àµÔÁ¤íÒÊѧè 䴌 â´Â¡ÒÃàÅ×Í¡àÁ¹Ù Tools à Options ¨Ò¡¹Ñ¹é àÅ×Í¡ä»·ÕËè ÇÑ ¢ŒÍ Auto Correct

¡ÒáíÒ˹´µíÒá˹‹§ Bookmark 㹡óշÕèâ»Ãá¡ÃÁÁÕ¤ÇÒÁÂÒÇÁÒ¡æ à¾×èÍãˌ¡ÒáÃÐâ´´ä»ÂѧµíÒá˹‹§µ‹Ò§æ ¢Í§â»Ãá¡ÃÁ 䴌Í‹ҧÃÇ´àÃçÇ ¼ÙŒãªŒ§Ò¹ÊÒÁÒö¡íÒ˹´µíÒá˹‹§ºÃ÷Ѵ·Õ赌ͧ¡ÒÃänj㹠Bookmark «Öè§ÊÒÁÒö ¡ÃÐâ´´ä»ÂѧµíÒá˹‹§¹Ñ¹é æ 䴌Í‹ҧÃÇ´àÃçÇ ¡íÒ˹´µíÒá˹‹§ Bookmark ·íÒ䴌â´Â ¡´»ØÁ† CTRL µÒÁ´ŒÇ SHIFT áÅФÕµ ÇÑ àÅ¢ ¨Ò¡¹Ñ¹é â»Ãá¡ÃÁ¨ÐáÊ´§µíÒá˹‹§ bookmark ·Õµè ͌ §¡Òà ¡´ Ctrl+Shift+1 ¡´ Ctrl+Shift+2 ¡´ Ctrl+Shift+3

¡ÒÃࢌҶ֧µíÒá˹‹§ Bookmark ·íÒ䴌â´Â¡Òá´»ØÁ† CTRL+µÑÇàÅ¢áÊ´§µíÒá˹‹§ Bookmark


12 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

¡ÒáÃÐâ´´ä»ÂѧµíÒá˹‹§ºÃ÷Ѵ·Õµè ͌ §¡Òà : GotoLine ¹Í¡¨Ò¡¡ÒÃ㪌 bookmark áÅŒÇ ¼Ù㌠ªŒ§Ò¹ÂѧÊÒÁÒö¡ÃÐâ´´ä»ÂѧºÃ÷Ѵµ‹Ò§æ ¢Í§â»Ãá¡ÃÁ 䴌â´ÂµÃ§ ¼‹Ò¹¡ÒᴤՏ CTRL+G ¨Ð»ÃÒ¡¯Ë¹ŒÒµ‹Ò§ GotoLine ¢Ö¹é ÁÒãˌ»Í‡ ¹àÅ¢ºÃ÷Ѵ·Õµè ͌ §¡Òà ¡ÃÐâ´´ áŌǤÅÔ¡»ØÁ† OK

Code Explorer

¤×Íʋǹ¢Í§Ë¹ŒÒµ‹Ò§·ÕèÇÒ§ÍÂً·Ò§´ŒÒ¹«ŒÒÂÁ×ͧ͢â»Ãá¡ÃÁ㪌áÊ´§¿˜§¡ªÑè¹áÅеÑÇá»Ã·Õè㪌 §Ò¹ÍÂًã¹â»Ãá¡ÃÁ¢³Ð¹Ñé¹


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 13

˹ŒÒµ‹Ò§¢Í§¢ŒÍÁÙŪ‹ÇÂàËÅ×Íà˧´‹Ç¹ : Quick Help (QHelp) ã¹á·ç»µ‹Í¨Ò¡ Code Explorer ໚¹Ë¹ŒÒµ‹Ò§¢ŒÍÁÙŪ‹ÇÂàËÅ×Í à¾×Íè ãˌ·ÃÒº¶Ö§¡ÒÃ㪌§Ò¹¿˜§¡ª¹Ñè ·Õºè ÃèØÍÂÙ㋠¹â»Ãá¡ÃÁ ઋ¹ ¡ÒÃàÃÕ¡㪌§Ò¹¿˜§¡ª¹Ñè µÔ´µ‹Í˹‹Ç¤ÇÒÁ¨íÒ Compact Flash ໚¹µŒ¹

ÃÒ¡ÒäÕÂŏ ´Ñ : Keyboard Shotcut ÍÂÙ㋠¹µíÒá˹‹§á·ç»¢ÇÒÊØ´¢Í§Ë¹ŒÒµ‹Ò§ Code Explorer àÁ×Íè àÅ×͡ࢌÒÁÒ ¨ÐÁÕÃÒ¡Òâͧ¤ÕÂŏ ´Ñ µ‹Ò§ æ ·Õãè ªŒã¹â»Ãá¡ÃÁ ª‹ÇÂãˌÊÒÁÒö㪌§Ò¹¤íÒÊÑ§è µ‹Ò§æ 䴌Í‹ҧÃÇ´àÃçÇÂÔ§è ¢Ö¹é


14 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

˹ŒÒµ‹Ò§á¨Œ§¼ÅÅѾ¸ : Message Window

໚¹Ë¹ŒÒµ‹Ò§ÊíÒËÃѺáÊ´§¼ÅÅѾ¸¨Ò¡¡ÒäÍÁä¾Å ¶ŒÒ¡ÒäÍÁä¾ÅÁ¢Õ ͌ ¼Ô´¾ÅÒ´ ˹ŒÒµ‹Ò§¹Õé ¨ÐáÊ´§¢ŒÍ¼Ô´¾ÅÒ´´ŒÇÂÍÑ¡ÉÃÊÕá´§ 㹡óÕäÁ‹¼´Ô ¾ÅÒ´ ˹ŒÒµ‹Ò§¹Õ¨é ÐáÊ´§¢ŒÍ¤ÇÒÁÊÕÁNj § ¾ÃŒÍÁ·Ñ§é ᨌ§¢¹Ò´Ë¹‹Ç¤ÇÒÁ¨íÒ·Õãè ªŒä» ¡Ã³ÕÁ¢Õ ͌ ¼Ô´¾ÅÒ´â»Ãá¡ÃÁ¨ÐäÁ‹ÊÌҧä¿Å hex ¼Ù㌠ªŒ§Ò¹ÊÒÁÒÃ¶à¢ŒÒ ¶Ö§ºÃ÷Ѵ·Õèà¡Ô´¤ÇÒÁ¼Ô´¾ÅҴ䴌â´Â¡ÒôѺàºÔŤÅÔ¡·ÕèºÃ÷Ѵ·ÕèáÊ´§¢ŒÍ¼Ô´¾ÅÒ´ â»Ãá¡ÃÁ¨Ð ¡ÃÐâ´´ä»ÂѧµíÒá˹‹§·Õèà¡Ô´¢ŒÍ¼Ô´¾ÅÒ´·Ñ¹·Õ

1.2.2 à¤Ã×Íè §Á×;ÔàÈɪ‹ÇÂ㹡ÒþѲ¹Òâ»Ãá¡ÃÁ 1.2.2.1 µÒÃÒ§ÃËÑÊáÍÊ¡Õé (ASCII Chart) ໚¹à¤Ã×èͧÁ×Í·Õ誋ÇÂ㹡ÒÃà¢Õ¹â»Ãá¡ÃÁ·Õèà¡ÕèÂÇ¢ŒÍ§¡ÑºµÑÇÍÑ¡Éà ËÃ×Í¡ÒèѴ¡ÒâŒÍÁÙÅẺ String â´ÂµÒÃÒ§áÍÊ¡Õ¹é ªéÕ Çè ÂãËé¼àéÙ ¢Õ¹â»Ãá¡ÃÁ·ÃÒº¶Ö§ ¤èÒ¢éÍÁÙÅÃËÑÊáÍÊ¡Õ¢é ͧµÑÇÍÑ¡ÉÃËÃ×͵ÑÇàÅ¢ ·Õ¹è ÂÔ Á㪌ÁÒ¡ ¤×Í ¡ÒÃà¢Õ¹¢ŒÍÁÙÅä»áÊ´§¼Å·Õâè Á´ÙÅ LCD ໚¹µŒ¹ ¡ÒÃàÃÕ¡㪌§Ò¹µÒÃÒ§áÍÊ¡Õé ¨ÐàÃÕ¡¨Ò¡àÁ¹Ù Tools à Ascii Chart


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 15

1.2.2.2 á»Å⤌´â»Ãá¡ÃÁ໚¹ HTML (Export Code to HTML) ËÒ¡ÁÕ¤ÇÒÁµŒÍ§¡ÒáÒùíÒàʹͫÍÏÊâ»Ãáá¡ÃÁº¹ Blog ËÃ×ͺ¹àǻ䫵¹¹Ñé ¶ŒÒ·íÒ¡ÒäѴ Å͡੾ÒÐ⤌´·Õè໚¹¢ŒÍ¤ÇÒÁä»Í‹ҧà´ÕÂǹÑé¹ ÍÒ¨ãˌ¤ÇÒÁÊÁºÙóä´ŒäÁ‹à·‹Ò¡Ñº·ÕèáÊ´§ã¹Ë¹ŒÒµ‹Ò§ Code Editor ´Ñ§¹Ñ¹é ¶ŒÒµŒÍ§¡ÒÃãˌàǻ䫵áÊ´§«ÍÏÊâ»Ãá¡ÃÁ·ÕÁè ÃÕ »Ù ẺàËÁ×͹¡Ñºã¹â¤Œ´àÍ´ÔàµÍÏ ÊÒÁÒöàÃÕ¡㪌§Ò¹à¤Ã×Íè §Á×͵ÑǹÕäé ´Œâ´Âä»·Õàè Á¹Ù Tools à Export Code to HTML

1.2.2.3 ˹ŒÒµ‹Ò§Ê×Íè ÊÒâŒÍÁÙż‹Ò¹¾Íϵ͹ءÃÁ : USART Terminal à¤Ã×èͧÁ×Íà¾×èͪ‹ÇÂÃѺáÅÐʋ§¢ŒÍÁÙÅ͹ءÃÁÃÐËNjҧäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏáÅФÍÁ¾ÔÇàµÍÏ ¡ÒÃàÃÕ¡㪌§Ò¹·íÒ䴌â´Âä»·Õàè Á¹Ù Tools à USART Terminal ËÃ×Í¡´»ØÁ† Ctrl+T ¡ç䴌


16 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

1.2.2.4 ˹ŒÒµ‹Ò§Ê×Íè ÊÒâŒÍÁÙż‹Ò¹¾Íϵ USB ã¹âËÁ´ HID : HID Terminal ໚¹à¤Ã×Íè §Á×Íà¾×Íè ª‹ÇÂ㹡ÒõԴµ‹ÍÃÐËNjҧäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ¡ºÑ ¤ÍÁ¾ÔÇàµÍÏ ¼‹Ò¹¾Íϵ USB «Ö§è ã¹äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ºÒ§àºÍϨÐÁÕâÁ´ÙÅ USB µÔ´µÑ§é ÍÂً ઋ¹ PIC18F4550 ໚¹µŒ¹ ¡ÒÃàÃÕ¡㪌§Ò¹¨ÐµŒÍ§ä»·Õàè Á¹Ù Tools à HID Teminal

1.2.2.5 ˹ŒÒµ‹Ò§Ê×Íè ÊÒâŒÍÁÙż‹Ò¹Ãкºà¹çµàÇÔᏠ: UDP Terminal ໚¹à¤Ã×Íè §Á×ÍÃѺʋ§á¾ç¡à¡çµ¢ŒÍÁÙÅã¹Ãкºà¹çµàÇÔᏠâ´Âä»·Õàè Á¹Ù Tools à UDP Terminal


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 17

1.2.2.6 ˹ŒÒµ‹Ò§Ê×Íè ÊÒâŒÍÁÙšѺ¡Òϴ˹‹Ç¤ÇÒÁ¨íÒ : MMC Card Terminal ໚¹à¤Ã×Íè §Á×ÍÊíÒËÃѺ¡ÒÃ͋ҹáÅÐà¢Õ¹¡Òϴ˹‹Ç¤ÇÒÁ¨íÒª¹Ô´ MMC ¼‹Ò¹¡ÒÃÊ×Íè ÊÒÃ͹ءÃÁ ¡ÒÃàÃÕ¡㪌§Ò¹¨ÐµŒÍ§ä»·Õàè Á¹Ù Tools à MMC Card Terminal

1.2.2.7 â»Ãá¡ÃÁ¡íÒ˹´¨Ø´ÀÒ¾ÊíÒËÃѺâÁ´ÙÅ¡ÃÒ¿¡ LCD : GLCD Bitmap Editor ໚¹à¤Ã×Íè §Á×ͪ‹Ç¡íÒ˹´¢ŒÍÁÙÅ¡ÒÃÊÌҧÀÒ¾ãˌ¡ºÑ ¡ÃÒ¿¡ LCD ä»à»š¹â¤Œ´ÀÒÉÒ C ÊÒÁÒö 㪌§Ò¹¡Ñº¡ÃÒ¿¡ LCD ËÅÒÂàºÍÏ ÍÒ·Ô KS0108, T6963 ��Ã×Í¡ÃÒ¿¡ LCD ¢Í§â·ÃÈѾ· Nokia3110 ¡ÒÃàÃÕ¡㪌§Ò¹¨ÐµŒÍ§à¢ŒÒä»·Õàè Á¹Ù Tools à GLCD Bitmap Editor

1.2.2.8 â»Ãá¡ÃÁ¡íÒ˹´¢ŒÍÁÙÅÊíÒËÃѺ LED µÑÇàÅ¢ 7 ʋǹ : Seven Segment Decoder ໚¹à¤Ã×Íè §Á×ͪ‹Ç¡íÒ˹´¢ŒÍÁÙÅ 1 亵ÊÒí ËÃѺ¡ÒÃáÊ´§¼Å¢Í§ LED µÑÇàÅ¢ 7 ʋǹ ¡ÒÃàÃÕ¡ 㪌§Ò¹¨ÐµŒÍ§à¢ŒÒ·Õàè Á¹Ù Tools à Seven Segment Decoder


18 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

1.2.2.9 â»Ãá¡ÃÁÊÌҧµÑÇÍÑ¡ÉÃ੾ÒÐÊíÒËÃѺâÁ´ÙÅ LCD : LCD Custom Charactor ໚¹à¤Ã×Íè §Á×ͪ‹ÇÂÊÌҧµÑÇÍÑ¡ÉþÔàÈÉãˌ¡ºÑ âÁ´ÙÅ LCD ẺµÑÇÍÑ¡Éà â´ÂàÁ×Íè ÊÌҧµÑÇÍÑ¡Éà àÊÃç¨àÃÕºÌÍÂáŌǡ´»ØÁ† Generate â»Ãá¡ÃÁ¨ÐÊÌҧ⤌´ãˌâ´ÂÍѵâ¹ÁÑµÔ ¡ÒÃàÃÕ¡㪌§Ò¹¨ÐµŒÍ§à¢ŒÒ ·Õàè Á¹Ù Tools à LCD Custom Charactor

1.2.2.10 ˹ŒÒµ‹Ò§Ë¹‹Ç¤ÇÒÁ¨íÒ¢ŒÍÁÙÅÍÕÍ¾Õ ÃÍÁ : EEPROM Editor 㪌áÊ´§¤‹ÒáÅÐᡌ䢢ŒÍÁÙÅã¹Ë¹‹Ç¤ÇÒÁ¨íÒ¢ŒÍÁÙÅÍÕÍÕ¾ÃÍÁ¢Í§äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 19

1.3 àÃÔÁè µŒ¹ãªŒ§Ò¹ mikroC 1.3.1 㪌§Ò¹¨Ò¡µÑÇÍ‹ҧ mikroC àµÃÕÂÁµÑÇÍ‹ҧ¡ÒÃà¢Õ¹â»Ãá¡ÃÁ¾×¹é °Ò¹ÁÒãˌ¼ãٌ ªŒ§Ò¹·´ÅͧËÅÒÂâ»Ãá¡ÃÁ â´Â ËÅѧ¨Ò¡µÔ´µÑé§â»Ãá¡ÃÁ mikroC áÅŒÇ µÑÇÍ‹ҧâ»Ãá¡ÃÁµ‹Ò§æ ¨Ðà¡çºÍÂًã¹â¿Åà´ÍÏ C:\Program Files\Mikroelektronika\mikroC\Examples\EasyPic4 ÊíÒËÃѺä¿Åâ»Ãà¨ç¡µáá·Õè¨Ðແ´¢Öé¹à»š¹µÑÇÍ‹ҧ¹Õé㪌äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC16F877A «Ö§è ¡ç¤Í× ä¿Å¢Í§â»Ãá¡ÃÁ信оÃÔº (LED Blinking) ¢Ñ鹵͹¡ÒÃແ´µÑÇÍ‹ҧâ»Ãà¨ç¡µ¢Öé¹ÁÒ㪌§Ò¹ ÁÕ¢Ñ鹵͹´Ñ§¹Õé (1) ແ´â»Ãá¡ÃÁ mikroC áŌÇä»·Õàè Á¹Ù Project à Open Project

(2) ·Õè˹ŒÒµ‹Ò§ Open àÅ×Í¡â¿Åà´ÍÃì C:\Program Files\Mikroelektronika\mikroC\ Examples\EasyPic4\P16F877A\LED_Blinking àÃÕ¡ແ´ä¿Å LED_Blinking.ppc


20 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

(3) ·ÕËè ¹ŒÒµ‹Ò§ Project Setup (ºÃÔàdz´ŒÒ¹«ŒÒ¢ͧ¨ÍÀÒ¾) ¨ÐáÊ´§àºÍϢͧäÁâ¤Ã ¤Í¹â·ÃÅàÅÍÏ PIC ·Õàè Å×͡㪌 ¾ÃŒÍÁ·Ñ§é ¤ÇÒÁ¶ÕÊè ­ Ñ ­Ò³¹Ò́¡Ò ÊÒÁÒöà»ÅÕÂè ¹à»š¹àºÍÏ͹×è ·Õµè ͌ §¡ÒÃ䴌 ¨Ò¡Ãٻ໚¹¡ÒÃàÅ×Í¡à»ÅÕÂè ¹à»š¹àºÍÏ PIC16F887 áÅФÇÒÁ¶ÕÊè ­ Ñ ­Ò³¹Ò́¡Ò (Clock) ෋ҡѺ 20MHz

(4) ·ÕËè ¹ŒÒµ‹Ò§á¡Œä¢â»Ãá¡ÃÁËÃ×Í Code Editor ¨ÐáÊ´§ÃÒÂÅÐàÍÕ´¢Í§«ÍÏÊâ»Ãá¡ÃÁ LED_Blinking.c ´Ñ§áÊ´§ã¹â»Ãá¡ÃÁ·Õè 1-1 /* * Project name: LED_Blinking (Simple 'Hello World' project) * Copyright: (c) Mikroelektronika, 2005. * Description: This is a simple project. It turns on/off diodes connected to PORTC. It uses bitwise negation to toggle PORTB pins. * Test configuration: MCU: PIC16F877A Dev.Board: EasyPIC4 Oscillator: HS, 08.0000 MHz Ext. Modules: SW: mikroC v7.0 * NOTES: None. */ void main() { PORTC = 0; TRISC = 0; while(1) { PORTC = ~PORTC; Delay_ms(1000); } }

¡Ò÷íÒ§Ò¹¢Í§â»Ãá¡ÃÁ

àÃÔÁè µŒ¹´ŒÇ¡ÒáíÒ˹´ãˌ¾Íϵ C ໚¹àÍÒµ¾µØ áŌÇ˹‹Ç§àÇÅÒ 500 äÁâ¤ÃÇÔ¹Ò·Õ ¨Ò¡¹Ñ¹é Êѧè ãˌâ»Ãá¡ÃÁ ·íÒ§Ò¹ã¹ÅÙ» While(1) à¾×Íè ¡ÅѺʶҹÐÅͨԡ·Õàè ÍÒµ¾µØ ¢Í§¾Íϵ C áŌÇ˹‹Ç§àÇÅÒ 100 ä«à¡ÔÅ

â»Ãá¡ÃÁ·Õè 1-1 µÑÇÍ‹ҧâ»Ãá¡ÃÁÀÒÉÒ C ¢Í§ä¿Å LED_Blinking.c ·Õ·è Òí §Ò¹º¹ mikroC ¤ÍÁä¾àÅÍÏ


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 21

(5) ·íÒ¡ÒäÍÁä¾Å«ÍÏÊ⤌´ÀÒÉÒ C ãˌ໚¹ÀÒÉÒà¤Ã×Íè § (.hex) â´Â¡´¤Õ CTRL+F9 ·Õºè ÃÔàdzᶺ´ŒÒ¹º¹¢Í§â»Ãá¡ÃÁ ËÃ×Íä»·Õàè Á¹Ù Project áŌÇàÅ×Í¡¤íÒÊѧè ËÃ×ͤÅÔ¡·Õäè ͤ͹ Build ¡ç䴌 (6) ËÅѧ¨Ò¡¤ÍÁä¾Å ¼ÅÅѾ¸·äÕè ´Œ¨ÐáÊ´§·ÕËè ¹ŒÒµ‹Ò§ Messages ´ŒÒ¹Å‹Ò§¢Í§â»Ãá¡ÃÁ «Ö觶ŒÒäÁ‹ÁÕ¢ŒÍ¼Ô´¾ÅÒ´ã´æ ˹ŒÒµ‹Ò§ Messages ¨ÐáÊ´§¢ŒÍ¤ÇÒÁ Success ÊÕà¢ÕÂÇ áÅÐáÊ´§¨íҹǹ ˹‹Ç¤ÇÒÁ¨íÒâ»Ãá¡ÃÁáÅÐ˹‹Ç¤ÇÒÁ¨íÒ RAM ·Õ¶è ¡Ù 㪌§Ò¹

1.3.2 µÃǨÊͺ¼Å¡Ò÷íÒ§Ò¹¢Í§â»Ãá¡ÃÁ¼‹Ò¹ Debugger â»Ãá¡ÃÁ Debugger ¢Í§ mikroC ·íÒ§Ò¹àËÁ×͹¡Ñº¡ÒèíÒÅͧ¡Ò÷íÒ§Ò¹ËÃ×Í«ÔÁàÙ ÅàµÍÏ à¾×Íè ´Ù¼Å¡ÒÃà»ÅÕÂè ¹á¼Å§·ÕÃè ¨Õ ÊÔ àµÍϵҋ § æ ÀÒÂã¹µÑÇäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ â´ÂäÁ‹¨Òí ໚¹µŒÍ§´ÒǹâËÅ´ â»Ãá¡ÃÁä»Ãѹãˌ·Òí §Ò¹¨ÃÔ§ ÁÕ¢¹Ñé µÍ¹¡ÒÃ㪌§Ò¹´Ñ§¹Õé (1) ä»·Õè àÁ¹Ù RUN àÅ×Í¡¤íÒÊѧè Start Debugger ËÃ×ͤÅÔ¡»ØÁ† ´ŒÒ¹º¹¢Í§â»Ãá¡ÃÁ ËÃ×Í¡´¤Õ F9 à¾×Íè àÃÔÁè µŒ¹à»´Ë¹ŒÒµ‹Ò§ Debugger

ºÃÔàdzᶺà¤Ã×Íè §Á×Í

ËÁÒÂà赯 ¡‹Í¹¡ÒÃ㪌 Debugger ËÃ×ͨíÒÅͧ¡Ò÷íÒ§Ò¹ µŒÍ§·íÒ¡ÒäÍÁä¾Åâ»Ãá¡ÃÁ¡‹Í¹àÊÁÍ


22 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

(2) â»Ãá¡ÃÁ¨Ðແ´Ë¹ŒÒµ‹Ò§ Watch ¢Ö¹é ÁÒ à¾×Íè ´Ù¤Ò‹ µÑÇá»Ãµ‹Ò§æ ¼‹Ò¹Ë¹ŒÒµ‹Ò§ Watch Window â´Â¨ÐµŒÍ§àÅ×Í¡ÃÕ¨ÊÔ àµÍÏ·µÕè ͌ §¡ÒõÃǨÊͺ¤‹ÒàÊÕ¡‹Í¹ ÊíÒËÃѺâ»Ãá¡ÃÁ LED_Blinking ÃÕ¨ÊÔ àµÍÏ·ãÕè ªŒ§Ò¹¤×Í PORTC áÅÐ TRISC ºÃÔàdzᶺáÊ´§Ê¶Ò¹Ð¨Í§Ë¹ŒÒµ‹Ò§ Watch ¨ÐáÊ´§ µíÒá˹‹§¢Í§â»Ãá¡ÃÁà¤Ò¹àµÍÏ (¢³Ð«ÔÁàÙ ÅàµÍÏâ»Ãá¡ÃÁ) áÊ´§àÇÅÒ·Õãè ªŒàÁ×Íè ·íÒ¤íÒÊѧè (Stop watch) áÅÐá¿Å¡áÊ´§Ê¶Ò¹Ð (ÃÕ¨ÊÔ àµÍÏ Stutus)

à¾ÔÁè µÑÇá»Ã àÅ×Í¡µÑÇá»Ã

µíÒá˹‹§â»Ãá¡ÃÁà¤Ò¹àµÍÏ

á¿Å¡ã¹ÃÕ¨ÊÔ àµÍÏ STATUS

áÊ´§àÇÅÒàÁ×Íè ·íÒ¤íÒÊѧè

(3) 㹡ÒèíÒÅͧ¡Ò÷íÒ§Ò¹ÊÒÁÒö㪌»Ø†Áµ‹Ò§æ ·ÕèÍÂًºÃÔàdz´ŒÒ¹º¹¢Í§Ë¹ŒÒµ‹Ò§ Watch 㹡ÒôټšÒ÷íҧҹ䴌 ËÃ×Í¡´»ØÁ† ¤ÕÂŏ ´Ñ ´Ñ§¹Õé Ãѹâ»Ãá¡ÃÁ·ÕèµíÒá˹‹§à¤ÍÏà«ÍÏ»ÃÒ¡¯ÍÂً F4 ËÃ×Í ¡íÒ˹´/¡àÅÔ¡¨Ø´àºÃ¡ F5 ËÃ×Í Ãѹ/ËÂØ´ªÑèǤÃÒÇ¡ÒèíÒÅͧ¡Ò÷íÒ§Ò¹ F6 ËÃ×Í F7 ËÃ×Í Ãѹ໚¹Êàµç»â´ÂࢌÒä»ã¹â»Ãá¡ÃÁ‹Í Ãѹ໚¹Êàµç» â´ÂäÁ‹à¢ŒÒä»ã¹â»Ãá¡ÃÁ‹Í F8 ËÃ×Í Ãѹ໚¹Êàµç»¨¹ÍÍ¡¨Ò¡â»Ãá¡ÃÁ‹Í Ctrl+F8 ËÃ×Í à»´¡ÒèíÒÅͧ¡Ò÷íÒ§Ò¹ËÃ×Í«ÔÁÙàÅàµÍÏ F9 ËÃ×Í »´¡ÒèíÒÅͧ¡Ò÷íÒ§Ò¹ËÃ×Í«ÔÁÙàÅàµÍÏ Ctrl+F2 ËÃ×Í Alt+D áÊ´§Ë¹ŒÒµ‹Ò§´ÔÊáÍÊà«ÁàºÅÍÏ


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 23

(4) ¤ÅÔ¡·Õ»è Á؆ ËÃ×Í¡´»ØÁ† F8 à¾×Íè Ãѹâ»Ãá¡ÃÁãˌ·Òí §Ò¹áººÊàµç» ᵋäÁ‹à¢ŒÒä» ´Ù¡Ò÷íÒ§Ò¹ã¹â»Ãá¡ÃÁ‹Í ¡´»ØÁ† F8 ä»àÃ×Íè Âæ ¨¹¶Ö§¤íÒÊѧè PORTC = ~PORTC; Êѧࡵ¤‹Ò¢Í§ PORTC Ç‹Ò ÁÕ¡ÒÃà»ÅÕÂè ¹á»Å§ â´Â¶ŒÒ¤‹Òà´ÔÁ໚¹ 0x00 ¨Ðà»ÅÕÂè ¹à»š¹ 0xFF áÅжŒÒ¤‹Òà´ÔÁ໚¹ 0xFF ¨Ðà»ÅÕÂè ¹à»š¹ 0x00

1.3.3 ¡ÒõÃǨÇÑ´¤‹Òä«à¡ÔÅ¡Ò÷íÒ§Ò¹´ŒÇ Stopwatch àÅ×Í¡·Õàè Á¹Ù View à Debug WindowsView à Clock ¨Ð໚¹¡ÒÃແ´Ë¹ŒÒµ‹Ò§ Stopwatch à¾×Íè 㪌´¤Ù ҋ àÇÅÒ·Õãè ªŒã¹¡Ò÷íÒ¤íÒÊѧè ËÃ×Í¿˜§¡ª¹Ñè ¨Ò¡¡ÒÃÃѹẺ·ÕÅÐÊàµç» ËÃ×ͨҡÃѹä»ËÂØ´·Õµè Òí á˹‹§ Breakpoint (¡íÒ˹´µíÒá˹‹§ËÂØ´¡Ò÷íÒ§Ò¹¢Í§â»Ãá¡ÃÁ ã¹âËÁ´«ÔÁÅÙ àÅàµÍÏ)

â´Âã¹Ë¹ŒÒµ‹Ò§ Stopwatch áÊ´§¢ŒÍÁÙŴѧ¹Õé

1. Current Count áÊ´§¨íҹǹä«à¡ÔÅ¡Ò÷íÒ§Ò¹áÅÐàÇÅÒ·Õãè ªŒµ§Ñé ᵋແ´Ë¹ŒÒµ‹Ò§ Debug 2. Delta ໚¹¡ÒÃáÊ´§¨íҹǹä«à¡ÔŢͧ¤íÒÊѧè ËÃ×Í¿˜§¡ª¹Ñè ·Õàè ¾Ô§è Ãѹãˌ·Òí §Ò¹ä»Å‹ÒÊØ´ Reset to Zero

3. Stopwatch ໚¹¡ÒÃáÊ´§¨íҹǹä«à¡ÔÅ¡Ò÷íÒ§Ò¹áÅÐàÇÅÒ·Õè㪌 ËÅѧ¨Ò¡¡´»Ø†Á


24 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

4. Clock ໚¹Ë¹ŒÒµ‹Ò§ÊíÒËÃѺ¡íÒ˹´¤‹Ò¤ÇÒÁ·ÕÍè ÍÊ«ÔÅàÅàµÍÏ·ãÕè ªŒ «Ö§è äÁ‹¨Òí ໚¹µŒÍ§à»š¹ ¤‹Òà´ÕÂǡѺ¤‹Ò·Õ¡è Òí ˹´ã¹ä¿Åâ»Ãà¨ç¡µ¡äç ´Œ â´Â¡ÒÃà»ÅÕÂè ¹á»Å§¤‹Ò¤ÇÒÁ¶ÕÍè ÍÊ«ÔÅàÅàµÍÏã¹Ë¹ŒÒµ‹Ò§ Stopwatch ¨ÐäÁ‹Á¼Õ šѺ¡ÒÃà»ÅÕÂè ¹á»Å§¤‹Ò¤ÇÒÁ¶ÕÍè ÍÊ«ÔÅàÅàµÍÏã¹ä¿Åâ»Ãà¨ç¡µ 5. »ØÁ† 㪌à¾×Íè ÃÕ૵¹Ò́¡Ò¨ÑºàÇÅÒãˌ¡ÅѺä»àÃÔÁè ¹Ñº·Õè 0 (ᵋ¨ÐäÁ‹ ÁռšѺ¤‹Ò Current Count ) 㪌ÊÒí ËÃѺ¡ÒèѺàÇÅÒ੾Ò¡ª¹Ñè ·Õµè ͌ §¡Òà ¤‹Ò·ÕèáÊ´§¨ÐáÊ´§·Ñé§ã¹ÃٻẺ¢Í§ä«à¡ÔÅ¡Ò÷íÒ§Ò¹¢Í§äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏáÅФҺ ÃÐÂÐàÇÅÒ·Õè㪌㹡Ò÷íÒ¤íÒÊÑè§ËÃ×Í¿˜§¡ªÑè¹ ËÅѧ¨Ò¡ÃÙ¨Œ ¡Ñ ¡ÑºÊ‹Ç¹»ÃСͺ¢Í§Ë¹ŒÒµ‹Ò§ Stopwatch áŌǵ‹Íä»à»š¹¡Ò÷´ÅͧÃѹâ»Ãá¡ÃÁ Ẻ«Ô§à¡ÔÅÊàµç» à¾×Íè ´Ù¨Òí ¹Ç¹ä«à¡ÔÅáÅФҺàÇÅÒ·Õâè »Ãá¡ÃÁ㪌 â´Â·íÒµÒÁ¢Ñ¹é µÍ¹´Ñ§¹Õé (1) ¡íÒ˹´ÊÑ­­Ò³¹Ò́¡Ò (Clock) ෋ҡѺ 20 MHz (2) à¤ÅÕÂϤҋ ä«à¡ÔÅáÅФҺàÇÅÒ â´Â¤ÅÔ¡·Õ»è Á؆ (3) ¤ÅÔ¡·Õ»è Á؆ Step Over «Ö§è àÇÅÒ·Õè Stopwatch 㪌෋ҡѺ 3 ä«à¡ÔÅ

ËÃ×Í¡´»ØÁ† F8 Ë¹Ö§è ¤Ãѧé ໚¹¡Ò÷íÒ¤íÒÊѧè PORTC

= 0

(4) ¤ÅÔ¡·Õ»è Á؆ Step Over ÍÕ¡¤Ãѧé ໚¹¡Ò÷íÒ¤íÒÊѧè TRISC = 0 Êѧࡵ·ÕËè ÇÑ ¢ŒÍ Delta ¤‹Ò¨ÐÁÕ¤‹Òà·‹Ò 2 ä«à¡ÔÅ áÊ´§Ç‹Ò¤íÒÊÑè§ TRISC = 0 㪌¤ÒºàÇÅÒ 2 ä«à¡ÔÅᵋ¤‹ÒÃÐÂÐàÇÅҢͧ Stopwatch ¨ÐÁդҋ ෋ҡѺ 5 à¹×Íè §¨Ò¡à»š¹¡ÒÃÃÇÁ¤íÒÊÑ§è ¡‹Í¹Ë¹ŒÒ¹Õàé ¢ŒÒä»´ŒÇ (5) ¤ÅÔ¡·Õ»è Á؆ Step Over ÍÕ¡¤ÃÑ§é ¨Ð໚¹¡Ò÷íÒ¿˜§¡ª¹Ñè ˹‹Ç§àÇÅÒ 1 ÇÔ¹Ò·Õ ã¹ ¡Ã³Õ·ãÕè ªŒ Step Over â»Ãá¡ÃÁ¨ÐäÁ‹à¢ŒÒä»áÊ´§¡Ò÷íҧҹ㹿˜§¡ª¹Ñè ¹Ñ¹é ᵋ¨ÐáÊ´§¤‹Ò¨íҹǹä«à¡ÔÅ ¢Í§¿˜§¡ª¹Ñè Delay_ms(1000) ÍÍ¡ÁÒ (ÍÒ¨µŒÍ§ãªŒàÇÅÒÃÍÊÑ¡¤Ãً ) ¼ÅÅѾ¸·äÕè ´Œ¤Í× 5,018,507 ä«à¡ÔÅ ËÃ×Í໚¹ÃÐÂÐàÇÅÒ 1003.70 ÁÔÅÅÔÇ¹Ô Ò·Õ ËÃ×Í 1 ÇԹҷվʹÕ


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 25

1.4 ˹ŒÒµ‹Ò§áÊ´§Ê¶Ôµ¡Ô ÒÃ㪌˹‹Ç¤ÇÒÁ¨íÒ ËÅѧ¨Ò¡¤ÍÁä¾Åâ»Ãá¡ÃÁáÅŒÇ ¼Ù㌠ªŒ§Ò¹ÊÒÁÒöàÃÕ¡´ÙʶԵµÔ ҋ §æ Íѹ»ÃСͺ仴ŒÇ 1. Memory usage áÊ´§¡ÒÃ㪌§Ò¹Ë¹‹Ç¤ÇÒÁ¨íÒâ»Ãá¡ÃÁáÅÐ˹‹Ç¤ÇÒÁ¨íÒ RAM «Ö觨ÐáÊ´§·Ñ駻ÃÔÁҳ˹‹Ç¤ÇÒÁ¨íÒ·ÕèNjҧÍÂًáÅÐ˹‹Ç¤ÇÒÁ¨íÒ·Õè㪌ä»

2.Procedures (sizes) áÊ´§»ÃÔÁÒ³¡ÒÃ㪌˹‹Ç¤ÇÒÁ¨íÒẋ§µÒÁ¿˜§¡ª¹Ñè ·Õàè ÃÕ¡㪌


26 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

3.Procedures (locations) áÊ´§µíÒá˹‹§¡ÒÃ㪌§Ò¹Ë¹‹Ç¤ÇÒÁ¨íÒâ»Ãá¡ÃÁ¢Í§ ¿˜§¡ª¹Ñè µ‹Ò§ æ ÀÒÂã¹â»Ãá¡ÃÁ ÃÇÁ¶Ö§»ÃÔÁÒ³¡ÒÃ㪌˹‹Ç¤ÇÒÁ¨íÒã¹áµ‹Å¡ª¹Ñè ´ŒÇÂ

4.Procedures (details) áÊ´§ÃÒÂÅÐàÍÕ´¢Í§¿˜§¡ªÑè¹µ‹Ò§æ â´ÂÁÕ¡ÒÃÃкصíÒá˹‹§ áÍ´à´ÃÊàÃÔèÁµŒ¹áÅÐÊÔé¹ÊØ´¢Í§¿˜§¡ªÑè¹


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 27

5.RAM ໚¹¡ÒÃáÊ´§ÃÒÂÅÐàÍÕ´¢Í§Ë¹‹Ç¤ÇÒÁ¨íÒ RAM áÅÐÃÕ¨ÊÔ àµÍϵҋ §æ â´Â áÊ´§ª×èÍáÅеíÒá˹‹§áÍ´à´ÃʢͧÃÕ¨ÔÊàµÍÏáÅÐ˹‹Ç¤ÇÒÁ¨íÒàËŋҹÑé¹

6.ROM ໚¹Ë¹ŒÒµ‹Ò§áÊ´§ÃÒÂÅÐàÍÕ´â»Ãá¡ÃÁÀÒÉÒáÍÊá«ÁºÅÕ·àÕè ¡çºÍÂÙ㋠¹Ë¹‹Ç ¤ÇÒÁ¨íÒâ»Ãá¡ÃÁ


28 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

1.5 ·´Êͺ¡Ò÷íÒ§Ò¹·Ò§ÎÒÏ´áÇÏ 㹡Ò÷´Êͺ¡Ò÷íÒ§Ò¹¢Í§â»Ãá¡ÃÁÀÒÉÒ C ·Õèà¢Õ¹¢Öé¹à¾×èÍãˌàË繼šÒ÷íÒ§Ò¹¨ÃÔ§æ ·Ò§ÎÒÏ´áÇÏ ¨ÐµŒÍ§ÁÕ¡ÒôÒǹâËÅ´ä¿Å .hex «Ö§è ໚¹ä¿Å¼ÅÅѾ¸¢Í§ mirkoC ¤ÍÁä¾àÅÍÏä»Âѧ ˹‹Ç¤ÇÒÁ¨íÒâ»Ãá¡ÃÁ¢Í§äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´Ñ§¹Ñ¹é 㹡Ò÷´Êͺ¹Õ¨é ÐÁÕà¤Ã×Íè §Á×Í 2 µÑÇ·Õè à¡ÕèÂÇ¢ŒÍ§¤×Í ÎÒÏ´áÇÏÊíÒËÃѺâ»Ãá¡ÃÁáÅз´ÅͧäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC áÅЫͿµáÇÏ ÊíÒËÃѺ¡ÒôÒǹâËÅ´â»Ãá¡ÃÁ ÎÒÏ´áÇÏ·Õèá¹Ð¹íÒã¹Ë¹Ñ§Ê×ÍàŋÁ¹Õé»ÃСͺ´ŒÇ 1. ºÍÏ´ÊíÒËÃѺâ»Ãá¡ÃÁäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC/dsPIC ¼‹Ò¹¾Íϵ USB ª×Íè Ãع‹ PX-200 ¢Í§ºÃÔÉ·Ñ ÍÔ¹â¹àǵտ àÍç¡à¾ÍÃÔàÁ¹µ ¨íÒ¡Ñ´ (www.inex.co.th) 2. ºÍÏ´·´ÅͧÃع‹ NX-877 plusII ÊíÒËÃѺ¡Ã³ÕµÍŒ §¡Ò÷´Åͧ·ÑÇè ä» »¡µÔµ´Ô µÑ§é äÁâ¤Ã ¤Í¹â·ÃÅàÅÍÏ PIC18F877A änj ᵋÊÒÁÒöà»ÅÕÂè ¹à»š¹àºÍÏ PIC16F887 ËÃ×Í 18F4xxx ã¹Ãع‹ 40 ¢Ò䴌 3. RBX-877 V2.0 ºÍÏ´¤Çº¤ØÁËع‹ ¹µ Robo-PICA ·Õµè ´Ô µÑ§é äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC16F887 ᵋÊÒÁÒöà»ÅÕÂè ¹à»š¹àºÍÏ PIC16F877A ËÃ×Í 18F4xxx ã¹Ãع‹ 40 ¢Ò 䴌 ʋǹ«Í¿µáÇÏ·Õè㪌¤×Í PICkit2TM Programming Software ¢Í§ Microchip ¼ÙŒ¼ÅÔµäÁâ¤Ã ¤Í¹â·ÃÅàÅÍÏ PIC ÒÁÒö´ÒǹâËŴ䴌¿ÃÕ·Õè www.microchip.com

1.5.1 ¡ÒõԴµÑ§é «Í¿µáÇÏ PICkit2TM Programming software PICkit2TM Programming Software ໚¹«Í¿µáÇÏÊÒí ËÃѺâ»Ãá¡ÃÁäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC áÅÐ dsPIC Ẻá¿Åª·Õµè ͌ §·íҧҹËÇÁ¡Ñºà¤Ã×Íè §â»Ãá¡ÃÁ PICkit2TM ¢Í§ Microchip ËÃ×Íà¤Ã×Íè § â»Ãá¡ÃÁÍ×¹è ·ÕÁè ¡Õ Ò÷íÒ§Ò¹µÃ§¡Ñ¹ ã¹·Õ¹è àÕé Å×͡㪌ºÍÏ´â»Ãá¡ÃÁÃع‹ PX-200 «Ö§è ¾Ñ²¹Ò¢Ö¹é â´ÂºÃÔÉ·Ñ ÍÔ¹â¹àǵտ àÍç¡à¾ÍÃÔàÁ¹µ ¨íÒ¡Ñ´ ¨Ò¡¢ŒÍÁÙÅ·Õàè ¼Âá¾Ã‹ÊÊً Ò¸ÒóТͧà¤Ã×Íè §â»Ãá¡ÃÁ PICkit2TM â´Â Microchip ÊíÒËÃѺ¡ÒÃâ»Ãá¡ÃÁ¢ŒÍÁÙÅŧã¹Ë¹‹Ç¤ÇÒÁ¨íÒâ»Ãá¡ÃÁ¢Í§äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ¢Í§ºÍÏ´ PX-200 ¨Ð໚¹áººâ»Ãá¡ÃÁã¹Ç§¨Ã¼‹Ò¹·Ò§á¨¡ ICD2

1.5.1.1 µÔ´µÑ§é ¨Ò¡á¼‹¹«Õ´ÃÕ ÍÁ·ÕÁè ҡѺªØ´ PX-200 «Í¿µáÇÏ·ãÕè ªŒ¡ºÑ PX-200 ¤×Í PICkit2TM Programming Software â´Âã¹àÇÍÃìª¹Ñ ãËÁèæ ¹Ñ¹é ¾Ñ²¹ÒÁÒ¨Ò¡ Microsoft .NET ´Ñ§¹Ñ¹é ¡ÒõԴµÑ§é â»Ãá¡ÃÁà¾×Íè 㪌§Ò¹ ¼Ù㌠ªŒ§Ò¹µŒÍ§ÁÕ¡ÒõԴµÑ§é Microsoft .NET Framework àÊÕ¡‹Í¹


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 29

(¡) ¡Ã³Õ·¤Õè ÍÁ¾ÔÇàµÍÏäÁ‹Á¡Õ ÒõԴµÑ§é Microsoft .NET Framework ÁÒ¡‹Í¹ µÔ´µÑ§é Microsoft .NET Framework â´ÂàÅ×Í¡µÔ´µÑ§é ¨Ò¡â¿Åà´ÍÏ PICkit 2 Setup v2.01 dotNET à dotnetfx ã¹á¼‹¹«Õ´ÃÕ ÍÁ ´ÑºàºÔŤÅÔ¡·Õäè ¿Å dotnetfx.exe ¨Ð㪌àÇÅÒ㹡ÒõԴµÑ§é ¤ÃÙˋ ¹Ö§è ¨Ò¡¹Ñ¹é ¨Ö§µÔ´µÑ§é â»Ãá¡ÃÁ PICkit2TM Programming Software â´ÂàÅ×Í¡ä¿Å PICkit2Setup.msi (¢) ¡Ã³Õ·¤Õè ÍÁ¾ÔÇàµÍÏÁ¡Õ ÒõԴµÑ§é Microsoft .NET Framework áÅŒÇ µÔ´µÑ§é â»Ãá¡ÃÁ PICkit2TM Programming Software â´ÂࢌÒä»·Õâè ¿Åà´ÍÏ PICkit 2 Setup v2.01 ã¹á¼‹¹«Õ´ÃÕ ÍÁ ´ÑºàºÔŤÅÔ¡·Õäè ¿Å PICkit2Setup.msi

1.5.1.2 µÔ´µÑ§é ¨Ò¡ä¿Å·´Õè ÒǹâËÅ´¨Ò¡àǺ䫵¢Í§ Microchip ࢌÒä»ÂѧàǺ䫵¢Í§ Microchip ·Õè www.microchip.com àÅ×Í¡ËÑÇ¢ŒÍ Development tools à Pickit 2 Programmer/Debugger (¡) ¡Ã³Õ·¤Õè ÍÁ¾ÔÇàµÍÏäÁ‹Á¡Õ ÒõԴµÑ§é Microsoft .NET Framework ÁÒ¡‹Í¹ ãˌ´ÒǹâËÅ´ä¿Å¨Ò¡ËÑÇ¢ŒÍ PICkit2V2.01 Install with .NET Framework ¨Ð䴌ä¿Å PICkit 2 Setup v2.01 dotNET.zip (µÑÇàÅ¢àÇÍϪ¹Ñ ÍÒ¨à»ÅÕÂè ¹á»Å§µÒÁÃع‹ ·ÕÁè ¡Õ ÒûÃѺ»ÃاŋÒÊØ´) áÅŒÇ ·íÒ¡ÒâÂÒÂä¿Å·Õè´ÒǹâËÅ´ÁÒ à¡çºänjã¹â¿Åà´ÍÏ PICkit 2 Setup v2.01 dotNET ¨Ò¡¹Ñé¹à¢ŒÒä» ã¹â¿Åà´ÍÏ PICkit 2 Setup v2.01 dotNET à dotnetfx àÅ×Í¡´ÑºàºÔŤÅÔ¡·Õäè ¿Å dotnetfx.exe ¨Ð㪌 àÇÅÒ㹡ÒõԴµÑ§é ¤ÃÙˋ ¹Ö§è ¨Ò¡¹Ñ¹é ¨Ö§·íÒ¡ÒõԴµÑ§é â»Ãá¡ÃÁ Pickiit2TM Programming Software â´Â ࢌÒä»áŌǴѺàºÔŤÅÔ¡·Õäè ¿Å PICkit2Setup.msi (¢) ¡Ã³Õ·¤Õè ÍÁ¾ÔÇàµÍÏÁ¡Õ ÒõԴµÑ§é Microsoft .NET Framework áÅŒÇ ãˌ´ÒǹâËÅ´ä¿Å¨Ò¡ËÑÇ¢ŒÍ PICkit2V2.01 Install ¨Ð䴌ä¿Å PICkit 2 Setup v2.01.zip (µÑÇàÅ¢àÇÍϪ¹Ñ ÍÒ¨à»ÅÕÂè ¹á»Å§µÒÁÃع‹ ·ÕÁè ¡Õ ÒûÃѺ»ÃاŋÒÊØ´) áŌǷíÒ¡ÒâÂÒÂä¿Å·´Õè ÒǹâËÅ´ÁÒ à¡çº änjã¹â¿Åà´ÍÏ PICkit 2 Setup v2.01 ¨Ò¡¹Ñ¹é ࢌÒä»ã¹â¿Åà´ÍÏ àÅ×Í¡´ÑºàºÔŤÅÔ¡·Õäè ¿Å PICkit2Setup.msi à¾×Íè àÃÔÁè µŒ¹µÔ´µÑ§é â»Ãá¡ÃÁ


30 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

1.5.1.3 ¢Ñ¹é µÍ¹¡ÒõԴµÑ§é â»Ãá¡ÃÁ (1) ´ÑºàºÔŤÅÔ¡ä¿Åµ´Ô µÑ§é PICkit2Setup.msi ¨Ð»ÃÒ¡¯Ë¹ŒÒµ‹Ò§àÃÔÁè ¡ÒõԴµÑ§é ¤ÅÔ¡»ØÁ† Next

(2) ¨Ð»ÃÒ¡¯Ë¹ŒÒµ‹Ò§á¨Œ§µíÒá˹‹§â¿Åà´ÍÏ·¨Õè зíÒ¡ÒõԴµÑ§é ËÒ¡äÁ‹µÍŒ §¡ÒÃà»ÅÕÂè ¹á»Å§µíÒ á˹‹§â¿Åà´ÍÏãˌ¤ÅÔ¡»ØÁ† Next à¾×Íè ¼‹Ò¹¢Ñ¹é µÍ¹¹Õäé »

(3) ¨Ð»ÃÒ¡¯Ë¹ŒÒµ‹Ò§á¨Œ§Â×¹Âѹ¡ÒõԴµÑ§é â»Ãá¡ÃÁ ¤ÅÔ¡»ØÁ† Next à¾×Íè µÍºµ¡Å§


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 31

(4) ˹ŒÒµ‹Ò§á¨Œ§ÅÔ¢ÊÔ·¸«Í¿µáÇÏ»ÃÒ¡¯¢Ö¹é ¤ÅÔ¡ I agree à¾×Íè µÍºÃѺ ¨Ò¡¹Ñ¹é ¤ÅÔ¡»ØÁ† Next

(5) ˹ŒÒµ‹Ò§á¨Œ§Ê¶Ò¹Ð¡ÒõԴµÑ§é â»Ãá¡ÃÁ»ÃÒ¡¯¢Ö¹é Ãͨ¹¡ÃÐ·Ñ§è ¡ÒõԴµÑ§é àÊÃç¨ÊÁºÙó

¨Ò¡¹Ñ¹é ˹ŒÒµ‹Ò§á¨Œ§¡ÒõԴµÑ§é â»Ãá¡ÃÁàÊÃç¨ÊÁºÙó ¨Ð»ÃÒ¡¯¢Ö¹é ãˌ¤ÅÔ¡»ØÁ† Close à¾×Íè »´ ˹ŒÒµ‹Ò§¹Õé ໚¹ÍѹàĘ̂ÊÔ¹é ¢Ñ¹é µÍ¹¡ÒõԴµÑ§é â»Ãá¡ÃÁ PICkit2TM Programming Software


32 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

1.5.2 ¡ÒÃàª×Íè Áµ‹Íà¾×Íè ·´Åͧ·Ò§ÎÒÏ´áÇÏ (1) àÃÔÁè µŒ¹´ŒÇ¡Òõ‹ÍÊÒ USB ÃÐËNjҧ PX-200 ¡Ñº¾Íϵ USB ¢Í§¤ÍÁ¾ÔÇàµÍÏ ¨Ò¡¹Ñ¹é ແ´â»Ãá¡ÃÁ Pickit2TM Programming Software â´ÂàÅ×Í¡·Õè Start à All programs à Microchip à Pickit 2 V201 (àÅ¢àÇÍϪ¹ Ñ ÍÒ¨à»ÅÕÂè ¹á»Å§ä´ŒµÒÁ¡ÒûÃѺ»ÃاŋÒÊØ´) ã¹ÃÙ»·Õè 1-2 áÊ´§Ë¹ŒÒµ‹Ò§ ËÅÑ¡¢Í§â»Ãá¡ÃÁ Pickit2TM Programming Software àÁ¹Ù¤Òí Êѧè ᶺáÊ´§¡Ò÷íÒ§Ò¹

»ØÁ† ª‹ÇÂ㹡Òà ·íÒ§Ò¹

¡Ãͺ Configuration ª‹Í§áÊ´§Ê¶Ò¹Ð

˹ŒÒµ‹Ò§ºÑ¿à¿ÍϢ͌ ÁÙŢͧ˹‹Ç¤ÇÒÁ¨íÒâ»Ãá¡ÃÁ

ᨌ § ·Õè Á Ңͧ ä¿Å .hex ·ÕÃè ºÑ à¢ŒÒÁÒà¡çºänj㹠ºÑ¿à¿ÍÏ »ØÁ† ª‹ÇÂ㹡Òà ·íÒ§Ò¹

˹ŒÒµ‹Ò§ºÑ¿à¿ÍϢ͌ ÁÙŢͧ˹‹Ç¤ÇÒÁ¨íÒÍÕ; Õ ÃÍÁ

ÃÙ»·Õè 1-2 ˹ŒÒµ‹Ò§ËÅÑ¡¢Í§â»Ãá¡ÃÁ Pickiit2TM Programming Software


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 33

(2) µ‹ÍÊÒ ICD ÃÐËNjҧºÍÏ´â»Ãá¡ÃÁ PX-200 ¡ÑººÍϴ໇ÒËÁÒÂËÃ×ͺÍÏ´·´Åͧ (2.1) ¡Ã³Õ㪌¡ºÑ ºÍÏ´·´Åͧ NX-877 plusII µ‹Í¾Íϵ USB

àª×Íè Áµ‹Í¡Ñ¹´ŒÇÂÊÒ ICD2

¨‹ÒÂä¿àÅÕÂé § ¡´ÊÇÔµªàÅ×Í¡âËÁ´ â»Ãá¡ÃÁ (ä¿ÊÕá´§µÔ´ÊNjҧ) ¾×¹é ·ÕÊè Òí ËÃѺµ‹Íǧ¨Ã·´Åͧ

(2.1) ¡Ã³Õ㪌¡ºÑ ºÍÏ´¤Çº¤ØÁËع‹ ¹µ RBX-877 Rev.2007) ºÍÏ´â»Ãá¡ÃÁ PIC

ÊÒÂ ICD2

4.8-6V

RC7 RxD

DOWNLOAD

POWER

RC6 TxD

RC4 SDA

RC3 SCL LCD back light

ICD2

ON ON

M-1 M-2

S+

SERVO #1 #2

#3

µ‹Í¡Ñº¾Íϵ USB ¢Í§¤ÍÁ¾ÔÇàµÍÏ

Sound RC0

LCD

BATT

RA4

RESET

RB0

Robo - PICARRobotR PICRMicroRobotic RB3 RB0/INT

RA0

RA1

RA2

RA3

RA5

RE0

RE1

RE2

ÊíÒËÃѺâ»Ãá¡ÃÁµÑÇÍ‹ҧ LED_Blinking ·ÕÁè Ò¨Ò¡µÑÇÍ‹ҧã¹â¿Åà´ÍÏ C:\Program Files\Mikroelektronika\mikroC\Examples\EasyPic4\P16F877A\LED_Blinking ÁÕ¡ÒáíÒ˹´¡Òõ‹Í ǧ¨ÃäÁ‹µÃ§¡ÑººÍÏ´ RBX-877 Rev.2007 ´Ñ§¹Ñ¹é 㹡Ò÷´Êͺ¹Õ¨é Ð͌ҧ¶Ö§ºÍÏ´·´Åͧ NX-877 plus II ໚¹ËÅÑ¡ ÊíÒËÃѺµÑÇÍ‹ҧ¡Ò÷´Êͺ¡ÑººÍÏ´ RBX-877 Rev.200 ´ÙÃÒÂÅÐàÍÕ´à¾ÔÁè àµÔÁã¹Ë¹Ñ§Ê×Í àÃÕ¹ÃÙጠÅÐÊÌҧËع‹ ¹µÍµÑ â¹Áѵ¡Ô ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC «Ö§è ¨Ñ´ÁÒã¹Ø´Ëع‹ ¹µ Robo-PICA


34 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

(3) ¨Ò¡¹Ñ鹫ͿµáÇϨеÃǨÊͺ¡ÒÃàª×èÍÁµ‹ÍáÅÐàºÍϢͧäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ·Õèàª×èÍÁµ‹Í´ŒÇÂÍ‹ҧÍѵâ¹ÁÑµÔ áŌǨÐᨌ§àºÍÏ·Õ辺º¹Ë¹ŒÒµ‹Ò§¢Í§â»Ãá¡ÃÁ (4) àÅ×Í¡ä¿Å .hex ·Õ赌ͧ¡ÒÃâ»Ãá¡ÃÁ â´Âä»·ÕèàÁ¹Ù File àÅ×Í¡ Import Hex ¨ÐÁÕ˹ŒÒµ‹Ò§ ÊíÒËÃѺແ´ä¿Å *.hex ·Õ赌ͧ¡ÒÃâ»Ãá¡ÃÁŧäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ ã¹·Õè¹ÕéàÅ×Í¡ä»·Õè C:\Program Files\Mikroelektronika\mikroC\Examples\EasyPic4\P16F877A\LED_Blinking àÅ× Í ¡ä¿Å ª×è Í LED_Blinking.hex (5) ¤ÅÔ¡»ØÁ† Write à¾×Íè ´ÒǹâËÅ´â»Ãá¡ÃÁä»ÂѧºÍÏ´äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC

(6) µ‹Íǧ¨Ãâ´Âµ‹Í¢Ò¾Íϵ C ã´æ ࢌҡѺ LED +5V 11

32

C6 0.1PF 50V

+5V

1+! 21+$.&%%

R5 4k7 SW2 4-5-6

1 R6 1k

MCLR

LED RCx 12 31

13 C2 22pF

XTAL1 20MHz

R1 390

14 C3 22pF

(7) ¡Ã³Õ㪌ºÍÏ´ NX-877 plus II ËÅѧ¨Ò¡â»Ãá¡ÃÁáÅŒÇ ãˌ¡´ÊÇÔµª MODE ÁÒã¹âËÁ´ Run áŌǡ´ÊÇÔµª RESET äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ¨ÐàÃÔÁè ·íÒ§Ò¹ LED ·Õµè ͋ ÍÂÙ¨‹ еԴ¡Ð¾ÃԺ͋ҧµ‹Íà¹×Íè §

INNOVATIVE EXPERIMENT


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 35

º··Õè 2

¡ÒÃà¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C àº×Íé §µŒ¹ ÊíÒËÃѺ mikroC ¤ÍÁä¾àÅÍÏ ¡ÒþѲ¹Òâ»Ãá¡ÃÁÀÒÉÒ C ´ŒÇ mikroC ¨ÐµŒÍ§ÊÌҧä¿Åã¹Åѡɳмѧ§Ò¹ËÃ×Íä¿Åâ»Ãà¨ç¡µ (project) «Ö§è ÁÕ¢¹Ñé µÍ¹·Õäè Á‹Â§Ø‹ ÂÒ¡ à¹×Íè §¨Ò¡ mikroC ÁÕà¤Ã×Íè §Á×Í·Õªè Nj ÂʹѺʹع¡ÒÃÊÌҧä¿Å áÅÐÅ´ÀÒÃР㹡ÒÃà¢Õ¹â»Ãá¡ÃÁáÅСÒÃàª×Íè Áâ§ä¿Å

2.1 ¡ÒÃÊÌҧä¿Åâ»Ãà¨ç¡µ ÁÕ¢Ñ鹵͹â´ÂÊÃØ»´Ñ§¹Õé (1) ແ´â»Ãá¡ÃÁ mikroC ä»·Õàè Á¹Ù Project àÅ×Í¡¤íÒÊѧè New Project ¨Ð»ÃÒ¡¯Ë¹ŒÒµ‹Ò§ ¡ÒÃÊÌҧ Project ¢Ö¹é ÁҴѧÃÙ» ¡íÒ˹´ª×Íè â»Ãà¨ç¡µ ¡íÒ˹´µíÒá˹‹§â¿Åà´ÍÏ ÃÒÂÅÐàÍÕ´â»Ãà¨ç¡µ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ·àÕè Å×͡㪌 ¤ÇÒÁ¶ÕÊè ­ Ñ ­Ò³¹Ò́¡Ò ª‹Í§¡íÒ˹´¤‹Ò¤Ø³Åѡɳзҧ ÎÒÏ´áÇÏËÃ×ͤ͹¿¡à٠êѹè


36 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

¡ÒáíÒ˹´¤‹Òµ‹Ò§æ ã¹Ë¹ŒÒµ‹Ò§ New Project ÁÕ´§Ñ ¹Õé (1.1) Project Name ໚¹¡ÒáíÒ˹´ª×Íè ä¿Åâ»Ãà¨ç¡µáÅЪ×Íè «ÍÏÊâ»Ãá¡ÃÁ ÀÒÉÒ C ´ŒÇ â´Âä¿Åâ»Ãà¨ç¡µÁ¹Õ ÒÁÊ¡ÅØ໚¹ .ppc ʋǹä¿Å«ÍÏÊâ»Ãá¡ÃÁÁÕ¹ÒÁÊ¡ØÅ໚¹ .c ã¹·Õè ¹Õ¡é Òí ˹´ª×Íè Ç‹Ò Project01 (1.2) Project Path ໚¹¡ÒáíÒ˹´µíÒá˹‹§â¿Åà´ÍÏ·ãÕè ªŒà¡çºä¿Åâ»Ãà¨ç¡µ áÅЫÍÏÊâ»Ãá¡ÃÁ ã¹·Õ¹è ¡Õé Òí ˹´µíÒá˹‹§änj·Õè C:\MikroC_Code ¼Ù㌠ªŒ§Ò¹ÊÒÁÒö¡´¤Õ à¾×èÍàÅ×Í¡µíÒá˹‹§â¿Åà´ÍÏ´ŒÇµ¹àͧ䴌 ¨ÐÃкØËÃ×ÍäÁ‹¡äç ´Œ

(1.3) Description ໚¹¾×¹é ·ÕÊè Òí ËÃѺÃкØÃÒÂÅÐàÍÕ´à¡ÕÂè ǡѺâ»Ãà¨ç¡µ·ÊÕè Ìҧ¢Ö¹é

(1.4) Device ໚¹ª‹Í§ÊíÒËÃѺ¡íÒ˹´àºÍÏäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ·àÕè Å×͡㪌§Ò¹ à¹×Íè §¨Ò¡äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ᵋÅÐàºÍÏÁ¤Õ ³ Ø ÊÁºÑµäÔ Á‹àËÁ×͹¡Ñ¹ ¨Ö§µŒÍ§àÅ×Í¡àºÍϢͧäÁâ¤Ã ¤Í¹â·ÃÅàÅÍÏ PIC ãˌ¶¡Ù µŒÍ§ ã¹·Õ¹è àÕé Å×͡໚¹àºÍÏ PIC16F887 (1.5) Clock 㪌¡Òí ˹´¤‹Ò¤ÇÒÁ¶ÕÊè ­ Ñ ­Ò³¹Ò́¡Ò·Õãè ªŒ¡ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ˹‹ÇÂ໚¹ MHz ã¹·Õ¹è ãÕé ªŒ¤ÇÒÁ¶Õàè ·‹Ò¡Ñº 20 MHz (2) ¡íÒ˹´¤‹Ò¤Ø³ÊÁºÑµÔ·Ò§ÎÒÏ´áÇÏËÃ×ͤ͹¿¡ÙàêÑè¹ (configuration) ¢Í§äÁâ¤Ã ¤Í¹â·ÃÅàÅÍÏ PIC àºÍÏ·àÕè Å×͡㪌§Ò¹·Õªè ͋ § Device Flags ÁÕµÇÑ àÅ×Í¡ãˌ㪌§Ò¹ËÅÒÂÃٻẺઋ¹ _XT_OSC : ǧ¨Ã¡íÒà¹Ô´ÊÑ­­Ò³¹Ò́¡ÒẺ¤ÃÔʵÍÅ ¤ÇÒÁ¶Õäè Á‹à¡Ô¹ 4MHz _HS_OSC : ǧ¨Ã¡íÒà¹Ô´ÊÑ­­Ò³¹Ò́¡ÒẺ¤ÃÔʵÍŤÇÒÁ¶ÕÊè §Ù (4MHz ¢Ö¹é ä») _LP_OSC : ǧ¨Ã¡íÒà¹Ô´ÊÑ­­Ò³¹Ò́¡ÒẺ¤ÃÔʵÍžÅѧ§Ò¹µèÒí _RC_OSC : ǧ¨Ã¡íÒà¹Ô´ÊÑ­­Ò³¹Ò́¡ÒẺ㪌µÇÑ µŒÒ¹·Ò¹áÅеÑÇà¡çº»ÃÐ¨Ø _LVP_ON,_LVP_OFF : àÍç¹àÍàºÔÅáÅдÔÊàÍàºÔÅ¡ÒÃâ»Ãá¡ÃÁ´ŒÇÂáç´Ñ¹µèÒí _WDT_ON ,_WDT_OFF : àÍç¹àÍàºÔÅáÅдÔÊàÍàºÔÅÇ͵ª´Íç ¡ä·àÁÍÏ _CP_OFF,_CP_ALL : àÅ×Í¡ËÃ×ÍäÁ‹àÅ×Í¡»‡Í§¡Ñ¹¡ÒÃ͋ҹ¢ŒÍÁÙÅ (Code Protect) ¡ÒáíÒ˹´¤‹ÒàËÅ‹Ò¹Õ¢é ¹Öé ÍÂÙ¡‹ ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏᵋÅеÑÇ ÃÒÂÅÐàÍÕ´à¾ÔÁè àµÔÁÊÒÁÒöÈÖ¡ÉÒ䴌¨Ò¡´ÒµŒÒªÕµ¢Í§äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏàºÍϹ¹Ñé æ


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 37

ÊíÒËÃѺ¡ÒÃÊÌҧâ»Ãà¨ç¡µ¤ÃÑ駹Õéãˌ¡íÒ˹´¤‹Ò໚¹¤‹ÒµÑ駵Œ¹ â´Â¤ÅÔ¡·Õè»Ø†Á ÊíÒËÃѺ PIC16F887 ¤‹ÒµÑ§é µŒ¹ ¤×Í l

ǧ¨ÃÊÑ­­Ò³¹Ò́¡Ò໚¹áºº HS_OSC

l

´ÔÊàÍàºÔÅÇ͵ª´Íç ¡ä·àÁÍÏ (WDT_OFF)

l

´ÔÊàÍàºÔÅ¡ÒÃâ»Ãá¡ÃÁẺ㪌áç´Ñ¹µèÒí (LVP_OFF)

ÊíÒËÃѺ»ØÁ†

໚¹¡ÒáàÅÔ¡à¤Ã×Íè §ËÁÒ¶١˹ŒÒËÑÇ¢ŒÍ Config ·Ñ§é ËÁ´

àÁ×Íè ¡íÒ˹´¤‹ÒàÃÕºÌÍÂãˌ¡´»ØÁ† OK ໚¹ÍѹàÊÃç¨ÊÔ¹é ¡ÒÃÊÌҧä¿Åâ»Ãà¨ç¡µãËÁ‹ â»Ãá¡ÃÁ¨ÐÊÌҧä¿Å Project01.c ¢Ö¹é ÁÒãˌâ´ÂÍѵâ¹ÁÑµÔ (3) ËÅѧ¨Ò¡ÊÌҧä¿Åâ»Ãà¨ç¡µàÃÕºÌÍÂáÅŒÇ ¼ÙŒãªŒ§Ò¹ÂѧÊÒÁÒöᡌ䢢ŒÍÁÙÅ㹡Òà ¡íÒ˹´¤‹Òµ‹Ò§æ ¢Í§ä¿Åâ»Ãà¨ç¡µä´Œ â´Âä»·Õàè Á¹Ù Project à Edit Project ËÃ×ͤÅÔ¡äͤ͹ ·Õáè ¶ºà¤Ã×Íè §Á×ͧ͢â»Ãá¡ÃÁ mikroC ᵋ¶ŒÒµŒÍ§¡ÒÃà»ÅÕè¹àºÍϢͧäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏËÃ×ͤÇÒÁ¶ÕèÊÑ­­Ò³ ¹Ò́¡Ò ¼Ù¾Œ ² Ñ ¹Òâ»Ãá¡ÃÁÊÒÁÒö¡íÒ˹´¤‹Ò䴌¨Ò¡á·ç» Project Setup ·ÕÁè ÁØ Å‹Ò§´ŒÒ¹«ŒÒ¢ͧ˹ŒÒµ‹Ò§ ËÅѡ䴌·Ñ¹·Õ

(4) ËÅѧ¨Ò¡á¡Œä¢à»ÅÕè¹á»Å§¢ŒÍÁÙÅã¹ä¿Åâ»Ãà¨ç¡µáÅŒÇ ¨ÐµŒÍ§ºÑ¹·Ö¡¡ÒÃá¡Œä¢ â´ÂàÅ×Í¡·Õàè Á¹Ù Project à Save Project ËÃ×ͤÅÔ¡·Õäè ͤ͹ ¡ç䴌


38 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

2.2 â¤Ã§ÊÌҧâ»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺ mikroC ¤ÍÁä¾àÅÍÏ ÃٻẺÀÒÉÒ C ¢Í§ mikroC ÁÕ¤ÇÒÁ¤ÅŒÒ¤ÅÖ§¡Ñºâ»Ãá¡ÃÁÀÒÉÒ C Áҵðҹ ´Ñ§¹Ñ¹é ¼Ù·Œ ÁÕè ¾Õ ¹×é °Ò¹ÀÒÉÒ C ÁÒºŒÒ§áÅŒÇ ÊÒÁÒö·íÒ¤ÇÒÁࢌÒã¨áÅÐà¢Õ¹â»Ãá¡ÃÁ¡Ñº mikroC 䴌·¹Ñ ·Õ ʋǹ·Õµè ͌ §à¾ÔÁè àµÔÁ¤×Í ¡Ò÷íÒ¤ÇÒÁࢌÒ㨡Ѻ¡Ò÷íÒ§Ò¹¢Í§âÁ´ÙÅ·Ò§ÎÒÏ´áÇÏÀÒÂã¹äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ã¹ÃÙ»·Õè 2-1 ໚¹¡ÒÃáÊ´§Ê‹Ç¹»ÃСͺ¢Í§â»Ãá¡ÃÁÀÒÉÒ C ·Õ¾è ² Ñ ¹Òº¹ mikroC »ÃСͺ ´ŒÇ µÑÇá»ÃáÅСÒûÃСÒȵÑÇá»Ã, ¿˜§¡ª¹Ñè â»Ãá¡ÃÁËÅÑ¡ (main program), ¿˜§¡ª¹Ñè â»Ãá¡ÃÁ, ªØ´¤íÒÊѧè , ä¿Åàδà´ÍÏ (Header) áÅФíÒ͸ԺÒÂâ»Ãá¡ÃÁ (comment) ᵋÍ‹ҧ¹ŒÍ·ÕèÊش㹡ÒÃà¢Õ¹â»Ãá¡ÃÁ¨ÐµŒÍ§ÁÕâ»Ãá¡ÃÁËÅÑ¡ «Öè§ÁÕÅѡɳдѧ¹Õé void main () {

ªØ´¤íÒÊѧè ; }

ä¿Å Header ¤íÒ͸ԺÒÂâ»Ãá¡ÃÁ µÑÇá»Ã â»Ãá¡ÃÁËÅÑ¡ ¤íÒ͸ԺÒÂâ»Ãá¡ÃÁ ¤íÒÊѧè

ÃÙ»·Õè 2-1 áÊ´§Ê‹Ç¹»ÃСͺ¢Í§â»Ãá¡ÃÁÀÒÉÒ C ·Õ¾è ² Ñ ¹Òº¹ mikroC


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 39

2.3 ¡ÒûÃСÒȵÑÇá»Ãã¹ÀÒÉÒ C ÊíÒËÃѺ mikroC ¡ÒûÃСÒȵÑÇá»Ãã¹â»Ãá¡ÃÁÀÒÉÒ C ¢Í§ mikroC àËÁ×͹¡ÑºÀÒÉÒ C ·ÑÇè ä» ÁÕû٠Ẻ´Ñ§¹Õé Type VAR_name;

â´Â

Type

¤×Í ª¹Ô´¢Í§¢ŒÍÁÙÅ·Õµè ͌ §¡ÒáíÒ˹´¤‹Ò

VAR_name

¤×Í ª×Íè µÑÇá»Ã·Õ»è ÃСÒÈ

ª×Íè ·Õãè ªŒÊÒí ËÃѺµÑÇá»ÃËÃ×ͪ×Íè ¿˜§¡ª¹Ñè ã¹ÀÒÉÒ C ÊÒÁÒöµÑ§é ª×Íè ·ÕÁè ·Õ §Ñé µÑÇ˹ѧÊ×ÍËÃ×͵ÑÇàÅ¢ ÃÇÁÍÂÙ´‹ nj ¡ѹ䴌 ᵋµÇÑ ÍÑ¡ÉÃ¢Ö¹é µŒ¹ ¨ÐµŒÍ§à»š¹µÑÇ˹ѧÊ×Í ËÃ×͵ÑÇ¢Õ´àʌ¹ãµŒ _ ෋ҹѹé ઋ¹ _dat1 , mark64 ໚¹µŒ¹

2.4 ª¹Ô´µÑÇàÅ¢¢Í§µÑÇá»Ã ª¹Ô´µÑÇàÅ¢¢Í§µÑÇá»Ã·Õè㪌¡Ñº mikroC ¤ÍÁä¾àÅÍÏ ẋ§à»š¹ÃٻẺËÅÑ¡æ 䴌 2 Ẻ¤×Í µÑÇá»ÃàÅ¢¨íҹǹàµçÁ (integral) áÅеÑÇá»ÃẺàÅ¢·È¹ÔÂÁ (floating)

2.4.1 µÑÇá»ÃẺ¨íҹǹàµçÁ (integral) µÑÇá»ÃẺ¨íҹǹàµçÁÁÕ¤Òí ʧǹ·Õ¹è Òí ÁÒ㪌äÁ‹ÁÒ¡ »ÃСͺ´ŒÇ char , int, short, long, signed áÅÐ unsigned â´Âá¡áÂШíҹǹª‹Ç§µÑÇàÅ¢´Ñ§áÊ´§ã¹µÒÃÒ§·Õè 2-1 ¡ÒáíÒ˹´¤‹ÒºÒ§ ¤‹Ò ઋ¹ short ËÃ×Í signed char ÁÕ¢¹Ò´µÑ§é ᵋ -128 ¶Ö§¤‹Ò 127 ෋ҡѹ ´Ñ§¹Ñ¹é àÁ×Íè ¹íÒä»ãªŒ§Ò¹¨ÃÔ§ 㪌 short à¾Õ§Í‹ҧà´ÕÂÇ¡ç䴌 ª¹Ô´¢Í§µÑÇá»Ã

¨íҹǹ亵

char ËÃ×Í unsigned char signed char short ËÃ×Í signed short int unsigned shortËÃ×Í unsigned short int

intËÃ×Í signed int unsigned

ËÃ×Í unsigned

int

longËÃ×Í signed long int unsigned long ËÃ×Í unsigned long int

1 1 1 1 2 2 4 4

ª‹Ç§µÑÇàÅ¢ 0 ¶Ö§ 255 -128 ¶Ö§ 127 -128 ¶Ö§ 127 0 ¶Ö§ 255 -32,768 ¶Ö§ 32,767 0 ¶Ö§ 65,535 -2,147,483,648 ¶Ö§ 2,147,483,647

0 ¶Ö§ 4,294,967,295

µÒÃÒ§·Õè 2-1áÊ´§ª¹Ô´¢Í§µÑÇá»ÃẺ¨íҹǹàµçÁ


40 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

µÑÇÍ‹ҧ·Õè 2-1 int a,b,c,d ; unsigned height; long size;

¹Í¡¨Ò¡¹Õµé ÇÑ á»Ã·Õ¡è Òí ˹´äÁ‹¨Òí ໚¹µŒÍ§ÍÂÙ㋠¹ºÃ÷Ѵà´ÕÂǡѹ¡ç䴌 ઋ¹ unsigned short tmp, caracter, loop, loop2;

«Ö§è ÊÒÁÒö·íҧҹ䴌ઋ¹à´ÕÂǡѺ unsigned short

tmp, caracter, loop, loop2;

2.4.2 µÑÇá»ÃẺµÑÇàÅ¢·È¹ÔÂÁ (Floating point) ÊíÒËÃѺâ»Ãá¡ÃÁ mikroC äÁ‹Çҋ ¨Ð໚¹µÑÇá»ÃẺ Float , double ËÃ×͵ÑÇá»ÃẺ long double ¨ÐÁÕ¨Òí ¹Ç¹äºµà·‹Ò¡Ñ¹¤×Í 4 亵 áÅÐÁÕ¢¹Ò´ª‹Ç§µÑÇàÅ¢µÑ§ é ᵋ s1.17549435082 x 10-38 ¶Ö§ s6.80564774407 x 1038 ´Ñ§¹Ñ¹é µÑÇá»ÃẺµÑÇàÅ¢·È¹ÔÂÁÍÒ¨¨Ð㪌ªÍ×è Float à¾Õ§Í‹ҧà´ÕÂÇ¡ç䴌


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 41

2.5 ª¹Ô´¢Í§µÑÇá»Ã 2.5.1 µÑÇá»ÃẺNjҧ (Void) µÑÇá»ÃẺ Void ໚¹µÑÇá»ÃẺ¾ÔàÈÉ â´Â㪌ÃкØ˹ŒÒ¿˜§¡ª¹Ñè àÁ×Íè äÁ‹µÍŒ §¡ÒÃãˌÁ¡Õ Òä׹¤‹Ò µÑÇÍ‹ҧ·Õè 2-2 void print_temp(char temp) { Lcd_Out_Cp(“Temperature:”); Lcd_Out_Cp(temp); Lcd_Chr_Cp(223);

// degree character

Lcd_Chr_Cp(‘C’); }

¨Ò¡µÑÇÍ‹ҧ໚¹¿˜§¡ª¹Ñè áÊ´§¤‹Ò¢ŒÍÁÙÅ·Õâè Á´ÙÅ LCD «Ö§è ¿˜§¡ª¹Ñè ¹Õäé Á‹Á¡Õ Òä׹¤‹Ò¡ÅѺ

2.5.2 µÑÇá»ÃẺÍÐàÏ µÑÇá»ÃÍÐàϤÍ× ¡ÅØÁ‹ ¢Í§µÑÇá»Ã·ÕÁè ªÕ ¹Ô´¢ŒÍÁÙÅ໚¹áººà´ÕÂǡѹ áÅШѴÇÒ§ã¹áÍ´à´ÃÊ·Õàè ÃÕ§ µ‹Í¡Ñ¹ â´ÂÁÕû٠Ẻ¡ÒûÃСÒȵÑÇá»Ã´Ñ§¹Õé type name[size] ;

â´Â·Õè

type name size

¡ÒÃà¡çº¢ŒÍÁÙÅ

¤×Í ª¹Ô´¢ŒÍÁÙŢͧµÑÇá»ÃÍÐàÏ ¤×Í ª×Íè ¢Í§µÑÇá»ÃÍÐàÏ ¤×Í ¤‹ÒµÑÇàÅ¢·Õãè ªŒ¡Òí ˹´¨íҹǹÍÐàÏ char A[7]; A[0] A[1] A[2] A[3] A[4] A[5] A[6]

¡ÒÃà¡çº¢ŒÍÁÙÅ

(¡) ÍÐàÏ 1 ÁÔµÔ int A[6][4] ; A[0][2]

A[0][3]

A[1][1]

A[1][2]

A[1][3]

A[2][0]

A[2][1]

A[2][2]

A[2][3]

A[3][0]

A[3][1]

A[3][2]

A[3][3]

A[4][0]

A[3][1]

A[3][2]

A[3][3]

A[5][0]

A[4][1]

A[4][2]

A[4][3]

(¢) ÍÐàÏ 2 ÁÔµÔ

ÃÙ»·Õè 2-1 áÊ´§ÃٻẺ¢Í§ÍÐàÏ 1, 2 áÅÐ 3 ÁÔµÔ

¢ŒÍ ÁÙÅ

A[0][1]

A[1][0]

¡ÒÃà¡çº¢ŒÍÁÙÅ

¡ÒÃà¡çº¢ŒÍÁÙÅ

A[0][0]

¡Ò Ãà¡ çº

¡ÒÃà¡çº¢ŒÍÁÙÅ

(¤) ÍÐàÏ 3 ÁÔµÔ


42 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

àÁ×Íè µŒÍ§¡ÒÃàÃÕ¡㪌§Ò¹¨ÐµŒÍ§ÃкصÇÑ ªÕµé Òí á˹‹§·ÕÃè ÐºØ â´ÂàÃÔÁè ¹ÑºµíÒá˹‹§ááµÑ§é ᵋ 0 仨¹ ¶Ö§µíÒá˹‹§ size -1 ´Ñ§¹Ñ¹é ¶ŒÒ¡íÒ˹´µÑÇá»Ã int digit[5] ;

¤‹Ò digit ¨ÐÁÕÊÁÒªÔ¡¤×Í digit[0] ,digit[1] , digit[2], digit[3], digit[4]

µÑÇÍ‹ҧ·Õè 2-3 char i ; char digit[9];

// reserve 9 byte

void main() { for (i=0;i<9;i++)

// Loop 9 times

{ digit[i] = 0xff ;

// Load Memory with 0xff

} }

¨Ò¡µÑÇÍ‹ҧâ»Ãá¡ÃÁ â»Ãá¡ÃÁ¨Ð¨Í§¾×¹é ·ÕÊè Òí ËÃѺà¡çº¢ŒÍÁÙÅänj 9 µíÒá˹‹§ ÊíÒËÃѺµÑÇá»Ã digit áÅŒÇ ãªŒ¤Òí Êѧè for à¾×Íè ǹÅÙ»¨íҹǹ 9 ¤Ãѧé à¾×Íè »‡Í¹¤‹Ò 0xFF ãˌ¡ºÑ ÊÁÒªÔ¡¢Í§µÑÇá»ÃÍÐàÏ·§Ñé ËÁ´

2.5.2.1 ¡ÒáíÒ˹´¤‹ÒàÃÔÁè µŒ¹ãˌ¡ºÑ µÑÇá»ÃÍÐàÏ mikroC ¨ÐÂÍÁãˌ»‡Í¹¤‹Ò¢ŒÍÁÙÅ䴌·Ñ¹·ÕàÁ×èÍÁÕ¡ÒûÃСÒȤ‹ÒµÑÇá»Ã¤ÃÑé§áá ¹Í¡¨Ò¡¹Õé¡Òà »ÃСÒȤ‹ÒµÑÇá»ÃÍÒ¨¨Ð¨Í§¾×¹é ·ÕËè ¹‹Ç¤ÇÒÁ¨íÒ´ŒÇ¡ÒÃÃкآ¹Ò´Å§ä»â´ÂµÃ§ËÃ×Íànj¹Ç‹Ò§änj¡äç ´Œ ´Ñ§µÑÇÍ‹ҧµ‹Í仹Õé µÑÇÍ‹ҧ·Õè 2-4 char digit[10]= {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};

໚¹¡ÒÃà¡çº¤‹Ò¢ŒÍÁÙŢͧ LED µÑÇàÅ¢ 7 ʋǹänj·µÕè ÇÑ á»Ã digit ·ÕÁè ¡Õ ÒÃÊíÒÃͧ¾×¹é ·Õäè nj 10 áÍ´à´ÃÊ char digit[]= {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};

໚¹¡ÒûÃСÒȵÑÇá»Ãà¾×Íè à¡çº¤‹Ò¢ŒÍÁÙŢͧ LED µÑÇàÅ¢ 7 ʋǹänj·µÕè ÇÑ á»Ã digit ઋ¹¡Ñ¹ áµ‹Ç¸Ô ¹Õ äÕé Á‹ÁÕ ¡ÒÃÃкآ¹Ò´¢Í§¾×¹é ·Õè ÊíÒËÃѺ¼Å¡Ò÷íÒ§Ò¹¹Ñ¹é àËÁ×͹¡Ñ¹·Ø¡»ÃСÒÃ


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 43

2.5.3 µÑÇá»ÃÍÐàÏẺËÅÒÂÁÔµÔ ã¹ºÒ§¤ÃÑ§é ¡ÒÃà¡çº¢ŒÍÁÙÅ ¢ŒÍÁÙÅÍÒ¨¨ÐÍÂÙ㋠¹ÃÙ»¢Í§µÒÃÒ§ ¡ÒÃÇÒ§¢ŒÍÁÙÅã¹ÍÐàÏÁµÔ àÔ ´ÕÂÇ¡Òà ¨Ñ´ÇÒ§áÅÐࢌҶ֧¢ŒÍÁÙÅÍÒ¨¨Ð·íÒ䴌ÂÒ¡ ¡Òèͧ¾×é¹·ÕèÍÐàϨ֧ÊÒÁÒö¨Í§¾×é¹·Õè໚¹áººÊͧÁÔµÔ䴌 ËÃ×Í 3 ÁÔµäÔ ´Œ ´Ñ§áÊ´§ã¹ÃÙ»·Õè 2-1 µÑÇÍ‹ҧ·Õè 2-5 char i,j ; char digit[5][15] ; void main() { for (i=0;i<9;i++)

// Loop 9 times

{ for (j=0;j<5;j++) digit[i][j] = 0xff ;

// Load Memory with 0xff

} }

â»Ãá¡ÃÁ¹Õàé »š¹µÑÇÍ‹ҧâ»Ãá¡ÃÁà¡çº¤‹Ò¢ŒÍÁÙÅŧ㹵ÑÇá»ÃÍÐàÏẺ 2 ÁÔµÔ

2.5.4 µÑÇá»Ãª¹Ô´¾Í¹àµÍÏ (Pointer Variables) µÑÇá»Ã¾ÍÂàµÍÏ·íÒ˹ŒÒ·Õèà¡çº¤‹ÒáÍ´à´ÃʢͧµÑÇá»Ã·ÕèªÕéµíÒá˹‹§ à¾×èÍࢌҶ֧˹‹Ç¤ÇÒÁ¨íÒ ·Ò§ÍŒÍÁ â´ÂÁÕû٠Ẻ¡ÒûÃСÒȤ‹ÒµÑÇá»Ã´Ñ§¹Õé Type

â´Â

*name ; Type name

¤×Í ª¹Ô´¢Í§¢ŒÍÁÙŢͧµÑÇá»Ã¾Í¹àµÍÏ ¤×Í ª×Íè ¢Í§µÑÇá»Ã¾Í¹àµÍÏ

¹Í¡¨Ò¡¹Õé¡ÒÃÃкØáÍ´à´Ãʢͧ˹‹Ç¤ÇÒÁ¨íÒËÃ×ÍÃÕ¨ÔÊàµÍÏ·Õè໚¹¤‹ÒµÑÇá»Ã¹Ñé¹ÊÒÁÒö㪌 ÊÑ­Åѡɳ & ¹íÒ˹ŒÒµÑÇá»Ãà¾×Íè ÃкØáÍ´à´ÃʢͧµÑÇá»Ã¹Ñ¹é æ 䴌 ઋ¹ a = &PORTB ;

µÑÇá»Ã a Áդҋ ෋ҡѺ 0x06 «Ö§è ¡ç¤Í× ¤‹ÒáÍ´à´Ãʢͧ PORTB ¹Ñ¹è àͧ


44 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

µÑÇÍ‹ҧ·Õè 2-6 char *DAT ; void main() { PORTB = 0 ; PORTC = 0 ; DAT = &PORTB ; *DAT = 0x90 ; DAT = DAT++ ; *DAT = 0x80;

//

¡íÒ˹´¤‹ÒµÑÇá»Ã໚¹µÑÇá»ÃẺ¾ÍÂàµÍÏ

¡íÒ˹´¤‹ÒÃÕ¨ÊÔ àµÍÏ PORTB = 0 // ¡íÒ˹´¤‹ÒÃÕ¨Ê Ô àµÍÏ PORTC = 0 // µÑÇá»Ã DAT à¡çº¤‹ÒáÍ´à´Ãʢͧ PORTB «Ö§è ෋ҡѺ 0x06 // ʋ§¤‹Ò¢ŒÍÁÙÅä»ÂѧÃÕ¨Ê Ô àµÍÏ·¶Õè ¡Ù ªÕ´é nj µÑÇá»Ã DAT (PORTB = 0x90) // à¾ÔÁ è ¤‹ÒµÑǪբé ͌ ÁÙÅ ·íÒãˌ¤Ò‹ ¢Í§µÑÇá»Ã DAT ෋ҡѺ 0x07 // ʋ§¤‹Ò¢ŒÍÁÙÅä»ÂѧÃÕ¨Ê Ô àµÍÏ·ªÕè ´Õé nj µÑÇá»Ã DAT (PORTC = 0x80) //

}

¨Ò¡â»Ãá¡ÃÁµÑÇÍ‹ҧ·Õè 2-6 ÊÒÁÒö¹íÒÁÒµÃǨÊͺà¾×Íè ´Ù¼Å¡Ò÷íÒ§Ò¹¢Í§µÑÇá»Ã¾Í¹àµÍÏ ´ŒÇ¡ÒëÔÁàÙ ÅàµÍÏËÃ×Í¡ÒèíÒÅͧ¡Ò÷íÒ§Ò¹º¹«Í¿µáÇÏ mikroC IDE 䴌 â´ÂÁÕ¢¹Ñé µÍ¹´Ñ§¹Õé (1) ແ´â»Ãá¡ÃÁ mikroC ÊÌҧâ»Ãà¨ç¡µªÍ×è Test_Pointer àÅ×Í¡äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ໚¹àºÍÏ PIC16F887 ¤ÇÒÁ¶ÕÊè ­ Ñ ­Ò³¹Ò́¡Ò 20MHz ¨Ò¡¹Ñ¹é ¤ÅÔ¡»ØÁ† à¾×Íè àÅ×Í¡¤‹Ò¤Í¹¿¡à»š¹¤‹Ò Áҵðҹ


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 45

(2) ¾ÔÁ¾«ÍÏÊâ»Ãá¡ÃÁµÒÁµÑÇÍ‹ҧ·Õè 2-6 ¨Ò¡¹Ñ¹é ·íÒ¡ÒäÍÁä¾Å ¨¹ä´Œä¿Å .hex

(3) ແ´Ë¹ŒÒµ‹Ò§ Debugger â´Â¤ÅÔ¡·Õ»è Á؆ ¨Ð»ÃÒ¡¯Ë¹ŒÒµ‹Ò§ Debugger ¢Ö¹é ÁÒ ã¹ª‹Í§ Select Variable from list ãˌàÅ×Í¡ª×Íè µÑÇá»Ã DAT, PORTB áÅÐ PORTC à¾ÔÁè ࢌҷÕËè ¹ŒÒµ‹Ò§ Debugger

(4) à»ÅÕÂè ¹ÃٻẺ¡ÒÃáÊ´§¼Å (Value) ãˌ໚¹µÑÇàÅ¢°Ò¹ÊԺˡ â´Â´ÑºàºÔŤÅÔ¡·Õªè Í×è µÑÇá»Ã (5) ·ÕËè ¹ŒÒµ‹Ò§ Debugger ¤ÅÔ¡·Õ»è Á؆ àÁ×Íè ¶Ö§ºÃ÷Ѵ

à¾×Íè Ãѹ¤íÒÊÑ§è ·ÕÅФíÒÊѧè áŌǴ٤ҋ µÑÇá»Ã·Õàè »ÅÕÂè ¹á»Å§

DAT = &PORTB ; µÑÇá»Ã

àÁ×Íè ¶Ö§ºÃ÷Ѵ *DAT

DAT Áդҋ ෋ҡѺ෋Òã´

= 0x90 ; Êѧࡵ¤‹Ò·Õè

PORTB à»ÅÕÂè ¹á»Å§ä»Í‹ҧäÃ

áÅÐàÁ×Íè ¶Ö§ºÃ÷Ѵ *DAT = 0x80; ÍÕ¡¤ÃÑ§é ¤‹Ò·Õàè »ÅÕÂè ¹á»Å§·íÒäÁ¶Ö§à¡Ô´¢Ö¹é ·Õè PORTC


46 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

2.6 ÃкºµÑÇàÅ¢·Õãè ªŒã¹ mikroC ÃкºàÅ¢°Ò¹·Õè㪌¡ÑºäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏÁÕÍÂً 4 ª¹Ô´ 䴌ᡋ µÑÇàÅ¢°Ò¹Êͧ, °Ò¹á»´, °Ò¹ÊÔº áÅаҹÊԺˡ «Ö§è 㹡ÒÃà¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ¢Í§ mikroC ¨ÐÁÕ¡ÒÃẋ§á¡ÃٻẺàÅ¢ °Ò¹â´Â㪌à¤Ã×Íè §ËÁÒ¹íÒ˹ŒÒµÑÇàÅ¢ ´Ñ§ÁÕÃÒÂÅÐàÍÕ´µ‹Í仹Õé 1. µÑÇàÅ¢°Ò¹Êͧ µÑÇàÅ¢·Õ¡è Òí ˹´ãˌ໚¹àÅ¢°Ò¹Êͧ¨Ð¢Ö¹é µŒ¹´ŒÇ 0b ËÃ×Í 0B ઋ¹ 0b00010001 â´ÂµÑÇàÅ¢¨Ð¡íÒ˹´ª¹Ô´à»š¹áºº short 2. µÑÇàÅ¢°Ò¹á»´ µÑÇàÅ¢·Õ¡è Òí ˹´à»š¹àÅ¢°Ò¹á»´ ¨Ð¢Ö¹é µŒ¹´ŒÇ o (µÑÇÍÑ¡ÉÃâÍ) ઋ¹ o777 â´ÂµÑÇàÅ¢¨Ð¡íÒ˹´ª¹Ô´à»š¹áºº int 3. µÑÇàÅ¢°Ò¹ÊÔº à¹×èͧ¨Ò¡àÅ¢°Ò¹ÊԺ໚¹¤‹ÒµÑÇàÅ¢Áҵðҹ ¨Ö§äÁ‹¨Òí ໚¹µŒÍ§ÁÕ¡Òà ¢Ö¹é µé¹´éǵÑÇÍÑ¡ÉÃËÃ×ÍÊÑ­Åѡɳìã´æ â´ÂÊÒÁÒö¡Ó˹´¤èҢͧàÅ¢°Ò¹ÊÔºä´éµ§éÑ áµè -2,147,483,648 ¶Ö§ 4,294,967,295 ¶ŒÒ¤‹Ò·Õ¡è Òí ˹´à¡Ô¹¡Ç‹Ò¹Õ¨é Ðᨌ§¢ŒÍ¼Ô´¾ÅÒ´ “Out of range” 4. µÑÇàÅ¢°Ò¹ÊԺˡ µÑÇàÅ¢°Ò¹ÊԺˡ¨Ð¢Ö¹é µŒ¹´ŒÇ 0x ËÃ×Í 0X ઋ¹ 0xF8, 0xCF30 â´ÂµÑÇàÅ¢°Ò¹ÊԺˡ¨Ð¡íÒ˹´ª¹Ô´µÑÇàŢ໚¹áºº unsigned int

2.7 ¡ÒÃࢌҶ֧¢ŒÍÁÙÅÃдѺºÔµ mikroC ¤ÍÁä¾àÅÍÏÂÍÁãˌ¼ãٌ ªŒ§Ò¹ÊÒÁÒöࢌҶ֧ÃÕ¨ÊÔ àµÍϵҋ §æ ã¹ÃдѺºÔµä´Œ â´Â㪌 . (¨Ø´) ¤Ñ¹è ÃÐËNjҧ¢ŒÍÁÙÅáŌǵÒÁ´ŒÇ Fn «Ö§è n ËÁÒ¶֧ºÔµ·Õµè ͌ §¡ÒÃࢌҶ֧ Áդҋ µÑ§é ᵋ 0 ¶Ö§ 7 ¶ŒÒµŒÍ§¡ÒáíÒ˹´ãˌ ºÔµ 0 ¢Í§ÃÕ¨ÊÔ àµÍÏ PORTC Áդҋ ෋ҡѺ 1 à¢Õ¹䴌໚¹ PORTC.F0 = 1 ;

¡ÒõÃǨÊͺ¤‹Ò ¨Ò¡ºÔµ 1 ¢Í§ÃÕ¨ÊÔ àµÍÏ PORTC NjÒÁդҋ ෋ҡѺ 1 ËÃ×ÍäÁ‹ ¶ŒÒ㪋 ãˌ PORTB ºÔµ 0 ෋ҡѺ 0 à¢Õ¹䴌໚¹ if (PORTC.F1 == 1)

PORTB.F0 = 0 ;

¹Í¡¨Ò¡¹ÕÊé Òí ËÃѺÃÕ¨ÊÔ àµÍÏÊÒí ¤Ñ­¼Ù㌠ªŒ§Ò¹ÊÒÁÒöÃкتÍ×è ¢Í§ºÔµ¹Ñ¹é æ 䴌àÅ (´ÙÃÒÂÅÐàÍÕ´ à·Õºà¤Õ§䴌¨Ò¡´ÒµŒÒªÕµ¢Í§äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC àºÍϹ¹Ñé æ ) ઋ¹ INTCON.TMR0IF = 0; ໚¹¡ÒÃà¤ÅÕÂÏá¿Å¡ÍÔ¹àµÍÏû Ñ µ¨Ò¡ä·àÁÍÏ 0 ADCON0.Go=1; ໚¹¡ÒÃàÃÔÁ è µŒ¹¡Ò÷íÒ§Ò¹¢Í§âÁ´ÙÅá»Å§ÊÑ­­Ò³ÍйÒÅ͡໚¹´Ô¨µÔ ÍÅ


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 47

2.8 ¤íÒÊѧè à¡ÕÂè ǡѺ¡ÒèѴ¡ÒâŒÍÁÙÅẺºÔµ ÊíÒËÃѺ¡ÒÃ㪌§Ò¹äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ·Õ赌ͧÁÕ¡ÒÃ͋ҹ¤‹Ò¢ŒÍÁÙÅẺºÔµà¢ŒÒÁÒ»ÃÐÁÇżŠ¨ÐµŒÍ§ÁÕ¤Òí Êѧè à¾×Íè ¨Ð¨Ñ´¡ÒâŒÍÁÙÅàËŋҹѹé ãˌÍÂÙ㋠¹ÃٻẺ·Õàè ËÁÒÐÊÁ¡‹Í¹¡ÒûÃÐÁÇżŠઋ¹ ¡ÒáíҨѴ ºÔµ·ÕÁè ÅÕ Í¨Ô¡ “1” ã¹µÑÇá»Ã·Õµè ͌ §¡Òà ¡ÒÃàÅ×Íè ¹ºÔµ¢ŒÍÁÙÅ ¡ÒáÅѺʶҹÐÅͨԡ¢Í§ºÔµ ໚¹µŒ¹

2.8.1 ¤íÒÊѧè á͹´ : & (AND) 㪌à¾×Íè à»ÃÕºà·Õº¢ŒÍÁÙÅ㹺Ե·Õµè ͌ §¡Òà ãˌ¼Å໚¹ “1” ¶ŒÒºÔµ·Õàè »ÃÕºà·Õº·Ñ§é ¤Ùà‹ »š¹ “1” ¹Í¡ ¹Ñ¹é ãˌ¼ÅÅѾ¸à»š¹ “0” ÁÕµÒÃÒ§¤ÇÒÁ¨ÃÔ§áÊ´§¡Ò÷íÒ§Ò¹´Ñ§¹Õé *

)

:

























»ÃÐ⪹¢Í§¡Òà & ¤×Í ãªŒ»ÃѺ¤‹ÒàÅ¢°Ò¹Êͧ㹺Ե·Õµè ͌ §¡ÒÃãˌ໚¹ “0” µÑÇÍ‹ҧ·Õè 2-7 int value; void main() { value = 0xAA55 & 0x00FF ; }

// value = 0x0055

¨Ò¡â»Ãá¡ÃÁ ¡Òà AND ¤‹Ò´ŒÇ 0x00FF ໚¹¡Ò÷íÒãˌ 8 ºÔµº¹¢Í§ 0xAA55 ¡ÅÒÂ໚¹ “0” ãˌàËÅ×Í ¤‹Ò¢ŒÍÁÙÅ੾ÒÐ 8 ºÔµÅ‹Ò§à·‹Ò¹Ñ¹é ¤×Í 0x0055 à¾×Íè ¹íÒä»ãªŒ»ÃÐ⪹µÍ‹ ä»


48 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

2.8.2 ¤íÒÊѧè | (OR) 㪌à¾×Íè à»ÃÕºà·Õº¢ŒÍÁÙÅ㹺Ե·Õµè ͌ §¡Òà â´Âãˌ¼ÅÅѾ¸à»š¹ “0” àÁ×Íè ºÔµ·Õ¹è Òí ÁÒà»ÃÕºà·Õº ·Ñ§é ¤Ùà‹ »š¹ “0” ¹Í¡¹Ñ¹é ãˌ¼ÅÅѾ¸à»š¹ 1 ÁÕµÒÃÒ§¤ÇÒÁ¨ÃÔ§áÊ´§¡Ò÷íÒ§Ò¹´Ñ§¹Õé *

)

:

























»ÃÐ⪹¢Í§¡Òà | ¤×Í »ÃѺ¤‹Ò㹺Ե·Õµè ͌ §¡ÒÃãˌ໚¹ “1” µÑÇÍ‹ҧ·Õè 2-8 char value ; void main() { value = 0b00110000 | 0b11000000 ; // value = 0b11110000 }

¨Ò¡â»Ãá¡ÃÁ ¡Òà OR ¤‹Òà¾×Íè ·íÒãˌÊͧºÔµº¹¢Í§¤‹Ò¢ŒÍÁÙÅ 0b00110000 ÁÕÅͨԡ໚¹ “1” äÁ‹Çҋ ¤‹Òà´ÔÁ ¢Í§ 2 ºÔµº¹¹Ñ¹é ¨Ð໚¹ “1” ËÃ×Í “0” ¼ÅÅѾ¸·äÕè ´Œ¤Í× 0b11110000

2.8.3 ¤íÒÊѧè ^ (XOR) 㪌à¾×Íè à»ÃÕºà·Õº¢ŒÍÁÙÅ㹺Ե·Õµè ͌ §¡Òà â´Âãˌ¼ÅÅѾ¸à»š¹ “0” àÁ×Íè ºÔµ·Õãè ªŒà»ÃÕºà·Õº໚¹ “0” ·Ñ§é ¤Ùˋ Ã×Í໚¹ “1” ·Ñ§é ¤Ù‹ ¹Í¡¨Ò¡¹Õ¨é Ðãˌ¼ÅÅѾ¸à»š¹ 1 ÁÕµÒÃÒ§¤ÇÒÁ¨ÃÔ§áÊ´§¡Ò÷íÒ§Ò¹´Ñ§¹Õé *

)

:

























»ÃÐ⪹¢Í§¡Òà ^¤×Í ãªŒ¡ÅѺ¢ŒÍÁÙÅàÅ¢°Ò¹Êͧ㹺Ե·Õµè ͌ §¡ÒÃ໚¹µÃ§¢ŒÒÁ àÁ×Íè ^ ´ŒÇ “1”


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 49

µÑÇÍ‹ҧ·Õè 2-9 void main(){ TRISB = 0x00 ; // while (1){ // PORTB = (PORTB ^ 0xFF) ; // delay_ms(500); // } }

Set PORTB ==> OUTPUT Loop Toggle PORTB Call Delay 500 ms

¨Ò¡â»Ãá¡ÃÁ ¶ŒÒµ‹Í¢Ò PORTB ࢌҡѺ LED ¨Ð·íÒãˌ LED µÔ´¡Ð¾ÃÔº´ŒÇ¤ÇÒÁ¶Õè 1Hz à¹×Íè §¨Ò¡¡Òà XOR ¤‹Òã´æ ´ŒÇ “1” ·Ñ§é 8 ºÔµ à¢Õ¹ÍÂÙ㋠¹ÃÙ»µÑÇàÅ¢°Ò¹ÊԺˡ¤×Í 0xFF ¤‹Ò¼ÅÅѾ¸·äÕè ´Œ¨Ð໚¹µÃ§¢ŒÒÁ

2.8.4 ¤íÒÊѧè ~ (NOT) 㪌à¾×èÍ¡ÅѺʶҹÐÅͨԡ¢Í§ºÔµ·Õ赌ͧ¡Òà ¶ŒÒºÔµà´ÔÁÁÕÅͨԡ໚¹ “1” ¨Ð¡ÅÒÂ໚¹ “0” áÅÐ ¶ŒÒÁÕÅͨԡ໚¹ “0” ¨Ð¡ÅÒÂ໚¹ “1” µÑÇÍ‹ҧ·Õè 2-10 void main() { PORTB = 0x00 ; TRISB = 0x00 ; while (1) { PORTB = (~PORTB) ; delay_ms(500); } }

// Clear PORTB // Set PORTB ==> OUTPUT // Loop // Toggle PORTB // Call Delay 500 ms

â»Ãá¡ÃÁ¹Õ¨é зíҧҹ䴌ઋ¹à´ÕÂǡѺ¤íÒÊѧè XOR â´Â¨Ð͋ҹ¤‹Ò¨Ò¡ PORTB ÍÍ¡ÁÒáŌǡÅѺÅͨԡãˌ ʶҹÐ໚¹µÃ§¡Ñ¹¢ŒÒÁ áŌÇʋ§¡ÅѺä»áÊ´§¼Å·Õè PORTB ÍÕ¡¤Ãѧé


50 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

2.8.5 ¤íÒÊѧè àÅ×Íè ¹¢ŒÍÁÙÅ Áմnj ¡ѹ 2 ¤íÒÊÑ§è ¤×Í << ໚¹¤íÒÊѧè à¾×Íè àÅ×Íè ¹ºÔµ¢ŒÍÁÙÅä»·Ò§«ŒÒ áŌÇá·¹ºÔµ´ŒÒ¹¢ÇÒÊØ´´ŒÇÂÅͨԡ “0” >> ໚¹¤íÒÊѧè à¾×Íè àÅ×Íè ¹ºÔµ¢ŒÍÁÙÅä»·Ò§¢ÇÒ áŌÇá·¹ºÔµ´ŒÒ¹«ŒÒÂÊØ´´ŒÇÂÅͨԡ “0” µÑÇÍ‹ҧ·Õè 2-11 char i ;

// variable set

void main() { PORTB = 0x00 ;

// Clear PORTB

TRISB = 0x00 ;

// Set PORTB ==> OUTPUT

while (1)

// Loop

{ PORTB = 0x01 ;

// Start PORTB with 00000001

for (i = 0;i<8;i++ )

// 8 Time loop

{ delay_ms(500);

// 500 ms delay

PORTB = PORTB<<1 ; // Shift left 1 bit } PORTB = 0x80 ;

// Start PORTB with 10000000

for (i=0;i<8;i++)

// 8 Time loop

{ delay_ms(500);

// 500 ms delay

PORTB = PORTB>>1 ; // Shift right 1 bit } } }

â»Ãá¡ÃÁ¹Õàé »š¹¡ÒÃÊÌҧä¿ÇÔ§è 仡ÅѺ·Õè PORTB â´ÂÍÒÈÑ¡ÒÃàÅ×Íè ¹¤Ãѧé ÅÐ 1 ºÔµ ÊÒÁÒöµÃǨÊͺ ¡Ò÷íҧҹ䴌 â´Â㪌¡ÒèíÒÅͧ¡Ò÷íÒ§Ò¹


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 51

2.9 ¤íÒÊÑ§è ¡ÒáÃзíÒ·Ò§Åͨԡ ¤íÒÊÑ觷Õè㪌¡ÃзíÒ·Ò§Åͨԡ¹Ñ鹨Ð㪌áÊ´§¼ÅÅѾ¸¢Í§¡ÒáÃзíÒÍÍ¡ÁÒNjÒ໚¹¨ÃÔ§ËÃ×Íà·ç¨ ËÃ×Í໚¹ “0” ËÃ×ÍäÁ‹ãª‹ “0” ෋ҹѹé â´ÂäÁ‹Ê¹ã¨¤‹Ò¢Í§¼ÅÅѾ¸·äÕè ´Œ¨Ò¡¡ÒáÃзíÒ·Ò§Åͨԡ¹Ñ¹é æ äÁ‹àËÁ×͹¡Ñº¡ÒèѴ¡ÒâŒÍÁÙÅÃдѺºÔµ â´Â¤íÒÊÑ§è ·Õãè ªŒ¡ºÑ ¡ÒáÃзíÒ·Ò§Åͨԡ»ÃСͺ仴ŒÇ¤íÒÊÑ§è ´Ñ§¹Õé && ( Logical AND) ãˌ¼ÅÅѾ¸à»š¹¨ÃÔ§ àÁ×Íè à§×Íè ¹ä¢·Õ¹è Òí ÁÒµÃǨÊͺ໚¹¨ÃÔ§·Ñ§é ¤Ù‹ || (Logical OR)

ãˌ¼ÅÅѾ¸à»š¹¨ÃÔ§àÁ×èÍà§×è͹ä¢ã´à§×è͹ä¢Ë¹Öè§à»š¹¨ÃÔ§

! (Logical Not)

ãˌ¼ÅÅѾ¸à»š¹¨ÃÔ§àÁ×Íè à§×Íè ¹ä¢à»š¹à·ç¨ áÅÐ ãˌ¼ÅÅѾ¸à»š¹à·ç¨àÁ×èÍà§×è͹ä¢à»š¹¨ÃÔ§

==

෋ҡѺ

!=

äÁ‹à·‹Ò¡Ñº

>

ÁÒ¡¡Ç‹Ò

<

¹ŒÍ¡NjÒ

>=

ÁÒ¡¡Ç‹ÒËÃ×Í෋ҡѺ

<=

¹ŒÍ¡NjÒËÃ×Í෋ҡѺ

µÑÇÍ‹ҧ·Õè 2-12 char sensor1 ,sensor2 ; // variable set void main() { TRISB =0x00 ; if ((sensor1>300)&&(sensor2>300)) { PORTB.F1 = 0 ; // RB1 = 0 PORTB.F0 = 1 ; // RB0 = 1 } }

¨Ò¡â»Ãá¡ÃÁ µÑÇá»Ã sensor1 áÅÐ sensor2 ¨ÐµŒÍ§Áդҋ ÁÒ¡¡Ç‹Ò 300 ¨Ö§¨Ð·íÒãˌà§×Íè ¹ä¢ if ໚¹¨ÃÔ§


52 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

µÑÇÍ‹ҧ·Õè 2-13 void main() { TRISB =0b11111100 ; // RB0,RB1 = Output while(1) { if ((PORTB.F2==0)||(PORTB.F3==0)) PORTB.F0 = 1 ; // RB0 = 1 else PORTB.F0 = 0 ; // RB0 = 0 } }

¨Ò¡â»Ãá¡ÃÁ ¶ŒÒ RB2 ËÃ×Í RB3 ºÔµã´ºÔµË¹Ö§è ËÃ×Í·Ñ§é ¤ÙÁ‹ ÅÕ Í¨Ô¡à»š¹ “0” ¢Ò RB0 ¨ÐÁÕÅͨԡ໚¹ “1” ᵋ¶ÒŒ RB2 ËÃ×Í RB3 ໚¹ “1” ·Ñ§é ¤Ù‹ RB0 ¨ÐÁÕÅͨԡ໚¹ “0”

2.10 ¤íÒÊÑ§è ¤íҹdz·Ò§¤³ÔµÈÒʵÏ mikroC àµÃÕÂÁ¤íÒÊÑ觷Õè㪌㹡Òäíҹdz·Ò§¤³ÔµÈÒʵÏàÍÒänjÍ‹ҧ¤Ãº¶ŒÇ¹ ·Ñé§ÃٻẺ¢Í§ ¤íÒÊÑ§è »¡µÔÊÒí ËÃѺ¡Òúǡ ź ¤Ù³ ËÒà áÅ¡ª¹Ñè ¾ÔàÈÉ·Ò§µÃÕ⡳ÁÔµÔ ´Ñ§¹Õé +

¤íÒÊÑ§è ºÇ¡ ( Addition)

-

¤íÒÊѧè ź ( Subtraction)

*

¤íÒÊÑ§è ¤Ù³ ( Multiplication)

/

¤íÒÊѧè ËÒà (Division)

%

¤íÒÊѧè ËÒÃẺàÍÒàÈÉ (äÁ‹ÊÒÁÒö㪌¡ºÑ µÑÇàÅ¢·Õàè »š¹·È¹ÔÂÁ䴌)

µÑÇÍ‹ҧ·Õè 2-14 value = 12 / 5 ;

//

¼ÅÅѾ¸à·‹Ò¡Ñº 2 (value ໚¹µÑÇá»ÃẺ char)

value = 6 * 3 / 4 ;

// ¼ÅÅѾ¸à·‹Ò¡Ñº 4 â´Â¨Ð¹íÒ 6 ¤Ù³¡Ñº 3 ¡‹Í¹ áŌÇËÒôŒÇ 4

value = 12.5 / 2.3;

//

value = 12.5 / 2.3;

// ¡Ã³Õ·Õè value ໚¹µÑÇá»ÃẺ float ¼ÅÅѾ¸Á¤ Õ Ò‹ ෋ҡѺ 5.43478

value = 12 % 4 ;

//

¼ÅÅѾ¸·äÕè ´Œ¤Í× 0 à¹×Íè §¨Ò¡ 12 ËÒôŒÇ 4 áŌÇäÁ‹àËÅ×ÍàÈÉ

value = 14 % 5;

//

¼ÅÅѾ¸·äÕè ´Œ¤Í× 4

value = -14 % 5;

//

¼ÅÅѾ¸·äÕè ´Œ¤Í× -4 à¹×Íè §¨Ò¡¤‹Òà¤Ã×Íè §ËÁÒ¨ФԴ¨Ò¡µÑǵѧé àÊÁÍ

¡Ã³Õ·Õè value ໚¹µÑÇá»ÃẺ char ¼ÅÅѾ¸Á¤Õ ҋ ෋ҡѺ 5


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

2.11 ¤íÒÊѧè à¾×Íè 㪌à¾ÔÁè ËÃ×ÍÅ´¤‹Ò ++

㪌ÊÒí ËÃѺ¡ÒÃà¾ÔÁè ¤‹Ò¢Ö¹é 1 ¤‹Ò (increment)

--

㪌ÊÒí ËÃѺ¡ÒÃÅ´¤‹Òŧ 1 ¤‹Ò (decrement)

µÑÇÍ‹ҧ·Õè 2-15 int j,k ; void main() { k = 4 ; while(1) { j = k++ ; } }

¨Ò¡â»Ãá¡ÃÁµÑÇÍ‹ҧ ËÅѧ¨Ò¡·íÒ¤íÒÊѧè j = k++ ¤‹Ò k Áդҋ ෋ҡѺ 5 ᵋ¤Ò‹ j Áդҋ ෋ҡѺ 4 à¹×Íè §¨Ò¡â»Ãá¡ÃÁ·íÒ§Ò¹ â´Âãˌ j = k ¡‹Í¹ ¨Ò¡¹Ñ¹é ¨Ö§¡ÃзíÒ¤íÒÊѧè k++

µÑÇÍ‹ҧ·Õè 2-16 int j,k ; void main() { k = 4 ; while(1){ j = ++k ; } }

¨Ò¡â»Ãá¡ÃÁµÑÇÍ‹ҧ ËÅѧ¨Ò¡·íÒ¤íÒÊѧè j = ++k ¤‹Ò k Áդҋ ෋ҡѺ 5 áÅФ‹Ò j ¡ç෋ҡѺ 5 ´ŒÇ à¹×Íè §¨Ò¡â»Ãá¡ÃÁ¨Ðà¾ÔÁè ¤‹Ò k ¡‹Í¹·Õ¨è йíÒä»à·‹Ò¡Ñº¤‹Ò¢Í§ j

INNOVATIVE EXPERIMENT

l 53


54 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 55

º··Õè 3

¡ÒÃà¢Õ¹â»Ãá¡ÃÁà¾×͵Դµ‹Í¾Íϵ¢Í§ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏã¹Í¹Ø¡ÃÁ PIC16F8xx ÁÕ¾Íϵãˌ㪌§Ò¹µÑ§é ᵋ 3 ¶Ö§ 5 ¾Íϵ ¨íҹǹ 20 ¶Ö§ 35 ºÔµ ¢Ö¹é ÍÂÙ¡‹ ºÑ àºÍϢͧäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ ´ŒÇ¤ÇÒÁÊÒÁÒö¢Í§¾Íϵ㹠PIC16F8xx ·ÕÊè ÒÁÒö ·íҧҹ䴌ËÅÒÂÍ‹ҧ ¨Ö§¨íÒ໚¹Í‹ҧÂÔ§è ·Õ¼è ãٌ ªŒ§Ò¹µŒÍ§·íÒ¤ÇÒÁࢌÒ㨶֧â¤Ã§ÊÌҧ·Ò§ÎÒÏ´áÇÏáÅСÒà ¡íÒ˹´ËÃ×ÍàÅ×Í¡¿˜§¡ª¹Ñè ¡Ò÷íÒ§Ò¹ãˌᡋ¢Ò¾ÍϵᵋÅТҴŒÇ¡Ãкǹ¡Ò÷ҧ«Í¿µáÇÏ ·Ñ§é ¹Õàé ¾×Íè ãˌ ÊÒÁÒö㪌§Ò¹¾Íϵ·Ñ§é ËÁ´¢Í§ PIC16F87x 䴌Í‹ҧÁÕ»ÃÐÊÔ·¸ÔÀÒ¾ÊÙ§ÊØ´

3.1 ¤ÇÒÁÊÒÁÒö㹡Òè‹Ò¡ÃÐáʢͧ¢Ò¾Íϵ ¤ÇÒÁÊÒÁÒö㹡Òè‹Ò¡ÃÐáÊàÍÒµ¾µØ ¢Í§¢Ò¾Íϵ¢Í§äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC16F8xx ·Õäè ¿ àÅÕÂé § +5V ¤×Í 25mA µ‹Í¢Ò·Ñ§é ¡ÃÐáÊ«Ô§¡áÅСÃÐáÊ«ÍÃÊ ã¹¢³Ð·Õ¡è ÃÐáÊàÍÒµ¾µØ ÃÇÁ·Ñ§é ËÁ´¶Ù¡¨íÒ¡Ñ´ änj·Õè 200mA ´Ñ§¹Ñ¹é 㹡ÒÃÍ͡Ẻà¾×Íè ¢ÑºâËÅ´·Ò§àÍÒµ¾µØ ¢Í§¢Ò¾ÍϵäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC µŒÍ§ÃÐÇѧàÃ×Íè §¡ÃÐáÊàÍÒµ¾µØ ÃÇÁ·Õäè Áâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ÊÒÁÒö¢Ñºä´Œ´ÇŒ  ã¹ÃÙ»·Õè 3-1 áÊ´§¡Òà ¢ÑºâËÅ´·Ñé§áºº¡ÃÐáÊ«Ô§¡áÅЫÍÃÊ +5V

PIC16F8xx

Isink

PB1

R1 LED1

PIC16F8xx PB1

R1

Isource

(¡) ¡ÒâѺâËÅ´·Ò§¾ÍϵàÍÒµ¾Øµ Ẻ¡ÃÐáÊ«Ô§¡

(¢) ¡ÒâѺâËÅ´·Ò§¾ÍϵàÍÒµ¾Øµ Ẻ¡ÃÐáÊ«ÍÏÊ

ÃÙ»·Õè 3-1 ¡ÒâѺâËÅ´¢Í§¾ÍϵàÍÒµ¾µØ ¢Í§äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC

LED1


56 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

PORTC

4+ 4+! 4,

 

TRISC

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

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

ÃÙ»·Õè 3-2 áÊ´§¡ÒáíÒ˹´·ÔÈ·Ò§¢Í§¢Ò¾Íϵ´ŒÇÂÃÕ¨ÊÔ àµÍÏ TRISC

3.2 ÃÕ¨ÊÔ àµÍÏ TRIS ¢Ò¾Íϵ¢Í§äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ÊÒÁÒö¡íÒ˹´ãˌ໚¹¢ÒÍÔ¹¾ØµËÃ×Í¢ÒàÍÒµ¾µØ ¡ç䴌 ¡ÒáíÒ˹´¢Òãˌ໚¹ÍÔ¹¾Øµ·íÒ䴌â´Â¡Òû‡Í¹Åͨԡ “1” ãˌ¡ÑºÃÕ¨ÔÊàµÍÏ TRIS 㹺Ե·Õ赌ͧ¡Òà ¡ÒáíÒ˹´¢Òãˌ໚¹àÍÒµ¾Øµ·íÒ䴌â´Â¡Òû‡Í¹Åͨԡ “0” ãˌ¡ÑºÃÕ¨ÔÊàµÍÏ TRIS 㹺Ե·Õ赌ͧ¡Òà ´Ñ§áÊ´§ã¹ÃÙ»·Õè 3-2

3.3 ¢Ò¾ÍϵÍйÒÅÍ¡áÅдԨµÔ ÍÅ ÊíÒËÃѺ¾Íϵ A áÅÐ ¾Íϵ E ¢Í§äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC16F874 ËÃ×Í 16F877(A) àÁ×Íè ¨‹ÒÂä¿ãˌ¤ÃÑé§áá¨Ð·íÒ˹ŒÒ·Õè໚¹¢ÒÍÔ¹¾ØµÍйÒÅÍ¡ ¡ÒùíÒ¢ÒàËŋҹÕé·íÒ˹ŒÒ·Õè໚¹ÍÔ¹¾Øµ´Ô¨ÔµÍÅ ËÃ×ÍàÍÒµ¾µØ ´Ô¨µÔ ÍÅ ¨ÐµŒÍ§ÁÕ¡ÒáíÒ˹´¤‹Ò·ÕÃè ¨Õ ÊÔ àµÍÏ ADCON1 àÊÕ¡‹Í¹ ã¹ÃÙ»·Õè 3-3 ¨ÐáÊ´§ºÔµ µ‹Ò§æ ã¹ÃÕ¨ÊÔ àµÍÏ ADCON1 ¨ÐáÊ´§¤‹ÒàÁ×Íè µŒÍ§¡ÒáíÒ˹´ãˌ¢Ò¾Íϵ·íÒ˹ŒÒ·Õàè »š¹¢Ò´Ô¨µÔ ÍÅËÃ×Í ÍйÒÅÍ¡ ÊíÒËÃѺ¡ÒáíÒ˹´ãˌ¢Ò¾Íϵ A áÅоÍϵ E ·Ñ§é ËÁ´à»š¹¢Ò´Ô¨µÔ ÍÅ ¨Ð»‡Í¹¤‹Ò 0x07 ËÃ×Í 0x06 ãˌ¡ºÑ ÃÕ¨ÊÔ àµÍÏ ADCON1 ÊíÒËÃѺ¾Íϵ A ¾Íϵ E áÅÐ ¾Íϵ B ¢Í§äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC16F887 àÁ×Íè ¨‹ÒÂä¿ ¤Ãѧé áá ¢Ò·Õàè »š¹¢ÒÍÔ¹¾ØµÍйÒÅÍ¡¨Ð¤§Ê¶Ò¹Ð໚¹¢ÒÍÔ¹¾ØµÍйÒÅÍ¡ÍÂً ¡ÒÃà»ÅÕÂè ¹á»Å§Ê¶Ò¹Ð ¢Í§¢Òãˌ໚¹ÍйÒÅÍ¡ËÃ×Í´Ô��µÔ ÍÅ ¨ÐµŒÍ§¡ÃзíÒ¼‹Ò¹ÃÕ¨ÊÔ àµÍÏ ANSEL áÅÐ ANSELH ÃÙ»·Õè 3-4 áÊ´§µíÒá˹‹§ºÔµ¢Í§ÃÕ¨ÊÔ àµÍÏ ANSEL à¾×Íè 㪌à»ÅÕÂè ¹á»Å§¢Ò¾Íϵ A áÅÐ ¾Íϵ E ʋǹÃÙ»·Õè 3-5 áÊ´§µíÒá˹‹§ºÔµã¹ÃÕ¨ÊÔ àµÍÏ ANSELH à¾×Íè 㪌¡Òí ˹´¤‹Ò´Ô¨µÔ ÍÅËÃ×ÍÍйÒÅÍ¡ãˌ¡ºÑ ¾Íϵ B


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

ºÔµ 7

ADCON1

ADFM

ºÔµ 6

ADCS2*

ºÔµ 5

ºÔµ 4

-

-

R/W -0

ºÔµ 3

ºÔµ 2

ºÔµ 1

l 57

ºÔµ 0

PCFG3

PCFG2

PCFG1

PCFG0

R/W -0

R/W -0

R/W -0

R/W -0

ºÔµ 7 - ADFM (A/D Result Format Select bit - ºÔµ 7) : ºÔµàÅ×Í¡ÃٻẺ¼ÅÅѾ¸¢Í§¡ÒÃá»Å§ÊÑ­­Ò³ “0” - àÅ×Í¡¼ÅÅѾ¸áººªÔ´«ŒÒ (left justified) ÁÕû٠Ẻ¢Í§¢ŒÍÁÙŴѧ¹Õé ¼ÅÅѾ¸ 10 ºÔµ 7

0 7 6 5

0

b b b b b b b b b b 0 0 0 0 0 0

ÃÕ¨ÊÔ àµÍÏ ADRESH

ÃÕ¨ÊÔ àµÍÏ ADRESL

“1” - àÅ×Í¡¼ÅÅѾ¸áººªÔ´¢ÇÒ (right justified) ÁÕû٠Ẻ¢Í§¢ŒÍÁÙŴѧ¹Õé ¼ÅÅѾ¸ 10 ºÔµ 7

2 1 0 7

0

      > > > > > > > > > >

ÃÕ¨ÊÔ àµÍÏ ADRESH

ÃÕ¨ÊÔ àµÍÏ ADRESL

ºÔµ 6 - ADCS2 (A/D Conversion Clock Select bits) : ºÔµàÅ×Í¡¤ÇÒÁ¶Õ¢è ͧÊÑ­­Ò³¹Ò́¡Ò·Õãè ªŒã¹¡Òà á»Å§ÊÑ­­Ò³ÍйÒÅ͡໚¹´Ô¨µÔ ÍÅ ºÔµ¹ÕÁé àÕ ©¾ÒÐã¹Í¹Ø¡ÃÁ PIC16F87xA µŒÍ§ãªŒÃNj Á¡ÑººÔµ ADCS1 áÅÐ ADCS0 ã¹ÃÕ¨ÊÔ àµÍÏ ADCON0 ADCS2 ADCS1 0 0 0 0 0 1 0 1 1 0 1 0 1 1 1 1

ADCS1 ¤ÇÒÁ¶Õ¢è ͧÊÑ­­Ò³¹Ò́¡Ò 0 ¤ÇÒÁ¶ÕÊè ­ Ñ ­Ò³¹Ò́¡ÒËÒà 2 (FOSC/2) 1 ¤ÇÒÁ¶ÕÊè ­ Ñ ­Ò³¹Ò́¡ÒËÒà 8 (FOSC/8) 0 ¤ÇÒÁ¶ÕÊè ­ Ñ ­Ò³¹Ò́¡ÒËÒà 32 (FOSC/32) 1 㪌¤ÇÒÁ¶ÕÊè ­ Ñ ­Ò³¹Ò́¡Ò¨Ò¡Ç§¨Ã RC (FRC) 0 ¤ÇÒÁ¶ÕÊè ­ Ñ ­Ò³¹Ò́¡ÒËÒà 4 (FOSC/4) 1 ¤ÇÒÁ¶ÕÊè ­ Ñ ­Ò³¹Ò́¡ÒËÒà 16 (FOSC/16) 0 ¤ÇÒÁ¶ÕÊè ­ Ñ ­Ò³¹Ò́¡ÒËÒà 64 (FOSC/64) 1 㪌¤ÇÒÁ¶ÕÊè ­ Ñ ­Ò³¹Ò́¡Ò¨Ò¡Ç§¨Ã RC (FRC)

ºÔµ 5 áÅÐ 4 : äÁ‹ãªŒ§Ò¹ ¡íÒ˹´à»š¹ “0”

ÃÙ»·Õè 3-3 áÊ´§ÃÒÂÅÐàÍÕ´¢Í§ÃÕ¨ÊÔ àµÍÏ ADCON1 ã¹äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC16F874/877(A) (Áյ͋ )


58 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

ºÔµ 3, 2, 1 áÅÐ 0- PCFG3, PCFG2, PCFG1, PCFG0 (A/D Port Configuration Control bits - ) : ºÔµ¡íÒ˹´¡Ò÷íÒ§Ò¹¢Í§¢Ò¾Íϵ·Õãè ªŒã¹âÁ´ÙÅá»Å§ÊÑ­­Ò³ÍйÒÅ͡໚¹´Ô¨µÔ ÍÅ ÁÕÃÒÂÅÐàÍÕ´ ´Ñ§¹Õé PCFG3:PCFG0

AN7

AN6

AN5

AN4

AN3

AN2

AN1

AN0

VREF+

VREF-

¨íҹǹÍÔ¹¾ØµÍйÒÅÍ¡ : ¨íҹǹÍÔ¹¾Øµáç´Ñ¹ÍŒÒ§ÍÔ§

0000

A

A

A

A

A

A

A

A

VDD

VSS

8:0

0001

A

A

A

A

VREF+

A

A

A

AN3

VSS

7:1

0010

D

D

D

A

A

A

A

A

VDD

VSS

5:0

0011

D

D

D

A

VREF+

A

A

A

AN3

VSS

4:1

0100

D

D

D

D

A

D

A

A

VDD

VSS

3:0

0101

D

D

D

D

VREF+

D

A

A

AN3

VSS

2:1

011x

D

D

D

D

D

D

D

D

-

-

0:0

1000

A

A

A

A

VREF+

VREF-

A

A

AN3

AN2

6:2

1001

D

D

A

A

A

A

A

A

VDD

VSS

6:0

1010

D

D

A

A

VREF+

A

A

A

AN3

VSS

5:1

1011

D

D

A

A

VREF+

VREF-

A

A

AN3

AN2

4:2

1100

D

D

D

A

VREF+

VREF-

A

A

AN3

AN2

3:2

1101

D

D

D

D

VREF+

VREF-

A

A

AN3

AN2

2:2

1110

D

D

D

D

D

D

D

A

VDD

VSS

1:0

1111

D

D

D

D

VREF+

VREF-

D

A

AN3

AN2

1:2

A ¤×Í ÍÔ¹¾ØµÍйÒÅÍ¡, D ¤×Í ¾Íϵ´Ô¨µÔ ÍÅ, VREF+ ¤×Íáç´Ñ¹ÍŒÒ§ÍÔ§¢ÒºÇ¡, VREF- ¤×Íáç´Ñ¹ÍŒÒ§ÍÔ§¢Òź, VDD ¤×Í ä¿àÅÕÂé §, VSS ¤×Í¡ÃÒÇ´, AN3 ¤×Í ¢ÒÍÔ¹¾ØµÍйÒÅÍ¡ª‹Í§ 3 áÅÐ AN2 ¤×Í ¢ÒÍÔ¹¾ØµÍйÒÅÍ¡ª‹Í§ 2

ÃÙ»·Õè 3-3 áÊ´§ÃÒÂÅÐàÍÕ´¢Í§ÃÕ¨ÊÔ àµÍÏ ADCON1 ã¹äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC16F874/877(A) (¨º)


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 59

ÃÕ¨ÊÔ àµÍÏ ANSEL ໚¹ÃÕ¨ÊÔ àµÍÏàÅ×͡˹ŒÒ·Õãè ¹¡Ò÷íÒ§Ò¹¢Í§¢Ò¾Íϵ RA0 ¶Ö§ RA3, RA5 áÅÐ RE0 ¶Ö§ RE2 NjҵŒÍ§¡Òà ãˌ໚¹¢ÒÍÔ¹¾ØµÍйÒÅÍ¡ËÃ×ÍÍÔ¹¾ØµàÍÒµ¾µØ ´Ô¨µÔ ÍÅ â´Â¤‹ÒµÑ§é µŒ¹¨Ð¡íÒ˹´ãˌ·Òí §Ò¹à»š¹ÍÔ¹¾ØµÍйÒÅÍ¡

ºÔµ 7

ANSEL

ºÔµ 6

ºÔµ 5

ºÔµ 4

ºÔµ 3

ºÔµ 2

ºÔµ 1

ºÔµ 0

ANS7

ANS6

ANS5

ANS4

ANS3

ANS2

ANS1

ANS0

RE2 R/W -1

RE1 R/W -1

RE0 R/W -1

RA5 R/W -1

RA3 R/W -1

RA2 R/W -1

RA1 R/W -1

RA0 R/W -1

ºÔµ 7 ¶Ö§ 0 - ANS7 ¶Ö§ ANS0 : ºÔµàÅ×Í¡¡Ò÷íҧҹ໚¹ÍÔ¹¾ØµÍйÒÅÍ¡ “0” = ¡íÒ˹´à»š¹¢Ò¾Íϵ´Ô¨µÔ ÍÅ â´ÂÊÒÁÒö¡íÒ˹´à»š¹ÍÔ¹¾ØµËÃ×ÍàÍÒµ¾µØ ¡ç䴌 “1” = ¡íÒ˹´à»š¹¢Ò¾ÍϵÍÔ¹¾ØµÍйÒÅÍ¡ (໚¹¤‹ÒàÃÔÁè µŒ¹àÁ×Íè ÃÕ૵) àÁ×Íè àÅ×Í¡ãˌ¢ÒàËŋҹÕàé »š¹ÍÔ¹¾ØµÍйÒÅÍ¡ ¨Ð໚¹¡ÒáàÅÔ¡¡Ò÷íÒ§Ò¹ã¹Ê‹Ç¹´Ô¨µÔ Íŷѧé ËÁ´ ¹Í¡¨Ò¡ ¹Ñ¹é µŒÍ§¡íÒ˹´¤‹Ò¢Í§ÃÕ¨ÊÔ àµÍÏ TRIS ãˌ໚¹ÍÔ¹¾Øµ´ŒÇÂ

ÃÙ»·Õè 3-4 ÃÒÂÅÐàÍÕ´µíÒá˹‹§ºÔµ¢Í§ÃÕ¨ÊÔ àµÍÏ ANSEL ã¹äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC16F887

ÃÕ¨ÊÔ àµÍÏ ANSELH ໚¹ÃÕ¨ÊÔ àµÍÏàÅ×͡˹ŒÒ·Õãè ¹¡Ò÷íÒ§Ò¹¢Í§¢Ò¾Íϵ RB0 ¶Ö§ RB5 NjҵŒÍ§¡ÒÃãˌ໚¹¢ÒÍÔ¹¾ØµÍйÒÅÍ¡ ËÃ×ÍÍÔ¹¾ØµàÍÒµ¾µØ ´Ô¨µÔ ÍÅ â´Â¤‹ÒµÑ§é µŒ¹¨Ð¡íÒ˹´ãˌ¢Ò¾ÍϵàËŋҹշé Òí §Ò¹à»š¹¢Ò¾ÍϵÍÔ¹¾ØµÍйÒÅÍ¡ ANSELH

.

ºÔµ 7

ºÔµ 6 -

ANS13

ANS12

ANS11

ANS10

ANS9

ANS8

X -0

X -0

RB5 R/W -1

RB0 R/W -1

RB4 R/W -1

RB1 R/W -1

RB3 R/W -1

RB2 R/W -1

-

ºÔµ 5

ºÔµ 4

ºÔµ 3

ºÔµ 2

ºÔµ 1

ºÔµ 0

ºÔµ 7 áÅÐ 6 - äÁ‹ãªŒ§Ò¹ ͋ҹ¤‹Ò䴌໚¹ 0 ºÔµ 5 ¶Ö§ 0 - ANS13 ¶Ö§ ANS8 : ºÔµàÅ×Í¡¡Ò÷íҧҹ໚¹ÍÔ¹¾ØµÍйÒÅÍ¡ª‹Í§ AN13 ¶Ö§ AN8 “0” = ¡íÒ˹´à»š¹¢Ò¾Íϵ´Ô¨µÔ ÍÅ â´ÂÊÒÁÒö¡íÒ˹´à»š¹ÍÔ¹¾ØµËÃ×ÍàÍÒµ¾µØ ¡ç䴌 “1” = ¡íÒ˹´à»š¹¢Ò¾ÍϵÍÔ¹¾ØµÍйÒÅÍ¡ (໚¹¤‹ÒàÃÔÁè µŒ¹àÁ×Íè ÃÕ૵) àÁ×Íè àÅ×Í¡ãˌ¢ÒàËŋҹÕàé »š¹ÍÔ¹¾ØµÍйÒÅÍ¡ ¨Ð໚¹¡ÒáàÅÔ¡¡Ò÷íÒ§Ò¹ã¹Ê‹Ç¹´Ô¨µÔ Íŷѧé ËÁ´ ¹Í¡¨Ò¡ ¹Ñ¹é µŒÍ§¡íÒ˹´¤‹Ò¢Í§ÃÕ¨ÊÔ àµÍÏ TRIS ãˌ໚¹ÍÔ¹¾Øµ´ŒÇÂ

ÃÙ»·Õè 3-5 ÃÒÂÅÐàÍÕ´µíÒá˹‹§ºÔµ¢Í§ÃÕ¨ÊÔ àµÍÏ ANSEL ã¹äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC16F887


60 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

ÃÙ»·Õè 3-6 ǧ¨ÃÀÒÂ㹢ͧ¢Ò RA4 à·Õº¤ÇÒÁᵡµ‹Ò§¡Ñº¢ÒÍÔ¹¾ØµàÍÒµ¾µØ ¢ÒÍ×¹è æ

3.4 ¢Ò¾Íϵ RA4 : ¾ÍϵẺà´Ã¹à»´ (open drain) ¢Ò¾Íϵâ´Â·ÑÇè 仢ͧäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC16F877 ¨Ð໚¹áºº¾Ùª¾ÙÅ ÊÒÁÒö¨‹Ò¡ÃÐáÊ ä´Œ·§Ñé «Ô§¡áÅЫÍÃÊ Â¡ànj¹¢Ò RA4 ¨Ð໚¹¢ÒàÍÒµ¾µØ Ẻà´Ã¹à»´ ÊÒÁÒö¨‹Ò¡ÃÐáÊ«Ô§¡ä´Œà¾Õ§Í‹ҧ à´ÕÂÇà·‹Ò¹Ñ¹é ¶ŒÒµŒÍ§¡ÒÃãˌÁÅÕ Í¨Ô¡ “1” ¨ÐµŒÍ§µ‹ÍµÑǵŒÒ¹·Ò¹¾ÙÅÍѾãˌ¡ºÑ ¢Ò¹Õé ã¹ÃÙ»·Õè 3-6 áÊ´§ÅѡɳÐǧ¨ÃÀÒÂ㹢ͧ¢Ò RA4 à·Õº¡Ñº¢Ò¾ÍϵÍ×¹è æ


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 61

»¯ÔºµÑ ¡Ô Ò÷Õè 1 ¢Ñº LED ·Õ¾è Íϵ C ¡ÒÃàª×Íè Áµ‹Í·Ò§ÎÒÏ´áÇÏ 㪌ǧ¨Ãã¹ÃÙ»·Õè L1-1 㹡Ò÷´Åͧ ÊÒÁÒö㪌äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏàºÍÏ PIC16F877 ËÃ×Í PIC16F887 ¡ç䴌 ᵋµÍŒ §àÅ×Í¡ãˌ¶¡Ù µŒÍ§àÁ×Íè ·íÒ¡ÒÃÊÌҧä¿Åâ»Ãà¨ç¡µ´ÇŒ  l

l

Ëҡ㪌ºÍÏ´·´Åͧ NX-877 plus II ãˌµÍ‹ ǧ¨ÃµÒÁÃÙ»·Õè L1-1 áŌÇàÅ×Í¡äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ໚¹ PIC16F877

¢Ñ鹵͹¡Ò÷´Åͧ L1.1 ແ´â»Ãá¡ÃÁ mikroC ÊÌҧä¿Åâ»Ãà¨ç¡µ â´ÂàÅ×Í¡äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏáÅФÇÒÁ¶ÕÊè ­ Ñ ­Ò³¹Ò́¡Ò´Ñ§¹Õé l

¡Ã³Õ㪌ºÍÏ´ NX-877 plus II àÅ×Í¡ PIC16F877 ËÃ×Í PIC16F877A ¤ÇÒÁ¶ÕÊè ­ Ñ ­Ò³¹Ò́¡Ò 20MHz +5V

R11 10k

11 Vcc 1

MCLR

RC7 26 RC6 25

SW1 Reset

RC5

24

RC4 23 RC3

18

RC2 17 RC1 16 RC0

12

15

21+$.&%%

R1-R8 510

GND OSC1

OSC2

13

14

C1 22pF

XTAL1 20MHz

C2 22pF

ÃÙ»·Õè L1-1 ǧ¨Ã·´Êͺ¡Ò÷íÒ§Ò¹¢Í§¾ÍϵàÍÒµ¾µØ ´ŒÇÂâ»Ãá¡ÃÁÀÒÉÒ C â´Â㪌 mikroC ¤ÍÁä¾àÅÍÏ


62 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

void main() { TRISC=0x00; while(1) { PORTC=0x00; Delay_ms(500); PORTC=0xFF; Delay_ms(500); } }

// Set PORTC ==> output // loop // LED OFF // LED ON

¡Ò÷íÒ§Ò¹¢Í§â»Ãá¡ÃÁ â»Ãá¡ÃÁ¡íÒ˹´ãˌ¾Íϵ C ໚¹àÍÒµ¾µØ â´Â¡Òû‡Í¹¤‹Ò 0 ãˌ¡ºÑ ·Ø¡ºÔµ¢Í§ÃÕ¨ÊÔ àµÍÏ TRISC ¨Ò¡¹Ñ¹é »‡Í¹¤‹Ò 0x00 ãˌ¡ºÑ PORTC à¾×Íè ãˌ LED ·Õµè ͋ ÍÂÙ¡‹ ºÑ ¾Íϵ C ·Ñ§é ËÁ´´ÑºáÅÐ˹‹Ç§àÇÅÒ áŌǻ‡Í¹¤‹Ò FF à¾×Íè ãˌ LED ·Ñ§é ËÁ´·Õµè ͋ ÍÂÙ¡‹ ºÑ ¾Íϵ C µÔ´ÊNjҧ ·íÒÍ‹ҧ¹ÕÊé ÅѺ¡Ñ¹ä»Í‹ҧµ‹Íà¹×Íè §

â»Ãá¡ÃÁ·Õè L1-1 ä¿Å PortTest01.c â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺ mikroC à¾×èÍ·´Êͺ¡Ò÷íÒ§Ò¹ ໚¹¾ÍϵàÍÒµ¾µØ ¢Í§¾Íϵ C ¢Í§äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC16F877/887 L1.2 ÊÌҧâ»Ãà¨ç¡µªÍ×è PortTest01 áÅоÔÁ¾â»Ãá¡ÃÁÀÒÉÒ C µÒÁâ»Ãá¡ÃÁ·Õè L1-1 L1.3 ·íÒ¡ÒäÍÁä¾ÅáÅдÒǹâËÅ´â»Ãá¡ÃÁà¾×Íè ·´Êͺ¼ÅÅѾ¸¡Ò÷íÒ§Ò¹ L1.4 Ãѹâ»Ãá¡ÃÁ ¨ÐàËç¹ LED ·Õ·è §Ñé ËÁ´·Õ¾è Íϵ C ¡Ð¾ÃÔºµÔ´áÅдѺÊÅѺ¡Ñ¹ä»Í‹ҧµ‹Íà¹×Íè § L1.5 ᡌä¢â»Ãá¡ÃÁµÒÁ·Õáè Ê´§ã¹â»Ãá¡ÃÁ·Õè L1-2 µÑ§é ª×Íè â»Ãà¨ç¡µà»š¹ PortTest02.c ·íÒ¡ÒäÍÁä¾ÅáÅдÒǹ âËÅ´â»Ãá¡ÃÁà¾×Íè ·´Êͺ¼ÅÅѾ¸¡Ò÷íÒ§Ò¹ LED ·Õ¾è Íϵ C ¨Ð¡Ð¾ÃÔºàËÁ×͹¡Ñºâ»Ãá¡ÃÁ·Õè L1-1 Ëҡᵋã¹â»ÃááÁ¨Ð㪌¤Œ Òí Êѧè NOT (~ ) ࢌÒÁÒ ª‹ÇÂ㹡ÒÃÊÅѺºÔµ¢ŒÍÁÙÅá·¹ void main() { TRISC=0x00; PORTC=0xAA; while(1) { PORTC=~PORTC; Delay_ms(500); } }

// Set PORTC ==> output // Set Start LED // loop // Toggle

¡Ò÷íÒ§Ò¹¢Í§â»Ãá¡ÃÁ â»Ãá¡ÃÁ·íÒ§Ò¹àËÁ×͹¡Ñºâ»Ãá¡ÃÁ·Õè L1-1 à¾Õ§à»ÅÕÂè ¹ÃٻẺ¡ÒÃÊÅѺ¤‹Ò´ŒÇ¡ÒÃ㪌¤Òí Êѧè NOT á·¹

â»Ãá¡ÃÁ·Õè L1-1 ä¿Å PortTest02.c â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺ mikroC à¾×Íè ·´Êͺ¡Ò÷íÒ§Ò¹¢Í§ ¾ÍϵàÍÒµ¾µØ ¢Í§äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC16F877/887


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹ void main() { TRISC.F0=0; while(1) { PORTC.F0=1; Delay_ms(200); PORTC.F0=0; Delay_ms(200); } }

l 63

// RC0 ==> Output // Loop // LED on // LED off

¡Ò÷íÒ§Ò¹¢Í§â»Ãá¡ÃÁ â»Ãá¡ÃÁ¹Õ¨é ÐÊѧè ãˌ LED ·Õºè µÔ RC0 ¡Ð¾ÃÔºà¾Õ§ËÅÍ´à´ÕÂÇ

â»Ãá¡ÃÁ·Õè L1-3 ä¿Å PortTest03.c â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺ mikroC à¾×Íè ·´Êͺ¡Ò÷íÒ§Ò¹¢Í§ ¾ÍϵàÍÒµ¾µØ ¢Í§äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC16F877/887 â´ÂàÅ×Í¡¾Íϵ·Õµè ͌ §¡ÒâѺâËÅ´à¾Õ§ ºÔµà´ÕÂÇ ¹Ñ¹è ¤×Í RC0 ã¹ mikroC ¤ÍÁä¾àÅÍϼãٌ ªŒ§Ò¹ÊÒÁÒöࢌҶ֧ÃÕ¨ÊÔ àµÍϵҋ §æ ã¹ÃдѺºÔµä´Œ â´Â㪌 à¤Ã×Íè §ËÁÒ . (¨Ø´) ¤Ñ¹è ÃÐËNjҧ¢ŒÍÁÙÅáŌǵÒÁ´ŒÇ¿˜§¡ª¹Ñè Fn â´Â·Õè n ËÁÒ¶֧ ºÔµ·Õµè ͌ §¡Òà ࢌҶ֧ Áդҋ ÃÐËNjҧ 0 ¶Ö§ 7 L1.6 à¢Õ¹â»Ãá¡ÃÁãˌ LED ·Õµè Òí á˹‹§ RC0 ¡Ð¾ÃÔºà¾Õ§ËÅÍ´à´ÕÂÇ â´Âà¢Õ¹â»Ãá¡ÃÁ䴌´§Ñ â»Ãá¡ÃÁ·Õè L1-3 ¡íÒ˹´ª×Íè â»Ãà¨ç¡µà»š¹ PortTest03.c ·íÒ¡ÒäÍÁä¾ÅáÅдÒǹâËÅ´â»Ãá¡ÃÁà¾×Íè ·´Êͺ¼ÅÅѾ¸¡Ò÷íÒ§Ò¹ L1.7 ÊíÒËÃѺâ»Ãá¡ÃÁ·Õè L1-4 ໚¹¡ÒâѺ LED ã¹ÃٻẺ·ÕËè ÅÒ¡ËÅÒÂÁÒ¡¢Ö¹é â´Â㪌¤Òí ÊÑ§è ·Ò§¤³ÔµÈÒʵÏáÅÐ Åͨԡ·Õè mikroC ¤ÍÁä¾àÅÍÏàµÃÕÂÁänjãˌ ãˌ·Òí ¡ÒÃÊÌҧâ»Ãà¨ç¡µ PortTest04 áÅŒÇ áÅоÔÁ¾â»Ãá¡ÃÁÀÒÉÒ C µÒÁâ»Ãá¡ÃÁ·Õè L1-1 L1.8 ·íÒ¡ÒäÍÁä¾ÅáÅдÒǹâËÅ´â»Ãá¡ÃÁà¾×Íè ·´Êͺ¼ÅÅѾ¸¡Ò÷íÒ§Ò¹


64 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

char i,j; void main() { TRISC=0x00; // PORTC ==> Output while(1) // Loop { // **** Running and fill LED **** for (i=0;i<5;i++) { PORTC=0x01; for (j=1;j<8;j++) { PORTC|=(PORTC*2); Delay_ms(200);} // Use OR for fill "1" for (j=1;j<8;j++) { PORTC&=(PORTC/2); Delay_ms(200);} // Use AND for fill "0" } // **** Running LED From Right to Left **** for (i=0;i<5;i++) { PORTC=0x01; for (j=1;j<8;j++) { PORTC*=2;Delay_ms(200); } // Use Multiply for Rotate Left // **** Running LED From Left to Right **** for (j=1;j<8;j++) { PORTC/=2;Delay_ms(200); } // Use Division for Rotate Right } // **** Blinking LED **** PORTC=0x55; for (i=0;i<10;i++) { PORTC^=0xFF;Delay_ms(200); } } }

// Use XOR For Toggle bit

¡Ò÷íÒ§Ò¹¢Í§â»Ãá¡ÃÁ â»Ãá¡ÃÁ¹Õ¡é Òí ˹´ÃٻẺä¿ÇÔ§è LED ËÅÒÂÃٻẺâ´ÂàÍÒ¤íÒÊÑ§è ¡ÒèѴ¡ÒâŒÍÁÙÅÃдѺºÔµáÅФíÒÊÑ§è ¤Ù³áÅÐËÒÃࢌÒÁÒª‹Ç â´Â (1) 㪌¤Òí ÊÑ§è ¤Ù³¤‹Ò´ŒÇ 2 à¾×Íè àÅ×Íè ¹ºÔµ¢ŒÍÁÙÅä»·Ò§«ŒÒ (2) ¤íÒÊѧè ËÒôŒÇÂÊͧ à¾×Íè àÅ×Íè ¹ºÔµ¢ŒÍÁÙÅä»·Ò§¢ÇÒ (3) ¡Òà OR ¢ŒÍÁÙÅ´ŒÇ¼Ťٳ໚¹¡ÒÃà¾ÔÁè Åͨԡ “1” ãˌ¡ºÑ ¢ŒÍÁÙÅà´ÔÁ â´Â㪌û٠Ẻ·ÕÇè ҋ ¢ŒÍÁÙÅ ºÔµã´¶Ù¡ OR ´ŒÇ “1” ¨Ð໚¹ “1” àÊÁÍ (4) ¡Òà AND ´ŒÇ¼ÅËÒà ໚¹¡ÒÃà¾ÔÁè Åͨԡ “0” ãˌ¡ºÑ ¢ŒÍÁÙÅ ºÔµã´¶Ù¡ AND ´ŒÇ “0” ¨Ð໚¹ “0” (5) ¤íÒÊѧè XOR ໚¹¡ÒáÅѺʶҹÐÅͨԡ¢Í§ºÔµ·Õµè ͌ §¡ÒáÅѺʶҹРËÅÑ¡¡Òä×Í ºÔµã´¶Ù¡ XOR ´ŒÇ “1” ºÔµ¹Ñ¹é ¨Ð¶Ù¡¡ÅѺÅͨԡ áµ��¶ÒŒ ºÔµã´¶Ù¡ XOR ´ŒÇ “0” ¤‹Ò¨Ð໚¹¤‹Òà´ÔÁ

â»Ãá¡ÃÁ·Õè L1-4 ä¿Å PortTest04.c â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺ mikroC à¾×Íè ·´Êͺ¡Ò÷íÒ§Ò¹¢Í§ ¾ÍϵàÍÒµ¾µØ ¢Í§äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC16F877/887 â´Â㪌¤Òí ÊÑ§è ·Ò§¤³ÔµÈÒʵÏáÅÐÅͨԡ ÁÒª‹ÇÂ㹡ÒáíÒ˹´ÃٻẺ¡Ò÷íÒ§Ò¹ mikroC


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 65

»¯ÔºµÑ ¡Ô Ò÷Õè 2 ¡ÒÃ㪌§Ò¹¾Íϵ A áÅÐ E ¢Í§ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC16F877 ໚¹¾ÍϵàÍÒµ¾Øµ ¾Íϵ A áÅÐ E ¢Í§äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC16F877 ¨Ð·íÒ˹ŒÒ·Õàè »š¹ÍÔ¹¾ØµÍйÒÅÍ¡àÁ×Íè àÃÔÁè ¨‹ÒÂä¿ ãˌ¤Ãѧé áá ´Ñ§¹Ñ¹é ËÒ¡¹íÒÁÒ㪌§Ò¹à»š¹¾ÍϵàÍÒµ¾µØ ´Ô¨µÔ ÍÅ ¨Ö§µŒÍ§¡íÒ˹´¤‹Òãˌ¡ºÑ ÃÕ¨ÊÔ àµÍÏ ADCON1 ¡‹Í¹

¡ÒÃàª×Íè Áµ‹Í·Ò§ÎÒÏ´áÇÏ l

㪌ǧ¨Ãã¹ÃÙ»·Õè L2-1 㹡Ò÷´Åͧ

l

Ëҡ㪌ºÍÏ´·´Åͧ NX-877 plus II ãˌµÍ‹ ǧ¨ÃµÒÁÃÙ»·Õè L2-1 áŌÇàÅ×Í¡äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ໚¹ PIC16F877 +5V

R11 10k

11 Vcc 1

MCLR

RE2 10 RE1 9

SW1 Reset

RE0

8

+5V

RA5 7 RA4

1k

6

RA4

RA3 5

LED1

RA2 4 RA1 RA0

12

3 2

21+$.&%%

R1-R8 510

GND OSC1

OSC2

13

14

C1 22pF

XTAL1 20MHz

510

C2 22pF

ÃÙ»·Õè L2-1 ǧ¨ÃÊíÒËÃѺ¡Ò÷´Êͺ㪌§Ò¹¾Íϵ A áÅÐ E ໚¹¾ÍϵàÍÒµ¾µØ


66 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

int dat=0,dat1=0; void main() { ADCON1=0x07; TRISA=0x00; TRISE=0x00; PORTE=0x00; PORTA=0x00; while(1) { Delay_ms(100); if (dat<64) { PORTA=dat; dat++; } else { dat=0; if (dat1<8) { dat1++; PORTE=dat1; } else dat1=0; } } }

// // // //

Set PORTA and PORTE ==> Digital PORTA ==> Output PORTE ==> Output OFF all LED

// Loop // Fast speed delay // In Rang of PORTA // Show data on PORTA // Increment Data

// //

Overflow PORTA Check data for PORTE

// Show data on PORTE // Clear data aftr over flow

¡Ò÷íÒ§Ò¹¢Í§â»Ãá¡ÃÁ àÃÔÁè ¨Ò¡¡íÒ˹´ãˌ¢Ò¾Íϵ A áÅÐ E ໚¹¢Ò¾Íϵ´Ô¨µÔ ÍÅ¡‹Í¹ ¨Ò¡¹Ñ¹é ¨Ö§¡íÒ˹´ãˌ໚¹¢ÒàÍÒµ¾µØ ¡‹Í¹ ·Õ¨è ÐǹÅÙ»à¾×Íè ʋ§¤‹ÒÍÍ¡ä»áÊ´§¼ÅÂѧ LED ·Õµè ͋ ÍÂÙ¡‹ ºÑ ¢Ò¾Íϵ A áÅÐ E

â»Ãá¡ÃÁ·Õè L2-1 ä¿Å PortAEtest.c â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺ mikroC à¾×Íè ·´Åͧ㪌§Ò¹¾ÍϵA áÅÐ E ¢Í§äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC16F877 ໚¹¾ÍϵàÍÒµ¾µØ

¢Ñ鹵͹¡Ò÷´Åͧ L2.1 ແ´â»Ãá¡ÃÁ mikroC ÊÌҧä¿Åâ»Ãà¨ç¡µ â´ÂàÅ×Í¡äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏáÅФÇÒÁ¶ÕÊè ­ Ñ ­Ò³¹Ò́¡Ò´Ñ§¹Õé l

¡Ã³Õ㪌ºÍÏ´ NX-877 plus II àÅ×Í¡ PIC16F877 ËÃ×Í PIC16F877A ¤ÇÒÁ¶ÕÊè ­ Ñ ­Ò³¹Ò́¡Ò 20MHz

L2.2 ÊÌҧâ»Ãà¨ç¡µªÍ×è PortAEtest áÅоÔÁ¾â»Ãá¡ÃÁÀÒÉÒ C µÒÁâ»Ãá¡ÃÁ·Õè L2-1 L2.3 ·íÒ¡ÒäÍÁä¾ÅáÅдÒǹâËÅ´â»Ãá¡ÃÁà¾×Íè ·´Êͺ¼ÅÅѾ¸¡Ò÷íÒ§Ò¹ LED ¨ÐáÊ´§¤‹Ò¢Í§àÅ¢°Ò¹Êͧã¹áººà¾ÔÁè ¤‹Ò¢Ö¹é ä»Í‹ҧµ‹Íà¹×Íè § ᵋ·Õè LED ¢Í§¢Ò¾Íϵ RA4 ¨ÐäÁ‹ µÔ´ÊNjҧà¹×Íè §¨Ò¡ PIC16F877 ¢Ò RA4 ໚¹¢ÒẺ¤ÍÅàÅç¡àµÍÏແ´ ¶ŒÒËÒ¡µŒÍ§¡ÒÃãˌ LED ·Õè RA4 µÔ´ÊNjҧ ¨ÐµŒÍ§µ‹ÍµÑǵŒÒ¹·Ò¹¤‹Ò»ÃÐÁÒ³ 1k: ¾ÙÅÍÑ»àÍÒänj à¾×Íè ãˌ䴌àÍÒµ¾µØ Åͨԡ “1” ´Ñ§ã¹Ç§¡ÅÁ¢Í§ÃÙ»·Õè L2-1

mikroC


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 67

»¯ÔºµÑ ¡Ô Ò÷Õè 3 㪌§Ò¹¾Íϵ A áÅÐ B ¢Í§ PIC16F887 ໚¹¾ÍϵàÍÒµ¾µØ ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC16F887 ·Õ¾è Íϵ A áÅÐ B ¨Ð¶Ù¡¡íÒ˹´ãˌ໚¹¾ÍϵÍÔ¹¾ØµÍйÒÅÍ¡ àÊÁÍàÁ×èÍàÃÔèÁ¨‹ÒÂä¿ãˌ¤ÃÑé§áá ´Ñ§¹Ñé¹ã¹¡ÒùíÒÁÒ㪌§Ò¹à»š¹¾ÍϵàÍÒµ¾Øµ¨Ö§µŒÍ§ÁÕ¡ÒáíÒ˹´¤‹ÒãˌÃÕ¨ÔÊàµÍÏ ADCON1 ¡‹Í¹àÊÁÍ

¡ÒÃàª×Íè Áµ‹Í·Ò§ÎÒÏ´áÇÏ l

㪌ǧ¨Ãã¹ÃÙ»·Õè L3-1 㹡Ò÷´Åͧ

l

Ëҡ㪌ºÍÏ´·´Åͧ NX-877 plus II ãˌµÍ‹ ǧ¨ÃµÒÁÃÙ»·Õè L3-1 áŌÇà»ÅÕÂè ¹äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ໚¹ PIC16F887 +5V

R11 10k

11 Vcc 1

MCLR

RB7 26 RB6 25

SW1 Reset

RB5

24

RB4 23 RB3

18

RB2 17 RB1 16 RB0

21+$.&&% RA5

15

24

RA4 23 RA3

18

RA2 17 12

RA1 16 GND

RA0

OSC1

OSC2

13

14

C1 22pF

XTAL1 20MHz

15 R1-R14 510

C2 22pF

ÃÙ»·Õè L3-1 ǧ¨Ã·´Êͺ㪌§Ò¹¾Íϵ A áÅÐ B ¢Í§äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC16F887 ໚¹¾ÍϵàÍÒµ¾µØ


68 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

¢Ñ鹵͹¡Ò÷´Åͧ L3.1 ແ´â»Ãá¡ÃÁ mikroC ÊÌҧä¿Åâ»Ãà¨ç¡µ â´ÂàÅ×Í¡äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏáÅФÇÒÁ¶ÕÊè ­ Ñ ­Ò³¹Ò́¡Ò´Ñ§¹Õé l àÅ×Í¡äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ໚¹ PIC16F887 ¤ÇÒÁ¶ÕÊ è­ Ñ ­Ò³¹Ò́¡Ò 20MHz L3.2 ÊÌҧâ»Ãà¨ç¡µªÍ×è PortTest887 áÅоÔÁ¾â»Ãá¡ÃÁÀÒÉÒ C µÒÁâ»Ãá¡ÃÁ·Õè L3-1 L3.3 ·íÒ¡ÒäÍÁä¾ÅáÅдÒǹâËÅ´â»Ãá¡ÃÁà¾×Íè ·´Êͺ¼ÅÅѾ¸¡Ò÷íÒ§Ò¹ L3.4 Ãѹâ»Ãá¡ÃÁ LED ¨ÐµÔ´ÊNjҧ¨Ò¡µÃ§¡ÅÒ§á¡ÍÍ¡ä»·Ò§´ŒÒ¹¢ŒÒ§·Ñ§é Êͧ´ŒÒ¹áÅдѺ¨Ò¡µíÒá˹‹§µÃ§¡ÅÒ§ÍÍ¡ä» ¨Ð¾ºÇ‹Ò·Õ¢è Ò¾Íϵ RA4 ¢Í§äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC16F887 ¨ÐÊÒÁÒö¢Ñº LED 䴌 â´ÂäÁ‹µÍŒ §µ‹ÍµÑǵŒÒ¹ ·Ò¹¾ÙÅÍÑ»à¾ÔÁè ·Ñ§é ¹Õàé ¹×Íè §¨Ò¡¢Ò¾Íϵ·Ø¡¾Íϵ¢Í§äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC16F887 ໚¹áºº¾Øª¾ÙÅ int dat[14] = {0xC0,0x1E0,0x3F0,0x7F8,0xFFC,0x1FFE,0x3FFF, 0x3F3F,0x3E1F,0x3C0F,0x3807,0x3003,0x2001,0x00}; // LED pattern array int buff; char i; void main() { ANSEL=0b11100000; // PORTA ==> digital IO ANSELH=0x00; // PORTB ==> digital IO TRISA=0xC0; // PORTA ==> output TRISB=0x00; // PORTB ==> output while(1) // Loop { for (i=0;i<14;i++) // Loop for get data from array { buff=dat[i]&0x00FF; // Clear high byte PORTB=buff; // Show low byte ==> PORTB buff=(dat[i]&0x3F00)/0xff; // Clear low byte and shift right PORTA=buff; // show high byte ==> PORTA Delay_ms(100); // Fast Delay } } }

¡Ò÷íÒ§Ò¹¢Í§â»Ãá¡ÃÁ

â»Ãá¡ÃÁ¹ÕÊé ÌҧµÑÇá»Ã dat ¢Ö¹é ÁÒ໚¹µÑÇá»ÃẺÍÐàÏà¾×Íè à¡çº¤‹ÒÃٻẺ¡ÒÃáÊ´§¼Å¢Í§ LED ʋǹ ¡Ò÷íÒ§Ò¹ËÅÑ¡¨ÐàÃÔÁè ¨Ò¡¡íÒ˹´ãˌ¢Ò¾Íϵ A áÅÐ B ໚¹¾Íϵ´Ô¨µÔ ÍÅ â´Â¡íÒ˹´¤‹Ò·ÕÃè ¨Õ ÊÔ àµÍÏ ANSEL áÅÐ ANSELH ¨Ò¡¹Ñ¹é ¡íÒ˹´ãˌ໚¹¾ÍϵàÍÒµ¾µØ ¨Ò¡¹Ñ¹é ·íÒ¡ÒÃǹÅÙ»à¾×Íè ¹íÒ¤‹Ò¨Ò¡ÍÐàÏä»Ê‹§ÍÍ¡·Õ¢è Ò¾ÍϵàÍÒµ¾µØ â´ÂÁÕ¡ÒÃẋ§¢ŒÍÁÙÅ໚¹äºµÅҋ § 8 ºÔµÊíÒËÃѺáÊ´§¼Å·Õ¾è Íϵ B áÅÐ 6 ºÔµº¹ÊíÒËÃѺáÊ´§¼Å·Õ¾è Íϵ A

â»Ãá¡ÃÁ·Õè L3-1 ä¿Å PortTest887.c â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺ mikroC ¤ÍÁä¾àÅÍÏà¾×Íè 㪌§Ò¹¾Íϵ A áÅÐ B ¢Í§äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC16F887 ໚¹¾ÍϵàÍÒµ¾µØ mikroC


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 69

»¯ÔºµÑ ¡Ô Ò÷Õè 4 ·´Êͺ͋ҹ¤‹Ò¨Ò¡ÊÇÔµª ¡ÒÃàª×Íè Áµ‹Í·Ò§ÎÒÏ´áÇÏ ã¹ÃÙ»·Õè L4-1 áÊ´§Ç§¨Ã¢Í§äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC16F877 ·ÕÁè ¡Õ Òõ‹ÍÊÇÔµª¡´µÔ´»Å‹Í´Ѻࢌҷբè Ò RA4 áÅÐ RB0 áÅÐÁÕ¡Òõ‹ÍµÑǵŒÒ¹·Ò¹¾ÙÅÍÑ»¤‹Ò 10k: änjà¾×Íè ¡íÒ˹´ÊÀÒÇÐÅͨԡ “1” ã¹¢³Ð·Õäè Á‹Á‹ ¡Õ Òá´ ÊÇÔµª àÁ×Íè ÁÕ¡Òá´ÊÇÔµªà»ÅÕÂè ¹Ê¶Ò¹Ð໚¹Åͨԡ “0” ʋǹ·Õ¾è Íϵ C ¶Ù¡¡íÒ˹´ãˌ໚¹¾ÍϵàÍÒµ¾µØ µ‹Í¡Ñº LED à¾×Íè 㪌áÊ´§Ê¶Ò¹Ð¡Ò÷íÒ§Ò¹ l 㪌ǧ¨Ãã¹ÃÙ»·Õè L4-1 㹡Ò÷´Åͧ l Ëҡ㪌ºÍÏ´·´Åͧ NX-877 plus II ãˌµÍ ‹ ǧ¨ÃµÒÁÃÙ»·Õè L4-1 +5V

R10 10k

R9 10k

C3 0.1PF

11 32 Vcc 1

MCLR

SW1 RESET

R8

RC6 25

R7

24

R6

RC4 23

R5

RC5

R11 10k 33

RB0

RC3

S1 RB0

18

RC2 17 RC1 16

6 S2 RA4

RC7 26

RA4

12 31

RC0

15

21+$.&%%)

R4 R3 R2 R1 R1-R8 510:

GND OSC1 13

C1 22pF

OSC2 14

XTAL1 20MHz

C2 22pF

ÃÙ»·Õè L4-1 ǧ¨Ã·´Êͺ¡Ò÷íÒ§Ò¹¢Í§¾ÍϵÍÔ¹¾ØµàÍÒµ¾µØ ´Ô¨µÔ ÍŢͧäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC16F877 ÀÒÂ㵌¡ÒäǺ¤ØÁ´ŒÇÂâ»Ãá¡ÃÁÀÒÉÒ C ·Õãè ªŒ mikroC ¤ÍÁä¾àÅÍÏ㹡ÒþѲ¹Òâ»Ãá¡ÃÁ


70 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

void main() { TRISC.F0=0; while(1) PORTC.F0=~PORTB.F0; }

// RC0 ==> Output // LED at RC0 On when key press

¡Ò÷íÒ§Ò¹¢Í§â»Ãá¡ÃÁ â»Ãá¡ÃÁ¡íÒ˹´¢Ò RC0 ໚¹àÍÒµ¾µØ ʋǹ¢Ò RB0 ·Õãè ªŒà»š¹ÊÇÔµªà»š¹ÍÔ¹¾ØµÍÂÙዠŌÇàÁ×Íè ¨‹ÒÂ俤Ãѧé áá ¨Ò¡¹Ñ¹é ໚¹¡ÒÃǹÅÙ»à¾×Íè ¹íÒ¤‹Ò¨Ò¡¡Òá´ÊÇÔµª RB0 áÊ´§·Õè LED RC0 â´Â¨ÐáÊ´§´ŒÇ¡ÒáÅѺ¤‹Ò Åͨԡ à¾×Íè ãˌ¡´ÊÇÔµªáÅŒÇ LED µÔ´ÊNjҧ

â»Ãá¡ÃÁ·Õè L4-1 ä¿Å BasicSwitch01.c â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺ mikroC ¤ÍÁä¾àÅÍÏ à¾×Íè ·´Êͺ ¡Ò÷íÒ§Ò¹¢Í§¾ÍϵÍÔ¹¾ØµàÍÒµ¾µØ ¢Í§äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC16F877

¢Ñ鹵͹¡Ò÷´Åͧ L4.1 ແ´â»Ãá¡ÃÁ mikroC ÊÌҧä¿Åâ»Ãà¨ç¡µ â´ÂàÅ×Í¡äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏáÅФÇÒÁ¶ÕÊè ­ Ñ ­Ò³¹Ò́¡Ò´Ñ§¹Õé l

¡Ã³Õ㪌ºÍÏ´ NX-877 plus II àÅ×Í¡ PIC16F877 ËÃ×Í PIC16F877A ¤ÇÒÁ¶ÕÊè ­ Ñ ­Ò³¹Ò́¡Ò 20MHz

L4.2 ÊÌҧâ»Ãà¨ç¡µªÍ×è BasicSwitch01 áÅоÔÁ¾â»Ãá¡ÃÁÀÒÉÒ C µÒÁâ»Ãá¡ÃÁ·Õè L4-1 L4.3 ·íÒ¡ÒäÍÁä¾ÅáÅдÒǹâËÅ´â»Ãá¡ÃÁà¾×Íè ·´Êͺ¼ÅÅѾ¸¡Ò÷íÒ§Ò¹ L4.4 Ãѹâ»Ãá¡ÃÁ ·íÒ¡Òá´ÊÇÔµª S1 ·Õµè ͋ ¡Ñº¢Ò¾Íϵ RB0 Êѧࡵ¡Ò÷íÒ§Ò¹¢Í§ LED àÁ×Íè äÁ‹¡´ÊÇÔµª LED ·Õµè ͋ ÍÂÙ¡‹ ºÑ ¾Íϵ C ´Ñº·Ñ§é ËÁ´ àÁ×Íè ¡´ÊÇÔµª S1 ·Õ¢è Ò¾Íϵ RB0 ·íÒãˌ LED ·Õµè Òí á˹‹§¾Íϵ RC0 µÔ´ÊNjҧ L4.5 ÊÌҧâ»Ãà¨ç¡µªÍ×è BasicSwitch02 áÅоÔÁ¾â»Ãá¡ÃÁÀÒÉÒ C µÒÁâ»Ãá¡ÃÁ·Õè L4-2 L4.6 ·íÒ¡ÒäÍÁä¾ÅáÅдÒǹâËÅ´â»Ãá¡ÃÁà¾×Íè ·´Êͺ¼ÅÅѾ¸¡Ò÷íÒ§Ò¹ L4.7 Âѧ¤§ãªŒÇ§¨Ãã¹ÃÙ»·Õè L4-1 㹡Ò÷´Åͧ ¨Ò¡¹Ñ¹é ·íÒ¡ÒÃÃѹâ»Ãá¡ÃÁ ¡´ÊÇÔµª S1 ·Õµè ͋ ¡Ñº¢Ò¾Íϵ RB0 áÅÐ S2 ·Õµè ͋ ¡Ñº¾Íϵ RA4 Êѧࡵ¡Ò÷íÒ§Ò¹¢Í§ LED àÁ×Íè ¡´ÊÇÔµª S1 ·Õ¾è Íϵ RB0 ·íÒãˌ LED µÔ´ÊNjҧµÒÁ¤‹Ò¢Í§àÅ¢°Ò¹Êͧ â´Â¨Ðà¾ÔÁè ¤‹Ò¢Ö¹é àÁ×Íè ÁÕ¡Òà ¡´ÊÇÔµª S1 Í‹ҧµ‹Íà¹×Íè § àÁ×Íè ¡´ÊÇÔµª S2 ·Õ¾è Íϵ RA4 ·íÒãˌ LED µÔ´ÊNjҧµÒÁ¤‹Ò¢Í§àÅ¢°Ò¹Êͧ ᵋ¤Ò‹ ¨ÐŴŧ àÁ×Íè ÁÕ¡Òà ¡´ÊÇÔµª S2 Í‹ҧµ‹Íà¹×Íè §


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹ void main() { TRISC=0x00; while(1) { if (PORTB.F0==0) { PORTC++; Delay_ms(200); } if (PORTA.F4==0) { PORTC--; Delay_ms(200); } } }

l 71

// All PORTC ==> Output // // // //

Check SW at RB0 Increment PORTC Check SW at RA4 Decrement PORTC

¡Ò÷íÒ§Ò¹¢Í§â»Ãá¡ÃÁ â»Ãá¡ÃÁ¹Õé¨Ð㪌¾Íϵ C ·Ñ駾Íϵà¾×èÍáÊ´§¼Å¢ŒÍÁÙÅ·Õè䴌¨Ò¡¡Òá´ÊÇÔµª â´Â¶ŒÒ¡´ÊÇÔµªµíÒá˹‹§ RB0 ¨Ð໚¹¡ÒÃà¾ÔÁè ¤‹Ò¢ŒÍÁÙÅã¹ÃÕ¨ÊÔ àµÍÏ ¾Íϵ C ᵋ¶ÒŒ ¡´ÊÇÔµª RA4 ¨Ð໚¹¡ÒÃÅ´¤‹Ò¢ŒÍÁÙÅã¹ÃÕ¨ÊÔ àµÍϾÍϵ C ÃÐËNjҧ¡Òá´ÊÇÔµªáµ‹ÅФÃÑ§é ¨ÐÁÕ¡ÒÃ˹‹Ç§àÇÅÒ 200 ÁÔÅÅÔÇ¹Ô Ò·Õà¾×Íè Å´¼Å¨Ò¡Ë¹ŒÒÊÑÁ¼ÑʢͧÊÇÔµª ·Õ·è Òí ãˌà¡Ô´¤‹Ò¡´ÊÇÔµª«Òéí ¢Ö¹é ËÅÒÂ æ ¤Ãѧé

â»Ãá¡ÃÁ·Õè L4-2 ä¿Å BasicSwitch02.c â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺ mikroC ¤ÍÁä¾àÅÍÏ à¾×Íè ·´Êͺ ¡Ò÷íÒ§Ò¹¢Í§¾ÍϵÍÔ¹¾ØµàÍÒµ¾µØ ¢Í§äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC16F877 â´Â¡íÒ˹´ãˌ¾Íϵ A áÅÐ B ໚¹ÍÔ¹¾Øµ´Ô¨µÔ ÍŠʋǹ¾Íϵ C ໚¹àÍÒµ¾µØ ´Ô¨µÔ ÍÅ

INNOVATIVE EXPERIMENT


72 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 73

º··Õè 4

¡íÒà¹Ô´àÊÕ§´ŒÇ¿˜§¡ª¹Ñè Sound mikroC 䴌àµÃÕÂÁ¿˜§¡ªÑè¹ÊíÒËÃѺ¡ÒáíÒà¹Ô´ÊÑ­­Ò³àÊÕ§à¾×è͢ѺÍÍ¡·ÕèÅíÒ⾧ â´ÂÊÒÁÒö ¡íÒ˹´µíÒá˹‹§¢Ò¾Íϵ·Õ赌ͧ¡ÒÃàª×èÍÁµ‹Í䴌µÒÁµŒÍ§¡ÒÃ

4.1 ¿˜§¡ª¹Ñè Sound_Init ໚¹¿˜§¡ªÑè¹à¾×èÍ¡íÒ˹´µíÒá˹‹§¾ÍϵáÅкԵ·Õè㪌㹡ÒâѺÊÑ­­Ò³àÊÕ§ ÃٻẺ¡ÒÃ㪌§Ò¹¤íÒÊѧè void Sound_Init(unsigned short *port, unsigned short pin);

¾ÒÃÒÁÔàµÍÏ port

ËÁÒ¶֧ µíÒá˹‹§¾Íϵ·Õàè Å×͡㪌ÊÒí ËÃѺµ‹ÍÅíÒ⾧

pin ËÁÒ¶֧ µíÒá˹‹§ºÔµ·Õàè Å×͡㪌ÊÒí ËÃѺµ‹ÍÅíÒ⾧

µÑÇÍ‹ҧ·Õè 4-1 Sound_Init(&PORTB, 2);

໚¹¡ÒáíÒ˹´ãˌ¢Ò RB2 ໚¹¢ÒÊíÒËÃѺ¢ÑºàÊÕ§ÍÍ¡ÅíÒ⾧


74 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

4.2 ¿˜§¡ª¹Ñè Sound_Play ໚¹¿˜§¡ªÑ蹡íÒà¹Ô´ÊÑ­­Ò³àÊÕ§ ÃٻẺ¡ÒÃ㪌§Ò¹¤íÒÊѧè void Sound_Play(unsigned freq_in_hz, unsigned duration_ms);

¾ÒÃÒÁÔàµÍÏ ໚¹¤‹Ò¤ÇÒÁ¶Õ¢è ͧ¡ÒáíÒà¹Ô´àÊÕ§ ˹‹ÇÂ໚¹àÎÔõ« à¹×Íè §¨Ò¡à»š¹µÑÇá»ÃẺ unsigned ¨Ö§¡íÒ˹´¤‹Ò䴌µ§Ñé ᵋ 0 ¶Ö§ 65535 â´Ââ»Ãá¡ÃÁäÁ‹á¨Œ§¢ŒÍ¼Ô´¾Å��´áµ‹ªÇ‹ §¤ÇÒÁ¶Õ·è ÊÕè Ìҧ䴌¨ÃÔ§¹Ñ¹é ¤íҹdz䴌¨Ò¡ ¤‹Ò¤ÇÒÁ¶ÕÊè §Ù ÊØ´ = FOSC/(80x3) = FOSC/240 freq_in_hz

¤‹Ò¤ÇÒÁ¶Õµè Òèí ÊØ´ = FOSC/(80x255) = FOSC/20,400 â´Â¤‹Ò¤ÇÒÁ¶Õ·è ¶Õè ¡Ù ÊÌҧ¢Ö¹é ໚¹¤‹Ò»ÃÐÁÒ³·Õ¡è Òí ˹´¨Ò¡¿˜§¡ª¹Ñè Delay_Cyc «Ö§è ÍÒ¨ ᵡµ‹Ò§¨Ò¡¤‹Ò¤ÇÒÁ¶Õ·è áÕè ·Œ¨ÃÔ§àÅ硹ŒÍ ¡Ã³Õ·ãÕè ªŒ¤ÇÒÁ¶ÕÊè ­ Ñ ­Ò³¹Ò́¡Ò 20MHz ¤‹Ò¤ÇÒÁ¶ÕÊè §Ù ÊØ´·ÕÊè Ìҧ䴌෋ҡѺ 20,000,000/240 = 83,333 Hz ᵋ¤ÇÒÁ¶Õ·è ÊÕè Ìҧ䴌áÁ‹¹ÂíÒ໚¹ª‹Ç§·Õµè Òèí ¡Ç‹Ò 20,000Hz ËÃ×Í 20kHz ¤‹Ò¤ÇÒÁ¶Õµè Òèí ÊØ´·ÕÊè Ìҧ䴌෋ҡѺ 20,000,000/20,400 = 980 Hz duration_ms 㪌¡Òí ˹´ÃÐÂÐàÇÅÒ㹡ÒáíÒà¹Ô´àÊÕ§ ˹‹ÇÂ໚¹ÁÔÅÅÔǹ Ô Ò·Õ à»š¹µÑÇá»ÃẺ

unsigned ¨Ö§¡íÒ˹´¤‹Ò䴌µ§Ñé ᵋ 0 ¶Ö§ 65,535

µÑÇÍ‹ҧ·Õè 4-2

µŒÍ§¡ÒÃÊÌҧÊÑ­­Ò³¤ÇÒÁ¶Õè 2kHz ໚¹àÇÅÒ 1 ÇÔ¹Ò·Õ ¹íÒÁÒà¢Õ¹໚¹â»Ãá¡ÃÁ䴌´§Ñ ¹Õé Sound_Play(2000,1000)


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 75

»¯ÔºµÑ ¡Ô Ò÷Õè 5 ¢ÑºàÊÕ§´ŒÇ¿˜§¡ª¹Ñè Sound ¡ÒÃàª×èÍÁµ‹Í·Ò§ÎÒÏ´áÇÏ 㪌ǧ¨Ãã¹ÃÙ»·Õè L5-1 㹡Ò÷´Åͧ ÊÒÁÒö㪌äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏàºÍÏ PIC16F877 ËÃ×Í PIC16F887 ¡ç䴌 ᵋµÍŒ §àÅ×Í¡ãˌ¶¡Ù µŒÍ§àÁ×Íè ·íÒ¡ÒÃÊÌҧä¿Åâ»Ãਵ硵´ÇŒ  l

l

Ëҡ㪌ºÍÏ´ RBX-877 Rev.2007 ÊÒÁÒö·´Åͧ䴌àÅ â´ÂàÅ×Í¡äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ໚¹àºÍÏ PIC16F887

l

Ëҡ㪌ºÍÏ´·´Åͧ NX-877 plus II ãˌµÍ‹ ǧ¨ÃµÒÁÃÙ»·Õè L5-1 áŌÇàÅ×Í¡äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ໚¹ PIC16F877

¢Ñ鹵͹¡Ò÷´Åͧ L5.1 ແ´â»Ãá¡ÃÁ mikroC ÊÌҧä¿Åâ»Ãà¨ç¡µ â´ÂàÅ×Í¡äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏáÅФÇÒÁ¶ÕÊè ­ Ñ ­Ò³¹Ò́¡Ò´Ñ§¹Õé l

¡Ã³Õ㪌ºÍÏ´ RBX-877 Rev.2007 àÅ×Í¡ PIC16F887 ¤ÇÒÁ¶ÕÊè ­ Ñ ­Ò³¹Ò́¡Ò 20MHz

l

¡Ã³Õ㪌ºÍÏ´ NX-877 plus II àÅ×Í¡ PIC16F877 ËÃ×Í PIC16F877A ¤ÇÒÁ¶ÕÊè ­ Ñ ­Ò³¹Ò́¡Ò 20MHz +5V

R1 10k SW1 RESET

32 1

VDD RC0

12 31

+5V

MCLR

11

SP1 PIEZO

Vss Vss PIC16F887

2

AN0/RA0

CR1 Ceramic Resonator 20MHz

15

RA0/AN0

13

C1 0.1PF C2 10PF

12 31

14

ÃÙ»·Õè L5-1 ǧ¨Ãàª×Íè Áµ‹ÍÅíÒ⾧ໂÂ⫡ѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC16F887 (ËÃ×Í PIC16F877A) à¾×Íè ·´Êͺ¡Ò÷íÒ§Ò¹¢Í§¿˜§¡ª¹Ñè Sound 㹡ÒáíÒà¹Ô´ÊÑ­­Ò³àÊÕ§¢ÑºÍÍ¡ÅíÒ⾧


76 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

void main() { Sound_Init(&PORTC,0); while(1) { Sound_Play(2000,1000); Delay_ms(1000); } }

// Make pin RC0 ==> Speaker // Make 2kHz every 1 second

¡Ò÷íÒ§Ò¹¢Í§â»Ãá¡ÃÁ â»Ãá¡ÃÁ¡íÒ˹´µíÒá˹‹§¢Ò RC0 ໚¹¢ÒàÍÒµ¾µØ ·Õàè ª×Íè Áµ‹Í¡ÑºÅíÒ⾧ ¨Ò¡¹Ñ¹é áÅзíÒ¡ÒÃǹÅٻʋ§àÊÕ§ ¤ÇÒÁ¶Õè 2kHz ÍÍ¡ä»·Ø¡æ 1 ÇÔ¹Ò·Õ

â»Ãá¡ÃÁ·Õè L5-1 ä¿Å SoundTest.c â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺ mikroC à¾×Íè ¡íÒà¹Ô´àÊÕ§¨Ò¡¢Ò¾Íϵ PC0 ¢Í§äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC16F887 ËÃ×Í PIC16F877A ¢ÑºÍÍ¡ÅíÒ⾧ L5.2 ÊÌҧä¿Åâ»Ãà¨ç¡µªÍ×è SoundTest áÅоÔÁ¾â»Ãá¡ÃÁÀÒÉÒ C µÒÁâ»Ãá¡ÃÁ·Õè L5-1 L5.3 ·íÒ¡ÒäÍÁä¾ÅáÅдÒǹâËÅ´â»Ãá¡ÃÁà¾×Íè ·´Êͺ¼ÅÅѾ¸¡Ò÷íÒ§Ò¹ L5.4 Ãѹâ»Ãá¡ÃÁ 1 ÇÔ¹Ò·Õ

¨Ðä´ŒÂ¹Ô àÊÕ§¤ÇÒÁ¶Õ¤è ͋ ¹¢ŒÒ§ÊÙ§ (à¹×Íè §¨Ò¡ÁÕ¤ÇÒÁ¶Õè 2kHz) ´Ñ§ÍÍ¡¨Ò¡ÅíÒ⾧ໂÂâ«à»š¹¨Ñ§ËÇÐã¹·Ø¡æ

L5.5 ·´Åͧᡌä¢â»Ãá¡ÃÁà¾×Íè à»ÅÕÂè ¹¤‹Ò¤ÇÒÁ¶Õ¢è ͧÊÑ­­Ò³àÊÕ§ â´Â¤ÇáíÒ˹´ãˌÁÂÕ Ò‹ ¹¤ÇÒÁ¶ÕÃè ÐËNjҧ 1kHz ¶Ö§ 3kHz à¹×Íè §¨Ò¡ÅíÒ⾧·Õãè ªŒº¹ºÍÏ´ RBX-877 áÅÐ NX-877 ໚¹ÅíÒ⾧ໂÂâ«Áդҋ ¤ÇÒÁµŒÒ¹·Ò¹ 16 ¶Ö§ 32: ÊÒÁÒöµÍºÊ¹Í§ÊÑ­­Ò³Â‹Ò¹¤ÇÒÁ¶Õàè ÊÕ§㹪‹Ç§ 1kHz ¶Ö§ 3kHz ËÒ¡¤ÇÒÁ¶ÕÊè §Ù ËÃ×͵èÒí ¡Ç‹Ò¹Õé ÍÒ¨äÁ‹ä´ŒÂ¹Ô ËÃ×Íä´ŒÂ¹Ô àºÒŧ

INNOVATIVE EXPERIMENT


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 77

º··Õè 5

ÍÔ¹àµÍÃÃ»Ñ µã¹äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ¡Ñº mikroC ¤ÍÁä¾àÅÍÏ ¡ÒÃÍÔ¹àµÍÏÃÑ»µ (interrupt) ËÃ×Í¡ÒâѴ¨Ñ§ËÇСÒ÷íÒ§Ò¹¢Í§«Õ¾ÕÂÙ ¹Ñºà»š¹¤Ø³ÊÁºÑµÔ·Õ赌ͧ ÁÕã¹äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏÊÁÑÂãËÁ‹ áÅÐ໚¹¤Ø³ÊÁºÑµÔ·ÕèÁÕº·ºÒ·ÊíҤѭÍ‹ҧÁÒ¡àÁ×è͹íÒäÁâ¤Ã ¤Í¹â·ÃÅàÅÍÏÁÒÊÌҧÃкº¤Çº¤ØÁÍѵâ¹ÁÑµÔ ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC16F87x ÊÒÁÒö ¡íÒà¹Ô´áÅеͺʹͧ¡ÒÃà¡Ô´ÍÔ¹àµÍÃÃ»Ñ µä´Œ¶§Ö 15 áËŋ§´Ñ§¹Õé l ¨Ò¡ä·àÁÍÏ 3 áËŋ§¤×Í ¡ÒÃà¡Ô´¤‹Òà¡Ô¹ËÃ×ÍâÍàÇÍÏâ¿ÅÇã¹ä·àÁÍÏ 0 áÅÐä·àÁÍÏ 1 ÍÕ¡ áËŋ§Ë¹Ö§è ¨Ò¡¤‹Ò·Õàè ·‹ÒËÃ×͵ç¡Ñ¹ã¹ä·àÁÍÏ 2 l ¨Ò¡¡ÒÃà¢Õ¹˹‹Ç¤ÇÒÁ¨íÒ¢ŒÍÁÙÅÍÕ; Õ ÃÍÁàÊÃç¨ÊÁºÙó 1 áËŋ§ l ¨Ò¡âÁ´ÙÅà»ÃÕºà·Õº-µÃǨ¨ÑºÊÑ­­Ò³ áÅÐ PWM ËÃ×Í CCP 2 áËŋ§ l ¨Ò¡âÁ´ÙÅÊ×Í è ÊÒÃ͹ءÃÁ (USART) 2 áËŋ§¤×Í à¡Ô´ÍÔ¹àµÍÃÃ»Ñ µàÁ×Íè ÃѺáÅÐʋ§¢ŒÍÁÙÅÊÁºÙó l ¨Ò¡Ç§¨Ãà»ÃÕºà·Õºáç´Ñ¹ÍйÒÅÍ¡ 1 áËŋ§ (੾ÒÐã¹ PIC16F87xA) l ¨Ò¡¾Íϵ B 2 áËŋ§¤×Í ¨Ò¡ÊÑ­­Ò³ÍÔ¹àµÍÏû Ñ µÀÒ¹͡·Õ¢è Ò RB0/INT áÅСÒÃà»ÅÕÂè ¹ á»Å§Åͨԡ·Õ¢è Ò RB4 ¶Ö§ RB7 l ¨Ò¡âÁ´ÙÅàª×èÍÁµ‹Í͹ءÃÁẺ«Ô§â¤Ã¹ÑÊ 2 áËŋ§¤×Í ¨Ò¡¡Ò÷íÒ§Ò¹¢Í§âÁ´ÙÅàª×èÍÁµ‹Í ͹ءÃÁẺ«Ô§â¤Ã¹ÑÊ, ¨Ò¡¡Ò깡ѹ¢Í§¢ŒÍÁÙÅã¹Ê‹Ç¹àª×Íè Áµ‹ÍÃкººÑÊ I2C l ¨Ò¡âÁ´ÙÅá»Å§ÊÑ­­Ò³ÍйÒÅ͡໚¹´Ô¨µ Ô ÍÅ 1 áËŋ§ l ¨Ò¡¡Ò÷íÒ§Ò¹¢Í§¾Íϵ¢¹Ò¹àÊÃÔÁ (PSP) 1 áËŋ§ (੾ÒÐã¹ PIC16F87x Ãع ‹ 40 ¢Ò) â´ÂàÁ×Íè à§×Íè ¹ä¢¢Í§¡ÒÃà¡Ô´ÍÔ¹àµÍÃÃ»Ñ µà»š¹¨ÃÔ§ ¨ÐÁÕ¡ÒÃ૵á¿Å¡¢Í§¡ÒÃÍÔ¹àµÍÃÃ»Ñ µ¹¹Ñé æ ¢Ö¹é (ª×Íè ¢Í§á¿Å¡¨Ðŧ·ŒÒ´ŒÇµÑÇÍÑ¡Éà F) ¨Ò¡¹Ñ¹é ¨ÐµÃǨÊÍºÇ‹Ò ÁÕ¡ÒÃàÍç¹àÍàºÔÅ¡ÒÃÍÔ¹àµÍÃÃ»Ñ µ¹¹Ñé æ ËÃ×ÍäÁ‹ (ª×Íè ¢Í§¡ÒÃàÍç¹àÍàºÔÅ¡ÒÃÍÔ¹àµÍÃÃ»Ñ µ¨ÐàËÁ×͹¡Ñºª×Íè á¿Å¡áµ‹Å§·ŒÒ´ŒÇµÑÇÍÑ¡Éà E) ઋ¹ ËÒ¡à¡Ô´ÊÑ­­Ò³ÍÔ¹àµÍÃÃ»Ñ µ·¢Õè Ò RB0/INT á¿Å¡ INTF ¨Ð૵ áÅжŒÒÁÕ¡ÒÃàÍç¹àÍàºÔÅ¡ÒÃÍÔ¹àµÍÃÃ»Ñ µ


78 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

E EIE

E EIF

T 0IE

T 0IF

R B IE

R B IF

IN T E

IN T F

IN T E R R U P T

G IE

ÃÙ»·Õè 5-1 á¹Ç¤Ô´ã¹¡ÒáíÒ˹´¤‹Òµ‹Ò§æ à¾×Íè ÃͧÃѺ¡ÒÃà¡Ô´ÍÔ¹àµÍÃÃ»Ñ µã¹äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC Ẻ¹Õé «Ö觵ÃǨÊͺ¨Ò¡ºÔµ INTE áÅÐÁÕ¡ÒÃàÍç¹àÍàºÔÅ¡ÒÃÍÔ¹àµÍÏÃÑ»µÃÇÁ (GIE) änj ¡ç¨Ð·íÒà¡Ô´ ¡ÒÃÍÔ¹àµÍÃÃ»Ñ µ¢¹Öé ã¹Ãкº ´Ñ§áÊ´§ã¹ÃÙ»·Õè 5-1 ¹Ñ¹è ËÁÒ¤ÇÒÁÇ‹Ò ¡ÒèСíÒ˹´ãˌà¡Ô´ÍÔ¹àµÍÃÃ»Ñ µ ¢Ö¹é 䴌 µŒÍ§ÁÕ¡ÒÃàµÃÕÂÁ¡Òôѧ¹Õé (1) àÍç¹àÍàºÔÅ¡ÒÃÍÔ¹àµÍÃÃ»Ñ µÃÇÁ â´Â¡ÒÃ૵ºÔµ GIE ã¹ÃÕ¨ÊÔ àµÍÏ INTCON ËÒ¡ºÔµ¹Õäé Á‹ÁÕ ¡ÒÃ૵ ¨ÐäÁ‹Á·Õ Ò§à¡Ô´¡ÒÃÍÔ¹àµÍÃÃ»Ñ µ¢¹Öé 䴌 áÁŒÇҋ à§×Íè ¹ä¢ã¹¡ÒÃà¡Ô´ÍÔ¹àµÍÃÃ»Ñ µ¢Í§áËŋ§¡íÒà¹Ô´ µ‹Ò§æ ¨Ð໚¹¨ÃÔ§ áÅÐÁÕ¡ÒÃàÍç¹àÍàºÔÅáËŋ§¡íÒà¹Ô´ÍÔ¹àµÍÃÃ»Ñ µáŌǡçµÒÁ (2) àÍç¹àÍàºÔÅáËŋ§¡íÒà¹Ô´ÍÔ¹àµÍÃÃ»Ñ µ (3) à¢Õ¹¿˜§¡ª¹Ñè ºÃÔ¡ÒÃÍÔ¹àµÍÃÃ»Ñ µ ÊíÒËÃѺ mikroC ¡íÒ˹´ª×Íè änj໚¹ void interrupt() (4) ÃͤÍÂãˌà§×Íè ¹ä¢¢Í§¡ÒÃà¡Ô´ÍÔ¹àµÍÃÃ»Ñ µã¹Åѡɳе‹Ò§æ à¡Ô´¢Ö¹é (5) àÁ×Íè ¨º¢Ñ¹é µÍ¹ã¹¿˜§¡ª¹Ñ ºÃÔ¡ÒÃÍÔ¹àµÍÃÃ»Ñ µáŌǨеŒÍ§à¤ÅÕÂÏá¿Å¡ÍÔ¹àµÍÃÃ»Ñ µ´ÇŒ Â

5.1 ÃÕ¨ÊÔ àµÍÏ·àÕè ¡ÕÂè Ç¢ŒÍ§¡Ñº¡ÒÃÍÔ¹àµÍÃÃ»Ñ µ Áմnj ¡ѹ 5 µÑÇ ¤×Í 1. INTCON ໚¹ÃÕ¨ÊÔ àµÍϤǺ¤ØÁ¡ÒÃÍÔ¹àµÍÃÃ»Ñ µËÅÑ¡ 2. PIE1 ໚¹ÃÕ¨ÊÔ àµÍÏàÍç¹àÍàºÔÅáËŋ§¡íÒà¹Ô´ÍÔ¹àµÍÃÃ»Ñ µ¨Ò¡ÍØ»¡Ã³à¾ÍÃÔà¿ÍÃÑÅ 3. PIE2 ໚¹ÃÕ¨ÊÔ àµÍÏàÍç¹àÍàºÔÅáËŋ§¡íÒà¹Ô´ÍÔ¹àµÍÃÃ»Ñ µ¨Ò¡ÍØ»¡Ã³à¾ÍÃÔà¿ÍÃÑŵÑÇ·Õè 2 4. PIR1 ໚¹ÃÕ¨ÊÔ àµÍÏáÊ´§Ê¶Ò¹Ðà§×Íè ¹ä¢¢Í§áËŋ§¡íÒà¹Ô´ÍÔ¹àµÍÃÃ»Ñ µ¨Ò¡ÍØ»¡Ã³ à¾ÍÃÔà¿ÍÃÑÅ·íҧҹËÇÁ¡Ñº PIE1 5. PIR2 ໚¹ÃÕ¨ÊÔ àµÍÏáÊ´§Ê¶Ò¹Ðà§×Íè ¹ä¢¢Í§áËŋ§¡íÒà¹Ô´ÍÔ¹àµÍÃÃ»Ñ µ¨Ò¡ÍØ»¡Ã³ µ‹Í¾‹Ç§µÑÇ·Õè 2 ·íҧҹËÇÁ¡Ñº PIE2


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 79

ÊíÒËÃѺÃÒÂÅÐàÍÕ´·Ñ§é ËÁ´¢Í§ÃÕ¨ÊÔ àµÍÏ·§Ñé 5 µÑÇÁÕ´§Ñ ¹Õé

5.1.1 ÃÕ¨ÊÔ àµÍÏ INTCON ໚¹ÃÕ¨ÊÔ àµÍÏ·ÁÕè ¹Õ ÂÑ ÊíҤѭÊÙ§ÊØ´ÊíÒËÃѺ¡ÒÃÍÔ¹àµÍÃÃ»Ñ µ ÊÒÁÒö͋ҹáÅÐà¢Õ¹䴌·¡Ø ºÔµ 㪌àÍç¹ àÍàºÔÅ¡ÒÃÍÔ¹àµÍÃÃ»Ñ µÃÇÁáÅСÒÃÍÔ¹àµÍÃÃ»Ñ µ¾¹×é °Ò¹¢Í§äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ÁÕÃÒÂÅÐàÍÕ´´Ñ§¹Õé ºÔµ 7 INTCON

ºÔµ 6

ºÔµ 5

ºÔµ 4

ºÔµ 3

ºÔµ 2

ºÔµ 1

ºÔµ 0

GIE

PEIE

T0IE

INTE

RBIE

T0IF

INTF

RBIF

R/W -0

R/W -0

R/W -0

R/W -0

R/W -0

R/W -0

R/W -0

R/W -x

ºÔµ 7 - GIE (Global Interrupt Enable bit) : ºÔµàÍç¹àÍàºÔÅ¡ÒÃÍÔ¹àµÍÃÃ»Ñ µÃÇÁ “0” - àÅ×Í¡äÁ‹ãˌÁ¡Õ ÒÃÍÔ¹àµÍÃÃ»Ñ µà¡Ô´¢Ö¹é ·Ñ§é ËÁ´ “1” - àÅ×Í¡ãˌÁ¡Õ ÒÃÍÔ¹àµÍÃÃ»Ñ µà¡Ô´¢Ö¹é 䴌·§Ñé ËÁ´ ºÔµ 6 - PEIE (Peripheral Interrupt Enable bit) : ºÔµàÍç¹àÍàºÔÅ¡ÒÃÍÔ¹àµÍÃÃ»Ñ µ¨Ò¡ÍØ»¡Ã³µÍ‹ ¾‹Ç§ ૵໚¹ “1” à¾×Íè àÍç¹àÍàºÔÅ¡ÒÃÍÔ¹àµÍÃÃ»Ñ µáºº¹Õé ºÔµ 5 - T0IE (TMR0 Overflow Interrupt Enable bit) : ºÔµàÍç¹àÍàºÔÅÍÔ¹àµÍÃÃ»Ñ µ¨Ò¡âÍàÇÍÏâ¿ÅǢͧä·àÁÍÏ 0 ૵໚¹ “1” à¾×Íè àÍç¹àÍàºÔÅ¡ÒÃÍÔ¹àµÍÃÃ»Ñ µáºº¹Õé ºÔµ 4 - INTE (RBO/INT External Interrupt Enable bit) : ºÔµàÍç¹àÍàºÔÅÍÔ¹àµÍÃÃ»Ñ µ¨Ò¡ÀÒ¹͡·Õ¢è Ò RB0/INT ૵໚¹ “1” à¾×Íè àÍç¹àÍàºÔÅ¡ÒÃÍÔ¹àµÍÃÃ»Ñ µáºº¹Õé ºÔµ 3 - RBIE (Port B Change Interrupt Enable bit) : ºÔµàÍç¹àÍàºÔÅÍÔ¹àµÍÃÃ»Ñ µ¨Ò¡¡ÒÃà»ÅÕÂè ¹á»Å§Åͨԡ·Õè ¾Íϵ B ¢Ò RB4-RB7 ૵໚¹ “1” à¾×Íè àÍç¹àÍàºÔÅ¡ÒÃÍÔ¹àµÍÃÃ»Ñ µáºº¹Õé ºÔµ 2 - T0IF (TMR0 Overflow Interrupt Flag bit) : ºÔµáÊ´§¡ÒÃâÍàÇÍÏâ¿ÅǢͧä·àÁÍÏ 0 “0” - ä·àÁÍÏ 0 äÁ‹à¡Ô´âÍàÇÍÏâ¿ÅÇ “1” - ä·àÁÍÏ 0 âÍàÇÍÏâ¿ÅÇ ·íÒãˌà¡Ô´ÍÔ¹àµÍÃÃ»Ñ µËÒ¡àÍç¹àÍàºÔÅänj à¤ÅÕÂÏ·Ò§«Í¿µáÇÏ ºÔµ 1 - INTF (RB0/INT External Interrupt Flag bit) : ºÔµáÊ´§¡ÒÃÍÔ¹àµÍÃÃ»Ñ µ¨Ò¡ÀÒ¹͡·Õ¢è Ò RB0/INT “0” - äÁ‹ÁÊÕ ­ Ñ ­Ò³ÍÔ¹àµÍÃÃ»Ñ µ¨Ò¡ÀÒ¹͡à¡Ô´¢Ö¹é ·Õ¢è Ò RB0/INT “1” - ÁÕÊ­ Ñ ­Ò³ÍÔ¹àµÍÃÃ»Ñ µ¨Ò¡ÀÒ¹͡à¡Ô´¢Ö¹é ·Õ¢è Ò RB0/INT ·íÒãˌà¡Ô´¡ÒÃÍÔ¹àµÍÃÃ»Ñ µ ËÒ¡ ÁÕ¡ÒÃàÍç¹àÍàºÔÅänj µŒÍ§à¤ÅÕÂÏ໚¹ “0” ´ŒÇ¡Ãкǹ¡Ò÷ҧ«Í¿µáÇÏ ºÔµ 0 - RBIF (Port B Chang Interrupt Flag bit) : ºÔµáÊ´§¡ÒÃà»ÅÕÂè ¹á»Å§ÃдѺÅͨԡ·Õ¢è Ò RB4-RB7 “0” - äÁ‹Á¡Õ ÒÃà»ÅÕÂè ¹á»Å§à¡Ô´¢Ö¹é ·Õ¢è Ò RB4-RB7 “1” - ÁÕ¡ÒÃà»ÅÕÂè ¹á»Å§ÃдѺÅͨԡà¡Ô´¢Ö¹é ·Õ¢è Ò RB4-RB7 Í‹ҧ¹ŒÍ 1 ¢Ò ·íÒãˌà¡Ô´¡ÒÃÍÔ¹àµÍÏ ÃÑ»µ ËÒ¡ÁÕ¡ÒÃàÍç¹àÍàºÔÅänj µŒÍ§à¤ÅÕÂÏ໚¹ “0” ´ŒÇ¡Ãкǹ¡Ò÷ҧ«Í¿µáÇÏ


80 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

5.1.2 ÃÕ¨ÊÔ àµÍÏ PIE1

໚¹ÃÕ¨ÔÊàµÍÏàÍç¹àÍàºÔÅáËŋ§¡íÒà¹Ô´ÊÑ­­Ò³ÍÔ¹àµÍÏÃÑ»µ¨Ò¡ÍØ»¡Ã³µ‹Í¾‹Ç§ã¹áËŋ§µ‹Ò§æ ÊÒÁÒöࢌҶ֧à¾×Íè ͋ҹáÅÐà¢Õ¹䴌ã¹ÃдѺºÔµ ÁÕÃÒÂÅÐàÍÕ´¢Í§áµ‹ÅкԵ´Ñ§¹Õé ºÔµ 7

PIE1

ºÔµ 6

ºÔµ 5

ºÔµ 4

ºÔµ 3

PSPIE

ADIE

RCIE

TXIE

SSPIE

R/W -0

R/W -0

R/W -0

R/W -0

R/W -0

ºÔµ 2

ºÔµ 1

ºÔµ 0

R/W -0

R/W -0

R/W -0

CCP1IE TMR2IE TMR1IE

ºÔµ 7 - PSPIE (Parallel Slave Port Read/Write Interrupt Enable bit ) : ºÔµàÍç¹àÍàºÔÅ¡ÒÃÍÔ¹àµÍÃÃ»Ñ µà¹×Íè §¨Ò¡ ¡ÒÃ͋ҹáÅÐà¢Õ¹¢ŒÍÁÙŢͧ¾Íϵ¢¹Ò¹àÊÃÔÁËÃ×Í PSP ૵໚¹ “1” à¾×Íè àÍç¹àÍàºÔÅ¡ÒÃÍÔ¹àµÍÃÃ»Ñ µáºº¹Õé ¶ŒÒ໚¹ “0” ¨Ð໚¹¡ÒÃàÅ×Í¡´ÔÊàÍàºÔÅ ºÔµ 6 - ADIE (A/D Converter Interrupt Enable bit) : ºÔµàÍç¹àÍàºÔÅ¡ÒÃÍÔ¹àµÍÃÃ»Ñ µ¢Í§âÁ´ÙÅ A/D ૵໚¹ “1” à¾×Íè àÍç¹àÍàºÔÅ¡ÒÃÍÔ¹àµÍÃÃ»Ñ µáºº¹Õé ¶ŒÒ໚¹ “0” ¨Ð໚¹¡ÒÃàÅ×Í¡´ÔÊàÍàºÔÅ ºÔµ 5 - RCIE (USART Receive Interrupt Enable bit) : ºÔµàÍç¹àÍàºÔÅ¡ÒÃÍÔ¹àµÍÃÃ»Ñ µàÁ×Íè ¡ÒÃÃѺ¢ŒÍÁÙŢͧ âÁ´ÙÅÊ×Íè ÊÒâŒÍÁÙÅ͹ءÃÁ (USART) àÊÃç¨ÊÔ¹é ÊÁºÙó ૵໚¹ “1” à¾×Íè àÍç¹àÍàºÔÅ¡ÒÃÍÔ¹àµÍÃÃ»Ñ µáºº¹Õé ¶ŒÒ໚¹ “0” ¨Ð໚¹¡ÒÃàÅ×Í¡´ÔÊàÍàºÔÅ ºÔµ 4 - TXIE (USART Transmit Interrupt Enable bit) : º��µàÍç¹àÍàºÔÅ¡ÒÃÍÔ¹àµÍÃÃ»Ñ µàÁ×Íè ¡ÒÃʋ§¢ŒÍÁÙŢͧ âÁ´ÙÅÊ×Íè ÊÒâŒÍÁÙÅ͹ءÃÁ (USART) àÊÃç¨ÊÔ¹é ÊÁºÙó ૵໚¹ “1” à¾×Íè àÍç¹àÍàºÔÅ¡ÒÃÍÔ¹àµÍÃÃ»Ñ µáºº¹Õé ¶ŒÒ໚¹ “0” ¨Ð໚¹¡ÒÃàÅ×Í¡´ÔÊàÍàºÔÅ ºÔµ 3 - SSPIE (Synchronous Serial Port Interrupt Enable bit) : ºÔµàÍç¹àÍàºÔÅ¡ÒÃÍÔ¹àµÍÏÃÑ»µ¨Ò¡âÁ´ÙÅ àª×Íè Áµ‹Í͹ءÃÁẺ«Ô§â¤Ã¹ÑÊ (MSSP) ૵໚¹ “1” à¾×Íè àÍç¹àÍàºÔÅ¡ÒÃÍÔ¹àµÍÃÃ»Ñ µáºº¹Õé ¶ŒÒ໚¹ “0” ¨Ð໚¹¡ÒÃàÅ×Í¡´ÔÊàÍàºÔÅ ºÔµ 2 - CCP1IE (CCP1 Interrupt Enable bit) : ºÔµàÍç¹àÍàºÔÅ¡ÒÃÍÔ¹àµÍÃÃ»Ñ µ¨Ò¡âÁ´ÙÅ CCP1 ૵໚¹ “1” à¾×Íè àÍç¹àÍàºÔÅ¡ÒÃÍÔ¹àµÍÃÃ»Ñ µáºº¹Õé ¶ŒÒ໚¹ “0” ¨Ð໚¹¡ÒÃàÅ×Í¡´ÔÊàÍàºÔÅ ºÔµ 1 - TMR2IE (TMR2 to PR2 match Interrupt Enable bit) : ºÔµàÍç¹àÍàºÔÅ¡ÒÃÍÔ¹àµÍÃÃ»Ñ µ¨Ò¡¡Ò÷դè ҋ ¢Í§ ä·àÁÍÏ 2 áÅÐÃÕ¨ÊÔ àµÍϤҺàÇÅÒ 2 (Period register 2) ෋ҡѹ ૵໚¹ “1” à¾×Íè àÍç¹àÍàºÔÅ¡ÒÃÍÔ¹àµÍÃÃ»Ñ µáºº¹Õé ¶ŒÒ໚¹ “0” ¨Ð໚¹¡ÒÃàÅ×Í¡´ÔÊàÍàºÔÅ ºÔµ 0 - TMR1IE (TMR1 Overflow Interrupt Enable bit ) : ºÔµàÍç¹àÍàºÔÅÍÔ¹àµÍÃÃ»Ñ µ¨Ò¡ä·àÁÍÏ 1 âÍàÇÍÏâ¿ÅÇ à«µà»š¹ “1” à¾×Íè àÍç¹àÍàºÔÅ¡ÒÃÍÔ¹àµÍÃÃ»Ñ µáºº¹Õé ¶ŒÒ໚¹ “0” ¨Ð໚¹¡ÒÃàÅ×Í¡´ÔÊàÍàºÔÅ


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 81

5.1.3 ÃÕ¨ÊÔ àµÍÏ PIE2 ໚¹ÃÕ¨ÔÊàµÍÏàÍç¹àÍàºÔÅáËŋ§¡íÒà¹Ô´ÊÑ­­Ò³ÍÔ¹àµÍÏÃÑ»µ¨Ò¡ÍØ»¡Ã³µ‹Í¾‹Ç§ã¹áËŋ§µ‹Ò§æ µÑÇ·Õè 2 ÊÒÁÒöࢌҶ֧à¾×Íè ͋ҹáÅÐà¢Õ¹䴌ã¹ÃдѺºÔµ ÁÕÃÒÂÅÐàÍÕ´´Ñ§¹Õé ºÔµ 7 PIE2

-

ºÔµ 6

CMIE R/W -0

ºÔµ 5 -

ºÔµ 4

ºÔµ 3

EEIE

BCLIE

R/W -0

R/W -0

ºÔµ 2 -

ºÔµ 1 -

ºÔµ 0

CCP2IE R/W -0

ºÔµ 7 : äÁ‹ãªŒ§Ò¹ ¡íÒ˹´à»š¹ “0” ºÔµ 6 - CMIE (Comparator Interrupt Enable bit) : ºÔµàÍç¹àÍàºÔÅ¡ÒÃÍÔ¹àµÍÏÃÑ»µ¨Ò¡âÁ´ÙÅà»ÃÕºà·Õº áç´Ñ¹ÍйÒÅÍ¡ “0” - ´ÔÊàÍàºÔÅ¡ÒÃÍÔ¹àµÍÃÃ»Ñ µáºº¹Õé “1” - àÍç¹àÍàºÔÅ¡ÒÃÍÔ¹àµÍÃÃ»Ñ µáºº¹Õé ºÔµ 5 : äÁ‹ãªŒ§Ò¹ ¡íÒ˹´à»š¹ “0” ºÔµ 4 - EEIE (EEPROM Write Operation Interrupt Enable bit) : ºÔµàÍç¹àÍàºÔÅ¡ÒÃÍÔ¹àµÍÃÃ»Ñ µàÁ×Íè ¡ÒÃà¢Õ¹ ¢ŒÍÁÙÅŧã¹Ë¹‹Ç¤ÇÒÁ¨íÒ¢ŒÍÁÙÅÍÕÍÕ¾ÃÍÁàÊÃç¨ÊÁºÙó “0” - ´ÔÊàÍàºÔÅ¡ÒÃÍÔ¹àµÍÃÃ»Ñ µáºº¹Õé “1” - àÍç¹àÍàºÔÅ¡ÒÃÍÔ¹àµÍÃÃ»Ñ µáºº¹Õé ºÔµ 3 - BCLIE (ÚBus Collision Interrupt Enable bit) : ºÔµàÍç¹àÍàºÔÅ¡ÒÃÍÔ¹àµÍÃÃ»Ñ µ¨Ò¡ª¹¡Ñ¹¢Í§¢ŒÍÁÙÅã¹ ¡Ò÷íÒ§Ò¹¢Í§âÁ´ÙÅàª×Íè Áµ‹Í͹ءÃÁẺ«Ô§â¤Ã¹ÑÊ (SSP) àÁ×Íè ·íÒ§Ò¹ã¹âËÁ´ I2C ÁÒÊàµÍÏ “0” - ´ÔÊàÍàºÔÅ¡ÒÃÍÔ¹àµÍÃÃ»Ñ µáºº¹Õé “1” - àÍç¹àÍàºÔÅ¡ÒÃÍÔ¹àµÍÃÃ»Ñ µáºº¹Õé ºÔµ 2 áÅÐ 1 : äÁ‹ãªŒ§Ò¹ ¡íÒ˹´à»š¹ “0” ºÔµ 0 - CCP2IE (CCP2 Interrupt Enable bit) : ºÔµàÍç¹àÍàºÔÅ¡ÒÃÍÔ¹àµÍÃÃ»Ñ µ¨Ò¡âÁ´ÙÅ CCP2 “0” - ´ÔÊàÍàºÔÅ¡ÒÃÍÔ¹àµÍÃÃ»Ñ µáºº¹Õé “1” - àÍç¹àÍàºÔÅ¡ÒÃÍÔ¹àµÍÃÃ»Ñ µáºº¹Õé


82 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

5.1.4 ÃÕ¨ÊÔ àµÍÏ PIR1 ໚¹ÃÕ¨ÊÔ àµÍÏáÊ´§Ê¶Ò¹Ðà§×Íè ¹ä¢¡ÒÃà¡Ô´ÍÔ¹àµÍÃÃ»Ñ µà¹×Íè §¨Ò¡ÍØ»¡Ã³µÍ‹ ¾‹Ç§ ·íҧҹËÇÁ¡Ñº ÃÕ¨ÊÔ àµÍÏ PIE1 àÁ×Íè à§×Íè ¹ä¢¢Í§¡ÒÃÍÔ¹àµÍÃÃ»Ñ µà»š¹¨ÃÔ§ ¨Ð૵ºÔµÊ¶Ò¹Ðã¹ÃÕ¨ÊÔ àµÍÏ PIR1 ¨Ò¡¹Ñ¹é µÃǨÊͺNjÒÁÕ¡ÒÃàÍç¹àÍ--àºÔÅ¡ÒÃÍÔ¹àµÍÃÃ»Ñ µ¹¹Ñé æ ·ÕÃè ¨Õ ÊÔ àµÍÏ PIE1 ËÃ×ÍäÁ‹ ¶ŒÒÁÕ¡¨ç еÃǨÊÍºÇ‹Ò ºÔµ PEIE áÅÐ GIE ã¹ÃÕ¨ÊÔ àµÍÏ INTCON ૵änj·§Ñé ¤Ùˋ Ã×ÍäÁ‹ ¶ŒÒËҡ૵änj ¡ç¨Ðà¡Ô´¡ÒÃÍÔ¹àµÍÃÃ»Ñ µ¢¹Öé Í‹ҧÊÁºÙó ÊÒÁÒö͋ҹáÅÐà¢Õ¹䴌·¡Ø ºÔµÂ¡ànj¹ºÔµ 4 áÅÐ 5 (ºÔµ TXIF áÅÐ RCIF) ·ÕÍè ҋ ¹ä´Œ Í‹ҧà´ÕÂÇ ÁÕÃÒÂÅÐàÍÕ´´Ñ§¹Õé ºÔµ 7

PIR1

ºÔµ 6

ºÔµ 5

ºÔµ 4

ºÔµ 3

PSPIF

ADIF

RCIF

TXIF

SSPIF

R/W -0

R/W -0

R -0

R -0

R/W -0

ºÔµ 2

ºÔµ 1

ºÔµ 0

R/W -0

R/W -0

R/W -0

CCP1IF TMR2IF TMR1IF

ºÔµ 7 - PSPIF (Parallel Slave Port Read/Write Interrupt Flag bit) : ºÔµáÊ´§Ê¶Ò¹Ð¡ÒÃ͋ҹËÃ×Íà¢Õ¹ ¢ŒÍÁÙŢͧ¾Íϵ¢¹Ò¹àÊÃÔÁ (PSP) “0” - äÁ‹Á¡Õ ÒÃ͋ҹ-à¢Õ¹à¡Ô´¢Ö¹é “1” - à¢Õ¹ËÃ×Í͋ҹ¢ŒÍÁÙšѺ¾Íϵ¢¹Ò¹àÊÃÔÁàÊÃç¨ µŒÍ§à¤ÅÕÂϴnj ¡Ãкǹ¡Ò÷ҧ«Í¿µáÇÏ ºÔµ 6 - ADIF (A/D Converter Interrupt Flag bit) : ºÔµáÊ´§Ê¶Ò¹Ð¡Ò÷íÒ§Ò¹¢Í§âÁ´ÙÅ A/D “0” - ¡ÒÃá»Å§ÊÑ­­Ò³ÂѧäÁ‹ÊÁºÙó “1” - ¡ÒÃá»Å§ÊÑ­­Ò³ÍйÒÅ͡໚¹´Ô¨µÔ ÍÅàÊÃç¨ÊÁºÙó ºÔµ 5 - RCIF (USART Receive Interrupt Flag bit) : ºÔµáÊ´§Ê¶Ò¹Ð¡ÒÃÃѺ¢ŒÍÁÙŢͧâÁ´ÙÅ USART “0” - ºÑ¿à¿ÍϵÇÑ ÃѺ¢Í§Ç§¨Ã USART Njҧ “1” - ºÑ¿à¿ÍϵÇÑ ÃѺ¢Í§Ç§¨Ã USART àµçÁ à¡Ô´¡ÒÃÃѺ¢ŒÍÁÙÅÊÁºÙó ºÔµ 4 - TXIF (USART Transmit Interrupt Flag bit) : ºÔµáÊ´§Ê¶Ò¹Ð¡ÒÃʋ§¢ŒÍÁÙŢͧâÁ´ÙÅ USART “0” - ºÑ¿à¿ÍϵÇÑ Ê‹§¢Í§Ç§¨Ã USART àµçÁ ÂѧäÁ‹à¡Ô´¡ÒÃʋ§¢ŒÍÁÙÅ “1” - ºÑ¿à¿ÍϵÇÑ Ê‹§¢Í§Ç§¨Ã USART Njҧ áÊ´§Ç‹Ò¡ÒÃʋ§¢ŒÍÁÙÅÊÁºÙó ·íÒãˌà¡Ô´¡ÒÃÍÔ¹àµÍÏ ÃÑ»µ ËÒ¡ÁÕ¡ÒÃàÍç¹àÍàºÔÅänj ºÔµ 3 - SSPIF (Synchronous Serial Port (SSP) Interrupt Flag bit) : ºÔµáÊ´§Ê¶Ò¹Ð¡Ò÷íÒ§Ò¹¢Í§âÁ´ÙÅ SSP “0” - äÁ‹ÁàÕ §×Íè ¹ä¢ã´ã¹¡Ò÷íÒ§Ò¹¢Í§âÁ´ÙÅ MSSP ·Õ·è Òí ãˌà¡Ô´¡ÒÃÍÔ¹àµÍÃÃ»Ñ µà»š¹¨ÃÔ§ “1” - ÁÕà§×è͹䢷Õè·íÒãˌà¡Ô´¡ÒÃÍÔ¹àµÍÏÃÑ»µ¨Ò¡¡Ò÷íÒ§Ò¹¢Í§âÁ´ÙÅ SSP ໚¹¨ÃÔ§ ·íÒãˌà¡Ô´ ÍÔ¹àµÍÃÃ»Ñ µ ËÒ¡ÁÕ¡ÒÃàÍç¹àÍàºÔÅänj à§×Íè ¹ä¢·Õàè »š¹¨ÃÔ§¢Í§¡Ò÷íҧҹᵋÅÐâËÁ´¤×Í âËÁ´ SPI à¡Ô´¡ÒÃÃѺËÃ×Íʋ§¢ŒÍÁÙÅ¢Ö¹é Í‹ҧÊÁºÙó


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 83

âËÁ´ I2C ÁÒÊàµÍÏ - à¡Ô´¡ÒÃÃѺËÃ×Íʋ§¢ŒÍÁÙÅ¢Ö¹é Í‹ҧÊÁºÙó - à¡Ô´Ê¶Ò¹ÐàÃÔÁè µŒ¹¢Ö¹é Í‹ҧÊÁºÙ󏺹ºÑÊ I2C â´ÂâÁ´ÙÅ SSP - à¡Ô´Ê¶Ò¹ÐËÂØ´¢Ö¹é Í‹ҧÊÁºÙ󏺹ºÑÊ I2C â´ÂâÁ´ÙÅ SSP - à¡Ô´Ê¶Ò¹ÐàÃÔÁè µŒ¹«éÒí ¢Ö¹é Í‹ҧÊÁºÙ󏺹ºÑÊ I2C â´ÂâÁ´ÙÅ SSP - à¡Ô´Ê¶Ò¹ÐÃѺÃٌ (acknowledge) ¢Ö¹é Í‹ҧÊÁºÙ󏺹ºÑÊ I2C â´ÂâÁ´ÙÅ SSP - à¡Ô´Ê¶Ò¹ÐàÃÔÁè µŒ¹¢Ö¹é ã¹¢³Ð·Õâè Á´ÙÅ SSP ÍÂÙ㋠¹ÊÀÒÇÐNjҧËÃ×ÍäÍà´ÔÅ (idle) ¡Ã³ÕÊÌҧ໚¹ÃкºÁÑŵÔÁÒÊàµÍÏ - à¡Ô´Ê¶Ò¹ÐËÂØ´¢Ö¹é ã¹¢³Ð·Õâè Á´ÙÅ SSP ÍÂÙ㋠¹ÊÀÒÇÐNjҧËÃ×ÍäÍà´ÔÅ (idle) ¡Ã³Õ ÊÌҧ໚¹ÃкºÁÑŵÔÁÒÊàµÍÏ âËÁ´ I2C ÊàÅ¿ à¡Ô´¡ÒÃÃѺËÃ×Íʋ§¢ŒÍÁÙÅ¢Ö¹é Í‹ҧÊÁºÙó ºÔµ 2 - CCP1IF (CCP1 Interrupt Flag bit) : ºÔµáÊ´§Ê¶Ò¹Ð¡Ò÷íÒ§Ò¹¢Í§âÁ´ÙÅ CCP1 ÊÒÁÒöẋ§á¡µÒÁâËÁ´¡Ò÷íÒ§Ò¹¢Í§âÁ´ÙÅ CCP1 䴌´§Ñ ¹Õé âËÁ´µÃǨ¨ÑºÊÑ­­Ò³ËÃ×Í᤻à¨ÍÏ (capture mode) “0” - äÁ‹Á¡Õ ÒõÃǨ¨ÑºÊÑ­­Ò³à¡Ô´¢Ö¹é ·ÕÃè ¨Õ ÊÔ àµÍÏ TMR1 “1” - ÁÕ¡ÒõÃǨ¨ÑºÊÑ­­Ò³à¡Ô´¢Ö¹é ·ÕÃè ¨Õ ÊÔ àµÍÏ TMR1 ·íÒãˌà¡Ô´ÍÔ¹àµÍÃÃ»Ñ µä´Œ ËÒ¡àÍç¹àÍàºÔÅ änj µŒÍ§à¤ÅÕÂÏ໚¹ “0” ´ŒÇ¡Ãкǹ¡Ò÷ҧ«Í¿µáÇÏ âËÁ´à»ÃÕºà·ÕºÊÑ­­Ò³ (compare mode) “0” - ¡ÒÃà»ÃÕºà·Õº¢ŒÍÁÙÅ·ÕÃè ¨Õ ÊÔ àµÍÏ TMR1 äÁ‹à·‹Ò¡Ñ¹ËÃ×ÍäÁ‹àËÁ×͹¡Ñ¹ “1” - ¡ÒÃà»ÃÕ Â ºà·Õ  º¢Œ Í ÁÙ Å ·Õè ÃÕ ¨Ô Ê àµÍÏ TMR1 ෋ Ò ¡Ñ ¹ ËÃ× Í àËÁ× Í ¹¡Ñ ¹ ·í Ò ãˌ à ¡Ô ´ ¡Òà ÍÔ¹àµÍÃÃ»Ñ µä´Œ ËÒ¡ÁÕ¡ÒÃàÍç¹àÍàºÔÅänj µŒÍ§à¤ÅÕÂÏ໚¹ “0” ´ŒÇ¡Ãкǹ¡Ò÷ҧ«Í¿µáÇÏ äÁ‹Á¡Õ ÒÃ㪌§Ò¹ºÔµ¹ÕËé Ò¡¡íÒ˹´ãˌ CCP1 ·íÒ§Ò¹ã¹âËÁ´ PWM ºÔµ 1 - TMR2IF (TMR2 to PR2 match Interrupt Flag bit) : ºÔµáÊ´§Ê¶Ò¹Ð¡ÒÃà¡Ô´ÍÔ¹àµÍÃÃ»Ñ µÍ¹Ñ à¹×Íè §ÁÒ ¨Ò¡¤‹Ò¢Í§ä·àÁÍÏ 2 áÅлÃÕÊà¡ÅàÅÍÏ 2 ෋ҡѹ “0” - ¤‹Ò¢Í§ä·àÁÍÏ 2 áÅÐÃÕ¨ÊÔ àµÍϤҺàÇÅÒ 2 (Period register 2) äÁ‹à·‹Ò¡Ñ¹ “1” - ¤‹Ò¢Í§ä·àÁÍÏ 2 áÅÐÃÕ¨ÊÔ àµÍϤҺàÇÅÒ 2 (Period register 2) ෋ҡѹ ·íÒãˌà¡Ô´ÍÔ¹àµÍÃÃ»Ñ µ 䴌 ËÒ¡ÁÕ¡ÒÃàÍç¹àÍàºÔÅänj µŒÍ§à¤ÅÕÂÏ໚¹ “0” ´ŒÇ¡Ãкǹ¡Ò÷ҧ«Í¿µáÇÏ ºÔµ 0 - TMR1IF (TMR1 Overflow Interrupt Flag bit) : ºÔµáÊ´§Ê¶Ò¹Ð¡ÒÃà¡Ô´âÍàÇÍÏâ¿ÅǢͧä·àÁÍÏ 1 “0” - ä·àÁÍÏ 1 äÁ‹à¡Ô´âÍàÇÍÏâ¿ÅÇ “1” - ä·àÁÍÏ 1 à¡Ô´âÍàÇÍÏâ¿ÅÇ ·íÒãˌà¡Ô´¡ÒÃÍÔ¹àµÍÃÃ»Ñ µä´Œ ËÒ¡ÁÕ¡ÒÃàÍç¹àÍàºÔÅänj µŒÍ§à¤ÅÕÂÏ ໚¹ “0” ´ŒÇ¡Ãкǹ¡Ò÷ҧ«Í¿µáÇÏ


84 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

5.1.5 ÃÕ¨ÊÔ àµÍÏ PIR2

໚¹ÃÕ¨ÊÔ àµÍÏáÊ´§Ê¶Ò¹Ðà§×Íè ¹ä¢¡ÒÃà¡Ô´ÍÔ¹àµÍÃÃ»Ñ µà¹×Íè §¨Ò¡ÍØ»¡Ã³à¾ÍÃÔà¿ÍÃÑŵÑÇ·Õè 2 ·íÒ§Ò¹ ËÇÁ¡ÑºÃÕ¨ÊÔ àµÍÏ PIE2 àÁ×Íè à§×Íè ¹ä¢¢Í§¡ÒÃÍÔ¹àµÍÃÃ»Ñ µà»š¹¨ÃÔ§ ¨Ð૵ºÔµÊ¶Ò¹Ðã¹ÃÕ¨ÊÔ àµÍÏ PIR2 ¨Ò¡¹Ñ¹é µÃǨÊͺNjÒÁÕ¡ÒÃàÍç¹àÍàºÔÅ¡ÒÃÍÔ¹àµÍÃÃ»Ñ µ¹¹Ñé æ ·ÕÃè ¨Õ ÊÔ àµÍÏ PIE2 ËÃ×ÍäÁ‹ ¶ŒÒÁÕ¡¨ç еÃǨÊͺ Ç‹Ò ºÔµ PEIE áÅÐ GIE ã¹ÃÕ¨ÊÔ àµÍÏ INTCON ૵änj·§Ñé ¤Ùˋ Ã×ÍäÁ‹ ¶ŒÒËҡ૵änj ¡ç¨Ðà¡Ô´¡ÒÃÍÔ¹àµÍÃÃ»Ñ µ¢¹Öé ºÔµ 7 PIR2

-

ºÔµ 6

CMIF R/W -0

ºÔµ 5 -

ºÔµ 4

ºÔµ 3

EEIF

BCLIF

R/W -0

R/W -0

ºÔµ 2 -

ºÔµ 1 -

ºÔµ 0

CCP2IF R/W -0

ºÔµ 7 : äÁ‹ãªŒ§Ò¹ ¡íÒ˹´à»š¹ “0” ºÔµ 6 - CMIF (Comparator Interrupt Flag bit) : ºÔµáÊ´§Ê¶Ò¹Ð¡ÒÃà»ÃÕºà·Õºáç´Ñ¹ “0” - ÍÔ¹¾Øµ¢Í§Ç§¨Ãà»ÃÕºà·Õºáç´Ñ¹ÍйÒÅÍ¡äÁ‹à¡Ô´¡ÒÃà»ÅÕÂè ¹á»Å§ “1” - ÍÔ¹¾Øµ¢Í§Ç§¨Ãà»ÃÕºà·Õºáç´Ñ¹ÍйÒÅÍ¡à¡Ô´¡ÒÃà»ÅÕÂè ¹á»Å§ ºÔµ 5 : äÁ‹ãªŒ§Ò¹ ¡íÒ˹´à»š¹ “0” ºÔµ 4 - EEIF (EEPROM Write Operation Interrupt Flag bit) : ºÔµáÊ´§Ê¶Ò¹Ð¡ÒÃà¢Õ¹¢ŒÍÁÙÅŧã¹Ë¹‹Ç ¤ÇÒÁ¨íÒ¢ŒÍÁÙÅÍÕÍÕ¾ÃÍÁ “0” - ¡ÒÃà¢Õ¹¢ŒÍÁÙÅŧã¹Ë¹‹Ç¤ÇÒÁ¨íÒ¢ŒÍÁÙÅÍÕÍ¾Õ ÃÍÁÂѧäÁ‹ÊÁºÙóËÃ×ÍÂѧäÁ‹àÃÔÁè µŒ¹à¢Õ¹ “1” - ¡ÒÃà¢Õ¹¢ŒÍÁÙÅŧã¹Ë¹‹Ç¤ÇÒÁ¨íÒ¢ŒÍÁÙÅÍÕÍ¾Õ ÃÍÁàÊÃç¨ÊÁºÙó ºÔµ 3 - BCLIF (ÚBus Collision Interrupt Flag bit) : ºÔµáÊ´§Ê¶Ò¹Ð¡Ò깡ѹ¢Í§¢ŒÍÁÙÅ㹡Ò÷íÒ§Ò¹¢Í§ âÁ´ÙÅàª×Íè Áµ‹Í͹ءÃÁẺ«Ô§â¤Ã¹ÑÊ (SSP) àÁ×Íè ·íÒ§Ò¹ã¹âËÁ´ I2C ÁÒÊàµÍÏ “0” - äÁ‹Á¡Õ Ò깡ѹ¢Í§¢ŒÍÁÙÅ¢Ö¹é “1” - à¡Ô´¡Ò깡ѹ¢Í§¢ŒÍÁÙź¹ºÑÊ ºÔµ 2 áÅÐ 1 : äÁ‹ãªŒ§Ò¹ ¡íÒ˹´à»š¹ “0” ºÔµ 0 - CCP2IF (CCP2 Interrupt Flag bit) : ºÔµáÊ´§Ê¶Ò¹Ð¡Ò÷íÒ§Ò¹¢Í§âÁ´ÙÅ CCP2 ÊÒÁÒöẋ§á¡µÒÁ âËÁ´¡Ò÷íÒ§Ò¹¢Í§âÁ´ÙÅ CCP1 䴌´§Ñ ¹Õé âËÁ´µÃǨ¨ÑºÊÑ­­Ò³ËÃ×Í᤻à¨ÍÏ (capture mode) “0” - äÁ‹Á¡Õ ÒõÃǨ¨ÑºÊÑ­­Ò³à¡Ô´¢Ö¹é ·ÕÃè ¨Õ ÊÔ àµÍÏ TMR1 “1” - ÁÕ¡ÒõÃǨ¨ÑºÊÑ­­Ò³à¡Ô´¢Ö¹é ·ÕÃè ¨Õ ÊÔ àµÍÏ TMR1 à¤ÅÕÂϴnj ¡Ãкǹ¡Ò÷ҧ«Í¿µáÇÏ âËÁ´à»ÃÕºà·ÕºÊÑ­­Ò³ (compare mode) “0” - ¡ÒÃà»ÃÕºà·Õº¢ŒÍÁÙÅ·ÕÃè ¨Õ ÊÔ àµÍÏ TMR1 äÁ‹à·‹Ò¡Ñ¹ËÃ×ÍäÁ‹àËÁ×͹¡Ñ¹ “1” - ¡ÒÃà»ÃÕºà·Õº¢ŒÍÁÙÅ·ÕÃè ¨Õ ÊÔ àµÍÏ TMR1 ෋ҡѹËÃ×ÍàËÁ×͹¡Ñ¹ µŒÍ§à¤ÅÕÂÏ·Ò§«Í¿µáÇÏ äÁ‹Á¡Õ ÒÃ㪌§Ò¹ºÔµ¹ÕËé Ò¡¡íÒ˹´ãˌ CCP2 ·íÒ§Ò¹ã¹âËÁ´ PWM


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 85

»¯ÔºµÑ ¡Ô Ò÷Õè 6 㪌§Ò¹ÍÔ¹àµÍÃÃ»Ñ µ¨Ò¡¢Ò RB0/INT ¡ÒÃàª×Íè Áµ‹Í·Ò§ÎÒÏ´áÇÏ ÃÙ»·Õè L6-1 áÊ´§ãˌàËç¹Ç§¨Ã¢Í§äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC16F877 ·Õµè ͋ ÊÇÔµªà¢ŒÒ¡Ñº¢Ò RA4 áÅÐ RB0 â´ÂÁÕ¡Òõ‹ÍµÑǵŒÒ¹·Ò¹¾ÙÅÍÑ»à¾×Íè ¡íÒ˹´ÊÀÒÇÐÅͨԡ໚¹ “1” àÁ×Íè äÁ‹Á¡Õ Òá´ÊÇÔµª àÁ×Íè ÁÕ¡Òá´ÊÇÔµª¨Ðà»ÅÕÂè ¹ ʶҹÐ໚¹Åͨԡ “0” ʋǹ¾Íϵ C ¡íÒ˹´ãˌ໚¹¾ÍϵàÍÒµ¾µØ µ‹Í¡Ñº LED à¾×Íè áÊ´§ÊÀÒÇСÒ÷íÒ§Ò¹ ¡Òõ‹ÍÊÇÔµªà¢ŒÒ·Õ¢è Ò¾Íϵ RB0 à¾×Íè 㪌ÊÌҧÊÑ­­Ò³ÍÔ¹àµÍÃÃ»Ñ µ¨Ò¡ÀÒ¹͡ «Ö§è 㹡Ò÷´Åͧ¹Õ¨é Р㪌¤ÇÒÁÊÒÁÒö¢Í§¡ÒÃ໚¹¢ÒÍÔ¹¾ØµÃѺÊÑ­­Ò³ÍÔ¹àµÍÃÃ»Ñ µ¨Ò¡ÀÒ¹͡ (INT) ¢Í§¢Ò¾Íϵ RB0 ໚¹ËÅÑ¡ l 㪌ǧ¨Ãã¹ÃÙ»·Õè L6-1 㹡Ò÷´Åͧ l Ëҡ㪌ºÍÏ´·´Åͧ NX-877 plus II ãˌµÍ ‹ ǧ¨ÃµÒÁÃÙ»·Õè L6-1 +5V

R10 10k

R9 10k

11 32 Vcc 1

RC7 26

MCLR

SW1 RESET

33

RB0

R7

24

R6

RC4 23

R5

RC3

S1 RB0

18

RC2 17 RC1 16

6 S2 RA4

RC6 25 RC5

R11 10k

RA4

12 31

C3 0.1PF R8

RC0

15

21+$.&%%)

R4 R3 R2 R1 R1-R8 510:

GND OSC1 13

C1 22pF

OSC2 14

XTAL1 20MHz

C2 22pF

ÃÙ»·Õè L6-1 ǧ¨Ã·´Êͺ¡ÒÃÍÔ¹àµÍÃÃ»Ñ µ¨Ò¡ÀÒ¹͡¼‹Ò¹·Ò§¢Ò RB0/INT ¢Í§äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC16F877 ÀÒÂ㵌¡ÒäǺ¤ØÁ´ŒÇÂâ»Ãá¡ÃÁÀÒÉÒ C ·Õãè ªŒ mikroC ¤ÍÁä¾àÅÍÏ㹡ÒþѲ¹Òâ»Ãá¡ÃÁ


86 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

¢Ñ鹵͹¡Ò÷´Åͧ L6.1 ແ´â»Ãá¡ÃÁ mikroC ÊÌҧä¿Åâ»Ãà¨ç¡µ â´ÂàÅ×Í¡äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏáÅФÇÒÁ¶ÕÊè ­ Ñ ­Ò³¹Ò́¡Ò´Ñ§¹Õé l ¡Ã³Õ㪌ºÍÏ´ NX-877 plus II àÅ×Í¡ PIC16F877 ËÃ×Í PIC16F877A ¤ÇÒÁ¶ÕÊ è­ Ñ ­Ò³¹Ò́¡Ò 20MHz L6.2 ÊÌҧâ»Ãà¨ç¡µªÍ×è BasicInterrupt áÅоÔÁ¾â»Ãá¡ÃÁÀÒÉÒ C µÒÁâ»Ãá¡ÃÁ·Õè L6-1 L6.3 ·íÒ¡ÒäÍÁä¾ÅáÅдÒǹâËÅ´â»Ãá¡ÃÁà¾×Íè ·´Êͺ¼ÅÅѾ¸¡Ò÷íÒ§Ò¹ L6.4 Ãѹâ»Ãá¡ÃÁ àÁ×Íè äÁ‹¡´ÊÇÔµª â»Ãá¡ÃÁ¨Ð·íÒ§Ò¹µÒÁâ»Ãá¡ÃÁ»¡µÔ ¤×Í ãˌ LED µÔ´ÊÅѺ¡Ñ¹ ᵋàÁ×Íè ¡´ÊÇÔµª·¢Õè Ò RB0/ INT ·íÒãˌ LED ¡Ð¾ÃÔºÍÕ¡ÃÙ»áººË¹Ö§è ·Ñ¹·Õ «Ö§è ËÁÒ¤ÇÒÁÇ‹Ò äÁ‹Çҋ â»Ãá¡ÃÁ¨Ð·íÒ§Ò¹»¡µÔ ´ŒÇ¤íÒÊѧè ã´ÍÂÙà‹ Á×Íè ÁÕ ¡ÒÃÍÔ¹àµÍÃÑ»µ «Õ¾ÂÕ ¨Ù СÃÐâ´´ä»·íÒ§Ò¹·Õâè »Ãá¡ÃÁ‹ÍºÃÔ¡ÒÃÍÔ¹àµÍÃÃ»Ñ µ·¹Ñ ·Õ char i; void interrupt() { for(i=0;i<10;i++) { PORTC=0xFF; Delay_ms(100); PORTC=0x00; Delay_ms(100); } INTCON.INTF=0; } void main() { TRISC=0x00; INTCON.INTF=0; INTCON.INTE=1; INTCON.GIE=1; while(1) { PORTC=0x55; Delay_ms(200); PORTC=0xAA; Delay_ms(200); } }

// Interrupt Service Routine // Blinking LED when interrupt

// Clear Interrupt flag // Main program // // // // //

PORTC ==> Output Clear Interrupt flag before use Enable interrupt from INT/RB0 Pin Enable Global interrupt Main program

// Running LED

¡Ò÷íÒ§Ò¹¢Í§â»Ãá¡ÃÁ

àÃÔèÁµŒ¹â»Ãá¡ÃÁ´ŒÇ¿˜§¡ªÑè¹ interrupt «Öè§àÁ×èÍà¡Ô´ÍÔ¹àµÍÏÃÑ»µâ»Ãá¡ÃÁ¨Ð¡ÃÐâ´´ÁÒ·íÒ§Ò¹·Õè¹Õè·Ñ¹·Õ â´Ââ»Ãá¡ÃÁã¹Ê‹Ç¹¹Õ¨é Ð໚¹¡ÒáоÃÔº LED ¤¹ÅÐÃٻẺ¡Ñºâ»Ãá¡ÃÁËÅÑ¡ áÅÐàÁ×Íè ¨º§Ò¹ã¹Ê‹Ç¹ºÃÔ¡Òà ÍÔ¹àµÍÃÃ»Ñ µáŌǨеŒÍ§à¤ÅÕÂÏá¿Å¡ÍÔ¹àµÍÃÃ»Ñ µ¡Í‹ ¹ÍÍ¡¨Ò¡ÍÔ¹àµÍÃÃ»Ñ µàÊÁÍ ÊíÒËÃѺâ»Ãá¡ÃÁËÅÑ¡ ¨ÐàÃÔÁè ¨Ò¡¡ÒÃà¤ÅÕÂÏá¿Å¡ÍÔ¹àµÍÃÃ»Ñ µ ¨Ò¡¹Ñ¹é àÍç¹àÍàºÔÅ¡ÒÃÍÔ¹àµÍÃÃ»Ñ µ¨Ò¡¢Ò INT áÅÐàÍç¹àÍàºÔÅÍÔ¹àµÍÃÃ»Ñ µ ÃÇÁ ¡‹Í¹ä»Ç¹ÅÙ»·íÒ§Ò¹»¡µÔ Ãͨ¹¡Ç‹Ò¨Ðà¡Ô´ÍÔ¹àµÍÃÃ»Ñ µ

â»Ãá¡ÃÁ·Õè L6-1 ä¿Å BasicInterrupt.c â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺ mikroC ¤ÍÁä¾àÅÍÏà¾×Íè ·´Êͺ¡ÒÃÍÔ¹àµÍÃÃ»Ñ µ¨Ò¡ÀÒ¹͡¢Í§äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC16F877(A)

INNOVATIVE EXPERIMENT


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 87

º··Õè 6

¡ÒõԴµ‹Í¡ÑºâÁ´ÙÅ LCD ´ŒÇ ¿˜§¡ª¹Ñè ¢Í§ mikroC ¤ÍÁä¾àÅÍÏ âÁ´ÙÅ LCD ໚¹âÁ´ÙÅáÊ´§¼ÅµÑÇÍÑ¡É÷ÕÁè ¼Õ ¹ÙŒ ÂÔ Á㪌¡¹Ñ ÁÒ¡ à¹×Íè §¨Ò¡ÊÒÁÒöà¢Õ¹â»Ãá¡ÃÁ µÔ´µ‹Í䴌§Ò‹  áÅÐÊÒÁÒöáÊ´§µÑÇÍÑ¡Éà (ÀÒÉÒÍѧ¡ÄÉ) 䴌¤Ãº¶ŒÇ¹ ·Ñ§é ÂѧÁÕÃÒ¤ÒäÁ‹Ê§Ù ¹Ñ¡ âÁ´ÙÅ LCD ẺµÑÇÍÑ¡Éà (character LCD module) ÁÕÊNj ¹»ÃСͺËÅÑ¡´Ñ§¹Õé 1. ʋǹáÊ´§¼Å ໚¹¼ÅÖ¡àËÅÇ (liquid crystal) ·Õ·è Òí ãˌàËç¹µÑÇÍÑ¡¢ÃÐ䴌â´ÂÍÒÈÑÂáʧ ¨Ò¡ÀÒ¹͡ ÁÕ¨Òí ¹Ç¹µÑÇÍÑ¡Éõѧé ᵋ 8 µÑÇÍÑ¡Éà 1 ºÃ÷Ѵ 仨¹¶Ö§ 40 µÑÇÍÑ¡Éà 4 ºÃ÷Ѵ Ãع‹ ·Õ¹è ÂÔ Á 㪌ÁÒ¡·ÕÊè ´Ø à»š¹Ãع‹ 16 µÑÇÍÑ¡Éà 2 ºÃ÷Ѵ 2. ʋǹ¤Çº¤ØÁ ໚¹µÑÇÃѺ¢ŒÍÁÙŨҡÀÒ¹͡à¾×Íè 㪌¤Çº¤ØÁ¡Ò÷íÒ§Ò¹¢Í§âÁ´ÙÅ LCD ઋ¹ ¡ÒÃź¨ÍÀÒ¾ ¡ÒÃáÊ´§µÑÇÍÑ¡Éà ËÃ×Í¡ÒÃàÅ×Íè ¹à¤ÍÏà«ÍÏ ໚¹µŒ¹ µÑǤǺ¤ØÁ¨Ð㪌ª»Ô ¤Çº¤ØÁ â´Â੾ÒÐ àºÍÏ·àÕè »š¹Áҵðҹ¤×Í HD44780 ã¹»˜¨¨Øº¹Ñ ÁÕ¡ÒÃ㪌àºÍÏ͹×è æ ·´á·¹ ᵋû٠Ẻ¡Òà µÔ´µ‹ÍÂѧµÃ§¡ÑºàºÍÏ HD44780 ¢Ò 1 : GND ¢Ò 2 : +V ¢Ò 3 : Vo ¢Ò 4 : RS ¢Ò 5 : R/W ¢Ò 6 : E ¢Ò 7 ¶Ö§ 14 : D0 ¶Ö§ D7

ÃÙ»·Õè 6-1 áÊ´§ÃٻËҧáÅеíÒá˹‹§¢Ò¢Í§âÁ´ÙÅ LCD Áҵðҹ ÊíÒËÃѺÃع‹ ·ÕÁè äÕ ¿Ê‹Í§ËÅѧ (back light) ¨ÐÁÕ¢Òà¾ÔÁè ÍÕ¡ 2 ¢Ò (¢Ò 15 áÅÐ 16) ÊíÒËÃѺ¤Çº¤ØÁ¡Ò÷íÒ§Ò¹¢Í§ä¿Ê‹Í§ËÅѧ


88 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

6.1 ¢ÒÊÑ­­Ò³¢Í§âÁ´ÙÅ LCD ¢Ò·Õè 1 ¢Ò·Õè 2 ¢Ò·Õè 3 ¢Ò·Õè 4

GND +V Vo RS

µ‹Íŧ¡ÃÒÇ´ µ‹ÍࢌҡѺä¿àÅÕÂé § +5V ໚¹¢ÒÃѺÍÔ¹¾Øµáç´Ñ¹à¾×èÍ»ÃѺ¤ÇÒÁÊNjҧ ໚¹¢ÒÍÔ¹¾Øµà¾×èÍᡪ¹Ô´¢Í§¢ŒÍÁÙŤíÒÊÑè§ËÃ×Í¢ŒÍÁÙÅáÊ´§¼Å ¶ŒÒ¢Ò¹Õàé »š¹ “0” ¤‹Ò·ÕÊè §‹ ÁÒ¨Ð໚¹¤íÒÊÑ§è ¶ŒÒ¢Ò¹Õàé »š¹ “1” ¤‹Ò·ÕÊè §‹ ÁÒ¨Ð໚¹¢ŒÍÁÙÅáÊ´§¼Å ¢Ò·Õè 5 R/W ໚¹¢ÒÍÔ¹¾Øµ·Õãè ªŒà¾×Íè àÅ×Í¡¡ÒÃ͋ҹËÃ×Íà¢Õ¹¢ŒÍÁÙšѺâÁ´ÙÅ LCD “0” ໚¹¡ÒÃà¢Õ¹¢ŒÍÁÙÅ “1” ໚¹¡ÒÃ͋ҹ¢ŒÍÁÙÅ »¡µÔ¡Òí ˹´à»š¹ “0” à¹×Íè §¨Ò¡¡ÒÃ㪌§Ò¹»¡µÔÁ¡Ñ ¨ÐäÁ‹Á¡Õ ÒÃ͋ҹ¤‹Ò ¢Ò·Õè 6 E ໚¹¢ÒÍÔ¹¾ØµãªŒà¾×Íè ¡ÒÃàÍç¹àÍàºÔÅ¡ÒõԴµ‹Í¡ÑºâÁ´ÙÅ LCD ¢Ò·Õè 7 ¶Ö§ 14 D0 ¶Ö§ D7 ໚¹¢Ò¢ŒÍÁÙŢͧâÁ´ÙÅ LCD ËÒ¡µŒÍ§¡ÒõԴµ‹ÍẺ 8 ºÔµ ¨Ð㪌¢Ò¢ŒÍÁÙŤú·Ñ§é 8 ¢Ò ËÒ¡µŒÍ§¡ÒõԴµ‹ÍẺ 4 ºÔµ ¨Ð㪌¢Ò D4 ¶Ö§ 7 +5V 11

32

C6 0.1PF 50V

+5V

1+! 21+$.&&% RD2

+5V

RD3

R5 4k7 SW2 RESET

1 R6 1k

MCLR

RD7 RD6 RD5 RD4

JP1

21

4

22

6

30

RS E

DSP1 LCD 16x2

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

BLA +V Vo BLK GND 15 2

R13 16 47

BACK LIGHT ON

1 3 VR1 10k

BRIGHTNESS

29 28 27 12 31

CR1 Ceramic Resonator 20MHz

13

14

ÃÙ»·Õè 6-2 áÊ´§¡ÒÃàª×Íè Áµ‹ÍâÁ´ÙÅ LCD º¹ºÍÏ´ RBX-877 Rev.2007 «Ö§è µÃ§¡Ñº¿˜§¡ª¹Ñè Lcd_Init()


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 89

6.2 ¿˜§¡ª¹Ñè µÔ´µ‹Í¡ÑºâÁ´ÙÅ LCD ẺÁҵðҹ ã¹ mikroC ÁÕ¿§˜ ¡ª¹Ñè ÁҵðҹÊíÒËÃѺµÔ´µ‹Í¡ÑºâÁ´ÙÅ LCD à¾×Íè ÍíҹǤÇÒÁÊдǡ㹡ÒÃÒ ¾Ñ²¹Òâ»Ãá¡ÃÁ â´Â㹿˜§¡ª¹Ñè Áҵðҹ¹Ñ¹é ¨ÐàÅ×Í¡¡ÒõԴµ‹Í¡ÑºâÁ´ÙÅ LCD Ẻ 4 ºÔµ ¨Ö§ãªŒ¢Ò ¢ŒÍÁÙÅ D4 ¶Ö§ D7 ෋ҹѹé ʋǹ¢Ò¾Íϵ¢Í§äÁâ¤Ã¤Í¹â·ÃàÅÍÏ PIC ·Õµè ͋ ¡ÑºâÁ´ÙÅ LCD ¨ÐµŒÍ§ÍÂً ÀÒÂã¹¾Íϵà´ÕÂǡѹ

6.2.1 ¿˜§¡ª¹Ñè ¡ÒÃàµÃÕÂÁ¡ÒõԴµ‹Í¡ÑºâÁ´ÙÅ LCD 6.2.1.1 ¿˜§¡ª¹Ñè Lcd_Init ¿˜§¡ª¹Ñè ¹Õàé »š¹¡ÒáíÒ˹´¤‹ÒàÃÔÁè µŒ¹ãˌ¡ºÑ ¡ÒõԴµ‹ÍâÁ´ÙÅ LCD â´Â¡íÒ˹´µíÒá˹‹§¢Òµ‹Ò§ æ àÍÒänjáÅŒÇ ¡ÒûÃѺà»ÅÕÂè ¹¤‹Ò»ÃѺà»ÅÕÂè ¹ä´Œà©¾ÒеíÒá˹‹§¾Íϵ·Õãè ªŒàª×Íè Áµ‹Í෋ҹѹé â´ÂÃÒÂÅÐàÍÕ´ µíÒá˹‹§¢Ò·Õãè ªŒàª×Íè Áµ‹ÍÃÐËNjҧâÁ´ÙÅ LCD ¡Ñº¢Ò¾Íϵ¢Í§äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ໚¹´Ñ§¹Õé D7

àª×Íè Áµ‹Í¡Ñº¢Ò¾ÍϵºÔµ 7

D6

àª×Íè Áµ‹Í¡Ñº¢Ò¾ÍϵºÔµ 6

D5

àª×Íè Áµ‹Í¡Ñº¢Ò¾ÍϵºÔµ 5

D4

àª×Íè Áµ‹Í¡Ñº¢Ò¾ÍϵºÔµ 4

E

àª×Íè Áµ‹Í¡Ñº¢Ò¾ÍϵºÔµ 3

RS

àª×Íè Áµ‹Í¡Ñº¢Ò¾ÍϵºÔµ 2

ÃٻẺ¡ÒÃ㪌§Ò¹¤íÒÊѧè void Lcd_Init(unsigned short *port);

¾ÒÃÒÁÔàµÍÏ port

ËÁÒ¶֧ µíÒá˹‹§¾Íϵ¢Í§äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ·ãÕè ªŒàª×Íè Áµ‹Í¡ÑºâÁ´ÙÅ LCD

µÑÇÍ‹ҧ·Õè 6-1 Lcd_Init(&PORTB);

໚¹¡ÒÃ㪌§Ò¹ PORTB 㹡ÒÃàª×Íè Áµ‹Í¡ÑºâÁ´ÙÅ LCD


90 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

6.2.1.2 ¿˜§¡ª¹Ñè Lcd_Config ¡íÒ˹´µíÒá˹‹§¾ÍϵáÅеíÒá˹‹§¢Òµ‹Ò§æ ·Õè㪌àª×èÍÁµ‹Í¡ÑºâÁ´ÙÅ LCD ¾ÃŒÍÁ·Ñ駡íÒ˹´ ¿˜§¡ª¹Ñè ¡Ò÷íÒ§Ò¹¾×¹é °Ò¹ãˌ¡ºÑ âÁ´ÙÅ LCD â´ÂµíÒá˹‹§¢Ò·Ñ§é ËÁ´·Õ¡è Òí ˹´ÊÒÁÒöàÅ×͡䴌Í‹ҧ ÍÔÊÃРᵋÁàÕ §×Íè ¹ä¢Ç‹Ò µŒÍ§ÁÒ¨Ò¡¾Íϵà´ÕÂǡѹ «Ö§è ᵡµ‹Ò§¨Ò¡¿˜§¡ª¹Ñè Lcd_Init() ·Õè¡íÒ˹´¢Ò ¾ÍϵänjáŌÇᵋáá ÃٻẺ¡ÒÃ㪌§Ò¹¤íÒÊѧè void Lcd_Config(unsigned short *port, unsigned short RS, unsigned short EN, unsigned short WR, unsigned short D7, unsigned short D6, unsigned short D5, unsigned short D4);

¾ÒÃÒÁÔàµÍÏ

*port ËÁÒ¶֧ ¾Íϵ¢Í§äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ·Õàè ª×Í è Áµ‹Í¡ÑºâÁ´ÙÅ LCD â´Â¢Ò¤Çº¤ØÁáÅТÒ

¢ŒÍÁÙŷѧé ËÁ´·Õµè ͋ ࢌҡѺ¨Ò¡äÁâ¤Ã¤Í¹â·ÃÅàÅÍϨеŒÍ§ÁÒ¨Ò¡¾Íϵà´ÕÂǡѹ RS

໚¹µíÒá˹‹§ºÔµ¢Í§¾Íϵ·Õàè ª×Íè Áµ‹Í¡Ñº¢Ò RS ¢Í§âÁ´ÙÅ LCD

EN

໚¹µíÒá˹‹§ºÔµ¢Í§¾Íϵ·Õàè ª×Íè Áµ‹Í¡Ñº¢Ò EN ¢Í§âÁ´ÙÅ LCD

WR

໚¹µíÒá˹‹§ºÔµ¢Í§¾Íϵ·Õàè ª×Íè Áµ‹Í¡Ñº¢Ò R/W ¢Í§âÁ´ÙÅ LCD

D7,D6,D5,D4

໚¹µíÒá˹‹§ºÔµ¢Í§¾Íϵ·Õàè ª×Íè Áµ‹Í¡Ñº¢Ò D4 ¶Ö§ D7 ¢Í§âÁ´ÙÅ LCD

µÑÇÍ‹ҧ·Õè 6-2 Lcd_Config(PORTD,1,2,0,3,4,5,6);

໚¹¡ÒáíÒ˹´ãˌ PORTD ໚¹¾Íϵ·Õãè ªŒàª×Íè Áµ‹Í¡ÑºâÁ´ÙÅ LCD ¢Ò RD1 µ‹Í¡Ñº¢Ò RS ¢Í§âÁ´ÙÅ LCD ¢Ò RD2 µ‹Í¡Ñº¢Ò EN ¢Ò RD0 µ‹Í¡Ñº¢Ò R/W ¢Ò RD3, RD4, RD5, RD6 µ‹Í¡Ñº¢Ò D7, D6, D5, D4 µÒÁÅíҴѺ


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 91

6.2.2 ¿˜§¡ª¹Ñè ʋ§¢ŒÍÁÙÅáÊ´§¼Å 6.2.2.1 ¿˜§¡ª¹Ñè Lcd_Out 㪌ÊíÒËÃѺʋ§¢ŒÍÁÙÅÍÍ¡ä»áÊ´§¼Å·ÕèâÁ´ÙÅ LCD â´Â¨ÐµŒÍ§ÃкصíÒá˹‹§á¶Ç (row) áÅÐ µíÒá˹‹§µÑÇÍÑ¡Éà (col) ¡‹Í¹·Õ¨è Ðʋ§¢ŒÍÁÙÅÍÍ¡ä» â´ÂÃٻẺ¢Í§¢ŒÍÁÙŨÐ໚¹µÑÇÍÑ¡ÉõÑÇà´ÕÂÇËÃ×Í໚¹ ¢ŒÍ¤ÇÒÁ¡ç䴌 â´Â¡‹Í¹ãªŒ§Ò¹¿˜§¡ª¹Ñè ¹Õ¨é еŒÍ§ÁÕ¡ÒÃàÃÕ¡¿˜§¡ª¹Ñè Lcd_Init ËÃ×Í Lcd_Config ¡‹Í¹ ÃٻẺ¡ÒáÒÃ㪌§Ò¹¤íÒÊѧè void Lcd_Out(unsigned short row, unsigned short col, char *text);

¾ÒÃÒÁÔàµÍÏ ËÁÒ¶֧ µíÒá˹‹§á¶Ç¢Í§âÁ´ÙÅ LCD col ËÁÒ¶֧ µíÒá˹‹§µÑÇÍÑ¡ÉÃã¹á¶Ç·ÕÃè ÐºØ *text ËÁÒ¶֧ µÑÇÍÑ¡ÉÃËÃ×Í¢ŒÍ¤ÇÒÁ·Õµ è ͌ §¡ÒùíÒä»áÊ´§¼Å row

µÑÇÍ‹ҧ·Õè 6-3 Lcd_Out(1, 3, “Hello!”);

໚¹¡ÒÃʋ§¢ŒÍ¤ÇÒÁ Hello ÍÍ¡ä»áÊ´§¼Å·Õ¨è Í LCD 㹺Ã÷Ѵ·Õè 1 àÃÔÁè ·Õµè Òí á˹‹§µÑÇÍÑ¡ÉõÑÇ·Õè 3

Hello


92 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

6.2.2.2 ¿˜§¡ª¹Ñè Lcd_Out_Cp ໚¹¤íÒÊÑ§è ·Õ·è Òí §Ò¹àËÁ×͹¡Ñº¤íÒÊѧè LCD_Out ᵋ¤Òí ÊÑ§è ¹Õàé »š¹¡ÒÃʋ§¢ŒÍ¤ÇÒÁä»áÊ´§·Õµè Òí á˹‹§ à¤ÍÏà«ÍÏ»˜¨¨ØºÑ¹ ¨Ö§äÁ‹¨íÒ໚¹µŒÍ§ÃкصíÒá˹‹§à¤ÍÏà«ÍÏ¡‹Í¹ãªŒ¿˜§¡ªÑè¹¹Õé Í‹ҧäáçµÒÁ¡‹Í¹ãªŒ §Ò¹¿˜§¡ª¹Ñè ¹Õ¨é еŒÍ§ÁÕ¡ÒÃàÃÕ¡¿˜§¡ª¹Ñè Lcd_Init ËÃ×Í Lcd_Config ¡‹Í¹ ÃٻẺ¡ÒÃ㪌§Ò¹¤íÒÊѧè void Lcd_Out_Cp(char *text);

¾ÒÃÒÁÔàµÍÏ *text ËÁÒ¶֧

µÑÇÍÑ¡ÉÃËÃ×Í¢ŒÍ¤ÇÒÁ·Õµè ͌ §¡ÒùíÒä»áÊ´§

µÑÇÍ‹ҧ·Õè 6-3 Lcd_Out_Cp(“Here!”);

໚¹¡ÒÃáÊ´§¢ŒÍ¤ÇÒÁ 0AHA ·Õâè Á´ÙÅ LCD ã¹µíÒá˹‹§à¤ÍÏà«ÍÏ»¨˜ ¨Øº¹Ñ

6.2.2.3 ¿˜§¡ª¹Ñè Lcd_Chr ໚¹¡ÒÃʋ§¢ŒÍÁÙÅà¾Õ§亵à´ÕÂÇÍÍ¡áÊ´§¼Å·Õâè Á´ÙÅ LCD â´Â¡‹Í¹¡ÒÃʋ§¨ÐµŒÍ§ÁÕ¡ÒáíÒ˹´ µíÒá˹‹§á¶Ç (row) áÅÐ µíÒá˹‹§µÑÇÍÑ¡Éà (col) ¡‹Í¹·Õ¨è Ðʋ§¢ŒÍÁÙÅÍÍ¡ä» Í‹ҧäáçµÒÁ¡‹Í¹ãªŒ§Ò¹ ¿˜§¡ª¹Ñè ¹Õ¨é еŒÍ§ÁÕ¡ÒÃàÃÕ¡¿˜§¡ª¹Ñè Lcd_Init ËÃ×Í Lcd_Config ¡‹Í¹ ÃٻẺ¡ÒÃ㪌§Ò¹¤íÒÊѧè void Lcd_Chr(unsigned short row, unsigned short col, char character);

¾ÒÃÒÁÔàµÍÏ ËÁÒ¶֧ µíÒá˹‹§á¶Ç¢Í§âÁ´ÙÅ LCD col ËÁÒ¶֧ µíÒá˹‹§µÑÇÍÑ¡ÉÃã¹á¶Ç·ÕÃè ÐºØ character ËÁÒ¶֧ µÑÇÍÑ¡Éà 1 亵·µ Õè ͌ §¡ÒùíÒä»áÊ´§ row

µÑÇÍ‹ҧ·Õè 6-4 char i ; void main() { i = 0x30 ; TRISC = 0; Lcd_Init(&PORTC); do { Lcd_Chr(1,1,i);

// Start i with 0x20 in ASCII // PORTC is output // Initial default on PORTC // show i on LCD


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹ i++ ; delay_ms(500);

l 93

// Increment i

} while(i < 0x3f) ;

// Loop Until I more than 0x3F

}

໚¹¡ÒùíÒ¤‹Ò i ä»áÊ´§¼Å·Õâè Á´ÙÅ LCD â´ÂÁÕ¡ÒÃà¾ÔÁè ¤‹Ò¢Í§µÑÇá»Ã i ¨Ò¡ 0x30 仨¹¶Ö§ ¤‹Ò 0x3F µíÒá˹‹§¡ÒÃáÊ´§¼Å¨Ð¤§·ÕÍè ÂÙ·‹ áÕè ¶Ç·Õè 1 ¤ÍÅÑÁ¹·Õè 1

6.2.2.4 ¿˜§¡ª¹Ñè Lcd_Chr_Cp ໚¹¡ÒÃʋ§¢ŒÍÁÙÅ亵à´ÕÂÇÍÍ¡ä»áÊ´§¼Å·ÕèâÁ´ÙÅ LCD ᵋäÁ‹ÁÕ¡ÒÃÃкصíÒá˹‹§·Õ赌ͧ¡Òà ʋ§¢ŒÍÁÙÅ â´Â¨Ðʋ§¢ŒÍÁÙÅä»áÊ´§·ÕèµíÒá˹‹§à¤ÍÏà«ÍÏ»˜¨¨ØºÑ¹ Í‹ҧäáçµÒÁ¡‹Í¹ãªŒ§Ò¹¿˜§¡ªÑè¹¹Õé ¨ÐµŒÍ§ÁÕ¡ÒÃàÃÕ¡¿˜§¡ª¹Ñè Lcd_Init ËÃ×Í Lcd_Config ¡‹Í¹ ÃٻẺ¡ÒÃ㪌§Ò¹¤íÒÊѧè void Lcd_Chr_Cp(char character);

¾ÒÃÒÁÔàµÍÏ character

ËÁÒ¶֧ µÑÇÍÑ¡Éà 1 亵·µÕè ͌ §¡ÒùíÒä»áÊ´§

µÑÇÍ‹ҧ·Õè 6-5 char i ; void main() { i = 0x30 ; TRISD = 0; Lcd_Init(&PORTD); Lcd_Cmd(LCD_SHIFT_RIGHT); do { Lcd_Chr_Cp(i); i++ ; delay_ms(500); } while(i < 0x3f) ; }

// Start i with 0x20 in ASCII // PORTD is output // Initial default on PORTD

// show i on LCD // Increment i

// Loop Until I more than 0x3F

¨Ò¡µÑÇÍ‹ҧ·íҧҹ䴌ઋ¹à´ÕÂǡѺ¤íÒÊѧè LCD_Chr ᵋ¨ÐàËç¹Ç‹Ò àÁ×Íè àÅ×Íè ¹à¤ÍÏà«ÍϵÇÑ ÍÑ¡É÷ÕÍè ÂÙº‹ ¹ ˹ŒÒ¨ÍâÁ´ÙÅ LCD ¡ç¨ÐàÃÕ§µ‹Íà¹×Íè §¡Ñ¹ä´Œ ´Ñ§ÃÙ» 01234567890:;<= >


94 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

6.2.2.5 ¿˜§¡ª¹Ñè Lcd_Cmd ໚¹¤íÒÊѧè à¾×Íè ¤Çº¤ØÁ¡Ò÷íÒ§Ò¹¢Í§âÁ´ÙÅ LCD ã¹âËÁ´µ‹Ò§æ ÃÒÂÅÐàÍÕ´¢Í§¤íÒÊÑ§è µ‹Ò§ æ áÊ´§ ã¹µÒÃÒ§µ‹Í仹Õé ª×èͤíÒÊÑè§ LCD_FIRST_ROW LCD_SECOND_ROW LCD_THIRD_ROW LCD_FOURTH_ROW LCD_CLEAR LCD_RETURN_HOME LCD_CURSOR_OFF LCD_UNDERLINE_ON LCD_BLINK_CURSOR_ON LCD_MOVE_CURSOR_LEFT LCD_MOVE_CURSOR_RIGHT LCD_TURN_ON LCD_TURN_OFF LCD_SHIFT_LEFT Shift LCD_SHIFT_RIGHT

ÃÒÂÅÐàÍÕ´ ŒÒÂà¤ÍÏà«ÍÏä»Âѧá¶Ç·Õè 1 áÅÐàÃÔèÁµŒ¹·Õè¤ÍÅÑÁ¹·Õè 1 ŒÒÂà¤ÍÏà«ÍÏä»Âѧá¶Ç·Õè 2 áÅÐàÃÔèÁµŒ¹·Õè¤ÍÅÑÁ¹·Õè 1 ŒÒÂà¤ÍÏà«ÍÏä»Âѧá¶Ç·Õè 3 áÅÐàÃÔèÁµŒ¹·Õè¤ÍÅÑÁ¹·Õè 1 ŒÒÂà¤ÍÏà«ÍÏä»Âѧá¶Ç·Õè 4 áÅÐàÃÔèÁµŒ¹·Õè¤ÍÅÑÁ¹·Õè 1 à¤ÅÕÂÏ˹ŒÒ¨Í ä»ÂѧµíÒá˹‹§àÃÔèÁµŒ¹ (á¶Ç 1 ¤ÍÅÑÁ¹ 1 ) ¢ŒÍÁÙÅã¹ RAM äÁ‹à»ÅÕè¹ »´¡ÒÃáÊ´§¼Åà¤ÍÏà«ÍÏ áÊ´§à¤ÍÏà«ÍÏ ໚¹àʌ¹ªÔ´¢ÍºÅ‹Ò§¤ŒÒ§änj áÊ´§à¤ÍÏà«ÍÏẺ¡Ð¾ÃÔº àÅ×è͹à¤ÍÏà«ÍÏ价ҧ´ŒÒ¹«ŒÒ ¢ŒÍÁÙÅ˹‹Ç¤ÇÒÁ¨íÒ RAM äÁ‹à»ÅÕè¹ àÅ×è͹à¤ÍÏà«ÍÏ价ҧ´ŒÒ¹¢ÇÒ ¢ŒÍÁÙÅ˹‹Ç¤ÇÒÁ¨íÒ RAM äÁ‹à»ÅÕè¹ ແ´¡ÒÃáÊ´§¼Å¢Í§¨Í LCD »´¡ÒÃáÊ´§¼Å¢Í§¨Í LCD àÅ×è͹¢ŒÍÁÙÅä»·Ò§«ŒÒ ¢ŒÍÁÙÅ˹‹Ç¤ÇÒÁ¨íÒ RAM äÁ‹à»ÅÕè¹ àÅ×è͹¢ŒÍÁÙÅä»·Ò§¢ÇÒ ¢ŒÍÁÙÅã¹Ë¹‹Ç¤ÇÒÁ¨íÒ RAM äÁ‹à»ÅÕè¹

ÃٻẺ¡ÒÃ㪌§Ò¹¤íÒÊѧè void Lcd_Cmd(unsigned short command);

¾ÒÃÒÁÔàµÍÏ Command

ËÁÒ¶֧ ¤íÒÊѧè à¾×Íè ÊÑ§è §Ò¹ãˌâÁ´ÙÅ LCD ·íÒ§Ò¹ã¹âËÁ´µ‹Ò§ æ


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 95

»¯ÔºµÑ ¡Ô Ò÷Õè 7 ʋ§¢ŒÍÁÙÅáÊ´§¼Å·Õâè Á´ÙÅ LCD â´Â㪌¿§˜ ¡ª¹Ñè Lcd ¡ÒÃàª×èÍÁµ‹Í·Ò§ÎÒÏ´áÇÏ 㪌ǧ¨Ãã¹ÃÙ»·Õè L7-1 㹡Ò÷´Åͧ ÊÒÁÒö㪌äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏàºÍÏ PIC16F877 ËÃ×Í PIC16F887 ¡ç䴌 ᵋµÍŒ §àÅ×Í¡ãˌ¶¡Ù µŒÍ§àÁ×Íè ·íÒ¡ÒÃÊÌҧä¿Åâ»Ãà¨ç¡µ´ÇŒ  l Ëҡ㪌ºÍÏ´ RBX-877 Rev.2007 ÊÒÁÒö·´Åͧ䴌àÅ â´ÂàÅ×Í¡äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ໚¹àºÍÏ PIC16F887 l Ëҡ㪌ºÍÏ´·´Åͧ NX-877 plus II ãˌµÍ ‹ ǧ¨ÃµÒÁÃÙ»·Õè L7-1 áŌÇàÅ×Í¡äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ໚¹ PIC16F877 l

¢Ñ鹵͹¡Ò÷´Åͧ L7.1 ແ´â»Ãá¡ÃÁ mikroC ÊÌҧä¿Åâ»Ãà¨ç¡µ LDCtest01 áÅоÔÁ¾â»Ãá¡ÃÁÀÒÉÒ C µÒÁâ»Ãá¡ÃÁ·Õè L7-1 L7.3 ·íÒ¡ÒäÍÁä¾ÅáÅдÒǹâËÅ´â»Ãá¡ÃÁà¾×Íè ·´Êͺ¼ÅÅѾ¸¡Ò÷íÒ§Ò¹ L7.4 Ãѹâ»Ãá¡ÃÁ ¨ÐàË繡ÒÃáÊ´§¼Å¢ŒÍ¤ÇÒÁº¹¨ÍáÊ´§¼Å¢Í§âÁ´ÙÅ LCD ·Ñ§é 2 ºÃ÷Ѵã¹ÃٻẺµ‹Ò§æ ËÅÒ¡ËÅÒ äÁ‹Çҋ ¨Ð໚¹¡ÒÃແ´»´Ë¹ŒÒ¨Í àÅ×Íè ¹¢ŒÍÁÙÅ àÅ×Íè ¹à¤ÍÏà«ÍÏ áÊ´§¢ŒÍÁŵÑÇÍÑ¡Éà áÊ´§¢ŒÍÁÙŵÑÇàÅ¢ +5V 11

32

C6 0.1PF 50V

+5V

1+! 21+$.&&%

+5V

SW2 4-5-6

RD2 21

4

22

6

RD3

R5 4k7

1 R6 1k

MCLR

JP1

RD7 RD6 RD5 RD4

30

RS E

DSP1 LCD 16x2

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

BLA +V Vo BLK GND 15 2

R13 16 47

BACK LIGHT ON

1 3 VR1 10k

BRIGHTNESS

29 28 27 12 31

CR1 Ceramic Resonator 20MHz

13

14

ÃÙ»·Õè L7-1 ǧ¨Ã·´ÅͧáÊ´§¼Åº¹âÁ´ÙÅ LCD ´ŒÇ¿˜§¡ª¹Ñè Lcd ¢Í§ mikroC ¤ÍÁä¾àÅÍÏ


96 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

char *text1 = "Innovative"; char *text2 = "Experiment"; char txt[4]; char i; void main() { Delay_ms(1000); TRISD =0x00; Lcd_init(&PORTD); Lcd_Cmd(LCD_CURSOR_OFF) ; for (i=0;i<4;i++) { Lcd_Cmd(LCD_CLEAR); Delay_ms(500); Lcd_Out(1, 3, text1); Lcd_Out(2, 3, text2); Delay_ms(1000); } for (i=0;i<11;i++) { Lcd_Cmd(LCD_SHIFT_LEFT); Delay_ms(200); } for (i=0;i<11;i++) { Lcd_Cmd(LCD_SHIFT_RIGHT); Delay_ms(200); } for (i=0;i<4;i++) { Lcd_Cmd(LCD_CLEAR); Delay_ms(300); Lcd_Out(1, 3, text1); Lcd_Out(2, 3, text2); Delay_ms(300); } Lcd_Cmd(LCD_CLEAR); i=0; while(1) { i++; Lcd_Out(1,1,"Counter "); ByteToStr(i,txt); Lcd_Out_cp(txt); Delay_ms(400); } }

// // // // //

Start up Dalay PORTD ==> OUTPUT for LCD Initialize LCD connected to PORTD Turn cursor off Blink lcd

// Clear display // Print text to LCD, 2nd row, 1st column // Print text to LCD, 2nd row, 1st column // Shift data left

// Shift data right

// blink lcd // Clear display // Print text to LCD, 2nd row, 1st column // Print text to LCD, 2nd row, 1st column // Clear Screen

// // // //

Increment Show text Convert Data to String Chow Lcd at cursor

¡Ò÷íÒ§Ò¹¢Í§â»Ãá¡ÃÁ 1. ˹‹Ç§àÇÅÒ 1 ÇÔ¹Ò·Õ áŌǡíÒ˹´ãˌ¾Íϵ D ໚¹àÍÒµ¾µØ à¾×Íè µÔ´µ‹Í¡ÑºâÁ´ÙÅ LCD 2. ¡íÒ˹´¤‹ÒàÃÔÁè µŒ¹ãˌ¡ºÑ âÁ´ÙÅ LCD áÅÐµÑ§é ¤‹Òãˌ»´ ¡ÒÃáÊ´§¼Å¢Í§à¤ÍÏà«ÍÏ 3. ǹÅÙ» 4 Ãͺà¾×Íè áÊ´§¢ŒÍ¤ÇÒÁ·Õâè Á´ÙÅ LCD ¼Ù㌠ªŒ§Ò¹¨ÐàËç¹¢ŒÍ¤ÇÒÁ·Õâè Á´ÙÅ LCD µÔ´¡Ð¾ÃÔº 4. ǹÅÙ»à¾×Íè àÅ×Íè ¹¢ŒÍ¤ÇÒÁä»·Ò§«ŒÒ áŌÇÊѧè àÅ×Íè ¹¢ŒÍ¤ÇÒÁ¡ÅѺÁÒÍÕ¡¤ÃÑ§é ¨Ò¡¹Ñ¹é ÊÑ§è ¡Ð¾ÃÔº¢ŒÍ¤ÇÒÁ 5. ǹÅÙ»¹íÒ¤‹ÒµÑǹѺ·ÕÁè ÒáÊ´§·Õâè Á´ÙÅ LCD à¾×Íè ãˌàËç¹Ç‹ÒâÁ´ÙÅ LCD ÊÒÁÒöáÊ´§¤‹ÒµÑÇàŢ䴌ᵋ¨ÐµŒÍ§ à»ÅÕÂè ¹¤‹ÒµÑÇàÅ¢ãˌÍÂÙ㋠¹ÃÙ»¢Í§ÃËÑÊáÍÊ¡Õ¡é ͋ ¹ â´Â㪌¿§˜ ¡ª¹Ñè ByteToStr

â»Ãá¡ÃÁ·Õè L7-1 áÊ´§¡ÒäǺ¤ØÁ¡Ò÷íÒ§Ò¹¢Í§âÁ´ÙÅ LCD ã¹ÃٻẺµ‹Ò§æ

mikroC


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 97

6.3 ¿˜§¡ª¹Ñè Lcd8 µÔ´µ‹Í¡ÑºâÁ´ÙÅ LCD Ẻ 8 ºÔµ â´Â»¡µÔâÁ´ÙÅ LCD ÁÕ¢Ò·Õãè ªŒàª×Íè Áµ‹Í¡ÑºäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ 11 ¢Ò´ŒÇ¡ѹ¤×Í ¢Ò¢ŒÍÁÙÅ D0 ¶Ö§ D7 ÃÇÁ 8 àʌ¹ ¢Ò R/W ÊíÒËÃѺ¡ÒÃÊѧè ͋ҹËÃ×Íà¢Õ¹¢ŒÍÁÙšѺâÁ´ÙÅ LCD ¢Ò RS ÊíÒËÃѺàÅ×Í¡¡ÒÃà¢Õ¹¢ŒÍÁÙÅËÃ×ͤíÒÊÑ§è ¢Ò E ÊíÒËÃѺàÍç¹àÍàºÔÅ¡ÒõԴµ‹Í¡ÑºâÁ´ÙÅ LCD ´Ñ§¹Ñ¹é ¿˜§¡ª¹Ñè LCD ÊíÒËÃѺ¡ÒõԴµ‹Í¡ÑºâÁ´ÙÅ LCD Ẻ 8 ºÔµ¨Ö§áµ¡µ‹Ò§¨Ò¡¿˜§¡ª¹Ñè LCD Áҵðҹ à¹×Íè §¨Ò¡ÁÕ¡Òõ‹Í㪌§Ò¹ÊÒÂÊÑ­­Ò³¤Ãº·Ø¡àʌ¹ +5V +5V

R1 10k

32 1

MCLR

11

0.1PF 50V

+Vcc DSP1 LCD module 16x2

8DD

GND

1+

SW1 RESET

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

21+$.&%% 12 31

8ss 8ss

Vo

RD7 RD6 RD5 RD4 RD3 RD2 RD1 RD0

30 29 28 27 22 21 20 19

RC0 15 RC2 RC3 OSC1

18

OSC2

4 22pF

17

5 XTAL 20MHz

22pF

ÃÙ»·Õè 6-3 áÊ´§¡ÒÃàª×Íè Áµ‹ÍäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ¡ÑºâÁ´ÙÅ LCD Ẻ 8 ºÔµ

2 3 1

VR1 10k BRIGHTNESS


98 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

6.3.1 ¿˜§¡ª¹Ñè Lcd8_Config 㪌ÊÒí ËÃѺ¡íÒ˹´¤‹ÒµíÒá˹‹§¾Íϵ·Õãè ªŒàª×Íè Áµ‹Í¡Ñº LCD áÅеíÒá˹‹§¢Òµ‹Ò§ æ ·Õãè ªŒàª×Íè Áµ‹Í ¾ÃŒÍÁ·Ñ§é ¡íÒ˹´¿˜§¡ª¹Ñè ¡Ò÷íÒ§Ò¹¾×¹é °Ò¹ãˌ¡ºÑ âÁ´ÙÅ â´ÂÊÒÁÒöÊÅѺµíÒá˹‹§¢Ò䴌µÒÁµŒÍ§¡Òà ÃٻẺ¡ÒÃ㪌§Ò¹¤íÒÊѧè void Lcd8_Config(unsigned short *ctrlport, unsigned short *dataport, unsigned short RS, unsigned short EN, unsigned short WR, unsigned short D7, unsigned short D6, unsigned short D5, unsigned short D4, unsigned short D3, unsigned short D2, unsigned short D1, unsigned short D0);

¾ÒÃÒÁÔàµÍÏ *ctrlport

ËÁÒ¶֧ ¾Íϵ·Õãè ªŒàª×Íè Áµ‹Í¡Ñº¢Ò¤Çº¤ØÁ¢Í§âÁ´ÙÅ LCD

*dataport

ËÁÒ¶֧ ¾Íϵ·Õãè ªŒà¾×Íè àª×Íè Áµ‹Í¡Ñº¢Ò¢ŒÍÁÙŢͧâÁ´ÙÅ LCD

RS

ËÁÒ¶֧ ¢Ò¾Íϵ·Õµè ͋ ¡Ñº¢Ò RS ¢Í§âÁ´ÙÅ LCD

EN

ËÁÒ¶֧ ¢Ò¾Íϵ·Õµè ͋ ¡Ñº¢Ò EN ¢Í§âÁ´ÙÅ LCD

WR

ËÁÒ¶֧ ¢Ò¾Íϵ·Õµè ͋ ¡Ñº¢Ò R/W ¢Í§âÁ´ÙÅ LCD

D7-D0

ËÁÒ¶֧ ¢Ò¾Íϵ·Õµè ͋ ¡Ñº¢Ò¢ŒÍÁÙÅ D0 ¶Ö§ D7 ¢Í§âÁ´ÙÅ LCD

µÑÇÍ‹ҧ·Õè 6-5 Lcd8_Config(&PORTC,&PORTD,2,3,0,7,6,5,4,3,2,1,0);

µÑÇÍ‹ҧ¹Õàé Å×͡㪌 PORTC ໚¹¾ÍϵÊíÒËÃѺµ‹Í¡Ñº¢Ò¤Çº¤ØÁ áÅÐ㪌 PORTD ໚¹¾ÍϵÊíÒËÃѺµ‹Í ¡Ñº¢Ò¢ŒÍÁÙÅ áÅСíÒ˹´¢Òàª×Íè Áµ‹ÍµÒÁáÊ´§ã¹ÃÙ»·Õè 6-3 ¹Ñ¹è ¤×Í ¢Ò¾Íϵ RC2 µ‹Í¡Ñº¢Ò RS ¢Í§âÁ´ÙÅ LCD ¢Ò¾Íϵ RC3 µ‹Í¡Ñº¢Ò E ¢Í§âÁ´ÙÅ LCD ¢Ò¾Íϵ RC0 µ‹Í¡Ñº¢Ò R/W ¢Í§âÁ´ÙÅ LCD ¢Ò¾Íϵ RD0 ¶Ö§ RD7 µ‹Í¡Ñº¢Ò D0 ¶Ö§ D7 ¢Í§âÁ´ÙÅ LCD


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 99

6.3.2 ¿˜§¡ª¹Ñè Lcd8_Init 㪌¡Òí ˹´µíÒá˹‹§¾ÍϵáÅТҢͧäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ·Õãè ªŒàª×Íè Áµ‹Í¡ÑºâÁ´ÙÅ LCD ã¹áºº 8 ºÔµ ¾ÃŒÍÁ·Ñ駡íÒ˹´¿˜§¡ªÑ蹡Ò÷íÒ§Ò¹¾×é¹°Ò¹ãˌ¡ÑºâÁ´ÙÅ LCD ઋ¹à´ÕÂǡѺ¤íÒÊÑè§ Lcd8_Config ᵋäÁ‹µÍ Œ §¡íÒ˹´µíÒá˹‹§¢Ò¾Íϵ·Õãè ªŒàª×Íè Áµ‹Í ÃкØà¾Õ§¾Íϵ·Õãè ªŒã¹¡ÒÃàª×Íè Áµ‹Í ¡ÑºâÁ´ÙÅ LCD ෋ҹѹé ÃٻẺ¡ÒÃ㪌§Ò¹¤íÒÊѧè void Lcd8_Init(unsigned short *ctrlport, unsigned short *dataport);

¾ÒÃÒÁÔàµÍÏ

㪌¡Òí ˹´¾Íϵ¢Í§äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ·ãÕè ªŒàª×Íè Áµ‹Í¡Ñº¢Ò¤Çº¤ØÁ¢Í§âÁ´ÙÅ LCD «Ö§è ÁÕ¡ÒáíÒ˹´µíÒá˹‹§änj´§Ñ ¹Õé

*ctrlport

¢Ò E ¢Í§âÁ´ÙÅ LCD µ‹Í¡Ñº¢Ò¾Íϵ¤Çº¤ØÁºÔµ 6 ¢Ò RS ¢Í§âÁ´ÙÅ LCD µ‹Í¡Ñº¢Ò¾Íϵ¤Çº¤ØÁºÔµ 4 ¢Ò R/W ¢Í§âÁ´ÙÅ LCD µ‹Í¡Ñº¢Ò¾Íϵ¤Çº¤ØÁºÔµ 5 *dataport

㪌¡Òí ˹´¾ÍϵÊíÒËÃѺ¢Ò¢ŒÍÁÙÅ â´ÂÁÕ¡ÒáíÒ˹´¡ÒÃàª×Íè Áµ‹Íänj´§Ñ ¹Õé

¢Ò D7 ¢Í§âÁ´ÙÅ LCD µ‹Í¡Ñº¢Ò¾ÍϵäÁâ¤Ã¤Í¹â·ÃÅàÅÍϺµÔ 7 ¢Ò D6 ¢Í§âÁ´ÙÅ LCD µ‹Í¡Ñº¢Ò¾ÍϵäÁâ¤Ã¤Í¹â·ÃÅàÅÍϺµÔ 6 ¢Ò D5 ¢Í§âÁ´ÙÅ LCD µ‹Í¡Ñº¢Ò¾ÍϵäÁâ¤Ã¤Í¹â·ÃÅàÅÍϺµÔ 5 ¢Ò D4 ¢Í§âÁ´ÙÅ LCD µ‹Í¡Ñº¢Ò¾ÍϵäÁâ¤Ã¤Í¹â·ÃÅàÅÍϺµÔ 4 ¢Ò D3 ¢Í§âÁ´ÙÅ LCD µ‹Í¡Ñº¢Ò¾ÍϵäÁâ¤Ã¤Í¹â·ÃÅàÅÍϺµÔ 3 ¢Ò D2 ¢Í§âÁ´ÙÅ LCD µ‹Í¡Ñº¢Ò¾ÍϵäÁâ¤Ã¤Í¹â·ÃÅàÅÍϺµÔ 2 ¢Ò D1 ¢Í§âÁ´ÙÅ LCD µ‹Í¡Ñº¢Ò¾ÍϵäÁâ¤Ã¤Í¹â·ÃÅàÅÍϺµÔ 1 ¢Ò D0 ¢Í§âÁ´ÙÅ LCD µ‹Í¡Ñº¢Ò¾ÍϵäÁâ¤Ã¤Í¹â·ÃÅàÅÍϺµÔ 0

µÑÇÍ‹ҧ·Õè 6-6 Lcd8_Init(&PORTC,&PORTD);

µÑÇÍ‹ҧ¹Õãé ªŒ PORTC µ‹Í¡Ñº¢Ò¤Çº¤ØÁ¢Í§âÁ´ÙÅ LCD áÅÐ㪌 PORTD µ‹Í¡Ñº¢Ò¢ŒÍÁÙÅ ¡ÒÃãʋà¤Ã×Íè §ËÁÒ & ¹íÒ˹ŒÒ PORTC áÅÐ PORTD à¹×Íè §¨Ò¡µŒÍ§¡ÒÃÃкض§Ö áÍ´à´Ãʢͧ¾Íϵ ǧ¨Ã¡ÒÃàª×Íè Áµ‹ÍÊíÒËÃѺ¤íÒÊÑ§è ¹Õáé Ê´§ã¹ÃÙ»·Õè 6-3


100 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

6.3.3 ¿˜§¡ª¹Ñè Lcd8_Out ¿˜§¡ª¹Ñè ¹Õãé ªŒÊÒí ËÃѺʋ§¢ŒÍÁÙÅÍÍ¡ä»áÊ´§¼Å·ÕËè ¹ŒÒ¨Í LCD ¨ÐµŒÍ§ÃкصÒí á˹‹§á¶Ç (row) áÅеíÒá˹‹§µÑÇÍÑ¡Éà (col) ¡‹Í¹Ê‹§¢ŒÍÁÙÅÍÍ¡ä» â´ÂÃٻẺ¢Í§¢ŒÍÁÙŨÐ໚¹µÑÇÍÑ¡ÉõÑÇà´ÕÂÇËÃ×Í à»š¹¢ŒÍ¤ÇÒÁ¡ç䴌 ¡‹Í¹ãªŒ§Ò¹¿˜§¡ª¹Ñè ¹Õ¨é еŒÍ§àÃÕ¡¿˜§¡ª¹Ñè Lcd8_Init ËÃ×Í Lcd8_Config ¡‹Í¹ ÃٻẺ¡ÒÃ㪌§Ò¹¤íÒÊѧè void Lcd8_Out(unsigned short row, unsigned short col, char *text);

¾ÒÃÒÁÔàµÍÏ

ËÁÒ¶֧ µíÒá˹‹§á¶Ç¢Í§âÁ´ÙÅ LCD col ËÁÒ¶֧ µíÒá˹‹§µÑÇÍÑ¡ÉÃã¹á¶Ç·ÕÃè ÐºØ *text ËÁÒ¶֧ µÑÇÍÑ¡ÉÃËÃ×Í¢ŒÍ¤ÇÒÁ·Õµ è ͌ §¡ÒùíÒä»áÊ´§¼Å row

µÑÇÍ‹ҧ·Õè 6-6 Lcd8_Out(1, 3, “Hello!”);

໚¹¡ÒÃʋ§¢ŒÍ¤ÇÒÁ Hello ÍÍ¡ä»áÊ´§¼Å·Õ¨è Í LCD 㹺Ã÷Ѵ·Õè 1 àÃÔÁè ·Õµè Òí á˹‹§µÑÇÍÑ¡ÉõÑÇ·Õè 3 ´Ñ§ÃÙ»

Hello

6.3.4 ¿˜§¡ª¹Ñè Lcd8_Out_Cp ໚¹¤íÒÊÑ§è ·Õ·è Òí §Ò¹¤ÅŒÒ¡Ѻ¤íÒÊѧè Lcd8_Out ᵋ¤Òí ÊÑ§è ¹ÕÊé §‹ ¢ŒÍ¤ÇÒÁä»áÊ´§ÂѧµíÒá˹‹§¢Í§ à¤ÍÏà«ÍÏ»¨˜ ¨Øº¹Ñ ¨Ö§äÁ‹µÍŒ §ÃкصÒí á˹‹§à¤ÍÏà«ÍÏ ¡‹Í¹ãªŒ§Ò¹¿˜§¡ª¹Ñè ¹Õ¨é еŒÍ§àÃÕ¡¿˜§¡ª¹Ñè Lcd8_Init ËÃ×Í Lcd8_Config ¡‹Í¹ ÃٻẺ¡ÒÃ㪌§Ò¹¤íÒÊѧè void Lcd8_Out_Cp(char *text);

¾ÒÃÒÁÔàµÍÏ

*text ËÁÒ¶֧

µÑÇÍÑ¡ÉÃËÃ×Í¢ŒÍ¤ÇÒÁ·Õµè ͌ §¡ÒùíÒä»áÊ´§¼Å

µÑÇÍ‹ҧ·Õè 6-7 Lcd8_Out_Cp(“Here!”);

໚¹¡ÒÃáÊ´§¢ŒÍ¤ÇÒÁ 0AHA ·Õâè Á´ÙÅ LCD ã¹µíÒá˹‹§à¤ÍÏà«ÍÏ»¨˜ ¨Øº¹Ñ


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 101

6.3.5 ¿˜§¡ª¹Ñè Lcd8_Chr ໚¹¡ÒÃʋ§¢ŒÍÁÙÅ˹֧è 亵ä»áÊ´§¼Å·Õâè Á´ÙÅ LCD ¨ÐµŒÍ§¡íÒ˹´µíÒá˹‹§á¶Ç (row) áÅÐ µíÒá˹‹§µÑÇÍÑ¡Éà (col) ¡‹Í¹Ê‹§¢ŒÍÁÙÅÍÍ¡ä» ¡‹Í¹ãªŒ§Ò¹¿˜§¡ª¹Ñè ¹Õ¨é еŒÍ§àÃÕ¡¿˜§¡ª¹Ñè Lcd8_Init ËÃ×Í Lcd8_Config ¡‹Í¹ ÃٻẺ¡ÒÃ㪌§Ò¹¤íÒÊѧè void Lcd8_Chr(unsigned short row, unsigned short col, char character);

¾ÒÃÒÁÔàµÍÏ

ËÁÒ¶֧ µíÒá˹‹§á¶Ç¢Í§âÁ´ÙÅ LCD col ËÁÒ¶֧ µíÒá˹‹§µÑÇÍÑ¡ÉÃã¹á¶Ç·ÕÃè ÐºØ character ËÁÒ¶֧ µÑÇÍÑ¡Éà 1 亵·µ Õè ͌ §¡ÒùíÒä»áÊ´§¼Å row

µÑÇÍ‹ҧ·Õè 6-8 char i=0x30; void main() { Delay_ms(1000); Lcd8_Init(&PORTC,&PORTD); while(i<0x3A) { Lcd8_Chr(1,5,i); Delay_ms(1000); i++; } }

// Start String 0

// Start up Delay // E => RC6 : RS => RC4 : R/W => RC5 // Loop until i > 9 // Show i => LCD // Increment i

¨Ò¡µÑÇÍ‹ҧ໚¹¡ÒùíÒ¤‹Ò i ä»áÊ´§¼Å·Õâè Á´ÙÅ LCD â´ÂÁÕ¡ÒÃà¾ÔÁè ¤‹Ò¢Í§µÑÇá»Ã i ¨Ò¡ 0x30 仨¹ ¶Ö§¤‹Ò 0x39 ʋǹµíÒá˹‹§¡ÒÃáÊ´§¼Å¨Ð¤§·ÕÍè ÂÙ·‹ áÕè ¶Ç·Õè 1 ¤ÍÅÑÁ¹·Õè 5

0


102 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

6.3.6 ¿˜§¡ª¹Ñè Lcd8_Chr_Cp ໚¹¡ÒÃʋ§¢ŒÍÁÙÅ˹֧è 亵ä»áÊ´§¼Å·Õâè Á´ÙÅ LCD ·Õµè Òí á˹‹§à¤ÍÏà«ÍÏ»¨˜ ¨Øº¹Ñ ¡‹Í¹ãªŒ§Ò¹ ¿˜§¡ª¹Ñè ¹Õ¨é еŒÍ§àÃÕ¡¿˜§¡ª¹Ñè Lcd8_Init ËÃ×Í Lcd8_Config ¡‹Í¹ ÃٻẺ¡ÒÃ㪌§Ò¹¤íÒÊѧè void Lcd8_Chr_Cp(char character);

¾ÒÃÒÁÔàµÍÏ character

ËÁÒ¶֧ µÑÇÍÑ¡Éà 1 亵·µÕè ͌ §¡ÒùíÒä»áÊ´§¼Å

µÑÇÍ‹ҧ·Õè 6-9 char i=0x30; void main() { Delay_ms(1000); Lcd8_Init(&PORTC,&PORTD); while(i<0x40) { Lcd8_Chr_Cp(i); Delay_ms(1000); i++; } }

// Start String 0

// Start up Delay // E=RC6:RS=RC4 // Loop until 16 character // Show i => LCD // Increment i

¨Ò¡µÑÇÍ‹ҧ·íҧҹ䴌ઋ¹à´ÕÂǡѺ¤íÒÊѧè LCD8_Chr àÁ×Íè àÅ×Íè ¹à¤ÍÏà«ÍÏ µÑÇÍÑ¡É÷ÕÍè ÂÙº‹ ¹¨ÍáÊ´§¼Å ¡ç¨ÐàÃÕ§µ‹Íà¹×Íè §¡Ñ¹ä´Œ ´Ñ§ÃÙ»

0 1 2 3 4 5 6 7 8 9 : ; < = >?


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 103

6.3.7 ¿˜§¡ª¹Ñè Lcd8_Cmd ໚¹¿˜§¡ª¹Ñè ·Õºè ÃèؤÒí ÊÑ§è ¤Çº¤ØÁ¡Ò÷íÒ§Ò¹¢Í§âÁ´ÙÅ LCD ã¹âËÁ´µ‹Ò§æ ´Ñ§áÊ´§ã¹µÒÃÒ§ µ‹Í仹Õé ÃÙ»ª×èÍẺ¡ÒÃ㪌 §Ò¹¤íÒÊÑ§è ¤íÒÊÑè§ LCD_FIRST_ROW LCD_SECOND_ROW LCD_THIRD_ROW LCD_FOURTH_ROW LCD_CLEAR LCD_RETURN_HOME LCD_CURSOR_OFF LCD_UNDERLINE_ON LCD_BLINK_CURSOR_ON LCD_MOVE_CURSOR_LEFT LCD_MOVE_CURSOR_RIGHT LCD_TURN_ON LCD_TURN_OFF LCD_SHIFT_LEFT Shift LCD_SHIFT_RIGHT

ÃÒÂÅÐàÍÕ´ ŒÒÂà¤ÍÏà«ÍÏä»Âѧá¶Ç·Õè 1 áÅÐàÃÔèÁµŒ¹·Õè¤ÍÅÑÁ¹·Õè 1 ŒÒÂà¤ÍÏà«ÍÏä»Âѧá¶Ç·Õè 2 áÅÐàÃÔèÁµŒ¹·Õè¤ÍÅÑÁ¹·Õè 1 ŒÒÂà¤ÍÏà«ÍÏä»Âѧá¶Ç·Õè 3 áÅÐàÃÔèÁµŒ¹·Õè¤ÍÅÑÁ¹·Õè 1 ŒÒÂà¤ÍÏà«ÍÏä»Âѧá¶Ç·Õè 4 áÅÐàÃÔèÁµŒ¹·Õè¤ÍÅÑÁ¹·Õè 1 à¤ÅÕÂÏ˹ŒÒ¨Í ä»ÂѧµíÒá˹‹§àÃÔèÁµŒ¹ (á¶Ç 1 ¤ÍÅÑÁ¹ 1 ) ¢ŒÍÁÙÅã¹ RAM äÁ‹à»ÅÕè¹ »´¡ÒÃáÊ´§¼Åà¤ÍÏà«ÍÏ áÊ´§à¤ÍÏà«ÍÏ ໚¹àʌ¹ªÔ´¢ÍºÅ‹Ò§¤ŒÒ§änj áÊ´§à¤ÍÏà«ÍÏẺ¡Ð¾ÃÔº àÅ×è͹à¤ÍÏà«ÍÏ价ҧ´ŒÒ¹«ŒÒ ¢ŒÍÁÙÅ˹‹Ç¤ÇÒÁ¨íÒ RAM äÁ‹à»ÅÕè¹ àÅ×è͹à¤ÍÏà«ÍÏ价ҧ´ŒÒ¹¢ÇÒ ¢ŒÍÁÙÅ˹‹Ç¤ÇÒÁ¨íÒ RAM äÁ‹à»ÅÕè¹ ແ´¡ÒÃáÊ´§¼Å¢Í§¨Í LCD »´¡ÒÃáÊ´§¼Å¢Í§¨Í LCD àÅ×è͹¢ŒÍÁÙÅä»·Ò§«ŒÒ ¢ŒÍÁÙÅ˹‹Ç¤ÇÒÁ¨íÒ RAM äÁ‹à»ÅÕè¹ àÅ×è͹¢ŒÍÁÙÅä»·Ò§¢ÇÒ ¢ŒÍÁÙÅã¹Ë¹‹Ç¤ÇÒÁ¨íÒ RAM äÁ‹à»ÅÕè¹

ÃٻẺ¡ÒÃ㪌§Ò¹¤íÒÊѧè void Lcd8_Cmd(unsigned short command);

¾ÒÃÒÁÔàµÍÏ Command

ËÁÒ¶֧ ¤íÒÊѧè à¾×Íè ÊÑ§è §Ò¹ãˌâÁ´ÙÅ LCD ·íÒ§Ò¹ã¹âËÁ´µ‹Ò§ æ


104 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

»¯ÔºµÑ ¡Ô Ò÷Õè 8 ʋ§¢ŒÍÁÙÅáÊ´§¼Å·Õâè Á´ÙÅ LCD â´Â㪌¿§˜ ¡ª¹Ñè Lcd8 ¡ÒÃàª×èÍÁµ‹Í·Ò§ÎÒÏ´áÇÏ 㪌ǧ¨Ãã¹ÃÙ»·Õè L8-1 㹡Ò÷´Åͧ l Ëҡ㪌ºÍÏ´·´Åͧ NX-877 plus II ãˌµÍ ‹ ǧ¨ÃµÒÁÃÙ»·Õè L8-1 áŌÇàÅ×Í¡äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ໚¹ PIC16F877 l

¢Ñ鹵͹¡Ò÷´Åͧ L8.1 ແ´â»Ãá¡ÃÁ mikroC ÊÌҧä¿Åâ»Ãà¨ç¡µ LCD8test áÅоÔÁ¾â»Ãá¡ÃÁÀÒÉÒ C µÒÁâ»Ãá¡ÃÁ·Õè L8-1 L8.2 ·íÒ¡ÒäÍÁä¾ÅáÅдÒǹâËÅ´â»Ãá¡ÃÁà¾×Íè ·´Êͺ¼ÅÅѾ¸¡Ò÷íÒ§Ò¹ +5V +5V

R1 10k

32 1

MCLR

11

0.1F 50V

+Vcc DSP1 LCD module 16x2

8DD

GND

1+

SW1 RESET

8ss 8ss

3 1

VR1 10k BRIGHTNESS

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

21+$.&%% 12 31

Vo

2

RD7 RD6 RD5 RD4 RD3 RD2 RD1 RD0

30 29 28 27 22 21 20 19

RC5 38 RC4 RC6 OSC1 4 22pF

37 39

OSC2 5 XTAL 20MHz

22pF

ÃÙ»·Õè L8-1 ǧ¨Ã·´Åͧ¡ÒÃàª×Íè Áµ‹ÍâÁ´ÙÅ LCD Ẻ 8 ºÔµ¢Í§äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 105

char *text1 = "Innovative" ; char *text2 = "Experiment" ; char text3=0x30,i ; void main() { Lcd8_Init(&PORTC,&PORTD); Lcd8_Out(1,1,text1); Lcd8_Out(2,1,text2); Delay_ms(1000); Lcd8_Cmd(LCD_CLEAR); Lcd8_Out_Cp("Show Number 0-9"); Lcd8_Cmd(LCD_SECOND_ROW); while(text3<0x3A) { Lcd8_Chr_Cp(text3); text3++; // Increment number delay_ms(300); } for (i=0;i<16;i++) { Lcd8_Cmd(LCD_SHIFT_LEFT); delay_ms(300); } for (i=0;i<16;i++) { Lcd8_Cmd(LCD_SHIFT_RIGHT); delay_ms(300); } Lcd8_Cmd(LCD_CURSOR_OFF); }

// Initial default on PORTC // Show string from memory // Show string from memory // Clear LCD display // Show text on first row // Set cursor to second row

// Show number 0 to 9

// Shift display left

// Shift display right

// Off cursor

¡Ò÷íÒ§Ò¹¢Í§â»Ãá¡ÃÁ â»Ãá¡ÃÁẋ§¡Ò÷íÒ§Ò¹´Ñ§¹Õé 1. ¡íÒ˹´¾Íϵ D ໚¹¾ÍϵÊíÒËÃѺ¾Íϵ¢ŒÍÁÙŢͧâÁ´ÙÅ LCD ʋǹ¾Íϵ C ໚¹¾Íϵ¤Çº¤ØÁ 2. ¹íÒ¢ŒÍ¤ÇÒÁ Innovative

Experiment

áÊ´§·Õâè Á´ÙÅ LCD

3. ˹‹Ç§àÇÅÒ 1 ÇÔ¹Ò·ÕáŌÇà¤ÅÕÂÏ˹ŒÒ¨ÍáÊ´§¼Å à¾×Íè áÊ´§¢ŒÍ¤ÇÒÁ Show

Number 0-9

·Õºè Ã÷Ѵáá

4. ãˌà¤ÍÏà«ÍÏä»áÊ´§·Õºè Ã÷Ѵ·Õè 2 (ºÃ÷Ѵŋҧ) à¾×Íè ÃÍ¡ÒÃáÊ´§¼Å·Õºè Ã÷Ѵ·Õè 2 5. ǹÅÙ»áÊ´§¤‹ÒµÑÇàÅ¢ 0 ¶Ö§ 9 â´ÂáÊ´§áººäŋÅÒí ´Ñºä»Í‹ҧµ‹Íà¹×Íè § 6. Êѧè àÅ×Íè ¹¢ŒÍ¤ÇÒÁä»´ŒÒ¹«ŒÒÂáÅСÅѺÁÒ·Õµè Òí á˹‹§à´ÔÁ ¤ŒÒ§¢ŒÍÁÙÅänj áŌǻ´¡ÒÃáÊ´§¼Åà¤ÍÏà«ÍÏ

â»Ãá¡ÃÁ·Õè L8-1 ä¿Å LCD8test.c â»Ãá¡ÃÁÀÒÉÒ C à¾×Íè ¤Çº¤ØÁ¡ÒÃáÊ´§¼ÅâÁ´ÙÅ LCD ´ŒÇ¿˜§¡ª¹Ñè Lcd8 ¢Í§ mikroC ¤ÍÁä¾àÅÍÏ


106 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

L8.3 Ãѹâ»Ãá¡ÃÁ ¨ÐàË繡ÒÃáÊ´§¼Å¢ŒÍ¤ÇÒÁ

In n o v a t i v e Experiment áŌÇà»ÅÕè¹໚¹

Show Number 0123456789

0- 9

mikroC


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 107

6.4 ¿˜§¡ªÑè¹ Lcd_Custom ÊíÒËÃѺ¡íÒ˹´¡ÒÃàª×èÍÁµ‹ÍâÁ´ÙÅ LCD âËÁ´ 4 ºÔµ Ẻ¡íÒ˹´¢Òàͧ䴌 ÊíÒËÃѺ¡ÒÃÍ͡Ẻǧ¨Ã㹺ҧ¤ÃÑé§äÁ‹ÊÒÁÒö¡íÒ˹´¢Ò¾ÍϵãˌµÃ§µÒÁäźÃÒÃÕ LCD »¡µÔ䴌 mikroC 䴌àµÃÕÂÁ¿˜§¡ªÑè¹ Lcd_Custom à¾×èͪ‹ÇÂãˌÊÒÁÒö¡íÒ˹´¢Ò¾Íϵ¢Í§äÁâ¤Ã ¤Í¹â·ÃÅàÅÍÏ PIC ãˌÊÒÁÒöàª×Íè Áµ‹Í¡ÑºâÁ´ÙÅ LCD 䴌Í‹ҧÍÔÊÃÐ â´ÂÃͧÃѺ¡ÒÃàª×Íè Áµ‹ÍẺ 4 ºÔµ ¢Ò¾Íϵ·Õãè ªŒµÍ‹ ¡Ñº¢Ò¢ŒÍÁÙŨеŒÍ§ÍÂÙ㋠¹¾Íϵ¡ÅØÁ‹ à´ÕÂǡѹ ʋǹ¢Ò¾Íϵ·Õãè ªŒµÍ‹ ¡Ñº¢Ò¤Çº¤ØÁ ¢Í§âÁ´ÙÅ LCD ¡çµÍŒ §ÍÂÙ㋠¹¾Íϵ¡ÅØÁ‹ à´ÕÂǡѹઋ¹¡Ñ¹ ᵋ¢Ò¾Íϵ·Ñ§é 2 ¡ÅØÁ‹ ¹ÕÊé ÒÁÒö㪌¤¹ÅСÅØÁ‹ 䴌 àÁ×Íè àÅ×͡㪌¿§˜ ¡ª¹Ñé ¹Õé ¢Ò¾Íϵ·Õãè ªŒÊÒí ËÃѺµÔ´µ‹Í¡ÑºâÁ´ÙÅ LCD ¨ÐµŒÍ§¡íÒ˹´ãˌ໚¹¢Ò¾Íϵ àÍÒµ¾µØ ¡‹Í¹àÃÕ¡㪌§Ò¹¿˜§¡ª¹Ñè ¹Õé

6.4.1 ¿˜§¡ª¹Ñè Lcd_Custom_Config ¿˜§¡ª¹Ñè ¹Õãé ªŒÊÒí ËÃѺ¡íÒ˹´¾ÍϵáÅТҾÍϵ·Õãè ªŒàª×Íè Áµ‹Í¡ÑºâÁ´ÙÅ LCD ¾ÃŒÍÁ·Ñ§é ¡íÒ˹´ ¿˜§¡ª¹Ñè ¡Ò÷íÒ§Ò¹¾×¹é °Ò¹ãˌ¡ºÑ âÁ´ÙÅ â´ÂÊÒÁÒöÊÅѺµíÒá˹‹§¢Ò¾Íϵ·Õãè ªŒàª×Íè Áµ‹Í䴌µÒÁµŒÍ§¡Òà ÃٻẺ¡ÒÃ㪌§Ò¹¤íÒÊѧè void Lcd_Custom_Config(char * data_port, char D7, char D6, char D5, char D4, char * ctrl_port, char RS, char WR, char EN);

¾ÒÃÒÁÔàµÍÏ *dataport D7-D4

㪌¡Òí ˹´¾ÍϵÊíÒËÃѺàª×Íè Áµ‹Í¡Ñº¢Ò¢ŒÍÁÙŢͧâÁ´ÙÅ LCD

ËÁÒ¶֧ µíÒá˹‹§ºÔµ¢Í§¢Ò¾Íϵ¢ŒÍÁÙÅ·Õãè ªŒµÍ‹ ¡Ñº¢Ò D7 ¶Ö§ D4 ¢Í§âÁ´ÙÅ LCD

*ctrlport

㪌¡Òí ˹´¾Íϵ¢Í§äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ·ãÕè ªŒàª×Íè Áµ‹Í¡Ñº¢Ò¤Çº¤ØÁ¢Í§âÁ´ÙÅ LCD

RS

ËÁÒ¶֧ µíÒá˹‹§ºÔµ¢Í§¢Ò¾Íϵ¤Çº¤ØÁ·Õãè ªŒÊÒí ËÃѺ¢Ò RS ¢Í§âÁ´ÙÅ LCD

EN

ËÁÒ¶֧ µíÒá˹‹§ºÔµ¢Í§¢Ò¾Íϵ¤Çº¤ØÁ·Õãè ªŒÊÒí ËÃѺ¢Ò EN ¢Í§âÁ´ÙÅ LCD

WR

ËÁÒ¶֧ µíÒá˹‹§ºÔµ¢Í§¢Ò¾Íϵ¤Çº¤ØÁ·Õãè ªŒÊÒí ËÃѺ¢Ò R/W ¢Í§âÁ´ÙÅ LCD

µÑÇÍ‹ҧ·Õè 6-10 Lcd_Custom_Config(&PORTD,7,6,5,4,&PORTC,4,5,6);

໚¹¡ÒÃàÅ×͡㪌 PORTD ໚¹¾ÍϵÊíÒËÃѺ¢Ò¢ŒÍÁÙÅ áÅÐ㪌 PORTC ໚¹¾ÍϵÊíÒËÃѺ¢Ò¤Çº¤ØÁ ÁÕ ¡ÒáíÒ˹´¢Òàª×Íè Áµ‹ÍáÊ´§ã¹ÃÙ»·Õè 6-4


108 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹ +5V +5V

R1 10k

32 1

MCLR

11

0.1F 50V

8DD

D7 D6 D5 D4 14 13 12 11

21+$.&%% 12 31

8ss 8ss

Vo GND

1+

SW1 RESET

+Vcc DSP1 LCD module 16x2

RD7 RD6 RD5 RD4

2 3 1

VR1 10k BRIGHTNESS

R/W RS E 4 6 5

30 29 28 27

RC5 38 RC4 RC6

OSC1

39

OSC2

4 22pF

37

5 XTAL 20MHz

22pF

ÃÙ»·Õè 6-4 ¡ÒÃàª×Íè Áµ‹ÍâÁ´ÙÅ LCD ࢌҡѺ PIC16F877 µÒÁ¿˜§¡ª¹Ñè Lcd_Custom_Config()

6.4.2 ¿˜§¡ª¹Ñè Lcd_Custom_Out ໚¹¿˜§¡ª¹Ñè ¹ÕÊé §‹ ¢ŒÍÁÙÅä»áÊ´§¼Å·Õâè Á´ÙÅ LCD µŒÍ§ÃкصÒí á˹‹§á¶Ç (row) áÅÐ µíÒá˹‹§ µÑÇÍÑ¡Éà (col) ¡‹Í¹Ê‹§¢ŒÍÁÙÅÍÍ¡ä» ÃٻẺ¢Í§¢ŒÍÁÙŨÐ໚¹µÑÇÍÑ¡ÉõÑÇà´ÕÂÇËÃ×Í໚¹¢ŒÍ¤ÇÒÁ¡ç 䴌 â´Â㹡ÒÃ㪌§Ò¹¿˜§¡ª¹Ñè ¹Õ¨é еŒÍ§àÃÕ¡㪌¿§˜ ¡ª¹Ñè Lcd_Custom_Config ¡‹Í¹ ÃٻẺ¡ÒÃ㪌§Ò¹¤íÒÊѧè void Lcd_Custom_Out(char row, char col, char *text);

¾ÒÃÒÁÔàµÍÏ

ËÁÒ¶֧ µíÒá˹‹§á¶Ç¢Í§âÁ´ÙÅ LCD col ËÁÒ¶֧ µíÒá˹‹§µÑÇÍÑ¡ÉÃã¹á¶Ç·ÕÃè ÐºØ *text ËÁÒ¶֧ µÑÇÍÑ¡ÉÃËÃ×Í¢ŒÍ¤ÇÒÁ·Õµ è ͌ §¡ÒùíÒä»áÊ´§¼Å row

µÑÇÍ‹ҧ·Õè 6-11 Lcd_Custom_Out(1, 3, “Hello!”);

໚¹¡ÒÃʋ§¢ŒÍ¤ÇÒÁ Hello ä»áÊ´§·Õâè Á´ÙÅ LCD 㹺Ã÷Ѵ·Õè 1 (ºÃ÷Ѵº¹) àÃÔÁè ·Õµè Òí á˹‹§ÍÑ¡ÉõÑÇ·Õè 3

Hello


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 109

6.4.3 ¿˜§¡ª¹Ñè Lcd_Custom_Out_Cp ໚¹¿˜§¡ªÑè¹·Õè·íÒ§Ò¹¤ÅŒÒ¡Ѻ¿˜§¡ªÑè¹ Lcd_Custom_Out ᵋ¤íÒÊÑ觹Õéʋ§¢ŒÍ¤ÇÒÁä»áÊ´§·Õè µíÒá˹‹§à¤ÍÏà«ÍÏ»¨˜ ¨Øº¹Ñ 㹡ÒÃ㪌§Ò¹¿˜§¡ª¹Ñè ¹Õ¨é еŒÍ§àÃÕ¡㪌¿§˜ ¡ª¹Ñè Lcd_Custom_Config ¡‹Í¹ ÃٻẺ¡ÒÃ㪌§Ò¹¤íÒÊѧè void Lcd_Custom_Out_Cp(char *text);

¾ÒÃÒÁÔàµÍÏ

*text ËÁÒ¶֧

µÑÇÍÑ¡ÉÃËÃ×Í¢ŒÍ¤ÇÒÁ·Õµè ͌ §¡ÒùíÒä»áÊ´§¼Å

µÑÇÍ‹ҧ·Õè 6-12 Lcd_Custom_Out_Cp(“Here!”);

໚¹¡ÒÃáÊ´§¢ŒÍ¤ÇÒÁ 0AHA ·Õâè Á´ÙÅ LCD ã¹µíÒá˹‹§à¤ÍÏà«ÍÏ»¨˜ ¨Øº¹Ñ

6.4.4 ¿˜§¡ª¹Ñè Lcd_Custom_Chr ໚¹¿˜§¡ª¹Ñè ʋ§¢ŒÍÁÙÅ˹֧è 亵ä»áÊ´§¼Å·Õâè Á´ÙÅ LCD â´Â¡‹Í¹Ê‹§µŒÍ§¡íÒ˹´µíÒá˹‹§á¶Ç (row) áÅеíÒá˹‹§µÑÇÍÑ¡Éà (col) 㹡ÒÃ㪌§Ò¹¿˜§¡ª¹Ñè ¹Õ¨é еŒÍ§àÃÕ¡㪌¿§˜ ¡ª¹Ñè Lcd_Custom_Config ¡‹Í¹ ÃٻẺ¡ÒÃ㪌§Ò¹¤íÒÊѧè void Lcd_Custom_Chr(char row, char col, char character);

¾ÒÃÒÁÔàµÍÏ

ËÁÒ¶֧ µíÒá˹‹§á¶Ç¢Í§âÁ´ÙÅ LCD col ËÁÒ¶֧ µíÒá˹‹§µÑÇÍÑ¡ÉÃã¹á¶Ç·ÕÃè ÐºØ character ËÁÒ¶֧ µÑÇÍÑ¡Éà 1 亵·µ Õè ͌ §¡ÒùíÒä»áÊ´§¼Å row

µÑÇÍ‹ҧ·Õè 6-13 char i=0x30; // Start String 0 void main() { TRISC=0b10001111; // Bit 4-6 as OUTPUT TRISD=0x0F; // Bit 4-7 as OUTPUT Delay_ms(1000); // Start up Delay Lcd_Custom_Config(&PORTD,7,6,5,4,&PORTC,4,5,6); while(i<0x3A) // Loop until i > 9 { Lcd_Custom_Chr(1,5,i); // Show i => LCD Delay_ms(1000); i++; // Increment i } }

໚¹¡ÒùíÒ¤‹Ò i ä»áÊ´§¼Å·Õâè Á´ÙÅ LCD áŌÇà¾ÔÁè ¤‹Ò¢Í§ i ¨Ò¡ 0x30 ¶Ö§ ¤‹Ò 0x39 â´ÂáÊ´§¼Å·Õáè ¶Ç·Õè 1 (ºÃ÷Ѵº¹) ¤ÍÅÑÁ¹·Õè 5


110 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

6.4.5 ¿˜§¡ª¹Ñè Lcd_Custom_Chr_Cp ໚¹¿˜§¡ª¹Ñè ʋ§¢ŒÍÁÙÅ˹֧è 亵ä»áÊ´§¼Å·Õâè Á´ÙÅ LCD ·Õµè Òí á˹‹§à¤ÍÏà«ÍÏ»¨˜ ¨Øº¹Ñ 㹡Òà 㪌§Ò¹¿˜§¡ª¹Ñè ¹Õ¨é еŒÍ§àÃÕ¡㪌¿§˜ ¡ª¹Ñè Lcd_Custom_Config ¡‹Í¹ ÃٻẺ¡ÒÃ㪌§Ò¹¤íÒÊѧè void Lcd_Custom_Chr_Cp(char character);

¾ÒÃÒÁÔàµÍÏ character

ËÁÒ¶֧ µÑÇÍÑ¡Éà 1 亵·µÕè ͌ §¡ÒùíÒä»áÊ´§¼Å

µÑÇÍ‹ҧ·Õè 6-14 char i=0x30; // Start String 0 void main() { TRISC=0b10001111; // Bit 4-6 as OUTPUT TRISD=0x0F; // Bit 4-7 as OUTPUT Delay_ms(1000); // Start up Delay Lcd_Custom_Config(&PORTD,7,6,5,4,&PORTC,4,5,6); Lcd_Custom_Cmd(LCD_CLEAR); while(i<0x40) // Loop until i > 9 { Lcd_Custom_Chr_Cp(i); // Show i => LCD Delay_ms(1000); i++; // Increment i } }

¨Ò¡µÑÇÍ‹ҧ·íҧҹ䴌ઋ¹à´ÕÂǡѺ¤íÒÊѧè LCD_Custom_Chr àÁ×Íè àÅ×Íè ¹à¤ÍÏà«ÍÏ µÑÇÍÑ¡É÷ÕÍè ÂÙº‹ ¹ ¨ÍáÊ´§¼Å¢Í§âÁ´ÙÅ LCD ¡ç¨ÐàÃÕ§µ‹Íà¹×Íè §¡Ñ¹ä´Œ ´Ñ§ÃÙ»

0 1 2 3 4 5 6 7 8 9 : ; < = >?


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 111

6.4.6 ¿˜§¡ª¹Ñè Lcd_Custom_Cmd ໚¹¿˜§¡ª¹Ñè ·Õºè ÃèؤÒí ÊÑ§è ¤Çº¤ØÁ¡Ò÷íÒ§Ò¹¢Í§âÁ´ÙÅ LCD ã¹âËÁ´µ‹Ò§æ ´Ñ§áÊ´§ã¹µÒÃÒ§ µ‹Í仹Õé ª×èͤíÒÊÑè§ LCD_FIRST_ROW LCD_SECOND_ROW LCD_THIRD_ROW LCD_FOURTH_ROW LCD_CLEAR LCD_RETURN_HOME LCD_CURSOR_OFF LCD_UNDERLINE_ON LCD_BLINK_CURSOR_ON LCD_MOVE_CURSOR_LEFT LCD_MOVE_CURSOR_RIGHT LCD_TURN_ON LCD_TURN_OFF LCD_SHIFT_LEFT Shift LCD_SHIFT_RIGHT

ÃÒÂÅÐàÍÕ´ ŒÒÂà¤ÍÏà«ÍÏä»Âѧá¶Ç·Õè 1 áÅÐàÃÔèÁµŒ¹·Õè¤ÍÅÑÁ¹·Õè 1 ŒÒÂà¤ÍÏà«ÍÏä»Âѧá¶Ç·Õè 2 áÅÐàÃÔèÁµŒ¹·Õè¤ÍÅÑÁ¹·Õè 1 ŒÒÂà¤ÍÏà«ÍÏä»Âѧá¶Ç·Õè 3 áÅÐàÃÔèÁµŒ¹·Õè¤ÍÅÑÁ¹·Õè 1 ŒÒÂà¤ÍÏà«ÍÏä»Âѧá¶Ç·Õè 4 áÅÐàÃÔèÁµŒ¹·Õè¤ÍÅÑÁ¹·Õè 1 à¤ÅÕÂÏ˹ŒÒ¨Í ä»ÂѧµíÒá˹‹§àÃÔèÁµŒ¹ (á¶Ç 1 ¤ÍÅÑÁ¹ 1 ) ¢ŒÍÁÙÅã¹ RAM äÁ‹à»ÅÕè¹ »´¡ÒÃáÊ´§¼Åà¤ÍÏà«ÍÏ áÊ´§à¤ÍÏà«ÍÏ ໚¹àʌ¹ªÔ´¢ÍºÅ‹Ò§¤ŒÒ§änj áÊ´§à¤ÍÏà«ÍÏẺ¡Ð¾ÃÔº àÅ×Íè ¹à¤ÍÏà«ÍÏ价ҧ´ŒÒ¹«ŒÒ ¢ŒÍÁÙÅ˹‹Ç¤ÇÒÁ¨íÒ RAM äÁ‹à»ÅÕè¹ àÅ×Íè ¹à¤ÍÏà«ÍÏ价ҧ´ŒÒ¹¢ÇÒ ¢ŒÍÁÙÅ˹‹Ç¤ÇÒÁ¨íÒ RAM äÁ‹à»ÅÕè¹ ແ´¡ÒÃáÊ´§¼Å¢Í§¨Í LCD »´¡ÒÃáÊ´§¼Å¢Í§¨Í LCD àÅ×Íè ¹¢ŒÍÁÙÅä»·Ò§«ŒÒ ¢ŒÍÁÙÅ˹‹Ç¤ÇÒÁ¨íÒ RAM äÁ‹à»ÅÕè¹ àÅ×Íè ¹¢ŒÍÁÙÅä»·Ò§¢ÇÒ ¢ŒÍÁÙÅã¹Ë¹‹Ç¤ÇÒÁ¨íÒ RAM äÁ‹à»ÅÕè¹

ÃٻẺ¡ÒÃ㪌§Ò¹¤íÒÊѧè void Lcd_Custom_Cmd(char out_char);

¾ÒÃÒÁÔàµÍÏ Command

ËÁÒ¶֧ ¤íÒÊѧè à¾×Íè ÊÑ§è §Ò¹ãˌâÁ´ÙÅ LCD ·íÒ§Ò¹ã¹âËÁ´µ‹Ò§ æ


112 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

»¯ÔºµÑ ¡Ô Ò÷Õè 9 ʋ§¢ŒÍÁÙÅáÊ´§¼Å·Õâè Á´ÙÅ LCD â´Â㪌¿§˜ ¡ª¹Ñè Lcd_Custom ¡ÒÃàª×èÍÁµ‹Í·Ò§ÎÒÏ´áÇÏ 㪌ǧ¨Ãã¹ÃÙ»·Õè L9-1 㹡Ò÷´Åͧ l Ëҡ㪌ºÍÏ´·´Åͧ NX-877 plus II ãˌµÍ ‹ ǧ¨ÃµÒÁÃÙ»·Õè L9-1 áŌÇàÅ×Í¡äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ໚¹ PIC16F877 l

¢Ñ鹵͹¡Ò÷´Åͧ L9.1 ແ´â»Ãá¡ÃÁ mikroC ÊÌҧä¿Åâ»Ãà¨ç¡µ LCDcustomTest áÅоÔÁ¾â»Ãá¡ÃÁÀÒÉÒ C µÒÁâ»Ãá¡ÃÁ ·Õè L9-1 L9.2 ·íÒ¡ÒäÍÁä¾ÅáÅдÒǹâËÅ´â»Ãá¡ÃÁà¾×Íè ·´Êͺ¼ÅÅѾ¸¡Ò÷íÒ§Ò¹ +5V +5V

R1 10k

32 1

MCLR

11

0.1F 50V

+Vcc DSP1 LCD module 16x2

8DD

GND

1+

SW1 RESET

D7 D6 D5 D4 14 13 12 11

21+$.&%% 12 31

8ss 8ss

Vo

RD7 RD6 RD5 RD4

2 3 1

VR1 10k BRIGHTNESS

R/W RS E 5 4 6

30 29 28 27

RC5 38 RC4 RC6

OSC1

39

OSC2

4 22pF

37

5 XTAL 20MHz

22pF

ÃÙ»·Õè L9-1 ǧ¨Ã·´Åͧ¡ÒÃàª×Íè Áµ‹ÍâÁ´ÙÅ LCD Ẻ¡íÒ˹´¢Ò¾ÍϵÍÔÊÃТͧäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC à¾×Íè ·´Êͺ¡Ò÷íÒ§Ò¹¢Í§¿˜§¡ª¹Ñè Lcd_Custom


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 113

char *text1 = "Innovative" ; char *text2 = "Experiment" ; char text3=0x30,i ; void main() { TRISC=0b10001111; // Bit 4-6 as OUTPUT TRISD=0x0F; // Bit 4-7 as OUTPUT Delay_ms(1000); // Start up Delay Lcd_Custom_Config(&PORTD,7,6,5,4,&PORTC,4,5,6); Lcd_Custom_Cmd(LCD_CLEAR); Lcd_Custom_Cmd(LCD_CURSOR_OFF); Lcd_Custom_Out(1,1,text1); // Show string from memory Lcd_Custom_Out(2,1,text2); // Show string from memory Delay_ms(1000); Lcd_Custom_Cmd(LCD_CLEAR); // Clear LCD display Lcd_Custom_Out_Cp("Show Number 0-9"); // Show text on first row Lcd_Custom_Cmd(LCD_SECOND_ROW); // Set cursor to second row while(text3<0x3A) { Lcd_Custom_Chr_Cp(text3); // Show number 0 to 9 text3++; // Increment number delay_ms(300); } for (i=0;i<16;i++) { Lcd_Custom_Cmd(LCD_SHIFT_LEFT); // Shift display left delay_ms(300); } for (i=0;i<16;i++) { Lcd_Custom_Cmd(LCD_SHIFT_RIGHT); // Shift display right delay_ms(300); } }

¡Ò÷íÒ§Ò¹¢Í§â»Ãá¡ÃÁ àÃÔÁè µŒ¹´ŒÇ¡ÒáíÒ˹´ãˌ¢Ò¾Íϵ·Õãè ªŒ§Ò¹à»š¹¢ÒàÍÒµ¾µØ ¨Ò¡¹Ñ¹é ¡íÒ˹´¤‹ÒàÃÔÁè µŒ¹¡ÑºâÁ´ÙÅ LCD â´Â 㪌¾Íϵ D ໚¹¾ÍϵÊíÒËÃѺàª×èÍÁµ‹Í¢Ò¢ŒÍÁÙÅ áÅоÍϵ C µ‹Í¡Ñº¢Ò¤Çº¤ØÁ¢Í§âÁ´ÙÅ LCD ÅíҴѺµ‹Íä» â»Ãá¡ÃÁʋ§¤íÒÊÑè§à¤ÅÕÂÏ˹ŒÒ¨Í »´¡ÒÃáÊ´§¼Åà¤ÍÏà«ÍÏ áŌÇáÊ´§¼Å¢ŒÍ¤ÇÒÁ Innovative Experiment ·Õ¨è ÍáÊ´§¼Å ˹‹Ç§àÇÅÒ 1 ÇÔ¹Ò·ÕáŌÇà¤ÅÕÂÏ˹ŒÒ¨ÍáÊ´§¼Å à¾×Íè áÊ´§¢ŒÍ¤ÇÒÁ Show Number 0-9 ·Õºè Ã÷Ѵº¹ ¨Ò¡¹Ñ¹é ¡íÒ˹´ãˌà¤ÍÏà«ÍÏä»áÊ´§·Õºè Ã÷Ѵ·Õè 2 (ºÃ÷Ѵŋҧ) à¾×Íè ÃÍ¡ÒÃáÊ´§¼Å·Õºè Ã÷Ѵ·Õè 2 â»Ãá¡ÃÁ ·íÒ¡ÒÃǹÅÙ»à¾×èÍʋ§¤‹ÒµÑÇàÅ¢ 0 ¶Ö§ 9 à¾×èÍáÊ´§º¹âÁ´ÙÅ LCD â´ÂãˌáÊ´§áººäŋÅíҴѺä»Í‹ҧµ‹Íà¹×èͧ ¨Ò¡¹Ñ¹é Êѧè àÅ×Íè ¹¢ŒÍ¤ÇÒÁä»´ŒÒ¹«ŒÒÂáÅСÅѺÁÒ·Õµè Òí á˹‹§à´ÔÁ

â»Ãá¡ÃÁ·Õè L9-1 ä¿Å LDCcustomTest.c â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺ·´Åͧ¡ÒÃàª×Íè Áµ‹ÍâÁ´ÙÅ LCD Ẻ¡íÒ˹´¢Ò¾ÍϵÍÔÊÃТͧäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC à¾×èÍ·´Êͺ¡Ò÷íÒ§Ò¹¢Í§¿˜§¡ªÑè¹ Lcd_Custom ¢Í§ mikroC ¤ÍÁä¾àÅÍÏ


114 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

L9.3 Ãѹâ»Ãá¡ÃÁ ¨ÐàË繡ÒÃáÊ´§¼Å¢ŒÍ¤ÇÒÁ

In n o v a t i v e Experiment áŌÇà»ÅÕè¹໚¹

Show Number 0123456789

0- 9

INNOVATIVE EXPERIMENT


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 115

º··Õè 7

¿˜§¡ª¹Ñè µÔ´µ‹Í¡ÑºË¹‹Ç¤ÇÒÁ¨íÒ¢ŒÍÁÙÅ ÍÕÍ¾Õ ÃÍÁ¢Í§äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ʋǹãË­‹¹Í¡¨Ò¡ÁÕ˹‹Ç¤ÇÒÁ¨íÒâ»Ãá¡ÃÁẺá¿Åª ˹‹Ç ¤ÇÒÁ¨íÒ¢ŒÍÁÙÅáÃÁ ÂѧÁÕ˹‹Ç¤ÇÒÁ¨íÒ¢ŒÍÁÙÅÍÕÍ¾Õ ÃÍÁà¾×Íè 㪌¾¡Ñ ¢ŒÍÁÙÅËÃ×Íà¡çº¤‹Òµ‹Ò§æ ·Õäè Á‹Ê­ Ù ËÒÂàÁ×Íè äÁ‹ÁÕä¿àÅÕé§ ´Ñ§¹Ñ鹡ÒõԴµ‹Íà¾×èÍ͋ҹáÅÐà¢Õ¹¢ŒÍÁÙšѺ˹‹Ç¤ÇÒÁ¨íÒ¢ŒÍÁÙÅʋǹ¹Õé¨Ö§à»š¹ÊÔ觷Õè ¤ÍÁä¾àÅÍϪ¹Ñé ´Õ¾§Ö ÁÕ áÅÐ mikroC ¡ç䴌àµÃÕÂÁä¿ÅäźÃÒÃÕÊÒí ËÃѺ¡Ò÷íÒ§Ò¹¹Õäé nj´ÇŒ Â

7.1 ¿˜§¡ª¹Ñè Eeprom_Read ໚¹¿˜§¡ªÑè¹ÊíÒËÃѺ͋ҹ¤‹Ò¢ŒÍÁÙÅã¹Ë¹‹Ç¤ÇÒÁ¨íÒ¢ŒÍÁÙÅÍÕÍÕ¾ÃÍÁẺÃкصíÒá˹‹§ ã¹ ÃÐËNjҧ¡ÒÃà¢Õ¹áÅÐ͋ҹ¢ŒÍÁÙŨеŒÍ§Ë¹‹Ç§àÇÅÒÍ‹ҧ¹ŒÍ 20 ÁÔÅÅÔÇ¹Ô Ò·Õ à¾×Íè ãˌ¡ÒÃà¢Õ¹¢ŒÍÁÙÅàÊÃç¨ ÊÁºÙó ÃٻẺ¡ÒÃ㪌§Ò¹¤íÒÊѧè unsigned short Eeprom_Read(unsigned int address);

¾ÒÃÒÁÔàµÍÏ address

ËÁÒ¶֧ áÍ´à´Ãʢͧ˹‹Ç¤ÇÒÁ¨íÒ¢ŒÍÁÙÅÍÕÍ¾Õ ÃÍÁ·Õµè ͌ §¡ÒÃ͋ҹ¤‹Ò

¨ÐàËç¹Ç‹ÒµÑÇá»Ã address ໚¹µÑÇá»Ã int (16 ºÔµ) áÊ´§Ç‹Ò ¤íÒÊÑ§è ¹ÕÊé ÒÁÒöµÔ´µ‹Í¡ÑºË¹‹Ç¤ÇÒÁ¨íÒ ¢ŒÍÁÙÅÍÕÍ¾Õ ÃÍÁ䴌ÁÒ¡¡Ç‹Ò 256 µíÒá˹‹§

µÑÇÍ‹ҧ·Õè 7-1 unsigned short take; ... take = Eeprom_Read(0x3F);

¨Ò¡µÑÇÍ‹ҧ໚¹¡ÒÃ͋ҹ¢ŒÍÁÙÅ¢¹Ò´ 1 亵ã¹Ë¹‹Ç¤ÇÒÁ¨íÒ¢ŒÍÁÙÅÍÕÍ¾Õ ÃÍÁµíÒá˹‹§ 0x3F áŌǹíÒä» à¡çºänj·µÕè ÇÑ á»Ã take


116 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

7.2 ¿˜§¡ª¹Ñè Eeprom_Write ໚¹¿˜§¡ª¹Ñè ¡ÒÃà¢Õ¹¢ŒÍÁÙÅä»Âѧ˹‹Ç¤ÇÒÁ¨íÒ¢ŒÍÁÙÅÍÕÍ¾Õ ÃÍÁ â´Â¡‹Í¹à¢Õ¹¢ŒÍÁÙŵŒÍ§·íÒ¡Òà ´ÔÊàÍàºÔÅ¡Ò÷íÒ§Ò¹¢Í§ÍÔ¹àµÍÃÃ»Ñ µàÊÕ¡‹Í¹ à¾×Íè äÁ‹ãˌà¡Ô´ÍÔ¹àµÍÃÃ»Ñ µÁҢѴ¨Ñ§ËÇСÒÃà¢Õ¹¢ŒÍÁÙÅ ä»Âѧ˹‹Ç¤ÇÒÁ¨íÒ¢ŒÍÁÙÅÍÕÍ¾Õ ÃÍÁ ´ŒÇ¡ÒÃà¤ÅÕÂϺµÔ GIE ã¹ÃÕ¨ÊÔ àµÍÏ INTCON ¡ÒÃà¢Õ¹¢ŒÍÁÙŨеŒÍ§Ë¹‹Ç§àÇÅÒÍ‹ҧ¹ŒÍ 20 ÁÔÅÅÔÇ¹Ô Ò·Õà¾×Íè ãˌ¡ÒÃà¢Õ¹¢ŒÍÁÙÅÊÁºÙó ¡‹Í¹ ·Õè¨Ð͋ҹ¤‹Ò¢ŒÍÁÙÅ¡ÅѺ ÃٻẺ¡ÒÃ㪌§Ò¹¤íÒÊѧè void Eeprom_Write(unsigned int address, unsigned short data);

¾ÒÃÒÁÔàµÍÏ

ËÁÒ¶֧ áÍ´à´Ãʢͧ˹‹Ç¤ÇÒÁ¨íÒ¢ŒÍÁÙÅÍÕÍ¾Õ ÃÍÁ·Õµè ͌ §¡ÒÃà¢Õ¹¢ŒÍÁÙÅ ËÁÒ¶֧ ¢ŒÍÁÙÅ·Õµè ͌ §¡ÒÃà¢Õ¹ä»Âѧ˹‹Ç¤ÇÒÁ¨íÒ¢ŒÍÁÙÅÍÕÍ¾Õ ÃÍÁ

address data

µÑÇÍ‹ҧ·Õè 7-2 EEprom_Write(0x3F,0);

¨Ò¡µÑÇÍ‹ҧ໚¹¡ÒÃà¢Õ¹¤‹Ò 0 ä»Âѧ˹‹Ç¤ÇÒÁ¨íÒ¢ŒÍÁÙÅÍÕÍ¾Õ ÃÍÁ·Õáè Í´à´ÃÊ 0x3F


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 117

»¯ÔºµÑ ¡Ô Ò÷Õè 10 ·´Êͺ¡ÒÃ͋ҹáÅÐà¢Õ¹ ˹‹Ç¤ÇÒÁ¨íÒ¢ŒÍÁÙÅÍÕÍÕ¾ÃÍÁ ¡ÒÃàª×èÍÁµ‹Í·Ò§ÎÒÏ´áÇÏ 㪌ǧ¨Ãã¹ÃÙ»·Õè L10-1 㹡Ò÷´Åͧ ÊÒÁÒö㪌äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏàºÍÏ PIC16F877 ËÃ×Í PIC16F887 ¡ç䴌 ᵋµÍŒ §àÅ×Í¡ãˌ¶¡Ù µŒÍ§àÁ×Íè ·íÒ¡ÒÃÊÌҧä¿Åâ»Ãà¨ç¡µ´ÇŒ  l Ëҡ㪌ºÍÏ´ RBX-877 Rev.2007 ÊÒÁÒö·´Åͧ䴌àÅ â´ÂàÅ×Í¡äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ໚¹àºÍÏ PIC16F887 l Ëҡ㪌ºÍÏ´·´Åͧ NX-877 plus II ãˌµÍ ‹ ǧ¨ÃµÒÁÃÙ»·Õè L10-1 áŌÇàÅ×Í¡äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ໚¹ PIC16F877 l

¢Ñ鹵͹¡Ò÷´Åͧ L10.1 ແ´â»Ãá¡ÃÁ mikroC ÊÌҧä¿Åâ»Ãà¨ç¡µ EepromTest áÅоÔÁ¾â»Ãá¡ÃÁÀÒÉÒ C µÒÁâ»Ãá¡ÃÁ·Õè L10-1 L10.2 ·íÒ¡ÒäÍÁä¾ÅáÅдÒǹâËÅ´â»Ãá¡ÃÁà¾×Íè ·´Êͺ¼ÅÅѾ¸¡Ò÷íÒ§Ò¹ +5V 11

32

C6 0.1PF 50V

+5V

1+! 21+$.&&% RD2

+5V

RD3

R5 4k7 SW2 4-5-6

1 R6 1k

MCLR

RD7 RD6 RD5 RD4

JP1

21

4

22

6

30

RS E

DSP1 LCD 16x2

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

BLA +V Vo BLK GND 15 2

R13 16 47

BACK LIGHT ON

1 3 VR1 10k

BRIGHTNESS

29 28 27 12 31

CR1 Ceramic Resonator 20MHz

13

14

ÃÙ»·Õè L10-1 ǧ¨Ã·´Åͧ¡ÒÃà¢Õ¹áÅÐ͋ҹ¢ŒÍÁÙÅ˹‹Ç¤ÇÒÁ¨íÒ¢ŒÍÁÙÅÍÕÍ¾Õ ÃÍÁÁÒáÊ´§¼Å·Õâè Á´ÙÅ LCD ´ŒÇ¿˜§¡ª¹Ñè ¢Í§ mikroC ¤ÍÁä¾àÅÍÏ


118 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

char i,j=0x30; void main() { Lcd_Init(&PORTD); Lcd_Cmd(LCD_CLEAR); Lcd_Cmd(LCD_CURSOR_OFF); for (i=0;i<16;i++) { Lcd_Chr_Cp(j); Eeprom_Write(i,j++); } Delay_ms(20); Lcd_Cmd(LCD_SECOND_ROW); for (i=16;i>0;i--) { j=Eeprom_Read(i-1); Lcd_Chr_Cp(j); Delay_ms(500); } }

// J = Ascii[0]

// // // //

Use LCD show data Clear screen Cursor off 16 Byte writing

// Show writing data on LCD // Write EEPROM // Delay after write // Goto Line 2 // 16 Byte Reading // Read Last byte first // show on LCD

¡Ò÷íÒ§Ò¹¢Í§â»Ãá¡ÃÁ â»Ãá¡ÃÁ·íÒ¡ÒÃǹÅÙ» 16 ¤Ãѧé à¾×Íè à¢Õ¹¢ŒÍÁÙŢͧµÑÇàÅ¢µÑ§é ᵋ 0 ¨¹¤Ãº 16 µÑÇä»Âѧ˹‹Ç¤ÇÒÁ¨íÒ ¢ŒÍÁÙÅÍÕÍÕ¾ÃÍÁ ¨Ò¡¹Ñé¹Í‹Ò¹¤‹Ò¡ÅѺ â´ÂàÃÔèÁ͋ҹ¤‹Ò¨Ò¡áÍ´à´ÃÊËÅѧÊØ´¡‹Í¹ áŌǹíÒ¼ÅÅѾ¸ä»áÊ´§¼Å·Õè ºÃ÷Ѵ·Õè 2 (ºÃ÷Ѵŋҧ) ¢Í§âÁ´ÙÅ LCD

â»Ãá¡ÃÁ·Õè L10-1 ä¿Å EepromTest.c â»Ãá¡ÃÁÀÒÉÒ C ·´Êͺ¡ÒÃà¢Õ¹áÅÐ͋ҹ¢ŒÍÁÙÅ˹‹Ç¤ÇÒÁ¨íÒ ¢ŒÍÁÙÅÍÕÍ¾Õ ÃÍÁã¹äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ¿˜§¡ª¹Ñè ¢Í§ mikroC ¤ÍÁä¾àÅÍÏ L10.3 Ãѹâ»Ãá¡ÃÁ ¡Ò÷´Åͧ¹Õãé ªŒâÁ´ÙÅ LCD 㹡ÒÃáÊ´§¼Å¢Í§¡ÒÃà¢Õ¹áÅÐ͋ҹ¢ŒÍÁÙšѺ˹‹Ç¤ÇÒÁ¨íÒ¢ŒÍÁÙÅÍÕÍ¾Õ ÃÍÁ â´ÂºÃ÷Ѵº¹¢Í§âÁ´ÙÅ LCD ¨ÐáÊ´§¢ŒÍÁÙÅ·Õàè ¢Õ¹ ʋǹºÃ÷Ѵŋҧ¨ÐáÊ´§¢ŒÍÁÙÅ·ÕÍè ҋ ¹¡ÅѺÁÒ â´ÂáÊ´§¤‹Ò¨Ò¡ 亵Ê´Ø ·ŒÒ¡‹Í¹

INNOVATIVE EXPERIMENT


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 119

º··ÕÕè 8

¿˜§¡ªÑè¹Í‹Ò¹¤‹Ò¨Ò¡âÁ´ÙÅá»Å§ÊÑ­­Ò³ ÍйÒÅ͡໚¹´Ô¨µÔ ÍŢͧäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ã¹äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ËÅÒÂæàºÍϨÐÁÕâÁ´ÙÅá»Å§ÊÑ­­Ò³ÍйÒÅ͡໚¹´Ô¨µÔ ÍŵԴ µÑ§é ÍÂÙÀ‹ ÒÂã¹ ¡ÒÃ͋ҹ¤‹Ò¼ÅÅѾ¸Ê­ Ñ ­Ò³ÍйÒÅÍ¡¨Ò¡âÁ´ÙŹÕé ¨ÐµŒÍ§ÁÕ¡ÒáíÒ˹´¤‹ÒËÅÒÂ¢Ñ¹é µÍ¹ ᵋ mikroC 䴌àµÃÕÂÁ¿˜§¡ª¹Ñè Adc_Read() àÍÒänjà¾×Íè ãˌÊÒÁÒö͋ҹ¤‹Ò¨Ò¡âÁ´ÙÅá»Å§ÊÑ­­Ò³ ÍйÒÅ͡໚¹´Ô¨µÔ ÍÅ䴌Í‹ҧ§‹Ò æ

8.1 ¿˜§¡ª¹Ñè Adc_Read ¤íÒÊÑ§è ¹Õé ¡íÒ˹´ãˌâÁ´ÙÅá»Å§ÊÑ­­Ò³ÍйÒÅ͡໚¹´Ô¨µÔ ÍÅ·íÒ§Ò¹´ŒÇÂǧ¨Ã¡íÒà¹Ô´ÊÑ­­Ò³ ¹Ò́¡ÒẺ RC ÀÒÂã¹ ª‹Í§ÍйÒÅÍ¡·Õµè ͌ §¡ÒÃ͋ҹ¤‹Ò¹Ñ¹é ¢Ö¹é ÍÂÙ¡‹ ºÑ àºÍϢͧäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ᵋÅеÑÇ â´Â´Ù䴌¨Ò¡´ÒµŒÒªÕµ¢Í§äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC àºÍϹ¹Ñé æ ÃٻẺ¡ÒÃ㪌§Ò¹¤íÒÊѧè unsigned Adc_Read(unsigned short channel);

¾ÒÃÒÁÔàµÍÏ channel

ËÁÒ¶֧ ª‹Í§ÊÑ­­Ò³ÍйÒÅÍ¡·Õµè ͌ §¡ÒÃ͋ҹ¤‹Ò

¡Òä׹¤‹Ò

¤×¹¤‹Ò໚¹µÑÇàÅ¢¨íҹǹàµçÁ ·Õè¤íҹdzÁÒ¨Ò¡¢ŒÍÁÙÅàÅ¢°Ò¹Êͧ 10 ºÔµ«Öè§Í‹Ò¹ä´Œ¨Ò¡âÁ´ÙÅá»Å§ ÊÑ­­Ò³ÍйÒÅ͡໚¹´Ô¨µÔ ÍŢͧÍÔ¹¾ØµÍйÒÅÍ¡·ÕÃè кبҡµÑÇá»Ã channel

8.1.1 ¡ÒáíÒ˹´¤‹ÒàÃÔÁè µŒ¹ ¡‹Í¹ãªŒ§Ò¹äźÃÒÃÕ ADC µŒÍ§¡íÒ˹´ãˌ¢Ò¾Íϵ·Õãè ªŒ§Ò¹ãˌ໚¹ÍÔ¹¾ØµàÊÕ¡‹Í¹ ¡µÑÇÍ‹ҧ µŒÍ§¡ÒÃ㪌¢Ò AN0 «Ö§è ໚¹¢Ò¾Íϵ A ºÔµ 0 ¨Ö§µŒÍ§¡íÒ˹´ãˌ¾Íϵ A ºÔµ 0 ໚¹ÍÔ¹¾ØµàÊÕ¡‹Í¹ (Í‹ҧ äáçµÒÁ»¡µÔàÁ×Íè 㪌§Ò¹¤Ãѧé áá ¢Ò¹Õ¨é Ð໚¹ÍÔ¹¾ØµÍÂÙዠŌÇ)


120 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

8.1.2 µÑÇÍ‹ҧ¡ÒÃ㪌§Ò¹àº×Íé §µŒ¹ ã¹µÑÇÍ‹ҧâ»Ãá¡ÃÁµ‹Í仹Õàé »š¹¡ÒÃ͋ҹ¤‹Ò¨Ò¡ª‹Í§ AN0 ÁÒà¡çºã¹µÑÇá»Ã temp «Ö§è ¨Í§¾×¹é ·Õè änj໚¹áºº int à¹×Íè §¨Ò¡¢ŒÍÁÙżÅÅѾ¸Á¢Õ ¹Ò´ 10 ºÔµ¨Ö§µŒÍ§à¡çºã¹µÑÇá»Ã·ÕÁè ¢Õ ¹Ò´ 16 ºÔµ int temp; void main() { temp = Adc_Read(0); }

// read analog value from channel 0

8.2 ÃÕ¨ÔÊàµÍÏ·Õèà¡ÕèÂÇ¢ŒÍ§¡ÑºâÁ´ÙÅá»Å§ÊÑ­­Ò³ÍйÒÅ͡໚¹´Ô¨ÔµÍŢͧ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC16F877(A) ¡ÒÃ㪌§Ò¹¤íÒÊÑ觷Õèà¡ÕèÂÇ¢ŒÍ§¡ÑºâÁ´ÙÅá»Å§ÊÑ­­Ò³ÍйÒÅ͡໚¹´Ô¨ÔµÍŢͧ mikroC ¤ÍÁ ä¾àÅÍÏãˌ䴌»ÃÐ⪹Ê§Ù ÊØ´ ¤ÇÃÃÙ¨Œ ¡Ñ ¡ÑºÃÕ¨ÊÔ àµÍÏ·àÕè ¡ÕÂè Ç¢ŒÍ§¡ÑºâÁ´ÙÅá»Å§ÊÑ­­Ò³ÍйÒÅ͡໚¹ ´Ô¨µÔ ÍÅàÊÕ¡‹Í¹ ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏàºÍÏ PIC16F87x áÅÐ PIC16F87xA ÁÕÃÒÂÅÐàÍÕ´â´Â ÊÃØ»´Ñ§¹Õé CHS2:CHS0

ÊÇÔµªÁÅÑ µÔà¾Åç¡«

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

8AIN áç´Ñ¹ÍйÒÅÍ¡ÍÔ¹¾Øµ

111

RE2/AN7

110

RE1/AN6

101

RE0/AN5

100

RA5/AN4

011

RA3/AN3/VREF+

010

VDD

001 000

8REF+ áç´Ñ¹ÍŒÒ§ÍÔ§ VREF-

RA2/AN2/VREFRA1/AN1 RA0/AN0

PCFG3:PCFG0 PCFG3:PCFG0

áç´Ñ¹ÍŒÒ§ÍÔ§

VSS

ÃÙ»·Õè 8-1 ä´ÍÐá¡ÃÁ¡Ò÷íÒ§Ò¹¢Í§âÁ´ÙÅá»Å§ÊÑ­­Ò³ÍйÒÅ͡໚¹´Ô¨µÔ ÍÅã¹ PIC16F87x


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 121

8.2.1 ÃÕ¨ÊÔ àµÍÏ ADRESH áÅÐ ÃÕ¨ÊÔ àµÍÏ ADRESL ໚¹ÃÕ¨ÊÔ àµÍÏ 8 ºÔµ 㪌à¡çº¼ÅÅѾ¸·äÕè ´Œ¨Ò¡¡ÒÃá»Å§ÊÑ­­Ò³ ¡ÒÃ㪌§Ò¹¿˜§¡ª¹Ñè Adc_Read() äÁ‹µÍŒ §ÍŒÒ§¶Ö§ÃÕ¨ÊÔ àµÍϹÕé ᵋËÅѧ¨Ò¡àÃÕ¡㪌§Ò¹¿˜§¡ª¹Ñè Adc_Read() ¤‹Ò·Õäè ´Œ¨Ò¡¡ÒÃá»Å§¨Ðà¡çºänj ·ÕÃè ¨Õ ÊÔ àµÍÏ·§Ñé ÊͧµÑǹÕé

8.2.2 ÃÕ¨ÊÔ àµÍÏ ADCON0 ÁÕ¢¹Ò´ 8 ºÔµ ໚¹ÃÕ¨ÊÔ àµÍÏËÅÑ¡·Õãè ªŒ¤Çº¤ØÁ¡Ò÷íÒ§Ò¹¢Í§âÁ´ÙÅá»Å§ÊÑ­­Ò³ÍйÒÅ͡໚¹ ´Ô¨µÔ ÍÅ ÃÒÂÅÐàÍÕ´¡ÒÃ㪌§Ò¹ÁÕ´§Ñ ¹Õé ºÔµ 7

ADCON0

ºÔµ 6

ºÔµ 5

ºÔµ 4

ºÔµ 3

ºÔµ 2

ADCS1

ADCS0

CHS2

CHS1

CHS0

GO/DONE

R/W -0

R/W -0

R/W -0

R/W -0

R/W -0

R/W -0

ºÔµ 1 -

ºÔµ 0

ADON R/W -0

ºÔµ 7 áÅÐ 6 - ADCS1, ADCS0 (A/D Conversion Clock Select bits) : ºÔµàÅ×Í¡¤ÇÒÁ¶Õ¢è ͧÊÑ­­Ò³ ¹Ò́¡Ò·Õè㪌㹡ÒÃá»Å§ÊÑ­­Ò³ÍйÒÅ͡໚¹´Ô¨ÔµÍÅ “00” - ¤ÇÒÁ¶ÕÊè ­ Ñ ­Ò³¹Ò́¡ÒËÒà 2 (FOSC/2) “01” - ¤ÇÒÁ¶ÕÊè ­ Ñ ­Ò³¹Ò́¡ÒËÒà 8 (FOSC/8) “10” - ¤ÇÒÁ¶ÕÊè ­ Ñ ­Ò³¹Ò́¡ÒËÒà 32 (FOSC/32) “11” - 㪌¤ÇÒÁ¶ÕÊè ­ Ñ ­Ò³¹Ò́¡Ò¨Ò¡Ç§¨Ã RC (FRC) ¹Í¡¨Ò¡¹Ñ¹é Âѧ㪌ÃNj Á¡ÑººÔµ ADCS2 (ºÔµ 6 ã¹ÃÕ¨ÊÔ àµÍÏ ADCON1) ੾ÒÐã¹Í¹Ø¡ÃÁ PIC16F87xA à¾×Íè àÅ×Í¡¤ÇÒÁ¶ÕèÊÑ­­Ò³¹Ò́¡Ò䴌ÁÒ¡¢Öé¹ ºÔµ 5, 4 áÅÐ 3 - CHS2, CHS1, CHS0 (Analog Channel Select bits) : ºÔµàÅ×Í¡ª‹Í§ÊÑ­­Ò³ ÍйÒÅÍ¡ “000” - ª‹Í§ 0 (AN0/RA0) “001” - ª‹Í§ 1 (AN1/RA1) “010” - ª‹Í§ 2 (AN2/RA2) “011” - ª‹Í§ 3 (AN3/RA3) “100” - ª‹Í§ 4 (AN4/RA5) “101” - ª‹Í§ 5 (AN5/RE0) äÁ‹ÁãÕ ¹Ãع‹ 28 ¢Ò “110” - ª‹Í§ 6 (AN6/RE1) äÁ‹ÁãÕ ¹Ãع‹ 28 ¢Ò “111” - ª‹Í§ 7 (AN7/RE2) äÁ‹ÁãÕ ¹Ãع‹ 28 ¢Ò


122 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

ADON

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

ºÔµ 2 - GO/DONE (A/D Conversion Status bit) : ºÔµáÊ´§Ê¶Ò¹Ð¡ÒÃá»Å§ÊÑ­­Ò³ ·íҧҹËÇÁ¡ÑººÔµ ¡Ã³ÕºµÔ ADON ໚¹ “1” “0” - ¡ÒÃá»Å§ÊÑ­­Ò³àÊÃç¨ÊÁºÙó ËÃ×ÍÂѧäÁ‹àÃÔÁè ¡ÒÃá»Å§ÊÑ­­Ò³ “1” - ÂѧÍÂÙ㋠¹ÃÐËNjҧ¡ÒÃá»Å§ÊÑ­­Ò³ ºÔµ¹ÕÂé §Ñ ÊÒÁÒöà¤ÅÕÂÃÍµÑ â¹ÁѵàÔ Á×Íè ¡ÒÃá»Å§àÊÃç¨ÊÁºÙó áÅÐà¤ÅÕÂÏà¹×Íè §¨Ò¡à¡Ô´à¾ÒàÇÍÏÍ͹ÃÕ૵ ºÔµ 1 : äÁ‹ãªŒ§Ò¹ ¡íÒ˹´à»š¹ “0”

ºÔµ 0 - ADON (A/D On bit) : ºÔµà»´¡Ò÷íÒ§Ò¹¢Í§âÁ´ÙÅá»Å§ÊÑ­­Ò³ÍйÒÅ͡໚¹´Ô¨µÔ ÍÅ “0” - »´¡Ò÷íÒ§Ò¹ “1” - ແ´¡Ò÷íÒ§Ò¹

8.2.3 ÃÕ¨ÊÔ àµÍÏ ADCON1 ໚¹ÃÕ¨ÊÔ àµÍϤǺ¤ØÁ¡Ò÷íÒ§Ò¹¢Í§âÁ´ÙÅá»Å§ÊÑ­­Ò³ÍйÒÅ͡໚¹´Ô¨µÔ ÍÅ·Õ·è Òí §Ò¹Ã‹ÇÁ¡Ñº ADCON0 ÁÕ¢¹Ò´ 8 ºÔµ â´ÂÃÕ¨ÊÔ àµÍϵÇÑ ¹Õãé ªŒ¡Òí ˹´¡Ò÷íÒ§Ò¹¢Í§¢Ò¾Íϵ·Õàè ¡ÕÂè Ç¢ŒÍ§¡ÑºâÁ´ÙÅá»Å§ ÊÑ­­Ò³ÍйÒÅ͡໚¹´Ô¨µÔ ÍÅ áÅÐ㪌àÅ×Í¡ÃٻẺ¢Í§¢ŒÍÁÙżÅÅѾ¸·äÕè ´Œ¨Ò¡¡ÒÃá»Å§ÊÑ­­Ò³ ºÔµ 7

ADCON1

ADFM

ºÔµ 6

ºÔµ 5

ADCS2*

-

ºÔµ 4 -

R/W -0

ºÔµ 3

ºÔµ 2

ºÔµ 1

ºÔµ 0

PCFG3

PCFG2

PCFG1

PCFG0

R/W -0

R/W -0

R/W -0

R/W -0

ºÔµ 7 - ADFM (A/D Result Format Select bit - ºÔµ 7) : ºÔµàÅ×Í¡ÃٻẺ¼ÅÅѾ¸¢Í§¡ÒÃá»Å§ÊÑ­­Ò³ “0” - àÅ×Í¡¼ÅÅѾ¸áººªÔ´«ŒÒ (left justified) ÁÕû٠Ẻ¢Í§¢ŒÍÁÙŴѧ¹Õé ¼ÅÅѾ¸ 10 ºÔµ 7

0 7 6 5

0

b b b b b b b b b b 0 0 0 0 0 0

ÃÕ¨ÊÔ àµÍÏ ADRESH

ÃÕ¨ÊÔ àµÍÏ ADRESL

“1” - àÅ×Í¡¼ÅÅѾ¸áººªÔ´¢ÇÒ (right justified) ÁÕû٠Ẻ¢Í§¢ŒÍÁÙŴѧ¹Õé ¼ÅÅѾ¸ 10 ºÔµ 7

2 1 0 7

0

      > > > > > > > > > >

ÃÕ¨ÊÔ àµÍÏ ADRESH

ÃÕ¨ÊÔ àµÍÏ ADRESL


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 123

ºÔµ 6 - ADCS2 (A/D Conversion Clock Select bits) : ºÔµàÅ×Í¡¤ÇÒÁ¶ÕÊè ­ Ñ ­Ò³¹Ò́¡Ò·Õãè ªŒã¹¡ÒÃá»Å§ ÊÑ­­Ò³ÍйÒÅ͡໚¹´Ô¨ÔµÍÅ ºÔµ¹ÕÁé àÕ ©¾ÒÐã¹Í¹Ø¡ÃÁ PIC16F87xA µŒÍ§ãªŒÃNj Á¡ÑººÔµ ADCS1 áÅÐ ADCS0 ã¹ÃÕ¨ÊÔ àµÍÏ ADCON0 ADCS2 ADCS1 ADCS1 ¤ÇÒÁ¶Õ¢è ͧÊÑ­­Ò³¹Ò́¡Ò 0 0 0 ¤ÇÒÁ¶ÕÊè ­ Ñ ­Ò³¹Ò́¡ÒËÒà 2 (FOSC/2) 0 0 1 ¤ÇÒÁ¶ÕÊè ­ Ñ ­Ò³¹Ò́¡ÒËÒà 8 (FOSC/8) 0 1 0 ¤ÇÒÁ¶ÕÊè ­ Ñ ­Ò³¹Ò́¡ÒËÒà 32 (FOSC/32) 0 1 1 㪌¤ÇÒÁ¶ÕÊè ­ Ñ ­Ò³¹Ò́¡Ò¨Ò¡Ç§¨Ã RC (FRC) 1 0 0 ¤ÇÒÁ¶ÕÊè ­ Ñ ­Ò³¹Ò́¡ÒËÒà 4 (FOSC/4) 1 0 1 ¤ÇÒÁ¶ÕÊè ­ Ñ ­Ò³¹Ò́¡ÒËÒà 16 (FOSC/16) 1 1 0 ¤ÇÒÁ¶ÕÊè ­ Ñ ­Ò³¹Ò́¡ÒËÒà 64 (FOSC/64) 1 1 1 㪌¤ÇÒÁ¶ÕÊè ­ Ñ ­Ò³¹Ò́¡Ò¨Ò¡Ç§¨Ã RC (FRC) ºÔµ 5 áÅÐ 4 : äÁ‹ãªŒ§Ò¹ ¡íÒ˹´à»š¹ “0” ºÔµ 3, 2, 1 áÅÐ 0- PCFG3, PCFG2, PCFG1, PCFG0 (A/D Port Configuration Control bits - ) : ºÔµ¡íÒ˹´¡Òà ·íÒ§Ò¹¢Í§¢Ò¾Íϵ·Õãè ªŒã¹âÁ´ÙÅá»Å§ÊÑ­­Ò³ÍйÒÅ͡໚¹´Ô¨µÔ ÍÅ ÁÕÃÒÂÅÐàÍÕ´´Ñ§¹Õé PCFG3:PCFG0

AN7

AN6

AN5

AN4

AN3

AN2

AN1

AN0

VREF+

VREF-

¨íҹǹÍÔ¹¾ØµÍйÒÅÍ¡ : ¨íҹǹÍÔ¹¾Øµáç´Ñ¹ÍŒÒ§ÍÔ§

0000

A

A

A

A

A

A

A

A

VDD

VSS

8:0

0001

A

A

A

A

VREF+

A

A

A

AN3

VSS

7:1

0010

D

D

D

A

A

A

A

A

VDD

VSS

5:0

0011

D

D

D

A

VREF+

A

A

A

AN3

VSS

4:1

0100

D

D

D

D

A

D

A

A

VDD

VSS

3:0

0101

D

D

D

D

VREF+

D

A

A

AN3

VSS

2:1

011x

D

D

D

D

D

D

D

D

-

-

0:0

1000

A

A

A

A

VREF+

VREF-

A

A

AN3

AN2

6:2

1001

D

D

A

A

A

A

A

A

VDD

VSS

6:0

1010

D

D

A

A

VREF+

A

A

A

AN3

VSS

5:1

1011

D

D

A

A

VREF+

VREF-

A

A

AN3

AN2

4:2

1100

D

D

D

A

VREF+

VREF-

A

A

AN3

AN2

3:2

1101

D

D

D

D

VREF+

VREF-

A

A

AN3

AN2

2:2

1110

D

D

D

D

D

D

D

A

VDD

VSS

1:0

1111

D

D

D

D

VREF+

VREF-

D

A

AN3

AN2

1:2

A ¤×Í ÍÔ¹¾ØµÍйÒÅÍ¡, D ¤×Í ¾Íϵ´Ô¨µÔ ÍÅ, V REF+ ¤×Íáç´Ñ¹ÍŒÒ§ÍÔ§¢ÒºÇ¡, V REF- ¤×Íáç´Ñ¹ÍŒÒ§ÍÔ§¢Òź, V DD ¤×Í ä¿àÅÕÂé §, VSS ¤×Í¡ÃÒÇ´, AN3 ¤×Í ¢ÒÍÔ¹¾ØµÍйÒÅÍ¡ª‹Í§ 3 áÅÐ AN2 ¤×Í ¢ÒÍÔ¹¾ØµÍйÒÅÍ¡ª‹Í§ 2


124 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

8.3 ¡ÒÃàÅ×Í¡¤ÇÒÁ¶ÕÊè ­ Ñ ­Ò³¹Ò́¡Ò¢Í§âÁ´ÙÅá»Å§ÊÑ­­Ò³ÍйÒÅ͡໚¹ ´Ô¨µÔ ÍŢͧäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC16F877(A) ¡ÒÃàÅ×Í¡¤ÇÒÁ¶ÕÊè ­ Ñ ­Ò³¹Ò́¡Ò¢Í§¡ÒÃá»Å§ÊÑ­­Ò³¹ÕÁé ¤Õ ÇÒÁÊíҤѭÁÒ¡ à¹×Íè §¨Ò¡Áռŵ‹Í ¤‹ÒàÇÅÒÃÇÁ㹡ÒÃá»Å§ÊÑ­­Ò³ â´Âã¹âÁ´ÙÅá»Å§ÊÑ­­Ò³ÍйÒÅ͡໚¹´Ô¨µÔ ÍŢͧ PIC16F87x µŒÍ§ãªŒàÇÅÒá»Å§ÊÑ­­Ò³ÍйÒÅ͡໚¹¢ŒÍÁÙÅ´Ô¨µÔ ÍÅ 10 ºÔµà·‹Ò¡Ñº 12TAD â´Â¤‹ÒàÇÅÒ TAD ¤×ͤ‹Ò àÇÅÒ·Õãè ªŒã¹¡ÒÃá»Å§ÊÑ­­Ò³µ‹ÍºÔµ áÅФ‹Ò TAD ·Õ¹è ͌ ·ÕÊè ´Ø ·ÕÂè §Ñ ¤§ãˌ¼Å¡ÒÃá»Å§ÊÑ­­Ò³·Õ¶è ¡Ù µŒÍ§ ¤×Í 1.6 äÁâ¤ÃÇÔ¹Ò·Õ ´Ñ§¹Ñ¹é ¡ÒÃàÅ×Í¡¤ÇÒÁ¶ÕÊè ­ Ñ ­Ò³¹Ò́¡ÒµŒÍ§¤íÒ¹Ö§¶Ö§»˜¨¨Ñ¹մé nj  ¡ÒÃàÅ×Í¡¤ÇÒÁ¶Õ·è Òí 䴌â´Â¡íÒ˹´¢ŒÍÁÙÅ㹺Ե ADCS1 áÅÐ ADCS0 ã¹Í¹Ø¡ÃÁ PIC16F87x Áҵðҹ áÅÐÊÒÁÒö㪌ºÔµ ADCS2 à¾ÔèÁ㹡óÕäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ͹ءÃÁ PIC16F87xA ã¹ µÒÃÒ§·Õè 8-1 áÊ´§¶Ö§¤ÇÒÁÊÑÁ¾Ñ¹¸¢Í§¡ÒÃàÅ×Í¡¤ÒºàÇÅÒÊÑ­­Ò³¹Ò́¡Ò¢Í§âÁ´ÙÅá»Å§ÊÑ­­Ò³ ÍйÒÅ͡໚¹´Ô¨µÔ ÍšѺ¤ÇÒÁ¶ÕÊè §Ù ÊØ´¢Í§ÊÑ­­Ò³¹Ò́¡ÒËÅÑ¡à¾×Íè ãˌ䴌¤Ò‹ TAD 1.6 äÁâ¤ÃÇÔ¹Ò·Õ Â¡µÑÇÍ‹ҧઋ¹·Õ¤è ÇÒÁ¶ÕÍè ÍÊ«ÔÅàÅàµÍÏ 20 MHz ¤‹Ò 64TOSC Áդҋ ෋ҡѺ 3.2 äÁâ¤ÃÇÔ¹Ò·Õ«§Öè ÁÕ ¤‹ÒÁÒ¡·ÕÊè ´Ø ´Ñ§¹Ñ¹é àÁ×Íè 㪌¤ÇÒÁ¶Õè 20 MHz ¤ÇÃ㪌¤Ò‹ 64TOSC ËÃ×Í㪌ÍÍÊ«ÔÅàÅàµÍÏẺ RC ¤ÒºàÇÅÒÊÑ­­Ò³¹ÒÌԡҢͧâÁ´ÙÅ ADC

ADCS2:ADCS1:ADCS0

¤ÇÒÁ¶ÕèÊÙ§ÊØ´¢Í§ÊÑ­­Ò³¹ÒÌÔ¡ÒËÅÑ¡ (MHz)

6 OSC

000

1.25

"6 OSC

100

2.50

&6 OSC

001

5

$6 OSC

101

10

! 6 OSC

010

20

$"6 OSC

110

20

4+ (1,2)

x11

(2)

੾ÒÐã¹Í¹Ø¡ÃÁ PIC16F87xA ෋ҹÑé¹ (1) àÁ×èÍ㪌ǧ¨Ã RC ¤‹Ò TAD »¡µÔ¨Ð෋ҡѺ 4 äÁâ¤ÃÇÔ¹Ò·Õ áµ‹ÊÒÁÒöÁÕ¤‹Ò䴌ÃÐËNjҧ 2-6 äÁâ¤ÃÇÔ¹Ò·Õ (2) ¶ŒÒËÒ¡¤ÇÒÁ¶ÕèÊÑ­­Ò³¹ÒÌÔ¡ÒËÅÑ¡ÁÒ¡¡Ç‹Ò 1MHz ¤ÇÃ㪌ÊÑ­­Ò³¹ÒÌԡҨҡǧ¨Ã RC 㹡óշíÒ§Ò¹ã¹âËÁ´ÊÅÕ»

µÒÃÒ§·Õè 8-1 áÊ´§¤ÇÒÁÊÑÁ¾Ñ¹¸¢Í§¤ÒºàÇÅÒÊÑ­­Ò³¹Ò́¡Ò¢Í§âÁ´ÙÅá»Å§ÊÑ­­Ò³ÍйÒÅÍ¡ ໚¹´Ô¨µÔ ÍÅ ¡Ñº¤ÇÒÁ¶ÕÊè §Ù ÊØ´¢Í§ÊÑ­­Ò³¹Ò́¡ÒËÅÑ¡¢Í§ PIC16F87x(A)


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 125

»¯ÔºµÑ ¡Ô Ò÷Õè 11 ¡ÒÃ㪌§Ò¹¿˜§¡ª¹Ñè Adc_Read() ¡Ñº PIC16F877A ¡ÒÃàª×èÍÁµ‹Í·Ò§ÎÒÏ´áÇÏ 㪌ǧ¨Ãã¹ÃÙ»·Õè L11-1 㹡Ò÷´Åͧ l Ëҡ㪌ºÍÏ´·´Åͧ NX-877 plus II ãˌµÍ ‹ ǧ¨ÃµÒÁÃÙ»·Õè L11-1 áŌÇàÅ×Í¡äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ໚¹ PIC16F877 l

¢Ñ鹵͹¡Ò÷´Åͧ L11.1 ແ´â»Ãá¡ÃÁ mikroC ÊÌҧä¿Åâ»Ãà¨ç¡µ ADCtest áÅоÔÁ¾â»Ãá¡ÃÁÀÒÉÒ C µÒÁâ»Ãá¡ÃÁ·Õè L11-1 L11.2 ·íÒ¡ÒäÍÁä¾ÅáÅдÒǹâËÅ´â»Ãá¡ÃÁà¾×Íè ·´Êͺ¼ÅÅѾ¸¡Ò÷íÒ§Ò¹

ÃÙ»·Õè L11-1 ǧ¨Ã͋ҹ¤‹ÒÊÑ­­Ò³ÍйÒÅÍ¡áÊ´§¼Å·Õè LED 10 ´Ç§¢Í§äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC16F877 â´Â㪌¿§˜ ¡ª¹Ñè Adc_Read ¢Í§ mikroC ¤ÍÁä¾àÅÍÏ


126 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

unsigned int temp_res; void main() { ADCON1 = 0x80; TRISA = 0xFF; TRISB = 0x3F; TRISD = 0; while(1) { temp_res = Adc_Read(2); PORTD = temp_res; PORTB = temp_res >> 2; } }

// // // //

Configure analog inputs and Vref PORTA is input Pins RB7, RB6 are outputs PORTD is output

// Get results of AD conversion // Send lower 8 bits to PORTD // Send 2 most significant bits to RB7, RB6

¡Ò÷íÒ§Ò¹¢Í§â»Ãá¡ÃÁ àÁ×Íè 㪌§Ò¹âÁ´ÙÅ ADC ¨ÐµŒÍ§¡íÒ˹´ãˌ¢Ò¾Íϵ·Õãè ªŒà»š¹ÍÔ¹¾Øµ ʋǹ¾Íϵ·Õàè ª×Íè Áµ‹Í¡Ñº LED ¤×;Íϵ D áÅоÍϵ B ºÔµ 6 áÅкԵ 7 ¹Ñ¹é µŒÍ§¡íÒ˹´ãˌ໚¹àÍÒµ¾µØ ¡ÒÃ͋ҹ¤‹Ò¨Ò¡âÁ´ÙÅá»Å§ÊÑ­­Ò³ÍйÒÅ͡໚¹´Ô¨µÔ ÍŨÐ͋ҹ¤‹ÒÁÒà¡çºänj㹵ÑÇá»Ã temp_res (໚¹µÑÇá»ÃẺ interger 16 ºÔµ) ¨Ò¡¹Ñ¹é ¹íÒ¤‹Ò 8 ºÔµ´ŒÒ¹Å‹Ò§»‡Í¹ãˌ¡ºÑ ¾Íϵ B áŌÇàÅ×Íè ¹ºÔµä»·Ò§¢ÇÒ 2 ¤Ãѧé à¾×Íè ãˌºµÔ 8 áÅÐ 9 ¢Í§µÑÇá»Ã temp_res ¶Ù¡àÅ×Íè ¹ÁÒÍÂÙ·‹ µÕè Òí á˹‹§ºÔµ 6 áÅÐ 7 ¡‹Í¹·Õ¨è Ðʋ§ÍÍ¡áÊ´§ ·Õºè µÔ 6 áÅÐ 7 ¢Í§¾Íϵ B ¨Ò¡¹Ñ¹é ǹÅÙ»·íÒ«éÒí Í‹ҧµ‹Íà¹×Íè §

â»Ãá¡ÃÁ·Õè L11-1 ä¿Å ADCtest.c â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺ·´Êͺ¡ÒÃ͋ҹ¤‹ÒÊÑ­­Ò³ÍйÒÅÍ¡ ÁÒáÊ´§¼Å·Õè LED 10 ´Ç§¢Í§äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC16F877 â´Â㪌¿§˜ ¡ª¹Ñè Adc_Read ¢Í§ mikroC ¤ÍÁä¾àÅÍÏ L6.3 ·´Åͧ»ÃѺ¤‹ÒµÑǵŒÒ¹·Ò¹»ÃѺ¤‹Ò䴌 VR1 Êѧࡵ¡ÒÃà»ÅÕÂè ¹á»Å§¤‹Ò·Õè LED ¡Ò÷´Åͧ¹Õé¨ÐáÊ´§ãˌàË繶֧¡ÒÃ͋ҹ¤‹Ò¨Ò¡âÁ´ÙÅá¼Å§ÊÑ­­Ò³ÍйÒÅ͡໚¹´Ô¨ÔµÍÅ áŌǹíÒ¤‹Ò·Õè ͋ҹ䴌áÊ´§¼ÅÍÍ¡·Õè LED ã¹ÃٻẺ¢Í§µÑÇàÅ¢°Ò¹Êͧ â´ÂáÊ´§¤‹Ò¢ŒÍÁÙÅ 10 ºÔµ´ŒÇ LED 10 ´Ç§ L6.4 ¤íҹdz¤‹ÒàÅ¢°Ò¹Êͧ·ÕÍè ÂÙ㋠¹ÃÙ» LED ·Õàè ÍÒµ¾µØ ¢Í§Ç§¨Ã·´Åͧãˌ໚¹¤‹ÒµÑÇàÅ¢ÃÐËNjҧ 0 ¶Ö§ 1,023

mikroC


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 127

8.4 ÃÕ¨ÊÔ àµÍÏ·àÕè ¡ÕÂè Ç¢ŒÍ§¡ÑºâÁ´ÙÅá»Å§ÊÑ­­Ò³ÍйÒÅ͡໚¹´Ô¨µÔ ÍŢͧ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC16F887 ¿˜§¡ª¹Ñè Adc_Read()Áբ͌ ¨íÒ¡Ñ´àÁ×Íè 㪌¡ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏàºÍÏãËÁ‹æ ઋ¹ àºÍÏ PIC 16F887 (ã¹àÇÍϪ¹Ñ ËÅѧ¨Ò¡ 7.0.0.3 ÍÒ¨ÁÕ¡ÒÃᡌ»­ ˜ ËÒ¹Õ)é à¾×Íè ãˌÊÒÁÒö㪌§Ò¹¡ÑºäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ 䴌·Ø¡àºÍÏ ·Ò§·Õè´Õ·ÕèÊØ´¡ç¤×Í¡ÒÃà¢Õ¹â»Ãá¡ÃÁµÔ´µ‹Í¡ÑºâÁ´ÙÅ ADC ´ŒÇµ¹àͧ ¨Ð¹íÒäÁâ¤Ã ¤Í¹â·ÃÅàÅÍÏàºÍÏ PIC16F887 ÁÒ໚¹µÑÇÍ‹ҧ㹡ÒÃà¢Õ¹â»Ãá¡ÃÁ ´Ñ§¹Ñ¹é ¨Ö§µŒÍ§ÁÕ¤ÇÒÁÃÙàŒ ¡ÕÂè ǡѺ ÃÕ¨ÊÔ àµÍÏã¹äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC16F887 ·Õàè ¡ÕÂè Ç¢ŒÍ§´ŒÇÂ

8.4.1 ÃÕ¨ÊÔ àµÍÏ ADRESH áÅÐ ÃÕ¨ÊÔ àµÍÏ ADRESL ໚¹ÃÕ¨ÔÊàµÍÏ¢¹Ò´ 8 ºÔµ 㪌à¡çº¼ÅÅѾ¸·Õè䴌¨Ò¡¡ÒÃá»Å§ÊÑ­­Ò³ 㹡ÒÃ㪌§Ò¹¿˜§¡ªÑè¹ Adc_Read() äÁ‹¨Òí ໚¹µŒÍ§ÍŒÒ§¶Ö§ÃÕ¨Ê Ô àµÍϹÕé ᵋËÅѧ¨Ò¡àÃÕ¡㪌§Ò¹¿˜§¡ª¹Ñè Adc_Read() áŌǤ‹Ò ·Õè䴌¨Ò¡¡ÒÃá»Å§¨Ðà¡çºänj·ÕèÃÕ¨ÔÊàµÍÏ·Ñé§ÊͧµÑǹÕ鴌ÇÂ

8.4.2 ÃÕ¨ÊÔ àµÍÏ ADCON0 ÁÕ¢¹Ò´ 8 ºÔµ ໚¹ÃÕ¨ÊÔ àµÍÏËÅÑ¡·Õãè ªŒ¤Çº¤ØÁ¡Ò÷íÒ§Ò¹¢Í§âÁ´ÙÅá»Å§ÊÑ­­Ò³ÍйÒÅ͡໚¹ ´Ô¨µÔ ÍÅ ÃÒÂÅÐàÍÕ´¡ÒÃ㪌§Ò¹ÁÕ´§Ñ ¹Õé ºÔµ 7

ADCON0

ºÔµ 6

ºÔµ 5

ºÔµ 4

ºÔµ 3

ºÔµ 2

ºÔµ 1

ºÔµ 0

ADCS1

ADCS0

CHS3

CHS2

CHS1

CHS0

GO/DONE

ADON

R/W -0

R/W -0

R/W -0

R/W -0

R/W -0

R/W -0

R/W -0

R/W -0

ºÔµ 7 áÅÐ 6 : ADCS1 áÅÐ ADCS0 ºÔµ¡íÒ˹´ÊÑ­­Ò³¹Ò́¡Ò·Õãè ªŒ¡ºÑ âÁ´ÙÅ ADC â´Â “00” - FOSC/2 ÊÑ­­Ò³¹Ò́¡ÒÀÒÂã¹ËÒà 2 “01” - FOSC/8 ÊÑ­­Ò³¹Ò́¡ÒÀÒÂã¹ËÒà 8 “10” - FOSC/32 ÊÑ­­Ò³¹Ò́¡ÒÀÒÂã¹ËÒà 32 “11” - FRC ÊÑ­­Ò³¹Ò́¡Ò¨Ò¡Ç§¨Ã RC ÀÒÂã¹ ¤‹ÒÊÙ§ÊØ´ 500 kHz


128 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

ºÔµ 5 ¶Ö§ 2 : CHS3 ¶Ö§ CHS0 ºÔµàÅ×Í¡ª‹Í§ÊÑ­­Ò³ÍйÒÅÍ¡·Õµè ͌ §¡ÒÃ͋ҹ¤‹Ò “0000” - AN0 “0001” - AN1 “0010” - AN2 “0011” - AN3 “0100” - AN4 “0101” - AN5 “0110” - AN6 “0111” - AN7 “1000” - AN8 “1001” - AN9 “1010” - AN10 “1011” - AN11 “1100” - AN12 “1101” - AN13 “1110” - CVREF (áç´Ñ¹ÍŒÒ§ÍԧẺâ»Ãá¡ÃÁ䴌ÊÒí ËÃѺâÁ´ÙŤÍÁ¾ÒàÃàµÍÏ) “1111” - áç´Ñ¹ÍŒÒ§ÍÔ§¤§·Õè (0.6V) ºÔµ 1 : GO/DONE ºÔµàÃÔÁè µŒ¹¡Ò÷íÒ§Ò¹¢Í§âÁ´ÙÅ ADC “0” = ¡ÒÃá»Å§¢ŒÍÁÙÅàÊÃç¨ÊÔ¹é “1” - ໚¹¡ÒÃàÃÔÁè µŒ¹¡ÒÃá»Å§¢ŒÍÁÙÅ ÊíÒËÃѺ¡ÒõÃǨÊͺ¢ŒÍÁÙÅ ¶ŒÒºÔµ¹Õàé »š¹ “1” áÊ´§Ç‹ÒÍÂÙ㋠¹ÃÐËNjҧ¢Ñ¹é µÍ¹¡ÒÃá»Å¢ŒÍÁÙÅ ºÔµ¹Õ¨é Ð໚¹ “0” ÍѵâÁÁѵàÔ Á×Íè ¢Ñ¹é µÍ¹¡ÒÃá»Å§¢ŒÍÁÙÅàÊÃç¨ÊÔ¹é ºÔµ 0 : ADON ºÔµàÍç¹àÍàºÔÅâÁ´ÙÅá»Å§ÊÑ­­Ò³ÍйÒÅ͡໚¹´Ô¨µÔ ÍÅ “0” - âÁ´ÙÅ ADC ¶Ù¡´ÔÊàÍàºÔÅ “1” - àÍç¹àÍàºÔÅâÁ´ÙÅ ADC


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 129

8.4.3 ÃÕ¨ÊÔ àµÍÏ ADCON1 ÃÕ¨ÊÔ àµÍÏ ADCON1 㪌à¾×Íè ¡íÒ˹´¤‹Òáç´Ñ¹ÍŒÒ§ÍÔ§ãˌ¡ºÑ âÁ´ÙÅ ADC áÅСíÒ˹´ÃٻẺ¢Í§ ¼ÅÅѾ¸àÍÒµ¾µØ ·ÕÃè ¨Õ ÊÔ àµÍÏ ADRESH áÅÐ ADRESL ADCON1

ºÔµ 7

ºÔµ 6 -

R/W -0

X -0

ADFM

ºÔµ 5

VCFG1

ºÔµ 4

VCFG0

ºÔµ 3

ºÔµ 2

ºÔµ 1

ºÔµ 0

R/W -0

R/W -0

X -0

X -0

X -0

X -0

-

-

ºÔµ 7 : ADFM ¡íÒ˹´ÃٻẺ¼ÅÅѾ¸·ÃÕè ¨Õ ÊÔ àµÍÏ ADRESH áÅÐ ADRESL “0” - àÅ×Í¡¼ÅÅѾ¸áººªÔ´«ŒÒ (left justified) ÁÕû٠Ẻ¢Í§¢ŒÍÁÙŴѧ¹Õé ¼ÅÅѾ¸ 10 ºÔµ 7

0 7 6 5

0

b b b b b b b b b b 0 0 0 0 0 0

ÃÕ¨ÊÔ àµÍÏ ADRESH

ÃÕ¨ÊÔ àµÍÏ ADRESL

“1” - àÅ×Í¡¼ÅÅѾ¸áººªÔ´¢ÇÒ (right justified) ÁÕû٠Ẻ¢Í§¢ŒÍÁÙŴѧ¹Õé ¼ÅÅѾ¸ 10 ºÔµ %

  %



      > > > > > > > > > >

ÃÕ¨ÊÔ àµÍÏ ADRESH

ºÔµ 6 : äÁ‹ãªŒ§Ò¹ ͋ҹ䴌໚¹ “0” ºÔµ 5 : VCFG1 ¢ÒàÅ×Í¡áç´Ñ¹ÍŒÒ§ÍÔ§ (ź) “0” - VSS “1” - ¢Ò VREFºÔµ 4 : VCFG0: ¢ÒàÅ×Í¡áç´Ñ¹ÍŒÒ§ÍÔ§ (ºÇ¡) “0” - VDD “1” - ¢Ò VREF+ ºÔµ 3 ¶Ö§ 0 : äÁ‹ãªŒ§Ò¹ ͋ҹ䴌໚¹ “0”

ÃÕ¨ÊÔ àµÍÏ ADRESL

-

-


130 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

8.4.4 ÃÕ¨ÊÔ àµÍÏ ANSEL ໚¹ÃÕ¨ÊÔ àµÍÏÊÒí ËÃѺàÅ×Í¡ãˌ¢Ò¾Íϵ RA0 ¶Ö§ RA3, RA5 áÅÐ RE0 ¶Ö§ RE2 ·íҧҹ໚¹¢Ò ¾ÍϵÍÔ¹¾ØµÍйÒÅÍ¡ËÃ×Í´Ô¨µÔ ÍÅ »¡µÔ¶ÒŒ äÁ‹ä´Œ¡Òí ˹´¤‹Ò ¢ÒàËŋҹըé Ð໚¹¢ÒÍÔ¹¾ØµÍйÒÅÍ¡ «Ö§è ᵡµ‹Ò§¨Ò¡ PIC16F877 ·Õ¨è еŒÍ§¡íÒ˹´¢ÒàËŋҹշé ÃÕè ¨Õ ÊÔ àµÍÏ ADCON1 ºÔµ 7

ANSEL

ºÔµ 6

ºÔµ 5

ºÔµ 4

ºÔµ 3

ºÔµ 2

ºÔµ 1

ºÔµ 0

ANS7

ANS6

ANS5

ANS4

ANS3

ANS2

ANS1

ANS0

RE2 R/W -1

RE1 R/W -1

RE0 R/W -1

RA5 R/W -1

RA3 R/W -1

RA2 R/W -1

RA1 R/W -1

RA0 R/W -1

8.4.5 ÃÕ¨ÊÔ àµÍÏ ANSELH ໚¹ÃÕ¨ÊÔ àµÍÏÊÒí ËÃѺàÅ×Í¡ãˌ¢Ò¾Íϵ RB0 ¶Ö§ RB5 ·íҧҹ໚¹¢Ò¾ÍϵÍÔ¹¾ØµÍйÒÅÍ¡ËÃ×Í ´Ô¨ÔµÍÅ »¡µÔ¶ŒÒäÁ‹ä´Œ¡íÒ˹´¤‹Ò ¢ÒàËŋҹÕé¨Ð໚¹¢ÒÍйÒÅÍ¡ ¢ŒÍÃÐÇѧ µíÒá˹‹§¢Í§ª‹Í§ÍйÒÅÍ¡ ᵋÅЪ‹Í§äÁ‹ä´ŒàÃÕ§ÅíҴѺµÒÁµíÒá˹‹§¢Ò¾Íϵ ઋ¹ ÍÔ¹¾Øµ AN8 ¨Ð໚¹¢Ò¾Íϵ RB2 ໚¹µŒ¹

.

ANSELH

ºÔµ 7

ºÔµ 6 -

ANS13

ANS12

ANS11

ANS10

ANS9

ANS8

X -0

X -0

RB5 R/W -1

RB0 R/W -1

RB4 R/W -1

RB1 R/W -1

RB3 R/W -1

RB2 R/W -1

-

ºÔµ 5

ºÔµ 4

ºÔµ 3

ºÔµ 2

ºÔµ 1

ºÔµ 0

8.5 ¢Ñ¹é µÍ¹¡ÒÃà¢Õ¹â»Ãá¡ÃÁà¾×Íè µÔ´µ‹Í¡ÑºâÁ´ÙÅ ADC (1) ¡íÒ˹´¢Ò¾Íϵ ADC ·Õµè ͌ §¡ÒõԴµ‹Íãˌ໚¹¢ÒÍÔ¹¾Øµ â´Â¡íÒ˹´·ÕÃè ¨Õ ÊÔ àµÍÏ TRIS (2) ¡íÒ˹´¤‹Òãˌ¡ºÑ ÃÕ¨ÊÔ àµÍÏ ANSEL áÅÐ ANSELH à¾×Íè ¡íÒ˹´ãˌ¢Ò¾Íϵ·Õµè ͌ §¡ÒÃ໚¹ ¢ÒÍÔ¹¾ØµÍйÒÅÍ¡ (3) ¡íÒ˹´¤‹Òáç´Ñ¹ÍŒÒ§ÍÔ§áÅÐÃٻẺ¢Í§¼ÅÅѾ¸ãˌª´Ô «ŒÒÂËÃ×Í¢ÇÒ¨Ò¡ÃÕ¨ÊÔ àµÍÏ ADCON1 (4) àÅ×Í¡ª‹Í§ÍÔ¹¾Øµ·Õµè ͌ §¡ÒõԴµ‹Í áÅÐàÍç¹àÍàºÔÅ¡Ò÷íÒ§Ò¹¢Í§âÁ´ÙÅ ADC â´Â¡íÒ˹´¤‹Ò ·ÕÃè ¨Õ ÊÔ àµÍÏ ADCON0 (5) ¡íÒ˹´ºÔµ GO/DONE ã¹ÃÕ¨ÊÔ àµÍÏ ADCON0 ãˌ໚¹ “1” à¾×Íè àÃÔÁè µŒ¹¡ÒÃá»Å§¢ŒÍÁÙÅ (6) Ãͨ¹ºÔµ Go/DONE ໚¹ “0” «Ö§è ËÁÒ¶֧ ¡ÒÃá»Å§¢ŒÍÁÙÅÍйÒÅÍ¡àÊÃç¨ÊÔ¹é ¨Ò¡¹Ñ¹é ͋ҹ ¤‹Ò¢ŒÍÁÙŨҡÃÕ¨ÊÔ àµÍÏ ADRESL áÅÐ ADRESH


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 131

»¯ÔºµÑ ¡Ô Ò÷Õè 12 ͋ҹ¤‹Ò¨Ò¡âÁ´ÙÅ ADC ´ŒÇ¡Òà à¢Õ¹â»Ãá¡ÃÁµÔ´µ‹Íâ´ÂµÃ§ ¡ÒÃàª×èÍÁµ‹Í·Ò§ÎÒÏ´áÇÏ 㪌ǧ¨Ãã¹ÃÙ»·Õè L12-1 㹡Ò÷´Åͧ l Ëҡ㪌ºÍÏ´·´Åͧ NX-877 plus II ãˌµÍ ‹ ǧ¨ÃµÒÁÃÙ»·Õè L12-1 áŌÇà»ÅÕÂè ¹äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ໚¹ PIC16F887 l

+5V

R11 10k

11 1

32 Vcc

C3 0.1PF

MCLR

RB7 40

SW1 RESET

RB6

21+$.&&%

RD7

RD4 RD3 RD2 VR1 10k

4 12 31

39

LED8-bit7 LED7-bit6

28

LED6-bit5

27

LED5-bit4

22

LED4-bit3

21

LED3-bit2

RD1 20

AN2/RA2

RD0

GND

LED2-bit1

19

LED1-bit0 (LSB) R1-R10 510:

GND OSC1

OSC2

13 C1 22pF

LED9-bit8

30

RD6 29 RD5

+5V

LED10-bit9 (MSB)

14 XTAL1 20MHz

C2 22pF

ÃÙ»·Õè L12-1 ǧ¨Ã͋ҹ¤‹ÒÊÑ­­Ò³ÍйÒÅÍ¡áÊ´§¼Å·Õè LED 10 ´Ç§¢Í§äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC16F887 ´ŒÇ¡ÒÃà¢Õ¹â»Ãá¡ÃÁà¾×Íè µÔ´µ‹Í¡ÑºâÁ´ÙÅá»Å§ÊÑ­­Ò³ÍйÒÅ͡໚¹´Ô¨µÔ ÍÅâ´ÂµÃ§


132 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

¢Ñ鹵͹¡Ò÷´Åͧ L12.1 ແ´â»Ãá¡ÃÁ mikroC ÊÌҧä¿Åâ»Ãà¨ç¡µ â´ÂàÅ×Í¡äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏáÅФÇÒÁ¶ÕÊè ­ Ñ ­Ò³¹Ò́¡Ò´Ñ§¹Õé l ¡Ã³Õ㪌ºÍÏ´ NX-877 plus II àÅ×Í¡ PIC16F877 ËÃ×Í PIC16F877A ¤ÇÒÁ¶ÕÊ è­ Ñ ­Ò³¹Ò́¡Ò 20MHz L12.2 ÊÌҧä¿Åâ»Ãà¨ç¡µªÍ×è ADC_Test áÅоÔÁ¾â»Ãá¡ÃÁÀÒÉÒ C µÒÁâ»Ãá¡ÃÁ·Õè L12-1 L12.3 ·íÒ¡ÒäÍÁä¾ÅáÅдÒǹâËÅ´â»Ãá¡ÃÁà¾×Íè ·´Êͺ¼ÅÅѾ¸¡Ò÷íÒ§Ò¹ L12.4 ·´Åͧ»ÃѺ¤‹ÒµÑǵŒÒ¹·Ò¹»ÃѺ¤‹Ò䴌 VR1 Êѧࡵ¡ÒÃà»ÅÕÂè ¹á»Å§¤‹Ò·Õè LED LED ·Ñ§é 10 ´Ç§¨Ð㪌᷹¤ÇÒÁËÁÒ¢ͧàÅ¢°Ò¹Êͧ 10 ºÔµ «Ö§è ໚¹¼ÅÅѾ¸¨Ò¡¡ÒÃá»Å§ÊÑ­­Ò³ ÍйÒÅ͡໚¹´Ô¨µÔ ÍÅ â´Â LED10 ·Õµè ͋ ¡Ñº¾Íϵ RB7 ¨Ðá·¹ºÔµ¼ÅÅѾ¸·ÁÕè ¹Õ ÂÑ ÊíҤѭÊÙ§ÊØ´ (MSB) ËÃ×ͺԵ 9 ¨Ò¡¹Ñ¹é äŋµÒÁÅíҴѺ¨¹¶Ö§ LED1 ·Õµè ͋ ¡Ñº¾Íϵ RD0 ¨Ðá·¹ºÔµ¼ÅÅѾ¸·ÁÕè ¹Õ ÂÑ ÊíҤѭµèÒí ÊØ´ (LSB) ËÃ×ͺԵ 0 L12.5 ¤íҹdz¤‹ÒàÅ¢°Ò¹Êͧ·ÕÍè ÂÙ㋠¹ÃÙ» LED ·Õàè ÍÒµ¾µØ ¢Í§Ç§¨Ã·´Åͧãˌ໚¹¤‹ÒµÑÇàÅ¢ÃÐËNjҧ 0 ¶Ö§ 1,023 void main() { TRISA = 0xFF; TRISB = 0x3F; TRISD = 0; ANSEL = 0xFF; ADCON1 = 0x80; ADCON0=0b11001001; while(1) { ADCON0.GO=1; while(ADCON0.GO); PORTD=ADRESL; PORTB=ADRESH*64; } }

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

PORTA => input Pins RB7, RB6 are outputs PORTD is output PORTA => Analog Select Vref and Right Justify Select Analog1 RC_Mode and ADON

// // // //

Start conversion Waiting conversion done? Load Low Byte to LED Load High Byte and shift left 6 bit to LED

¡Ò÷íÒ§Ò¹¢Í§â»Ãá¡ÃÁ â»Ãá¡ÃÁ¨Ð¡íÒ˹´ãˌ¾Íϵ A ໚¹ÍÔ¹¾Øµ ÃÇÁ·Ñ§é ¡íÒ˹´¤‹ÒãËŒÃ¨Õ ÊÔ àµÍÏ ANSEL à¾×Íè ãˌ¢Ò¾Íϵ A ໚¹ÍйÒÅÍ¡´ŒÇ ¨Ò¡¹Ñ¹é ¡íÒ˹´ãˌ¾Íϵ D áÅÐ¢Ò RB6 ¡Ñº RB7 ໚¹àÍÒµ¾µØ à¾×Íè áÊ´§¼Å LED µÒÁ´ŒÇ ¡ÒáíÒ˹´¤‹Òãˌ¡ºÑ ÃÕ¨ÊÔ àµÍÏ ADCON0 áÅÐ ADCON1 à¾×Íè àÅ×͡㪌§Ò¹ª‹Í§ÍÔ¹¾Øµ AN2 ¨Ò¡¹Ñ¹é ·íÒ¡ÒÃǹÅÙ» àÃÔÁè µŒ¹¡ÒÃá»Å§¢ŒÍÁÙÅâ´Â૵ºÔµ GO ¢Í§ ADCON0 ໚¹ “1” Ãͨ¹¡ÃÐ·Ñ§è ºÔµ ¹Õ¡é ÅÒÂ໚¹ “0” áÊ´§Ç‹Ò ¡ÒÃá»Å§àÊÃç¨ÊÁºÙó ãˌ¹Òí ¤‹Ò¼ÅÅѾ¸ã¹ÃÕ¨ÊÔ àµÍÏ ADRESL »‡Í¹ãˌ¡ºÑ ÃÕ¨ÊÔ àµÍÏ PORTD áÅФ‹Ò¼ÅÅѾ¸ã¹ÃÕ¨ÊÔ àµÍÏ ADRESH àÅ×Íè ¹ºÔµ¨Ò¡ºÔµ 0 áÅÐ 1 ä»à»š¹ºÔµ 6 áÅÐ 7 ´ŒÇ¡Òäٳ´ŒÇ 64 ¨Ò¡¹Ñ¹é »‡Í¹ä»ÂѧÃÕ¨ÊÔ àµÍÏ PORTB à¾×Íè áÊ´§¼Å¡Ò÷íÒ§Ò¹·Õ¢è Ò¾Íϵ RB6 áÅÐ RB7

â»Ãá¡ÃÁ·Õè L12-1 ä¿Å ADC_Test1.c â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺ·´Êͺ¡ÒÃ͋ҹ¤‹ÒÊÑ­­Ò³ÍйÒÅÍ¡ ÁÒáÊ´§¼Å·Õè LED 10 ´Ç§¢Í§äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC16F887 ´ŒÇ¡ÒÃà¢Õ¹â»Ãá¡ÃÁà¾×Íè µÔ´µ‹Í ¡ÑºâÁ´ÙÅá»Å§ÊÑ­­Ò³ÍйÒÅ͡໚¹´Ô¨µÔ ÍÅâ´ÂµÃ§ mikroC


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 133

»¯ÔºÑµÔ¡Ò÷Õè 13 ͋ҹ¤‹Ò¨Ò¡âÁ´ÙÅ ADC áÊ´§¼Å ·Õâè Á´ÙÅ LCD ¡Ò÷´Åͧ¹Õàé »š¹µÑÇÍ‹ҧ㹡ÒùíҼŷÕäè ´Œ¨Ò¡¡ÒÃ͋ҹ¤‹Ò¨Ò¡âÁ´ÙÅá»Å§ÊÑ­­Ò³ÍйÒÅ͡໚¹´Ô¨µÔ ÍÅ ¢Í§äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC16F887 ÁÒáÊ´§ÂѧâÁ´ÙÅ LCD â´Â¡ÒÃà¢Õ¹â»Ãá¡ÃÁà¾×Íè µÔ´µ‹Í¡ÑºâÁ´ÙÅ LCD ¹Ñ¹é àÅ×͡㪌¿§˜ ¡ª¹Ñè LCD ¢Í§ mikroC ÊÒÁÒö´ÙÃÒÂÅÐàÍÕ´¡ÒÃà¢Õ¹â»Ãá¡ÃÁà¾ÔÁè àµÔÁ䴌¨Ò¡µÑÇÍ‹ҧ¡ÒÃ㪌§Ò¹¿˜§¡ª¹Ñè LCD ÊíÒËÃѺ¢ŒÍÁÙŵÑÇàÅ¢·Õäè ´Œ¨Ò¡¡ÒÃá»Å§ÊÑ­­Ò³¡‹Í¹·Õ¨è йíÒä»áÊ´§¼Å·Õâè Á´ÙÅ LCD ¨ÐµŒÍ§á»Å§ãˌÍÂÙ㋠¹ÃÙ» Ẻ¢Í§ÃËÑÊáÍÊ¡Õ¡é ͋ ¹ â´Â¶ŒÒ໚¹¤‹Ò¨íҹǹàµçÁ¨Ð㪌¿§˜ ¡ª¹Ñè WordToStr() 㹡ÒÃá»Å§¤‹Ò áÅÐ㪌¿§˜ ¡ª¹Ñè FloatToStr() 㹡ÒÃá»Å§¤‹Ò¢ŒÍÁÙÅ·Õàè »š¹àÅ¢·È¹ÔÂÁ

¡ÒÃàª×èÍÁµ‹Í·Ò§ÎÒÏ´áÇÏ 㪌ǧ¨Ãã¹ÃÙ»·Õè L13-1 㹡Ò÷´Åͧ l Ëҡ㪌ºÍÏ´ RBX-877 Rev.2007 ÊÒÁÒö·´Åͧ䴌àÅ â´ÂàÅ×Í¡äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ໚¹àºÍÏ PIC16F887 l

l

Ëҡ㪌ºÍÏ´·´Åͧ NX-877 plus II ãˌµÍ‹ ǧ¨ÃµÒÁÃÙ»·Õè L13-1 áŌÇà»ÅÕÂè ¹äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ໚¹ PIC16F887

ÃÙ»·Õè L13-1 ǧ¨Ã·´Åͧ¡ÒÃ͋ҹ¤‹ÒÍйÒÅÍ¡¨Ò¡µÑǵŒÒ¹·Ò¹»ÃѺ¤‹Ò䴌ÁÒáÊ´§¼Å·Õâè Á´ÙÅ LCD


134 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

int dat=0; float volts; char txt1[6]; char txt2[13]; char *text1 ="DATA="; char *text2 ="Volt="; void main() { TRISA = 0xFF; ANSEL = 0xFF; ADCON1 = 0x00; ADCON0=0b11001001; Lcd_Init(&PORTD); Lcd_Cmd(LCD_CURSOR_OFF); Lcd_Cmd(LCD_CLEAR); while(1) { ADCON0.GO=1; while(ADCON0.GO); dat = (ADRESH*4)+(ADRESL/64); WordToStr(dat,txt1); Lcd_Out(1,1,text1); Lcd_Out(1,6,txt1); volts=(dat*5.0)/1023.0; FloatToStr(volts,txt2); Lcd_Out(2,1,text2); Lcd_out(2,6,txt2); Delay_ms(300); Lcd_Cmd(LCD_CLEAR); } }

// Save ADC // Save String // Show Text

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

PORTA => input PORTA => Analog Select Vref and Right Justify Select Analog1 RC_Mode and ADON Init LCD Port LCD cursor off

// // // // //

Start conversion Waiting conversion done? Sum highbyte and lowbyte Convert Raw Data to String Show Line 1 Text

// Convert to Volt // Convert to String // Show LCD Line 2 // Clear LCD

¡Ò÷íÒ§Ò¹¢Í§â»Ãá¡ÃÁ 1.àÅ×Í¡¡Òõ‹ÍâÁ´ÙÅ LCD ໚¹áººÁҵðҹ¡Ñº¾Íϵ D ¨Ö§äÁ‹µÍŒ §ÁÕ¡ÒáíÒ˹´¢Ò¾Íϵ·Õàè ª×Íè Áµ‹Íã¹â»Ãá¡ÃÁ 2. ¼ÅÅѾ¸¨Ò¡âÁ´ÙÅá»Å§ÊÑ­­Ò³ÍйÒÅ͡໚¹´Ô¨µÔ ÍŶ١àÅ×Í¡ãˌÁÃÕ »Ù Ẻ¢ŒÍÁÙŪԴ´ŒÒ¹«ŒÒ (ADCON1 =0x00) 3. ǹÅٻ͋ҹ¤‹Ò¨Ò¡Á´ÙÅá»Å§ÊÑ­­Ò³ÍйÒÅ͡໚¹´Ô¨µÔ ÍÅ ¹íÒ¤‹Ò¢Í§ ADRESH áÅÐ ADRESL ÁÒÃÇÁ¡Ñ¹ â´Â¡‹Í¹¡ÒÃÃÇÁ¨ÐµŒÍ§àÅ×Íè ¹ºÔµ¢Í§ÃÕ¨ÊÔ àµÍÏ ADRESH ä»·Ò§«ŒÒ 2 ºÔµ (¤Ù³ 4) áÅÐàÅ×Íè ¹ºÔµ¢Í§ÃÕ¨ÊÔ àµÍÏ ADRESL ä»·Ò§¢ÇÒ 6 ºÔµ (ËÒà 64) 4. ¹íÒ¤‹Ò·Õäè ´Œá»Å§à»š¹ÃËÑÊáÍÊ¡Õ¼é ҋ ¹¿˜§¡ª¹Ñè WordToStr â´ÂµŒÍ§¨Í§¾×¹é ·ÕËè ¹‹Ç¤ÇÒÁ¨íÒ 6 亵ÊÒí ËÃѺ à¡çº¼ÅÅѾ¸ ¡‹Í¹¹íÒ¤‹Òä»áÊ´§·Õâè Á´ÙÅ LCD 5. ¤íҹdz¤‹Ò·ÕÍè ҋ ¹ä´ŒÍÍ¡ÁÒã¹ÃÙ»¢Í§áç´Ñ¹ 0 ¶Ö§ 5V â´Âãˌ¤Ò‹ ¼ÅÅѾ¸à»š¹àÅ¢·È¹ÔÂÁ 6. á»Å§àÅ¢·È¹ÔÂÁãˌ໚¹ÃËÑÊáÍÊ¡Õ¼é ҋ ¹¿˜§¡ª¹Ñè FloatToStr â´Â¨ÐµŒÍ§¨Í§¾×¹é ·ÕËè ¹‹Ç¤ÇÒÁ¨íÒ 13 亵 ÊíÒËÃѺà¡çº¼ÅÅѾ¸ ¡‹Í¹¹íÒ¤‹ÒáÊ´§·Õâè Á´ÙÅ LCD

â»Ãá¡ÃÁ·Õè L13-1 ä¿Å ADCtestLCD.c â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺ·´Êͺ¡ÒÃ͋ҹ¤‹ÒÊÑ­­Ò³ÍйÒÅÍ¡ ÁÒáÊ´§¼Å·Õâè Á´ÙÅ LCD


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ��ä͹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 135

¢Ñ鹵͹¡Ò÷´Åͧ L13.1 ແ´â»Ãá¡ÃÁ mikroC ÊÌҧä¿Åâ»Ãà¨ç¡µ â´ÂàÅ×Í¡äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏáÅФÇÒÁ¶ÕÊè ­ Ñ ­Ò³¹Ò́¡Ò´Ñ§¹Õé l ¡Ã³Õ㪌ºÍÏ´ NX-877 plus II àÅ×Í¡ PIC16F887 ¤ÇÒÁ¶ÕÊ è­ Ñ ­Ò³¹Ò́¡Ò 20MHz L13.2 ÊÌҧä¿Åâ»Ãà¨ç¡µªÍ×è ADCtestLCD áÅоÔÁ¾â»Ãá¡ÃÁÀÒÉÒ C µÒÁâ»Ãá¡ÃÁ·Õè L13-1 L13.3 ·íÒ¡ÒäÍÁä¾ÅáÅдÒǹâËÅ´â»Ãá¡ÃÁà¾×Íè ·´Êͺ¼ÅÅѾ¸¡Ò÷íÒ§Ò¹ L13.4 ·´Åͧ»ÃѺ¤‹ÒµÑǵŒÒ¹·Ò¹»ÃѺ¤‹Ò䴌 VR1 Êѧࡵ¡ÒÃà»ÅÕÂè ¹á»Å§¤‹Ò·Õâè Á´ÙÅ LCD àÁ×Íè Ãѹâ»Ãá¡ÃÁ áŌǷ´Åͧ»ÃѺ¤‹Ò¢Í§µÑǵŒÒ¹·Ò¹ ·Õâè Á´ÙÅ LCD áÊ´§¼Å´Ñ§¹Õé

Data= 813 Volts=3.973606

INNOVATIVE EXPERIMENT


136 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 137

º··Õè 9

㪌§Ò¹¿˜§¡ª¹Ñè PWM ¢Í§ mikroC âÁ´ÙÅ CCP (Capture-Compare-PWM) ໚¹âÁ´ÙÅ·ÕÁè ÍÕ ÂÙÀ‹ ÒÂã¹äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ÃдѺ ¡ÅÒ§ ÁÕ˹ŒÒ·Õãè ¹¡ÒõÃǨ¨ÑºÊÑ­­Ò³, à»ÃÕºà·ÕºÊÑ­­Ò³ áÅСíÒà¹Ô´ÊÑ­­Ò³ PWM (Pulse Width Modulation) ËÃ×ÍÊÑ­­Ò³Á´ÙàŪÑè¹·Ò§¤ÇÒÁ¡ÇŒÒ§¢Í§¾ÑÅʏ ÊíÒËÃѺ㹺·¹Õé¨Ð¡Å‹ÒǶ֧âÁ´ÙÅ CCP 㹡Ò÷íÒ§Ò¹ÊÌҧÊÑ­­Ò³ PWM â´Â㪌¿§˜ ¡ª¹Ñè PWM ·ÕÁè ÁÕ Ò¾ÃŒÍÁ¡Ñº mikroC ¤ÍÁä¾àÅÍÏ ã¹ÃÙ»·Õè 9-1 áÊ´§µÑÇÍ‹ҧ¢Í§ÊÑ­­Ò³ PWM ·ÕÁè ¤Õ ÇÒÁ¡ÇŒÒ§áµ¡µ‹Ò§¡Ñ¹ Íѹʋ§ãˌà¡Ô´áç´Ñ¹ à©ÅÕèÂäÁ‹à·‹Ò¡Ñ¹´ŒÇ â´Â¶ŒÒÂÔè§ÊÑ­­Ò³ PWM ÁÕ¤ÇÒÁ¡ÇŒÒ§à·‹Òã´ áç´Ñ¹ä¿µÃ§·Õè䴌¨ÐÁҡ෋ҹÑé¹ ¨Ö§ÊÒÁÒö¹íÒÊÑ­­Ò³ PWM ä»ãªŒã¹¡ÒäǺ¤ØÁ¤ÇÒÁàÃçǢͧÁÍàµÍÏ俵ç䴌 áç´Ñ¹ (V)

áç´Ñ¹ (V)

4.8V

4.8V

áç´Ñ¹à©ÅÕÂè

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

2.4V

(¡) áç´Ñ¹ (V)

áç´Ñ¹à©ÅÕÂè

àÇÅÒ

(¢) áç´Ñ¹ (V)

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

4 .8 V

àÇÅÒ

áç´Ñ¹à©ÅÕÂè ´ÔǵÕäé «à¡ÔÅ 25 %

4 .8 V

3 .6 V 1 .2 V

(¤)

àÇÅÒ

(§)

ÃÙ»·Õè 9-1 áÊ´§¡ÒÃà»ÃÕºà·Õº¤‹Òáç´Ñ¹·Õàè ¡Ô´¢Ö¹é àÁ×Íè 㪌 PWM (¡) »‡Í¹ÊÑ­­Ò³ä¿µÃ§ (¢) PWM ÁÕ´ÇÔ µÕäé «à¡ÔÅ 50% (¤) PWM ÁÕ´ÇÔ µÕäé «à¡ÔÅ 75% (§) PWM ÁÕ´ÇÔ µÕäé «à¡ÔÅ 25%

àÇÅÒ


138 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

µÑÇÍ‹ҧ¨Ò¡ÃÙ»·Õè 9-1 (¢) Áդҋ ´ÔǵÕäé «à¡ÔÅ 50% ËÁÒ¶֧ ¤ÇÒÁ¡ÇŒÒ§¢Í§¾ÑÅʏªÇ‹ §ºÇ¡ÁÕ¤ÇÒÁ ¡ÇŒÒ§à»š¹ 50% ¢Í§¤ÇÒÁ¡ÇŒÒ§·Ñ§é ËÁ´ ´Ñ§¹Ñ¹é áç´Ñ¹à©ÅÕÂè ·Õäè ´Œà·‹Ò¡Ñº (50 N 4.8) /100 = 2.4V ÊíÒËÃѺ ÃÙ»·Õè 9-1 (¤) áÅÐ (§) ໚¹¡ÒáíÒ˹´¤‹Ò´ÔǵÕäé «à¡ÔÅ 75% áÅÐ 25% µÒÁÅíҴѺ 㹡ÒäǺ¤ØÁÁÍàµÍÏâ´Â»¡µÔ¹¹Ñé ¨Ð»‡Í¹áç´Ñ¹ä¿µÃ§ãˌ¡ºÑ ÁÍàµÍÏ ÁÍàµÍÏ¡¨ç зíÒ§Ò¹àµçÁ ¡íÒÅѧ «Ö§è ÍÒ¨¨ÐÁÕ¤ÇÒÁàÃçÇÁÒ¡à¡Ô¹ä» ´Ñ§¹Ñ¹é ¡ÒûÃѺ¤ÇÒÁàÃçǢͧÁÍàµÍϨ§Ö ãªŒÇ¸Ô ÅÕ ´áç´Ñ¹ä¿¿‡Ò·Õ»è ͇ ¹ ãˌ¡ºÑ ÁÍàµÍÏ ÇÔ¸·Õ ¹Õè ÂÔ Á¤×Í ¡Òû‡Í¹¾ÑÅʏ仢ѺÁÍàµÍÏ᷹ áŌǻÃѺ¤ÇÒÁ¡ÇŒÒ§¾ÑÅʏªÇ‹ §ºÇ¡ à¾×Íè ãˌ 䴌¤Ò‹ áç´Ñ¹à©ÅÕÂè µÒÁµŒÍ§¡Òà ÇÔ¸¡Õ ÒùÕàé ÃÕÂ¡Ç‹Ò ¾ÑÅÊÇ´Ô ¸ÁÍ´ÙàÅàµÍÏ (PWM) â´Â¤ÇÒÁ¡ÇŒÒ§¾ÑÅʏªÇ‹ §ºÇ¡ àÁ×Íè à·Õº¡Ñº¤ÇÒÁ¡ÇŒÒ§¾ÑÅʏ·§Ñé ËÁ´àÃÕÂ¡Ç‹Ò ´ÔǵÕäé «à¡ÔÅ â´Â¨Ð¤Ô´¤‹Ò´ÔǵÕäé «à¡ÔÅ໚¹à»ÍÏà«ç¹µ¢Í§¤‹Ò¤ÇÒÁ ¡ÇŒÒ§¾ÑÅʏ·§Ñé ËÁ´ ã¹ mikroC ¤ÍÁä¾àÅÍÏ䴌àµÃÕÂÁ¿˜§¡ª¹Ñè PWM änjÃͧÃѺ¡ÒÃ㪌§Ò¹âÁ´ÙÅ CCP ã¹âËÁ´ ÊÌҧÊÑ­­Ò³ PWM ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC16F887 ÁÕâÁ´ÙÅ PWM ÁÒ¡¡Ç‹Ò 1 ªØ´ ´Ñ§¹Ñ¹é ¡ÒÃ㪌§Ò¹¿˜§¡ª¹Ñè PWM ¨Ðẋ§à»š¹ PWM1 áÅÐ PWM2 ઋ¹ Pwm1_Start() ໚¹µŒ¹

9.1 ¿˜§¡ª¹Ñè Pwm_Init ໚¹¤íÒÊÑ§è ¡íÒ˹´¤‹ÒàÃÔÁè µŒ¹ã¹¡Ò÷íÒ§Ò¹ãˌᡋâÁ´ÙÅ PWM áÅСíÒ˹´¤‹Ò´ÔǵÕäé «à¡ÔÅ෋ҡѺ 0 ¿˜§¡ª¹Ñè ¹Õ¨é еŒÍ§¶Ù¡àÃÕ¡¡‹Í¹ãªŒ§Ò¹¿˜§¡ª¹Ñè Í×¹è æ ÃٻẺ¡ÒÃ㪌§Ò¹ void Pwm_Init(unsigned long freq);

¾ÒÃÒÁÔàµÍÏ freq

㪌¡Òí ˹´¤ÇÒÁ¶Õ¢è ͧ PWM ˹‹ÇÂ໚¹ Hz

µÑÇÍ‹ҧ·Õè 9-1 Pwm_Init(5000);

໚¹¡ÒáíÒ˹´¤‹ÒàÃÔÁè µŒ¹ãˌ¡ºÑ âÁ´ÙÅ PWM ·íÒ§Ò¹·Õ¤è ÇÒÁ¶Õè 5kHz


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 139

9.2 ¿˜§¡ª¹Ñè Pwm_Change_Duty ໚¹¤íÒÊÑ§è ¡íÒ˹´¤‹Ò¤ÇÒÁ¡ÇŒÒ§¢Í§ÊÑ­­Ò³ PWM ËÃ×Í·Õàè ÃÕÂ¡Ç‹Ò ´ÔǵÕäé «à¡ÔÅ ÃٻẺ¡ÒÃ㪌§Ò¹ void Pwm_Change_Duty(unsigned short duty_ratio);

¾ÒÃÒÁÔàµÍÏ

Áդҋ 0 ¶Ö§ 255 ÊíÒËÃѺ¤‹Ò´ÔǵÕäé «à¡ÔÅ 0 ¶Ö§ 100% ઋ¹ ¶ŒÒ¤‹Ò෋ҡѺ 0 ´ÔǵÕäé «à¡ÔŨÐ໚¹ 0% ËÒ¡¤‹Ò෋ҡѺ 127 ´ÔǵÕäé «à¡ÔÅ෋ҡѺ 50%áÅжŒÒ෋ҡѺ 255 ¤‹Ò´ÔǵÕäé «à¡ÔŨÐ໚¹ 100% ËÃ×ͤíҹdz䴌¨Ò¡ duty_ratio

% ´ÔǵÕäé «à¡ÔÅ = (duty_ratio x 255)/100

µÑÇÍ‹ҧ·Õè 9-2 Pwm_Change_Duty(192);

໚¹¡ÒáíÒ˹´¤‹Ò´ÔǵÕäé «à¡ÔÅ 75% ãˌዠ¡‹âÁ´ÙÅÊÌҧÊÑ­­Ò³ PWM

9.3 ¿˜§¡ª¹Ñè Pwm_Start ໚¹¿˜§¡ªÑè¹àÃÔèÁµŒ¹¡Ò÷íÒ§Ò¹¢Í§¡ÒÃÊÌҧÊÑ­­Ò³ PWM 㹡ÒÃ㪌§Ò¹µŒÍ§àÃÕ¡¿˜§¡ªÑè¹ Pwm_Init áÅÐ Pwm_Change_Duty ¡‹Í¹ ÃٻẺ¡ÒÃ㪌§Ò¹ void Pwm_Start(void);

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

9.4 ¿˜§¡ª¹Ñè Pwm_Stop ໚¹¿˜§¡ª¹Ñè ËÂØ´¡Ò÷íÒ§Ò¹¢Í§âÁ´ÙÅ PWM ÃٻẺ¡ÒÃ㪌§Ò¹ void Pwm_Stop(void);

µÑÇÍ‹ҧ·Õè 9-4 Pwm_Stop();


140 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

»¯ÔºµÑ ¡Ô Ò÷Õè 14 ¤Çº¤ØÁ¤ÇÒÁàÃçǢͧÁÍàµÍϴnj ¿˜§¡ª¹Ñè PWM 㹡Ò÷´Åͧ¹Õàé »š¹µÑÇÍ‹ҧ¡ÒÃÊÌҧÊÑ­­Ò³ PWM à¾×Íè ¹íÒ令Ǻ¤ØÁ¤ÇÒÁàÃçǢͧÁÍàµÍÏ 俵ç â´Â·íҧҹËÇÁ¡ÑºÇ§¨Ã¢ÑºÁÍàµÍÏ·ãÕè ªŒäÍ«Õ¢ºÑ âËÅ´¡ÃÐáÊÊ٧ẺàͪºÃÔ´¨ (H-bridge) àºÍÏ L293D

¡ÒÃàª×èÍÁµ‹Í·Ò§ÎÒÏ´áÇÏ 㪌ǧ¨Ãã¹ÃÙ»·Õè L14-1 㹡Ò÷´Åͧ l Ëҡ㪌ºÍÏ´ RBX-877 Rev.2007 ÊÒÁÒö·´Åͧ䴌àÅ â´ÂàÅ×Í¡äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ໚¹àºÍÏ PIC16F887 l

Ëҡ㪌ºÍÏ´·´Åͧ NX-877 plus II ãˌµÍ‹ ǧ¨ÃµÒÁÃÙ»·Õè L9-1 áŌÇàÅ×Í¡äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ໚¹ PIC16F877 ËÃ×Í PIC16F877A ¤ÇÒÁ¶ÕÊè ­ Ñ ­Ò³¹Ò́¡Ò 20MHz ÊíÒËÃѺ LED ·Õãè ªŒáÊ´§¼Å¡Ò÷íÒ§Ò¹¢Í§Ç§¨Ã¢ÑºÁÍàµÍÏ ¹Ñ¹é ໚¹ LED 2 ÊÕ (á´§/à¢ÕÂÇ) 2 ¢Ò ʋǹáç´Ñ¹ +Vm ໚¹áç´Ñ¹ä¿àÅÕÂé §ÊíÒËÃѺǧ¨Ã¢ÑºÁÍàµÍÏ «Ö§è µŒÍ§á¡ÍÍ¡ ¨Ò¡ä¿àÅÕÂé §¢Í§äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC

l

K18A-K18B MOTOR-A +5V

11

C7 0.1F 50V

+5V +Vm

32

C5 0.1F 50V

RD0 RD1

19

2

20

7 2A

RC2/CCP1 17 RB1 RB2 RC1/CCP2

IC2 PIC16F887

16

1

1A

12EN

34

15 4A

35

10

16

9

8

Vcc1

1Y 3 2Y

+ +

R9 2k2

DIRECT LED3 DIR. #A

6

14 IC3 4Y L293D 11 3Y

LED4 DIR. #B

R10 2k2

3A

INVERT

34EN

13 12 5

4

INVERT

+

C8 0.1F 50V

+

K19A-K19B MOTOR-B

DIRECT

Motor Driver CR1 Ceramic Resonator 20MHz

13

14

ÃÙ»·Õè L14-1 ǧ¨Ã¤Çº¤ØÁ¤ÇÒÁàÃçǢͧÁÍàµÍÏ俵ç â´Â㪌ʭ Ñ ­Ò³ PWM ·ÕÊè Ìҧ¢Ö¹é ¨Ò¡äÁâ¤Ã ¤Í¹â·ÃÅàÅÍÏ PIC16F87/887 ´ŒÇ¿˜§¡ª¹Ñè PWM ¢Í§ mikroC ¤ÍÁä¾àÅÍÏ


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹ void main() { /* Initial port for control motor */ ANSELH.F0=0; // RB1 ==> ANSELH.F2=0; // RB2 ==> TRISB.F1=0; // Motor B TRISB.F2=0; // Motor B TRISD.F0=0; // Motor A TRISD.F1=0; // MOtor A

l 141

Digital IO Digital IO 2A 2B 1A 1B

/* Initial PWM module freq = 5 kHz */ Pwm1_Init(5000); // Initail PWM 1E Pwm2_Init(5000); // Initail PWM 2E Pwm1_Start(); Pwm2_Start(); Pwm1_Change_Duty(190); // Motor A 75% Speed Pwm2_Change_Duty(190); // Motor B 75% Speed while(1) { PORTD.F0 =0;PORTD.F1 =1; // Motor A Forward PORTB.F1 =0;PORTB.F2 =1; // Motor B Forward Delay_ms(2000); PORTB.F1 =1;PORTB.F2 =0; // Motor B Backward PORTD.F0 =1;PORTD.F1 =0; // Motor A Backward Delay_ms(2000); } }

¡Ò÷íÒ§Ò¹¢Í§â»Ãá¡ÃÁ â»Ãá¡ÃÁ¡íÒ˹´¤ÇÒÁ¶Õ¢è ͧÊÑ­­Ò³¾ÑÅÊÇ´Ô ¸ÁÍ´ÙàŪѹè ËÃ×Í PWM änj·Õè 5kHz ¼‹Ò¹¤íÒÊѧè Pwm_Init() ¨Ò¡¹Ñ¹é àÃÔÁè µŒ¹¡Ò÷íÒ§Ò¹¢Í§âÁ´ÙÅ PWM ´ŒÇ¡ÒÃʋ§¤íÒÊѧè Pwm_Start() áÅСíÒ˹´¤‹Ò¤ÇÒÁ¡ÇŒÒ§¢Í§ ÊÑ­­Ò³ PWM ´ŒÇ¤íÒÊѧè Pwm_Chage_Duty()ã¹·Õ¹è ¡Õé Òí ˹´¤‹Òänj෋ҡѺ 190 «Ö§è àÁ×Íè ¤íҹdz¤‹ÒÍ͡໚¹ à»ÍÏૹµ ¨ÐÁդҋ »ÃÐÁÒ³ 75% àÁ×Íè ¹íÒ仢ѺÁÍàµÍÏ ÁÍàµÍϨÐËÁعä»ã¹·ÔÈ·Ò§µÃ§ 2 ÇÔ¹Ò·Õ áÅÐËÁع¡ÅѺ 2 ÇÔ¹Ò·ÕÊÅѺ¡Ñ¹ä»àÃ×Íè  æ

â»Ãá¡ÃÁ·Õè L14-1 ä¿Å PWMtest.c â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺ·´Êͺ¡ÒäǺ¤ØÁ¤ÇÒÁàÃçǢͧÁÍàµÍÏ 俵ç â´Â㪌ʭ Ñ ­Ò³ PWM ·ÕÊè Ìҧ¢Ö¹é ¨Ò¡äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC16F87/887 ´ŒÇ¿˜§¡ª¹Ñè PWM ¢Í§ mikroC ¤ÍÁä¾àÅÍÏ


142 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

¢Ñ鹵͹¡Ò÷´Åͧ L14.1 ແ´â»Ãá¡ÃÁ mikroC ÊÌҧä¿Åâ»Ãà¨ç¡µ PWMtest áÅоÔÁ¾â»Ãá¡ÃÁÀÒÉÒ C µÒÁâ»Ãá¡ÃÁ·Õè L9-1 L14.2 ·íÒ¡ÒäÍÁä¾ÅáÅдÒǹâËÅ´â»Ãá¡ÃÁà¾×Íè ·´Êͺ¼ÅÅѾ¸¡Ò÷íÒ§Ò¹ L14.3 ¹íÒÁÍàµÍÏ俵ç 6V ¨íҹǹ 2 µÑÇÁÒµ‹ÍࢌҡѺàÍÒµ¾µØ ¢Í§Ç§¨Ã¢ÑºÁÍàµÍÏ L14.4 ¨Ò¡¹Ñ¹é ¨‹ÒÂä¿ãˌᡋǧ¨Ã (¡) ¶ŒÒËҡ㪌ºÍÏ´ RBX-877 Rev.2007 ÊÒÁÒö¨‹ÒÂ俪شà´ÕÂÇáŌǷ´Åͧ䴌·¹Ñ ·Õ (¢) ¶ŒÒ㪌ºÍÏ´·´Åͧ NX-877 plus II ËÃ×͵‹Íǧ¨Ã¢Ö¹é àͧ µŒÍ§ËÒáËŋ§¨‹ÒÂ俵ç +6V ÊíÒËÃѺǧ¨Ã¢Ñº ÁÍàµÍϨҋ Âá¡ãˌµÒ‹ §ËÒ¡ ¢Íá¹Ð¹íÒãˌ㪌ẵàµÍÃÕáè ºº»ÃШØ䴌 ËÃ×ÍẺµÐ¡ÑÇè -¡Ã´ ¶ŒÒËҡ㪌ÍÅÑ ¤Ò䬏 ÍÒ¨ ·´Åͧ䴌ã¹àÇÅÒäÁ‹¹Ò¹¹Ñ¡ à¹×Íè §¨Ò¡áºµàµÍÃÕáè ººÍÑŤÒ䬏ÊÒÁÒö¨‹Ò¡ÃÐáÊä¿¿‡Ò䴌äÁ‹ÁÒ¡¹Ñ¡ àÁ×Íè ¹íÒÁÒ ãªŒ¡ºÑ ÁÍàµÍÏ «Ö§è µŒÍ§¡ÒáÃÐáÊä¿¿‡Ò㹡Ò÷íÒ§Ò¹ÁÒ¡¾ÍÊÁ¤Çà (200mA ¢Ö¹é ä») ËÃ×ͶŒÒµŒÍ§¡ÒÃ㪌ÍÐá´» àµÍÏ俵ç¤ÇÃàÅ×͡Ẻ·ÕÁè ¤Õ ÇÒÁÊÒÁÒö㹡Òè‹Ò¡ÃÐáÊä¿¿‡Ò 1000mA ¢Ö¹é ä» L14.5 Ãѹâ»Ãá¡ÃÁ áŌÇÊѧࡵ¡Ò÷íÒ§Ò¹¢Í§ÁÍàµÍÏ ÁÍàµÍÏ·§Ñé ÊͧµÑǨÐä´ŒÃºÑ ¡ÒâѺãˌËÁعä»ã¹·ÔÈ·Ò§µÃ§ 2 ÇÔ¹Ò·Õ â´ÂÊѧ¡µä´Œ¨Ò¡ä¿áÊ´§¡Ò÷íÒ§Ò¹ «Ö§è ¨Ð໚¹ÊÕà¢ÕÂÇ·Ñ§é ¤Ù‹ ¨Ò¡¹Ñ¹é ËÁع¡ÅѺ·ÔÈ·Ò§ 2 ÇÔ¹Ò·Õ ä¿áÊ´§¡Ò÷íÒ§Ò¹¨Ðà»ÅÕÂè ¹à»š¹ÊÕá´§ ËÒ¡äÁ‹µÃ§µÒÁ ¹Õéãˌ·íÒ¡ÒáÅѺ¢ÑéÇÁÍàµÍϨ¹¡ÃзÑ觷ÔÈ·Ò§¡ÒÃËÁع¶Ù¡µŒÍ§

INNOVATIVE EXPERIMENT


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 143

º··ÕÕè 10

¡ÒõԴµ‹Í USART âÁ´ÙÅÊ×Íè ÊÒâŒÍÁÙÅ ¾Íϵ͹ءÃÁ¢Í§äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ¿˜§¡ª¹Ñè ¢Í§ mikroC ¤ÍÁä¾àÅÍÏ

USART (Universal Synchronous Asynchronous Receiver Transmitter) ໚¹âÁ´ÙÅÊ×Íè ÊÒà ¢ŒÍÁÙÅ͹ءÃÁà͹¡»ÃÐʧ¤·ÊÕè ÒÁÒö·íҧҹ໚¹µÑÇÃѺËÃ×Íʋ§¢ŒÍÁÙÅã¹áººÍЫԧâ¤Ã¹ÑÊ â´ÂÊÒÁÒö ¡íÒ˹´ÍѵÃÒàÃçÇ㹡ÒÃÃѺʋ§¢ŒÍÁÙÅ (Baudrate) áÅФÇÒÁàÃçÇ㹡ÒÃÃѺʋ§¢ŒÍÁÙÅ䴌 àÁ×Íè ¡íÒ˹´ãˌ·Òí §Ò¹áºº¿ÙÅ´Ùà¾Åç¡«ÍЫԧâ¤Ã¹ÑÊ (full duplex asynchronous) ¨ÐÊÒÁÒöÃѺ ʋ§¢ŒÍÁÙÅ 2 ·Ôȷҧ䴌ã¹àÇÅÒà´ÕÂǡѹ ·Ñ§é ¹Õàé ¾×Íè 㪌µ´Ô µ‹Í¡Ñº¾Íϵ͹ءÃÁ¢Í§¤ÍÁ¾ÔÇàµÍÏËÃ×ÍÍØ»¡Ã³ à·ÍÏÁ¹Ô ÍÅẺÍ×¹è

10.1 ¿˜§¡ª¹Ñè USART à¾×Íè ¡ÒÃÃѺʋ§¢ŒÍÁÙÅ͹ءÃÁẺ¿ÙÅ´Ùà¾Åç¡« ¿˜§¡ª¹Ñè USART ¢Í§ mikroC ¤ÍÁä¾àÅÍÏÊÒÁÒö㪌¡ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ·ÕÁè âÕ Á´ÙÅ USART ÍÂÙÀ‹ ÒÂã¹µÑÇ ÍÒ·Ô PIC16F877(A), PIC16F887 ໚¹µŒ¹ â´Â¿˜§¡ª¹Ñè USART ¡íÒ˹´ÃٻẺ ¡ÒÃÊ×Íè ÊÒÃ໚¹áºº 8N1 ¤×Í ¨íҹǹºÔµ¢ŒÍÁÙÅ 8 ºÔµ ºÔµËÂØ´ËÃ×ͺԵÊÔ¹é ÊØ´ 1 ºÔµ áÅÐäÁ‹Á¡Õ ÒõÃǨ Êͺ¾ÒÃÔµÕé â´Â¿˜§¡ª¹Ñè USART ÁÕ¤Òí ÊÑ§è ·Õàè ¡ÕÂè Ç¢ŒÍ§´Ñ§¹Õé

10.1.1 Usart_Init(

)

㪌à¾×Íè ¡íÒ˹´¤‹Ò¢Í§ÍѵÃÒàÃçÇ㹡Òö‹Ò·ʹ¢ŒÍÁÙÅËÃ×ͺʹàõ (baud rate) ãˌ¡ºÑ ¡ÒÃÊ×Íè ÊÒà ¢ŒÍÁÙÅ͹ءÃÁ â´Â¡íÒ˹´¤‹ÒºÍ´àõŧä»ä´Œâ´ÂµÃ§ ¤‹ÒºÍ´àõ¨Ð໚¹µÑÇá»Ãª¹Ô´ Long (Áդҋ 0 ¶Ö§ 4,294,967,295 ) ᵋ¡ÒáíÒ˹´¤‹ÒµÒÁ¤ÇÒÁ໚¹¨ÃÔ§äÁ‹ÊÒÁÒö¡íÒ˹´ä´Œ¶§Ö ¤‹ÒÊÙ§ÊØ´¢Í§µÑÇá»Ã ª¹Ô´ Long à¹×Íè §¨Ò¡¢ŒÍ¨íÒ¡Ñ´àÃ×Íè §¤ÇÒÁ¶Õãè ¹¡Ò÷íÒ§Ò¹¢Í§äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ


144 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

ÃÙ»·Õè 10-1 áÊ´§¢ŒÍ¼Ô´¾ÅÒ´àÁ×Íè ¤‹ÒºÍ´àõ·Õ¡è Òí ˹´ äÁ‹ÊÒÁÒöÊÌҧ䴌à¹×Íè §¨Ò¡¤ÇÒÁ¶ÕÊè ­ Ñ ­Ò³ ¹Ò́¡ÒäÁ‹àËÁÒÐÊÁ ´Ñ§¹Ñ¹é ¤‹Ò·Õ¹è ÂÔ Á¹íÒÁÒ㪌§Ò¹¤×Í ¤‹Ò 2400, 9600, 19200, 57600 ໚¹µŒ¹ àÁ×Íè ¡íÒ˹´¤‹ÒºÍ´àõ ·Õµè ÇÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ໚¹à·‹Òã´ ·ÕÍè »Ø ¡Ã³»ÅÒ·ҧ«Ö§è ·íÒ¡ÒÃÊ×Íè ÊÒâŒÍÁÙÅ´ŒÇ¨еŒÍ§¡íÒ˹´ ¤‹ÒºÍ´àõãˌµÃ§¡Ñ¹´ŒÇ ¶ŒÒËÒ¡ÁÕ¡ÒáíÒ˹´¤‹ÒºÍ´àõ·ÕÊè ­ Ñ ­Ò³¹Ò́¡Ò¢Í§äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC äÁ‹ÊÒÁÒö ÃͧÃѺ䴌 ËÅѧ¨Ò¡¤ÍÁä¾Åâ»Ãá¡ÃÁ mikroC ¤ÍÁä¾àÅÍϨÐᨌ§¢ŒÍ¼Ô´¾ÅÒ´ÍÍ¡ÁҴѧÃÙ»·Õè 10-1 ¡ÒÃ㪌§Ò¹¿˜§¡ª¹Ñè Usart_Init( ) ¨ÐµŒÍ§àÃÕ¡㪌§Ò¹¡‹Í¹¿˜§¡ª¹Ñè ·Õàè ¡ÕÂè Ç¢ŒÍ§¡Ñº¡ÒÃ㪌§Ò¹ âÁ´ÙÅ USART µÑÇÍ×¹è æ à¹×Íè §¨Ò¡¿˜§¡ª¹Ñè ¹Õãé ªŒÊÒí ËÃѺ¡íÒ˹´¤‹ÒàÃÔÁè µŒ¹ã¹¡Ò÷íÒ§Ò¹ ÃٻẺ¡ÒÃ㪌§Ò¹ void Usart_Init(const long baud_rate);

µÑÇá»Ã baud_rate

ËÁÒ¶֧ ÍѵÃҺʹ·Õ¡è Òí ˹´¤‹Òãˌ¡ºÑ ¡ÒÃÊ×Íè ÊÒÃÃÐËNjҧµŒ¹·Ò§áÅлÅÒ·ҧ

µÑÇÍ‹ҧ·Õè 10-1 Usart_Init(2400);

໚¹¡ÒáíÒ˹´¤‹ÒºÍ´àõ 2400 ºÔµµ‹ÍÇÔ¹Ò·Õ

10.1.2 Usart_Data_Ready ¿˜§¡ª¹Ñè ¹Õãé ªŒµÃǨÊÍºÇ‹Ò ¢ŒÍÁÙÅ㹺ѿà¿ÍϾÌÍÁÊíÒËÃѺ¡ÒÃ͋ҹ¤‹ÒËÃ×ÍäÁ‹ ¿˜§¡ª¹Ñè ¹Õ¨é Ф׹ ¤‹Ò “1” ¶ŒÒ¢ŒÍÁÙÅ㹺ѿà¿ÍϾÌÍÁÊíÒËÃѺ͋ҹ¤‹Ò䴌 áÅФ׹¤‹Ò “0” ¶ŒÒäÁ‹Á¢Õ ͌ ÁÙÅÍÂÙ㋠¹ºÑ¿à¿ÍÏ â´Â ¡‹Í¹¨Ð㪌§Ò¹¿˜§¡ª¹Ñè ¹Õ¨é еŒÍ§àÃÕ¡¿˜§¡ª¹Ñè Usart_Init() ¢Ö¹é ÁÒ¡‹Í¹ ÃٻẺ¡ÒÃ㪌§Ò¹ unsigned short Usart_Data_Ready(void);


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 145

10.1.3 Usart_Read ¿˜§¡ªÑè¹¹Õé㪌͋ҹ¤‹Ò¢ŒÍÁÙŨҡºÑ¿à¿ÍϢͧâÁ´ÙÅ USART â´Â¨Ð¤×¹¤‹Ò¢ŒÍÁÙÅ¢¹Ò´ 1 亵 ÍÍ¡ÁÒ áµ‹¶ÒŒ ÂѧäÁ‹Á¢Õ ͌ ÁÙÅ ¨Ð¤×¹¤‹Ò “0” ¡ÅѺÍÍ¡ÁÒ ¡‹Í¹¡ÒÃ㪌§Ò¹¤íÒÊÑ§è ¹Õ¨é еŒÍ§¡íÒ˹´¤‹ÒàÃÔÁè µŒ¹ ãˌ¡ºÑ ¾Íϵ͹ءÃÁ â´Â㪌¤Òí Êѧè Usart_Init() ¡‹Í¹ áÅÐà¾×Íè ãˌ¡ÒÃ͋ҹ¤‹Ò¢ŒÍÁÙŶ١µŒÍ§ ¤ÇõÃǨ Êͺ¤‹Ò¢Í§¢ŒÍÁÙÅ¡‹Í¹¡ÒÃ͋ҹ´ŒÇ¿˜§¡ª¹Ñè Usart_Data_Ready ÃٻẺ¡ÒÃ㪌§Ò¹ unsigned short Usart_Read(void);

µÑÇÍ‹ҧ·Õè 10-2 if (Usart_Data_Ready()) { PORTB = Usart_Read(); }

¡Ò÷íÒ§Ò¹¤×Í ¶ŒÒÁբ͌ ÁÙÅà¡çºÍÂÙ㋠¹ºÑ¿à¿ÍÏ ãˌ¹Òí ÁÒáÊ´§·Õè PORTB

10.1.4 Usart_Write ¿˜§¡ª¹Ñè ¹Õãé ªŒÊ§‹ ¤‹ÒÍÍ¡ä»Âѧ¾Íϵ͹ءÃÁ â´Â¢ŒÍÁÙÅ data ·Õãè ªŒÊ§‹ ໚¹¢ŒÍÁÙÅ¢¹Ò´ 8 ºÔµ ¡‹Í¹ 㪌§Ò¹¤íÒÊÑ§è ¹Õ¨é еŒÍ§ÁÕ¡ÒáíÒ˹´¤‹ÒàÃÔÁè µŒ¹ãˌ¡ºÑ ¾Íϵ͹ءÃÁ â´Â㪌 Usart_Init( ) ¡‹Í¹ ÃٻẺ¡ÒÃ㪌§Ò¹ void Usart_Write(unsigned short data);

µÑÇá»Ã data

ËÁÒ¶֧ ¤‹Ò·Õµè ͌ §¡ÒÃʋ§ÍÍ¡ä»Âѧ¾Íϵ͹ءÃÁ

µÑÇÍ‹ҧ·Õè 10-3 Usart_Write(DAT);

// Send data via USART

ʋ§¤‹Ò¢ŒÍÁÙŨҡµÑÇá»Ã DAT ÍÍ¡ä»Âѧ¾Íϵ͹ءÃÁ


146 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

»¯ÔºµÑ ¡Ô Ò÷Õè 15 ¡ÒÃÃѺʋ§¢ŒÍÁÙÅÃÐËNjҧ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏáÅФÍÁ¾ÔÇàµÍÏ ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC16F877(A) ÁÕ¢Ò¾ÍϵÊíÒËÃѺàª×Íè Áµ‹Í¡Ñº¾Íϵ͹ءÃÁ¤×Í¢Ò RC6/TxD áÅÐ¢Ò RC7/RxD àÁ×Íè µŒÍ§àª×Íè Áµ‹Í¡Ñº¤ÍÁ¾ÔÇàµÍÏ«§Öè 㪌Áҵðҹ RS-232 ¨ÐµŒÍ§ÁÕäÍ«Õ··Õè Òí ˹ŒÒ·Õ»è ÃѺà»ÅÕÂè ¹ ÃдѺáç´Ñ¹ã¹·Õ¹è ¢Õé Íá¹Ð¹íÒàºÍÏ MAX232 ËÃ×Í ICL232 â´ÂÁÕǧ¨Ãàª×Íè Áµ‹ÍáÊ´§ã¹ÃÙ»·Õè L15-1

¡ÒÃàª×èÍÁµ‹Í·Ò§ÎÒÏ´áÇÏ 㪌ǧ¨Ãã¹ÃÙ»·Õè L15-1 㹡Ò÷´Åͧ l Ëҡ㪌ºÍÏ´·´Åͧ NX-877 plus II ãˌµÍ ‹ ǧ¨ÃµÒÁÃÙ»·Õè L15-1 áŌÇàÅ×Í¡äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ໚¹ PIC16F877 l

ÃÙ»·Õè L15-1 ǧ¨Ãàª×Íè Áµ‹ÍäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC16F877 ¡Ñº¤ÍÁ¾ÔÇàµÍϼҋ ¹¾Íϵ͹ءÃÁ


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 147

USART Terminal â»Ãá¡ÃÁÃѺʋ§¢ŒÍÁÙÅ͹ءÃÁº¹¤ÍÁ¾ÔÇàµÍϢͧ mikroC ¤ÍÁä¾àÅÍÏ ÊíÒËÃѺ¤ÍÁ¾ÔÇàµÍÏà¾×èÍÃѺáÅÐʋ§¢ŒÍÁÙÅ ¨ÐµŒÍ§ÁÕâ»Ãá¡ÃÁ·Õè·íÒ˹ŒÒ·ÕèÃѺáÅÐʋ§¢ŒÍÁÙÅ ËÃ×Í·ÕèàÃÕÂ¡Ç‹Ò â»Ãá¡ÃÁà·ÍÏÁÔ¹ÍÅ (USART Terminal) mikroC ¤ÍÁä¾àÅÍÏ䴌¨Ñ´àµÃÕÂÁâ»Ãá¡ÃÁ Terminal àÍÒänjáÅŒÇ ºÃÔàdzᶺà¤Ã×Íè §Á×Í â´Âä»·Õàè Á¹Ù Tools à USART Terminal ËÃ×Í¡´»ØÁ† CTRL+T ËÃ×ͤÅÔ¡·Õäè ͤ͹ ·Ò§´ŒÒ¹¢ÇÒÁ×Í¡ç䴌 ÊíÒËÃѺ˹ŒÒµÒ¢Í§â»Ãá¡ÃÁ USART Terminal áÊ´§´Ñ§ÃÙ» ã¹â»Ãá¡ÃÁ·Õè L15-1 䴌¡Òí ˹´ãˌäÁâ¤Ã¤Í¹â·ÃÅàÅÍÃÃºÑ Ê‹§¢ŒÍÁÙÅ´ŒÇºʹàõ 9600 ºÔµµ‹ÍÇÔ¹Ò·Õ, 8 ºÔµ¢ŒÍÁÙÅ 8 ºÔµ, äÁ‹ãªŒ¾ÒÃÔµÕé áÅÐ 1 ºÔµ»´·ŒÒÂËÃ×ͺԵÊÔ¹é ÊØ´ ´Ñ§¹Ñ¹é ·Õâè »Ãá¡ÃÁ USART Terminal ¨ÐµŒÍ§¡íÒ˹´ ¾ÒÃÒÁÔàµÍÏ·ãÕè ªŒãŒ ¹¡ÒÃÊ×Íè ÊÒâŒÍÁÙÅãˌµÃ§¡Ñ¹´ŒÇÂ

¡íÒ˹´¤‹ÒºÍ´àõ

»‡Í¹¢ŒÍÁÙÅà¾×Íè ʋ§

áÊ´§¢ŒÍÁÙŠ¡àÅÔ¡¡ÒÃàª×èÍÁµ‹Í

àª×Íè Áµ‹Í


148 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

¢Ñ鹵͹¡Ò÷´Åͧ L15.1 ແ´â»Ãá¡ÃÁ mikroC ÊÌҧä¿Åâ»Ãà¨ç¡µ â´ÂàÅ×Í¡äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏáÅФÇÒÁ¶ÕÊè ­ Ñ ­Ò³¹Ò́¡Ò´Ñ§¹Õé l ¡Ã³Õ㪌ºÍÏ´ NX-877 plus II àÅ×Í¡ PIC16F877 ËÃ×Í PIC16F877A ¤ÇÒÁ¶ÕÊ è­ Ñ ­Ò³¹Ò́¡Ò 20MHz L15.2 ÊÌҧâ»Ãà¨ç¡µªÍ×è USARTtest áÅоÔÁ¾â»Ãá¡ÃÁÀÒÉÒ C µÒÁâ»Ãá¡ÃÁ·Õè L15-1 L15.3 ·íÒ¡ÒäÍÁä¾ÅáÅдÒǹâËÅ´â»Ãá¡ÃÁà¾×Íè ·´Êͺ¼ÅÅѾ¸¡Ò÷íÒ§Ò¹ L15.4 ·Õâè »Ãá¡ÃÁ mikroC ¤ÍÁä¾àÅÍÏ ແ´â»Ãá¡ÃÁ USART Terminal â´Âä»·Õàè Á¹Ù Tools à USART Terminal ºÃÔàdzᶺà¤Ã×Íè §Á×Í·Ò§´ŒÒ¹¢ÇÒÁ×Í ¨Ò¡¹Ñ¹é ·íÒ¡ÒáíÒ˹´µíÒá˹‹§ ËÃ×Í¡´»ØÁ† CTRL+T ËÃ×ͤÅÔ¡·Õäè ͤ͹ ¾Íϵ͹ءÃÁ áÅСíÒ˹´¤‹ÒºÍ´àõ෋ҡѺ 9600 ºÔµ ʵçÍ» 1 ºÔµ äÁ‹Á¾Õ ÒÃÔµáÕé ÅШíҹǹºÔµ¢ŒÍÁÙÅ 8 ºÔµ L15.5 ¡´»ØÁ† à¾×Íè ແ´¡ÒÃÊ×Íè ÊÒÃ͹ءÃÁ L15.6 ¤ÅÔ¡à¾×Íè ¡Òà¤Ã×Íè §ËÁÒ¶١·ÕËè ¹ŒÒËÑÇ¢ŒÍ Send as number à¾×Íè àÅ×Í¡¡ÒÃʋ§¢ŒÍÁÙÅ໚¹µÑÇàÅ¢ L15.7 Ãѹâ»Ãá¡ÃÁ·Ò§½˜§› äÁâ¤Ã¤Í¹â·ÃÅàÅÍϴnj ¡ÒÃແ´»´ä¿àÅÕÂé §ãËÁ‹Í¡Õ ¤Ãѧé ËÃ×Í¡´ÊÇÔµª RESET

void main() { Usart_Init(9600); TRISD=0x00; while(1) { if (Usart_Data_Ready()) { Usart_Write('O'); Usart_Write('K'); PORTD = Usart_Read(); } } }

// Initial Serial Commucation [RC6,RC7] // Set PortD==> output // Loop // Check Data From Serial // Send "OK" // Show Data from PC ==> LED

¡Ò÷íÒ§Ò¹¢Í§â»Ãá¡ÃÁ (1) ¡íÒ˹´¤‹ÒàÃÔÁè µŒ¹ãˌ¡ºÑ ¡ÒÃÊ×Íè ÊÒâŒÍÁÙÅ͹ءÃÁ໚¹ 9600 8N1 ´ŒÇ¤íÒÊѧè Usart_Init(9600); (2) ¡íÒ˹´ãˌ¾Íϵ D ໚¹àÍÒµ¾µØ (3) ǹÅÙ»µÃǨÊÍºÇ‹Ò Áբ͌ ÁÙŶ١ʋ§ÁÒËÃ×ÍäÁ‹ ´ŒÇ¤íÒÊѧè if

(Usart_Data_Ready())

(4) ¶ŒÒÁբ͌ ÁÙÅʋ§ÁÒ ãˌʧ‹ ÍÑ¡Éà OK µÍº¡ÅÑºä» (5) ¹íÒ¤‹Ò·ÕÃè ºÑ ä´ŒáÊ´§·Õ¾è Íϵ D

â»Ãá¡ÃÁ·Õè L15-1 ä¿Å USARTtest.c â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺ·´Êͺ¡ÒÃ㪌§Ò¹âÁ´ÙÅ USART ¢Í§äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC16F877(A) â´Â㪌¿§˜ ¡ª¹Ñè USART ¢Í§ mikroC ¤ÍÁä¾àÅÍÏ


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

L15.8 »‡Í¹µÑÇàÅ¢ 128 ·Õªè ͋ §»‡Í¹¢ŒÍÁÙÅ ¨Ò¡¹Ñ¹é ¡´»ØÁ† LED áÅÐ˹ŒÒµ‹Ò§ÃѺ¢ŒÍÁÙÅ͹ءÃÁ¢Í§ USART Terminal

l 149

à¾×Íè ʋ§¢ŒÍÁÙÅÍÍ¡ä» Êѧࡵ¡ÒÃáÊ´§¼Å¢Í§

·Õè LED µíÒá˹‹§ RD7 ¨ÐµÔ´à¾Õ§µíÒá˹‹§à´ÕÂÇ෋ҹѹé à¹×Íè §¨Ò¡¤‹Ò 128 àÁ×Íè á»Å§à»š¹àÅ¢°Ò¹Êͧ ¨ÐÁդҋ ້¹š 10000000 «Ö§è ÁÕà¾Õ§ºÔµ 7 à·‹Ò¹Ñ¹é ·ÕÁè ¤Õ Ò‹ ໚¹ “1” ·ÕËè ¹ŒÒµ‹Ò§ÃѺ¢ŒÍÁÙÅ͹ءÃÁ¨ÐáÊ´§¢ŒÍ¤ÇÒÁ Received: OK ´Ñ§ÃÙ» à¾×Íè ᨌ§Ç‹Òä´ŒÃºÑ ¢ŒÍÁÙŵͺ¡ÅѺ¨Ò¡ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏáŌÇ

L15.9 ·´Åͧʋ§¢ŒÍÁÙŤ‹ÒÍ×¹è æ ઋ¹ 255 ¨Ð·íÒãˌ LED µÔ´·Ø¡´Ç§ ËÃ×Íʋ§¤‹ÒµÑÇàÅ¢°Ò¹ÊԺˡ â´Â¾ÔÁ¾ 0x ¹íÒ˹ŒÒ ¤‹ÒµÑÇàÅ¢°Ò¹ÊԺˡ·Õµè ͌ §¡ÒÃʋ§ ઋ¹ 0xAA ·íÒãˌ LED µÔ´´ÑºÊÅѺ¡Ñ¹ ໚¹µŒ¹

mikroC


150 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

10.2 ¿˜§¡ª¹Ñè Soft_Usart ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ·Õäè Á‹ÁâÕ Á´ÙÅ USART ÍÂÙÀ‹ ÒÂã¹µÑÇËÃ×ÍâÁ´ÙÅ USART ·ÕÁè Õ ÍÂÙä‹ ´ŒÃºÑ ¡ÒèѴÊÃÃä»áÅŒÇ ·íÒãˌ¨Òí ໚¹µŒÍ§ãªŒ¢Ò¾Íϵ»¡µÔ·ÁÕè ÍÕ ÂÙÁ‹ ҨѴ¡ÒÃ໚¹¢Ò¾ÍϵÊíÒËÃѺÊ×Íè ÊÒâŒÍÁÙÅ͹ءÃÁ â´Â㪌¡ÒÃà¢Õ¹â»Ãá¡ÃÁࢌҪ‹Ç áÅ¡ªÑè¹·Õè·íÒ˹ŒÒ·Õè¤×Í Soft_Usart ÁÕ ¿˜§¡ªÑè¹·Õèà¡ÕèÂÇ¢ŒÍ§¡Ñº·íÒ§Ò¹´Ñ§¹Õé

10.2.1 Soft_Usart_Init ¤íÒÊÑ§è ¹Õãé ªŒà¾×Íè ¡íÒ˹´¤‹ÒàÃÔÁè µŒ¹ãˌ¡ºÑ ¡ÒÃÊ×Íè ÊÒâŒÍÁÙÅ â´ÂµŒÍ§àÃÕ¡㪌¤Òí Êѧè Soft_Uart_Init ¹Õé ¡‹Í¹ãªŒ§Ò¹¤íÒÊѧè ËÃ×Í¿˜§¡ª¹Ñè Í×¹è æ ·Õàè ¡ÕÂè Ç¢ŒÍ§¡Ñº¡ÒÃÊ×Íè ÊÒâŒÍÁÙż‹Ò¹¢Ò¾Íϵã´æ ¹Õé ÃٻẺ¡ÒÃ㪌§Ò¹ void Soft_Uart_Init(unsigned short *port, unsigned short rx, unsigned short tx, unsigned short baud_rate, unsigned short inverted);

µÑÇá»Ã

port ËÁÒ¶֧ µíÒá˹‹§¾Íϵ¢Í§äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ·ãÕè ªŒã¹¡ÒÃÊ×Í è ÊÒà â´Â·Ñ§é ¢Ò Tx áÅÐ¢Ò Rx ¨ÐµŒÍ§

ÍÂÙº‹ ¹¾Íϵà´ÕÂǡѹ rx

ËÁÒ¶֧ µÑÇàÅ¢·ÕÃè кصÒí á˹‹§¢Ò¢Í§¢ÒÃѺ¢ŒÍÁÙÅ¡íÒ˹´¤‹Ò䴌µ§Ñé ᵋ 0 ¶Ö§ 7

tx

ËÁÒ¶֧ µÑÇàÅ¢·ÕÃè кصÒí á˹‹§¢Ò¢Í§¢Òʋ§¢ŒÍÁÙÅ ¡íÒ˹´¤‹Ò䴌µ§Ñé ᵋ 0 ¶Ö§ 7 â´Â¢Ò rx áÅÐ tx ˌÒÁ¡íÒ˹´à»š¹¢Òà´ÕÂǡѹ

baud_rate ËÁÒ¶֧ ¤‹ÒÍѵÃÒàÃçÇã¹ÃѺáÅÐʋ§¢ŒÍÁÙÅ «Ö§è ¡ÒáíÒ˹´¤‹ÒÊÙ§ÊØ´áÅеèÒí ÊØ´¡ç¢¹ Öé ÍÂÙ¡‹ ºÑ ¤ÇÒÁ

àÃçǢͧÊÑ­­Ò³¹Ò́¡Ò·Õãè ªŒã¹¢³Ð¹Ñ¹é ´ŒÇ ¤‹Ò·ÕÁè ¡Ñ ¨Ð㪌§Ò¹¤×Í ¤‹Ò 2400 , 9600 , 19200 , 57600 ໚¹µŒ¹

ËÁÒ¶֧ à¾×Íè ¡ÅѺʶҹÐÅͨԡ·Õ¨è Ðʋ§ÍÍ¡ä»Âѧ¾Íϵ͹ءÃÁ ¶ŒÒ¡íÒ˹´à»š¹ 0 ¨ÐäÁ‹Á¡Õ Ò Ã¡ÅѺʶҹРᵋ¶ÒŒ ໚¹¤‹ÒÍ×¹è æ ·Õäè Á‹ãª‹ 0 ¨Ð໚¹¡ÒáÅѺʶҹРinverted

µÑÇÍ‹ҧ·Õè 10-4 Soft_Uart_Init(PORTC, 7, 6, 9600,0);

໚¹¡ÒáíÒ˹´¤‹ÒàÃÔÁè µŒ¹ãˌ¡ºÑ Ê×Íè ÊÒâŒÍÁÙÅ͹ءÃÁ â´Â㪌¢Ò RC7 ໚¹¢ÒÃѺ¢ŒÍÁÙÅ (Rx) áÅÐ㪌¢Ò RC6 ໚¹¢Òʋ§¢ŒÍÁÙÅ (Tx) â´ÂºÍ´àõ·Õãè ªŒã¹¡ÒõԴµ‹Í¤×Í 9600 ºÔµµ‹ÍÇÔ¹Ò·Õ äÁ‹Á¡Õ ÒáÅѺʶҹТŒÍÁÙÅ


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 151

10.2.2 Soft_Uart_Read ¿˜§¡ª¹Ñè ¹Õãé ªŒà¾×Íè ÃѺ¢ŒÍÁÙż‹Ò¹¡ÒÃÊ×Íè ÊÒÃ͹ءÃÁ µÑÇá»Ã error ¨ÐÁդҋ ໚¹ 0 àÁ×Íè ¡ÒÃÃѺ¢ŒÍÁÙÅ ÊÁºÙó â´ÂÊÒÁÒöµÃǨÊͺ䴌Çҋ ä´ŒÃºÑ ¢ŒÍÁÙÅËÃ×ÍäÁ‹ â´ÂµÃǨÊͺ·Õµè ÇÑ á»Ã error ¡‹Í¹àÃÕ¡ 㪌¿§˜ ¡ª¹Ñè ¹Õ¨é еŒÍ§àÃÕ¡㪌¿§˜ ¡ª¹Ñè Soft_Uart_Init ¡‹Í¹ ÃٻẺ¡ÒÃ㪌§Ò¹ unsigned short Soft_Uart_Read(unsigned short *error);

10.2.3 Soft_Uart_Write ¿˜§¡ª¹Ñè ¹Õ¨é Ðʋ§¢ŒÍÁÙÅã¹µÑÇá»Ã data ä»Âѧ¾Íϵ͹ءÃÁ ã¹µíÒá˹‹§·Õ¶è ¡Ù ¡íÒ˹´¨Ò¡¢Ò Tx ¡‹Í¹àÃÕ¡㪌¿§˜ ¡ª¹Ñè ¹Õ¨é еŒÍ§àÃÕ¡㪌¿§˜ ¡ª¹Ñè Soft_Uart_Init ¡‹Í¹ ÃٻẺ¡ÒÃ㪌§Ò¹ void Soft_Uart_Write(char data);


152 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

»¯ÔºµÑ ¡Ô Ò÷Õè 16 Ê×Íè ÊÒâŒÍÁÙÅ͹ءÃÁâ´Â㪌 ¡Ãкǹ¡Ò÷ҧ«Í¿µáÇÏ 㹡Ò÷´Åͧ¹Õ¹é Òí àʹ͡ÒÃÊ×Íè ÊÒâŒÍÁÙÅ͹ءÃÁ¡Ñº¤ÍÁ¾ÔÇàµÍÏâ´ÂäÁ‹ãªŒâÁ´ÙÅ USART ·ÕÁè ãÕ ¹µÑÇäÁâ¤Ã ¤Í¹â·ÃÅàÅÍÏ PIC ᵋ¨Ð㪌¡ÒÃà¢Õ¹â»Ãá¡ÃÁà¾×èÍ¡íÒ˹´¡ÒÃÃѺʋ§¢ŒÍÁÙÅá·¹ËÃ×ÍàÃÕÂ¡Ç‹Ò Software UART ·Ñ§é ¹Õàé ¾×Íè ໚¹á¹Ç·Ò§ã¹¡ÒÃ㪌§Ò¹¢Ò¾Íϵã´æ ¢Í§äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC 㹡ÒÃÊ×Íè ÊÒâŒÍÁÙÅ͹ءÃÁã¹¡Ã³Õ ·Õâè Á´ÙÅ USART ¶Ù¡¨Ñ´ÊÃÃä»ãªŒ§Ò¹Í×¹è áÅÐÂѧ¤§ÁÕ¤ÇÒÁµŒÍ§¡ÒÃÊ×Íè ÊÒâŒÍÁÙÅ͹ءÃÁ¡ÑºÍØ»¡Ã³Í¹×è ËÃ×Í໚¹á¹Ç ·Ò§ã¹¡ÒÃà¾ÔÁè ¨íҹǹ¢Ò¾ÍϵÊ×Íè ÊÒâŒÍÁÙÅ͹ءÃÁ¹Ñ¹è àͧ char *err ; char DAT ; void main() { Soft_Uart_Init(PORTC,7,6,9600,0); TRISD=0x00; while(1) { DAT = Soft_Uart_Read(&err); if (err==0) { Soft_Uart_Write('O'); Soft_Uart_Write('K'); PORTD=DAT; } } }

// For serial Data Buffer // Save Data // Initial Software serial port // PORTD ==> Output // Read Serial data // Check Data tranfer complete ? // send "OK" To PC // Show Data ==> LED

¡Ò÷íÒ§Ò¹¢Í§â»Ãá¡ÃÁ (1) ¡íÒ˹´¤‹ÒàÃÔÁè µŒ¹ãˌ¡ºÑ ¡ÒÃÊ×Íè ÊÒâŒÍÁÙÅ͹ءÃÁ໚¹ 9600 8N1 ´ŒÇ¤íÒÊѧè Soft_Uart_Init(PORTC,7,6,9600,0);

(2) ¡íÒ˹´ãˌ¾Íϵ D ໚¹àÍÒµ¾µØ (3) ǹÅÙ»µÃǨÊÍºÇ‹Ò Áբ͌ ÁÙŶ١ʋ§ÁÒËÃ×ÍäÁ‹ ´ŒÇ¤íÒÊѧè DAT

= Soft_Uart_Read(&err);

(4) ¶ŒÒÁբ͌ ÁÙÅʋ§ÁÒ ãˌʧ‹ ÍÑ¡Éà OK µÍº¡ÅÑºä» (5) ¹íÒ¤‹Ò·ÕÃè ºÑ ä´ŒáÊ´§·Õ¾è Íϵ D

â»Ãá¡ÃÁ·Õè L16-1 ä¿Å SoftUART.c â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺ·´Êͺ¡ÒÃ㪌§Ò¹âÁ´ÙÅ USART ¢Í§äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC16F877(A) â´Â㪌¿§˜ ¡ª¹Ñè Soft_Uart ¢Í§ mikroC ¤ÍÁä¾àÅÍÏ


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 153

¡ÒÃàª×Íè Áµ‹Í·Ò§ÎÒÏ´áÇÏ Âѧ¤§ãªŒÇ§¨Ãã¹ÃÙ»·Õè L15-1 㹡Ò÷´Åͧ l Ëҡ㪌ºÍÏ´·´Åͧ NX-877 plus II ãˌµÍ ‹ ǧ¨ÃµÒÁÃÙ»·Õè L15-1 áŌÇàÅ×Í¡äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ໚¹ PIC16F877 l

¢Ñ鹵͹¡Ò÷´Åͧ L16.1 ແ´â»Ãá¡ÃÁ mikroC ÊÌҧä¿Åâ»Ãà¨ç¡µ â´ÂàÅ×Í¡äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏáÅФÇÒÁ¶ÕÊè ­ Ñ ­Ò³¹Ò́¡Ò´Ñ§¹Õé l ¡Ã³Õ㪌ºÍÏ´ NX-877 plus II àÅ×Í¡ PIC16F877 ËÃ×Í PIC16F877A ¤ÇÒÁ¶ÕÊ è­ Ñ ­Ò³¹Ò́¡Ò 20MHz L16.2 ÊÌҧâ»Ãà¨ç¡µªÍ×è SoftUART áÅоÔÁ¾â»Ãá¡ÃÁÀÒÉÒ C µÒÁâ»Ãá¡ÃÁ·Õè L16-1 L16.3 ·íÒ¡ÒäÍÁä¾ÅáÅдÒǹâËÅ´â»Ãá¡ÃÁà¾×Íè ·´Êͺ¼ÅÅѾ¸¡Ò÷íÒ§Ò¹ L16.4 ·Õâè »Ãá¡ÃÁ mikroC ¤ÍÁä¾àÅÍÏ ແ´â»Ãá¡ÃÁ USART Terminal â´Âä»·Õàè Á¹Ù Tools à USART Terminal ºÃÔàdzᶺà¤Ã×Íè §Á×Í·Ò§´ŒÒ¹¢ÇÒÁ×Í ¨Ò¡¹Ñ¹é ·íÒ¡ÒáíÒ˹´µíÒá˹‹§ ËÃ×Í¡´»ØÁ† CTRL+T ËÃ×ͤÅÔ¡·Õäè ͤ͹ ¾Íϵ͹ءÃÁ áÅСíÒ˹´¤‹ÒºÍ´àõ෋ҡѺ 9600 ºÔµ ʵçÍ» 1 ºÔµ äÁ‹Á¾Õ ÒÃÔµáÕé ÅШíҹǹºÔµ¢ŒÍÁÙÅ 8 ºÔµ à¾×Íè ແ´¡ÒÃÊ×Íè ÊÒÃ͹ءÃÁ L16.5 ¡´»ØÁ† L16.6 ¤ÅÔ¡à¾×Íè ¡Òà¤Ã×Íè §ËÁÒ¶١·ÕËè ¹ŒÒËÑÇ¢ŒÍ Send as number à¾×Íè àÅ×Í¡¡ÒÃʋ§¢ŒÍÁÙÅ໚¹µÑÇàÅ¢ L16.7 Ãѹâ»Ãá¡ÃÁ·Ò§½˜§› äÁâ¤Ã¤Í¹â·ÃÅàÅÍϴnj ¡ÒÃແ´»´ä¿àÅÕÂé §ãËÁ‹Í¡Õ ¤Ãѧé ËÃ×Í¡´ÊÇÔµª RESET L16.8 »‡Í¹µÑÇàÅ¢ 128 ·Õªè ͋ §»‡Í¹¢ŒÍÁÙÅ ¨Ò¡¹Ñ¹é ¡´»ØÁ† LED áÅÐ˹ŒÒµ‹Ò§ÃѺ¢ŒÍÁÙÅ͹ءÃÁ¢Í§ USART Terminal

à¾×Íè ʋ§¢ŒÍÁÙÅÍÍ¡ä» Êѧࡵ¡ÒÃáÊ´§¼Å¢Í§

·Õè LED µíÒá˹‹§ RD7 ¨ÐµÔ´à¾Õ§µíÒá˹‹§à´ÕÂÇ෋ҹѹé à¹×Íè §¨Ò¡¤‹Ò 128 àÁ×Íè á»Å§à»š¹àÅ¢°Ò¹Êͧ ¨ÐÁդҋ ້¹š 10000000 «Ö§è ÁÕà¾Õ§ºÔµ 7 à·‹Ò¹Ñ¹é ·ÕÁè ¤Õ Ò‹ ໚¹ “1” ·ÕËè ¹ŒÒµ‹Ò§ÃѺ¢ŒÍÁÙÅ͹ءÃÁ¨ÐáÊ´§¢ŒÍ¤ÇÒÁ Received: OK à¾×Íè ᨌ§Ç‹Òä´ŒÃºÑ ¢ŒÍÁÙŵͺ¡ÅѺ¨Ò¡äÁâ¤Ã ¤Í¹â·ÃÅàÅÍÏáŌÇ

mikroC


154 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

10.3 ÅÖ¡ÍÕ¡¹Ô´¡ÑºÃÕ¨ÊÔ àµÍÏ·àÕè ¡ÕÂè Ç¢ŒÍ§¡ÑºâÁ´ÙÅ USART ¹Í¡¨Ò¡¡ÒÃ㪌¿˜§¡ªÑè¹â»Ãá¡ÃÁÀÒÉÒ C ·Õè mikroC ¤ÍÁä¾àÅÍÏàµÃÕÂÁÁÒãˌáÅŒÇ ¼ÙŒ¾Ñ²¹Ò â»Ãá¡ÃÁÊÒÁÒöà¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C à¾×Íè ¡íÒ˹´¡Ò÷íÒ§Ò¹¢Í§âÁ´ÙÅ USART 䴌´ÇŒ µÑÇàͧ ᵋ ¨ÐµŒÍ§·ÃÒº¶Ö§¡Ò÷íÒ§Ò¹áÅСÒáíÒËô¢Í§ÃÕ¨ÊÔ àµÍϤǺ¤ØÁ·Õàè ¡ÕÂè Ç¢ŒÍ§¡ÑºâÁ´ÙÅ USART àÊÕ¡‹Í¹ ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ·ÕÁè âÕ Á´ÙÅ USART ÁÕÃ¨Õ ÊÔ àµÍÏËÅÑ¡·Õãè ªŒ¤Çº¤ØÁ¡Ò÷íÒ§Ò¹ 3 µÑÇ áÅÐÃÕ¨ÊÔ àµÍÏà¡çº¢ŒÍÁÙÅ 2 µÑÇ ¤×Í 1. TXSTA (Transmit status and control register) 㪌¤Çº¤ØÁáÅÐáÊ´§Ê¶Ò¹Ð¡ÒÃʋ§¢ŒÍÁÙÅ 2. RCSTA (Receive status and control register) 㪌¤Çº¤ØÁáÅÐáÊ´§Ê¶Ò¹Ð¡ÒÃÃѺ¢ŒÍÁÙÅ 3. SPBRG (Serial port baud rate generator) 㪌¡Òí ˹´ÍѵÃÒàÃçÇ㹡Òö‹Ò·ʹ¢ŒÍÁÙÅ 4. TXREG (Transmit data register) 㪌à¡çº¢ŒÍÁÙÅ·Õµè ͌ §¡ÒÃʋ§ 5. RCREG (Receive data register) 㪌à¡çº¢ŒÍÁÙÅ·ÕÃè ºÑ à¢ŒÒÁÒ

10.3.1 TXSTA ÃÕ¨ÊÔ àµÍÏáÊ´§Ê¶Ò¹ÐáÅФǺ¤ØÁ¡ÒÃʋ§¢ŒÍÁÙÅ ºÔµ 7

TXSTA

CSRC R/W -0

ºÔµ 6

ºÔµ 5

ºÔµ 4

TX9

TXEN

SYNC

R/W -0

R/W -0

R/W -0

ºÔµ 3 -/SENDB

ºÔµ 2

ºÔµ 1

ºÔµ 0

BRGH

TRMT

TX9D

R/W -0

R -1

R/W -0

ºÔµ 7 - CSRS (Clock source select bit) : ºÔµàÅ×Í¡áËŋ§¡íÒà¹Ô´ÊÑ­­Ò³¹Ò́¡Ò ã¹âËÁ´ÍЫԧâ¤Ã¹ÑÊ : äÁ‹ÁãÕ ªŒ§Ò¹ºÔµ¹Õé ã¹âËÁ´«Ô§â¤Ã¹ÑÊ : “0” - ·íҧҹ໚¹ÍØ»¡Ã³ÊàÅ¿ àÅ×Í¡áËŋ§¡íÒà¹Ô´ÊÑ­­Ò³¹Ò́¡ÒÀÒ¹͡ “1” - ·íҧҹ໚¹ÍØ»¡Ã³ÁÒÊàµÍÏ 㪌áËŋ§¡íÒà¹Ô´ÊÑ­­Ò³¹Ò́¡Ò¨Ò¡Ê‹Ç¹¡íÒà¹Ô´ÍѵÃҺʹËÃ×Í ÍѵÃÒàÃçÇ㹡Òö‹Ò·ʹ¢ŒÍÁÙÅÀÒÂã¹äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ :BRG ºÔµ 6 - TX9 (9-bit transmit enable bit) : ºÔµàÅ×Í¡¡ÒÃʋ§¢ŒÍÁÙÅẺ 9 ºÔµ “0” - àÅ×Í¡¡ÒÃʋ§¢ŒÍÁÙÅẺ 8 ºÔµ “1” - àÅ×Í¡¡ÒÃʋ§¢ŒÍÁÙÅẺ 9 ºÔµ ºÔµ 5 - TXEN (Transmit enable bit) : ºÔµàÅ×Í¡¡Ò÷íÒ§Ò¹¢Í§µÑÇʋ§ “0” - ´ÔÊàÍàºÔŵÑÇʋ§¢ŒÍÁÙÅ “1” - àÍç¹àÍàºÔŵÑÇʋ§¢ŒÍÁÙÅ


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 155

ºÔµ 4 - SYNC (USART mode select bit) : ºÔµàÅ×Í¡âËÁ´¡Ò÷íÒ§Ò¹¢Í§âÁ´ÙÅ USART “0” - àÅ×Í¡âËÁ´ÍЫԧâ¤Ã¹ÑÊ “1” - àÅ×Í¡âËÁ´«Ô§â¤Ã¹ÑÊ ºÔµ 3 - äÁ‹Á¡Õ ÒÃ㪌§Ò¹ ͋ҹ¤‹Ò໚¹ “0” (ÊíÒËÃѺ PIC16F87x) ºÔµ 3 - SENDB: Send Break Character bit : ʋ§ºÔµ Break (ÊíÒËÃѺ PIC16F88x) ã¹âËÁ´ÍЫԧâ¤Ã¹ÑÊ : “1” - ʋ§ break ÊíÒËÃѺ¡ÒÃʋ§¢ŒÍÁÙŤÃÑ§é µ‹Íä» (ºÔµ¹Õ¨é ж١à¤ÅÕÂÏ·Ò§ÎÒÏ´áÇÏ) “0” - ¡ÒÃʋ§ºÔµ break à¾×Íè «Ô§¡¢ÍŒ ÁÙÅÊÁºÙó ã¹âËÁ´«Ô§â¤Ã¹ÑÊ : äÁ‹ãªŒ§Ò¹ºÔµ¹Õé ºÔµ 2 - BRGH (High baud rate select bit) : ºÔµàÅ×Í¡âËÁ´¢Í§ÍѵÃÒàÃçÇ㹡Òö‹Ò·ʹ¢ŒÍÁÙÅ ã¹âËÁ´ÍЫԧâ¤Ã¹ÑÊ : “0” - àÅ×Í¡ÍѵÃÒàÃçÇ㹡Òö‹Ò·ʹ¢ŒÍÁÙŵèÒí (ÍѵÃҺʹËÃ×ͺʹàõµèÒí ) “1” - àÅ×Í¡ÍѵÃÒàÃçÇ㹡Òö‹Ò·ʹ¢ŒÍÁÙÅÊÙ§ (ÍѵÃҺʹËÃ×ͺʹàõÊÙ§) ã¹âËÁ´«Ô§â¤Ã¹ÑÊ : äÁ‹Á¡Õ ÒÃ㪌§Ò¹ºÔµ¹Õé ºÔµ 1 - TRMT (Transmit shift register status bit : ºÔµ 1) : ºÔµáÊ´§Ê¶Ò¹ÐÃÕ¨ÊÔ àµÍϺ¿Ñ à¿ÍÏʧ‹ “0” - ÃÕ¨ÊÔ àµÍϺ¿Ñ à¿ÍÏ (TSR) àµçÁ ËÃ×ÍäÁ‹Çҋ § “1” - ÃÕ¨ÊÔ àµÍϺ¿Ñ à¿ÍÏ (TSR) Njҧ ºÔµ 0 - TX9D (9th bit of transmit data) : ºÔµà¡çº¢ŒÍÁÙźԵ·Õè 9 ¢Í§¡ÒÃʋ§¢ŒÍÁÙÅẺ 9 ºÔµ ºÔµ¹Õãé ªŒºÃèآ͌ ÁÙźԵ·Õè 9 㹡óշàÕè Å×Í¡¡ÒÃʋ§¢ŒÍÁÙÅẺ 9 ºÔµ â´Â¡ÒÃ૵ºÔµ TX9 áÅÐÂѧÊÒÁÒö 㪌໚¹ºÔµ¾ÒÃÔµ¢Õé ͧ¡ÒÃÊ×Íè ÊÒâŒÍÁÙÅ͹ءÃÁ䴌

10.3.2 RCSTA ÃÕ¨ÊÔ àµÍÏáÊ´§Ê¶Ò¹ÐáÅФǺ¤ØÁ¡ÒÃÃѺ¢ŒÍÁÙÅ RCSTA

ºÔµ 7

ºÔµ 6 RX9

SREN

CREN

ADDEN

FERR

OERR

RX9D

R/W -0

R/W -0

R/W -0

R/W -0

R/W -0

R -0

R -0

R -x

SPEN

ºÔµ 5

ºÔµ 4

ºÔµ 3

ºÔµ 2

ºÔµ 1

ºÔµ 0

ºÔµ 7 - SPEN (Serial port enable bit) : ºÔµàÅ×Í¡¡Ò÷íÒ§Ò¹¢Í§âÁ´ÙÅ USART “0” - ´ÔÊàÍàºÔÅ “1” - àÍç¹àÍàºÔÅà¾×èÍ㪌§Ò¹âÁ´ÙÅ USART ·íÒãˌ¢Ò RB7/RxD áÅÐ RB6/TxD 㪌§Ò¹¡ÑºâÁ´ÙÅ USART à¾×Íè Ê×Íè ÊÒâŒÍÁÙÅ͹ءÃÁ àÁ×Íè ºÔµ 7 áÅÐ 6 ¢Í§ÃÕ¨ÊÔ àµÍÏ TRISC ¶Ù¡à«µà»š¹ “1”


156 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

ºÔµ 6 - RX9 (9-bit receive enable bit) : ºÔµàÅ×Í¡¡ÒÃÃѺ¢ŒÍÁÙÅẺ 9 ºÔµ “0” - àÅ×Í¡¡ÒÃÃѺ¢ŒÍÁÙÅẺ 8 ºÔµ “1” - àÅ×Í¡¡ÒÃÃѺ¢ŒÍÁÙÅẺ 9 ºÔµ ºÔµ 5 - SREN (Single receive enable bit) : ºÔµàÅ×Í¡¡ÒÃÃѺ¢ŒÍÁÙŤÃѧé à´ÕÂÇ ¨Ðà¤ÅÕÂÏËÅѧ¨Ò¡ÃѺ¢ŒÍÁÙÅÊÁºÙó ã¹âËÁ´ÍЫԧâ¤Ã¹ÑÊ áÅЫԧâ¤Ã¹ÑÊ-ÊàÅ¿ : äÁ‹ãªŒ§Ò¹ºÔµ¹Õé ã¹âËÁ´«Ô§â¤Ã¹ÑÊ-ÁÒÊàµÍÏ : “0” - ´ÔÊàÍàºÔÅ¡ÒÃÃѺ¢ŒÍÁÙŤÃѧé à´ÕÂÇ “1” - àÍç¹àÍàºÔÅ¡ÒÃÃѺ¢ŒÍÁÙŤÃѧé à´ÕÂÇ ºÔµ 4 - CREN (Continous receive enable bit) : ºÔµàÅ×Í¡¡ÒÃÃѺ¢ŒÍÁÙŵ‹Íà¹×Íè § “0” - ´ÔÊàÍàºÔÅ¡ÒÃÃѺ¢ŒÍÁÙŵ‹Íà¹×Íè § “1” - àÍç¹àÍàºÔÅ¡ÒÃÃѺ¢ŒÍÁÙŵ‹Íà¹×Íè § ºÔµ 3 - ADDEN (Address detect enable bit) : ºÔµàÅ×Í¡¡ÒõÃǨ¨ÑºáÍ´à´ÃÊ ãªŒã¹¡Ã³ÕàÅ×Í¡¡ÒÃÃѺ¢ŒÍÁÙÅẺ 9 ºÔµà·‹Ò¹Ñ¹é “0” - ´ÔÊàÍàºÔÅ¡ÒõÃǨ¨ÑºáÍ´à´ÃÊ ¢ŒÍÁÙŨж١ÃѺ·Ñ§é ËÁ´ áÅкԵ·Õè 9 㪌໚¹ºÔµ¾ÒÃÔµäÕé ´Œ “1” - àÍç¹àÍàºÔÅ¡ÒõÃǨ¨ÑºáÍ´à´ÃÊ Ê‹§¼Åãˌà¡Ô´¡ÒÃàÍç¹àÍàºÔÅÍÔ¹àµÍÃÃ»Ñ µ áÅÐÁÕ¡Òö‹Ò·ʹ ¢ŒÍÁÙÅä»ÂѧºÑ¿à¿ÍÏàÁ×Íè ºÔµ RSR ¶Ù¡à«µ ºÔµ 2 - FERR (Framing error bit) : ºÔµá¨Œ§¤ÇÒÁ¼Ô´¾ÅÒ´·Ò§à¿ÃÁ¢ŒÍÁÙŠ૵໚¹ “1” àÁ×Íè ÁÕ¤ÇÒÁ¼Ô´¾ÅÒ´·Ò§à¿ÃÁ¢ŒÍÁÙÅà¡Ô´¢Ö¹é ºÔµ 1 - OERR (Overrun error bit) : ºÔµá¨Œ§¤ÇÒÁ¼Ô´¾ÅÒ´à¹×Íè §¨Ò¡¡Ò깡ѹ¢Í§¢ŒÍÁÙÅ “0” - äÁ‹Á¤Õ ÇÒÁ¼Ô´¾ÅÒ´à¡Ô´¢Ö¹é “1” - à¡Ô´¤ÇÒÁ¼Ô´¾ÅÒ´¢Ö¹é ÊÒÁÒöà¤ÅÕÂϺµÔ ¹Õäé ´Œ´ÇŒ ¡ÒÃà¤ÅÕÂϺµÔ CREN ºÔµ 0 - RX9D (9th bit of received data) : ºÔµà¡çº¢ŒÍÁÙźԵ·Õè 9 ¢Í§¡ÒÃÃѺ¢ŒÍÁÙÅẺ 9 ºÔµ ºÔµ¹Õãé ªŒºÃèآ͌ ÁÙźԵ·Õè 9 㹡óշàÕè Å×Í¡¡ÒÃÃѺ¢ŒÍÁÙÅẺ 9 ºÔµ â´Â¡ÒÃ૵ºÔµ RX9 áÅÐÂѧÊÒÁÒö 㪌໚¹ºÔµ¾ÒÃÔµ¢Õé ͧ¡ÒÃÊ×Íè ÊÒâŒÍÁÙÅ͹ءÃÁ䴌


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 157

10.3.3 ʋǹ¡íÒà¹Ô´ÍѵÃÒàÃçÇ㹡Òö‹Ò·ʹ¢ŒÍÁÙÅËÃ×ͺʹàõà¨à¹ÍàÃàµÍÏ ã¹âÁ´ÙÅ USART 㪌ÊNj ¹¡íÒà¹Ô´ÍѵÃÒàÃçÇ㹡Òö‹Ò·ʹ¢ŒÍÁÙÅËÃ×ͺʹàõà¨à¹ÍààµÍÏà¾Õ§ ªØ´à´ÕÂÇ ÊÒÁÒöÃͧÃѺ·Ñ§é ¡Ò÷íÒ§Ò¹ã¹âËÁ´ÍЫԧâ¤Ã¹ÑÊáÅЫԧâ¤Ã¹ÑÊ â´ÂÁÕ¤ÇÒÁÅÐàÍÕ´ 8 ºÔµ ÃÕ¨ÔÊàµÍϤǺ¤ØÁ¡Ò÷íÒ§Ò¹¤×Í SPBRG â´Â¨Ð¤Çº¤ØÁ¤ÒºàÇÅҢͧä·àÁÍÏÍÔÊÃТ¹Ò´ 8 ºÔµà¾×èÍ ¡íÒà¹Ô´ÍѵÃÒàÃçÇ㹡Òö‹Ò·ʹ¢ŒÍÁÙÅ àÁ×Íè ·íÒ§Ò¹ã¹âËÁ´ÍЫԧâ¤Ã¹ÑʵŒÍ§·íҧҹËÇÁ¡Ñº¢ŒÍÁÙÅ·Õºè µÔ BRGH (ºÔµ 2 ¢Í§ÃÕ¨ÊÔ àµÍÏ TXSTA) à¾×Íè ¡íÒ˹´Â‹Ò¹¢Í§ÍѵÃÒàÃçÇ໚¹áººµèÒí áÅÐÊÙ§ ´Ñ§áÊ´§¤ÇÒÁ ÊÑÁ¾Ñ¹¸ã¹µÒÃÒ§·Õè 3.4.1 «Ö§è ã¹µÒÃҧ䴌áÊ´§ÊÙµÃÊíÒËÃѺ¤íҹdzËÒ¤‹ÒÍѵÃÒàÃçÇ㹡Òö‹Ò·ʹ¢ŒÍÁÙÅ ËÃ×ͺʹàõã¹áµ‹ÅÐà§×è͹ä¢änj´ŒÇ ÊíÒËÃѺ¤‹Ò¢Í§¢ŒÍÁÙÅã¹ÃÕ¨ÊÔ àµÍÏ SPBRG «Ö§è 㪌㹡ÒáíÒ˹´ÍѵÃÒàÃçÇ㹡Òö‹Ò·ʹ¢ŒÍÁÙÅ ÊÒÁÒö¤íҹdz䴌¨Ò¡Êٵ÷ÕÃè кØã¹µÒÃÒ§·Õè 10-1 ᵋ¨ÐÁդҋ ¼Ô´¾ÅÒ´à¡Ô´¢Ö¹é ´Ñ§µÑÇÍ‹ҧµ‹Í仹Õé ¡íÒ˹´ãˌ¤ÇÒÁ¶ÕèÊÑ­­Ò³¹Ò́¡ÒËÅÑ¡ (ËÃ×ͤÇÒÁ¶Õè¢Í§¤ÃÔʵÍÅ) ෋ҡѺ 16MHz µŒÍ§¡Òà ºÍ´àõ෋ҡѺ 9,600 ºÔµµ‹ÍÇÔ¹Ò·Õ àÅ×͡‹ҹºÍ´àõµèÒí ·íҧҹ໚¹áººÍЫԧâ¤Ã¹ÑÊ ÊÒÁÒöÊÃØ» ¤ÇÒÁÊÑÁ¾Ñ¹¸·Ò§¤³ÔµÈÒʵÏ䴌´Ñ§¹Õé ¤‹ÒºÍ´àõ = 9,600

fOSC 64 ( X 1)

16 x106 64( X 1)

X 25.042

â´Â·Õ¤è ҋ X ¤×ͤ‹Ò¢Í§ÃÕ¨ÊÔ àµÍÏ SPBRG ᵋà¹×Íè §¨Ò¡¢ŒÍÁÙÅã¹ÃÕ¨ÊÔ àµÍÏ SPBRG µŒÍ§ ໚¹àÅ¢¨íҹǹàµçÁ ¤‹Ò¢Í§ SPBRG ¨Ö§à»š¹ 25 ¹íÒ¤‹Ò 25 á·¹¡ÅѺࢌÒä»ã¹ÊÙµÃËÒ¤‹ÒºÍ´àõ ¨Ð䴌 SYNC

ºÔµ BRGH = 0 (ÍѵÃÒàÃçǵíèÒ)

0

ºÍ´àõ (ÍЫԧâ¤Ã¹ÑÊ) =

1

ºÍ´àõ («Ô§â¤Ã¹ÑÊ) =

fOSC 64 X 1

ºÔµ BRGH = 1 (ÍѵÃÒàÃçÇÊÙ§) ºÍ´àõ =

fOSC 16 X 1

fOSC 4X 1

µÒÃÒ§·Õè 10-1 áÊ´§ÊٵáÒäíҹdzËÒ¤‹ÒÍѵÃÒàÃçÇ㹡Òö‹Ò·ʹ¢ŒÍÁÙÅËÃ×ͺʹàõ â´Â¤‹Ò¢Í§ X ¤×ͤ‹Ò¢Í§ÃÕ¨ÊÔ àµÍÏ SPBRG


158 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

ºÍ´àõ =

16 x106 64(25 1)

= 9,615 ºÔµµ‹ÍÇÔ¹Ò·Õ

¹Ñ¹è ¤×Íà¡Ô´¤‹Ò¼Ô´¾ÅÒ´ 15 ºÔµµ‹ÍÇÔ¹Ò·Õ ÊÒÁÒö¤íҹdz໚¹à»ÍÏà«ç¹µ¤ÇÒÁ¼Ô´¾ÅҴ䴌¨Ò¡ % ¤ÇÒÁ¼Ô´¾ÅÒ´ = (ºÍ´àõ·Õ¤è Òí ¹Ç³-ºÍ´àõ·Õµè ͌ §¡ÒÃ)/ ºÍ´àõ·Õµè ͌ §¡Òà x100% = 0.15625% Í‹ҧäáçµÒÁ 㹺ҧ¡Ã³ÕÊÒÁÒöŴ¤ÇÒÁ¼Ô´¾ÅҴ䴌â´Â¡ÒáíÒ˹´ãˌÊÌҧºÍ´àõµèÒí ´ŒÇ ¤ÇÒÁàÃçÇÊ٧᷹ â´Â¡íÒ˹´ºÔµ BRGH ໚¹ “1” f

= 20MHz

f

= 10MHz

OSC OSC ºÍ´àõ (kbps) ºÍ´àõ ¼Ô´¾ÅÒ´ ¤‹Ò¢Í§ ºÍ´àõ ¼Ô´¾ÅÒ´ ¤‹Ò¢Í§ ·Õàè ¡Ô´¨ÃÔ§ (%) SPBRG ·Õàè ¡Ô´¨ÃÔ§ (%) SPBRG

0.3 1.2 2.4 9.6 19.2 76.8 96 300 500 ÊÙ§ÊØ´ µíÒè ÊØ´

N/A 1.221 2.404 9.469 19.53 78.13 104.2 312.5 N/A 312.5 1.221

+1.73 +0.16 -1.36 +1.73 +1.73 +8.51 +4.17 -

255 129 32 15 3 2 0 0 255

N/A 1.202 2.404 9.766 19.53 78.13 N/A N/A N/A 156.3 0.6104

+0.16 +0.16 +1.73 +1.73 +1.73 -

129 64 15 7 1 0 255

fOSC = 4MHz

ºÍ´àõ ¼Ô´¾ÅÒ´ ¤‹Ò¢Í§ ·Õàè ¡Ô´¨ÃÔ§ (%) SPBRG

0.3005 1.202 2.404 10.42 20.83 N/A N/A N/A N/A 62.500 0.244

-0.17 +1.67 +1.67 +8.54 +8.50 -

207 51 25 5 2 0 255

fOSC = 3.579545MHz

fOSC = 32.768kHz

ºÍ´àõ ¼Ô´¾ÅÒ´ ¤‹Ò¢Í§ ºÍ´àõ ¼Ô´¾ÅÒ´ ¤‹Ò¢Í§ ·Õàè ¡Ô´¨ÃÔ§ (%) SPBRG ·Õàè ¡Ô´¨ÃÔ§ (%) SPBRG

0.301 1.190 2.432 9.322 18.64 N/A N/A N/A N/A 55.93 0.2185

+0.23 -0.83 +1.32 -2.90 -2.90 -

185 46 22 5 2 0 255

0.256 N/A N/A N/A N/A N/A N/A N/A N/A 0.512 0.0020

-14.67 -

1 0 255

µÒÃÒ§·Õè 10-2 áÊ´§ÃÒÂÅÐàÍÕ´¤‹Ò¢Í§ÃÕ¨ÔÊàµÍ�� SPBRG, ¤‹ÒºÍ´àõ¨ÃÔ§ áÅÐà»ÍÏà«ç¹µ¤ÇÒÁ ¼Ô´¾ÅÒ´àÁ×Íè ¡íÒ˹´ãˌâÁ´ÙÅ USART ·íÒ§Ò¹ã¹âËÁ´ÍЫԧâ¤Ã¹ÑÊÍѵÃÒàÃçǵèÒí ( BRGH = “0”) f

= 20MHz

f

= 10MHz

f

= 4MHz

f

= 3.579545MHz

OSC OSC OSC OSC ºÍ´àõ ºÍ´àõ ¼Ô ´ ¾ÅÒ´ ¤‹ Ò ¢Í§ ºÍ´àõ ¼Ô ´ ¾ÅÒ´ ¤‹ Ò ¢Í§ ºÍ´àõ ¼Ô ´ ¾ÅÒ´ ¤‹ Ò ¢Í§ ºÍ´àõ ¼Ô´¾ÅÒ´ ¤‹Ò¢Í§ (kbps) ·Õèà¡Ô´¨ÃÔ§ (%) SPBRG ·Õèà¡Ô´¨ÃÔ§ (%) SPBRG ·Õèà¡Ô´¨ÃÔ§ (%) SPBRG ·Õèà¡Ô´¨ÃÔ§ (%) SPBRG

9.6 9.615 19.2 19.230 38.4 37.878 57.6 56.818 115.2 113.636 250 250 625 625 1250 1250

+0.16 +0.16 -1.36 -1.36 -1.36 0 0 0

129 64 32 21 10 4 1 0

9.615 18.939 39.062 56.818 125 312.50 625 N/A

+0.16 -1.36 +1.7 -1.36 +8.51 +28.6 0 -

64 32 15 10 4 1 0 -

9.615 19.230 41.667 62.5 125.0 250 N/A N/A

+0.16 +0.16 +8.51 +8.51 +8.51 0 -

25 12 5 3 1 0 -

9.727 +1.32 18.643 -2.90 37.286 -2.90 55.930 -2.90 111.860 -2.90 223.721 -10.51 N/A N/A -

22 11 5 3 1 0 -

µÒÃÒ§·Õè 10-3 áÊ´§ÃÒÂÅÐàÍÕ´¤‹Ò¢Í§ÃÕ¨ÔÊàµÍÏ SPBRG, ¤‹ÒºÍ´àõ¨ÃÔ§ áÅÐà»ÍÏà«ç¹µ¤ÇÒÁ ¼Ô´¾ÅÒ´àÁ×Íè ¡íÒ˹´ãˌâÁ´ÙÅ USART ·íÒ§Ò¹ã¹âËÁ´ÍЫԧâ¤Ã¹ÑÊÍѵÃÒàÃçÇÊÙ§ ( BRGH = “1”)


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 159

·Ø¡¤ÃÑ§é ·Õ¡è Òí ˹´¤‹ÒºÍ´àõãËÁ‹â´Âà¢Õ¹¢ŒÍÁÙÅŧã¹ÃÕ¨ÊÔ àµÍÏ SPBRG ¨Ð·íÒãˌ¤Ò‹ ¢Í§ä·àÁÍÏ ÍÔÊÃÐÀÒÂã¹Ê‹Ç¹¡íÒà¹Ô´ºÍ´àõ¶Ù¡ÃÕ૵ à¤ÅÕÂϤҋ ໚¹Èٹ ´Ñ§¹Ñ¹é àÁ×Íè µŒÍ§¡ÒáíÒ˹´ÍѵÃÒàÃçÇ㹡Òö‹Ò ·Í´¢ŒÍÁÙÅËÃ×ͺʹàõãËÁ‹ ¤ÇÃËÂØ´¡Ò÷íÒ§Ò¹¢Í§âÁ´ÙÅ USART ¹Õ¡é ͋ ¹ àÁ×Íè ¡íÒ˹´¤‹ÒãËÁ‹àÃÕºÌÍ áŌǨ֧àÃÔÁè µŒ¹·íÒ§Ò¹ãËÁ‹ à¾×Íè ¤ÇÒÁÊдǡ㹡ÒÃ㪌§Ò¹¨Ö§ä´ŒÊÃØ»¤‹Ò¢Í§ÃÕ¨ÊÔ àµÍÏ SPBRG ·Õºè Í´àõ µ‹Ò§æ ÃÇÁ·Ñ§é ¤‹Ò¤ÇÒÁ¼Ô´¾ÅÒ´ àÁ×Íè ·íÒ§Ò¹·Ñ§é ã¹âËÁ´«Ô§â¤Ã¹ÑÊáÅÐÍЫԧâ¤Ã¹ÑÊänj㹵ÒÃÒ§·Õè 10-2 áÅÐ 10-3 ¼Ù㌠ªŒ§Ò¹ÊÒÁÒö¹íÒ¤‹Òã¹µÒÃÒ§ä»ãªŒä´ŒàÅÂâ´ÂäÁ‹µÍŒ §àÊÕÂàÇÅÒ¤íҹdzãËÁ‹

10.4 ¡Ò÷íÒ§Ò¹¢Í§âÁ´ÙÅ USART ã¹âËÁ´ÍЫԧâ¤Ã¹ÑÊ 10.4.1 ¡ÒÃʋ§¢ŒÍÁÙÅã¹âËÁ´ÍЫԧâ¤Ã¹ÑÊ

ã¹ÃÙ»·Õè 10-2 áÊ´§ä´ÍÐá¡ÃÁ¡ÒÃʋ§¢ŒÍÁÙÅã¹âËÁ´ÍЫԧâ¤Ã¹ÑÊ ËÑÇã¨ÊíҤѭ¢Í§¡Ò÷íÒ§Ò¹ÍÂً ·Õªè ¿Ô µÃ¨Õ ÊÔ àµÍϵÇÑ Ê‹§ (transmit shift register : TSR) â´Â㹪ԿµÃ¨Õ ÊÔ àµÍϺÃèآ͌ ÁÙÅ·Õäè ´ŒÁÒ¨Ò¡ÃÕ¨ÊÔ àµÍÏ à¡çº¢ŒÍÁÙÅÊíÒËÃѺ¡ÒÃʋ§ËÃ×Í TXREG ¢ŒÍÁÙÅ·Õµè ͌ §¡ÒÃʋ§¨Ð¶Ù¡à¢Õ¹ÁÒÂѧ TXREG ¨Ò¡¹Ñ¹é ¨Ð¶‹Ò·ʹ µ‹Íä»ÂѧªÔ¿µÃ¨Õ ÊÔ àµÍÏ TSR àÁ×Íè ÁÕ¡ÒÃʋ§ºÔµ»´·ŒÒ¢ͧ¢ŒÍÁÙÅ㹪ش¡‹Í¹Ë¹ŒÒÍÍ¡ä»áÅŒÇ ËÅѧ¨Ò¡·ÕÁè ¡Õ ÒÃʋ§ºÔµ»´·ŒÒÂÍÍ¡ä» TSR ¨Ðä´ŒÃºÑ ¢ŒÍÁÙÅãËÁ‹¨Ò¡ÃÕ¨ÊÔ àµÍÏ TXREG «Ö§è à¡Ô´¢Ö¹é ÀÒÂã¹ä«à¡ÔÅ¡Ò÷íÒ§Ò¹à´ÕÂÇ ÃÕ¨ÊÔ àµÍÏ TXREG ¨ÐNjҧ áÅкԵ TXIF (ºÔµ 4 ¢Í§ÃÕ¨ÊÔ àµÍÏ PIR1) ¨Ð૵ ËÒ¡ÁÕ¡ÒÃàÍç¹àÍàºÔÅÍÔ¹àµÍÃÃ»Ñ µänj ¡ç¨Ðà¡Ô´¡ÒÃÍÔ¹àµÍÃÃ»Ñ µ¢¹Öé ºÔµ TXIF ¨Ðà¤ÅÕÂÏ䴌¡µç ͋ àÁ×Íè ÁÕ¡Òö‹Ò ·Í´¢ŒÍÁÙÅãËÁ‹Å§ã¹ÃÕ¨ÊÔ àµÍÏ TXREG ʋǹʶҹТͧªÔ¿µÃ¨Õ ÊÔ àµÍÏ TSR ¨ÐáÊ´§¼‹Ò¹ºÔµ TRMT (ºÔµ 1 ¢Í§ÃÕ¨ÊÔ àµÍÏ TXSTA) àÁ×Íè TSR Njҧ¨Ð૵ºÔµ¹Õé ´Ñ§¹Ñ¹é ¼Ù㌠ªŒ§Ò¹ÊÒÁÒöà¢Õ¹â»Ãá¡ÃÁµÃǨ¨Ñº¡Òà à»ÅÕÂè ¹á»Å§·Õºè µÔ ¹Õàé ¾×Íè àÃÔÁè µŒ¹¡ÒÃʋ§¢ŒÍÁÙÅ¡ç䴌 ºÑÊ¢ŒÍÁÙÅ 8 ºÔµ TXIF

TXREG

TXIE (8)

(0)

TSR

ºÑ¿à¿ÍÏáÅРʋǹ¤Çº¤ØÁ¢Ò¾Íϵ

RC6/TxD/CK

ÍÔ¹àµÍÏÃÑ»µ TXEN

TRMT

ÊÑ­­Ò³¹ÒÌÔ¡Ò ¢Í§ºÍ´àõ SPBRG

ʋǹ¡íÒà¹Ô´ºÍ´àõ

SPEN

TX9 TX9D

ÃÙ»·Õè 10-2 ä´ÍÐá¡ÃÁ¡ÒÃʋ§¢ŒÍÁÙÅã¹âËÁ´ÍЫԧâ¤Ã¹ÑʢͧâÁ´ÙÅ USART ã¹ PIC16F87x


160 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

ÊÑ­­Ò³¹ÒÌÔ¡Ò ¢Í§ºÍ´àõ ÊÑ­­Ò³·Õ¢è Ò RC6/TxD/CK

ºÔµàÃÔÁè µŒ¹

ºÔµ 0

ʶҹТͧ ºÔµá¿Å¡ TXIF

ºÔµ 7 ËÃ×Í 8

ºÔµ»´·ŒÒÂ

¢ŒÍÁÙÅàÇÔÏ´·Õè 1 àÃÔèÁàÅ×è͹¢ŒÍÁÙÅàÇÔÏ´·Õè 1

~

ʶҹТͧ ºÔµá¿Å¡ TRMT

ºÔµ 1

~ ~~~~

ÊÑ­­Ò³à¢Õ¹ ÃÕ¨ÊÔ àµÍÏ TXREG

ÃÙ»·Õè 10-3 ä´ÍÐá¡ÃÁàÇÅÒ¡ÒÃʋ§¢ŒÍÁÙÅâËÁ´ÍЫԧâ¤Ã¹ÑʢͧâÁ´ÙÅ USART ã¹ PIC16F87x ¡ÒÃàÃÔèÁµŒ¹Ê‹§¢ŒÍÁÙÅÍÍ¡¨Ò¡âÁ´ÙÅ USART à¡Ô´¢Öé¹àÁ×èÍ૵ºÔµ TXEN (ºÔµ 5 ¢Í§ÃÕ¨ÔÊàµÍÏ TXSTA) ᵋ¡ÒÃàÃÔÁè µŒ¹¨Ãԧ㹷ҧ»¯ÔºµÑ ¨Ô Ðà¡Ô´¢Ö¹é ËÅѧ¨Ò¡·ÕÃè ¨Õ ÊÔ àµÍÏ TXREG ä´ŒÃºÑ ¡ÒÃà¢Õ¹¢ŒÍÁÙÅ áÅкʹàõà¨à¹ÍàÃàµÍÏÊÌҧÊÑ­­Ò³¹Ò́¡Ò¢Í§¡Òö‹Ò·ʹ¢ŒÍÁÙÅ¢Ö¹é (´Ùû٠·Õè 10-2 »ÃСͺ) ¢ŒÍÁÙÅ ¨Ò¡ TXREG ¨Ðʋ§ä»ÂѧªÔ¿µÃ¨Õ ÊÔ àµÍÏ TSR áŌǷÂÍÂʋ§ÍÍ¡ä»·Ò§¢Ò¾Íϵ RC6/TxD/CK â´Â·Õè ¢Ò¾ÍϵÂѧÁÕǧ¨ÃºÑ¿à¿ÍÏáÅÐÊÑ­­Ò³¤Çº¤ØÁÍÕ¡ 1 àʌ¹¤×Í ºÔµ SPEN (ºÔµ 7 ¢Í§ÃÕ¨ÊÔ àµÍÏ RCSTA) «Ö§è µŒÍ§à«µºÔµ¹Õé ÁÔ©Ð¹Ñ¹é ¢ŒÍÁÙŨÐäÁ‹ÊÒÁÒöʋ§ÍÍ¡ä»ä´Œ ¡ÒÃàÅ×Íè ¹¢ŒÍÁÙŨÐàÃÔÁè µŒ¹´ŒÇºԵàÃÔÁè µŒ¹µ‹Í´ŒÇ¢ŒÍÁÙźԵ 0 ¨¹¶Ö§¢ŒÍÁÙźԵ 7 ËÃ×Í 8 ¢Ö¹é ÍÂÙ¡‹ ºÑ ¡ÒáíÒ˹´¢¹Ò´¢Í§¢ŒÍÁÙÅNjҵŒÍ§¡ÒÃʋ§áºº 8 ËÃ×Í 9 ºÔµ áÅÐʋ§ºÔµ»´·ŒÒÂÍÕ¡ 1 ºÔµÊØ´·ŒÒ ໚¹ Íѹ¤Ãº 1 ªØ´¢Í§¢ŒÍÁÙÅ ºÔµ TRMT ¨Ðᨌ§Ç‹ÒªÔ¿µÃ¨Õ ÊÔ àµÍÏäÁ‹Çҋ §µÑ§é ᵋàÃÔÁè ʋ§ºÔµàÃÔÁè µŒ¹ áÅШÐ૵ à¾×Íè ᨌ§Ç‹ÒNjҧËÅѧ¨Ò¡·ÕÊè §‹ ¢ŒÍÁÙźԵÊØ´·ŒÒ ¡‹Í¹Ê‹§ºÔµ»´·ŒÒ ÊÒÁÒö´Ùû٠·Õè 10-3 »ÃСͺ ÊÃØ»¢Ñ¹é µÍ¹¡ÒáíÒ˹´ãˌâÁ´ÙÅ USART ʋ§¢ŒÍÁÙÅã¹âËÁ´ÍЫԧâ¤Ã¹ÑÊ䴌´§Ñ ¹Õé BRGH ´ŒÇÂ

(1) ¡íÒ˹´¢ŒÍÁÙÅÊÌҧºÍ´àõŧ㹠SPBRG Ëҡ㪌ºÍ´àõ¤ÇÒÁàÃçÇÊÙ§µŒÍ§à«µºÔµ

(2) àÅ×Í¡¿˜§¡ª¹Ñè ¡ÒõԴµ‹Í¼‹Ò¹¾Íϵ͹ءÃÁáÅСíÒ˹´ãˌ·Òí §Ò¹ã¹âËÁ´ÍЫԧâ¤Ã¹ÑÊ â´Â¡ÒÃ૵ºÔµ SPEN áÅÐà¤ÅÕÂϺµÔ SYNC (3) ¶ŒÒµŒÍ§¡ÒÃãˌà¡Ô´ÍÔ¹àµÍÃ»Ñ µ µŒÍ§·íÒ¡ÒÃ૵ºÔµ TXIE (4) àÍç¹àÍàºÔÅ¡ÒÃʋ§¢ŒÍÁÙÅâ´Â૵ºÔµ TXEN ʋ§¼Åãˌá¿Å¡ TXIF ¶Ù¡à«µµÒÁä»´ŒÇ (5) ¡íÒ˹´¢ŒÍÁÙÅŧã¹ÃÕ¨ÊÔ àµÍÏ TXREG à¾×Íè àÃÔÁè µŒ¹Ê‹§¢ŒÍÁÙÅ


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 161

10.4.2 ¡ÒÃÃѺ¢ŒÍÁÙÅã¹âËÁ´ÍЫԧâ¤Ã¹ÑÊ ä´ÍÐá¡ÃÁ¡Ò÷íÒ§Ò¹áÊ´§ã¹ÃÙ»·Õè 10-4 ¡íÒ˹´ãˌµÇÑ ÃѺ¢ŒÍÁÙÅẺÍЫԧâ¤Ã¹ÑÊã¹âÁ´ÙÅ USART ·íÒ§Ò¹ µŒÍ§à«µºÔµ CREN (ºÔµ 4 ¢Í§ÃÕ¨ÊÔ àµÍÏ RCSTA) ʋǹ¢ŒÍÁÙŨж١ʋ§à¢ŒÒÁÒ·Ò§¢Ò¾Íϵ RC7/RxD/DT ·ÕÅкԵ ¼‹Ò¹Ç§¨ÃºÑ¿à¿ÍÏࢌÒÊÙʋ Nj ¹àÃÕ¡¤×¹¢ŒÍÁÙÅ (data recovery) «Ö§è ÀÒÂã¹à»š¹µÑÇàÅ×Íè ¹ ¢ŒÍÁÙŤÇÒÁàÃçÇÊÙ§ ·íÒ§Ò¹·Õ¤è ÇÒÁàÃçÇ 16 ෋ҢͧºÍ´àõ ·Ñ§é ¹Õàé ¾×Íè ãˌâÁ´ÙÅ USART ÊÒÁÒöÃѺ¢ŒÍÁÙÅà¢ŒÒ ÁÒáÅж‹Ò·ʹµ‹Íä»ÂѧªÔ¿µÃ¨Õ ÊÔ àµÍϵÇÑ ÃѺËÃ×Í RSR (receive shift register) 䴌àÃçÇ¢Ö¹é ʋ§¼ÅãˌÊÒÁÒö ÃѺ¢ŒÍÁÙÅ䴌Í‹ҧ·Ñ¹·‹Ç§·ÕáÅФú¶ŒÇ¹ ¢ŒÍÁÙÅ·ÕÃè ºÑ à¢ŒÒÁÒ¨ÐàÃÔÁè ¨Ò¡ºÔµàÃÔÁè µŒ¹ µ‹Í´ŒÇ¢ŒÍÁÙźԵ 0 ¨¹¶Ö§ºÔµ 7 ËÃ×Í 8 ¢Ö¹é ÍÂÙ¡‹ ºÑ ¡ÒÃàÅ×Í¡áÅÐÊØ´·ŒÒ¤×ͺԵ»´·ŒÒ OERR

ÊÑ­­Ò³¹ÒÌԡҢͧ ºÍ´àõ x64

FERR

CREN 64

SPBRG

RSR

MSB

16

STOP

ʋǹ¡íÒà¹Ô´ºÍ´àõ

(8)

7

LSB

1

0

56)46

RX9

ºÑ¿à¿ÍÏáÅРʋǹ¤Çº¤ØÁ¢Ò¾Íϵ

ʋǹàÃÕ¡¤×¹¢ŒÍÁÙÅ (data recovery) ºÑÊ¢ŒÍÁÙÅ 8 ºÔµ

RC7/RxD/CK

SPEN

àÍç¹àÍàºÔÅãˌ ºÑ¿à¿ÍÃÃºÑ ¢ŒÍÁÙÅ

RX9

ºÑÊ¢ŒÍÁÙÅ 8 ºÔµ

ADEN RX9 ADEN RSR <8>

4+1.

ÍÔ¹àµÍÃÃ»Ñ µ

4+1-

TX9D

RCRE

TX9D

RCRE

ºÑÊ¢ŒÍÁÙÅ 8 ºÔµ

ÃÙ»·Õè 10-4 ä´ÍÐá¡ÃÁ¡ÒÃÃѺ¢ŒÍÁÙÅã¹âËÁ´ÍЫԧâ¤Ã¹ÑʢͧâÁ´ÙÅ USART ã¹ PIC16F87x

FIFO


162 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

ËÑÇ㨢ͧ¡ÒÃÃѺ¢ŒÍÁÙŤ×ͪԿµÃ¨Õ ÊÔ àµÍÏ RSR ËÅѧ¨Ò¡·Õµè ÃǨ¨ÑººÔµ»´·ŒÒ¢ͧ¡ÒÃÃѺ¢ŒÍÁÙŠ䴌 ¢ŒÍÁÙÅã¹ RSR ¨Ðʋ§ä»ÂѧÃÕ¨ÊÔ àµÍϢ͌ ÁÙŵÑÇÃѺ RCREG ËÅѧ¨Ò¡·Õ¶è ҋ ·ʹ¢ŒÍÁÙÅàÊÃç¨ÊÁºÙó ºÔµá¿Å¡ RCIF (ºÔµ 5 ¢Í§ ÃÕ¨ÊÔ àµÍÏ PIR1) ¨Ð¶Ù¡à«µ ËÒ¡ÁÕ¡ÒÃàÍç¹àÍàºÔÅÍÔ¹àµÍÃÃ»Ñ µänj¡¨ç Ðà¡Ô´ ÍÔ¹àµÍÃÃ»Ñ µ¢¹Öé á¿Å¡ RCIF ¨Ðà¤ÅÕÂÏàÁ×Íè ¢ŒÍÁÙÅã¹ÃÕ¨ÊÔ àµÍÏ RCREG ¶Ù¡Í‹Ò¹ÍÍ¡ä»áÅÐNjҧŧ ÃÕ¨ÊÔ àµÍϢ͌ ÁÙŵÑÇÃѺ RCREG ÁÕ 2 ªØ´ ໚¹áºº FIFO ¤×Í¢ŒÍÁÙÅ·Õàè ¢ŒÒ¡‹Í¹¨Ð¶Ù¡Ê‹§ÍÍ¡¡‹Í¹ ·íÒãˌ ÃѺ¢ŒÍÁÙÅ䴌ÃÇ´àÃçÇ à¾ÃÒÐÃѺ¢ŒÍÁÙÅ 3 亵ä´ŒµÍ‹ à¹×Íè § ã¹¢³Ð·Õ¡è Òí Åѧ¶‹Ò·ʹ¢ŒÍÁÙŷѧé 2 亵¨Ò¡ÃÕ¨ÊÔ àµÍÏ RCREG ¢ŒÍÁÙÅã¹äºµ·ÊÕè ÒÁ ÊÒÁÒöࢌÒÊÙ⋠Á´ÙÅ USART 䴌·¹Ñ ·Õ â´Â¢ŒÍÁÙŨж١à¡çºänj·Õè RSR ¡‹Í¹ 㹡óշÊÕè ÒÁÒöµÃǨ¨ÑººÔµ»´·ŒÒÂ䴌 ᵋã¹ÃÕ¨ÊÔ àµÍÏ RCREG ÂѧÁբ͌ ÁÙÅÍÂً ¨Ðà¡Ô´à˵ءÒó ª¹¡Ñ¹¢Í§¢ŒÍÁÙÅ àÃÕÂ¡Ç‹Ò âÍàÇÍÃÃ¹Ñ (overrun error) ºÔµ OERR ¨Ð¶Ù¡à«µà¾×Íè ᨌ§¤ÇÒÁ¼Ô´¾ÅÒ´´Ñ§¡Å‹ÒÇ Ê‹§¼Åãˌ¢ÍŒ ÁÙÅã¹ RSR ËÒÂä» áÅСÒö‹Ò·ʹ¢ŒÍÁÙŨҡ RSR ä»ÂѧÃÕ¨ÊÔ àµÍÏ RCREG ¨Ð¶Ù¡ÂѺÂѧé änj µÃÒºà·‹Ò·Õºè µÔ OERR Âѧ¤§à«µÍÂً ºÔµ OERR ÊÒÁÒöà¤ÅÕÂÏ䴌´ÇŒ ¡Ãкǹ¡Ò÷ҧ«Í¿µáÇÏ ¤ÇÒÁ¼Ô´¾ÅÒ´ÍÕ¡áººË¹Ö§è ·ÕÍè Ò¨à¡Ô´¢Ö¹é 䴌¤Í× ¤ÇÒÁ¼Ô´¾ÅÒ´·Ò§à¿ÃÁ¢ŒÍÁÙÅ (framing error) ¨Ðà¡Ô´¢Ö¹é àÁ×Íè ¡ÒõÃǨ¨ÑººÔµ»´·ŒÒ¾ºÇ‹Ò໚¹Åͨԡ “0” «Ö§è µÃ§¡ÑºÊ¶Ò¹Ð¢Í§ºÔµàÃÔÁè µŒ¹¢Í§¢ŒÍÁÙÅã¹ ªØ´¶Ñ´ä» ʋ§¼ÅãˌäÁ‹ÊÒÁÒöµÕ¤ÇÒÁ䴌Çҋ ä´ŒÃºÑ ¢ŒÍÁÙÅÍ‹ҧÊÁºÙó¡Í‹ ¹Ë¹ŒÒ¹ÕËé Ã×ÍäÁ‹ àÁ×Íè ¡ÒõÃǨ ¨Ñº¾ºÇ‹Ò ໚¹Åͨԡ “0” ¡ÒÃÃѺ¢ŒÍÁÙŢͧµÑÇÃѺ¡ç¨ÐàÃÔÁè µŒ¹µ‹Íä» Ê‹§¼Åãˌà¡Ô´¡ÒÃ͋ҹ¢ŒÍÁÙźԵáá໚¹ “0” «Ö§è ᷌·¨Õè ÃÔ§áŌÇÁѹ¤×ͺԵàÃÔÁè µŒ¹¢Í§¢ŒÍÁÙŪشµ‹Íä» ·íÒãˌ¢ÍŒ ÁÙÅ·ÕÃè ºÑ à¢ŒÒÁÒ¼Ô´¾ÅÒ´ ºÔµ FERR ¨Ð¶Ù¡ ૵à¾×Íè ᨌ§¤ÇÒÁ¼Ô´¾ÅÒ´´Ñ§¡Å‹ÒÇ àÁ×Íè ÁÕ¡ÒÃ͋ҹ¤‹Ò¢Í§ÃÕ¨ÊÔ àµÍÏ RCREG ºÔµ FERR ¨Ð¶Ù¡»ÃѺ»Ãاâ´Â Íѵâ¹ÁÑµÔ ¶ŒÒËÒ¡¡ÒÃÃѺ¢ŒÍÁÙŤÃѧé ãËÁ‹¶¡Ù µŒÍ§ ºÔµ FERR ¨Ð¡ÅÒÂ໚¹ “0” â´ÂÍѵâ¹ÁÑµÔ Í‹ҧäáçµÒÁ ËÒ¡à¡Ô´¤ÇÒÁ¼Ô´¾ÅÒ´·Ò§à¿ÃÁ¢ŒÍÁÙÅ¢Ö¹é áÅÐàÅ×Í¡¡ÒÃÃѺ¢ŒÍÁÙÅẺ 9 ºÔµ ¨íÒ໚¹Í‹ҧÂÔ§è ·Õ¨è еŒÍ§·íÒ¡Òà ͋ҹ¢ŒÍÁÙŨҡ RCSTA ¢Ö¹é ÁÒ¡‹Í¹à¾×Íè ÃÑ¡ÉÒ¢ŒÍÁÙźԵ·Õè 9 àÍÒänj ¨Ò¡¹Ñ¹é ¨Ö§Í‹Ò¹¤‹Ò¢Í§ÃÕ¨ÊÔ àµÍÏ RCREG à¾×Íè ͋ҹ¢ŒÍÁÙÅã¹ 8 ºÔµ·Õàè ËÅ×Í ¾ÃŒÍÁ¡Ñº»ÃѺ»Ãا¤‹Ò¢Í§ºÔµ FERR ´ŒÇ ÊÃØ»¢Ñ¹é µÍ¹¡ÒáíÒ˹´ãˌâÁ´ÙÅ USART ÃѺ¢ŒÍÁÙÅã¹âËÁ´ÍЫԧâ¤Ã¹ÑÊ䴌´§Ñ ¹Õé (1) ¡íÒ˹´¢ŒÍÁÙÅÊÌҧºÍ´àõŧ㹠SPBRG Ëҡ㪌ºÍ´àõ¤ÇÒÁàÃçÇÊÙ§µŒÍ§à«µºÔµ BRGH (2) àÅ×Í¡¿˜§¡ª¹Ñè ¡ÒõԴµ‹Í¼‹Ò¹¾Íϵ͹ءÃÁáÅСíÒ˹´ãˌ·Òí §Ò¹ã¹âËÁ´ÍЫԧâ¤Ã¹ÑÊ â´Â¡ÒÃ૵ºÔµ SPEN áÅÐà¤ÅÕÂϺµÔ SYNC (3) ¶ŒÒµŒÍ§¡ÒÃãˌà¡Ô´ÍÔ¹àµÍÃÃ»Ñ µ µŒÍ§à«µºÔµ RCIE (4) ¶ŒÒàÅ×Í¡¡ÒÃÃѺ¢ŒÍÁÙÅẺ 9 ºÔµãˌ૵ºÔµ RX9


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 163

(5) àÍç¹àÍàºÔÅ¡ÒÃÃѺ¢ŒÍÁÙÅâ´Â¡ÒÃ૵ºÔµ CREN (6) ºÔµá¿Å¡ RCIF ¶Ù¡à«µàÁ×Íè ÃѺ¢ŒÍÁÙÅàÊÃç¨ÊÔ¹é ¨Ðà¡Ô´ÍÔ¹àµÍÃÃ»Ñ µËÒ¡àÍç¹àÍàºÔÅänj (7) ͋ҹ¢ŒÍÁÙŢͧÃÕ¨ÊÔ àµÍÏ RCSTA à¾×Íè ͋ҹ¢ŒÍÁÙźԵ·Õè 9 ¶ŒÒËÒ¡àÅ×Í¡¡ÒÃÃѺ¢ŒÍÁÙŠẺ 9 ºÔµ áÅеÃǨÊͺ¤ÇÒÁ¼Ô´¾ÅÒ´·ÕÍè Ò¨à¡Ô´¢Ö¹é ÃÐËNjҧ¡ÒÃÃѺ¢ŒÍÁÙÅ (8) ͋ҹ¢ŒÍÁÙÅ 8 ºÔµ·ÕÃè ºÑ à¢ŒÒÁÒ â´Â͋ҹ¼‹Ò¹·Ò§ÃÕ¨ÊÔ àµÍÏ RCREG (9) ¶ŒÒà¡Ô´¤ÇÒÁ¼Ô´¾ÅÒ´¢Ö¹é ãˌà¤ÅÕÂϺµÔ ᨌ§¤ÇÒÁ¼Ô´¾ÅÒ´ËÃ×ͺԵ CREN

10.5 ¡ÒÃãªŒÍ¹Ô àµÍÃÃ»Ñ µà¾×Íè ÃѺ¢ŒÍÁÙÅ ¡ÒÃÃѺ¢ŒÍÁÙÅ͹ءÃÁẺ㪌ÇÔ¸Õ¡ÒÃǹÅÙ»Ãͨ¹¡Ç‹Ò¨ÐÁÕ¢ŒÍÁÙŶ١ʋ§ÍÍ¡ÁÒ Áըش͋͹µÃ§·Õè ¶ŒÒâ»Ãá¡ÃÁÁÕ¡ÒáÃÐâ´´ä»·íҧҹʋǹÍ×¹è æ áŌǡÅѺÁÒÃѺ¢ŒÍÁÙÅäÁ‹·¹Ñ ¢ŒÍÁÙÅ·ÕÊè §‹ ÁÒÍÒ¨ÊÙ­ËÒÂ䴌 ·Ò§á¡Œä¢¤×Í ãªŒ¡ÒÃÍÔ¹àµÍÃÃ»Ñ µà¢ŒÒÁÒª‹Ç ·íÒãˌâ»Ãá¡ÃÁäÁ‹¨Òí ໚¹µŒÍ§ÁÕ¡ÒõÃǨÊͺ¡Òà ÃѺ¢ŒÍÁÙÅ ÊÒÁÒö价íÒ§Ò¹ã¹Ê‹Ç¹Í×¹è æ 䴌 Ãͨ¹¡Ç‹Ò¨ÐÁÕ¡ÒáÃеعŒ ¨Ò¡ÍÔ¹àµÍÃÃ»Ñ µ¤Ãѧé Ë¹Ö§è ¨Ö§ ¡ÃÐâ´´ÁÒ·íÒ§Ò¹ã¹â»Ãá¡ÃÁ‹ÍºÃÔ¡ÒÃÍÔ¹àµÍÏÃÑ»µ¢Í§¡ÒÃÃѺ¢ŒÍÁÙÅ͹ءÃÁ ʋ§¼Åãˌ¡ÒÃÃѺ ¢ŒÍÁÙÅÁÕ¤ÇÒÁ¶Ù¡µŒÍ§ÁÒ¡ÂÔ觢Öé¹ ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC 䴌àµÃÕÂÁʋǹºÃÔ¡ÒÃÍÔ¹àµÍÃÃ»Ñ µÊÒí ËÃѺâÁ´ÙÅÊ×Íè ÊÒâŒÍÁÙŠ͹ءÃÁ USART änjáÅŒÇ â´Âãˌ·Òí ¡ÒÃàÍç¹àÍàºÔÅ¡ÒÃÍÔ¹àµÍÃÃ»Ñ µ¹´Õé nj ¡ÒÃ૵ºÔµ RCIE ã¹ÃÕ¨ÊÔ àµÍÏ PIE1 àÁ×Íè Áբ͌ ÁÙŶ١ʋ§à¢ŒÒÁÒ ºÔµ RCIF ¨Ð¶Ù¡à«µ àÁ×Íè à¡Ô´¡ÒÃÍÔ¹àµÍÃÃ»Ñ µ «Õ¾ÂÕ Ù¨Ð¡ÃÐâ´´ä»Âѧ¿˜§¡ª¹Ñè ºÃÔ¡ÒÃÍÔ¹àµÍÃÃ»Ñ µ·¹Ñ ·Õ áÅзíÒ§Ò¹ã¹â»Ãá¡ÃÁ‹ÍºÃÔ¡ÒÃÍÔ¹àµÍÃÃ»Ñ µ¨¹àÊÃ稡‹Í¹·Õ¨è СÅѺÁÒ·íÒ§Ò¹ â»Ãá¡ÃÁ»¡µÔµÍ‹ ä» ã¹¿˜§¡ª¹Ñè ºÃÔ¡ÒÃÍÔ¹àµÍÃÃ»Ñ µ¨ÐµŒÍ§ÁÕ¤Òí Êѧè ͋ҹ¤‹Ò¨Ò¡ÃÕ¨ÊÔ àµÍÏ RCREG «Ö§è àÁ×Íè ͋ҹ¤‹ÒáŌǨР໚¹¡ÒÃà¤ÅÕÂÏá¿Å¡ÍÔ¹àµÍÃÃ»Ñ µ RCIF â´ÂÍѵâ¹ÁѵÔ


164 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

»¯ÔºµÑ ¡Ô Ò÷Õè 17 ¡ÒÃʋ§¢ŒÍÁÙÅẺÍЫԧâ¤Ã¹ÑÊ áºº¡íÒ˹´¤‹ÒÃÕ¨ÔÊàµÍÏâ´ÂµÃ§ 㹡Ò÷´Åͧ¹Õé໚¹¡ÒùíÒàʹ͡ÒÃà¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C à¾×èͤǺ¤ØÁãˌäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ÊÒÁÒöÊ×Íè ÊÒâŒÍÁÙÅ͹ءÃÁ¡Ñº¤ÍÁ¾ÔÇàµÍϼҋ ¹¾Íϵ͹ءÃÁ䴌 â´ÂäÁ‹ãª‹¿§˜ ¡ª¹Ñè ËÃ×ÍäźÃÒÃÕ¾àÔ Èɢͧ mikroC ¤ÍÁä¾àÅÍÏ ᵋ¨Ð໚¹¡ÒÃà¢Õ¹¤‹Òŧã¹ÃÕ¨ÊÔ àµÍÏ·àÕè ¡ÕÂè Ç¢ŒÍ§á·¹ ÊíÒËÃѺ㹡Ò÷´Åͧ¹Õàé »š¹¡Ò÷´Åͧʋ§¢ŒÍÁÙÅ͹ءÃÁ¨Ò¡äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC16F877 ä»áÊ´§ ¼Å·ÕËè ¹ŒÒµ‹Ò§ UART Terminal ¢Í§ mirkoC ¤ÍÁä¾àÅÍÏ

¡ÒÃàª×Íè Áµ‹Í·Ò§ÎÒÏ´áÇÏ Âѧ¤§ãªŒÇ§¨Ãã¹ÃÙ»·Õè L15-1 㹡Ò÷´Åͧ l Ëҡ㪌ºÍÏ´·´Åͧ NX-877 plus II ãˌµÍ ‹ ǧ¨ÃµÒÁÃÙ»·Õè L15-1 áŌÇàÅ×Í¡äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ໚¹ PIC16F877 l

¢Ñ鹵͹¡Ò÷´Åͧ L17.1 ແ´â»Ãá¡ÃÁ mikroC ÊÌҧä¿Åâ»Ãà¨ç¡µ â´ÂàÅ×Í¡äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏáÅФÇÒÁ¶ÕÊè ­ Ñ ­Ò³¹Ò́¡Ò´Ñ§¹Õé l ¡Ã³Õ㪌ºÍÏ´ NX-877 plus II àÅ×Í¡ PIC16F877 ËÃ×Í PIC16F877A ¤ÇÒÁ¶ÕÊ è­ Ñ ­Ò³¹Ò́¡Ò 20MHz char dat ; void main() { SPBRG = 129; TXSTA.BRGH = 1 ; TXSTA.TXEN = 1 ; RCSTA.SPEN = 1 ; while(1) { dat = 'A' ; while(PIR1.TXIF) { TXREG = dat ; delay_ms(500); dat = dat++ ; } } }

// // // //

9600 bps Baudrate mode set High Enble Transmitter Enable Serial Port

// First Character // Test Buffer Blank // Send dat // Delay 500 ms // Increment dat

¡Ò÷íÒ§Ò¹¢Í§â»Ãá¡ÃÁ ໚¹â»Ãá¡ÃÁ¹íÒ¤‹ÒµÑÇÍÑ¡Éà (ÃËÑÊáÍÊ¡Õ)é ¢Í§µÑÇÍÑ¡Éà A ໚¹µŒ¹ä»Ê‹§ä»áÊ´§¼Å¼‹Ò¹¾Íϵ͹ءÃÁ â´Â ànj¹ª‹Ç§µÑÇÍÑ¡ÉÃÅÐ 0.5 ÇÔ¹Ò·Õ â´Â¤íҹdz¤‹ÒºÍ´àõänj෋ҡѺ 9,600 ºÔµµ‹ÍÇÔ¹Ò·Õ

â»Ãá¡ÃÁ·Õè L17-1 ä¿Å TxUART.c â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺ·´Êͺ¡ÒÃʋ§¢ŒÍÁÙż‹Ò¹âÁ´ÙÅ USART ¢Í§ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC16F877(A) ´ŒÇ¡ÒáíÒ˹´¤‹Òä»ÂѧÃÕ¨ÊÔ àµÍϤǺ¤ØÁ¡Ò÷íÒ§Ò¹â´ÂµÃ§


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 165

L17.2 ÊÌҧâ»Ãà¨ç¡µªÍ×è TxUART áÅоÔÁ¾â»Ãá¡ÃÁÀÒÉÒ C µÒÁâ»Ãá¡ÃÁ·Õè L17-1 L17.3 ·íÒ¡ÒäÍÁä¾ÅáÅдÒǹâËÅ´â»Ãá¡ÃÁà¾×Íè ·´Êͺ¼ÅÅѾ¸¡Ò÷íÒ§Ò¹ L17.4 ·Õâè »Ãá¡ÃÁ mikroC ¤ÍÁä¾àÅÍÏ ແ´â»Ãá¡ÃÁ USART Terminal â´Âä»·Õàè Á¹Ù Tools à USART Terminal ºÃÔàdzᶺà¤Ã×Íè §Á×Í·Ò§´ŒÒ¹¢ÇÒÁ×Í ¨Ò¡¹Ñ¹é ·íÒ¡ÒáíÒ˹´µíÒá˹‹§ ËÃ×Í¡´»ØÁ† CTRL+T ËÃ×ͤÅÔ¡·Õäè ͤ͹ ¾Íϵ͹ءÃÁ áÅСíÒ˹´¤‹ÒºÍ´àõ෋ҡѺ 9600 ºÔµ ʵçÍ» 1 ºÔµ äÁ‹Á¾Õ ÒÃÔµáÕé ÅШíҹǹºÔµ¢ŒÍÁÙÅ 8 ºÔµ à¾×Íè ແ´¡ÒÃÊ×Íè ÊÒÃ͹ءÃÁ L17.5 ¡´»ØÁ† L17.6 Ãѹâ»Ãá¡ÃÁ·Ò§½˜§› äÁâ¤Ã¤Í¹â·ÃÅàÅÍϴnj ¡ÒÃແ´»´ä¿àÅÕÂé §ãËÁ‹Í¡Õ ¤Ãѧé ËÃ×Í¡´ÊÇÔµª RESET ·ÕËè ¹ŒÒµ‹Ò§ÃѺ¢ŒÍÁÙÅ͹ءÃÁ¨ÐáÊ´§¢ŒÍ¤ÇÒÁ Received: A áÅŒÇ¢Ö¹é ºÃ÷ѴãËÁ‹à¾×Íè ᨌ§¡ÒÃÃѺÍÑ¡Éà B, C, D,... ä»Í‹ҧµ‹Íà¹×Íè § ´Ñ§ÃÙ»

mikroC


166 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

»¯ÔºµÑ ¡Ô Ò÷Õè 18 ¡ÒÃÃѺ¢ŒÍÁÙÅẺÍЫԧâ¤Ã¹ÑÊ áºº¡íÒ˹´¤‹ÒÃÕ¨ÔÊàµÍÏâ´ÂµÃ§ 㹡Ò÷´Åͧ¹Õé໚¹¡ÒùíÒàʹ͡ÒÃà¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C à¾×èͤǺ¤ØÁãˌäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ÃѺ¢ŒÍÁÙÅ͹ءÃÁ¨Ò¡¤ÍÁ¾ÔÇàµÍÏ·ÊÕè §‹ ÁÒ¨Ò¡â»Ãá¡ÃÁ UART Terminal ¢Í§ mirkoC ¤ÍÁä¾àÅÍÏ à¾×Íè áÊ´§¼Å ¼‹Ò¹ LED ·Õµè ͋ ÍÂÙ¡‹ ºÑ ¾Íϵ D â´Â¡ÒõԴµ‹Í¡ÑºâÁ´ÙÅ USART ¹Ñ¹é ¨Ð໚¹¡ÒÃà¢Õ¹â»Ãá¡ÃÁà¾×Íè ¡íÒ˹´¤‹Òä»Âѧ ÃÕ¨ÊÔ àµÍϤǺ¤ØÁ¢Í§âÁ´ÙÅ USART â´ÂµÃ§

¡ÒÃàª×Íè Áµ‹Í·Ò§ÎÒÏ´áÇÏ Âѧ㪌ǧ¨Ãã¹ÃÙ»·Õè L15-1 㹡Ò÷´Åͧ l Ëҡ㪌ºÍÏ´·´Åͧ NX-877 plus II ãˌµÍ ‹ ǧ¨ÃµÒÁÃÙ»·Õè L15-1 áŌÇàÅ×Í¡äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ໚¹ PIC16F877 l

¢Ñ鹵͹¡Ò÷´Åͧ L18.1 ແ´â»Ãá¡ÃÁ mikroC ÊÌҧä¿Åâ»Ãà¨ç¡µ â´ÂàÅ×Í¡äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏáÅФÇÒÁ¶ÕÊè ­ Ñ ­Ò³¹Ò́¡Ò´Ñ§¹Õé l ¡Ã³Õ㪌ºÍÏ´ NX-877 plus II àÅ×Í¡ PIC16F877 ËÃ×Í PIC16F877A ¤ÇÒÁ¶ÕÊ è­ Ñ ­Ò³¹Ò́¡Ò 20MHz char dat; void main() { TRISD = 0 ; SPBRG = 129; TXSTA.BRGH = 1 ; RCSTA.SPEN = 1 ; RCSTA.CREN = 1 ; dat = RCREG ; dat = RCREG ; dat = RCREG ; while(1) { while(!PIE1.RCIF) { PORTD = RCREG } } }

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

PORTB ==> output 9600 bps Baudrate mode set High Enable Serial Port Enable Continuous receive clear buffer data clear buffer data clear buffer data

// Test Received data ;

// Show data on PORTB

¡Ò÷íÒ§Ò¹¢Í§â»Ãá¡ÃÁ ໚¹â»Ãá¡ÃÁÃÍÃѺ¢ŒÍÁÙŨҡ¾Íϵ͹ءÃÁ´ŒÇ¡ÒõÃǨÊͺºÔµ RCIF àÁ×Íè Áբ͌ ÁÙÅࢌÒÁÒ㹺ѿà¿ÍÏ ºÔµ¹Õ¨é Ð໚¹ “1” ¨Ò¡¹Ñ¹é ¹íÒ¢ŒÍÁÙÅ·ÕÃè ºÑ ä´ŒÊ§‹ ä»Âѧ¾Íϵ D 㹡ÒáíÒ˹´¤‹ÒàÃÔÁè µŒ¹¡Ò÷íÒ§Ò¹¹Ñ¹é ¹Í¡¨Ò¡¡Òà àÍç¹àÍàºÔÅ¡ÒÃÊ×Íè ÊÒâŒÍÁÙÅ͹ءÃÁáÅСíÒ˹´¤‹ÒºÍ´àõáÅŒÇ ¨ÐµŒÍ§Í‹Ò¹¢ŒÍÁÙÅ 3 ¤Ãѧé à¾×Íè à¤ÅÕÂϤҋ 㹺ѿà¿ÍÏ

â»Ãá¡ÃÁ·Õè L18-1 ä¿Å RxUART.c â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺ·´Êͺ¡ÒÃÃѺ¢ŒÍÁÙż‹Ò¹âÁ´ÙÅ USART ¢Í§ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC16F877(A) ´ŒÇ¡ÒáíÒ˹´¤‹Òä»ÂѧÃÕ¨ÊÔ àµÍϤǺ¤ØÁ¡Ò÷íÒ§Ò¹â´ÂµÃ§


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 167

L18.2 ÊÌҧâ»Ãà¨ç¡µªÍ×è RxUART áÅоÔÁ¾â»Ãá¡ÃÁÀÒÉÒ C µÒÁâ»Ãá¡ÃÁ·Õè L18-1 L18.3 ·íÒ¡ÒäÍÁä¾ÅáÅдÒǹâËÅ´â»Ãá¡ÃÁà¾×Íè ·´Êͺ¼ÅÅѾ¸¡Ò÷íÒ§Ò¹ L18.4 ·Õâè »Ãá¡ÃÁ mikroC ¤ÍÁä¾àÅÍÏ ແ´â»Ãá¡ÃÁ USART Terminal â´Âä»·Õàè Á¹Ù Tools à USART Terminal ºÃÔàdzᶺà¤Ã×Íè §Á×Í·Ò§´ŒÒ¹¢ÇÒÁ×Í ¨Ò¡¹Ñ¹é ·íÒ¡ÒáíÒ˹´µíÒá˹‹§ ËÃ×Í¡´»ØÁ† CTRL+T ËÃ×ͤÅÔ¡·Õäè ͤ͹ ¾Íϵ͹ءÃÁ áÅСíÒ˹´¤‹ÒºÍ´àõ෋ҡѺ 9600 ºÔµ ʵçÍ» 1 ºÔµ äÁ‹Á¾Õ ÒÃÔµáÕé ÅШíҹǹºÔµ¢ŒÍÁÙÅ 8 ºÔµ à¾×Íè ແ´¡ÒÃÊ×Íè ÊÒÃ͹ءÃÁ L18.5 ¡´»ØÁ† L18.6 ¤ÅÔ¡à¾×Íè ¡Òà¤Ã×Íè §ËÁÒ¶١·ÕËè ¹ŒÒËÑÇ¢ŒÍ Send as number à¾×Íè àÅ×Í¡¡ÒÃʋ§¢ŒÍÁÙÅ໚¹µÑÇàÅ¢ L18.7 Ãѹâ»Ãá¡ÃÁ·Ò§½˜§› äÁâ¤Ã¤Í¹â·ÃÅàÅÍϴnj ¡ÒÃແ´»´ä¿àÅÕÂé §ãËÁ‹Í¡Õ ¤Ãѧé ËÃ×Í¡´ÊÇÔµª RESET L18.8 »‡Í¹µÑÇàÅ¢ 65 ·Õªè ͋ §»‡Í¹¢ŒÍÁÙÅ ¨Ò¡¹Ñ¹é ¡´»ØÁ†

à¾×Íè ʋ§¢ŒÍÁÙÅÍÍ¡ä» Êѧࡵ¡ÒÃáÊ´§¼Å¢Í§ LED

·Õè LED µíÒá˹‹§ºÔµ 6 áÅкԵ 0 ¨ÐµÔ´ «Ö§è ෋ҡѺàÅ¢°Ò¹Êͧ໚¹ 01000001 ËÃ×Í 0x41 ¢Í§àÅ¢°Ò¹ ÊԺˡ à¹×Íè §¨Ò¡¤‹Ò 65 ·ÕÊè §‹ ÍÍ¡Áҹѹé ໚¹àÅ¢°Ò¹ÊÔº àÁ×Íè äÁâ¤Ã¤Í¹â·ÃÅàÅÍÃÃºÑ ä´Œ¨ÐµÕ¤ÇÒÁ໚¹àÅ¢°Ò¹Êͧ ËÃ×Í°Ò¹ÊԺˡ «Ö§è ¤‹Ò 65 °Ò¹ÊÔº¨Ð෋ҡѺ 01000001 °Ò¹Êͧ áÅÐ 0x41 °Ò¹ÊԺˡ

ÃÙ» LED ´ŒÒ¹º¹à»š¹Ê‹Ç¹Ë¹Ö§è ¢Í§ LED Á͹ÔàµÍϺ¹ºÍÏ´·´Åͧ NX-877 plus II ·Õãè ªŒÍҌ §Íԧ㹡Òà ·´Åͧ¹Õé

mikroC


168 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

»¯ÔºµÑ ¡Ô Ò÷Õè 19 ¡ÒÃÃѺ¢ŒÍÁÙÅẺÍЫԧâ¤Ã¹ÑÊ â´Â㪌ÍÔ¹àµÍÏÃÑ»µ ¡Ò÷´Åͧ¹Õ¹é Òí àʹ͡ÒÃà¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C à¾×Íè ¤Ç¤ºØÁãˌäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ÃѺ¢ŒÍÁÙÅ͹ءÃÁ Ẻ㪌¡Œ ÒÃÍÔ¹àµÍÃÃ»Ñ µ â´Â¡íÒ˹´ãˌäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ¢Ñº LED ·Õ¢è Ò¾Íϵ RD7 µÔ´¡Ð¾ÃԺ͋ҧµ‹Íà¹×Íè § ã¹ÊÀÒÇл¡µÔ·äÕè Á‹Á¡Õ ÒÃÃѺ¢ŒÍÁÙÅ áÅÐàÁ×Íè ÁÕ¡ÒÃÃѺ¢ŒÍÁÙÅࢌÒÁÒ ¡ç¨ÐáÊ´§¤‹Ò¢Í§¢ŒÍÁÙŹѹé Á·Õ¾è Íϵ D á·¹

¡ÒÃàª×Íè Áµ‹Í·Ò§ÎÒÏ´áÇÏ Âѧ㪌ǧ¨Ãã¹ÃÙ»·Õè L15-1 㹡Ò÷´Åͧ l Ëҡ㪌ºÍÏ´·´Åͧ NX-877 plus II ãˌµÍ ‹ ǧ¨ÃµÒÁÃÙ»·Õè L15-1 áŌÇàÅ×Í¡äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ໚¹ PIC16F877 l

¢Ñ鹵͹¡Ò÷´Åͧ L19.1 ແ´â»Ãá¡ÃÁ mikroC ÊÌҧä¿Åâ»Ãà¨ç¡µ â´ÂàÅ×Í¡äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏáÅФÇÒÁ¶ÕÊè ­ Ñ ­Ò³¹Ò́¡Ò´Ñ§¹Õé l ¡Ã³Õ㪌ºÍÏ´ NX-877 plus II àÅ×Í¡ PIC16F877 ËÃ×Í PIC16F877A ¤ÇÒÁ¶ÕÊ è­ Ñ ­Ò³¹Ò́¡Ò 20MHz L19.2 ÊÌҧâ»Ãà¨ç¡µªÍ×è InterruptUART áÅоÔÁ¾â»Ãá¡ÃÁÀÒÉÒ C µÒÁâ»Ãá¡ÃÁ·Õè L19-1 L19.3 ·íÒ¡ÒäÍÁä¾ÅáÅдÒǹâËÅ´â»Ãá¡ÃÁà¾×Íè ·´Êͺ¼ÅÅѾ¸¡Ò÷íÒ§Ò¹ L19.4 ·Õâè »Ãá¡ÃÁ mikroC ¤ÍÁä¾àÅÍÏ ແ´â»Ãá¡ÃÁ USART Terminal â´Âä»·Õàè Á¹Ù Tools à USART Terminal ºÃÔàdzᶺà¤Ã×Íè §Á×Í·Ò§´ŒÒ¹¢ÇÒÁ×Í ¨Ò¡¹Ñ¹é ·íÒ¡ÒáíÒ˹´µíÒá˹‹§ ËÃ×Í¡´»ØÁ† CTRL+T ËÃ×ͤÅÔ¡·Õäè ͤ͹ ¾Íϵ͹ءÃÁ áÅСíÒ˹´¤‹ÒºÍ´àõ෋ҡѺ 9600 ºÔµ ʵçÍ» 1 ºÔµ äÁ‹Á¾Õ ÒÃÔµáÕé ÅШíҹǹºÔµ¢ŒÍÁÙÅ 8 ºÔµ à¾×Íè ແ´¡ÒÃÊ×Íè ÊÒÃ͹ءÃÁ L19.5 ¡´»ØÁ† L19.6 ¤ÅÔ¡à¾×Íè ¡Òà¤Ã×Íè §ËÁÒ¶١·ÕËè ¹ŒÒËÑÇ¢ŒÍ Send as number à¾×Íè àÅ×Í¡¡ÒÃʋ§¢ŒÍÁÙÅ໚¹µÑÇàÅ¢ L19.7 Ãѹâ»Ãá¡ÃÁ·Ò§½˜§› äÁâ¤Ã¤Í¹â·ÃÅàÅÍϴnj ¡ÒÃແ´»´ä¿àÅÕÂé §ãËÁ‹Í¡Õ ¤Ãѧé ËÃ×Í¡´ÊÇÔµª RESET Êѧࡵ ¡ÒÃáÊ´§¼Å¢Í§ LED LED ·Õ¾è Íϵ RD7 µÔ´¡Ð¾ÃÔº L19.8 »‡Í¹µÑÇàÅ¢ 65 ·Õªè ͋ §»‡Í¹¢ŒÍÁÙÅ ¨Ò¡¹Ñ¹é ¡´»ØÁ†

à¾×Íè ʋ§¢ŒÍÁÙÅÍÍ¡ä» Êѧࡵ¡ÒÃáÊ´§¼Å¢Í§ LED

·Õè LED µíÒá˹‹§ºÔµ 6 áÅкԵ 0 ¨ÐµÔ´ «Ö§è ෋ҡѺàÅ¢°Ò¹Êͧ໚¹ 01000001 ËÃ×Í 0x41 ¢Í§àÅ¢°Ò¹ ÊԺˡ à¹×Íè §¨Ò¡¤‹Ò 65 ·ÕÊè §‹ ÍÍ¡Áҹѹé ໚¹àÅ¢°Ò¹ÊÔº àÁ×Íè äÁâ¤Ã¤Í¹â·ÃÅàÅÍÃÃºÑ ä´Œ¨ÐµÕ¤ÇÒÁ໚¹àÅ¢°Ò¹Êͧ ËÃ×Í°Ò¹ÊԺˡ «Ö§è ¤‹Ò 65 °Ò¹ÊÔº¨Ð෋ҡѺ 01000001 °Ò¹Êͧ áÅÐ 0x41 °Ò¹ÊԺˡ


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

char dat ; void interrupt() { PORTD = RCREG ; } void enable_int() { PIE1.RCIE = 1 ; INTCON.PEIE = 1 ; INTCON.GIE = 1 ; } void main() { TRISD = 0 ; SPBRG = 129; enable_int() ; TXSTA.BRGH = 1 ; RCSTA.SPEN = 1 ; RCSTA.CREN = 1 ; dat = RCREG ; dat = RCREG ; dat = RCREG ; while(1) { PORTD.F7 =0; delay_ms(500); PORTD.F7 = 1; delay_ms(500); } }

l 169

// Receive Interrupt

// Enable receive interrupt // Enable peripheral interrupt // Enable Global interrupt

// PORTD ==> output // 9600 bps // // // // // //

Baudrate mode set High Enable Serial Port Enable Continuous receive clear buffer data clear buffer data clear buffer data

// Blinking LED // ON RD7

¡Ò÷íÒ§Ò¹¢Í§â»Ãá¡ÃÁ ¨Ò¡â»Ãá¡ÃÁ໚¹¡ÒÃãªŒÍ¹Ô àµÍÃÃ»Ñ µà¢ŒÒÁÒª‹ÇÂ㹡ÒõÃǨÊͺ¡ÒÃÃѺ¢ŒÍÁÙÅ àÁ×Íè ÁÕ¡ÒÃÃѺ¢ŒÍÁÙÅࢌÒÁÒ ¨Ðà¡Ô´¡ÒÃÍÔ¹àµÍÃÃ»Ñ µ áŌǫվÂÕ ¨Ù СÃÐâ´´ä»·íÒ§Ò¹µÒÁ¿˜§¡ª¹Ñè ºÃÔ¡ÒÃÍÔ¹àµÍÃÃ»Ñ µ «Ö§è ¨Ðʋ§¤‹Ò¢Í§¢ŒÍÁÙÅ·Õè ÃѺ䴌ä»Âѧ·Õ¾è Íϵ D ʋǹ㹢³Ð·Õäè Á‹Á¡Õ ÒÃÍÔ¹àµÍÃÃ»Ñ µ â»Ãá¡ÃÁ¨Ð¡íÒ˹´ãˌäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏʧ‹ ¢ŒÍÁÙÅ “1” áÅÐ “0” ä»Âѧ¢Ò¾Íϵ RD7 «Ö§è µ‹Í LED änj ·íÒãˌ LED à¡Ô´¡ÒáоÃÔº ¿˜§¡ª¹Ñè enable_int() 㪌¡Òí ˹´¤‹ÒàÃÔÁè µŒ¹ÊíÒËÃѺ¡ÒÃÍÔ¹àµÍÃÃ»Ñ µ â´Â¨ÐàÍç¹àÍàºÔÅ¡Ò÷íÒ§Ò¹¢Í§ ÍÔ¹àµÍÃÃ»Ñ µ 3 µÑǤ×Í àÍç¹àÍàºÔÅÍÔ¹àµÍÃÃ»Ñ µ¡ÒÃÃѺ¢ŒÍÁÙÅ͹ءÃÁ àÍç¹àÍàºÔÅÍÔ¹àµÍÃÃ»Ñ µ¨Ò¡ÍØ»¡Ã³µÍ‹ ¾‹Ç§ áÅÐ àÍç¹àÍàºÔÅ¡ÒÃÍÔ¹àµÍÃÃ»Ñ µËÅÑ¡

â»Ãá¡ÃÁ·Õè L19-1 ä¿Å InterruptUART.c â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺ·´Êͺ¡ÒÃÃѺ¢ŒÍÁÙż‹Ò¹âÁ´ÙÅ USART ¢Í§äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC16F877(A) ´ŒÇ¡ÒÃãªŒÍ¹Ô àµÍÃÃ»Ñ µ

INNOVATIVE EXPERIMENT


170 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 171

º··ÕÕè 11

¡ÒõԴµ‹ÍÍØ»¡Ã³ÃкººÑÊ I2C ´ŒÇ¿˜§¡ª¹Ñè ¢Í§ mikroC ¤ÍÁä¾àÅÍÏ ¡ÒõԴµ‹Í¡ÑºÍØ»¡Ã³¼Ò‹ ¹ÃкººÑÊ I2C ໚¹ÍÕ¡Ë¹Ö§è ¤ÇÒÁÊÒÁÒö·Õäè Áâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ÃͧÃѺ䴌 áÅÐ mirkoC ¤ÍÁä¾àÅÍÏ¡Áç äÕ ÅºÃÒÃÕáÅ¡ª¹Ñè ·ÕÊè ¹ÑºÊ¹Ø¹¡Ò÷íÒ§Ò¹´ŒÇ â´Âẋ§ÍÍ¡ ໚¹ 2 Ẻ¤×Í äźÃÒÃÕÊÒí ËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ·ÕÁè âÕ Á´ÙÅÊ×Íè ÊÒâŒÍÁÙÅ I2C (Master MSSP) áÅÐäźÃÒÃÕÊÒí ËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ·Õäè Á‹ÁâÕ Á´ÙŴѧ¡Å‹ÒÇËÃ×͡óբҾÍϵ¢Í§âÁ´ÙźÑÊ I2C äÁ‹à¾Õ§¾Íµ‹Í¤ÇÒÁµŒÍ§¡ÒÃ㪌§Ò¹

11.1 äźÃÒÃÕÃкººÑÊ I2C 11.1.1 ¿˜§¡ª¹Ñè I2C_Init 㪌¡Òí ˹´¤‹ÒàÃÔÁè µŒ¹áÅФÇÒÁ¶ÕÊè ­ Ñ ­Ò³¹Ò́¡Ò¢Í§ÃкººÑÊ I2C àÁ×Íè µŒÍ§¡ÒõԴµ‹ÍâÁ´ÙźÑÊ I2C ¨ÐµŒÍ§àÃÕ¡㪌§Ò¹¿˜§¡ª¹Ñè ¹Õ¡é ͋ ¹àÊÁÍ ÃٻẺ¡ÒÃ㪌§Ò¹¤íÒÊѧè void I2C_Init(unsigned long clock);

µÑÇá»Ã

¤×Í ¤ÇÒÁ¤ÇÒÁ¶Õ¢è ͧÊÑ­­Ò³¹Ò́¡Ò·Õãè ªŒã¹¡ÒÃÊ×Íè ÊÒâŒÍÁÙŠ˹‹ÇÂ໚¹ Hz ÊíÒËÃѺºÑÊ I2C ÁҵðҹÁÕ 2 âËÁ´ ¤×Í âËÁ´¤ÇÒÁàÃçǵèÒí 100kHz áÅÐâËÁ´¤ÇÒÁàÃçÇÊÙ§ 400 kHz clock

µÑÇÍ‹ҧ·Õè 11-1 I2C_Init(100000);

໚¹¡ÒáíÒ˹´¤‹ÒàÃÔÁè µŒ¹ãˌ¡ºÑ âÁ´ÙÅ I2C áÅСíÒ˹´ÊÑ­­Ò³¹Ò́¡Ò໚¹âËÁ´¤ÇÒÁàÃçǵèÒí 100kHz


172 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

11.1.2 ¿˜§¡ª¹Ñè I2C_Start 㪌¡Òí ˹´ãˌºÊÑ Ç‹Ò§ áÅÐʋ§ÊÑ­­Ò³ Start ÍÍ¡ä» ¡‹Í¹ãªŒ§Ò¹µŒÍ§àÃÕ¡¿˜§¡ª¹Ñè I2C_Init ¡‹Í¹ ÃٻẺ¡ÒÃ㪌§Ò¹¤íÒÊѧè unsigned short I2C_Start(void);

¡Òä׹¤‹Ò

¶ŒÒäÁ‹Á¢Õ ͌ ¼Ô´¾ÅÒ´¿˜§¡ª¹Ñè ¹Õ¨é Ф׹¤‹Ò 0 ¡ÅѺÁÒ

µÑÇÍ‹ҧ·Õè 11-2 I2C_Start();

11.1.3 ¿˜§¡ª¹Ñè I2C_Repeated_Start ¿˜§¡ª¹Ñè ¹Õ¨é Ðʋ§ÊÑ­­Ò³àÃÔÁè µŒ¹ËÃ×Í Start ¤ÃÑ§é ·Õè 2 ÍÍ¡ä» â´ÂÂѧäÁ‹Á¡Õ ÒÃʋ§ÊÑ­­Ò³ËÂØ´ËÃ×Í Stop ÃٻẺ¡ÒÃ㪌§Ò¹¤íÒÊѧè void I2C_Repeated_Start(void);

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

11.1.4 ¿˜§¡ª¹Ñè I2C_Is_Idle ໚¹¿˜§¡ª¹Ñè ·´ÊͺÃкººÑÊ I2C Ç‹Ò Ç‹Ò§¾ÃŒÍÁ·ÕÃè ºÑ Ê‹§¢ŒÍÁÙÅÍÍ¡ä»ËÃ×ÍäÁ‹ ÃٻẺ¡ÒÃ㪌§Ò¹¤íÒÊѧè unsigned short I2C_Is_Idle(void);

¡Òä׹¤‹Ò

¶ŒÒºÑÊNjҧ ¨Ð¤×¹¤‹Ò “1” ¡ÅѺÁÒ ¡Ã³Õ͹×è æ ¨Ð¤×¹¤‹Ò “0”

µÑÇÍ‹ҧ·Õè 11-4 if (I2C_Is_Idle()) { I2C_Start(); ... }


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 173

11.1.5 ¿˜§¡ª¹Ñè I2C_Rd

㪌Íҋ ¹¢ŒÍÁÙÅ¢¹Ò´ 1 亵¨Ò¡ÍØ»¡Ã³ÊàÅ¿ã¹ÃкººÑÊ I2C â´Â¨ÐµŒÍ§ÁÕ¡ÒÃʋ§ÊÑ­­Ò³ ÃѺÃٌ (Acknowledge : ACK) ËÃ×Í äÁ‹ÃºÑ Ãٌ (Not Acknowledge : NACK) ÍÍ¡ä»´ŒÇ ¡‹Í¹ãªŒ§Ò¹¿˜§¡ª¹Ñè ¹Õé ¨ÐµŒÍ§àÃÕ¡㪌§Ò¹¿˜§¡ª¹Ñè I2C_Init áÅÐ I2C_Start ¡‹Í¹ ÃٻẺ¡ÒÃ㪌§Ò¹¤íÒÊѧè unsigned short I2C_Rd(unsigned short ack);

µÑÇá»Ã ack

¶ŒÒ໚¹ “0” ¨ÐäÁ‹Ê§‹ Acknowledge ¶ŒÒ໚¹¤‹ÒÍ×¹è æ ¨Ðʋ§ÊÑ­­Ò³ÃѺÃÙˌ Ã×Í Acknowledge

µÑÇÍ‹ҧ·Õè 11-5 unsigned short take; ... take = I2C_Rd(0);

11.1.6 ¿˜§¡ª¹Ñè I2C_Wr ໚¹¿˜§¡ªÑè¹ÊíÒËÃѺʋ§¢ŒÍÁÙÅ¢¹Ò´ 1 亵ÍÍ¡ä»ÂѧÃкººÑÊ I2C ¡‹Í¹ãªŒ§Ò¹¿˜§¡ªÑè¹¹Õé¨Ð µŒÍ§àÃÕ¡㪌§Ò¹¿˜§¡ª¹Ñè I2C_Init áÅÐ I2C_Start ¡‹Í¹ ÃٻẺ¡ÒÃ㪌§Ò¹¤íÒÊѧè unsigned short I2C_Wr(unsigned short data);

µÑÇá»Ã data

¤×Í¢ŒÍÁÙÅ¢¹Ò´ 1 亵·µÕè ͌ §¡ÒÃà¢Õ¹ä»ÂѧÃкººÑÊ I2C

¡Òä׹¤‹Ò

¤×¹¤‹Ò “0” ¶ŒÒäÁ‹Á¢Õ ͌ ¼Ô´¾ÅÒ´à¡Ô´¢Ö¹é

µÑÇÍ‹ҧ·Õè 11-6 I2C_Write(0xA3);

11.1.7 ¿˜§¡ª¹Ñè I2C_Stop ໚¹¿˜§¡ª¹Ñè ÊíÒËÃѺʋ§ÊÑ­­Ò³ Stop ãˌ¡ºÑ ÃкººÑÊ I2C ÃٻẺ¡ÒÃ㪌§Ò¹¤íÒÊѧè void I2C_Stop(void);

µÑÇÍ‹ҧ·Õè 11-7 I2C_Stop();


174 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

¡Ãͺá¡·Õè 11-1 : ¤ÇÒÁÃÙàŒ º×Íé §µŒ¹¢Í§ÃкººÑÊ I2C I2C ‹ÍÁÒ¨Ò¡ Inter-IC Communication ËÁÒ¶֧ ¡ÒõԴµ‹ÍÊ×Íè ÊÒÃÃÐËNjҧäÍ«Õ â´ÂºÑÊ I2C ä´ŒÃºÑ ¡ÒþѲ¹Ò¢Ö¹é â´Â¿Å»Ô ʏ (Philips) ´ŒÇ¨شÁا‹ ËÁÒÂËÅÑ¡¤×Í µŒÍ§¡ÒÃãˌäÍ«ÕËÃ×ÍâÁ´ÙÅÊÒÁÒöµÔ´µ‹Í ÊÑ§è §Ò¹ áÅФǺ¤ØÁÀÒÂ㵌ÊÒÂÊÑ­­Ò³ à¾Õ§ 2 àʌ¹ àʌ¹Ë¹Ö§è ¤×Í ÊÒ¢ŒÍÁÙÅ ÍÕ¡àʌ¹Ë¹Ö§è ¤×Í ÊÒÂÊÑ­­Ò³¹Ò́¡Ò·Õãè ªŒã¹¡ÒáíÒ˹´¨Ñ§ËÇСÒ÷íÒ§Ò¹ ¡Òõ‹ÍËÇÁ ¡Ñ¹¢Í§ÍØ»¡Ã³º¹ºÑÊ I2C ·íÒ䴌§Ò‹ ÂÁÒ¡ à¾Õ§µ‹ÍÊÒ¢ŒÍÁÙÅáÅÐÊÒÂÊÑ­­Ò³¹Ò́¡Ò¢Í§ÍØ»¡Ã³áµ‹ÅеÑÇ¢¹Ò¹¡Ñ¹ä» ʋǹ ¡ÒáíÒ˹´áÍ´à´ÃÊÊíÒËÃѺµÔ´µ‹ÍÍØ»¡Ã³áµ‹ÅеÑÇ ¨Ð㪌ÃËÑÊ¢ŒÍÁÙÅËÇÁ¡Ñº¡ÒáíÒ˹´ÊÀÒÇÐÅͨԡ·Õ¢è ÒáÍ´à´ÃÊ ÊÒ¢ŒÍÁÙź¹ºÑÊ I2C ÁÕªÍ×è àÃÕ¡Í‹ҧ໚¹·Ò§¡ÒÃÇ‹Ò ÊÒ¢ŒÍÁÙÅ͹ءÃÁ ËÃ×Í SDA (Serial Data line) ʋǹÊÒ ÊÑ­­Ò³¹Ò́¡ÒÁÕªÍ×è àÃÕÂ¡Ç‹Ò ÊÒÂÊÑ­­Ò³¹Ò́¡Ò͹ءÃÁ ËÃ×Í SCL (Serial Clock line) 㹡ÒÃ͸ԺÒµ‹Í仹ըé Ð àÃÕ¡ÊÒÂÊÑ­­Ò³·Ñ§é ÊÍ§Ç‹Ò ÊÒ SDA áÅÐ SCL

¤Ø³ÊÁºÑµâÔ ´Â·ÑÇè 仢ͧºÑÊ I2C ÊÒ SDA áÅÐ SCL ໚¹ÊÒÂÊÑ­­Ò³ 2 ·ÔÈ·Ò§ (bi-directional line) µŒÍ§ÁÕ¡Òõ‹ÍµÑǵŒÒ¹·Ò¹¾ÙÅÍÑ»¡Ñºáç´Ñ¹ +5V änjµÅÍ´àÇÅÒ à¾×Íè ãˌÊÒÂÁÕʶҹÐÅͨԡÊ٧㹢³Ð·Õäè Á‹Á¡Õ ÒõԴµ‹Í㪌§Ò¹ ·Ñ§é Âѧª‹ÇÂ㹡Òû‡Í§¡Ñ¹ÊÑ­­Ò³Ãº¡Ç¹·Õè ÍÒ¨ÁÕࢌÒÁÒã¹ÊÒÂÊÑ­­Ò³·Ñ§é Êͧǧ¨ÃàÍÒµ¾µØ ¢Í§ÍØ»¡Ã³·µÕè ͋ ÍÂÙº‹ ¹ºÑÊ I2C µŒÍ§ÁÕÅ¡Ñ É³Ð໚¹Ç§¨Ãà´Ã¹à»´ (opendrain) ËÃ×ͤÍÅàÅç¡àµÍÏແ´ (open-collector) ´Ñ§áÊ´§ÃÒÂÅÐàÍÕ´ã¹ÃÙ»·Õè A3-1 ÍѵÃÒ¡Òö‹Ò·ʹ¢ŒÍÁÙź¹ºÑÊ I2C ÊÙ§¶Ö§ 100 ¡ÔâźԵµ‹ÍÇÔ¹Ò·Õã¹âËÁ´»¡µÔ (standard mode) áÅÐÊÙ§¶Ö§ 400 ¡ÔâźԵµ‹ÍÇÔ¹Ò·Õã¹âËÁ´¤ÇÒÁàÃçÇÊÙ§ (fast mode) (µÒÁ¤Ø³ÊÁºÑµ·Ô ҧ෤¹Ô¤·Õè Philips ¡íÒ˹´) ÍØ»¡Ã³·µÕè ͋ ËÇÁÍÂÙº‹ ¹ ºÑÊ I2C ¨ÐµŒÍ§Áդҋ ¤ÇÒÁ¨Øä¿¿‡ÒÃÇÁ·Õàè ¡Ô´¢Ö¹é ÃÐËNjҧÊÒ SDA áÅÐ SCL äÁ‹à¡Ô¹ 400pF ¡ÒÃࢌҶ֧ÍØ»¡Ã³º¹ºÑÊ I2C 㪌¢ÍŒ ÁÙÅÊíÒËÃѺ¡ÒÃࢌҶ֧ 2 ¤‹Ò¤×Í 7 ºÔµ (7-bit addressing) ËÃ×Í 10 ºÔµ (10-bit addressing)

ËÅÑ¡¡ÒâͧºÑÊ I2C ºÑÊ I2C »ÃСͺ´ŒÇÂÊÒÂÊÑ­­Ò³ 2 àʌ¹ ´Ñ§·Õäè ´Œ¡Å‹ÒÇÁÒáŌǤ×Í SDA áÅÐ SCL ÍØ»¡Ã³·µÕè ͋ ¾‹Ç§º¹ºÑÊÊÒÁÒö ÁÕ䴌ÁÒ¡ÁÒ ´Ñ§¹Ñ¹é ¨Ö§µŒÍ§ÁÕ¡ÒáíÒ˹´ÃٻẺ¢Í§¡ÒõԴµ‹Íº¹ºÑÊ ËÃ×ÍàÃÕÂ¡Ç‹Ò â»ÃⵤÍÅ (protocol) à¾×Íè ãˌ¼ãٌ ªŒ§Ò¹ ·ÃÒºÇ‹Ò ¢³Ð¹ÕÍé »Ø ¡Ã³ã´µÔ´µ‹Í¡Ñ¹ÍÂً áÅÐÍØ»¡Ã³µÇÑ ã´à»š¹µÑÇÃѺËÃ×͵ÑÇʋ§ µ‹Í仹ըé ТÍ͸ԺÒÂÅѡɳР˹ŒÒ·Õè áÅйÔÂÒÁ ¢Í§ÍØ»¡Ã³·µÕè ͋ ÍÂÙº‹ ¹ºÑÊ I2C à¾×Íè ໚¹¢ŒÍµ¡Å§¾×¹é °Ò¹¡‹Í¹·Õ¨è Ð͸ԺÒ¡Ò÷íÒ§Ò¹¢Í§ºÑÊ I2C µ‹Íä» ÍØ»¡Ã³·àÕè »š¹¼Ùʌ Ìҧ¢ŒÍÁÙÅËÃ×Íʋ§¢ŒÍÁÙÅ àÃÕÂ¡Ç‹Ò µÑÇʋ§ (transmitter) ÍØ»¡Ã³·àÕè »š¹¼ÙÌ ºÑ ¢ŒÍÁÙÅ àÃÕÂ¡Ç‹Ò µÑÇÃѺ (receiver) ã¹ÍØ»¡Ã³º¹ºÑÊ I2C ÊÒÁÒö໚¹ä´Œ·§Ñé µÑÇÃѺáÅеÑÇʋ§ ºÒ§ÍØ»¡Ã³·Òí ˹ŒÒ·Õàè »š¹µÑÇÃѺà¾Õ§Í‹ҧà´ÕÂÇ ¨ÐäÁ‹ÁÕ ÍØ»¡Ã³ã´º¹ºÑÊ I2C ·Õ·è Òí ˹ŒÒ·Õàè »š¹µÑÇʋ§à¾Õ§Í‹ҧà´ÕÂÇ ÍØ » ¡Ã³ ·Õè ·í Ò Ë¹Œ Ò ·Õè ¤ Ǻ¤Ø Á ¨Ñ § ËÇСÒ÷í Ò §Ò¹ ËÃ×Í¡ÒõԴµ‹Íº¹ºÑÊ I2C àÃÕÂ¡Ç‹Ò ÁÒÊàµÍÏ (master) ÍØ»¡Ã³·Õè¶Ù¡¤Çº¤ØÁËÃ×ÍÍØ»¡Ã³·Õ赋;‹Ç§à¢ŒÒä» º¹ºÑÊ I2C àÃÕÂ¡Ç‹Ò ÊàÅ¿ (slave) ¢ŒÍ¡íÒ˹´¢Í§¡ÒõԴµ‹Íº¹ºÑÊ I2C ¤×Í (1) ¡Òö‹Ò·ʹ¢ŒÍÁÙŨÐà¡Ô´¢Ö¹é àÁ×Íè ºÑÊNjҧ෋ҹѹé (2) ã¹ÃÐËNjҧ¡Òö‹Ò·ʹ¢ŒÍÁÙÅ àÁ×Íè ã´¡çµÒÁ·ÕÊè Ò ÍØ»¡Ã³ ºÑÊ µÑÇ·Õè 1 ÍØ»¡Ã³ ºÑÊ µÑÇ·Õè 2 SCL ÁÕʶҹÐ໚¹ÅͨԡÊÙ§ ÊÒ¢ŒÍÁÙŵŒÍ§ÃÑ¡ÉÒ¢ŒÍÁÙÅänj ÍÂ‹Ò ÃÙ»·Õè A11-1 â¤Ã§ÊÌҧǧ¨Ã·Ò§àÍÒµ¾µØ ¢Í§ÍØ»¡Ã³ ãˌà¡Ô´¡ÒÃà»ÅÕÂè ¹á»Å§¢Ö¹é à´ç´¢Ò´ Áԩйѹé ÊÑ­­Ò³·Õàè ¡Ô´¢Ö¹é ¨Ðä´ŒÃºÑ ¡ÒÃá»Å¤ÇÒÁËÁÒÂ໚¹ÊÑ­­Ò³¤Çº¤ØÁá·¹ ÃкººÑÊ I2C +5V

R2

SDA (Serial Data line)

R2

SCL (Serial Clock line)

SCLKN1 OUT

DATAN1 OUT

SCLKN2 OUT

DATAN2 OUT

SCLK IN

DATA IN

SCLK IN

DATA IN

IC

IC


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 175

ÊÀÒÇзÕàè ¡Ô´¢Ö¹é º¹ºÑÊ I2C Áմnj ¡ѹ 5 ÊÀÒÇÐ ´Ñ§¹Õé (1) ºÑÊNjҧ (Bus not busy) ÊÀÒÇйÕàé ¡Ô´¢Ö¹é àÁ×Íè ʶҹÐÅͨԡº¹ÊÒ SDA áÅÐ SCL ໚¹ÅͨԡÊÙ§·Ñ§é ¤Ù‹ ¹Ñ¹è ËÁÒ¤ÇÒÁÇ‹Ò ¡Òö‹Ò·ʹ¢ŒÍÁÙÅÊÒÁÒöàÃÔÁè µŒ¹¢Ö¹é 䴌 (2) àÃÔÁè µŒ¹¡Òö‹Ò·ʹ¢ŒÍÁÙÅ (start data transfer) à¡Ô´¢Ö¹é àÁ×Íè ÊÒ SDA ÁÕ¡ÒÃà»ÅÕÂè ¹á»Å§ÃдѺÅͨԡ¨Ò¡ Ê٧仵èÒí ã¹¢³Ð·ÕÊè Ò SCL ÁÕʶҹÐÅͨԡÊÙ§ àÃÕ¡ÊÀÒÇзÕàè ¡Ô´¢Ö¹é ¹ÕÇé ҋ ÊÀÒÇÐàÃÔÁè µŒ¹ (START) (3) ¢ŒÍÁÙÅ´íÒçÍÂÙº‹ ¹ºÑÊ (data valid) ÊÀÒÇйÕàé ¡Ô´¢Ö¹é ¶Ñ´¨Ò¡ÊÀÒÇÐàÃÔÁè µŒ¹ â´ÂʶҹÐÅͨԡ·Õàè ¡Ô´¢Ö¹é º¹ÊÒ SDA ¡ç¤Í× ¢ŒÍÁÙÅ·Õ·è Òí ¡Òö‹Ò·ʹ àÁ×Íè ÊÒ SCL ໚¹ÅͨԡÊÙ§ ʶҹзÕÊè Ò SDA µŒÍ§¤§·Õè à¾×Íè ãËŒÍ»Ø ¡Ã³ÃºÑ ÃÙ¢Œ ͌ ÁÙÅã¹ ¨Ñ§ËÇйѹé Ç‹Ò à»š¹ “0” ËÃ×Í “1” ¢ŒÍÁÙÅÍÒ¨à¡Ô´¡ÒÃà»ÅÕÂè ¹á»Å§ä´Œã¹¢³Ð·ÕÊè Ò SCL ໚¹ÅͨԡµèÒí ᵋàÁ×Íè ã´¡çµÒÁ·Õµè ͌ § ¡ÒÃãˌà¡Ô´¡Òö‹Ò·ʹ¢ŒÍÁÙÅÍ‹ҧÊÁºÙó ʶҹÐÅͨԡ·Õ¢è Ò SDA µŒÍ§¤§·Õµè ÅÍ´ª‹Ç§àÇÅÒ·ÕÊè Ò SCL ÁÕʶҹÐÅͨԡÊÙ§ ËÒ¡à¡Ô´¡ÒÃà»ÅÕÂè ¹á»Å§Ê¶Ò¹ÐÅͨԡ㹢³Ð·ÕÊè Ò SCL ÁÕÅͨԡÊÙ§ÍÂÙ¹‹ ¹Ñé ÍØ»¡Ã³ÁÒÊàµÍÏ··Õè Òí ¡ÒäǺ¤ØÁ¡Òö‹Ò·Í´ ¢ŒÍÁÙŨÐá»Å¤ÇÒÁËÁÒÂ໚¹ÊÀÒÇÐËÂØ´ËÃ×ÍÊÀÒÇÐàÃÔÁè µŒ¹¡ç䴌 ·íÒãˌ¢ÍŒ ÁÙÅ·Õ·è Òí ¡Òö‹Ò·ʹ¹Ñ¹é à¡Ô´¤ÇÒÁ¼Ô´¾ÅÒ´¢Ö¹é (4) ÃѺÃÙ¢Œ ͌ ÁÙÅ (acknowledge) à¡Ô´¢Ö¹é ËÅѧ¨Ò¡·Õ¡è Òö‹Ò·ʹ¢ŒÍÁÙŨҡµÑÇʋ§ÁÒÂѧµÑÇÃѺà¡Ô´¢Ö¹é Í‹ҧÊÁºÙó â´ÂµÑÇʋ§¨Ð·íÒ¡ÒÃʋ§¢ŒÍÁÙÅÁÒ 1 ºÔµàÃÕÂ¡Ç‹Ò ºÔµÃѺÃٌ (acknowledge bit) ÁÕʶҹÐ໚¹ÅͨԡÊÙ§ ËÅѧ¨Ò¡Ê‹§¢ŒÍÁÙÅÁҤú ¶ŒÇ¹ ʋǹÍØ»¡Ã³ÁÒÊàµÍϨзíÒ¡ÒÃʋ§ÊÑ­­Ò³ÃѺÃÙ¾Œ àÔ ÈÉ«Ö§è ÊÑÁ¾Ñ¹¸¡ºÑ ÊÑ­­Ò³¹Ò́¡Ò ÍØ»¡Ã³ÊàÅ¿·Õ¶è ¡Ù ͌ҧ¶Ö§ã¹¡Òà µÔ ´ µ‹ Í ËÃ× Í ¡í Ò ÅÑ § µÔ ´ µ‹ Í ÍÂً ã ¹¢³Ð¹Ñé ¹ ¡ç ¨ Сí Ò à¹Ô ´ ºÔ µ ÃÑ º Ãٌ ·Õè ÁÕ Ê ¶Ò¹ÐÅÍ¨Ô ¡ µèí Ò à¾×è Í µÍºÊ¹Í§ãˌ · ÃҺNj Ò ä´Œ ÃѺ¢ŒÍÁÙÅã¹áµ‹ÅÐ亵àÃÕºÌÍÂáÅŒÇ (5) ËÂØ´¡Òö‹Ò·ʹ¢ŒÍÁÙÅ (stop data transfer) à¡Ô´¢Ö¹é àÁ×Íè ÊÒ SDA ÁÕ¡ÒÃà»ÅÕÂè ¹á»Å§ÃдѺÅͨԡ¨Ò¡µèÒí ä»ÊÙ§ ã¹¢³Ð·ÕÊè Ò SCL ÁÕʶҹÐÅͨԡÊÙ§ àÃÕ¡ÊÀÒÇзÕàè ¡Ô´¢Ö¹é ¹ÕÇé ҋ ÊÀÒÇÐËÂØ´ (STOP) ã¹ÃÙ»·Õè A11-2 ໚¹ä´ÍÐá¡ÃÁàÇÅÒ·Õáè Ê´§¶Ö§¡ÒÃà¡Ô´ÊÀÒÇе‹Ò§æ º¹ºÑÊ I2C äÁ‹Çҋ ¨Ð໚¹ÊÀÒÇкÑÊNjҧ,àÃÔÁè µŒ¹, ¶‹Ò·ʹ¢ŒÍÁÙÅ, ÃѺÃٌ áÅÐËÂØ´¡Òö‹Ò·ʹ¢ŒÍÁÙÅ

¡Ò÷íÒ§Ò¹º¹ºÑÊ I2C ¡‹Í¹·Õ¨è ÐàÃÔÁè µŒ¹¡Òö‹Ò·ʹ¢ŒÍÁÙÅÃÐËNjҧÍØ»¡Ã³µÒ‹ §æ ·Õµè ͋ ÍÂÙº‹ ¹ºÑÊ µŒÍ§ÁÕ¡ÒÃ͌ҧ¶Ö§ÍØ»¡Ã³àÊÕ¡‹Í¹ â´Â¡Òà ͌ҧ¶Ö§ÍØ»¡Ã³º¹ºÑÊ I2C ¹Ñ¹é ¨Ð㪌¡ÒÃ͌ҧ¶Ö§áºº 7 ºÔµËÃ×Í 10 ºÔµ 㹡óշÁÕè ÍÕ »Ø ¡Ã³µÍ‹ ÍÂÙº‹ ¹ºÑÊäÁ‹ÁÒ¡ 㪌¡ÒÃ͌ҧ¶Ö§ Ẻ 7 ºÔµ¡çà¾Õ§¾Í ᵋ¶ÒŒ ÁÕÍ»Ø ¡Ã³µÍ‹ ÍÂÙº‹ ¹ºÑÊÁÒ¡¡Ç‹Ò 127 áÍ´à´ÃÊ ¨íÒ໚¹µŒÍ§ãªŒ¡ÒÃ͌ҧ¶Ö§áºº 10 ºÔµ ËÅѧ¨Ò¡ ·Õµè ´Ô µ‹ÍÍØ»¡Ã³áµ‹ÅеÑÇ䴌àÃÕºÌÍÂáÅŒÇ ¡ç¨ÐàÃÔÁè µŒ¹¡Òö‹Ò·ʹ¢ŒÍÁÙšѹµ‹Íä» SDA à»ÅÕÂè ¹¨Ò¡ "1" ໚¹ "0" ã¹¢³Ð·Õè SCL ໚¹ "1" ¹Õ¤è ×Í ÊÀÒÇÐàÃÔèÁµŒ¹

SDA à»ÅÕÂè ¹¨Ò¡ "0" ໚¹ "1" ã¹¢³Ð·Õè SCL ໚¹ "1" ¹Õ¤è ×Í ÊÀÒÇÐËÂØ´

SDA

SCL

SDA ໚¹ "1" SCL ໚¹ "1" ÊÀÒÇкÑÊNjҧ

SDA ¤§·Õ·è ÅèÕ Í¨Ô¡ "0" SDA ¤§·Õ·è ÅèÕ Í¨Ô¡ "1" ã¹¢³Ð·Õè SCL ໚¹ "1" ¶×Í໚¹ ã¹¢³Ð·Õè SCL ໚¹ "1" ¶×Í໚¹ ¡Òö‹Ò·ʹ¢ŒÍÁÙÅ "0" ¡Òö‹Ò·ʹ¢ŒÍÁÙÅ "1"

ÃÙ»·Õè A11-2 ä´ÍÐá¡ÃÁàÇÅÒáÊ´§Ê¶Ò¹Ðµ‹Ò§æ º¹ÃкººÑÊ I2C


176 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

ºÔµ 7

ºÔµ 6

ºÔµ 5

ºÔµ 4

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

ºÔµ 3

ºÔµ 2

ºÔµ 1

¡ÒÃ͌ҧ¶Ö§áºº 7 ºÔµ (7-bit addressing)

ºÔµ 0

¢ŒÍÁÙÅ亵áá·Õèà¡Ô´¢Öé¹ËÅѧ¨Ò¡ÊÀÒÇÐàÃÔèÁµŒ¹¤×Í ºÔµ¡íÒ˹´áÍ´à´Ãʤ§·Õè ¢ŒÍÁÙÅ·Õè㪌㹡ÒÃ͌ҧ¶Ö§ÍØ»¡Ã³·Õ赌ͧ¡ÒõԴµ‹Í â´ÂÁÕÃÙ» ẺáÊ´§ã¹ÃÙ»·Õè A11-3 ã¹ 7 ºÔµº¹ÃÇÁ·Ñ§é ºÔµ MSB ´ŒÇ ÃÙ»·Õè A11-3 ÃٻẺ¢ŒÍÁÙ��㹡ÒÃ͌ҧáÍ´à´ÃÊ ¨Ð໚¹¢ŒÍÁÙÅáÍ´à´ÃʢͧÍØ»¡Ã³ÊàÅ¿·Õµè ͌ §¡ÒõԴµ‹Í â´Â ẋ§à»š¹ ºÔµ¡íÒ˹´áÍ´à´Ãʤ§·Õè (fixed address bit) ¨íҹǹ 4 ºÔµ «Ö§è ¢ŒÍÁÙŹÕÍé »Ø ¡Ã³áµ‹ÅеÑǨж١¡íÒ˹´ÁÒ¨Ò¡¼ÙŒ ¼ÅÔµ äÁ‹ÊÒÁÒöà»ÅÕè¹á»Å§á¡Œä¢ä´Œ ¶Ñ´ÁÒÍÕ¡ 3 ºÔµà»š¹ºÔµ¡íÒ˹´áÍ´à´ÃÊ·ÕèÊÒÁÒöâ»Ãá¡ÃÁ䴌 (programmable address bit) â´Â¼Ù㌠ªŒ§Ò¹µŒÍ§¡íÒ˹´Ê¶Ò¹ÐÅͨԡãˌᡋ¢Ò A0-A2 ¢Í§ÍØ»¡Ã³·ÁÕè ¡Õ ÒÃàª×Íè Áµ‹ÍẺºÑÊ I2C ʋǹ㹺Ե LSB ໚¹ºÔµ·Õãè ªŒ¡Òí ˹´¡ÒÃ͋ҹËÃ×Íà¢Õ¹¢ŒÍÁÙšѺÍØ»¡Ã³ÊàÅ¿µÑǹѹé æ ËÒ¡ºÔµ LSB ໚¹ “0” ËÁÒ¶֧µŒÍ§¡ÒÃà¢Õ¹ ¢ŒÍÁÙÅä»ÂѧÍØ»¡Ã³¹¹Ñé ¶ŒÒ໚¹ “1” ¨Ð໚¹¡ÒÃ͋ҹ¢ŒÍÁÙŨҡÍØ»¡Ã³ÊàÅ¿ ¢ŒÍÁÙÅã¹äºµµÍ‹ ÁÒ¤×Í ¢ŒÍÁÙŤǺ¤ØÁ (control byte) ã¹ÍØ»¡Ã³áµ‹ÅеÑÇÁÕ¡ÒáíÒ˹´¢ŒÍÁÙŤǺ¤ØÁ·Õáè µ¡µ‹Ò§¡Ñ¹ ä» Â¡µÑÇÍ‹ҧ äÍ«Õ¢ÂÒ¾ÍϵÁբ͌ ÁÙŤǺ¤ØÁ·Õãè ªŒ¡Òí ˹´Ç‹Ò ºÔµã´à»š¹ÍÔ¹¾Øµ ºÔµã´à»š¹àÍÒµ¾µØ ã¹¢³Ð·Õäè Í«Õ ADC/ DAC µŒÍ§¡ÒâŒÍÁÙŤǺ¤ØÁà¾×Íè ¡íÒ˹´ãˌ·Òí §Ò¹à»š¹Ç§¨Ã ADC ËÃ×Í DAC ໚¹µŒ¹ ¢ŒÍÁÙÅã¹äºµµÍ‹ ÁÒ¤×Í ¢ŒÍÁÙÅ·Õ·è Òí ¡Òö‹Ò·ʹ¨ÃÔ§ (data) ËÅѧ¨Ò¡·ÕÁè ¡Õ Òö‹Ò·ʹ¢ŒÍÁÙÅã¹áµ‹ÅÐ亵 ÍØ»¡Ã³ÊàÅ¿·Õäè ´ŒÃºÑ ¡ÒõԴµ‹ÍµŒÍ§Ê‹§ÊÑ­­Ò³ÃѺÃÙµŒ ͺ¡ÅѺÁÒ´ŒÇ·ء ¤Ãѧé ã¹ÃÙ»·Õè A11-4 áÊ´§ÃٻẺ¢ŒÍÁÙÅ͹ءÃÁ·Õàè ¡Ô´¢Ö¹é 㹡ÒõԴµ‹Íº¹ºÑÊ I2C ¢Í§¡ÒÃ͌ҧ¶Ö§áºº 7 ºÔµ X

X

X

X

A2

A1

A0

R/W

ºÔµ¡íÒ˹´áÍ´à´ÃÊ â»Ãá¡ÃÁ䴌

¡ÒÃ͌ҧ¶Ö§áºº 10 ºÔµ ¨ÐÁբ͌ ÁÙÅà¾ÔÁè àµÔÁ¢Ö¹é ÁÒàÅ硹ŒÍ â´Âã¹äºµááËÅѧ¨Ò¡à¡Ô´ÊÀÒÇÐàÃÔÁè µŒ¹ µŒÍ§¡íÒ˹´ãˌ 5 ºÔµº¹Áբ͌ ÁÙÅ໚¹ 11110 ʋǹÍÕ¡ 2 ºÔµ¶Ñ´ÁÒ໚¹ºÔµáÍ´à´ÃʢͧÍØ»¡Ã³·µÕè ͌ §¡ÒõԴµ‹Í 㹺Ե LSB ¢Í§¢ŒÍÁÙÅ亵ááÂѧ¤§à»š¹¡ÒáíÒ˹´ Ç‹Ò µŒÍ§¡ÒÃ͋ҹËÃ×Íà¢Õ¹¢ŒÍÁÙšѺÍØ»¡Ã³ÊàÅ¿µÑÇ·Õµè ͌ §¡ÒõԴµ‹Í´ŒÇ ¢ŒÍÁÙÅ亵µÍ‹ ÁÒ໚¹¢ŒÍÁÙÅáÍ´à´ÃÊã¹äºµ·Õè 2 ¢Í§ÍØ»¡Ã³·µÕè ͌ §¡ÒõԴµ‹Í´ŒÇ ¢ŒÍÁÙÅ亵¶´Ñ 仨֧໚¹¢ŒÍÁÙŤǺ¤ØÁ ¢ŒÍÁÙÅËÅѧ¨Ò¡¹Ñ¹é ¡ç¨Ð໚¹¢ŒÍÁÙŨÃÔ§·Õãè ªŒã¹¡ÒõԴµ‹Í ઋ¹à´ÕÂǡѺ¡ÒÃ͌ҧ¶Ö§áºº 7 ºÔµ ËÅѧ¨Ò¡¶‹Ò·ʹ¢ŒÍÁÙŤú·Ø¡äºµ µŒÍ§ÁÕÊÀÒÇÐÃѺÃÙàŒ ¡Ô´¢Ö¹é à¾×Íè ãˌ¡Ãкǹ ¡Òö‹Ò·ʹ¢ŒÍÁÙÅÊÒÁÒö´íÒà¹Ô¹µ‹Íä»ä´Œ ã¹ÃÙ»·Õè A11-5 áÊ´§ÃٻẺ¢ŒÍÁÙÅ͹ءÃÁ¢Í§¡ÒÃ͌ҧ¶Ö§áºº 10 ºÔµ ¢ŒÍÁÙÅ亵·èÕ 1 àÅ×Í¡áÍ´à´Ãʢͧ ÍØ»¡Ã³·µèÕ ÍŒ §¡ÒõԴµ‹Í áÅШشÁا‹ ËÁÒÂ㹡Òà µÔ´µ‹Í (͋ҹËÃ×Íà¢Õ¹)

àÃÔÁè µŒ¹

A6-A0

¢ŒÍÁÙÅ亵·èÕ 2 ʋ§¢ŒÍÁÙŤǺ¤ØÁ¡Òà ·íÒ§Ò¹¢Í§ÍØ»¡Ã³ ·Õµè ͌ §¡ÒõԴµ‹ÍËÃ×Í control word R/W=0

ÃѺÃٌ

C6-C0

ʋ§ÁÒ¨Ò¡ÍØ»¡Ã³ÊàÅ¿·Õ·è Òí ¡ÒõԴµ‹Í´ŒÇÂ

¢ŒÍÁÙÅ亵µÍ‹ ä» (n 亵) ¢ŒÍÁÙŨÃÔ§·Õµè ͌ §¡Òö‹Ò·ʹ R/W

ÃѺÃٌ

ÃѺÃٌ

D7-D0

ËÂØ´

ʋ§ÁÒ¨Ò¡ÍØ»¡Ã³ÊàÅ¿·Õ·è Òí ¡ÒõԴµ‹Í´ŒÇÂ

ÃÙ»·Õè A11-4 ÃٻẺ¢ŒÍÁÙÅ㹡ÒÃ͌ҧ¶Ö§áºº 7 ºÔµ ¢ŒÍÁÙÅ亵·Õè 1 àÅ×Í¡¡ÒÃ͌ҧ¶Ö§áºº 10 ºÔµ (¢ŒÍÁÙŤ×Í 11110xx â´Â xx ¤×Í ºÔµáÍ´à´ÃÊ 2 ºÔµº¹)

àÃÔÁè µŒ¹

1 1 1 1 0 A9 A8

R/W=0 ÃѺÃٌ

¢ŒÍÁÙÅ亵·Õè 3 ʋ§¢ŒÍÁÙŤǺ¤ØÁ ¡Ò÷íÒ§Ò¹ ¢Í§ÍØ»¡Ã³·Õ赌ͧ¡Òà µÔ´µ‹ÍËÃ×Í control word

¢ŒÍÁÙÅ亵·Õè 2 ¢ŒÍÁÙÅáÍ´à´ÃÊ 8 ºÔµÅ‹Ò§

A7-A0

ÃѺÃٌ

ʋ§ÁÒ¨Ò¡ÍØ»¡Ã³ÊàÅ¿ ·Õè·íÒ¡ÒõԴµ‹Í´ŒÇÂ

ÃÙ»·Õè A11-5 ÃٻẺ¢ŒÍÁÙÅ㹡ÒÃ͌ҧ¶Ö§áºº 10 ºÔµ

C6-C0

¢ŒÍÁÙÅ亵µ‹Íä» (n 亵) ¢ŒÍÁÙŨÃÔ§·Õ赌ͧ¡Òà ¶‹Ò·ʹ

R/W

ÃѺÃٌ

D7-D0 ʋ§ÁÒ¨Ò¡ÍØ»¡Ã³ÊàÅ¿ ·Õè·íÒ¡ÒõԴµ‹Í´ŒÇÂ

ÃѺÃٌ ËÂØ´


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 177

»¯ÔºµÑ ¡Ô Ò÷Õè 20 µÔ´µ‹ÍºÍÏ´¢ÑºÃÕàŏ 16 ª‹Í§ ZX-RELAY16 ´ŒÇ¿˜§¡ª¹Ñè I2C ZX-RELAY16 ໚¹ºÍÏ´¢ÑºÃÕàŏ 16 ª‹Í§·Õ¤è Ǻ¤ØÁ¼‹Ò¹ÃкººÑÊ I2C â´Âº¹µÑǺÍϴ㪌äÍ«Õ¢ÂÒ¾Íϵ ¼‹Ò¹ÃкººÑÊ I2C àºÍÏ MCP23016 ·íÒ˹ŒÒ·Õ¤è Ǻ¤ØÁ¡Ò÷íÒ§Ò¹·Ñ§é ËÁ´ ÃÙ»·Õè L20-1 áÊ´§Ë¹ŒÒµÒ¢Í§ºÍÏ´ ZXRELAY16 ʋǹÃÙ»·Õè L20-2 ໚¹Ç§¨ÃÊÁºÙó¢Í§ºÍÏ´ ZX-RELAY16 à¹×Íè §¨Ò¡ãªŒäÍ«Õ··Õè Òí §Ò¹¼‹Ò¹ÃкººÑÊ I2C ¨Ö§·íÒãˌÊÒÁÒöµ‹Í¾‹Ç§ºÍÏ´¢ÑºÃÕàŏ ZX-RELAY16 ãˌ·Òí §Ò¹Ã‹ÇÁ¡Ñ¹ä´ŒÁÒ¡¶Ö§ 8 ºÍÏ´ â´Â㪌ÊÒÂÊÑ­­Ò³ SDA áÅÐ SCL à¾Õ§ªØ´à´ÕÂÇ áÅСíÒ˹´áÍ´à´ÃʢͧᵋÅкÍÏ´ãˌᵡµ‹Ò§¡Ñ¹´ŒÇ¡ÒÃàÅ×Í¡¨ÑÁê à»ÍϺ¹ºÍÏ´

¤Ø³ÊÁºÑµ¢Ô ͧºÍÏ´ ZX-RELAY16 l

㪌äÍ«Õ MCP23016 ¢Í§ Microchip ໚¹äÍ«Õ¢ÂÒ¾Íϵ 2 ·ÔÈ·Ò§ (ÍÔ¹¾Øµ/àÍÒµ¾µØ ) ¼‹Ò¹ÃкººÑÊ I2C

l

¢ÑºÃÕàŏ 12V 䴌 16 µÑÇ ¾ÃŒÍÁä¿áÊ´§Ê¶Ò¹Ð¡Ò÷íÒ§Ò¹

l

µ‹Í¾‹Ç§¡Ñ¹ä´Œ 8 ºÍÏ´ ·íÒãˌÊÒÁÒö¢ÑºÃÕàŏ䴌 128 µÑÇ´ŒÇÂÊÒÂÊÑ­­Ò³¤Çº¤ØÁà¾Õ§Êͧàʌ¹

¨Ø´µ‹Í˹ŒÒÊÑÁ¼ÑÊÃÕàŏ໚¹à·ÍÏÁ¹Ô ÍźÅçÍ¡ Áդú·Ñ§é ¨Ø´µ‹Í»¡µÔແ´Ç§¨Ã (NO) áÅл¡µÔµÍ‹ ǧ¨Ã (NC) ÊÒÁÒö㪌䢤ǧà¾×Íè µ‹ÍËÃ×ͻŴÊÒµ‹Í䴌§Ò‹  l

l

¾Ô¡´Ñ ˹ŒÒÊÑÁ¼ÑʢͧÃÕàŏ 220Vac/5A ¢ÑºâËŴ䴌ʧ٠ÊØ´ 600 ÇѵµµÍ‹ ª‹Í§

l

¨Ø´µ‹ÍÃкººÑÊ I2C Ẻ IDC µÑǼÙ/Œ µÑÇàÁÕÂáÅÐẺ JST 3 ¢Ò

l

¢¹Ò´ºÍÏ´ 15x8 ૹµÔàÁµÃ

¨ÑÁê à»ÍÏàÅ×Í¡áÍ´à´ÃʢͧºÍÏ´

¨Ø´µ‹ÍẺ IDC µÑǼÙ/Œ µÑÇàÁÕ SCL ¨Ø´µ‹ÍẺ JST SDA ¨Ø´µ‹ÍẺ JST

ÃÙ»·Õè L20-1 áÊ´§Ë¹ŒÒµÒ¢Í§ ZX-RELAY16 ºÍÏ´¢ÑºÃÕàŏ 16 ª‹Í§¼‹Ò¹ÃкººÑÊ I2C


178 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

15 SDA 14 SCL +5V

17 16

RY6

R10 1.5k LED6

RY7

R11 1.5k

9

LED7

IC1 Vss MCP23016 Vss

RY8

R12 1.5k LED8

R4 4.7k*4 18

LED5

RY9 R13 1.5k

A1

LED9

A0

R14 1.5k

RY10

LED10

INT IC3 ULN2803 1 GP1.0 2 GP1.1 3 GP1.2 4 GP1.3 10 GP1.4 11 GP1.5 12 GP1.6 13 GP1.7

R15 1.5k

+VL 10

1 I1 2 I2 3 I3 4 I4 5 I5 6 I6 7 I7 8 I8

RY11

LED11

18 Q1 17 Q2 16 Q3 15 Q4 14 Q5 13 Q6 12 Q7 11 Q8

R16 1.5k LED12 R17 1.5k

RY12

RY13

LED13 R18 1.5k LED14 R19 1.5k

9

LED15 R20 1.5k

RY14

RY15

RY16

LED16

J1 DC INPUT 12Vdc

D5 1N5819

SW1 POWER ON / OFF

D6 1N4001

K21 Supply +12Vdc

IC4 7805

D1-D4 1N4001 x4

C3 220/25V

NO C NC NO C NC NO C NC

K6 RELAY2 K7 RELAY3

+12V A2

JP3 JP2 JP1 A2 A1 A0 6

NO C NC

C4 0.1/63V

C5 47/16V

+VL C7 220/16V

C6 0.1/ 63V

+5V

ÃÙ»·Õè L20-2 ǧ¨ÃÊÁºÙó¢Í§ºÍÏ´ ZX-RELAY16 «Ö§è ÁÕäÍ«ÕàºÍÏ MCP23016 ໚¹ËÑÇã¨

NO C NC NO C NC NO C NC NO C NC NO C NC NO C NC

K13 RELAY9

8 19

+5V

RY5

K14 RELAY10

K3 SCL

LED4 R9 1.5k

NO C NC

K15 RELAY11

C2 33pF

IC2 ULN2803

CLK

RY4

R8 1.5k

K16 RELAY12

9

18 Q1 17 Q2 16 Q3 15 Q4 14 Q5 13 Q6 12 Q7 11 Q8

K17 RELAY13

R3 3.9k

K2 SDA

1 I1 2 I2 3 I3 4 I4 5 I5 6 I6 7 I7 8 I8

K18 RELAY14

R2 3.9k

10 21 GP0.0 22 GP0.1 23 GP0.2 24 GP0.3 25 GP0.4 26 GP0.5 27 GP0.6 28 GP0.7

NO C NC

NO C NC

K19 RELAY15

R1 4.7k

+5V SDA SCL GND

20 Vdd

LED3

NO C NC

K20 RELAY16

K1A/1B IC EXPANSION

RY3

R7 1.5k

+VL

NO C NC

K8 RELAY4

C1 0.1F

R21 510

LED2

K9 RELAY5

+5V

LED21 POWER

K10 RELAY6

RY2

R6 1.5k

NO C NC

K11 RELAY7

LED1

K12 RELAY8

RY1 R5 1.5k

K5 RELAY1

+VL


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 179

MCP23016 äÍ«Õ¢ÂÒ¾ÍϵÍÔ¹¾ØµàÍÒµ¾µØ ¼‹Ò¹ÃкººÑÊ I2C MCP23016 ໚¹äÍ«Õ¢ÂÒ¾ÍϵÍÔ¹¾ØµàÍÒµ¾µØ ¢¹Ò´ 16 ºÔµ ¼‹Ò¹ÃкººÑÊ I2C ·ÕÊè ÒÁÒö¡íÒ˹´ãˌ ໚¹ÍÔ¹¾ØµËÃ×ÍàÍÒµ¾µØ 䴌Í‹ҧÍÔÊÃÐ â´ÂÁÕ¤³ Ø ÊÁºÑµ·Ô ¹Õè ҋ ʹ㨴ѧ¹Õé

¢Ò¾Íϵ໚¹áºº 2 ·ÔÈ·Ò§ ¤Çº¤ØÁ໚¹ÍÔ¹¾ØµËÃ×ÍàÍÒµ¾µØ 䴌µÅÍ´àÇÅÒ ÊÀÒÇÐàÃÔÁè µŒ¹à»š¹ÍÔ¹¾Øµ l Ê×Í è ÊÒôŒÇÂÃкººÑÊ I2C ´ŒÇÂâËÁ´¤ÇÒÁàÃçÇÊ٧䴌 l ÁÕ¢ÒàÅ×Í¡áÍ´à´ÃÊ 3 ¢Ò à¾×Í è ãˌÊÒÁÒöµ‹Í¾‹Ç§äÍ«Õ䴌 8 µÑÇâ´Â㪌ºÊÑ I2C à´ÕÂǡѹ l ¢Ò¾ÍϵÊÒÁÒö¢Ñº¡ÃÐáÊ·Ñ§é «ÍÏÊáÅЫԧ¡ä´Œ 25mA µ‹Í¢Ò l ÁÕ¢ÒÍÔ¹àµÍÏû Ñ µáººà´Ã¹à»´ ·íÒ§Ò¹àÁ×Íè ÁÕ¡ÒÃà»ÅÕÂè ¹á»Å§·Õ¢è Ò¾ÍϵÍÔ¹¾Øµ l ÁÕè Õ ÊÔ àµÍÏÊÒí ËÃѺà¡çº¤‹Ò¢Ò¾ÍϵÍÔ¹¾Øµ·Õàè ¡Ô´ÍÔ¹àµÍÃÃ»Ñ µ l ÊÒÁÒöàª×Í è Áµ‹Í¡ÑºäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏÂÍ´¹ÔÂÁ䴌·¡Ø µÑÇ l

¢Ñ¹é µÍ¹¡ÒõԴµ‹Í¡ÑººÍÏ´ ZX-RELAY16 ¡ÒõԴµ‹Í¡ÑººÍÏ´ ZX-RELAY16 ¡ç¤Í× ¡ÒõԴµ‹Í¡ÑºäÍ«Õ MCP23016 ¹Ñ¹è àͧ â´ÂµŒÍ§à¢Õ¹â»Ãá¡ÃÁà¾×Íè ¡íÒ˹´ãˌ MCP23016 ·íÒ§Ò¹à¾×Íè ¡íÒ˹´ãˌ¾Íϵ·Ñ§é ËÁ´à»š¹àÍÒµ¾µØ ¨Ò¡¹Ñ¹é ¨Ö§Ê‹§¢ŒÍÁÙÅÍÍ¡ä»Âѧ¢Ò¾Íϵ·Ñ§é ËÁ´¡ç¨Ð·íÒãˌÊÒÁÒö¤Çº¤ØÁÃÕàŏ·§Ñé 16 µÑÇ䴌 ÁÕ¢¹Ñé µÍ¹¡ÒõԴµ‹Íâ´ÂÊÃØ»´Ñ§¹Õé (1) ʋ§¤íÒÊÑ§è µÔ´µ‹Í¡ÑºäÍ«Õ MCP23016 à¾×Íè ¡íÒ˹´à»š¹âËÁ´à¢Õ¹¢ŒÍÁÙÅ (2) ʋ§¤íÒÊѧè à¾×Íè µÔ´µ‹Í¡ÑºÃÕ¨ÊÔ àµÍÏ IODIR0 (áÍ´à´ÃÊ 0x06) (3) ¡íÒ˹´ãˌ¢Ò¾Íϵ 8 ºÔµÅ‹Ò§à»š¹àÍÒµ¾µØ â´Âà¢Õ¹¤‹Ò “0” ãˌ¡ºÑ ·Ø¡ºÔµ¢Í§ÃÕ¨ÊÔ àµÍÏ IODIR0 (4) ʋ§¤íÒÊѧè à¾×Íè µÔ´µ‹Í¡ÑºÃÕ¨ÊÔ àµÍÏ IODIR1 (áÍ´à´ÃÊ 0x07) (5) ¡íÒ˹´ãˌ¢Ò¾Íϵ 8 ºÔµº¹ ໚¹àÍÒµ¾µØ â´Âà¢Õ¹¤‹Ò “0” ãˌ¡ºÑ ·Ø¡ºÔµ¢Í§ÃÕ¨ÊÔ àµÍÏ IODIR1 (6) ʋ§¤íÒÊѧè à¾×Íè µÔ´µ‹Íʋ§¢ŒÍÁÙšѺÃÕ¨ÊÔ àµÍϢͧ¢Ò¾Íϵ亵Åҋ §·Õáè Í´à´ÃÊ 0x00 (7) ʋ§¤íÒÊѧè à¾×Íè µÔ´µ‹Íʋ§¢ŒÍÁÙšѺÃÕ¨ÊÔ àµÍϢͧ¢Ò¾Íϵ亵º¹·Õáè Í´à´ÃÊ 0x01 ¨Ò¡¢Ñ¹é µÍ¹¡ÒõԴµ‹Í·Õ¡è ŋÒÇÁÒáŌÇÊÒÁÒö¹íÒÁÒà¢Õ¹໚¹â»Ãá¡ÃÁÀÒÉÒ C à¾×Íè ·´Êͺ¡Ò÷íÒ§Ò¹ ´Ñ§ã¹â»Ãá¡ÃÁ·Õè L20-1


180 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

char I2CInit_=0; char y; int i,j; void Relay16(unsigned int x) { if (I2CInit_==0) { I2C_Init(100000); // I2C_Start(); // I2C_Wr(0x40); // I2C_Wr(0x06); // I2C_Wr(0x00); // I2C_Stop(); // I2C_Start(); I2C_Wr(0x40); I2C_Wr(0x07); I2C_Wr(0x00); I2C_Stop(); I2CInit_=1;

// // // // //

Call initial L2C Start communication Address MCP23016 and Write Command Command for access IODIR0 Set DIR0 to OUTPUT Stop communication Start communication Address MCP23016 and Write Command Command for access IODIR1 Set DIR0 to OUTPUT Stop communication

} y=x%256; I2C_Start(); I2C_Wr(0x40); I2C_Wr(0x00); I2C_Wr(y); I2C_Stop();

// // // //

Issue I2C Send byte Send byte Send byte

start signal via I2C (device address + W) (data address) (data address)

x=x/256; I2C_Start(); I2C_Wr(0x40); I2C_Wr(0x01); I2C_Wr(x); I2C_Stop();

// // // //

Issue I2C Send byte Send byte Send byte

start signal via I2C (device address + W) (data address) (data address)

} void main() { while(1) { i=1; while(i<32768) { Relay16(i); Delay_ms(500); i=i*2; } Relay16(i); Delay_ms(500); } }

// Loop // Start value // End loop value // Send Data to ZX-relay16 // Shift left // Send Last bit

¡Ò÷íÒ§Ò¹¢Í§â»Ãá¡ÃÁ â»Ãá¡ÃÁËÅÑ¡¨Ð¹íÒ¤‹Ò i ÁÒ·íÒ¡ÒÃàÅ×Íè ¹ºÔµä»·Ò§«ŒÒ áŌÇʋ§ãˌ¡ºÑ ¿˜§¡ª¹Ñè Relay16 () «Ö§è ·íÒ˹ŒÒ·Õè µÔ´µ‹Í¡ÑººÍÏ´ ZX-RELAY16 ÊíÒËÃѺ¿˜§¡ª¹Ñè Relay16() ¨Ð¡íÒ˹´ãˌ¢Ò¾Íϵ·Ñ§é ËÁ´à»š¹àÍÒµ¾µØ ¨Ò¡¹Ñ¹é ÃѺ¤‹ÒµÑÇá»Ã x ࢌÒÁÒ á¡໚¹ 8 ºÔµÅ‹Ò§áÅÐ 8 ºÔµº¹ ¹íÒ¤‹Ò 8 ºÔµÅ‹Ò§Ê‹§ä»ÂѧáÍ´à´ÃÊ 0x00 à¾×Íè ¢Ñº RELAY1 ¶Ö§ 8 áÅйíÒ¤‹Ò 8 ºÔµº¹Ê‹§ä»ÂѧáÍ´à´ÃÊ 0x01 à¾×Íè ʋ§ä»¢Ñº RELAY9 ¶Ö§ 16

â»Ãá¡ÃÁ·Õè L20-1 ä¿Å I2CrelayTest.c â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺ¡ÒÃ㪌§Ò¹äźÃÒÃÕ i2c ¢Í§ mikroC ¤ÍÁä¾àÅÍÏ à¾×Íè µÔ´µ‹Í¡ÑºâÁ´ÙÅÃкººÑÊ I2C ¢Í§äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC16F877/887


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 181

¡ÒÃàª×Íè Áµ‹Í·Ò§ÎÒÏ´áÇÏ 㪌ǧ¨Ãã¹ÃÙ»·Õè L20-3 㹡Ò÷´Åͧ l Ëҡ㪌ºÍÏ´·´Åͧ NX-877 plus II ãˌµÍ ‹ ǧ¨ÃµÒÁÃÙ»·Õè L20-3 l Ëҡ㪌ºÍÏ´ RBX-877rev.2007 ãˌµÍ ‹ ǧ¨ÃµÒÁÃÙ»·Õè L20-4 l

¢Ñ鹵͹¡Ò÷´Åͧ L20.1 ແ´â»Ãá¡ÃÁ mikroC ÊÌҧä¿Åâ»Ãà¨ç¡µ â´ÂàÅ×Í¡äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏáÅФÇÒÁ¶ÕÊè ­ Ñ ­Ò³¹Ò́¡Ò´Ñ§¹Õé l

¡Ã³Õ㪌ºÍÏ´ NX-877 plus II àÅ×Í¡ PIC16F877 ËÃ×Í PIC16F877A ¤ÇÒÁ¶ÕÊè ­ Ñ ­Ò³¹Ò́¡Ò 20MHz

l

¡Ã³Õ㪌ºÍÏ´ RBX-877 rev.2007 àÅ×Í¡ PIC16F887 ¤ÇÒÁ¶ÕÊè ­ Ñ ­Ò³¹Ò́¡Ò 20MHz NC C NO NC C NO NC C NO NC C NO NC C NO NC C NO NC C NO NC C NO NC C NO NC C NO NC C NO NC C NO NC C NO NC C NO NC C NO NC C NO RELAY16 RELAY15 RELAY14 RELAY13 RELAY12 RELAY11 RELAY10 RELAY9 RELAY8 RELAY7 RELAY6 RELAY5 RELAY4 RELAY3 RELAY2 RELAY1

ON

POWER

SDA

SCL

-

SDA SCL

I C EXPANSION A2 A1 A0

ãʋ¨ÑêÁà»ÍÏ = Åͨԡ "0" +5V

+12V 2A

R9 10k

C3 0.1PF

11 32 Vcc 1

SW1 RESET

R8

MCLR

21+$.&%%) 21+$.&&% RC4/SDA 23 12 31

RC3/SCL

18

GND OSC1

OSC2

13

14

C1 22pF

XTAL1 20MHz

C2 22pF

ÃÙ»·Õè L20-3 ǧ¨Ã·´Åͧ㪌§Ò¹âÁ´ÙÅÃкººÑÊ I2C ¢Í§äÁâ¤Ã¤Í¹â·��ÅàÅÍÏ PIC16F877/887


182 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

NC C NO NC C NO NC C NO NC C NO NC C NO NC C NO NC C NO NC C NO NC C NO NC C NO NC C NO NC C NO NC C NO NC C NO NC C NO NC C NO RELAY16 RELAY15 RELAY14 RELAY13 RELAY12 RELAY11 RELAY10 RELAY9 RELAY8 RELAY7 RELAY6 RELAY5 RELAY4 RELAY3 RELAY2 RELAY1

ON

POWER

SDA

SCL

-

SDA SCL

I C EXPANSION A2 A1 A0

ãʋ¨ÁêÑ à»ÍÏ = Åͨԡ "0"

+12V 2A 4.8-6V

Sound RC0

LCD RC7 RxD

BATT DOWNLOAD

RC6 TxD

RC4 SDA

RC3 SCL

POWER

LCD back light

ICD2 ON ON M-1

RESET

RBX - 877 rR ev 2 0 0 7 R PIC16F887 board

M-2 #1

#2

#3

SERVO

S+

RA4

RB0

RB3 RB0/INT

RA0

RA1

RA2

RA3

RA5

RE0

RE1

RE2

ÃÙ»·Õè L20-4 ǧ¨Ã·´Åͧ㪌§Ò¹âÁ´ÙÅÃкººÑÊ I2C ¢Í§äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC16F887 ¡Ã³Õ㪌 ºÍÏ´ RBX-877 rev.2007 L20.2 ÊÌҧâ»Ãà¨ç¡µªÍ×è I2CrelayTest áÅоÔÁ¾â»Ãá¡ÃÁÀÒÉÒ C µÒÁâ»Ãá¡ÃÁ·Õè L20-1 L20.3 ·íÒ¡ÒäÍÁä¾ÅáÅдÒǹâËÅ´â»Ãá¡ÃÁà¾×Íè ·´Êͺ¼ÅÅѾ¸¡Ò÷íÒ§Ò¹ L20.4 ¨‹ÒÂä¿ãˌᡋºÍÏ´ ZX-RELAY16 áÅкÍÏ´·´ÅͧäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ L20.5 Ãѹâ»Ãá¡ÃÁ Êѧࡵ¡Ò÷íÒ§Ò¹¢Í§ LED áÅÐÃÕàŏº¹ºÍÏ´ ZX-RELAY16 ÊѧࡵàËç¹ LED ·Õµè Òí á˹‹§ RELAY1 µÔ´äŋÅÒí ´Ñºä»¨¹¶Ö§ RELAY16 áÅÐä´ŒÂ¹Ô àÊÕ§˹ŒÒÊÑÁ¼Ñʢͧ ÃÕàŏµÑ´µ‹ÍäŋÅíҴѺ¡Ñ¹ä»Í‹ҧµ‹Íà¹×èͧ

mikroC


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 183

»¯ÔºµÑ ¡Ô Ò÷Õè 21µÔ´µ‹Í¡ÑºâÁ´ÙÅà¢çÁ·ÔÈ´Ô¨ÔµÍÅ CMPS03 ´ŒÇ¿˜§¡ª¹Ñè I2C âÁ´ÙÅà¢çÁ·ÔÈ´Ô¨µÔ ÍÅ CMPS03 ÊÒÁÒöÊ×Íè ÊÒáѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ䴌¼Ò‹ ¹¡ÒõÃǨ¨Ñº¤ÇÒÁ¡ÇŒÒ§ ¾ÑÅʏáÅм‹Ò¹ÃкººÑÊ I2C â´Â ÃкººÑÊ I2C ¨ÐÊÒÁÒöʋ§¤‹Ò¢ŒÍÁÙŵíÒá˹‹§ÍÍ¡ÁÒ·Õ¤è ÇÒÁÅÐàÍÕ´ÊÙ§ÊØ´ 0.1 ͧÈÒâ´ÂäÁ‹¨Òí ໚¹µŒÍ§ÁÕ¡Òäíҹdz¤‹ÒËÃ×Í·íÒ¡ÒÃá»Å§¤‹Òã´ æ ÍÕ¡ ã¹ÃÙ»·Õè L21-1 áÊ´§Ë¹ŒÒµÒáÅСÒèѴ¢Ò¢Í§âÁ´ÙÅ CMPS03 ʋǹã¹ÃÙ»·Õè L21-2 áÊ´§¢ŒÍÁÙŢͧºÍÏ´ àª×Íè Áµ‹Í ADX-CMPS03 «Ö§è Í͡Ẻà¾×Íè ÍíҹǤÇÒÁÊдǡ㹡Òõ‹Í㪌§Ò¹ ËÒ¡·íÒ¡Ò÷´Åͧ¡ÑººÍÏ´ RBX877 rev.2007 ÊÒÁÒöàª×Íè Áµ‹ÍࢌҡѺ¤Í¹à¹ç¡àµÍÏẺ JST 䴌·¹Ñ ·Õ ¶ŒÒËÒ¡µ‹Í·´Åͧ¡ÑººÍÏ´·´Åͧ NX877 plus II ãˌ㪌ÊÒµ‹Íǧ¨Ãµ‹Íࢌҷդè ͹à¹ç¡àµÍÏ IDC µÑÇàÁÕÂ

¢Ñ¹é µÍ¹¡ÒõԴµ‹Í¡ÑºâÁ´ÙÅ CMPS03 à¹×Íè §¨Ò¡µŒÍ§¡ÒõԴµ‹Í¼‹Ò¹ÃкººÑÊ I2C ¨Ö§µŒÍ§·ÃÒº¶Ö§áÍ´à´ÃʢͧâÁ´ÙÅ CMPS03 «Ö§è ¡ç¤Í× $C0 ÊíÒËÃѺ¡ÒÃʋ§¢ŒÍÁÙÅ áÅÐ $C1 ÊíÒËÃѺ¡ÒÃ͋ҹ¤‹Ò¢ŒÍÁÙÅ â´Â¢Ñ¹é µÍ¹¡ÒõԴµ‹Í¡ÑºâÁ´ÙÅ CMPS03 ÁÕ´§Ñ ¹Õé µÑǵÃǨ¨ÑºÊ¹ÒÁáÁ‹àËÅç¡ KMZ51 äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏËÅÑ¡ÊíÒËÃѺ»ÃÐÁÇżŠ¡ÃÒÇ´ äÁ‹µÍ‹ 㪌§Ò¹ äÁ‹µÍ‹ 㪌§Ò¹ »ÃѺµÑ§é ¤‹Ò¡ÒÃÇÑ´ àÍÒµ¾µØ ¢Í§¡ÒûÃѺᵋ§¤‹ÒÇÑ´ àÍÒµ¾µØ ẺÊÑ­­Ò³¾ÑÅʏ SDA ÊíÒËÃѺàª×Íè Áµ‹Í¼‹Ò¹ÃкººÑÊ I2C SCL ÊíÒËÃѺàª×Íè Áµ‹Í¼‹Ò¹ÃкººÑÊ I2C +5V

·ÔÈà˹×Í ÃÙ»·Õè L21-1 áÊ´§ÃٻËҧáÅеíÒá˹‹§¢ÒÊíÒËÃѺ¡Òõ‹Í㪌§Ò¹âÁ´ÙÅ CMPS03


184 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

33.0mm 28.6mm

ADX-CMPS03

R1 4.7k

K1 SCL

R2 4.7k

S1 CAL

K4 I2C BUS

+5V

GND SCL SDA +5V

K3 PWM

31.5mm 26.7mm

>31.5mm

K2 SDA

front +5V CMPS-03 SCL Compass Digital SDA PWM CAL_OUT CAL NC NC GND

GND

PWM

>33.0mm

¨Ø´µ‹ÍÊÑ­­Ò³¢Í§ÃкººÑÊ I2C ÊíÒËÃѺàÊÕºÊÒµ‹Íǧ¨Ãà¾×èÍ㪌¡ÑººÍÏ´·´ÅͧËÃ×Í á¼§µ‹Íǧ¨Ã (àºÃ´ºÍÏ´)

¤Í¹à¹ç¡àµÍϵÇÑ àÁÕ ÊíÒËÃѺµÔ´µÑ§é âÁ´ÙÅ CMPS03

ÊÇÔµª¡´µÔ´»Å‹Í´ѺÊíÒËÃѺ»ÃѺᵋ§à¾×Íè ËÒµíÒá˹‹§ ͌ҧÍÔ§ (calibration) ¤Í¹à¹ç¡àµÍÏ PCB ¢Í§àÍÒµ¾µØ ÊÑ­­Ò³¾ÑÅʏ

¤Í¹à¹ç¡àµÍÏ PCB ¢Í§ ÊÑ­­Ò³ÃкººÑÊ I2C

ÃÙ»·Õè L21-2 áÊ´§Ç§¨Ã¢Í§ºÍÏ´ ADX-CMPS03 áÅСÒÃàª×Íè Áµ‹Í¡ÑºâÁ´ÙÅ CMPS03 S ta rt B it

S ta rt B it

áÍ´à´ÃÊÊíÒËÃѺʋ§¤‹Ò¢ŒÍÁÙÅä»Âѧ CMPS03 = $CO 1

1

0

0

0

0

0

A7

A6

A5

A4

A3

A2

A1

1

2

3

4

5

6

7

0 R /W A C K

8

Start bit

¢ŒÍÁÙŵíÒá˹‹§ÃÕ¨ÊÔ àµÍÏ·µÕè ͌ §¡ÒÃ͋ҹ¤‹Ò

9

áÍ´à´ÃÊÊíÒËÃѺ͋ҹ¤‹Ò¢ŒÍÁÙŨҡ CMPS03 = $C1

D7

D6

D5

D4

D3

D2

D1

D0

ACK

1

2

3

4

5

6

7

8

9

Stop bit

¢ŒÍÁÙÅ 8 ºÔµ·ÕÍè ҋ ¹ä´Œ¨Ò¡ CMPS03

1

1

0

0

0

0

0

1

A7

A6

A5

A4

A3

A2

A1

R /W

ACK

D7

D6

D5

D4

D3

D2

D1

D0

ACK

1

2

3

4

5

6

7

8

9

1

2

3

4

5

6

7

8

9

ÃÙ»·Õè L21-3 áÊ´§ä´ÍÐá¡ÃÁàÇÅҢͧ¡ÒõԴµ‹Í¡ÑºâÁ´ÙÅ CMPS03 ¼‹Ò¹ÃкººÑÊ I2C


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 185

(1) ʋ§ºÔµàÃÔÁè µŒ¹ (Start) à¾×Íè ᨌ§ãˌÃкººÑÊ I2C àµÃÕÂÁ¾ÃŒÍÁÃѺ¢ŒÍÁÙÅ (2) ʋ§¤‹ÒáÍ´à´ÃÊ $C0 à¾×Íè ÃкØÇҋ µŒÍ§¡ÒõԴµ‹Íà¾×Íè à¢Õ¹¢ŒÍÁÙšѺâÁ´ÙÅ CMPS03 ã¹µÒÃÒ§·Õè L21-1

(3) ʋ§¤‹ÒµíÒá˹‹§ÃÕ¨ÊÔ àµÍϢͧ CMPS03 ·Õµè ͌ §¡ÒÃ͋ҹ¤‹Ò «Ö§è ÃÒÂÅÐàÍÕ´ÃÕ¨ÊÔ àµÍϵҋ §æ áÊ´§ (4) ʋ§¤‹ÒáÍ´à´ÃÊ $C1 à¾×Íè ÃкØÇҋ µŒÍ§¡ÒÃ͋ҹ¤‹Ò¢ŒÍÁÙŨҡâÁ´ÙÅ CMPS03 (5) ͋ҹ¤‹Ò¢ŒÍÁÙŨҡâÁ´ÙÅ CMPS03 ÁÒà¡çºänjã¹Ë¹‹Ç¤ÇÒÁ¨íÒ (6) ʋ§ºÔµËÂØ´ (Stop) à¾×Íè Âص¡Ô ÒÃÊ×Íè ÊÒâŒÍÁÙÅ áÅСíÒ˹´ãˌºÊÑ ÍÂÙ㋠¹ÊÀÒÇкÑÊNjҧ

¨Ò¡¢Ñ¹é µÍ¹¡ÒõԴµ‹Í·Õ¡è ŋÒÇÁÒáŌÇÊÒÁÒö¹íÒÁÒà¢Õ¹໚¹â»Ãá¡ÃÁÀÒÉÒ C à¾×Íè ·´Êͺ¡Ò÷íÒ§Ò¹ ´Ñ§ã¹â»Ãá¡ÃÁ·Õè L21-1

µíÒá˹‹§ÃÕ¨ÔÊàµÍÏ ÃÒÂÅÐàÍÕ´ 0 µÑÇàÅ¢áÊ´§Ãع‹ ¢Í§ºÍÏ´ CMP03 1 ʋ§¤‹ÒµíÒá˹‹§áººËÂÒº (0-255) ʋ§¤‹ÒµíÒá˹‹§áººÅÐàÍÕ´´ŒÇµÑÇàÅ¢ 16 ºÔµ (0-3599) 2,3 ÊÒÁÒöá»Å§¤‹Òà¾×Íè áÊ´§Í§ÈÒ 0-359.9 ͧÈÒ䴌â´ÂµÃ§ ÊíÒËÃѺµÃǨÊͺ¤‹ÒÀÒÂã¹ â´Â¨ÐáÊ´§¤‹Ò¤ÇÒÁµ‹Ò§¢Í§ 4,5 Sensor1 ໚¹µÑÇàÅ¢ 16 ºÔµáºº¤Ô´à¤Ã×èͧËÁÒ ÊíÒËÃѺµÃǨÊͺ¤‹ÒÀÒÂã¹ â´Â¨ÐáÊ´§¤‹Ò¤ÇÒÁµ‹Ò§¢Í§ 6,7 Sensor2 ໚¹µÑÇàÅ¢ 16 ºÔµáºº¤Ô´à¤Ã×èͧËÁÒ áÊ´§¤‹ÒµÑÇàÅ¢¡ÒûÃѺᵋ§ÀÒÂã¹ (calibration value1) 8,9 ໚¹µÑÇàÅ¢ 16 ºÔµáºº¤Ô´à¤Ã×Íè §ËÁÒ áÊ´§¤‹ÒµÑÇàÅ¢¡ÒûÃѺᵋ§ÀÒÂã¹ (calibration value2) 10,11 ໚¹µÑÇàÅ¢ 16 ºÔµáºº¤Ô´à¤Ã×Íè §ËÁÒ 12,13 äÁ‹ãªŒ§Ò¹Í‹Ò¹¤‹Ò䴌໚¹ 0 14 äÁ‹ãªŒ§Ò¹ äÁ‹ä´Œ¡Òí ˹´¤‹Òänj ¤íÒÊѧè ÊíÒËÃѺ¡ÒûÃѺᵋ§¤‹Ò â´ÂàÁ×Íè µŒÍ§¡ÒûÃѺᵋ§¤‹Ò 15 µŒÍ§à¢Õ¹¢ŒÍÁÙÅ 255 ࢌҷÕÃè ¨Õ ÊÔ àµÍϵÒí á˹‹§¹Õé

µÒÃÒ§·Õè L21-1 ÃÒÂÅÐàÍÕ´¢Í§ÃÕ¨ÊÔ àµÍÏ·§Ñé ËÁ´¢Í§âÁ´ÙÅ CMPS03


186 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

int Dat,Dat1,Dat2,High; char Version,Unused,Low; char *Text1="CMPS03 Ver."; char Text[6]; void show() { ByteToSTr(Version,Text); Lcd_Out(1,11,Text); Dat=(HIGH*256)+Low; Dat1=Dat/10; Dat2=(Dat%10)&0x0F; WordToStr(Dat1,Text); Lcd_Out(2,1,Text); Lcd_Out(2,6,"."); Lcd_Chr(2,7,(Dat2+0x30)); Lcd_Out_Cp(" Degree"); } void main() { Lcd_Init(&PORTD); Lcd_Cmd(LCD_CURSOR_OFF); Lcd_Cmd(LCD_CLEAR); Lcd_Out(1,1,text1); while(1) { I2C_Init(100000); I2C_Start(); I2C_Wr(0xC0); I2C_Wr(0x00); I2C_Repeated_Start(); I2C_Wr(0xC1); Version=I2C_Rd(1); Delay_ms(5); Unused=I2C_Rd(1); Delay_ms(5); High=I2C_Rd(1); Delay_ms(5); Low=I2C_Rd(0); I2C_Stop(); Show(); Delay_ms(500);

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

// // // // //

Convert data to string Show LCD line1 Sum low byte + high byte Convert to degree Convert decimal notation Convert to string show compass degree ==> LCD decimal point

issue I2C send byte send byte issue I2C send byte

start signal via I2C to cmps03 addres 0xC0 (register adres) signal repeated start (request data from EEPROM)

// Read revision // Read unused // Read High byte // Read Low byte // Stop I2C // Show on LCD

} }

¡Ò÷íÒ§Ò¹¢Í§â»Ãá¡ÃÁ â»Ãá¡ÃÁẋ§¡Ò÷íҧҹ໚¹ 2 ʋǹ ʋǹáá໚¹¡ÒõԴµ‹Í¡ÑºâÁ´ÙÅ CMPS03 ¼‹Ò¹ºÑÊ I2C à¾×Íè ͋ҹ ¤‹ÒÃÕ¨ÊÔ àµÍÏᵋÅÐ亵Í‹ҧµ‹Íà¹×Íè § â´ÂàÃÔÁè ÃÕ¨ÊÔ àµÍÏáÊ´§Ãع‹ ¢Í§ºÍÏ´ ÃÕ¨ÊÔ àµÍÏáÊ´§¤‹ÒẺËÂÒº (͋ҹÁÒ áµ‹äÁ‹ä´ŒãªŒ§Ò¹) ÃÕ¨ÊÔ àµÍÏÍҋ ¹¤‹ÒͧÈÒẺÅÐàÍÕ´ (16 ºÔµ) ʋǹ·Õè 2 ໚¹¡ÒùíÒ¤‹Ò·ÕÍè ҋ ¹ä´Œ¨Ò¡âÁ´ÙÅ CMPS03 ÁÒá»Å§ãˌÍÂÙ㋠¹ÃÙ»µÑÇÍÑ¡ÉÃà¾×Íè ¹íÒä»áÊ´§¼Å·Õè âÁ´ÙÅ LCD â´ÂÃع‹ ¢Í§ºÍÏ´¨Ð¹íÒä»áÊ´§·Õºè Ã÷Ѵº¹¢Í§âÁ´ÙÅ LCD ¨Ò¡¹Ñ¹é ¹íÒ¤‹Ò¢Í§·ÔÈ·Ò§·ÕÍè ҋ ¹ä´Œã¹ 亵º¹áÅÐŋҧ¢Í§ÃÕ¨ÊÔ àµÍÏÍҋ ¹¤‹ÒẺÅÐàÍÕ´ÁÒÃÇÁ¡Ñ¹ ËÒôŒÇ 10 à¾×Íè ãˌ䴌¤Ò‹ ·ÔÈ·Ò§¨ÃÔ§ã¹Ë¹‹ÇÂͧÈÒ Ê‹Ç¹àÈÉ·Õäè ´Œ¨Ò¡¡ÒÃËÒùíÒÁÒáÊ´§à»š¹¤‹Ò·È¹ÔÂÁ¢Í§Ë¹‹ÇÂͧÈÒ

â»Ãá¡ÃÁ·Õè L21-1 ä¿Å CMPS03test.c â»Ãá¡ÃÁÀÒÉÒ C ¢Í§ mikroC ·Õãè ªŒ·´Êͺ¡Ò÷íÒ§Ò¹¢Í§ äźÃÒÃÕ I2C ´ŒÇ¡ÒõԴµ‹Í¡ÑºâÁ´ÙÅ CMPS-03 à¾×Íè ͋ҹ¤‹Ò¢Í§·ÔÈ


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 187

¡ÒûÃѺᵋ§¤‹Ò·Ôȷҧ͌ҧÍԧᡋâÁ´ÙÅ CMPS03 à¾×Íè ãˌ¡ÒÃÇÑ´·ÔÈ·Ò§¢Í§âÁ´ÙÅ CMPS03 ÁÕ¤ÇÒÁáÁ‹¹ÂíÒÁÒ¡·ÕÊè ´Ø ¨Ö§ÁÕÍ¹Ô ¾ØµÊíÒËÃѺ»ÃѺᵋ§¤‹Ò·ÔÈ·Ò§ ͌ҧÍÔ§ ·Ñ§é ¹Õ¾é Í×è »ÃÐ⪹ã¹¡ÒáíÒ˹´·Ôȷҧ͌ҧÍԧ੾ÒÐÊíÒËÃѺ¼Ù㌠ªŒ§Ò¹ â´ÂµŒÍ§»‡Í¹ÊÑ­­Ò³Åͨԡ “0” ࢌҷÕè ¢ÒÍÔ¹¾ØµÊíÒËÃѺ»ÃѺᵋ§âÁ´ÙÅ CMPS03 «Ö§è ¡ç¤Í× ¢Ò 6 Ëҡ㪌ºÍÏ´ ADX-CMPS03 ¡ÑºâÁ´ÙÅ CMPS03 ¨ÐÁÕÊÇÔµª ¡´µÔ´»Å‹Í´ѺµÔ´µÑ§é änjãˌáÅŒÇ ¡ÒûÃѺᵋ§ÁÕ¢¹Ñé µÍ¹´Ñ§¹Õé (1) ÇÒ§âÁ´ÙÅ CMPS03 ¢¹Ò¹¡Ñº¾×¹é Ëѹ´ŒÒ¹Ë¹ŒÒ¢Í§âÁ´ÙÅä»·Ò§·ÔÈà˹×Í ¡´ÊÇÔµª 1 ¤Ãѧé (2) ÇÒ§âÁ´ÙÅ CMPS03 ¢¹Ò¹¡Ñº¾×¹é Ëѹ´ŒÒ¹Ë¹ŒÒ¢Í§âÁ´ÙÅä»·Ò§·ÔȵÐÇѹÍÍ¡ ¡´ÊÇÔµª (3) ÇÒ§âÁ´ÙÅ CMPS03 ¢¹Ò¹¡Ñº¾×¹é Ëѹ´ŒÒ¹Ë¹ŒÒ¢Í§âÁ´ÙÅä»·Ò§·ÔÈ㵌 ¡´ÊÇÔµª 1 ¤Ãѧé (4) ÇÒ§âÁ´ÙÅ CMPS03 ¢¹Ò¹¡Ñº¾×¹é Ëѹ´ŒÒ¹Ë¹ŒÒ¢Í§âÁ´ÙÅä»·Ò§·ÔȵÐÇѹµ¡ ¡´ÊÇÔµª ໚¹ÍѹÊÔé¹ÊØ´¡ÒûÃѺµÑ駤‹Ò·Ôȷҧ͌ҧÍÔ§¢Í§âÁ´ÙÅ CMPS03 â´ÂâÁ´ÙŨÐà¡çº¤‹Ò͌ҧÍÔ§¹Õéänjã¹Ë¹‹Ç ¤ÇÒÁ¨íÒÍÕÍ¾Õ ÃÍÁáÅÐäÁ‹µÍŒ §»ÃѺµÑ§é ¤‹ÒãËÁ‹Í¡Õ àÁ×Íè ¨‹ÒÂä¿àÅÕÂé §¤Ãѧé ãËÁ‹ +5V 32

IC3 PIC16F887

RD3

21

4

22

6

+5V RD7

R5 4k7 SW2 RESET

1 R6 1k

MCLR

RD6 RD5 RD4

RC4/SDA RC3/SCL

30

14

E

BLA

DSP1 LCD 16x2

+V Vo

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

BLK GND 15 2

1 3 VR1 10k

BRIGHTNESS

+5V

28 27 23 18

31 13

RS

BACK LIGHT ON

29

12

CR1 Ceramic Resonator 20MHz

JP1

R13 16 47

SCL SDA +5V

RD2

+5V

C6 0.1PF 50V

GND

11

front +5V CMPS-03 SCL Digital Compass SDA PWM CAL_OUT CAL N NC NC GND SDA

SCL

PWM

ÃÙ»·Õè L20-4 ǧ¨Ã·´Åͧ㪌§Ò¹âÁ´ÙÅÃкººÑÊ I2C ¢Í§äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC16F877 㹡ÒõԴµ‹Í ¡ÑºâÁ´ÙÅà¢çÁ·ÔÈ CMPS03


C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

GND

SCL SDA +5V

188 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

+

front +5V CMPS-03 SCL Digital Compass SDA PWM CAL_OUT CAL N NC NC GND

+ + +

SDA

SCL

PWM

4.8-6V

Sound RC0

LCD RC7 RxD

BATT DOWNLOAD

RC6 TxD

RC4 SDA

RC3 SCL

POWER

LCD back light

ICD2 ON ON M-1

CMPS03 VR er 1 1 5 7 R P I 3 5 9 . 9 8 D eg r e e d

RESET

M-2 #1

#2

#3

SERVO

S+

RA4

RB0

RB3 RB0/INT

RA0

RA1

RA2

RA3

RA5

RE0

RE1

RE2

ÃÙ»·Õè L20-4 ǧ¨Ã·´Åͧ㪌§Ò¹âÁ´ÙÅÃкººÑÊ I2C ¢Í§äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC16F887 ¡Ã³Õ㪌ºÍÏ´ RBX-877 rev.2007

¡ÒÃàª×Íè Áµ‹Í·Ò§ÎÒÏ´áÇÏ 㪌ǧ¨Ãã¹ÃÙ»·Õè L21-4 㹡Ò÷´Åͧ l Ëҡ㪌ºÍÏ´·´Åͧ NX-877 plus II ãˌµÍ ‹ ǧ¨ÃµÒÁÃÙ»·Õè L21-4 l Ëҡ㪌ºÍÏ´ RBX-877rev.2007 ãˌµÍ ‹ ǧ¨ÃµÒÁÃÙ»·Õè L21-5 l

¢Ñ鹵͹¡Ò÷´Åͧ L21.1 ແ´â»Ãá¡ÃÁ mikroC ÊÌҧä¿Åâ»Ãà¨ç¡µ â´ÂàÅ×Í¡äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏáÅФÇÒÁ¶ÕÊè ­ Ñ ­Ò³¹Ò́¡Ò´Ñ§¹Õé l ¡Ã³Õ㪌ºÍÏ´ NX-877 plus II àÅ×Í¡ PIC16F877 ËÃ×Í PIC16F877A ¤ÇÒÁ¶ÕÊ è­ Ñ ­Ò³¹Ò́¡Ò 20MHz l ¡Ã³Õ㪌ºÍÏ´ RBX-877 rev.2007 àÅ×Í¡ PIC16F887 ¤ÇÒÁ¶ÕÊ è­ Ñ ­Ò³¹Ò́¡Ò 20MHz L21.2 ÊÌҧâ»Ãà¨ç¡µªÍ×è CMPS03test áÅоÔÁ¾â»Ãá¡ÃÁÀÒÉÒ C µÒÁâ»Ãá¡ÃÁ·Õè L21-1


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 189

L21.3 ·íÒ¡ÒäÍÁä¾ÅáÅдÒǹâËÅ´â»Ãá¡ÃÁà¾×Íè ·´Êͺ¼ÅÅѾ¸¡Ò÷íÒ§Ò¹ L21.4 »ÃѺᵋ§·ÔȢͧ¢ÑÇé áÁ‹àËÅç¡âš͌ҧÍÔ§ãˌᡋâÁ´ÙÅ CMPS03 L21.5 Ãѹâ»Ãá¡ÃÁ ¨Ò¡¹Ñ¹é ·´Åͧ¢ÂѺâÁ´ÙÅ CMPS03 ãËŒË¹Ñ ä»ã¹·ÔÈ·Ò§µ‹Ò§æ áŌǵÃǨÊͺ¼Å¡Ò÷íÒ§Ò¹ ¼‹Ò¹·Ò§âÁ´ÙÅ LCD ·Õâè Á´ÙÅ LCD ¨ÐáÊ´§ËÁÒÂàÅ¢àÇÍϪ¹Ñ ¢Í§âÁ´ÙÅ CMPS-03 ·Õºè Ã÷Ѵº¹ ¨Ò¡¹Ñ¹é áÊ´§µíÒá˹‹§Í§ÈÒ ¢Í§âÁ´ÙÅ CMPS03 ã¹¢³Ð¹Ñ¹é à·Õº¡Ñº·ÔÈà˹×Í·Õºè Ã÷Ѵ´ŒÒ¹Å‹Ò§ ´Ñ§ÃÙ»

C M P S 03 3 359.9

Ver 15 D e gr ee

mikroC


190 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

11.2 äźÃÒÃÕè Soft_I2C µÔ´µ‹ÍÍØ»¡Ã³ÃкººÑÊ I2C â´ÂäÁ‹ãªŒâÁ´ÙźÑÊ I2C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ·ÕèäÁ‹ÁÕâÁ´ÙÅ I2C ÍÂًÀÒÂ㹡çÊÒÁÒöµÔ´µ‹Í¡ÑºÍØ»¡Ã³ ÃкººÑÊ I2C 䴌´ŒÇ¡ÒÃ㪌äźÃÒÃÕ Soft_I2C ã¹ mikroC ¤ÍÁä¾àÅÍÏ â´Â¨Ð·íÒ§Ò¹ã¹âËÁ´ ÁÒÊàµÍÏáÅÐäÁ‹Ê¹ÑºÊ¹Ø¹¡ÒõԴµ‹ÍẺÁÑŵÔÁÒÊàµÍÏ 㹡Ò÷íÒ§Ò¹¢Í§äźÃÒÃÕ¹ãÕé ªŒ°Ò¹àÇÅÒÀÒÂã¹äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏà¾×Íè ¡íÒ˹´àÇÅÒ㹡Òà ÊÌҧʶҹе‹Ò§æ ¢Í§ºÑÊ I2C ´Ñ§¹Ñ¹é ¡ÒÃ㪌§Ò¹äźÃÒÃÕ¹è ¨Õé еŒÍ§Â¡àÅÔ¡¡ÒÃ㪌§Ò¹ÍÔ¹àµÍÃÃ»Ñ µ¡Í‹ ¹àÊÁÍ

11.2.1 ¿˜§¡ª¹Ñè Soft_I2C_Config 㪌㹡ÒáíÒ˹´¤‹ÒàÃÔÁè µŒ¹ãˌ¡ºÑ ¡ÒõԴµ‹Í¡ÑºÃкººÑÊ I2C ᡋ¢Ò¾Íϵ·Õ¹è Òí ÁÒ㪌§Ò¹ ¨ÐµŒÍ§ àÃÕ¡㪌§Ò¹¡‹Í¹·Õ¨è Ð㪌§Ò¹¿˜§¡ª¹Ñè Í×¹è æ ¢Í§äźÃÒÃÕ Soft_I2C ÃٻẺ¡ÒÃ㪌§Ò¹¤íÒÊѧè void Soft_I2C_Config(unsigned short *port, const unsigned short SDA, const unsigned short SCL);

µÑÇá»Ã port

¤×Í ¾Íϵ¢Í§äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ·ãÕè ªŒ¡Òí ˹´à»š¹ SDA áÅÐ SCL

SDA ¤×Í µíÒá˹‹§¢Ò¢Í§äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ·µ Õè ͌ §¡ÒÃ㪌§Ò¹à»š¹¢Ò SDA Áդҋ 0 ¶Ö§ 7 (¤‹Ò¤§·Õàè ·‹Ò¹Ñ¹é ) SCL

¤×Í µíÒá˹‹§¢Ò¢Í§äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ·µÕè ͌ §¡ÒÃ㪌§Ò¹à»š¹¢Ò SCL Áդҋ 0 ¶Ö§ 7 (¤‹Ò¤§·Õàè ·‹Ò¹Ñ¹é )

µÑÇÍ‹ҧ·Õè 11-8 Soft_I2C_Config(&PORTB, 1, 2);

¡íÒ˹´¤‹ÒàÃÔÁè µŒ¹á¡‹¾Íϵ B à¾×Íè µÔ´µ‹Í¡ÑººÑÊ I2C â´Â¡íÒ˹´ãˌ¢Ò RB1 ໚¹¢Ò SDA áÅÐ¢Ò RB2 ໚¹¢Ò SCL

11.2.2 ¿˜§¡ª¹Ñè Soft_I2C_Start 㪌ÊÌҧÊÑ­­Ò³ Start ¡‹Í¹·íÒ¡ÒÃ͋ҹËÃ×Íà¢Õ¹¢ŒÍÁÙšѺºÑÊ I2C ¡‹Í¹¡ÒÃàÃÕ¡㪌§Ò¹¿˜§¡ª¹Ñè ¹Õ¨é еŒÍ§àÃÕ¡¿˜§¡ª¹Ñè I2C_Init ¡‹Í¹ ÃٻẺ¡ÒÃ㪌§Ò¹¤íÒÊѧè void Soft_I2C_Start(void);

µÑÇÍ‹ҧ·Õè 11-9 Soft_I2C_Start();


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 191

11.2.3 ¿˜§¡ª¹Ñè Soft_I2C_Read

㪌Íҋ ¹¢ŒÍÁÙÅ¢¹Ò´ 1 亵¨Ò¡ÍØ»¡Ã³ÊàÅ¿ã¹ÃкººÑÊ I2C ¡‹Í¹ãªŒ§Ò¹¿˜§¡ª¹Ñè ¹Õ¨é еŒÍ§àÃÕ¡ 㪌§Ò¹¿˜§¡ª¹Ñè Soft_I2C_Config áÅÐ Soft_I2C_Start ¡‹Í¹ ÃٻẺ¡ÒÃ㪌§Ò¹¤íÒÊѧè unsigned short Soft_I2C_Read(unsigned short ack);

¡Òä׹¤‹Ò ¢ŒÍÁÙÅ 1 亵¨Ò¡ÍØ»¡Ã³ÊàÅ¿·Õµè ´Ô µ‹Í´ŒÇÂ

µÑÇá»Ã ack

¶ŒÒ໚¹ “0” ¨ÐäÁ‹Ê§‹ ÊÑ­­Ò³ÃѺÃٌ (Acknowledge) ¶ŒÒ໚¹¤‹ÒÍ×¹è æ ¨Ðʋ§ÊÑ­­Ò³ÃѺÃٌ

µÑÇÍ‹ҧ·Õè 11-10 take = Soft_I2C_Read(0);

໚¹¡ÒÃ͋ҹ¤‹Ò¢ŒÍÁÙŨҡÍØ»¡Ã³ÊàÅ¿ÁÒà¡çºänj·µÕè ÇÑ á»Ã take

11.2.3 ¿˜§¡ª¹Ñè Soft_I2C_Write

໚¹¿˜§¡ª¹Ñè ʋ§¢ŒÍÁÙÅ¢¹Ò´ 1 亵ÍÍ¡ä»ÂѧÃкººÑÊ I2C ¡‹Í¹ãªŒ§Ò¹¿˜§¡ª¹Ñè ¹Õ¨é еŒÍ§àÃÕ¡ 㪌§Ò¹¿˜§¡ª¹Ñè Soft_I2C_Config áÅÐ Soft_I2C_Start ¡‹Í¹ ÃٻẺ¡ÒÃ㪌§Ò¹¤íÒÊѧè unsigned short Soft_I2C_Write(unsigned short data);

µÑÇá»Ã data

¤×Í¢ŒÍÁÙÅ 1 亵·µÕè ͌ §¡ÒÃà¢Õ¹ä»ÂѧÃкººÑÊ I2C

¡Òä׹¤‹Ò ¤×¹¤‹Ò “0” ¶ŒÒäÁ‹Á¢Õ ͌ ¼Ô´¾ÅÒ´à¡Ô´¢Ö¹é

µÑÇÍ‹ҧ·Õè 11-11 Soft_I2C_Write(0xA3);

à¢Õ¹¢ŒÍÁÙÅ 0xA3 ŧ仺¹ºÑÊ I2C

11.2.4 ¿˜§¡ª¹Ñè Soft_I2C_Stop

໚¹¿˜§¡ª¹Ñè ʋ§ÊÑ­­Ò³ Stop ãˌ¡ºÑ ºÑÊ I2C ÃٻẺ¡ÒÃ㪌§Ò¹¤íÒÊѧè void Soft_I2C_Stop(void);

µÑÇÍ‹ҧ¡ÒÃ㪌§Ò¹ Soft_I2C_Stop();


192 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

»¯ÔºµÑ ¡Ô Ò÷Õè 22 µÔ´µ‹Í ZX-RELAY16 ºÍÏ´¢ÑºÃÕàŏ 16 ª‹Í§´ŒÇ ¿˜§¡ª¹Ñè Soft_I2C ã¹»¯ÔºµÑ ¡Ô Ò÷Õè 20 ໚¹¡ÒõԴµ‹ÍäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC16F877(A) ËÃ×Í PIC16F887 ¡Ñº ZX-RELAY16 ºÍÏ´¢ÑºÃÕàŏ 16 ª‹Í§¼‹Ò¹ÃкººÑÊ I2C â´Â㪌âÁ´ÙźÑÊ I2C ÀÒÂã¹äÁâ¤Ã¤Í¹â·ÃÅàÅÍϼҋ ¹·Ò§¢Ò RC4/SDA áÅÐ¢Ò RC3/SCL áŌÇàÃÕ¡㪌§Ò¹¿˜§¡ª¹Ñè i2c NC C NO NC C NO NC C NO NC C NO NC C NO NC C NO NC C NO NC C NO NC C NO NC C NO NC C NO NC C NO NC C NO NC C NO NC C NO NC C NO RELAY16 RELAY15 RELAY14 RELAY13 RELAY12 RELAY11 RELAY10 RELAY9 RELAY8 RELAY7 RELAY6 RELAY5 RELAY4 RELAY3 RELAY2 RELAY1

ON

POWER

SDA

SCL

-

SDA SCL

I C EXPANSION A2 A1 A0

ãʋ¨ÑêÁà»ÍÏ = Åͨԡ "0" +5V

+12V 2A

R9 10k

C3 0.1PF R8

11 32 Vcc 1

SW1 RESET

MCLR

PIC16F877(A) PIC16F887 RC7 12 31

RC6

26 25

GND OSC1 13

C1 22pF

OSC2 14

XTAL1 20MHz

C2 22pF

ÃÙ»·Õè L22-1 ǧ¨Ã·´ÅͧµÔ´µ‹Í¡ÑºÍØ»¡Ã³ÃкººÑÊ I2C â´Â㪌¢Ò¾Íϵã´æ áÅ¡ª¹Ñè Soft_i2c ¢Í§ mikroC ¤ÍÁä¾àÅÍÏ


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 193

NC C NO NC C NO NC C NO NC C NO NC C NO NC C NO NC C NO NC C NO NC C NO NC C NO NC C NO NC C NO NC C NO NC C NO NC C NO NC C NO RELAY16 RELAY15 RELAY14 RELAY13 RELAY12 RELAY11 RELAY10 RELAY9 RELAY8 RELAY7 RELAY6 RELAY5 RELAY4 RELAY3 RELAY2 RELAY1

ON

POWER

SDA

SCL

-

SDA SCL

I C EXPANSION A2 A1 A0

ãʋ¨ÁêÑ à»ÍÏ = Åͨԡ "0"

+12V 2A 4.8-6V

Sound RC0

LCD RC7 RxD

BATT DOWNLOAD

RC6 TxD

RC4 SDA

RC3 SCL

POWER

LCD back light

ICD2 ON ON M-1

RESET

RBX - 877 rR ev 2 0 0 7 R PIC16F887 board

M-2 #1

#2

#3

SERVO

S+

RA4

RB0

RB3 RB0/INT

RA0

RA1

RA2

RA3

RA5

RE0

RE1

RE2

ÃÙ»·Õè L22-2 ǧ¨Ã·´ÅͧµÔ´µ‹Í¡ÑºÍØ»¡Ã³ÃкººÑÊ I2C â´Â㪌¢Ò¾Íϵã´æ áÅ¡ª¹Ñè Soft_i2c ¢Í§ mikroC ¤ÍÁä¾àÅÍÏ ¡Ã³Õ㪌ºÍÏ´ RBX-877 rev.2007 ÊíÒËÃѺ㹡Ò÷´Åͧ¹Õ¨é Ðᵡµ‹Ò§¨Ò¡»¯ÔºµÑ ¡Ô Ò÷Õè 20 µÃ§·Õè 㪌¿§˜ ¡ª¹Ñè Soft_I2C á·¹ ·íÒãˌÊÒÁÒö 㪌§Ò¹¢Ò¾Íϵã´æ ¢Í§äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC 㹡ÒõԴµ‹Í¡ÑºÍØ»¡Ã³ÃкººÑÊ I2C ÊíÒËÃѺ㹡Ò÷´Åͧ ¹Õàé Å×͡㪌¢Ò RC7 ໚¹¢Ò SDA áÅÐ㪌¢Ò RC6 ໚¹¢Ò SCL ´Ñ§áÊ´§Ç§¨Ãàª×Íè Áµ‹Íã¹ÃÙ»·Õè L22-1 ʋǹµÑÇÍ‹ҧ â»Ãá¡ÃÁÀÒÉÒ C ·Õãè ªŒã¹¡Ò÷´Êͺ¡Ò÷íÒ§Ò¹áÊ´§ã¹â»Ãá¡ÃÁ·Õè L22-1

¡ÒÃàª×Íè Áµ‹Í·Ò§ÎÒÏ´áÇÏ 㪌ǧ¨Ãã¹ÃÙ»·Õè L22-1 㹡Ò÷´Åͧ l Ëҡ㪌ºÍÏ´·´Åͧ NX-877 plus II ãˌµÍ ‹ ǧ¨ÃµÒÁÃÙ»·Õè L22-1 l Ëҡ㪌ºÍÏ´ RBX-877rev.2007 ãˌµÍ ‹ ǧ¨ÃµÒÁÃÙ»·Õè L22-2 l


194 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

char I2CInit_=0; char y; int i,j; void Relay16(unsigned int x) { if (I2CInit_==0) { Soft_I2C_Config(&PORTC,7,6); Soft_I2C_Start(); // Soft_I2C_Write(0x40); // Soft_I2C_Write(0x06); // Soft_I2C_Write(0x00); // Soft_I2C_Stop(); // Soft_I2C_Start(); Soft_I2C_Write(0x40); Soft_I2C_Write(0x07); Soft_I2C_Write(0x00); Soft_I2C_Stop(); I2CInit_=1; } y=x%256; Soft_I2C_Start(); Soft_I2C_Write(0x40); Soft_I2C_Write(0x00); Soft_I2C_Write(y); Soft_I2C_Stop(); x=x/256; Soft_I2C_Start(); Soft_I2C_Write(0x40); Soft_I2C_Write(0x01); Soft_I2C_Write(x); Soft_I2C_Stop(); } void main() { while(1) { i=1; while(i<32768) { Relay16(i); Delay_ms(500); i=i*2; } Relay16(i); Delay_ms(500); } }

// Call initial L2C Start communication Address MCP23016 and Write Command Command for access IODIR0 Set DIR0 to OUTPUT Stop communication

// // // // //

Start communication Address MCP23016 and Write Command Command for access IODIR1 Set DIR0 to OUTPUT Stop communication

// // // // //

y=low Byte of data Issue I2C start signal Send byte via I2C (device address + W) Send byte (data address) Send byte (data address)

// // // // //

x=high byte Issue I2C start signal Send byte via I2C (device address + W) Send byte (data address) Send byte (data address)

// Loop // Start value // End loop value // Send Data to ZX-relay16 // Shift left // Send Last bit

¡Ò÷íÒ§Ò¹¢Í§â»Ãá¡ÃÁ â»Ãá¡ÃÁËÅÑ¡¨Ð¹íÒ¤‹Ò i ÁÒ·íÒ¡ÒÃàÅ×Íè ¹ºÔµä»·Ò§«ŒÒ áŌÇʋ§ãˌ¡ºÑ ¿˜§¡ª¹Ñè Relay16 () «Ö§è ·íÒ˹ŒÒ·Õè µÔ´µ‹Í¡ÑººÍÏ´ ZX-RELAY16 ÊíÒËÃѺ¿˜§¡ª¹Ñè Relay16() ¨ÐàÃÕ¡㪌¿§˜ ¡ª¹Ñè Soft_i2c à¾×Íè ¡íÒ˹´¡ÒõԴµ‹Í ¡ÑºÍØ»¡Ã³ÃкººÑÊ I2C ãˌᡋ¢Ò¾Íϵ·Õàè Å×͡㪌§Ò¹ ¨Ò¡¹Ñ¹é ÃѺ¤‹ÒµÑÇá»Ã x ࢌÒÁÒ á¡໚¹ 8 ºÔµÅ‹Ò§áÅÐ 8 ºÔµº¹ ¹íÒ¤‹Ò 8 ºÔµÅ‹Ò§Ê‹§ä»ÂѧáÍ´à´ÃÊ 0x00 à¾×Íè ¢Ñº RELAY1 ¶Ö§ 8 áÅйíÒ¤‹Ò 8 ºÔµº¹Ê‹§ä»ÂѧáÍ´à´ÃÊ 0x01 à¾×Íè ʋ§ä»¢Ñº RELAY9 ¶Ö§ 16

â»Ãá¡ÃÁ·Õè L20-1 ä¿Å SoftI2CTest.c â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺ¡ÒÃ㪌§Ò¹äźÃÒÃÕ Soft_i2c ¢Í§ mikroC ¤ÍÁä¾àÅÍÏ à¾×Íè µÔ´µ‹Í¡ÑºÃкººÑÊ I2C ´ŒÇ¢ҾÍϵã´æ ¢Í§äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC16F877/887


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 195

¢Ñ鹵͹¡Ò÷´Åͧ L22.1 ແ´â»Ãá¡ÃÁ mikroC ÊÌҧä¿Åâ»Ãà¨ç¡µ â´ÂàÅ×Í¡äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏáÅФÇÒÁ¶ÕÊè ­ Ñ ­Ò³¹Ò́¡Ò´Ñ§¹Õé l

¡Ã³Õ㪌ºÍÏ´ NX-877 plus II àÅ×Í¡ PIC16F877 ËÃ×Í PIC16F877A ¤ÇÒÁ¶ÕÊè ­ Ñ ­Ò³¹Ò́¡Ò 20MHz

l

¡Ã³Õ㪌ºÍÏ´ RBX-877 rev.2007 àÅ×Í¡ PIC16F887 ¤ÇÒÁ¶ÕÊè ­ Ñ ­Ò³¹Ò́¡Ò 20MHz

L22.2 ÊÌҧâ»Ãà¨ç¡µªÍ×è SoftI2CTest áÅоÔÁ¾â»Ãá¡ÃÁÀÒÉÒ C µÒÁâ»Ãá¡ÃÁ·Õè L22-1 L22.3 ·íÒ¡ÒäÍÁä¾ÅáÅдÒǹâËÅ´â»Ãá¡ÃÁà¾×Íè ·´Êͺ¼ÅÅѾ¸¡Ò÷íÒ§Ò¹ L22.4 ¨‹ÒÂä¿ãˌᡋºÍÏ´ ZX-RELAY16 áÅкÍÏ´·´ÅͧäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ L22.5 Ãѹâ»Ãá¡ÃÁ Êѧࡵ¡Ò÷íÒ§Ò¹¢Í§ LED áÅÐÃÕàŏº¹ºÍÏ´ ZX-RELAY16 ÊѧࡵàËç¹ LED ·Õµè Òí á˹‹§ RELAY1 µÔ´äŋÅÒí ´Ñºä»¨¹¶Ö§ RELAY16 áÅÐä´ŒÂ¹Ô àÊÕ§˹ŒÒÊÑÁ¼Ñʢͧ ÃÕàŏµÑ´µ‹ÍäŋÅíҴѺ¡Ñ¹ä»Í‹ҧµ‹Íà¹×èͧ


196 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 197

º··ÕÕè 12

¡ÒõԴµ‹ÍâÁ´ÙÅ SPI ¢Í§ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ¿˜§¡ª¹Ñè ¢Í§ mikroC ¤ÍÁä¾àÅÍÏ

âÁ´ÙÅàª×Íè Áµ‹ÍÍØ»¡Ã³Í¹Ø¡ÃÁËÃ×Í SPI (Serial Peripheral Interface) ໚¹âÁ´ÙÅ¿˜§¡ª¹Ñè ¾ÔàÈÉ ·Õ誋ÇÂãˌäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ÊÒÁÒöµÔ´µ‹Í¡ÑºÍØ»¡Ã³·Õè㪌ÊÒÂÊÑ­­Ò³ 3 ËÃ×Í 4 ÊÒµÒÁ Áҵðҹ Microwire ËÃ×Í SPI 䴌 â´Â·íÒ§Ò¹ÀÒÂ㵌ÊNj ¹¤Çº¤ØÁËÅÑ¡·Õªè Í×è Ç‹Ò âÁ´ÙÅàª×Íè Áµ‹Í͹ءÃÁ Ẻ«Ô§â¤Ã¹ÑÊ ËÃ×Í MSSP (Master Synchronous Serial Port module) ÀÒÂã¹âÁ´ÙÅ MSSP ºÃÃ¨Ø Ê‹Ç¹àª×Íè Áµ‹ÍÍØ»¡Ã³ 2 Ẻ¤×Í SPI (Serial Peripheral Interface) ËÃ×Íʋǹàª×Íè Áµ‹ÍÍØ»¡Ã³Í¹Ø¡ÃÁ áÅÐ I2C (Inter-Integrated Circuit) ËÃ×Íʋǹàª×Íè Áµ‹ÍÃкººÑÊ I2C ÊÒÂÊÑ­­Ò³·Õãè ªŒã¹¡ÒõԴµ‹ÍẺ SPI »ÃСͺ´ŒÇ ÊÒ¢ŒÍÁÙÅà¢ŒÒ (data in), ÊÒ¢ŒÍÁÙÅÍÍ¡ (data out), ÊÒÂÊÑ­­Ò³¹Ò́¡Ò (serial clock : SCK) áÅÐÊÒÂàÅ×Í¡ÍØ»¡Ã³ ÊàÅ¿ (slave select : SS) â´Âã¹Ãкº 3 ÊÒ¨ÐÃÇÁ¢Ò¢ŒÍÁÙÅࢌҴŒÇ¡ѹ µÑÇÍ‹ҧ¢Í§ÍØ»¡Ã³·ãÕè ªŒ¡ÒõԴµ‹ÍẺ¹Õäé ´Œá¡‹ äÍ«Õ˹‹Ç¤ÇÒÁ¨íÒÍÕÍ¾Õ ÃÍÁã¹Í¹Ø¡ÃÁ 25LCxxx, äÍ«Õá»Å§ÊÑ­­Ò³ÍйÒÅ͡໚¹´Ô¨µÔ ÍÅ (ઋ¹ ADS7841 ¢Í§ Texas Instrument), äÍ«Õá»Å§ÊÑ­­Ò³ ´Ô¨µÔ ÍÅ໚¹ÍйÒÅÍ¡ (ઋ¹ MCP4922 ¢Í§ Microchip), äÍ«Õàª×Íè Áµ‹Íà¤Ã×Í¢‹ÒÂÍÕà·ÍÏà¹çµ (ÍÒ·Ô ECN2862 ¢Í§ Microchip), äÍ«ÕÇ´Ñ ÍسËÀÙÁÔ (ઋ¹ DS1620 ¢Í§ Dallas Semiconductor), äÍ«ÕáÊ´§¼ÅµÑÇàÅ¢ 7 ʋǹËÅÒÂËÅÑ¡ (ઋ¹ MAX7219 ¢Í§ Maxim) ໚¹µŒ¹


198 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

12.1 ¡Ò÷íÒ§Ò¹¢Í§âÁ´ÙÅ SPI (Serial Peripheral Interface) àÁ×Íè àÍç¹àÍàºÔÅãˌâÁ´ÙÅ MSSP ·íÒ§Ò¹ã¹âËÁ´ SPI ¢ÒÊÑ­­Ò³·Õãè ªŒà»š¹ËÅÑ¡ÁÕ 3 ¢Ò¤×Í 1. ¢Ò¢ŒÍÁÙÅÍ͡Ẻ͹ءÃÁ (Serial Data Out : SDO) ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC16F877/887 ໚¹¢Ò·Õè 24 2. ¢Ò¢ŒÍÁÙÅࢌÒẺ͹ءÃÁ (Serial Data In : SDI) ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC16F877/887 ໚¹¢Ò·Õè 23 3. ¢ÒÊÑ­­Ò³¹Ò́¡Ò͹ءÃÁ (Serial Clock : SCK) ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC16F877/887 ໚¹¢Ò·Õè 18 ¶ŒÒËÒ¡àÅ×Í¡âËÁ´ÊàÅ¿ ¨ÐÁÕ¢ÒÊÑ­­Ò³·Õ赌ͧ㪌§Ò¹à¾ÔèÁÍÕ¡ 1 ¢Ò¤×Í ¢ÒàÅ×Í¡ÍØ»¡Ã³ÊàÅ¿ (Slave Select : SS) ÊíÒËÃѺäÁâ¤Ã¤¤Í¹â·ÃÅàÅÍÏ PIC16F877/887 ໚¹¢Ò·Õè 7 㹡Ò÷íÒ§Ò¹¢Í§âÁ´ÙÅ MSSP ã¹âËÁ´ SPI ÊÒÁÒöàÅ×Í¡ÃٻẺ·íҧҹ䴌ËÅÒ¡ËÅÒ â´Â ¡íÒ˹´ä´Œ¨Ò¡ºÔµ¤Çº¤ØÁã¹ÃÕ¨ÊÔ àµÍÏ SSPCON ºÔµ 0 ¶Ö§ 5 áÅÐÃÕ¨ÊÔ àµÍÏ SSPSTAT ºÔµ 6 áÅÐ 7 ÊíÒËÃѺÅíҴѺ¡ÒÃàÅ×͡㪌§Ò¹âËÁ´ SPI ÊÒÁÒöÊÃػ䴌´§Ñ ¹Õé (1) àÅ×Í¡âËÁ´ÁÒÊàµÍÏËÃ×ÍÊàÅ¿ àÁ×Íè àÅ×Í¡âËÁ´ÁÒÊàµÍÏ ¢Ò SCK ໚¹¢ÒàÍÒµ¾µØ ÊÑ­­Ò³¹Ò́¡Ò ¶ŒÒ໚¹âËÁ´ÊàÅ¿ ¢Ò SCK ໚¹¢ÒÍÔ¹¾ØµÊÑ­­Ò³¹Ò́¡Ò (2) àÅ×Í¡¢ÑÇé ¢Í§ÊÑ­­Ò³¹Ò́¡Ò (3) àÅ×Í¡¡ÒÃÊØÁ‹ ÊÑ­­Ò³¢ŒÍÁÙÅÍÔ¹¾Øµ (¡Ö§è ¡ÅÒ§ËÃ×ͪ‹Ç§»ÅÒÂ) (4) àÅ×Í¡¢Íº¢ÒÊÑ­­Ò³¹Ò́¡Ò (¢Íº¢Ò¢Ö¹é ËÃ×Íŧ) (5) àÅ×Í¡ÍѵÃÒÊÑ­­Ò³¹Ò́¡Ò 㹡óշÒí §Ò¹ã¹âËÁ´ÁÒÊàµÍÏ (6) àÅ×Í¡âËÁ´¢Í§ÍØ»¡Ã³ÊàÅ¿ 㹡óշÒí §Ò¹ã¹âËÁ´ÊàÅ¿ ã¹ÃÙ»·Õè 12-1 áÊ´§ä´ÍÐá¡ÃÁ¡Ò÷íÒ§Ò¹¢Í§âÁ´ÙÅ MSSP àÁ×Íè ·íÒ§Ò¹ã¹âËÁ´ SPI âÁ´ÙÅ MSSP »ÃСͺ´ŒÇªԿµÃ¨Õ ÊÔ àµÍϵÇÑ Ê‹§áÅÐÃѺ¢ŒÍÁÙÅËÃ×Í SSPSR áÅÐÃÕ¨ÊÔ àµÍϺ¿Ñ à¿ÍÏ SSPBUF ªÔ¿µÃ¨Õ ÊÔ àµÍÏ SSPSR 㪌ÊÒí ËÃѺàÅ×Íè ¹¢ŒÍÁÙÅࢌÒÍÍ¡¨Ò¡µÑÇÍØ»¡Ã³ â´ÂàÅ×Íè ¹ºÔµ MSB ¡‹Í¹ 㹡óÕÃºÑ ¢ŒÍÁÙÅ ¢ŒÍÁÙÅ͹ءÃÁ¨Ð·ÂͶ١ʋ§à¢ŒÒÁÒ·Ò§¢Ò SDI ࢌÒÊً SSPSR ¨¹¤Ãº 8 ºÔµËÃ×Í 1 亵 ¡ç¨Ð¶‹Ò·ʹä»ÂѧÃÕ¨ÊÔ àµÍÏ SSPBUF 㹡óÕʧ‹ ¢ŒÍÁÙÅ ¢ŒÍÁÙÅ·Õµè ͌ §¡ÒÃʋ§¨Ò¡ºÑÊ¢ŒÍÁÙŨж١ʋ§ÁÒÂѧ ÃÕ¨ÊÔ àµÍÏ SSPBUF áŌÇʋ§µ‹ÍÁÒÂѧ SSPSR ¨Ò¡¹Ñ¹é SSPSR ¨ÐàÅ×Íè ¹¢ŒÍÁÙÅÍÍ¡·Ò§¢Ò SDO Ẻ͹ءÃÁ


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 199

ºÑÊ¢ŒÍÁÙÅÀÒÂã¹ ÊÑ­­Ò³à¢Õ¹

ÊÑ­­Ò³Í‹Ò¹

ÃÕ¨ÔÊàµÍÏ SSPBUF

ªÔ¿µÃÕ¨ÔÊàµÍÏ SSPSR

RC4/SDI/SDA ºÔµ 0

ÊÑ­­Ò³¹ÒÌÔ¡Ò àÅ×è͹¢ŒÍÁÙÅ

RC5/SDO ÊÑ­­Ò³ Peripheral Output Enable

RA5/SS/AN4

SS

àÅ×Í¡ÊÑ­­Ò³¹ÒÌÔ¡Ò

ʋǹàÅ×Í¡ ¢Íº¢ÒÊÑ­­Ò³ ºÔµ SMP : CKE RC3/SCK/SCL

àÍÒµ¾Øµ¨Ò¡ TMR2 2

ʋǹàÅ×Í¡ ¢Íº¢ÒÊÑ­­Ò³

»ÃÕÊà¡ÅàÅÍÏ 4, 16, 64 SSPM3 : SSPM0

¢ŒÍÁÙÅ·Õè¨Ðʋ§ä»Âѧ SSPSR ºÔµ TRIS ¤Çº¤ØÁ·ÔÈ·Ò§

ÃÙ»·Õè 12-1 ä´ÍÐá¡ÃÁ¡Ò÷íÒ§Ò¹¢Í§âÁ´ÙÅ MSSP àÁ×Íè ·íÒ§Ò¹ã¹âËÁ´ SPI

TOSC


200 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

àÁ×Íè ¢ŒÍÁÙÅ㹺ѿà¿ÍÏàµçÁ á¿Å¡ BF (ºÔµ 0 ã¹ÃÕ¨ÊÔ àµÍÏ SSPDAT) ¨Ð૵ ¾ÃŒÍÁæ ¡Ñºá¿Å¡ SSPIF (ºÔµ 3 ã¹ÃÕ¨ÊÔ àµÍÏ PIR1) «Ö§è ·íÒãˌà¡Ô´¡ÒÃÍÔ¹àµÍÃÃ»Ñ µ¢¹Öé ËÒ¡ÁÕ¡ÒÃàÍç¹àÍàºÔÅänj ¨Ö§ÊÒÁÒö 㪌»ÃÐ⪹¨Ò¡á¿Å¡·Ñ§é Êͧ㹡ÒäǺ¤ØÁ¨Ñ§ËÇСÒÃà¢Õ¹áÅÐ͋ҹ¢ŒÍÁÙÅ à¾×Íè äÁ‹ãˌà¡Ô´¡Ò깡ѹ ¢Í§¢ŒÍÁÙÅ¢Öé¹ ´ŒÒ¹¤ÇÒÁ¶Õ¢è ͧÊÑ­­Ò³¹Ò́¡Òã¹âËÁ´ SPI ¹ÕÊé ÒÁÒöàÅ×͡䴌¶§Ö 4 ¤‹Ò¤×Í 1. ¤ÇÒÁ¶ÕÊè ­ Ñ ­Ò³¹Ò́¡ÒËÅÑ¡/4 «Ö§è ෋ҡѺ¤ÇÒÁ¶ÕÊè ­ Ñ ­Ò³¹Ò́¡ÒÀÒÂã¹ (TCY) 2. ¤ÇÒÁ¶ÕÊè ­ Ñ ­Ò³¹Ò́¡ÒËÅÑ¡/16 3. ¤ÇÒÁ¶ÕÊè ­ Ñ ­Ò³¹Ò́¡ÒËÅÑ¡/64 4. ¤‹Ò¢Í§ÃÕ¨ÊÔ àµÍÏ TMR2/2 ´Ñ§¹Ñ¹é ¤ÇÒÁ¶ÕÊè ­ Ñ ­Ò³¹Ò́¡ÒÊÙ§ÊØ´ã¹âËÁ´ SPI ¤×Í 5MHz ·Õ¤è ÃÔʵÍÅ 20MHz ¨Ò¡¢ŒÍÁÙÅ¢ŒÒ§µŒ¹ ÊÒÁÒöÊÃػ໚¹¢Ñ¹é µÍ¹¡ÒÃ㪌§Ò¹âÁ´ÙÅ MSSP à¾×Íè ·íÒ§Ò¹ã¹âËÁ´ SPI 䴌´Ñ§¹Õé (1) ¡íÒ˹´ãˌ¢Ò RC4/SDI ໚¹ÍÔ¹¾Øµ ¢Ò RC5/SDO áÅÐ RC3/SCK ໚¹àÍÒµ¾µØ ¾ÃŒÍÁ·Ñ駡íÒ˹´ãˌ¢Ò¾Íϵ·Õè㪌àÍç¹àÍàºÔÅÍØ»¡Ã³Êàſ໚¹¢Ò¾ÍϵàÍÒµ¾Øµ (2) ¡í Ò Ë¹´¤ÇÒÁ¶Õè ¢ ͧÊÑ ­ ­Ò³¹Ò́ ¡ Ò·Õè ã ªŒ ¡í Ò Ë¹´¨Ñ § ËÇСÒ÷í Ò §Ò¹¨Ò¡ºÔ µ SSPM3:SSPM0 áÅТÑÇé ¢Í§ÊÑ­­Ò³¹Ò́¡Ò¨Ò¡ºÔµ CKP ã¹ÃÕ¨ÊÔ àµÍÏ SSPCON ãˌµÃ§¡Ñº¤ÇÒÁµŒÍ§ ¡ÒâͧÍØ»¡Ã³ÊàÅ¿ ÊíÒËÃѺ¤ÇÒÁ¶Õµè ͌ §àÅ×Í¡ãˌµÒèí ¡Ç‹Ò¤ÇÒÁ¶Õ·è Òí §Ò¹¢Í§ÍØ»¡Ã³ÊàÅ¿ (3) àÅ×Í¡à¿Ê¢Í§ÊÑ­­Ò³¹Ò́¡Ò¨Ò¡ºÔµ CKE ã¹ÃÕ¨ÊÔ àµÍÏ SSPSTAT ãˌµÃ§¡ÑºÍØ»¡Ã³ ÊàÅ¿ (4) àÍç¹àÍàºÔÅÍØ»¡Ã³ÊàÅ¿ (5) àÍç¹àÍàºÔÅÍØ»¡Ã³ÁÒÊàµÍÏ â´Â¡ÒÃ૵ºÔµ SSPEN ã¹ÃÕ¨ÊÔ àµÍÏ SSPCON (6) à¢Õ¹¢ŒÍÁÙÅä»·ÕÃè ¨Õ ÊÔ àµÍÏ SSPBUF à¾×Íè Êѧè ãˌâÁ´ÙÅ SPI àÃÔÁè µŒ¹·íÒ§Ò¹ (7) ÃÍãˌ¡ÒÃÃѺ-ʋ§¢ŒÍÁÙÅàÊÃç¨ÊÁºÙó â´Â¡ÒõÃǨÊͺá¿Å¡ BF ã¹ÃÕ¨ÔÊàµÍÏ SSPSTAT ¶ŒÒºÔµ¹Õàé «µà»š¹ “1” áÊ´§Ç‹Ò¡ÒÃÃѺʋ§¢ŒÍÁÙÅã¹äºµ¹¹Ñé æ àÊÃç¨ÊÁºÙóáÅŒÇ (8) ͋ҹ¢ŒÍÁÙÅ·ÕÃè ºÑ ä´Œ¨Ò¡ÃÕ¨ÊÔ àµÍÏ SSPBUF «Ö§è ¡ÒÃ͋ҹ¢ŒÍÁÙÅ·ÕÃè ¨Õ ÊÔ àµÍϵÇÑ ¹Õé ¨Ð໚¹ ¡ÒÃà¤ÅÕÂÏá¿Å¡ BF â´ÂÍѵâ¹ÁÑµÔ (9) ¹íÒ¢ŒÍÁÙÅä»»ÃÐÁÇżÅËÃ×Í·íÒ¡ÒÃÃѺ-ʋ§ ¢ŒÍÁÙÅã¹äºµ¶´Ñ ä»


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

SPI ÁÒÊàµÍÏ (SSPM3 : SSPM0 = 00xx)

SPI ÊàÅ¿ (SSPM3 : SSPM0 = 010x) SDO

SDI

ÃÕ¨ÊÔ àµÍÏ SSPBUF

ªÔ¿µÃ¨Õ ÊÔ àµÍÏ SSPSR MSB

ÃÕ¨ÊÔ àµÍÏ SSPBUF

SDI

SDO

LSB

äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ 1

l 201

ªÔ¿µÃ¨Õ ÊÔ àµÍÏ SSPSR MSB

SCK

SCK

LSB

äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ 2

ÃÙ»·Õè 12-2 ¡ÒÃàª×Íè Áµ‹ÍäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ 2 µÑÇࢌҴŒÇ¡ѹâ´Â㪌âÁ´ÙÅ SPI

12.2 ÃٻẺ¡ÒÃàª×Íè Áµ‹Í 㹡óշÕè¡íÒ˹´ãˌäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC16F877/887 ·íÒ§Ò¹ã¹âËÁ´ SPI ÁÒÊàµÍÏ ÍØ»¡Ã³·¹Õè Òí ÁÒàª×Íè Áµ‹Í·Ñ§é ËÁ´¨Ö§à»š¹ÍØ»¡Ã³ÊàÅ¿ áÅÐ㪌§Ò¹à¾Õ§ 3 ¢Ò¤×Í SDO, SDI áÅÐ SCK áÅдŒÇ¤ÇÒÁÊÒÁÒö¢Í§âÁ´ÙÅ MSSP ã¹âËÁ´ SPI ÊÒÁÒö·Õ¨è СíÒ˹´ãˌ·Òí §Ò¹ä´Œ·§Ñé ÁÒÊàµÍÏ áÅÐÊàÅ¿ ã¹ÃÙ»·Õè 12-2 áÊ´§¡ÒÃàª×Íè Áµ‹ÍäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏÃNj Á¡Ñ¹àÁ×Íè ·íÒ§Ò¹ã¹âËÁ´ SPI â´Â µÑÇ˹Ö觨Ð໚¹ÍØ»¡Ã³ÁÒÊàµÍÏáÅÐÍÕ¡µÑÇ˹Ö觵ŒÍ§à»š¹ÍØ»¡Ã³ÊàÅ¿


202 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

12.3 äźÃÒÃÕàª×Íè Áµ‹ÍÍØ»¡Ã³Í¹Ø¡ÃÁËÃ×Í SPI 12.3.1 ¿˜§¡ª¹Ñè Spi_Init ໚¹¿˜§¡ª¹Ñè ¡íÒ˹´¤‹ÒàÃÔÁè µŒ¹á¡‹¡ÒÃàª×Íè Áµ‹ÍẺ SPI Áҵðҹ â´Â¤‹ÒÁҵðҹ»ÃСͺ´ŒÇ 1. ·íҧҹ໚¹âËÁ´ÁÒÊàµÍÏ 2. ¤ÇÒÁ¶ÕÊè ­ Ñ ­Ò³¹Ò́¡Ò Fosc/4 3. ʶҹÐËÂØ´¹Ô§è ¢Í§¢ÒÊÑ­­Ò³¹Ò́¡ÒÅͨԡ “0” 4. ¡ÒÃʋ§¢ŒÍÁÙÅà¡Ô´¢Ö¹é ·Õ¢è ͺ¢Ò¢Ö¹é 5. µÃǨÃѺÊÑ­­Ò³ÍÔ¹¾Øµà¢ŒÒÁÒ·Õµè Òí á˹‹§¡Ö§è ¡ÅÒ§¢Í§ÅÙ¡¤Å×¹è ÊíÒËÃѺ¡ÒûÃѺᵋ§¤‹Ò·Õäè Á‹à»š¹µÒÁÁҵðҹ¹Õ¨é еŒÍ§àÃÕ¡㪌¿§˜ ¡ª¹Ñè Spi_Init_Advanced ÃٻẺ¡ÒÃ㪌§Ò¹¤íÒÊѧè void Spi_Init(void);

µÑÇÍ‹ҧ·Õè 12-1 Spi_Init();

12.3.2 ¿˜§¡ª¹Ñè Spi_Init_Advanced ໚¹¡ÒáíÒ˹´¤‹ÒàÃÔÁè µŒ¹ â´Â·íҧҹ໚¹âËÁ´ÁÒÊàµÍÏ ¤ÇÒÁ¶ÕÊè ­ Ñ ­Ò³¹Ò́¡Ò Fosc/4 µÃǨ ÃѺÊÑ­­Ò³ÍÔ¹¾Øµà¢ŒÒÁÒ·Õµè Òí á˹‹§¡Ö§è ¡ÅÒ§¢Í§ÅÙ¡¤Å×¹è ʶҹÐËÂØ´¹Ô§è ¢Í§¢ÒÊÑ­­Ò³¹Ò́¡ÒÅͨԡ “0” áÅСÒÃʋ§¢ŒÍÁÙÅà¡Ô´¢Ö¹é ·Õ¢è ͺ¢Ò¢Ö¹é ÃٻẺ¡ÒÃ㪌§Ò¹¤íÒÊѧè void Spi_Init_Advanced(unsigned short master, unsigned short data_sample, unsigned short clock_idle, unsigned short transmit_edge);

µÑÇá»Ã

㪌¡Òí ˹´âËÁ´¡Ò÷íÒ§Ò¹¢Í§ SPI áÅÐÊÑ­­Ò³¹Ò́¡Ò MASTER_OSC_DIV4 // ·íÒ§Ò¹ã¹âËÁ´ÁÒÊàµÍÏ áÅÐÊÑ­­Ò³¹Ò́¡Ò෋ҡѺ Fosc/4 MASTER_OSC_DIV16 // ·íÒ§Ò¹ã¹âËÁ´ÁÒÊàµÍÏ áÅÐÊÑ­­Ò³¹Ò́¡Ò෋ҡѺ Fosc/16 MASTER_OSC_DIV64 // ·íÒ§Ò¹ã¹âËÁ´ÁÒÊàµÍÏ áÅÐÊÑ­­Ò³¹Ò́¡Ò෋ҡѺ Fosc/64 MASTER_TMR2 // ·íÒ§Ò¹ã¹âËÁ´ÁÒÊàµÍÏ áÅÐ㪌ʭ Ñ ­Ò³¹Ò́¡Ò¨Ò¡ä·àÁÍÏ 2 SLAVE_SS_ENABLE // àÍç¹àÍàºÔÅ¡ÒÃàÅ×Í¡âËÁ´ÁÒÊàµÍÏËÃ×ÍÊàÅ¿ SLAVE_SS_DIS // ´ÔÊàÍàºÔÅ¡ÒÃàÅ×Í¡âËÁ´ÁÒÊàµÍÏËÃ×ÍÊàÅ¿

master


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 203

໚¹¡ÒáíÒ˹´µíÒá˹‹§ã¹¡ÒõÃǨ¨ÑºÊÑ­­Ò³ÍÔ¹¾Øµ â´Â DATA_SAMPLE_MIDDLE // µÃǨ¨ÑºÊÑ­­Ò³·Õµ è Òí á˹‹§¡ÅÒ§¢Í§ÃÙ»¤Å×¹è DATA_SAMPLE_END // µÃǨ¨ÑºÊÑ­­Ò³·Õµ è Òí á˹‹§»ÅÒÂÊØ´¢Í§ÃÙ»¤Å×¹è

data_sample

໚¹¡ÒáíÒ˹´Ê¶Ò¹ÐËÂØ´¹Ô§è ¢Í§¢ÒÊÑ­­Ò³¹Ò́¡Ò CLK_IDLE_HIGH // ÊÀÒÇÐËÂØ´¹Ô§è ໚¹Åͨԡ “1” CLK_IDLE_LOW // ÊÀÒÇÐËÂØ´¹Ô§è ໚¹Åͨԡ “0”

clock_idle

໚¹¡ÒáíÒ˹´¢Íº¢ÒÊíÒËÃѺ¡ÒÃʋ§¢ŒÍÁÙÅÍÍ¡ä» LOW_2_HIGH // ¢ŒÍÁÙŶ١ʋ§àÁ×Í è ÊÑ­­Ò³¹Ò́¡Òà»ÅÕÂè ¹¨Ò¡ “0” ä»à»š¹ “1” HIGH_2_LOW // ¢ŒÍÁÙŶ١ʋ§àÁ×Í è ÊÑ­­Ò³¹Ò́¡Òà»ÅÕÂè ¹¨Ò¡ “1” ä»à»š¹ “0”

transmit_edge

µÑÇÍ‹ҧ·Õè 12-2 Spi_Init_Advanced(MASTER_OSC_DIV4, DATA_SAMPLE_MIDDLE, CLK_IDLE_LOW, LOW_2_HIGH)

3.12.3 ¿˜§¡ª¹Ñè Spi_Read ໚¹¿˜§¡ª¹Ñè ʋ§¤‹Òã¹µÑÇá»Ã buffer Í͡仾ÌÍÁÊÑ­­Ò³¹Ò́¡Ò áÅÐÃÍÃѺ¤‹Ò¢ŒÍÁÙÅ¡ÅѺÁÒ ÃٻẺ¡ÒÃ㪌§Ò¹¤íÒÊѧè unsigned short Spi_Read(unsigned short buffer);

µÑÇÍ‹ҧ·Õè 12-3 short take, buffer; ... take = Spi_Read(buffer);

໚¹¡ÒÃ͋ҹ¢ŒÍÁÙŨҡÍØ»¡Ã³ SPI ÁÒà¡çºänj·µÕè ÇÑ á»Ã take

3.12.4 ¿˜§¡ª¹Ñè Spi_Write ໚¹¿˜§¡ª¹Ñè ʋ§¢ŒÍÁÙÅä»ÂѧÍØ»¡Ã³ SPI â´Â¢ŒÍÁÙÅã¹µÑÇá»Ã data¨Ð¶Ù¡à¢Õ¹ä»ÂѧÃÕ¨ÊÔ àµÍÏ SSPBUF áÅÐàÃÔÁè µŒ¹¡ÒÃʋ§¢ŒÍÁÙÅ ÃٻẺ¡ÒÃ㪌§Ò¹¤íÒÊѧè void Spi_Write(unsigned short data);

µÑÇÍ‹ҧ·Õè 12-4 Spi_Write(1);

໚¹¡ÒÃʋ§¢ŒÍÁÙŵÑÇàÅ¢ 1 ÍÍ¡ä»ÂѧÍØ»¡Ã³ SPI


204 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

»¯ÔºµÑ ¡Ô Ò÷Õè 23 µÔ´µ‹Í¡Ñº MCP4922 äÍ«Õá»Å§ ÊÑ­­Ò³´Ô¨µÔ ÍÅ໚¹ÍйÒÅÍ¡ 㹡Ò÷´Åͧ¹Õ¹é Òí àʹͶ֧ÇÔ¸¡Õ ÒÃ㪌§Ò¹áÅеÑÇÍ‹ҧ¡ÒÃàª×Íè Áµ‹Í¡ÑºÍØ»¡Ã³·ãÕè ªŒ¡ÒõԴµ‹ÍẺ SPI ÍØ»¡Ã³ ·Õ¹è Òí ÁÒ㪌໚¹µÑÇÍ‹ҧ¤×Í MCP4922 äÍ«Õá»Å§ÊÑ­­Ò³´Ô¨µÔ ÍÅ໚¹ÍйÒÅÍ¡ (DAC) ¤ÇÒÁÅÐàÍÕ´ 12 ºÔµ

¤Ø³ÊÁºÑµ¢Ô ͧ MCP4922 äÍ«Õá»Å§ÊÑ­­Ò³´Ô¨µÔ ÍÅ໚¹ÍйÒÅÍ¡ ¤ÇÒÁÅÐàÍÕ´㹡ÒÃá»Å§ÊÑ­­Ò³ÊÙ§¶Ö§ 12 ºÔµ l ÁÕàÍÒµ¾µ Ø 2 ª‹Í§ ÊÒÁÒöàÅ×Í¡ãˌ·Òí §Ò¹à¾Õ§ª‹Í§à´ÕÂÇ䴌 l µÔ´µ‹Í¼‹Ò¹ÃкººÑÊ SPI ¤ÇÒÁ¶ÕÊ è­ Ñ ­Ò³¹Ò́¡ÒÊÙ§ÊØ´ 20MHz l ÊÒÁÒöáŵªàÍÒµ¾µ Ø ä´Œ l ä¿àÅÕÂ é § +2.7 ¶Ö§ +5.5V ÃÙ»·Õè L23-1 áÊ´§¡ÒèѴ¢ÒáÅÐÃÒÂÅÐàÍÕ´¢ŒÍÁÙÅã¹ÃÕ¨ÊÔ àµÍÏ¡Òí ˹´¡Ò÷íÒ§Ò¹ã¹äÍ«Õ MCP4922 l

ºÔµ 15

ºÔµ 14

ºÔµ 13

W -x

W -x

ºÔµ 7

11 VREFB

D7

10 VOUTB

W -x

14 VOUTA

NC 2

13 VREFA

CS 3 SCK 4 SDI 5

MCP4922

VDD 1

12 AVss

NC 6

9 SHDN

NC 7

8 LDAC

A/B

GA

ºÔµ 12

SHDN

ºÔµ 11

ºÔµ 10

ºÔµ 9

ºÔµ 8

W -x

W -0

W -x

W -x

W -x

W -x

ºÔµ 6

ºÔµ 5

ºÔµ 4

ºÔµ 3

ºÔµ 2

ºÔµ 1

ºÔµ 0

W -x

W -x

W -x

W -x

W -x

W -x

W -x

BUF

D6

D5

D4

D11

D3

D10

D2

D9

D1

D8

D0

ºÔµ 15 - A/B (DACA/DACB select bit) : ºÔµàÅ×͡ǧ¨Ãá»Å§ÊÑ­­Ò³·Õµè ͌ §¡ÒõԴµ‹Í´ŒÇ “0” = à¢Õ¹¢ŒÍÁÙÅä»Âѧ DACA “1” = à¢Õ¹¢ŒÍÁÙÅä»Âѧ DACB ºÔµ 14 - BUF (VREF Input Buffer Control bit) : ºÔµàÅ×Í¡ºÑ¿à¿ÍÏãËŒá¡‹Í¹Ô ¾Øµáç´Ñ¹ÍŒÒ§ÍÔ§ “0” = äÁ‹àÅ×͡㪌ǧ¨ÃºÑ¿à¿ÍÏ “1” = àÅ×͡㪌ǧ¨ÃºÑ¿à¿ÍÏ ºÔµ 13 - GA (Output Gain Select bit) : ºÔµàÅ×Í¡ÍѵÃÒ¢ÂÒÂÊÑ­­Ò³àÍÒµ¾µØ “0” = ¢ÂÒ 2 à·‹Ò ¹Ñ¹è ¤×Í áç´Ñ¹àÍÒµ¾µØ ෋ҡѺ 2 x VREF x ¢ŒÍÁÙÅ´Ô¨µÔ ÍÅ/4096 “1” = äÁ‹¢ÂÒ ¹Ñ¹è ¤×Í áç´Ñ¹àÍÒµ¾µØ ෋ҡѺ VREF x ¢ŒÍÁÙÅ´Ô¨µÔ ÍÅ/4096 ºÔµ 12 - SHDN (Output Power Down Control bit) : ºÔµ¤Çº¤ØÁ¡Òè‹Ò¾Åѧ§Ò¹á¡‹Ç§¨ÃàÍÒµ¾µØ “0” = ´ÔÊàÍàºÔÅǧ¨ÃºÑ¿à¿ÍÏàÍÒµ¾µØ ·íÒãˌ¢ÒàÍÒµ¾µØ ÁÕÍÁÔ ¾Õá´¹«Ê§Ù “1” = ¤Çº¤ØÁãˌÁ¡Õ ÒÃÁÕ¡Òè‹Ò¾Åѧ§Ò¹á¡‹Ç§¨ÃàÍÒµ¾µØ ºÔµ 0 ¶Ö§ 11 - D0 ¶Ö§ D11 (DAC Data bits) : ºÔµ¢ŒÍÁÙÅ´Ô¨µÔ ÍÅ 12 ºÔµ

ÃÙ»·Õè L23-1 ¡ÒèѴ¢ÒáÅТŒÍÁÙŢͧÃÕ¨ÊÔ àµÍÏ¡Òí ˹´¡Ò÷íÒ§Ò¹¢Í§äÍ«Õ MCP4922


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 205

CS 



!

"

#

$

%

&

'







!

"

#

D3

D2

D1

D0

SCK ºÔµ¤Çº¤ØÁ SDI

A/B

BUF

GA

ºÔµ¢ŒÍÁÙÅ 12 ºÔµ SHDN

D11

D10

D9

D8

D7

D6

D5

D4

áç´Ñ¹àÍÒµ¾µØ µÒÁ·Õ¡è Òí ˹´

Vout

ÃÙ»·Õè L23-2 ä´ÍÐá¡ÃÁàÇÅÒ㹡Ò÷íÒ§Ò¹¢Í§äÍ«Õ MCP4922

ÃÕ¨ÊÔ àµÍÏà¢Õ¹¤íÒÊÑ§è ¢Í§ MCP4922 MCP4922 ÁÕÃ¨Õ ÊÔ àµÍÏÊÒí ËÃѺ¡íÒ˹´¡Ò÷íÒ§Ò¹áÅÐÃѺ¢ŒÍÁÙÅ´Ô¨µÔ ÍÅà¾Õ§µÑÇà´ÕÂÇ ·íÒãˌ§Ò‹ µ‹Í¡ÒÃ㪌§Ò¹ ¹Ñ¹è ¤×Í ÃÕ¨ÊÔ àµÍÏà¢Õ¹¤íÒÊѧè (WRTE COMMAD) ã¹ÃÙ»·Õè L23-2 áÊ´§ä´ÍÐá¡ÃÁàÇÅҢͧ¡ÒÃà¢Õ¹¢ŒÍÁÙÅä»ÂѧÃÕ¨ÊÔ àµÍϤÒí ÊÑ§è ¢Í§äÍ«Õ MCP4922 ¨ÐàËç¹ ä´ŒÇҋ ¡ÒÃàÃÔÁè µŒ¹µÔ´µ‹Í¡Ñº MCP4922 µŒÍ§·íÒãˌ¢Ò CS ໚¹ “0” ¨Ò¡¹Ñ¹é »‡Í¹ÊÑ­­Ò³¹Ò́¡Òࢌҷբè Ò SCK µ‹Í´ŒÇ à¢Õ¹¢ŒÍÁÙÅ¡íÒ˹´¡Ò÷íÒ§Ò¹ËÃ×Í Config bit 4 ºÔµº¹ µ‹Í´ŒÇºԵ¢ŒÍÁÙÅ´Ô¨µÔ ÍÅÍÕ¡ 12 ºÔµ ã¹¢³Ðà´ÕÂǡѹµŒÍ§ ·íÒãˌ¢Ò LDAC ໚¹ “1” ¤ŒÒ§änj¨¹¡Ç‹Ò¨Ðʋ§¢ŒÍÁÙŤú áŌÇà»ÅÕÂè ¹à»š¹Åͨԡ “0” ·Õ¢è Ò CS ¡çµÍŒ §à»ÅÕÂè ¹à»š¹Åͨԡ “1” ´ŒÇ áç´Ñ¹ÍйÒÅÍ¡¨Ö§¶Ù¡Ê‹§ÍÍ¡ä»·Ò§ Vout ¨Ò¡ÃٻẺ¡ÒõԴµ‹Í㪌§Ò¹´Ñ§·Õ¡è ŋÒÇÁÒáÅŒÇ ÊÒÁÒö¹íÒ MCP4922 ÁÒ㪌§Ò¹Ã‹ÇÁ¡Ñº PIC16F877 䴌 ´Ñ§áÊ´§ã¹ÃÙ»·Õè L23-3 â´Â¨Ð㪌¢Ò·Õµè ͋ 㪌§Ò¹à¾Õ§ 3 àʌ¹à·‹Ò¹Ñ¹é ¢Ò SDI «Ö§è ໚¹¢ÒÃѺ¢ŒÍÁÙÅÊíÒËÃѺ¡ÒõԴµ‹Í ¡Ñº MCP4922 äÁ‹¨Òí ໚¹µŒÍ§ãªŒ§Ò¹ ÊíÒËÃѺâ»Ãá¡ÃÁà¾×Íè ÊÑ§è §Ò¹ãˌ MCP4922 ·íÒ§Ò¹¹Ñ¹é áÊ´§ã¹â»Ãá¡ÃÁ·Õè L23-1 â´Â ¨Ð໚¹¡ÒÃʋ§¤‹Ò¢ŒÍÁÙÅä»Âѧ MCP4922 ·Ñ§é ª‹Í§ A áÅÐ B â´Â¨Ðà¾ÔÁè ¤‹Òáç´Ñ¹¢Ö¹é ·Ø¡æ 3 ÇÔ¹Ò·Õ ¡ÒÃà¾ÔÁè áç´Ñ¹à»š¹¡ÒÃà¾ÔÁè Ẻ·ÇÕ¤³ Ù ¤‹Ò¢Ö¹é àÃ×Íè Â æ ¡ÒõÃǨÊͺ¼ÅÅѾ¸¨ÐãªŒÇ¸Ô ¡Õ ÒùíÒÁÑŵÔÁàÔ µÍÃÇ´Ñ ·Õ¢è ÒàÍÒµ¾µØ ¢Í§ äÍ«Õ MCP4922

¡ÒÃàª×Íè Áµ‹Í·Ò§ÎÒÏ´áÇÏ l

㪌ǧ¨Ãã¹ÃÙ»·Õè L23-3 㹡Ò÷´Åͧ

¢Ñ鹵͹¡Ò÷´Åͧ L23.1 ແ´â»Ãá¡ÃÁ mikroC ÊÌҧä¿Åâ»Ãà¨ç¡µ â´ÂàÅ×Í¡äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏáÅФÇÒÁ¶ÕÊè ­ Ñ ­Ò³¹Ò́¡Ò´Ñ§¹Õé l

¡Ã³Õ㪌ºÍÏ´ NX-877 plus II àÅ×Í¡ PIC16F877 ËÃ×Í PIC16F877A ¤ÇÒÁ¶ÕÊè ­ Ñ ­Ò³¹Ò́¡Ò 20MHz


206 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

#include <built_in.h> char i; int vout; //***********************************' // MCP4922 Configuration Constants ' //***********************************' const char A_B1_G1 = 0x70; //Channel A Buffered x1Gain const char A_B1_G2 = 0x50; //Channel A Buffered x2Gain const char A_B0_G1 = 0x30; //Channel A nonBuffered x1Gain const char A_B0_G2 = 0x10; //Channel A nonBuffered x2Gain const char B_B1_G1 = 0xF0; //Channel B Buffered x1Gain const char B_B1_G2 = 0xD0; //Channel B Buffered x2Gain const char B_B0_G1 = 0xB0; //Channel B nonBuffered x1Gain const char B_B0_G2 = 0x90; //Channel B nonBuffered x2Gain const char SHDN = 0x00; //Channel A & B Output = High Impedance void DAC_OUT(char config,int output) { PORTC.F0=0; // Enable Chip Select SPI_Write(config+Hi(output)); SPI_Write(Lo(output)); PORTC.F0=1; // 'DAC output refreshed at this point } void main() { PORTC=0x00; // clear portC TRISC.F0=0; // Chip select line set to output Spi_Init(); // Initial Spi PORTC.F0=1; // Disable Chip Select DAC_OUT(SHDN,0); while(1) { for (i=1;i<13;i++) { vout=(1<<i)-1; DAC_OUT(A_B0_G1,vout); // Output on channel A DAC_OUT(B_B0_G1,vout); // Output on channel B Delay_ms(3000); // 3 Second delay to watch output on mulltimeter } } }

¡Ò÷íÒ§Ò¹¢Í§â»Ãá¡ÃÁ ¿˜§¡ª¹Ñè DAC_OUT ·íÒ˹ŒÒ·ÕÊè §‹ ¢ŒÍÁÙÅ 2 亵ä»ÂѧäÍ«Õ MCP4922 â´ÂàÃÔÁè ¨Ò¡àÍç¹àÍàºÔÅ¡ÒõԴµ‹Í¡Ñº MCP4922 ¨Ò¡¹Ñ¹é ʋ§¢ŒÍÁÙÅ¡íÒ˹´¡Ò÷íҧҹ仾ÌÍÁ¡Ñº¢ŒÍÁÙÅ 4 ºÔµº¹¢Í§¤‹Ò´Ô¨µÔ ÍÅ·Õµè ͌ §¡ÒÃʋ§ µÒÁ´ŒÇ ¢ŒÍÁÙÅ 8 ºÔµÅ‹Ò§ (¡ÒÃá¡ 4 ºÔµº¹ áÅÐ 8 ºÔµÅ‹Ò§¨Ð㪌¿§˜ ¡ª¹Ñè hi áÅÐ lo ÁÒª‹Ç â´ÂàÁ×Íè 㪌§Ò¹¿˜§¡ª¹Ñè ¹Õ¨é Ð µŒÍ§ include ä¿Å build_in.h à¾ÔÁè àµÔÁࢌÒÁÒ´ŒÇÂ) ÊíÒËÃѺâ»Ãá¡ÃÁËÅÑ¡ ¨Ð¡íÒ˹´¢Ò¾Íϵ RC0 ໚¹àÍÒµ¾µØ ʋǹ¢ÒÍ×¹è æ ¨Ð¶Ù¡¡íÒ˹´¨Ò¡¿˜§¡ª¹Ñè Spi_Init() ¨Ò¡¹Ñ¹ é Êѧè ãˌâ»Ãá¡ÃÁǹʋ§¢ŒÍÁÙÅä»Âѧ MCP4922 «Ö§è ¡íÒ˹´ãˌ·Òí §Ò¹ã¹âËÁ´äÁ‹ãªŒº¿Ñ à¿ÍÏ ÍѵÃÒ¢ÂÒÂ෋ҡѺ 1 â´Â㹡ÒÃʋ§¢ŒÍÁÙÅ´Ô¨µÔ ÍÅ·Õµè ͌ §¡ÒÃá»Å§¤‹Ò໚¹áç´Ñ¹¨Ðà¾ÔÁè ¤‹Ò¢Ö¹é ¤Ãѧé ÅÐ 2 à·‹Ò àª‹¹ ¨Ò¡ 0 ໚¹ 2, 4, 8 ໚¹µŒ¹

â»Ãá¡ÃÁ·Õè L23-1 ä¿Å SPItest.c â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺ mikroC ¤ÍÁä¾àÅÍÏà¾×Íè µÔ´µ‹Í¡ÑºäÍ«Õ MCP4922 â´Â㪌¿§˜ ¡ª¹Ñè SPI ¢Í§ mikroC


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

+5V

+5V

11

VDD

1 VDD

32

VDD

VREFB

15 RC0 18 RC3/SCK 24 RC5/SDO

3 CS 4 SCK 5 SDI 8 LDAC

VREFA SHDN VOUTA

IC2 MCP4922

IC1 PIC16F877-20

OSC1 13 C1 22pF

l 207

VOUTB VSS 12

11 13 9 14 DC outputA 10 DC outputB

OSC2 14 XTAL1 20MHz

C2 22pF

ÃÙ»·Õè L23-3 ǧ¨Ã·´Åͧ¡ÒÃàª×Íè Áµ‹ÍäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC16F877 ¡ÑºÍØ»¡Ã³ SPI «Ö§è ã¹·Õ¹è ¤Õé Í× äÍ«ÕàºÍÏ MCP4922 L23.2 ÊÌҧâ»Ãà¨ç¡µªÍ×è SPITest áÅоÔÁ¾â»Ãá¡ÃÁÀÒÉÒ C µÒÁâ»Ãá¡ÃÁ·Õè L23-1 L23.3 ·íÒ¡ÒäÍÁä¾ÅáÅдÒǹâËÅ´â»Ãá¡ÃÁà¾×Íè ·´Êͺ¼ÅÅѾ¸¡Ò÷íÒ§Ò¹ L23.3 àµÃÕÂÁ¡Òõ‹Í㪌§Ò¹ÁÑŵÔÁàµÍÏ â´ÂàÅ×͡‹ҹÇÑ´áç´Ñ¹ +10Vdc ¢Ö¹é ä» áŌǵ‹ÍÊÒºǡࢌҷբè Ò 14 ¢Í§äÍ«Õ MCP4922 ʋǹÊÒÂźµ‹Í¡Ñº¡ÃÒÇ´ L23.4 Ãѹâ»Ãá¡ÃÁ Êѧࡵ¼Å¡Ò÷íÒ§Ò¹¼‹Ò¹ÁÑŵÔÁàÔ µÍÏ ¨ÐÊѧࡵàË繤‹Òáç´Ñ¹à¾ÔÁè ¢Ö¹é Í‹ҧµ‹Íà¹×Íè § ¨Ò¡ 0 ¨¹¶Ö§ +5Vdc ¨Ò¡¹Ñ¹é ¡ÅѺä»àÃÔÁè µŒ¹·Õè 0V ÍÕ¡¤Ãѧé â´Â¡ÒÃà»ÅÕÂè ¹á»Å§áç´Ñ¹¨Ðà¡Ô´¢Ö¹é ·Ø¡æ 3 ÇÔ¹Ò·Õ


208 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 209

º··ÕÕè 13

¡ÒõԴµ‹ÍÍØ»¡Ã³ÃкººÑÊ 1 ÊÒ ´ŒÇ¿˜§¡ª¹Ñè ¢Í§ mikroC ¤ÍÁä¾àÅÍÏ ÃкººÑÊ 1 ÊÒ (1-Wire bus) ¼Ù¤Œ ¹Œ ¤Ô´¤×Í ´ÑÅÅÑÊà«ÁԤ͹´Ñ¡àµÍÏ ÃкºÊ×Íè ÊÒâŒÍÁÙÅẺ¹ÕÁé Õ ¤ÇÒÁªÒ­©ÅÒ´áÅÐ㪌ÊÒÂÊÑ­­Ò³à¾Õ§ 1 àʌ¹à·‹Ò¹Ñé¹ äÁ‹µŒÍ§ÁÕÊÒÂÊÑ­­Ò³¹Ò́¡Ò¤Çº¤ØÁ¨Ñ§ËÇÐ ¡Òö‹Ò·ʹ¢ŒÍÁÙÅàËÁ×͹¡ÑºÃкºÊ×Íè ÊÒÃ͹ءÃÁẺÍ×¹è æ à¹×Íè §¨Ò¡ÊÒÂÊÑ­­Ò³¹Ñ¹é ¨Ð·íÒ˹ŒÒ·Õàè »š¹ ÊÒÂÊÑ­­Ò³¹Ò́¡Òã¹µÑÇ Ê‹Ç¹¤‹Ò¢Í§¢ŒÍÁÙžԨÒóҨҡÅѡɳТͧÃÙ»ÊÑ­­Ò³·Õ»è ÃÒ¡¯º¹ÊÒÂ㹠ᵋÅЪ‹Í§¢Í§àÇÅÒËÃ×ÍàÃÕÂ¡Ç‹Ò ä·ÁÊÅç͵ (time-slot) â´Â¤ÒºàÇÅÒµèÒí ÊØ´áÅÐÊÙ§ÊØ´¢Í§Ê¶Ò¹Ðµ‹Ò§æ 㹡ÒÃÊ×Íè ÊÒâŒÍÁÙÅã¹áµ‹ÅÐä·ÁÊÅç͵ÁÕ¡ÒáíÒ˹´¢Íºà¢µänjÍ‹ҧªÑ´à¨¹ ¡Òö‹Ò·ʹ¢ŒÍÁÙŨÐà¡Ô´ ¢Ö¹é ã¹áµ‹ÅÐä·ÁÊÅç͵¹Ñ¹é ¡Òö‹Ò·ʹ¢ŒÍÁÙÅ໚¹áººÍЫԧâ¤Ã¹ÑÊã¹ÃдѺºÔµ

13.1 ¤Ø³ÊÁºÑµ·Ô ҧ෤¹Ô¤¢Í§ÃкººÑÊ˹֧è ÊÒ ÊÒÂÊÑ­­Ò³º¹ÃкººÑʹÕàé »š¹ÊÒÂÊÑ­­Ò³áººÊͧ·ÔÈ·Ò§ ᵋ¢ÍŒ ÁÙÅÊÒÁÒöà´Ô¹·Ò§ä´Œã¹·ÔÈ ·Ò§à´ÕÂÇÀÒÂ㹪‹Ç§àÇÅÒ˹֧è æ ÍØ»¡Ã³º¹ÃкººÑʵŒÍ§ÁÕ¡ÒÃÃкØÍ‹ҧªÑ´à¨¹Ç‹ÒµÑÇã´à»š¹ÁÒÊàµÍÏ µÑÇ㴠໚ ¹ ÊàÅ¿ â´Âʋ Ç ¹ãË­‹ ÍØ » ¡Ã³ Á ÒÊàµÍÏ ¤× Í äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ ʋ Ç ¹ÍØ » ¡Ã³ Ê àÅ¿ 䴌ᡋ äÍ«ÕÇ´Ñ ÍسËÀÙÁ,Ô Ë¹‹Ç¤ÇÒÁ¨íÒáÃÁ ໚¹µŒ¹ ÍØ»¡Ã³ÁÒÊàµÍÏ໚¹µÑǨѴàµÃÕÂÁ¤ÇÒÁ¾ÃŒÍÁáÅФǺ ¤ØÁ¡Òö‹Ò·ʹ¢ŒÍÁÙź¹ÊÒÂÊÑ­­Ò³¹Ñ¹é ¢ŒÍÁÙŷѧé ËÁ´¨Ð¶Ù¡Ê‹§Å§º¹ÊÒÂÊÑ­­Ò³·ÕÁè ÍÕ ÂÙà‹ ¾Õ§àʌ¹à´ÕÂÇ ¹Õ·é §Ñé ËÁ´ ã¹ÃÐËNjҧ¡Ò÷íÒ§Ò¹ÍØ»¡Ã³ÁÒÊàµÍÏáÅÐÊàÅ¿ÊÒÁÒö໚¹ä´Œ·§Ñé µÑÇʋ§áÅеÑÇÃѺ ¢Ö¹é ÍÂÙ¡‹ ºÑ à§×Íè ¹ä¢¢Í§¡Ò÷íҧҹ㹢³Ð¹Ñ¹é ¡µÑÇÍ‹ҧ ¶ŒÒËÒ¡ÁÕ¡ÒÃà¢Õ¹¢ŒÍÁÙŨҡÍØ»¡Ã³ÁÒÊàµÍÏä»ÂѧÍØ»¡Ã³ ÊàÅ¿ µÑÇʋ§¤×ÍÍØ»¡Ã³ÁÒÊàµÍÏ µÑÇÃѺ¤×ÍÍØ»¡Ã³ÊàÅ¿ ã¹·Ò§µÃ§¢ŒÒÁ Ëҡ໚¹¡ÒÃ͋ҹ¢ŒÍÁÙŨҡÍØ »¡Ã³ÊàÅ¿ µÑÇʋ§¨Ð¡ÅÒÂ໚¹ÍØ»¡Ã³ÊàÅ¿ áÅеÑÇÃѺ¤×ÍÍØ»¡Ã³ÁÒÊàµÍÏ ã¹ÃкººÑÊ 1 ÃкºµŒÍ§ ÁÕÍ»Ø ¡Ã³ÁÒÊàµÍÏà¾Õ§µÑÇà´ÕÂÇ෋ҹѹé ÊÒÂÊÑ­­Ò³¢Í§ÃкººÑʹÕ鵌ͧ¡íÒ˹´ÊÀÒÇл¡µÔänj·ÕèÅͨԡÊÙ§ ·íÒ䴌â´Â¡Òõ‹ÍµÑǵŒÒ¹ ·Ò¹¤‹Ò 1 ¶Ö§ 4.7k: ¾ÙÅÍÑ»¡Ñºä¿àÅÕÂé § +5V ´Ñ§¹Ñ¹é ÍØ»¡Ã³·¹Õè Òí ࢌÒÁÒµ‹Íº¹ÃкººÑÊ¹Õ¨é §Ö µŒÍ§Í͡Ẻ ãˌàÍÒµ¾µØ ·Õµè ͌ §µ‹Í¡ÑºÊÒÂÊÑ­­Ò³ÁÕÅ¡Ñ É³Ð໚¹¤ÍÅàÅç¡àµÍÏແ´ËÃÔÍà´Ã¹à»´ ã¹ÃÙ»·Õè 13-1 áÊ´§ ä´ÍÐá¡ÃÁ¡ÒÃÊ×èÍÊÒâŒÍÁÙÅẺ˹Öè§ÊÒÂàº×éͧµŒ¹


210 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹ +5V

4k7

ÍØ»¡Ã³ ÁÒÊàµÍÏ

ÍØ»¡Ã³ ÊàÅ¿ #1

ÍØ»¡Ã³ ÊàÅ¿ #2

ÍØ»¡Ã³ ÊàÅ¿ #n

ÃÙ»·Õè 13-1 ¡ÒÃàª×Íè Áµ‹Íº¹ÃкººÑÊ˹֧è ÊÒÂ

13.2 ¤Ø³ÊÁºÑµ¢Ô ͧä·ÁÊÅç͵ ÍØ»¡Ã³ÁÒÊàµÍÏ໚¹ÍØ»¡Ã³à¾Õ§µÑÇà´ÕÂǺ¹ÃкººÑÊ·ÕÊè ÒÁÒöÍÔ¹àÔ ªÕÂÅÊÒÂÊÑ­­Ò³ä´Œ â´Â ¨Ð¡íÒà¹Ô´¨Ø´àÃÔÁè µŒ¹¢Í§ä·ÁÊÅç͵´ŒÇ¡Ò÷íÒãˌÊÒÂÊÑ­­Ò³à»š¹ÅͨԡµèÒí 㹪‹Ç§àÇÅÒË¹Ö§è ¨Ò¡¹Ñ¹é ·íÒ ãˌ¡ÅѺÁÒ໚¹ÅͨԡÊÙ§ ¶ŒÒËÒ¡ÍØ»¡Ã³ÊàÅ¿µŒÍ§¡ÒÃʋ§¢ŒÍÁÙÅ¡ÅѺÁÒÂѧÁÒÊàµÍÏ ÍØ»¡Ã³ÊàÅ¿¨Ð໚¹ µÑǤǺ¤ØÁÊÀÒÇТͧÊÒÂÊÑ­­Ò³µ‹Í仨¹àÊÃç¨ÊÔ¹é ¡Ãкǹ¡Òà ᵋ¶ÒŒ ËÒ¡ÍØ»¡Ã³ÁÒÊàµÍϵ͌ §¡Òà ʋ§¢ŒÍÁÙÅ¡ç¨ÐÊÒÁÒö´íÒà¹Ô¹¡Òõ‹Íä»ä´ŒàÅ ¿˜§¡ª¹Ñè ¢Í§ä·ÁÊÅç͵·Õ¡è Òí ˹´â´ÂÍØ»¡Ã³ÁÒÊàµÍÏÁ´Õ nj ¡ѹ 4 ¿˜§¡ª¹Ñè ¤×Í ä·ÁÊÅç͵¢Í§ ¡ÒÃÃÕ૵ (RESET), ¡ÒÃ͋ҹ¢ŒÍÁÙÅ (READ DATA), ¡ÒÃà¢Õ¹¢ŒÍÁÙÅ “1” (WRITE ONE) áÅСÒà à¢Õ¹¢ŒÍÁÙÅ “0” (WRITE ZERO) ä·ÁÊÅç͵ÃÕ૵㪌㹡ÒÃàÃÔÁè µŒ¹µÔ´µ‹Í¡ÑºÍØ»¡Ã³ÊàÅ¿ ã¹¢³Ð ·Õäè ·ÁÊÅç͵¡ÒÃ͋ҹ¨ÐÊíÒËÃѺ͋ҹ¢ŒÍÁÙÅ·ÕÊè §‹ ÁÒ¨Ò¡ÍØ»¡Ã³ÊàÅ¿ ʋǹä·ÁÊÅç͵¡ÒÃà¢Õ¹¢ŒÍÁÙÅ “1” áÅÐ “0” 㪌ÊÒí ËÃѺà¢Õ¹¢ŒÍÁÙÅä»ÂѧÍØ»¡Ã³ÊàÅ¿¼‹Ò¹ÊÒÂÊÑ­­Ò³¢Í§Ãкº ·Ò§´ŒÒ¹ÍØ»¡Ã³ÊàÅ¿ÁÕ¿§˜ ¡ª¹Ñè ·Ñ§é ÊÔ¹é 3 ¿˜§¡ª¹Ñè ¤×Í ä·ÁÊÅç͵¢Í§¡Òõͺʹͧ (PRESENCE), ¡ÒÃà¢Õ¹¢ŒÍÁÙÅ “1” (WRITE ONE) áÅСÒÃà¢Õ¹¢ŒÍÁÙÅ “0” (WRITE ZERO) ä·ÁÊÅç͵¢Í§¡Òà µÍºÊ¹Í§ãªŒÊÒí ËÃѺµÍºÊ¹Í§¡ÒõԴµ‹Í¨Ò¡ÍØ»¡Ã³ÁÒÊàµÍÏ â´ÂÍØ»¡Ã³ÊàÅ¿µÑÇ·Õ¶è ¡Ù àÅ×Í¡¨ÐµŒÍ§Ê‹§ ÊÑ­­Ò³µÍºÊ¹Í§Å§º¹ÊÒÂÊÑ­­Ò³à¾×Íè ᨌ§ãËŒÍ»Ø ¡Ã³ÁÒÊàµÍÏ·ÃÒºÇ‹Ò ¢³Ð¹Õµé ´Ô µ‹Í¡Ñ¹ä´ŒáÅŒÇ Ê‹Ç¹ ä·ÁÊÅç͵¡ÒÃà¢Õ¹¢ŒÍÁÙÅ “1” áÅÐ “0” 㪌ʧ‹ ¢ŒÍÁÙÅä»ÂѧÍØ»¡Ã³ÁÒÊàµÍϼҋ ¹ÊÒÂÊÑ­­Ò³¢Í§Ãкº «Ö§è ¨ÐÊÑÁ¾Ñ¹¸¡ºÑ ä·ÁÊÅç͵¡ÒÃ͋ҹ¢ŒÍÁÙŢͧÍØ»¡Ã³ÁÒÊàµÍÏ ¡ÒÃá¡áÂ¡ª¹Ñè ¢Í§áµ‹ÅÐä·ÁÊÅç͵ ¨Ð㪌¤ÇÒÁÂÒǢͧ¤ÒºàÇÅÒáÅÐÅѡɳТͧÃÙ»ÊÑ­­Ò³à»š¹µÑÇ¡íÒ˹´ áÅзء¤ÃÑ§é ·ÕÁè ¡Õ ÒÃà»ÅÕÂè ¹á»Å§ ¿˜§¡ª¹Ñè µŒÍ§·íÒãˌÊÒÂÊÑ­­Ò³ÍÂÙ㋠¹ÊÀÒÇÐNjҧàÊÁÍ «Ö§è ¡ç¤Í× ¡Ò÷íÒãˌÊÒÂÊÑ­­Ò³à»š¹ÅͨԡÊ٧͋ҧ¹ŒÍ 1 äÁâ¤ÃÇÔ¹Ò·Õ


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹ 480-960 äÁâ¤ÃÇÔ¹Ò·Õ ÍØ»¡Ã³ÁÒÊàµÍÏ·íÒãˌ໚¹ÅͨԡµíèÒ

l 211

60-240 äÁâ¤ÃÇÔ¹Ò·Õ ÍØ»¡Ã³ÁÒÊàµÍÏ·íÒãˌ໚¹ÅͨԡÊÙ§

ÃÕ૵

µÍºÃѺ 15-60 äÁâ¤ÃÇÔ¹Ò·Õ ÍØ»¡Ã³ÊàÅ¿ ·íÒãˌ໚¹ÅͨԡµíèÒ

ÍØ»¡Ã³ÊàÅ¿ ·íÒãˌ໚¹ÅͨԡÊÙ§

ÃÙ»·Õè 13-2 ä·ÁÊÅç͵¡ÒÃÃÕ૵áÅСÒõͺÃѺ¢Í§ÍØ»¡Ã³º¹ÃкººÑÊ˹֧è ÊÒÂ

13.2.1 ä·ÁÊÅç͵¡ÒÃÃÕ૵áÅеͺʹͧ ÍØ»¡Ã³ÁÒÊàµÍÏ·Òí ãˌà¡Ô´¡ÒÃÃÕ૵º¹ÊÒÂÊÑ­­Ò³à¾×Íè ᨌ§á¡‹Í»Ø ¡Ã³ÊàÅ¿ â´Â¡Òà ·íÒãˌÊÒÂÊÑ­­Ò³à»š¹ÅͨԡµèÒí µ‹Íà¹×Íè §Í‹ҧ¹ŒÍ 480 äÁâ¤ÃÇÔ¹Ò·Õ áÅШеŒÍ§·íÒãˌÊÒÂÊÑ­­Ò³¡ÅѺ ÁÒ໚¹ÅͨԡÊÙ§ÀÒÂã¹ 480 äÁâ¤ÃÇÔ¹Ò·ÕËÅѧ¨Ò¡¹Ñé¹ ¶ŒÒËÒ¡ÁÕÍØ»¡Ã³ÊàÅ¿µ‹ÍÍÂًº¹ÊÒÂÊÑ­­Ò³ ¨ÐÁÕ¡ÒõͺʹͧÊÑ­­Ò³ÃÕ૵¹Ñé¹´ŒÇÂÊÑ­­Ò³µÍºÊ¹Í§ (PRESENCE) â´Â¡Ò÷íÒãˌÊÒ ÊÑ­­Ò³à»š¹ÅͨԡµèÒí µ‹Íà¹×Íè §¹Ò¹»ÃÐÁÒ³ 60-240 äÁâ¤ÃÇÔ¹Ò·Õ ËÅѧ¨Ò¡ÊÑ­­Ò³ÃÕ૵»ÃÒ¡¯»ÃÐ ÁÒ³ 15-60 äÁâ¤ÃÇÔ¹Ò·Õ ÃÙ»·Õè 13-2 áÊ´§ä·ÁÊÅç͵¢Í§¡ÒÃÃÕ૵áÅСÒõͺʹͧ

13.2.2 ä·ÁÊÅç͵¡ÒÃ͋ҹ¢ŒÍÁÙŢͧÍØ»¡Ã³ÁÒÊàµÍÏáÅСÒÃà¢Õ¹¢ŒÍÁÙŢͧÍØ»¡Ã³ ÊàÅ¿ àÁ×Íè µŒÍ§¡ÒÃ͋ҹ¢ŒÍÁÙŨҡÍØ»¡Ã³ÊàÅ¿ ÍØ»¡Ã³ÁÒÊàµÍϨзíÒãˌÊÒÂÊÑ­­Ò³à»š¹ÅͨԡµèÒí ¹Ò¹»ÃÐÁÒ³ 1-15 äÁâ¤ÃÇÔ¹Ò·Õ ¨Ò¡¹Ñ¹é µŒÍ§·íÒãˌʶҹТͧÊÒ¡ÅѺÁÒ໚¹ÅͨԡÊÙ§ ÍØ»¡Ã³ÊàÅ ¿¨Ðʋ§¢ŒÍÁÙÅÁÒãˌÍØ»¡Ã³ÁÒÊàµÍÏâ´Â¶ŒÒ¢ŒÍÁÙÅ໚¹ “0” ÍØ»¡Ã³ÊàÅ¿¨Ð·íÒãˌÊÒÂÊÑ­­Ò³à»š¹ ÅͨԡµèíÒ¹Ò¹»ÃÐÁÒ³ 45 äÁâ¤ÃÇÔ¹Ò·Õ áŌǷíÒãˌ¡ÅѺÁÒÊًÊÀÒÇÐÅͨԡÊÙ§ÍÕ¡¤ÃÑé§ áµ‹¶ŒÒ໚¹¢ŒÍÁÙÅ “1” ÍØ»¡Ã³ÊàÅ¿¨Ð·íÒãˌÊÒÂÊÑ­­Ò³à»š¹ÅͨԡÊÙ§µ‹Íà¹×Íè §ä»ÍÕ¡ 45 äÁâ¤ÃÇÔ¹Ò·Õ ÃÇÁàÇÅҷѧé ËÁ´ ã¹ä·ÁÊÅç͵¹Õ»é ÃÐÁÒ³ 60-120 äÁâ¤ÃÇÔ¹Ò·Õ ¹Ñ¹è ¤×Íã¹ä·ÁÊÅç͵¹Õãé ªŒàÇÅÒÃÇÁäÁ‹à¡Ô¹ 120 äÁâ¤Ã ÇÔ¹Ò·Õ ã¹¢³Ð·ÕÍè »Ø ¡Ã³ÁÒÊàµÍϨÐ㪌àÇÅÒ㹡ÒÃ͋ҹ¢ŒÍÁÙÅÍÂÙË ÐËNjҧ 15 áÅÐ 60 äÁâ¤ÃÇÔ¹Ò·ÕËÅѧ ¨Ò¡àÃÔèÁµŒ¹ä·ÁÊÅç͵¹Õé ã¹ÃÙ»·Õè 13-3 áÊ´§ÃÙ»ÊÑ­­Ò³¢Í§ä·ÁÊÅç͵¡ÒÃ͋ҹ¢ŒÍÁÙŢͧÍØ»¡Ã³ ÁÒÊàµÍÏ«§Öè ÁÕÅ¡Ñ É³ÐàËÁ×͹¡Ñº¡ÒÃà¢Õ¹¢ŒÍÁÙŢͧÍØ»¡Ã³ÊàÅ¿ áÅÐä·ÁÊÅç͵·Ñ§é Êͧ¨Ðà¡Ô´¢Ö¹é ã¹ àÇÅÒà´ÕÂǡѹ ¡Å‹ÒǤ×Í àÁ×Íè ÍØ»¡Ã³ÁÒÊàµÍÏÍҋ ¹ ÍØ»¡Ã³ÊàÅ¿¡çµÍŒ §à¢Õ¹


212 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

60-120 äÁâ¤ÃÇÔ¹Ò·Õ ÍØ»¡Ã³ÁÒÊàµÍÏ·Òí ãˌ໚¹ÅͨԡµíÒè ÍØ»¡Ã³ÁÒÊàµÍÏ·Òí ãˌ໚¹ÅͨԡÊÙ§

ÍØ»¡Ã³ÁÒÊàµÍϨзíÒ¡ÒÃ͋ҹ㹪‹Ç§àÇÅÒ¹Õé 1-15 äÁâ¤ÃÇÔ¹Ò·Õ

45 äÁâ¤ÃÇÔ¹Ò·Õ

ÍØ»¡Ã³ÊàÅ¿µŒÍ§·íÒãˌ໚¹ ÅͨԡÊÙ§ËÅѧ¨Ò¡Ê‹§¢ŒÍÁÙÅ ¤ÃºáŌÇ

໚¹ÅͨԡµíÒè ¶ŒÒÍØ»¡Ã³Êàſʋ§¢ŒÍÁÙÅ "0" ໚¹ÅͨԡÊÙ§ ¶ŒÒÍØ»¡Ã³Êàſʋ§¢ŒÍÁÙÅ "1"

ÃÙ»·Õè 13-3 ä·ÁÊÅç͵¡ÒÃ͋ҹ¢ŒÍÁÙŢͧÁÒÊàµÍÏ «Ö§è µÃ§¡Ñºä·ÁÊÅç͵¡ÒÃà¢Õ¹¢ŒÍÁÙŢͧÍØ»¡Ã³ÊàÅ¿

13.2.3 ä·ÁÊÅç͵¡ÒÃà¢Õ¹¢ŒÍÁÙŢͧÍØ»¡Ã³ÁÒÊàµÍÏ àÁ×Íè ÍØ»¡Ã³ÁÒÊàµÍϵ͌ §¡ÒÃà¢Õ¹¢ŒÍÁÙÅ ÍØ»¡Ã³ÁÒÊàµÍϨзíÒãˌÊÒÂÊÑ­­Ò³à»š¹ÅͨԡµèÒí ¹Ò¹»ÃÐÁÒ³ 1-15 äÁâ¤ÃÇÔ¹Ò·Õ ¨Ò¡¹Ñ¹é µŒÍ§·íÒãˌʶҹТͧÊÒ¡ÅѺÁÒ໚¹ÅͨԡÊÙ§ áŌÇà¢Õ¹¢ŒÍÁÙŠ䴌㹷ѹ·Õ ¶ŒÒ¢ŒÍÁÙÅ·Õµè ͌ §¡ÒÃà¢Õ¹ä»ÂѧÍØ»¡Ã³Êàſ໚¹ “0” ÍØ»¡Ã³ÁÒÊàµÍϨзíÒãˌÊÒÂÊÑ­­Ò³ ໚¹ÅͨԡµèíÒ¹Ò¹»ÃÐÁÒ³ 45 äÁâ¤ÃÇÔ¹Ò·Õ áŌǷíÒãˌÊÒÂÊÑ­­Ò³¡ÅѺÁÒÊًÊÀÒÇÐÅͨԡÊÙ§ÍÕ¡¤ÃÑé§ áµ‹¶ÒŒ µŒÍ§¡ÒÃà¢Õ¹¢ŒÍÁÙÅ “1” ÍØ»¡Ã³ÁÒÊàµÍϨзíÒãˌÊÒÂÊÑ­­Ò³à»š¹ÅͨԡÊÙ§µ‹Íà¹×Íè §ä»ÍÕ¡ 45 äÁâ¤ÃÇÔ¹Ò·Õ ÃÇÁàÇÅҷѧé ËÁ´ã¹ä·ÁÊÅç͵¹Õ»é ÃÐÁÒ³ 60-120 äÁâ¤ÃÇÔ¹Ò·Õ ã¹ÃÙ»·Õè 13-4 áÅÐ13-5 áÊ´§ÃÙ»ÊÑ­­Ò³¢Í§ä·ÁÊÅç͵¡ÒÃà¢Õ¹¢ŒÍÁÙŢͧÍØ»¡Ã³ÁÒÊàµÍÏ«§Öè ÁÕÅ¡Ñ É³ÐàËÁ×͹¡Ñº¡ÒÃ͋ҹ ¢ŒÍÁÙŢͧÍØ»¡Ã³ÊàÅ¿ áÅÐä·ÁÊÅç͵·Ñé§Êͧ¨Ðà¡Ô´¢Öé¹ã¹ª‹Ç§àÇÅÒà´ÕÂǡѹ ¡Å‹ÒǤ×Í àÁ×èÍÍØ»¡Ã³ ÁÒÊàµÍÏà¢Õ¹ ÍØ»¡Ã³ÊàÅ¿¡çµÍŒ §Í‹Ò¹¢ŒÍÁÙÅ


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 213

60-120 äÁâ¤ÃÇÔ¹Ò·Õ ÍØ»¡Ã³ÁÒÊàµÍÏ·íÒãˌ໚¹ÅͨԡµíèÒ ÍØ»¡Ã³ÁÒÊàµÍÏ·íÒãˌ໚¹ÅͨԡÊÙ§

ÍØ»¡Ã³ÊàÅ¿¨Ð·íÒ¡ÒÃ͋ҹ㹪‹Ç§àÇÅÒ¹Õé 1-15 äÁâ¤ÃÇÔ¹Ò·Õ

45 äÁâ¤ÃÇÔ¹Ò·Õ

ÃÙ»·Õè 13-4 ä·ÁÊÅç͵¡ÒÃà¢Õ¹¢ŒÍÁÙÅ 1 ¢Í§ÍØ»¡Ã³ÁÒÊàµÍÏ «Ö§è µÃ§¡Ñºä·ÁÊÅç͵¡ÒÃ͋ҹ¢ŒÍÁÙŢͧ ÍØ»¡Ã³ÊàÅ¿ 60-120 äÁâ¤ÃÇÔ¹Ò·Õ ÍØ»¡Ã³ÁÒÊàµÍÏ·Òí ãˌ໚¹ÅͨԡµíÒè ÍØ»¡Ã³ÁÒÊàµÍÏ·Òí ãˌ໚¹ÅͨԡÊÙ§

15 äÁâ¤ÃÇÔ¹Ò·Õ

45 äÁâ¤ÃÇÔ¹Ò·Õ

ÍØ»¡Ã³ÊàÅ¿¨Ð·íÒ¡ÒÃ͋ҹ㹪‹Ç§àÇÅÒ¹Õé

ÃÙ»·Õè 13-5 ä·ÁÊÅç͵¡ÒÃà¢Õ¹¢ŒÍÁÙÅ “0” ¢Í§ÍØ»¡Ã³ÁÒÊàµÍÏ


214 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

13.3 ÃٻẺ¢Í§¡ÒÃÊ×Íè ÊÒâŒÍÁÙÅẺ˹էè ÊÒ (1-WireTM communication protocol) 㹡ÒÃÊ×èÍÊÒâŒÍÁÙÅÃкººÑÊ˹Öè§ÊÒ ÍØ»¡Ã³ÁÒÊàµÍÏÊÒÁÒöµÔ´µ‹ÍÍØ»¡Ã³Êàſ䴌¤ÃÑé§ ÅÐ 1 µÑÇ෋ҹѹé ÍØ»¡Ã³ÊàſᵋÅеÑǨ֧µŒÍ§Áբ͌ ÁÙÅ¡íÒ˹´áÍ´à´ÃÊ੾ÒеÑÇ â´Âà¡çºänjã¹Ë¹‹Ç ¤ÇÒÁ¨íÒÃÍÁÀÒÂã¹ÍØ»¡Ã³ÊàÅ¿µÑǹѹé æ â´Â»¡µÔÍ»Ø ¡Ã³ÊàÅ¿ã¹ÃкººÑÊẺ¹Õ¨é ÐÁÕ˹‹Ç¤ÇÒÁ ¨íÒ¢¹Ò´ 64 ºÔµËÃ×Í 8 亵 ÊíÒËÃѺà¡çº¢ŒÍÁÙŵ‹Ò§æ ·ÕÊè Òí ¤Ñ­¢Í§ÍØ»¡Ã³áµ‹ÅеÑÇ «Ö§è »ÃСͺ´ŒÇ 1. ÃËÑʢͧµÃСÙÅ ¨íҹǹ 8 ºÔµ 2. àÅ¢ËÁÒ»ÃШíÒµÑÇ (serial number) ¨íҹǹ 48 ºÔµ 3. ÃËÑʵÃǨÊͺ¤ÇÒÁ¼Ô´¾ÅÒ´ (CRC : Cyclical Redundancy Check) ¨íҹǹ 8 ºÔµ ¼Ù㌠ªŒ§Ò¹ÊÒÁÒö͋ҹ¢ŒÍÁÙÅ»ÃШíÒµÑǢͧÍØ»¡Ã³Êàſ䴌 ´ŒÇ¡ÒÃ㪌¤Òí Êѧè ͋ҹ˹‹Ç¤ÇÒÁ¨íÒÃÍÁ (Read ROM : $33) 㹡óշºÕè ¹ºÑÊÁÕÍ»Ø ¡Ã³ÊàÅ¿µÑÇà´ÕÂÇäÁ‹¨Òí ໚¹µŒÍ§ÍŒÒ§áÍ´à´ÃÊ㹡ÒõԴµ‹Í ÃٻẺ¡ÒõԴµ‹Íº¹ÃкººÑÊ˹Öè§ÊÒ¨ÐàÃÔèÁµŒ¹¢Öé¹ àÁ×èÍÍØ»¡Ã³ÁÒÊàµÍÏ·íÒ¡ÒÃÃÕ૵áÅÐ ¡íÒ˹´áÍ´à´ÃʢͧÍØ»¡Ã³··Õè Òí ¡ÒõԴµ‹Í ¶ŒÒËÒ¡ÁÕÍ»Ø ¡Ã³ÊàÅ¿à¾Õ§µÑÇà´ÕÂÇÊÒÁÒö¢ŒÒÁ¢Ñ¹é µÍ¹ ¡ÒõԴµ‹Í¡ÑºË¹‹Ç¤ÇÒÁ¨íÒÃÍÁã¹ÍØ»¡Ã³Êàſ䴌 ¨ÐàÃÕ¡ÇÔ¸¡Õ Òôѧ¡Å‹ÒÇ ¡ÒÃäÁ‹µ´Ô µ‹Í˹‹Ç¤ÇÒÁ ¨íÒÃÍÁ ËÃ×Í Ê¤Ô»ÃÍÁ (Skip ROM : $CC) ¨Ò¡¹Ñ¹é ÃÍ¡ÒõͺÃѺ¨Ò¡ÍØ»¡Ã³ÊàÅ¿ àÁ×Íè ¡ÒõͺÃѺ ÊÁºÙó¡ç¨ÐÊÒÁÒöàÃÔèÁµŒ¹¢Ñ鹵͹¡ÒÃ͋ҹËÃ×Íà¢Õ¹¢ŒÍÁÙÅ䴌µ‹Íä»

13.4 äźÃÒÃÕÃкººÑÊ˹֧è ÊÒ (onewire) äźÃÒÃÕ onewire Í͡ẺÁÒà¾×Íè Ê×Íè ÊÒáѺÃкººÑÊ 1 ÊÒ áÅÐÁբ͌ ¡íÒ˹´ã¹àÃ×Íè §¤ÇÒÁ¶Õè ¢Í§ÊÑ­­Ò³¹Ò́¡Ò·Õãè ªŒ¡ºÑ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC µŒÍ§äÁ‹¹ÍŒ Â¡Ç‹Ò 4MHz ÁÕ¿§˜ ¡ª¹Ñè µ‹Ò§æ ã¹äźÃÒÃÕ onewire ãˌ㪌§Ò¹´Ñ§¹Õé


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 215

13.4.1 ¿˜§¡ª¹Ñè Ow_Reset 㪌ÊÒí ËÃѺÃÕ૵ÍØ»¡Ã³ onewire ·Õàè ª×Íè Áµ‹ÍÍÂÙ¡‹ ºÑ ¢Ò¾Íϵ·Õ¡è Òí ˹´ ᵋ¿§˜ ¡ª¹Ñè ¹Õ¨é Ð㪌䴌੾ÒÐ ¡ÑºäÍ«Õ DS1820 ෋ҹѹé ÃٻẺ¡ÒÃ㪌§Ò¹¤íÒÊѧè unsigned short Ow_Reset(unsigned short *port, unsigned short pin);

µÑÇá»Ã port pin

໚¹µíÒá˹‹§¾Íϵ·Õãè ªŒàª×Íè Áµ‹ÍºÑÊ˹֧è ÊÒÂ

໚¹µíÒá˹‹§¢Ò¾Íϵ·Õàè ª×Íè Áµ‹Í¡ÑººÑÊ˹֧è ÊÒÂ

¡Òä׹¤‹Ò ¤×¹¤‹Ò໚¹ 0 ¶ŒÒµÃǨ¾ºÍØ»¡Ã³ onewire ¤×¹¤‹Ò໚¹ 1 ¶ŒÒäÁ‹¾ºÍØ»¡Ã³ onewire

µÑÇÍ‹ҧ·Õè 13-1 Ow_Reset(&PORTA, 5);

ÃÕ૵ÍØ»¡Ã³ºÊÑ Ë¹Ö§è ÊÒ·յè ͋ ÍÂÙ¡‹ ºÑ ¢Ò¾Íϵ RA5

13.4.2 ¿˜§¡ª¹Ñè Ow_Read ໚¹¿˜§¡ªÑè¹Í‹Ò¹¢ŒÍÁÙŨҡÍØ»¡Ã³º¹ÃкººÑÊ˹Öè§ÊÒ ÃٻẺ¡ÒÃ㪌§Ò¹¤íÒÊѧè unsigned short Ow_Read(unsigned short *port, unsigned short pin);

µÑÇá»Ã port pin

໚¹µíÒá˹‹§¾Íϵ·Õãè ªŒàª×Íè Áµ‹ÍºÑÊ˹֧è ÊÒÂ

໚¹µíÒá˹‹§¢Ò¾Íϵ·Õàè ª×Íè Áµ‹Í¡ÑººÑÊ˹֧è ÊÒÂ

¡Òä׹¤‹Ò

¤×¹¤‹Ò¢ŒÍÁÙÅ 1 亵·ÍÕè ҋ ¹ä´Œ¨Ò¡ÍØ»¡Ã³º¹ÃкººÑÊ

µÑÇÍ‹ҧ·Õè 13-2 unsigned short tmp; ... tmp = Ow_Read(&PORTA, 5);

໚¹¡ÒÃ͋ҹ¤‹Ò¢ŒÍÁÙÅ 1 亵¨Ò¡ºÑÊ˹֧è ÊÒ·բè Ò¾Íϵ RA5 â´Âà¡çº¤‹Ò·ÕÍè ҋ ¹ä´Œänj·µÕè ÇÑ á»Ã tmp


216 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

3.13.3 ¿˜§¡ª¹Ñè Ow_Write ໚¹¿˜§¡ªÑè¹Ê‹§¢ŒÍÁÙÅä»ÂѧÃкººÑÊ˹Öè§ÊÒ ÃٻẺ¡ÒÃ㪌§Ò¹¤íÒÊѧè void Ow_Write(unsigned short *port, unsigned short pin, unsigned short par);

µÑÇá»Ã port

໚¹µíÒá˹‹§¾Íϵ·Õãè ªŒàª×Íè Áµ‹ÍºÑÊ˹֧è ÊÒÂ

pin

໚¹µíÒá˹‹§¢Ò¾Íϵ·Õàè ª×Íè Áµ‹Í¡ÑººÑÊ˹֧è ÊÒÂ

par

໚¹¢ŒÍÁÙÅ 1 亵·µÕè ͌ §¡ÒÃʋ§ä»º¹ºÑÊ˹֧è ÊÒÂ

µÑÇÍ‹ҧ·Õè 13-3 Ow_Write(&PORTA, 5, 0xCC);

໚¹¡ÒÃʋ§¤‹Ò 0xCC ä»ÂѧÃкººÑÊ˹֧è ÊÒÂ«Ö§è µ‹Í¡Ñº¢Ò¾Íϵ RA5


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 217

»¯ÔºµÑ ¡Ô Ò÷Õè 24 ͋ҹ¤‹ÒÍسËÀÙÁ¨Ô Ò¡äÍ«Õ DS18B20 ´ŒÇ¿˜§¡ª¹Ñè onewire ¢ŒÍÁÙŷҧ෤¹Ô¤â´ÂÊÃØ»¢Í§äÍ«ÕµÃǨ¨ÑºÍسËÀÙÁÔ DS18B20 ໚¹äÍ«ÕµÃǨ¨ÑºÍسËÀÙÁ·Ô ãÕè ªŒ¡ÒõԴµ‹ÍẺÃкººÑÊ˹֧è ÊÒ ÁÕ¢Òµ‹Í㪌§Ò¹ 3 ¢Ò ¤×Í DQ ໚¹¢Òàª×Íè Á µ‹ÍÃкººÑÊ, ¢Òµ‹Íä¿àÅÕÂé §ÀÒ¹͡ áÅТҡÃÒÇ´ ÁÕ¤ÇÒÁÅÐàÍÕ´㹡ÒÃá»Å§ÍسËÀÙÁÍÔ ÂÙ·‹ Õè 12 ºÔµ â´Â㪌àÇÅÒ ã¹¡ÒÃá»Å§¤‹ÒÍسËÀÙÁÊÔ §Ù ÊØ´ 750 ÁÔÅÅÔÇ¹Ô Ò·Õ ÃٻẺ¢ŒÍÁÙŢͧ DS18B20 ໚¹´Ñ§¹Õé ºÔµ 15 ºÔµ 14 ºÔµ 13 ºÔµ 12 ºÔµ 11 ºÔµ 10 ºÔµ 9 ºÔµ 8 ºÔµ 7 ºÔµ 6 ºÔµ 5 ºÔµ 4 ºÔµ 3 ºÔµ 2 ºÔµ 1 ºÔµ 0 5

5

5

5

5

6

5

4

3

2

1

¢ŒÍÁÙÅ亵Ê§Ù

0

-1

-2

-3

¢ŒÍÁÙÅ亵µÒèí

ã¹µÒÃÒ§·Õè L24-1 áÊ´§ºÔµ¢ŒÍÁÙÅ¢¹Ò´ 12 ºÔµ ¢Í§ÍسËÀÙÁ·Ô ÍÕè ҋ ¹ä´Œ¨Ò¡ DS18B20

ÍسËÀÙÁÔ (ͧÈÒà«Åà«ÕÂÊ)

¢ŒÍÁÙÅàÍÒµ¾µØ (àÅ¢°Ò¹Êͧ)

¢ŒÍÁÙÅàÍÒµ¾µØ (àÅ¢°Ò¹ÊԺˡ)

+125

0000 0111 1101 0000

07D0H

+85

0000 0101 0101 0000

0550H

+25.0625

0000 0001 1001 0001

0191H

+10.125

0000 0000 1010 0010

00A2H

+0.5

0000 0000 0000 1000

0008H

0

0000 0000 0000 0000

0000H

-0.5

1111 1111 1111 1000

FFF8H

-10.125

1111 1111 0101 1101

FF5EH

-25.0625

1111 1101 0110 1111

FE6FH

-55

1111 1100 1001 0000

FC90H

µÒÃÒ§·Õè L24-1 ¢ŒÍÁÙÅ´Ô¨µÔ ÍÅ·Õ¤è ÇÒÁÅÐàÍÕ´ 12 ºÔµ¡Ñº¤‹ÒÍسËÀÙÁ·Ô ÇÕè ´Ñ ä´Œ¢Í§ DS18B20

-4


218 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

˹‹Ç¤ÇÒÁ¨íÒÊá¤Ã絪á¾´ 亵 0 亵 1

¤‹ÒÍسËÀÙÁäÔ ºµµÒèí ¤‹ÒÍسËÀÙÁäÔ ºµÊ§Ù

˹‹Ç¤ÇÒÁ¨íÒÍÕ; Õ ÃÍÁ

亵 2 亵 3

ÃÕ¨ÔÊàµÍÏ TH ËÃ×Í¢ŒÍÁÙÅ亵 1

ÃÕ¨ÔÊàµÍÏ TH ËÃ×Í¢ŒÍÁÙÅ亵 1

ÃÕ¨ÔÊàµÍÏ TL ËÃ×Í¢ŒÍÁÙÅ亵 2

ÃÕ¨ÔÊàµÍÏ TL ËÃ×Í¢ŒÍÁÙÅ亵 2

亵 4

ÃÕ¨ÔÊàµÍÏ Configuration

ÃÕ¨ÔÊàµÍÏ Configuration

亵 5 亵 6

ÊíÒÃͧänj (FFH) ÊíÒÃͧänj (0CH)

亵 7 亵 8

ÊíÒÃͧänj (10H) CRC

ÃÙ»·Õè L24-1 áÊ´§¡ÒèѴÊÃÃ¾×¹é ·Õ¢è ͧ˹‹Ç¤ÇÒÁ¨íÒÀÒÂã¹ DS18B20

¡ÒèѴ˹‹Ç¤ÇÒÁ¨íÒã¹ DS18B20 ã¹ÃÙ»·Õè L24-1 áÊ´§¡ÒèѴ˹‹Ç¤ÇÒÁ¨íÒÊá¤Ã絪á¾´ã¹ DS18B20 ¨ÐàËç¹ä´ŒÇҋ ÁÕ˹‹Ç¤ÇÒÁ¨íÒÍÕÍ¾Õ ÃÍÁ ·Õãè ªŒã¹¡ÒÃà¡çº¤‹Ò¢Íºà¢µÍسËÀÙÁ´Ô Ҍ ¹ÊÙ§ (TH), ¤‹Ò¢Íºà¢µÍسËÀÙÁ´Ô Ҍ ¹µèÒí (TL) áÅФ‹Ò¢Í§ÃÕ¨ÊÔ àµÍÏ¡Òí ˹´¡Òà ·íÒ§Ò¹ (configuration register) â´ÂàÁ×Íè àÃÔÁè µŒ¹¨‹ÒÂä¿ãˌᡋ DS18B20 ¤‹Ò¢Í§ TH, TL áÅФ‹ÒÃÕ¨ÊÔ àµÍÏ¡Òí ˹´ ¡Ò÷íÒ§Ò¹¨Ð¶Ù¡Í‹Ò¹ÍÍ¡ÁҨҡ˹‹Ç¤ÇÒÁ¨íÒÍÕÍ¾Õ ÃÍÁâ´ÂÍѵâ¹ÁÑµÔ ¼Ù㌠ªŒ§Ò¹ÊÒÁÒöᡌä¢áÅÐà¢Õ¹¡ÅѺࢌÒä»ãËÁ‹ à¾×Íè ¡íÒ˹´¤‹ÒÊíÒËÃѺ¡ÒÃàÃÔÁè µŒ¹·íÒ§Ò¹ãËÁ‹ä´Œ

Configuration register : ÃÕ¨ÊÔ àµÍÏ¡Òí ˹´¡Ò÷íÒ§Ò¹ 㪌ÊÒí ËÃѺàÅ×Í¡¤‹Ò¤ÇÒÁÅÐàÍÕ´¢Í§ÍسËÀÙÁ·Ô Õè DS18B20 ͋ҹ䴌 ÁÕû٠Ẻ¡ÒèѴ¢ŒÍÁÙŴѧ¹Õé ºÔµ 7 ºÔµ 6 ºÔµ 5 ºÔµ 4 ºÔµ 3 ºÔµ 2 ºÔµ 1 ºÔµ 0 

4

4











ʋǹ¡ÒÃàÅ×Í¡¤‹Ò¤ÇÒÁÅÐàÍÕ´¨Ð¡ÃзíÒ¼‹Ò¹ºÔµ R1 áÅÐ R0 ´Ñ§¹Õé R1

R0

¤ÇÒÁÅÐàÍÕ´

¤‹ÒàÇÅÒÊÙ§Êش㹡ÒÃá»Å§¤‹Ò

0

0

9 ºÔµ

93.75 ÁÔÅÅÔÇ¹Ô Ò·Õ (tconv/8)

0

1

10 ºÔµ

187.5 ÁÔÅÅÔÇ¹Ô Ò·Õ (tconv/4)

1

0

11 ºÔµ

375 ÁÔÅÅÔÇ¹Ô Ò·Õ (tconv/2)

1

1

12 ºÔµ

750 ÁÔÅÅÔÇ¹Ô Ò·Õ (tconv)


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 219

IC2 +5V

R1 4.7k

11

32

1

SW1 RESET

RD2 21

4

22

6

RD3

R3 2.2k

IC1 PIC16F877 15

RC0

+5V

C1 0.1PF 50V

,5&* 

RD7 RD6 RD5 RD4

JP1

E

+V Vo

D7 30

BLA

DSP1 LCD 16x2 (back light option)

RS

14

D6 D5 13

12

D4 11

D3 D2 10

9

D1

D0

8

7

R/W 5

BLK GND 15

2

R2 16 47

BACK LIGHT ON

1 3 VR1 10k

BRIGHTNESS

29 28 27

OSC1 OSC2 13 C2 22pF

14 XTAL1 20MHz C3 22pF

ÃÙ»·Õè L24-2 ǧ¨Ã·´Åͧ¡ÒõԴµ‹ÍÍØ»¡Ã³ÃкººÑÊ˹֧è ÊÒ¢ͧäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC16F877/ 887 ´ŒÇ¿˜§¡ª¹Ñè onewire ¢Í§ mikroC ¤ÍÁä¾àÅÍÏ

¡ÒÃàª×Íè Áµ‹Í·Ò§ÎÒÏ´áÇÏ 㪌ǧ¨Ãã¹ÃÙ»·Õè L24-2 㹡Ò÷´Åͧ â´Âµ‹ÍäÍ«Õ DS18B20 ࢌҡѺ¢Ò¾Íϵ RC0 «Ö§è ·Õµè Òí á˹‹§¢Ò¹Õ¨é еŒÍ§µ‹Í µÑǵŒÒ¹·Ò¹ 2.2k: àÍÒänj´ÇŒ  ÊíÒËÃѺ¡ÒÃáÊ´§¼Å¨ÐáÊ´§¼‹Ò¹âÁ´ÙÅ LCD «Ö§è µ‹Í¡Ñº¾Íϵ D

l

¢Ñ鹵͹¡Ò÷´Åͧ L24.1 ແ´â»Ãá¡ÃÁ mikroC ÊÌҧä¿Åâ»Ãà¨ç¡µ â´ÂàÅ×Í¡äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏáÅФÇÒÁ¶ÕÊè ­ Ñ ­Ò³¹Ò́¡Ò´Ñ§¹Õé l

¡Ã³Õ㪌ºÍÏ´ NX-877 plus II àÅ×Í¡ PIC16F877 ËÃ×Í PIC16F877A ¤ÇÒÁ¶ÕÊè ­ Ñ ­Ò³¹Ò́¡Ò 20MHz

L24.2 ÊÌҧâ»Ãà¨ç¡µªÍ×è DS18B20Test áÅоÔÁ¾â»Ãá¡ÃÁÀÒÉÒ C µÒÁâ»Ãá¡ÃÁ·Õè L24-1 L24.3 ·íÒ¡ÒäÍÁä¾ÅáÅдÒǹâËÅ´â»Ãá¡ÃÁà¾×Íè ·´Êͺ¼ÅÅѾ¸¡Ò÷íÒ§Ò¹ L24.4 Ãѹâ»Ãá¡ÃÁ ´Ù¼Å¡Ò÷íÒ§Ò¹·Õâè Á´ÙÅ LCD ·Õâè Á´ÙÅ LCD áÊ´§¤‹ÒÍسËÀÙÁ·Ô ÍÕè ҋ ¹ä´Œ¨Ò¡ DS18B20 ´Ñ§¹Õé

Temp= 26.18 75 L24.5 ãˌ¤ÇÒÁÌ͹ᡋäÍ«Õ DS18B20 à¾×Íè ´Ù¡ÒÃà»ÅÕÂè ¹á»Å§¤‹ÒÍسËÀÙÁÔ


220 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

int Temp; char Temp1; void Display(int Dat) { int Dat1; char text[6];

// Save temperature // Save lowbyte temperature // Function display temp on LCD // Internal variable

Dat1=Dat/16; Dat%=16; Dat*=625; if (Dat==0) Lcd_Out(1,13,"0000"); else if(Dat==625) Lcd_Out(1,13,"0625"); else { WordToStr(Dat,text); Lcd_Out(1,12,text); } WordToStr(Dat1,text); Lcd_Out(1,7,text); Lcd_Out(1,12,"."); Lcd_Out(1,4,"TEMP=");

// // // // //

Integer value Decimal point value Convert to 4 digit point value Leading zero Leading zero if namber < 4 digit

// Convert decimal point to String // Show string on LCD // Convert Integer value to String // Show string on LCD

// Show Text } void main() { // Initialize LCD on PORTD and prepare for output Lcd_Init(&PORTD); Lcd_Cmd(Lcd_CURSOR_OFF); while(1) // main loop { Ow_Reset(&PORTC,0); // Onewire reset signal Ow_Write(&PORTC,0,0xCC); // Issue command SKIP_ROM Ow_Write(&PORTC,0,0x44); // Issue command CONVERT_T Delay_us(120); Ow_Reset(&PORTC,0); Ow_Write(&PORTC,0,0xCC); Ow_Write(&PORTC,0,0xBE); Delay_ms(400);

// Issue command SKIP_ROM // Issue command READ_SCRATCHPAD

//Format DS18B20 [S][S][S][S][S][D6][D5][D4]==[D3][D2][D1][D0][D-1][D-2][D-3][D-4] Temp1= Ow_Read(&PORTC,0); // Get temperature LSB Temp = Ow_Read(&PORTC,0); // Get temperature MSB Temp <<= 8; // Shift to highbyte Temp += Temp1; // Form the result Display(Temp); // Format and display result on LCD Delay_ms(500); } }

¡Ò÷íÒ§Ò¹¢Í§â»Ãá¡ÃÁ

¡Ò÷íÒ§Ò¹ã¹Ê‹Ç¹¢Í§â»Ãá¡ÃÁËÅÑ¡àÃÔÁè ¨Ò¡ 1. ¡íÒ˹´¤‹ÒàÃÔÁè µŒ¹ãˌ¡ºÑ âÁ´ÙÅ LCD ã¹·Õ¹è àÕé Å×͡㪌¾Íϵ D

â»Ãá¡ÃÁ·Õè L24-1 ä¿Å DS18B20Test.c â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺ mikroC ¤ÍÁä¾àÅÍÏà¾×Íè ·´Êͺ ¡ÒÃ㪌¿§˜ ¡ª¹Ñè onewire 㹡ÒõԴ¤‹Í¡ÑºÍØ»¡Ã³ÃкººÑÊ˹֧è ÊÒ¢ͧäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC16F877


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 221

2. µÔ´µ‹Í¡ÑººÑÊ˹֧è ÊÒ â´Âʋ§ÊÑ­­Ò³ÃÕ૵ä»Âѧ DS18B20 3. à¢Õ¹¤íÒÊѧè SKIP ROM à¾×Íè ¢ŒÒÁ¡ÒÃ͋ҹºÔµáÍ´à´Ãʢͧ DS18B20 4. ʋ§¤íÒÊѧè CONVERT à¾×Íè ãˌ DS18B20 àÃÔÁè ¢Ñ¹é µÍ¹¡ÒÃá»Å§¤‹ÒÍسËÀÙÁÔ 5. àÃÔèÁµŒ¹¢Ñ鹵͹ãËÁ‹ à¾×èÍʋ§¤íÒÊÑè§á¨Œ§ DS18B20 Ç‹Ò µŒÍ§¡ÒÃ͋ҹ¤‹Ò¨Ò¡Ë¹‹Ç¤ÇÒÁ¨íÒ SCRATCHPAD «Ö§è à¡çº¤‹ÒÍسËÀÙÁ·Ô áÕè »Å§¤‹ÒàÊÃç¨áŌÇàÍÒänj 6. àÃÔÁè ¢Ñ¹é µÍ¹¡ÒÃ͋ҹ¤‹ÒÍسËÀÙÁÔ â´Â͋ҹ¤‹Ò亵µÒèí ¡‹Í¹ áŌǵÒÁ´ŒÇ¢ŒÍÁÙÅ亵Ê§Ù 7. ¹íÒ¢ŒÍÁÙÅ·ÕÍè ҋ ¹ä´Œà¡çºänj㹵ÑÇá»Ã temp ¨Ò¡¹Ñ¹é àÃÕ¡㪌§Ò¹¿˜§¡ª¹Ñè Display() à¾×Íè ¹íÒ ¤‹ÒáÊ´§¼Å·ÕËè ¹ŒÒ¨Í LCD ¡Ò÷íÒ§Ò¹¢Í§¿˜§¡ª¹Ñè Display() ÁÕ´§Ñ ¹Õé 1. ¹íÒ¤‹ÒÍسËÀÙÁÁÔ Òá¡໚¹¤‹ÒÍسËÀÙÁ¨Ô Òí ¹Ç¹àµçÁ¡Ñº¤‹ÒÍسËÀÙÁ·Ô àÕè »š¹àÅ¢·È¹ÔÂÁ 2. á»Å§¤‹ÒµÑÇàÅ¢·È¹ÔÂÁ¡‹Í¹ â´Â¤Ù³´ŒÇ 625 «Ö§è ¨Ð䴌¤Ò‹ µÑÇàÅ¢·È¹ÔÂÁ¢¹Ò´ 4 ºÔµ¾Í´Õ 3. ·íÒ¡ÒÃá»Å§¤‹ÒãˌÍÂÙ㋠¹ÃÙ»¢Í§µÑÇÍÑ¡ÉÃà¾×Íè ¹íÒä»áÊ´§¼Å·Õâè Á´ÙÅ LCD 4. ¶ŒÒ¤‹Ò·Õäè ´Œ¨Ò¡¡ÒÃá»Å§à»š¹ 0 ¤íÒÊѧè á»Å§¤‹Ò (WordToStr) ¨Ðãˌ¤Ò‹ ໚¹ 0 «Ö§è àÁ×Íè ¹íÒä» áÊ´§¼Å¨Ðà¡Ô´à»š¹ª‹Í§Ç‹Ò§ ¨Ö§µŒÍ§ãªŒ¤Òí Êѧè if ÁÒª‹Ç áÅÐàµÔÁàÅ¢Èٹ㹡óշ¤Õè ҋ ·Õäè ´Œà»š¹ 0 ËÃ×Í 625 5.¹íÒ¤‹Ò¨íҹǹàµçÁÁÒá»Å§à»š¹µÑÇÍÑ¡Éà áŌǹíÒä»áÊ´§¼Å·Õâè Á´ÙÅLCD

â»Ãá¡ÃÁ·Õè L24-1 ä¿Å DS18B20Test.c â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺ mikroC ¤ÍÁä¾àÅÍÏà¾×Íè ·´Êͺ ¡ÒÃ㪌¿§˜ ¡ª¹Ñè onewire 㹡ÒõԴ¤‹Í¡ÑºÍØ»¡Ã³ÃкººÑÊ˹֧è ÊÒ¢ͧäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC16F877


222 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 223

ÀÒ¤¼¹Ç¡ A

á¹Ð¹íÒà¤Ã×Íè §Á×Í㹡Ò÷´Åͧ·Ò§ÎÒÏ´áÇÏ A.1 NX-877 PlusII ªØ´·´ÅͧäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC16F87x

໚¹à¤Ã×Íè §Á×ͪ‹ÇÂ㹡Ò÷´ÅͧÅíҴѺáá·Õáè ¹Ð¹íÒ à¹×Íè §¨Ò¡à»š¹ºÍÏ´·´Åͧ·Õäè ´ŒÃºÑ ¡ÒÃÍÍ¡ ẺãˌÃͧÃѺ¡ÑºäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC16F87x Í‹ҧàËÁÒÐÊÁ â´Âº¹ºÍÏ´ÁÕÊNj ¹â»Ãá¡ÃÁ˹‹Ç ¤ÇÒÁ¨íÒ â´ÂàÅ×Í¡âËÁ´¡Ò÷íÒ§Ò¹´ŒÇÂÊÇÔµª¡´à¾Õ§µÑÇà´ÕÂÇ ÃͧÃѺ¡ÑºäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC16F87x ·Ñ§é ¢¹Ò´ 40 ¢Ò ¤Ãͺ¤ÅØÁ¤Ãº·Ø¡àºÍÏäÁ‹Çҋ ¨Ð໚¹ PIC16F870/871/874/874A/877/877A ÃٻËҧ˹ŒÒµÒ¢Í§ºÍÏ´·´ÅͧáÅÐÍØ»¡Ã³»ÃСͺ·Õ¨è ´Ñ ÁҾÌÍÁ¡Ñ¹ã¹ªØ´áÊ´§ã¹ÃÙ»·Õè A-1

NX-877 PlusII

PIC16F87x/18F Microcontroller Experiment Board ªØ´·´ÅͧäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC16F87x/18F

㹪شÁÕºÍÏ´·´Åͧ, ¤ÙÁ‹ Í× , ÍÐá´»àµÍÏ, «Í¿µáÇÏ, ÊÒµ‹Í PC, âá´ÙÅ LCD 16x2 áÅÐÊÒµ‹Íǧ¨Ã l 㪌䴌¡º Ñ PIC16F87x Ãع‹ 40 ¢Ò䴌·¡Ø àºÍÏ º¹ºÍÏ´àµÃÕÂÁ PIC16F877-20/P ¤ÇÒÁ¶Õè 20MHz l áËŋ§¨‹ÒÂ俵ç +5V 500mA l â»Ãá¡ÃÁ˹‹Ç¤ÇÒÁ¨íÒ¼‹Ò¹¾Íϵ USB ´ŒÇºÍÏ´â»Ãá¡ÃÁ PX-200 (¨Ñ´ÁҾÌÍÁ㹪ش) l ǧ¨Ãàª×Í è Áµ‹Í‹ ¾Íϵ͹ءÃÁà¾×Íè Ê×Íè ÊÒâŒÍÁÙÅ l àÅ×Í¡âËÁ´ RUN áÅÐ PROGRAM ´ŒÇÂÊÇÔµª¾ÃŒÍÁä¿áÊ´§Ê¶Ò¹Ð l ǧ¨Ã¡íÒà¹Ô´ÊÑ­­Ò³¾ÑÅʏ¤ÇÒÁ¶Õè 1Hz- 1kHz l ÊÇÔµª¡´µÔ´»Å‹Í´Ѻ 4 µÑÇ l LED Á͹ÔàµÍÏ 32 ª‹Í§ l áËŋ§¨‹ÒÂáç´Ñ¹»ÃѺ¤‹Ò䴌 0-5V ÊíÒËÃѺ·´Åͧǧ¨Ã A/D 4 ªØ´ l ÅíÒ⾧ໂÂâ« l ǧ¨Ãàª×Í è Áµ‹ÍâÁ´ÙÅ LCD l ἧµ‹Íǧ¨Ã¢¹Ò´ 2.5x7 ¹ÔÇé ¨íҹǹ¨Ø´µ‹Í 800 ¨Ø´ l LED µÑÇàÅ¢ 7 ʋǹ 3 ËÅÑ¡ l ǧ¨ÃÊÇÔµªàÁµÃÔ¡« 4x3 ¨Ø´

ÃÙ»·Õè A-1 áÊ´§ÃÒÂÅÐàÍÕ´¢Í§ªØ´·´Åͧ NX-877 PlusII


224 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

SW1 POWER

+V

D1-D4 1N4001x4

IC1 7805

J1 DC input

C3 47/16V

C2 0.1/50V

DC input : 16-20Vdc

+5V

C1 220/25V

K1 ICD2 (RJ11-6)

PORT B RB7

RB7

RB6 40

RB6

39 Vpp +5V

R1 1k

R2 1k

RB7

RB6

LED1 RUN

SW3 MODE

R4 1k

1 13

RE0/AN5/RD

RB0/INT

C6 22pF

XTAL1 20MHz

RA5 RA4 RA3 RA2 RA1 RA0

RE2 RE1 RE0

ÃÙ»·Õè A-2 ǧ¨ÃʋǹäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ

C5 0.1/50V

MCLR

PORT C

OSC1 OSC2

C7 22pF 7 RA5 6 RA4 5 RA3 4 RA2 3 RA1 2 RA0

PORT E RE2/AN7/CS RE1/AN6/WR

RB4 RB3/LVP RB2 RB1

31

14

RA0/AN0

RB6 RB5

11

12

PORT A

RB7

32

SW2 RESET

RA4/T0CKI RA3/AN3/Vref+ RA2/AN2/VrefRA1/AN1

37 RB4 36 RB3 35 RB2 34 RB1 33 RB0/INT

IC2 PIC16F877

+5V

RA5/AN4/SS

RB5

38

+5V

LED2 PROG.

R3 4k7

C4 0.1/50

10 RE2 9 RE1 8 RE0

26 RC7 25 RC6 24 RC5 23 RC4 18 RC3 17 RC2 16 RC1 15 RC0

RC7/RxD/DT* RC6/TxD/CK* RC5/SDO RC4/SDI/SDA RC3/SCK/SCL RC2/CCP1 RC1/T1OSI/ CCP2 RC0/T1OSO/T1C

PORT D 30

RD7 29 RD6 28 RD5 27 RD4 22 RD3 21 RD2 20 RD1 19 RD0

RD7/PSP7 RD6/PSP6 RD5/PSP5 RD4/PSP4 RD3/PSP3 RD2/PSP2 RD1/PSP1 RD0/PSP0


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 225

A.1.1 ÃÒÂÅÐàÍÕ´¢Í§Ê‹Ç¹»ÃСͺµ‹Ò§æ º¹ºÍÏ´·´Åͧ NX-877 PlusII A.1.1.1 ʋǹäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ

ËÑÇã¨ÊíҤѭÍÂÙ·‹ äÕè Áâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC16F87x »ÃШíÒºÍÏ´ «Ö§è ä´ŒÃºÑ ¡ÒÃâ»Ãá¡ÃÁ¼‹Ò¹ ·Ò§á¨¡ ICD2 «Ö§è µŒÍ§ãªŒºÍÏ´â»Ãá¡ÃÁ PX-200 ·Õàè ª×Íè Áµ‹Í¡Ñº¾Íϵ USB ¢Í§¤ÍÁ¾ÔÇàµÍÏ (´ÙÃÒ ÅÐàÍÕ´ã¹ËÑÇ¢ŒÍ A1.2) ÊÒÁÒöàÅ×Í¡âËÁ´ÃѹËÃ×Íâ»Ãá¡ÃÁ´ŒÇÂÊÇÔµª MODE à¾Õ§µÑÇà´ÕÂÇ ¾Íϵ·Õ¨è ´Ñ àµÃÕÂÁãˌÊÒí ËÃѺ¡Ò÷´ÅͧÁդú·Ñ§é 5 ¾Íϵ (㹡óÕ㪌ù؋ 40 ¢Ò) ¤×Í ¾Íϵ A 6 ºÔµ (RA0-RA5), ¾Íϵ B 8 ºÔµ (RB0-RB7), ¾Íϵ C 8 ºÔµ (RC0-RC7), ¾Íϵ D 8 ºÔµ (RD0-RD7) áÅоÍϵ E 3 ºÔµ (RE0-RE2) Ëҡ㪌ã¹Ãع‹ 28 ¢Ò ¨ÐäÁ‹ÊÒÁÒö㪌§Ò¹¨Ø´µ‹Í¾Íϵ D áÅÐ E 䴌 ʋǹǧ¨Ã¡íÒà¹Ô´ÊÑ­­Ò³¹Ò́¡Ò㪌¤ÃÔʵÍŤÇÒÁ¶Õè 20 MHz ´Ñ§¹Ñ¹é 㹡ÒÃà¢Õ¹â»Ãá¡ÃÁµŒÍ§ ¡íÒ˹´ª¹Ô´¢Í§Ç§¨Ã¡íÒà¹Ô´ÊÑ­­Ò³¹Ò́¡Ò໚¹áºº HS ÊíÒËÃѺáËŋ§¨‹ÒÂ俺¹ºÍÏ´ NX-877 PlusII ÁÕáËŋ§¨‹ÒÂä¿ÊíÒËÃѺ·´Åͧ 1 ªØ´¤×Í +5V ·Ñ§é Âѧ㪌ä¿àÅÕé§ãˌÍØ»¡Ã³·Ñé§ËÁ´º¹ºÍÏ´·´Åͧ´ŒÇÂ

A.1.1.2 LED Á͹ÔàµÍÏ 16 ª‹Í§ à¤Ã×Íè §Á×Íáá·Õ¢è Íá¹Ð¹íÒº¹ºÍÏ´·´Åͧ¤×Í LED Á͹ÔàµÍÏ«§Öè ÁÕ¶§Ö 32 ª‹Í§ â´Âẋ§à»š¹ 2 ʋǹ ʋǹÅÐ 16 ª‹Í§ ´Ñ§Ç§¨ÃÃÙ»·Õè A-3 ·Ø¡ª‹Í§µ‹ÍµÑǵŒÒ¹·Ò¹à¾×Íè ¨íÒ¡Ñ´¡ÃÐáÊänjàÃÕºÌÍÂáŌÇ

A.1.1.3 ǧ¨ÃáÊ´§¼Å LED µÑÇàÅ¢ 7 ʋǹ º¹ºÍÏ´·´Åͧ¨Ñ´àµÃÕÂÁʋǹáÊ´§¼ÅµÑÇàÅ¢ 7 ʋǹänj 3 ËÅÑ¡ ໚¹áººá¤â·´Ã‹ÇÁ ¹Ñ¹è ¤×Í µŒÍ§»‡Í¹Åͨԡ “0” ࢌҷբè ÒËÇÁ¢Í§ LED ¨Ö§¨Ð·íÒãˌ LED µÔ´ÊNjҧ䴌 ¡Òõ‹Íǧ¨Ã¢Í§áµ‹ÅÐà«¡àÁ¹µ ໚¹áººÁÑŵÔà¾Åç¡« â´ÂÁÕµÇÑ µŒÒ¹·Ò¹¨íÒ¡Ñ´¡ÃÐáʵ‹Íã¹áµ‹ÅÐà«¡àÁ¹µänjãˌ´ÇŒ  LED125-LED132 RED x8

LED117-LED124 RED x8

LED109-LED116 RED x8

LED101-LED108 RED x8

D15

D7

D15

D7

D14

D6

D14

D6

D13

D5

D13

D5

D12

D4

D12

D4

D11

D3

D11

D3

D10

D2

D10

D2

D9

D1

D9

D1

D8

D0

D8

R109-R116 510 x8

R101-R108 510 x8

LED Monitor active high 16 bit (UPPER)

ÃÙ»·Õè A-3 ǧ¨Ã LED Á͹ÔàµÍÏ 32 ª‹Í§

D0 R109-R116 510 x8

R101-R108 510 x8

LED Monitor active high 16 bit (LOWER)


226 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

DSP201-DSP203 : LED 7 SEGMENTS COMMON CATHODE P201 SEGMENT

a b c d e f g dp P202 R201-R208 DIGIT 220x8

a b c d e f g dp

DSP203

common

DSP202

DSP201

common

VR404 10k

VR403 10k

VR402 10k

VR401 10k +5V

common

V4

3 2 1

ÃÙ»·Õè A-4 ǧ¨Ã LED µÑÇàÅ¢ 7 ʋǹ 3 ËÅѡẺÁÑŵÔà¾Åç¡«

V3

V2

V1

ÃÙ»·Õè A-5 ǧ¨ÃáËŋ§¨‹ÒÂáç´Ñ¹»ÃѺ¤‹Ò 䴌 0-5V ÊíÒËÃѺ·´Åͧǧ¨Ã ADC

A.1.1.4 áËŋ§¨‹ÒÂáç´Ñ¹»ÃѺ¤‹Ò䴌ÊÒí ËÃѺǧ¨Ãá»Å§ÊÑ­­Ò³ÍйÒÅ͡໚¹´Ô¨µÔ ÍÅ

à¾×Íè ãˌ¡Ò÷´Åͧà¡ÕÂè ǡѺǧ¨Ãá»Å§ÊÑ­­Ò³ÍйÒÅ͡໚¹´Ô¨µÔ ÍÅÀÒÂã¹ PIC16F87x ÊÒÁÒö ¡ÃзíÒ䴌â´ÂÊдǡ º¹ºÍÏ´·´Åͧ NX-877 PlusII ¨Ö§ä´ŒàµÃÕÂÁáËŋ§¨‹ÒÂáç´Ñ¹»ÃѺ¤‹Ò䴌 0-5V ÊíÒËÃѺ·´Åͧ 4 ª‹Í§ â´Â㪌µÇÑ µŒÒ¹·Ò¹»ÃѺ¤‹Ò䴌¤Ò‹ 10k: ໚¹µÑÇ¡íÒ˹´»ÃÔÁÒ³¢Í§áç´Ñ¹

A.1.1.5 ǧ¨ÃÊÇÔµªÍ¹Ô ¾Øµ

໚¹áºº·íÒ§Ò¹·ÕÅè ͨԡ “0” ¢¹Ò´ 4 ª‹Í§ â´Â㪌ÊÇÔµª¡´µÔ´»Å‹Í´Ѻ 4 µÑÇáÅÐÁÕ¡Òõ‹ÍµÑÇ µŒÒ¹·Ò¹¾ÙÅÍÑ»änj ·íÒãˌËÒ¡ÊÇÔµªäÁ‹¶¡Ù ¡´ ËÃ×ÍäÁ‹Á¡Õ Òõ‹Íǧ¨Ã ·Õàè ÍÒµ¾µØ ¢Í§Ç§¨Ã¨ÐÁÕÅͨԡ “1” àÁ×Íè ÊÇÔµª¶¡Ù ¡´ËÃ×ÍÁÕ¡Òõ‹Íǧ¨Ã ¨Ðà¡Ô´Åͨԡ “0” ·Õàè ÍÒµ¾µØ

A.1.1.6 ǧ¨Ã¾ÑÅʏà¨à¹ÍàÃàµÍÏ

ÊÒÁÒö¡íÒà¹Ô´ÊÑ­­Ò³¾ÑÅʏÊÕèàËÅÕèÂÁ´ÔǵÕéä«à¡ÔÅ 50% àÅ×Í¡¤ÇÒÁ¶Õè䴌 4 ¤‹Ò¤×Í 1Hz, 10Hz, 100Hz áÅÐ 1kHz â´ÂÁÕ äÍ«ÕàºÍÏ PGX4100 ·íÒ˹ŒÒ·ÕÊè ÌҧÊÑ­­Ò³¾ÑÅʏ ¡ÒÃàÅ×Í¡¤ÇÒÁ¶Õ¨è Ð㪌ÊÇÔµª ¡´µÔ´»Å‹Í´Ѻà¾Õ§µÑÇà´ÕÂÇ ¡ÒÃáÊ´§¤‹Ò¤ÇÒÁ¶ÕÁè Õ LED 4 µÑÇ»ÃШíÒ¤‹Ò¤ÇÒÁ¶Õè ᨌ§ãˌ¼ãٌ ªŒ§Ò¹·ÃÒº S304 ,! S303 , S302 ,

R301 10k*8

S301 ,

FREQUENCY (Hz) 1 10 100 1k

C501 0.1/50V

+5V

R501 10k

,!

R502 510

1 4

, , ,

LED501-504 RED x4

S501 FREQ.

2 IC501 PGX4100

7 6 5 3

P501 OUT

8

ÃÙ»·Õè A-6 ǧ¨ÃÊÇÔµªÍ¹Ô ¾Øµ 4 ª‹Í§

ÃÙ»·Õè A-7 ǧ¨Ã¾ÑÅʏà¨à¹ÍàÃàµÍÏ 1Hz-1kHz


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

VR701 10k RS RW E D0 D1 D2 D3 D4 D5 D6 D7

SP601 Piezo Speaker

K701

UIC-LCD RS E D1 D3 D5 D7

+5V

+

RW D0 D2 D4 D6

l 227

-

P602

PIEZO

ÃÙ»·Õè A-8 ʋǹàª×Íè Áµ‹ÍâÁ´ÙÅ LCD áÅÐÅíÒ⾧ໂÂâ«

A.1.1.7 ʋǹàª×Íè Áµ‹ÍâÁ´ÙÅ LCD áÅÐÅíÒ⾧ໂÂâ«

º¹ºÍϴ䴌¨´Ñ àµÃÕÂÁ¤Í¹à¹ç¡àµÍÏÊÒí ËÃѺàª×Íè Áµ‹ÍâÁ´ÙÅ LCD ẺÍÑ¡¢ÃТ¹Ò´µÑ§é ᵋ 16 ¶Ö§ 20 µÑÇÍÑ¡Éà 1 ¶Ö§ 2 ºÃ÷Ѵ â´ÂÁÕ¡ÒèѴ¢ÒµÒÁÁҵðҹ UIC-LCD áÅÐÁÕµÇÑ µŒÒ¹·Ò¹»ÃѺ¤‹Ò䴌 10k: ÊíÒËÃѺ»ÃѺ¤ÇÒÁªÑ´à¨¹ã¹¡ÒÃáÊ´§¼Å¢Í§âÁ´ÙÅ LCD ʋǹ¡ÒâѺÊÑ­­Ò³àÊÕ§ ÁÕÅÒí ⾧ໂÂâ« 1 ªØ´ â´ÂÅíÒ⾧ໂÂ⫹ըé ÐÁդҋ ¤ÇÒÁ¶Õàè Ãâ«á¹¹« »ÃШíÒµÑÇ ËÒ¡¤ÇÒÁ¶Õè·Õ軇͹ࢌÒÁҵç¡Ñº¤‹Ò¤ÇÒÁ¶Õè¹Ñé¹ ÊÑ­­Ò³¨ÐÁÕ¤ÇÒÁ´Ñ§ÁÒ¡·ÕèÊØ´ »¡µÔ¨ÐÁÕ¤‹Ò »ÃÐÁÒ³ 2 ¶Ö§ 3kHz

R901 2.2k

+5V 3 VCC

P901 DQ

2 DQ GND 1 IC901 DS18B20

+5V C902 10/50V 16 2 C903 K901 10/50V 1 IC902 SERIAL PORT MAX232 3 1 6 7 2 7 3 8 8 4 5 6 15 C905 10/50V

C901 10/50V

4 5

C904 10/50V

10

TxD

9

RxD GND P902 SERIAL DATA

ÃÙ»·Õè A-9 ǧ¨Ãàª×Íè Áµ‹Í DS18B20 äÍ«ÕÇ´Ñ ÍسËÀÙÁµÔ ´Ô µ‹Í¼‹Ò¹ÃкººÑÊ 1 ÊÒ ÃÙ»·Õè A-9 ʋǹàª×Íè Áµ‹Í¾Íϵ͹ءÃÁ RS-232


228 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

A.1.1.8 ʋǹàª×Íè Áµ‹ÍÍØ»¡Ã³ÃкººÑÊ 1 ÊÒ (1-Wire)

º¹ºÍϴ䴌¨Ñ´àµÃÕÂÁǧ¨Ãàª×èÍÁµ‹Í DS18B20 äÍ«ÕÇÑ´ÍسËÀÙÁÔẺ´Ô¨ÔµÍÅ «Öè§à»š¹Ë¹Öè§ã¹ ÍØ»¡Ã³·Õè㪌¡ÒõԴµ‹Í¼‹Ò¹ÃкººÑÊ˹Öè§ÊÒ (1-Wire) ¢Í§ Dallas Seminconductor â´Âº¹ºÍÏ´ àµÃÕÂÁäÍ«Õ DS18B20 änjãˌ¾ÃŒÍÁ㪌§Ò¹

A1.1.9 ʋǹàª×Íè Áµ‹Í¾Íϵ͹ءÃÁ RS-232 à¾×Íè ãˌºÍÏ´ NX-877 PlusII ÊÒÁÒöÃͧÃѺ¡Ò÷´Åͧ䴌Í‹ҧ¤Ãº¶ŒÇ¹ ¨Ö§ä´Œ¨´Ñ àµÃÕÂÁǧ¨Ã àª×Íè Áµ‹Í¡Ñº¾Íϵ͹ءÃÁ RS-232 ¢Í§¤ÍÁ¾ÔÇàµÍÏ à¾×Íè 㪌»ÃÐ⪹ã¹¡ÒÃÊ×Íè ÊÒâŒÍÁÙÅâ´Â㪌äÍ«Õ àºÍÏ ICL232 ËÃ×Í MAX232 ÁÕ¨´Ø µ‹ÍÊíÒËÃѺÃѺ¢ŒÍÁÙÅẺ͹ءÃÁ (RxD) ࢌÒÁÒ¨Ò¡¤ÍÁ¾ÔÇàµÍÏà¾×Íè ʋ§µ‹Íä»ÂѧäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ áÅШشµ‹ÍÊíÒÃѺʋ§¢ŒÍÁÙÅÍ͡Ẻ͹ءÃÁ (TxD) ¨Ò¡äÁâ¤Ã ¤Í¹â·ÃÅàÅÍÏä»Âѧ¤ÍÁ¾ÔÇàµÍÏ ÊíÒËÃѺ¨Ø´àª×Íè Áµ‹Í¡Ñº¤ÍÁ¾ÔÇàµÍÏ㪌¤Í¹à¹ç¡àµÍÏ DB-9 µÑÇàÁÕÂ

A.1.1.10 ἧµ‹Íǧ¨Ã

ÁÕ¢¹Ò´ 2.5x7 ¹ÔÇé ÁÕ¨´Ø µ‹Í·Ñ§é ÊÔ¹é 800 ¨Ø´

A.2 PX-200 ºÍÏ´â»Ãá¡ÃÁäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC Ẻá¿Åª¼‹Ò¹ ¾Íϵ USB A.2.1 ¤Ø³ÊÁºÑµ·Ô ҧ෤¹Ô¤ àª×Íè Áµ‹Í¤ÍÁ¾ÔÇàµÍϼҋ ¹¾Íϵ USB 㪌ä¿àÅÕÂé §¨Ò¡¾Íϵ USB l â»Ãá¡ÃÁäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC º¹ºÍϴ໇ÒËÁÒ¼‹Ò¹·Ò§á¨¡ ICD2 l ÁÕ LED áÊ´§Ê¶Ò¹Ð¡Ò÷íÒ§Ò¹ 3 ´Ç§ (POWER, TARGET áÅÐ BUSY) l ÊÒÁÒöÍÑ»à¡Ã´à¿ÃÁ  áÇÏ䴌 ��‹Ò¹·Ò§«Í¿µáÇÏ l

l

ÊÒÁÒö͋ҹáÅÐà¢Õ¹˹‹Ç¤ÇÒÁ¨íÒâ»Ãá¡ÃÁẺá¿ÅªáÅÐ˹‹Ç¤ÇÒÁ¨íÒ¢ŒÍÁÙÅÍÕÍ¾Õ ÃÍÁ

l

àÅ×Í¡¡Òû‡Í§¡Ñ¹¡ÒÃ͋ҹ·Ñ§é ˹‹Ç¤ÇÒÁ¨íÒâ»Ãá¡ÃÁẺá¿ÅªáÅÐ˹‹Ç¤ÇÒÁ¨íÒ¢ŒÍÁÙÅÍÕÍ¾Õ ÃÍÁ

l µ‹Í¡ÑººÍÏ´ÍÐá´»àµÍÏ KX-200plus (¨Ñ´«×Í é á¡) à¾×Íè â»Ãá¡ÃÁäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC áÅÐ dsPIC

µÑǶѧ DIP ¼‹Ò¹·Ò§á¨¡ ICD2 l

«Í¿µáÇÏ·Òí §Ò¹º¹Ãкº»¯ÔºµÑ ¡Ô ÒÃÇÔ¹â´Çʏ 98SE/ME/XP

l

·íÒ§Ò¹â´ÂäÁ‹µÍŒ §µ‹Íä¿àÅÕÂé §ÀÒ¹͡à¾ÔÁè

â´Â¡ÒÃà¾ÔèÁàµÔÁàºÍϢͧäÁâ¤Ã¤Í¹â·ÃÅàÅÍϹÑé¹ ÊÒÁÒö´Ù¢ŒÍÁÙÅà¾ÔèÁàµÔÁ áÅдÒǹ âËÅ´â»Ãá¡ÃÁÃÇÁ·Ñ§é ࿁ÃÁ áÇÏÅҋ Êش䴌·Õè www.microchip.com ¤Œ¹ËÒ˹ŒÒ PICkit2TM ã¹ÃÙ»·Õè A-10 áÊ´§Ë¹ŒÒµÒáÅÐʋǹ»ÃСͺ·Ñ§é ËÁ´¢Í§ PX-200


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

l 229

¤Í¹à¹ç¡àµÍϵ͋ ¾Íϵ USB ˹‹Ç¤ÇÒÁ¨íÒÍÕ; Õ ÃÍÁ ÊíÒËÃѺà¾ÔÁè àµÔÁ¤ÇÒÁÊÒÁÒö 㹡ÒÃ㪌§Ò¹â´ÂÅíҾѧ (໚¹ÍØ»¡Ã³àÊÃÔÁ áÅФÇÒÁ ÊÒÁÒö¹Õ¨é ÐÁÕã¹Í¹Ò¤µ) ࿁ÃÁ áÇϤǺ¤ØÁ¡Ò÷íÒ§Ò¹ ÊÒÁÒöÍÑ»à¡Ã´ä´Œ´ÇŒ  ä¿áÊ´§ÀÒÇÐä¿àÅÕÂé § ä¿áÊ´§¡Òè‹ Ò Âä¿àÅÕé  §ä»ÂÑ § äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ໇ÒËÁÒ ä¿á¨Œ§Ê¶Ò¹ÐäÁ‹Çҋ § ᨍ¡ ICD2 ¨Ø´µ‹Í ICSP

ÊÇÔµª PROGRAM 㪌¡´à¾×Íè ·íÒ¡ÒÃâ»Ãá¡ÃÁ ·Ò§ÎÒÏ´áÇÏ

ÃÙ»·Õè A-10 áÊ´§Ê‹Ç¹»ÃСͺ·ÕÊè Òí ¤Ñ­¢Í§ PX-200 ºÍÏ´â»Ãá¡ÃÁäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC Ẻ ISP ¼‹Ò¹¾Íϵ USB


230 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

A.3 RBX-877 rev.2007 ºÍÏ´¤Çº¤ØÁÊíÒËÃѺËع‹ ¹µÍµÑ â¹Áѵ¢Ô ¹Ò´àÅç¡ ºÍÏ´ RBX-877 rev.2007 »ÃСͺ´ŒÇ äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC16F887 ·íÒ˹ŒÒ·Õàè »š¹µÑÇ »ÃÐÁÇżÅËÅÑ¡, ǧ¨Ã¢ÑºÁÍàµÍÏ俵ç, ʋǹáÊ´§¼Å LCD, ʋǹàª×Íè Áµ‹Íà¤Ã×Íè §â»Ãá¡ÃÁÀÒ¹͡ áÅÐʋǹµÔ´µ‹Í¡ÑºÍØ»¡Ã³ÀÒ¹͡·Ñ駵ÑǵÃǨ¨ÑºáÅÐǧ¨ÃÊ×èÍÊÒâŒÍÁÙÅ͹ءÃÁ â´ÂÃÒÂÅÐàÍÕ´ ʋǹ»ÃСͺµ‹Ò§æ áÊ´§à»š¹ºÅçÍ¡ä´ÍÐá¡ÃÁ´Ñ§ÃÙ»·Õè A-11

A3.1 ¤Ø³ÊÁºÑµ·Ô ҧ෤¹Ô¤ 㪌äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC16F887 ¤Çº¤ØÁ¡Ò÷íÒ§Ò¹·Ñ§é ËÁ´ l ´ÒǹâËÅ´â»Ãá¡ÃÁ¨Ò¡¤ÍÁ¾ÔÇàµÍÏ´Ç Œ Âà¤Ã×Íè §â»Ãá¡ÃÁÀÒ¹͡¼‹Ò¹·Ò§á¨¡ ICD2 l ÁÕǧ¨Ã¢ÑºÁÍàµÍÏ俵ç 2 ªØ´¾ÃŒÍÁä¿áÊ´§·ÔÈ·Ò§¡ÒÃËÁع l ¢Ñº RC à«ÍÏâÇÁÍàµÍÏ¢¹Ò´àÅç¡ 4.8 ¶Ö§ 6V 䴌 3 µÑÇ l ÁÕÅÒ í ⾧ໂÂâ«ÊíÒËÃѺ¢ÑºàÊÕ§ l

¨Ø´µ‹ÍÊ×Íè ÊÒÃ͹ءÃÁ

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

¨Ø´µ‹Íà¤Ã×Íè §â»Ãá¡ÃÁ (µ‹Í¢Ò RC6 áÅÐ RC7) ¨Ø´µ‹ÍâÁ´ÙÅ LCD ¨Ø´µ‹ÍÃкººÑÊ I2C (µ‹Í¢Ò RD2-RD7)

¨Ø´µ‹ÍẵàµÍÃÕè

(µ‹Í¢Ò RC3 áÅÐ RC4)

äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ

¨Ø´µ‹ÍÁÍàµÍÏ俵ç

(µ‹Í¢Ò RC0)

21+$.&&%

LED áÊ´§¼Å

¨Ø´µ‹Íà«ÍÏâÇÁÍàµÍÏ (µ‹Í¢Ò RB4,RB5,RC5)

ÅíÒ⾧ໂÂâ«

¾ÍϵÍÔ¹àµÍÃÃ»Ñ µ ÊÇÔµª¡´

(µ‹Í¢Ò RB0)

(µ‹Í¢Ò RD0, RD1, RC2 áÅÐ RB1, RB2, RC1) (µ‹Í¢Ò RA4 áÅÐ RB0)

¾ÍϵÍÔ¹¾ØµàÍÒµ¾µØ

(µ‹Í¢Ò RA0-RA3, RA5, RE0-RE2)

ÃÙ»·Õè A-11 áÊ´§Ê‹Ç¹»ÃСͺ·ÕÊè Òí ¤Ñ­¢Í§ºÍÏ´ RBX-877rev.2007

(µ‹Í¢Ò RB3)


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹ l 231

ÁÕ LED áÊ´§¼Åµ‹ÍµÃ§¡Ñº¢Ò¾Íϵ¢Í§äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ l âÁ´ÙÅ LCD 16 µÑÇÍÑ¡Éà 2 ºÃ÷Ѵ ẺÁÕä¿Ê‹Í§ËÅѧ l ¨Ø´µ‹ÍÊíÒËÃѺàª×Í è Áµ‹Í¡ÑºÍØ»¡Ã³·ÁÕè ¡Õ ÒÃÊ×Íè ÊÒâŒÍÁÙÅẺ͹ءÃÁáÅÐÃкººÑÊ I2C l ÁÕ¨´ Ø µ‹ÍÍÔ¹¾ØµàÍÒµ¾µØ Ẻ´Ô¨µÔ ÍÅáÅÐÍÔ¹¾ØµÍйÒÅÍ¡ 9 ª‹Í§ l ·íÒ§Ò¹´ŒÇÂẵàµÍÃÕè AA 4 ¡ŒÍ¹ 㪌䴌·§ Ñé ẺÍÑŤÒ䬏áÅÐẺ»ÃШØ䴌 l ¢¹Ò´ 2.38 x 6.25 ¹ÔÇ é

l

A3.2 ¡Ò÷íÒ§Ò¹¢Í§Ç§¨Ã A3.2.1 ʋǹäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ ËÑÇã¨ÊíҤѭ¤×Í äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC16F887 ÁÕ¢Ò¾ÍϵÍÔ¹¾ØµàÍÒµ¾µØ ãˌµÍ‹ 㪌§Ò¹ä´Œ ÁÒ¡¶Ö§ 35 ¢Ò ÀÒÂã¹äÁâ¤Ã¤Í¹â·ÃÅàÅÍÃÂ§Ñ »ÃСͺ仴ŒÇÂâÁ´Ùŵ‹Ò§ æ ÁÒ¡ÁÒ ઋ¹âÁ´ÙÅ PWM à¾×Íè ãˌÊÒÁÒö»ÃѺ¤ÇÒÁàÃçÇÁÍàµÍÏ䴌µÒÁµŒÍ§¡Òà âÁ´ÙÅá»Å§ÊÑ­­Ò³ÍйÒÅ͡໚¹´Ô¨µÔ ÍÅ(ADC) «Ö§è PIC16F887 䴌àµÃÕÂÁ ADC ¤ÇÒÁÅÐàÍÕ´ 10 ºÔµänjÁÒ¡¶Ö§ 14 ª‹Í§ ´Ñ§¹Ñ¹é ´ŒÇÂäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ à¾Õ§µÑÇà´ÕÂǨ֧µ‹Í¡ÑºÍØ»¡Ã³Í¹Ô ¾ØµàÍÒµ¾µØ à¾×Íè ÊÌҧ໚¹Ëع‹ ¹µä´ŒÍ‹ҧ§‹Ò´Ò â´ÂÁÕà«ÃÒÁÔ¡àÃâ« à¹àµÍÏ 20MHz ¡íÒà¹Ô´¤ÇÒÁ¶ÕÊè ­ Ñ ­Ò³¹Ò́¡Òà¾×Íè ãˌäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ·Òí §Ò¹ä´Œ·¤Õè ÇÒÁàÃçǶ֧ 5 Ōҹ¤íÒÊÑ觵‹ÍÇÔ¹Ò·Õ

A3.2.2 ÀÒ¤¨‹ÒÂä¿ ºÍÏ´ RBX-877 µŒÍ§¡ÒÃáç´Ñ¹ä¿àÅÕÂé §ÍÔ¹¾Øµà»š¹áç´Ñ¹ä¿¿‡Ò¡ÃÐáʵç 4.8 ¶Ö§ 6V (äÁ‹ ¤ÇÃà¡Ô¹ +6V) â´Âº¹ºÍÏ´ÁÕ¡ÒõԴµÑ§é ¡ÐºÐ¶‹Ò¹ AA ¢¹Ò´ 4 ¡ŒÍ¹ áÅе‹ÍÊÒÂÁÒÂѧ¨Ø´µ‹Íä¿àÅÕÂé § Ẻà·ÍÏÁ¹Ô ÍźÅçÍ¡ 2 ¢ÒµÑÇàÅç¡ àÁ×Íè µŒÍ§¡ÒÃ㪌§Ò¹¡çà¾Õ§ºÃèØẵàµÍÃÕ¢è ¹Ò´ AA ¨íҹǹ 4 ¡ŒÍ¹ ŧ㹡кж‹Ò¹


232 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

SW1 ON

+Vm

L1 10uH

D1 1N4001

R1 0.47

D2 1N5819 +5V

K1 BATT. 4.5-6V

C1 100PF 10V

C2 100PF 10V

ZD1 5.1V

IC1 NCP1450 5.0

Q1 FDS6680A

5 4

K2 ISP

CE OUT

GND

C3 1000PF 6.3V

1 2

LED1 ON

ZD2 5.6V

C4 0.01PF 50V

Switching Power Supply

PGC PGD

+5V

R2 1k

+5V

+5V Vpp

11

PGD

40

J1 ICD2

39

PGC GND

32

C5 0.1PF 50V

RB7 RB6

Vpp

+5V

RD2 21

4

22

6

RD3 IC2 PIC16F887

R3 4k7

1 R4 1k

SW2 RESET

MCLR

RD7 RD6 RD5

+5V 23

K3 SDA/RC4

18

K4 SCL/RC3

RD4

RB3

K6 RxD/RC7

26

RC6/TxD RB5

R6 4k7

+5V R8 150

K7 INT/RB0

S2 RB0

+5V

6 33

RB1 RB2 RC1/CCP2

+5V

K15 AN5/RE0

8

K14 AN4/RA5

7

K9 AN3/RA3

5

K13 AN2/RA2

4

K12 AN1/RA1

3

K11 AN0/RA0

2

CR1 Ceramic Resonator 20MHz

BRIGHTNESS

27

36

LED2 RB3

R5 510

37 +Vm

K8 SERVO1

+Vm

K9 SERVO2

+Vm

K10 SERVO3

38

24

K18A-K18B Motor M-1 C7 0.1PF 50V

+5V +Vm

19

2

20

7 2A

RC2/CCP1 17

9

VR1 10k

3

BLK GND 15 2

28

16

RD1

K16 AN6/RE1

1

29

RB0/INT RD0

10

BACK LIGHT ON

1A

1

15 4A

35

10

16

9

RE2/AN7

RC0

15

RE0/AN5

31 RA1/AN1

13

INVERT

+ +

K19A-K19B Motor M-2

DIRECT

Motor Driver +Vm

R9 47k

1 +V RESET GND 2

RA0/AN0

R10 2k2 C8 0.1PF 50V

4

C6 10PF 50V

R8 1k 12

LED4 DIR. #B

3A

RA3/AN3 RA2/AN2

DIRECT LED3 DIR. #A

34EN

SP1 PIEZO

RA5/AN4

+

R9 2k2

6

14 IC3 4Y L293D 11 3Y

13 12 5

RE1/AN6

1Y 3 2Y

12EN

34

8

Vcc1

INVERT

+

RA4

S1 RA4

K17 AN7/RE2

+V Vo

RC7/RxD RC5

R7 4k7

E

R5 16 47

RC3/SCL RB4

25

BLA

DSP1 LCD 16x2 (back light option)

RS

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

30

RC4/SDA

+5V K5 TxD/RC6

+5V JP1

3

LED5 LOW-BAT

Battery Indicator 14

ÃÙ»·Õè A-12 áÊ´§Ç§¨ÃÊÁºÙó¢Í§ RBX-877 rev.2007 ºÍÏ´¤Çº¤ØÁÊíÒËÃѺËع‹ ¹µÍµÑ â¹Áѵ¢Ô ¹Ò´àÅç¡


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹ l 233

º¹ºÍϴ䴌ºÃèØǧ¨ÃÀÒ¤¨‹ÒÂä¿ÊÇÔµªªÔè§áºº»ÃѺ¢Öé¹ (step up) à¾×èÍ¡íÒ˹´ÃдѺáç´Ñ¹ ä¿àÅÕé§ǧ¨Ã·Ñé§ËÁ´änj·Õè +5V ·Ñ駹Õéà¹×èͧ¨Ò¡àÁ×èÍàÃÔèÁµŒ¹¨‹ÒÂä¿ãˌᡋǧ¨Ã ÃдѺ¢Í§áç´Ñ¹·Õè ẵàµÍÃÕ¨è Ф‹ÍÂæ Ŵŧ ´ŒÇ¡ÒÃ㪌ǧ¨Ã¨‹ÒÂä¿ÊÇÔµªª§Ôè Ẻ»ÃѺ¢Ö¹é ¹Õ·é Òí ãˌáç´Ñ¹ä¿àÅÕÂé §Ç§¨ÃÃÇÁ Âѧ¤§ÃÑ¡ÉÒÃдѺÍÂÙ·‹ Õè +5V ÍÂÙä‹ ´Œ ¨¹¡Ç‹ÒÃдѺáç´Ñ¹¢Í§áºµàµÍÃÕ¨è ÐŴŧµèÒí ÁÒ¡¨ÃÔ§æ (»ÃÐÁÒ³ 1.5V) ¨Ö§äÁ‹ÊÒÁÒöÃÑ¡ÉÒÃдѺáç´Ñ¹änj䴌 ÊíÒËÃѺ㹡óշáÕè ç´Ñ¹¨Ò¡áºµàµÍÃÕÅè ´µèÒí ¡Ç‹Ò 4V ǧ¨ÃµÃǨ¨Ñºáç´Ñ¹áºµàµÍÃÕ¨è зíÒ§Ò¹ áÅÐᨌ§àµ×͹ÍÍ¡ÁÒ·Õè LED ÊÕàËÅ×ͧµíÒá˹‹§ BATT. ¼Ù㌠ªŒ§Ò¹¤ÇÃà»ÅÕÂè ¹áºµàµÍÃÕãè ËÁ‹·¹Ñ ·Õ

A3.2.3 ʋǹáÊ´§¼Å Áմnj ¡ѹ 3 ʋǹ »ÃСͺ´ŒÇ (1) âÁ´ÙÅ LCD ÊíÒËÃѺáÊ´§¼Å¢ŒÍ¤ÇÒÁ ºÍÏ´ RBX-877 ÁÕâÁ´ÙÅ LCD 16 µÑÇÍÑ¡Éà 2 ºÃ÷Ѵàª×Íè Áµ‹Í¡ÑºäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ â´Âµ‹Í¢Ò RD4 ¶Ö§ RD7 ¢Í§ PIC16F887 ࢌҡѺ¢Ò D4 ¶Ö§ D7 ¢Í§âÁ´ÙÅ LCD à¾×Íè à¢Õ¹¢ŒÍÁÙÅáÊ´§¼ÅáÅФíÒÊѧè , µ‹Í¢Ò RD3 ¡Ñº¢Ò E à¾×Íè »‡Í¹ÊÑ­­Ò³¾ÑÅʏ ÊíÒËÃѺàÍç¹àÍàºÔÅ¡Ò÷íÒ§Ò¹¢Í§âÁ´ÙÅ LCD áÅе‹Í¢Ò RD2 ࢌҡѺ¢Ò RS ¢Í§âÁ´ÙÅ LCD à¾×Íè àÅ×Í¡ ¡ÒÃà¢Õ¹¢ŒÍÁÙÅËÃ×ͤíÒÊѧè ä»ÂѧâÁ´ÙÅ LCD ÁÕµÇÑ µŒÒ¹·Ò¹»ÃѺ¤‹Ò䴌 VR1 ¤‹Ò 10k: ·íÒ˹ŒÒ·Õ»è ÃѺ¤ÇÒÁ ࢌÁ¢Í§¨ÍáÊ´§¼Å ¶ŒÒËÒ¡µŒÍ§¡ÒÃãˌແ´ä¿Ê‹Í§ËÅѧ¢Í§âÁ´ÙÅ LCD ãˌ·Òí ¡Òõ‹Í¨ÑÁê à»ÍÏ·ÍÕè ÂÙ´‹ Ҍ ¹ ¢ÇÒÁ×ͧ͢âÁ´ÙÅ LCD (»¡µÔ¨Ðµ‹ÍänjáŌÇ) ¶ŒÒµŒÍ§¡Òû´à¾×Íè »ÃÐËÂÑ´¾Åѧ§Ò¹ãˌ»Å´¨ÑÁê à»ÍϹÍÕé Í¡

(2) LED 㪌áÊ´§Ê¶Ò¹Ðàº×Íé §µŒ¹ºÍÏ´ RBX-877 â´Âµ‹Í LED änj 1 ´Ç§à¢ŒÒ¡Ñº¢Ò ¾Íϵ RB3 ¢Í§äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC16F887


234 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

(3) ÅíÒ⾧ 㪌áÊ´§¼Å¡Ò÷íÒ§Ò¹´ŒÇÂÊÑ­­Ò³àÊÕ§ â´Âµ‹ÍÅíÒ⾧ໂÂâ«à¢ŒÒ·Õ¢è Ò¾Íϵ RC0 ¤ÇÒÁ¶Õàè Ãâ«á¹¹«¢Í§ÅíÒ⾧ໂÂ⫹ÕÍé ÂÙË ÐËNjҧ 1 ¶Ö§ 3kHz

LED áÊ´§Ê¶Ò¹ÐẵàµÍÃÕè

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

A3.2.4 ʋǹ¾ÍϵÍÔ¹¾ØµàÍÒµ¾µØ ¡ÒÃÃѺÊÑ­­Ò³ÍÔ¹¾ØµËÃ×͢ѺÍØ»¡Ã³àÍÒµ¾µØ ÀÒ¹͡ÊÒÁÒö¡ÃзíÒ䴌â´ÂµÃ§¼‹Ò¹¢Ò RB0 , RA0 ¶Ö§ RA3, RA5, RE0 ¶Ö§ RE2 áÅÐ RC3 ¶Ö§ RC7 ¢Ò¾ÍϵàËŋҹÕÊé ÒÁÒö¡íÒ˹´¡Ò÷íҧҹ䴌 ËÅÒÂÃٻẺ ´Ñ§¹Õé

(1) RB0 , RA0 ¶Ö§ RA3, RA5 áÅÐ RE0 ¶Ö§ RE2

¡ÒèѴ¢Ò¢Í§ ¨Ø´µ‹Í 3 ¢Ò

(1.1) ÍÔ¹¾ØµÍйÒÅÍ¡ à¾×Íè ÃѺÊÑ­­Ò³ÍÔ¹¾ØµáººÍйÒÅ͡ʋ§à¢ŒÒä»»ÃÐÁÇżÅÂѧ ǧ¨Ãá»Å§ÊÑ­­Ò³ÍйÒÅ͡໚¹´Ô¨µÔ ÍÅ¢¹Ò´ 10 ºÔµÀÒÂã¹äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC16F887 (1.2) ÍÔ¹¾Øµ´Ô¨µÔ ÍÅ à¾×Íè ÃѺÊÑ­­Ò³Åͨԡ “0” áÅÐ “1” ¨Ò¡ÍØ»¡Ã³ÀÒ¹͡


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹ l 235

(1.3) àÍÒµ¾µØ ´Ô¨µÔ ÍÅ à¾×Íè ʋ§ÊÑ­­Ò³Åͨԡ “0” áÅÐ “1” ä»ÂѧÍØ»¡Ã³ÀÒ¹͡ (1.4) ÍÔ¹¾ØµÍÔ¹àµÍÃÃ»Ñ µ ÊíÒËÃѺ¡Ò÷íҧҹẺ¹Õ¨é ÐÃͧÃѺ੾ÒТҾÍϵ RB0/INT ෋ҹѹé àÁ×Íè àÍç¹àÍàºÔÅ¡ÒõͺʹͧÍÔ¹àµÍÃÃ»Ñ µ¨Ò¡ÀÒ¹͡ áÅÐÁÕ¡ÒÃà»ÅÕÂè ¹á»Å§Ê¶Ò¹Ð·ÕÅè ͨԡ à¡Ô´¢Ö¹é ·Õ¢è Ò¾Íϵ RB0 ¹Õé «Õ¾ÂÕ ¨Ù СÃÐâ´´ä»·íÒ§Ò¹·Õâè »Ãá¡ÃÁ‹ÍºÃÔ¡ÒÃÍÔ¹àµÍÃÃ»Ñ µ·¹Ñ ·Õ â´Â¤‹ÒµÑ§é µŒ¹¨Ð¡íÒ˹´ãˌ¢Ò¾Íϵ·Ñ§é 9 ¢Ò¹Õ·é Òí §Ò¹à»š¹ÍÔ¹¾ØµÍйÒÅÍ¡

(2) RC3 áÅÐ RC4

(2.1) ÍÔ¹¾Øµ´Ô¨µÔ ÍÅ à¾×Íè ÃѺÊÑ­­Ò³Åͨԡ “0” áÅÐ “1” ¨Ò¡ÍØ»¡Ã³ÀÒ¹͡ (2.2) àÍÒµ¾µØ ´Ô¨µÔ ÍÅ à¾×Íè ʋ§ÊÑ­­Ò³Åͨԡ “0” áÅÐ “1” ä»ÂѧÍØ»¡Ã³ÀÒ¹͡ (2.3) ¢Òàª×Íè Áµ‹ÍÃкººÑÊ I2C â´Â¡íÒ˹´ãˌ RC3 ໚¹¢ÒÊÑ­­Ò³¹Ò́͡Ò͹ءÃÁ (SCL) áÅÐ¢Ò RC4 ໚¹¢Ò¢ŒÍÁÙÅ͹ءÃÁ (SDA) ÊíÒËÃѺàª×Íè Áµ‹Í¡ÑºÍØ»¡Ã³ÀÒ¹͡¼‹Ò¹ÃкººÑÊ I2C µÑÇÍ‹ҧ¢Í§ÍØ»¡Ã³ÃкººÑÊ I2C 䴌ᡋ CMPS03 âÁ´ÙÅà¢çÁ·ÔÈÍÔàÅç¡·Ã͹ԡʏ, DS1307 äÍ«Õ °Ò¹àÇÅÒ¹Ò́¡Ò¨ÃÔ§ (ÃÕÅä·Á¤ÅçÍ¡), 24Cxx äÍ«Õ˹‹Ç¤ÇÒÁ¨íÒ¢ŒÍÁÙÅÍÕÍ¾Õ ÃÍÁ, ZX-RELAY16 ºÍÏ´ ¢ÑºÃÕàŏ 16 ª‹Í§ , DS1621 äÍ«ÕÇ´Ñ ÍسËÀÙÁ,Ô SHT11 âÁ´ÙÅÇÑ´¤ÇÒÁª×¹é ÊÑÁ¾Ñ·¸ ໚¹µŒ¹

ZX-RELAY16 ºÍÏ´¢ÑºÃÕàŏ 16 ª‹Í§¼‹Ò¹ÃкººÑÊ I2C CMPS03 âÁ´ÙÅà¢çÁ·ÔÈ´Ô¨µÔ ÍÅ


236 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

(3) RC6 áÅÐ RC7

(3.1) ÍÔ¹¾Øµ´Ô¨µÔ ÍÅ à¾×Íè ÃѺÊÑ­­Ò³Åͨԡ “0” áÅÐ “1” ¨Ò¡ÍØ»¡Ã³ÀÒ¹͡ (3.2) àÍÒµ¾µØ ´Ô¨µÔ ÍÅ à¾×Íè ʋ§ÊÑ­­Ò³Åͨԡ “0” áÅÐ “1” ä»ÂѧÍØ»¡Ã³ÀÒ¹͡ (3.3) ¢ÒÊíÒËÃѺÊ×èÍÊÒâŒÍÁÙÅ͹ءÃÁ â´Â¢Ò¾Íϵ¹Õéàª×èÍÁµ‹Í¡ÑºâÁ´ÙÅÊ×èÍÊÒâŒÍÁÙŠ͹ءÃÁËÃ×Í USART ÀÒÂã¹ PIC16F887 ´Ñ§¹Ñ¹é ¨Ö§ÊÒÁÒö¹íÒä»ãªŒàª×Íè Áµ‹Í¡ÑºÍØ»¡Ã³ÀÒ¹͡ÃÇÁ ·Ñ駾Íϵ͹ءÃÁ¢Í§¤ÍÁ¾ÔÇàµÍÏ à¾×èÍÊ×èÍÊÒâŒÍÁÙÅ͹ءÃÁÃÐËNjҧ¡Ñ¹áÅСѹ â´Â¢Ò RC6 ໚¹¢Ò ÃѺ¢ŒÍÁÙÅ͹ءÃÁ (RxD) áÅÐ¢Ò RC7 ໚¹¢Òʋ§¢ŒÍÁÙÅ͹ءÃÁ (TxD) 㹡óշÕ赌ͧ¡ÒÃàª×èÍÁµ‹Í¡Ñº ¾Íϵ͹ءÃÁ¢Í§¤ÍÁ¾ÔÇàµÍϨеŒÍ§µ‹Í¢Ò·Ñ§é Êͧ¹Õ¼é ҋ ¹Ç§¨Ãá»Å§ÃдѺÊÑ­­Ò³´ŒÇÂäÍ«Õ·ÃÒ¹«Õ¿ àÇÍÏàºÍÏ MAX232 ໚¹µŒ¹ ¹Í¡¨Ò¡¹Ñé¹ÂѧÊÒÁÒöµ‹Í¢Ò¾Íϵ·Ñé§Êͧ¹Õé¡ÑºÍØ»¡Ã³Ê×èÍÊÒâŒÍÁÙŠ͹ءÃÁẺäÌÊÒ ÍÒ·Ô ZX-BLUETOOTH à¾×èÍÃѺʋ§¢ŒÍÁÙÅ͹ءÃÁ¡Ñº¤ÍÁ¾ÔÇàµÍÏËÃ×Íâ·ÃÈѾ· à¤Å��Íè ¹·Õ¼è ҋ ¹ÃкººÅÙ·¸Ù , âÁ´ÙÅ XBee áÅÐ XBee PRO ¾×Íè ÃѺʋ§¢ŒÍÁÙÅ͹ءÃÁ¼‹Ò¹Ãкº ZIGBEE, ZX-SERVO16 ºÍÏ´¢Ñºà«ÍÏâÇàµÍÏ 16 ª‹Í§, ZX-17 ºÍÏ´°Ò¹àÇÅÒ¹Ò́¡Ò¨ÃԧẺ͹ءÃÁ, ZX-19 ºÍÏ´ºÑ¹·Ñ¡áÅÐàŋ¹¡ÅѺàÊÕ§Ẻ͹ءÃÁ, ZX-44 ºÍÏ´ÊÇÔµªàÁµÃÔ¡« 4x4 ¨Ø´ ໚¹µŒ¹

ZX-BLUETOOTH âÁ´ÙÅÊ×Íè ÊÒà ¢ŒÍÁÙÅäÌÊÒ¼‹Ò¹ºÅÙ·¸Ù ZX-44 ºÍÏ´ÊÇÔµª àÁµÃÔ¡« 4x4 ¨Ø´ XBee-PRO

ZX-SERVO16ºÍÏ ´ ¢Ñ º à«ÍÏâÇÁÍàµÍÏ 16 ª‹Í§


à¢Õ¹â»Ãá¡ÃÁÀÒÉÒ C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹ l 237

A3.2.5 ǧ¨Ã¢ÑºÁÍàµÍÏ俵ç

¡ÒâѺÁÍàµÍÏ俵ç¢Í§ºÍÏ´ RBX-877 㪌äÍ«ÕàºÍÏ L293D ÊÒÁÒö¢ÑºÁÍàµÍÏ俵ç䴌 2 ª‹Í§ â´Â¡íÒ˹´à»š¹ª‹Í§ M-1 áÅÐ M-2 â´Âǧ¨Ã¢ÑºÁÍàµÍϪ͋ § M-1 ä´ŒÃºÑ ¡ÒäǺ¤ØÁ¨Ò¡¢Ò RD0 ¡Ñº RD1 áÅÐàÍç¹àÍàºÔÅ¡Ò÷íÒ§Ò¹´ŒÇÂ¢Ò RC2 ã¹¢³Ð·Õªè ͋ § M-2 ä´ŒÃºÑ ¡ÒäǺ¤ØÁ¨Ò¡¢Ò¾Íϵ RB1 ¡Ñº RB2 áÅÐàÍç¹àÍàºÔÅ¡Ò÷íÒ§Ò¹´ŒÇ¢ҾÍϵ RC1 ·Õàè ÍÒµ¾µØ ¢Í§Ç§¨Ã¢ÑºÁÍàµÍÏ·§Ñé Êͧª‹Í§ÁÕ LED 2 ÊÕ áÊ´§¼Å¡Ò÷íÒ§Ò¹¢Í§ÁÍàµÍÏ (ÊÕà¢ÕÂÇᨌ§¡Òè‹ÒÂ俵ç¢ÑÇé áÅÐÊÕᴧᨌ§¡Òè‹Ò¡ÅѺ¢ÑÇé ) ÊíÒËÃѺ¢ÒàÍç¹àÍàºÔÅ¹Ñ¹é ¹Í¡¨Ò¡¹íÒÁÒ㪌àÍç¹àÍàºÔÅËÃ×Íແ´»´¡Ò÷íÒ§Ò¹¢Í§Ç§¨Ã¢ÑºÁÍàµÍÏ ᵋÅеÑÇáÅŒÇ Âѧ㪌¤Çº¤ØÁ¤ÇÒÁàÃçÇ㹡ÒÃËÁع¢Í§ÁÍàµÍÏ䴌´ÇŒ  â´Â»‡Í¹ÊÑ­­Ò³ PWM ࢌҷբè Ò àÍç¹àÍàºÔŹÕéá·¹

A3.2.6 ʋǹ¢Ñºà«ÍÏâÇÁÍàµÍÏ

¢Ò RB4, RB5 áÅÐ RC5 㪌ÊÒí ËÃѺ¢Ñºà«ÍÏâÇÁÍàµÍÏ â´Âä¿àÅÕÂé §¢Í§à«ÍÏâÇÁÍàµÍÏ䴌ÁÒ â´ÂµÃ§¨Ò¡áºµàµÍÃÕè à¾×Íè ãˌÁ¡Õ ÃÐáÊÁÒ¡¾Í·Õãè ªŒ¢ºÑ ÁÍàµÍÏ ઋ¹à´ÕÂǡѺÁÍàµÍÏ俵ç ¡ÒÃÊÑ§è §Ò¹ ¨Ò¡äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ㪌¡Òû‡Í¹ÊÑ­­Ò³¾ÑÅʏà¾×èͤǺ¤ØÁ¡ÒÃËÁع¢Í§á¡¹à«ÍÏâÇÁÍàµÍÏ


238 là¢Õ¹â»Ãá¡ÃÁÀÒÉÒ

C ÊíÒËÃѺäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC ´ŒÇ mikroC áÅСÒ÷´Åͧàº×Íé §µŒ¹

A3.2.7 ǧ¨ÃÊÇÔµª º¹ºÍÏ´ RBX-877 ÁÕǧ¨ÃÊÇÔµª¡´µÔ´»Å‹Í´Ѻ 2 ª‹Í§ «Ö§è µ‹ÍࢌҡѺ¢Ò¾Íϵ RA4 áÅÐ RB0 ¢Í§äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC16F887 ÊíÒËÃѺ¢Ò RB0 ¹Ñ¹é ¨Ð໚¹µíÒá˹‹§¢Òà´ÕÂǡѺ¢Ò¾ÍϵÍÔ¹¾Øµ ÍÔ¹àµÍÃÃ»Ñ µ ¨Ö§·íÒãˌÊÇÔµª·¢Õè Ò RB0 ÊÒÁÒö㪌ÊÌҧÊÑ­­Ò³ÍÔ¹àµÍÃÃ»Ñ µàÁ×Íè ¡´ÊÇÔµªä´Œ´ÇŒ  º¹ºÍÏ´ RBX-877 ÂѧÁÕÊÇÔµª¡´µÔ´»Å‹Í´ѺÍÕ¡Ë¹Ö§è µÑÇ ¹Ñ¹è ¤×Í ÊÇÔµª RESET «Ö§è µ‹ÍࢌҡѺ¢Ò RE3/MCLR â´Â»¡µÔ¨Ð㪌§Ò¹à»š¹¢ÒÃÕ૵¡Ò÷íÒ§Ò¹¢Í§äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ ᵋ¼ãٌ ªŒ§Ò¹ÊÒÁÒö ¡íÒ˹´¤‹Ò¤Í¹¿¡ãËÁ‹ à¾×Íè 㪌§Ò¹¢Ò¹Õàé »š¹¢ÒÍÔ¹¾Øµ´Ô¨µÔ ÍÅ䴌 â´Â¢Ò¹Õ¨é СÅÒÂ໚¹¢Ò¾Íϵ RE3 á·¹ ÊÇÔµª RESET ËÃ×Í RE3

A3.2.8 ᨍ¡ ICD2 : ¨Ø´µ‹Íà¤Ã×Íè §â»Ãá¡ÃÁäÁâ¤Ã¤Í¹â·ÃÅàÅÍÏÀÒ¹͡ 㹡ÒÃ㪌§Ò¹ºÍÏ´ RBX-877 ¨ÐµŒÍ§ÁÕ¡ÒþѲ¹Òâ»Ãá¡ÃÁº¹¤ÍÁ¾ÔÇàµÍÏ à¾×Íè ãˌ䴌ä¿Å¼ÅÅѾ¸ ¹ÒÁÊ¡ØÅ .hex ¨Ò¡¹Ñ¹é ¹íÒä¿Å .hex â»Ãá¡ÃÁŧã¹Ë¹‹Ç¤ÇÒÁ¨íÒâ»Ãá¡ÃÁ¢Í§äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC16F887 â´ÂºÍÏ´â»Ãá¡ÃÁ¹Ñé¹µŒÍ§·íÒ§Ò¹¡Ñº¤ÍÁ¾ÔÇàµÍÏâ´Âµ‹ÍࢌҡѺ¾Íϵ USB áŌÇ㪌 «Í¿µáÇÏ PICkit2 Programming 㹡ÒÃâ»Ãá¡ÃÁ¢ŒÍÁÙÅŧã¹äÁâ¤Ã¤Í¹â·ÃÅàÅÍÏ PIC16F887 â´Â¡ÒõԴµ‹ÍÃÐËNjҧºÍÏ´â»Ãá¡ÃÁ¡ÑººÍÏ´ RBX-877 ¹Ñ¹é ¨Ð¡ÃзíÒ¼‹Ò¹á¨¡ ICD2 ´Ñ§ÃÙ» ºÍÏ´â»Ãá¡ÃÁ PIC

ÊÒÂ ICD2

4.8-6V

Sound RC0

LCD RC7 RxD

BATT

RC6 TxD

RC4 SDA

RC3 SCL

DOWNLOAD

POWER

Robo - PICARRobotR PICRMicroRobotic

ON M-1 M-2

S+

SERVO #1 #2

#3

µ‹Í¡Ñº¾Íϵ USB ¢Í§¤ÍÁ¾ÔÇàµÍÏ

LCD back light

ICD2

ON

RA4

RESET

RB0

RB3 RB0/INT

INNOVATIVE EXPERIMENT

RA0

RA1

RA2

RA3

RA5

RE0

RE1

RE2


mikroC_book