msp430驱动GY-273程序

[复制链接]
855|2
 楼主| dirtwillfly 发表于 2017-10-24 21:29 | 显示全部楼层 |阅读模式
该资料为msp430单片机驱动GY-273的驱动程序,能够准确的测量方位角,并对部分语句做了详细的解释,适合新手下载参考

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
 楼主| dirtwillfly 发表于 2017-10-24 21:30 | 显示全部楼层
  1. //main.c
  1. #include  <msp430x42x0.h>
  2. #include  <MSP430_IIC.h>
  3. #include "math.h"
  4. #include  <math.h>
  5. #include  <ymath.h>

  6. void LCD_CHECK(void);
  7. void Disp_BCD( unsigned long Value);


  8. #define a 0x01



  9. #define b 0x02
  10. #define c 0x04
  11. #define d 0x80
  12. #define e 0x40
  13. #define f 0x10
  14. #define g 0x20
  15. #define PI  3.141592653589793238462643
  16. const char char_gen[] =
  17. {                                     // definitions for digits
  18.   a+b+c+d+e+f,                              // Displays "0"
  19.   b+c,                                      // Displays "1"
  20.   a+b+d+e+g,                                // Displays "2"
  21.   a+b+c+d+g,                                // Displays "3"
  22.   b+c+f+g,                                  // Displays "4"
  23.   a+c+d+f+g,                                // Displays "5"
  24.   a+c+d+e+f+g,                              // Displays "6"
  25.   a+b+c,                                    // Displays "7"
  26.   a+b+c+d+e+f+g,                            // Displays "8"
  27.   a+b+c+d+f+g,                              // Displays "9"
  28.   
  29. // 0,                                        // Blank
  30. // 0,                                        // Blank
  31. // 0,                                        // Blank
  32. // 0,                                        // Blank
  33. // 0,                                        // Blank
  34. // 0                                         // Blank
  35. a+b+f+g,                                     //a
  36. c+d+e+f+g,                                   //b
  37. a+d+e+f,                                     //c
  38. b+c+d+e+g,                                   //d
  39. a+d+e+f+g,                                   //e
  40. a+e+f+g,                                     //f
  41. };

  42. //主函数部分
  43.   
  44. void main(void)
  45. {
  46.   
  47.   volatile unsigned int i;
  48.   WDTCTL = WDTPW + WDTHOLD;                 // Stop WDT
  49.   FLL_CTL0 |= XCAP14PF;                     // Set load cap for 32k xtal
  50.   // LFXT1 startup delay、
  51.   
  52.   
  53.   do
  54.   {
  55.     IFG1 &= ~OFIFG;                         // Clear OSCFault flag
  56.     for (i = 0x47FF; i > 0; i--);           // Time for flag to set
  57.   }
  58.   while (IFG1 & OFIFG);                     // OSCFault flag still set?
  59.   // Basic Timer
  60.   BTCTL = BT_ADLY_1000;                 // 1s BT Interrupt
  61.   IE2 |= BTIE;                          // Enable BT interrupt
  62.   
  63.   // Ports
  64.    
  65.   // Ports
  66.   P1OUT = 0;                            // All P1.x reset
  67.   P1IES = 0xC0;                         // P1.0, P1.1 中断边缘选择为从高到低。其他为低到高
  68.   P1DIR |=0x3F;                         // P1.0/1 = input (switches)p1.7 p1.6为输入,其他为输出


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

  94. //------------------------------------------------------------------------------
  95. // LCD Test Code LCD 测试程序
  96. //------------------------------------------------------------------------------

  97. void LCD_CHECK()
  98. {

  99.   volatile unsigned int i;
  100.   int j;
  101.   const unsigned int delay = 10000;         // SW delay for LCD stepping

  102.   for( j = 0; j < 7; j ++)
  103.   {
  104.     LCDMEM[j] = 0;                   // Clear LCD
  105.   }

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

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

  112.   for( j = 0; j < 7; j ++)
  113.   {
  114.     LCDMEM[j] = 0;                   // Clear LCD
  115.   }

  116. }
  117. //***************************十进制数的显示********************************//
  118. void Disp_BCD( unsigned long Value)
  119. //void Disp_BCD(int Value)
  120. {
  121.   LCDM1=0x00;
  122.   LCDM2=0x00;
  123.   LCDM3=0x00;
  124.   LCDM4=0x00;
  125.   LCDM5=0x00;
  126.   LCDM6=0x00;
  127.   
  128.   char *pLCD = (char *)&LCDM1;//指向lcd显示存储区
  129.   
  130.   int ge = 0;
  131.   
  132.   while(Value!=0)
  133.   {
  134.     ge=Value%10;
  135.     *pLCD++ = char_gen[ge];
  136.     Value = Value/10;
  137.   }
  138.   
  139. }

  140. void Disp_BCD_X(unsigned long Value)

  141. {
  142.   char *pLCD = (char *)&LCDM1;
  143.   int i;

  144.   for (i = 0; i < 6; i++)                       // Process 7 digits
  145.   {
  146.     *pLCD++ = char_gen[Value & 0x0f];           // Segments to LCD
  147.     Value >>= 4;                                // Process next digit
  148.   }
  149. }
  150. //******************************角度计算********************************//
  151. #pragma vector=BASICTIMER_VECTOR
  152. __interrupt void basic_timer(void)
  153. {

  154.      unsigned int x,y;
  155.      double angle_f;
  156.      unsigned long angle;
  157.      unsigned long temp;
  158.      int temp_x,temp_y,temp_xe,temp_ye;
  159.      double Axis_x,Axis_y;
  160.      double result;
  161.      
  162.      Init_QMC5883();
  163.    
  164.      while(1)
  165.      {      
  166.         
  167.         
  168.          Read_NByte_Randomaddress(inbuffer,6,0x03);
  169.        // Read_NByte_Randomaddress(inbuffer,6,0x00);
  170.        // Read_1Byte_Randomaddress(inbuffer,1,0x12);
  171.         x=(inbuffer[1]<<8)|inbuffer[0];//Combine MSB and LSB of X Data output register
  172.         y=(inbuffer[5]<<8)|inbuffer[4]; //Combine MSB and LSB of Y Data output register
  173.         
  174.         Disp_BCD_X(y);
  175.         
  176.         if(x>0xf800) x-=0xf800;          
  177.         if(y>0xf800) y-=0xf800;
  178.                          
  179.        // x=0x0000;
  180.       //  y=0x0689;
  181.         //angle_f=atan2((double)y,(double)x)*(180/3.14159265)+180;//单位:角度 (0~360)
  182.         
  183.         temp_x=x&0x8000;
  184.         temp_y=y&0x8000;
  185.         
  186.         Axis_x=x&0x7fff;//去除符号位
  187.         Axis_y=y&0x7fff;//去除符号位
  188.         
  189.        //angle_f= atan2( Axis_y,Axis_x)* (180 / PI);
  190.         
  191.         if( temp_y==0x8000)//y是负数
  192.         {
  193.           if(temp_x==0x8000)//若x是负数
  194.             {
  195.               angle_f= atan2( Axis_y,Axis_x) * (180 /PI)+180;  
  196.             }
  197.             else
  198.             {
  199.               angle_f= 360-atan2( Axis_y,Axis_x) * (180 / PI);
  200.             }
  201.          
  202.         }
  203.         else//y是正数
  204.         {
  205.             if( temp_x==0x8000)//若x是负数
  206.             {
  207.             
  208.               angle_f= 180-atan2( Axis_y,Axis_x) * (180 / PI);
  209.               
  210.             }
  211.             else
  212.             {
  213.               
  214.               angle_f= atan2( Axis_y,Axis_x) * (180 / PI);
  215.             }
  216.          
  217.         }
  218.       
  219.         angle=angle_f*1000;
  220.         //Disp_BCD( angle);
  221.         LCDM4|=0x08;
  222.       
  223.        // Disp_BCD(Axis_y);
  224.       
  225.        // Disp_BCD(Axis_x);
  226.          
  227.      }
  228.      
  229. }      



 楼主| dirtwillfly 发表于 2017-10-24 21:31 | 显示全部楼层
MSP430_IIC.h
  1. #include <msp430x42x0.h>
  2. typedef unsigned char uchar;
  3. typedef unsigned int  uint;
  4. #define IIC_IOinit  P1DIR &=~BIT2;P1DIR |= BIT0+BIT1;P1OUT |= BIT0+BIT1;//端口初始化

  5. #define deviceaddress  0x3c

  6. //#define deviceaddress  0x1a

  7. #define SCL_H P1OUT |= BIT0
  8. #define SCL_L P1OUT &=~BIT0
  9. #define SDA_H P1OUT |= BIT1
  10. #define SDA_L P1OUT &=~BIT1

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

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

  15. #define TRUE    1
  16. #define FALSE   0

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


  18. /********************************************/
  19. /*******************************************
  20. 函数名称:delay
  21. 功    能:延时约5us的时间
  22. 参    数:无
  23. 返回值  :无
  24. ********************************************/
  25. void delay(void)
  26. {
  27.         uchar i;
  28.         for (i = 0; i < 50; i++)
  29.          ;
  30. }

  31. /*void Delay(uint t)
  32. {
  33.         while (t--);
  34. }*/

  35. /*******************************************
  36. 函数名称:start
  37. 功    能:完成IIC的起始条件操作
  38. 参    数:无
  39. 返回值  :无
  40. ********************************************/
  41. void start(void)
  42. {
  43.         SCL_H;
  44.         
  45.         SDA_H;
  46.         delay();
  47.         SDA_L;
  48.         delay();
  49.         SCL_L;
  50.         delay();
  51. }
  52. /*******************************************
  53. 函数名称:stop
  54. 功    能:完成IIC的终止条件操作
  55. 参    数:无
  56. 返回值  :无
  57. ********************************************/
  58. void stop(void)
  59. {
  60.         SDA_L;
  61.         delay();
  62.         SCL_H;
  63.         delay();
  64.         SDA_H;
  65.         delay();
  66. }
  67. /*******************************************
  68. 函数名称:MACK
  69. 功    能:完成IIC的主机应答操作
  70. 参    数:无
  71. 返回值  :无
  72. ********************************************/
  73. void mack(void)
  74. {
  75.         SDA_L;
  76.         _NOP(); _NOP();
  77.         SCL_H;
  78.         delay();
  79.         SCL_L;
  80.         _NOP(); _NOP();
  81.         SDA_H;
  82.         delay();
  83. }
  84. /*******************************************
  85. 函数名称: MNACK
  86. 功    能:完成IIC的主机无应答操作
  87. 参    数:无
  88. 返回值  :无
  89. ********************************************/
  90. void mnack(void)
  91. {
  92.         SDA_H;
  93.         _NOP(); _NOP();
  94.         SCL_H;
  95.         delay();
  96.         SCL_L;
  97.         _NOP(); _NOP();
  98.         SDA_L;
  99.         delay();
  100. }

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

  112.         SDA_H;
  113.         _NOP(); _NOP();
  114.         SCL_H;
  115.         _NOP(); _NOP();
  116.         SDA_in;
  117.         _NOP(); _NOP();
  118.         slaveack = SDA_val;   //读入SDA数值
  119.         SCL_L;
  120.         delay();
  121.         SDA_out;
  122.         if (slaveack)   
  123.           return FALSE;
  124.         else           
  125.           return TRUE;
  126. }
  127. /*******************************************
  128. 函数名称:write1
  129. 功    能:向IIC总线发送一个1
  130. 参    数:无
  131. 返回值  :无
  132. ********************************************/
  133. void write1(void)
  134. {
  135.         SDA_H;
  136.         delay();
  137.         SCL_H;
  138.         delay();
  139.         SCL_L;
  140.         delay();
  141. }
  142. /*******************************************
  143. 函数名称:write0
  144. 功    能:向IIC总线发送一个0
  145. 参    数:无
  146. 返回值  :无
  147. ********************************************/
  148. void write0(void)
  149. {
  150.         SDA_L;
  151.         delay();
  152.         SCL_H;
  153.         delay();
  154.         SCL_L;
  155.         delay();
  156. }
  157. /*******************************************
  158. 函数名称:write1byte
  159. 功    能:向IIC总线发送一个字节的数据
  160. 参    数:wdata--写入的要发送的数据
  161. 返回值  :无
  162. ********************************************/
  163. void write1byte(uchar wdata)
  164. {
  165.         uchar i;
  166.         for (i = 8; i > 0; i--)
  167.         {
  168.                 if (wdata & 0x80)       
  169.                   write1();
  170.                 else                           
  171.                   write0();
  172.                 wdata <<= 1;
  173.         }

  174.         SDA_H;
  175.         _NOP();
  176. }

  177. /*******************************************
  178. 函数名称:read1byte
  179. 功    能:从IIC总线读取一个字节
  180. 参    数:无
  181. 返回值  :读取的数据
  182. ********************************************/

  183. uchar read1byte(void)
  184. {
  185.         uchar  rdata = 0x00, i;
  186.         uchar flag;

  187.         for (i = 0; i < 8; i++)
  188.         {
  189.                 SDA_H;
  190.                 delay();
  191.                 SCL_H;
  192.                 SDA_in;
  193.                 delay();
  194.                 flag = SDA_val;
  195.                 rdata <<= 1;
  196.                 if (flag)       
  197.                   rdata |= 0x01;
  198.                 SDA_out;
  199.                 SCL_L;
  200.                 delay();
  201.         }

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

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

  214.         for (i = 0; i < n; i++)
  215.         {
  216.                 inbuffer[i] = read1byte();
  217.                 if (i<(n-1))       
  218.                   mack();
  219.                 else            
  220.                   mnack();
  221.                // inbuffer[0] = 0x56;
  222.                // inbuffer[1] = 0x56;
  223.         }

  224.         stop();
  225. }

  226. /*******************************************
  227. 函数名称:delay_1ms
  228. 功    能:延时约6ms,等待EEPROM完成内部写入
  229. 参    数:无
  230. 返回值  :无
  231. ********************************************/
  232. void delay_1ms(void)
  233. {
  234.         uint i = 50000;
  235.         //while(j--)
  236.         while (i--)
  237.                 _NOP();
  238. }
  239. /*******************************************
  240. 函数名称:Write_1Byte
  241. 功    能:向EEPROM中写入1个字节的数据
  242. 参    数:Wdata--写入的数据
  243. dataaddress--数据的写入地址
  244. 返回值  :写入结果:1--成功,0--失败
  245. *********************************************/
  246. uchar Write_1Byte(uchar wdata,uchar dataaddress)
  247. {
  248.         start();
  249.         write1byte(deviceaddress);
  250.         if (check())
  251.                 write1byte(dataaddress);//dataaddress寄存器的地址
  252.         else
  253.                 return 0;
  254.         if (check())
  255.                 write1byte(wdata);
  256.         else
  257.                 return 0;
  258.         if (check())            
  259.              stop();
  260.          else            
  261.             return 0;

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

  265. /*******************************************
  266. 函数名称:Read_1Byte_Randomaddress
  267. 功    能:从EEPROM的指定地址读取1个字节的数据
  268. 参    数:dataaddress--数据读取的地址
  269. 返回值  :读取的数据
  270. *********************************************/
  271. uchar Read_1Byte_Randomaddress(uchar * readbuf, uchar n,uchar dataaddress)
  272. {
  273.         start();
  274.         write1byte(deviceaddress);
  275.         if (check())
  276.                 write1byte(dataaddress);
  277.         else
  278.                 return 0;
  279.         if (check())
  280.         {
  281.                 start();
  282.                 write1byte(deviceaddress | 0x01);
  283.         }
  284.         else
  285.                 return 0;
  286.         if (check())
  287.                 readNbyte(readbuf, n);
  288.         else
  289.                 return 0;

  290.         return 1;
  291. }

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

  301. {
  302.         start();
  303.         write1byte(deviceaddress);
  304.         if (check())
  305.                 write1byte(dataaddress);
  306.         else
  307.                 return 0;
  308.         if (check())
  309.         {
  310.                 start();
  311.                 write1byte(deviceaddress | 0x01);
  312.         }
  313.         else
  314.                 return 0;
  315.         if (check())
  316.                 readNbyte(readbuf, n);
  317.         else
  318.                 return 0;

  319.         return 1;
  320. }

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

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

  329.        
  330.                
  331. }


您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

1200

主题

35134

帖子

1122

粉丝
快速回复 在线客服 返回列表 返回顶部