View previous topic :: View next topic |
Author |
Message |
bonbino
Joined: 29 Jun 2008 Posts: 5
|
problem with chronometer |
Posted: Tue Jul 01, 2008 7:00 am |
|
|
I want to trigger the counter of a timer to a PIC by another, by a RS232.
I declared a global variable (Chrono), used by the interruption of the timer to see if we increment the counter or not, and if we get the data 70 in the serial link, the variable Chrono changes state to start counting.
Only this method does not work and the clock does not work.
Has my opinion there is a competition between the two interruption to access a variable and carrying out the interruption timer is very quickly it prevents the interruption series to change the state of timing.
Then you have a solution to this problem.
Sender’s program
Code: | void main()
{
port_b_pullups(TRUE);
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_spi(SPI_SS_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_64);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
enable_interrupts(INT_RDA);
enable_interrupts(GLOBAL);
while (true)
{
If (input(PIN_B1))//activate chronometer
{
Printf(“70”);
}
}
} |
Receiver program
Code: | //variables
int1 Chrono; //chono en marche ou non
int1 ChChrono,C
#int_RDA
void RDA_isr(void)
{
car=getchar();
switch (car)
{
case 70: //declenché le chrono
Chrono=1;
//enable_interrupts(INT_TIMER0); // I can’t use enable interrupt in this function
break;
}
}
#int_TIMER0
void TIMER0_isr(void)
{
if (Chrono)
{
set_timer0(4);
msec++;
if (msec==10)
{
msec=0;
sec++;
if (sec==60)
{
sec=0;
min++;
if (min==60)
{
min=0;
heure++;
}
}
ChChrono=1;//value of chono have change
}
}
}
void main()
{
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_spi(SPI_SS_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_64);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
enable_interrupts(INT_RDA);
disable_interrupts(INT_TIMER0); //I disable the interrupt here
enable_interrupts(GLOBAL);
while (TRUE)
{
if (ChChrono)//if the value of chronometer chage then display
{
display();//function of display
ChChrono=0;
}
}
} |
this is a part of the program
thank you for your help. |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Tue Jul 01, 2008 7:44 am |
|
|
This " Printf(“70”); " is NOT sending the value 70 it is sending 2 chars '7' and '0'
Try
putc(70); or
putc(0x46); or
printf("%c", 70); or
putc('\70'); // Note the single quotes or
putc('\x46'); |
|
|
bonbino
Joined: 29 Jun 2008 Posts: 5
|
|
Posted: Fri Jul 04, 2008 3:33 am |
|
|
the problem is not in printf ( "70"), actually I do:
...
...
int CarSend;
...
...
CarSend=70;
printf("%c",CarSend);
With the simulation I found that it very well received. |
|
|
RLScott
Joined: 10 Jul 2007 Posts: 465
|
|
Posted: Fri Jul 04, 2008 4:57 am |
|
|
bonbino wrote: | the problem is not in printf ( "70"), actually I do:
...
...
int CarSend;
...
...
CarSend=70;
printf("%c",CarSend);
|
If that is what you do, then why does the code you posted say:
Printf(“70”);
?
Are there any other areas of your code where it is not really as you posted it? _________________ Robert Scott
Real-Time Specialties
Embedded Systems Consulting |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Fri Jul 04, 2008 7:55 am |
|
|
A minor bug: Code: | setup_spi(SPI_SS_DISABLED); | This is an invalid configuration, the define SPI_SS_DISABLED should only be used in combination with SPI_SLAVE. The command to disable SPI is:
Code: | //enable_interrupts(INT_TIMER0); // I can’t use enable interrupt in this function | Why did you disable this line? As far as I can see it is allowed to use this construction.
Without this line the timer interrupt is never enabled and that's why your code is not working. |
|
|
bonbino
Joined: 29 Jun 2008 Posts: 5
|
|
Posted: Mon Jul 07, 2008 6:16 am |
|
|
Quote: | If that is what you do, then why does the code you posted say:
Printf(“70”);
?
Are there any other areas of your code where it is not really as you posted it? |
I tried to minimize the code maximum for it is understandable (because it is very long) and I have not paid attention to this error.
Quote: | setup_spi(FALSE);
//enable_interrupts(INT_TIMER0); // I can’t use enable interrupt in this function
Why did you disable this line? As far as I can see it is allowed to use this construction.
Without this line the timer interrupt is never enabled and that's why your code is not working.
|
I left //enable_interrupts(INT_TIMER0); to show you that I used a variable verification and even //enable_interrupts(INT_TIMER0); and it did not work (I think that we can not activate a intrruput from another).
With the instruction setup_spi(FALSE); it changes nothing. The problem when I do disable_interrupts(INT_TIMER0); in the part of initializing the clock works very well. |
|
|
RLScott
Joined: 10 Jul 2007 Posts: 465
|
|
Posted: Mon Jul 07, 2008 7:59 am |
|
|
You have two things that must happen to make the clock run. One is that the RDA_isr needs to receive a 70 so that it will set Chrono=1. The second thing is that TIMER0_isr needs to run and see that Chrono is set so it will start counting. Since both of these things are in doubt, it seems you should separate the two problems and attack them one at a time. First see if RDA_isr is recognizing the 70. Change your RDA_isr so that it sets some output pin that you can monitor with a scope or a meter. That will check out the RDA_isr independent of any timer interrupts. Then, to check out the timer interrupts, set Chrono=1 in your main program and see if the clock counts. That will check out the TIMER0_isr without involving any serial input. Divide and conquer. _________________ Robert Scott
Real-Time Specialties
Embedded Systems Consulting |
|
|
Ttelmah Guest
|
|
Posted: Mon Jul 07, 2008 10:21 am |
|
|
Also, note the big error in the 'sender'. INT_RDA is enabled, but no handler is shown. You must _never_ enable an interrupt without a handler. Sure way to hang the processor.
Best Wishes |
|
|
bonbino
Joined: 29 Jun 2008 Posts: 5
|
|
Posted: Wed Jul 09, 2008 9:38 am |
|
|
I have this idea before, so I put Chrono=1 the clock works very well, and I wanted to know if I receive the data'70' on the other side with another port, and it gave a good result. Maybe you have another solution, different than mine.
Quote: | Also, note the big error in the 'sender'. INT_RDA is enabled, but no handler is shown. You must _never_ enable an interrupt without a handler. Sure way to hang the processor.
|
I have not understood your advice, please clarify. |
|
|
bonbino
Joined: 29 Jun 2008 Posts: 5
|
|
Posted: Sat Jul 12, 2008 4:36 pm |
|
|
Ttelmah wrote: | Also, note the big error in the 'sender'. INT_RDA is enabled, but no handler is shown. You must _never_ enable an interrupt without a handler. Sure way to hang the processor.
Best Wishes |
can you give me an example that i can hang the processor.
thank you. |
|
|
|