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

PCW C Compiler IDE for 16F877 Microchip

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







PCW C Compiler IDE for 16F877 Microchip
PostPosted: Tue Nov 11, 2003 9:10 pm     Reply with quote

Hello.

I am very frustrated - I have been trying to send text through the serial port using my 16F877 microchip using the following code.
I am using the PCW C Compiler IDE to compile / load my C program onto the chip.
---

#include <16F877.h>

#fuses nowdt, hs
#device ICD=true

void delay(void);
void pause(unsigned int count);

void main() {
unsigned int i;

while (1) {
output_bit(PIN_B4, 0);
pause(4);
output_bit(PIN_B4, 1);
pause(4);

output_bit(PIN_C6, 0);
delay();
for (i=0; i<4; i++) {
output_bit(PIN_C6, 1);
delay();
output_bit(PIN_C6, 0);
delay();
}
output_bit(PIN_C6, 1);
delay();
}

}

void pause (unsigned int count){
unsigned long int i,j;
for(i=0;i<count;i++)
for (j=0;j<0xffff;j++);
}

void delay() {
setup_timer_0(RTCC_DIV_8);
set_timer0(0);
while (get_timer0() < 130);
}

---

Now the above setup assumes that the receiving end has baud 4800, parity = none, stop bits = 1, data bits = 8, flow control = none.
PIN_B4 is the LED pin (just for show) and PIN_C6 is the chip's output pin.

When the program is run, I get the following on my hyperterminal:

UUUUUUUUUWUUUUUUUUUUUUUUUUUUUUUUUUUU�UUUUU]UUUU�UUUUUUUUUUUUUUuUU]UUUUUUUUUUUUU��U�UUU�UUuUWUUUUUUUUUUUWU�UUUUUU�UUUUUUUUUUUUUUWUUUUUUUUUUUUUUUUUUUUUUUUUUUUUuUUUUUUUUUU�UUUUUUUWUUUUUUUUUUUU�UUUUUUU

From the code, I should be getting all U's, as follows:

UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU

I am not using the printf function because that results in similar mistranscriptions - I wanted to get down to the nitty-gritty basics in order to understand the problem.

I am totally lost on this one. Thank you for your time and consideration.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Nov 11, 2003 10:02 pm     Reply with quote

You shouldn't do it that way. You should use the CCS RS-232 functions
and if you're doing a delay, use the CCS delay functions.
That's why we buy this compiler.

The key to making everything work is the #use Delay statement,
which tells the compiler your crystal frequency. From that value,
it will calculate the correct timing for everything. So if you're using
a 20 MHz crystal, then do it as shown in the example below.
If you're using the ICD, then put in the #device statement for that, too.


Code:
#include <16F877.h>
#fuses HS, NOWDT, NOPROTECT, PUT, BROWNOUT, NOLVP
#use Delay(clock=20000000)
#use rs232(baud=4800, xmit=PIN_C6, rcv=PIN_C7, errors)

main()
{

while(1)
  {
    putc('U');
  }

}


This assumes that your PC (presumably running HyperTerminal) is
set for 4800, N, 8, 1.
StI2yICeR
Guest







Still some anomalies...
PostPosted: Wed Nov 12, 2003 9:25 am     Reply with quote

Hi. I am grateful for your help. The following is my code:

Code:
#include <16F877.h>
#fuses HS, NOWDT, NOPROTECT, PUT, BROWNOUT, NOLVP
#device ICD=true
#use Delay(clock=20000000)
#use rs232(baud=4800, xmit=PIN_C6, rcv=PIN_C7, errors)

main()
{

while(1)
   {
      putc('U');
   }

}


Yet, I received the following on my HyperTerminal:

UU�UUUUUUUUUUUUU�UUUUUUUUUUUUUUUUUUUU]UUWUUUUUUUUUUUUUUUUUUUU�UUUUUUUUUUUUUUUUUuUUU�UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU�WUUUUUUUUUUU�

What could be the cause of such mistransmissions?
I suppose I would have to resort to manual error checking (by using feedback to see if what was sent equals what was received) if this anomaly is an error in the chip itself.
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

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

PostPosted: Wed Nov 12, 2003 1:07 pm     Reply with quote

How are you connecting the PIC to the PC. Maybe the problem is there.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Nov 12, 2003 1:29 pm     Reply with quote

I tested my sample program with HyperTerminal on Win98,
at 4800,N,8,1 and it works fine. It displays hundreds of "UUU"
with no problems.

I'm using a MAX232A chip for the RS-232 level conversion.

As a test, I went into Device Manager and in the Advanced section
for Com Port 1, and turned off the receive fifo. It still worked OK.

So, I think the problem could be:

1. Your crystal. Is it really running at 20 MHz ?
Are you using a crystal, or something else (resonator ?).

2. As Mark says, how are you connecting it to your PC ?
Do you have a Ground wire, going from pin 5 on the DB-9
(on the PC side) to ground on your PIC board ?

Do you have a MAX232A (or equivalent chip), and is it
putting out proper RS-232 levels ?
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