【问题标题】:PIC Programming Counter and Touch Sensor IssuePIC 编程计数器和触摸传感器问题
【发布时间】:2021-12-17 20:40:37
【问题描述】:

我是这个 PIC 编程的新手,目前使用 C 进行编程,因为 ASM 需要一些时间来学习。但是,似乎我遇到了最基本的问题。我用 TMR0 设置的计数器工作不正常,触摸传感器似乎没有做任何它应该重置计数器的事情。谁能告诉我我哪里做错了?一直坚持这一点。谢谢! 对于 PIC10F320

[更新]

大家好,感谢 Kozmotronik 发现的错误。重新计算已完成 + 重新分配位,因为全局中断似乎由于某种原因溢出。由于默认情况下 T0CS 是一个 8 位定时器,因此还去掉了 option_reg。在 proteus 中进行了 10 秒的模拟,现在计时器看起来很好! 唯一剩下的就是触摸传感器的重置,我还在想办法。附上我电路中的照片,临时性用普通按钮替换了触摸传感器,因为没有触摸传感器的模型。可以的话请看一下。Proteus circuit


// CONFIG
#pragma config FOSC = INTOSC  // Oscillator Selection 
#pragma config BOREN = OFF    // Brown-out Reset
#pragma config WDTE = OFF    // Watchdog Timer
#pragma config PWRTE = OFF    // Power-up Timer
#pragma config MCLRE = OFF   // MCLR Pin Function Select bit->MCLR pin function is digital input, MCLR internally tied to VDD
#pragma config CP = OFF      // Code Protection 
#pragma config LVP = OFF     // Low-Voltage Programming 
#pragma config LPBOR = OFF    // Brown-out Reset Selection bits
#pragma config BORV = LO    // Brown-out Reset Voltage Selection
#pragma config WRT = OFF    // Flash Memory Self-Write Protection

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
#include <xc.h>
#include <pic10f320.h>

#define _XTAL_FREQ 8000000
#define SWITCH PORTAbits.RA2
#define PWMPin PORTAbits.RA1
#define LEDPin PORTAbits.RA0

unsigned long cnt;
/*
                            Main application
    */


void setup(void)
{    
        /**
    LATx registers
    */
    LATA = 0x00;

    /**
    TRISx registers
    */
    TRISA = 0x04;

    /**
    ANSELx registers
    */
    ANSELA = 0x00;

    //Clear out the duty cycle registers
    PR2 = 255;   
    T2CONbits.T2CKPS = 0b00;
    T2CONbits.TMR2ON = 0x01;

    /*
     * My Calculations:
     * At 8MHz, Tosc is 125ns, Tcyc=4*Tosc=500ns.
     * With PR2=52decimal, prescaler=1, the PWM period is Tcyc(PR2+1) = 500ns*53 = 26.5us.
     * For 100% duty cycle, 
     * PulseWidth = PWMxDCH/L (10bits) * Tosc * 1(prescaler) giving
     * 26.5us/125ns=PWMxDCH/L = 212decimal 
     * PWMxDCH = d4h
     * PWMxDCL = 00h
     * 
     * For 75% duty cycle, the duty cycle registers must count off 26.5us*0.75=19.9us.
     * PulseWidth = PWMxDCH/L (10bits) * Tosc * 1(prescaler) giving
     * 19.9us/125ns=PWMxDCH/L = 159decimal 
     * PWMxDCH = 9fh
     * PWMxDCL = 00h
     */
    
    //Setting timer-0 for the generation of the delay
    TMR0 = 0xFF;  //Set TMR0 to 0
//  OPTION_REG = 0x07;
    //INTCON = 0xA0;
   

>  INTCONbits.TMR0IF = 0; //Clear the TMR0 interrupt flag
>  INTCONbits.TMR0IE = 1;


    OPTION_REGbits.T0CS = 0; //Set TMR0 Clock source to FOS 
    OPTION_REGbits.PSA = 1; //Assign No-Prescaler to TMR0 
    /*
     * My formulas is:
       Fosc/4 = 8MHZ/4 = 2MHZ 

       period for a tick= 1 / 2MHZ = 0.0000005s

       time for 8 bit count = 0.0000005 * 256 = 0.000128s
     * 
     * Overflows every 128us 
     * for one second delay loop cnt = 1000000*(1/128) = 8
     * LC = 7812.5*60 = 480 for 1 minute
     * LC = 468750*60 = 28800 for 1 hour 
     * LC = 28125000*12 = 345600 for 12 hours 
     */
}


void main(void)
{
    setup();
    cnt = 0;
    
    //OSCCON = 2;
    
    while(1){
        PWMPin = 1;
        LEDPin = 1;
        
        if(SWITCH==1){
            cnt++;    
        }
        
        if(INTCONbits.TMR0IF){
            cnt++; //Reset the timer
                //100% Duty Cycle on RA1
            

          > TMR0 = 0 ;  /// reset TMR0 value

            INTCONbits.TMR0IF=0; //Reset Timer Interrupt Flag 
            
        }
            //If 10sec minute limit is reached (change this number to change the delay)
        if(cnt>78125){
                //75% Duty Cycle on RA1
                // PWM1POL active_hi; PWM1OE enabled; PWM1EN enabled; 
            PWM2CON = 0xC0;   
            PWM1CONbits.PWM1OE = 0x01;   //PWM1 Turn on 
            PWM1CONbits.PWM1EN = 0x01;  //PWM1 Enable Output 
            PWM2DCH = 0x00;
            PWM2DCL = 0x00;  
            PWM1DCH = 0x00;
            PWM1DCL = 0x00;
                    
            PWM2DCH = 0xBF;
            PWM2DCL = 0x00;
            
            //LED breathing effect
            for(int i=0;i<212;i++){
                PWM1DCH = i;
                __delay_ms(25);
            }
            for(int i=212;i>0;i--){
                PWM1DCH = i;
                __delay_ms(25);
            }
        }
    }
}

【问题讨论】:

  • PIC 什么?仔细检查时钟树。
  • pic10f320 抱歉

标签: c counter microcontroller interrupt pic


【解决方案1】:

您的计时器溢出公式似乎是错误的。

每 128 毫秒溢出一次

不是真的,我想你在这里混淆了。 8 位计数的时间为 0.000128 秒,即每次定时器溢出为 128 微秒。所以你的循环计数必须是:

一秒延迟循环cnt = 1000000*(1/128) = 7812,5

您必须根据此修正值重新计算余数。对于触摸输入,您必须确保接收到正确的触摸信号,因为我们无法在没有看到的情况下进行诊断。

【讨论】:

  • 你的意思是电路?如果你需要它,我会上传它。但如果我是正确的,它的功能与普通按钮相同,信号输入连接到引脚?还是我必须做一些不同的事情,哇感谢你发现了这个愚蠢的错误
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-29
  • 1970-01-01
相关资源
最近更新 更多