pic - PIC - timer0 和中断计数和闪烁 LED
问题描述
我正在使用带有中断的 PIC10F322 和 timer0,试图让 LED 闪烁,打开 1 秒,然后再关闭 1 秒。我曾尝试编写要求中断的计时器,并且效果很好。但是计算时间是不正确的。计数大约持续 2 秒,然后又关闭 2 秒,这是不正确的。我想知道计算有什么问题吗?芯片是16MHZ,定时器0是8位,预分频器设置为256。我的尝试是每1ms触发一次中断,然后进行999循环计数以达到1秒。
我的计算是:
256 - [(Delay * Fosc) / (prescaler*4)] = 256 - [(1ms * 16000000)/(256*4)] = 240
#define _XTAL_FREQ 16000000
#include <xc.h>
#pragma config FOSC = INTOSC // Oscillator Selection bits (INTOSC oscillator: CLKIN function disabled)
#pragma config BOREN = OFF // Brown-out Reset Enable (Brown-out Reset disabled)
#pragma config WDTE = OFF // Watchdog Timer Enable (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = OFF // MCLR Pin Function Select bit (MCLR pin function is MCLR)
#pragma config CP = OFF // Code Protection bit (Program memory code protection is disabled)
#pragma config LVP = OFF // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming)
#pragma config LPBOR = ON // Brown-out Reset Selection bits (BOR disabled)
#pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
#pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off)
int z = 0,count=0;
void main(void) {
ANSELA = 0x00;
TRISA = 0b0100;
TRISAbits.TRISA2 = 1;
LATAbits.LATA0 = 0;
INTCONbits.GIE=1; /* Enable Global Interrupt*/
INTCONbits.PEIE = 1; /* Enable peripheral Interrupt */
OPTION_REGbits.T0CS = 0;
OPTION_REGbits.PSA = 0;
OPTION_REGbits.PS0 = 1; /* set prescaler to 256 */
OPTION_REGbits.PS1 = 1;
OPTION_REGbits.PS2 = 1;
OPTION_REGbits.INTEDG = 0;
TMR0 = 240;
INTCONbits.TMR0IF = 0;
INTCONbits.TMR0IE = 1;
while(1){
}
return;
}
void __interrupt(high_priority) tcInt(void)
{
if (TMR0IE && TMR0IF)
{
TMR0 = 240;
TMR0IF = 0;
if (count == 999)
{
z = 0;
LATAbits.LATA0 = ~LATAbits.LATA0;
count =0;
}
count++;
}
return;
}
解决方案
你的定时器计算很好,但我猜你的微控制器运行在 8 MHz,如果你使用内部振荡器(#pragma config FOSC = INTOSC
),这是复位后的默认值。如果您喜欢 16 MHz,则必须在OSCCON
寄存器中进行选择。
OSCCON = 0b01110000;
推荐阅读
- google-sheets - 在 Google 表格中每隔 n 行计算一个单元格中出现的次数
- objective-c - 从objective c 转换为swift,objective c 中的self.activeKeyboardSize 是什么?
- r - 如何使用 R 找到至少三个 HbA1c 值超过 6.5 的患者
- php - 如何在 php 中使用 curl 的 header
- python - 如何避免我的 sns 热图边缘被切断?
- python - DJANGO CHECK CONSTRAINTS:SystemCheckError:(models.E032)约束名称'age_gte_18'在模型中不是唯一的
- javascript - 将 SVG 路径垂直切成两半并获得结果路径
- reactjs - 在 redux-react 中进行 API 调用时,动作被无限调用
- sql - 为什么不过滤这两个属性?
- performance - 更少的数据位表示是否会提高机器学习模型的训练时间?