基于ARM的时钟+闹钟设计_第1页
基于ARM的时钟+闹钟设计_第2页
基于ARM的时钟+闹钟设计_第3页
基于ARM的时钟+闹钟设计_第4页
基于ARM的时钟+闹钟设计_第5页
已阅读5页,还剩15页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、嵌入式系统课程设计报告 基于arm的时钟系统院 系: 学生姓名: 专 业: 应用电子技术 班 级: 指导教师: 完成时间: 目 录1 引言12 stm32单片机rtc介绍13 总体设计框图24 硬件电路24.1 stm32芯片管脚介绍24.2 stm32复位和时钟电路设计34.3闹钟提醒电路45 程序流程图45.1 主程序流程图45.2 中断程序流程图66 总结与体会6参考文献:7附录:8基于arm的时钟系统摘要:本设计选择stm32为核心控制元件,设计了用rtc定时器实现时钟的控制与设计,本设计能作为普通时钟用,而且能设置闹钟。程序使用c语言进行编程,能动态显示当前时间,包括时、分、秒,并且

2、用串口助手显示。关键词:stm32 arm 时钟 闹钟1 引言 随着科技的发展,嵌入式系统广泛应用于工业控制和商业管理领域,在多媒体手机、袖珍电脑,掌上电脑,车载导航器等方面的应用,更是极大地促进了嵌入式技术深入到生活和工作各个方面。嵌入式系统主要由嵌入式处理器、相关支撑硬件及嵌入式软件系统组成。本文介绍基于stm32f103r6t6的嵌入式微处理器的电子时钟设计,并且在液晶上显示。2 stm32单片机rtc介绍stm32的实时时钟(rtc)是一个独立的定时器。rtc模块拥有一组连续计数的计数器,在相应软件配置下,可以提供时钟日历的功能,修改计数器的值可以重新设置系统当前的时间和日期。stm3

3、2f10x系列微控制器片上内置的rtc模块,主要特性如下:(1) 可编程的预分频系数,分频系数最高位220。(2) 32位的可编程计数器,可用于长程时间段的测量。(3) 两个单独的时钟:用于apb1接口的plck1和rtc时钟(此时rtc时钟的频率必须小于pclk1时钟的四分之一以上)。(4) 可以选择一下三种rtc的时钟源: hse(high speed external)时钟除以128,即高速外部时钟,接石英/陶瓷谐振器,或者接外部时钟源,频率范围为416mhz。 lsi(low speed internal)振荡器时钟,即低速内部时钟,频率为40khz。 lse(low speed ex

4、ternal)振荡器时钟,即低速外部时钟,接石英晶体,频率为32.768khz。(5)2钟独立的复位类型: apb1接口由系统复位。 rtc核(预分频器、闹钟、计数器和分频器)只能由备份域复位。(6)3个专门的可屏蔽中断:闹钟中断,用来产生一个软件可编程的闹钟中断。秒中断,用来产生一个可编程的周期性中断信号(最长可达1s)。溢出中断,检测内部可编程计数器溢出并回转为0的状态。3 总体设计框图 本电路主要由3大部分电路组成:arm最小系统电路、时钟显示电路和闹钟警报电路(本设计用led灯指示)。其中atm最小系统主要由复位电路和时钟电路组成。在该设计中,闹钟提醒由led灯代替,当闹钟时间到的话,

5、led灯亮,延时设定的时间后自动关闭。总体设计方框图,如图1所示。stm32液晶显示电路 复位电路 闹铃提醒电路 时钟电路图1总体设计方框图4 硬件电路 该设计分为软件设计和硬件设计两大模块,硬件电路由arm最小系统电路、时钟显示电路和闹钟提醒电路组成。4.1 stm32芯片管脚介绍stm32f103r6t6管脚示意图,如图2所示。图2 stm32f103r6t6管脚示意图4.2 stm32复位和时钟电路设计 此电路主要是复位电路和时钟电路两部分,其中复位电路采用按键手动复位和上电自动复位组合,电路如图2(右)所示:其中7脚为stm32的复位端。时钟电路如图2(左)所示:晶振采用的是8mhz和

