{"id":2827,"date":"2019-09-27T08:41:22","date_gmt":"2019-09-27T08:41:22","guid":{"rendered":""},"modified":"2019-09-27T16:43:36","modified_gmt":"2019-09-27T08:43:36","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8pic%e5%8d%95%e7%89%87%e6%9c%ba%e4%b8%8a%e5%ae%9e%e7%8e%b0%e5%8f%91%e9%80%81%e6%95%b0%e6%8d%ae","status":"publish","type":"post","link":"http:\/\/www.szryc.com\/?p=2827","title":{"rendered":"\u5982\u4f55\u5728PIC\u5355\u7247\u673a\u4e0a\u5b9e\u73b0\u53d1\u9001\u6570\u636e"},"content":{"rendered":"
\n\t\u8be5\u7a0b\u5e8f\u8fd0\u884c\u5728pI<\/u>C\u5355\u7247\u673a<\/u>p18f458\u4e0a\u5b9e\u73b0\u53d1\u9001\u7f13\u51b2\u56680\u5411\u63a5\u6536\u7f13\u51b2\u56680\u53d1\u9001\u6570\u636e\u7684\u81ea\u6d4b\u8bd5<\/u>\u6a21\u5f0f\uff0c\u5176\u4e2d\u63a5\u6536\u91c7\u7528\u4e2d\u65ad\u65b9\u5f0f\u3002<\/p>\n
\n\t\/\/\u53d1\u9001\u91c7\u7528\u67e5\u8be2\u65b9\u5f0f<\/p>\n
\n\t#include “p18f458.h”<\/p>\n
\n\tintCAN<\/u>_FLAG; \/\/\u5b9a\u4e49\u6807\u5fd7\u5bc4\u5b58\u5668<\/u><\/p>\n
\n\t\/\/**************\u521d\u59cb\u5316\u5b50\u7a0b\u5e8f***************<\/p>\n
\n\tvoid initcan\uff08\uff09<\/p>\n
\n\t{<\/p>\n
\n\tTRISB=\uff08TRISB|0X08\uff09&0XFB; \/\/\u8bbe\u7f6eCANRX\/RB3\u4e3a\u8f93\u5165\uff0cCANTX\/RB2\u4e3a\u8f93\u51fa<\/p>\n
\n\t\/\/\u8bbe\u7f6eCAN\u7684\u6ce2\u7279\u7387\u4e3a125K\uff0c\u5728Fosc=4M\u65f6\uff0cTbit=8us\uff0c\u5047\u8bbeBRP=01h<\/p>\n
\n\t\/\/\u5219TQ=\uff3b2*\uff081+BRP\uff09\uff3d\/Fosc=2*\uff081+1\uff09\/4=1us\u3002<\/p>\n
\n\t<\/p>\n
\n\t\/\/NOMI<\/u>NAL BIT RATE<\/u>=8TQ\uff0cSJW=1\uff0cSync_Seg=1TQ\uff0cProg _Seg=1TQ\uff0cPhase_Seg1=3TQ\uff0cPhase_Seg2=3TQ<\/p>\n
\n\tCANCON=0X80; \/\/\u8bf7\u6c42\u8fdb\u5165CAN\u914d\u7f6e\u6a21\u5f0fREQOP=100<\/p>\n
\n\twhile\uff08CANSTAT&0X80==0\uff09{;} \/\/\u7b49\u5f85\u8fdb\u5165CAN\u914d\u7f6e\u6a21\u5f0fOPMODE=100<\/p>\n
\n\tBRGCON1=0X01; \/\/\u8bbe\u7f6eSJW\u548cBRP\uff0cSJW=1TQ\uff0cBRP=01H<\/p>\n
\n\tBRGCON2=0X90; \/\/\u8bbe\u7f6ePhase_Seg1=3TQ\u548cProg _Seg=1TQ<\/p>\n
\n\tBRGCON3=0X42; \/\/\u8bbe\u7f6ePhase_Seg2=3TQ<\/p>\n
\n\t\/\/\u8bbe\u7f6e\u53d1\u9001\u90ae\u7bb10\u6807\u8bc6\u7b26\u53f7\u548c\u53d1\u9001\u7684\u6570\u636e<\/p>\n
\n\tTXB0CON=0X03; \/\/\u53d1\u9001\u4f18\u5148\u7ea7\u4e3a\u6700\u9ad8\u4f18\u5148\u7ea7\uff0cTXPRI=11<\/p>\n
\n\tTXB0SIDH=0XFF; \/\/\u8bbe\u7f6e\u53d1\u9001\u7f13\u51b2\u56680\u7684\u6807\u51c6\u6807\u8bc6\u7b26\uff0c\u672c\u7a0b\u5e8f\u91c7\u7528\u6807\u51c6\u8bc6\u7b26<\/p>\n
\n\tTXB0SIDL=0XE0;<\/p>\n
\n\tTXB0DLC=0X08; \/\/\u8bbe\u7f6e\u6570\u636e\u957f\u5ea6\u4e3a8\u4e2a\u5b57\u8282<\/p>\n
\n\tTXB0D0=0X00; \/\/\u5199\u53d1\u9001\u7f13\u51b2\u5668\u6570\u636e\u533a\u7684\u6570\u636e<\/p>\n
\n\tTXB0D1=0X01;<\/p>\n
\n\tTXB0D2=0X02;<\/p>\n
\n\tTXB0D3=0X03;<\/p>\n
\n\tTXB0D4=0X04;<\/p>\n
\n\tTXB0D5=0X05;<\/p>\n
\n\tTXB0D6=0X06;<\/p>\n
\n\tTXB0D7=0X07;<\/p>\n
\n\t\/\/\u8bbe\u7f6e\u63a5\u6536\u90ae\u7bb10\u7684\u6807\u8bc6\u7b26\u548c\u521d\u59cb\u5316\u6570\u636e<\/p>\n
\n\tRXB0SIDH=0XFF; \/\/\u8bbe\u7f6e\u63a5\u6536\u7f13\u51b2\u56680\u7684\u6807\u8bc6\u7b26<\/p>\n
\n\tRXB0SIDL=0XE0;<\/p>\n
\n\tRXB0CON=0X20; \/\/\u4ec5\u4ec5\u63a5\u6536\u6807\u51c6\u6807\u8bc6\u7b26\u7684\u6709\u6548\u4fe1\u606f\uff0cFILHIT0=0\u8868\u793aRXB0\u91c7\u7528fiLTEr0<\/p>\n
\n\tRXB0DLC=0X08; \/\/\u8bbe\u7f6e\u63a5\u6536\u7f13\u51b2\u56680\u7684\u6570\u636e\u533a\u957f\u5ea6<\/p>\n
\n\tRXB0D0=0X00; \/\/\u521d\u59cb\u5316\u63a5\u6536\u7f13\u51b2\u56680\u7684\u6570\u636e\u533a\u6570\u636e<\/p>\n
\n\tRXB0D1=0X00;<\/p>\n
\n\tRXB0D2=0X00;<\/p>\n
\n\tRXB0D3=0X00;<\/p>\n
\n\tRXB0D4=0X00;<\/p>\n
\n\tRXB0D5=0X00;<\/p>\n
\n\tRXB0D6=0X00;<\/p>\n
\n\tRXB0D7=0X00;<\/p>\n
\n\t\/\/\u521d\u59cb\u5316\u63a5\u6536\u6ee4\u6ce2\u5668<\/u>0\u548c\u63a5\u6536\u5c4f\u853d\uff0c<\/p>\n
\n\tRXF0SIDH=0XFF;<\/p>\n
\n\tRXF0SIDL=0XE0;<\/p>\n
\n\tRXM0SIDH=0X00;<\/p>\n
\n\tRXM0SIDL=0X00;<\/p>\n
\n\t\/\/\u521d\u59cb\u5316CAN\u6a21\u5757\u7684I\/O<\/u>\u63a7\u5236\u5bc4\u5b58\u5668<\/p>\n
\n\t\/\/CIOCON=0X00;<\/p>\n
\n\t\/\/\u4f7fCAN\u8fdb\u5165\u67d0\u79cd\u5de5\u4f5c\u6a21\u5f0f\u6a21\u5f0f<\/p>\n
\n\tCANCON=0X00; \/\/=0X40\uff0c\u8fdb\u5165\u81ea\u6d4b\u8bd5\u6a21\u5f0f;=0x00\uff0c\u6b63\u5e38\u64cd\u4f5c\u6a21\u5f0f<\/p>\n
\n\twhile\uff08CANSTAT&0XE0\uff01=0\uff09{;}<\/p>\n
\n\t\/\/while\uff08CANSTAT&0X40==0\uff09{;}<\/p>\n
\n\t\/\/\u521d\u59cb\u5316CAN\u7684\u4e2d\u65ad<\/p>\n
\n\tPIR3=0X00; \/\/\u6e05\u6240\u6709\u4e2d\u65ad\u6807\u5fd7<\/p>\n
\n\tPIE3=0X01; \/\/\u4f7f\u80fd\u63a5\u6536\u7f13\u51b2\u56680\u7684\u63a5\u6536\u4e2d\u65ad<\/p>\n
\n\tIPR3=0X01; \/\/\u63a5\u6536\u7f13\u51b2\u56680\u7684\u63a5\u6536\u4e2d\u65ad\u4e3a\u6700\u9ad8\u4f18\u5148\u7ea7<\/p>\n
\n\t}<\/p>\n
\n\t\/\/*********\u63a5\u6536\u7f13\u51b2\u56680\u63a5\u6536\u4e2d\u65ad\u670d\u52a1\u7a0b\u5e8f***********<\/p>\n
\n\t#pragma interrupt can_isr<\/p>\n
\n\t#pragma code low_ISR=0x18<\/p>\n
\n\tvoid low_ISR\uff08\uff09<\/p>\n
\n\t{<\/p>\n
\n\t_asm<\/p>\n
\n\tgoto can_isr<\/p>\n
\n\t_endasm<\/p>\n
\n\t}<\/p>\n
\n\t#pragma code<\/p>\n
\n\tvoid can_isr\uff08\uff09<\/p>\n
\n\t{<\/p>\n
\n\tif<\/u>\uff08PIR3bits.RXB0IF==1\uff09 CAN_FLAG=1;<\/p>\n
\n\tPIR3bits.RXB0IF=0; \/\/\u6e05\u63a5\u6536\u4e2d\u65ad\u6807\u5fd7<\/p>\n
\n\tRXB0CONbits.RXFUL=0; \/\/\u6253\u5f00\u63a5\u6536\u7f13\u51b2\u5668\u6765\u63a5\u6536\u65b0\u4fe1\u606f<\/p>\n
\n\t}<\/p>\n
\n\t\/\/****************\u4e3b\u7a0b\u5e8f**can\u603b\u7ebf\u6536\u53d1\u7f16\u7a0b****************<\/p>\n
\n\tmai<\/u>n\uff08\uff09<\/p>\n
\n\t{<\/p>\n
\n\tINTCON=0x00; \/\/\u7981\u6b62\u6240\u6709\u4e2d\u65ad<\/p>\n
\n\tinitcan\uff08\uff09;<\/p>\n
\n\tWDTCON=0; \/\/=1\uff0c\u770b\u95e8\u72d7<\/u>\u4f7f\u80fd;=0\uff0c\u7981\u6b62\u770b\u95e8\u72d7<\/p>\n
\n\tINTCON=0xc0; \/\/\u4f7f\u80fd\u4e2d\u65ad<\/p>\n
\n\twhile\uff081\uff09<\/p>\n
\n\t{<\/p>\n
\n\tTXB0CONbits.TXREQ=1; \/\/\u8bf7\u6c42\u53d1\u9001\uff0cTXREQ=1<\/p>\n
\n\twhile\uff08PIR3bits.TXB0IF\uff01=1\uff09{;} \/\/\u7b49\u5f85\u53d1\u9001\u5b8c\u6210<\/p>\n
\n\twhile\uff08CAN_FLAG==0\uff09{;} \/\/\u7b49\u5f85\u63a5\u6536\u6570\u636e<\/p>\n
\n\tCAN_FLAG=0; \/\/\u6e05\u63a5\u6536\u5230\u6807\u5fd7<\/p>\n
\n\tTXB0CONbits.TXREQ=0; \/\/\u7981\u6b62\u53d1\u9001<\/p>\n
\n\tTXB0D0=RXB0D0+1; \/\/\u7528\u63a5\u6536\u6570\u636e\u52a01\u6765\u66f4\u65b0\u53d1\u9001\u6570\u636e<\/p>\n
\n\tTXB0D1=RXB0D1+1;<\/p>\n
\n\tTXB0D2=RXB0D2+1;<\/p>\n
\n\tTXB0D3=RXB0D3+1;<\/p>\n
\n\tTXB0D4=RXB0D4+1;<\/p>\n
\n\tTXB0D5=RXB0D5+1;<\/p>\n
\n\tTXB0D6=RXB0D6+1;<\/p>\n
\n\tTXB0D7=RXB0D7+1;<\/p>\n
\n\t}<\/p>\n
\n\t}
\n\t\u6765\u6e90\uff1b21ic<\/p>\n","protected":false},"excerpt":{"rendered":"
\u8be5\u7a0b\u5e8f\u8fd0\u884c\u5728 pI C \u5355\u7247\u673a p18f458\u4e0a\u5b9e\u73b0\u53d1\u9001\u7f13\u51b2\u56680\u5411\u63a5\u6536\u7f13\u51b2\u56680\u53d1\u9001\u6570\u636e\u7684\u81ea \u6d4b\u8bd5 \u6a21\u5f0f\uff0c\u5176\u4e2d\u63a5\u6536\u91c7\u7528\u4e2d\u65ad\u65b9\u5f0f\u3002 \/\/\u53d1\u9001\u91c7\u7528\u67e5\u8be2\u65b9\u5f0f #include p18f458.h int CAN _FLAG; \/\/\u5b9a\u4e49\u6807\u5fd7 \u5bc4\u5b58\u5668<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14],"tags":[],"_links":{"self":[{"href":"http:\/\/www.szryc.com\/index.php?rest_route=\/wp\/v2\/posts\/2827"}],"collection":[{"href":"http:\/\/www.szryc.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.szryc.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.szryc.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.szryc.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2827"}],"version-history":[{"count":0,"href":"http:\/\/www.szryc.com\/index.php?rest_route=\/wp\/v2\/posts\/2827\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.szryc.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2827"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.szryc.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2827"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.szryc.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2827"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}