PIC16F819 with スーファミコントローラ　ユニポーラステッピングモータ TS3103N124

# ユニポーラステッピングモータ制御 with ｽｰﾌｧﾐｺﾝﾄﾛｰﾗ
<iframe width="560" height="315" src="https://www.youtube.com/embed/4jU10VRXu4U?si=09rvDVfXX26zC3Ce" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>

![IMG_4778.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3693934/caf24492-0d74-7a9b-b44a-569b98839310.jpeg)
ずいぶん昔に秋月で買ったステッピングモータを、16F819にスーファミコントローラを使って、CW,CCW方向に、動かしてみました。
# TS3103N124
![IMG_4774.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3693934/2822e315-735e-42fc-ee19-84881cebde9c.jpeg)

# 配線
![回路図TS3103N124.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3693934/7a96b978-2d88-2ed3-e163-4558f9ae35ad.png)


|線色|機能|
|:----:|:----:|
|白|電源9V|
|緑|X1|
|赤|Y1|
|黄|X2|
|青|Y2|

# タイミングチャート
![タイミングチャート.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3693934/7ea2821a-2a3f-db6c-ec37-83adb9fa3aa1.jpeg)

# IRF520 Nch FET
ドライブ用のNch　FET
![IMG_4775.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3693934/ce26fac2-c5f7-a468-0bac-84dfb5b9e056.jpeg)



# コード　PIC16F819
```C:main.c

// PIC16F1827 Configuration Bit Settings

// 'C' source line config statements

// CONFIG1
//#pragma config FOSC = HS      // Oscillator Selection (HS Oscillator, High-speed crystal/resonator connected between OSC1 and OSC2 pins)
#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 = OFF      // Power-up Timer Enable (PWRT disabled)
#pragma config MCLRE = ON       // MCLR Pin Function Select (MCLR/VPP pin function is MCLR)
#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 = ON        // Internal/External Switchover (Internal/External Switchover mode is enabled)
#pragma config FCMEN = ON       // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is enabled)

// CONFIG2
#pragma config WRT = OFF        // Flash Memory Self-Write Protection (Write protection off)
#pragma config PLLEN = ON       // PLL Enable (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 LVP =ON      // Low-Voltage Programming Enable (Low-voltage programming enabled)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

#include <xc.h>
#include <stdlib.h>
#include <stdio.h>
#include "Interrupt.h"
#include "Peripheral.h"
#include "famicon.h"

#define _XTAL_FREQ  16000000

void Oscillator_Init(void);
void Port_Init(void);
void delay_ms(uint16_t time);

uint8_t table[4]={
    0b11000000,
    0b10010000,
    0b00110000,
    0b01100000
};


void main(void)
{    
    uint8_t cnt=0;
    uint8_t i,j,temp;
    uint16_t buttonState;
    
    //Basic Hard Initialize
    Oscillator_Init();
    Port_Init(); 
    //Peripheral 
    Timer0_INIT();
    Timer2_INIT();
    USART_INIT();
    ADC_INIT();
    
    famiconInit();
    //割り込み許可
    Interrupt_START();
    
    while(1)
    {
        //USART interrupt processing
        if(usart.rxCompleted)
        {
            usart.rxCompleted=false;
            printf("%s\n",usart.rxBuf);
            usart.length=0;
            PIE1bits.RCIE=1;
        }
        
        //Timer0 interrupt processing
        if(tm0.up)
        {
            tm0.up=false;
            LATBbits.LATB0=~LATBbits.LATB0;
            //printf("%d\n",cnt++);
            if(ADC.Completed)
            {
                ADC.Completed=false;
                printf("%d\n", ADC.data[0]);
                PIE1bits.ADIE=1;
                PIE1bits.TMR2IE=1;
            }
            INTCONbits.TMR0IE=1;
        }
        
        buttonState = getButton();
        
        
        if((buttonState&0x0200)==0x0)
        {
            printf("%02X\n",buttonState);
            for(j=0; j<4; j++)
            {
                temp=table[j];
                table[j]|=(LATB&0x0F);
                LATB = table[j];
                table[j]=temp;
                delay_ms((1083-ADC.data[0])/10);
            }
            
        }
        if((buttonState&0x0100)==0x0)
        {
           printf("%02X\n",buttonState);
            for(j=0; j<4; j++)
            {
                temp=table[3-j];
                table[3-j]|=(LATB&0x0F);
                LATB = table[3-j];
                table[3-j]=temp;
                delay_ms((1083-ADC.data[0])/10);
            }
           
        }
    }
    return;
}

void Oscillator_Init(void)
{
    OSCCONbits.SPLLEN=0;
    OSCCONbits.IRCF=0b1111;//16Mhz
    OSCCONbits.SCS=0b10;//InternalOscillator
}

void Port_Init(void)
{
    TRISA=0x00;
    ANSELA=0x00;
    TRISB=0x02;     //RX:RB1
    ANSELB=0x00;
    PORTA=0x00;
    PORTB=0x00;  
    //Altanative Pin Selective
    APFCON0bits.RXDTSEL=0;  //RX:RB1
    APFCON1bits.TXCKSEL=0;  //TX:RB2
}

void delay_ms(uint16_t time)
{
    uint16_t t;
    for(t=0; t<time; t++)
        __delay_ms(1);
}


```

