采样和电机驱动

2019-07-18 15:07发布

不用全部看,各路大神只需根据我出现的问题看看关键部分就行了。
#include  <msp430x14x.h>
#include "BoardConfig.h"
#include  "cry1602.h"
#include  "cry1602.c“
uchar shuzi[] = {"0123456789."};
uchar tishi[] = {"The volt is:"};
uchar xs1[]={"1:"};
uchar xs2[]={"2:"};
uchar xs3[]={"3:"};
uchar xs4[]={"4:"};
int result0;
int result1;
int result2;
int result3;
int c1,c2;
uchar m,n;
uchar  FFW[8]={0xFE,0xFC,0xFD,0xF9,0xFB,0xF3,0xF7,0xF6};//正转数组
uchar  REV[8]={0xF6,0xF7,0xF3,0xFB,0xF9,0xFD,0xFC,0xFE};//反转数组
void delay( uint y)
{
while(y!=0x00)
y--;

}
void Trans_val(uint Hex_Val)
{
    unsigned long caltmp;
    uint Curr_Volt;
    uchar t1,i;
    uchar ptr[4];
   
    caltmp = Hex_Val;
    caltmp = (caltmp << 5) + Hex_Val;           //caltmp = Hex_Val * 33
    caltmp = (caltmp << 3) + (caltmp << 1);     //caltmp = caltmp * 10
    Curr_Volt = caltmp >> 12;                   //Curr_Volt = caltmp / 2^n
    ptr[0] = Curr_Volt / 100;                   //Hex->Dec变换
    t1 = Curr_Volt - (ptr[0] * 100);
    ptr[2] = t1 / 10;
    ptr[3] = t1 - (ptr[2] * 10);
    ptr[1] = 10;                                //shuzi表中第10位对应符号"."
    //在液晶上显示变换后的结果
    for(i = 0;i < 4;i++)
      Disp1Char((6 + i),1,shuzi[ptr[i]]);
}
#pragma vector=ADC_VECTOR
__interrupt void ADC12ISR (void)
{
  result0=ADC12MEM0;
  result1=ADC12MEM1;
  result2=ADC12MEM2;
  result3=ADC12MEM6;
  c1=result0-result1;
  c2=result2-result3;
  delay(100);
  DispNChar(2,0,2,xs1);
  Trans_val(result0);
  delay(100);
  DispNChar(2,0,2,xs2);
  Trans_val(result1);
  delay(50);
  DispNChar(2,0,2,xs3);
  Trans_val(result2);
  delay(50);
  DispNChar(2,0,2,xs4);
  Trans_val(result3);
  
    if(c1<0)
    {
      P3DIR=0x0f;
      for(m=0;m<8;m++)
      {
        P3OUT=FFW[m];
        delay(1000);
      }
    }
    else
    {
      if(c1>0)
      {
        P3DIR=0x0f;
        for(m=0;m<8;m++)
        {
          P3OUT=REV[m];
          delay(1000);
        }
      }
      else
      {
        P3DIR=0x00;
      }
    }
    if(c2<0)
    {
      P4DIR=0x0f;
      for(n=0;n<8;n++)
      {
        P4OUT=FFW[n];
        delay(1000);
      }
    }
    else
    {
      if(c2>0)
      {
        P4DIR=0x0f;
        for(n=0;n<8;n++)
        {
          P4OUT=REV[n];
          delay(1000);
        }
      }
      else
      {
        P4DIR=0x00;
      }
    }
}


//保存ADC转换结果的数组                                                     // is not used for anything.
           

/************************主函数****************************/

void main(void)
{
  WDTCTL = WDTPW+WDTHOLD;                   //关闭看门狗
  
  /*下面六行程序关闭所有的IO口*/
    P1DIR = 0XFF1OUT = 0XFF;
    P2DIR = 0XFF2OUT = 0XFF;
    P3DIR = 0XFF3OUT = 0XFF;
    P4DIR = 0XFF4OUT = 0XFF;
    P5DIR = 0XFF5OUT = 0XFF;
    P6DIR = 0XFF6OUT = 0XFF;
  
  //P6DIR |= BIT26OUT |= BIT2;              //关闭电平转换
  while(1)
  {
  LcdReset();                               //复位1602液晶
  DispNChar(2,0,12,tishi);                  //显示提示信息
  Disp1Char(11,1,'V');                      //显示电压单位
  P6SEL |= 0x47;                            // 使能ADC通道
  ADC12CTL0 = ADC12ON+SHT0_2+MSC;           // 打开ADC,设置采样时间
  ADC12CTL1 = SHP+CONSEQ_3;
  ADC12MCTL0=INCH_0;            //channel=A0
  ADC12MCTL1=INCH_1;            //channel=A1
  ADC12MCTL2=INCH_2;            //channel=A2
  ADC12MCTL6=INCH_6+EOS;
   delay(100);
  // 使用采样定时器
  ADC12IE = 0x01;                           // 使能ADC中断
  ADC12CTL0 |= ENC;                         // 使能转换
  ADC12CTL0 |= ADC12SC;                     // 开始转换
  _EINT();
  LPM0;
  }程序功能呢说明:四路AD采样,然后根据两个AD采集的型号差值来驱动电机。为了分别测试,我用液晶显示采集回来的电压值。
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。