dev8051:code8051
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| dev8051:code8051 [2024/11/12 10:29] – [Code: relay] 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. | | ||
| 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 228: | Line 394: | ||
| ++++ | ++++ | ||
| - | ++++ Testing Base Kit (202425s1). | | + | ==== Code: RFID module and OLED ==== |
| - | <file c nmk322_basetest.c> | + | |
| - | / | + | |
| - | /* override default tick values */ | + | |
| - | #define TIMER_TICK_LEN TIMER_VAL50MS | + | |
| - | #define TIMER_TICK_CNT TIMER_LOOP_1S | + | |
| - | / | + | |
| - | #include " | + | |
| - | #include " | + | |
| - | #include " | + | |
| - | / | + | |
| - | void main(void) { | + | |
| - | unsigned char loop, mask, seg7[] | + | |
| - | P1MODE1(); // better pullup current | + | |
| - | loop = 0; mask = (!P2_7)? | + | |
| - | P1 = seg7[loop] ^ mask; | + | |
| - | timer_init(); | + | |
| - | timer_tick_exec(); | + | |
| - | while (1) { | + | |
| - | P2_0 | + | |
| - | P2_1 | + | |
| - | P2_2 = !P3_5; | + | |
| - | P2_3 = !P3_4; | + | |
| - | if (timer_ticked()) { | + | |
| - | loop++; if (loop==10) loop = 0; | + | |
| - | mask = (!P2_7)? | + | |
| - | P1 = seg7[loop] ^ mask; | + | |
| - | timer_tick00(); | + | |
| - | } | + | |
| - | } | + | |
| - | } | + | |
| - | / | + | |
| - | </ | + | |
| - | ++++ | + | |
| ++++ Testing RFID (FRC522) and OLED (ssd1306). | | ++++ Testing RFID (FRC522) and OLED (ssd1306). | | ||
| - | <file c nmk322_test2.c> | + | <file c nmk322_test2rfid.c> |
| / | / | ||
| #include " | #include " | ||
| Line 292: | Line 425: | ||
| if (!temp||temp==0xff) { | if (!temp||temp==0xff) { | ||
| uart_puts(" | uart_puts(" | ||
| + | oled1306_set_cursor(5, | ||
| + | oled1306_puts(" | ||
| hang(); | hang(); | ||
| } | } | ||
| Line 297: | Line 432: | ||
| uart_send_hexbyte(temp); | uart_send_hexbyte(temp); | ||
| uart_puts(" | uart_puts(" | ||
| + | cstr_null(& | ||
| + | cstr_append(& | ||
| + | cstr_append_hexbyte(& | ||
| + | oled1306_set_cursor(5, | ||
| + | oled1306_puts(buff.buff); | ||
| /** main loop */ | /** main loop */ | ||
| while (1) { | while (1) { | ||
| Line 336: | Line 476: | ||
| loop_delay(3000); | loop_delay(3000); | ||
| oled1306_clear_row(3); | oled1306_clear_row(3); | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | / | ||
| + | </ | ||
| + | ++++ | ||
| + | |||
| + | ==== Code: Testing Base Kit ==== | ||
| + | |||
| + | ++++ Testing Base Kit (202425s1). | | ||
| + | <file c nmk322_basetest.c> | ||
| + | / | ||
| + | /* override default tick values */ | ||
| + | #define TIMER_TICK_LEN TIMER_VAL50MS | ||
| + | #define TIMER_TICK_CNT TIMER_LOOP_1S | ||
| + | / | ||
| + | #include " | ||
| + | #include " | ||
| + | #include " | ||
| + | / | ||
| + | void main(void) { | ||
| + | unsigned char loop, mask, seg7[] = { _7SEGNUM_CC_ }; | ||
| + | P1MODE1(); // better pullup current | ||
| + | loop = 0; mask = (!P2_7)? | ||
| + | P1 = seg7[loop] ^ mask; | ||
| + | timer_init(); | ||
| + | timer_tick_exec(); | ||
| + | while (1) { | ||
| + | P2_0 = !P3_7; | ||
| + | P2_1 = !P3_6; | ||
| + | P2_2 = !P3_5; | ||
| + | P2_3 = !P3_4; | ||
| + | if (timer_ticked()) { | ||
| + | loop++; if (loop==10) loop = 0; | ||
| + | mask = (!P2_7)? | ||
| + | P1 = seg7[loop] ^ mask; | ||
| + | timer_tick00(); | ||
| } | } | ||
| } | } | ||
dev8051/code8051.1731378549.txt.gz · Last modified: by azman
