我做了一个da的中断,一个ad的中断,现在我想通过外接按键控单片机执行其中某一个中断,但是仿真只执行其中da的中断,我是初学者,请大神指导。我同学说可以用中断嵌套,但是我不会这个。
下面是程序清单:
#include<reg51.h>
#include<math.h>
#define DAC_PORT P2
sbit DAC_CS_WR=P3^0;
sbit K1=P1^5;
sbit K2=P1^6;
sbit K3=P1^7;
sbit K4=P3^3;
sbit K5=P3^2;
sbit P10=P1^0;
sbit P11=P1^1;
sbit P12=P1^2;
#include<intrins.h>
#include<tlc0832.c>
#define uchar unsigned char
#define nop _nop_()
sbit CS = P3^0;
sbit CLK = P3^1;
sbit DIDO = P3^7;
code uchar seven_seg[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
uchar cp1,cp2,dat_ad;
typedef unsigned char u8;
typedef unsigned int u16;
typedef unsigned long u32;
u8 mode;
u8 time;
u8 AM;
void Timer0Int(void);
void scanKey(void);
void sys_int(void);
void squ_wave(u8 location);
void tri_wave(u8 location);
void tra_wave(u8 location);
void saw_wave(u8 location);
//Ä£êy×a»»
void tlc0832_init(void)
{
CS = 1;nop;
CLK = 1;
CS = 0;
}
uchar tlc0832_ch0(void)
{
uchar dat1,i;
tlc0832_init();
DIDO = 1;CLK = 0;nop;CLK = 1;nop;
DIDO = 1;CLK = 0;nop;CLK = 1;nop;
DIDO = 0;CLK = 0;nop;CLK = 1;nop;
DIDO = 1;
for(i = 0;i<=8;i++)
{
dat1 = dat1<<1;
CLK = 0;nop;
if(DIDO)dat1 = dat1|0x01;
CLK = 1;nop;
}
return(dat1);
CS = 1;
}
void timer0_init() {
TMOD = 0x01;
TL1 = 0x78;
TH1 = 0xec;
EA = 1;
TR1 = 1;
ET1 = 1;
}
void timer0_isr()interrupt 0
{
TL1 = 0x78;
TH1 = 0xec;
cp1++;
if(cp1>100) //0.5s
{
cp1 = 0;
dat_ad=tlc0832_ch0();
}
P0 = 0xff;
cp2++;
if(cp2>=3)
cp2 = 0;
switch(cp2)
{
case 0:P0 = seven_seg[dat_ad%10];P10=1,P11=0,P12=0;break;
case 1:P0 = seven_seg[dat_ad%100/10];P10=0,P11=1,P12=0;break;
case 2:P0 = seven_seg[dat_ad/100];P10=0,P11=0,P12=1;break;
}
}
//êyÄ£×a»»
void Timer0Int(void)
{
TMOD &= 0xF0;
TMOD |= 0x02;
TL0 = 0x9C;
TH0 = 0x9C;
TF0 = 0;
TR0 = 1;
EA = 1;
ET0 = 1;
}
void scanKey(void)
{
if(K1==0)
{
mode=1;
}
if(K2==0)
{
mode=2;
}
if(K3==0)
{
mode=3;
}
}
void sys_int(void)
{
time=0;
DAC_CS_WR=0;
DAC_PORT=0;
mode=0;
AM=255;
}
void squ_wave(u8 location)
{
if(location<50)
DAC_PORT=AM;
else
DAC_PORT=0x00;
}
void tri_wave(u8 location)
{
u8 y;
if(location<50)
y=(50-location)*AM/50;
else
y=(location-50)*AM/50;
DAC_PORT=y;
}
void saw_wave(u8 location)
{
DAC_PORT=location*AM/100;
}
void Timer0Work() interrupt 2
{
switch(mode)
{
case 1:squ_wave((u8) time);break;
case 2:tri_wave((u8) time);break;
case 3:saw_wave((u8) time);break;
}
time++;
if(time>=100)
time=0;
}
void IntConfiguration()
{
IT0=1;
EX0=1;
PX0=0;
IT1=1;
EX1=1;
PX1=1;
EA=1;
}
int main(void)
{
IntConfiguration();
if(K4==0){
sys_int();
Timer0Int();
while(1)
{
scanKey();
}
}
if(K5==0){
tlc0832_init();
timer0_init();
}
}
仿真图:
|