# ICD bits -- ADC and 16F886 E/SP.

#### R!f@@

Joined Apr 2, 2009
9,734
So I tried with these settings
C:
#define Beepermask   0x40              // Buzzer @ RC6
#define TMR2period_us 118
#define msec5PSset 5000/TMR2period_us
#define PR2set 1
Period is 118us and PR = 1

And I get this

What do you think ?

#### R!f@@

Joined Apr 2, 2009
9,734
Another question ?
I wanted to turn on the LCD backlit LED. As before it comes on and goes off. so I changed the location to while loop as below
C:
   while(1){                           // While
//------------------- Generate Buzzer Pattern --------------------
// Shifts BeepPattern each time period turning beeper on/off in pattern
if(BeepTimer_5ms == 0){            // time to do something?
if(BeepPattern != 0){            // more to do?
if(BeepPattern & BeepCtrlmask) // yes, beep on/off according
// to bit in pattern
soundBEEPER = 1;               // Beep
else
soundBEEPER = 0;               // Else don't beep
BeepTimer_5ms = BeepTimerSet;  // then reload timer
BeepPattern = BeepPattern >> 1;// shift it right for
}                                // next pattern bit
else
soundBEEPER = 0;               // no bits in pattern, turn it off
}
Backlit = 1;
}

}
And it comes on. I decided to scope the PIC out and guess what I found

I do not understand why it is pulsing. Shouldn't it be a steady output

#### R!f@@

Joined Apr 2, 2009
9,734
I was messing with it & thinking and I came with this in the IRQ
C:
//------------ Service Backlight ----------/
if(Back_lit)
else
PORTC = PORTCimg;
And Backlit (RC7) is a hard on - 5V at 0Hz
Good or Bad ?

#### R!f@@

Joined Apr 2, 2009
9,734
Writing to LCD is not working cause of interrupt.
Disable interrupt and it works.
LCD characters are jumbled up.
I think it is due to PORTCimg writing to PORTC.

Is there anyway to write to LCD using interrupt vector?

I just need one example on how to do this.
Does anyone has any idea ?

Or will I2C interface to LCD solve my problem ?

Last edited:

#### JohnInTX

Joined Jun 26, 2012
4,199
@R!f@@
Some confusion here. Let's sort it out.
Recall, the dimmer project from which this one comes used Timer 0 as a system TIK (because no Timer2) AND we ran it at 150us interrupt rate so that we could generate the 3KHz buzzer output (because no hardware PWM was present AND we did not want to use MikroC's buzzer routine because it is blocking code). The downside was that with such a fast interrupt rate, we had to watch out that we did not use too many instructions in the IRQ service routine. It worked but with the extra hardware in the PIC, we can do better.

Before we do that, the reason your TMR2 code is faulty is that it works differently than TMR0. To generate a period, TMR2 counts up from 0 to the value stored in PR2. Once it hits that value, it is automatically reset and generates an interrupt (through the postscaler). Unlike TMR0, once its running, you don't have to reload it (like you are doing). Its fully automatic. By reloading it like you are doing, you are fouling the way its supposed to work. TMR2 is a good way to generate a uniform period interrupt because it runs itself - I use it for a system TIK always.. EXCEPT.. When I need a PWM. Then TMR2 becomes the PWM timebase and I do the system tik another way.

Since you have that pesky 3KHz beeper, I would drive it with the hardware PWM. Configure it for 50% duty cycle at 3KHz and turn the beeper on / off by controlling the PWM itself. The attraction is that, like all TMR2/PR2 configurations, once you set it up, you don't have to mess with it - contrast that with interrupting every 150uS. Better, yes?

If you think so..
Since the '886 has a hardware PWM and the buzzer is connected to CCP1 (RC2) why not use that? Modify main's beep control routines to use MikroC's PWM libraries, PWMinit(3000), PWM1_SetDuty(127), PWM1_Start/Stop. Since these use the hardware PWM they don't block. Nice.
They do use TMR2/PR2 however so you can no longer use that as a system tik. No worries, just stay on TMR0, remove the old PWM output and beep output toggle code then set the system TIK time to something more sane, 5msec is a pretty good place to start.

That's what I would do...
Good luck

All of this assumes that I know what this latest project is to do... so
What does it do??