打印

msp430驱动GY-273程序

[复制链接]
721|2
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
dirtwillfly|  楼主 | 2017-10-24 21:29 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
沙发
dirtwillfly|  楼主 | 2017-10-24 21:30 | 只看该作者
//main.c
#include  <msp430x42x0.h>
#include  <MSP430_IIC.h>
#include "math.h"
#include  <math.h>
#include  <ymath.h>

void LCD_CHECK(void);
void Disp_BCD( unsigned long Value);


#define a 0x01



#define b 0x02
#define c 0x04
#define d 0x80
#define e 0x40
#define f 0x10
#define g 0x20
#define PI  3.141592653589793238462643
const char char_gen[] =
{                                     // definitions for digits
  a+b+c+d+e+f,                              // Displays "0"
  b+c,                                      // Displays "1"
  a+b+d+e+g,                                // Displays "2"
  a+b+c+d+g,                                // Displays "3"
  b+c+f+g,                                  // Displays "4"
  a+c+d+f+g,                                // Displays "5"
  a+c+d+e+f+g,                              // Displays "6"
  a+b+c,                                    // Displays "7"
  a+b+c+d+e+f+g,                            // Displays "8"
  a+b+c+d+f+g,                              // Displays "9"
  
// 0,                                        // Blank
// 0,                                        // Blank
// 0,                                        // Blank
// 0,                                        // Blank
// 0,                                        // Blank
// 0                                         // Blank
a+b+f+g,                                     //a
c+d+e+f+g,                                   //b
a+d+e+f,                                     //c
b+c+d+e+g,                                   //d
a+d+e+f+g,                                   //e
a+e+f+g,                                     //f
};

//主函数部分
  
void main(void)
{
  
  volatile unsigned int i;
  WDTCTL = WDTPW + WDTHOLD;                 // Stop WDT
  FLL_CTL0 |= XCAP14PF;                     // Set load cap for 32k xtal
  // LFXT1 startup delay、
  
  
  do
  {
    IFG1 &= ~OFIFG;                         // Clear OSCFault flag
    for (i = 0x47FF; i > 0; i--);           // Time for flag to set
  }
  while (IFG1 & OFIFG);                     // OSCFault flag still set?
  // Basic Timer
  BTCTL = BT_ADLY_1000;                 // 1s BT Interrupt
  IE2 |= BTIE;                          // Enable BT interrupt
  
  // Ports
   
  // Ports
  P1OUT = 0;                            // All P1.x reset
  P1IES = 0xC0;                         // P1.0, P1.1 中断边缘选择为从高到低。其他为低到高
  P1DIR |=0x3F;                         // P1.0/1 = input (switches)p1.7 p1.6为输入,其他为输出


  
  P6OUT = 0;                            // All P6.x reset
  P6DIR = 0xFF;                         // All P6.x outputs
  P5SEL  = 0x1C;                            // Set COM pins for LCD
  
  P1IE |=BIT6+BIT7;
  // LCD_A
  LCDACTL = LCDON + LCD4MUX + LCDFREQ_128;   // 4mux LCD, ACLK/128,LCDFREQ_128选择lcd的频率
  LCDAPCTL0 = 0x0F;                          // Segs S0-S15 = outputs
  LCDAVCTL0 = LCDCPEN;                       // Enable LCDA charge pump选择内部电荷泵
  LCDAVCTL1 = VLCD_3_26;                          // to 3.26V
  LCD_CHECK();
  Init_QMC5883();
// repeat forever  
  while (1)                  
  {
    IFG2 &= ~BTIFG;                         // Clear pending interrupt,无中断等待
    IE2 |= BTIE;               
    // Enable BT interrupt
    // wait for event, hold CPU in low-power mode 3
    __bis_SR_register(LPM3_bits + GIE);//进入低功耗模式3,同时打开通用中断使能位
    __disable_interrupt();
    IE2 &= ~BTIE;                           // Disable BT interrupt
  }
}

//------------------------------------------------------------------------------
// LCD Test Code LCD 测试程序
//------------------------------------------------------------------------------

