📄 main.c
字号:
#include <hidef.h> /* common defines and macros */
#include <mc9s12dg128.h> /* derivative information */
#pragma LINK_INFO DERIVATIVE "mc9s12dg128b"
/*=======================define=====================*/
unsigned char num1; //为传感器检测服务
unsigned int LED; //记录传感器的状态
unsigned char state[3]; //细分传感器的情况,state[0]记录相同状态的次数
unsigned int speed,test_speed; //小车的速度
unsigned int steer;
unsigned int e,eb,ebb;
unsigned int kp,kd;
unsigned int former_sensor,now_sensor;
unsigned char directcount, outcount;
//unsigned char PID_val[16]= {
// 14,21,26,33,38,41,47,51,57,62,66,73,79,83,88,91 //2s对应的脉冲数
//} ;
// 通过拟合,脉冲数和pwm对应值存在现行关系Set_Speed=PID_val+19
unsigned int Set_Speed[16]= {
35,40,45,50,55,60,65,70,75,80,85,90,95,100,105,110
};
#define Big_Right 1830
//#define Middle 1550
#define Middle 1500
//#define Big_Left 1235
#define Big_Left 1190
/*===========================ADC==================*/
void sensor_set(void){
PORTA=(PORTA|0x01);
DDRA=(DDRA|0x01);
}
void sensor_clear(void){
DDRA=(DDRA&~0x01);
PORTA=(PORTA&~0x01);
}
void ADC_init(void){
PORTB=0xff;
DDRB=0xff;
ATD0CTL2=0xc0; //AD模块上电, 快速清零, 无等待模式, 禁止外部触发
ATD0CTL3=0x08; // 每个序列1次转换, 1号传感器先开启
ATD0CTL4=0x81; //2MHz A2D clock 8位精度
ATD0DIEN=0x00; // 禁止数字输入
//////////////
ATD1CTL2=0xc0; //AD模块上电, 快速清零, 无等待模式, 禁止外部触发
ATD1CTL3=0x08; // 每个序列1次转换, 1号传感器先开启
ATD1CTL4=0x81; //2MHz A2D clock 8位精度
ATD1DIEN=0x00; // 禁止数字输入
}
void ADC0_12(void){
unsigned char ADT_v;
switch(num1){
case 1:
sensor_set();
ATD0CTL5=0x80;
ATD0CTL3=0x08;
while(ATD0STAT1_CCF0!=1){}
ADT_v=ATD0DR0L;
if(ADT_v<150) {
LED=(LED&~0x01)|0x01;
//PORTB=~0x01;
state[0]=state[0]+1;
if(state[1]==0xff)state[1]=0;
else state[2]=0;
}
else
LED=LED&~0x01;
sensor_clear();
break;
case 2:
sensor_set();
ATD0CTL5=0x81;
ATD0CTL3=0x10;
while(ATD0STAT1_CCF1!=1){}
ADT_v=ATD0DR1L;
if(ADT_v<150){
LED=(LED&~0x02)|0x02;
//PORTB=~0x02;
state[0]=state[0]+1;
if(state[1]==0xff)state[1]=1;
else state[2]=1;
}
else
LED=LED&~0x02;
sensor_clear();
break;
case 3:
sensor_set();
ATD0CTL5=0x82;;
ATD0CTL3=0x18;
while(ATD0STAT1_CCF2!=1){}
ADT_v=ATD0DR2L;
if(ADT_v<150) {
LED=(LED&~0x04)|0x04;
//PORTB=~0x03;
state[0]=state[0]+1;
if(state[1]==0xff)state[1]=2;
else state[2]=2;
}
else
LED=LED&~0x04;
sensor_clear();
break;
case 4:
sensor_set();
ATD0CTL5=0x83;
ATD0CTL3=0x20;
while(ATD0STAT1_CCF3!=1){}
ADT_v=ATD0DR3L;
if(ADT_v<150) {
LED=(LED&~0x08)|0x08;
//PORTB=~0x04;
state[0]=state[0]+1;
if(state[1]==0xff)state[1]=3;
else state[2]=3;
}
else
LED=LED&~0x08;
sensor_clear();
break;
case 5:
sensor_set();
ATD0CTL5=0x84;
ATD0CTL3=0x28;
while(ATD0STAT1_CCF4!=1){}
ADT_v=ATD0DR4L;
if(ADT_v<150) {
LED=(LED&~0x10)|0x10;
//PORTB=~0x05;
state[0]=state[0]+1;
if(state[1]==0xff)state[1]=4;
else state[2]=4;
}
else
LED=LED&~0x10;
sensor_clear();
break;
case 6:
sensor_set();
ATD0CTL5=0x85;
ATD0CTL3=0x30;
while(ATD0STAT1_CCF5!=1){}
ADT_v=ATD0DR5L;
if(ADT_v<150) {
LED=(LED&~0x20)|0x20;
//PORTB=~0x06;
state[0]=state[0]+1;
if(state[1]==0xff)state[1]=5;
else state[2]=5;
}
else
LED=LED&~0x20;
sensor_clear();
break;
case 7:
sensor_set();
ATD0CTL5=0x86;
ATD0CTL3=0x38;
while(ATD0STAT1_CCF6!=1){}
ADT_v=ATD0DR6L;
if(ADT_v<150) {
LED=(LED&~0x40)|0x40;
// PORTB=~0x07;
state[0]=state[0]+1;
if(state[1]==0xff)state[1]=6;
else state[2]=6;
}
else
LED=LED&~0x40;
sensor_clear();
break;
case 8:
sensor_set();
ATD0CTL5=0x87;
ATD0CTL3=0x0;
while(ATD0STAT1_CCF7!=1){}
ADT_v=ATD0DR0L;
if(ADT_v<150) {
LED=(LED&~0x80)|0x80;
//PORTB=~0x08;
state[0]=state[0]+1;
if(state[1]==0xff)state[1]=7;
else state[2]=7;
}
else
LED=LED&~0x80;
sensor_clear();
break;
case 9:
sensor_set();
ATD1CTL5=0x80;
ATD1CTL3=0x08;
while(ATD1STAT1_CCF0!=1){}
ADT_v=ATD1DR0L;
if(ADT_v<150) {
LED=(LED&~0x100)|0x100;
//PORTB=~0x09;
state[0]=state[0]+1;
if(state[1]==0xff)state[1]=8;
else state[2]=8;
}
else
LED=LED&~0x100;
sensor_clear();
break;
case 10:
sensor_set();
ATD1CTL5=0x81;;
ATD1CTL3=0x10;
while(ATD1STAT1_CCF1!=1){}
ADT_v=ATD1DR1L;
if(ADT_v<150) {
LED=(LED&~0x200)|0x200;
//PORTB=~0x0a;
state[0]=state[0]+1;
if(state[1]==0xff)state[1]=9;
else state[2]=9;
}
else
LED=LED&~0x200;
sensor_clear();
break;
case 11:
sensor_set();
ATD1CTL5=0x82;
ATD1CTL3=0x18;
while(ATD1STAT1_CCF2!=1){}
ADT_v=ATD1DR2L;
if(ADT_v<150) {
LED=(LED&~0x400)|0x400;
//PORTB=~0x0b;
state[0]=state[0]+1;
if(state[1]==0xff)state[1]=10;
else state[2]=10;
}
else
LED=LED&~0x400;
sensor_clear();
break;
case 12:
sensor_set();
ATD1CTL5=0x83;
ATD1CTL3=0x20;
while(ATD1STAT1_CCF3!=1){}
ADT_v=ATD1DR3L;
if(ADT_v<150) {
LED=(LED&~0x800)|0x800;
//PORTB=~0x0c;
state[0]=state[0]+1;
if(state[1]==0xff)state[1]=11;
else state[2]=11;
}
else
LED=LED&~0x800;
sensor_clear();
num1=0;
//state[0]=LED;
// PORTB=~LED; //PORTB口显示传感器情况
default:
num1=0;
}
}
/*======================PWM======================*/
void PWM_init(void){
PTP=0;
DDRP=0xff;
PORTA=(PORTA&~0x0C)|(0x08); //SET D1/D2
DDRA=(DDRA&~0x0c)|(0x0c);
PWMPOL=0x2a;
PWMCAE=0;
PWMCLK=0x2a;
PWMCTL=0x70;
PWMPRCLK=0;
PWMSCLA=4; //f_SB=100khz 8MHZ/80=100KHZ 100KHZ/200=0.5KHZ
PWMSCLB=4; //4 8MHZ/2/4=1MHZ 1MHZ/200=5KHZ=PWM's Frequency
PWME=0x00; //PWM disable
}
void PWM_01(unsigned int w,unsigned int p){
PWMPER01=p;
PWMDTY01=w;
PWME=(PWME|0x02); //PWM1使能
}
void PWM_23(unsigned int w,unsigned int p){
PWMPER23=p;
PWMDTY23=w;
PWME=(PWME|0x08); //PWM3使能
}
void PWM_45(unsigned int w,unsigned int p){
PWMPER45=p;
PWMDTY45=w;
PWME=(PWME|0x20); //PWM5使能
}
/*===============interrupt===================*/
void interrupt_init(void) {
PTT=(PTT&~0x03);
DDRT=(DDRT|0x03);
TSCR1=0x80;
TSCR2=0x07;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -