if(FALSE==open) {
PORTB_PB0=1; PWMDTY67=0;
pwm_motor=(setnum[4]-cur_speed/3)*20;
if(pwm_motor>PWMPER45) PWMDTY45=PWMPER45; else if(pwm_motor>0) PWMDTY45=pwm_motor; else
PWMDTY45=0; }
else if(TRUE==open) {
PORTB_PB0=0; PWMDTY45=0;
pwm_motor=(setnum[4]-cur_speed/3)*20;
if(pwm_motor>PWMPER67) PWMDTY67=PWMPER45; else if(pwm_motor>0) PWMDTY67=pwm_motor;
else
PWMDTY67=0; } } } /*
for(;;)
{
disply_number(16,5,cur_speed/5,5);
pwm_motor=(setnum[4]-cur_speed/5)*25;
if(pwm_motor>PWMPER45) PWMDTY45=PWMPER45; else if(pwm_motor>0) PWMDTY45=pwm_motor; else
PWMDTY45=0; } } */
5.电磁车程序
#include
#include \
#include \#include \#include \
///延迟函数///
void delay(unsigned int ms) {
unsigned int i; unsigned char j; for(i=0;i for(j=0;j<100;j++); } } ///延迟函数/// #define servo_period 20 //ms #define motor_pwm_frequ 1 //khz //#define distance 10000 unsigned int SERVO_MID; //=3078 int SERVO_LIMIT=1000; // /** PWM初始化***/ void Init_PWMout(void) { PWME = 0x00; //禁止输出 0b00000000 PWMPOL = 0xff;//极性寄存器,1 先输出高电平 后为低电平 0 先输出低电平 后为高电平 0b11111111 PWMCTL = 0xf0; //01 23 45 67 通道级联 0b11110000 PWMPRCLK=0x55; //Clock A=Bus Clock/32 ; Clock B=Bus Clock/32; 0b01010101 PWMCLK=0b00000000; //选择clock:7B,6B,5A,4A,3B,2B,1A,0A PWME = 0xaa; //pp1,pp3,pp5,pp7口输出 0b10101010 PWMPER67 = 80/4*125/motor_pwm_frequ; PWMPER45 = 80/4*125/motor_pwm_frequ; PWMPER23 = 80/4*servo_period*125; PWMPER01 = 80/4*servo_period*125; PWMDTY67 =0; PWMDTY45 =0; PWMDTY23 =0; PWMDTY01 =0; } /** PWM初始化***/ ///舵机函数/// void set_servo(int servo) { if(servo>SERVO_LIMIT)servo=SERVO_LIMIT; if(servo<0-SERVO_LIMIT)servo=0-SERVO_LIMIT; PWMDTY01=SERVO_MID-servo; }