2020/03/21

Arduino メロディ Happy Birthday to You 30秒

//Happy Birthday to You

const unsigned char SPPIN = A5;
int Piano[20];

unsigned char xx[] = {50, 50, 6, 5, 11, 7, 0,
                      50, 50, 6, 5, 12, 11, 0,
                      50, 50, 15, 13, 11, 7, 6,
                      14, 140, 13, 11, 12, 11, 0
                     };

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Piano[0] = 0; //C4
  Piano[1] = 261.626; //C4
  Piano[2] = 293.665; //D4
  Piano[3] = 329.628; //E4
  Piano[4] = 349.228; //F4
  Piano[5] = 391.995; //G4
  Piano[6] = 440.000; //A4
  Piano[7] = 493.883; //B5
  Piano[11] = 523.251; //C5
  Piano[12] = 587.330; //D5
  Piano[13] = 659.255; //E5
  Piano[14] = 698.456; //F5
  Piano[15] = 783.991; //G5
}

void loop() {
  // put your main code here, to run repeatedly:
  Serial.println("a");
  music();
  music();
  Serial.println("b");
  while (1) {
    noTone(A0);
  }
}

void music() {
  for (unsigned char i = 0; i < sizeof(xx); i++) {
    if (xx[i] == 50 || xx[i] == 40 || xx[i] == 140) {
      PianoS((xx[i] / 10), 300);
    } else {
      PianoS(xx[i], 600);
    }
  }
}

void PianoS(unsigned char x, int tempo) {
  tone(SPPIN, Piano[x]);
  delay(tempo);
  noTone(SPPIN);
  delay(0);
  return;
}

2020/03/15

PIC 12F1822 備考

/*
 * File:   main.c
 * PIC 12F1822
 * 32 MHz 
 * 0.03125us
 * Created on 2020/03/06, 23:25
 */

// CONFIG1
#pragma config FOSC = INTOSC    // Oscillator Selection (INTOSC oscillator: I/O function on CLKIN pin)
#pragma config WDTE = OFF       // Watchdog Timer Enable (WDT disabled)
#pragma config PWRTE = ON       // Power-up Timer Enable (PWRT enabled)
#pragma config MCLRE = ON      // 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 CPD = OFF        // Data Memory Code Protection (Data 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 (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 PLLEN = OFF      // PLL Enable (4x PLL disabled)
#pragma config STVREN = ON      // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset)
#pragma config BORV = HI        // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), high trip point selected.)
#pragma config LVP = OFF        // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming)

#include <xc.h>
#include<stdio.h>
#define _XTAL_FREQ 32000000
__EEPROM_DATA('t', 'b', 2, 3, 4, 5, 6, 7);

void SET_INIT();
void TMR1_INIT();
void T0(void);
void T1(void);
void EEPROMWRITE(unsigned char adr, unsigned char data);
unsigned char EEPROMREAD(unsigned char addr);

void interrupt T1_isr(void) {
    //TMR1H = 255; //40
    //TMR1L = 160; //40
    //TMR1H = 255; //20
    //TMR1L = 239; //20
    if (TMR1IF == 1) {
        PORTAbits.RA2 = ~PORTAbits.RA2;
        TMR1H = 255;
        TMR1L = 254; //240
        TMR1IF = 0;
    }
}

void main(void) {
    SET_INIT();
    TMR1_INIT();
    __delay_ms(1);
    PORTAbits.RA2 = 0;
    TMR1H = 255;
    TMR1L = 240;
    T1GCONbits.TMR1GE = 1;
    PIE1bits.TMR1IE = 1;

    while (1) {
        NOP();
        NOP();
        NOP();
        NOP();
        NOP();
        NOP();
        NOP();
        NOP();
        NOP();
    }
}

void T0(void) {
    PORTAbits.RA2 = 1;
    PORTAbits.RA2 = 0;
    return;
}

void T1(void) {
    PORTAbits.RA2 = 1;
    PORTAbits.RA2 = 0;
    return;
}

void TMR1_INIT() {
    T1CONbits.TMR1CS = 1;
    T1CONbits.TMR1ON = 1;
    //    T1GCONbits.TMR1GE = 1;
    //    PIE1bits.TMR1IE = 1;
    return;
}

void SET_INIT() {
    OSCCONbits.SPLLEN = 1; //4xPPL ??
    OSCCONbits.IRCF3 = 1;
    OSCCONbits.IRCF2 = 1;
    OSCCONbits.IRCF1 = 1;
    OSCCONbits.IRCF0 = 0;
    OSCCONbits.SCS1 = 1;
    OSCCONbits.SCS0 = 0;
    ANSELAbits.ANSA4 = 0;
    ANSELAbits.ANSA2 = 0;
    ANSELAbits.ANSA1 = 0;
    ANSELAbits.ANSA0 = 0;
    TRISAbits.TRISA0 = 0;
    TRISAbits.TRISA1 = 0;
    TRISAbits.TRISA2 = 0;
    TRISAbits.TRISA3 = 1;
    TRISAbits.TRISA4 = 0;
    TRISAbits.TRISA5 = 0;
    INTCONbits.PEIE = 1;
    INTCONbits.GIE = 1;
    return;
}

void EEPROMWRITE(unsigned char add, unsigned char data) {
    EEADRLbits.EEADRL = add;
    EEDATLbits.EEDATL = data;
    EECON1bits.CFGS = 0;
    EECON1bits.EEPGD = 0;
    EECON1bits.WREN = 1;
    INTCONbits.GIE = 0;
    EECON2bits.EECON2 = 0x55;
    EECON2bits.EECON2 = 0xaa;
    EECON1bits.WR = 1;
    while (EECON1bits.WR == 1);
    INTCONbits.GIE = 1;
    EECON1bits.WREN = 0;
    return;
}

