Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
09/06/02 08:33
Read: times


 
#28706 - Serial port dilemma....SOLVED!!!
Hello all!

The problem is with my microcontroller code or with the way I am using it.

What happens is that at startup, as soon as I enable the global interrupt, I get a serial interrupt (even with nothing attached to the serial port). When it reads from the SBUF register, it gets 0xFF.

Now, if I turn on the uC before starting my PC program, that 0xFF (transmitted by uC - if you remember, my code transmits what it reads from SBUF) goes waste. However, if I reset the uC, that 0xFF is received by the PC. This explains why I received 0xFF after resetting uC. And this also explains why what I receieved was what I transmitted the previous time.

How single step solved the problem...well, during a normal run (NOT single step), I read the port as soon as I write to it, so uC had not responded by then...result, I get previous data, however, during single step, the data in PC receive buffer is over written with the new data sent, AND viola!!!Everything comes back in sync!!

NOW THE PROBLEM
---------------

Why is uC getting an interrupt after startup ??? I am pasting below my main function, the serial ISR and a timer ISR that I use to toggle an LED at 2Hz so I can see its heart beat.


I havenot tried a delay yet (just a suspicion that the port pin was not charged and it got a stop bit somehow - very illogical).

Below is my code....any suggestions ???

-------------------------------------------
void main ()
{
// ** Initialize global variables ****
g_cTimerCount = 0;
g_cActivityCounter = 0;
g_cTemp4 = 0;
g_cTemp5 = 0;
// *** Initialize global variables ****

// ** Initialize serial port *******
REN = 1; // Enable receiver
TB8 = 1; // Set 9th transmit bit = 1
SM0 = 0; // Put Serial port in mode1 (8-bit UART variable freq)
SM1 = 1; // Put Serial port in mode1 (8-bit UART variable freq)
PCON = SMOD_; // Double baud rate
// ** Initialize serial port ***


// **** Initialize TIMER0 ***********
TMOD |= T0_M0_; // Set 16-bit timer mode
TH0 = TIMER_25MSEC_RELOAD_HIGH; // Load overflow value
TL0 = TIMER_25MSEC_RELOAD_LOW; // Load overflow value
// DON'T START TIMER YET
// ****** Initialize TIMER0 **********

// ******* Initialize TIMER1 *********
// This timer is used for baud rate generation
TMOD |= T1_M1_; // Set 8-bit timer mode
TH1 = TIMER1_57600_BAUD_RELOAD_VALUE; // Auto reload value
TL1 = TIMER1_57600_BAUD_RELOAD_VALUE; // Reload value
TR1 = 1; // Start timer 0
// **** Initialize TIMER1 ******

TR0 = 1; // Start timer0

// * Enable interrupts & set priority ****
PS = 1; // Assign high priority level to SERIAL PORT interrupt
ET0 = 1; // Enable timer0 interrupt
RI = 0; // Clear RI, so any false trigger during plugging can be cleared
TI = 0; // Clear TI, so any false trigger during plugging can be cleared
ES = 1; // Enable serial port interrupt
EA = 1; // Enable global interrupt
// ***Enable interrupts & set priority *****

P0 = 0xFF; // Init port for input
P1 = 0xFF; // Init port for input
P2 = 0xFF; // Init port for input
P3 = 0xFF; // Init port for input

// loop here forever
while (1)
{
}
}



------------------------------------------


void serial () interrupt SIO_VECTOR
{
RI = 0; // Clear RI

g_cTemp4 = SBUF; // move data to local variable

g_cTemp5 = g_cTemp4; // copy to this variable

P2 = ~g_cTemp5; // complement is writen, so I can see it on LEDs, which are connected cathode to port pin.

SBUF = g_cTemp5; // Send data back

LED_PORT_1 ^= 1; // Toggle LED. Its value is 1'b1 on startup

while (TI != 1)
{
}

TI = 0; // Clear TI
}



-------------------------------------------


void timer0 () interrupt TF0_VECTOR
{
// ********************** RELOAD TIMER0 **********************
TR0 = 0; // Stop timer0
TH0 = TIMER_25MSEC_RELOAD_HIGH; // Reload overflow value
TL0 = TIMER_25MSEC_RELOAD_LOW; // Reload overflow value
TR0 = 1; //Start timer0
// ********************** RELOAD TIMER0 **********************

g_cTimerCount++;

if (g_cTimerCount == (unsigned char)TIMER_25MSEC_LOOP_LIMIT) // 100msec gone??
{
g_cTimerCount = 0; // Reset g_cTimerCount

g_cActivityCounter++; // Increment counter

// 500 msec over? OR in other words, 0.5 seconds gone ??
// *************** The Green OK LED will toggle at 2Hz ****************
if (g_cActivityCounter == (unsigned char)GREEN_OK_LED_TOGGLE_TIME)
{
g_cActivityCounter = 0; // Reset g_cActivityCounter
GREEN_OK_LED_PORT ^= 1; // Toggle GREEN OK LED
}
// *************** The Green OK LED will toggle at 2Hz ****************

} // if (g_cTimerCount == TIMER_25MSEC_LOOP_LIMIT) // 100msec gone??

if (DTR_PORT == 1)
{
LED_PORT_2 = 0; // turn on LED
DSR_PORT = 1; // Assert DSR (signalling, link IS established)
}
else
{
LED_PORT_2 = 1; // turn off LED
DSR_PORT = 0; // De-Assert DSR (signalling, link NOT established)
}
}


-------------------------------------------

Regards
Aman


List of 5 messages in thread
TopicAuthorDate
Serial port dilemma....SOLVED!!!            01/01/70 00:00      
RE: Serial port dilemma....SOLVED!!!            01/01/70 00:00      
RE: Serial port dilemma....SOLVED!!!            01/01/70 00:00      
RE: Serial port dilemma....SOLVED!!!            01/01/70 00:00      
RE: Serial port dilemma....SOLVED!!!            01/01/70 00:00      

Back to Subject List