User Tools

Site Tools


dev8051:code8051

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
dev8051:code8051 [2024/11/12 10:29] – [Code: relay] azmandev8051: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(" cm\r\n");  uart_puts(" cm\r\n");
  for (loop=65000;loop;loop--);  for (loop=65000;loop;loop--);
 + }
 + }
 +}
 +/*----------------------------------------------------------------------------*/
 +</file>
 +++++
 +
 +==== 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:PWM)(2|red:vcc)(3|brown:ground)
 +**/
 +/*----------------------------------------------------------------------------*/
 +#define APPTITLE "NMK322 BT/SERVO"
 +#define BTNAME "nmk322bt"
 +#define BTPASS "0000"
 +#include "hc06.h"
 +#include "hc06_conf.h"
 +#include "uart.h"
 +#include "uart_hexascii.h"
 +#include "timer.h"
 +#include "utils.h"
 +#include "oled_ssd1306.h"
 +#include "cstr_hexascii.h"
 +/*----------------------------------------------------------------------------*/
 +#define OLED_ROW_DATA 2
 +#define OLED_ROW_INFO1 4
 +#define OLED_ROW_INFO2 6
 +/*----------------------------------------------------------------------------*/
 +#define SERVO_PIN P1_0
 +#include "servo.h"
 +/*----------------------------------------------------------------------------*/
 +_sbit(GO000,PIN15);
 +_sbit(GO1MS,PIN16);
 +_sbit(GO2MS,PIN17);
 +/*----------------------------------------------------------------------------*/
 +#define BUFFSIZE 128
 +/*----------------------------------------------------------------------------*/
 +__xdata char buff[BUFFSIZE];
 +/*----------------------------------------------------------------------------*/
 +void uart_show_buff(void) {
 + char* pchk = buff;
 + while (*pchk) {
 + if (pchk[0]<0x20||pchk[0]>0x7e) {
 + uart_puts("[0x");
 + uart_send_hexbyte(pchk[0]);
 + uart_send(']');
 + }
 + else uart_send(pchk[0]);
 + pchk++;
 + }
 + uart_send('\n');
 +}
 +/*----------------------------------------------------------------------------*/
 +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("\n---------------\n");
 + uart_puts(APPTITLE);
 + uart_puts("\n---------------\n\n");
 + hc06_init();
 + curr = 3;
 + do {
 + uart_puts("-- Looking for HC06... ");
 + oled1306_set_cursor(OLED_ROW_INFO1,0);
 + oled1306_puts("Finding HC06... ");
 + hc06_find();
 + if (hc06_wait_ok()==HC06_OK) {
 + uart_puts("OK.\r\n");
 + oled1306_puts("OK");
 + oled1306_set_cursor(OLED_ROW_INFO2,0);
 + oled1306_puts("Setting up HC06... ");
 + uart_puts("\r\nSet default name & pin\r\n");
 + uart_puts("-- Set Name (");
 + uart_puts(BTNAME);
 + uart_puts("): ");
 + hc06_setname(buff,BUFFSIZE);
 + uart_puts(buff);
 + uart_puts("\r\n");
 + timer_delay1s(test,1);
 + /* set pass */
 + uart_puts("-- Set Pin (");
 + uart_puts(BTPASS);
 + uart_puts("): ");
 + hc06_setpin(buff,BUFFSIZE);
 + uart_puts(buff);
 + uart_puts("\r\n");
 + timer_delay1s(test,1);
 + oled1306_puts("OK");
 + timer_delay1s(test,1);
 + oled1306_clear_row(OLED_ROW_INFO1);
 + oled1306_clear_row(OLED_ROW_INFO2);
 + break;
 + }
 + uart_puts("timeout.\n");
 + timer_delay1s(test,1);
 + } while (--curr);
 + while (1) {
 + if (hc06_peek()) {
 + if ((test=hc06_wait(buff,BUFFSIZE))>1) {
 + if (buff[0]=='#') {
 + if (test==BUFFSIZE) test--;
 + buff[test] = 0x0;
 + while (buff[test-1]=='\r'||buff[test-1]=='\n') {
 + test--;
 + buff[test] = 0x0;
 + }
 + curr = (unsigned char) str2uint(&buff[1]);
 + if (curr>=5&&curr<=25) {
 + uart_puts("## Turning to (");
 + uart_puts(&buff[1]);
 + uart_puts(")\r\n");
 + oled1306_set_cursor(OLED_ROW_DATA,0);
 + oled1306_puts("ServoTurn: ");
 + oled1306_puts(&buff[1]);
 + servo_turn(curr);
 + timer_delay1s(test,1);
 + oled1306_clear_row(OLED_ROW_DATA);
 + }
 + else {
 + uart_puts("** Invalid angle (");
 + uart_puts(&buff[1]);
 + uart_puts(")\r\n");
 + }
 + }
 + else {
 + uart_puts(">> ");
 + uart_puts(buff);
 + uart_puts("\r\n");
 + }
 + }
 + }
 + if (GO000==0) {
 + oled1306_set_cursor(OLED_ROW_DATA,0);
 + oled1306_puts("ServoTurn CENTER");
 + servo_turn(15);
 + while (!GO000);
 + oled1306_clear_row(OLED_ROW_DATA);
 + }
 + if (GO1MS==0) {
 + oled1306_set_cursor(OLED_ROW_DATA,0);
 + oled1306_puts("ServoTurn PWM1ms");
 + servo_turn(10); // 1ms pwm
 + while (!GO1MS);
 + oled1306_clear_row(OLED_ROW_DATA);
 + }
 + if (GO2MS==0) {
 + oled1306_set_cursor(OLED_ROW_DATA,0);
 + oled1306_puts("ServoTurn PWM2ms");
 + servo_turn(20); // 2ms pwm
 + while (!GO2MS);
 + oled1306_clear_row(OLED_ROW_DATA);
  }  }
  }  }
