![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
jventerprises
Joined: 01 Apr 2004 Posts: 43 Location: Boston
|
SPBRG not programming with #use rs232 for F874 |
Posted: Tue Nov 23, 2004 11:52 pm |
|
|
hi everyone,
i am trying to use the UART on an F874. everything seems to work except the SPBRG doesn't get programmed! (i have the same code working on a F877a)
the relevant bits from my header file are:
#include <16F874.h>
#device ICD=TRUE
#device adc=10
#use delay(clock=4000000)
#fuses NOWDT,XT, PUT, NOPROTECT, NOLVP, NOCPD, NOWRT, BROWNOUT, DEBUG
#use rs232(baud=4800,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
i have to add the following line in main() for things to work:
SPBRG = 12;
and this in the include:
#byte SPBRG = 0x99
the 874 is operating at 4mhz, so this should give me 4800 baud. I have the same code (really!) for an 877a running at 20mhz and the SPBRG programs just fine. when i include the two lines above everything runs error free (for hours at least, that is as long as i have run the BER)
anybody seen this before? my compiler is 3.187. I checked the bug list, but didn't see anything that sounded like this.
thanks. _________________ Jon |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Nov 24, 2004 1:29 pm |
|
|
Quote: |
I am trying to use the UART on an F874. everything seems to
work except the SPBRG doesn't get programmed! (I have the
same code working on a F877a). My compiler is 3.187 |
I installed PCM vs. 3.187 and tested your program on a 16F877
without the "SPBRG = 12" statement, and it worked OK.
The 16F874 is the same thing as a F877, except it has less ROM
and RAM. So the test should be valid. I commented out the ICD
stuff because I'm not using it on my demo board.
I got this output from my demo board:
Code: | #include <16F874.h>
//#device ICD=TRUE
#device adc=10
#use delay(clock=4000000)
#fuses NOWDT,XT, PUT, NOPROTECT, NOLVP, NOCPD, NOWRT, BROWNOUT //, DEBUG
#use rs232(baud=4800,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
#byte SPBRG = 0x99
main()
{
// Comment out this line for the test.
// SPBRG = 12;
printf("Hello World\n\r");
while(1);
} |
Version 3.187 does program the SPBRG register properly.
Here is the startup code inserted by the compiler, from the .LST file.
Note that the compiler is moving 12 (0x0C) into the SPBRG register
at 0x99. (A switch to Bank 1 turns the "0x19" into 0x99 for the MOVWF)
Code: | 0000 00293 .................... main()
0000 00294 .................... {
0016 0184 00295 CLRF 04
0017 301F 00296 MOVLW 1F
0018 0583 00297 ANDWF 03,F
0019 3006 00298 MOVLW 06
001A 1683 00299 BSF 03.5 // Go to Bank 1
001B 009F 00300 MOVWF 1F
001C 300C 00301 MOVLW 0C // Load 0x0C into the W register
001D 0099 00302 MOVWF 19 // Move W into the SPBRG register
001E 3022 00303 MOVLW 22
001F 0098 00304 MOVWF 18
0020 3090 00305 MOVLW 90
0021 1283 00306 BCF 03.5
0022 0098 00307 MOVWF 18 |
The code above does the same thing as your manual code:
Code: | 0000 00309 .................... SPBRG = 12;
0023 300C 00310 MOVLW 0C // Load 0x0c into the W register
0024 1683 00311 BSF 03.5 // Go to Bank 1
0025 0099 00312 MOVWF 19 // Move W into the SPBRG register
|
You should look at your .LST file to see if it has the same startup
code as shown above. If it doesn't, then you may not really be using
vs. 3.187. Or, maybe you should re-install the compiler. |
|
![](templates/subSilver/images/spacer.gif) |
jventerprises
Joined: 01 Apr 2004 Posts: 43 Location: Boston
|
my code shows something funny.. |
Posted: Wed Nov 24, 2004 3:43 pm |
|
|
hi!
well here is my list code for the init stuff
.................... void main()
.................... {
*
05DA: CLRF 30
05DB: MOVLW 59
05DC: MOVWF 2F
05DD: MOVLW 14
05DE: MOVWF 59
05DF: MOVLW 71
05E0: MOVWF 5A
05E1: MOVLW 00
05E2: MOVWF 5B
05E3: MOVLW 0B
05E4: MOVWF 71
05E5: MOVLW A0
05E6: MOVWF 72
05E7: MOVLW 00
05E8: MOVWF 73
05E9: MOVLW 45
05EA: BSF 03.5
05EB: MOVWF 20
05EC: MOVLW F1
05ED: MOVWF 21
05EE: MOVLW 00
05EF: MOVWF 22
05F0: MOVLW 0B
05F1: MOVWF 71
05F2: MOVLW 00
05F3: MOVWF 72
05F4: MOVWF 73
05F5: CLRF 04
05F6: MOVLW 1F
05F7: ANDWF 03,F
05F8: MOVLW 06
05F9: MOVWF 1F
05FA: MOVLW 0C
05FB: MOVWF 19
05FC: MOVLW 22
05FD: MOVWF 18
05FE: MOVLW 90
05FF: BCF 03.5
0600: MOVWF 18
.................... int1 M433_RX_IN; // a message has come in the 433 and is in the buffer
....................
.................... // SPBRG = 12;
.................... setup_adc_ports(RA0_RA1_RA3_ANALOG);
these two lines should do it...
05FA: MOVLW 0C
05FB: MOVWF 19
but they dont because the bank bit for bank1 was cleared by
05F6: MOVLW 1F
05F7: ANDWF 03,F
and i am missing your line that sets back to bank!
!!
therefore address 0x19 is updated, not 0x99. my init functions look like
int1 M433_RX_IN; // a message has come in the 433 and is in the buffer
// SPBRG = 12;
setup_adc_ports(RA0_RA1_RA3_ANALOG);
setup_adc(ADC_CLOCK_DIV_8);
setup_psp(PSP_DISABLED);
setup_spi(SPI_MASTER|SPI_H_TO_L|SPI_CLK_DIV_16);
setup_counters(RTCC_INTERNAL,RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DIV_BY_4,125,4);
enable_interrupts(INT_TIMER2);
output_high(SPI_CS);
// make sure the XCVR is idle
XCVR_433_control(TRANSMIT_IDLE);
XCVR_433_control(RECEIVE_ACTIVE);
// allocate memory for buffer and clear
TXRX_buffer = malloc(60);
memset(TXRX_buffer, 0, 60);
.
.
.
here is the relevant part of my header file
#include <16F874.h>
#device ICD=TRUE
#device adc=10
#use delay(clock=4000000)
#fuses NOWDT,XT, PUT, NOPROTECT, NOLVP, NOCPD, NOWRT, BROWNOUT, DEBUG
#use rs232(baud=4800,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
#include "STDLIBM.h"
#byte SPBRG = 0x99
something funny is going on. _________________ Jon |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Nov 24, 2004 4:16 pm |
|
|
Post a test program that causes that output.
The problem is apparently caused by some #use statement
or #device statement that affects the startup code.
Post a test program that is small, but shows the problem. |
|
![](templates/subSilver/images/spacer.gif) |
jventerprises
Joined: 01 Apr 2004 Posts: 43 Location: Boston
|
sure... |
Posted: Wed Nov 24, 2004 6:09 pm |
|
|
but it might not be until friday. thanks for your help! _________________ Jon |
|
![](templates/subSilver/images/spacer.gif) |
jventerprises
Joined: 01 Apr 2004 Posts: 43 Location: Boston
|
its #include "STDLIBM.h" thats doing it |
Posted: Wed Nov 24, 2004 6:51 pm |
|
|
it seems to be the inclusion of STDLIBM.h that does it. here are the stripped files.
sprofiler.c::
#include "sprofiler.h"
#int_TIMER2
TIMER2_isr()
{
}
/************************************/
/************************************/
void main()
{
// SPBRG = 12;
setup_adc_ports(RA0_RA1_RA3_ANALOG);
setup_adc(ADC_CLOCK_DIV_8);
setup_psp(PSP_DISABLED);
setup_spi(SPI_MASTER|SPI_H_TO_L|SPI_CLK_DIV_16);
setup_counters(RTCC_INTERNAL,RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DIV_BY_4,125,4);
enable_interrupts(INT_TIMER2);
}
and sprofiler.h::
#include <16F874.h>
#device ICD=TRUE
#device adc=10
#use delay(clock=4000000)
#fuses NOWDT,XT, PUT, NOPROTECT, NOLVP, NOCPD, NOWRT, BROWNOUT, DEBUG
#use rs232(baud=4800,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
#include "STDLIBM.h"
if you build without the STDLIBM.H include, everything is ok. if you build with it, you get the bank fault as before....
maybe the include is in the wrong place? _________________ Jon |
|
![](templates/subSilver/images/spacer.gif) |
jventerprises
Joined: 01 Apr 2004 Posts: 43 Location: Boston
|
its #include "STDLIBM.h" thats doing it |
Posted: Fri Dec 03, 2004 10:05 pm |
|
|
its #include "STDLIBM.h" thats doing it
Posted: Wed Nov 24, 2004 6:51 pm
--------------------------------------------------------------------------------
i posted this again since i posted it right before thanksgiving and people were busy. i really would like to get this fixed....
it seems to be the inclusion of STDLIBM.h that does it. here are the stripped files.
sprofiler.c::
#include "sprofiler.h"
#int_TIMER2
TIMER2_isr()
{
}
/************************************/
/************************************/
void main()
{
// SPBRG = 12;
setup_adc_ports(RA0_RA1_RA3_ANALOG);
setup_adc(ADC_CLOCK_DIV_8);
setup_psp(PSP_DISABLED);
setup_spi(SPI_MASTER|SPI_H_TO_L|SPI_CLK_DIV_16);
setup_counters(RTCC_INTERNAL,RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DIV_BY_4,125,4);
enable_interrupts(INT_TIMER2);
}
and sprofiler.h::
#include <16F874.h>
#device ICD=TRUE
#device adc=10
#use delay(clock=4000000)
#fuses NOWDT,XT, PUT, NOPROTECT, NOLVP, NOCPD, NOWRT, BROWNOUT, DEBUG
#use rs232(baud=4800,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
#include "STDLIBM.h"
if you build without the STDLIBM.H include, everything is ok. if you build with it, you get the bank fault as before....
maybe the include is in the wrong place? _________________ Jon |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat Dec 04, 2004 2:36 am |
|
|
The problem is caused by this line, in stdlibm.h:
#USE DYNAMIC_MEMORY
That line causes the compiler to put in startup code for malloc, etc.
Apparently the compiler does not realize that it has cleared
the bank select bits when it puts in these two lines:
MOVLW 1F
ANDWF STATUS,F
So it doesn't re-select Bank 1, as it should.
There may well be some trick that can cause the compiler
to do it correctly. I didn't find one in the little time that
I played with it. Even so, you'd still have to document it
if you did find one. The best solution may be to do what
you're doing. Set the SPBRG manually, and document why
you did it. |
|
![](templates/subSilver/images/spacer.gif) |
|
|
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
|