📄 motor.c
字号:
#include "hardware.h"#include "motor.h"#include <stdio.h>#include <pthread.h>#define ADCCON_SLEEP 0x20#define ADCCON_FLAG 0x40#define ADCCON_ENABLE_START 0x1void init_MotorPort(){ //Dead Zone=0, PreScalero1=2; TCFG0|=(0<<24)|2; //divider timer0=1/2; TCFG1&=(~0xf); TCNTB0= MOTOR_CONT; TCMPB0= MOTOR_MID; TCON|=0x2; //update mode for TCNTB0 and TCMPB0. TCON&=~0x2; TCON|=0x19; //timer0 = auto reload, start. Dead Zone}void SetPWM(int value){ TCMPB0= MOTOR_MID+value; //TCMPB0= MOTOR_MID+30;}void exit_MotorPort(void){ TCON&=~0x1f;}void Delay(int t){ int i; for(;t>0;t--)//延迟一段时间 for(i=0;i<400;i++);}void init_ADdevice(void){ ADCPSR=20; ADCCON=ADCCON_SLEEP;}int GetADresult(int channel){ ADCCON=(channel<<2)|ADCCON_ENABLE_START; Delay(1); while(!( (ADCCON) & ADCCON_FLAG)); return ADCDAT;}#define ADMID 512void Run_DC_Motor(void){ int ADData; //kbd_init(); init_ADdevice(); init_MotorPort(); Delay(1); ADData=GetADresult(0); Delay(10); // printf((ADData-ADMID)*MOTOR_CONT/1024); SetPWM((ADData-ADMID)*MOTOR_CONT/1024); Delay(10); }void run(int flag){ if(flag==1) Run_DC_Motor(); else exit_MotorPort(); //init_MotorPort();}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -