打印
[PIC®/AVR®/dsPIC®产品]

使用_delay()函数使LED闪烁

[复制链接]
671|9
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
huangcunxiake|  楼主 | 2024-3-11 23:09 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#include <xc.h>
// PIC16F1719 Configuration Bit Settings
// For more on Configuration Bits, consult y**ice data sheet
// CONFIG1
#pragma config FOSC = ECH // External Clock, 4-20 MHz
#pragma config WDTE = OFF // Watchdog Timer (WDT) disabled
#pragma config PWRTE = OFF // Power-up Timer disabled
#pragma config MCLRE = ON // MCLR/VPP pin function is MCLR
#pragma config CP = OFF // Flash Memory Code Protection off
#pragma config BOREN = ON // Brown-out Reset enabled
#pragma config CLKOUTEN = OFF // Clock Out disabled.
#pragma config IESO = ON // Internal/External Switchover on
#pragma config FCMEN = ON // Fail-Safe Clock Monitor enabled
// CONFIG2
#pragma config WRT = OFF // Flash Memory Self-Write Protect off
#pragma config PPS1WAY = ON // PPS one-way control enabled
#pragma config ZCDDIS = ON // Zero-cross detect disabled
#pragma config PLLEN = OFF // Phase Lock Loop disable
#pragma config STVREN = ON // Stack Over/Underflow Reset enabled
#pragma config BORV = LO // Brown-out Reset low trip point
#pragma config LPBOR = OFF // Low-Power Brown Out Reset disabled
#pragma config LVP = OFF // Low-Voltage Programming disabled
void main(void) {
unsigned char portValue;
// Port D access
ANSELD = 0x0; // set to digital I/O (not analog)
TRISD = 0x0; // set all port bits to be output
// Port B access
ANSELB = 0x0; // set to digital I/O (not analog)
TRISB = 0x0; // set all port bits to be output
while(1) {
portValue = 0x05;
LATD = portValue; // write to port latch - RD[0:3] = LED[0:3]
LATB = portValue; // write to port latch - RB[0:3] = LED[4:7]
// delay value change
_delay(25000); // delay in instruction cycles
portValue = 0x0A;
LATD = portValue; // write to port latch - RD[0:3] = LED[0:3]]
LATB = portValue; // write to port latch - RB[0:3] = LED[4:7]
_delay(25000); // delay in instruction cycles
}
return;
}


使用特权

评论回复
沙发
huangcunxiake|  楼主 | 2024-3-11 23:09 | 只看该作者
由于执行速度在大多数情况下都会导致LED的闪烁速度超出人眼的识别能力,因此需要降低执行速度。_delay()是编译器的内置函数。

使用特权

评论回复
板凳
盗铃何须掩耳| | 2024-3-13 09:49 | 只看该作者
编译器还能内置delay函数吗?那也就是软件延时吧

使用特权

评论回复
地板
可怜的小弗朗士| | 2024-3-15 12:08 | 只看该作者
闪烁是需要延时的,不然分辨不出来

使用特权

评论回复
5
黑心单片机| | 2024-3-22 16:55 | 只看该作者
这个delay不需要实现函数吗

使用特权

评论回复
6
guijial511| | 2024-3-23 12:42 | 只看该作者
delay函数是效率最低的

使用特权

评论回复
7
wutaosamuel| | 2024-3-25 11:06 | 只看该作者
delay() 是cpu通过执行多个NOP指令来达到延时效果,也可以使用timer的中断来做

使用特权

评论回复
8
黑心单片机| | 2024-3-26 14:39 | 只看该作者
这是内联函数吗

使用特权

评论回复
9
稳稳の幸福| | 2024-3-26 20:06 | 只看该作者
锁存器和端口寄存器是什么关系。怎么好像两个都是一样的结果。

使用特权

评论回复
10
菜鸟的第一步| | 2024-3-28 13:03 | 只看该作者
用延时函数让LED闪烁属于常见的方法

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

189

主题

3407

帖子

9

粉丝