简单的8051 C中断 [英] Simple 8051 C interrupts

查看:72
本文介绍了简单的8051 C中断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C在8051微控制器上编写程序。我正在使用的编译器是Keil microvision。我陷入困境,无法弄清楚代码中缺少的内容。我知道这是非常基本的代码,我无法弄清楚我应该做些什么。



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

这是我的所有代码



I'm using C to write a program on an 8051 microcontroller. The compiler I'm using is Keil microvision. I'm stuck and having trouble figuring out what is missing from my code. I know it's very basic code I just can't figure out what I'm supposed to do.

So pretty much what I am doing is taking sending a sentence out to the user and having them answer yes or know through the serial port and I used a serial interrupt. That part works fine. If I get a no from the person I want to generate a square wave 5kHz by a timer interrupt. I want this square wave to be controlled by an external interrupt turning it on and off when the external interrupt on pin P3.2 is either on or off.
Here is all my code

#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);
}





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



The part I'm having trouble with is these two interrupt from the previous code

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

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





任何正确方向的提示都将不胜感激!谢谢



Any hints in the right direction would be greatly appreciated! Thanks

推荐答案

你陷入了while循环。使用getchar获取char输入。一些 getchar教程



您的代码应该是看起来像这样:



You are stuck in the while loop. Use getchar to get a char input. Some tutorial for getchar.

Your code should look like this:

printf("Hello, Are you okay? Press y for yes and n for no ");
c = getchar();
// implement your logic and handle when OTHER chars.  
if( c == 'y' ) {
}


这篇关于简单的8051 C中断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