Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

PIC timer XC8 interrupt problem

Status
Not open for further replies.

Nigel Goodwin

Super Moderator
Most Helpful Member
Hi,

I'm still playing with XC8, and thought I'd port the Ringtone player from to play on an 8 pin enhanced 12F1840.

I first moved the code to XC8, and confirmed it worked on a 16F876, then altered the timing values for 8MHz rather than 20MHz (so as to use the internal oscillator on the 12F1840), again confirming it worked on the 876.

So next I moved to the 12F1840, altering the TONE define to RA0, and the TRIS statement accordingly - but while it compiles fine, all you get out is a few clicks. With the scope on RA0 it appears to be getting the timing from TMR0, but not the frequency pules from TMR1?. I've tried various of the RA pins, and none of them work.

So this afternoon I tried a 16F1827 instead, starting from the working 876 code - just altered the config and set the oscillator to 8MHz, and it worked perfectly.

So next I tried RA0 - same result as the 12F1840 - so I though perhaps it didn't like PORTA?, so tried RB1 instead - same result :banghead:

As I understand the program it uses the TONE definition to toggle the output port, so why will it only work with RB0? - I can't help but think I'm missing something somewhere?.

Any ideas? - I've attached the code I'm using, complete with commented out changes for TONE and TRIS
 

Attachments

  • ringtone_16f1827.c
    8.8 KB · Views: 276
Your code TRISB0 = 0; sets only RB0 as output. For PORTA to have output add TRISA=0; you know that.

pic12f1840 has ADC so you also need code ANSELA=0; to make port digital
pic16f1827 you have to set PORTA to digital with ANSELA,ANSELB
 
Last edited:
Your code TRISB0 = 0; sets only RB0 as output. For PORTA to have output add TRISA=0;

Where do you see that he's trying to use PORTA? I don't see that anywhere in the code.
 
Your code TRISB0 = 0; sets only RB0 as output. For PORTA to have output add TRISA=0;

As I explained above, I added TRISA when I tried PORTA, the code above though has options for RB0 (which works) and RB1 (which doesn't).

pic12f1840 has ADC so you also need code ANSELA=0; to make port digital

According to the datasheet it's not required if the pin is set as an output, as I'm doing. Also, of course, this wouldn't prevent RB1 working in the 16F1827 example.

But thanks for your input.
 
Hi Nigel,

Are you simulating the code or is it an actual hardware implementation?

John
 
Where do you see that he's trying to use PORTA? I don't see that anywhere in the code.
Look at the OP.

"I do not think there is any thrill that can go through the human heart like that felt by the inventor as he sees some creation of the brain unfolding to success... such emotions make a man forget food, sleep, friends, love, everything." --Nikola Tesla
I guess you know that every man that manages to repair his car goes through the same emotional experience as Tesla described.
 
Well, that blows my theory out of the water. I have had problems with that specific chip in hardware simulation using ICD3 and PK3. So, I effectively did what you have been doing. I wrote compatible code for a 16F enhanced chip (16F1519 0r 16F1829), got it working, then just changed the configuration and header to the 12F1840. I confirmed that the very same code (adjusted) would not work in the hardware sim, but did work in real life. It was a long shot. Sorry, I couldn't help.

John
 
According to the datasheet it's not required if the pin is set as an output, as I'm doing. Also, of course, this wouldn't prevent RB1 working in the 16F1827 example.

But thanks for your input.
Capture1.gif

This is from the datasheet.
I'm also talking from experience.
Tt is your lose not to try.
 
A little further info, I'm accessing RB0 and RB1 in the ISR, code here:

TRISB0 = 0; /* Make TONE an output */
TRISB1 = 0; /* Make RB1 an output */

void interrupt interr(void)
{
if (T0IF) {
TMR0 = beat_speed;
if (TMR0Count) TMR0Count--;
T0IF = 0;
}
if (TMR1IF) {
if (beep) TONE = !TONE;
else TONE = 0;
if (beep) RB1 = !RB1;
else RB1 = 0;
TMR1H = preloadTMR1H;
TMR1L = preloadTMR1L;
TMR1IF = 0; /* Clear Flag */
}
}

And this is what output I get, yellow trace is RB0 and blue trace RB1, probes are x10 - hence the 200mV setting.

NewFile0.png
NewFile1.png


Notice a nice square-wave on RB0, but only a brief spike on RB1
 
Hi Nigel. Happy Holidays.

Could you post the 12F1840 XC8 version, please?

Thank you... Cheerful regards, Mike
 
Hi Nigel. Happy Holidays.

Could you post the 12F1840 XC8 version, please?

Thank you... Cheerful regards, Mike

Isn't it the same as the pic16f1827.... You only need to change the output for port A.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top