dev8051:code8051
Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| dev8051:code8051 [2024/11/11 11:50] – created azman | dev8051:code8051 [2026/01/21 00:05] (current) – azman | ||
|---|---|---|---|
| Line 4: | Line 4: | ||
| ===== Codes for NMK322 ===== | ===== Codes for NMK322 ===== | ||
| + | |||
| + | Codes to test hardware modules available for our NMK322 Microcontroller lab. | ||
| + | |||
| + | ==== Code: IR module and Ultrasonic sensor module ==== | ||
| ++++ Testing IR module and HC-SR04 ultrasonic sensor module. | | ++++ Testing IR module and HC-SR04 ultrasonic sensor module. | | ||
| - | <file c test0.c> | + | <file c nmk322_test0.c> |
| / | / | ||
| #include " | #include " | ||
| Line 94: | Line 98: | ||
| uart_puts(" | uart_puts(" | ||
| for (loop=65000; | for (loop=65000; | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | / | ||
| + | </ | ||
| + | ++++ | ||
| + | |||
| + | ==== Code: BT module and servo ==== | ||
| + | |||
| + | ++++ Testing HC06 bluetooth module , TowerPro MG996R servo. | | ||
| + | <file c nmk322_test1btservo.c> | ||
| + | / | ||
| + | /** | ||
| + | * Testing HC06 bluetooth module , TowerPro MG996R servo | ||
| + | * - note: servo pin (1|orange: | ||
| + | **/ | ||
| + | / | ||
| + | #define APPTITLE " | ||
| + | #define BTNAME " | ||
| + | #define BTPASS " | ||
| + | #include " | ||
| + | #include " | ||
| + | #include " | ||
| + | #include " | ||
| + | #include " | ||
| + | #include " | ||
| + | #include " | ||
| + | #include " | ||
| + | / | ||
| + | #define OLED_ROW_DATA 2 | ||
| + | #define OLED_ROW_INFO1 4 | ||
| + | #define OLED_ROW_INFO2 6 | ||
| + | / | ||
| + | #define SERVO_PIN P1_0 | ||
| + | #include " | ||
| + | / | ||
| + | _sbit(GO000, | ||
| + | _sbit(GO1MS, | ||
| + | _sbit(GO2MS, | ||
| + | / | ||
| + | #define BUFFSIZE 128 | ||
| + | / | ||
| + | __xdata char buff[BUFFSIZE]; | ||
| + | / | ||
| + | void uart_show_buff(void) { | ||
| + | char* pchk = buff; | ||
| + | while (*pchk) { | ||
| + | if (pchk[0]< | ||
| + | uart_puts(" | ||
| + | uart_send_hexbyte(pchk[0]); | ||
| + | uart_send(' | ||
| + | } | ||
| + | else uart_send(pchk[0]); | ||
| + | pchk++; | ||
| + | } | ||
| + | uart_send(' | ||
| + | } | ||
| + | / | ||
| + | void main(void) { | ||
| + | unsigned char curr, test; | ||
| + | GO000 = 1; GO1MS = 1; GO2MS = 1; | ||
| + | timer_init(); | ||
| + | servo_init(); | ||
| + | i2c_init(); | ||
| + | oled1306_init(); | ||
| + | oled1306_puts(APPTITLE); | ||
| + | uart_init(); | ||
| + | uart_puts(" | ||
| + | uart_puts(APPTITLE); | ||
| + | uart_puts(" | ||
| + | hc06_init(); | ||
| + | curr = 3; | ||
| + | do { | ||
| + | uart_puts(" | ||
| + | oled1306_set_cursor(OLED_ROW_INFO1, | ||
| + | oled1306_puts(" | ||
| + | hc06_find(); | ||
| + | if (hc06_wait_ok()==HC06_OK) { | ||
| + | uart_puts(" | ||
| + | oled1306_puts(" | ||
| + | oled1306_set_cursor(OLED_ROW_INFO2, | ||
| + | oled1306_puts(" | ||
| + | uart_puts(" | ||
| + | uart_puts(" | ||
| + | uart_puts(BTNAME); | ||
| + | uart_puts(" | ||
| + | hc06_setname(buff, | ||
| + | uart_puts(buff); | ||
| + | uart_puts(" | ||
| + | timer_delay1s(test, | ||
| + | /* set pass */ | ||
| + | uart_puts(" | ||
| + | uart_puts(BTPASS); | ||
| + | uart_puts(" | ||
| + | hc06_setpin(buff, | ||
| + | uart_puts(buff); | ||
| + | uart_puts(" | ||
| + | timer_delay1s(test, | ||
| + | oled1306_puts(" | ||
| + | timer_delay1s(test, | ||
| + | oled1306_clear_row(OLED_ROW_INFO1); | ||
| + | oled1306_clear_row(OLED_ROW_INFO2); | ||
| + | break; | ||
| + | } | ||
| + | uart_puts(" | ||
| + | timer_delay1s(test, | ||
| + | } while (--curr); | ||
| + | while (1) { | ||
| + | if (hc06_peek()) { | ||
| + | if ((test=hc06_wait(buff, | ||
| + | if (buff[0]=='#' | ||
| + | if (test==BUFFSIZE) test--; | ||
| + | buff[test] = 0x0; | ||
| + | while (buff[test-1]==' | ||
| + | test--; | ||
| + | buff[test] = 0x0; | ||
| + | } | ||
| + | curr = (unsigned char) str2uint(& | ||
| + | if (curr> | ||
| + | uart_puts("## | ||
| + | uart_puts(& | ||
| + | uart_puts(" | ||
| + | oled1306_set_cursor(OLED_ROW_DATA, | ||
| + | oled1306_puts(" | ||
| + | oled1306_puts(& | ||
| + | servo_turn(curr); | ||
| + | timer_delay1s(test, | ||
| + | oled1306_clear_row(OLED_ROW_DATA); | ||
| + | } | ||
| + | else { | ||
| + | uart_puts(" | ||
| + | uart_puts(& | ||
| + | uart_puts(" | ||
| + | } | ||
| + | } | ||
| + | else { | ||
| + | uart_puts(">> | ||
| + | uart_puts(buff); | ||
| + | uart_puts(" | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | if (GO000==0) { | ||
| + | oled1306_set_cursor(OLED_ROW_DATA, | ||
| + | oled1306_puts(" | ||
| + | servo_turn(15); | ||
| + | while (!GO000); | ||
| + | oled1306_clear_row(OLED_ROW_DATA); | ||
| + | } | ||
| + | if (GO1MS==0) { | ||
| + | oled1306_set_cursor(OLED_ROW_DATA, | ||
| + | oled1306_puts(" | ||
| + | servo_turn(10); | ||
| + | while (!GO1MS); | ||
| + | oled1306_clear_row(OLED_ROW_DATA); | ||
| + | } | ||
| + | if (GO2MS==0) { | ||
| + | oled1306_set_cursor(OLED_ROW_DATA, | ||
| + | oled1306_puts(" | ||
| + | servo_turn(20); | ||
| + | while (!GO2MS); | ||
| + | oled1306_clear_row(OLED_ROW_DATA); | ||
| } | } | ||
| } | } | ||
| Line 227: | Line 393: | ||
| </ | </ | ||
| ++++ | ++++ | ||
| + | |||
| + | ==== Code: RFID module and OLED ==== | ||
| + | |||
| + | ++++ Testing RFID (FRC522) and OLED (ssd1306). | | ||
| + | <file c nmk322_test2rfid.c> | ||
| + | / | ||
| + | #include " | ||
| + | #include " | ||
| + | #include " | ||
| + | #include " | ||
| + | #define APPTITLE " | ||
| + | / | ||
| + | void main(void) { | ||
| + | __xdata cstr_t buff; /* default: 64-bytes long */ | ||
| + | unsigned char temp, stat, loop, size; | ||
| + | unsigned char pdat[FRC522_MAX_RXSIZE], | ||
| + | /** initialize */ | ||
| + | cstr_init(& | ||
| + | uart_init(); | ||
| + | i2c_init(); | ||
| + | oled1306_init(); | ||
| + | spi_init(); | ||
| + | atqa = reqa; | ||
| + | /* initialize mf contactless card reader */ | ||
| + | /** say something... */ | ||
| + | uart_puts(" | ||
| + | uart_puts(APPTITLE); | ||
| + | uart_puts(" | ||
| + | oled1306_puts(APPTITLE); | ||
| + | temp = frc522_init(); | ||
| + | if (!temp||temp==0xff) { | ||
| + | uart_puts(" | ||
| + | oled1306_set_cursor(5, | ||
| + | oled1306_puts(" | ||
| + | hang(); | ||
| + | } | ||
| + | uart_puts(" | ||
| + | uart_send_hexbyte(temp); | ||
| + | uart_puts(" | ||
| + | cstr_null(& | ||
| + | cstr_append(& | ||
| + | cstr_append_hexbyte(& | ||
| + | oled1306_set_cursor(5, | ||
| + | oled1306_puts(buff.buff); | ||
| + | /** main loop */ | ||
| + | while (1) { | ||
| + | stat = frc522_scan(pdat,& | ||
| + | if (stat==FRC522_OK) { | ||
| + | uart_puts("## | ||
| + | uart_send_hexbyte(stat); | ||
| + | uart_puts(" | ||
| + | uart_send_hexbyte(reqa[0]); | ||
| + | uart_puts("," | ||
| + | uart_send_hexbyte(reqa[1]); | ||
| + | uart_puts(" | ||
| + | cstr_null(& | ||
| + | for (loop=0; | ||
| + | uart_send(' | ||
| + | uart_send_hexbyte(pdat[loop]); | ||
| + | cstr_append_hexbyte(& | ||
| + | uart_send(' | ||
| + | } | ||
| + | uart_send(' | ||
| + | oled1306_set_cursor(2, | ||
| + | oled1306_puts("# | ||
| + | oled1306_puts(buff.buff); | ||
| + | loop_delay(3000); | ||
| + | oled1306_clear_row(2); | ||
| + | } | ||
| + | else if (stat!=FRC522_ERROR_NO_TAG&& | ||
| + | uart_puts(" | ||
| + | uart_send_hexbyte(stat); | ||
| + | uart_puts(" | ||
| + | for (loop=0; | ||
| + | uart_send(' | ||
| + | uart_send_hexbyte(pdat[loop]); | ||
| + | uart_send(' | ||
| + | } | ||
| + | uart_send(' | ||
| + | oled1306_set_cursor(3, | ||
| + | oled1306_puts(" | ||
| + | loop_delay(3000); | ||
| + | oled1306_clear_row(3); | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | / | ||
| + | </ | ||
| + | ++++ | ||
| + | |||
| + | ==== Code: Testing Base Kit ==== | ||
| ++++ Testing Base Kit (202425s1). | | ++++ Testing Base Kit (202425s1). | | ||
| - | <file c basetest.c> | + | <file c nmk322_basetest.c> |
| / | / | ||
| /* override default tick values */ | /* override default tick values */ | ||
| Line 265: | Line 522: | ||
| ===== Code: relay ===== | ===== Code: relay ===== | ||
| - | ++++ Testing basic relay. | + | Testing basic relay. |
| <file c relay.c> | <file c relay.c> | ||
| / | / | ||
| Line 295: | Line 552: | ||
| / | / | ||
| </ | </ | ||
| - | ++++ | ||
| ===== Code: gtuc51 ===== | ===== Code: gtuc51 ===== | ||
dev8051/code8051.1731297017.txt.gz · Last modified: by azman