```C:famicon.h
#ifndef FAMICON_H
#define	FAMICON_H

#ifdef	__cplusplus
extern "C" {
#endif

#include <xc.h>
    
#define _XTAL_FREQ 10000000//10Mhz
    
#define PS PORTAbits.RA1
#define CLK PORTAbits.RA0
#define DATA1 PORTAbits.RA2

extern uint16_t famiconButtonState;
extern  void famiconInit(void);    
extern  uint16_t getButton(void);
    
   
#ifdef	__cplusplus
}
#endif

#endif	/* FAMICON_H */
```
```C:famicon.c
#include "famicon.h"

uint16_t famiconButtonState;
///@brief コントローラーの初期化
void famiconInit(void)
{
    TRISAbits.TRISA2=1;//DATA1線
}

///@brief ボタン押下状況取得関数
///return ボタン押下16ビットデータ
uint16_t getButton(void)
{
    uint16_t w_val;
    uint8_t cnt;
    w_val=0x0000;
        cnt=0;
        
        //最初の1ビット
        PS=1;
        CLK=1;
        __delay_us(10);
        PS=0;
        CLK=0;
        __delay_us(10);
        
        w_val|=DATA1;
        w_val<<=1;
        cnt++;
        //残り15ビットの取得
        do{
            CLK=1;
            __delay_us(10);
            CLK=0;
           // __delay_us(5);
            w_val|=DATA1;
            if(cnt!=15)
            {
                w_val<<=1;
            }
            cnt++;
        }while(cnt!=16);
        //受信完了    
        w_val&=0xFFF0;//ボタンデータのみを、抽出
        /*if(w_val!=0xFFF0)    
        {
            if((w_val&0x8000)==0x0)
            {
                printf("B ");
            }
            
            if((w_val&0x4000)==0x0)
            {
                printf("Y ");
            }
            
            if((w_val&0x2000)==0x0)
            {
                printf("SELECT ");
            }
            
            if((w_val&0x1000)==0x0)
            {
                printf("START ");
            }
            
            if((w_val&0x0800)==0x0)
            {
                printf("UP ");             
            }
                        
            if((w_val&0x0400)==0x0)
            {
                printf("DOWN ");
            }
            
            if((w_val&0x0200)==0x0)
            {
                printf("LEFT ");
            }
            
            if((w_val&0x0100)==0x0)
            {
                printf("RIGHT ");
            }
            
            if((w_val&0x0080)==0x0)
            {
                printf("A ");
            }
            
            if((w_val&0x0040)==0x0)
            {
                printf("X ");
            }
            
            if((w_val&0x0020)==0x0)
            {
                printf("L ");
            }
            
            if((w_val&0x0010)==0x0)
            {
                printf("R ");
            }
            printf("%04x\r",w_val);
        }*/
        return w_val;
}

```