




已阅读5页,还剩44页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
PIC24FJ单片机RTCC程序有感(2008-08-05 18:08:41)转载标签:杂谈分类:IC程序RTCC很是简单,但是别人写出来就太漂亮了,要借鉴的太多太多。工程实用* ?2007 Microchip Technology Inc.* FileName:RTCC.c* Dependencies:Header (.h) files if applicable, see below* Processor:PIC24Fxxxx* Compiler:MPLAB?C30 v3.00 or higher* SOFTWARE LICENSE AGREEMENT:* Microchip Technology Incorporated (Microchip) retains all* ownership and intellectual property rights in the code accompanying* this message and in all derivatives hereto.You may use this code,* and any derivatives created by any person or entity by or on your* behalf, exclusively with Microchips proprietary products.Your* acceptance and/or use of this code constitutes agreement to the* terms and conditions of this notice.* CODE ACCOMPANYING THIS MESSAGE IS SUPPLIED BY MICROCHIP AS IS. NO* WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT* NOT LIMITED TO, IMPLIED WARRANTIES OF NON-INFRINGEMENT,* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS* CODE, ITS INTERACTION WITH MICROCHIPS PRODUCTS, COMBINATION WITH* ANY OTHER PRODUCTS, OR USE IN ANY APPLICATION.* YOU ACKNOWLEDGE AND AGREE THAT, IN NO EVENT, SHALL MICROCHIP BE* LIABLE, WHETHER IN CONTRACT, WARRANTY, TORT (INCLUDING NEGLIGENCE OR* BREACH OF STATUTORY DUTY), STRICT LIABILITY, INDEMNITY,* CONTRIBUTION, OR OTHERWISE, FOR ANY INDIRECT, SPECIAL, PUNITIVE,* EXEMPLARY, INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, FOR COST OR* EXPENSE OF ANY KIND WHATSOEVER RELATED TO THE CODE, HOWSOEVER* CAUSED, EVEN IF MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE* DAMAGES ARE FORESEEABLE.TO THE FULLEST EXTENT ALLOWABLE BY LAW,* MICROCHIPS TOTAL LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO THIS* CODE, SHALL NOT EXCEED THE PRICE YOU PAID DIRECTLY TO MICROCHIP* SPECIFICALLY TO HAVE THIS CODE DEVELOPED.* You agree that you are solely responsible for testing the code and* determining its suitability.Microchip has no obligation to modify,* test, certify, or support the code.* REVISION HISTORY:* AuthorDateComments on this revision* Albert Z.03/24/08First release of source file* Albert Z.07/10/08Add RTCC alarm* ADDITIONAL NOTES:* Copy the RTCC functions to your application.* Code Tested on:* Explorer 16 Development Board withPIC24FJ128GA010 controller* DESCRIPTION:* Microchips PIC24F microcontrollers feature a useful Real Time Clock* for users to get an on chip clock in their applications.* This code example demonstrates how to grab and set the RTC, set alarm* and RTCC clock output / alarm output.*/#include / Setup configuration bits/ JTAG/Code Protect/Write Protect/Clip-on Emulation mode/ Watchdog Timer/ICD pins select_CONFIG1(JTAGEN_OFF & GCP_OFF & GWRP_OFF & COE_OFF & FWDTEN_OFF & ICS_PGx2)/ Disable CLK switch and CLK monitor, OSCO or Fosc/2, HS oscillator,/ Primary oscillator_CONFIG2(FCKSM_CSDCMD & OSCIOFNC_OFF & POSCMOD_HS & FNOSC_PRI)/ Union to access rtcc registerstypedef union tagRTCC struct unsigned char sec;unsigned char min;unsigned char hr;unsigned char wkd;unsigned char day;unsigned char mth;unsigned char yr;struct unsigned int prt00;unsigned int prt01;unsigned int prt10;unsigned int prt11; RTCC;RTCC _time;RTCC _time_chk;RTCC _alarm;/ macro#define mRTCCDec2Bin(Dec) (10*(Dec4)+(Dec&0x0f)#define mRTCCBin2Dec(Bin) (Bin/10)= 60)Sec = 0;_time_chk.sec = mRTCCBin2Dec(Sec);* Function: RTCCSetBinMin* Preconditions: None.* Overview: The function verifies a setting minutes range, translates* it into BCD format and writes into _time_chk structure. To write* the structure into clock RTCCSet must be called.* Input: Minutes binary value.* Output: Checked BCD value in _time_chk structure.*/void RTCCSetBinMin(unsigned char Min)if(Min = 60)Min = 0;_time_chk.min = mRTCCBin2Dec(Min);* Function: RTCCSetBinHour* Preconditions: None.* Overview: The function verifies a setting hours range, translates* it into BCD format and writes into _time_chk structure. To write* the structure into clock RTCCSet must be called.* Input: Hours binary value.* Output: Checked BCD value in _time_chk structure.*/void RTCCSetBinHour(unsigned char Hour)if(Hour = 24) Hour = 0;_time_chk.hr = mRTCCBin2Dec(Hour);* Function: RTCCCalculateWeekDay* Preconditions:* Valid values of day, month and year must be presented in* _time_chk structure.* Overview: The function reads day, month and year from _time_chk and* calculates week day. Than It writes result into _time_chk. To write* the structure into clock RTCCSet must be called.* Input: _time_chk with valid values of day, month and year.* Output: Zero based week day in _time_chk structure.*/void RTCCCalculateWeekDay()const char MonthOffset =/jan feb mar apr may jun jul aug sep oct nov dec0,3,3,6,1,4,6,2,5,0,3,5 ;unsigned Year;unsigned Month;unsigned Day;unsigned Offset;/ calculate week dayYear= mRTCCDec2Bin(_time_chk.yr);Month = mRTCCDec2Bin(_time_chk.mth);Day= mRTCCDec2Bin(_time_chk.day);/ 2000s century offset = 6 +/ every year 365%7 = 1 day shift +/ every leap year adds 1 dayOffset = 6 + Year + Year/4;/ Add month offset from tableOffset += MonthOffsetMonth-1;/ Add dayOffset += Day;/ If its a leap year and before March theres no additional day yetif(Year%4) = 0)if(Month Daymax) Day = 1;_time_chk.day = mRTCCBin2Dec(Day);* Function: RTCCSetBinMonth* Preconditions: None.* Overview: The function verifies a setting month range, translates* it into BCD format and writes into _time_chk structure. To write* the structure into clock RTCCSet must be called.* Input: Month binary value.* Output: Checked BCD value in _time_chk structure.*/void RTCCSetBinMonth(unsigned char Month)if(Month 12) Month = 1;_time_chk.mth = mRTCCBin2Dec(Month);* Function: RTCCSetBinYear* Preconditions: None.* Overview: The function verifies a setting year range, translates it* into BCD format and writes into _time_chk structure. To write the* structure into clock RTCCSet must be called.* Input: Year binary value.* Output: Checked BCD value in _time_chk structure.*/void RTCCSetBinYear(unsigned char Year)if(Year = 100)Year = 0;_time_chk.yr = mRTCCBin2Dec(Year);/ Recheck day. Leap year influences to Feb 28/29.RTCCSetBinDay(mRTCCDec2Bin(_time_chk.day);* Interrupt for RTCC Alarm.*/void _attribute_(interrupt, no_auto_psv) _RTCCInterrupt(void)IFS3bits.RTCIF = 0;_builtin_btg(unsigned int *)&LATA, 6);/ Toggle RA6/ TO DO:/ User Implementation.调试RTCC的乐趣(2008-08-06 19:16:31)转载标签:杂谈分类:调试乐趣在调试PIC24FJ的RTCC是,只要报警时间一到,单片机即出现复位或死机的状态,查了很久。因为是调试,是把中断服务程序都关了的,怎么还是会出现呢?实在没有办法了,只好打电话问Microchip公司的工程师了。高工程师说,你确定把中断关了吗,还有RTCC输出引脚也是关了的吗?这种现象是跑飞的现象,问题应该是在中断。再次检查时才发现,我们把中断服务程序关了,但是却忘了把中断使能关掉了。小细节,都没有注意,中断一触发,没有程序要执行,当然要跑飞。* Enable the RTCC interrupt*IFS3bits.RTCIF = 0;/clear the RTCC interrupt flagIEC3bits.RTCIE = 1;/enable the RTCC interrupt* Interrupt for RTCC Alarm.*/void _attribute_(interrupt, no_auto_psv) _RTCCInterrupt(void)IFS3bits.RTCIF = 0;WarnState=WarnState|Battery_Maint;/电池更换告警,需要人为消除该警告标志/Sleep();Idle();/电池确认需要维护了,则不需要再工作了,节电!以待更换电池,应该通过电池的电量来判断。* Function: RTCCALMSet* Preconditions: None.* Overview:* The function upload time and date from _alarm into RTCC alarm.* Input: _alarm - structure containing time and date.* Output: None.*/void RTCCALMSet(void)RTCCUnlock();/ Unlock the RTCCwhile(RCFGCALbits.RTCSYNC=1);/wait for RTCSYNC bit to become 0ALCFGRPTbits.ALRMEN= 0;/disable alarm to update itALCFGRPTbits.ALRMPTR= 2;/Point to Month/Day registerALRMVAL = _alarm.prt10;/load month & dayALRMVAL = _alarm.prt01;/load weekday & hourALRMVAL = _alarm.prt00;/load minute & secondsALCFGRPTbits.AMASK= 9;/alarm every yearALCFGRPTbits.ARPT= 0x01;/alarm 1 times,即重复一次。是执行一次中断吗,ALCFGRPTbits.CHIME= 1;/enable chime,ARPTis allowed to roll over from 00h to FFh.ALCFGRPTbits.ALRMEN= 1;/enable the alarmRCFGCALbits.RTCWREN = 0;/ Lock the RTCC,手动清0应用PIC24自带的USB功能
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 《公共管理研究方法教程》课件第六章
- 车库防涝培训课件
- 车库保洁员工安全培训课件
- 文化创意产品数字化展示技术与虚拟现实技术融合研究报告
- 2025年工业互联网平台IPv6技术升级对工业控制系统的影响报告
- 海洋能发电技术创新引领2025年海岛能源供应行业变革报告
- 稀土冶炼工职业技能考核试卷及答案
- 新质生产力重在质量
- 濮阳初中数学竞赛试题及答案
- 2025年城市污水处理厂智能化改造对城市排水管网的影响报告
- 地砖铺贴分包合同协议书
- 2025年山东省青岛市中考英语真题
- 煤矿智能掘进员内部技能考核试卷及答案
- 新《斜视弱视学》期末考试复习题库(含答案)
- 幼儿园数学活动《6和7的认识》课件
- 大语言模型与安全 课件 第3章 多模态大语言模型
- 人民医院开展“改善就医感受提升患者体验”主题活动实施方案
- 2025四川成都崇州市国有资产监督管理局市属国有企业面向社会招聘中层管理人员和员工19人笔试模拟试题及答案解析
- 2025年《医疗器械质量管理规范》培训试题(附答案)
- 2025年小学生宪法素养竞赛题库
- 2025年php考试题及答案笔试
评论
0/150
提交评论