PCの仮想COMポート経由のコマンド文字列によりUSB電源制御を可能にしてみた。
Microchip PIC16F1454 でPC電源連動型USB-HUBを作ってみた。
Windows … COM??
Linux … /dev/ttyACM?
WindowsもLinuxも古いバージョンのものは駄目だが比較的新しいバージョンのOSであれば標準ドライバーでアクセスできるはず。たぶん。
2018-07-01
USB電源端子をPWM駆動する機能を追加。ブザー、LED、ヒーター、ファン制御等に使えて便利です。って、確か、これってUSB-HUBだったような気もするが...もはや、何でもあり? (/・ω・)/
2018-06-29
予想通りバグバグしてたのでかなりの部分を修正。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 |
/******************************************************************************* Copyright 2016 Microchip Technology Inc. (www.microchip.com) Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. To request to license the code under the MLA license (www.microchip.com/mla_license), please contact mla_licensing@microchip.com *******************************************************************************/ /** INCLUDES *******************************************************/ #include <stdlib.h> #include <string.h> #include "system.h" #include "usb.h" #include "usb_device.h" #include "usb_device_cdc.h" #include "tmr0.h" #include "pwm.h" #define PORTBIT(r, f, x, n) r##x##bits.##f##x##n #define BOARD_LED(r) PORTBIT(r, r, C, 3) #define POWER_OUT(r) PORTBIT(r, r, A, 5) #define POWER_PIN() PORTBIT(PORT, R, A, 5) #define PWM_OUT PWM_RA5 const char NEWLINE[] = "\r\n"; const char BACKSPC[] = "\b \b"; const char HELP[] = "HELP"; const char ECHO[] = "ECHO"; const char POWER[] = "POWER"; const char AUTO[] = "AUTO"; const char ON[] = "ON"; const char OFF[] = "OFF"; const char PWM[] = "PWM"; const char CLOCK[] = "CLOCK"; const char DUTY[] = "DUTY"; bool echomode = false; bool automode = true; bool usbpower = false; uint32_t pwmclk = 1000; // Hz uint8_t pwmduty = 100; // % void echoback_char(char c) { putUSBUSART((uint8_t *)&c, 1); do CDCTxService(); while (!USBUSARTIsTxTrfReady()); } void echoback_str(const char *str) { putsUSBUSART((uint8_t *)str); do CDCTxService(); while (!USBUSARTIsTxTrfReady()); } void help() { echoback_str(NEWLINE); echoback_str("USB Power Controller, Version 1.00"); echoback_str(NEWLINE); echoback_str("Copyright 2018 All Rights Reserved."); echoback_str(NEWLINE); echoback_str(NEWLINE); echoback_str("Command List:"); echoback_str(NEWLINE); echoback_str(NEWLINE); echoback_str(" POWER [ON|OFF|AUTO] ... USB Power Control (default AUTO)"); echoback_str(NEWLINE); echoback_str(" PWM [{CLOCK|DUTY} [n]] ... PWM Control (default CLOCK=1KHz/DUTY=100%)"); echoback_str(NEWLINE); echoback_str(" ECHO [ON|OFF] ... Echoback Control (default OFF)"); echoback_str(NEWLINE); echoback_str(" HELP ... this help"); echoback_str(NEWLINE); echoback_str(NEWLINE); } void error(const char *str) { if (*str && echomode) { echoback_str(NEWLINE); echoback_str("Invalid Token: "); echoback_str(str); echoback_str(NEWLINE); echoback_str(NEWLINE); } } void status(bool b) { echoback_str(b ? ON : OFF); echoback_str(NEWLINE); } void integer(uint32_t val) { static char buf[16]; ultoa(buf, val, 10); echoback_str(buf); echoback_str(NEWLINE); } void command(uint8_t *str) { uint8_t i, prmcnt, *params[4]; bool spskip; // // parse command // spskip = true; prmcnt = 0; for (i = 0; str[i]; ++i) { if (spskip) { if (str[i] > ' ') { if (prmcnt < sizeof(params)/sizeof(params[0])) params[prmcnt++] = &str[i]; spskip = false; } } else { if (str[i] == ' ') { str[i] = 0; spskip = true; } } } // // control // if (prmcnt == 0) ; else if (stricmp((char *)params[0], POWER) == 0) { if (prmcnt < 2) status(!POWER_PIN()); else if (stricmp((char *)params[1], ON) == 0) { automode = false; POWER_OUT(TRIS) = false; } else if (stricmp((char *)params[1], OFF) == 0) { automode = false; POWER_OUT(TRIS) = true; } else if (stricmp((char *)params[1], AUTO) == 0) { automode = true; POWER_OUT(TRIS) = !usbpower; } else error((char *)params[1]); } else if (stricmp((char *)params[0], PWM) == 0) { if (prmcnt < 2) { integer(pwmclk); integer(pwmduty); } else if (stricmp((char *)params[1], CLOCK) == 0) { if (prmcnt < 3) integer(pwmclk); else { pwm_clock(pwmclk = atol(params[2])); pwm_duty(PWM_OUT, pwmduty); } } else if (stricmp((char *)params[1], DUTY) == 0) { if (prmcnt < 3) integer(pwmduty); else pwm_duty(PWM_OUT, pwmduty = atol(params[2])); } else error((char *)params[1]); } else if (stricmp((char *)params[0], ECHO) == 0) { if (prmcnt < 2) status(echomode); else if (stricmp((char *)params[1], ON) == 0) echomode = true; else if (stricmp((char *)params[1], OFF) == 0) echomode = false; else error((char *)params[1]); } else if (stricmp((char *)params[0], HELP) == 0) help(); else error((char *)params[0]); } void terminal() { static uint8_t len = 0; static uint8_t pos = 0; static uint8_t buf[32]; // // check buffer overflow // if (len >= sizeof(buf)) pos = len = sizeof(buf) - 1; // // read command // len += getsUSBUSART(&buf[len], sizeof(buf) - len); // // simple terminal control // while (pos < len) { switch (buf[pos]) { // // back space // case '\b': if (pos == 0) memcpy(buf, &buf[1], --len); else { if (echomode) echoback_str(BACKSPC); --pos; memcpy(&buf[pos], &buf[pos + 2], (len -= 2) - pos); } break; // // carriage return // case '\r': if (echomode) echoback_str(NEWLINE); buf[pos++] = 0; command(buf); memcpy(buf, &buf[pos], len -= pos); pos = 0; break; // // character // default: if (buf[pos] < ' ') buf[pos] = '?'; if (echomode) echoback_char(buf[pos]); ++pos; break; } } } /******************************************************************** * Function: void main(void) * * PreCondition: None * * Input: None * * Output: None * * Side Effects: None * * Overview: Main program entry point. * * Note: None *******************************************************************/ MAIN_RETURN main(void) { uint32_t led, usb = 0; SYSTEM_Initialize(SYSTEM_STATE_USB_START); // // Init USB // USBDeviceInit(); USBDeviceAttach(); // // Init PWM // pwm_init(PWM_OUT, 1); // pwm output pwm_clock(pwmclk); // pwm clock pwm_duty(PWM_OUT, pwmduty); // pwm duty cycle // // Init I/O Port // POWER_OUT(TRIS) = 1; // PWR OUTPUT BOARD_LED(TRIS) = 0; // LED OUTPUT // // Init TMR0 // tmr0_init(); // // Read TMR0 // led = tmr0_read(); // // メイン・ループ // while(1) { // // Read TMR0 // uint32_t now = tmr0_read(); // // Check USB State // bool active = !USBIsDeviceSuspended() && (USBGetDeviceState() == CONFIGURED_STATE); if (usbpower != active) { usbpower = active; // // Power Control // if (automode) POWER_OUT(TRIS) = !usbpower; // // Detach USB // if (!usbpower) { usb = now; USBDeviceDetach(); } } // // Attach USB // else if (usb) { if (now - usb >= TMR0_MS2TICK(5000)) { usb = 0; USBDeviceAttach(); } } // // Blink LED // if (now - led >= TMR0_MS2TICK(1000)) { led += TMR0_MS2TICK(1000); if(active) BOARD_LED(LAT) ^= 1; } else if (!active) { BOARD_LED(LAT) = (now - led >= TMR0_MS2TICK(10)); } // // Simple Terminal // terminal(); SYSTEM_Tasks(); #if defined(USB_POLLING) // Interrupt or polling method. If using polling, must call // this function periodically. This function will take care // of processing and responding to SETUP transactions // (such as during the enumeration process when you first // plug in). USB hosts require that USB devices should accept // and process SETUP packets in a timely fashion. Therefore, // when using polling, this function should be called // regularly (such as once every 1.8ms or faster** [see // inline code comments in usb_device.c for explanation when // "or faster" applies]) In most cases, the USBDeviceTasks() // function does not take very long to execute (ex: <100 // instruction cycles) before it returns. USBDeviceTasks(); #endif }//end while }//end main /******************************************************************************* End of File */ |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
/******************************************************************************* Copyright 2016 Microchip Technology Inc. (www.microchip.com) Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. To request to license the code under the MLA license (www.microchip.com/mla_license), please contact mla_licensing@microchip.com *******************************************************************************/ #include "system.h" #include "usb.h" #include "tmr0.h" /** CONFIGURATION Bits **********************************************/ // PIC16F1459 configuration bit settings: #if defined (USE_INTERNAL_OSC) // Define this in system.h if using the HFINTOSC for USB operation // CONFIG1 #pragma config FOSC = INTOSC // Oscillator Selection Bits (INTOSC oscillator: I/O function on CLKIN pin) #pragma config WDTE = OFF // Watchdog Timer Enable (WDT disabled) #pragma config PWRTE = OFF // Power-up Timer Enable (PWRT disabled) #pragma config MCLRE = OFF // MCLR Pin Function Select (MCLR/VPP pin function is digital input) #pragma config CP = OFF // Flash Program Memory Code Protection (Program memory code protection is disabled) #pragma config BOREN = ON // Brown-out Reset Enable (Brown-out Reset enabled) #pragma config CLKOUTEN = OFF // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin) #pragma config IESO = OFF // Internal/External Switchover Mode (Internal/External Switchover Mode is disabled) #pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is disabled) // CONFIG2 #pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off) #pragma config CPUDIV = NOCLKDIV// CPU System Clock Selection Bit (NO CPU system divide) #pragma config USBLSCLK = 48MHz // USB Low SPeed Clock Selection bit (System clock expects 48 MHz, FS/LS USB CLKENs divide-by is set to 8.) #pragma config PLLMULT = 3x // PLL Multipler Selection Bit (3x Output Frequency Selected) #pragma config PLLEN = ENABLED // PLL Enable Bit (3x or 4x PLL Enabled) #pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset) #pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.) #pragma config LPBOR = OFF // Low-Power Brown Out Reset (Low-Power BOR is disabled) #pragma config LVP = OFF // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming) #else // CONFIG1 #pragma config FOSC = HS // Oscillator Selection Bits (HS Oscillator, High-speed crystal/resonator connected between OSC1 and OSC2 pins) #pragma config WDTE = OFF // Watchdog Timer Enable (WDT disabled) #pragma config PWRTE = OFF // Power-up Timer Enable (PWRT disabled) #pragma config MCLRE = OFF // MCLR Pin Function Select (MCLR/VPP pin function is digital input) #pragma config CP = OFF // Flash Program Memory Code Protection (Program memory code protection is disabled) #pragma config BOREN = ON // Brown-out Reset Enable (Brown-out Reset enabled) #pragma config CLKOUTEN = OFF // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin) #pragma config IESO = OFF // Internal/External Switchover Mode (Internal/External Switchover Mode is disabled) #pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is disabled) // CONFIG2 #pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off) #pragma config CPUDIV = NOCLKDIV// CPU System Clock Selection Bit (NO CPU system divide) #pragma config USBLSCLK = 48MHz // USB Low SPeed Clock Selection bit (System clock expects 48 MHz, FS/LS USB CLKENs divide-by is set to 8.) #pragma config PLLMULT = 4x // PLL Multipler Selection Bit (4x Output Frequency Selected) #pragma config PLLEN = ENABLED // PLL Enable Bit (3x or 4x PLL Enabled) #pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset) #pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.) #pragma config LPBOR = OFF // Low-Power Brown Out Reset (Low-Power BOR is disabled) #pragma config LVP = OFF // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming) #endif /********************************************************************* * Function: void SYSTEM_Initialize( SYSTEM_STATE state ) * * Overview: Initializes the system. * * PreCondition: None * * Input: SYSTEM_STATE - the state to initialize the system into * * Output: None * ********************************************************************/ void SYSTEM_Initialize( SYSTEM_STATE state ) { switch(state) { case SYSTEM_STATE_USB_START: #if defined(USE_INTERNAL_OSC) //Make sure to turn on active clock tuning for USB full speed //operation from the INTOSC OSCCON = 0xFC; //HFINTOSC @ 16MHz, 3X PLL, PLL enabled ACTCON = 0x90; //Active clock tuning enabled for USB #endif break; case SYSTEM_STATE_USB_SUSPEND: break; case SYSTEM_STATE_USB_RESUME: break; } } void interrupt SYS_InterruptHigh(void) { #if defined(USB_INTERRUPT) USBDeviceTasks(); #endif // // TIMER0 // TMR0_ISR(); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
/* * File: tmr0.h * Author: Sasapea's Lab * * Created on 2018/03/14 */ #ifndef _TMR0_H #define _TMR0_H #include <xc.h> // include processor files - each processor file is guarded. #include <stdint.h> #ifndef SYS_FOSC #define SYS_FOSC 48000000 #endif ///////////////////////////////////////////// // Prescaler Setting's (SYS_FOSC=48MHz) // // max time(s) period(us) irq-cycle(us)// ///////////////////////////////////////////// // 0: 357.913, 0.083, 21.333 // // 1: 715.827, 0.166, 42.666 // // 2: 431.655, 0.333, 85.333 // // 3: 2863.311, 0.666, 170.666 // // 4: 5726.623, 1.333, 341.333 // // 5: 11453.246, 2.666, 682.666 // // 6: 22906.492, 5.333, 1365.333 // // 7: 45812.984, 10.666, 2730.666 // // 8: 91625.968, 21.333, 5461.333 // ///////////////////////////////////////////// #ifndef TMR0_PS #define TMR0_PS 8 // 0=1/1, 1=1/2, 2=1/4,...8=1/256 #endif #define TMR0_MS2TICK(t) ((t) * (uint32_t)(SYS_FOSC / 4 / 1000) / (1 << TMR0_PS)) #define TMR0_US2TICK(t) ((t) * (uint32_t)(SYS_FOSC / 4 / 1000000) / (1 << TMR0_PS)) #define TMR0_ISR() do { \ if (INTCONbits.TMR0IF) \ { \ tmr0_isr(); \ INTCONbits.TMR0IF = 0; \ } \ } while (0) #ifdef __cplusplus extern "C" { #endif void tmr0_isr(); void tmr0_init(); uint32_t tmr0_read(); #ifdef __cplusplus } #endif #endif // _TMR0_H |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
/* * File: tmr0.c * Author: Sasapea's Lab * * Created on 2018/03/14 */ #include "tmr0.h" static volatile uint32_t _counter; void tmr0_isr() { _counter += 0x0100; } void tmr0_init() { OPTION_REGbits.TMR0CS = 0; OPTION_REGbits.PSA = !TMR0_PS; OPTION_REGbits.PS = TMR0_PS - 1; TMR0 = 0; INTCONbits.TMR0IF = 0; INTCONbits.TMR0IE = 1; } uint32_t tmr0_read() { union { uint8_t b[4]; uint16_t w[2]; uint32_t d; } x; do { x.b[1] = ((uint8_t *)&_counter)[1]; x.w[1] = ((uint16_t *)&_counter)[1]; x.b[0] = TMR0; } while (x.b[1] != ((uint8_t *)&_counter)[1]); return x.d; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
/* * File: pwm.h * Author: Sasapea's Lab * * Created on 2018/07/01 */ #ifndef PWM_H #define PWM_H #include <xc.h> // include processor files - each processor file is guarded. #include <stdint.h> #include <stdbool.h> #ifndef SYS_FOSC #define SYS_FOSC 48000000 #endif #if defined(_16F1454) || defined(_16F1455) #define PWM_RC3 0 #define PWM_RC5 1 #define PWM_RA5 2 #elif defined(_16F1459) #define PWM_RC5 0 #define PWM_RC6 1 #endif #ifdef __cplusplus extern "C" { #endif bool pwm_init(uint8_t ch, uint8_t op); bool pwm_duty(uint8_t ch, uint8_t dc); bool pwm_clock(uint32_t clock); #ifdef __cplusplus } #endif #endif /* PWM_H */ |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
/* * File: pwm.c * Author: Sasapea's Lab * * Created on 2018/07/01 */ #include "pwm.h" bool pwm_init(uint8_t ch, uint8_t op) { switch (ch) { #if defined(_16F1454) || defined(_16F1455) case 2: if (!APFCONbits.P2SEL && PWM2CONbits.PWM2OE) TRISCbits.TRISC5 = 1; TRISAbits.TRISA5 = 1; APFCONbits.P2SEL = 1; PWM2CON = 0; PWM2DCH = 0; PWM2DCL = 0; PWM2CONbits.PWM2POL = op; PWM2CONbits.PWM2OE = 1; PWM2CONbits.PWM2EN = 1; TRISAbits.TRISA5 = 0; return true; #endif #if defined(_16F1454) || defined(_16F1455) case 1: if (APFCONbits.P2SEL && PWM2CONbits.PWM2OE) TRISAbits.TRISA5 = 1; TRISCbits.TRISC5 = 1; APFCONbits.P2SEL = 0; PWM2CON = 0; PWM2DCH = 0; PWM2DCL = 0; PWM2CONbits.PWM2POL = op; PWM2CONbits.PWM2OE = 1; PWM2CONbits.PWM2EN = 1; TRISCbits.TRISC5 = 0; return true; #elif defined(_16F1459) case 1: TRISCbits.TRISC6 = 1; PWM2CON = 0; PWM2DCH = 0; PWM2DCL = 0; PWM2CONbits.PWM2POL = op; PWM2CONbits.PWM2OE = 1; PWM2CONbits.PWM2EN = 1; TRISCbits.TRISC6 = 0; return true; #endif #if defined(_16F1454) || defined(_16F1455) case 0: TRISCbits.TRISC3 = 1; PWM1CON = 0; PWM1DCH = 0; PWM1DCL = 0; PWM1CONbits.PWM1POL = op; PWM1CONbits.PWM1OE = 1; PWM1CONbits.PWM1EN = 1; TRISCbits.TRISC3 = 0; return true; #elif defined(_16F1459) case 0: TRISCbits.TRISC5 = 1; PWM1CON = 0; PWM1DCH = 0; PWM1DCL = 0; PWM1CONbits.PWM1POL = op; PWM1CONbits.PWM1OE = 1; PWM1CONbits.PWM1EN = 1; TRISCbits.TRISC5 = 0; return true; #endif } return false; } bool pwm_duty(uint8_t ch, uint8_t dc) { uint16_t c; if (dc <= 100) { switch (ch) { #if defined(_16F1454) || defined(_16F1455) case 2: #endif case 1: c = ((((uint32_t)PR2 + 1) << _PWM2DCL_PWM2DCL_SIZE) * dc + 50) / 100; PWM2DCHbits.PWM2DCH = c >> _PWM2DCL_PWM2DCL_SIZE; PWM2DCLbits.PWM2DCL = c; return true; case 0: c = ((((uint32_t)PR2 + 1) << _PWM1DCL_PWM1DCL_SIZE) * dc + 50) / 100; PWM1DCHbits.PWM1DCH = c >> _PWM1DCL_PWM1DCL_SIZE; PWM1DCLbits.PWM1DCL = c; return true; } } return false; } bool pwm_clock(uint32_t clock) { uint32_t pr = clock ? ((SYS_FOSC * 10 / 4) / clock + 5) / 10 : 0; uint8_t ps; for (ps = 0; pr > 0x0100; ++ps) pr >>= _T2CON_T2CKPS_SIZE; if (pr && (ps < (1 << _T2CON_T2CKPS_SIZE))) { T2CONbits.TMR2ON = 0; PIE1bits.TMR2IE = 0; PIR1bits.TMR2IF = 0; T2CON = 0; TMR2 = 0; PR2 = pr - 1; T2CONbits.T2CKPS = ps; T2CONbits.TMR2ON = 1; return true; } return false; } |
PCB剥き出しのままだと可愛そうなので3Dプリンターでケースを作ってあげた。いつものごとくハメこみ式。ネジ類は使っていない。
本体をPCに接続したらTera-term等のターミナル・ソフトで仮想COMポートを開いてコマンドを送信することができる。アプリとの連携利用を考慮し、本体はエコー・オフ状態で起動するためターミナル・ソフトを使う場合は、最初にエコーをONにすることをお勧めする。
ECHO ON
その後、
HELP
を入力すると使い方の説明が表示されるので参考にしてほしい。