This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

external interrupt problem

Hello,

This problem begins to drive me crazy :
With a 87LPC767, I try to transmit a square signal via TXD.
With a button, I choose the period of the signal.
The button is linked to the interrupt pin 8 of the µcontroller and to another pin.

I define a baudrate in the initialize_system( ) function but I need to change the value of TH1 when the button is pushed.

When the com_baudrate() function is not in the code (like in the example beneath), everything works fine but when I move the // and I run the code, it goes once till the end of the main() and then enter the External_1() function and stay inside (TXD stays high).

I thought it was maybe a bank problem but adding "using 2" to the functions called in the External_1() function changes nothing…

Sorry to post all this code but I think it's easier to understand and not as long… :)

Thanks for any help and sorry for my English.

Arnaud Stoumont

/*---------------------------------------------------------------------------------------
IMPULSEUR
  ---------------------------------------------------------------------------------------*/
#include <stdio.h>
#include <rg_impuls.h>    /* SPECIAL FUNCTION REGISTER 87LPC767 */
#include <stdlib.h>

unsigned char debug;
unsigned char impulseur;

//------------------------------------------------------------------------------
// Support Function Prototypes
//------------------------------------------------------------------------------
void initialize_system (void);				// Initializes MCU, except I2C
void delay_time (unsigned int time_end);    // To pause execution for pre-determined time
void TESTSW(void);
void com_baudrate (unsigned char baudrate);


void TESTSW(void) using 2
{
	unsigned char LECTURESWITCH;	/*PERIODE DE LECTURE DES SWITCHES*/
	unsigned char BUTSELECTION;		/*Pour considéré le switch on doit etre > 90*/

	BUTSELECTION = 0;			
	LECTURESWITCH = 0;			/*Variable de comptage pour le nombre de lectures des boutons*/

do								/*Lecture de l'état des deux boutons*/
{
	if (BSELECTION == 0)		/*Lecture bouton selection*/
 	{
 		BUTSELECTION++;				/*Bouton sélection lu actionné*/
	}

	LECTURESWITCH++;			/*Variable de comptage pour le nombre de lectures des boutons*/
}
while (LECTURESWITCH < 100 );	/*Test pour 100 lectures du bouton sélection*/


			
if (BUTSELECTION > 90 ) /*Le bouton sélection est activé*/
{
	impulseur++;
}


if ( impulseur > 2 )
{
	impulseur = 0;
}

return;
}	

void initialize_system (void)
{
PCON &= 0x7F;							// Clear bit 7 of the PCON register (SMOD1 = 0) pour pouvoir diviser Fcpu par 192 et pas 96 

SCON = 0x50;			// 0101,0000 (Mode 1 and RxD enable)	
TMOD = 0x20;			// Timer #1 in autoreload 8 bit mode

TH1 = 0x11;			
											// Timer #1 overflow rate
											// Baud Rate = (Fcpu / 192) / (256 - TH1) avec TH1=230 --> 1202 bits/s
											// Fcpu = 8.00 MHz (XTAL)
TL1=TH1;
ET1 = 0;			    /* disable timer 1 interrupt */
TR1 = 1;			// Turn on Timer 1
REN=0;
//mettre la priorité de l'interrupt externe plus haute que celle du timer
IP0=0x84;
IP0H=0x84;
return;
}

/*------------------------------------------------------------------------------
------------------------------------------------------------------------------*/
void com_baudrate (unsigned char baudrate) using 2
{
/*------------------------------------------------
Clear transmit interrupt and buffer.
------------------------------------------------*/
TI = 0;				    /* clear transmit interrupt */
/*------------------------------------------------
Set timer 1 up as a baud rate generator.
------------------------------------------------*/
TR1 = 0;			    /* stop timer 1 */
ET1 = 0;			    /* disable timer 1 interrupt */
TH1 = baudrate;//(unsigned char) (256 - ((XTAL) / ((32L/2) * 12L * baudrate)));
TL1=TH1;
TR1 = 1;			    /*

Parents
  • Thanks a lot, Frank, for all these informations!

    now I understand why it enters the External_1() function directly. I suppose it's the next assembly code after the place where the timer1 ISR is supposed to be.

    In fact, I've already tried to use the timer1 ISR to toggle TXD but I couldn't get the wanted baudrate (it was something like 10 times faster)so I used SBUF and it worked alright. But I didn't know I could use the T0 and T1 pins to do that!

    I think there is a problem inside the data sheets of the 87LPC767 with the baudrate of the differents modes.
    On page 36 :
    Mode 2 Baud Rate = (1+SMOD1)/32*CPU clock freq
    and just after, it's written that to use UART, we have to use the timer1 in mode2 (auto-reload mode)and then they say : "in that case the baud rate is given by the formula :
    Mode 1,3 baud rate =
    (CPU clock/192)/(256-TH1)"
    But auto-reload mode is mode 2!
    On the Timer Mode 2 figure, you can see that THn is reloaded after each overflow.

    I know English is not my mother language, maybe I missed something, but...

    Anyway, thanks again for the informations and don't worry for me, I'll have a lot of fun ;)

    Arnaud

Reply
  • Thanks a lot, Frank, for all these informations!

    now I understand why it enters the External_1() function directly. I suppose it's the next assembly code after the place where the timer1 ISR is supposed to be.

    In fact, I've already tried to use the timer1 ISR to toggle TXD but I couldn't get the wanted baudrate (it was something like 10 times faster)so I used SBUF and it worked alright. But I didn't know I could use the T0 and T1 pins to do that!

    I think there is a problem inside the data sheets of the 87LPC767 with the baudrate of the differents modes.
    On page 36 :
    Mode 2 Baud Rate = (1+SMOD1)/32*CPU clock freq
    and just after, it's written that to use UART, we have to use the timer1 in mode2 (auto-reload mode)and then they say : "in that case the baud rate is given by the formula :
    Mode 1,3 baud rate =
    (CPU clock/192)/(256-TH1)"
    But auto-reload mode is mode 2!
    On the Timer Mode 2 figure, you can see that THn is reloaded after each overflow.

    I know English is not my mother language, maybe I missed something, but...

    Anyway, thanks again for the informations and don't worry for me, I'll have a lot of fun ;)

    Arnaud

Children
No data