写了一段 单片机模拟信号源的程序,通过外部中断0来控制波形的选择,通过定时器T0中断来控制波形的输出,但无论怎么调试都无法进入中断,波形生成和波形输出程序好像没什么问题,放在主程序下可以正常出波形,但用中断就出不来了。调试了一天也不知道哪里出错了,请各位大佬帮忙检查检查。不是很会用这个代码编辑程序,程序有点乱请见谅。
- #include
- #include
- typedef unsigned char uchar;
- typedef unsigned int uint;
- ***it Key1= P1 ^ 0;
- ***it Key2= P1 ^ 1;
- ***it Key3= P1 ^ 2;
- ***it Key4= P1 ^ 3;
- uchar k = 0;
- uchar k1 = 0;
- void Delay(uchar m)
- {
- while(m--);
- }
- void delayms(uint ms)
- {
- uint j;
- while(ms--)
- for(j=114; j>0; j--);
- }
- void triangle_wave_display()
- {
- while(1)
- {
- P0 =k++;
- Delay(1);
- if (k == 0xff)
- break;
- }
- while(1)
- {
- P0 = k--;
- Delay(1);
- if (k == 0x00)
- break;
- }
- }
- void sawtooth_wave_display()
- {
- while(1)
- {
- P0 = k--;
- Delay(1);
- if (k == 0x00)
- break;
- }
- }
- void square_wave_display()
- {
- while(1)
- {
- P0 = 0xff;
- k++;
- Delay(1);
- if (k == 0xff)
- break;
- }
- while(1)
- {
- P0 = 0x00;
- k--;
- Delay(1);
- if (k == 0x00)
- break;
- }
- }
- uchar code sin_wave[256]={
- 0x80,0x83,0x86,0x89,0x8d,0x90,0x93,0x96,0x99,0x9c,0x9f,0xa2,0xa5,0xa8,0xab,0xae,
- 0xb1,0xb4,0xb7,0xba,0xbc,0xbf,0xc2,0xc4,0xc7,0xca,0xcc,0xcf,0xd1,0xd4,0xd6,0xd8,
- 0xdb,0xdd,0xdf,0xe1,0xe3,0xe5,0xe7,0xe9,0xea,0xec,0xee,0xef,0xf1,0xf2,0xf4,0xf5,
- 0xf6,0xf7,0xf9,0xfa,0xfa,0xfb,0xfc,0xfd,0xfe,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,
- 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0xfe,0xfd,0xfc,0xfb,0xfa,0xfa,0xf9,0xf7,
- 0xf6,0xf5,0xf4,0xf2,0xf1,0xef,0xee,0xec,0xea,0xe9,0xe7,0xe5,0xe3,0xe1,0xdf,0xdd,
- 0xdb,0xd8,0xd6,0xd4,0xd1,0xcf,0xcc,0xca,0xc7,0xc4,0xc2,0xbf,0xbc,0xba,0xb7,0xb4,
- 0xb1,0xae,0xab,0xa8,0xa5,0xa2,0x9f,0x9c,0x99,0x96,0x93,0x90,0x8d,0x89,0x86,0x83,
- 0x80,0x7d,0x7a,0x77,0x73,0x70,0x6d,0x6a,0x67,0x64,0x61,0x5e,0x5b,0x58,0x55,0x52,
- 0x4f,0x4c,0x49,0x46,0x44,0x41,0x3e,0x3c,0x39,0x36,0x34,0x31,0x2f,0x2c,0x2a,0x28,
- 0x25,0x23,0x21,0x1f,0x1d,0x1b,0x19,0x17,0x16,0x14,0x12,0x11,0x0f,0x0e,0x0c,0x0b,
- 0x0a,0x09,0x07,0x06,0x06,0x05,0x04,0x03,0x02,0x02,0x01,0x01,0x01,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x02,0x02,0x03,0x04,0x05,0x06,0x06,0x07,0x09,
- 0x0a,0x0b,0x0c,0x0e,0x0f,0x11,0x12,0x14,0x16,0x17,0x19,0x1b,0x1d,0x1f,0x21,0x23,
- 0x25,0x28,0x2a,0x2c,0x2f,0x31,0x34,0x36,0x39,0x3c,0x3e,0x41,0x44,0x46,0x49,0x4c,
- 0x4f,0x52,0x55,0x58,0x5b,0x5e,0x61,0x64,0x67,0x6a,0x6d,0x70,0x73,0x77,0x7a,0x7d
- };
- void Keyboard(void)
- {
- if(Key1==0)
- {
- delayms(10);
- if(Key1==0)
- {
- while(!Key1);
- k1=0;
- }
- }
- else if(Key2==0)
- {
- delayms(10);
- if(Key2==0)
- {
- while(!Key2);
- k1=1;
- }
- }
- else if(Key3==0)
- {
- delayms(10);
- if(Key3==0)
- {
- while(!Key3);
- k1=2;
- }
- }
- else if(Key4==0)
- {
- delayms(10);
- if(Key4==0)
- {
- while(!Key4);
- k1=3;
- }
- }
- }
- void interrupt_keyboard() interrupt 0
- {
- Keyboard();
- }
- void interrupt_waveout() interrupt 1
- {
- int t;
- switch(k1)
- {
- case 0:
- triangle_wave_display();
- break;
- case 1:
- sawtooth_wave_display();
- break;
- case 2:
- square_wave_display();
- break;
- case 3:
- for(t =0 ; t < 256 ; t++)
- {
- P0 = sin_wave[t];
- }
- break;
- }
- }
- void main()
- {
- SP=0xA0;
-
- TMOD=0x11;
- EA=1; ET0=1; ET1=0;
-
- TL0=(65536 - 45872) % 256;
- TH0=(65536 - 45872) / 256;
-
TR0=1;
IT0=0;
EX0=1;
while(1);
- }
复制代码
附图
1
已退回2积分
|
|
|
|