【问题标题】:8051 c interrupts8051 c 中断
【发布时间】:2012-11-24 01:53:06
【问题描述】:

我正在使用 C 在 8051 微控制器上编写程序。我使用的编译器是 Keil Microvision。我被卡住了,无法弄清楚我的代码中缺少什么。我知道这是非常基本的代码,我只是不知道我应该做什么。

所以我所做的几乎就是向用户发送一个句子并让他们通过串行端口回答是或否,我使用了串行中断。那部分工作正常。如果我从那个人那里得到否定,我想通过定时器中断生成一个 5kHz 的方波。我希望这个方波由外部中断控制,当引脚 P3.2 上的外部中断打开或关闭时打开和关闭它。 这是我所有的代码

#include <REG52.H>
#include <stdio.h>
sbit WAVE = P1 ^ 7;
#define BIT(x) (1 << (x))

void timer0() interrupt 1  // timer is controlling square wave timer 0
{
  WAVE = ~WAVE;
}

void interrupt0() interrupt 0
{
  IE ^= BIT(1);
}

void serial0() interrupt 4
{
  unsigned char x;
  unsigned int i, z;
  unsigned char yes[] = " YES ";
  unsigned char no[] = " NO ";
  unsigned char nvalid[] = " NOT VALID TRY AGAIN ";

  while (RI == 1) {

    x = SBUF;
    RI = 0;

    if (z < 1) {
      if (x == 'n') {
        for (i = 0; i < 4; i++) {
          SBUF = no[i];
          while (TI == 0) ;  //wait for transmit
          TI = 0;
          z++;
        }
      }
    } else {
      return;
    }

    if (x == 'y') {
      for (i = 0; i < 5; i++) {
        SBUF = yes[i];
        while (TI == 0) ;
        TI = 0;
      }
    } else if (x != 'n') {
      for (i = 0; i < 21; i++) {
        SBUF = nvalid[i];
        while (TI == 0) ;
        TI = 0;
      }
    }

    TI = 0;
    return;
  }
}

void main()
{
  TMOD = 0x20;
  TH1 = 0xF6;    //baud rate
  SCON = 0x50;
  TH0 = 0xA4;
  IE = 0x93;    //enable interrupts
  IP = 0x10;    // propriety to serial interrupt
  TR1 = 1;    //start timer 1
  TR0 = 1;    //clear timer 0
  TI = 1;
  printf("Hello, Are you okay? Press y for yes and n for no ");
  while (1) ;
} 

我遇到问题的部分是前面代码中的这两个中断

void timer0() interrupt 1 // timer is controlling square wave timer 0
{ 
    WAVE=~WAVE;
}

void interrupt0() interrupt 0
{
  IE ^= BIT(1);
} 

任何正确方向的提示将不胜感激!谢谢。抱歉格式化

【问题讨论】:

  • 如果你能更好地格式化事情会有所帮助。
  • 如果您解释了没有发生的事情也会有所帮助。

标签: c 8051


【解决方案1】:

被中断修改的变量应该定义为volatile:

volatile sbit WAVE = P1 ^ 7;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多