微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > msp430单片机模拟IIC总线读取MMA7660三轴加速度传感器

msp430单片机模拟IIC总线读取MMA7660三轴加速度传感器

时间:11-27 来源:互联网 点击:

if (X_value_final>

if(X_value_final<0)

P1OUT&=~ BIT0;

else _nop();

//------------------------------

TimerA_UART_print("X_value---->");

output((unsigned int )X_value,4);

TimerA_UART_print(" ");

//------------------------------

TimerA_UART_print("Y_value---->");

output((unsigned int )Y_value,4);

TimerA_UART_print(" ");

//------------------------------

TimerA_UART_print("Z_value---->");

output((unsigned int )Z_value,4);

TimerA_UART_print("rn");

//------------------------------

P1OUT ^= BIT6 ;

//delay_us(20000);

}

}

//-------------------华丽的分割线-----------------------------------------------------------------

//file2 : g2452uart.h

#include "msp430g2452.h"

//------------------------------------------------------------------------------

// Hardware-related definitions

//------------------------------------------------------------------------------

#define UART_TXD 0x02 // TXD on P1.1 (Timer0_A.OUT0)

#define UART_RXD 0x04 // RXD on P1.2 (Timer0_A.CCI1A)

//------------------------------------------------------------------------------

// Conditions for 9600 Baud SW UART, SMCLK = 1MHz

//------------------------------------------------------------------------------

#define UART_TBIT_div_2 (1000000 / (9600 * 2))

#define UART_TBIT (1000000 / 9600)

//------------------------------------------------------------------------------

// Global variables used for full-duplex UART communication

//------------------------------------------------------------------------------

unsigned int txData; // UART internal variable for TX

unsigned char rxBuffer; // Received UART character

//------------------------------------------------------------------------------

// 串口通信的接口函数说明:

//------------------------------------------------------------------------------

void TimerA_UART_init(void);

//TimerA_UART_init()模拟串口通信初始化函数,在main()函数中调用此函数就可以使用一下两个函数了

void TimerA_UART_tx(unsigned char byte);

//TimerA_UART_tx( byte ) 通过串口发送一字节的数据

void TimerA_UART_print(char *string);

//TimerA_UART_print(*string) 通过串口发送任意字节的字符穿

void output(unsigned int num , int n );

//输出一个数字

//注:接收的一字节数据存在rxBuffer变量里。

void Port1_Init()

{

P1OUT = 0x00; // Initialize all GPIO

P1SEL = UART_TXD + UART_RXD; // Timer function for TXD/RXD pins

// P1DIR = 0xFF & ~UART_RXD; // Set all pins but RXD to output

//设置TXD为输出模式,P1.1和P1.2两个调试小灯也为输出模式。RXD为输入模式。

P1DIR |= UART_TXD+BIT0+BIT6;

P1DIR &=~ UART_RXD;

P1OUT &=~ BIT0+BIT6 ; //初始化让两个灯都灭。

}

//------------------------------------------------------------------------------

// Function configures Timer_A for full-duplex UART operation

//------------------------------------------------------------------------------

void TimerA_UART_init(void)

{

Port1_Init(); //P1端口初始化!

TACCTL0 = OUT; // Set TXD Idle as Mark = 1

TACCTL1 = SCS + CM1 + CAP + CCIE; // Sync, Neg Edge, Capture, Int

TACTL = TASSEL_2 + MC_2; // SMCLK, start in continuous mode

}

//------------------------------------------------------------------------------

// Outputs one byte using the Timer_A UART

//------------------------------------------------------------------------------

void TimerA_UART_tx(unsigned char byte)

{

while (TACCTL0 & CCIE); // Ensure last char got TXd

TACCR0 = TAR; // Current state of TA counter

TACCR0 += UART_TBIT; // One bit time till first bit

TACCTL0 = OUTMOD0 + CCIE; // Set TXD on EQU0, Int

txData = byte; // Load global variable

txData |= 0x100; // Add mark stop bit to TXData

txData <= 1; // Add space start bit

}

//------------------------------------------------------------------------------

// Prints a string over using the Timer_A UART

//--------------------

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top