void LCD_CHECK()
{

  volatile unsigned int i;
  int j;
  const unsigned int delay = 10000;         // SW delay for LCD stepping

  for( j = 0; j < 7; j ++)
  {
    LCDMEM[j] = 0;                   // Clear LCD
  }

  for( j = 0; j < 7; j ++) //共0~7个寄存器
  {
    LCDMEM[j] = 0xFF;                // All segments on
    for (i = delay; i>0; i--);              // Delay
  }

  for (i = delay; i>0; i--);                // Delay

  for( j = 0; j < 7; j ++)
  {
    LCDMEM[j] = 0;                   // Clear LCD
  }

}
//***************************十进制数的显示********************************//
void Disp_BCD( unsigned long Value)
//void Disp_BCD(int Value)
{
  LCDM1=0x00;
  LCDM2=0x00;
  LCDM3=0x00;
  LCDM4=0x00;
  LCDM5=0x00;
  LCDM6=0x00;
  
  char *pLCD = (char *)&LCDM1;//指向lcd显示存储区
  
  int ge = 0;
  
  while(Value!=0)
  {
    ge=Value%10;
    *pLCD++ = char_gen[ge];
    Value = Value/10;
  }
  
}

void Disp_BCD_X(unsigned long Value)

{
  char *pLCD = (char *)&LCDM1;
  int i;

  for (i = 0; i < 6; i++)                       // Process 7 digits
  {
    *pLCD++ = char_gen[Value & 0x0f];           // Segments to LCD
    Value >>= 4;                                // Process next digit
  }
}
//******************************角度计算********************************//
#pragma vector=BASICTIMER_VECTOR
__interrupt void basic_timer(void)
{

     unsigned int x,y;
     double angle_f;
     unsigned long angle;
     unsigned long temp;
     int temp_x,temp_y,temp_xe,temp_ye;
     double Axis_x,Axis_y;
     double result;
     
     Init_QMC5883();
   
     while(1)
     {      
        
        
         Read_NByte_Randomaddress(inbuffer,6,0x03);
       // Read_NByte_Randomaddress(inbuffer,6,0x00);
       // Read_1Byte_Randomaddress(inbuffer,1,0x12);
        x=(inbuffer[1]<<8)|inbuffer[0];//Combine MSB and LSB of X Data output register
        y=(inbuffer[5]<<8)|inbuffer[4]; //Combine MSB and LSB of Y Data output register
        
        Disp_BCD_X(y);
        
        if(x>0xf800) x-=0xf800;          
        if(y>0xf800) y-=0xf800;
                         
       // x=0x0000;
      //  y=0x0689;
        //angle_f=atan2((double)y,(double)x)*(180/3.14159265)+180;//单位:角度 (0~360)
        
        temp_x=x&0x8000;
        temp_y=y&0x8000;
        
        Axis_x=x&0x7fff;//去除符号位
        Axis_y=y&0x7fff;//去除符号位
        
       //angle_f= atan2( Axis_y,Axis_x)* (180 / PI);
        
        if( temp_y==0x8000)//y是负数
        {
          if(temp_x==0x8000)//若x是负数
            {
              angle_f= atan2( Axis_y,Axis_x) * (180 /PI)+180;  
            }
            else
            {
              angle_f= 360-atan2( Axis_y,Axis_x) * (180 / PI);
            }
         
        }
        else//y是正数
        {
            if( temp_x==0x8000)//若x是负数
            {
            
              angle_f= 180-atan2( Axis_y,Axis_x) * (180 / PI);
              
            }
            else
            {
              
              angle_f= atan2( Axis_y,Axis_x) * (180 / PI);
            }
         
        }
      
        angle=angle_f*1000;
        //Disp_BCD( angle);
        LCDM4|=0x08;
      
       // Disp_BCD(Axis_y);
      
       // Disp_BCD(Axis_x);
         
     }
     
}      



使用特权

评论回复
板凳
dirtwillfly|  楼主 | 2017-10-24 21:31 | 只看该作者
MSP430_IIC.h
#include <msp430x42x0.h>
typedef unsigned char uchar;
typedef unsigned int  uint;
#define IIC_IOinit  P1DIR &=~BIT2;P1DIR |= BIT0+BIT1;P1OUT |= BIT0+BIT1;//端口初始化