Line 228: Line 394:
 ++++ ++++
  
-++++ Testing Base Kit (202425s1). | +==== CodeRFID 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 "my1stc51.h" +
-#include "timer_tick.h" +
-#include "led7seg.h" +
-/*----------------------------------------------------------------------------*/ +
-void main(void) { +
- unsigned char loop, mask, seg7[] { _7SEGNUM_CC_ }; +
- P1MODE1(); // better pullup current +
- loop 0; mask (!P2_7)?0xff:0x00; +
- 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)?0xff:0x00; +
- P1 seg7[loop] ^ mask; +
- timer_tick00(); +
-+
-+
-+
-/*----------------------------------------------------------------------------*/ +
-</file> +
-+++++
  
 ++++ Testing RFID (FRC522) and OLED (ssd1306). | ++++ Testing RFID (FRC522) and OLED (ssd1306). |
-<file c nmk322_test2.c>+<file c nmk322_test2rfid.c>
 /*----------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
 #include "uart_hexascii.h" #include "uart_hexascii.h"
Line 292: Line 425:
  if (!temp||temp==0xff) {  if (!temp||temp==0xff) {
  uart_puts("** Cannot find FRC522 hardware! Aborting!\n");  uart_puts("** Cannot find FRC522 hardware! Aborting!\n");
 + oled1306_set_cursor(5,0);
 + oled1306_puts("** Hardware Error!");
  hang();  hang();
  }  }
Line 297: Line 432:
  uart_send_hexbyte(temp);  uart_send_hexbyte(temp);
  uart_puts(".\n");  uart_puts(".\n");
 + cstr_null(&buff);
 + cstr_append(&buff,"FW Vers: 0x");
 + cstr_append_hexbyte(&buff,temp);
 + oled1306_set_cursor(5,0);
 + 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);
 + }
 + }
 +}
 +/*----------------------------------------------------------------------------*/
 +</file>
 +++++
 +
 +==== 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 "my1stc51.h"
 +#include "timer_tick.h"
 +#include "led7seg.h"
 +/*----------------------------------------------------------------------------*/
 +void main(void) {
 + unsigned char loop, mask, seg7[] = { _7SEGNUM_CC_ };
 + P1MODE1(); // better pullup current
 + loop = 0; mask = (!P2_7)?0xff:0x00;
 + 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)?0xff:0x00;
 + P1 = seg7[loop] ^ mask;
 + timer_tick00();
  }  }
  }  }
dev8051/code8051.1731378549.txt.gz · Last modified: by azman