毕业课题求问`
时间:10-02
整理:3721RD
点击:
3足 万向轮 单片机 红外传感器的 移动小车
原理图硬件都完成 但小组这边没人会C语言编写 有大神会么?
C语言剩马达和传感器部分没有编写.贴出的C语言测试后没有问题3轮同时运动涉及到计算公式不同的速度来完成移动目的
真心求教马达和传感器的C程序`下面也贴出去年毕业的文档,但没有传感器部分,马达可以动.#include <stdio.h>
#include <stdlib.h>
#include <p24FJ64GA002.h>
//使用机能的設定
_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("%d\r\n",ADCdata.ADData1);
printf("%d\r\n",ADCdata.ADData2);
printf("%d\r\n",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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <p24FJ64GA002.h>
// 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] = '\0'; // Null追加
Behavior(); // 行动決定
//printf("%d %s",V[0],ReceiveData[0]);
keisan();
len = 0; // 接受信号文字数清零
}
else if (len < 30)
{
len++; // 加载接受信号文字数
}
}
}
return (EXIT_SUCCESS);
}
//时间1的設定
/*void Init_Timer1(void)
{
T1CONbits.TON = 1; // 开关ON
T1CONbits.TCKPS = 3; // prescaler的設定(1/256)
PR1 = 1599; // 时间的计算値
_T1IE = 1; // 加载許可
_T1IP = 5; // 加载优先顺序(最高7)
_T1IF = 0; // 加载清零
}
*/
//計算用
//#define M1B2 -0.5
//#define R3B2 0.8660254038 // (√3) / 2
//接受信号的命令中止后转变int型
void Behavior()
{
int i=0;
if(ReceiveData[0]=='V'){
int j=0;
//V的初始化
for(i=0;i<5;i++){
V=0;
}
for(i=0;i<len;i++){
if(0x30<=ReceiveData && ReceiveData<=0x39){
V[j]*=10;
V[j]+=ReceiveData-0x30;
}else if(ReceiveData==','){
j++;
}
}
}
}
//滚轮的回转速度计算
void keisan(void){
Mref[0] = (int)(-0.5 * V[0] + 0.8660254038 * V[1] + L * V[2]);
Mref[1] = (int)(-0.5 * V[0] - 0.8660254038 * V[1] + L * V[2]);
Mref[2] = (int)( V[0] + L * V[2]);
if(0>Mref[0]){
OC1RS = 5000; // 低WPM信号的计算値
_RP2R = 18; // RPn的PWM出力(OC1)
_RP3R = 21; // RPn的PWM出力(OC4)
} else if(0<Mref[0]){
OC1RS = 5000; // 低WPM信号的计算値
_RP2R = 21; // RPn的PWM出力(OC4)
_RP3R = 18; // RPn的PWM出力(OC1)
}else{
_RP2R = 21; // RPn的PWM出力(OC4)
_RP3R = 21; // RPn的PWM出力(OC4)
}
if(0>Mref[1]){
OC2RS = 5000; // 低WPM信号的计算値
_RP4R = 19; // RPn的PWM出力(OC2)
_RP5R = 21; // RPn的PWM出力(OC4)
}else if(0<Mref[1]){
OC2RS = 5000; // 低WPM信号的计算値
_RP4R = 21; // RPn的PWM出力(OC4)
_RP5R = 19; // RPn的PWM出力(OC2)
}else{
_RP4R = 21; // RPn的PWM出力(OC4)
_RP5R = 21; // RPn的PWM出力(OC4)
}
if(0>Mref[2]){
OC3RS = 5000; // 低WPM信号的计算値
_RP6R = 20; // RPn的PWM出力(OC3)
_RP7R = 21; // RPn的PWM出力(OC4)
}else if(0<Mref[2]){
OC3RS = 5000; // 低WPM信号的计算値
_RP6R = 21; // RPn的PWM出力(OC4)
_RP7R = 20; // RPn的PWM出力(OC3)
}else{
_RP6R = 21; // RPn的PWM出力(OC4)
_RP7R = 21; // RPn的PWM出力(OC4)
}
printf("OK");
}
//时间2的設定
void Init_Timer2(void)
{
T2CONbits.TON = 1; // 时间ON
T2CONbits.TCKPS = 0; // prescaler的設定(1/256)
PR2 = 15999; // 计算的値
}
//马达1的時間設定里加入a数据计算,马达实验和通信实验
/*void __attribute__ ((interrupt, auto_psv)) _T1Interrupt(void){
// a++;
// if(a>=4000){
// a=0;
// }
printf("a");
_T1IF = 0; // flag清零
}
*/
//PWM初期設定
void Init_PWM()
{
OC1R = 0; // 高PWM信号的计算値
OC1RS = 5000; // 低WPM信号的计算值
OC1CONbits.OCM = 6; // PWM形式
OC2R = 0; // 高PWM信号的计算値
OC2RS = 8000; // 低WPM信号的计算值
OC2CONbits.OCM = 6; // PWM形式
OC3R = 0; // 高PWM信号的计算値
OC3RS = 10000; // 低WPM信号的计算值
OC3CONbits.OCM = 6; // PWM形式
//PWM4的分配值21
OC4R = 0; // 高PWM信号的计算値
OC4RS = 0; // 低WPM信号的计算值
OC4CONbits.OCM = 6; // PWM形式
_RP2R = 21;
_RP3R = 18;
_RP4R = 19;
_RP5R = 21;
_RP6R = 21;
_RP7R = 18;
}
//通信用設定
void Init_UART(void)
{
_U1RXR = 14; // RX pin 的番号(RP2)
_RP15R = 3; // RP3的机能(U1TX)
// _UTXINV = 1;
// _RXINV = 1;
U1MODEbits.BRGH = 1; // 有效的高速波特率
//115200为無線最快无限通信数值
U1BRG = 34; // 115200 bps
// U1BRG = 103; // 38400 bps
// _U1RXIE = 1; // 允许加载收信号
// _U1RXIP = 7; // 加载收信号优先度
// _U1RXIF = 0; // 加载收信号flag清零
U1MODEbits.UARTEN = 1; // UART有效
}
// 加载收信号
/*void __attribute__((interrupt, auto_psv)) _U1RXInterrupt(void)
{
while(U1STAbits.URXDA) // 信号动作读取
{
// ReceiveData[len] = U1RXREG; //加载收信号
//ReceiveData[1] = 0x00; // Nullの追加
// printf("%s",ReceiveData); // 接受信号
}
_U1RXIF = 0; // flag清零
}
*/
没人愿意一分帮你弄的 有可能得几天
第2345 是同一张图 分开传的清晰图```