#define deviceaddress  0x3c

//#define deviceaddress  0x1a

#define SCL_H P1OUT |= BIT0
#define SCL_L P1OUT &=~BIT0
#define SDA_H P1OUT |= BIT1
#define SDA_L P1OUT &=~BIT1

#define SDA_in  P1DIR &=~BIT1   //SDA改成输入模式
#define SDA_out P1DIR |= BIT1    //SDA变回输出模式
#define SDA_val P1IN&BIT1        //SDA位的值

#define DRDY_val P1IN&BIT2        //DRDY的位值 //传感器中断信号

#define TRUE    1
#define FALSE   0

unsigned char inbuffer[6]={0x00,0x00,0x00,0x00,0x00,0x00};


/********************************************/
/*******************************************
函数名称:delay
功    能:延时约5us的时间
参    数:无
返回值  :无
********************************************/
void delay(void)
{
        uchar i;
        for (i = 0; i < 50; i++)
         ;
}

/*void Delay(uint t)
{
        while (t--);
}*/

/*******************************************
函数名称:start
功    能:完成IIC的起始条件操作
参    数:无
返回值  :无
********************************************/
void start(void)
{
        SCL_H;
        
        SDA_H;
        delay();
        SDA_L;
        delay();
        SCL_L;
        delay();
}
/*******************************************
函数名称:stop
功    能:完成IIC的终止条件操作
参    数:无
返回值  :无
********************************************/
void stop(void)
{
        SDA_L;
        delay();
        SCL_H;
        delay();
        SDA_H;
        delay();
}
/*******************************************
函数名称:MACK
功    能:完成IIC的主机应答操作
参    数:无
返回值  :无
********************************************/
void mack(void)
{
        SDA_L;
        _NOP(); _NOP();
        SCL_H;
        delay();
        SCL_L;
        _NOP(); _NOP();
        SDA_H;
        delay();
}
/*******************************************
函数名称: MNACK
功    能:完成IIC的主机无应答操作
参    数:无
返回值  :无
********************************************/
void mnack(void)
{
        SDA_H;
        _NOP(); _NOP();
        SCL_H;
        delay();
        SCL_L;
        _NOP(); _NOP();
        SDA_L;
        delay();
}

/**********检查应答信号函数******************/
/*如果返回值为1则证明有应答信号,反之没有*/
/*******************************************
函数名称:check 等待接收方(从机)发来的应答信号
功    能:检查从机的应答操作
参    数:无
返回值  :从机是否有应答:1--有,0--无
********************************************/
uchar check(void)
{
        uchar slaveack;

        SDA_H;
        _NOP(); _NOP();
        SCL_H;
        _NOP(); _NOP();
        SDA_in;
        _NOP(); _NOP();
        slaveack = SDA_val;   //读入SDA数值
        SCL_L;
        delay();
        SDA_out;
        if (slaveack)   
          return FALSE;
        else           
          return TRUE;
}
/*******************************************
函数名称:write1
功    能:向IIC总线发送一个1
参    数:无
返回值  :无
********************************************/
void write1(void)
{
        SDA_H;
        delay();
        SCL_H;
        delay();
        SCL_L;
        delay();
}
/*******************************************
函数名称:write0
功    能:向IIC总线发送一个0
参    数:无
返回值  :无
********************************************/
void write0(void)
{
        SDA_L;
        delay();
        SCL_H;
        delay();
        SCL_L;
        delay();
}
/*******************************************
函数名称:write1byte
功    能:向IIC总线发送一个字节的数据
参    数:wdata--写入的要发送的数据
返回值  :无
********************************************/
void write1byte(uchar wdata)
{
        uchar i;
        for (i = 8; i > 0; i--)
        {
                if (wdata & 0x80)       
                  write1();
                else                           
                  write0();
                wdata <<= 1;
        }

        SDA_H;
        _NOP();
}

/*******************************************
函数名称:read1byte
功    能:从IIC总线读取一个字节
参    数:无
返回值  :读取的数据
********************************************/

