基于MSP430单片机循迹小车课程设计报告

7.附录

#include #define uchar unsigned char #define uint unsigned int void Init_PWM(void) {

TACTL = TASSEL1 + MC0 + TACLR;

//TimerA选择MCLK时钟,不分频,增计数模式,

CCTL1 = OUTMOD2 + OUTMOD1 + OUTMOD0 + CCIE; P1DIR|=0X0C; P1SEL|=0X0C;

//捕获,比较模块1选定为输出比较模式,输出模式7,禁止比较中断 CCTL2 = OUTMOD2 + OUTMOD1 + OUTMOD0 + CCIE;

//捕获,比较模块2选定为输出比较模式,输出模式7,禁止比较中断 CCR0 = 8000; }

/*******设置PWM1的占空比*******/ void Set_PWM1_Duty(uchar duty) {

if((duty >= 5) && (duty <= 95)) //将占空比限制在5% ~ 95%之间 {

CCR1 = 8000 / 100 * duty; }

else //否则将占空比强制设置为50% {

CCR1 = 4000; } }

/*******设置PWM2的占空比*******/ void Set_PWM2_Duty(uchar duty) {

if((duty >= 5) && (duty <= 95)) //将占空比限制在5% ~ 95%之间 {

CCR2 = 8000 / 100 * duty; }

else //否则将占空比强制设置为50% {

CCR2 = 4000; } }

void stop() {

P4OUT=0X0f; }

void zhizou(char D) {

if(D==1) {

P4OUT = 0X05;

Set_PWM1_Duty(50); Set_PWM2_Duty(50);

}

if(D==2) {

P4OUT = 0X0a;

Set_PWM1_Duty(50); Set_PWM2_Duty(50); } }

void right(char L) {

P4OUT = 0X05; if(L==1) {

Set_PWM1_Duty(30); Set_PWM2_Duty(70); }

if(L==2) {

Set_PWM1_Duty(30); Set_PWM2_Duty(90); } }

void left(char R) {

P4OUT = 0X05; if(R==1) {

Set_PWM1_Duty(70); Set_PWM2_Duty(30); }

if(R==2) {

Set_PWM1_Duty(90); Set_PWM2_Duty(30); } }

void main(void) {

WDTCTL=WDTPW+WDTHOLD;

P3DIR &= ~(BIT1 + BIT2 + BIT3);//选中P3.0~P3.4为采集信号输入端 P4DIR |= 0X0F;//控制轮子转向 Init_PWM(); while(1) {

switch(P3IN & 0X0E) {

case 0x0A:zhizou(1);break; //case 0x1c:left(2);break;

case 0x02:case 0x06:left(1);break; //case 0x07:right(2);break;

case 0x08:case 0x0c:right(1);break; case 0x00:stop();break; default:zhizou(1); break; } } }

联系客服:779662525#qq.com(#替换为@) 苏ICP备20003344号-4