各位大师好,我写了这样一段代码,通过串口数据来更改RTC定时器的年,月,日,时,分,秒.现在的情况是发送串口指令后,RTC的时间确实被改了,我用MDK查看RTC->CNTL寄存器的值也在每秒发生变化,但就是RTC_Get()函数取不出RTC->CNTL的值,观察了RTC的全部寄存器也没看到发串口指令前和发串口指令后有什么不同,百思不得其解,跪求大师们帮忙给看看,已经在这个坑里爬了两天了
这是主函数代码:
#include <string.h>
#include "led.h"
#include "delay.h"
#include "sys.h"
#include "usart.h"
#include "key.h"
#include "adc.h"
#include "timer.h"
#include "rtc.h"
u8 flag1;
u8 flag2;
u8 flag3;
u32 timecount=0;
u32 temp=0;
u16 temp1=0;
u16 Current_year;
u8 Current_yearH;
u8 Current_yearL;
u8 Current_month;
u8 Current_day;
u8 Current_hour;
u8 Current_min;
u8 Current_sec;
u8 get_10sec_flag=0;
u16 Target_TDS;
u8 Target_TDSH;
u8 Target_TDSL;
u8 Target_PH;
u16 Target_Light;
u8 Target_LightH;
u8 Target_LightL;
u16 Current_TDS;
u8 Current_TDSH;
u8 Current_TDSL;
u8 Current_PH;
u16 Current_TMP;
u8 Current_TMPH;
u8 Current_TMPL;
u16 Current_Light;
u8 Current_LightH;
u8 Current_LightL;
u8 Level_sensor_H;
u8 Level_sensor_L;
u8 i0=1;
u8 i1=1;
u8 CommandFromPc[18];
u8 CommandFromTDS[7];
u8 CommandFromTMP[7];
u8 CommandFromlight[12];
u8 ADD;
u8 ADD1;
u8 RXD_OK_flag;
u8 RXD_TDS_OK_flag;
u8 RXD_TMP_OK_flag;
u8 tmp;
u8 tmp1;
u8 len;
u8 AnswertoPc[17];
u8 Start_flag=0;
u16 cnt1;
u8 arrive_time_flag=0;
u8 System_mode=0;
u8 flow_water=0;
u8 Pump_time_flag=0;
u8 i2=1;
u8 ADD2=0;
u8 tmp2=0;
u8 RXD_light_OK_flag=0;
u8 cnt_answer=0;
u8 arrive_answer=0;
u8 TDS_Rec_flag=0;
u8 TMP_Rec_flag=0;
int main(void)
{
Initparameter();
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);// éèÖÃÖD¶ÏóÅÏ輶·Ö×é2
delay_init(); //Ñóê±oˉêy3õê¼»ˉ
uart_init(9600); //′®¿ú3õê¼»ˉÎa9600
uart_init2(9600);
uart_init4(9600);
LED_Init(); //LED3õê¼»ˉ
Adc_Init();
uart_send_light_cut();
uart_send_light_GUANDENG();
uart_send_light_GUANDENG_SAVE();
TIM3_Int_Init(9999,7199);
TIM4_Int_Init(9999,7199);
while(RTC_Init()) //RTC3õê¼»ˉ £¬ò»¶¨òa3õê¼»ˉ3é1|
{
delay_ms(800);
}
delay_ms(500);
while(1)
{
if(arrive_answer==1)
{
arrive_answer=0;
uart_send_answer_system_state();
}
if(RXD_OK_flag==1)
{
RXD_OK_flag=0;
switch(CommandFromPc[3])
{
case 0x11 : //此处是收到指令后,修改RTC的计数值为当前时间
Current_yearH=CommandFromPc[5];
Current_yearL=CommandFromPc[6];
Current_year=Current_yearH*256+Current_yearL;
Current_month=CommandFromPc[7];
Current_day=CommandFromPc[8];
Current_hour=CommandFromPc[9];
Current_min=CommandFromPc[10];
Current_sec=CommandFromPc[11];
RTC_WaitForSynchro();
RTC_Set(Current_year,Current_month,Current_day,Current_hour,Current_min,Current_sec); //éèÖÃê±¼ä
RTC_ExitConfigMode();
RTC_WaitForSynchro();
uart_send_answer(0X11);
break ;
case 0x19 :
uart_send_answer(0X19);
Start_flag=1;
break ;
}
}
if(get_10sec_flag==1)
{
flag1=1;
get_10sec_flag=0;
RTC_Get();
RTC_SetAlarm(RTC_GetCounter()+10);
RTC_WaitForLastTask();
flag1=1;
if((calendar.hour>=8)&&(calendar.hour<=20))
{
flag2=1;
}
if((calendar.hour>=7)&&(calendar.hour<8))
{
flag3=1;
}
}
delay_ms(200);
LED0=!LED0;
}
}
这是RTC代码,我参照了原子哥的代码,做了部分修改,把中断里的秒中断去掉了,然后把timecount做成了全局变量,以方便调试时候观察。
#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "rtc.h"
//Mini STM32¿a·¢°å
//RTCêμê±ê±Öó Çy¶ˉ′úÂë
//ÕyμãÔ-×ó@ALIENTEK
//2010/6/6
extern u32 timecount;
extern u32 temp;
extern u16 temp1;
extern u8 get_10sec_flag;
extern u16 Current_year;
extern u8 Current_month;
extern u8 Current_day;
extern u8 Current_hour;
extern u8 Current_min;
extern u8 Current_sec;
_calendar_obj calendar;//ê±Öó½á11ìå
static void RTC_NVIC_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = RTC_IRQn; //RTC諾ÖÖD¶Ï
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //ÏèÕ¼óÅÏ輶1λ,′óóÅÏ輶3λ
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //ÏèÕ¼óÅÏ輶0λ,′óóÅÏ輶4λ
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //ê1Äü¸Ãí¨μàÖD¶Ï
NVIC_Init(&NVIC_InitStructure); //¸ù¾YNVIC_InitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉíaéèNVIC¼Ä′æÆ÷
}
//êμê±ê±ÖóÅäÖÃ
//3õê¼»ˉRTCê±Öó,í¬ê±¼ì2aê±ÖóêÇ·ñ1¤×÷Õy3£
//BKP->DR1óÃóú±£′æêÇ·ñμúò»′ÎÅäÖÃμÄéèÖÃ
//·μ»Ø0:Õy3£
//ÆäËû:′íÎó′úÂë
u8 RTC_Init(void)
{
//¼ì2éêÇ2»êÇμúò»′ÎÅäÖÃê±Öó
u8 temp=0;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE); //ê1ÄüPWRoíBKPíaéèê±Öó
PWR_BackupAccessCmd(ENABLE); //ê1Äüo󱸼Ä′æÆ÷·ÃÎê
if (BKP_ReadBackupRegister(BKP_DR1) != 0x5050) //′óÖ¸¶¨μÄo󱸼Ä′æÆ÷ÖD¶á3öêy¾Y:¶á3öáËóëD′èëμÄÖ¸¶¨êy¾Y2»Ïàoõ
{
BKP_DeInit(); //¸′λ±¸·YÇøóò
RCC_LSEConfig(RCC_LSE_ON); //éèÖÃía2¿μíËù¾§Õñ(LSE),ê1óÃíaéèμíËù¾§Õñ
while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET) //¼ì2éÖ¸¶¨μÄRCC±ê־λéèÖÃóë·ñ,μè′yμíËù¾§Õñ¾íD÷
{
temp++;
delay_ms(10);
}
if(temp>=250)return 1;//3õê¼»ˉê±Öó꧰ü,¾§ÕñóDÎêìa
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); //éèÖÃRTCê±Öó(RTCCLK),Ñ¡ÔñLSE×÷ÎaRTCê±Öó
RCC_RTCCLKCmd(ENABLE); //ê1ÄüRTCê±Öó
RTC_WaitForLastTask(); //μè′y×î½üò»′ζÔRTC¼Ä′æÆ÷μÄD′2ù×÷íê3é
RTC_WaitForSynchro(); //μè′yRTC¼Ä′æÆ÷í¬2½
RTC_ITConfig(RTC_IT_ALR, ENABLE); //ê1ÄüRTCÃëÖD¶Ï
RTC_WaitForLastTask(); //μè′y×î½üò»′ζÔRTC¼Ä′æÆ÷μÄD′2ù×÷íê3é
RTC_EnterConfigMode();/// ÔêDíÅäÖÃ
RTC_SetPrescaler(32767); //éèÖÃRTCÔ¤·ÖÆμμÄÖμ
RTC_WaitForLastTask(); //μè′y×î½üò»′ζÔRTC¼Ä′æÆ÷μÄD′2ù×÷íê3é
RTC_Set(Current_year,Current_month,Current_day,Current_hour,Current_min,Current_sec); //éèÖÃê±¼ä
RTC_ExitConfigMode(); //íË3öÅäÖÃÄ£ê½
BKP_WriteBackupRegister(BKP_DR1, 0X5050); //ÏòÖ¸¶¨μÄo󱸼Ä′æÆ÷ÖDD′èëóû§3ìDòêy¾Y
}
else//Ïμí3¼ìDø¼Æê±
{
RTC_WaitForSynchro(); //μè′y×î½üò»′ζÔRTC¼Ä′æÆ÷μÄD′2ù×÷íê3é
RTC_ITConfig(RTC_IT_ALR, ENABLE); //ê1ÄüÄÖÖóÃëÖD¶Ï
RTC_ClearITPendingBit(RTC_IT_ALR);
RTC_WaitForLastTask(); //μè′y×î½üò»′ζÔRTC¼Ä′æÆ÷μÄD′2ù×÷íê3é
}
RTC_NVIC_Config();//RCTÖD¶Ï·Ö×ééèÖÃ
RTC_Get();//¸üDÂê±¼ä
RTC_SetAlarm(RTC_GetCounter()+10);
RTC_WaitForLastTask();
return 0; //ok
}
//RTCê±ÖóÖD¶Ï
//ÿÃë′¥·¢ò»′Î
//extern u16 tcnt;
void RTC_IRQHandler(void)
{
if(RTC_GetITStatus(RTC_IT_ALR)!= RESET)//ÄÖÖóÖD¶Ï
{
RTC_ClearITPendingBit(RTC_IT_ALR); //ÇåÄÖÖóÖD¶Ï
get_10sec_flag=1;
}
RTC_ClearITPendingBit(RTC_IT_OW);
RTC_WaitForLastTask();
}
//ÅD¶ÏêÇ·ñêÇèòÄêoˉêy
//Ô·Y 1 2 3 4 5 6 7 8 9 10 11 12
//èòÄê 31 29 31 30 31 30 31 31 30 31 30 31
//·ÇèòÄê 31 28 31 30 31 30 31 31 30 31 30 31
//êäèë:Äê·Y
//êä3ö:¸ÃÄê·YêÇ2»êÇèòÄê.1,êÇ.0,2»êÇ
u8 Is_Leap_Year(u16 year)
{
if(year%4==0) //±ØDëÄü±»4Õû3y
{
if(year%100==0)
{
if(year%400==0)return 1;//èç1ûòÔ00½áÎ2,»1òaÄü±»400Õû3y
else return 0;
}else return 1;
}else return 0;
}
//éèÖÃê±Öó
//°ÑêäèëμÄê±Öó×a»»ÎaÃëÖó
//òÔ1970Äê1ÔÂ1èÕÎa»ù×¼
//1970~2099ÄêÎaoÏ·¨Äê·Y
//·μ»ØÖμ:0,3é1|;ÆäËû:′íÎó′úÂë.
//Ô·Yêy¾Y±í
u8 const table_week[12]={0,3,3,6,1,4,6,2,5,0,3,5}; //ÔÂDTÕyêy¾Y±í
//ƽÄêμÄÔ·YèÕÆú±í
const u8 mon_table[12]={31,28,31,30,31,30,31,31,30,31,30,31};
u8 RTC_Set(u16 syear,u8 smon,u8 sday,u8 hour,u8 min,u8 sec)
{
u16 t;
u32 seccount=0;
if(syear<1970||syear>2099)return 1;
for(t=1970;t<syear;t++) //°ÑËùóDÄê·YμÄÃëÖóÏà¼ó
{
if(Is_Leap_Year(t))seccount+=31622400;//èòÄêμÄÃëÖóêy
else seccount+=31536000; //ƽÄêμÄÃëÖóêy
}
smon-=1;
for(t=0;t<smon;t++) //°ÑÇ°ÃæÔ·YμÄÃëÖóêyÏà¼ó
{
seccount+=(u32)mon_table[t]*86400;//Ô·YÃëÖóêyÏà¼ó
if(Is_Leap_Year(syear)&&t==1)seccount+=86400;//èòÄê2Ô·YÔö¼óò»ììμÄÃëÖóêy
}
seccount+=(u32)(sday-1)*86400;//°ÑÇ°ÃæèÕÆúμÄÃëÖóêyÏà¼ó
seccount+=(u32)hour*3600;//D¡ê±ÃëÖóêy
seccount+=(u32)min*60; //·ÖÖóÃëÖóêy
seccount+=sec;//×îoóμÄÃëÖó¼óéÏè¥
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE); //ê1ÄüPWRoíBKPíaéèê±Öó
PWR_BackupAccessCmd(ENABLE); //ê1ÄüRTCoío󱸼Ä′æÆ÷·ÃÎê
RTC_WaitForLastTask();
RTC_SetCounter(seccount); //éèÖÃRTC¼ÆêyÆ÷μÄÖμ
RTC_WaitForLastTask(); //μè′y×î½üò»′ζÔRTC¼Ä′æÆ÷μÄD′2ù×÷íê3é
RTC_Get();
return 0;
}
//μÃμ½μ±Ç°μÄê±¼ä
//·μ»ØÖμ:0,3é1|;ÆäËû:′íÎó′úÂë.
u8 RTC_Get(void)
{
static u16 daycnt=0;
//u32 timecount=0;
//u32 temp=0;
//u16 temp1=0;
timecount=RTC_GetCounter();
temp=timecount/86400; //μÃμ½ììêy(ÃëÖóêy¶Ôó|μÄ)
if(daycnt!=temp)//3¬1yò»ììáË
{
daycnt=temp;
temp1=1970; //′ó1970Äê¿aê¼
while(temp>=365)
{
if(Is_Leap_Year(temp1))//êÇèòÄê
{
if(temp>=366)temp-=366;//èòÄêμÄÃëÖóêy
else {temp1++;break;}
}
else temp-=365; //ƽÄê
temp1++;
}
calendar.w_year=temp1;//μÃμ½Äê·Y
temp1=0;
while(temp>=28)//3¬1yáËò»¸öÔÂ
{
if(Is_Leap_Year(calendar.w_year)&&temp1==1)//μ±ÄêêÇ2»êÇèòÄê/2Ô·Y
{
if(temp>=29)temp-=29;//èòÄêμÄÃëÖóêy
else break;
}
else
{
if(temp>=mon_table[temp1])temp-=mon_table[temp1];//ƽÄê
else break;
}
temp1++;
}
calendar.w_month=temp1+1; //μÃμ½Ô·Y
calendar.w_date=temp+1; //μÃμ½èÕÆú
}
temp=timecount%86400; //μÃμ½ÃëÖóêy
calendar.hour=temp/3600; //D¡ê±
calendar.min=(temp%3600)/60; //·ÖÖó
calendar.sec=(temp%3600)%60; //ÃëÖó
return 0;
}
我发送的指令是:ED 27 11 0D 07 E3 07 03 06 3A 00 34 EA,也就是把当前时间改为2019年7月3日6点58分0秒
就是发出这条指令后,我观察到RTC CNT计数器的值在变,但是RTC_Get就是取不出这个变化的值,想了半天我真不知道是什么原因。
为什么每次发完帖子就搞定了?
难道论坛真是神一般的存在,已经好几次出现这种情况了。
潭子里有大神。。。。。。
一周热门 更多>