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>

鎶€鏈浘鐗? src=

 

 

鎿嶄綔寰帶鍒跺櫒鐨勬湰璐?/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;鎿嶄綔鐨勭壒鐐规槸锛岃浣嶆湁0缁撴灉灏变负0锛岃嫢涓?鍒欎繚瀛樺師鏉ュ€间笉鍙樸€?/p>

 

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銆傛湰棰樹腑鐨勫紩鑴氬浘

 

鎶€鏈浘鐗? src=

 

 

娑夊強鍒扮殑瀵勫瓨鍣?/p>

绔彛鍔熻兘閫夋嫨瀵勫瓨鍣ㄣ€?/p>

鎶€鏈浘鐗? src=

 

 

绔彛浼犺緭鏂瑰悜璁剧疆瀵勫瓨鍣ㄣ€?/p>

鎶€鏈浘鐗? src=

 

 

杈撳叆绔彛闇€瑕佽缃叾杈撳叆鏂瑰紡鐨勫瘎瀛樺櫒

绔彛鑳藉鎻愪緵“涓婃媺”銆?ldquo;涓嬫媺”鍜?ldquo;涓夋€?rdquo;涓夌杈撳叆妯″紡

鎶€鏈浘鐗? src=

 

 鎶€鏈浘鐗? src=

 

 

渚嬪瓙锛氬皢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>

鎶€鏈浘鐗? src=

 

 

璁捐鎬濊矾锛?/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>

 

鎶€鏈浘鐗? id=
 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 }
View Code

 

以上是关于CC2530鐨勯€氱敤IO鍙g殑杈撳叆杈撳嚭的主要内容,如果未能解决你的问题,请参考以下文章

瑙嗛杈撳嚭 鑼冧緥

杈撳叆浼樺寲妯℃澘

鐢ㄦ埛杈撳叆鍜寃hile寰幆

Python瀛︿範涔嬭緭鍏ュ拰杈撳嚭