[原创] 【R7F0C809】LED+TIMER+UART+键中断

ljj3166   2015-8-27 14:50 楼主

折腾了一下午,把基本可能会用到的外设跑了一下
基本上就是GPIO、定时器、串口和按键这类
还有AD再找例程来玩吧

1、LED
这个很通用,基本上就是通用IO口模式的配置
PMx、PMCx和Px这三个寄存器就可以摆平

  1. P0 |= 0x03U;<br />
  2. PM0 &amp;= 0xFCU;<br />
  3. PMC0 &amp;= 0xFCU;
具体可以查询硬件手册
妹的,点这个LED1灯,花了哥3个小时,最后发现板子上这个LED1默认是断开的
坑得里焦外嫩
自己焊上吧
IMG_20150827_143240784.jpg

2、TIMER
也是很好理解,按照手册一步步配置,基本上没有大问题
  1. void R_TAU0_Create(void)<br />
  2. {<br />
  3. &nbsp; &nbsp; TAU0EN = 1U;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;/* supplies input clock */<br />
  4. &nbsp; &nbsp; TPS0 &amp;= 0x00U;<br />
  5. &nbsp; &nbsp; /* Stop all channels */<br />
  6. &nbsp; &nbsp; TT0 |= 0x0FU;<br />
  7. &nbsp; &nbsp; /* Mask channel 0 interrupt */<br />
  8. &nbsp; &nbsp; TMMK00 = 1U;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;/* disable INTTM00 interrupt */<br />
  9. &nbsp; &nbsp; TMIF00 = 0U;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;/* clear INTTM00 interrupt flag */<br />
  10. &nbsp; &nbsp;<br />
  11. &nbsp; &nbsp; /* Set INTTM00 low priority */<br />
  12. &nbsp; &nbsp; TMPR100 = 1U;<br />
  13. &nbsp; &nbsp; TMPR000 = 1U;<br />
  14. &nbsp; &nbsp;<br />
  15. &nbsp; &nbsp; /* Channel 0 used as interval timer */<br />
  16. &nbsp; &nbsp; TMR00H = 0x00U;<br />
  17. &nbsp; &nbsp; TMR00L = 0x00U;<br />
  18. &nbsp; &nbsp;<br />
  19. &nbsp; &nbsp; TDR00H = 0x9CU;<br />
  20. &nbsp; &nbsp; TDR00L = 0x3FU;<br />
  21. &nbsp; &nbsp; TO0 &amp;= 0x0EU;<br />
  22. &nbsp; &nbsp; TOE0 |= 0x01U;<br />
  23. }<br />
  24. <br />
  25. void R_TAU0_Channel0_Start(void)<br />
  26. {<br />
  27. &nbsp; &nbsp; TMIF00 = 0U;&nbsp; &nbsp; /* clear INTTM00 interrupt flag */<br />
  28. &nbsp; &nbsp; TMMK00 = 0U;&nbsp; &nbsp; /* enable INTTM00 interrupt */<br />
  29. &nbsp; &nbsp; TS0 |= 0x01U;<br />
  30. }<br />
  31. <br />
  32. void R_TAU0_Channel0_Stop(void)<br />
  33. {<br />
  34. &nbsp; &nbsp; TT0 |= 0x01U;<br />
  35. &nbsp; &nbsp; /* Mask channel 0 interrupt */<br />
  36. &nbsp; &nbsp; TMMK00 = 1U;&nbsp; &nbsp; /* disable INTTM00 interrupt */<br />
  37. &nbsp; &nbsp; TMIF00 = 0U;&nbsp; &nbsp; /* clear INTTM00 interrupt flag */<br />
  38. }
定时器配置2ms来一次中断
服务程序:
  1. #pragma interrupt INTTM00 R_TAU0_Channel0_Interrupt&nbsp;&nbsp;//中断服务函数入口<br />
  2. <br />
  3. __interrupt void R_TAU0_Channel0_Interrupt(void)<br />
  4. {<br />
  5. &nbsp; &nbsp; /* Start user code. Do not edit comment generated here */<br />
  6. <br />
  7. &nbsp; &nbsp;if (++inttm00counter == 250U)&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;/* 250 times interrupt ? */<br />
  8. &nbsp; &nbsp; {<br />
  9. <br />
  10. &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;TDR00H = 0x9CU;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;/* Set new timer interval to TAU0 channel 0 */<br />
  11. &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;TDR00L = 0x3FU;<br />
  12. &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; P0.1 = !P0.1;//500ms LED2翻转一次<br />
  13. &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; inttm00counter = 0U;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;/* Clear TM00 interrupt count */<br />
  14. &nbsp; &nbsp; }<br />
  15. <br />
  16. &nbsp; &nbsp; /* End user code. Do not edit comment generated here */<br />
  17. }


