Jump to content

Recommended Posts

Help. I am lost and at end of my rope.

 

I'm trying to print stings from 18F2620 using library SUART function thats on the l i k a . b e web site (link found on sourceboost.com). I've been using the same code sucessfully with PIC16 (16f88, 16f777, etc) without any problems!!!!

 

I am NOW trying to get the routines to work on 18F2620 and it won't work. I can send a character ok, I can receive a character ok (i.e. serial link is alive and well). However when I try to print a string I get gibberish (like baud rate is incorrect - but its not since I can send/receive characters).

 

Is this poor C coding, SourceBoost bug or quirk with HyperTerm? I tried changing baud rates, terminal emulation, and nothing seems to work

 

I'm using SourceBoost IDE and SourceboostC v 6.60. Programmer is PICKit2 v 2.10 on Windows 2000.

 

Here are the functions:

 

void sendChar(char value)

{

while((txsta & 1 << TRMT) == 0);

txreg = value;

}

 

 

void sendString(const char* text)

{

char i = 0;

while(text != 0)

sendChar(text[i++]);

 

}

 

 

NOTE:

sendChar ('A') ; // works ok!!!

sendString ("AAAA"); // Fails

 

Any help would be greatly appreciated.

 

Thanks.

Link to post
Share on other sites

Hyperterminal is VERY far from a reliable terminal. ComDebug/Windmill will help

you find out where the timming issue is, there are other decent teminals out there.

 

With that said, it sounds more of a timming issue for you.

 

Most PICs have room for three Bytes of data in rxreg before it starts dropping data.

If your not handling the incomming data fast enoughyou will have to implement

the backoff and retransmit signals which is no fun. Using an interrupt and jamming

your incomming data into an array as fast as your device can is usually how strings

are handled by most people i know, once you have the data you can parse it in your

own time.

 

Also remember that the 18Fs have priority interrupts, so if strings are that important

place it in the high priority.

 

Hope any of that might help.

Link to post
Share on other sites

Thanks.

 

Funny thing is that sendChar in a loop works fine. For example:

 

for (i=0; i<5; i++) putChar ('A'); // Works fine every time!!

 

where as printString does not work

 

printString ("AAAAA"); // Does not work

 

 

I thought of timing so I put a delay (200 ms) in the while loop within printString and it does not work. i.e.

 

void sendString(const char* text)

{

char i = 0;

while(text != 0) {

sendChar(text[i++]); // Does not work

delay_ms (200);

}

 

}

 

Next, I tried Windmill (thnks for tip) and it receives more characters than expected during sendString.

 