6、32.786khz,8mkz分别接stm32的5脚和6脚,32.786khz分别接stm32的3脚和4脚。图3 stm32复位和时钟电路4.3 闹钟提醒电路本设计的闹钟提醒电路没有采用蜂鸣器,为了调试方便用实验板上的发光二极管led1led4指示,其与实验板对应接口为pc0pc3。电路如图4所示。图4 闹钟提醒电路5 程序流程图5.1主程序流程图 主程序流程图,如图5所示。图5主程序流程图5.2中断程序流程图 中断程序流程图,如图6所示。图6中断程序流程图6 总结与体会通过这次时钟的设计,使我对arm有了更深的理解。刚开始拿到题提目,我先是查找相关资料,从图书馆和网上找到相关的课题,参考借鉴别

7、人的设计,从而理清我们设计的思路。此次作业设计大致可以分为两部分,电路图部分和程序编程部分,其中最有难度的是程序的编写与调试。在编写程序的过程中,我遇到了各种各样的问题,工程之间的结合,对于其中的错误怎样解决,需要配置什么,更改哪里等等。对于arm我学的很浅,编程遇到问题不知道如何解决,我知道这个是我的弱点,但在这两周的课程设计中,用keil uvision4在arm开发板上进行程序调试,遇到问题解决问题,在这个过程中我收获了不少。参考文献1 彭刚、秦志强等.基于arm cortex-m3的stm32系列嵌入式微控制器应用实践m.北京:电子工业出版社2 李宁.基于mdk的stm32处理器开发应

8、用m.北京航空航天大学出版社,2008.3 王永红、徐炜、赫立平.stm32系列arm cortex-m3微控制器原理与实践m.北京航空航天大学出版社,2008.4 arm limited.cortex-m3 technical reference manual(r2p0). arm ddi 0037g 2008.5 附录1 总体电路图2 串口助手演示效果3 源程序/* * 函数名:nvic_configuration * 描述 :配置rtc秒中断的主中断优先级为1,次优先级为0 * 输入 :无 * 输出 :无 * 调用 :外部调用 */void nvic_configuration(void

9、) nvic_inittypedef nvic_initstructure; /* configure one bit for preemption priority */ nvic_prioritygroupconfig(nvic_prioritygroup_1); /* enable the rtc interrupt */ nvic_initstructure.nvic_irqchannel = rtc_irqn; nvic_initstructure.nvic_irqchannelpreemptionpriority = 1; nvic_initstructure.nvic_irqch

10、annelsubpriority = 1; nvic_initstructure.nvic_irqchannelcmd = enable; nvic_init(&nvic_initstructure); nvic_initstructure.nvic_irqchannel = rtcalarm_irqn; nvic_initstructure.nvic_irqchannelsubpriority =0 ; nvic_init(&nvic_initstructure);/* * 函数名:gpio_configuration * 描述 :配置gpio * 输入 :无 * 输出 :无 * 调用 :外

11、部调用 */void gpio_configuration(void)/*定义一个gpio_inittypedef类型的结构体*/gpio_inittypedef gpio_initstructure;/*开启gpioc的外设时钟*/rcc_apb2periphclockcmd( rcc_apb2periph_gpioc, enable); /*选择要控制的gpioc引脚*/ gpio_initstructure.gpio_pin = gpio_pin_0|gpio_pin_1|gpio_pin_2|gpio_pin_3;/*设置引脚模式为通用推挽输出*/ gpio_initstructure

12、.gpio_mode = gpio_mode_out_pp; /*设置引脚速率为50mhz */ gpio_initstructure.gpio_speed = gpio_speed_50mhz; /*调用库函数,初始化gpioc*/ gpio_init(gpioc, &gpio_initstructure);gpio_setbits(gpioc,gpio_pin_0|gpio_pin_1|gpio_pin_2|gpio_pin_3); /* * 函数名:rtc_configuration * 描述 :配置rtc * 输入 :无 * 输出 :无 * 调用 :外部调用 */void rtc_co

