完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
扫一扫,分享给好友
本帖最后由 shmilw3303 于 2015-1-10 00:20 编辑 3足 万向轮 单片机 红外传感器的 移动小车 原理图硬件都完成 但小组这边没人会C语言编写 有大神会么??? C语言剩马达和传感器部分没有编写.贴出的C语言测试后没有问题3轮同时运动涉及到计算公式不同的速度来完成移动目的 真心求教马达和传感器的C程序`***出去年毕业的文档,但没有传感器部分,马达可以动.#include #include #include //使用机能的設定 _CONFIG1(JTAGEN_OFF & GCP_OFF & GWRP_OFF & BKBUG_OFF & COE_OFF & ICS_PGx1 & FWDTEN_OFF & WINDIS_OFF & FWPSA_PR128 & WDTPS_PS16384); _CONFIG2(IESO_OFF & SOSCSEL_SOSC & WUTSEL_FST & FNOSC_FRCPLL & FCKSM_CSDCMD & OSCIOFNC_OFF & IOL1WAY_ON & I2C1SEL_PRI & POSCMOD_NONE); void Init_UART(void); void Init_IO(void); void Init_ADC1(void); void Init_ADC2(void); void Init_ADC3(void); void Init_timer1(void); void Init_Timer3(void); int main(int argc, char** argv) { Init_IO(); Init_UART(); Init_ADC1(); Init_ADC2(); Init_ADC3(); Init_Timer1(); Init_Timer3(); while(1){ } return (EXIT_SUCCESS); } //PIN設定 void Init_IO(void){ CLKDIVbits.RCDIV = 0; //定时器prescaler的設定 AD1PCFGbits.PCFG9 = 1; //AN9PIN数字化 TRISBbits.TRISB8 = 0; // TRISBbits.TRISB9 = 0; } //*************************************************************** //UART?? //pic <-> FT232 //baud rate -> 115200bps //RP15 -> U1TX //RP14 -> U1RX //*************************************************************** void Init_UART(void){ RPINR18bits.U1RXR = 15; RPOR7bits.RP14R = 3; U1MODEbits.BRGH = 1; U1BRG = 34; U1MODEbits.PDSEL = 0; U1MODEbits.STSEL = 0; IEC0bits.U1RXIE = 1; IPC2bits.U1RXIP = 1; IFS0bits.U1RXIF = 0; U1MODEbits.UARTEN = 1; } void Init_ADC1(void){ _SSRC = 2; _ASAM = 1; _ADCS = 1; _SAMC = 1; _CH0SA = 0; _AD1IE = 1; _AD1IF = 0; _ADON = 1; } void Init_ADC2(void){ _SSRC = 2; _ASAM = 1; _ADCS = 1; _SAMC = 1; _CH0SA = 1; _AD1IE = 1; _AD1IF = 0; _ADON = 1; } void Init_ADC3(void){ _SSRC = 2; _ASAM = 1; _ADCS = 1; _SAMC = 1; _CH0SA = 11; _AD1IE = 1; _AD1IF = 0; _ADON = 1; } typedef struct { int ADData1; int ADData2; int ADData3; }ADChange; ADChange ADCdata; void __attribute__ ((interrupt, auto_psv)) _ADC1Interrupt(void){ ADCdata.ADData1 = ADC1BUF0; ADCdata.ADData2 = ADC1BUF0; ADCdata.ADData3 = ADC1BUF0; _AD1IF = 0; } //时间1的設定 void Init_Timer1(void){ T1CONbits.TON = 1; //时间ON T1CONbits.TCKPS = 1; //定时器prescaler的設定(1/256) PR1 = 20000; //时间加载値 _T1IE = 1; //加载許可 _T1IF = 0; //加载flag清除 } //加载的处理 void __attribute__ ((interrupt, auto_psv)) _T1Interrupt(void){ printf("%drn",ADCdata.ADData1); printf("%drn",ADCdata.ADData2); printf("%drn",ADCdata.ADData3); IFS0bits.T1IF = 0; //加载flag清除 } //时间3的設定 void Init_Timer3(void){ T3CONbits.TON = 1; T3CONbits.TCKPS = 1; PR3 = 20000; } //马达時間設定用的参数a //int a=0; int len=0,V[5]={0,0,0,0,0},Mref[5]={0,0,0,0,0}; //char test; char ReceiveData[64]; float L=0.06; //基板中心到轮胎的長度 去年的文档如下 #include #include #include #include // CONFIG2 #pragma config POSCMOD = HS // Primary Oscillator Select (HS Oscillator mode selected) #pragma config I2C1SEL = PRI // I2C1 Pin Location Select (Use default SCL1/SDA1 pins) #pragma config IOL1WAY = ON // IOLOCK Protection (Once IOLOCK is set, cannot be changed) #pragma config OSCIOFNC = OFF // Primary Oscillator Output Function (OSC2/CLKO/RC15 functions as CLKO (FOSC/2)) #pragma config FCKSM = CSDCMD // Clock Switching and Monitor (Clock switching and Fail-Safe Clock Monitor are disabled) #pragma config FNOSC = FRCPLL // Oscillator Select (Fast RC Oscillator with PLL module (FRCPLL)) #pragma config SOSCSEL = SOSC // Sec Oscillator Select (Default Secondary Oscillator (SOSC)) #pragma config WUTSEL = LEG // Wake-up timer Select (Legacy Wake-up Timer) #pragma config IESO = ON // Internal External Switch Over Mode (IESO mode (Two-Speed Start-up) enabled) // CONFIG1 #pragma config WDTPS = PS32768 // Watchdog Timer Postscaler (1:32,768) #pragma config FWPSA = PR128 // WDT Prescaler (Prescaler ratio of 1:128) #pragma config WINDIS = ON // Watchdog Timer Window (Standard Watchdog Timer enabled,(Windowed-mode is disabled)) #pragma config FWDTEN = OFF // Watchdog Timer Enable (Watchdog Timer is disabled) #pragma config ICS = PGx1 // Comm Channel Select (Emulator EMUC1/EMUD1 pins are shared with PGC1/PGD1) #pragma config GWRP = OFF // General Code Segment Write Protect (Writes to program memory are allowed) #pragma config GCP = OFF // General Code Segment Code Protect (Code protection is disabled) #pragma config JTAGEN = OFF // JTAG Port Enable (JTAG port is disabled) //马达1の時間設定用的変数a //int a=0; int len=0,V[5]={0,0,0,0,0},Mref[5]={0,0,0,0,0}; //char test; char ReceiveData[64]; float L=0.06; //基板中心到滚轮的距离 //定義 void Init_UART(void); void Behavior(); void Init_Timer2(); void Init_PWM(); void keisan(void); int main(void) { _RCDIV = 0; //AN的机能off _PCFG0 = 1; _PCFG1 = 1; _PCFG2 = 1; _PCFG3 = 1; _PCFG4 = 1; _PCFG5 = 1; _PCFG9 = 1; _PCFG10 = 1; _PCFG11 = 1; _PCFG12 = 1; //马达的設定 /*_TRISB2 = 0; _TRISB3 = 0; _TRISB4 = 0; _TRISB5 = 0; _TRISB6 = 0; _TRISB7 = 0; 马达1 _LATB2 = 0; _LATB3 = 1; 马达2 _LATB4 = 0; _LATB5 = 0; 马达3 _LATB6 = 0; _LATB7 = 1; */ //Init_Timer1(); Init_Timer2(); //时间2使用PWM计算 Init_UART(); Init_PWM(); //PWM的初期設定 //無限循环 while(1){ // if(test){ if(U1STAbits.URXDA){ // 接受信号行动命令 ReceiveData[len] = U1RXREG; // 信号接受写入 // ReceiveData[len] = test; printf("%c",ReceiveData[len]); if ((ReceiveData[len] == 'r') || (ReceiveData[len] == 'n')) // 收信号改行代码 { ReceiveData[len] = ' |