【发布时间】:2020-05-20 08:40:37
【问题描述】:
我熟悉 MPLAB X IDE 并开始使用 Microchip PIC 12F617 使用 C 语言进行开发。
我有一个简单的程序(从别处复制)来让 LED 闪烁:亮 1 秒然后灭 1 秒。一切正常,但我对延迟的计算与 1 秒不匹配。谁能告诉我哪里出错了?
选择内部振荡器 @ 8 MHz
到 TMR2 的时钟是 FOSC / 4 是 2 MHz
PR2 默认为 255,因此 2 MHz / 255 的中断为 7843 Hz
TMR2 预分频 = 1;后标度设置为 16;为 490 赫兹
循环计数 675 是 0.726 Hz,因此 LED 应该开/关 1.38 秒,但它不是
考虑到执行指令的时间,led on/off 应该更长。
我错过了什么?
/*
* File: main.c
* Author: user2439830
* Target: PIC12F617
* Compiler: XC8 v2.05
*
* PIC12F617
* +------v------+
* 5v0 ->:1 VDD VSS 8:<- GND
* <>:2 GP5 GP0 7:<> PGD
* <>:3 GP4 GP1 6:<> PGC
* VPP ->:4 GP3 GP2 5:<>
* +-------------+
* DIP-8
*
* Created on May 20, 2020, 5:51 PM
*/
#pragma config FOSC = INTOSCIO // Oscillator Selection bits (INTOSCIO oscillator: I/O function on RA4/AN3/T1G/OSC2/CLKOUT, I/O function on RA5/T1CKI/OSC1/CLKIN)
#pragma config WDTE = ON // Watchdog Timer Enable bit (WDT enabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = ON // MCLR Pin Function Select bit (MCLR pin is MCLR function and weak internal pull-up is enabled)
#pragma config CP = OFF // Code Protection bit (Program memory is not code protected)
#pragma config IOSCFS = 8MHZ // Internal Oscillator Frequency Select (8 MHz)
#pragma config BOREN = ON // Brown-out Reset Selection bits (BOR enabled)
#pragma config WRT = OFF // Flash Program Memory Self Write Enable bits (Write protection off)
#include <xc.h>
void t2delay( void );
void main(void)
{
// Note: TRISIO = TRISA; IO direction register
TRISIO = 0; // 0 set corresponding pin in GPIO to output
// 76543210
T2CON = 0b01111000; // postscale=16, prescale=1, timer off
T2CON |= ( 1 << 2 ); //timer2 on TMR2ON set to 1
while (1)
{
// Note: GPIO = PORTA
GPIO = 255;
t2delay();
GPIO = 0;
t2delay();
}
return;
}
// TMR2IF set to when when TMR2 == PR2 (set to 255 on reset)
void t2delay( void )
{
unsigned int i;
for( i = 0; i < 675; i++)
{
while( !TMR2IF );
TMR2IF = 0;
}
}
【问题讨论】: