CC2530鐨勯€氱敤IO鍙g殑杈撳叆杈撳嚭
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CC2530鐨勯€氱敤IO鍙g殑杈撳叆杈撳嚭相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/%e7%8a%b6%e6%80%81' title='鐘舵€?>鐘舵€?/a> style clu 浣嶇疆 show 鏈川 鎶栧姩 杈撳叆
涓€銆佸紩鑴氭杩?/p>
CC2530鏈?0 涓紩鑴氥€傚叾涓紝鏈?strong>21涓暟瀛桰/O绔彛
锛屽叾涓?strong>P0鍜孭1鏄? 浣嶇鍙?/strong>锛?strong>P2浠呮湁5浣?/strong>鍙互浣跨敤銆侾2绔彛鐨?涓紩鑴氫腑锛屾湁2涓渶瑕佺敤浣滀豢鐪燂紝鏈?涓渶瑕佺敤浣滄櫠鎸€傛墍浠ュ彲渚涙垜浠娇鐢ㄧ殑灏卞彧鏈?7涓紩鑴氫簡銆?/p>
鎿嶄綔寰帶鍒跺櫒鐨勬湰璐?/strong>锛屽氨鏄杩欎簺鐗规畩鍔熻兘瀵勫瓨鍣紙SFR锛夎繘琛岃鍐欐搷浣滐紝骞朵笖鏌愪簺鐗规畩鍔熻兘瀵勫瓨鍣ㄥ彲浠?strong>浣嶅鍧€銆?/p>
姣忎竴涓?strong>鐗规畩鍔熻兘瀵勫瓨鍣?/strong>鏈川涓婂氨鏄竴涓?strong>鍐呭瓨鍗曞厓锛屼负浜嗕究浜庝娇鐢紝姣忎釜鐗规畩鍔熻兘瀵勫瓨鍣ㄩ兘浼氳捣涓€涓悕瀛楋紝鍦ㄧ▼搴忚璁℃椂锛屽彧瑕佸紩鍏ュご鏂囦欢“ioCC2530.h”锛屽氨鍙互鐩存帴浣跨敤瀵勫瓨鍣ㄧ殑鍚嶇О璁块棶鍐呭瓨鍦板潃浜嗐€?/p>
CC2530鐨勯€氱敤I/O绔彛鐩稿叧鐨勫父鐢ㄥ瘎瀛樺櫒鏈変笅闈?涓細
<1> PxSEL锛氱鍙e姛鑳介€夋嫨锛岃缃鍙f槸閫氱敤I/O杩樻槸澶栬鍔熻兘銆?/p>
<2> PxDIR锛氫綔涓洪€氱敤I/O鏃讹紝鐢ㄦ潵璁剧疆鏁版嵁鐨勪紶杈撴柟鍚戙€?/p>
<3> PxINP锛氫綔涓洪€氱敤杈撳叆绔彛鏃讹紝閫夋嫨杈撳叆妯″紡鏄?strong>涓婃媺銆?strong>涓嬫媺杩樻槸涓夋€?/strong>銆?/p>
<4> Px锛氭暟鎹鍙o紝鐢ㄦ潵鎺у埗绔彛鐨勮緭鍑烘垨鑾峰彇绔彛鐨勮緭鍏ャ€?/p>
鍏蜂綋鐨勫瘎瀛樺櫒鍔熻兘鍜屽簲鐢ㄤ笅闈細璇村埌銆?/p>
浜屻€佽缃瘎瀛樺櫒鐨勬柟娉?/p>
<1> 瀵瑰瘎瀛樺櫒鐨勬煇浜涗綅娓?鑰屼笉褰卞搷鍏朵粬浣嶃€?/strong> 浣跨敤“&=”灏嗗瘎瀛樺櫒鎸囧畾浣嶆竻0锛?/strong>鍚屾椂涓嶅奖鍝嶅叾浠栦綅鐨勫€笺€?nbsp; 鍥犱负锛?strong>閫昏緫“涓?rdquo;
Tip锛氳鏂规硶鍙兘鎿嶄綔澶氫綅鍚屾椂娓?锛屾垨鑰呮煇涓€浣嶆竻0鐨勬儏鍐碉紝濡傛灉瑕佸皢瀵勫瓨鍣ㄧ殑浣嶆棦瑕佹竻0鍙堣缃?锛屽垯涓嶈兘閲囩敤杩欑鍐欐硶銆?/p>
瀵逛簬瀵勫瓨鍣ㄧ殑绗琻浣嶇殑娓?鎿嶄綔涔熷彲浠ュ啓鎴愶細瀵勫瓨鍣?nbsp;&= ~(0x01<<(n))
<2> 瀵瑰瘎瀛樺櫒鐨勬煇浜涗綅缃?鑰屼笉褰卞搷鍏朵粬浣嶃€?/strong>
浣跨敤“|=”灏嗗瘎瀛樺櫒鎸囧畾浣嶇疆1锛屽悓鏃朵笉褰卞搷鍏朵粬浣嶇殑鍊笺€?/p>
鍥犱负锛?strong>閫昏緫“鎴?rdquo;鎿嶄綔鐨勭壒鐐规槸锛岃浣嶆湁1缁撴灉灏变负1锛岃嫢涓?鍒欎繚瀛樺師鏉ュ€间笉鍙樸€?/p>
瀵逛簬瀵勫瓨鍣ㄧ殑绗琻浣嶇殑娓?鎿嶄綔涔熷彲浠ュ啓鎴愶細瀵勫瓨鍣?nbsp;|= (0x01<<(n))锛?/strong>
棰樼洰锛氬疄鐜?涓寜閿垎鍒帶鍒?涓狶ED鐏紑鍏崇殑鍔熻兘锛屽嵆SW1鎸変笅鍚庢澗寮€锛孡ED5浜紝SW1鍐嶆鎸変笅鍚庢澗寮€, LED5鐏€係W2浠ュ悓鏍风殑鏂瑰紡鎺у埗LED6銆傛湰棰樹腑鐨勫紩鑴氬浘
娑夊強鍒扮殑瀵勫瓨鍣?/p>
绔彛鍔熻兘閫夋嫨瀵勫瓨鍣ㄣ€?/p>
绔彛浼犺緭鏂瑰悜璁剧疆瀵勫瓨鍣ㄣ€?/p>
杈撳叆绔彛闇€瑕佽缃叾杈撳叆鏂瑰紡鐨勫瘎瀛樺櫒
绔彛鑳藉鎻愪緵“涓婃媺”銆?ldquo;涓嬫媺”鍜?ldquo;涓夋€?rdquo;涓夌杈撳叆妯″紡
渚嬪瓙锛氬皢P1_2绔彛涓烘寜閿緭鍏ワ紝璁剧疆涓鸿緭鍏ヤ笂鎷夋ā寮忋€?/p>
1 P1INP &=~0x04 //0000 0100->1111 1011 棣栧厛闇€瑕佸皢P1_2璁剧疆涓轰笂/涓嬫媺妯″紡 2 P2INP &=~0x40 //0100 0000 ->1011 1111 鍦ㄥ皢P1_2纭畾涓轰笂鎷夋ā寮?/span>
閫氱敤I/O绔彛瀵勫瓨鍣ㄩ厤缃殑鍩烘湰鎬濊矾銆?/strong>
璁捐鎬濊矾锛?/p>
1.璁捐涓€涓鍙e垵濮嬪寲鍑芥暟锛屽绔彛鐨勫姛鑳借繘琛岄厤缃紝LED鐩稿叧鐨勭鍙h缃负杈撳嚭锛屾寜閿浉鍏崇殑绔彛璁剧疆涓鸿緭鍏ワ紝骞朵笖閰嶇疆鎴愪笂鎷夋ā寮忋€?/p>
璁捐绔彛鍒濆鍖栧嚱鏁癐nitPort()銆?/strong>
<1>璁剧疆P1SEL瀵勫瓨鍣紝灏哖1_2銆丳1_3鍜孭1_4璁剧疆涓洪€氱敤I/O绔彛銆?/p>
<2>璁剧疆P1DIR瀵勫瓨鍣紝灏哖1_3鍜孭1_4璁剧疆涓鸿緭鍑猴紝灏哖1_2璁剧疆涓鸿緭鍏ャ€?/p>
<3>璁剧疆P0SEL瀵勫瓨鍣紝灏哖0_1璁剧疆涓洪€氱敤I/O绔彛銆?/p>
<4>璁剧疆P0DIR瀵勫瓨鍣紝灏哖0_1璁剧疆涓鸿緭鍏ャ€?/p>
<5>璁剧疆PxINP瀵勫瓨鍣紝灏哖0_1鍜孭1_2璁剧疆涓轰笂鎷夋ā寮忥紝涔熷彲浠ヤ笉璁剧疆銆?/p>
void InitPort() { P1SEL &= ~0x18; //灏哖1_3鍜孭1_4璁剧疆涓洪€氱敤I/O绔彛鍔熻兘 P1DIR |= 0x18; //灏哖1_3鍜孭1_4鐨勭鍙d紶杈撴柟寮忚缃负杈撳嚭 P1SEL &= ~0x04; //灏哖1_2璁剧疆涓洪€氱敤I/O绔彛鍔熻兘 P1DIR &= ~0x04; //灏哖1_2鐨勭鍙d紶杈撴柟寮忚缃负杈撳叆 P0SEL &= ~0x02; //灏哖0_1璁剧疆涓洪€氱敤I/O绔彛鍔熻兘 P0DIR &= ~0x02; //灏哖0_1鐨勭鍙d紶杈撴柟寮忚缃负杈撳叆 P0INP &= ~0x02; //灏哖0_1鐨勭鍙h緭鍏ユ柟寮忚缃负锛氫笂鎷?涓嬫媺 P1INP &= ~0x04; //灏哖1_2鐨勭鍙h緭鍏ユ柟寮忚缃负锛氫笂鎷?涓嬫媺 P2INP &= ~0x60; //灏哖0绔彛鍜孭1绔彛寮曡剼璁剧疆涓猴細涓婃媺 LED5 = 0; //涓婄數鐨勬椂鍊欙紝LED5涓嶄寒 LED6 = 0; //涓婄數鐨勬椂鍊欙紝LED6涓嶄寒 }
2.涓诲嚱鏁颁腑鎵弿鎸夐敭绔彛鐨勫彉鍖栵紝榛樿鏄珮鐢靛钩銆傚綋鎸夐敭鎸変笅鐨勬椂鍊欙紝涓轰綆鐢靛钩銆傚綋鍑虹幇浣庣數骞崇殑鏃跺€欙紝闇€瑕佽繘琛屽幓鎶栫殑澶勭悊銆?/p>
銆€銆€<1>娌℃湁鎸夐敭涓嬫椂锛岀鍙g殑杈撳叆涓?strong>楂樼數骞?/strong>锛屽綋鍙戠幇璇ョ鍙f湁浣庣數骞?/strong>浜х敓鏃讹紝鍒欐湁鍙兘浼氭槸鎸夐敭鎸変笅锛岄渶瑕佺粡杩?strong>鍘绘姈鍔?/strong>澶勭悊锛屽鏋滆绔彛杩樻槸浣庣數骞筹紝鍒?strong>纭涓烘寜閿寜涓嬨€?/p>
<2>鍦ㄨ繘琛屾寜閿鐞嗘椂锛屽厛绛夊緟鎸夐敭鏉惧紑锛岀劧鍚庡啀灏嗙浉鍏崇殑LED杩涜寮€鍏崇姸鎬佺殑鍙栧弽鎺у埗銆?/p>
void main() { InitPort(); while(1) { ScanKeys(); } }
鏈€缁堜唬鐮佸睍绀?/p>
1 #include "ioCC2530.h" 2 3 #define LED5 P1_3 4 #define LED6 P1_4 5 #define SW1 P1_2 6 #define SW2 P0_1 7 /*===================寤舵椂鍑芥暟=========================*/ 8 void Delay(unsigned int t) 9 { 10 while(t--); 11 } 12 /*================绔彛鍒濆鍖栧嚱鏁?=====================*/ 13 void InitPort() //杈撳叆杈撳嚭闇€瑕佸崟鐙缃紝涓嶅彲鑳戒竴涓嬭缃畬鍏?/span> 14 { 15 P1SEL &= ~0x18; //灏哖1_3鍜孭1_4璁剧疆涓洪€氱敤I/O绔彛鍔熻兘 0001 1000->1110 0111 灏哖1_3鍜孭1_4璁剧疆鏅€欼O鍙?/span> 16 P1DIR |= 0x18; //灏哖1_3鍜孭1_4鐨勭鍙d紶杈撴柟寮忚缃负杈撳嚭 0001 1000 灏哖1_3鍜孭1_4璁剧疆涓鸿緭鍑烘ā寮?/span> 17 P1SEL &= ~0x04; //灏哖1_2璁剧疆涓洪€氱敤I/O绔彛鍔熻兘 0000 0100->1111 1011 灏哖1_2璁剧疆涓烘櫘閫欼O鍙?/span> 18 P1DIR &= ~0x04; //灏哖1_2鐨勭鍙d紶杈撴柟寮忚缃负杈撳叆 1111 1011 灏哖1_2鐨勮缃负杈撳叆妯″紡銆傝繕浼氭湁P1INP涓嶱2INP瀵勫瓨鍣ㄨ缃?/span> 19 P0SEL &= ~0x02; //灏哖0_1璁剧疆涓洪€氱敤I/O绔彛鍔熻兘 0000 0010 ->1111 1101 灏哖0_1璁剧疆涓烘櫘閫欼O鍙?/span> 20 P0DIR &= ~0x02; //灏哖0_1鐨勭鍙d紶杈撴柟寮忚缃负杈撳叆 1111 1101 灏哖0_1璁剧疆涓鸿緭鍏?/span> 21 P0INP &= ~0x02; //灏哖0_1鐨勭鍙h緭鍏ユ柟寮忚缃负锛氫笂鎷?涓嬫媺 0000 0010->1111 1101 鍏堣缃笂鎷夋垨鑰呬笅鎷夋ā寮?P0_1璁剧疆涓恒€?/span> 22 P1INP &= ~0x04; //灏哖1_2鐨勭鍙h緭鍏ユ柟寮忚缃负锛氫笂鎷?涓嬫媺 1111 1011 P1_2璁剧疆涓婃媺鎴栬€呬笅鎷夋ā寮?/span> 23 P2INP &= ~0x60; //灏哖0绔彛鍜孭1绔彛寮曡剼璁剧疆涓猴細涓婃媺 0110 0000 -> 1001 1111 鍙湅瀵勫瓨鍣?5涓?瀵瑰簲鐨凱0鍜孭1 0涓轰笂鎷夋ā寮?/span> 24 LED5 = 0; //涓婄數鐨勬椂鍊欙紝LED5涓嶄寒 25 LED6 = 0; //涓婄數鐨勬椂鍊欙紝LED6涓嶄寒 26 } 27 /*=================鎸夐敭鎵弿鍑芥暟=======================*/ 28 void ScanKeys() 29 { 30 if(SW1 == 0) 31 { //鍙戠幇SW1鏈変綆鐢靛钩淇″彿 32 Delay(100); //鎸夐敭鍘绘姈鍔? 寤舵椂鍘绘姈 33 if(SW1 == 0) 34 { //纭疄鏄湁鎸夐敭鍔ㄤ綔 35 while(SW1 == 0); //绛夊緟鎸夐敭1鏉惧紑 36 //灏哃ED5鐨勭伅鍏夊紑鍏崇姸鎬佸彇鍙?/span> 37 LED5 = ~LED5; 38 } 39 if(SW2 == 0) 40 { //鍙戠幇SW2鏈変綆鐢靛钩淇″彿 41 Delay(100); //鎸夐敭鍘绘姈鍔?/span> 42 if(SW2 == 0) 43 { //纭疄鏄湁鎸夐敭鍔ㄤ綔 44 while(SW2 == 0); //绛夊緟鎸夐敭2鏉惧紑 45 LED6 = ~LED6; 46 } 47 } 48 } 49 /*=====================涓诲嚱鏁?========================*/ 50 void main() 51 { 52 InitPort(); 53 while(1) 54 { 55 ScanKeys(); //鎸夐敭鎵弿鍑芥暟閲岄潰鍖呭惈浜嗙偣鐏?/span> 56 } 57 }
以上是关于CC2530鐨勯€氱敤IO鍙g殑杈撳叆杈撳嚭的主要内容,如果未能解决你的问题,请参考以下文章