This really seems like a pointer problem or a C compiler problem. The routine works fine (I've debugged it and it also runs on PIC16F). Your point about timing make sense (PIC16 vs PIC18) but in light of above...

 

The only other thing to try is a different compiler or interrupts with txbuffer.

 

 

Any suggestions?

Link to post
Share on other sites

Thank you for pointing that out.

 

Typo in post. No typo in code. I don't think the linker would like that :D

 

Its supposed to be sendString and sendChar.

 

Sorry bout the confusion.

 

Any help or suggestions would be greatly appreciated.

Link to post
Share on other sites

From your previous posts I would guess that the code you are using to set up the serial function block is not correct.

 

It may work for the 16Fxxx but fails on the 18F2620.

 

I don't have any way to verify on real hardware.

 

This is the code I used for the simulator:

// File: example1.C
// Target: PIC18F2620
// OS: WinXP, SP2
// SourceBoostIDE: 6.60
// Compiler: BoostC 6.60
//
// Description: 
//
#include <system.h> 

#define SIMDEBUG

#ifdef SIMDEBUG
volatile char a;
#endif

void sendChar(char value)
{
#ifdef SIMDEBUG
a=value;
#else
while((txsta & 1 << TRMT) == 0); 
txreg = value;
#endif
}

void sendString(const char* text)
{
char i = 0;
while(text[i] != 0)
sendChar(text[i++]);
}

void sendString(rom char* text)
{
char i = 0;
while(text[i] != 0)
sendChar(text[i++]);
}

rom char * litString = "CODE";

void main()
{
while(1) 
{
 sendChar('a');
 sendString("RAM");
 sendString(litString); 
}
}

 

Please add your serial function block initialization to the code in this post and see if it works for your hardware.

Edited by cac001
Link to post
Share on other sites

Sorry I don't know how to add to your source code or how to check it via simulator. Code enclosed below.

 

 

Hardware config was my first thought. The 18F2620 uses EUSART instead of AUSART but I was told (from Micorchip forum) its bascially the same. The difference is 16 bit baud generator and autobaud (both disabled in my configuration - see below).

 

I will also post init code on Microchip Forum to verify.

 

Thanks!!

 

#pragma CLOCK_FREQ 20000000 // 20 Mhz

#pragma DATA _CONFIG1H, _OSC_HS_1H & _IESO_OFF_1H & _FCMEN_OFF_1H

#pragma DATA _CONFIG2L, _BOREN_OFF_2L

#pragma DATA _CONFIG2H, _WDT_OFF_2H

#pragma DATA _CONFIG3H, _MCLRE_OFF_3H & _LPT1OSC_OFF_3H & _PBADEN_OFF_3H & _CCP2MX_PORTC_3H

 

 

void setupUart( void )

{

trisc.7 = 1; // Input - Rx

trisc.6 = 1; // Ouput - Tx (Mandated by Datasheet)

 

pie1.TXIE = 0; // Disable RS232 TX Int

pie1.RCIE = 0; // Disable RS232 RD Int

 

rcsta.SPEN = 1; // set serial port enable

rcsta.CREN = 1; // set continuous receive enable

rcsta.RX9 = 0; // 8 Bit transmission

rcsta.ADDEN = 0; // Disable Addres Detection

 

// Disable Autobaud

baudcon.WUE = 0; // Don't monitor RX pin(Autobaud or wakeup)

baudcon.ABDEN = 0; // Disable Autobaud

 

// Set Baud

baudcon.BRG16 = 0; // Set to 8bit baud rate generator

txsta.BRGH = 1; // set high baud rate speed

spbrgh = 0; // for 20 MHz and 19200 Kbps (0.16% error)

spbrg = 64; // for 20 MHz and 19200 Kbps (0.16% error)

 

// txsta.SENDB = 0; // Don't send break character (transmission complete)

txsta.TXEN = 1; // set transmit enable

txsta.TX9 = 0; // 8 Bit transmission

txsta.SYNC = 0; // set async

}

Link to post
Share on other sites

Here is your initialization add to my sample:

// File: example1.C
// Target: PIC18F2620
// OS: WinXP, SP2
// SourceBoostIDE: 6.60
// Compiler: BoostC 6.60
//
// Description: 
//
#include <system.h> 

#pragma CLOCK_FREQ 20000000 // 20 Mhz
#pragma DATA _CONFIG1H, _OSC_HS_1H & _IESO_OFF_1H & _FCMEN_OFF_1H
#pragma DATA _CONFIG2L, _BOREN_OFF_2L
#pragma DATA _CONFIG2H, _WDT_OFF_2H 
#pragma DATA _CONFIG3H, _MCLRE_OFF_3H & _LPT1OSC_OFF_3H & _PBADEN_OFF_3H & _CCP2MX_PORTC_3H

void setupUart( void )
{
trisc.7 = 1;       // Input - Rx
trisc.6 = 1;       // Ouput - Tx (Mandated by Datasheet)

pie1.TXIE = 0;     // Disable RS232 TX Int
pie1.RCIE = 0;     // Disable RS232 RD Int

rcsta.SPEN = 1;    // set serial port enable
rcsta.CREN = 1;    // set continuous receive enable
rcsta.RX9 = 0;     // 8 Bit transmission
rcsta.ADDEN = 0;   // Disable Addres Detection

// Disable Autobaud
baudcon.WUE = 0;   // Don't monitor RX pin(Autobaud or wakeup)
baudcon.ABDEN = 0; // Disable Autobaud

// Set Baud 
baudcon.BRG16 = 0; // Set to 8bit baud rate generator
txsta.BRGH = 1;    // set high baud rate speed
spbrgh = 0;        // for 20 MHz and 19200 Kbps (0.16% error)
spbrg = 64;        // for 20 MHz and 19200 Kbps (0.16% error)

// txsta.SENDB = 0;  // Don't send break character (transmission complete)
txsta.TXEN = 1;    // set transmit enable
txsta.TX9 = 0;     // 8 Bit transmission
txsta.SYNC = 0;    // set async
}

//#define SIMDEBUG

#ifdef SIMDEBUG
volatile char a;
#endif

void sendChar(char value)
{
#ifdef SIMDEBUG
a=value;
#else
while((txsta & 1 << TRMT) == 0); 
txreg = value;
#endif
}

void sendString(const char* text)
{
char i = 0;
while(text[i] != 0)
sendChar(text[i++]);
}

void sendString(rom char* text)
{
char i = 0;
while(text[i] != 0)
sendChar(text[i++]);
}

rom char * romString = "CODE";

void main()
{
setupUart();
while(1) 
{
 sendChar('a');
 sendString("RAM");
 sendString(romString);
}
}

 

Your init code looks correct and simulates correctly using MPLAB but will not work with the SourceBoost simulator.

 

If this code fails with your hardware then there may be issues with your circuit or how your code downloads into the PIC.

 

Depending on your hardware you may want to disable Low Voltage Progamming (LVP bit in CONFIG4L). With LVP disabled RB5 becomes available as a digital I/O pin.

Edited by cac001
Link to post
Share on other sites

Disabling LVP has no affect. I'm using PICKit2 to program with ICSP as per PICKit 2 documentation. I'll check out the PICKit forum to see if others have had similar problems with programming devices.

 

I tool a look at the asm from pic18 and from pic16 generated code and I noticed that the code is quite different for both devices. I'm no asm guru (this is why I'm using C).

 

I recall seeing something about legacy mode and I found a config parameter (XINST) for legacy mode. When I disabled extended instruction set, it works perfectly!!

 

Do you know why this should make a difference?

 

Thanks.

Link to post
Share on other sites
I took a look at the asm from pic18 and from pic16 generated code and I noticed that the code is quite different for both devices. I'm no asm guru (this is why I'm using C).

 

I recall seeing something about legacy mode and I found a config parameter (XINST) for legacy mode.  When I disabled extended instruction set, it works perfectly!!

 

Do you know why this should make a difference?

The code generated for the PIC18F is very different from the PIC16F. It is kind of like the differences between the Z80 and the Z8. They are similar in may ways but different enough to be very confusing.

 

Enabling the extended instructions changes the way memory address work for the address range of 0x00 to 0x5F of the "ACCESS" bank of memory. When using this range the memory address is the value of the FSR2 plus the OFFSET (0x00-0x5F). When FSR2 is not ZERO the memory accessed may not be location that BoostC intended.

 

I do not think that BoostC uses 18F extended instructions or the Indexed Literal Offset address mode. The examples from SourceBoost are targeted only at 18F series PICs that do not have extended instruction sets.

 

This looks like where your code gets into trouble, so set the XINST to zero in CONFIG4L.

 

<added 2007-JAN-16>

Picxie is correct see Extended Instruction Set And Boostc

Edited by cac001
Link to post
Share on other sites
  • 3 years later...

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...