Commit 7051ae95 by zhanggf

initial

parents
#include "IIC.h"
//frequent 400kHZ
uint32_t T = 0;
//void Delay5us(void)//2.5us
//{
// nop();nop(); nop();nop(); nop();nop(); nop();nop();
// nop();nop(); nop();nop(); nop();nop(); nop();nop();
// //nop();nop(); nop();nop(); nop();nop();
// nop();nop(); nop();nop(); nop();nop(); nop();nop();
// nop();nop(); nop();nop(); nop();nop();
//}
void Delay5us(void)//2.5us
{
nop();nop(); nop();nop(); nop();nop(); nop();nop();
nop();nop(); nop();nop(); nop();nop(); nop();nop();
nop();nop(); nop();nop(); nop();nop(); nop();nop();
nop();nop(); nop();nop(); nop();nop();
}
//i2c pins initial
void HMC5883_InitPin(void)
{
I2C_GPIO->DDR |= (I2C_SCL_PIN)|(I2C_SDA_PIN);
I2C_GPIO->CR1 |= ((I2C_SCL_PIN)|(I2C_SDA_PIN));
I2C_GPIO->CR2 |= (I2C_SCL_PIN)|(I2C_SDA_PIN);
I2C_GPIO->ODR &= ~((I2C_SCL_PIN)|(I2C_SDA_PIN));
SCL_L;
Delay5us();
asm("NOP;");
}
bool HMC5883_Start(void)
{
SDA_H;
SCL_H;
Delay5us();
SDA_L;
Delay5us();
SCL_L;
Delay5us();
return TRUE;
}
void HMC5883_Stop(void)
{
SDA_L;
SCL_H;
Delay5us();
SDA_H;
Delay5us();
SCL_L;
Delay5us();
}
bool HMC5883_WaitACK(void)
{
SDA_H;
SDA_IN;
Delay5us();
SCL_H;
Delay5us();
if(SDA_read){SCL_L;SDA_OUT;return FALSE;}
Delay5us();
SCL_L;
SDA_OUT;
return TRUE;
}
/**************************************
发送应答信号
入口参数:ack (0:ACK 1:NAK)
**************************************/
void HMC5883_SendACK(bool ack)
{
if(ack==TRUE)SDA_H;
else{SDA_L;}
Delay5us();
SCL_H;
Delay5us();
SCL_L;
Delay5us();
}
/*I2C的NoACK函数----------------------------------------*/
void I2C_NoAck(void)
{
SCL_L;
Delay5us();
SDA_H;
Delay5us();
SCL_H;
Delay5us();
SCL_L;
Delay5us();
}
bool HMC5883_SendByte(BYTE dat)
{
BYTE i;
for (i=0; i<8; i++) //8位计数器
{
if(dat&0x80)SDA_H;
else
SDA_L;
Delay5us();
SCL_H;
Delay5us();
SCL_L;
// Delay5us();
dat<<=1;
}
asm("NOP");
if(!HMC5883_WaitACK())return FALSE;
return TRUE;
}
/**************************************
从IIC总线接收一个字节数据
**************************************/
BYTE HMC5883_RecvByte()
{
BYTE i;
BYTE dat = 0;
SDA_H;
SDA_IN;
//使能内部上拉,准备读取数据,
for (i=0; i<8; i++) //8位计数器
{
dat <<= 1;
SCL_H; //拉高时钟线
Delay5us(); //延时
if(SDA_read)dat|=0x01;//读数据
SCL_L; //拉低时钟线
Delay5us(); //延时
}
SDA_OUT;
return dat;
}
bool error_id = FALSE;
uint8_t count1 = 0;
//************************写入单字节数据***************************
bool Single_Write_HMC5883(uchar SlaveAddress,uchar REG_Address,uchar REG_data)
{
uint8_t cnt = 0;
while(cnt++<5)
{
count1++;
HMC5883_Start(); //起始信号
HMC5883_SendByte(SlaveAddress); //发送设备地址+写信号
HMC5883_SendByte(REG_Address); //内部寄存器地址,请参考中文pdf
error_id=HMC5883_SendByte(REG_data); //内部寄存器数据,请参考中文pdf
HMC5883_Stop();
if(!error_id)break;
}
asm("NOP");
if(cnt>=5)return FALSE;
else
return TRUE;
}
//************************读取内部寄存器*************************
uchar Single_Read_HMC5883(uchar SlaveAddress,uchar REG_Address)
{
uchar REG_data;
asm("NOP;");
HMC5883_Start(); //起始信号
HMC5883_SendByte(SlaveAddress); //发送设备地址+写信号
HMC5883_SendByte(REG_Address); //发送存储单元地址,从0开始
HMC5883_Start(); //起始信号
HMC5883_SendByte(SlaveAddress+1); //发送设备地址+读信号
REG_data=HMC5883_RecvByte(); //读出寄存器数据
HMC5883_SendACK(TRUE);
HMC5883_Stop(); //停止信号
return REG_data;
}
//************************读取内部寄存器*************************
/*
uchar Single_Read_HMC5883(uchar SlaveAddress,uchar REG_Address)
{
uchar REG_data;
HMC5883_Start(); //起始信号
HMC5883_SendByte(SlaveAddress); //发送设备地址+写信号
HMC5883_SendByte(REG_Address); //发送存储单元地址,从0开始
HMC5883_Start(); //起始信号
HMC5883_SendByte(SlaveAddress+1); //发送设备地址+读信号
REG_data=HMC5883_RecvByte(); //读出寄存器数据
HMC5883_SendACK(TRUE);
HMC5883_Stop(); //停止信号
return REG_data;
}
*/
void Multiple_Read_HMC5883(uchar SlaveAddress,uchar *BUFF)
{ uchar i;
HMC5883_Start(); //起始信号
HMC5883_SendByte(SlaveAddress); //发送设备地址+写信号
HMC5883_SendByte(0x00); //发送存储单元地址,从0x3开始
HMC5883_Start(); //起始信号
HMC5883_SendByte(SlaveAddress+1); //发送设备地址+读信号
for (i=0; i<6; i++) //连续读取6个地址数据,存储中BUF
{
BUFF[i] = HMC5883_RecvByte(); //BUF[0]存储数据
if (i == 5)
{
HMC5883_SendACK(TRUE); //最后一个数据需要回NOACK
}
else
{
HMC5883_SendACK(FALSE); //回应ACK
}
}
HMC5883_Stop(); //停止信号
}
//初始化HMC5883,根据需要请参考pdf进行修改****
uint8_t hhj = 0;
bool Init_HMC5883()
{
int temp = 0;
HMC5883_InitPin();
temp += Single_Write_HMC5883(SLAVE_ADD,0x0a,0x00);
temp += Single_Write_HMC5883(SLAVE_ADD,0x09,0x11);
temp += Single_Write_HMC5883(SLAVE_ADD,0x0B,0x01);
asm("NOP");
if(temp<3)return FALSE;
else
return TRUE;
}
#ifndef _IIC_H
#define _IIC_H
#include "user.h"
typedef unsigned char BYTE;
typedef unsigned short WORD;
#define uchar unsigned char
#define uint unsigned int
/*ģI2C˿붨*/
#define I2C_GPIO GPIOB
#define I2C_SCL_PIN GPIO_Pin_3
#define I2C_SDA_PIN GPIO_Pin_2
#define SCL_H (I2C_GPIO->ODR |= I2C_SCL_PIN)
#define SCL_L (I2C_GPIO->ODR &= (uint8_t)( ~I2C_SCL_PIN ))
#define SDA_H (I2C_GPIO->ODR |= I2C_SDA_PIN)
#define SDA_L (I2C_GPIO->ODR &= (uint8_t)( ~I2C_SDA_PIN ))
#define SDA_OUT GPIO_Init(I2C_GPIO,I2C_SDA_PIN,GPIO_Mode_Out_PP_High_Fast)//((I2C_GPIO->DDR)|=(uint8_t)(I2C_SDA_PIN))
#define SDA_IN GPIO_Init(I2C_GPIO,I2C_SDA_PIN,GPIO_Mode_In_PU_No_IT)//((I2C_GPIO->DDR)&=(uint8_t)(~I2C_SDA_PIN))
#define SDA_read (I2C_GPIO->IDR & (uint8_t)(I2C_SDA_PIN))
extern uint32_t T;
enum{yes=1,no=0};
void Delay5us(void);
void HMC5883_InitPin(void);
bool HMC5883_Start(void);
void HMC5883_Stop(void);
bool HMC5883_WaitACK(void);
void HMC5883_SendACK(bool ack);
bool HMC5883_SendByte(BYTE dat);
BYTE HMC5883_RecvByte();
bool Single_Write_HMC5883(uchar SlaveAddress,uchar REG_Address,uchar REG_data);
uchar Single_Read_HMC5883(uchar SlaveAddress,uchar REG_Address);
void Multiple_Read_HMC5883(uchar SlaveAddress,uchar *BUFF);
bool Init_HMC5883();
#endif
#include "QMC5883L.h"
struct QMCSTR QMC5883LStr;
static void QmcTest(void)
{
static enum QMC_STATE QmcState = IDLE;
static uint8_t buff[MagDataNum][6] = {0},qmc_dec_count = 0;
uint32_t temp[6] = {0},i=0,j=0;
switch(QmcState)
{
case qmc_init:
{
if(Init_HMC5883())
{
QmcState = qmc_dec;
qmc_dec_count = 0;
ge_state = YES;
}
else
{
ge_state = NO;
}
break;
}
case qmc_dec:
{
if((Single_Read_HMC5883(SLAVE_ADD,0x06))&0x01){;}
else
return;
Multiple_Read_HMC5883(SLAVE_ADD,buff[qmc_dec_count++]);
if(qmc_dec_count>=MagDataNum)
{
QMC5883LStr.DataOkFlag = TRUE;
for(j=0;j<6;j++)
{
for(i=1;i<MagDataNum-1;i++)temp[j] += buff[i][j];
temp[j] = temp[j]/(MagDataNum-2);
}
for(i=0;i<6;i++)QMC5883LStr.QMC_CUR_ROPORT[i] = temp[i];
for(i=0;i<3;i++)
{
QMC5883LStr.QMC_CUR[i] = QMC5883LStr.QMC_CUR_ROPORT[i*2+1];
QMC5883LStr.QMC_CUR[i]<<=8;
QMC5883LStr.QMC_CUR[i] += QMC5883LStr.QMC_CUR_ROPORT[i*2];
}
if(HC_DEBUG)UsartPrint(QMC5883LStr.QMC_CUR_ROPORT,6);
QmcState = qmc_lowpower;
}
break;
}
case qmc_lowpower:
{
Single_Write_HMC5883(SLAVE_ADD,0x0a,0x80);
SCL_H;
SDA_H;
QmcState = IDLE;
break;
}
case IDLE:
{
if(QMC5883LStr.DataOkFlag==0)QmcState = qmc_init;
break;
}
default :break;
}
}
void QmcDeal(void)
{
QmcTest();
}
////////////////////地磁检测车进入//////////////////////////////////////////
/***
输入:无
功能:地磁检测车进入
输出:无
***/
int geomag_car_in(void)
{
static uint8_t stable_count = 0;
static int tempX,tempY,tempZ,tempStableX,tempStableY,tempStableZ;
tempX = abs(QMC5883LStr.QMC_CUR[0] - QMC5883LStr.QMC_BASE[0]);
tempY = abs(QMC5883LStr.QMC_CUR[1] - QMC5883LStr.QMC_BASE[1]);
tempZ = abs(QMC5883LStr.QMC_CUR[2] - QMC5883LStr.QMC_BASE[2]);
tempStableX = abs(QMC5883LStr.QMC_CUR[0] - QMC5883LStr.QMC_BEFORE[0]);
tempStableY = abs(QMC5883LStr.QMC_CUR[1] - QMC5883LStr.QMC_BEFORE[1]);
tempStableZ = abs(QMC5883LStr.QMC_CUR[2] - QMC5883LStr.QMC_BEFORE[2]);
if(( tempX > 2*SystemPara.QmcFixPara[2])||(tempZ > 2*SystemPara.QmcFixPara[2]))//已检测到车
{
//前后二次电磁值相比较,若大于阀值,则说明变化明显,有效
if(tempStableX > 0x50||tempStableZ > 0x50)//车未停稳
{
memcpy(QMC5883LStr.QMC_BEFORE,QMC5883LStr.QMC_CUR,3*sizeof(int16_t));
stable_count = 0;
}
else //车稳了
{
stable_count++;
if(stable_count >= 6)
{
stable_count = 6;
memcpy(QMC5883LStr.QMC_STABLE,QMC5883LStr.QMC_CUR,3*sizeof(int16_t));
return 1;
}
}
}
return 0;
}
/////////////////////////////地磁检测车离开/////////////////////////////
/***
输入:first_dec:对参数进行清零;next_dec:正常检测
功能:地磁检测车离开
输出:1:车离开;0:车未离开
***/
int geomag_car_out(int flag)
{
int tempX,tempY,tempZ,tempStableX,tempStableY,tempStableZ;
static uint8_t again_change_count = 0,again_change_count1 = 0;
tempX = abs(QMC5883LStr.QMC_CUR[0] - QMC5883LStr.QMC_STABLE[0]);
tempY = abs(QMC5883LStr.QMC_CUR[1] - QMC5883LStr.QMC_STABLE[1]);
tempZ = abs(QMC5883LStr.QMC_CUR[2] - QMC5883LStr.QMC_STABLE[2]);
tempStableX = abs(QMC5883LStr.QMC_CUR[0] - QMC5883LStr.QMC_BEFORE[0]);
tempStableY = abs(QMC5883LStr.QMC_CUR[1] - QMC5883LStr.QMC_BEFORE[1]);
tempStableZ = abs(QMC5883LStr.QMC_CUR[2] - QMC5883LStr.QMC_BEFORE[2]);
/***有波动***/
if((tempX > SystemPara.QmcFixPara[2])||(tempZ > SystemPara.QmcFixPara[2]))
{
if((tempStableX > 0x50)||(tempStableZ > 0x50))
{
again_change_count1 = 0;
}
else
{
again_change_count1++;
if(again_change_count>=SystemPara.QmcFixPara[1] )
{
geo_wave_flag = 1;//有波动
}
}
}
if((tempX > 2*SystemPara.QmcFixPara[2])||(tempZ > 2*SystemPara.QmcFixPara[2]))
{
if((tempStableX > 0x50)||(tempStableZ > 0x50))
{
memcpy(QMC5883LStr.QMC_BEFORE,QMC5883LStr.QMC_CUR,3*sizeof(int16_t));
again_change_count = 0;
}
else
{
again_change_count++;
if(again_change_count>=SystemPara.QmcFixPara[1] )
{
return 1;
}
}
}
return 0;
}
/////////////////////////////地磁自适应////////////////////////////////
/***
功能:地磁自适应
***/
void geomag_self_adaption(void)
{
//set_parking_space_status=0;
//mag_time_count=0;
int mag_x_OPT,mag_y_OPT;
int mag_x_filter = QMC5883LStr.QMC_CUR[0],mag_y_filter = QMC5883LStr.QMC_CUR[2];
int CARON_magX = QMC5883LStr.QMC_BASE[0],CARON_magY = QMC5883LStr.QMC_BASE[2];
mag_x_OPT=abs(mag_x_filter-CARON_magX);
mag_y_OPT=abs(mag_y_filter-CARON_magY);
if(mag_x_OPT>0x90||mag_y_OPT>0x90)
{
if(mag_x_OPT>mag_y_OPT)
{
SystemPara.QmcFixPara[2]= mag_x_OPT/3;
}
else
SystemPara.QmcFixPara[2]= mag_y_OPT/3;
}
else if(SystemPara.QmcFixPara[2]>=45)
{
SystemPara.QmcFixPara[2]=SystemPara.QmcFixPara[2]-0x10;
}
}
//////////////////////////////
/***
功能:有车处理函数
***/
void have_car_send()
{
memcpy(QMC5883LStr.QMC_STABLE,QMC5883LStr.QMC_CUR,3*sizeof(int16_t));
get_photoresistor_ad();
ph_stable_value = VAL_PHOTO;
judge_day_night(first_dec);
ph_car_out(first_dec);
geomag_car_out(first_dec);
BeepStart(4,0);
CarRecDataStr.CarState = 1;
CarRecDataStr.CmdSource = 3;
SendData(HAVE_CAR);
}
/***
功能:无车处理函数
***/
void NO_car_deal(void)//无车处理
{
mag_time_count = 0;
BeepStart(1,1);
CarRecDataStr.CarState = 0;
CarRecDataStr.CmdSource = 2;
no_car_count ++;
if(no_car_count>=6)//无车自动升起只允许尝试5次
{
no_car_count = 6;
return;
}
geomag_car_out(first_dec);//复位内部变量
ph_car_out(first_dec);//复位内部变量
CarAction(0,0,1,1);
CarRecDataStr.CarState = 0;
CarRecDataStr.CmdSource = 2;
judge_day_night(first_dec);
SendData(HAVE_CAR);
}
#ifndef _QMC5883L_H
#define _QMC5883L_H
#include "user.h"
enum IIC_ENUM
{
start_error = 0,
send_error = 1,
recive_error = 2,
sucess = 3
};
enum QMC_STATE
{
qmc_init = 0,
qmc_dec = 1,
qmc_lowpower = 2,
qmc_printf = 3,
IDLE = 4
};
struct QMCSTR
{
bool DataOkFlag;
uint8_t QMC_DEC_CNT;
uint8_t QMC_CUR_ROPORT[6];
int16_t QMC_BASE[3];
int16_t QMC_BEFORE[3];
int16_t QMC_STABLE[3];
int16_t QMC_CUR[3];
};
static int QMC5883L_Init(void);
static enum IIC_ENUM QMC_Reg_Write(uint8_t slave_add,uint8_t reg_add,uint8_t len,uint8_t* data);
static enum IIC_ENUM QMC_Reg_Read(uint8_t slave_add,uint8_t reg_add,uint8_t len,uint8_t* data);
static bool QMC5883L_init(void);
static void QmcTest(void);
void QmcDeal(void);
#define SLAVE_ADD 0x1A
#define HC_DEBUG 0
#define IIC_TEST_DEBUG 1
#define MagDataNum 6
extern struct QMCSTR QMC5883LStr;
#endif
#include "SPI.h"
void SpiInit1( void )
{
CLK_PeripheralClockConfig(CLK_Peripheral_SPI1,ENABLE);
SPI_IO_INIT;
SPI_DeInit(SPI1);
SPI0_DELAY;
SPI_Init(SPI1,SPI_FirstBit_MSB,SPI_BaudRatePrescaler_16,SPI_Mode_Master,SPI_CPOL_Low,SPI_CPHA_1Edge,SPI_Direction_2Lines_FullDuplex,SPI_NSS_Soft,0);
SPI_Cmd(SPI1,ENABLE);
// SPI1->CR2 = 0x01;
// SPI1->CR1 = 0x5C;
// SPI1->CR1 |= 0x40;
}
uint8_t SpiInOut( uint8_t outData )
{
u8 cc=0;
/* Send SPIy data */
//SPI_SendData( SPI1, outData );
SPI1->DR = outData;
while(!(SPI1->SR&0x02));
while(!(SPI1->SR&0x01));
cc= SPI1->DR;//SPI_ReceiveData( SPI1 );
SPI1->SR &= ~0x01;
return cc;
}
uint8_t regTemp=0,dataTemp=0,temp3=0;
uint8_t spi0ReadWrite(uint8_t reg,uint8_t data,enum DataDeal deal)
{
SPI0_SCS_L;
SPI0_DELAY;
dataTemp = data;
regTemp = reg;
if(deal==write)
{
regTemp |= 0x80;
}
else if(deal==read)
{
regTemp &= 0x7F;
dataTemp = 0xff;
}
SpiInOut(regTemp);
temp3 = SpiInOut(dataTemp);
SPI0_SCS_H;
return temp3;
}
uint8_t temp2;
void spiTest(void)
{
if(DelayTime)return;
DelayTime = 1;
temp2 = spi0ReadWrite(REG_LR_VERSION,0xff,read);;//SpiInOut(0xa5);
SPI0_DELAY;
//UsartPrint(&temp2,1);
}
#ifndef _SPI_H
#define _SPI_H
#include "user.h"
#define SPI_GPIO1 GPIOD
#define SPI_RESET_PIN GPIO_Pin_4
#define SPI_GPIO2 GPIOB
#define SPI_NSS_PIN GPIO_Pin_3
#define SPI_SCK_PIN GPIO_Pin_5
#define SPI_MOSI_PIN GPIO_Pin_6
#define SPI_MISO_PIN GPIO_Pin_7
#define SPI0_SCS_H {GPIO_WriteBit(SPI_GPIO2,SPI_NSS_PIN,SET);}
#define SPI0_SCS_L {GPIO_WriteBit(SPI_GPIO2,SPI_NSS_PIN,RESET);}
//#define SPI0_NSS_H {GPIO_WriteBit(SPI_GPIO2,SPI_NSS_PIN,SET);}
//#define SPI0_NSS_H {GPIO_WriteBit(SPI_GPIO2,SPI_NSS_PIN,SET);}
#define SPI_IO_INIT { GPIO_Init(SPI_GPIO2,SPI_NSS_PIN,GPIO_Mode_Out_PP_High_Fast);GPIO_WriteBit(SPI_GPIO2,SPI_NSS_PIN,TRUE);\
GPIO_Init(SPI_GPIO2,SPI_SCK_PIN,GPIO_Mode_Out_PP_High_Fast);\
GPIO_Init(SPI_GPIO2,SPI_MOSI_PIN,GPIO_Mode_Out_PP_High_Fast);\
GPIO_Init(SPI_GPIO2,SPI_MISO_PIN,GPIO_Mode_In_PU_No_IT);\
}
#define SPI0_DELAY {\
asm("NOP;");asm("NOP;");asm("NOP;");asm("NOP;");asm("NOP;");asm("NOP;");asm("NOP;");asm("NOP;");\
asm("NOP;");asm("NOP;");asm("NOP;");asm("NOP;");asm("NOP;");asm("NOP;");asm("NOP;");asm("NOP;");\
asm("NOP;");asm("NOP;");asm("NOP;");asm("NOP;");asm("NOP;");asm("NOP;");asm("NOP;");asm("NOP;");\
asm("NOP;");asm("NOP;");asm("NOP;");asm("NOP;");asm("NOP;");asm("NOP;");asm("NOP;");asm("NOP;");\
asm("NOP;");asm("NOP;");asm("NOP;");asm("NOP;");asm("NOP;");asm("NOP;");asm("NOP;");asm("NOP;");\
asm("NOP;");asm("NOP;");asm("NOP;");asm("NOP;");asm("NOP;");asm("NOP;");asm("NOP;");asm("NOP;");\
asm("NOP;");asm("NOP;");asm("NOP;");asm("NOP;");asm("NOP;");asm("NOP;");asm("NOP;");asm("NOP;");\
asm("NOP;");asm("NOP;");asm("NOP;");asm("NOP;");asm("NOP;");asm("NOP;");asm("NOP;");asm("NOP;");\
}
enum DataDeal
{
read=0,
write=1
};
void SpiInit1( void );
uint8_t SpiInOut( uint8_t outData );
uint8_t spi0ReadWrite(uint8_t reg,uint8_t data,enum DataDeal deal);
void spiTest(void);
#endif
#include "bsp.h"
void AdcInit(void)
{
CLK_PeripheralClockConfig(CLK_Peripheral_ADC1, ENABLE);
ADC_Init(ADC1,ADC_ConversionMode_Single,ADC_Resolution_12Bit,ADC_Prescaler_1);
ADC_SamplingTimeConfig(ADC1, ADC_Group_SlowChannels, ADC_SamplingTime_384Cycles);
ADC_Cmd(ADC1,ENABLE);
ADC_Cmd(ADC1,DISABLE);
}
void bsp_init(void)
{
disableInterrupts();
bsp_clk_init();
bsp_time4_init();
bsp_usart_init();
SpiInit1();
bsp_gpio_init();
SoftDelayMs(10);
FixedParaDeal();
SoftDelayMs(10);
printf("AT+RST:OK!\r\n");
WatchDogInit();
enableInterrupts();
}
void WatchDogInit(void)
{
IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);//允许操作独立看门狗
IWDG_SetPrescaler(IWDG_Prescaler_256);//看门狗预分频
IWDG_SetReload(IWDG_500MS_REST);//设置看门狗复位时间
IWDG_Enable();//启动独立看门狗
}
void FeedWatchDog(void)
{
IWDG_ReloadCounter();//看门狗复位 正常使用时不想发生看门狗复位,就执行
}
#ifndef _BSP_H
#define _BSP_H
#include "stm8l15x.h"
#include "math.h"
#include <stdio.h>
#include "bsp_gpio.h"
#include "bsp_time.h"
#include "bsp_usart.h"
#include "stm8l15x_itc.h"
#include "stm8l15x_iwdg.h"
#define IWDG_500MS_REST (uint8_t)250
#define IWDG_400MS_REST (uint8_t)200
#define IWDG_300MS_REST (uint8_t)150
#define IWDG_250MS_REST (uint8_t)125
#define IWDG_200MS_REST (uint8_t)100
#define IWDG_100MS_REST (uint8_t)50
#define IWDG_50MS_REST (uint8_t)25
#define IWDG_4MS_REST (uint8_t)2
void WatchDogInit(void);
void FeedWatchDog(void);
void bsp_init(void);
#endif
\ No newline at end of file
#include "bsp_gpio.h"
/***************GPIO_init***************/
USERSTR LedStr;
struct BEEP_STR UserBeep;
void bsp_gpio_init(void)
{
//LEDƳʼ
// GPIOB->DDR = 0x02;
// GPIOB->CR1 = 0x02;
// GPIOB->CR2 = 0x02;
// GPIOB->ODR = 0x02;
GPIO_Init(LED_GPIO,LED1_PIN,GPIO_Mode_Out_PP_Low_Fast);
GPIO_Init(LED_GPIO,LED2_PIN,GPIO_Mode_Out_PP_Low_Fast);
//pinʼ
GPIO_Init(LIMIT_GPIO,LIMIT_PIN,GPIO_Mode_In_FL_No_IT);
GPIO_Init(BEEP_GPIO,BEEP_PIN,GPIO_Mode_Out_PP_Low_Slow);
}
void LedTest(void)
{
if(!LEDTEST)return;
if(LedStr.Cnt>0)return;
LedStr.Cnt = 1000;
GPIO_ToggleBits(LED_GPIO,LED1_PIN);
}
/*
if(time_count4==0)
{
time_count4 = 4000;
}
else if(time_count4<=10)
{
GPIO_WriteBit(GPIOB,GPIO_Pin_3,SET);
}
else
{
GPIO_WriteBit(GPIOB,GPIO_Pin_3,RESET);
}
*/
void bsp_beep_one(void)
{
GPIO_WriteBit(BEEP_GPIO,BEEP_PIN,SET);
bsp_DelayMS(50);
GPIO_WriteBit(BEEP_GPIO,BEEP_PIN,RESET);
}
void bsp_beep_two(void)
{
GPIO_WriteBit(BEEP_GPIO,BEEP_PIN,SET);
bsp_DelayMS(BEEPTIMR);
GPIO_WriteBit(BEEP_GPIO,BEEP_PIN,RESET);
bsp_DelayMS(BEEPTIMR*2);
GPIO_WriteBit(BEEP_GPIO,BEEP_PIN,SET);
bsp_DelayMS(BEEPTIMR);
GPIO_WriteBit(BEEP_GPIO,BEEP_PIN,RESET);
}
void BeepCheck(void)
{
static uint32_t tim_cnt = 0;
if(UserBeep.BeepFlag==1)
{
if(UserBeep.times==0)
{
UserBeep.BeepFlag = 0;
tim_cnt = 0;
GPIO_WriteBit(BEEP_GPIO,BEEP_PIN,RESET);
return ;
}
tim_cnt++;
if(UserBeep.Model==0)
{
if(tim_cnt>=10)
{
if(UserBeep.times--);
tim_cnt = 0;
GPIO_ToggleBits(BEEP_GPIO,BEEP_PIN);
}
}
else if(UserBeep.Model==1)
{
if(tim_cnt==50)
{
if(UserBeep.times--);
GPIO_WriteBit(BEEP_GPIO,BEEP_PIN,SET);
}
else if(tim_cnt==200)
{
tim_cnt = 0;
if(UserBeep.times--);
GPIO_WriteBit(BEEP_GPIO,BEEP_PIN,RESET);
}
}
}
}
void BeepStart(uint32_t times,bool Model)
{
UserBeep.BeepFlag = 1;
UserBeep.Model = Model;
UserBeep.times = 2*times;
}
void BeepStop(void)
{
UserBeep.BeepFlag = 0;
GPIO_WriteBit(BEEP_GPIO,BEEP_PIN,RESET);
}
\ No newline at end of file
#ifndef _BSP_GPIO_H
#define _BSP_GPIO_H
#include "bsp.h"
#include "user.h"
#define BEEP_GPIO GPIOD
#define BEEP_PIN GPIO_Pin_1
#define LED_GPIO GPIOA
#define LED1_PIN GPIO_Pin_5
#define LED2_PIN GPIO_Pin_4
struct BEEP_STR
{
bool BeepFlag;
bool Model;//0=εУ1=
uint32_t times;
};
extern struct BEEP_STR UserBeep;
void bsp_gpio_init(void);
void bsp_beep_one(void);
void bsp_beep_two(void);
void LedTest(void);
#define BEEPTIMR 50
void BeepCheck(void);
void BeepStart(uint32_t times,bool Model);
void BeepStop(void);
#endif
\ No newline at end of file
#include "bsp_time.h"
u32 time_count4 = 0;
u16 time_count2 = 0;
u16 DelayMs = 0;
/*************系统时钟***********************/
void bsp_clk_init(void)
{
//#if(1)
// #if AUTO_SWITCH
// CLK->SWCR |= 0x02;
// CLK->SWR = 0x04;
// asm("NOP");
// CLK->CKDIVR = 0x00;
// #else
// CLK->SWR = 0x04;
// while((CLK->ECKCR & 0x02)==0);
// CLK->SWCR |= 0x02;
// while(!(CLK->SCSR & 0x04));
// asm("NOP");
// CLK->CKDIVR = SYSDIV;
// #endif
//#else
// CLK_SYSCLKSourceSwitchCmd(ENABLE);
// CLK_SYSCLKSourceConfig(CLK_SYSCLKSource_HSE);
// while(CLK_GetSYSCLKSource()!=CLK_SYSCLKSource_HSE);
// asm("NOP");
// CLK->CKDIVR = 0x00;
//#endif
//CLK->CSSR |= 0x01;
//asm("NOP");
CLK_SYSCLKSourceSwitchCmd(ENABLE);
CLK_SYSCLKSourceConfig(CLK_SYSCLKSource_HSI);
/* 系统时钟分频*/
CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_1);//之前是CLK_SYSCLKDiv_1
while (CLK_GetSYSCLKSource() != CLK_SYSCLKSource_HSI)
{}
}
void bsp_time4_init(void)
{
CLK_PeripheralClockConfig(CLK_Peripheral_TIM4,ENABLE);//CLK->PCKENR1 = 0x04;//使能定时器时钟
TIM4->PSCR = TIMER4_DIV;//预分频
TIM4->CR1 = 0x85;//工作模式
TIM4->IER = 0x01;//定时器中断使能
TIM4->ARR = TIMER4_ARR;//自动重载的值
}
void bsp_time2_init(void)
{
CLK->PCKENR1 = 0x05;//使能定时器时钟
TIM2->PSCR = 0x0E;//预分频
TIM2->CR1 = 0x85;//工作模式
TIM2->IER = 0x01;//定时器中断使能
TIM2->CNTRH = 0x00;//自动重载的值
TIM2->CNTRL = 0x00;
TIM2->ARRH = 0x03;
TIM2->ARRL = 0xE8;
}
void RtcConfig(void)
{
CLK_LSICmd(ENABLE);
while (CLK_GetFlagStatus(CLK_FLAG_LSIRDY) == RESET);
CLK_PeripheralClockConfig(CLK_Peripheral_RTC, ENABLE);
CLK_RTCClockConfig(CLK_RTCCLKSource_LSI, CLK_RTCCLKDiv_1);//38Khz
RTC_WakeUpCmd(DISABLE);
RTC_WakeUpClockConfig(RTC_WakeUpClock_RTCCLK_Div16);//16分频=2.375Khz
//RTC_SetWakeUpCounter(8);//1s
RTC_SetWakeUpCounter(2);
RTC_ITConfig(RTC_IT_WUT, ENABLE);
RTC_WakeUpCmd(ENABLE);
}
void bsp_delay_ms(u16 ms)
{
time_count4 = ms;
while(1)
{
if(time_count4==0)
{
break;
}
}
}
void bsp_delay_s(u16 s)
{
time_count2 = s;
while(1)
{
if(time_count2==0)
{
break;
}
}
}
void bsp_DelayMS(uint16_t ms)
{
DelayMs = ms;
while(DelayMs);
}
void SoftDelayMs(u32 ms)
{
int i=0,j=0;
while(ms--)
{
for(i=0;i<16;i++)
for(j=0;j<1000;j++)
{
;
}
}
}
#ifndef _BSP_TIME_H
#define _BSP_TIME_H
#include "bsp.h"
#define SYSDIV 0 //ʱԤƵ
#define TIMER4_DIV 0x07//timer4ԤƵ
#define TMIER4_DELAY 1 //ms
#define TIMER4_ARR (u8)(16000000u/pow(2,SYSDIV)/pow(2,TIMER4_DIV)/(1000/TMIER4_DELAY))
#define AUTO_SWITCH 1
#define Manual_SWITCH 0
extern u32 time_count4;
extern u16 time_count2;
extern u16 DelayMs;
void bsp_clk_init(void);
void bsp_time4_init(void);
void bsp_delay_ms(u16 ms);
void bsp_delay_s(u16 s);
void bsp_DelayMS(uint16_t ms);
void RtcConfig(void);
void SoftDelayMs(u32 ms);
#endif
\ No newline at end of file
#include "bsp_usart.h"
#include "parameter.h"
USERSTR UartStr;
struct WIRELESS_INIT wireless_init = WIRELESS_INIT_DEFAULT;
static uint8_t WirelessModuleSet[10] = {0xC1,0xC1,0xC1,0xC0,0x64,0x36,0x25,0x4C,0xCC};
u8 TestData[8] = {0x00,0xF8,0xF8,0x01,0x02,0x03,0x04,0x05};
uint8_t UartRecLen = 0;
char UartRecBuff[100] = {0};
uint8_t UartTimeOut = 0;
int BaudCnt = 3;
uint32_t BaudRateUser[10] = {4800,9600,14400,19200,38400,56000,57600,115200};
void UartRecDeal(void)
{
UartTimeOut = 2;
UartRecBuff[UartRecLen++] = USART_ReceiveData8(USART1);
USART_ClearITPendingBit(USART1,USART_IT_RXNE);
if(UartRecLen>=100)UartRecLen = 0;
}
void UartRecClr(void)
{
memset(UartRecBuff,0,100*sizeof(uint8_t));
UartRecLen = 0;
}
int fputc(int ch, FILE *f)//STM8S105,printfض
{
while (!(USART1->SR&0x80));
USART1->DR=ch;
return ch;
}
void HostPCSend(void)
{
//uint32_t LoraChannel = 0;
char temp[50] = {0},temp2[10] = {0};
if(UartRecLen&&(UartTimeOut==0)&&(TxModel))
{
GPIO_ToggleBits(LED_GPIO,LED2_PIN);
if((memcmp(UartRecBuff,"AT+TEST",sizeof("AT+TEST")))==NULL)//test
{
printf("OK!\r\n");
}
else if((strstr(UartRecBuff,"AT+FRECFG:"))!=NULL)//fre cfg
{
HexStrToByte(UartRecBuff+10,SystemPara.PcID,8);
FixedParaChange(TRUE);
printf("AT+FRECFG:OK!\r\n");
}
else if(memcmp(UartRecBuff,"AT+FREQUE",sizeof("AT+FREQUE"))==NULL)
{
ByteToHexStr(SystemPara.PcID,temp2,4);
sprintf(temp,"AT+FRECFG%s:OK!\r\n",temp2);
printf("%s",temp);
}
else if(memcmp(UartRecBuff,"AT+RST",sizeof("AT+RST"))==NULL)//soft rst
{
asm("LDW X, SP ");
asm("LD A, $FF");
asm("LD XL, A ");
asm("LDW SP, X ");
asm("JPF $8000");
}
else if(memcmp(UartRecBuff,"AT+BAUDRATE?",sizeof("AT+BAUDRATE?"))==NULL)//BaudRate
{
printf("AT+BAUDRATE=%d\r\n",BaudCnt);
}
else if((strstr(UartRecBuff,"AT+BAUDRATE="))!=NULL)//BaudRate cfg
{
if(((UartRecBuff[12] - 0x30)<0)||((UartRecBuff[12] - 0x30)>7))
{
printf("BaudCfgError!\r\n");
}
else
{
BaudCnt = UartRecBuff[12] - 0x30;
printf("AT+BAUDCFG:OK!\r\n");
while((USART1->SR & (1<<6))==0);
USART_Cmd(USART1,DISABLE);
USART_Init(USART1,BaudRateUser[BaudCnt],USART_WordLength_8b,USART_StopBits_1,USART_Parity_No,(USART_Mode_TypeDef)(USART_Mode_Tx |USART_Mode_Rx ));
USART_Cmd(USART1,ENABLE);
}
}
else if(memcmp(UartRecBuff,"AT+UPDATE",sizeof("AT+UPDATE"))==NULL)//UpDate
{
printf("UpDate Start...\r\n");
while((USART1->SR & (1<<6))==0);
asm("LDW X, SP ");
asm("LD A, $FF");
asm("LD XL, A ");
asm("LDW SP, X ");
asm("JPF $6000");
}
else
{
if(UartRecLen<4)
{
memset(UartRecBuff,0,100*sizeof(uint8_t));
UartRecLen = 0;
printf("DataError!\r\n");
return;
}
SX78Str.SX7xTxFlag = TRUE;
SX78Str.SX7xCmd = CmdTX;
//SystemPara.LockID[2] = UartRecBuff[2];
memcpy(SystemPara.LockID,UartRecBuff,3*sizeof(uint8_t));
// if(SystemPara.LockID[2]>83)SystemPara.LockID[2] = 23;
// LoraChannel = SystemPara.LockID[2] + 410;
// LoraChannel *= 1024*16;
// LoraPara.TxChannel[0] = (uint8_t)(LoraChannel);
// LoraPara.TxChannel[1] = (uint8_t)(LoraChannel>>8);
// LoraChannel = LoraChannel>>8;
// LoraPara.TxChannel[2] = (uint8_t)(LoraChannel>>8);
LoraParaRead();
memcpy(SX78Str.SX7xTxStr,UartRecBuff,SX78Str.SX7xTxlen = (UartRecLen)*sizeof(uint8_t));
}
UartRecClr();
}
}
void bsp_usart_init(void)
{
CLK_PeripheralClockConfig(CLK_Peripheral_USART1,ENABLE);
GPIO_ExternalPullUpConfig(GPIOC, GPIO_Pin_3, ENABLE);
GPIO_ExternalPullUpConfig(GPIOC, GPIO_Pin_2, ENABLE);
USART_Init(USART1,19200,USART_WordLength_8b,USART_StopBits_1,USART_Parity_No,(USART_Mode_TypeDef)(USART_Mode_Tx |USART_Mode_Rx ));
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
USART_Cmd(USART1,ENABLE);
}
void UartTest(void)
{
if(!wireless_init.InitFlag)
{
WirelessInit(&wireless_init,WirelessModuleSet,6);
}
else
{
if(UartStr.Cnt>0)return;
UartStr.Cnt = 1000;
UsartPrint(TestData,8);
}
/*
if(!UARTTEST)return;
if(UartStr.Cnt>0)return;
UartStr.Cnt = 4000;
UsartPrint(TestData,5);
*/
}
void UsartPrint(uint8_t *data,uint32_t len)
{
uint32_t i = 0;
while((USART1->SR&0x80)==0);
for(i=0;i<len;i++)
{
USART1->DR = *(data++);
while((USART1->SR&0x40)==0);
}
asm("NOP");
}
static void WirelessInit(struct WIRELESS_INIT *WirelessInitCp,uint8_t *wireless,uint8_t num)
{
if(WirelessInitCp->InitFlag)return;
if(WirelessInitCp->InitDelay)return;
if(UartTimeOut)return;
static int cmd_count = 0,cmd_state=0;
switch(cmd_state)
{
case init_start:
{
WiRELESSINIT;
ConfigMode3;
//nrf_delay_ms(10);
cmd_count = 0;
cmd_state = winit_cmd_send;
UsartPrint(wireless,3);
cmd_state = init_delay;
WirelessInitCp->SendDelay = 50;
break;
}
case init_cmd_send:
{
UsartPrint(wireless+3,num);
cmd_state = init_delay;
WirelessInitCp->SendDelay = 50;
break;
}
case init_delay:
{
if(WirelessInitCp->SendDelay)break;
cmd_state = init_ing;
break;
}
case init_ing:
{
if(strstr((char *)UartRecBuff,(char *)(WirelessModuleSet+3))!=NULL)
{
memset((char *)UartRecBuff,0,25*sizeof(char));
UartRecLen = 0;
cmd_state = init_ok;
break;
}
else
{
if(cmd_count>10)
{
cmd_state = init_error;
break;
}
else
{
cmd_count++;
cmd_state = init_cmd_send;
break;
}
}
}
case init_ok:
{
ConfigMode2;
//nrf_delay_ms(10);
//beep_start(1,2,2);
WirelessInitCp->InitFlag = TRUE;
cmd_state = winit_start;
cmd_count = 0;
USART_Init(USART1,19200,USART_WordLength_8b,USART_StopBits_1,USART_Parity_No,(USART_Mode_TypeDef)(USART_Mode_Tx | USART_Mode_Rx ));
break;
}
case init_error:
{
WirelessInitCp->InitFlag = FALSE;
WirelessInitCp->InitDelay = 1000;
cmd_state = winit_start;
cmd_count = 0;
}
default:break;
}
}
#ifndef _BSP_USART_H
#define _BSP_USART_H
#include "bsp.h"
#define UARTTEST 1
#define WiRELESSINIT {\
GPIO_Init(GPIOD,GPIO_Pin_0,GPIO_Mode_Out_PP_Low_Slow);\
GPIO_Init(GPIOD,GPIO_Pin_1,GPIO_Mode_Out_PP_Low_Slow);\
}
#define ConfigMode1 {\
GPIO_WriteBit(GPIOD,GPIO_Pin_0,RESET);\
GPIO_WriteBit(GPIOD,GPIO_Pin_1,SET);\
}
#define ConfigMode2 {\
GPIO_WriteBit(GPIOD,GPIO_Pin_0,RESET);\
GPIO_WriteBit(GPIOD,GPIO_Pin_1,RESET);\
}
#define ConfigMode3 {\
GPIO_WriteBit(GPIOD,GPIO_Pin_0,SET);\
GPIO_WriteBit(GPIOD,GPIO_Pin_1,SET);\
}
enum
{
winit_start = 0,
winit_cmd_send = 1,
winit_delay = 2,
winit_ing = 3,
winit_ok = 4,
winit_error = 5
};
enum
{
init_start = 0,
init_cmd_send = 1,
init_delay = 2,
init_ing = 3,
init_ok = 4,
init_error = 5
};
struct WIRELESS_INIT
{
bool InitFlag;
uint32_t SendDelay;
uint32_t InitDelay;
};
#define WIRELESS_INIT_DEFAULT {\
FALSE,\
0,\
0\
}
extern struct WIRELESS_INIT wireless_init;
extern uint8_t UartRecLen;
extern char UartRecBuff[100];
extern uint8_t UartTimeOut;
void bsp_usart_init(void);
void UartTest(void);
void UsartPrint(uint8_t *data,uint32_t len);
void UartRecDeal(void);
void HostPCSend(void);
static void WirelessInit(struct WIRELESS_INIT *WirelessInitCp,uint8_t *wireless,uint8_t num);
#endif
\ No newline at end of file
#include "QMC5883L.h"
bool qmc_flag =TRUE;
bool qmc_init_flag = FALSE;
static uint8_t qmc_dec_count = 0;
uint8_t qmc_init_delay = 0;
uint8_t qmc_test_delay = 0;
static uint8_t buff[MagDataNum][6] = {0};
static uint16_t MagData[3] = {0};
enum QMC_STATE cur_state,next_state=qmc_init;
uint32_t temp[6] = {0};
static void QmcTest(void)
{
int i = 0,j = 0;
//uint32_t temp[6] = {0};
if((!HC_DEBUG)||(qmc_flag==FALSE))return;
if(qmc_test_delay)return;
cur_state = next_state;
switch(cur_state)
{
case qmc_init:
{
HMC5883_InitPin();
Init_HMC5883();
if(qmc_init_flag==TRUE)
{
next_state = qmc_dec;
qmc_dec_count = 0;
}
break;
}
case qmc_dec:
{
uint8_t QmcDataOk = 0;
QmcDataOk = Single_Read_HMC5883(SLAVE_ADD,0x06);
if((QmcDataOk&0x01));
else
return;
//qmc_dec_count++;
Multiple_Read_HMC5883(SLAVE_ADD,buff[qmc_dec_count]);
qmc_dec_count++;
if(qmc_dec_count>=MagDataNum)
{
for(j=0;j<6;j++)
{
for(i=1;i<MagDataNum-1;i++)
{
temp[j] += buff[i][j];
}
temp[j] = temp[j]/(MagDataNum-2);
}
for(i=0;i<3;i++)
{
if(temp[i*2+1]&0x80)
{
MagData[i] = (temp[i*2+1]+256);
}
MagData[i] = MagData[i]<<8;
MagData[i] += temp[i*2];
}
for(i=0;i<6;i++)
{
uint8_t t;
t = temp[i];
UsartPrint(&t,1);
}
//UsartPrint((uint8_t *)temp,6);
qmc_dec_count = 0;
next_state = qmc_lowpower;
}
break;
}
case qmc_lowpower:
{
Single_Write_HMC5883(SLAVE_ADD,0x0a,0x80);
next_state = qmc_printf;
break;
}
case qmc_printf:
{
if(qmc_flag)
{
qmc_flag = FALSE;
qmc_init_flag =FALSE;
next_state = qmc_init;
qmc_init_delay = 0;
}
break;
}
default :break;
}
}
void QmcDeal(void)
{
QmcTest();
}
#include "QMC5883L.h"
bool qmc_flag =TRUE;
bool qmc_init_flag = FALSE;
static uint8_t qmc_dec_count = 0;
uint8_t qmc_init_delay = 0;
uint8_t qmc_test_delay = 0;
static uint8_t buff[MagDataNum][6] = {0};
static uint16_t MagData[3] = {0};
enum QMC_STATE cur_state,next_state=qmc_init;
uint32_t temp[6] = {0};
static void QmcTest(void)
{
int i = 0,j = 0;
//uint32_t temp[6] = {0};
if((!HC_DEBUG)||(qmc_flag==FALSE))return;
if(qmc_test_delay)return;
cur_state = next_state;
switch(cur_state)
{
case qmc_init:
{
HMC5883_InitPin();
Init_HMC5883();
if(qmc_init_flag==TRUE)
{
next_state = qmc_dec;
qmc_dec_count = 0;
}
break;
}
case qmc_dec:
{
uint8_t QmcDataOk = 0;
QmcDataOk = Single_Read_HMC5883(SLAVE_ADD,0x06);
if((QmcDataOk&0x01));
else
return;
//qmc_dec_count++;
Multiple_Read_HMC5883(SLAVE_ADD,buff[qmc_dec_count]);
qmc_dec_count++;
if(qmc_dec_count>=MagDataNum)
{
for(j=0;j<6;j++)
{
for(i=1;i<MagDataNum-1;i++)
{
temp[j] += buff[i][j];
}
temp[j] = temp[j]/(MagDataNum-2);
}
for(i=0;i<3;i++)
{
if(temp[i*2+1]&0x80)
{
MagData[i] = (temp[i*2+1]+256);
}
MagData[i] = MagData[i]<<8;
MagData[i] += temp[i*2];
}
for(i=0;i<6;i++)
{
uint8_t t;
t = temp[i];
UsartPrint(&t,1);
}
//UsartPrint((uint8_t *)temp,6);
qmc_dec_count = 0;
next_state = qmc_lowpower;
}
break;
}
case qmc_lowpower:
{
Single_Write_HMC5883(SLAVE_ADD,0x0a,0x80);
next_state = qmc_printf;
break;
}
case qmc_printf:
{
if(qmc_flag)
{
qmc_flag = FALSE;
qmc_init_flag =FALSE;
next_state = qmc_init;
qmc_init_delay = 0;
}
break;
}
default :break;
}
}
void QmcDeal(void)
{
QmcTest();
}
/**
******************************************************************************
* @file GPIO/GPIO_Toggle/stm8l15x_conf.h
* @author MCD Application Team
* @version V1.5.1
* @date 28-June-2013
* @brief Library configuration file.
******************************************************************************
*
* Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.st.com/software_license_agreement_liberty_v2
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************
*/
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __STM8L15x_CONF_H
#define __STM8L15x_CONF_H
/* Includes ------------------------------------------------------------------*/
#include "stm8l15x.h"
/* Uncomment the line below to enable peripheral header file inclusion */
#include "stm8l15x_adc.h"
#include "stm8l15x_aes.h"
#include "stm8l15x_beep.h"
#include "stm8l15x_clk.h"
#include "stm8l15x_comp.h"
#include "stm8l15x_dac.h"
#include "stm8l15x_dma.h"
#include "stm8l15x_exti.h"
#include "stm8l15x_flash.h"
#include "stm8l15x_gpio.h"
#include "stm8l15x_i2c.h"
#include "stm8l15x_irtim.h"
#include "stm8l15x_itc.h"
#include "stm8l15x_iwdg.h"
#include "stm8l15x_lcd.h"
#include "stm8l15x_pwr.h"
#include "stm8l15x_rst.h"
#include "stm8l15x_rtc.h"
#include "stm8l15x_spi.h"
#include "stm8l15x_syscfg.h"
#include "stm8l15x_tim1.h"
#include "stm8l15x_tim2.h"
#include "stm8l15x_tim3.h"
#include "stm8l15x_tim4.h"
#include "stm8l15x_tim5.h"
#include "stm8l15x_usart.h"
#include "stm8l15x_wfe.h"
#include "stm8l15x_wwdg.h"
/* Exported types ------------------------------------------------------------*/
/* Exported constants --------------------------------------------------------*/
/* Uncomment the line below to expanse the "assert_param" macro in the
Standard Peripheral Library drivers code */
//#define USE_FULL_ASSERT (1) //LYQ20180624
/* Exported macro ------------------------------------------------------------*/
#ifdef USE_FULL_ASSERT
/**
* @brief The assert_param macro is used for function's parameters check.
* @param expr: If expr is false, it calls assert_failed function
* which reports the name of the source file and the source
* line number of the call that failed.
* If expr is true, it returns no value.
* @retval : None
*/
#define assert_param(expr) ((expr) ? (void)0 : assert_failed((uint8_t *)__FILE__, __LINE__))
/* Exported functions ------------------------------------------------------- */
void assert_failed(uint8_t* file, uint32_t line);
#else
#define assert_param(expr) ((void)0)
#endif /* USE_FULL_ASSERT */
#endif /* __STM8L15x_CONF_H */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
#ifndef __STM8L15x_IT_H
#define __STM8L15x_IT_H
/* Includes ------------------------------------------------------------------*/
#include "stm8l15x.h"
//extern unsigned int ADCdata;
extern u8 RecNum; //接收计数
extern u8 RecBuffer[25];
//extern u8 THR_TIME;
//extern u8 ALARM_BIT; //=1,在立起来时,没有到位
//extern u8 AUX_BIT_SET;
/* Exported types ------------------------------------------------------------*/
/* Exported constants --------------------------------------------------------*/
/* Exported macro ------------------------------------------------------------*/
/* Exported functions ------------------------------------------------------- */
#ifdef _COSMIC_
void _stext(void); /* RESET startup routine */
INTERRUPT void NonHandledInterrupt(void);
#endif /* _COSMIC_ */
#ifndef _RAISONANCE_
INTERRUPT void TRAP_IRQHandler(void); /* TRAP */
INTERRUPT void FLASH_IRQHandler(void); /* FLASH EOP/PG_DIS */
INTERRUPT void DMA1_CHANNEL0_1_IRQHandler(void); /* DMA1 Channel0/1*/
INTERRUPT void DMA1_CHANNEL2_3_IRQHandler(void); /*DMA1 Channel2/3*/
INTERRUPT void RTC_CSSLSE_IRQHandler(void); /* RTC /CSS_LSE */
INTERRUPT void EXTIE_F_PVD_IRQHandler(void); /*EXTI PORTE/EXTI PORTF/PVD*/
INTERRUPT void EXTIB_G_IRQHandler(void); /* EXTI PORTB / EXTI PORTG */
INTERRUPT void EXTID_H_IRQHandler(void); /* EXTI PORTD / EXTI PORTH*/
INTERRUPT void EXTI0_IRQHandler(void); /* EXTI PIN0 */
INTERRUPT void EXTI1_IRQHandler(void); /* EXTI PIN1 */
INTERRUPT void EXTI2_IRQHandler(void); /* EXTI PIN2 */
INTERRUPT void EXTI3_IRQHandler(void); /* EXTI PIN3 */
INTERRUPT void EXTI4_IRQHandler(void); /* EXTI PIN4 */
INTERRUPT void EXTI5_IRQHandler(void); /* EXTI PIN5 */
INTERRUPT void EXTI6_IRQHandler(void); /* EXTI PIN6 */
INTERRUPT void EXTI7_IRQHandler(void); /* EXTI PIN7 */
INTERRUPT void LCD_AES_IRQHandler(void); /* LCD /AES */
INTERRUPT void SWITCH_CSS_BREAK_DAC_IRQHandler(void); /* Switch CLK/CSS/TIM1 Break/DAC */
INTERRUPT void ADC1_COMP_IRQHandler(void); /*ADC1/COMP*/
INTERRUPT void TIM2_UPD_OVF_TRG_BRK_USART2_TX_IRQHandler(void); /* TIM2 UPD/OVF/TRG/BRK / USART2 TX */
INTERRUPT void TIM2_CC_USART2_RX_IRQHandler(void); /* TIM2 CAP / USART2 RX */
INTERRUPT void TIM3_UPD_OVF_TRG_BRK_USART3_TX_IRQHandler(void); /* TIM3 UPD/OVF/TRG/BRK /USART3 TX*/
INTERRUPT void TIM3_CC_USART3_RX_IRQHandler(void); /* TIM3 CAP/ USART3 RX */
INTERRUPT void TIM1_UPD_OVF_TRG_COM_IRQHandler(void);/* TIM1 UPD/OVF/TRG/COM */
INTERRUPT void TIM1_CC_IRQHandler(void);/* TIM1 CAP*/
INTERRUPT void TIM4_UPD_OVF_TRG_IRQHandler(void); /* TIM4 UPD/OVF/TRI */
INTERRUPT void SPI1_IRQHandler(void); /* SPI1 */
INTERRUPT void USART1_TX_TIM5_UPD_OVF_TRG_BRK_IRQHandler(void); /* USART1 TX / TIM5 UPD/OVF/TRG/BRK */
INTERRUPT void USART1_RX_TIM5_CC_IRQHandler(void); /* USART1 RX / TIM5 CAP */
INTERRUPT void I2C1_SPI2_IRQHandler(void); /* I2C1 / SPI2 */
#endif /* _RAISONANCE_ */
#endif /* __STM8L15x_IT_H */
#include "IIC.h"
#include "IIC.h"
//frequent 400kHZ
uint32_t T = 0;
void Delay5us(void)
{
nop();nop(); nop();nop(); nop();nop(); nop();nop(); nop();nop();
nop();nop(); nop();nop(); nop();nop(); nop();nop(); nop();nop();
}
//i2c pins initial
void HMC5883_InitPin(void)
{
GPIOC->DDR |= (I2C_SCL_PIN)|(I2C_SDA_PIN);
GPIOC->CR1 |= (I2C_SCL_PIN)|(I2C_SDA_PIN);
GPIOC->CR2 |= (I2C_SCL_PIN)|(I2C_SDA_PIN);
GPIOC->ODR &= ~((I2C_SCL_PIN)|(I2C_SDA_PIN));
//GPIO_Init(I2C_GPIO,I2C_SCL_PIN,GPIO_Mode_Out_PP_Low_Fast);
//GPIO_Init(I2C_GPIO,I2C_SDA_PIN,GPIO_Mode_Out_PP_High_Fast);
SCL_L;
Delay5us();Delay5us();Delay5us();Delay5us();Delay5us();Delay5us();
Delay5us();Delay5us();Delay5us();Delay5us();Delay5us();Delay5us();
Delay5us();Delay5us();Delay5us();Delay5us();Delay5us();Delay5us();
//bsp_DelayMS(3);Delay5us();
asm("NOP;");
}
bool HMC5883_Start(void)
{
SDA_H;
SCL_H;
Delay5us();
SDA_L;
Delay5us();
SCL_L;
return TRUE;
}
void HMC5883_Stop(void)
{
SDA_L;
SCL_H;
Delay5us();
SDA_H;
Delay5us();
SCL_L;
Delay5us();
}
bool HMC5883_WaitACK(void)
{
SDA_H;
SDA_IN;
Delay5us();
SCL_H;
Delay5us(); Delay5us();
if(SDA_read){SCL_L;SDA_OUT;return FALSE;}
Delay5us(); Delay5us();
SCL_L;
SDA_OUT;
return TRUE;
}
/**************************************
发送应答信号
入口参数:ack (0:ACK 1:NAK)
**************************************/
void HMC5883_SendACK(bool ack)
{
if(ack==TRUE)SDA_H;
else{SDA_L;}
Delay5us();
SCL_H;
Delay5us();
SCL_L;
}
/*I2C的NoACK函数----------------------------------------*/
void I2C_NoAck(void)
{
SCL_L;
Delay5us();
SDA_H;
Delay5us();
SCL_H;
Delay5us();
SCL_L;
Delay5us();
}
void HMC5883_SendByte(BYTE dat)
{
BYTE i;
for (i=0; i<8; i++) //8位计数器
{
if(dat&0x80)SDA_H;
else
SDA_L;
Delay5us();Delay5us();
SCL_H;
Delay5us();Delay5us();
SCL_L;
Delay5us();
dat<<=1;
}
if(!HMC5883_WaitACK())return;
}
/**************************************
从IIC总线接收一个字节数据
**************************************/
BYTE HMC5883_RecvByte()
{
BYTE i;
BYTE dat = 0;
SDA_H;
SDA_IN;
//使能内部上拉,准备读取数据,
for (i=0; i<8; i++) //8位计数器
{
dat <<= 1;
SCL_H; //拉高时钟线
Delay5us(); //延时
if(SDA_read)dat|=0x01;//读数据
SCL_L; //拉低时钟线
Delay5us(); //延时
}
SDA_OUT;
return dat;
}
//************************写入单字节数据***************************
void Single_Write_HMC5883(uchar SlaveAddress,uchar REG_Address,uchar REG_data)
{
HMC5883_Start(); //起始信号
HMC5883_SendByte(SlaveAddress); //发送设备地址+写信号
HMC5883_SendByte(REG_Address); //内部寄存器地址,请参考中文pdf
HMC5883_SendByte(REG_data); //内部寄存器数据,请参考中文pdf
HMC5883_Stop();
//发送停止信号
}
//************************读取内部寄存器*************************
uchar Single_Read_HMC5883(uchar SlaveAddress,uchar REG_Address)
{
uchar REG_data;
asm("NOP;");
HMC5883_Start(); //起始信号
HMC5883_SendByte(SlaveAddress); //发送设备地址+写信号
HMC5883_SendByte(REG_Address); //发送存储单元地址,从0开始
HMC5883_Start(); //起始信号
HMC5883_SendByte(SlaveAddress+1); //发送设备地址+读信号
REG_data=HMC5883_RecvByte(); //读出寄存器数据
HMC5883_SendACK(TRUE);
HMC5883_Stop(); //停止信号
return REG_data;
}
/*
//************************读取内部寄存器*************************
uchar Single_Read_HMC5883(uchar SlaveAddress,uchar REG_Address)
{
uchar REG_data;
HMC5883_Start(); //起始信号
HMC5883_SendByte(SlaveAddress); //发送设备地址+写信号
HMC5883_SendByte(REG_Address); //发送存储单元地址,从0开始
HMC5883_Start(); //起始信号
HMC5883_SendByte(SlaveAddress+1); //发送设备地址+读信号
REG_data=HMC5883_RecvByte(); //读出寄存器数据
HMC5883_SendACK(TRUE);
HMC5883_Stop(); //停止信号
return REG_data;
}
*/
void Multiple_Read_HMC5883(uchar SlaveAddress,uchar *BUFF)
{ uchar i;
HMC5883_Start(); //起始信号
HMC5883_SendByte(SlaveAddress); //发送设备地址+写信号
HMC5883_SendByte(0x00); //发送存储单元地址,从0x3开始
HMC5883_Start(); //起始信号
HMC5883_SendByte(SlaveAddress+1); //发送设备地址+读信号
for (i=0; i<6; i++) //连续读取6个地址数据,存储中BUF
{
BUFF[i] = HMC5883_RecvByte(); //BUF[0]存储数据
if (i == 5)
{
HMC5883_SendACK(TRUE); //最后一个数据需要回NOACK
}
else
{
HMC5883_SendACK(FALSE); //回应ACK
}
}
HMC5883_Stop(); //停止信号
}
//初始化HMC5883,根据需要请参考pdf进行修改****
uint8_t hhj = 0;
void Init_HMC5883()
{
HMC5883_InitPin();
Single_Write_HMC5883(SLAVE_ADD,0x0a,0x00);
Single_Write_HMC5883(SLAVE_ADD,0x09,0x1d);
Single_Write_HMC5883(SLAVE_ADD,0x0B,0x01);
}
#ifndef _IIC_H
#ifndef _IIC_H
#define _IIC_H
#include "user.h"
typedef unsigned char BYTE;
typedef unsigned short WORD;
#define uchar unsigned char
#define uint unsigned int
/*ģI2C˿붨*/
#define I2C_GPIO GPIOC
#define I2C_SCL_PIN GPIO_Pin_1
#define I2C_SDA_PIN GPIO_Pin_0
#define SCL_H (I2C_GPIO->ODR |= I2C_SCL_PIN)
#define SCL_L (I2C_GPIO->ODR &= (uint8_t)( ~I2C_SCL_PIN ))
#define SDA_H (I2C_GPIO->ODR |= I2C_SDA_PIN)
#define SDA_L (I2C_GPIO->ODR &= (uint8_t)( ~I2C_SDA_PIN ))
#define SDA_OUT ((I2C_GPIO->DDR)|=(uint8_t)(I2C_SDA_PIN))
#define SDA_IN ((I2C_GPIO->DDR)&=(uint8_t)(~I2C_SDA_PIN))
#define SDA_read (I2C_GPIO->IDR & (uint8_t)(I2C_SDA_PIN))
extern uint32_t T;
enum{yes=1,no=0};
void Delay5us(void);
void HMC5883_InitPin(void);
bool HMC5883_Start(void);
void HMC5883_Stop(void);
bool HMC5883_WaitACK(void);
void HMC5883_SendACK(bool ack);
void HMC5883_SendByte(BYTE dat);
BYTE HMC5883_RecvByte();
void Single_Write_HMC5883(uchar SlaveAddress,uchar REG_Address,uchar REG_data);
uchar Single_Read_HMC5883(uchar SlaveAddress,uchar REG_Address);
void Multiple_Read_HMC5883(uchar SlaveAddress,uchar *BUFF);
void Init_HMC5883();
#endif
#include "QMC5883L.h"
#include "QMC5883L.h"
bool qmc_flag =TRUE;
bool qmc_init_flag = FALSE;
static uint8_t qmc_dec_count = 0;
uint8_t qmc_init_delay = 0;
uint8_t qmc_test_delay = 0;
uint8_t buff[9] = {0};
enum QMC_STATE cur_state,next_state=qmc_init;
static void QmcTest(void)
{
uint8_t temp = 1;
if((!HC_DEBUG)||(qmc_flag==FALSE))return;
if(qmc_test_delay)return;
cur_state = next_state;
switch(cur_state)
{
case qmc_init:
{
Init_HMC5883();
qmc_init_flag = TRUE;
if(qmc_init_flag==TRUE)
{
UsartPrint(&temp,1);
//UsartPrint((uint8_t *)"InitSucess!\n",sizeof("InitSucess!\n"));
next_state = qmc_dec;
qmc_dec_count = 0;
}
break;
}
case qmc_dec:
{
buff[8] = Single_Read_HMC5883(SLAVE_ADD,0x06);
if((buff[8]&0x01));
else
return;
qmc_dec_count++;
Multiple_Read_HMC5883(SLAVE_ADD,buff);
UsartPrint(buff,6);
if(qmc_dec_count>5)
{
qmc_dec_count = 0;
next_state = qmc_lowpower;
}
break;
}
case qmc_lowpower:
{
Single_Write_HMC5883(SLAVE_ADD,0x0a,0x80);
next_state = qmc_printf;
//UsartPrint((uint8_t *)"In Lowpower\n",sizeof("In Lowpower\n"));
temp = 3;
UsartPrint(&temp,1);
break;
}
case qmc_printf:
{
if(qmc_flag)
{
qmc_flag = FALSE;
qmc_init_flag =FALSE;
next_state = qmc_init;
qmc_init_delay = 0;
}
break;
}
default :break;
}
}
void QmcDeal(void)
{
QmcTest();
}
#ifndef _QMC5883L_H
#ifndef _QMC5883L_H
#define _QMC5883L_H
#include "user.h"
enum IIC_ENUM
{
start_error = 0,
send_error = 1,
recive_error = 2,
sucess = 3
};
enum QMC_STATE
{
qmc_init = 0,
qmc_dec = 1,
qmc_lowpower = 2,
qmc_printf = 3
};
static int QMC5883L_Init(void);
static enum IIC_ENUM QMC_Reg_Write(uint8_t slave_add,uint8_t reg_add,uint8_t len,uint8_t* data);
static enum IIC_ENUM QMC_Reg_Read(uint8_t slave_add,uint8_t reg_add,uint8_t len,uint8_t* data);
static bool QMC5883L_init(void);
static void QmcTest(void);
void QmcDeal(void);
#define SLAVE_ADD 0x1A
#define HC_DEBUG 1
#define IIC_TEST_DEBUG 1
extern bool qmc_flag;
extern uint8_t qmc_test_delay;
extern uint8_t qmc_init_delay;
#endif
///////////////////////////////////////////////////////////////////////////////
//
// IAR C/C++ Compiler V2.10.4.157 for STM8 10/Aug/2020 13:50:18
// Copyright 2010-2015 IAR Systems AB.
//
// Source file = F:\2-Lora\Code\stm8l151K6T6-WLAN\USER\CRC.c
// Command line =
// F:\2-Lora\Code\stm8l151K6T6-WLAN\USER\CRC.c -e -Ol --no_cse
// --no_unroll --no_inline --no_code_motion --no_tbaa --no_cross_call
// --debug --code_model small --data_model medium -o
// F:\2-Lora\Code\stm8l151K6T6-WLAN\Debug\Obj\ --dlib_config
// E:\IAR\stm8\LIB\dlstm8smf.h -lB
// F:\2-Lora\Code\stm8l151K6T6-WLAN\Debug\List\ -I
// F:\2-Lora\Code\stm8l151K6T6-WLAN\FWLIB\inc\ -I
// F:\2-Lora\Code\stm8l151K6T6-WLAN\FWLIB\src\ -I
// F:\2-Lora\Code\stm8l151K6T6-WLAN\USER\ -I
// F:\2-Lora\Code\stm8l151K6T6-WLAN\BSP\ -I
// F:\2-Lora\Code\stm8l151K6T6-WLAN\radio\ -I
// F:\2-Lora\Code\stm8l151K6T6-WLAN\SX1278\ --vregs 16
// List file = F:\2-Lora\Code\stm8l151K6T6-WLAN\Debug\List\CRC.s
//
///////////////////////////////////////////////////////////////////////////////
SECTION VREGS:DATA:REORDER:NOROOT(0)
END
// F:\2-Lora\Code\stm8l151K6T6-WLAN\USER\CRC.c
// 1 #include "user.h"
// 2
// 3 //int AnalyseHEX(char hex[],int len)
// 4 //{
// 5 // unsigned char i=1;
// 6 // unsigned char data;
// 7 // int cc=0;
// 8 // char temp[2];
// 9 // do
// 10 // {
// 11 // temp[0] = hex[i++];
// 12 // temp[1] = hex[i++];
// 13 // sscanf(temp,"%x",&data);
// 14 // cc += data;
// 15 // } while (i<(len-2));
// 16 // cc%=256;
// 17 // cc=0x100-cc;
// 18 // temp[0] = hex[i++];
// 19 // temp[1] = hex[i++];
// 20 // sscanf(temp,"%x",&data);
// 21 // return (cc==data)?1:0;
// 22 //}
//
//
//
//
//
//Errors: none
//Warnings: none
///////////////////////////////////////////////////////////////////////////////
//
// IAR C/C++ Compiler V2.10.4.157 for STM8 10/Aug/2020 13:50:19
// Copyright 2010-2015 IAR Systems AB.
//
// Source file = F:\2-Lora\Code\stm8l151K6T6-WLAN\BSP\SPI.c
// Command line =
// F:\2-Lora\Code\stm8l151K6T6-WLAN\BSP\SPI.c -e -Ol --no_cse
// --no_unroll --no_inline --no_code_motion --no_tbaa --no_cross_call
// --debug --code_model small --data_model medium -o
// F:\2-Lora\Code\stm8l151K6T6-WLAN\Debug\Obj\ --dlib_config
// E:\IAR\stm8\LIB\dlstm8smf.h -lB
// F:\2-Lora\Code\stm8l151K6T6-WLAN\Debug\List\ -I
// F:\2-Lora\Code\stm8l151K6T6-WLAN\FWLIB\inc\ -I
// F:\2-Lora\Code\stm8l151K6T6-WLAN\FWLIB\src\ -I
// F:\2-Lora\Code\stm8l151K6T6-WLAN\USER\ -I
// F:\2-Lora\Code\stm8l151K6T6-WLAN\BSP\ -I
// F:\2-Lora\Code\stm8l151K6T6-WLAN\radio\ -I
// F:\2-Lora\Code\stm8l151K6T6-WLAN\SX1278\ --vregs 16
// List file = F:\2-Lora\Code\stm8l151K6T6-WLAN\Debug\List\SPI.s
//
///////////////////////////////////////////////////////////////////////////////
EXTERN ?b0
EXTERN ?b1
EXTERN ?b10
EXTERN ?b2
EXTERN ?b3
EXTERN ?b4
EXTERN ?b5
EXTERN ?b6
EXTERN ?b8
EXTERN ?b9
EXTERN ?epilogue_w4
EXTERN ?push_w4
EXTERN CLK_PeripheralClockConfig
EXTERN DelayTime
EXTERN GPIO_Init
EXTERN GPIO_WriteBit
EXTERN SPI_Cmd
EXTERN SPI_DeInit
EXTERN SPI_Init
PUBLIC SpiInOut
PUBLIC SpiInit1
PUBLIC dataTemp
PUBLIC regTemp
PUBLIC spi0ReadWrite
PUBLIC spiTest
PUBLIC temp2
PUBLIC temp3
// F:\2-Lora\Code\stm8l151K6T6-WLAN\BSP\SPI.c
// 1 #include "SPI.h"
SECTION `.near_func.text`:CODE:REORDER:NOROOT(0)
CODE
// 2 void SpiInit1( void )
// 3 {
// 4 CLK_PeripheralClockConfig(CLK_Peripheral_SPI1,ENABLE);
SpiInit1:
MOV S:?b0, #0x1
LD A, #0x4
CALL L:CLK_PeripheralClockConfig
// 5 SPI_IO_INIT;
MOV S:?b0, #0xf0
LD A, #0x8
LDW X, #0x5005
CALL L:GPIO_Init
MOV S:?b0, #0x1
LD A, #0x8
LDW X, #0x5005
CALL L:GPIO_WriteBit
MOV S:?b0, #0xf0
LD A, #0x20
LDW X, #0x5005
CALL L:GPIO_Init
MOV S:?b0, #0xf0
LD A, #0x40
LDW X, #0x5005
CALL L:GPIO_Init
MOV S:?b0, #0x40
LD A, #0x80
LDW X, #0x5005
CALL L:GPIO_Init
// 6 SPI_DeInit(SPI1);
LDW X, #0x5200
CALL L:SPI_DeInit
// 7 SPI0_DELAY;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
// 8 SPI_Init(SPI1,SPI_FirstBit_MSB,SPI_BaudRatePrescaler_16,SPI_Mode_Master,SPI_CPOL_Low,SPI_CPHA_1Edge,SPI_Direction_2Lines_FullDuplex,SPI_NSS_Soft,0);
CLR S:?b6
MOV S:?b5, #0x2
CLR S:?b4
CLR S:?b3
CLR S:?b2
MOV S:?b1, #0x4
MOV S:?b0, #0x18
CLR A
LDW X, #0x5200
CALL L:SPI_Init
// 9 SPI_Cmd(SPI1,ENABLE);
LD A, #0x1
LDW X, #0x5200
JP L:SPI_Cmd
// 10 // SPI1->CR2 = 0x01;
// 11 // SPI1->CR1 = 0x5C;
// 12 // SPI1->CR1 |= 0x40;
// 13 }
// 14
SECTION `.near_func.text`:CODE:REORDER:NOROOT(0)
CODE
// 15 uint8_t SpiInOut( uint8_t outData )
// 16 {
// 17 u8 cc=0;
SpiInOut:
CLR S:?b0
// 18 /* Send SPIy data */
// 19 //SPI_SendData( SPI1, outData );
// 20 SPI1->DR = outData;
LD L:0x5204, A
// 21 while(!(SPI1->SR&0x02));
??SpiInOut_0:
BTJF L:0x5203, #0x1, L:??SpiInOut_0
// 22 while(!(SPI1->SR&0x01));
??SpiInOut_1:
BTJF L:0x5203, #0x0, L:??SpiInOut_1
// 23 cc= SPI1->DR;//SPI_ReceiveData( SPI1 );
LD A, L:0x5204
LD S:?b0, A
// 24 SPI1->SR &= ~0x01;
BRES L:0x5203, #0x0
// 25 return cc;
LD A, S:?b0
RET
// 26 }
SECTION `.near.bss`:DATA:REORDER:NOROOT(0)
// 27 uint8_t regTemp=0,dataTemp=0,temp3=0;
regTemp:
DS8 1
SECTION `.near.bss`:DATA:REORDER:NOROOT(0)
dataTemp:
DS8 1
SECTION `.near.bss`:DATA:REORDER:NOROOT(0)
temp3:
DS8 1
SECTION `.near_func.text`:CODE:REORDER:NOROOT(0)
CODE
// 28 uint8_t spi0ReadWrite(uint8_t reg,uint8_t data,enum DataDeal deal)
// 29 {
spi0ReadWrite:
CALL L:?push_w4
PUSH S:?b10
LD S:?b8, A
MOV S:?b9, S:?b0
MOV S:?b10, S:?b1
// 30 SPI0_SCS_L;
CLR S:?b0
LD A, #0x8
LDW X, #0x5005
CALL L:GPIO_WriteBit
// 31 SPI0_DELAY;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
// 32 dataTemp = data;
LD A, S:?b9
LD L:dataTemp, A
// 33 regTemp = reg;
LD A, S:?b8
LD L:regTemp, A
// 34 if(deal==write)
LD A, S:?b10
CP A, #0x1
JRNE L:??spi0ReadWrite_0
// 35 {
// 36 regTemp |= 0x80;
BSET L:regTemp, #0x7
JRA L:??spi0ReadWrite_1
// 37 }
// 38 else if(deal==read)
??spi0ReadWrite_0:
TNZ S:?b10
JRNE L:??spi0ReadWrite_1
// 39 {
// 40 regTemp &= 0x7F;
BRES L:regTemp, #0x7
// 41 dataTemp = 0xff;
MOV L:dataTemp, #0xff
// 42 }
// 43 SpiInOut(regTemp);
??spi0ReadWrite_1:
LD A, L:regTemp
CALL L:SpiInOut
// 44 temp3 = SpiInOut(dataTemp);
LD A, L:dataTemp
CALL L:SpiInOut
LD L:temp3, A
// 45 SPI0_SCS_H;
MOV S:?b0, #0x1
LD A, #0x8
LDW X, #0x5005
CALL L:GPIO_WriteBit
// 46 return temp3;
LD A, L:temp3
POP S:?b10
JP L:?epilogue_w4
// 47 }
SECTION `.near.bss`:DATA:REORDER:NOROOT(0)
// 48 uint8_t temp2;
temp2:
DS8 1
SECTION `.near_func.text`:CODE:REORDER:NOROOT(0)
CODE
// 49 void spiTest(void)
// 50 {
// 51 if(DelayTime)return;
spiTest:
LD A, L:DelayTime
CP A, #0x0
JRNE L:??spiTest_0
// 52 DelayTime = 1;
MOV L:DelayTime, #0x1
// 53 temp2 = spi0ReadWrite(REG_LR_VERSION,0xff,read);;//SpiInOut(0xa5);
CLR S:?b1
MOV S:?b0, #0xff
LD A, #0x42
CALL L:spi0ReadWrite
LD L:temp2, A
// 54 SPI0_DELAY;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
// 55 //UsartPrint(&temp2,1);
// 56 }
??spiTest_0:
RET
SECTION VREGS:DATA:REORDER:NOROOT(0)
END
// 57
//
// 4 bytes in section .near.bss
// 449 bytes in section .near_func.text
//
// 449 bytes of CODE memory
// 4 bytes of DATA memory
//
//Errors: none
//Warnings: 1
///////////////////////////////////////////////////////////////////////////////
//
// IAR C/C++ Compiler V2.10.4.157 for STM8 10/Aug/2020 13:50:17
// Copyright 2010-2015 IAR Systems AB.
//
// Source file = F:\2-Lora\Code\stm8l151K6T6-WLAN\BSP\bsp.c
// Command line =
// F:\2-Lora\Code\stm8l151K6T6-WLAN\BSP\bsp.c -e -Ol --no_cse
// --no_unroll --no_inline --no_code_motion --no_tbaa --no_cross_call
// --debug --code_model small --data_model medium -o
// F:\2-Lora\Code\stm8l151K6T6-WLAN\Debug\Obj\ --dlib_config
// E:\IAR\stm8\LIB\dlstm8smf.h -lB
// F:\2-Lora\Code\stm8l151K6T6-WLAN\Debug\List\ -I
// F:\2-Lora\Code\stm8l151K6T6-WLAN\FWLIB\inc\ -I
// F:\2-Lora\Code\stm8l151K6T6-WLAN\FWLIB\src\ -I
// F:\2-Lora\Code\stm8l151K6T6-WLAN\USER\ -I
// F:\2-Lora\Code\stm8l151K6T6-WLAN\BSP\ -I
// F:\2-Lora\Code\stm8l151K6T6-WLAN\radio\ -I
// F:\2-Lora\Code\stm8l151K6T6-WLAN\SX1278\ --vregs 16
// List file = F:\2-Lora\Code\stm8l151K6T6-WLAN\Debug\List\bsp.s
//
///////////////////////////////////////////////////////////////////////////////
EXTERN ?b0
EXTERN ?b1
EXTERN ?w0
EXTERN ?w1
EXTERN ADC_Cmd
EXTERN ADC_Init
EXTERN ADC_SamplingTimeConfig
EXTERN CLK_PeripheralClockConfig
EXTERN FixedParaDeal
EXTERN IWDG_Enable
EXTERN IWDG_ReloadCounter
EXTERN IWDG_SetPrescaler
EXTERN IWDG_SetReload
EXTERN IWDG_WriteAccessCmd
EXTERN SoftDelayMs
EXTERN SpiInit1
EXTERN bsp_clk_init
EXTERN bsp_gpio_init
EXTERN bsp_time4_init
EXTERN bsp_usart_init
EXTERN printf
PUBLIC AdcInit
PUBLIC FeedWatchDog
PUBLIC WatchDogInit
PUBLIC bsp_init
// F:\2-Lora\Code\stm8l151K6T6-WLAN\BSP\bsp.c
// 1 #include "bsp.h"
SECTION `.near_func.text`:CODE:REORDER:NOROOT(0)
CODE
// 2 void AdcInit(void)
// 3 {
// 4 CLK_PeripheralClockConfig(CLK_Peripheral_ADC1, ENABLE);
AdcInit:
MOV S:?b0, #0x1
LD A, #0x10
CALL L:CLK_PeripheralClockConfig
// 5 ADC_Init(ADC1,ADC_ConversionMode_Single,ADC_Resolution_12Bit,ADC_Prescaler_1);
CLR S:?b1
CLR S:?b0
CLR A
LDW X, #0x5340
CALL L:ADC_Init
// 6 ADC_SamplingTimeConfig(ADC1, ADC_Group_SlowChannels, ADC_SamplingTime_384Cycles);
MOV S:?b0, #0x7
CLR A
LDW X, #0x5340
CALL L:ADC_SamplingTimeConfig
// 7 ADC_Cmd(ADC1,ENABLE);
LD A, #0x1
LDW X, #0x5340
CALL L:ADC_Cmd
// 8 ADC_Cmd(ADC1,DISABLE);
CLR A
LDW X, #0x5340
JP L:ADC_Cmd
// 9 }
SECTION `.near_func.text`:CODE:REORDER:NOROOT(0)
CODE
// 10 void bsp_init(void)
// 11 {
// 12 disableInterrupts();
bsp_init:
SIM
// 13 bsp_clk_init();
CALL L:bsp_clk_init
// 14 bsp_time4_init();
CALL L:bsp_time4_init
// 15 bsp_usart_init();
CALL L:bsp_usart_init
// 16 SpiInit1();
CALL L:SpiInit1
// 17 bsp_gpio_init();
CALL L:bsp_gpio_init
// 18 SoftDelayMs(10);
LDW X, #0xa
LDW S:?w1, X
CLRW X
LDW S:?w0, X
CALL L:SoftDelayMs
// 19 FixedParaDeal();
CALL L:FixedParaDeal
// 20 SoftDelayMs(10);
LDW X, #0xa
LDW S:?w1, X
CLRW X
LDW S:?w0, X
CALL L:SoftDelayMs
// 21 printf("AT+RST:OK!\r\n");
LDW X, #?_0
CALL L:printf
// 22 WatchDogInit();
CALL L:WatchDogInit
// 23 enableInterrupts();
RIM
// 24 }
RET
// 25
// 26
SECTION `.near_func.text`:CODE:REORDER:NOROOT(0)
CODE
// 27 void WatchDogInit(void)
// 28 {
// 29 IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);//允许操作独立看门狗
WatchDogInit:
LD A, #0x55
CALL L:IWDG_WriteAccessCmd
// 30 IWDG_SetPrescaler(IWDG_Prescaler_256);//看门狗预分频
LD A, #0x6
CALL L:IWDG_SetPrescaler
// 31 IWDG_SetReload(IWDG_500MS_REST);//设置看门狗复位时间
LD A, #0xfa
CALL L:IWDG_SetReload
// 32 IWDG_Enable();//启动独立看门狗
JP L:IWDG_Enable
// 33 }
// 34
SECTION `.near_func.text`:CODE:REORDER:NOROOT(0)
CODE
// 35 void FeedWatchDog(void)
// 36 {
// 37 IWDG_ReloadCounter();//看门狗复位 正常使用时不想发生看门狗复位,就执行
FeedWatchDog:
JP L:IWDG_ReloadCounter
// 38 }
SECTION VREGS:DATA:REORDER:NOROOT(0)
SECTION `.near.rodata`:CONST:REORDER:NOROOT(0)
?_0:
DC8 "AT+RST:OK!\015\012"
END
// 39
// 40
// 41
// 42
//
// 13 bytes in section .near.rodata
// 119 bytes in section .near_func.text
//
// 119 bytes of CODE memory
// 13 bytes of CONST memory
//
//Errors: none
//Warnings: none
///////////////////////////////////////////////////////////////////////////////
//
// IAR C/C++ Compiler V2.10.4.157 for STM8 10/Aug/2020 13:50:18
// Copyright 2010-2015 IAR Systems AB.
//
// Source file = F:\2-Lora\Code\stm8l151K6T6-WLAN\USER\main.c
// Command line =
// F:\2-Lora\Code\stm8l151K6T6-WLAN\USER\main.c -e -Ol --no_cse
// --no_unroll --no_inline --no_code_motion --no_tbaa --no_cross_call
// --debug --code_model small --data_model medium -o
// F:\2-Lora\Code\stm8l151K6T6-WLAN\Debug\Obj\ --dlib_config
// E:\IAR\stm8\LIB\dlstm8smf.h -lB
// F:\2-Lora\Code\stm8l151K6T6-WLAN\Debug\List\ -I
// F:\2-Lora\Code\stm8l151K6T6-WLAN\FWLIB\inc\ -I
// F:\2-Lora\Code\stm8l151K6T6-WLAN\FWLIB\src\ -I
// F:\2-Lora\Code\stm8l151K6T6-WLAN\USER\ -I
// F:\2-Lora\Code\stm8l151K6T6-WLAN\BSP\ -I
// F:\2-Lora\Code\stm8l151K6T6-WLAN\radio\ -I
// F:\2-Lora\Code\stm8l151K6T6-WLAN\SX1278\ --vregs 16
// List file = F:\2-Lora\Code\stm8l151K6T6-WLAN\Debug\List\main.s
//
///////////////////////////////////////////////////////////////////////////////
EXTERN GPIO_ToggleBits
EXTERN UserTest
EXTERN bsp_init
PUBLIC main
// F:\2-Lora\Code\stm8l151K6T6-WLAN\USER\main.c
// 1 #include "user.h"
SECTION `.near_func.text`:CODE:REORDER:NOROOT(0)
CODE
// 2 void main(void)
// 3 {
// 4 bsp_init();
main:
CALL L:bsp_init
// 5 GPIO_ToggleBits(LED_GPIO,LED2_PIN);
LD A, #0x10
LDW X, #0x5000
CALL L:GPIO_ToggleBits
// 6 while(1)
// 7 {
// 8 UserTest();
??main_0:
CALL L:UserTest
JRA L:??main_0
// 9 //halt();
// 10 }
// 11 }
SECTION VREGS:DATA:REORDER:NOROOT(0)
END
//
// 16 bytes in section .near_func.text
//
// 16 bytes of CODE memory
//
//Errors: none
//Warnings: none
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
File added
File added
File added
File added
This diff is collapsed. Click to expand it.
This diff is collapsed. Click to expand it.
This diff is collapsed. Click to expand it.
This diff is collapsed. Click to expand it.
This diff is collapsed. Click to expand it.
This source diff could not be displayed because it is too large. You can view the blob instead.
This diff is collapsed. Click to expand it.
This diff is collapsed. Click to expand it.
This diff is collapsed. Click to expand it.
This diff is collapsed. Click to expand it.
This diff is collapsed. Click to expand it.
This diff is collapsed. Click to expand it.
This diff is collapsed. Click to expand it.
This diff is collapsed. Click to expand it.
This diff is collapsed. Click to expand it.
This diff is collapsed. Click to expand it.
This diff is collapsed. Click to expand it.
This diff is collapsed. Click to expand it.
This diff is collapsed. Click to expand it.
This diff is collapsed. Click to expand it.
This diff is collapsed. Click to expand it.
This diff is collapsed. Click to expand it.
This diff is collapsed. Click to expand it.
This diff is collapsed. Click to expand it.
This diff is collapsed. Click to expand it.
This diff is collapsed. Click to expand it.
This diff is collapsed. Click to expand it.
This diff is collapsed. Click to expand it.
This diff is collapsed. Click to expand it.
This diff is collapsed. Click to expand it.
This diff is collapsed. Click to expand it.
This diff is collapsed. Click to expand it.
This diff is collapsed. Click to expand it.
This diff is collapsed. Click to expand it.
This diff is collapsed. Click to expand it.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment