#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);
}
}