标题:
STM8S与HT1621b的24小时时钟带温度功能
[打印本页]
作者:
51黑mcu
时间:
2016-6-18 01:32
标题:
STM8S与HT1621b的24小时时钟带温度功能
#include "stm8s.h"
#include "ht1621.h"
unsigned char const smg_1[]={0x0a,0x00,0x06,0x04,0x0c,0x0c,0x0e,0x08,0x0e,0x0c};//0123456789--fge
unsigned char const smg_2[]={0xf0,0x60,0xd0,0xf0,0x60,0xb0,0xb0,0xe0,0xf0,0xf0};//abcd
//unsigned char const smg_2[]={0x50,0x00,0x60,0x20,0x30,0x30,0x70,0x00,0x70,0x30};//0123456789
//unsigned char const smg_1[]={0x0f,0x06,0x0b,0x0f,0x06,0x0d,0x0d,0x0e,0x0f,0x0f};
unsigned char smgys[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
unsigned char QX[]={0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
unsigned char QL[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
unsigned char adc1_value[];
unsigned char const Tab_Temp[]=
//1 2 3 4 5 6 7 8 9 0 1 2 3 4 5//比实际温度高30度
{ 0, 0, 5, 5, 10, 10, 15, 15, 20, 20, 25, 25, 25, 30, 30, 31, /* ADC: 0~15 */
32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, /* ADC: 16~31 */
48, 49, 49, 50, 50, 51, 51, 52, 53, 54, 55, 56, 56, 57, 57, 58, /* ADC: 32~47 */
58, 59, 60, 60, 61, 61, 62, 63, 63, 64, 64, 65, 65, 65, 66, 66, /* ADC: 48~63 */
67, 67, 68, 68, 69, 69, 70, 70, 71, 71, 72, 72, 73, 73, 74, 74, /* ADC: 64~79 */
75, 75, 76, 76, 77, 77, 78, 78, 78, 79, 79, 80, 80, 81, 81, 82, /* ADC: 80~95 */
82, 83, 83, 84, 84, 85, 85, 85, 86, 86, 87, 87, 88, 88, 89, 89, /* ADC: 96~111 */
89, 90, 90, 90, 91, 91, 92, 92, 93, 93, 93, 94, 94, 95, 95, 95, /* ADC: 112~127 */
96, 96, 97, 97, 98, 98, 99, 99, 100, 100, 100, 101, 101, 102, 102, 103, /* ADC: 128~143 */
103, 104, 104, 105, 105, 106, 106, 107, 107, 108, 108, 109, 109, 110, 110, 110, /* ADC: 144~159 */
111, 112, 113, 113, 113, 114, 114, 115, 115, 116, 116, 117, 118, 118, 119, 119, /* ADC: 160~175 */
120, 120, 121, 122, 122,123,124, 124, 125, 126, 127, 128, 128, 129, 129, 130, /* ADC: 176~191 */
130, 130, 79, 80, 81, 82, 83, 83, 84, 85, 86, 86, 87, 88, 89, 89, /* ADC: 192~207 */
89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100,0, 0, 0, 0, /* ADC: 208~223 */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* ADC: 224~239 */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* ADC: 240~255 */
};
unsigned char Hour,Min,Sec,Hour_H,Hour_L,Min_H,Min_L,Sec_H,Sec_L,Temp_HBit,Temp_LBit;
unsigned char HalfSec_fig,Half_SecFlag=1;
unsigned char Key_fig;
unsigned int Temp_value;
extern unsigned int u16_adc1_value,Temp_adc;
FlagStatus fs;
unsigned char Flash_Fig;
extern unsigned char Half_SecFlag,Key_mode,Key_Long_fig;
void DelayMS(u16 iMs)
{
u16 i,j;
for(i=0;i<iMs;i++)
{
IWDG_ReloadCounter();
for(j=0;j<65;j++)
{
Delay(1);
IWDG_ReloadCounter();
}
}
}
void Time_Data_process(void)
{
Hour_H=Hour/10;
Hour_L=Hour%10;
Min_H=Min/10;
Min_L=Min%10;
Sec_H=Sec/10;
Sec_L=Sec%10;
}
void Display(void)
{
if(Flash_Fig)
{
if(Key_mode==1)
{
smgys[4]=smg_1[Min_L]|smg_2[Min_H];smgys[5]=smg_2[Min_L];
smgys[3]=smg_1[Min_H]|smg_2[Hour_L]|0x01;
}
if(Key_mode==2)
{
smgys[2]=smg_1[Hour_L]|smg_2[Hour_H];smgys[3]=smg_1[Min_H]|smg_2[Hour_L]|0x01;
smgys[1]=smg_1[Hour_H];
}
if(Key_mode==0)
{
smgys[4]=smg_1[Min_L]|smg_2[Min_H];smgys[5]=smg_2[Min_L];
smgys[3]=smg_2[Hour_L]|smg_1[Min_H]|0x01;
if(Hour_H==0)
{smgys[1]=0;smgys[2]=smg_1[Hour_L];}
else
{
smgys[2]=smg_1[Hour_L]|smg_2[Hour_H];
smgys[1]=smg_1[Hour_H];
}
}
}
else
{ if((Key_mode==1)&(Key_Long_fig==0))
{
smgys[4]=0;smgys[5]=0;
smgys[3]=smg_2[Hour_L]&0xfe;
smgys[2]=smg_1[Hour_L]|smg_2[Hour_H];
smgys[1]=smg_1[Hour_H];
}
if((Key_mode==2)&(Key_Long_fig==0))
{
smgys[2]=0;smgys[3]=smg_1[Min_H];
smgys[1]=0;smgys[4]=smg_1[Min_L]|smg_2[Min_H];smgys[5]=smg_2[Min_L];
}
if(Key_mode==0)
{
smgys[4]=smg_1[Min_L]|smg_2[Min_H];smgys[5]=smg_2[Min_L];
//smgys[3]=(smg_1[Hour_H]|smg_2[Hour_L]|smg_1[Min_H])&0xfe;
smgys[3]=smg_2[Hour_L]|smg_1[Min_H]&0xfe;
if(Hour_H==0)
{
smgys[2]=smg_1[Hour_L];
smgys[1]=0;
}
else
{
smgys[2]=smg_1[Hour_L]|smg_2[Hour_H];
smgys[1]=smg_1[Hour_H];
}
}
}
/*
if(Halt_flag)
{
Ht1621WrAllData(8,QL,20);
Halt_flag=0;GPIO_WriteHigh(GPIOB, GPIO_PIN_0);
halt();
} */
}
void GPIO_int(void)
{
GPIO_Init(GPIOA, GPIO_PIN_HNIB, GPIO_MODE_OUT_PP_HIGH_FAST);
GPIO_Init(GPIOC, GPIO_PIN_1, GPIO_MODE_IN_PU_IT);
EXTI_SetExtIntSensitivity(EXTI_PORT_GPIOC, EXTI_SENSITIVITY_FALL_ONLY);
GPIO_Init(GPIOC, GPIO_PIN_2, GPIO_MODE_OUT_PP_LOW_FAST);
GPIO_Init(GPIOB, GPIO_PIN_0, GPIO_MODE_IN_FL_NO_IT);
GPIO_Init(GPIOE, GPIO_PIN_6, GPIO_MODE_OUT_PP_HIGH_FAST);
}
void TIM4_int(void)
{
TIM4_TimeBaseInit(TIM4_PRESCALER_32,249);//1ms_8MHz 8/32=250KHz=0.004ms*250=1ms
//TIM4_ClearFlag(TIM4_FLAG_UPDATE);
TIM4_SetCounter(249);
TIM4_ITConfig(TIM4_IT_UPDATE, ENABLE);
TIM4_Cmd(ENABLE);
}
void TIM2_int(void)
{
TIM2_TimeBaseInit(TIM2_PRESCALER_8, 1000);
TIM2_OC2Init(TIM2_OCMODE_PWM1, TIM2_OUTPUTSTATE_ENABLE, 0x0000, TIM2_OCPOLARITY_HIGH);
TIM2_OC2PreloadConfig(ENABLE);
TIM2_ARRPreloadConfig(ENABLE);
TIM2_Cmd(ENABLE);
}
void TIM1_int(void)
{
TIM1_TimeBaseInit(79, TIM1_COUNTERMODE_UP,50000,0);//500ms_8MHz
TIM1_ITConfig(TIM1_IT_UPDATE, ENABLE);
TIM1_GenerateEvent(TIM1_EVENTSOURCE_UPDATE);
//TIM1_OC2Init(TIM1_OCMODE_PWM1, TIM1_OUTPUTSTATE_ENABLE, TIM1_OUTPUTNSTATE_ENABLE, 0, TIM1_OCPOLARITY_HIGH, TIM1_OCNPOLARITY_HIGH, TIM1_OCIDLESTATE_SET, TIM1_OCNIDLESTATE_RESET);
//TIM1_OC2Init(TIM1_OCMODE_PWM1, TIM1_OUTPUTSTATE_ENABLE, TIM1_OUTPUTNSTATE_DISABLE, 0x0000, TIM1_OCPOLARITY_HIGH, TIM1_OCNPOLARITY_HIGH, TIM1_OCIDLESTATE_RESET, TIM1_OCNIDLESTATE_SET);
//TIM1_CtrlPWMOutputs(ENABLE);
//TIM1_OC2PreloadConfig(ENABLE);
//TIM1_ARRPreloadConfig(ENABLE);
TIM1_Cmd(ENABLE);
}
void Beep_int(void)
{
BEEP_DeInit();
BEEP_LSICalibrationConfig(128000);
BEEP_Init(BEEP_FREQUENCY_2KHZ);
}
void ClkInit(void)
{
ErrorStatus clk_return_status;
clk_return_status = CLK_ClockSwitchConfig(CLK_SWITCHMODE_AUTO, CLK_SOURCE_HSE, DISABLE, CLK_CURRENTCLOCKSTATE_DISABLE);
if (clk_return_status == SUCCESS) //SUCCESS or ERROR
{};
}
void Adc_init(void)
{
ADC1_DeInit();
ADC1_Init(ADC1_CONVERSIONMODE_CONTINUOUS, ADC1_CHANNEL_0, ADC1_PRESSEL_FCPU_D2, ADC1_EXTTRIG_TIM, DISABLE, ADC1_ALIGN_RIGHT, ADC1_SCHMITTTRIG_CHANNEL0, DISABLE);
ADC1_ITConfig(ADC1_IT_EOCIE, DISABLE);
ADC1_Cmd(ENABLE);
ADC1_StartConversion();
}
void WWdog_init(void)
{
fs = RST_GetFlagStatus(RST_FLAG_WWDGF);
if(fs != RESET)
{
RST_ClearFlag(RST_FLAG_WWDGF);
GPIO_WriteHigh(GPIOC, GPIO_PIN_2);
}
WWDG_Init(0x7f, 0x7f);
}
void IWdog_init(void)
{
IWDG_Enable();
IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);
IWDG_SetPrescaler(IWDG_Prescaler_16);
IWDG_SetReload(0xff);
}
void Refresh_WWdog(void)
{
u8 u8_value;
u8_value = (u8)(WWDG_GetCounter()&0x7f);
if(u8_value<0x7f)
{
WWDG_SetCounter(0x7f);
}
}
void Adc_process(void)
{
ADC1->CR1|=0x02;
ADC1_StartConversion();
unsigned char count=0;
while(count<10)
{
while (ADC1_GetFlagStatus(ADC1_FLAG_EOC)== RESET) // SET or RESET
ADC1_ClearFlag(ADC1_FLAG_EOC);
adc1_value[count] = ADC1_GetConversionValue();
count++;
}
ADC1->CR1&=~0x02;
unsigned char i,j;
unsigned int temp;
for(i=10;i>=1;i--)
{
for(j=0;j<(i-1);j++)
{
if(adc1_value[j]>adc1_value[j+1])
{
temp=adc1_value[j];
adc1_value[j]=adc1_value[j+1];
adc1_value[j+1]=temp;
}
}
}
Temp_adc=0;
for(i=2;i<=7;i++)
Temp_adc+=adc1_value[i];
Temp_adc/=6;
Temp_value=Tab_Temp[Temp_adc/4];
Temp_value=Temp_value-30;
}
void Temp_Data_process(void)
{
Temp_HBit=Temp_value/10;
Temp_LBit=Temp_value%10;
}
void main(void)
{
/* Infinite loop */
//u8 i;
ClkInit();
GPIO_int();
Ht1621_Init();
Ht1621WrAllData(8,QL,7);
Beep_int();
//Half_SecFlag=1;
TIM4_int();
//TIM2_int();
TIM1_int();
Adc_init();
//WWdog_init();
IWdog_init();
rim();
Ht1621WrAllData(8,QX,7);
while (1)
{
Time_Data_process();
//Adc_process();
Temp_Data_process();
if(Sec/10==0)
{
smgys[1]=smg_1[Temp_HBit];smgys[2]=smg_2[Temp_HBit]|smg_1[Temp_LBit];
//smgys[3]=smg_2[Temp_LBit]|0x0a;smgys[4]=0x9c;smgys[5]=0xc0;
smgys[3]=smg_2[Temp_LBit]|0x0c;smgys[4]=0xca;smgys[5]=0x90;
}
else
{ Display();}
Ht1621WrAllData(8,smgys,7);
IWDG_ReloadCounter();
}
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval : None
*/
void assert_failed(u8* file, u32 line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* Infinite loop */
while (1)
{
}
}
#endif
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
复制代码
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1