static prawn
static prawn

Reputation: 21

Pic16f723 fails trying to send data tx via rs 232, printf gets the pic stuck

I'm trying to send some data to my pc as first step of a bigger project, I'm working with the pic16f723.

I'm trying to use the same methods I've used before with pic16f876 , or pic16f818, etcetera, but there are not working.

Cant find where the problem is, I just want to transmit a byte. I am checking and rechecking the PIC16F723 datasheet but I am unable to solve anything.

As far as I know, it gets stuck in the loop above which checks the TXEN bit from the TXSTA register, and it is always in = so it gets always checking it. See the whole asm and ccs above

003F:  MOVLW  05
0040:  BTFSS  0C.4
0041:  GOTO   040

I wonder why ccs doesnt enable the TXEN , i actually have tried to make it work by this way:

 .................... #asm  
    BSF   0x0C,4  
    .................... #endasm 

But nothing was solved..

Can somebody help me with this? Any idea?

Here is my CCS code

#include <16F723.h>

 
#FUSES INTRC_IO 
#FUSES NOPROTECT
#FUSES NOMCLR 

#fuses NOWDT,NODEBUG,NOPUT,NOBROWNOUT 
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6,rcv=PIN_C7) 
 
 #byte PORTA=0x05
 #byte PORTB=0x06
 #byte PORTC=0x07
 #byte TRISA=0x85
 #byte TRISB=0x86
 #byte TRISC=0x87
void main () {
TRISA=0x00;  
TRISB=0x00;   
PORTA=0xFF; 
    
   while(1){
PORTB=0xF0; //<- This line gets the PIC stuck
 
printf("5");  // Trying to send data
delay_ms(500);// Those lines are just a bit sequence for tests
PORTB=0x0F; 
delay_ms(500);
         }
   }

And here you can check the asm/lst up as well:

CCS PCM C Compiler, Version 4.106, 10478               07-ago-16 20:03

               Filename: C:\Users\agf\Desktop\icom\swx_PIC16F723\PIC16f723b.lst

               ROM used: 85 words (2%)
                         Largest free fragment is 2048
               RAM used: 6 (3%) at main() level
                         7 (4%) worst case
               Stack:    1 locations

*
0000:  MOVLW  00
0001:  MOVWF  0A
0002:  GOTO   019
0003:  NOP
....................  
.................... #include <16F723.h> 
.................... //////// Standard Header file for the PIC16F723 device //////////////// 
.................... #device PIC16F723 
.................... #list 
....................  
....................  
....................   
.................... #FUSES XT                       //High speed Osc (> 4mhz)  
.................... #FUSES NOPROTECT 
.................... #FUSES NOMCLR  
....................  
.................... #fuses NOWDT,NODEBUG,NOPUT,NOBROWNOUT  
.................... #use delay(clock=4000000) 
0004:  MOVLW  22
0005:  MOVWF  04
0006:  BCF    03.7
0007:  MOVF   00,W
0008:  BTFSC  03.2
0009:  GOTO   018
000A:  MOVLW  01
000B:  MOVWF  78
000C:  CLRF   77
000D:  DECFSZ 77,F
000E:  GOTO   00D
000F:  DECFSZ 78,F
0010:  GOTO   00C
0011:  MOVLW  4A
0012:  MOVWF  77
0013:  DECFSZ 77,F
0014:  GOTO   013
0015:  GOTO   016
0016:  DECFSZ 00,F
0017:  GOTO   00A
0018:  RETURN
.................... #use rs232(baud=9600, xmit=PIN_C6,rcv=PIN_C7)  
.................... //#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8) 
....................   
....................     
....................  #byte PORTA=0x05 
....................  #byte PORTB=0x06 
....................  #byte PORTC=0x07 
....................  #byte TRISA=0x85 
....................  #byte TRISB=0x86 
....................  #byte TRISC=0x87 
.................... void main () { 
0019:  CLRF   04
001A:  BCF    03.7
001B:  MOVLW  1F
001C:  ANDWF  03,F
001D:  MOVLW  19
001E:  BSF    03.5
001F:  MOVWF  19
0020:  MOVLW  A6
0021:  MOVWF  18
0022:  MOVLW  90
0023:  BCF    03.5
0024:  MOVWF  18
0025:  BSF    03.5
0026:  BSF    03.6
0027:  BCF    05.0
0028:  BCF    05.1
0029:  BCF    05.2
002A:  MOVF   06,W
002B:  ANDLW  C0
002C:  MOVWF  06
002D:  BCF    03.6
002E:  BCF    1F.0
002F:  BCF    1F.1
0030:  BSF    03.6
0031:  MOVF   05,W
0032:  ANDLW  C0
0033:  MOVWF  05
.................... TRISA=0x00;   
0034:  BCF    03.6
0035:  CLRF   05
.................... TRISB=0x00;   
0036:  CLRF   06
....................  
.................... PORTA=0xFF; 
0037:  MOVLW  FF
0038:  BCF    03.5
0039:  MOVWF  05
....................              //Inicialización del display 
....................   
....................   // set_tris_a(0x1f);  //A ateak sarrerak 
....................   // set_tris_b(0x00); 
....................   
....................    setup_uart(TRUE); 
003A:  BSF    18.7
003B:  BSF    18.4
....................    while(1){ 
.................... PORTB=0xF0; 
003C:  MOVLW  F0
003D:  MOVWF  06 
.................... putc(0x05); 
003F:  MOVLW  05
0040:  BTFSS  0C.4
0041:  GOTO   040
0042:  MOVWF  19
....................  
.................... delay_ms(500); 
0043:  MOVLW  02
0044:  MOVWF  21
0045:  MOVLW  FA
0046:  MOVWF  22
0047:  CALL   004
0048:  DECFSZ 21,F
0049:  GOTO   045
.................... PORTB=0x0F;  
004A:  MOVLW  0F
004B:  MOVWF  06
.................... delay_ms(500); 
004C:  MOVLW  02
004D:  MOVWF  21
004E:  MOVLW  FA
004F:  MOVWF  22
0050:  CALL   004
0051:  DECFSZ 21,F
0052:  GOTO   04E
....................          } 
0053:  GOTO   03C
....................    } 
....................  
0054:  SLEEP

