#include <msp430x14x.h>
#include "cry1602.h"
#define Num_of_Results 32
#define Num1_of_Results 32
#define DIR1 P1OUT|=BIT0;
#define DIR0 P1OUT&=~BIT0;
uchar shuzi[] = {"0123456789."};
uchar tishi[] = {"The volt is:"};
uint Error=0; // Error
uint LastError=0; // Error[-1]
uint PrevError=0; // Error[-2]
uint d_Error=0,dd_Error=0;
uint speed_change=0;
unsigned long sum;
unsigned long int Curr_Volt;
unsigned long int Curr_Volt1;
//static uint results[Num_of_Results];//保存ADC转换结果的数组 //
static uint results1[Num1_of_Results];
int Kp = 0;
int Ki = 0;
int Kd = 0;
unsigned int PWMPeriod = 200;
unsigned int PWMPeriod1= 300;
unsigned int i=0;
unsigned int j=0;
void motor_fast_go()
{
DIR1;//正转
TBCCR0=800-1;
TBCCTL1 = OUTMOD_7;
TBCCR1 = 400; //50%
TBCTL = TBSSEL_2 + MC_1; // SMCLK, up mode
_BIS_SR(LPM3_bits);// Enter LPM0
}
void motor_fast_back()
{
DIR0;//反转
TBCCR0=800-1;
TBCCTL2 = OUTMOD_7;
TBCCR2 = 400; //50%
TBCTL = TBSSEL_2 + MC_1; // SMCLK, up mode
_BIS_SR(LPM3_bits); // Enter LPM0
}
void motor_slow_go()
{
DIR1;//正转
TBCCR0=800-1;
TBCCTL3 = OUTMOD_7;
TBCCR3 = 200; //25%
TBCTL = TBSSEL_2 + MC_1; // SMCLK, up mode
_BIS_SR(LPM3_bits); // Enter LPM0
}
void motor_slow_back()
{
DIR0;//反转
TBCCR0=800-1;
TBCCTL4 = OUTMOD_7;
TBCCR4 = 200; //25%
TBCTL = TBSSEL_2 + MC_1; // SMCLK, up mode
_BIS_SR(LPM3_bits); // Enter LPM0
}
void motor_stop()
{
DIR0;//DIR为任意状态
TBCCTL5 = OUTMOD_7;
TBCCR5 = 0; //0%
TBCTL = TBSSEL_2 + MC_1; // SMCLK, up mode
_BIS_SR(LPM3_bits); // Enter LPM0
}
void delay1ms(uint z)
{
unsigned int x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void Trans1_val(uint Hex_Val1)
{
unsigned long caltmp1;
//unsigned long Curr_Volt1;
uchar t2;
uchar ptr1[4];
caltmp1 = Hex_Val1;
caltmp1 = (caltmp1 << 5) + Hex_Val1; //caltmp = Hex_Val * 33
caltmp1 = (caltmp1 << 3) + (caltmp1 << 1); //caltmp = caltmp * 10
Curr_Volt1 = caltmp1 >> 12; //Curr_Volt = clatmp / 2^n
ptr1[0] = Curr_Volt1 / 100; //Hex->Dec变换
t2 = Curr_Volt1 - (ptr1[0] * 100);
ptr1[2] = t2 / 10;
ptr1[3] = t2 - (ptr1[2] * 10);
ptr1[1] = 10; //shuzi表中第10位对应符号"."
//for(j=0;j< 4;j++);
//Disp1Char((6 + j),0,shuzi[ptr1[j]]);
}
/*PID的总调节量在26~42,则中间值在34*/
/*假设在小于45度和大于135度时,电机不转*/
int PID_Cal(int SetPoint,int NextPoint)
{
int Speed=0;
Error = SetPoint - NextPoint;
d_Error = Error - LastError;
dd_Error = d_Error - PrevError;
LastError = Error; // △e(k)
PrevError = d_Error; // △e(k)-△e(k-1)
Speed = Kp * d_Error
+ Ki * Error
+ Kd * dd_Error;
/*
if(Speed > 90)
{
Speed =90 ;
}
*/
return Speed;
}
void ADCinit()
{
LcdReset(); //复位1602液晶
Disp1Char(11,0,'V'); //显示提示信息
Disp1Char(11,1,'V'); //显示电压单位
P6SEL |= 0x0F; // 使能ADC通道
ADC12CTL0 &= ~(ENC); //一开始必须将ENC位置为0,否则无法修改ADC12CTL0中的值,此句话必须加上
ADC12CTL0 = ADC12ON+SHT0_8+MSC; // 打开ADC,设置采样时间 MSC 多次采样/准换位
ADC12CTL1 = SHP+CONSEQ_1+ADC12SSEL_1;
// ADC12CTL1 = SHP+CONSEQ_1+CSTARTADD_1;// 使用采样定时器 CONSEQ_2单通道多次转换
ADC12MCTL0 |= INCH_0;
ADC12MCTL1 |= INCH_1;
ADC12IE = BIT1+BIT2;
//ADC12IE = 0x02; // 使能ADC中断
ADC12CTL0 |= ENC; // 使能转换
ADC12CTL0 |= ADC12SC; // 开始转换
_EINT();
LPM0;
}
/************************主函数****************************/
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT,一个PWM输出,作为电机速度的调节
ADCinit();
while(1)
{
if(Curr_Volt1>0)
{
P4DIR |= 0x7e; // P4.1 - P4.6 output
P4SEL |= 0x7e; // P4.1 - P4.6 TBx options
TBCCR0 = 512-1; // PWM Period 4.1口作为输出端
TBCCTL1 = OUTMOD_7; // CCR1 reset/set
TBCCR1 = 400; // CCR1 PWM duty cycle
TBCTL = TBSSEL_1 + MC_1; // ACLK, up mode
}
}
}
#pragma vector=ADC_VECTOR
__interrupt void ADC12ISR (void)
{
//static uint index = 0;
static uint index1 = 0;
//results[index++] = ADC12MEM0;
results1[index1++] = ADC12MEM1; // Move results
if(index1 == Num1_of_Results)
{
uchar j;
unsigned long sum1 = 0;
index1 = 0;
for(j = 0; j < Num1_of_Results; j++)
{
sum1 += results1[j];
}
sum1 >>= 5; //除以32
Trans1_val(sum1);
}
}
|