User Tools

Site Tools


dev8051:code8051

Differences

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

Link to this comparison view

Next revision
Previous revision
dev8051:code8051 [2024/11/11 11:50] – created 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. |
-<file c test0.c>+<file c nmk322_test0.c>
 /*----------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
 #include "uart.h" #include "uart.h"
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 227: Line 393:
 </file> </file>
 ++++ ++++
 +
 +==== Code: RFID module and OLED ====
 +
 +++++ Testing RFID (FRC522) and OLED (ssd1306). |
 +<file c nmk322_test2rfid.c>
 +/*----------------------------------------------------------------------------*/
 +#include "uart_hexascii.h"
 +#include "oled_ssd1306.h"
 +#include "cstr_hexascii.h"
 +#include "frc522.h"
 +#define APPTITLE "NMK322 RFID/OLED"
 +/*----------------------------------------------------------------------------*/
 +void main(void) {
 + __xdata cstr_t buff; /* default: 64-bytes long */
 + unsigned char temp, stat, loop, size;
 + unsigned char pdat[FRC522_MAX_RXSIZE], reqa[2];
 + /** initialize */
 + cstr_init(&buff);
 + uart_init();
 + i2c_init();
 + oled1306_init();
 + spi_init();
 + atqa = reqa;
 + /* initialize mf contactless card reader */
 + /** say something... */
 + uart_puts("\n----------------\n");
 + uart_puts(APPTITLE);
 + uart_puts("\n----------------\n\n");
 + oled1306_puts(APPTITLE);
 + temp = frc522_init();
 + if (!temp||temp==0xff) {
 + uart_puts("** Cannot find FRC522 hardware! Aborting!\n");
 + oled1306_set_cursor(5,0);
 + oled1306_puts("** Hardware Error!");
 + hang();
 + }
 + uart_puts("FRC522 found. Firmware version is 0x");
 + uart_send_hexbyte(temp);
 + 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 */
 + while (1) {
 + stat = frc522_scan(pdat,&size);
 + if (stat==FRC522_OK) {
 + uart_puts("## TAG(");
 + uart_send_hexbyte(stat);
 + uart_puts("|");
 + uart_send_hexbyte(reqa[0]);
 + uart_puts(",");
 + uart_send_hexbyte(reqa[1]);
 + uart_puts("):");
 + cstr_null(&buff);
 + for (loop=0;loop<size-1;loop++) { /** UID is size-1 bytes */
 + uart_send('[');
 + uart_send_hexbyte(pdat[loop]);
 + cstr_append_hexbyte(&buff,pdat[loop]);
 + uart_send(']');
 + }
 + uart_send('\n');
 + oled1306_set_cursor(2,0);
 + oled1306_puts("#TAG: ");
 + oled1306_puts(buff.buff);
 + loop_delay(3000);
 + oled1306_clear_row(2);
 + }
 + else if (stat!=FRC522_ERROR_NO_TAG&&stat!=FRC522_ERROR_REQ_A) {
 + uart_puts("** Scan Failed (0x");
 + uart_send_hexbyte(stat);
 + uart_puts("):");
 + for (loop=0;loop<FRC522_MAX_RXSIZE;loop++) {
 + uart_send('[');
 + uart_send_hexbyte(pdat[loop]);
 + uart_send(']');
 + }
 + uart_send('\n');
 + oled1306_set_cursor(3,0);
 + oled1306_puts("*TAG: ERROR!");
 + loop_delay(3000);
 + oled1306_clear_row(3);
 + }
 + }
 +}
 +/*----------------------------------------------------------------------------*/
 +</file>
 +++++
 +
 +==== 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:
 /*----------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
 </file> </file>
-++++ 
  
 ===== Code: gtuc51 ===== ===== Code: gtuc51 =====
dev8051/code8051.1731297017.txt.gz · Last modified: by azman