13、nfiguration(void) /* enable pwr and bkp clocks */ rcc_apb1periphclockcmd(rcc_apb1periph_pwr | rcc_apb1periph_bkp, enable); /* allow access to bkp domain */ pwr_backupaccesscmd(enable); /* reset backup domain */ bkp_deinit(); /* enable lse */ rcc_lseconfig(rcc_lse_on); /* wait till lse is ready */ wh

14、ile (rcc_getflagstatus(rcc_flag_lserdy) = reset) /* select lse as rtc clock source */ rcc_rtcclkconfig(rcc_rtcclksource_lse); /* enable rtc clock */ rcc_rtcclkcmd(enable); /* wait for rtc registers synchronization */ rtc_waitforsynchro(); /* wait until last write operation on rtc registers has finis

15、hed */ rtc_waitforlasttask(); /* enable the rtc second */ rtc_itconfig(rtc_it_sec, enable); rtc_itconfig(rtc_it_alr, enable); /* wait until last write operation on rtc registers has finished */ rtc_waitforlasttask(); /* set rtc prescaler: set rtc period to 1sec */ rtc_setprescaler(32767); /* rtc per

16、iod = rtcclk/rtc_pr = (32.768 khz)/(32767+1) */ /* wait until last write operation on rtc registers has finished */ rtc_waitforlasttask();/* * 函数名:time_regulate * 描述 :返回用户在超级终端中输入的时间值,并将值储存在 * rtc 计数寄存器中。 * 输入 :无 * 输出 :用户在超级终端中输入的时间值,单位为 s * 调用 :内部调用 */uint32_t time_regulate(void) uint32_t tmp_hh =

17、0xff, tmp_mm = 0xff, tmp_ss = 0xff; printf(rn=time settings=); printf(rn please set hours); while (tmp_hh = 0xff) tmp_hh = usart_scanf(23); printf(: %d, tmp_hh); printf(rn please set minutes); while (tmp_mm = 0xff) tmp_mm = usart_scanf(59); printf(: %d, tmp_mm); printf(rn please set seconds); while

