timer_init()之后的写入延迟来悬挂系统。

发布于 2025-01-22 05:45:17 字数 1228 浏览 2 评论 0原文

在编程PIC18F45K22时,我面临一个奇怪的问题。我正在启动系统模块,然后写一个阻止延迟3秒钟以等待触摸屏启动(因为我需要将一些信息发送到此屏幕)。但是,当我这样做时,我发现当我在编写延迟之前启动计时器时,我发现系统冻结了。 这是我使用的代码 注意:所有功能都可以正常工作,没有任何问题。

void main(void) { 
    Sys_Init(); 
     __delay_ms(3000);
   
 while(1){
//Code
} 

系统启动代码

void Sys_Init(void) {
     Delay_XTAL(); 
     OSCILLATOR_Init();
     DIGITAL_PIN_STATE();
     ANALOG_PIN_STATE();
     ADC_INIT(); //change clock select pins when oscillator changes
     Spi_Init(); 
     DHT22_init();
     Uart_Init(9600); //initiate UART (change baud values when oscillator value changed)
     Timer1_Init(); //initiate timer one (change prescaler and counter value if oscillator value changes)
}

计时器代码

void Timer1_Init(void){ //for interrupt program use this function
//Prescaler 1:1; TMR1 Preload = 61536; Actual Interrupt Time : 1 ms
T1CON = 0x01;
TMR1IF = 0;
TMR1H = 0xF0;
TMR1L = 0x60;
TMR1IE = 1;
INTCON = 0xC0;
}

中断代码

void __interrupt() ISR (void){
    Timer1();
    
    }

void Timer1(void){  //for interrupt program
      if (TMR1IF){
   cnt1++;
    TMR1H    = 0xF0; //overflow every 1 ms
    TMR1L    = 0x60;
   TMR1IF = 0;
  }
}

I am facing a strange issue while programming PIC18f45k22. I am initiating system modules and then I write a blocking delay for 3 seconds to wait for a touch screen to start (because I need to send some information to this screen). But when I do this I discovered that when I initiate timer before writing the delay, I discover that the system freezes.
This is the code that I used
NOTE: all the functions work fine without any problem.

void main(void) { 
    Sys_Init(); 
     __delay_ms(3000);
   
 while(1){
//Code
} 

System initiation code

void Sys_Init(void) {
     Delay_XTAL(); 
     OSCILLATOR_Init();
     DIGITAL_PIN_STATE();
     ANALOG_PIN_STATE();
     ADC_INIT(); //change clock select pins when oscillator changes
     Spi_Init(); 
     DHT22_init();
     Uart_Init(9600); //initiate UART (change baud values when oscillator value changed)
     Timer1_Init(); //initiate timer one (change prescaler and counter value if oscillator value changes)
}

Timer code

void Timer1_Init(void){ //for interrupt program use this function
//Prescaler 1:1; TMR1 Preload = 61536; Actual Interrupt Time : 1 ms
T1CON = 0x01;
TMR1IF = 0;
TMR1H = 0xF0;
TMR1L = 0x60;
TMR1IE = 1;
INTCON = 0xC0;
}

Interrupt code

void __interrupt() ISR (void){
    Timer1();
    
    }

void Timer1(void){  //for interrupt program
      if (TMR1IF){
   cnt1++;
    TMR1H    = 0xF0; //overflow every 1 ms
    TMR1L    = 0x60;
   TMR1IF = 0;
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文