/*******************************************************************************
* Copyright (C), 2017, XiaCheDan Tech. Co.,
Ltd.
* FileName: main.c
* Author: littleshrimp
* Version : 1.0
* Date:
2017-1-22 11:13
* Description:
* Function List: //
* 1. -------
* History:
* <author> <time> <version > <desc>
* littleshrimp 2017-1-22 11:13 1.0
build this moudle
******************************************************************************/
/*
MSP430FR5969
-----------------
| P2.0/UCA0TXD|----> PC
| |
| |
| P2.1/UCA0RXD|<---- PC
| |
SCL --o--|P3.5 |
| |
SDA --o--|P3.6 |
*/
/*******************************************************************************
* *
INCLUDES
* */
#include
"msp430.h"
#include
<stdio.h>
#include
<stdint.h>
#include
"uart.h"
#include
"soft_i2c.h"
#include
"timer.h"
/*******************************************************************************
* *
MACROS
* */
/*******************************************************************************
* *
CONSTANTS
* */
#define
BASE64 0
#define
STRING 0
/*******************************************************************************
* *
TYPEDEFS
* */
/*******************************************************************************
* *
GLOBAL VARIABLES
* */
/*******************************************************************************
* *
EXTERNAL VARIABLES
* */
/*******************************************************************************
* *
EXTERNAL FUNCTIONS
* */
/*******************************************************************************
* *
LOCAL VARIABLES
* */
uint8_t data[3];
uint16_t temp_code;
float temperature;
uint8_t crc;
/*******************************************************************************
* *
PROFILE CALLBACKS
* */
/*******************************************************************************
* *
LOCAL FUNCTIONS
* */
/*******************************************************************************
* *
PUBLIC FUNCTIONS
* */
#define
LIS3DSH_SLAVE_ADDR (0x1E <<
1)//7bit
#define
LIS3DSH_WHO_I_AM 0x0F
#define
LIS3DSH_OUT_T 0x0C
#define
LIS3DSH_STATUS 0x27
#define
LIS3DSH_CTRL_REG4 0x20
#define
LIS3DSH_CTRL_REG3 0x23
#define
LIS3DSH_CTRL_REG5 0x24
#define
LIS3DSH_CTRL_REG6 0x25
uint8_t buf[10];
void lis3dsh_init(void)
{
uint8_t data;
i2c_init();//初始化I2C
i2c_read_n_byte(LIS3DSH_SLAVE_ADDR,LIS3DSH_WHO_I_AM,&buf[0],1);
// data = 0x17;//0001:3.125Hz 0:continuous
update 111:x,y,z enable
data = 0x97;//1001:1600Hz 0:continuous update
111:x,y,z enable
i2c_write_n_byte(LIS3DSH_SLAVE_ADDR,LIS3DSH_CTRL_REG4,&data,1);
i2c_read_n_byte(LIS3DSH_SLAVE_ADDR,LIS3DSH_OUT_T,&buf[1],1);
i2c_read_n_byte(LIS3DSH_SLAVE_ADDR,LIS3DSH_STATUS,&buf[2],7);
}
void dco_init(void)
{
// Configure one FRAM waitstate as required by the
device datasheet for MCLK
// operation beyond 8MHz _before_ configuring the clock
system.
FRCTL0 = FRCTLPW | NWAITS_1;
// Clock System Setup
CSCTL0_H = CSKEY >> 8; // Unlock CS registers
CSCTL1 = DCORSEL | DCOFSEL_4; // Set DCO to 16MHz
CSCTL2 = SELA__VLOCLK | SELS__DCOCLK | SELM__DCOCLK; // Set SMCLK = MCLK = DCO,
// ACLK = VLOCLK
CSCTL3 = DIVA__1 | DIVS__1 | DIVM__1;
// Set all
dividers
CSCTL0_H = 0; // Lock CS registers
}
int main( void )
{
int16_t x = 0,y = 0,z = 0;
uint8_t status;
char inBuf[64];//base64缓存
char outBuf[64];//base64缓存
WDTCTL = WDTPW + WDTHOLD;//停止看门狗
PM5CTL0 &= ~LOCKLPM5;//这个是一定要加的,不然GPIO不能使用
dco_init();
lis3dsh_init();
uart_init();
while(1)
{
i2c_read_n_byte(LIS3DSH_SLAVE_ADDR,LIS3DSH_STATUS,&buf[2],7);
#if (BASE64 ==
1) //base 64输出
base64_encode((const uint8_t *)(&buf[2]),outBuf,7); //做BASE64转换
uart_tx_string(outBuf);//输出到PC
uart_tx_string("\r\n");//换行
#elif (STRING
== 1) //字符串输出 status = buf[2];//status
x = buf[3] + (buf[4] << 8);
y = buf[5] + (buf[6] << 8);
z = buf[7] + (buf[8] << 8);
sprintf(outBuf,"lis3dsh data out
x=%d,y=%d,z=%d\r\n",x,y,z);
uart_tx_string(outBuf);//输出到PC
uart_tx_string("\r\n");//换行
sleep(650);
#else //二进制输出
outBuf[0] = 0x55;
outBuf[1] = buf[4];
outBuf[2] = buf[3];
outBuf[3] = buf[6];
outBuf[4] = buf[5];
outBuf[5] = buf[8];
outBuf[6] = buf[7];
// if(x++ > 1000) x = 0;
// if(y++ > 10000) y = 0;
// if(z++ > 30000) y = 0;
// outBuf[0] = 0x55;
// outBuf[1] =x>>8;
// outBuf[2] =x;
// outBuf[3] = y>>8;
// outBuf[4] = y;
// outBuf[5] = z>>8;
// outBuf[6] = z;
uart_tx_bytes(outBuf,7);//输出到PC
#endif
//SPCI2C_Readpt(SPC1000AddrR,0x07,mPress,1); //获取气压数据高字节:3位
}
}
|