18、(tmp_ss = 0xff) tmp_ss = usart_scanf(59); printf(: %d, tmp_ss); /* return the value to store in rtc counter register */ return(tmp_hh*3600 + tmp_mm*60 + tmp_ss);/* * 函数名:time_adjust * 描述 :时间调节 * 输入 :无 * 输出 :无 * 调用 :外部调用 */void time_adjust(void) /uint32_t aaaa; /* wait until last write operation on r

19、tc registers has finished */ rtc_waitforlasttask(); /aaaa=time_regulate(); /* change the current time */ rtc_setcounter(time_regulate(); rtc_setalarm( time_regulate(); /* wait until last write operation on rtc registers has finished */ rtc_waitforlasttask();/* * 函数名:time_display * 描述 :显示当前时间值 * 输入 :

20、-timevar rtc计数值,单位为 s * 输出 :无 * 调用 :内部调用 */void time_display(uint32_t timevar) uint32_t thh = 0, tmm = 0, tss = 0; /* compute hours */ thh = timevar / 3600; /* compute minutes */ tmm = (timevar % 3600) / 60; /* compute seconds */ tss = (timevar % 3600) % 60; printf( time: %0.2d:%0.2d:%0.2dr, thh, tm

21、m, tss);/* * 函数名:time_show * 描述 :在超级终端中显示当前时间值 * 输入 :无 * 输出 :无 * 调用 :外部调用 */ void time_show(void) printf(nr); /* infinite loop */ while (1) /* if 1s has paased */ if (timedisplay = 1) /* display current time */ time_display(rtc_getcounter(); timedisplay = 0; /* * 函数名:usart_scanf * 描述 :串口从超级终端中获取数值 *

22、 输入 :- value 用户在超级终端中输入的数值 * 输出 :无 * 调用 :内部调用 */ uint8_t usart_scanf(uint32_t value) uint32_t index = 0; uint32_t tmp2 = 0, 0; while (index 2) /* loop until rxne = 1 */ while (usart_getflagstatus(usart1, usart_flag_rxne) = reset) tmpindex+ = (usart_receivedata(usart1);/ 从串口终端里面输进去的数是ascii码值 if (tmpi

23、ndex - 1 0x39) printf(nrplease enter valid number between 0 and 9); index-; /* calculate the corresponding value */ index = (tmp1 - 0x30) + (tmp0 - 0x30) * 10); /* checks */ if (index value) printf(nrplease enter valid number between 0 and %d, value); return 0xff; return index;/*/* stm32f10x periphe

24、rals interrupt handlers */* add here the interrupt handler for the used peripheral(s) (ppp), for the */* available peripheral interrupt handlers name please refer to the startup */* file (startup_stm32f10x_xx.s). */*/* * brief this function handles rtc global interrupt request. * param none * retval

25、 : none */void rtc_irqhandler(void) if (rtc_getitstatus(rtc_it_sec) != reset) /* clear the rtc second interrupt */ rtc_clearitpendingbit(rtc_it_sec); /* toggle gpio_led pin 6 each 1s */ /gpio_writebit(gpio_led, gpio_pin_6, (bitaction)(1 - gpio_readoutputdatabit(gpio_led, gpio_pin_6); /* enable time

26、update */ timedisplay = 1; /* wait until last write operation on rtc registers has finished */ rtc_waitforlasttask(); /* reset rtc counter when time is 23:59:59 */ if (rtc_getcounter() = 0x00015180) rtc_setcounter(0x0); /* wait until last write operation on rtc registers has finished */ rtc_waitforl

27、asttask(); if(rtc_getitstatus(rtc_it_alr)!=reset) rtc_clearitpendingbit(rtc_it_alr); printf(rn rtc alarm begin!); gpio_resetbits(gpioc,gpio_pin_0|gpio_pin_1|gpio_pin_2|gpio_pin_3); delay_nms(3000);gpio_setbits(gpioc,gpio_pin_0|gpio_pin_1|gpio_pin_2|gpio_pin_3); /* * 函数名:delay_nus(int16) * 描述 :微秒级延时n

28、32767 * 输入 :无 * 输出 :无 */void delay_nus(u16 n)u16 j;while(n-)j=8;while(j-);/*/* * 函数名:delay_nms(int16) * 描述 :毫秒级延时n32767 * 输入 :无 * 输出 :无 */void delay_nms(u16 n)while(n-)delay_nus(1100);/*嵌入式系统课程设计报告 * 文件名 :main.c* 描述 :利用stm32的rtc实现一个简易的电子时钟。在串口助手中显示时间值。* 显示格式为 time: xx:xx:xx(时:分:秒),当时间* 计数为:23:59:59

29、时将刷新为:00:00:00。 * 另外还能设置闹钟*基于arm的时钟系统*/int main(void) /* config the sysclock to 72m */ systeminit(); /* usart1 config */usart1_config();/* 配置rtc秒中断优先级 */nvic_configuration(); printf( rn this is a rtc demo. rn );gpio_configuration();if (bkp_readbackupregister(bkp_dr1) != 0x5a5a) /* backup data regist

30、er value is not correct or not yet programmed (when the first time the program is executed) */printf(rnthis is a rtc demo!rn); printf(rnn rtc not yet configured.); /* rtc configuration */ rtc_configuration(); printf(rn rtc configured.); /* adjust time by values entred by the user on the hyperterminal */ time_adjust(); bkp_writebackupregister(bkp_dr1, 0x5a5a); else /* check if the power on reset flag is set */ if (rcc_getflagstatus(rcc_flag_p

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论