关于代码中两个TMOD的问题

2020-02-04 09:29发布

我写了一个程序,用51单片机,用定时器方式产生PWM波形,同时用串口方式改变占空比,问题来了,在初始化中,都有对TMOD赋予初始值,可是我即使让TMOD成为同一个数,最后的程序, 还是不对,串口发送数据接收不到,请大家指教该如何处理呢?
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
3条回答
beck_ck
1楼-- · 2020-02-04 09:40
贴出程序看一下
millwood0
2楼-- · 2020-02-04 15:12
 精彩回答 2  元偷偷看……
millwood0
3楼-- · 2020-02-04 19:29
to continue:

the code to reset tmr1 would be something like this:


void tmr1_init(unsigned char mode) {
  TR1 = 0;  //stop the timer
  TMOD = (TMOD & 0x0f) | ((mode & 0x0f) << 4);
  //you may wish to initialize the tmr/counters now
  TR1 = 1;  //start the timer
}

so in your code, you set tmr1 similarly:

tmr1_init(TMR_COUNTER | TMR_MODE1); //set tmr1 as a counter, not gated and in mode 1

the beauty of this is that if you ever need to swap the assignment of tmr0/tmr1 (tmr0 for counter and tmr1 for timer),

all you need to do is to call different routines

//tmr0 as counter, not gated, mode 1
tmr0_init(TMR_COUNTER | TMR_MODE1);

//tmr1 as timer, gated, mode 1
tmr1_init(TMR_TIMER | TMR_GATED | TMR_MODE1);

the key to embedded programming is to minimize your touch points with the actual hardware. a very counter-intuitive point for many embedded programmers.

一周热门 更多>