/* H8/3052F Test Program */ #include "3048f.h" /* ================================================================== * 通信部分 * ================================================================== */ /* SCIを初期化 */ void InitializeSCI(void) { unsigned int i; /* SCI Initialize */ SCI1.SCR.BYTE = 0; SCI1.SMR.BYTE = 0; SCI1.BRR = 13; /* BRR: 8:115200bps 13:57600 19:38400 */ for(i=0;i<1000;i++) ; SCI1.SCR.BYTE = 0x30; /* T,R enable */ SCI1.SSR.BYTE &= 0x80; } /* 一文字送信 */ void PutChar(char c) { while(!SCI1.SSR.BIT.TDRE) ; SCI1.TDR = c; SCI1.SSR.BIT.TDRE = 0; } void PrintString(char *s) { while(*s) { PutChar(*s); s++; } } void PrintUInt(unsigned int i) { int f=0; if((i>=10000)||f) { PutChar((i/10000)+'0'); f=1; i=i%10000; } else { PutChar(' '); } if((i>=1000 )||f) { PutChar((i/1000 )+'0'); f=1; i=i%1000 ; } else { PutChar(' '); } if((i>=100 )||f) { PutChar((i/100 )+'0'); f=1; i=i%100 ; } else { PutChar(' '); } if((i>=10 )||f) { PutChar((i/10 )+'0'); f=1; i=i%10 ; } else { PutChar(' '); } PutChar(i+'0'); } char *_HexStr="0123456789ABCDEF"; void PrintXInt(unsigned int i) { PutChar(_HexStr[(i>>12)&0xf]); PutChar(_HexStr[(i>> 8)&0xf]); PutChar(_HexStr[(i>> 4)&0xf]); PutChar(_HexStr[ i &0xf]); } int __dummyint=0; int __dummygint; /* ================================================================== * 通信部分ここまで * ================================================================== */ /* ================================================================== * ステッピングモータ部 * ================================================================== */ #define STEPC 4 unsigned char StepPattern[STEPC]= { 0x01, 0x02, 0x04, 0x08, }; int stepseq; /* ステップ位置 */ unsigned char GetStep(int dir) { /* ステップ位置変更 */ if(dir>0) stepseq++; if(dir<0) stepseq--; /* 両端接続 */ if(stepseq<0) stepseq=STEPC-1; if(stepseq>=STEPC) stepseq=0; return StepPattern[stepseq]; } /* プログラム本体 */ void main(void) { int speed; int count; int i; unsigned char b; /* SCI 初期化 */ InitializeSCI(); /* AD初期化 */ AD.CSR.BYTE=0x33; /* CH0-3連続変換 */ /* 入力値:AD.DRA AD.DRB AD.DRC (AD.DRD>>6) 0-1023 */ /* DA初期化 */ DA.CR.BYTE=0xe0; /* DA0,1 有効 */ /* 出力値:DA.DR0 DA.DR1 0-255*/ /* ITU初期化 ITU3,4 パルス幅測定 */ ITU3.TIOR.BYTE=0x54; ITU3.TCR.BYTE=0x20; ITU4.TIOR.BYTE=0x54; ITU4.TCR.BYTE=0x20; ITU.TSTR.BYTE=0x18; /* ステッピングモータは P2 に接続 */ P2.DDR=0x0f; count=0; while(1) { speed=P1.DR.BYTE; /* 制限 */ if(speed>10000) speed=10000; if(speed<-10000) speed=-10000; /* DDS処理 */ if(count+speed>10000) /* +にあふれ */ { b=GetStep(+1); count=count+speed-10000; } else if(count+speed<-10000) /* −にあふれ */ { b=GetStep(-1); count=count+speed+10000; } else { b=GetStep(0); count=count+speed; } P2.DR.BYTE=b; for(i=0;i<100;i++) /* 時間つぶし */ ; } }