CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

bkbhit is true but nothing connected to the serial port!?!?!

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
LordFrith



Joined: 05 Jul 2006
Posts: 3

View user's profile Send private message

bkbhit is true but nothing connected to the serial port!?!?!
PostPosted: Mon Sep 18, 2006 5:57 am     Reply with quote

Hi

Scenario:
PIC16F73
1 x HW UART
1 x SW UART

Using sample code for int_rda.

Problem:
When I run the code, bkbhit returns true but garbage in the buffer and nothing is connected to the serial port?!?!?

Code:

#include    <16F73.h>
#include    "stdio.h"

#pragma type short=8,int=16,long=32

//*******************************
//*****                     *****
//*****  GLOBAL VARAIABLES  *****
//*****                     *****
//*******************************

#fuses HS,NOWDT,NOPROTECT,NOBROWNOUT,NOPUT

#define EEPROM_SDA      PIN_C4
#define EEPROM_SCL      PIN_C3

#use delay(clock=16000000)

#use i2c(master,sda=EEPROM_SDA, scl=EEPROM_SCL)
#use rs232(baud=4800, xmit=PIN_C6, rcv=PIN_C7, STREAM=MDM, ERRORS)
#use rs232(baud=4800, xmit=PIN_A3, rcv=PIN_A2, STREAM=DBG)


// PIC registers
#byte PORTA   = 0x05
#byte PORTB   = 0x06
#byte PORTC   = 0x07
#byte ADRES   = 0x1e
#byte ADCON0  = 0x1f
#byte ADCON1  = 0x9f
#byte OPTION  = 0x81
#byte INTCON  = 0x0b
#byte TMR0    = 0x01

#bit EDGE_BIT = OPTION.6
#bit E_B      = intcon.4
#bit C_B      = intcon.1



#define BUFFER_SIZE 16
BYTE buffer[BUFFER_SIZE];
BYTE next_in = 0;
BYTE next_out = 0;

#define bkbhit (next_in!=next_out)

BYTE bgetc() {
   BYTE c;

   while(!bkbhit) ;
   c=buffer[next_out];
   next_out=(next_out+1) % BUFFER_SIZE;
   return(c);
}


#int_rda
void read_serial()
{
   int t;

   buffer[next_in]=fgetc(MDM);
   t=next_in;
   next_in=(next_in+1) % BUFFER_SIZE;
   if(next_in==next_out)
     next_in=t;           // Buffer full !!
}

<!--- SNIP --->

    fprintf(DBG, "(Turning on modem power)\r\n" );
    DIGOUT_MOD_PWR = ON;

   enable_interrupts( GLOBAL );
   enable_interrupts( INT_RDA );
   
     delay_ms( 10000 );
   

    while(!bkbhit)
    {
   delay_us(10);
    }
    fprintf( DBG, "Data ready!\r\n");
    while(bkbhit)
   {
      fprintf( DBG, "%c", bgetc());
      
   }

// There's a TX bit here

if( boGotFirstModemRespChar( 10, PARAM_RESPONSE ) == FALSE )
    {   
        // but we didn't get one
        fprintf(DBG, "(Param Response Timed out)\r\n" );
           
        boReportSendSuccessful = FALSE;
        ucHoursToSleep       = 1;
        goto SendReportErrorExit;
    }
<!--- SNIP --->

BYTE     boGotFirstModemRespChar( unsigned long ulTimeoutSecs, unsigned char ucExpectedPacketType )
{
    unsigned long   ulModemRespTimeout10Us;
    BYTE         boGotFirstChar;
    BYTE            boWaitingForPcktHdr;
    unsigned char      ucChar;
    unsigned long      ulTimeout;
   
    boWaitingForPcktHdr = true;
    boGotFirstChar = FALSE;
   
    ulTimeout = ulTimeoutSecs;
      fprintf( DBG, "(Waiting for response from modem)\r\n" );
      while ( boWaitingForPcktHdr == true )
      {
         
         ulModemRespTimeout10Us = 0;
         
       while( !bkbhit && (ulModemRespTimeout10Us < 200000 ))
       {
           delay_us( 10 );
           ulModemRespTimeout10Us++;
       }
      
       if ( bkbhit )
       {
          //fprintf( DBG, "(Something on the rcv buffer)\r\n(" );
         
          ucChar = bgetc();
          fprintf( DBG, "%X", ucChar );
          if( ucChar == PKT_HEADER )
          {
             if( bgetc() == ucExpectedPacketType )
             {
                boGotFirstChar = true;
                boWaitingForPcktHdr = False;
             }

          }
       }
       
       ulTimeout--;
       
       if(ulTimeout == 0)
       {
          boGotFirstChar = FALSE;
          boWaitingForPcktHdr = false;
       }
   
     }
  //   fprintf( DBG, ")\r\n" );
    return boGotFirstChar;
}




Output:

(Turning on modem power)
Data ready!
� <--- where does this come from
TX: 86 12 08 00 00 01 00 9F
(Waiting for response from modem)
C30062180000C080FE <--- where does this come from
(Param Response Timed out)

However...there's nothing connected to the serial port.


Any thoughts?
Ttelmah
Guest







PostPosted: Mon Sep 18, 2006 7:19 am     Reply with quote

'Nothing connected to the serial port', implies the line can float. The '0' at the beginning, implies the line is low here (remember that for serial, the 'idle' state is a high signal). Then the line is floating upwards, so you see garbage.

Best Wishes
LordFrith



Joined: 05 Jul 2006
Posts: 3

View user's profile Send private message

PostPosted: Mon Sep 18, 2006 7:37 am     Reply with quote

Ttelmah wrote:
'Nothing connected to the serial port', implies the line can float. The '0' at the beginning, implies the line is low here (remember that for serial, the 'idle' state is a high signal). Then the line is floating upwards, so you see garbage.

Best Wishes


Apologies...the serial port is connected to a hyperterminal session...no data is traversing the line.
Ttelmah
Guest







PostPosted: Mon Sep 18, 2006 7:54 am     Reply with quote

In which case, the zero at the beginning is still normal. (this is because the charge pump in chips like the MAX232, takes a while to get working, and the chip doesn't work properly till this happens. The normal solution is to wait for perhaps 20mSec, then clear the UART, and enable the interrupts. However the 'garbage', is not. How are the units connected?. Good 0v connection?.

Best Wishes
LordFrith



Joined: 05 Jul 2006
Posts: 3

View user's profile Send private message

PostPosted: Mon Sep 18, 2006 8:00 am     Reply with quote

Ttelmah wrote:
In which case, the zero at the beginning is still normal. (this is because the charge pump in chips like the MAX232, takes a while to get working, and the chip doesn't work properly till this happens. The normal solution is to wait for perhaps 20mSec, then clear the UART, and enable the interrupts. However the 'garbage', is not. How are the units connected?. Good 0v connection?.

Best Wishes



Discovered the problem...had used set_tris_c incorrectly!!!

I've been using set_tris_c(0xFF) to save power but was turning on the outputs with set_tris_c(0x5B) instead of 0x9B

I feel very foolish!
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group