uchar read1byte(void)
{
        uchar  rdata = 0x00, i;
        uchar flag;

        for (i = 0; i < 8; i++)
        {
                SDA_H;
                delay();
                SCL_H;
                SDA_in;
                delay();
                flag = SDA_val;
                rdata <<= 1;
                if (flag)       
                  rdata |= 0x01;
                SDA_out;
                SCL_L;
                delay();
        }

        return rdata;//存放着IIC发来的数据
}
/*******************************************
函数名称:readNbyte
功    能:从IIC总线读取N个字节的数据
参    数:inbuffer--读取后数据存放的首地址
n--数据的个数
返回值  :无
********************************************/

void readNbyte(uchar * inbuffer, uchar n)//n是字节的个数
{
        uchar i;

        for (i = 0; i < n; i++)
        {
                inbuffer[i] = read1byte();
                if (i<(n-1))       
                  mack();
                else            
                  mnack();
               // inbuffer[0] = 0x56;
               // inbuffer[1] = 0x56;
        }

        stop();
}

/*******************************************
函数名称:delay_1ms
功    能:延时约6ms,等待EEPROM完成内部写入
参    数:无
返回值  :无
********************************************/
void delay_1ms(void)
{
        uint i = 50000;
        //while(j--)
        while (i--)
                _NOP();
}
/*******************************************
函数名称:Write_1Byte
功    能:向EEPROM中写入1个字节的数据
参    数:Wdata--写入的数据
dataaddress--数据的写入地址
返回值  :写入结果:1--成功,0--失败
*********************************************/
uchar Write_1Byte(uchar wdata,uchar dataaddress)
{
        start();
        write1byte(deviceaddress);
        if (check())
                write1byte(dataaddress);//dataaddress寄存器的地址
        else
                return 0;
        if (check())
                write1byte(wdata);
        else
                return 0;
        if (check())            
             stop();
         else            
            return 0;

        delay_1ms();       //等待EEPROM完成内部写入
        return 1;
}

/*******************************************
函数名称:Read_1Byte_Randomaddress
功    能:从EEPROM的指定地址读取1个字节的数据
参    数:dataaddress--数据读取的地址
返回值  :读取的数据
*********************************************/
uchar Read_1Byte_Randomaddress(uchar * readbuf, uchar n,uchar dataaddress)
{
        start();
        write1byte(deviceaddress);
        if (check())
                write1byte(dataaddress);
        else
                return 0;
        if (check())
        {
                start();
                write1byte(deviceaddress | 0x01);
        }
        else
                return 0;
        if (check())
                readNbyte(readbuf, n);
        else
                return 0;

        return 1;
}

/*******************************************
函数名称:Read_NByte_Randomaddress
功    能:从EEPROM的"指定地址"读取N个字节的数据
参    数:readbuf--指向保存数据地址的指针
          n--读取数据的个数
          dataaddress--数据读取的首地址
返回值  :读取结果:1--成功,0--失败
********************************************/
uchar Read_NByte_Randomaddress(uchar * readbuf, uchar n,uchar dataaddress)

{
        start();
        write1byte(deviceaddress);
        if (check())
                write1byte(dataaddress);
        else
                return 0;
        if (check())
        {
                start();
                write1byte(deviceaddress | 0x01);
        }
        else
                return 0;
        if (check())
                readNbyte(readbuf, n);
        else
                return 0;

        return 1;
}

//初始化QMC5883,根据需要参考pdf进行修改****
void Init_QMC5883()
{
        Write_1Byte(0x10,0x00);  
        Write_1Byte(0x20,0x01);
        Write_1Byte(0x00,0x02);  

       // Write_1Byte(0x0d,0x09);  //控制寄存器配置
       // Write_1Byte(0x01,0x0b);  //设置清除时间寄存器

       
               
}


使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:欢迎进入TI MCU论坛      21ic TI技术交流1群:61549143(已满),  21ic TI技术交流2群:311421422 我的博客:http://blog.timcu.com/

1180

主题

34793

帖子

1116

粉丝