下面switch语句中,能执行ConfigPWM()语句,点亮了LED0和LED1,但不能进入中断点亮LED2。为什么?- #include <intrins.h>
- unsigned char tmp;
- sbit led0=P0^0; //指示灯0
- sbit led1=P0^1; //指示灯1
- sbit led2=P0^2; //指示灯2
- unsigned long PeriodCnt = 0; //PWM 周期计数值
- unsigned char HighRH = 0; //高电平重载值的高字节
- unsigned char HighRL = 0; //高电平重载值的低字节
- unsigned char LowRH = 0; //低电平重载值的高字节
- unsigned char LowRL = 0; //低电平重载值的低字节
- unsigned char T1RH = 0; //T1 重载值的高字节
- unsigned char T1RL = 0; //T1 重载值的低字节
-
- void ConfigPWM(unsigned int fr, unsigned char dc);
- void init(); //串口初始化
- void main()
- {
-
- init();
- while(1)
- {
- if(RI==1) //是否有数据到来
- {
- RI = 0;
- tmp = SBUF; //暂存接收到的数据
- switch(tmp){
- case'1': //前进
- EA=1;
- ConfigPWM(100, 10);
- while(1);
- break;
- default: //停止
- led0=1;
- led1=1;
- led2=1;
- break;
- }
- }
- }
- void init() //串口初始化
- {
- ES=0; //关中断
- SCON = 0x50; 位,1位停止位,无奇偶校验),波特率可变
- TMOD = 0x20;
- TH1=TL1=0xFD;
- PCON &= 0x7f;
- TR1 = 1;
- ti=0;
- RI=0;
-
- ES=1;
- }
-
- /* 配置并启动 PWM,fr-频率,dc-占空比 */
- void ConfigPWM(unsigned int fr, unsigned char dc){
- unsigned int high, low;
- unsigned long tmp;
- tmp = (11059200/12) / fr; //计算一个周期所需的计数值
- high = (tmp*dc) / 100; //计算高电平所需的计数值
- low = tmp - high; //计算低电平所需的计数值
- high = 65536 - high + 12; //计算高电平的重载值并补偿中断延时
- low = 65536 - low + 12;//计算低电平的重载值并补偿中断延时
-
- HighRH = (unsigned char)(high>>8); //高电平重载值拆分为高低字节
- HighRL = (unsigned char)high;
- LowRH = (unsigned char)(low>>8); //低电平重载值拆分为高低字节
- LowRL = (unsigned char)low;
-
- TMOD &= 0xF0; //清零 T0 的控制位
- TMOD |= 0x01; //配置 T0 为模式 1
- TH0 = HighRH; //加载 T0 重载值
- TL0 = HighRL;
- ET0 = 1; //使能 T0 中断
- TR0 = 1; //启动 T0
- led0 = 0;
- led1=0;
- }
-
- /* T0 中断服务函数,产生 PWM 输出 */
- void InterruptTimer0() interrupt 1{
-
- led2=0;
- }
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>