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

Serial communication with 16F1847 - SOLVED
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
gjs_rsdi



Joined: 06 Feb 2006
Posts: 468
Location: Bali

View user's profile Send private message Send e-mail

Serial communication with 16F1847 - SOLVED
PostPosted: Thu Apr 28, 2016 4:53 am     Reply with quote

Hi

I can't find the reason the PIC16F1847 serial communication won't work at clock speed of 8MHz.
Code:
#include <16F648A.h>

#FUSES WDT                 
//***************FUSES for 8MHz external clock*********************
#FUSES EC_IO//for external clock                 
#use delay(clock=8000000,RESTART_WDT)//for external clock
//***************FUSES for 4MHz internal RC oscilator**************
//#FUSES INTRC_IO//for internal oscilator
//#use delay(clock=4000000,RESTART_WDT)//for internal oscilator
//*****************************************************************
#FUSES BROWNOUT,MCLR,NOLVP,PUT
//*****************************************************************
#FUSES PROTECT//for code protection
//#FUSES NOPROTECT//no code protection
//*****************************************************************
#FUSES NOCPD//Data EEPROM NOT Code Protected
//#FUSES CPD//Data EEPROM Code Protected
//*****************************************************************
#use rs232(baud=9600,parity=N,xmit=PIN_B2,rcv=PIN_B1,bits=8,restart_wdt)//for external clock
//#use rs232(baud=4800,parity=N,xmit=PIN_B2,rcv=PIN_B1,bits=8,restart_wdt)//for internal oscilator
//*****************************************************************

The above one works
Code:
#include <16F1847.h>
#device ADC=10
#FUSES WDT
#FUSES ECH
#FUSES PLL                   
#FUSES NOIESO               
#FUSES BORV25             
#FUSES MCLR,NOLVP,PUT         
#FUSES NOFCMEN               
//*****************************************************************
#FUSES PROTECT
//*****************************************************************
#FUSES NOCPD
//*****************************************************************
#use delay(clock=32M)
//*****************************************************************
#use rs232(baud=9600,parity=N,xmit=PIN_B2,rcv=PIN_B1,bits=8,restart_wdt)
//*****************************************************************

The above works
Code:
#include <16F1847.h>
#device ADC=10
#FUSES WDT                   
#FUSES INTRC_IO            
#FUSES NOIESO               
#FUSES BORV25               
#FUSES MCLR,NOLVP,PUT         
#FUSES NOFCMEN                
//*****************************************************************
#FUSES PROTECT//for code protection
//*****************************************************************
#FUSES NOCPD//Data EEPROM NOT Code Protected
//*****************************************************************
#use delay(internal=16000000,restart_wdt)
#use rs232(baud=9600,parity=N,xmit=PIN_B2,rcv=PIN_B1,bits=8,restart_wdt)

The above works also
Code:
#include <16F1847.h>
#device ADC=10
#FUSES WDT                     
#FUSES NOIESO               
#FUSES BORV25             
#FUSES MCLR,NOLVP,PUT         
#FUSES NOFCMEN               
//*****************************************************************
#FUSES PROTECT
//*****************************************************************
#FUSES NOCPD
//*****************************************************************
#use delay(internal=32M)
//*****************************************************************
#use rs232(baud=9600,parity=N,xmit=PIN_B2,rcv=PIN_B1,bits=8,restart_wdt)

The above works
Code:
#include <16F1847.h>
#device ADC=10
#FUSES WDT                      //Watch Dog Timer
#FUSES ECH,NOCLKOUT
#FUSES PLL_SW            
#FUSES NOIESO                   //Internal External Switch Over mode disabled
#FUSES BORV25                   //Brownout reset at 2.5V
#FUSES MCLR,NOLVP,PUT         //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOFCMEN                  
//*****************************************************************
#FUSES PROTECT//for code protection
//*****************************************************************
#FUSES NOCPD//Data EEPROM NOT Code Protected
//*****************************************************************
#use delay(clock=8000000,restart_wdt)
#use rs232(baud=9600,parity=N,xmit=PIN_B2,rcv=PIN_B1,bits=8,restart_wdt)

The above serial communication don't work, everything else works including LED blink
Code:
#include <16F1847.h>
#device ADC=10
#FUSES WDT                      //Watch Dog Timer
#FUSES INTRC_IO               //internal oscillator
#FUSES NOIESO                   //Internal External Switch Over mode disabled
#FUSES BORV25                   //Brownout reset at 2.5V
#FUSES MCLR,NOLVP,PUT         
#FUSES NOFCMEN                  
//*****************************************************************
#FUSES PROTECT//for code protection
//*****************************************************************
#FUSES NOCPD//Data EEPROM NOT Code Protected
//*****************************************************************
#use delay(internal=8000000,restart_wdt)
#use rs232(baud=9600,parity=N,xmit=PIN_B2,rcv=PIN_B1,bits=8,restart_wdt)

The above serial communication don't work, all other functions work including LED flash.

Any ideas?
Tested many time, same terminal, same hardware.

Best wishes
Joe


Last edited by gjs_rsdi on Fri Apr 29, 2016 11:32 pm; edited 1 time in total
temtronic



Joined: 01 Jul 2010
Posts: 9243
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu Apr 28, 2016 5:58 am     Reply with quote

OK, I don't use that PIC but did download the datasheet...
The 8MHz option appears to be tied to the PLL as wel as the clk mux( divider) so it could be a compiler 'bug' not setting the correct bits though there is no 'disable' bit, would have to see a listing to decode though. And the compiler version !
I do recall seeing a similar problem here awhile ago perhaps same PIC or family member, maybe search for that thread.
As a general comment, normally you do not det the 'protect' fuse until product goes out the door .
Also during testing I never enable the WDT until I'm sure the othe rcode is 100%.
Ttelmah



Joined: 11 Mar 2010
Posts: 19538

View user's profile Send private message

PostPosted: Thu Apr 28, 2016 7:22 am     Reply with quote

Your very first one is saying it does work.....

Don't protect code when debugging. Doing so _shortens the life of the chip_.
It implies a full erase has to be performed every time even when only a tiny part of the code changes.....
Having the watchdog enabled, and then scattering 'restarts' into the code in delays and serial, is basically pointless as well. Do some reading on the watchdog, and understand why this is _not_ the way to use it.
Removing all the pointless remarked lines (and the protect).
Code:

#include <16F648A.h>

#FUSES WDT                 
#FUSES EC_IO//for external clock                 
#use delay(clock=8000000,RESTART_WDT)
#FUSES BROWNOUT,MCLR,NOLVP,PUT
#use rs232(baud=9600, parity=N,x mit=PIN_B2,rcv=PIN_B1, bits=8, restart_wdt)
//for external clock


You say this does work?.

The one you say doesn't work, are you enabling the PLL in software?. If so, then 'of course' it won't work. The flash will be at the wrong speed.

Key thing with the LED flash, is to time it. If it is not at the speed it is meant to be, then your clock statement is wrong, and serial won't work.
gjs_rsdi



Joined: 06 Feb 2006
Posts: 468
Location: Bali

View user's profile Send private message Send e-mail

PostPosted: Thu Apr 28, 2016 8:48 am     Reply with quote

Thank you for the advises temtronic and Ttelmah

With the 16F648A is a finished product and it works with 8MHz external clock and with the 4MHz internal RC after implementing the advises I got here. With the internal RC the communication is with no problems at 4,800.
Just wanted to migrate to the 16F1847 because I can run it at higher speed with the internal RC and the AC motor turning more smooth at 8MHz and up.

Added the code protection and WDT after all was working, forget to change at the 8MHz for the 16F1847.

Regarding the clock on both internal and external 8MHz it is correct as all the functions are working correct, including the AC motor that turns in the correct speeds and the LED blinks correctly, ones a second.
Not enabling PLL in software.

I will clear the unnecessary things per Ttelmah advice (for the 16F1847 as the 16F648A works OK) and will post the results.

Best wishes
Joe
Ttelmah



Joined: 11 Mar 2010
Posts: 19538

View user's profile Send private message

PostPosted: Thu Apr 28, 2016 9:34 am     Reply with quote

OK.

Code:

#include <16F1847.h>
#device ADC=10
#FUSES NOWDT                      //Watch Dog Timer - off for test
#FUSES ECH,NOCLKOUT
#FUSES PLL_SW           
#FUSES NOIESO                   //Internal External Switch Over mode disabled
#FUSES BORV25                   //Brownout reset at 2.5V
#FUSES MCLR,NOLVP,PUT   
#FUSES NOFCMEN 
#FUSES STVREN               
#use delay(clock=8MHz)
#use rs232(baud=9600,parity=N,UART1,bits=8,errors)
#byte APFCON0 = getenv("SFR:APFCON0")
#byte APFCON1 = getenv("SFR:APFCON1")

