/*----------------------------------------------------------------------------*/ /** * 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); } } } /*----------------------------------------------------------------------------*/