📄 main.c
字号:
/************************************************\* by threewater<threewater@up-tech.com> ** 2003.12.18 ** *\***********************************************/#include <stdio.h>#include <unistd.h>#include <linux/delay.h>#include <sys/types.h>#include <sys/ipc.h>#include <sys/ioctl.h>#include <pthread.h>#include "hardware.h"#include "motor.h"#include "exio.h"#include "../keyboard/get_key.h"#define ADCCON_FLAG 0x40#define ADCCON_SLEEP 0x20#define ADCCON_ADIN0 (0x0<<2)#define ADCCON_ADIN1 (0x1<<2)#define ADCCON_ADIN2 (0x2<<2)#define ADCCON_ADIN3 (0x3<<2)#define ADCCON_ADIN4 (0x4<<2)#define ADCCON_ADIN5 (0x5<<2)#define ADCCON_ADIN6 (0x6<<2)#define ADCCON_ADIN7 (0x7<<2)#define ADCCON_READ_START 0x2#define ADCCON_ENABLE_START 0x1void Delay(int t){ //struct timespec dt={0,10000}; //dt.tv_nsec*=t; //nanosleep(&dt, NULL); 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;}static int stop=0;void* comMonitor(void* data){ get_key(); stop=1; get_key(); stop=2; return NULL;}#define ADMID 512int main(void){ //////////////////A, AB, B, BC, C, CD, D, DA int stepdata[]={0x10,0x30,0x20,0x60,0x40,0xc0,0x80,0x90}; int i,j,ADData,count; float d; pthread_t th_com; void * retval; kbd_init(); //set s3c44b0 AD register and start AD init_ADdevice(); init_exio(); init_MotorPort(); Delay(1); /* Create the threads */ pthread_create(&th_com, NULL, comMonitor, 0); printf("\nBegin control DC motor.\nPress Enter to control step motor.\n"); while( stop==0 ){ ADData=GetADresult(0); Delay(10); SetPWM((ADData-ADMID)*MOTOR_CONT/1024); Delay(10); } SetPWM(0); Delay(1000); exit_MotorPort(); printf("\nBegin control step motor.\nPress Enter to control DC motor.\n"); i=0; while( stop==1 ){ ADData=GetADresult(0); //printf("AD=%4.4d, i=%d\n",ADData,i); count=(ADData-ADMID); if(count>0){ i++; }else if (count<0){ i--; count=-count; } i&=0x7; SETEXIOBITMASK(stepdata[i], 0xf0); //Delay(3000); Delay(512*10/count); } /* Wait until producer and consumer finish. */ pthread_join(th_com, &retval); printf("\n"); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -