[PIC®/AVR®/dsPIC®产品] 使用_delay()函数使LED闪烁

[复制链接]
 楼主| huangcunxiake 发表于 2024-3-11 23:09 | 显示全部楼层 |阅读模式
  1. #include <xc.h>
  2. // PIC16F1719 Configuration Bit Settings
  3. // For more on Configuration Bits, consult y**ice data sheet
  4. // CONFIG1
  5. #pragma config FOSC = ECH // External Clock, 4-20 MHz
  6. #pragma config WDTE = OFF // Watchdog Timer (WDT) disabled
  7. #pragma config PWRTE = OFF // Power-up Timer disabled
  8. #pragma config MCLRE = ON // MCLR/VPP pin function is MCLR
  9. #pragma config CP = OFF // Flash Memory Code Protection off
  10. #pragma config BOREN = ON // Brown-out Reset enabled
  11. #pragma config CLKOUTEN = OFF // Clock Out disabled.
  12. #pragma config IESO = ON // Internal/External Switchover on
  13. #pragma config FCMEN = ON // Fail-Safe Clock Monitor enabled
  14. // CONFIG2
  15. #pragma config WRT = OFF // Flash Memory Self-Write Protect off
  16. #pragma config PPS1WAY = ON // PPS one-way control enabled
  17. #pragma config ZCDDIS = ON // Zero-cross detect disabled
  18. #pragma config PLLEN = OFF // Phase Lock Loop disable
  19. #pragma config STVREN = ON // Stack Over/Underflow Reset enabled
  20. #pragma config BORV = LO // Brown-out Reset low trip point
  21. #pragma config LPBOR = OFF // Low-Power Brown Out Reset disabled
  22. #pragma config LVP = OFF // Low-Voltage Programming disabled
  23. void main(void) {
  24. unsigned char portValue;
  25. // Port D access
  26. ANSELD = 0x0; // set to digital I/O (not analog)
  27. TRISD = 0x0; // set all port bits to be output
  28. // Port B access
  29. ANSELB = 0x0; // set to digital I/O (not analog)
  30. TRISB = 0x0; // set all port bits to be output
  31. while(1) {
  32. portValue = 0x05;
  33. LATD = portValue; // write to port latch - RD[0:3] = LED[0:3]
  34. LATB = portValue; // write to port latch - RB[0:3] = LED[4:7]
  35. // delay value change
  36. _delay(25000); // delay in instruction cycles
  37. portValue = 0x0A;
  38. LATD = portValue; // write to port latch - RD[0:3] = LED[0:3]]
  39. LATB = portValue; // write to port latch - RB[0:3] = LED[4:7]
  40. _delay(25000); // delay in instruction cycles
  41. }
  42. return;
  43. }


 楼主| huangcunxiake 发表于 2024-3-11 23:09 | 显示全部楼层
由于执行速度在大多数情况下都会导致LED的闪烁速度超出人眼的识别能力,因此需要降低执行速度。_delay()是编译器的内置函数。
盗铃何须掩耳 发表于 2024-3-13 09:49 | 显示全部楼层
编译器还能内置delay函数吗?那也就是软件延时吧
可怜的小弗朗士 发表于 2024-3-15 12:08 | 显示全部楼层
闪烁是需要延时的,不然分辨不出来
黑心单片机 发表于 2024-3-22 16:55 | 显示全部楼层
这个delay不需要实现函数吗
guijial511 发表于 2024-3-23 12:42 来自手机 | 显示全部楼层
delay函数是效率最低的
wutaosamuel 发表于 2024-3-25 11:06 | 显示全部楼层
delay() 是cpu通过执行多个NOP指令来达到延时效果,也可以使用timer的中断来做
黑心单片机 发表于 2024-3-26 14:39 | 显示全部楼层
这是内联函数吗
稳稳の幸福 发表于 2024-3-26 20:06 | 显示全部楼层
锁存器和端口寄存器是什么关系。怎么好像两个都是一样的结果。
菜鸟的第一步 发表于 2024-3-28 13:03 | 显示全部楼层
用延时函数让LED闪烁属于常见的方法
您需要登录后才可以回帖 登录 | 注册

本版积分规则

222

主题

3700

帖子

11

粉丝
快速回复 在线客服 返回列表 返回顶部