void main(void)
{
   APFCON0=APFCON1=0;
   delay_ms(1000);
   printf("Test string\n");
   while(TRUE);
}


The compiler is defaulting to setting the APFCON registers to 1. They are meant to default to 0. Hence the UART TX and RX are on the alternate pins, and your code will be using software serial.

I just tried this on a chip with an external frequency generator set to 8MHz, and it merrily output 9600bps data.

Compiler 5.058

As a comment you should always have 'ERRORS' when using the hardware UART, unless you are adding your own error handling code.
gjs_rsdi



Joined: 06 Feb 2006
Posts: 468
Location: Bali

View user's profile Send private message Send e-mail

PostPosted: Thu Apr 28, 2016 3:07 pm     Reply with quote

Thanks Ttelmah

If I understand correct, with my initialization I am using software rs232?
Including at 16 and 32MHz?
I always used hardware rs232 (hope so) with:
Code:
enable_interrupts(INT_RDA);
disable_interrupts(INT_TBE);
enable_interrupts(INT_TBE);

Same in this program
With:
Code:
#byte APFCON0 = getenv("SFR:APFCON0")
#byte APFCON1 = getenv("SFR:APFCON1")
APFCON0=APFCON1=0;

I am forcing the chip to work with hardware rs232?
My compiler is the demo compiler: CCS PCM C Compiler, Version 5.056d, 1
Going to test it shortly

Best wishes
Joe
gjs_rsdi



Joined: 06 Feb 2006
Posts: 468
Location: Bali

View user's profile Send private message Send e-mail

PostPosted: Thu Apr 28, 2016 3:47 pm     Reply with quote

Hi Ttelmah

Thank you for the help.
With 8MHz external clock tested and it works Smile
With 8MHz internal clock tested, works, but sometimes don't answer to my request from the PC
the settings are:
Code:
#include <16F1847.h>
#device ADC=10
#FUSES NOWDT                      //Watch Dog Timer - off for test
#FUSES INTRC_IO               //internal oscillator
//#FUSES ECH,NOCLKOUT
#FUSES PLL_SW           
#FUSES NOIESO                   //Internal External Switch Over mode disabled
#FUSES BORV25                   //Brownout reset at 2.5V
#FUSES MCLR,NOLVP,PUT   
#FUSES NOFCMEN 
#FUSES STVREN               
//#use delay(clock=8MHz)
#use delay(internal=8MHz)
#use rs232(baud=9600,parity=N,UART1,bits=8,errors)
#byte APFCON0 = getenv("SFR:APFCON0")
#byte APFCON1 = getenv("SFR:APFCON1")
APFCON0=APFCON1=0;//in main


My question also, I am working with the hardware rs232 now?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Apr 28, 2016 4:38 pm     Reply with quote

Quote:
My question also, I am working with the hardware rs232 now?

I'll tell you how to check this by yourself.

Make a short little program with your setup and a putc() statement
in main(). 0x55 is chosen as the character because it's a constant
and will be coded as a MOVLW 55 by the compiler, and it's easy to
locate in the .LST file. Other than that, the 0x55 is not important.
Code:

#include <16F1847.h>
#device ADC=10
#FUSES NOWDT                      //Watch Dog Timer - off for test
#FUSES INTRC_IO               //internal oscillator
//#FUSES ECH,NOCLKOUT
#FUSES PLL_SW           
#FUSES NOIESO                   //Internal External Switch Over mode disabled
#FUSES BORV25                   //Brownout reset at 2.5V
#FUSES MCLR,NOLVP,PUT   
#FUSES NOFCMEN 
#FUSES STVREN               
//#use delay(clock=8MHz)
#use delay(internal=8MHz)
#use rs232(baud=9600,parity=N,UART1,bits=8,errors)
#byte APFCON0 = getenv("SFR:APFCON0")
#byte APFCON1 = getenv("SFR:APFCON1")
 

//==========================
void main()     
{
APFCON0=APFCON1=0;

putc(0x55);

while(TRUE);   
}


