完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
好的,我知道这已经被讨论过很多次了,我已经看过了很多线索,发现了很多例子,但是我试过的每一个例子对我都不起作用。到目前为止,我已经在这上面敲了3个多小时,我不认为要让RTC工作是很困难的。我可以让它在一个专用的IIC设备上运行大约3分钟,那么为什么我在这里有这样的困难?不管怎样。有人能看看这个,告诉我我做错了什么吗?下面是代码:
以上来自于百度翻译 以下为原文 OK, I know this has been discussed many times, and I have looked through many of the thread and found many examples, but every example I have tried has not worked for me. I've been banging on this for more than 3 hours so far, and I don't think it should be this hard to get the rtc to work. I could have it running on a dedicated iic device in about 3 minutes, so why am I having such difficulty here? Anyway. Can someone please look at this and tell me what I'm doing wrong? Here's the code: /* // CONFIG2 #pragma config POSCMOD = HS // Primary Oscillator Select (HS Oscillator mode selected) #pragma config I2C1SEL = PRI // I2C1 Pin Location Select (Use default SCL1/SDA1 pins) #pragma config IOL1WAY = OFF // IOLOCK Protection (IOLOCK may be changed via unlocking seq) #pragma config OSCIOFNC = ON // Primary Oscillator Output Function (OSC2/CLKO/RC15 functions as port I/O (RC15)) #pragma config FCKSM = CSDCMD // Clock Switching and Monitor (Clock switching and Fail-Safe Clock Monitor are disabled) #pragma config FNOSC = PRI // Oscillator Select (Primary Oscillator (XT, HS, EC)) #pragma config SOSCSEL = LPSOSC // Sec Oscillator Select (Low Power Secondary Oscillator (LPSOSC)) #pragma config WUTSEL = LEG // Wake-up timer Select (Legacy Wake-up Timer) #pragma config IESO = OFF // Internal External Switch Over Mode (IESO mode (Two-Speed Start-up) disabled) // CONFIG1 #pragma config WDTPS = PS32768 // Watchdog Timer Postscaler (1:32,768) #pragma config FWPSA = PR128 // WDT Prescaler (Prescaler ratio of 1:128) #pragma config WINDIS = OFF // Watchdog Timer Window (Windowed Watchdog Timer enabled; FWDTEN must be 1) #pragma config FWDTEN = OFF // Watchdog Timer Enable (Watchdog Timer is disabled) #pragma config ICS = PGx1 // Comm Channel Select (Emulator EMUC1/EMUD1 pins are shared with PGC1/PGD1) #pragma config GWRP = OFF // General Code Segment Write Protect (Writes to program memory are allowed) #pragma config GCP = OFF // General Code Segment Code Protect (Code protection is disabled) #pragma config JTAGEN = OFF // JTAG Port Enable (JTAG port is disabled) // #pragma config statements should precede project file includes. // Use project enums instead of #define for ON and OFF. //needed for delay function #define FOSC 4000000UL #define FCY (FOSC/2) #include "xc.h" #include //needed for delay function #include //Global Variables volatile union { struct { unsigned f0:1; unsigned f1:1; unsigned f2:1; unsigned f3:1; unsigned f4:1; unsigned f5:1; unsigned f6:1; unsigned f7:1; }; struct { unsigned T0Int:1; unsigned T1Int:1; unsigned T2Int:1; unsigned T3Int:1; unsigned T4Int:1; unsigned T5Int:1; unsigned INT0:1; unsigned RTCINT:1; }; } FLAGbits; void uC_Init() { AD1PCFG = 0x1fff; // all digital LATA = 0x0000; TRISA = 0x0000; LATB = 0x0000; TRISB = 0x0000; } void T1_Init() { T1CON = 0x8006; // 1:1 with external 32768 crystal, synchronized } void T2_Init() { T2CON = 0x8010; // 1:8 = 262mS int IFS0bits.T2IF = 0; // clear flag IEC0bits.T2IE = 1; // tmr2 int enabled } void RTCCUnlock() { asm volatile("disi #5"); asm volatile("mov #0x55, w7"); // write 0x55 and 0xaa to asm volatile("mov w7, _NVMKEY");// NVMKEY to disable asm volatile("mov #0xaa, w8"); // write protection asm volatile("mov w8, _NVMKEY"); asm volatile("bset _RCFGCAL, #13"); // set the RTCWREN bit asm volatile("nop"); asm volatile("nop"); } void RTC_Init() { _RTCIE = 0; /* disable RTCC interrupts */ __builtin_disi(0x3FFF); /* disable interrupts for 16383 cycles */ /* Enable the secondary oscillator for 32.768KHz crystal */ //__builtin_write_OSCCONL(OSCCON | (1<<_OSCCON_LPOSCEN_POSITION)); _SOSCEN = 1; /* enable write to the RTCC */ #ifdef BUILTIN_WRITE_RTCWEN_BROKEN asm volatile( " mov %3,%0n" " mov %4,%0n" " bset %1, #%2n" /* set the RTCWREN bit */ : /* no outputs */ : "T"(NVMKEY), "T"(RCFGCAL), "i"(_RCFGCAL_RTCWREN_POSITION), "r"(0x55), "r"(0xAA) ); #else __builtin_write_RTCWEN(); #endif __builtin_disi(0x0000); /* enable interrupts */ _RTCEN = 1; /* enable the RTCC module */ _RTCOE = 0; /* disable the RTCC output pin */ _RTSECSEL = 1; /* set RTCC output pin to toggle each second */ _RTCPTR = 0b11; /* set the RTCC Value register window to Year */ RTCVAL = 0x0016; /* set year to xx16 */ RTCVAL = 0x0401; /* set Month = 4, Day = 1 */ RTCVAL = 0x0012; /* set Weekday = 0, Hour = 12 */ RTCVAL = 0x1030; /* set Minutes = 10, Seconds = 30 */ _RTCWREN = 0; /* lock the RTCC, no more writes */ // // Set the RTCWREN bit // __builtin_write_RTCWEN(); // // RCFGCALbits.RTCEN = 1; // // // set RTCC time 2017-12-08 13-56-34 // RCFGCALbits.RTCPTR = 3; // start the sequence // RTCVAL = 0x17; // YEAR // RTCVAL = 0x1208; // MONTH-1/DAY-1 // RTCVAL = 0x513; // WEEKDAY/HOURS // RTCVAL = 0x5634; // MINUTES/SECONDS // // // set AlARM time 2017-12-08 13-56-34 // ALCFGRPTbits.ALRMEN = 0; // ALCFGRPTbits.ALRMPTR = 2; // ALRMVAL = 0x1208; // ALRMVAL = 0x513; // ALRMVAL = 0x5634; // // // ALRMPTR MIN_SEC; AMASK Every SEC; ARPT 255; CHIME enabled; ALRMEN enabled; // ALCFGRPT = 0xC4FF; // // // RTSECSEL Alarm Pulse; // PADCFG1 = 0x0000; // // //the pointer is used to set all time values // //with the ptr = 3, the 16 bits are used to set the year only // //the ptr automatically decrements, so the next write to rtcval // //is already pointing to month/day. The value written here // //in 16 bits, represents April 1. // _RTCPTR = 3; // RTCVAL = 0016; // RTCVAL = 0401; // RTCVAL = 0012; // RTCVAL = 1030; // // // Enable RTCC, clear RTCWREN // RCFGCALbits.RTCEN = 1; // RCFGCALbits.RTCWREN = 0; // // //Enable RTCC interrupt // IEC3bits.RTCIE = 1; // IPC15bits.RTCIP = 1; } int main(void) { OSCCON = 0x0202; // external crystal, secondary osc enabled uC_Init(); //T1_Init(); T2_Init(); RTC_Init(); while(1) { if(FLAGbits.RTCINT) { FLAGbits.RTCINT = 0; if(PORTAbits.RA0) LATAbits.LATA0 = 0; else LATAbits.LATA0 = 1; } } return 0; } //ISR for Timer 2// void __attribute__((interrupt, no_auto_psv))_T2Interrupt(void) { IFS0bits.T2IF = 0; // clear flag if(PORTBbits.RB15) LATBbits.LATB15 = 0; else LATBbits.LATB15 = 1; } void __attribute__ ( ( interrupt, no_auto_psv ) ) _RTCCInterrupt( void ) { /* TODO : Add interrupt handling code */ IFS3bits.RTCIF = 0; FLAGbits.RTCINT = 1; } |
|
相关推荐
14个回答
|
|
这里是一个设置内部RTC的函数,以及一个使用内部结构来读取内部RTC的函数。
以上来自于百度翻译 以下为原文 Here is a function to set the internal rtc. .include "xc.inc" .include "UtilityLib2.inc" .global _setinternalclock .section utilitycode,code ;---------------------------------------------------- .equ DT, w0 ;DateTime_t * .equ tmp, w2 _setinternalclock: ;void setinternalclock(DateTime_t *DT); bcd mov #0x55,tmp mov tmp,NVMKEY mov #0xAA,tmp mov tmp,NVMKEY bset RCFGCAL,#RTCWREN ;Set RTCWREN bit bclr RCFGCAL,#RTCEN mov RCFGCAL,tmp ior #0x300,tmp ;all mov tmp,RCFGCAL ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ;set date mov [DT+DT_Year],tmp ;get year mov tmp,RTCVAL ;set year mov [DT+DT_DateMonth],tmp ;get date/month mov tmp,RTCVAL ;set month/date ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ;set time mov [DT+DT_HourDay],tmp ;get hour/day mov tmp,RTCVAL ;set hour/day mov [DT+DT_SecondMinute],tmp ;get sec/min mov tmp,RTCVAL ;set min/sec bset RCFGCAL,#RTCEN bclr RCFGCAL,#RTCWREN ;Set RTCWREN bit return ;---------------------------------------------------- .end And a function to read the internal rtc. .include "xc.inc" .include "UtilityLib2.inc" .global _getinternalclock .section utilitycode,code ;---------------------------------------------------- .equ DT, w0 ;DateTime_t * .equ tmp, w2 _getinternalclock: ;void getinternalclock(DateTime_t *DT); sync: btst RCFGCAL,#RTCSYNC bra nz,sync mov RCFGCAL,tmp ior #0x300,tmp ;all mov tmp,RCFGCAL mov RTCVAL,tmp ;get year mov #0x2000,w1 ;set 20yy ior w1,tmp,tmp mov tmp,[DT+DT_Year] ;store yyyy mov RTCVAL,tmp ;get date/month mov tmp,[DT+DT_DateMonth] mov RTCVAL,tmp ;get weekday/hour mov tmp,[DT+DT_HourDay] mov RTCVAL,tmp ;get min/sec mov tmp,[DT+DT_SecondMinute] return ;---------------------------------------------------- .end Using this timedate structure. //---------------------------------------------------- typedef struct{ union{ struct{ unsigned char Second; unsigned char Minute; }; unsigned int SecondMinute; }; union{ struct{ unsigned char Hour; unsigned char Day; }; unsigned int HourDay; }; union{ struct{ union{ struct{ unsigned char Date; unsigned char Month; }; unsigned int DateMonth; }; unsigned int Year; }; unsigned long TotalSeconds; }; }DateTime_t; //---------------------------------------------------- |
|
|
|
在您的代码中,您应该在修改W8之前修改W8,而不是W0-W7。这是我使用外部RTC之后不再使用的旧代码。
以上来自于百度翻译 以下为原文 In your code, you should push w8 before modifying it but not w0 - w7. That was old code that I no longer use since I use external rtc's. |
|
|
|
谢谢。我会在华氏一分钟测试它。是的,就像我说的,我可以在IIC或SPI上运行一个大约3分钟,但我正在尝试一个芯片解决方案运行,这是我的命。不应该这么难…
以上来自于百度翻译 以下为原文 Thanks for that. I'll test it here in hust a minute. Yeah, like I said, I can have one on IIC or SPI running in about 3 minutes, but I'm trying to get a single chip solution running and this is killing me. Shouldn't be this hard... |
|
|
|
谢谢。我会在华氏一分钟测试它。是的,就像我说的,我可以在IIC或SPI上运行一个大约3分钟,但我正在尝试一个芯片解决方案运行,这是我的命。不应该这么难…
以上来自于百度翻译 以下为原文 Thanks for that. I'll test it here in hust a minute. Yeah, like I said, I can have one on IIC or SPI running in about 3 minutes, but I'm trying to get a single chip solution running and this is killing me. Shouldn't be this hard... |
|
|
|
嗯,仔细看你的代码后,恐怕我不明白如何实现它。我理解这个结构,知道该怎么做,但剩下的不是我能理解的。这2个函数是以某种内嵌的ASM代码结构或什么东西来延续的吗?我对此有点迷茫。
以上来自于百度翻译 以下为原文 Well, after looking closer at your code, I'm afraid I don't understand how to implement it. I understand the timedate structure and know what to do with that, but the rest is not something I understand. Are the 2 functons contined in some sort of inline asm code structure or something? I'm a bit lost on that. |
|
|
|
这是我的一个PIC24FV32 KA302的RTCC代码,
以上来自于百度翻译 以下为原文 This was my RTCC code for a PIC24FV32KA302, void RTCC_init() { RTCCunlock(); // write default RTCC yyyy mmdd wdhh mmss RTCPWCbits.RTCCLK = 0; // external OSC RTCPWCbits.RTCOUT = 01; // 01 rtcc out = seconds RCFGCALbits.RTCPTR = 3; //**** pointer auto incremnent with data RTCVAL = 0x2017; // year 2013 val 3 RTCVAL = 0x0101; // month / day val 2 RTCVAL = 0x0112; // weekday / hour val 1 RTCVAL = 0x0055; // mins / sec val 55 ALCFGRPTbits.ARPT = 2; ALRMVAL = 0x0101; // month / day val 2 ALRMVAL = 0x0108; // weekday / hour val 1 ALRMVAL = 0x0000; // mins / 10SECS ALCFGRPTbits.AMASK = 3; // 5=hour 3 = each minute 2 = 10 sec ALCFGRPTbits.ARPT = 0; // ALCFGRPTbits.CHIME = 1; // repeat forever ALCFGRPTbits.ALRMEN = 1; // ENABLE RCFGCALbits.CAL = 0xF0; // Neg adjust OSC RCFGCALbits.RTCEN = 1; // enable RTCC RCFGCALbits.RTCOE = 1; // RTCC output enable RCFGCALbits.RTCWREN = 0; // lock the RTCC __delay_ms(50); } void RTCCunlock() { asm("push w7"); asm("push w8"); asm("disi #5"); asm("mov #0x55, w7"); asm("mov w7, _NVMKEY"); asm("mov #0xAA, w8"); asm("mov w8, _NVMKEY"); asm("bset _RCFGCAL, #13"); //set the RTCWREN bit asm("pop w8"); asm("pop w7"); } void RTC_update() { unsigned int mmss = 0; RTCCunlock(); ALCFGRPTbits.ALRMEN = 0; // disable allarm RCFGCALbits.RTCPTR = 3; //**** pointer auto incremnent with data RTCVAL = year; // year 2017 val 3 RTCVAL = mmdd; // month / day val 2 RTCVAL = Time_day_hr; // weekday / hour val 1 mmss = Time_min_sec; mmss = mmss << 8; mmss = (mmss | Rec_sec); // second ? RTCVAL = mmss; // ; // mins / sec ALCFGRPTbits.ALRMEN = 1; // d RCFGCALbits.RTCWREN = 0; // lock the RTCC } void RTC_write_Time() { unsigned int mmss = 0; RTCCunlock(); ALCFGRPTbits.ALRMEN = 0; // disable allarm RCFGCALbits.RTCPTR = 1; //**** pointer auto incremnent with data RTCVAL = Time_day_hr; // weekday / hour val 1 mmss = Time_min_sec; mmss = mmss << 8; mmss = (mmss | Rec_sec); // second ? RTCVAL = mmss; // ; // mins / sec ALCFGRPTbits.ALRMEN = 1; // d RCFGCALbits.RTCWREN = 0; // lock the RTCC } void get_time() { RCFGCALbits.RTCPTR = 3; //**** pointer auto decremnent with data year = RTCVAL; mmdd = RTCVAL; mhrs = (RTCVAL & 0x00FF); // weekday / hour val 1 mmss = RTCVAL; msec = mmss & 0x00FF; mmin = mmss >> 8; // mins / sec val 0 myy = (year & 0x00FF); // year mdd = mmdd & 0x00FF; mmm = mmdd >> 8; } |
|
|
|
不内联,只需使用汇编程序。可以在两个ASM文件或1个单个文件中。复制并粘贴到一个新的ASM文件中,删除。包括“UTITYLYB2.Inc”,使用原型从C.使用。
以上来自于百度翻译 以下为原文 Not inline, just using the assembler. Can be in two asm files or 1 single file. Copy and paste into a new asm file, remove the .include "UtilityLib2.inc" Use the prototypes to use from C. |
|
|
|
在PIC24FJ64 GA22中使用RTCC是非常困难的。第二个振荡器必须明确地启用,参见这里。RTCC被供电的方式几乎不可能在主VDD未被供电时具有有效的电池备份。或任何需要实时时钟日历功能的项目。
以上来自于百度翻译 以下为原文 Using the RTCC in the PIC24FJ64GA002 is very hard to get right. The Secondary Oscillator must be explicitly enabled, see here. Also the way the RTCC gets powered it is all but impossible to have an effective battery backup when the main VDD is not powered. Today the PIC24FJ64GA002 is a rotten choice for any project that needs a Real-Time-Clock-Calender feature. |
|
|
|
我使用的是MDSIM集成DS323,它只需要一个电池组。
以上来自于百度翻译 以下为原文 I use the Maxim Integrated DS3231. It only needs a cell battery. |
|
|
|
好啊。我设法使RTC运行。对不起,我没有使用上面提供的任何代码。我运行MCC,并能使它运行起来。现在我还有另外一个问题,或者是一个问题。时钟正在运行…真慢。我有一个32768晶体与15PF上限地面上的SOSC引脚。我已经在一些专用的RTC芯片上使用了这个EXACT晶体,没有问题-DT是按MS每天的顺序或类似的(我忘了它是什么,但它确实很紧)。这对RTC来说真的很糟糕。这意味着手术1年后,我将失去大约42天。不好的。所以,当我在MCC中设置这个东西时,我指定了时钟速度(再次,327 68赫兹)。是不是我做了什么错事,或者我应该做些什么来从中得到更好的时间?我想我会放弃机上的RTC,并坚持专用芯片,因为这是很难设置,显然缺乏严格的公差,真正的RTC应该有。对此有什么想法?
以上来自于百度翻译 以下为原文 Ok. I have managed to get the rtc running. Sorry, but I didn't use any of the code provided above. I ran MCC and canabilzed what I needed to get it to run. Now I have another question - or maybe a concern. The clock is running .... really slow. I have a 32768 crystal with 15pf caps to ground on the sosc pins. I have used this exaact same crystal on some dedicated rtc chips with no problem - dT was on the order of mS per day or something like that (Iforget exactly what it was, but it was really tight). With the pic24 and this crystal, my rtc pin is flashing with a period of 0.5137S and and on time of 257mS. This is really bad for an RTC. that means that after 1 year of operation I will have lost about 42 days. Not good. So, when I set the thing up in MCC I specified the clock speed (again, 32768Hz). Is there something I have done wrong or something else I should have done to get a better time out of this? I imagine I will just abandon the onboard rtc and stick with the dedicated chips as this is so difficult to set up and apparently lacks the tight tolerances that a real RTC should have. Any thoughts on this? |
|
|
|
我放弃了内部RTC由于ACTRESION。我有一个面包板与DS323 1,我还没有检查6个月。它是大约5秒,我可能没有同步的时钟时,它是最后一套。我用的PC时钟,我应该使用原子钟。DS331不使用外部XTAL。
以上来自于百度翻译 以下为原文 I gave up on the internal rtc due to accurracy. I had a breadboard with a DS3231 that I had not checked for 6 months. It was about 5 seconds out and I may not have synced' the clock when it was last set. I used the pc clock, I should have used an atomic clock. The DS2331 does not use an external xtal. |
|
|
|
Gort,我想你是说DS323?内部振荡器我可以挑一些。
以上来自于百度翻译 以下为原文 Gort, I assume you mean DS3231? Internal oscillator. I may pick up some of those. |
|
|
|
是的,只需要一个备用电池,还有一个温度传感器。
以上来自于百度翻译 以下为原文 Yeah, just need a backup cell battery. Also has a temperature sensor. |
|
|
|
刚从易趣网公司买了3台。我猜我正在丢弃车载RTC。羞耻。我真的希望微芯片有更好的公差。
以上来自于百度翻译 以下为原文 just bought 3 off ebay. guess I'm ditching the onboard rtc. shame. I really expected better tolerances from microchip. |
|
|
|
只有小组成员才能发言,加入小组>>
5189 浏览 9 评论
2009 浏览 8 评论
1933 浏览 10 评论
请问是否能把一个ADC值转换成两个字节用来设置PWM占空比?
3181 浏览 3 评论
请问电源和晶体值之间有什么关系吗?PIC在正常条件下运行4MHz需要多少电压?
2232 浏览 5 评论
743浏览 1评论
629浏览 1评论
有偿咨询,关于MPLAB X IPE烧录PIC32MX所遇到的问题
512浏览 1评论
PIC Kit3出现目标设备ID(00000000)与预期的设备ID(02c20000)不匹配。是什么原因
642浏览 0评论
538浏览 0评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-30 09:48 , Processed in 1.588156 second(s), Total 105, Slave 87 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号