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 support@ccsinfo.com

PIC16F687: problem with kbhit and getc

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



Joined: 21 Mar 2007
Posts: 24

View user's profile Send private message

PIC16F687: problem with kbhit and getc
PostPosted: Wed May 23, 2007 6:34 pm     Reply with quote

problem with kbhit and getc

hi,i got a problem with kbhit and getc, here is my code

Code:

#use rs232(baud=9600,parity=N,xmit=PIN_B7,rcv=PIN_B5,stream=GPS)
....

in main....
   while(1)
   {      
      if(kbhit(GPS))
         {
         temp_char = getc(GPS);
         printf("temp_char = %c", temp_char);
         }
      }
  }


what happens is that kbhit never detects any RS232 data even though there is data sent from my com port. No printf message comes up. i confirmed this with my O scope. is there something wrong with this algorithm?

i am using PIC16F687
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed May 23, 2007 7:16 pm     Reply with quote

Post your compiler version. You can find it at the top of the .LST file.
Look for that file in your project directory. It's a 4-digit number like
3.249, 4.013, 4.038, etc.
hanhao



Joined: 21 Mar 2007
Posts: 24

View user's profile Send private message

PostPosted: Wed May 23, 2007 7:51 pm     Reply with quote

PCM programmer wrote:
Post your compiler version. You can find it at the top of the .LST file.
Look for that file in your project directory. It's a 4-digit number like
3.249, 4.013, 4.038, etc.


4.013
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed May 23, 2007 9:54 pm     Reply with quote

I don't have that version. The closest I have is vs. 4.014.
Can you compile the following program, and post the first part of
the .LST file ?
I need to see the LST file code that comes just after the start of main().
Code:

#include <16F687.h>
#fuses XT,NOWDT,NOPROTECT,BROWNOUT,PUT 
#use delay(clock=4000000)
#use rs232(baud=9600,parity=N,xmit=PIN_B7,rcv=PIN_B5,stream=GPS)

//============================
void main()
{
int8 temp_char;

   while(1)
   {       
      if(kbhit(GPS))
         {
         temp_char = getc(GPS);
         printf("temp_char = %c", temp_char);
         }
       
  }

}


Here's the part of the .LST file that starts with main(). This code is
for vs. 4.038. Post the LST file code that you get after compiling
with vs. 4.013. This will show how the PIC's registers are setup.
I think there may be a problem with the setup of the UART registers
in your version.
Code:

.................... void main() 
.................... { 
0017:  CLRF   04
0018:  MOVLW  1F
0019:  ANDWF  03,F
001A:  BSF    03.5
001B:  BCF    1B.3
001C:  MOVLW  19
001D:  MOVWF  19
001E:  MOVLW  A6
001F:  MOVWF  18
0020:  MOVLW  90
0021:  BCF    03.5
0022:  MOVWF  18
0023:  BSF    03.6
0024:  BCF    1F.0
0025:  BCF    1F.1
0026:  BCF    1F.2
0027:  BCF    1F.3
0028:  BCF    03.6
0029:  BCF    1F.6
002A:  MOVLW  00
002B:  BSF    03.6
002C:  MOVWF  1E
002D:  CLRF   19
002E:  CLRF   1A
002F:  BSF    03.5
0030:  CLRF   1E
0031:  BCF    03.5
0032:  CLRF   1B
0033:  BCF    03.6
0034:  BCF    0D.5
hanhao



Joined: 21 Mar 2007
Posts: 24

View user's profile Send private message

PostPosted: Thu May 24, 2007 1:42 am     Reply with quote

sry i am not good in assembly
hope you can find something from here

Code:
void main()
.................... {
*
0017:  CLRF   04
0018:  BCF    03.7
0019:  MOVLW  1F
001A:  ANDWF  03,F
001B:  BSF    03.5
001C:  BCF    1B.3
001D:  MOVLW  0C
001E:  MOVWF  19
001F:  MOVLW  A2
0020:  MOVWF  18
0021:  MOVLW  90
0022:  BCF    03.5
0023:  MOVWF  18
0024:  BSF    03.6
0025:  BCF    1F.0
0026:  BCF    1F.1
0027:  BCF    1F.2
0028:  BCF    1F.3
0029:  BCF    03.6
002A:  BCF    1F.6
002B:  MOVLW  00
002C:  BSF    03.6
002D:  MOVWF  1E
002E:  MOVLW  07
002F:  MOVWF  19
0030:  MOVLW  FF
0031:  MOVWF  1A
0032:  BSF    03.5
0033:  CLRF   1E
0034:  BCF    03.5
0035:  CLRF   1B
0036:  BCF    03.6
0037:  BCF    0D.5
....................    setup_adc_ports(PIN_17_ADC|VSS_VDD);
*
003D:  BSF    03.6
003E:  BCF    1F.0
003F:  BCF    1F.1
0040:  BCF    1F.2
0041:  BCF    1F.3
0042:  BCF    03.6
0043:  BCF    1F.6
0044:  MOVLW  00
0045:  IORLW  02
0046:  BSF    03.6
0047:  MOVWF  1E
....................    setup_adc(ADC_CLOCK_INTERNAL);
0048:  BSF    03.5
0049:  BCF    03.6
004A:  BSF    1F.4
004B:  BSF    1F.5
004C:  BCF    1F.6
004D:  BCF    03.5
004E:  BSF    1F.7
004F:  BSF    1F.0
....................    setup_spi(FALSE);
0050:  BCF    14.5
0051:  BSF    03.5
0052:  BCF    07.7
0053:  BSF    06.4
0054:  BCF    06.6
0055:  MOVLW  00
0056:  BCF    03.5
0057:  MOVWF  14
0058:  BSF    03.5
0059:  MOVWF  14
....................  //  setup_timer_0(RTCC_EXT_L_TO_H|RTCC_DIV_1);
....................   // setup_timer_1(T1_DISABLED);
....................      //setup_oscillator(False);
.................... 
.................... setup_oscillator(OSC_8MHZ);     
005A:  MOVLW  71
005B:  MOVWF  0F
.................... 
.................... 
....................    output_float(PIN_17_PIN);
005C:  BSF    05.2
.................... 
....................    while(1)
....................    {
....................       while ( !input(PIN_C5) )
....................       {
005D:  BSF    07.5
005E:  BCF    03.5
005F:  BTFSC  07.5
0060:  GOTO   07B
....................       if(kbhit(GPS))
0061:  BTFSS  0C.5
0062:  GOTO   078
....................          {
....................          temp_char = getc(GPS);
0063:  BTFSS  0C.5
0064:  GOTO   063
0065:  MOVF   1A,W
0066:  MOVWF  2D
....................          printf("temp_char = %c", temp_char);
0067:  CLRF   2E
0068:  MOVF   2E,W
0069:  CALL   004
006A:  INCF   2E,F
006B:  MOVWF  20
006C:  MOVF   20,W
006D:  BTFSS  0C.4
006E:  GOTO   06D
006F:  MOVWF  19
0070:  MOVLW  0C
0071:  SUBWF  2E,W
0072:  BTFSS  03.2
0073:  GOTO   068
0074:  MOVF   2D,W
0075:  BTFSS  0C.4
0076:  GOTO   075
0077:  MOVWF  19
....................          }
.................... 
.................... //      main_sequence();
....................       }
0078:  BSF    03.5
0079:  GOTO   05D
007A:  BCF    03.5
....................    }
007B:  BSF    03.5
007C:  GOTO   05D
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu May 24, 2007 1:46 am     Reply with quote

Please compile the program that I posted.
hanhao



Joined: 21 Mar 2007
Posts: 24

View user's profile Send private message

PostPosted: Thu May 24, 2007 9:55 pm     Reply with quote

PCM programmer wrote:
Please compile the program that I posted.

Code:

CCS PCM C Compiler, Version 4.013, 28193               25-May-07 11:47

               Filename: kbit.lst

               ROM used: 81 words (4%)
                         Largest free fragment is 1967
               RAM used: 8 (6%) at main() level
                         8 (6%) worst case
               Stack:    1 locations

*
0000:  MOVLW  00
0001:  MOVWF  0A
0002:  GOTO   017
0003:  NOP
.................... #include <16F687.h>
.................... //////// Standard Header file for the PIC16F687 device ////////////////
.................... #device PIC16F687
.................... #list
.................... 
.................... #fuses XT,NOWDT,NOPROTECT,BROWNOUT,PUT 
.................... #use delay(clock=4000000)
.................... #use rs232(baud=9600,parity=N,xmit=PIN_B7,rcv=PIN_B5,stream=GPS)
.................... 
.................... //============================
.................... void main()
.................... {
0017:  CLRF   04
0018:  BCF    03.7
0019:  MOVLW  1F
001A:  ANDWF  03,F
001B:  BSF    03.5
001C:  BCF    1B.3
001D:  MOVLW  19
001E:  MOVWF  19
001F:  MOVLW  A6
0020:  MOVWF  18
0021:  MOVLW  90
0022:  BCF    03.5
0023:  MOVWF  18
0024:  BSF    03.6
0025:  BCF    1F.0
0026:  BCF    1F.1
0027:  BCF    1F.2
0028:  BCF    1F.3
0029:  BCF    03.6
002A:  BCF    1F.6
002B:  MOVLW  00
002C:  BSF    03.6
002D:  MOVWF  1E
002E:  MOVLW  07
002F:  MOVWF  19
0030:  MOVLW  FF
0031:  MOVWF  1A
0032:  BSF    03.5
0033:  CLRF   1E
0034:  BCF    03.5
0035:  CLRF   1B
0036:  BCF    03.6
0037:  BCF    0D.5
.................... int8 temp_char;
.................... 
....................    while(1)
....................    {       
....................       if(kbhit(GPS))
0038:  BTFSS  0C.5
0039:  GOTO   04F
....................          {
....................          temp_char = getc(GPS);
003A:  BTFSS  0C.5
003B:  GOTO   03A
003C:  MOVF   1A,W
003D:  MOVWF  26
....................          printf("temp_char = %c", temp_char);
003E:  CLRF   27
003F:  MOVF   27,W
0040:  CALL   004
0041:  INCF   27,F
0042:  MOVWF  20
0043:  MOVF   20,W
0044:  BTFSS  0C.4
0045:  GOTO   044
0046:  MOVWF  19
0047:  MOVLW  0C
0048:  SUBWF  27,W
0049:  BTFSS  03.2
004A:  GOTO   03F
004B:  MOVF   26,W
004C:  BTFSS  0C.4
004D:  GOTO   04C
004E:  MOVWF  19
....................          }
....................         
....................   }
004F:  GOTO   038
.................... 
.................... }
0050:  SLEEP

Configuration Fuses:
   Word  1: 3FE1   XT NOPROTECT BROWNOUT MCLR NOCPD NOWDT PUT IESO FCMEN
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu May 24, 2007 11:14 pm     Reply with quote

I looked at the startup code for your version, and added the comments
shown below. There are two bugs in it, regarding the setup of the
comparator registers. You can fix the bugs by adding the lines shown in
bold below. (In vs. 4.038, they have fixed these bugs).
Quote:

#include <16F687.h>
#fuses XT,NOWDT,NOPROTECT,BROWNOUT,PUT
#use delay(clock=4000000)
#use rs232(baud=9600,parity=N,xmit=PIN_B7,rcv=PIN_B5,stream=GPS)

#byte CM1CON0 = 0x119
#byte CM2CON0 = 0x11A

//============================
void main()
{
int8 temp_char;

CM1CON0 = 0x00;
CM2CON0 = 0x00;

while(1)
{
if(kbhit(GPS))
{
temp_char = getc(GPS);
printf("temp_char = %c", temp_char);
}

}

}


Here's the .LST file with comments added.
Code:

.................... void main()
.................... {
0017: CLRF 04
0018: BCF 03.7
0019: MOVLW 1F
001A: ANDWF 03,F

001B: BSF 03.5
001C: BCF 1B.3  // BRG16 = 0

001D: MOVLW 19
001E: MOVWF 19  // SPBRG = 0x19

001F: MOVLW A6
0020: MOVWF 18  // TXSTA = 0xA6

0021: MOVLW 90
0022: BCF 03.5
0023: MOVWF 18  // RCSTA = 0x90

0024: BSF 03.6
0025: BCF 1F.0  // ANSELH = 0  (all digital i/o)
0026: BCF 1F.1
0027: BCF 1F.2
0028: BCF 1F.3

0029: BCF 03.6
002A: BCF 1F.6  // VCFG = 0

002B: MOVLW 00
002C: BSF 03.6
002D: MOVWF 1E  // ANSEL = 0x00 (all digital i/o)

002E: MOVLW 07
002F: MOVWF 19  // CM1CON0 = 0x07 -- BUG: should be 0x00

0030: MOVLW FF
0031: MOVWF 1A  // CM2CON1 = 0xFF -- BUG: should be 0x00

0032: BSF 03.5
0033: CLRF 1E   // SRCON = 0x00

0034: BCF 03.5
0035: CLRF 1B   // CM2CON1 = 0x00

0036: BCF 03.6
0037: BCF 0D.5  // C1IF = 0
hanhao



Joined: 21 Mar 2007
Posts: 24

View user's profile Send private message

PostPosted: Mon May 28, 2007 2:22 am     Reply with quote

hi
thanks for your solution
but unfortunately it does not work
kbhit(GPS) never returns true and its sampling rate is higher than rs232 baud rate

a work around used is
Code:
      if(!input(PIN_B6))
         {
temp_char = getc(GPS);
         printf("temp_char = %c", temp_char);
         
         }

but unfortunately it's a "cheater" method that i wont want to use

another mistake i made earlier on is[b]
#use rs232(baud=9600,parity=N,xmit=PIN_B7,rcv=PIN_B5,stream=GPS)

i changed to
#use rs232(baud=9600,parity=N,xmit=PIN_B7,rcv=PIN_B6,stream=GPS)
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon May 28, 2007 2:49 am     Reply with quote

It wasn't really a solution. It was just one thing that I noticed was wrong
with the generated code.

If you use pin B6 as the 'rcv' pin with the 16F687, the compiler will
generate code for a software UART. The 'xmit' side will also be created
as a software UART.

You should change your hardware connections so the serial port uses
the hardware UART pins (B7 and B5 on the 16F687) and also add the
ERRORS parameter. Example:
Code:

#use rs232(baud=9600,xmit=PIN_B7,rcv=PIN_B5,stream=GPS, ERRORS)
hanhao



Joined: 21 Mar 2007
Posts: 24

View user's profile Send private message

PostPosted: Tue May 29, 2007 2:14 am     Reply with quote

yes that solved the problem
i shorted the pin to pin 12 instead of 11
no bug fixing was done
thanks
Douglas Kennedy



Joined: 07 Sep 2003
Posts: 755
Location: Florida

View user's profile Send private message AIM Address

PostPosted: Tue May 29, 2007 2:49 am     Reply with quote

Code:
if(!input(PIN_B6))
         {
temp_char = getc(GPS);
         printf("temp_char = %c", temp_char);
         
         }

The above prints 13 chars for every char received from the GPS. It may be good enough for testing but it requires that the GPS data stream be idle
(1-1/13)%=92% of the time for reliable receipt of GPS data.
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