[PIC®/AVR®/dsPIC®产品] 使用中断作为延时在LED上递增计数

[复制链接]
 楼主| huangcunxiake 发表于 2024-3-11 23:07 | 显示全部楼层 |阅读模式
  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. // Interrupt function
  24. void interrupt isr(void){
  25. // only process Timer0-triggered interrupts
  26. if(INTCONbits.TMR0IE && INTCONbits.TMR0IF) {
  27. // static variable for permanent storage duration
  28. static unsigned char portValue;
  29. // write to port latches
  30. LATD = portValue++; // RD[0:3] = LED[0:3]
  31. LATB = (portValue++ >> 4); // RB[0:3] = LED[4:7]
  32. // clear this interrupt condition
  33. INTCONbits.TMR0IF = 0;
  34. }
  35. }
  36. void main(void){
  37. // Port D access
  38. ANSELD = 0x0; // set to digital I/O (not analog)
  39. TRISD = 0x0; // set all port bits to be output
  40. // Port B access
  41. ANSELB = 0x0; // set to digital I/O (not analog)
  42. TRISB = 0x0; // set all port bits to be output
  43. // Timer0 setup
  44. OPTION_REG = 0xD7; // timer 0 internal clock, prescaler 1:256
  45. INTCONbits.TMR0IE = 1; // enable interrupts for timer 0
  46. ei(); // enable all interrupts
  47. while(1);
  48. return;
  49. }


 楼主| huangcunxiake 发表于 2024-3-11 23:08 | 显示全部楼层
同时展示了配置字的用法。
盗铃何须掩耳 发表于 2024-3-13 09:51 | 显示全部楼层
在LED上递增是啥意思
zhizia4f 发表于 2024-3-27 11:12 | 显示全部楼层
这操作溜啊,用中断当delay?
ex7s4 发表于 2024-3-27 12:24 | 显示全部楼层
写的还挺规矩的,支持楼主一波
y1n9an 发表于 2024-3-27 13:25 | 显示全部楼层
用的定时器中断吧这是
w2nme1ai7 发表于 2024-3-27 15:36 | 显示全部楼层
其实不如用STM32了,库函数很好搞的
cen9ce 发表于 2024-3-27 18:20 | 显示全部楼层
不错的,这逻辑思路都是可以的

您需要登录后才可以回帖 登录 | 注册

本版积分规则

222

主题

3700

帖子

11

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