Configuration Fuses:
   Word  1: 3CD1   XT NOWDT NOPUT NOMCLR NOPROTECT NOBROWNOUT BORV19 PLLEN NODEBUG
   Word  2: 3FFF   NOVCAP

Upvotes: 0

Views: 297

Answers (3)

JD_GRINDER
JD_GRINDER

Reputation: 354

  1. Check the clock to be the same frequency as delay clock.
  2. Look in Microchip docs for TRISB setting because you are doing it wrong and you lock the chip. Use the CCS defined functions #use_standard_io(port) or #use_fast_io(port) plus set_tris_X() function to work with ports.
  3. the #use rs232 pre-processor directive must be placed before you actually start sending data via serial port. Any change in port C (Tris C,etc) will mess up with the rs232 thing and ruin your setup. Also add ERRORS to your #use RS232 so the chip can recover from stalled characters.
  4. use putc(),puts() instead of printf(). look at CCS provided examples. they work.

Upvotes: 0

Oren Fivlovich
Oren Fivlovich

Reputation: 66

  1. Working with debuggers in order to verify where the code hang - make sure optimization is off.
  2. Recheck the code, TXEN is NOT the bit you need to pool in order to check if the byte was transmitted.
  3. Not using ISR? Verify all interrupts are closed by clear GIE GIEL bits.
  4. printf("5"); - try using putc('5') instead.
  5. What is PORTB=0xF0; doing? If you use a debugger, PORTB is using this port for communication. Be careful when changing PORTB pins while debugging.

Upvotes: 0

Matt Jordan
Matt Jordan

Reputation: 2181

I don't see any bank select instructions in your inline assembler code.

The PIC16F series uses banks of 128 bytes of memory (0-7F, 80-FF, 100-17F, etc). The upper bits of larger addresses are stored in the Status register, which is one of a few registers visible across all banks, and must be set correctly for the registers that exist in only one bank.

From the C-compiler-generated code,

.................... TRISA=0x00;   
0034:  BCF    03.6  <- this line is modifying the necessary bank selection before accessing memory
0035:  CLRF   05

PIR1 is not in the same memory file bank as PORTB, which is the last location accessed by the C-compiler generated code.

Your C compiler is removing unnecessary bank select operations, which is the reason you only see a few scattered throughout your code, but if you are inserting assembler code inline, you are responsible for the bank selects within that code, since the C compiler's built-in assembler will not know what you are trying to do.

Usually the assembler will provide a macro for matching the bank with a named variable (this is the reason e.g. TRISB has value 0x86 but is accessed with the byte-address 06 in the above snippet). So, for example,

bank TRISB

would check the upper bits and determine how to select. You will need to look up the specific macro for your compiler though. I normally use PIC assembler, so I am not very familiar with different C compilers for the PIC.

Upvotes: 1

Related Questions