Compile it, then look at the .LST file below. Scroll down past main() until
you see this section below. Sometimes, the complete code will be here (if
it's hardware UART code). With this PIC and this compiler version (5.056)
the putchar code is not placed here. Instead it calls the @putchar routine.
Code:
.................... putc(0x55);
0021:  MOVLW  55
0022:  MOVLB  00
0023:  GOTO   @PUTCHARI_BIU_1


The @putchar routine is not labeled in the .LST file if you're using Symbolic
format for the .LST file. There are 2-3 ways to find the address and
location of the putchar routine. One is to load the .SYM file and look at
this section:
Code:
ROM Allocation:
000003  @PUTCHARI_BIU_1
00000A  main
00000A  @cinit1
00001E  @cinit2

It shows us that @putchar is at program memory address 0003.
Going back to the .LST file, you can look at the code at address 0003.
It's just below the #use rs232() statement, which makes sense.
Quote:
.................... #use rs232(baud=9600,parity=N,UART1,bits=8,errors)
0003: BTFSS PIR1.TXIF
0004: GOTO 003
0005: MOVLB 03
0006: MOVWF TXREG
0007: MOVLP 00
0008: MOVLB 00
0009: GOTO 024 (RETURN)

You can see that @putchar is accessing the hardware transmit status bit
TXIF, and it's writing data to the hardware transmit data register TXREG.
Therefore, the compiler is using hardware rs232.
gjs_rsdi



Joined: 06 Feb 2006
Posts: 468
Location: Bali

View user's profile Send private message Send e-mail

PostPosted: Thu Apr 28, 2016 6:41 pm     Reply with quote

Hi PCM programmer

Thank you for the answer
I implemented the software you posted and tested
Following your instructions step by step, everything as you have written, except:
Code:
.................... #use rs232(baud=9600,parity=N,UART1,bits=8,errors) 
0003:  BTFSS  11.4
0004:  GOTO   003
0005:  MOVLB  03
0006:  MOVWF  1A
0007:  MOVLP  00
0008:  MOVLB  00
0009:  GOTO   024 (RETURN)

Is 11.4 the PIR1.TXIF ? In the data sheet is byte 11 bit 4, I suppose it is correct
Is 1A the TXREG ? In the data sheet TXREG is at 19A, TMR2 at 01A
Page 22 in the data sheet

Thanks again for your help
Joe
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Apr 28, 2016 6:47 pm     Reply with quote

Go to the Project Build Options menu in your IDE and select
Symbolic Format for the list file format. Then re-compile and
you will see the register names.
gjs_rsdi



Joined: 06 Feb 2006
Posts: 468
Location: Bali

View user's profile Send private message Send e-mail

PostPosted: Thu Apr 28, 2016 7:13 pm     Reply with quote

Hi PCM programmer

Made Symbolic Format for the LST file, project and .c but have no symbols, same as before.
I suppose is because of the demo program.

Joe
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Apr 28, 2016 7:17 pm     Reply with quote

The demo page claims the compiler demo is fully functional.
http://www.ccsinfo.com/ccsfreedemo.php
If it isn't, then CCS support needs to be informed of this.
After selecting Symbolic format, did you re-compile and re-load the .LST file ?

Anyway, look up the register addresses in the PIC data sheet.
That will allow you to interpret the .LST file.
gjs_rsdi



Joined: 06 Feb 2006
Posts: 468
Location: Bali

View user's profile Send private message Send e-mail

PostPosted: Thu Apr 28, 2016 8:50 pm     Reply with quote

Hi PCM programmer

I done as you said, it didn't show Symbolic format.
My old compiler version 3.249 changing to Symbolic format (16F648A).
Regarding .LST interpretation as I wrote that page 22 of the data sheet states TXREG at 19A, TMR2 at 01A.
I tried the demo on my old computer and is OK.
Must be something in the new computer MPLAB or CCS installation.

Best wishes
Joe
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Apr 28, 2016 9:16 pm     Reply with quote

I'm sorry I wasted all these posts on Symbolic mode. Here is the answer:

Quote:
Is 11.4 the PIR1.TXIF ? In the data sheet is byte 11 bit 4, I suppose it is correct

Yes, that's right.

Quote:
Is 1A the TXREG ? In the data sheet TXREG is at 19A

Yes, it's TXREG.

Quote:
TMR2 at 01A
Page 22 in the data sheet

That's right, but TMR2 is in Bank 0, but the code does a MOVLB 03 which
selects bank 3. That makes address 1A become 19A, which is TXREG.
gjs_rsdi



Joined: 06 Feb 2006
Posts: 468
Location: Bali

View user's profile Send private message Send e-mail

PostPosted: Thu Apr 28, 2016 10:28 pm     Reply with quote

Thanks PCM programmer

I don't think you wasted your time, you helped me a lot and I am sure helped and will help people that will face the same problem Smile

Thank you again for all your help
Joe
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
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