3、UART

QQ截图20150827150000.png
额,这个太多了,视频后面把代码打包上来吧
比较乱,仅供参考
要注意UART例程是启用了奇偶校验的,干掉才能正常通信

4、键中断
这个是R7F0C809的一个比较有特色的地方
把按键的外部中断直接固化成了一个模块
果然是为消费电子而生啊
配置好简单,比GPIO复杂不到哪里去
  1. KRMK = 1U;&nbsp;&nbsp;/* disable INTKR operation */<br />
  2. &nbsp; &nbsp; KRIF = 0U;&nbsp;&nbsp;/* clear INTKR interrupt flag */<br />
  3. &nbsp; &nbsp;&nbsp;&nbsp;KRCTL |= 0x80U;<br />
  4. &nbsp; &nbsp;&nbsp;&nbsp;KRCTL &amp;= 0xFEU;<br />
  5. &nbsp; &nbsp;&nbsp;&nbsp;KRM0 |= 0x10U;<br />
  6. <br />
  7. &nbsp; &nbsp; /* Set INTKR level 1 priority */<br />
  8. &nbsp; &nbsp; KRPR1 = 1U;<br />
  9. &nbsp; &nbsp; KRPR0 = 1U;&nbsp;&nbsp;<br />
  10. &nbsp; &nbsp; /* Key Interrupt Start */<br />
  11. &nbsp; &nbsp; KRF = 0U;<br />
  12. &nbsp; &nbsp; KRIF = 0U;&nbsp;&nbsp;/* clear INTKR interrupt flag */<br />
  13. &nbsp; &nbsp; KRMK = 0U;&nbsp;&nbsp;/* enable INTKR operation */&nbsp; &nbsp;
和键中断相关的配置主要是KRCTL、KRM和KRF
不过还得注意通用IO的配置要匹配再就是中断函数
  1. #pragma interrupt INTKR KEY_Interrupt //中断函数入口<br />
  2. <br />
  3. __interrupt void KEY_Interrupt()<br />
  4. {<br />
  5. &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;<br />
  6. &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;Key_Value = KRF;<br />
  7. &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;if(Key_Value &amp;&amp; 0x10U){P0.0 = ~P0.0;}<br />
  8. &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;KRF = 0U;&nbsp;&nbsp;<br />
  9. }<br />
可以多个按键进行键值判断,很方便

上个操作视频吧,很low


 

LED2由定时器每500ms翻转一次
PC键盘串口点亮或者熄灭LED1
按键K1完成键中断功能,翻转LED1不过实测消抖稍微差劲了些,偶尔会出现多次触发的情况
还要自己消抖的话,那和外部中断有什么区别?
键中断也是个噱头了
代码撸上,比较乱,随便看看吧,嗯,回复可见吧

游客,如果您要查看本帖隐藏内容请回复


发现板子上丝印的一个错误
IMG_20150827_144518801.jpg
K1是INTP0吗?
QQ截图20150827144905.png
似乎不是啊,顶了天被复用成INTP1呢
不知道是不是哪里看走眼了





 

So TM what......?

回复评论 (12)

我在长沙发现了楼主
So TM what......?
点赞  2015-8-27 14:56
看看                                 
我从不担心我努力了不优秀,只担心优秀的人都比我更努力。如果你无法忍受孤独,就不要追逐梦想。每一个优秀的人,都有一段沉默的时光。在那一段时光,你付出了很多努力,忍受孤独和寂寞,不抱怨不诉苦,最后渡过了这
点赞  2015-8-27 15:10
看看            
点赞  2015-8-27 18:08
不错开始进入轨道了。
点赞  2015-8-31 14:37
01.#pragma interrupt INTTM00 R_TAU0_Channel0_Interrupt  //中断服务函数入口
我想问下各种的中断函数名在哪里看

点赞  2015-8-31 15:49
引用: 240011814 发表于 2015-8-31 15:49
01.#pragma interrupt INTTM00 R_TAU0_Channel0_Interrupt  //中断服务函数入口
我想问下各种的中断函数名 ...

数据手册上 QQ截图20150831163012.png
So TM what......?
点赞  2015-8-31 16:30
我还想问下#pragma的用法,

02.PM0 &= 0xFCU;
0xFCU这个在哪有定义
点赞  2015-8-31 17:03
想看UART
点赞  2015-9-7 08:44
看看
每一刻都是崭新的,加油!
点赞  2015-9-12 22:35
刚开始着手弄,正好能参考,谢了!
点赞  2015-10-22 16:38
顶一个
点赞  2015-11-23 20:19
……………………
点赞  2015-12-10 14:52
电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 京公网安备 11010802033920号
    写回复