#include "main.h"
#include "RCC\bsp_rcc.h"
#include "KEY_LED\bsp_key_led.h"
#include "LCD\bsp_lcd.h"
#include "UART\bsp_uart.h"
#include "I2C\bsp_i2c.h"
#include "ADC\bsp_adc.h"
#include "TIM\bsp_tim.h"
#include "RTC\bsp_rtc.h"
__IO uint32_t uwTick_Key_Set_Point = 0;
__IO uint32_t uwTick_Led_Set_Point = 0;
__IO uint32_t uwTick_Lcd_Set_Point = 0;
__IO uint32_t uwTick_Usart_Set_Point = 0;
uint8_t ucKey_Val, unKey_Down, ucKey_Up, ucKey_Old;
uint8_t ucLed;
uint8_t Lcd_Disp_String[21];
uint16_t counter = 0;
uint8_t str[40];
uint32_t rx_buffer;
uint8_t EEPROM_String_1[5] = {0x11,0x22,0x33,0x44,0x55};
uint8_t EEPROM_String_2[5] = {0};
uint8_t RES_4017;
uint16_t PWM_T_Count;
uint16_t PWM_D_Count;
float PWM_Duty;
RTC_TimeTypeDef H_M_S_Time;
RTC_DateTypeDef Y_M_D_Date;
void Key_Proc(void);
void Led_Proc(void);
void Lcd_Proc(void);
void Usart_Proc(void);
int main(void)
{
HAL_Init();
SystemClock_Config();
KEY_LED_Init();
LCD_Init();
LCD_Clear(White);
LCD_SetBackColor(White);
LCD_SetTextColor(Blue);
UART1_Init();
I2CInit();
ADC1_Init();
ADC2_Init();
BASIC_TIM6_Init();
PWM_INPUT_TIM2_Init();
SQU_OUTPUT_TIM15_Init();
PWM_OUTPUT_TIM3_Init();
PWM_OUTPUT_TIM17_Init();
iic_24c02_write(EEPROM_String_1, 0, 5);
HAL_Delay(1);
iic_24c02_read(EEPROM_String_2, 0, 5);
write_resistor(0x77);
RES_4017 = read_resistor();
HAL_UART_Receive_IT(&huart1, (uint8_t *)(&rx_buffer), 4);
HAL_TIM_Base_Start_IT(&htim6);
HAL_TIM_Base_Start(&htim2);
HAL_TIM_IC_Start_IT(&htim2,TIM_CHANNEL_1);
HAL_TIM_IC_Start_IT(&htim2,TIM_CHANNEL_2);
HAL_TIM_OC_Start_IT(&htim15,TIM_CHANNEL_1);
HAL_TIM_PWM_Start(&htim3,TIM_CHANNEL_1);
HAL_TIM_PWM_Start(&htim17,TIM_CHANNEL_1);
RTC_Init();
while (1)
{
Key_Proc();
Led_Proc();
Lcd_Proc();
Usart_Proc();
}
}
void Key_Proc(void)
{
if((uwTick - uwTick_Key_Set_Point)<50) return;
uwTick_Key_Set_Point = uwTick;
ucKey_Val = Key_Scan();
unKey_Down = ucKey_Val & (ucKey_Old ^ ucKey_Val);
ucKey_Up = ~ucKey_Val & (ucKey_Old ^ ucKey_Val);
ucKey_Old = ucKey_Val;
if(unKey_Down == 4)
{
ucLed = 0x88;
}
if(unKey_Down == 3)
{
ucLed = 0x00;
}
}
void Led_Proc(void)
{
if((uwTick - uwTick_Led_Set_Point)<200) return;
uwTick_Led_Set_Point = uwTick;
LED_Disp(ucLed);
}
void Lcd_Proc(void)
{
if((uwTick - uwTick_Lcd_Set_Point)<100) return;
uwTick_Lcd_Set_Point = uwTick;
sprintf((char *)Lcd_Disp_String, "EE:%x%x%x%x%x R:%x",EEPROM_String_2[0],EEPROM_String_2[1],EEPROM_String_2[2],EEPROM_String_2[3],EEPROM_String_2[4], RES_4017);
LCD_DisplayStringLine(Line0, Lcd_Disp_String);
sprintf((char *)Lcd_Disp_String, "R38_Vol:%6.3fV",((((float)getADC1())/4096)*3.3));
LCD_DisplayStringLine(Line1, Lcd_Disp_String);
sprintf((char *)Lcd_Disp_String, "R37_Vol:%6.3fV",((((float)getADC2())/4096)*3.3));
LCD_DisplayStringLine(Line2, Lcd_Disp_String);
sprintf((char *)Lcd_Disp_String, "R40P:%05dHz,%4.1f%%",(unsigned int)(1000000/PWM_T_Count),PWM_Duty*100);
LCD_DisplayStringLine(Line3, Lcd_Disp_String);
HAL_RTC_GetTime(&hrtc, &H_M_S_Time, RTC_FORMAT_BIN);
HAL_RTC_GetDate(&hrtc, &Y_M_D_Date, RTC_FORMAT_BIN);
sprintf((char *)Lcd_Disp_String, "Time:%02d-%02d-%02d",(unsigned int)H_M_S_Time.Hours,(unsigned int)H_M_S_Time.Minutes,(unsigned int)H_M_S_Time.Seconds);
LCD_DisplayStringLine(Line4, Lcd_Disp_String);
}
void Usart_Proc(void)
{
}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
}
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if(htim->Instance==TIM6)
{
if(++counter == 10)
{
counter = 0;
sprintf(str, "Hello,world.\r\n");
HAL_UART_Transmit(&huart1,(unsigned char *)str, strlen(str), 50);
}
}
}
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
{
if(htim->Instance==TIM2)
{
if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1)
{
PWM_T_Count = HAL_TIM_ReadCapturedValue(htim,TIM_CHANNEL_1)+1;
PWM_Duty = (float)PWM_D_Count/PWM_T_Count;
}
else if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_2)
{
PWM_D_Count = HAL_TIM_ReadCapturedValue(htim,TIM_CHANNEL_2)+1;
}
}
}
void HAL_TIM_OC_DelayElapsedCallback(TIM_HandleTypeDef *htim)
{
if(htim->Instance==TIM15)
{
if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1)
{
__HAL_TIM_SET_COMPARE(htim,TIM_CHANNEL_1,(__HAL_TIM_GetCounter(htim)+500));
}
}
}
void Error_Handler(void)
{
__disable_irq();
while (1)
{
}
}
void iic_24c02_write(uint8_t *pucBuf, uint8_t ucAddr, uint8_t ucNum)
{
I2CStart();
I2CSendByte(0xa0);
I2CWaitAck();
I2CSendByte(ucAddr);
I2CWaitAck();
while(ucNum--)
{
I2CSendByte(*pucBuf++);
I2CWaitAck();
}
I2CStop();
delay1(500);
}
void iic_24c02_read(uint8_t *pucBuf, uint8_t ucAddr, uint8_t ucNum)
{
I2CStart();
I2CSendByte(0xa0);
I2CWaitAck();
I2CSendByte(ucAddr);
I2CWaitAck();
I2CStart();
I2CSendByte(0xa1);
I2CWaitAck();
while(ucNum--)
{
*pucBuf++ = I2CReceiveByte();
if(ucNum)
I2CSendAck();
else
I2CSendNotAck();
}
I2CStop();
}
void write_resistor(uint8_t value)
{
I2CStart();
I2CSendByte(0x5E);
I2CWaitAck();
I2CSendByte(value);
I2CWaitAck();
I2CStop();
}
uint8_t read_resistor(void)
{
uint8_t value;
I2CStart();
I2CSendByte(0x5F);
I2CWaitAck();
value = I2CReceiveByte();
I2CSendNotAck();
I2CStop();
return value;
}
|