|
/**
|
******************************************************************************
|
* @file : main.c
|
* @brief : Main program body
|
******************************************************************************
|
** This notice applies to any and all portions of this file
|
* that are not between comment pairs USER CODE BEGIN and
|
* USER CODE END. Other portions of this file, whether
|
* inserted by the user or by software development tools
|
* are owned by their respective copyright owners.
|
*
|
* COPYRIGHT(c) 2018 STMicroelectronics
|
*
|
* Redistribution and use in source and binary forms, with or without modification,
|
* are permitted provided that the following conditions are met:
|
* 1. Redistributions of source code must retain the above copyright notice,
|
* this list of conditions and the following disclaimer.
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
* this list of conditions and the following disclaimer in the documentation
|
* and/or other materials provided with the distribution.
|
* 3. Neither the name of STMicroelectronics nor the names of its contributors
|
* may be used to endorse or promote products derived from this software
|
* without specific prior written permission.
|
*
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
*
|
******************************************************************************
|
*/
|
/* Includes ------------------------------------------------------------------*/
|
#include "main.h"
|
#include "stm32f0xx_hal.h"
|
|
/* USER CODE BEGIN Includes */
|
#include "Globaldef.h"
|
#include "debug.h"
|
#include "Functions.h"
|
#include "KMachine.h"
|
#include "PLCfunctions.h"
|
//#include "KBus.h"
|
#include "KLink.h"
|
#include "string.h"
|
#include "BSP_UltraSonic.h"
|
#include "ModbusRTU.h"
|
#if (BOARD_TYPE == 13)
|
#include "w5500_port.h"
|
#include "../src/Ethernet/socket.h"
|
#include "../src/Ethernet/loopback.h"
|
#elif (BOARD_TYPE == 14)
|
#include "FP0.h"
|
#elif (BOARD_TYPE == 15 || BOARD_TYPE == 16)
|
#include "KWireless.h"
|
//#include "user.h"
|
//#include "../src/radio/inc/sx126x-board.h"
|
#endif
|
|
/* USER CODE END Includes */
|
|
/* Private variables ---------------------------------------------------------*/
|
|
/* USER CODE BEGIN PV */
|
/* Private variables ---------------------------------------------------------*/
|
|
#define RX2BUFSIZE 64
|
#define TX2BUFSIZE 64
|
|
unsigned char Uart1RxBuf[128];
|
unsigned char Uart1TxBuf[260];
|
|
unsigned char Uart2RxBuf[RX2BUFSIZE];
|
unsigned char Uart2TxBuf[TX2BUFSIZE];
|
|
unsigned char Uart1RxBuf1[Uart1RxBufSize];
|
unsigned char Uart1TxBuf1[260];
|
|
unsigned char Uart2RxBuf1[RX2BUFSIZE];
|
unsigned char Uart2TxBuf1[TX2BUFSIZE];
|
|
unsigned short Uart1RxBuf1DataLen = 0;
|
unsigned short Uart2RxBuf1DataLen = 0;
|
|
unsigned char Uart1Mode = 1; //Uart1工作模式, 0 : 普通, 1 : 透传模式
|
|
unsigned int Uart1Baud = DefaultUart1Baud;
|
unsigned int Uart2Baud = DefaultUart2Baud;
|
|
//unsigned char Uart1RecvBuf1[Uart1RecvBufSize];
|
//unsigned short Uart1RecvBuf1DataLen=0;
|
|
//unsigned char Uart2RecvBuf1[128];
|
//unsigned short Uart2RecvBuf1DataLen=0;
|
|
volatile char Uart1BaudGot=0;
|
volatile char Uart1BaudFirstGot=0;
|
volatile char Uart1DmaInts=0;
|
|
|
unsigned char SlowFlicker=0;
|
unsigned char FastFlicker=0;
|
|
unsigned int Uart1IdelTimer = 0;
|
|
uint32_t us1,us2,us3,us4,us5,us6;
|
|
|
stKBusDef KBus1; //
|
|
extern stDeviceInfo MyDeviceInfo;
|
/* USER CODE END PV */
|
|
/* Private function prototypes -----------------------------------------------*/
|
|
|
/* USER CODE BEGIN PFP */
|
/* Private function prototypes -----------------------------------------------*/
|
|
const unsigned char LEDSEGTAB[]={
|
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71, //0-F
|
0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef,0xf7,0xfc,0xb9,0xde,0xf9,0xf1, //0.-F.
|
0x00,0x40, // ,-,_,~,o,n,N,<,>,J,r,
|
};
|
|
/* USER CODE END PFP */
|
|
/* USER CODE BEGIN 0 */
|
#define SET_SCL LL_GPIO_SetOutputPin(GPIOB,LL_GPIO_PIN_6)
|
#define CLR_SCL LL_GPIO_ResetOutputPin(GPIOB,LL_GPIO_PIN_6)
|
#define GET_SCL LL_GPIO_IsInputPinSet(GPIOB,LL_GPIO_PIN_6)
|
#define SET_SDA LL_GPIO_SetOutputPin(GPIOB,LL_GPIO_PIN_7)
|
#define CLR_SDA LL_GPIO_ResetOutputPin(GPIOB,LL_GPIO_PIN_7)
|
#define GET_SDA LL_GPIO_IsInputPinSet(GPIOB,LL_GPIO_PIN_7)
|
|
|
void soft_i2c_start()
|
{
|
SET_SDA;
|
SET_SCL;
|
Delay100nS(1);
|
CLR_SDA;
|
Delay100nS(1);
|
CLR_SCL;
|
Delay100nS(1);
|
}
|
void soft_i2c_stop()
|
{
|
CLR_SDA;
|
Delay100nS(1);
|
SET_SCL;
|
Delay100nS(1);
|
SET_SDA;
|
Delay100nS(1);
|
}
|
void soft_i2c_send8(int nData)
|
{
|
int mask;
|
mask = 0x80;
|
for (int j=0;j<8;j++)
|
{
|
if (nData & mask) {SET_SDA;}
|
else {CLR_SDA;}
|
Delay100nS(1);
|
SET_SCL;
|
mask>>=1;
|
Delay100nS(1);
|
CLR_SCL;
|
}
|
return;
|
}
|
|
uint8_t soft_i2c_recv8()
|
{
|
unsigned char nData=0;
|
for (int j=0;j<8;j++)
|
{
|
nData <<=1;
|
Delay100nS(1);
|
SET_SCL;
|
nData |= GET_SDA;
|
Delay100nS(1);
|
CLR_SCL;
|
}
|
return nData;
|
}
|
|
void soft_i2c_send_ack()
|
{
|
CLR_SDA;
|
Delay100nS(2);
|
SET_SCL;
|
Delay100nS(2);
|
CLR_SCL;
|
SET_SDA;
|
Delay100nS(2);
|
|
}
|
|
void soft_i2c_send_nack()
|
{
|
SET_SDA;
|
Delay100nS(1);
|
SET_SCL;
|
Delay100nS(1);
|
CLR_SCL;
|
Delay100nS(1);
|
SET_SDA;
|
}
|
uint8_t soft_i2c_wait_ack(int nTime)
|
{
|
SET_SDA; // Open Drain;
|
Delay100nS(1);
|
SET_SCL;
|
for (int j=0;j<nTime;j++){
|
Delay100nS(1);
|
if (GET_SDA == 0) break;
|
if (j==nTime-1) return 0;
|
}
|
CLR_SCL;
|
return 1;
|
}
|
uint8_t soft_i2c_check_addr(uint8_t Addr)
|
{
|
uint8_t res=0;
|
soft_i2c_start();
|
// Send Device Addr 7bit;
|
soft_i2c_send8(Addr);
|
if (soft_i2c_wait_ack(10)) {res=1;}
|
//Stop
|
soft_i2c_stop();
|
// */
|
return res;
|
|
}
|
uint8_t soft_i2c_read_len( uint8_t Addr , uint8_t Reg, uint8_t len,uint8_t *buf)
|
{
|
int res=0;
|
//Start
|
soft_i2c_start();
|
// Send Device Addr 7bit;
|
soft_i2c_send8(Addr &0xfe);
|
// wait Ack;
|
if (!soft_i2c_wait_ack(1000)) {soft_i2c_stop();return 1;}
|
CLR_SCL;
|
// Send Reg Addr 8bit;
|
soft_i2c_send8(Reg);
|
if (!soft_i2c_wait_ack(1000)) {soft_i2c_stop();return 2;}
|
//Start
|
soft_i2c_start();
|
// Send Device Addr 7bit;
|
soft_i2c_send8(Addr | 1);
|
if (!soft_i2c_wait_ack(1000)) {soft_i2c_stop();return 3;}
|
|
// /*
|
// Recv Data(s) n * 8bit;
|
SET_SDA; // Open Drain;
|
for (int i=0;i<len;i++)
|
{
|
// recv 1 data 8bit;
|
unsigned char nData = 0;
|
nData = soft_i2c_recv8();
|
buf[i]=nData;
|
// Send ACK / NACK;
|
if (i != len -1) { //ACK
|
soft_i2c_send_ack();
|
} else { // NACK
|
soft_i2c_send_nack();
|
}
|
}
|
|
//Stop
|
soft_i2c_stop();
|
// */
|
return res;
|
}
|
|
uint8_t soft_i2c_write_len(uint8_t Addr , uint8_t Reg, uint8_t len, uint8_t *buf)
|
{
|
int res=0;
|
//Start
|
soft_i2c_start();
|
// Send Device Addr 7bit;
|
soft_i2c_send8(Addr &0xfe);
|
// wait Ack;
|
if (!soft_i2c_wait_ack(1000)) return 1;
|
CLR_SCL;
|
// Send Reg Addr 8bit;
|
soft_i2c_send8(Reg);
|
if (!soft_i2c_wait_ack(1000)) return 2;
|
for (int i=0;i<len;i++)
|
{
|
// send 1 data 8bit;
|
unsigned char nData = buf[i];
|
soft_i2c_send8(nData);
|
// wait Ack;
|
if (!soft_i2c_wait_ack(1000)) {res = 5; break;}
|
}
|
//Stop
|
soft_i2c_stop();
|
return res;
|
|
}
|
|
|
|
|
int HexToInt(char ch)
|
{
|
if (ch>='0' && ch <='9') return ch-'0';
|
if (ch>='A' && ch <='F') return ch-'A'+10;
|
if (ch>='a' && ch <='f') return ch-'a'+10;
|
return 0;
|
}
|
|
void HAL_SYSTICK_Callback(void)
|
{
|
return;
|
static int Count=0;
|
CurTickuS += 100;
|
nCurTick++;
|
KBus1.nSlaveTick++;
|
Count++;
|
if (Count>=10000)
|
{
|
Count=0;
|
KMem.CurTimeSec++;
|
KMem.ThisRunTime++; KMem.TotalRunTime++;
|
if (KMRunStat.bLEDFlick) KMRunStat.bLEDFlick--;
|
if (KMRunStat.bLEDFlick >120) KMRunStat.bLEDFlick=120;
|
}
|
|
return;
|
}
|
|
void PendSvCallBack()
|
{
|
/*
|
if (Uart2Stat.bPacketRecved)
|
{
|
KBusParsePacket(&KBus1, (pKBPacket)Uart2RxBuf1, Uart2RxBuf1DataLen);
|
Uart2RxBuf1DataLen=0;
|
Uart2Stat.bPacketRecved=0;
|
Uart2RecvDMA(Uart2RxBuf1,sizeof(Uart2RxBuf1));
|
KMem.WDT[2]++;
|
}
|
*/
|
}
|
|
/*
|
KBus通讯回调函数,当通讯状态改变或数据更新时被调用。
|
或者系统请求时。
|
*/
|
void * KBusEvCallBackFunc(void* pParam, int nEvent, void *pBuf, int nLen1)
|
{
|
switch (nEvent){
|
|
case KBusEvNone:
|
break;
|
case KBusEvCreate:
|
break;
|
case KBusEvConnected:
|
break;
|
case KBusEvDisConnected:
|
break;
|
case KBusEvClosed:
|
break;
|
case KBusEvStateChange:
|
break;
|
case KBusEvTimeSync:
|
break;
|
case KBusEvDataUpdate:
|
if (KBus1.bMaster) {
|
for (int i=0;i<16;i++)
|
{
|
KMem.WLX[i]=KBusMem.WLX[i]; //KPLC with KBus Master
|
KBusMem.WLY[i]=KMem.WLY[i];
|
}
|
} else if (KBus1.bSlave) {
|
KMem.WLX[0]=KBusMem.WLY[0]; //KPLC with KBus Slave
|
KBusMem.WLX[0]=KMem.WLY[0];
|
KMem.WLX[1]=KBusMem.WLY[1]; //KPLC with KBus Slave
|
KBusMem.WLX[1]=KMem.WLY[1];
|
KMem.WLX[2]=KBusMem.WLY[2]; //KPLC with KBus Slave
|
KBusMem.WLX[2]=KMem.WLY[2];
|
KMem.WLX[3]=KBusMem.WLY[3]; //KPLC with KBus Slave
|
KBusMem.WLX[3]=KMem.WLY[3];
|
}
|
|
break;
|
case KBusEvCmdResponse:
|
break;
|
|
default:
|
break;
|
}
|
return 0;
|
}
|
#define SET_STB() LL_GPIO_SetOutputPin(GPIOA,LL_GPIO_PIN_15)
|
#define CLR_STB() LL_GPIO_ResetOutputPin(GPIOA,LL_GPIO_PIN_15)
|
#define SET_SCK() LL_GPIO_SetOutputPin(GPIOB,LL_GPIO_PIN_3)
|
#define CLR_SCK() LL_GPIO_ResetOutputPin(GPIOB,LL_GPIO_PIN_3)
|
#define SET_DIO() LL_GPIO_SetOutputPin(GPIOB,LL_GPIO_PIN_5)
|
#define CLR_DIO() LL_GPIO_ResetOutputPin(GPIOB,LL_GPIO_PIN_5)
|
#define GET_DIO() LL_GPIO_IsInputPinSet(GPIOB,LL_GPIO_PIN_5)
|
|
void Set_Run_Led(uchar bOn)
|
{
|
if (bOn){ LL_GPIO_ResetOutputPin(GPIOB,LL_GPIO_PIN_12); }
|
else { LL_GPIO_SetOutputPin(GPIOB,LL_GPIO_PIN_12);}
|
|
}
|
void TM1629_Send_Byte(uchar byte)
|
{
|
CLR_STB();
|
unsigned char mask = 0x01;
|
for (int i=0;i<8;i++)
|
{
|
CLR_SCK();
|
if (byte & mask) { SET_DIO(); }
|
else {CLR_DIO();}
|
DelayUs(1);
|
SET_SCK();
|
mask<<=1;
|
DelayUs(1);
|
}
|
}
|
uchar TM1629_Read_Byte()
|
{
|
CLR_STB();
|
SET_DIO();
|
uchar byte=0;
|
unsigned char mask = 0x01;
|
for (int i=0;i<8;i++)
|
{
|
CLR_SCK();
|
DelayUs(1);
|
SET_SCK();
|
DelayUs(1);
|
if (GET_DIO()) { byte|=mask; }
|
mask<<=1;
|
}
|
return byte;
|
|
}
|
void TM1629_disp_on()
|
{
|
SET_STB();
|
SET_SCK();
|
SET_DIO();
|
DelayUs(2);
|
CLR_STB();
|
TM1629_Send_Byte(0x8a);
|
SET_STB();
|
SET_DIO();
|
}
|
|
void TM1629_Set_Mode_a()
|
{
|
SET_STB();
|
SET_SCK();
|
SET_DIO();
|
DelayUs(2);
|
CLR_STB();
|
TM1629_Send_Byte(0x40);
|
SET_STB();
|
}
|
void TM1629_Set_Address(uchar addr)
|
{
|
SET_STB();
|
SET_SCK();
|
SET_DIO();
|
DelayUs(2);
|
CLR_STB();
|
TM1629_Send_Byte(0xc0 | addr);
|
}
|
|
void TM1629_Send_Data(int addr, const uchar * pbuf, int len)
|
{
|
TM1629_Set_Mode_a();
|
TM1629_Set_Address(addr);
|
for (int i=0;i<len;i++)
|
{
|
TM1629_Send_Byte(pbuf[i]);
|
}
|
SET_STB();
|
|
}
|
|
void TM1629_Read_Keys(uchar * pkeybuf)
|
{
|
SET_STB();
|
SET_SCK();
|
SET_DIO();
|
DelayUs(2);
|
CLR_STB();
|
TM1629_Send_Byte(0x42);
|
for (int i=0;i<4;i++)
|
pkeybuf[i]=TM1629_Read_Byte();
|
SET_STB();
|
}
|
__asm int bintobcd(int a,int b)
|
{
|
add r0,r1,r0
|
|
BLX lr
|
}
|
|
/* USER CODE END 0 */
|
|
/**
|
* @brief The application entry point.
|
*
|
* @retval None
|
*/
|
int main(void)
|
{
|
/* USER CODE BEGIN 1 */
|
KMRunStat.bLEDFlick = 1;
|
|
InitUartstat(&Uart1Stat,Uart1RxBuf,sizeof(Uart1RxBuf),Uart1TxBuf,sizeof(Uart1TxBuf));
|
InitUartstat(&Uart2Stat,Uart2RxBuf,sizeof(Uart2RxBuf),Uart2TxBuf,sizeof(Uart2TxBuf));
|
/* USER CODE END 1 */
|
|
/* MCU Configuration----------------------------------------------------------*/
|
|
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
|
HAL_Init();
|
|
/* USER CODE BEGIN Init */
|
|
|
int nRunCount = 0;
|
|
KMem.LastScanTime=0;
|
KMem.ScanTimeuS=0;
|
KMem.MinScanTimeuS=99999;
|
KMem.MaxScanTimeuS=0;
|
|
// KMem.SDD[14]=(unsigned int)&KMStoreSysCfg;
|
// KMem.SDD[15]=(unsigned int)&KMStoreSysCfg1;
|
KMem.SDD[12]=((uint32_t *)UID_BASE)[0];
|
// KMem.SDD[13]=((uint32_t *)UID_BASE)[1];
|
// KMem.SDD[14]=((uint32_t *)UID_BASE)[2];
|
KMem.SDD[13]=PendSvCount;
|
KMem.SDD[14]=RCC->CSR;
|
// KMem.SDD[15]=*(uint32_t *)FLASHSIZE_BASE;
|
// KMem.SDD[16]=(unsigned int)&KMSysCfg;
|
|
/* USER CODE END Init */
|
DelayUs(10000);
|
/* Configure the system clock */
|
SystemClock_Config_New();
|
|
|
/* USER CODE BEGIN SysInit */
|
TickFreq=1000; //Tick频率
|
InituS(TickFreq);
|
// HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/TickFreq); //重新定义SysTick的频率
|
|
/* USER CODE END SysInit */
|
|
/* Initialize all configured peripherals */
|
// MX_GPIO_Init();
|
// MX_DMA_Init();
|
|
|
// KMachineInit();
|
// ReadSysCfgFromFlash(&storedKMSysCfg);
|
|
KMRunStat.bLEDFlick = 1;
|
|
|
KLinkInit(1);
|
|
//if (KMem.EffJumperSW == 0x00)
|
Uart1Baud = DefaultUart1Baud;
|
// MX_USART1_UART_Init();
|
// MX_USART2_UART_Init();
|
// MX_SPI1_Init();
|
// LL_SPI_EnableIT_RXNE(SPI1);
|
/*
|
// MX_I2C1_Init();
|
Soft_I2C1_Init();
|
|
|
|
// MX_IWDG_Init();
|
|
MX_TIM6_Init();
|
LL_TIM_EnableCounter(TIM6);
|
*/
|
/* USER CODE BEGIN 2 */
|
// LL_USART_EnableIT_RXNE(USART1);
|
// LL_USART_EnableIT_IDLE(USART1);
|
// LL_USART_EnableIT_TC(USART1);
|
|
// LL_USART_EnableIT_RXNE(USART2);
|
// Uart2RecvDMA(Uart2RxBuf1,sizeof(Uart2RxBuf1));
|
// LL_USART_EnableIT_IDLE(USART2);
|
// LL_USART_EnableIT_TC(USART2);
|
|
// if (bKBusSlave)
|
{
|
// LL_USART_EnableAutoBaudRate(USART1);
|
// LL_USART_SetAutoBaudRateMode(USART1, LL_USART_AUTOBAUD_DETECT_ON_FALLINGEDGE);
|
// LL_USART_EnableAutoBaudRate(USART2);
|
// LL_USART_SetAutoBaudRateMode(USART2, LL_USART_AUTOBAUD_DETECT_ON_FALLINGEDGE);
|
}
|
//LL_USART_EnableIT_TXE(USART1);
|
|
// KMem.WDT[50] = SPI_Flash_ReadID();
|
|
/* USER CODE END 2 */
|
|
|
/* Infinite loop */
|
/* USER CODE BEGIN WHILE */
|
|
// HAL_Delay(10);
|
// SetRunLed(1); //Turn On Run Led
|
// SetErrLed(0); //Turn Off Err Led
|
|
|
|
// ShowInitInfo();
|
KMem.LastScanTime = GetuS();
|
|
KMRunStat.WorkMode = storedKMSysCfg.theKMSysCfg.workmode;
|
|
KMRunStat.WorkMode = 1;
|
//KMRunStat.WorkMode2 = 0;
|
|
|
|
MX_TIM1_Init();
|
|
LL_TIM_CC_EnableChannel(TIM1,LL_TIM_CHANNEL_CH1);
|
LL_TIM_CC_EnableChannel(TIM1,LL_TIM_CHANNEL_CH1N);
|
LL_TIM_EnableCounter(TIM1);
|
LL_TIM_CC_EnableChannel(TIM1,LL_TIM_CHANNEL_CH4);
|
|
// LL_TIM_OC_SetCompareCH4(TIM1,600);
|
|
LL_TIM_OC_SetCompareCH2(TIM1,100);
|
|
|
MX_TIM15_Init();
|
LL_TIM_CC_EnableChannel(TIM15,LL_TIM_CHANNEL_CH1);
|
LL_TIM_CC_EnableChannel(TIM15,LL_TIM_CHANNEL_CH1N);
|
LL_TIM_EnableCounter(TIM15);
|
|
LL_TIM_EnableAllOutputs(TIM1);
|
LL_TIM_EnableAllOutputs(TIM15);
|
|
LL_TIM_DisableAllOutputs(TIM1);
|
LL_TIM_DisableAllOutputs(TIM15);
|
|
|
// LL_TIM_DisableCounter(TIM1);
|
// LL_TIM_DisableCounter(TIM15);
|
|
|
/*
|
MX_TIM16_Init();
|
|
LL_TIM_CC_EnableChannel(TIM16,LL_TIM_CHANNEL_CH1);
|
LL_TIM_CC_EnableChannel(TIM16,LL_TIM_CHANNEL_CH1N);
|
LL_TIM_OC_SetCompareCH1(TIM16,600);
|
LL_TIM_EnableCounter(TIM16);
|
|
LL_TIM_EnableAllOutputs(TIM16);
|
*/
|
LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOB);
|
|
LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
|
GPIO_InitStruct.Pin = LL_GPIO_PIN_12;
|
GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
|
GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
|
GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
|
GPIO_InitStruct.Pull = LL_GPIO_PULL_UP;
|
// GPIO_InitStruct.Alternate = LL_GPIO_AF_0;
|
LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
|
|
LL_GPIO_ResetOutputPin(GPIOB,LL_GPIO_PIN_12);
|
|
GPIO_InitStruct.Pin = LL_GPIO_PIN_15;
|
GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
|
GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
|
GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
|
GPIO_InitStruct.Pull = LL_GPIO_PULL_UP;
|
// GPIO_InitStruct.Alternate = LL_GPIO_AF_0;
|
LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
|
GPIO_InitStruct.Pin = LL_GPIO_PIN_3 ;
|
GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
|
GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
|
GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
|
GPIO_InitStruct.Pull = LL_GPIO_PULL_UP;
|
// GPIO_InitStruct.Alternate = LL_GPIO_AF_0;
|
LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
|
|
GPIO_InitStruct.Pin = LL_GPIO_PIN_5;
|
GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
|
GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
|
GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_OPENDRAIN;
|
GPIO_InitStruct.Pull = LL_GPIO_PULL_UP;
|
// GPIO_InitStruct.Alternate = LL_GPIO_AF_0;
|
LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
|
|
|
SET_STB();
|
SET_SCK();
|
SET_DIO();
|
|
|
|
TM1629_disp_on();
|
|
KMem.WDB[128]=0x56;
|
|
int nShift = 1;
|
int nDir = 1;
|
int nFreq = 28000;
|
int nOrgFreqFct = (48000000 / nFreq) - 1;
|
int nFreqFctDlt = 50 ;
|
|
int nMaxFreqFct = nOrgFreqFct + nFreqFctDlt;
|
int nMinFreqFrc = nOrgFreqFct - nFreqFctDlt;
|
|
int nCurFreqFct = nOrgFreqFct;
|
|
int bEnableSpan = 0; //1;
|
int bShift = 1;
|
|
uchar ledbuf1[16];
|
uchar line1[4];
|
uchar line2[4];
|
uchar line3[4];
|
uchar line4[4];
|
|
line1[0]=LEDSEGTAB[0];
|
line1[1]=LEDSEGTAB[1];
|
line1[2]=LEDSEGTAB[2];
|
line1[3]=LEDSEGTAB[3];
|
|
line2[0]=LEDSEGTAB[4];
|
line2[1]=LEDSEGTAB[5];
|
line2[2]=LEDSEGTAB[6];
|
line2[3]=LEDSEGTAB[7];
|
|
line3[0]=LEDSEGTAB[8];
|
line3[1]=LEDSEGTAB[9];
|
line3[2]=LEDSEGTAB[0xa];
|
line3[3]=LEDSEGTAB[0xb];
|
|
line4[0]=0x00;
|
line4[1]=0x55;
|
line4[2]=0xff;
|
line4[3]=0x0;
|
uchar keybuf[4];
|
int nCount1=0;
|
union{
|
uchar value;
|
struct
|
{
|
uchar Finc:1;
|
uchar sweep:1;
|
uchar Tdec:1;
|
uchar Pdec:1;
|
uchar Fdec:1;
|
uchar On:1;
|
uchar Tinc:1;
|
uchar Pinc:1;
|
};
|
}
|
keys,oldkeys;
|
|
int value1;
|
int value2;
|
int value3;
|
int value4;
|
|
int bPowerOn =0;
|
int bSweep = 0;
|
while (1)
|
{
|
Set_Run_Led((nRunCount>>8) & 1);;
|
nRunCount ++;
|
//int MyKeyStat1,MyKeyStat2;
|
//MyKeyStat1=GetInput();
|
TM1629_disp_on();
|
TM1629_Read_Keys(keybuf);
|
keys.value=keybuf[0];
|
|
|
// value1 = nRunCount;
|
if (keys.Pinc && !oldkeys.Pinc) {
|
value1++;
|
}
|
if (keys.Pdec && !oldkeys.Pdec) {
|
value1--;
|
}
|
if (keys.Tdec && !oldkeys.Tdec) {
|
value2--;
|
}
|
if (keys.Tinc && !oldkeys.Tinc) {
|
value2++;
|
}
|
|
if (keys.On && !oldkeys.On) {
|
bPowerOn = !bPowerOn;
|
if (bPowerOn) {
|
nShift=0;
|
LL_TIM_EnableAllOutputs(TIM1);
|
LL_TIM_EnableAllOutputs(TIM15);
|
}else {
|
nShift=0;
|
LL_TIM_DisableAllOutputs(TIM1);
|
LL_TIM_DisableAllOutputs(TIM15);
|
}
|
}
|
if (keys.sweep && !oldkeys.sweep) {
|
bSweep = !bSweep;
|
if (bSweep) {
|
bEnableSpan = 1;
|
}else {
|
bEnableSpan = 0;
|
}
|
}
|
|
oldkeys = keys;
|
|
int time1=GetuS();
|
// line1[0]= LEDSEGTAB[value1%10];
|
// line1[1]= LEDSEGTAB[value1/10%10];
|
// line1[2]= LEDSEGTAB[value1/100%10];
|
// line1[3]= LEDSEGTAB[value1/1000%10];
|
|
if (!bPowerOn) {
|
line1[0] = LEDSEGTAB[15];
|
line1[1] = LEDSEGTAB[15];
|
line1[2] = LEDSEGTAB[0];
|
line1[3] = LEDSEGTAB[33];
|
}else {
|
line1[3] = LEDSEGTAB[15];
|
line1[2] = LEDSEGTAB[2];
|
line1[1] = LEDSEGTAB[8+16];
|
line1[0] = LEDSEGTAB[0];
|
}
|
|
ledbuf1[8]=line1[0];
|
ledbuf1[0xa]=line1[1];
|
ledbuf1[0xc]=line1[2];
|
ledbuf1[0xe]=line1[3];
|
|
int time2=GetuS();
|
int dt = time2-time1;
|
// value2 = dt;
|
int nCurrent = 0;
|
value2 = nCurrent;
|
|
line2[0]= LEDSEGTAB[10]; // 'A'
|
line2[1]= LEDSEGTAB[value2%10]; // '0' - '9'
|
line2[2]= LEDSEGTAB[value2/10%10 + 16]; // '0.' - '9.'
|
if (value2>=100) { line2[3]= LEDSEGTAB[value2/100%10]; }
|
else {line2[3] = keybuf[0];}
|
ledbuf1[1]=line2[0];
|
ledbuf1[3]=line2[1];
|
ledbuf1[5]=line2[2];
|
ledbuf1[7]=line2[3];
|
|
int nTime = 0;
|
value3 = nTime;
|
line3[0]= LEDSEGTAB[value3%10];
|
line3[1]= LEDSEGTAB[value3/10%10];
|
line3[2]= LEDSEGTAB[value3/100%10];
|
line3[3]= LEDSEGTAB[value3/1000%10];
|
|
ledbuf1[0]=line3[0];
|
ledbuf1[2]=line3[1];
|
ledbuf1[4]=line3[2];
|
ledbuf1[6]=line3[3];
|
|
// ledbuf1[9]=line4[3];
|
|
int level = (1<<((nRunCount>>2) &0x1f)) - 1;
|
level = (1<<((nShift/32+4) &0x1f)) - 1;
|
line4[0]=level&0xff;
|
line4[1]=(level>>8)&0xff;
|
line4[2]=(level>>16)&0xff;
|
ledbuf1[0xb]=line4[2];
|
ledbuf1[0xd]=line4[1];
|
ledbuf1[0xf]=line4[0];
|
|
|
|
TM1629_Send_Data(0,ledbuf1,16);
|
DelayUs(100);
|
//*((unsigned int *)&(PLCMem.SDT[10]))=nRunCount;
|
// KMem.nRunCount=nRunCount;
|
if (bEnableSpan && (nRunCount&0x1) == 0x1) {
|
|
nCurFreqFct --;
|
if (nCurFreqFct <= nMinFreqFrc) {
|
nCurFreqFct = nMaxFreqFct;
|
}
|
LL_TIM_SetAutoReload(TIM15,nCurFreqFct);
|
LL_TIM_SetAutoReload(TIM1,nCurFreqFct);
|
LL_TIM_OC_SetCompareCH1(TIM1,(nCurFreqFct+1)/2-1);
|
LL_TIM_OC_SetCompareCH4(TIM1,(nCurFreqFct+1)/2-1);
|
LL_TIM_OC_SetCompareCH1(TIM15,(nCurFreqFct+1)/2-1);
|
}
|
|
if (bPowerOn&&bShift && (nRunCount&0x3) == 0x2) {
|
if (nDir == 1) {
|
if (nShift < 700 -1) {nShift+=1;}
|
else {nDir = 0;}
|
}else {
|
if (nShift > 1) {nShift-=1;}
|
else {nDir = 1;}
|
}
|
LL_TIM_OC_SetCompareCH2(TIM15,nShift);
|
}
|
|
|
ADCProcess();
|
int a;
|
a = LL_GPIO_ReadInputPort(GPIOA);
|
KMem.WDT[120]=a;
|
a = LL_GPIO_ReadInputPort(GPIOB);
|
KMem.WDT[121]=a;
|
a = LL_GPIO_ReadInputPort(GPIOC);
|
KMem.WDT[122]=a;
|
a = LL_GPIO_ReadInputPort(GPIOD);
|
KMem.WDT[123]=a;
|
|
us2=GetuS();
|
if (PowerDownFlag) { KMem.WX[0]=0;}
|
///*
|
if ((KMem.nRunCount &0x1f) == 0x02)
|
{
|
|
if (PowerDownFlag)
|
{
|
KMem.WX[0]=0;
|
if (!OldPowerDownFlag)
|
{
|
OldPowerDownFlag = PowerDownFlag;
|
OldPowerDownEventTime = nCurTick;
|
// PowerDownProcess();
|
}
|
}else
|
{
|
if (OldPowerDownFlag)
|
{
|
OldPowerDownFlag=PowerDownFlag;
|
// PowerRecoverProcess();
|
|
}
|
}
|
}
|
//*/
|
|
|
|
|
|
|
// int nSize=sizeof(stKBusChnStat);
|
// memcpy(&KMem.SDT[64],&KBusChnStats[1],nSize);
|
// memcpy(&KMem.SDT[64+nSize/2],&KBusChnStats[2],nSize);
|
// for (int i=0;i<128;i++) { SDT[i]=i; }
|
// SDT[48]=55;
|
/*
|
if (Uart1RxBuf1DataLen >0 && Uart1Stat.bPacketRecved)
|
{
|
int res1 = -1;
|
res1 = ModBusSlaveParsePkg(1, Uart1RxBuf1, Uart1RxBuf1DataLen);
|
if (res1 !=0)
|
{
|
KLParsePacket(1, Uart1RxBuf1, Uart1RxBuf1DataLen);
|
}
|
Uart1RxBuf1DataLen=0;
|
Uart1Stat.bPacketRecved=0;
|
Uart1IdelTimer = 0;
|
}else {
|
if (Uart1IdelTimer>600000) { // 超过60秒没有数据传输,重新进入自适应波特率状态
|
LL_USART_EnableAutoBaudRate(USART1);
|
LL_USART_SetAutoBaudRateMode(USART1, LL_USART_AUTOBAUD_DETECT_ON_FALLINGEDGE);
|
}else {
|
Uart1IdelTimer++;
|
}
|
}
|
*/
|
|
// LL_IWDG_ReloadCounter(IWDG);
|
|
} //while (1) ;
|
/* USER CODE END WHILE */
|
|
/* USER CODE BEGIN 3 */
|
|
/* USER CODE END 3 */
|
|
}
|
|
|
/* USER CODE BEGIN 4 */
|
|
/* USER CODE END 4 */
|
|
/**
|
* @brief This function is executed in case of error occurrence.
|
* @param file: The file name as string.
|
* @param line: The line in file as a number.
|
* @retval None
|
*/
|
void _Error_Handler(char *file, int line)
|
{
|
/* USER CODE BEGIN Error_Handler_Debug */
|
/* User can add his own implementation to report the HAL error return state */
|
while(1)
|
{
|
}
|
/* USER CODE END Error_Handler_Debug */
|
}
|
|
#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(uint8_t* file, uint32_t line)
|
{
|
/* USER CODE BEGIN 6 */
|
/* User can add his own implementation to report the file name and line number,
|
tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
|
/* USER CODE END 6 */
|
}
|
#endif /* USE_FULL_ASSERT */
|
|
/**
|
* @}
|
*/
|
|
/**
|
* @}
|
*/
|
|
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|