Skip to content

Instantly share code, notes, and snippets.

@goog
Created August 26, 2019 08:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save goog/bf784c5f63a199ddbde7cd49db92f73f to your computer and use it in GitHub Desktop.
Save goog/bf784c5f63a199ddbde7cd49db92f73f to your computer and use it in GitHub Desktop.
/**
******************************************************************************
* @file Project/STM8L15x_StdPeriph_Template/main.c
* @author MCD Application Team
* @version V1.6.1
* @date 30-September-2014
* @brief Main program body
******************************************************************************
* @attention
*
* <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
*
* 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.
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "stm8l15x.h"
#include "stm8l15x_gpio.h"
#include "stm8l15x_tim1.h"
#include "stdio.h"
#include "stdlib.h"
#include "math.h"
#include "string.h"
#include "stm8_eval_i2c_ee.h"
/** @addtogroup STM8L15x_StdPeriph_Template
* @{
*/
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
static void TIM1_Config(void);
/* Private functions ---------------------------------------------------------*/
//void LCD_OFF(void);
//void LCD_ON(void);
/**
* @brief Main program.
* @param None
* @retval None
*/
//#define LED_GPIO_PORT GPIOA
#define R4_GPIO_PINS GPIO_Pin_6
#define RNTC_GPIO_PINS GPIO_Pin_5
#define R6_GPIO_PINS GPIO_Pin_4
#define PUTCHAR_PROTOTYPE char putchar (char c)
#define GETCHAR_PROTOTYPE char getchar (void)
//uint32_t TIM1ClockFreq = 2000000;
//__IO uint32_t LSIClockFreq = 0;
uint16_t ICValue1 = 0, ICValue2 = 0;
__IO unsigned short int us_cnt = 0;
unsigned short int run_counter = 0;
unsigned char state = 0;
unsigned int stop_flag = 0;
int pos = 0;
unsigned char RX_buf[20] = {0};
void Delay(uint32_t nCount)
{
/* Decrement nCount value */
while (nCount != 0)
{
nCount--;
}
}
void delay_ms(u16 n_ms)
{
CLK_PeripheralClockConfig(CLK_Peripheral_TIM2, ENABLE);
/* Init TIMER 4 prescaler: / (2^6) = /64 */
//TIM2->PSCR = 6;
TIM2->PSCR = 3;
/* HSI div by 1 --> Auto-Reload value: 16M / 64 = 1/4M, 1/4M / 1k = 250*/
TIM2->ARRH = 0;
TIM2->ARRL = 250;
/* Counter value: 2, to compensate the initialization of TIMER*/
TIM2->CNTRH = 0;
TIM2->CNTRL = 2;
/* clear update flag */
TIM2->SR1 &= ~TIM_SR1_UIF;
/* Enable Counter */
TIM2->CR1 |= TIM_CR1_CEN;
while(n_ms--)
{
while((TIM2->SR1 & TIM_SR1_UIF) == 0) ;
TIM2->SR1 &= ~TIM_SR1_UIF;
}
/* Disable Counter */
TIM2->CR1 &= ~TIM_CR1_CEN;
}
void print_float(float f)
{
int tmpInt1; // Get the integer (678).
float tmpFrac; // Get fraction (0.0123).
int tmpInt2; // Turn into integer (123).
char buff[30] = {0};
tmpInt1 = (int)f; // Get the integer (678).
printf("int1 %d\n", tmpInt1);
tmpFrac = f - tmpInt1; // Get fraction (0.0123).
tmpInt2 = (int)(tmpFrac * 10000); // Turn into integer (123).
printf("int2 %d\n", tmpInt2);
// Print as parts, note that you need 0-padding for fractional bit.
sprintf (buff, "%d.%04d\n", tmpInt1, tmpInt2);
printf("float %s\n", buff);
}
void LCD_GLASS_Init(void)
{
/*
The LCD is configured as follow:
- clock source = LSE (32.768 KHz)
- Voltage source = Internal
- Prescaler = 2
- Divider = 18 (16 + 2)
- Mode = 1/8 Duty, 1/4 Bias
- LCD frequency = (clock source * Duty) / (Prescaler * Divider)
= 114 Hz ==> Frame frequency = 28,5 Hz*/
CLK_PeripheralClockConfig(CLK_Peripheral_RTC, ENABLE);
/* Enable LCD clock */
CLK_PeripheralClockConfig(CLK_Peripheral_LCD, ENABLE);
CLK_RTCClockConfig(CLK_RTCCLKSource_LSE, CLK_RTCCLKDiv_1);
/* Initialize the LCD */
LCD_Init(LCD_Prescaler_4, LCD_Divider_16, LCD_Duty_1_4,
LCD_Bias_1_3, LCD_VoltageSource_Internal);
/* Mask register*/
LCD_PortMaskConfig(LCD_PortMaskRegister_0, 0xFF);
LCD_PortMaskConfig(LCD_PortMaskRegister_1, 0x0F);
LCD_PortMaskConfig(LCD_PortMaskRegister_2, 0x00);
//LCD_PortMaskConfig(LCD_PortMaskRegister_3, 0xFF);
LCD_ContrastConfig(LCD_Contrast_Level_7);
//LCD_DeadTimeConfig(LCD_DeadTime_0);
LCD_PulseOnDurationConfig(LCD_PulseOnDuration_7);
LCD_Cmd(ENABLE); /*!< Enable LCD peripheral */
}
// A B F G E C P D
void show_c_unit(void)
{
//
//LCD_RAM11=(uint8)(SEG[3]>>4); //CoM3->B[11:4]
LCD->RAM[11]= 0x01; //CoM4->B[11:4]
// //COM1->B[11:8]
LCD->RAM[1] |= 0x04;
// LCD_RAM4=(uint8)(SEG[1]>>4); //COM1->B[11:4]
LCD->RAM[4] |= 0x40; //COM1->B[11:4]
// LCD_RAM8=(uint8)(SEG[2]>>8); //COM2->B[11:8]
LCD->RAM[8] |= 0x04;
LCD->RAM[11] |= 0x80; // 6d
}
static void RTC_Config(void)
{
/* Configures the RTC */
CLK_RTCClockConfig(CLK_RTCCLKSource_LSE, CLK_RTCCLKDiv_1);
CLK_PeripheralClockConfig(CLK_Peripheral_RTC, ENABLE);
RTC_WakeUpClockConfig(RTC_WakeUpClock_CK_SPRE_16bits);
RTC_ITConfig(RTC_IT_WUT, ENABLE);
/* Enable Interrupts*/
enableInterrupts();
}
#if 0
float get_temp(void)
{
int count = 0;
int count_ = 0;
float R_temp = 0;
float the_temp = 0;
int tmpInt1; // Get the integer (678).
float tmpFrac; // Get fraction (0.0123).
int tmpInt2; // Turn into integer (123).
char buff[100] = {0};
int buf_len = sizeof(buff);
float temp;
GPIO_Init(GPIOD, R4_GPIO_PINS, GPIO_Mode_Out_PP_Low_Slow);
GPIO_Init(GPIOD, RNTC_GPIO_PINS, GPIO_Mode_Out_PP_Low_Slow);
GPIO_Init(GPIOD, R6_GPIO_PINS, GPIO_Mode_Out_PP_Low_Slow);
GPIO_WriteBit(GPIOD, R4_GPIO_PINS, RESET);
GPIO_WriteBit(GPIOD, RNTC_GPIO_PINS, RESET);
GPIO_WriteBit(GPIOD, R6_GPIO_PINS, RESET);
printf("5555555555555\r\n");
// cap discharge
delay_ms(12000);
// SET TEMP R6 input
GPIO_Init(GPIOD, RNTC_GPIO_PINS, GPIO_Mode_In_FL_No_IT);
GPIO_Init(GPIOD, R6_GPIO_PINS, GPIO_Mode_In_FL_No_IT);
//GPIO_WriteBit(GPIOD, R4_GPIO_PINS, SET);
GPIO_SetBits(GPIOD, R4_GPIO_PINS);
while(GPIO_ReadInputDataBit(GPIOD, R6_GPIO_PINS) == 0) count++;
printf("count ::: %d\n", count);
GPIO_Init(GPIOD, R4_GPIO_PINS, GPIO_Mode_Out_PP_Low_Slow);
GPIO_Init(GPIOD, RNTC_GPIO_PINS, GPIO_Mode_Out_PP_Low_Slow);
GPIO_Init(GPIOD, R6_GPIO_PINS, GPIO_Mode_Out_PP_Low_Slow);
GPIO_WriteBit(GPIOD, R4_GPIO_PINS, RESET);
GPIO_WriteBit(GPIOD, RNTC_GPIO_PINS, RESET);
GPIO_WriteBit(GPIOD, R6_GPIO_PINS, RESET);
delay_ms(12000);
// read temp time count
GPIO_Init(GPIOD, R4_GPIO_PINS, GPIO_Mode_In_FL_No_IT);
GPIO_Init(GPIOD, R6_GPIO_PINS, GPIO_Mode_In_FL_No_IT);
//GPIO_WriteBit(GPIOD, R4_GPIO_PINS, SET);
GPIO_SetBits(GPIOD, RNTC_GPIO_PINS);
while(GPIO_ReadInputDataBit(GPIOD, R6_GPIO_PINS) == 0) count_++;
printf("count2 %d\n", count_);
// kohm
R_temp = (float) count_ * 10 / count;
//sprintf(buff, "%f", R_temp);
//printf("small %s\n", buff);
tmpInt1 = (int)R_temp; // Get the integer (678).
printf("int1 %d\n", tmpInt1);
tmpFrac = R_temp - tmpInt1; // Get fraction (0.0123).
tmpInt2 = (int)(tmpFrac * 10000); // Turn into integer (123).
printf("int2 %d\n", tmpInt2);
// Print as parts, note that you need 0-padding for fractional bit.
sprintf (buff, "ohm = %d.%04d\n", tmpInt1, tmpInt2);
printf("final %s\n", buff);
the_temp = -3.27653*R_temp + 57.76539;
memset(buff, 0, buf_len);
printf("the temp :");
print_float(the_temp);
return the_temp;
}
#endif
uint16_t get_adc_data(void)
{
uint16_t value = 0;
unsigned int total = 0;
int i = 0;
//for(i = 0; i < 2; i++)
ADC_SoftwareStartConv(ADC1);
while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == FALSE);
value = ADC_GetConversionValue(ADC1);
ADC_ClearFlag(ADC1, ADC_FLAG_EOC);
//value = (uint16_t)(total / 2);
return value;
}
float get_resi(void)
{
int count = 0;
int count2 = 0;
float R_temp = 0;
//float the_temp = 0;
//int tmpInt1; // Get the integer (678).
//float tmpFrac; // Get fraction (0.0123).
//int tmpInt2; // Turn into integer (123).
uint16_t adc1 = 0;
unsigned short int adc_value = 0;
//float temp;
// disable adc
ADC_Cmd(ADC1, DISABLE);
#if 1
GPIO_Init(GPIOD, R4_GPIO_PINS, GPIO_Mode_Out_PP_Low_Slow);
GPIO_Init(GPIOD, RNTC_GPIO_PINS, GPIO_Mode_Out_PP_Low_Slow);
GPIO_Init(GPIOD, R6_GPIO_PINS, GPIO_Mode_Out_PP_Low_Slow);
GPIO_WriteBit(GPIOD, R4_GPIO_PINS, RESET);
GPIO_WriteBit(GPIOD, RNTC_GPIO_PINS, RESET);
GPIO_WriteBit(GPIOD, R6_GPIO_PINS, RESET);
delay_ms(10);
//printf("after some time discharge \n");
GPIO_Init(GPIOD, RNTC_GPIO_PINS, GPIO_Mode_In_FL_No_IT);
GPIO_Init(GPIOD, R6_GPIO_PINS, GPIO_Mode_In_FL_No_IT);
printf("before charge\n");
ADC_ChannelCmd(ADC1, ADC_Channel_8, DISABLE);
ADC_ChannelCmd(ADC1, ADC_Channel_10, ENABLE);
ADC_Cmd(ADC1, ENABLE);
printf("tim1 counter %u\n", TIM1_GetCounter());
TIM1_Cmd(ENABLE); // enable early
delay_ms(2);
ICValue1 = TIM1_GetCounter();
GPIO_SetBits(GPIOD, R4_GPIO_PINS);
//printf("tim1 config end\n");
while(get_adc_data() < 3200) ;
ICValue2 = TIM1_GetCounter(); //us_cnt;
TIM1_SetCounter(0);
TIM1_Cmd(DISABLE);
ADC_Cmd(ADC1, DISABLE);
printf("val %u val2 %u\n", ICValue1, ICValue2);
count = (int)(ICValue1 - ICValue2);
printf("count ::: %d\n", count);
#endif
/// adc
// measure rntc ohm
#if 1
GPIO_Init(GPIOD, R4_GPIO_PINS, GPIO_Mode_Out_PP_Low_Slow);
GPIO_Init(GPIOD, RNTC_GPIO_PINS, GPIO_Mode_Out_PP_Low_Slow);
GPIO_Init(GPIOD, R6_GPIO_PINS, GPIO_Mode_Out_PP_Low_Slow);
GPIO_WriteBit(GPIOD, R4_GPIO_PINS, RESET);
GPIO_WriteBit(GPIOD, RNTC_GPIO_PINS, RESET);
GPIO_WriteBit(GPIOD, R6_GPIO_PINS, RESET);
delay_ms(10);
GPIO_Init(GPIOD, R4_GPIO_PINS, GPIO_Mode_In_FL_No_IT);
GPIO_Init(GPIOD, R6_GPIO_PINS, GPIO_Mode_In_FL_No_IT);
//ICValue1 = TIM1_GetCapture2();
//TIM1->CCR1H = 0;
//TIM1->CCR1L = 0;
//TIM1_SetCounter(0);
ADC_ChannelCmd(ADC1, ADC_Channel_10, DISABLE);
ADC_ChannelCmd(ADC1, ADC_Channel_8, ENABLE); // SP ADD
ADC_Cmd(ADC1, ENABLE);
delay_ms(10);
//printf("value 1 %u\n", ICValue1);
TIM1_Cmd(ENABLE);
delay_ms(1);
ICValue1 = TIM1_GetCounter();//us_cnt;
GPIO_SetBits(GPIOD, RNTC_GPIO_PINS);
//while(GPIO_ReadInputDataBit(GPIOD, R6_GPIO_PINS) == 0) ICValue2 = us_cnt;
while(get_adc_data() < 3200) ;
ICValue2 = TIM1_GetCounter();//us_cnt;
//printf("adc 1 %u\n", adc1);
printf("val %u val2 %u\n", ICValue1, ICValue2);
ADC_Cmd(ADC1, DISABLE);
count2 = (int)(ICValue1 - ICValue2);
#endif
// discharge
GPIO_Init(GPIOD, R4_GPIO_PINS, GPIO_Mode_Out_PP_Low_Slow);
GPIO_Init(GPIOD, RNTC_GPIO_PINS, GPIO_Mode_Out_PP_Low_Slow);
GPIO_Init(GPIOD, R6_GPIO_PINS, GPIO_Mode_Out_PP_Low_Slow);
GPIO_WriteBit(GPIOD, R4_GPIO_PINS, RESET);
GPIO_WriteBit(GPIOD, RNTC_GPIO_PINS, RESET);
GPIO_WriteBit(GPIOD, R6_GPIO_PINS, RESET);
printf("count2 %d\n", count2);
R_temp = (float) count2 * 10.0 / count;
return R_temp;
}
#define Sa 3
#define Sb 2
#define Sc 1
#define Sd 0
#define Se 4
#define Sf 5
#define Sg 6
#define S3a_ON() LCD->RAM[0] |= 1<<4
#define S3a_OFF() LCD->RAM[0] &= ~(1<<4)
#define S3b_ON() LCD->RAM[0] |= 1<<5
#define S3b_OFF() LCD->RAM[0] &= ~(1<<5)
#define S3c_ON() LCD->RAM[7] |= 1<<5
#define S3c_OFF() LCD->RAM[7] &= ~(1<<5)
#define S3d_ON() LCD->RAM[11] |= 1<<1
#define S3d_OFF() LCD->RAM[11] &= ~(1<<1)
#define S3e_ON() LCD->RAM[7] |= 1<<4
#define S3e_OFF() LCD->RAM[7] &= ~(1<<4)
#define S3f_ON() LCD->RAM[4] |= 1<<0 // com1-pin4
#define S3f_OFF() LCD->RAM[4] &= ~(1<<0)
#define S3g_ON() LCD->RAM[4] |= 1<<1 // com1-pin5
#define S3g_OFF() LCD->RAM[4] &= ~(1<<1)
#define S3p_ON() LCD->RAM[11] |= 1<<0 // com3-pin4
#define S3p_OFF() LCD->RAM[11] &= ~(1<<0)
// pos 4
#define S4a_ON() LCD->RAM[0] |= 1<<6
#define S4a_OFF() LCD->RAM[0] &= ~(1<<6)
#define S4b_ON() LCD->RAM[0] |= 1<<7
#define S4b_OFF() LCD->RAM[0] &= ~(1<<7)
#define S4c_ON() LCD->RAM[7] |= 1<<7
#define S4c_OFF() LCD->RAM[7] &= ~(1<<7)
#define S4d_ON() LCD->RAM[11] |= 1<<3
#define S4d_OFF() LCD->RAM[11] &= ~(1<<3)
#define S4e_ON() LCD->RAM[7] |= 1<<6
#define S4e_OFF() LCD->RAM[7] &= ~(1<<6)
#define S4f_ON() LCD->RAM[4] |= 1<<2 // com1-pin6
#define S4f_OFF() LCD->RAM[4] &= ~(1<<2)
#define S4g_ON() LCD->RAM[4] |= 1<<3 // com1-pin7
#define S4g_OFF() LCD->RAM[4] &= ~(1<<3)
#define S4p_ON() LCD->RAM[11] |= 1<<2 // com3-pin4
#define S4p_OFF() LCD->RAM[11] &= ~(1<<2)
// pos 5 pin 8 9
// com0.8 -> ram1.0
#define S5a_ON() LCD->RAM[1] |= 1<<0
#define S5a_OFF() LCD->RAM[1] &= ~(1<<0)
#define S5b_ON() LCD->RAM[1] |= 1<<1
#define S5b_OFF() LCD->RAM[1] &= ~(1<<1)
#define S5c_ON() LCD->RAM[8] |= 1<<1
#define S5c_OFF() LCD->RAM[8] &= ~(1<<1)
#define S5d_ON() LCD->RAM[11] |= 1<<5
#define S5d_OFF() LCD->RAM[11] &= ~(1<<5)
#define S5e_ON() LCD->RAM[8] |= 1<<0
#define S5e_OFF() LCD->RAM[8] &= ~(1<<0)
#define S5f_ON() LCD->RAM[4] |= 1<<4
#define S5f_OFF() LCD->RAM[4] &= ~(1<<4)
#define S5g_ON() LCD->RAM[4] |= 1<<5
#define S5g_OFF() LCD->RAM[4] &= ~(1<<5)
#define S5p_ON() LCD->RAM[11] |= 1<<4
#define S5p_OFF() LCD->RAM[11] &= ~(1<<4)
const unsigned char segment[] = {
(1<<Sa)|(1<<Sb)|(1<<Sc)|(1<<Sd)|(1<<Se)|(1<<Sf), //0
(1<<Sb)|(1<<Sc), //1
(1<<Sa)|(1<<Sb)|(1<<Sd)|(1<<Se)|(1<<Sg), //2
(1<<Sa)|(1<<Sb)|(1<<Sc)|(1<<Sd)|(1<<Sg), //3
(1<<Sb)|(1<<Sc)|(1<<Sf)|(1<<Sg), //4
(1<<Sa)|(1<<Sc)|(1<<Sd)|(1<<Sf)|(1<<Sg), //5
(1<<Sa)|(1<<Sc)|(1<<Sd)|(1<<Se)|(1<<Sf)|(1<<Sg), //6
(1<<Sa)|(1<<Sb)|(1<<Sc), //7
(1<<Sa)|(1<<Sb)|(1<<Sc)|(1<<Sd)|(1<<Se)|(1<<Sf)|(1<<Sg),//8
(1<<Sa)|(1<<Sb)|(1<<Sc)|(1<<Sd)|(1<<Sf)|(1<<Sg) //9
};
void display(unsigned char BCD, unsigned char x)
{
unsigned char seg;
seg = segment[BCD];
switch(x)
{
case 0:
break;
case 3:
if(seg & (1<<Sa)) S3a_ON(); else S3a_OFF();
if(seg & (1<<Sb)) { S3b_ON(); } else { S3b_OFF(); }
if(seg & (1<<Sc)) { S3c_ON(); } else { S3c_OFF(); }
if(seg & (1<<Sd)) { S3d_ON(); } else { S3d_OFF(); }
if(seg & (1<<Se)) { S3e_ON(); } else { S3e_OFF(); }
if(seg & (1<<Sf)) { S3f_ON(); } else { S3f_OFF(); }
if(seg & (1<<Sg)) { S3g_ON(); } else { S3g_OFF(); }
break;
case 4:
if(seg & (1<<Sa)) S4a_ON(); else S4a_OFF();
if(seg & (1<<Sb)) { S4b_ON(); } else { S4b_OFF(); }
if(seg & (1<<Sc)) { S4c_ON(); } else { S4c_OFF(); }
if(seg & (1<<Sd)) { S4d_ON(); } else { S4d_OFF(); }
if(seg & (1<<Se)) { S4e_ON(); } else { S4e_OFF(); }
if(seg & (1<<Sf)) { S4f_ON(); } else { S4f_OFF(); }
if(seg & (1<<Sg)) { S4g_ON(); } else { S4g_OFF(); }
break;
case 5:
if(seg & (1<<Sa)) S5a_ON(); else S5a_OFF();
if(seg & (1<<Sb)) { S5b_ON(); } else { S5b_OFF(); }
if(seg & (1<<Sc)) { S5c_ON(); } else { S5c_OFF(); }
if(seg & (1<<Sd)) { S5d_ON(); } else { S5d_OFF(); }
if(seg & (1<<Se)) { S5e_ON(); } else { S5e_OFF(); }
if(seg & (1<<Sf)) { S5f_ON(); } else { S5f_OFF(); }
if(seg & (1<<Sg)) { S5g_ON(); } else { S5g_OFF(); }
break;
}
}
float fastlog2(float x)
{
union { float f; uint32_t i; } vx = { x };
union { uint32_t i; float f; } mx = { (vx.i & 0x007FFFFF) | 0x3f000000 };
float y = vx.i;
y *= 1.1920928955078125e-7f;
return y - 124.22551499f
- 1.498030302f * mx.f
- 1.72587999f / (0.3520887068f + mx.f);
}
void EXTI_setup(void)
{
ITC_DeInit();
ITC_SetSoftwarePriority(EXTI7_IRQn, ITC_PriorityLevel_0);
EXTI_DeInit();
EXTI_SetPinSensitivity(GPIO_Pin_7, EXTI_Trigger_Falling);
//EXTI_SetTLISensitivity(EXTI_TLISENSITIVITY_FALL_ONLY);
enableInterrupts();
}
void RX_EXTI_setup(void)
{
//ITC_DeInit();
ITC_SetSoftwarePriority(EXTI2_IRQn, ITC_PriorityLevel_0);
//EXTI_DeInit();
EXTI_SetPinSensitivity(GPIO_Pin_2, EXTI_Trigger_Falling);
//EXTI_SetTLISensitivity(EXTI_TLISENSITIVITY_FALL_ONLY);
enableInterrupts();
}
void ADC1_setup(void)
{
ADC_DeInit(ADC1);
CLK_PeripheralClockConfig(CLK_Peripheral_ADC1, ENABLE);
ADC_Init(ADC1, ADC_ConversionMode_Continuous, ADC_Resolution_12Bit, ADC_Prescaler_2);
ADC_SamplingTimeConfig(ADC1, ADC_Group_SlowChannels, ADC_SamplingTime_192Cycles);
/* Enable ADC1 */
ADC_ChannelCmd(ADC1, ADC_Channel_10, ENABLE);
ADC_Cmd(ADC1, ENABLE);
/* Enable ADC1 Channel 3 */
}
void usart_setup(void)
{
GPIO_ExternalPullUpConfig(GPIOC, GPIO_Pin_3, ENABLE);
GPIO_ExternalPullUpConfig(GPIOC, GPIO_Pin_2, ENABLE);
CLK_PeripheralClockConfig(CLK_Peripheral_USART1, ENABLE);
USART_DeInit(USART1);
USART_Init(USART1, (uint32_t)9600, 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);
}
#define I2C_ENABLE 1
void main(void)
{
float temperature = 0;
const float Rp=10000.0; //10K
const float T2 = (273.15+25.0); //T2 298.15
const float Bx = 3380.0; //B
const float Ka = 273.15;
float Rt = 0;
double d_num = 0;
float ln_value = 0;
int tmp_int1 = 0;
int tmp_int2 = 0;
float tmp_frac = 0;
int i = 0;
char buf[10] = {0};
int buf_len = sizeof(buf);
//uint8_t write_data = 20;
uint8_t *p_write = NULL;
//uint16_t read_cnt = 1;
short int rd_data = 0;
short int write_data = 204;
uint16_t read_cnt = 2;
//short int rd_data = 0;
uint16_t write_addr = 0;
uint16_t k_addr = 0;
//write to eeprom temperature
signed short int temp100 = 0;
uint8_t ee_data[2] = {0};
CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_8);
//for(i = 0; i < 10; i++)
//delay_ms(1000 * 60);
//CLK_GetClockFreq(void)
TIM1_Config();
EXTI_setup();
RTC_Config();
ADC1_setup();
//PE7
GPIO_Init(GPIOE, GPIO_Pin_7, GPIO_Mode_In_PU_IT);//GPIO_MODE_IN_PU_IT);
//GPIO_Init(GPIOE, GPIO_Pin_7, GPIO_Mode_In_FL_IT);
usart_setup();
FLASH_DeInit();
// usb pluged in flag
FLASH_Unlock(FLASH_MemType_Data);
FLASH_EraseByte(0x1000);
delay_ms(20);
//FLASH_ProgramByte(0x1000, 0x66);
//delay_ms(20);
FLASH_Lock(FLASH_MemType_Data);
printf("eeprom %u\n", (unsigned int)FLASH_ReadByte(0x1000));
//if(FLASH_ReadByte(0x1000) == 0x66)
// printf("equ \n");
#if 1
LCD_GLASS_Init();
delay_ms(10);
LCD->CR4 &= (uint8_t)~LCD_CR4_PAGECOM;
show_c_unit();
#endif
printf("gpio init\r\n");
//printf("c freq %u\n", CLK_GetClockFreq());
#if I2C_ENABLE
sEE_Init();
#endif
#if 0
//memcpy((void *)ee_data, (void *)&write_data, 2);
//printf("write data %02x %02x\n", (unsigned int)ee_data[0], (unsigned int)ee_data[1]);
sEE_WriteBuffer((uint8_t *)&write_data, 0x55, 2);
//sEE_WriteBuffer(&ee_data[0], 0x55, 2);
//sEE_WriteBuffer(&ee_data[1], 0x56, 1);
sEE_ReadBuffer((uint8_t *)&rd_data, 0x55, &read_cnt);
printf("read data %02x %02x\n", (unsigned int)*(uint8_t *)&rd_data, (unsigned int)*((uint8_t *)&rd_data + 1));
printf("read data %d\n", rd_data);
#endif
//display(8, 3);
//display(8, 4);
//display(8, 5);
while(1)
{
//printf("measure begin\n");
if(FLASH_ReadByte(0x1000) == 0x66)
{
printf("equ \n");
printf("will loop and stop record temp\n");
printf("***print data:\n");
//read eeprom and printf
for(k_addr = 0; k_addr < write_addr; k_addr +=2)
{
printf("%u-", k_addr);
sEE_ReadBuffer((uint8_t *)&rd_data, k_addr, &read_cnt);
printf("%f -\n", (float)rd_data/100);
rd_data = 0;
read_cnt = 2;
}
continue;
}
printf("pos %d\n", pos);
if(pos > 0)
{
for(i = 0; i < pos && i < 20; i++)
printf("%c", RX_buf[i]);
printf("\n");
RX_buf[sizeof(RX_buf) - 1] = 0;
if(strstr(RX_buf, "ux"))
{
//stop_flag = 0;
printf("***print data:\n");
//read eeprom and printf
for(k_addr = 0; k_addr < write_addr; k_addr +=2)
{
printf("%u-", k_addr);
sEE_ReadBuffer((uint8_t *)&rd_data, k_addr, &read_cnt);
printf("%f -\n", (float)rd_data/100);
rd_data = 0;
read_cnt = 2;
}
// flash record status
//// adddddd internel eeprom write
//wfi();
//wfe();
}
pos = 0;
memset(RX_buf, 0, sizeof(RX_buf));
}
printf("measure resi\n");
Rt = get_resi();
printf("end get_resi\n");
#if 1
ln_value = log(Rt/10.0);
temperature = 1/(ln_value/Bx + (1/T2));
temperature = (temperature - Ka);
#endif
run_counter++;
//temperature = -2.96735*Rt + 54.67359;
printf("temp %f\n", temperature);
//print_float(temperature);
temp100 = (signed short int)(temperature * 100);
printf("temp100 %d\n", temp100);
//printf("write address %u\n", write_addr);
#if I2C_ENABLE
//USART_ClearITPendingBit(USART1, USART_IT_RXNE);
//USART_ITConfig(USART1, USART_IT_RXNE, DISABLE);
USART_Cmd(USART1, DISABLE);
p_write = (uint8_t *)&temp100;
sEE_WriteBuffer(p_write, write_addr, 2);
//USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_Cmd(USART1, ENABLE);
//printf("i2c write done\n");
#endif
if(temperature > 0)
{
memset(buf, 0, buf_len);
//sprintf(buf, "%4.1f", temperature);
tmp_int1 = (int)temperature;
tmp_int1 %= 100;
//printf("int1 %d\n", tmp_int1);
tmp_frac = temperature - tmp_int1; // Get fraction (0.0123).
tmp_int2 = (int)(tmp_frac * 10);
//printf("int2 %d\n", tmp_int2);
sprintf(buf, "%02d%01d", tmp_int1, tmp_int2);
printf("formatted temp %s\n", buf);
#if 1
display(buf[0] - '0', 3);
display(buf[1] - '0', 4);
S5p_ON();
display(buf[2] - '0', 5);
//show_c_unit();
#endif
}
else
{
memset(buf, 0, buf_len);
temperature = -temperature;
tmp_int1 = (int)temperature;
tmp_int1 %= 100;
printf("int1 %d\n", tmp_int1);
tmp_frac = temperature - tmp_int1; // Get fraction (0.0123).
tmp_int2 = (int)(tmp_frac * 10);
printf("int2 %d\n", tmp_int2);
sprintf(buf, "%02d%01d", tmp_int1, tmp_int2);
printf("formatted temp %s\n", buf);
display(buf[0] - '0', 3);
display(buf[1] - '0', 4);
S5p_ON();
display(buf[2] - '0', 5);
}
//printf("rtc sleep 20s\n");
RTC_SetWakeUpCounter(10); // 300s
RTC_WakeUpCmd(ENABLE);
if(run_counter == 2)
{
printf("**disable lcd\n");
LCD_Cmd(DISABLE);
CLK_PeripheralClockConfig(CLK_Peripheral_LCD, DISABLE);
}
else if(run_counter == 0 | run_counter == 1)
{
printf("to enable lcd clock\n");
CLK_PeripheralClockConfig(CLK_Peripheral_LCD, ENABLE);
LCD_Cmd(ENABLE);
delay_ms(5);
}
printf("ready to halt\n");
USART_DeInit(USART1);
//GPIO_Init(GPIOC, GPIO_Pin_2, GPIO_Mode_In_FL_No_IT); //RESET usart gpio
// set io low
GPIO_Init(GPIOC, GPIO_Pin_3, GPIO_Mode_Out_PP_Low_Slow);
// pc2 set interrupt
GPIO_Init(GPIOC, GPIO_Pin_2, GPIO_Mode_In_PU_IT);
RX_EXTI_setup();
//TIM1_Cmd(DISABLE);
//PWR_UltraLowPowerCmd(ENABLE);
EXTI_ClearITPendingBit(EXTI_IT_Pin2);
halt();
RTC_WakeUpCmd(DISABLE);
RTC_ClearITPendingBit(RTC_IT_WUT);
//PWR_UltraLowPowerCmd(DISABLE);
//LCD_ON();
write_addr += 2;
usart_setup();
//LCD_GLASS_Init();
}
}
PUTCHAR_PROTOTYPE
{
/* Write a character to the USART */
USART_SendData8(USART1, c);
/* Loop until the end of transmission */
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
return (c);
}
#if 0
/**
* @brief Retargets the C library scanf function to the USART.
* @param[in] None
* @retval char Character to Read
* @par Required preconditions:
* - None
*/
GETCHAR_PROTOTYPE
{
int c = 0;
/* Loop until the Read data register flag is SET */
while (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET);
c = USART_ReceiveData8(USART1);
return (c);
}
#endif
static void TIM1_Config(void)
{
CLK_PeripheralClockConfig(CLK_Peripheral_TIM1, ENABLE);
//TIM1_DeInit();
// 2m/ 40
TIM1_TimeBaseInit(99, TIM1_CounterMode_Up, 0xffff, 0); //timer freq = (clock CPU/16) -> 1bit = 1uS -> 92*1uS=92uS
//TIM1_ITConfig(TIM1_IT_Update, ENABLE);
//TIM1_Cmd(ENABLE);
TIM1->EGR |= (1 << TIM1_EGR_UG);
}
void LCD_OFF(void)
{
//CLK_PeripheralClockConfig(CLK_Peripheral_RTC, DISABLE);
CLK_PeripheralClockConfig(CLK_Peripheral_LCD, DISABLE);
}
void LCD_ON(void)
{
//CLK_PeripheralClockConfig(CLK_Peripheral_RTC, ENABLE);
CLK_PeripheralClockConfig(CLK_Peripheral_LCD, ENABLE);
}
void LCD_Clear(void)
{
uint8_t i=0;
for(i=0;i<LCD_RAMRegister_13;i++)
{
LCD_PageSelect(LCD_PageSelection_FirstPage);
LCD->RAM[i]=0;
//LCD_PageSelect(LCD_PageSelection_SecondPage);
//LCD->RAM[i]=0;
}
}
#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 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****/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment