|
求助大神,毕设遇阻,救命啊
具体是这样的 我做一个直流电机的调速 用的是msp430g2553的板子
P1.4 1.5控制电机正反转
P1.6输出pwm波调速
p1.3连接槽型光耦模块测速
问题是程序写好后电机不转了。。也没法串口通讯。。。悲剧了
求大神帮忙看看 跪谢!!!
程序如下
#include"msp430g2553.h"
#include "msp430.h"
#define uchar unsigned char
#define uint unsigned int
char js=' ';
void UartPutchar(double c);
unsigned char UartGetchar();
uchar flag;
uint n;
uint irtime;
unsigned long int t;
void pinlv();
//===========P1口的初始化==========//
//====P1.3为中断输入口======//
void initport()
{
P1OUT=BIT3;
P1REN=BIT3;
P1IE|=BIT3;
P1IES|=BIT3;//触发方式选择
P1IFG=0x00;
P1DIR |= BIT4+BIT5+BIT6;
P1OUT|=BIT4;
P1OUT&=~BIT5;
P1SEL= BIT6;
P1SEL = BIT1 + BIT2; // P1.1为 RXD, P1.2为TXD 接收数据引脚 发送数据引脚
P1SEL2 = BIT1 + BIT2; // P1.1为 RXD, P1.2为TXD
}
//===========OVER==================//
//=========定时器的初始化===========//
void init()
{
n=0;
TA1CTL=TASSEL_2+MC_1;//SMCLK upmode
TA1CCTL0=CCIE;
TA1CCR0=800;//100us
TA0CTL=TASSEL_2+TACLR+MC_1;
TA0CCR0 = 10000;
TA0CCTL1 = OUTMOD_7;
TA0CCR1 = 3000;
}
//===============OVER=============//
//=============main==============================//
void main( void )
{
WDTCTL = WDTPW + WDTHOLD;
BCSCTL2 = SELM_0 + DIVM_0 + DIVS_0;//分频?SMCLK==>8MHZ
BCSCTL1 = CALBC1_8MHZ; /* Set DCO to 8MHz */
DCOCTL = CALDCO_8MHZ;
UCA0CTL1 |= UCSSEL_2; // 选择时钟BRCLK
UCA0BR0 =65; // 8MHz 9600
UCA0BR1 = 3; // 8MHz 9600
UCA0MCTL = UCBRS1; // 波特率=BRCLK/(UBR+(M7+...0)/8)
UCA0CTL1 &= ~UCSWRST;
IE2 |= UCA0RXIE; // 使能接收中断
initport();
init();
_EINT();
while(1)
{
if(flag==1)
{
pinlv();
flag=0;
}
js=UartGetchar();
if(js=='o')
{
while(!(js=='o'));
P1OUT^=BIT4|BIT5;
}
if(js=='s')
{
while(!(js=='s'));
P1OUT^=BIT4;
js=' ';
}
if (js=='k')
{
while(!(js=='k'));
if (TA0CCR1>=10000)
TA0CCR1=1000;
else TA0CCR1+=1000;
js=' ';
}
if (js=='m')
{
while(!(js=='m'));
if (TA0CCR1<=1000)
TA0CCR1=9000;
else TA0CCR1-=1000;
js=' ';
}
}
}
//==========main OVER=================//
//=========100us定时中断服务函数=======//
#pragma vector=TIMER1_A0_VECTOR
__interrupt void timer(void)
{
irtime++;
if(irtime==10000)//1s
{
irtime=0;
flag=1;
}
}
//===============接收端中断服务=================//
#pragma vector=PORT1_VECTOR
__interrupt void Port (void)
{
if(P1IFG&BIT3)
{
n++;
t=n/2*60;
P1IFG&=~BIT3;
}
}
//============频率计===========//
void pinlv()
{
n=0;
}
/**********************************UART接收中断*************************/
#pragma vector=USCIAB0RX_VECTOR
__interrupt void USCI0RX_ISR(void)
{
while (!(IFG2&UCA0TXIFG)); // 等待发送完成
UCA0TXBUF = UCA0RXBUF; // TX ->RXed character
IFG2 &=~UCA0RXIFG;
//UartPutchar('a');
//temp=UCA0RXBUF;
}
/******************************UART发送字节函数*************************/
void UartPutchar(double c)
{
//while(!(IFG2 & UCA0TXIFG)); //待发送为空
UCA0TXBUF=c;
//IFG2 &=~UCA0RXIFG;
}
/*****************************UART接收字节函数**************************/
unsigned char UartGetchar()
{
unsigned char c;
while(!(IFG2 & UCA0RXIFG)); //等待接收完成
c=UCA0RXBUF;
IFG2 &=~UCA0TXIFG;
return c;
}
|
|