Discussing with me about my program
-
Respected sir: I am involving in a program which is written by myself about using MSP430F149 to control the step motor.I would like to show them here.( this is the datasheet of MSP430F149: [^] )And I think maybe they could be improved but I don’t know how to do it. Could you give me some suggestions? This is the program: <#include <msp430x14x.h> typedef unsigned int uint; typedef unsigned char uchar; #define PWM BIT2 void int_clk() { uchar i; BCSCTL1&=~XT2OFF; //open XT oscillator BCSCTL2|=SELM1+SELS;//MCLK 8M and SMCLK 1M do { IFG1 &= ~OFIFG; //clean the wrong sign of vibration for(i = 0; i < 100; i++) _NOP(); //delay waiting } while ((IFG1 & OFIFG) != 0); //If sign is 1,continue to wait IFG1&=~OFIFG; } void int_pwm() { P1SEL|=PWM;//choose P12 to bePWM output P1DIR|=PWM; TACCR0=800;//PWM signal cycle 10KHz TACCR1=400;//duty cycle 1:1 TACCTL1=OUTMOD0+OUTMOD1+OUTMOD2; //outputted mode choosing TACTL|=TASSEL1+MC0; } void main() { WDTCTL=WDTPW+WDTHOLD;//close watchdog int_clk(); //clock initialization int_pwm(); //initialize PWM while(1);//end> The program is base on controlling the return of step motor by using MSP4300f149. The outputted port of PWM is P1.2 while the signal cycle is 10KHZ. The duty cycle is 1:1. Do you have any advice about my program? Best wishes~
-
Respected sir: I am involving in a program which is written by myself about using MSP430F149 to control the step motor.I would like to show them here.( this is the datasheet of MSP430F149: [^] )And I think maybe they could be improved but I don’t know how to do it. Could you give me some suggestions? This is the program: <#include <msp430x14x.h> typedef unsigned int uint; typedef unsigned char uchar; #define PWM BIT2 void int_clk() { uchar i; BCSCTL1&=~XT2OFF; //open XT oscillator BCSCTL2|=SELM1+SELS;//MCLK 8M and SMCLK 1M do { IFG1 &= ~OFIFG; //clean the wrong sign of vibration for(i = 0; i < 100; i++) _NOP(); //delay waiting } while ((IFG1 & OFIFG) != 0); //If sign is 1,continue to wait IFG1&=~OFIFG; } void int_pwm() { P1SEL|=PWM;//choose P12 to bePWM output P1DIR|=PWM; TACCR0=800;//PWM signal cycle 10KHz TACCR1=400;//duty cycle 1:1 TACCTL1=OUTMOD0+OUTMOD1+OUTMOD2; //outputted mode choosing TACTL|=TASSEL1+MC0; } void main() { WDTCTL=WDTPW+WDTHOLD;//close watchdog int_clk(); //clock initialization int_pwm(); //initialize PWM while(1);//end> The program is base on controlling the return of step motor by using MSP4300f149. The outputted port of PWM is P1.2 while the signal cycle is 10KHZ. The duty cycle is 1:1. Do you have any advice about my program? Best wishes~
What kind of answer do you expect? You are mainly setting configuration registers which can't be improved. The only code portion that can be improved is the loop in your
int_clk()
function. The final clearing of theOFIFG
bits can be omitted because those are already cleared when the loop terminates. The innerfor
loop seems to be unnecessary when_NOP()
does what it indicates (just do nothing while the processor runs) and there is no requirement for a delay between consecutive writes to theIFG1
register. Then that function can be:void int_clk()
{
BCSCTL1&=~XT2OFF; //open XT oscillator
BCSCTL2|=SELM1+SELS;//MCLK 8M and SMCLK 1M
do
{
IFG1 &= ~OFIFG; //clean the wrong sign of vibration
}
while ((IFG1 & OFIFG) != 0); //If sign is 1,continue to wait
}