{"id":432,"date":"2025-11-27T14:40:15","date_gmt":"2025-11-27T06:40:15","guid":{"rendered":"https:\/\/www.axeuh.com\/?p=432"},"modified":"2025-12-05T16:28:16","modified_gmt":"2025-12-05T08:28:16","slug":"%e4%bb%a3%e7%a0%81","status":"publish","type":"post","link":"https:\/\/www.axeuh.com\/index.php\/2025\/11\/27\/%e4%bb%a3%e7%a0%81\/","title":{"rendered":"\u4ee3\u7801"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>#include \"iocc2530.h\"\n\n#define LED1 P1_0\n#define KEY1 P0_1\n#define KEY2 P0_2\n\n\/\/ \u521d\u59cb\u5316LED\nvoid intled()\n{\n  P1SEL &amp;= ~0x01;  \/\/ P1_0\u4f5c\u4e3aGPIO\n  P1DIR |= 0x01;   \/\/ P1_0\u8f93\u51fa\u65b9\u5411\n  LED1 = 0;        \/\/ \u521d\u59cb\u5173\u95edLED\n}\n\n\/\/ \u521d\u59cb\u5316\u6309\u952e1\nvoid intkey1()\n{\n  P0SEL &amp;= ~0x02;  \/\/ P0_1\u4f5c\u4e3aGPIO\n  P0DIR &amp;= ~0x02;  \/\/ P0_1\u8f93\u5165\u65b9\u5411\n}\n\n\/\/ \u521d\u59cb\u5316\u6309\u952e2\nvoid intkey2()\n{\n  P0SEL &amp;= ~0x04;  \/\/ P0_2\u4f5c\u4e3aGPIO\n  P0DIR &amp;= ~0x04;  \/\/ P0_2\u8f93\u5165\u65b9\u5411\n}\n\n\/\/ \u5ef6\u65f6\u51fd\u6570\nvoid delay(unsigned int ms)\n{\n  unsigned int i, j;\n  for(i = 0; i &lt; ms; i++)\n    for(j = 0; j &lt; 535; j++);\n}\n\n\/\/ \u521d\u59cb\u5316\u7cfb\u7edf\u65f6\u949f - \u4f7f\u7528\u5185\u90e816MHz RC\u632f\u8361\u5668\nvoid initClock()\n{\n  SLEEPCMD &amp;= ~0x04;       \/\/ \u5f00\u542f\u6676\u632f\n  while(!(SLEEPSTA &amp; 0x40)); \/\/ \u7b49\u5f85\u6676\u632f\u7a33\u5b9a\n  \n  CLKCONCMD = 0x80;        \/\/ 32MHz XOSC\n  while(CLKCONSTA &amp; 0x40); \/\/ \u7b49\u5f85\u65f6\u949f\u5207\u6362\u5b8c\u6210\n}\n\n\/\/ \u521d\u59cb\u5316UART - \u4f7f\u7528\u66f4\u53ef\u9760\u7684\u914d\u7f6e\nvoid initUART()\n{\n  \/\/ \u914d\u7f6eUART0\u4f7f\u7528\u5907\u7528\u4f4d\u7f6e1 (P0_2-&gt;RX, P0_3-&gt;TX)\n  PERCFG = 0x00;           \/\/ UART0\u5728\u4f4d\u7f6e0 (P0_2\u548cP0_3)\n  P0SEL |= 0x0C;           \/\/ P0_2\u548cP0_3\u7528\u4f5c\u5916\u8bbe\u529f\u80fd\n  \n  \/\/ \u914d\u7f6eUART\u53c2\u6570 - \u4f7f\u75289600\u6ce2\u7279\u7387\uff0c\u66f4\u7a33\u5b9a\n  U0CSR |= 0x80;           \/\/ UART\u6a21\u5f0f\n  \n  \/\/ 9600\u6ce2\u7279\u7387\u8ba1\u7b97: 32MHz\u7cfb\u7edf\u65f6\u949f\n  \/\/ \u516c\u5f0f: BAUD_M = 255 - (F\/(2^(BAUD_E-1)*8*BAUD_RATE))\n  \/\/ \u8fd9\u91cc\u4f7f\u7528\u9884\u8ba1\u7b97\u503c\n  U0GCR = 10;              \/\/ BAUD_E = 10\n  U0BAUD = 131;            \/\/ BAUD_M = 131, 9600\u6ce2\u7279\u7387\n  \n  \/\/ 115200\u6ce2\u7279\u7387\u914d\u7f6e\uff08\u5982\u679c9600\u4e0d\u884c\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u8fd9\u4e2a\uff09\n  \/\/ U0GCR = 11;           \/\/ BAUD_E = 11\n  \/\/ U0BAUD = 216;         \/\/ BAUD_M = 216, 115200\u6ce2\u7279\u7387\n  \n  UTX0IF = 0;              \/\/ \u6e05\u9664\u53d1\u9001\u4e2d\u65ad\u6807\u5fd7\n}\n\n\/\/ \u53d1\u9001\u5355\u4e2a\u5b57\u7b26\nvoid uartSendChar(char c)\n{\n  U0DBUF = c;              \/\/ \u5199\u5165\u53d1\u9001\u7f13\u51b2\u533a\n  while(!UTX0IF);          \/\/ \u7b49\u5f85\u53d1\u9001\u5b8c\u6210\n  UTX0IF = 0;              \/\/ \u6e05\u9664\u53d1\u9001\u5b8c\u6210\u6807\u5fd7\n  delay(1);                \/\/ \u77ed\u6682\u5ef6\u65f6\u786e\u4fdd\u5b57\u7b26\u53d1\u9001\u5b8c\u6210\n}\n\n\/\/ \u53d1\u9001\u5b57\u7b26\u4e32\nvoid uartSendString(char *str)\n{\n  while(*str)\n  {\n    uartSendChar(*str);\n    str++;\n  }\n}\n\nint main(void)\n{\n  \/\/ \u521d\u59cb\u5316\u5404\u6a21\u5757\n  intled();\n  intkey1();\n  intkey2();\n  initClock();\n  initUART();\n  \n  \/\/ \u53d1\u9001\u7b80\u5355\u7684\u6d4b\u8bd5\u5b57\u7b26\n  uartSendChar('S');\n  uartSendChar('T');\n  uartSendChar('A');\n  uartSendChar('R');\n  uartSendChar('T');\n  uartSendChar('\\r');\n  uartSendChar('\\n');\n  \n  while(1)\n  {\n    delay(10);  \/\/ \u57fa\u672c\u5ef6\u65f6\n    \n    \/\/ \u68c0\u6d4b\u6309\u952e1\n    if(KEY1 == 0)\n    {\n      delay(20); \/\/ \u53bb\u6296\n      if(KEY1 == 0)\n      {\n        uartSendChar('1'); \/\/ \u53d1\u9001\u5b57\u7b26'1'\n        LED1 = !LED1;      \/\/ \u5207\u6362LED\n        while(KEY1 == 0);  \/\/ \u7b49\u5f85\u91ca\u653e\n        delay(100);\n      }\n    }\n    \n    \/\/ \u68c0\u6d4b\u6309\u952e2\n    if(KEY2 == 0)\n    {\n      delay(20); \/\/ \u53bb\u6296\n      if(KEY2 == 0)\n      {\n        uartSendChar('2'); \/\/ \u53d1\u9001\u5b57\u7b26'2'\n        LED1 = !LED1;      \/\/ \u5207\u6362LED\n        while(KEY2 == 0);  \/\/ \u7b49\u5f85\u91ca\u653e\n        delay(100);\n      }\n    }\n  }\n  \n  return 0;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-432","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.axeuh.com\/index.php\/wp-json\/wp\/v2\/posts\/432","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.axeuh.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.axeuh.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.axeuh.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.axeuh.com\/index.php\/wp-json\/wp\/v2\/comments?post=432"}],"version-history":[{"count":4,"href":"https:\/\/www.axeuh.com\/index.php\/wp-json\/wp\/v2\/posts\/432\/revisions"}],"predecessor-version":[{"id":536,"href":"https:\/\/www.axeuh.com\/index.php\/wp-json\/wp\/v2\/posts\/432\/revisions\/536"}],"wp:attachment":[{"href":"https:\/\/www.axeuh.com\/index.php\/wp-json\/wp\/v2\/media?parent=432"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.axeuh.com\/index.php\/wp-json\/wp\/v2\/categories?post=432"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.axeuh.com\/index.php\/wp-json\/wp\/v2\/tags?post=432"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}