Discussion in "PIC Microcontroller Discussion" started by    mircea2012    Jan 2, 2013.
Wed Jan 02 2013, 02:03 pm
#1
Hy everybody.

I want to make a programm who accomplish the next tasks : generate a
delay of 3 seconds.
The RD7 pin must stay ON 3 seconds, then must stay OFF 3 seonds, and so
on.

freq. out = freq. clk / (Prescaler*(256 - TMR0)*count)
In my program I work with internal clk (T0CS=0) so the freq. clk is
4Mhz/4 = 1Mhz. ( Period = 1 / freq; internal instruction cycle is 1
microsec).

freq. out = 1 / T = 1 / 3 sec = 0.33Hz and replacing 0.33Hz = 1MHz / (256 * (256 - 0) * count)
count = 1MHz / 65536 * 0.33Hz
count = 46.

#include<pic.h>
unsigned char counter;
void interrupt Timer0_ISR(void)
{
if ( T0IE && T0IF ) // are TMR0 interrupts enabled and//is the TMR0 interrupt flag set?
{
T0IF = 0; // TMR0 interrupt flag must be cleared in software
++counter; // increment the counter variable by 1
if(counter == 46)
PORTDbits.RD7 ^= 1; // toggle the RD7 pin
}
}
Init(void)
{
TMR0 = 0; // clear the TMR0 register
OPTION_REG = 0B00000111; // Use the internal instruction clock, choosing to work with a Prescaler (1 : 256)
INTCONbits.T0IE = 1; // enable TMR0 overflow
INTCONbits.GIE = 1; // enable Global
interrupts
}
main(void)
{
TRISD = 0B01111111;
PORTDbits.RD7 = 0;
Init();
while(1)
{
}
}

I simulate this program with MPLAB SIM but timer0 dont want to
increment.
Please help me.
I dont know if my computations is good.
All the best.
Thu Jan 03 2013, 12:18 am
#2
Your calculations look okay to me.
I would remove this line, I'm not sure it is safe,and you don't need it.


if ( T0IE && T0IF ) // are TMR0 interrupts enabled and//is the TMR0 interrupt flag set?

mircea2012


This is the Timer0 interrupt handler, if there was no interrupt you would not be here.

I suggest a couple of changes.
             if(counter == 46)
             {
              counter =0;    //reset for next timer
             PORTDbits.RD7 ^= PORTDbits.RD7              // toggle the RD7 pin
            }


Which compiler are you using ?


[ Edited Thu Jan 03 2013, 12:19 am ]
Thu Jan 03 2013, 12:58 am
#3
I made the modification who you suggest me, but still no works.
I use the MPLAB IDE, and compiler is HI TECH ANSI C Compiler.
I am begginer in microcontroller programming and believe me I struggled with this program a couple of days.
I dont know what I supposed to do to work this program.
I have a board from microchip PICDEM Mechatronics.
Thank you for your interest.
Thu Jan 03 2013, 02:56 am
#4
Set the PEIE bit of the INTCON register.
Also make sure the Watchdog function is turned off.

Do you get any compilation errors ?

If it still does not work, post your updated code.



[ Edited Thu Jan 03 2013, 02:57 am ]
Thu Jan 03 2013, 11:51 am
#5
@ mircea2012
is it your complete code
are you configuring the pic in the burning software
in your code there is no configuration words
Thu Jan 03 2013, 10:06 pm
#6
@majoka yes, you are right in my code is no configuration words and yes this is my complete code.
Look at my "Configuration bits" in attachment.


Thu Jan 03 2013, 11:59 pm
#7
From the config bits you are using an internal oscillator.
It may be running but very slowly.
Set the low 3 bits of the OSCCON register to 6.

My suggestion of
PORTDbits.RD7 ^= PORTDbits.RD7
may not be a good idea, try your original as well.

You are using a simulator.
Is the program running at all ?
Is the interrupt being called ?


I don't have Hi Tech but I do have one of their examples.
It is for an 16f84, but should be a good starting point.

First see if it runs on your simulator.
Then see if you can adapt it for your chip.
Then change the example to your code in stages, see what happens.

I wish I had a working copy of Hi-Tech though !
Mon Jan 21 2013, 06:07 am
#8
I guess mircea2012 wanted to reply here.. mistakenly created new thread out there...

@ExperimenterUK first of all in my opinion i dont think is necessary to set the PEIE bit (see my attachment), but i tried like you said and not works.
I dont get any compilation errors.
Look at my code :

#includeunsigned int counter = 0;

void Interrupt_ISR(void)
{
if ( T0IE && T0IF )
{
T0IF = 0;
counter++;
if(counter == 46)
{
counter = 0;
PORTDbits.RD7 ^= PORTDbits.RD7;
}
}
}

Init(void)
{
TMR0 = 0;
OPTION_REG = 0B00000111;
INTCONbits.T0IE = 1;
INTCONbits.GIE = 1;
interrupts
}

main(void)
{
TRISD = 0B01111111;
PORTDbits.RD7 = 1;
while(1)
{

}
}



mircea2012

Fri Mar 08 2013, 01:44 pm
#9
looks like you always assign same value to pin RD7??
PORTDbits.RD7 ^= 1; // toggle the RD7 pin
Fri Mar 08 2013, 05:58 pm
#10
he is doing xor operation, which toggles your pin.

Get Social

Information

Powered by e107 Forum System

Downloads

Comments

Timothypet
Wed May 15 2024, 06:14 pm
RandyBence
Wed May 15 2024, 02:00 pm
JordanDic
Wed May 15 2024, 01:55 pm
DavidDeelf
Wed May 15 2024, 11:16 am
ytaletjkca
Wed May 15 2024, 09:45 am
MildredWoumb
Wed May 15 2024, 04:07 am
NaKrutkADamb
Wed May 15 2024, 12:03 am
MichaelGot
Tue May 14 2024, 04:08 pm