unsigned char EEPROMREAD(unsigned char addr) {
    EEADRL = addr;
    INTCONbits.GIE = 0;
    EECON1bits.RD = 1;
    while (EECON1bits.RD == 1);
    return EEDATA;
}

2020/03/11

PICマイコン (C言語) サイクル

PICマイコン (C言語) サイクル

12F1822 (32MHz) 設定
XC8-v2.10

for 15 サイクルかかる


while 3 サイクルかかる


x++ 5サイクルかかる





end

2020/03/06

Firmware Suite Version.....01.56.02


*****************************************************

Connecting to MPLAB PICkit 3...

Currently loaded firmware on PICkit 3
Firmware Suite Version.....01.28.90 *
Firmware type..............Midrange

Now Downloading new Firmware for target device: PIC12F675
Downloading bootloader
Bootloader download complete
Programming download...
Downloading RS...
RS download complete
Programming download...
Downloading AP...
AP download complete
Programming download...

Currently loaded firmware on PICkit 3
Firmware Suite Version.....01.56.02
Firmware type..............Midrange




20200411
Currently loaded firmware on PICkit 3
Firmware Suite Version.....01.56.07
Firmware type..............Midrange



end

2020/03/05

Arduino bit()

Arduino 日本語リファレンス

bit()

制御レジスタのビットに対応する値を確認







end







2020/03/01

RCWL-0516スイッチモジュール 動作確認

アナログの出力がないのが残念ですね.


[code]
/*
   マイクロ波レーダーセンサーRCWL-0516スイッチモジュールヒューマンインダクションボード検出器 検出距離 5-7m
*/

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(A0, INPUT);
  pinMode(A1, OUTPUT);
}

void loop() {
  //  digitalWrite(A1, 1);
  // put your main code here, to run repeatedly:
  int i;
  unsigned long  cont = millis();
  while (1) {
    delay(1000);
    i = analogRead(A0);
    if (i > 300) {
      digitalWrite(A1, 1);
      Serial.println("ON");
      cont = millis();
    } else {
      // 一定の時間(5分)以内人体の移動が感知がなければ,OFFにする
      if ((millis() - cont) > (1000l * 300l)) {
        digitalWrite(A1, 0);
        Serial.println("OFF");
        Serial.println("<0");
      }
    }
  }

}
[/code]

そのままでコピー
特徴:
- 動作電圧:4.0-28VDC
- 動作電流:3mA未満
- 出力電圧:3.3V(3V3ピンから)
- 出力電流:最大100mA
- 検出範囲:7メートル以内に120度未満のテーパー角度
- トリガーモード:トリガーを繰り返す
- 遅延時間:2秒(デフォルト値)、それを調整するためにC-TMパッド上の異なる容量のコンデンサを半田付けする
- アクティブレベル:高(移動オブジェクトを検出した場合)
- 出力インターフェイス:3V3(3.3V電源出力)、GND(電源グランド)、OUT(出力信号)、VIN(電源入力)、CDS(外付けフォト抵抗器をグランドに半田付けすることにより、日中に検出をオフにすることができます。一般的なアプリケーションでは、GND / OUT / VINだけを使用する必要があります。
- 用途:自動照明、スマートホーム、DIYプロジェクト


end

2020/02/24

Arduino 74HC595A_shiftOut_Pin 接続

Arduino 74HC595A_shiftOut_Pin 接続

 
/*
   shiftOut のテストと赤色7セグメントLED表示器のテスト
   https://github.com/kankanla/arduino/blob/master/sketch_feb23a.ino
*/

uint8_t resetPin  = A0;  //P10 SRCLR/RESET (常にH可)
uint8_t clockPin  = A1;  //P11 SRCLK/SHIFT CLOCK(クロックを出力するピン)
uint8_t latchPin  = A2;  //P12 RCLK/LATCH CLOCK(LOW->HIGH で更新)
uint8_t oePin     = A3;  //P13 OE/OUTPUT ENABLE (出力有効/無効,常にH可)
uint8_t dataPin   = A4;  //P14 SER/A (ビットを出力するピン)

void setup() {
  Serial.begin(9600);
  pinMode(resetPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(latchPin, OUTPUT);
  pinMode(oePin, OUTPUT);
  pinMode(dataPin, OUTPUT);
}

void loop() {
  Number();
}

void Number() {
  /*
    赤色7セグメントLED表示器LTS-547BJR-01J
     0から9までの数字のバタン
     ピンの配置
        A
       F B
        G
       E C
        D  dp
  */
  byte n2[] = {
    0b00111111,   //0
    0b00000110,   //1
    0b01011011,   //2
    0b01001111,   //3
    0b01100110,   //4
    0b01101101,   //5
    0b01111101,   //6
    0b00100111,   //7
    0b01111111,   //8
    0b01101111    //9
  };

  byte n[] = {
    0b00000001,   //A
    0b00000010,   //B
    0b00000100,   //C
    0b00001000,   //D
    0b00010000,   //E
    0b00100000,   //F
    0b01000000,   //G
    0b10000000,   //dp
  };

  digitalWrite(resetPin, 1);
  for (uint8_t i = 0; i < sizeof(n); i++) {
    Serial.println(n[i], DEC);
    Serial.println(i);
    shiftOut(dataPin, clockPin, MSBFIRST, n[i]);
    digitalWrite(latchPin, 0);
    digitalWrite(latchPin, 1);
    delay(200);
  }
}



end