先留着,有时间写
/* Includes ------------------------------------------------------------------*/
#include "stm8s.h"
#include "math.h"
#include "stdio.h"
/* Private defines -----------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#ifdef _RAISONANCE_
#define PUTCHAR_PROTOTYPE int putchar (char c)
#define GETCHAR_PROTOTYPE int getchar (void)
#elif defined (_COSMIC_)
#define PUTCHAR_PROTOTYPE char putchar (char c)
#define GETCHAR_PROTOTYPE char getchar (void)
#else /* _IAR_ */
#define PUTCHAR_PROTOTYPE int putchar (int c)
#define GETCHAR_PROTOTYPE int getchar (void)
#endif /* _RAISONANCE_ */
#define LED_GPIO_PORT (GPIOA)
#define LED_GPIO_PINS (GPIO_PIN_2)
#define APx_GPIO_PORT (GPIOD)
#define APx_GPIO_PINS (GPIO_PIN_3|GPIO_PIN_5|GPIO_PIN_6)
#define AP1_GPIO_PINS (GPIO_PIN_6)
#define AP2_GPIO_PINS (GPIO_PIN_5)
#define AP3_GPIO_PINS (GPIO_PIN_3)
#define OC1_GPIO_PORT (GPIOD)
#define OC1_GPIO_PINS (GPIO_PIN_4)
#define I2C_GPIO_PORT (GPIOA)
#define I2C_GPIO_PINS (GPIO_PIN_1|GPIO_PIN_3)
#define SDA_GPIO_PINS (GPIO_PIN_1)
#define SCL_GPIO_PINS (GPIO_PIN_3)
#define SET_SDA_OUT() {GPIO_Init(I2C_GPIO_PORT, (GPIO_Pin_TypeDef)SDA_GPIO_PINS, GPIO_MODE_OUT_OD_LOW_FAST);}
#define SET_SDA_IN() {GPIO_Init(I2C_GPIO_PORT, (GPIO_Pin_TypeDef)SDA_GPIO_PINS, GPIO_MODE_IN_FL_NO_IT);}
#define SDA_1 GPIO_WriteHigh(I2C_GPIO_PORT,SDA_GPIO_PINS)
#define SDA_0 GPIO_WriteLow(I2C_GPIO_PORT,SDA_GPIO_PINS)
#define SCL_1 GPIO_WriteHigh(I2C_GPIO_PORT,SCL_GPIO_PINS)
#define SCL_0 GPIO_WriteLow(I2C_GPIO_PORT,SCL_GPIO_PINS)
#define SDA_read GPIO_ReadInputPin(I2C_GPIO_PORT,SDA_GPIO_PINS)
#define slaveaddress 0x30 //write
#define regaddress 0x31 //read
#define DEVICE_ID 0Xf9
long int cnt,cnt_litte;
char LED_on;
int xVal,yVal,zVal;
int xUart,yUart,zUart;
double Atemp=0.9;
void delays(long int tp);
unsigned int AccelX,AccelY,AccelZ,SVM;
void Delay_5us(void);
unsigned char I2C_RecvByte();
unsigned char I2C_SendByte(unsigned char sendDAT);
void I2C_SendACK(unsigned char ack);
void I2C_Stop(void);
void I2C_Start(void);
void Delay (uint16_t nCount);
void bma250_write_reg(unsigned char addr,unsigned char val);
void bma250_init(void);
unsigned char bma250_read_reg(unsigned char addr);
void blink(void);
void main(void)
{
//char ans;
#ifdef uart1s
/*High speed internal clock prescaler: 1*/
CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);
UART1_DeInit();
/* UART1 configuration ------------------------------------------------------*/
/* UART1 configured as follow:
- BaudRate = 115200 baud
- Word Length = 8 Bits
- One Stop Bit
- No parity
- Receive and transmit enabled
- UART1 Clock disabled
*/
UART1_Init((uint32_t)115200, UART1_WORDLENGTH_8D, UART1_STOPBITS_1, UART1_PARITY_NO,
UART1_SYNCMODE_CLOCK_DISABLE, UART1_MODE_TXRX_ENABLE);
#endif
//printf("\n\rUART1 Example :retarget the C library printf()/getchar() functions to the UART\n\r");
//printf("\n\r振动传感器程序测试:\n\r");
#ifdef uart1s
GPIO_Init(APx_GPIO_PORT, (GPIO_Pin_TypeDef)AP3_GPIO_PINS, GPIO_MODE_OUT_PP_LOW_FAST);
#else
GPIO_Init(APx_GPIO_PORT, (GPIO_Pin_TypeDef)APx_GPIO_PINS, GPIO_MODE_OUT_PP_LOW_FAST);
#endif
/* Initialize I/Os in Output Mode */
GPIO_Init(LED_GPIO_PORT, (GPIO_Pin_TypeDef)LED_GPIO_PINS, GPIO_MODE_OUT_OD_LOW_FAST);
GPIO_Init(OC1_GPIO_PORT, (GPIO_Pin_TypeDef)OC1_GPIO_PINS, GPIO_MODE_OUT_PP_LOW_FAST);
GPIO_Init(I2C_GPIO_PORT, (GPIO_Pin_TypeDef)I2C_GPIO_PINS, GPIO_MODE_OUT_OD_LOW_FAST);
#ifdef uart1s
GPIO_WriteHigh(LED_GPIO_PORT,LED_GPIO_PINS);
GPIO_WriteLow(APx_GPIO_PORT,AP3_GPIO_PINS);
GPIO_WriteLow(OC1_GPIO_PORT,OC1_GPIO_PINS);
#else
GPIO_WriteHigh(LED_GPIO_PORT,LED_GPIO_PINS);
GPIO_WriteLow(APx_GPIO_PORT,AP1_GPIO_PINS);
GPIO_WriteLow(APx_GPIO_PORT,AP2_GPIO_PINS);
GPIO_WriteLow(APx_GPIO_PORT,AP3_GPIO_PINS);
GPIO_WriteLow(OC1_GPIO_PORT,OC1_GPIO_PINS);
#endif
bma250_init();
/*
printf("\n\rCheck Chip ID...\n\r");
if(bma250_read_reg(0x00) == DEVICE_ID)
{
printf("\n\rbma250 id读取正确\n\r");
printf("\n\rid为: 0x%02x\n\r",bma250_read_reg(0x00));
GPIO_WriteLow(LED_GPIO_PORT,LED_GPIO_PINS);
}else
{
printf("\n\rbma250 id错误\n\r");
printf("\n\rid为: 0x%02x\n\r",bma250_read_reg(0x00));
GPIO_WriteHigh(LED_GPIO_PORT,LED_GPIO_PINS);
}
printf("\n\r三轴加速度读取:\n\r");
{
int acc_x,x_temp1,x_temp2;
x_temp1 = (bma250_read_reg(0x02) >> 6) ;
x_temp2 = bma250_read_reg(0x03);
x_temp1 = x_temp1 | (x_temp2 << 2);
x_temp2 = x_temp2 >> 6;
acc_x = x_temp1 + (x_temp2 << 8);
printf("\n\rACC_X = %d\n\r",acc_x);
}
{
int acc_y,y_temp1,y_temp2;
y_temp1 = (bma250_read_reg(0x04) >> 6);
y_temp2 = bma250_read_reg(0x05);
y_temp1 = y_temp1 | (y_temp2 << 2);
y_temp2 = y_temp2 >> 6;
acc_y = y_temp1 + (y_temp2 << 8);
printf("\n\rACC_Y = %d\n\r",acc_y);
}
{
int acc_z,z_temp1,z_temp2;
z_temp1 = (bma250_read_reg(0x06) >> 6);
z_temp2 = bma250_read_reg(0x07);
z_temp1 = z_temp1 | (z_temp2 << 2);
z_temp2 = z_temp2 >> 6;
acc_z = z_temp1 + (z_temp2 << 8);
printf("\n\rACC_Z = %d\n\r",acc_z);
}
printf("\n\r温度读取:\n\r");
{
char temperature;
temperature = bma250_read_reg(0x08);
temperature = 24 + (temperature / 2);
printf("\n\r当前温度 = %d °C\n\r",temperature);
}
printf("\n\r状态检测:\n\r");
{
unsigned char temp_status,flat,orient,s_tap,d_tap,slope,high,low;
temp_status = bma250_read_reg(0x09);
((temp_status & (1<<0)) == (1<<0))?(low = 1):(low = 0);
((temp_status & (1<<1)) == (1<<1))?(high = 1):(high = 0);
((temp_status & (1<<2)) == (1<<2))?(slope = 1):(slope = 0);
((temp_status & (1<<4)) == (1<<4))?(d_tap = 1):(d_tap = 0);
((temp_status & (1<<5)) == (1<<5))?(s_tap = 1):(s_tap = 0);
((temp_status & (1<<6)) == (1<<6))?(orient = 1):(orient = 0);
((temp_status & (1<<7)) == (1<<7))?(flat = 1):(flat = 0);
printf("\n\r low低重力中断状态 = %d \n\r",low);
printf("\n\r high高重力中断状态 = %d \n\r",high);
printf("\n\r slope倾斜中断状态 = %d \n\r",slope);
printf("\n\r d_tap双击中断状态 = %d \n\r",d_tap);
printf("\n\r s_tap单击状态 = %d \n\r",s_tap);
printf("\n\r orient中断状态 = %d \n\r",orient);
printf("\n\r flat水平中断状态 = %d \n\r",flat);
}
printf("\n\rTap及倾斜检测:\n\r");
{
unsigned char temp_status,tap_sign,tap_first_z,tap_first_y,tap_first_x,slope_sign,slope_first_z,slope_first_y,slope_first_x;
temp_status = bma250_read_reg(0x0B);
((temp_status & (1<<0)) == (1<<0))?(slope_first_x = 1):(slope_first_x = 0);
((temp_status & (1<<1)) == (1<<1))?(slope_first_y = 1):(slope_first_y = 0);
((temp_status & (1<<2)) == (1<<2))?(slope_first_z = 1):(slope_first_z = 0);
((temp_status & (1<<3)) == (1<<3))?(slope_sign = 1):(slope_sign = 0);
((temp_status & (1<<4)) == (1<<4))?(tap_first_x = 1):(tap_first_x = 0);
((temp_status & (1<<5)) == (1<<5))?(tap_first_y = 1):(tap_first_y = 0);
((temp_status & (1<<6)) == (1<<6))?(tap_first_z = 1):(tap_first_z = 0);
((temp_status & (1<<7)) == (1<<7))?(tap_sign = 1):(tap_sign = 0);
printf("\n\r x方向首次倾斜状态 = %d \n\r",slope_first_x);
printf("\n\r y方向首次倾斜状态 = %d \n\r",slope_first_y);
printf("\n\r z方向首次倾斜状态 = %d \n\r",slope_first_z);
printf("\n\r 倾斜信号状态 = %d \n\r",slope_sign);
printf("\n\r x方向首次点击状态 = %d \n\r",tap_first_x);
printf("\n\r y方向首次点击状态 = %d \n\r",tap_first_y);
printf("\n\r z方向首次点击状态 = %d \n\r",tap_first_z);
printf("\n\r 点击信号状态 = %d \n\r",tap_sign);
}
*/
//printf("\n\r带宽设置:\n\r");
//00xx
//01000 0x08 7.81Hz
//01001 0x09 15.63Hz
//01010 0x0a 31.25Hz
//01011 0x0b 62.5Hz
//01100 0x0c 125Hz
//01101 0x0d 250Hz
//01110 0x0e 500Hz
//01111 0x0f 1000Hz
// 1xxxx 1000Hz
bma250_write_reg(0x10,0x0F);
#ifdef uart1s
if(bma250_read_reg(0x10) == 0x0F)
{
printf("\n\r 当前带宽为1000Hz,设置成功 \n\r");
}else
{
printf("\n\r 设置失败! \n\r");
}
#endif
cnt=0;
LED_on = 0;
while(1)
{
{
int acc_x,acc_y,acc_z;
int x_temp1,y_temp1,z_temp1;
int x_temp2,y_temp2,z_temp2;
x_temp1 = bma250_read_reg(0x02) & 0xc0;
x_temp2 = bma250_read_reg(0x03);
y_temp1 = bma250_read_reg(0x04) & 0xc0;
y_temp2 = bma250_read_reg(0x05);
z_temp1 = bma250_read_reg(0x06) & 0xc0;
z_temp2 = bma250_read_reg(0x07);
acc_x = x_temp1 + (x_temp2 << 8);
acc_y = y_temp1 + (y_temp2 << 8);
acc_z = z_temp1 + (z_temp2 << 8);
acc_x = acc_x / 64;
acc_y = acc_y / 64;
acc_z = acc_z / 64;
acc_x = (int)(acc_x * 3.9);
acc_y = (int)(acc_y * 3.9);
acc_z = (int)(acc_z * 3.9);
xVal = (int)((Atemp * xVal) + ((1-Atemp) * acc_x));
yVal = (int)((Atemp * yVal) + ((1-Atemp) * acc_y));
zVal = (int)((Atemp * zVal) + ((1-Atemp) * acc_z));
xUart = acc_x - xVal;
yUart = acc_y - yVal;
zUart = acc_z - zVal;
xUart = xUart + yUart + zUart;
yUart = 100;
zUart = -100;
//修改闪烁/长亮时间
if(cnt > 500)
{
LED_on = 0;
GPIO_WriteLow(OC1_GPIO_PORT,OC1_GPIO_PINS);
GPIO_WriteHigh(LED_GPIO_PORT,LED_GPIO_PINS);
GPIO_WriteLow(APx_GPIO_PORT,AP1_GPIO_PINS);
GPIO_WriteLow(APx_GPIO_PORT,AP2_GPIO_PINS);
GPIO_WriteLow(APx_GPIO_PORT,AP3_GPIO_PINS);
}
#define blink
#ifdef blink
//闪烁灯 频率
if(cnt_litte > 10)
{
cnt_litte = 0;
GPIO_WriteReverse(OC1_GPIO_PORT,OC1_GPIO_PINS);
GPIO_WriteReverse(LED_GPIO_PORT,LED_GPIO_PINS);
GPIO_WriteReverse(APx_GPIO_PORT,AP1_GPIO_PINS);
GPIO_WriteReverse(APx_GPIO_PORT,AP2_GPIO_PINS);
GPIO_WriteReverse(APx_GPIO_PORT,AP3_GPIO_PINS);
}
#endif
//常亮灯
//将#define blink注释
if((xUart > 100) || (xUart < (-100)))
{
if(LED_on == 0)
{
LED_on = 1;
cnt=0;
cnt_litte=0;
GPIO_WriteHigh(OC1_GPIO_PORT,OC1_GPIO_PINS);
GPIO_WriteLow(LED_GPIO_PORT,LED_GPIO_PINS);
GPIO_WriteHigh(APx_GPIO_PORT,AP1_GPIO_PINS);
GPIO_WriteHigh(APx_GPIO_PORT,AP2_GPIO_PINS);
GPIO_WriteLow(APx_GPIO_PORT,AP3_GPIO_PINS);
}
}
if(LED_on == 1)
{
cnt = cnt + 3;
cnt_litte = cnt_litte + 3;
}
#ifdef uart1s
printf("outX: %d ",xUart);
printf("outY: %d ",yUart);
printf("outZ: %d \r\n",zUart);
#endif
}
}
}
void delays(long int tp)
{
long int a,b;
a=tp;b=tp;
for(;a>0;a--)
for(;b>0;b--);
}
PUTCHAR_PROTOTYPE
{
/* Write a character to the UART1 */
UART1_SendData8(c);
/* Loop until the end of transmission */
while (UART1_GetFlagStatus(UART1_FLAG_TXE) == RESET);
return (c);
}
GETCHAR_PROTOTYPE
{
#ifdef _COSMIC_
char c = 0;
#else
int c = 0;
#endif
/* Loop until the Read data register flag is SET */
while (UART1_GetFlagStatus(UART1_FLAG_RXNE) == RESET);
c = UART1_ReceiveData8();
return (c);
}
void bma250_write_reg(unsigned char addr,unsigned char val)
{
I2C_Start();
I2C_SendByte(slaveaddress);
I2C_SendByte(addr);
I2C_SendByte(val);
I2C_Stop();
}
void bma250_init(void)
{
bma250_write_reg(0x0F,0x03); //加速度范围为+-2g
bma250_write_reg(0x10,0x08); //传输带宽为7.81HZ,64MS
bma250_write_reg(0x09,0x00); //清中断位??
bma250_write_reg(0x16,0xff); //允许中断
bma250_write_reg(0x17,0xff); //允许中断
}
unsigned char bma250_read_reg(unsigned char addr)
{
unsigned char temp = 0;
I2C_Start();
temp = I2C_SendByte(slaveaddress);
temp = I2C_SendByte(addr);
I2C_Start();
temp = I2C_SendByte(regaddress);
temp = I2C_RecvByte();
I2C_Stop();
return temp;
}
unsigned char I2C_RecvByte()
{
unsigned char i;
unsigned char revDAT = 0;
SDA_1; // latch the Data port befor reading
SET_SDA_IN();
for (i=0; i<8; i++)
{
revDAT <<= 1;
SCL_1;
asm("nop");
asm("nop");
if (SDA_read)
{
revDAT |= 0x01;
}
else
{
revDAT &= 0xfe;
}
Delay_5us();
SCL_0;
Delay_5us();
}
SET_SDA_OUT();
return revDAT;
}
unsigned char I2C_SendByte(unsigned char sendDAT)
{
unsigned char i,revack;
for (i= 0 ; i< 8; i++)
{
SCL_0;
Delay_5us();
if (sendDAT & 0x80) // write data
{
SDA_1;
}
else
{
SDA_0;
}
sendDAT <<= 1;
SCL_1;
Delay_5us();
}
SCL_0;
Delay_5us();
SET_SDA_IN();
SCL_1;
asm("nop");
asm("nop");
revack = (unsigned char)SDA_read;
Delay_5us();
SCL_0;
SET_SDA_OUT();
Delay_5us();
return revack;
}
void I2C_SendACK(unsigned char ack)
{
if(ack == 0)
{
SDA_0;
}
else
{
SDA_1;
}
SCL_1;
Delay_5us();
SCL_0;
Delay_5us();
}
void I2C_Stop(void)
{
// SDA 0->1 while SCL High
SDA_0;
SCL_1;
Delay_5us();
SDA_1;
Delay_5us();
}
void I2C_Start(void)
{
// SDA 1->0 while SCL High
SDA_1;
SCL_1;
Delay_5us();
SDA_0;
Delay_5us();
SCL_0;
}
void Delay(uint16_t nCount)
{
/* Decrement nCount value */
while (nCount != 0)
{
nCount--;
}
}
void Delay_5us(void)
{
unsigned char i; //fcpu 8MHz 时
for (i=50; i>0; i--);
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval : None
*/
void assert_failed(u8* file, u32 line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* Infinite loop */
while (1)
{
}
}
#endif
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/