注册会员
- 积分
- 175
- 威望
- 123
- 贡献
- 40
- 兑换币
- 0
- 注册时间
- 2012-6-27
- 在线时间
- 6 小时
|
我是大二的,蛋什么都不懂,但对这个很感兴趣,在学校拿一辆老车在试着玩,能把一个好点的光电程序给我一下吗,我不会程序?谢谢了!我的邮箱huoqiang333@126.com
学长给了我一程序,可我看不懂
#include <hidef.h>
#include <MC9S12XS128.h>
#include <stdio.h>
#pragma LINK_INFO DERIVATIVE "mc9s12xs128"
char unsigned bit_change (char unsigned in);
void pwminit(void);
void Timer_init(void);
void sysinit( void );
void handle( int angle0 );
void MotorDrive ( int ss );
unsigned char sensor_in(void);
int keypress(void);
void ShowSpeed(void);
void WaitStart(void);
#define SERVO_CENTER 1450
unsigned int timer01 = 0,timer02 = 0,HandleTimer=0;
#pragma CODE_SEG NON_BANKED
void interrupt 7 RTI(void){
timer01++;
timer02++;
HandleTimer++;
CRGFLG = 0x80;
}
#pragma CODE_SEG DEFAULT
void pwminit(void){
PWME_PWME1=0;
PWMCTL_CON01=1;
PWMCAE_CAE1=0;
PWMCNT01 = 0;
PWMPOL_PPOL2=1; //先输出高电平,计数到DTY时,反转电平
PWMPRCLK_PCKA = 0; //clockA不分频; clockA=busclock=8MHz;
PWMSCLA = 1; //对clockSA 进行2分频;PWM = clockSA=clockA/2=4MHz;
PWMDTY01 = 250; //高电平时间为0.0625ms;
PWMPER01 = 499 ; //周期0.125ms;8KHz;
PWMCLK_PCLK1 = 1; //选择clock SA做时钟源;
PWME_PWME1 = 1; //开启PWM
PWME_PWME3=0;
PWMCTL_CON23=1; //2和3联合成16位PWM;
PWMCAE_CAE3=0; //选择输出模式为左对齐输出模式
PWMCNT23 = 0; //计数器清零;
PWMPOL_PPOL3=1; //先输出高电平,计数到DTY时,反转电平
PWMPRCLK_PCKB = 0; //clockB不分频; clockB=busclock=8MKHz;
PWMSCLB = 4; //对clockSB 进行2分频;PWM = clockSB=clockB/8=1MHz;
PWMDTY23 = SERVO_CENTER; //高电平时间为1.5ms;
PWMPER23 = 20000 ; //周期20ms;50Hz;
PWMCLK_PCLK3 = 1; //选择clock SB做时钟源;
PWME_PWME3 = 1; //开启PWM PWM2输出接TA7257P的1脚,PWM2零时TA7257P的2脚接地反转。
}
void Timer_init(void){
asm sei; //关所有中断
TIOS=0X00; //通道0为输入捕捉方式
TSCR1=0X80;//定时器使能,正常工作
TSCR2=0X01;//禁止定时器溢出中断,预分频系数为2
TCTL4=0X02;//仅捕捉下降沿
TIE=0X01; //允许通道0中断
TFLG1=0X01;//清除通道0中断标志
RTICTL = 0x1f;
CRGINT = 0x80;
asm cli;//开中断
}
void sysinit( void ){
DDRA = 0x00;
DDRE = 0x0f;
DDRT = 0x00;
DDRJ = 0x00;
DDRP_DDRP0=0x01;
DDRB = 0xff;
Timer_init();
}
unsigned char sensor_in(void){
return PORTA;;
}
int keypress(void){
return !PTJ_PTJ6;
}
void handle( int angle0 ){
static Lh=-100;
if (angle0>450) angle0=450;
if (angle0<-450) angle0=-450;
PWMDTY23 = SERVO_CENTER + angle0;
}
void MotorDrive ( int ss ){
if ( ss > 0 ){
PTP_PTP0 = 1;
}
else{
PTP_PTP0 = 0;
ss=-ss;
}
PWMDTY01 = (100-ss)*5;
}
void main(void){
unsigned char in1;
int h=0,s=-30,lh;
sysinit();
pwminit();
EnableInterrupts;
MotorDrive(0);
handle(-100);
while(!keypress());
timer02=0;
while (1){
/*28084720沈小姐
28084726*/
in1=sensor_in();
PORTB=PORTA;
if (in1&0x01) {
h=330;
}
if (in1&0x02) {
h=250;
}
if (in1&0x04) {
h=70;
}
if (in1&0x08) {
h=0;
}
if (in1&0x10) {
h=-250;
}
if (in1&0x20) {
h=-330;
handle(h);
s=-20;
while((in1&0x30)!=0x10){
in1=sensor_in();
MotorDrive(s);
}
}
if (in1&0x80) {
h=-280;
}
if (in1&0x40) {
h=-380;
handle(h);
s=-20;
while((in1&0xc0)!=0x80){
in1=sensor_in();
MotorDrive(s);
}
}
if (h>-120 && h<120){
if (timer02>500)
s=-25;
else
s=-15;
} else {
s=-10;
timer02=0;
}
if(!in1 & 0x0f){
lh=0;
h=lh;
}
lh=h;
MotorDrive(s);
handle(h);
}
}
|
|