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

PIC programmer, beginner problems, setting up clock etc....

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



Joined: 08 Feb 2014
Posts: 23

View user's profile Send private message

PIC programmer, beginner problems, setting up clock etc....
PostPosted: Sat Feb 08, 2014 7:43 am     Reply with quote

Hi Guys
My set up as follows:
PIC16F87
IDE version 5.016
PCB version 5.016
PCM version 5.016
PCH version 5.016

ICD-U64
CCS LOAD V4.051

Windows 8.1 64 bit

My devboard
https://www.beta-estore.com/download/rk/RK-10121_103.pdf

I am new to pic programming, and i am having problems with rs232 communication which I believe it is down to the clock set up, which could be related to my devboard set up. can people have a look here and let me know of any faults
http://imgur.com/I5kKLdD
data sheet:
https://www.beta-estore.com/download/rk/RK-10121_103.pdf

When using external clock set up at 20MHZ it get a value in the debugger at 19.88MHZ which can be seen through the debug window.
I am sending data from an arduino (its all i got) and the best baud rate i can achieve is 2400 and the data is still not correct . I would prefere 9600.
Anyway i tried switching over to internal clock at 8MHZ (using PIC wizard) and all i am getting 0.03MHZ, from debug window.

What am i doing wrong? Is it my custom cable that i am using for the ICD to the devboard... is it my devboard????
What do people use to devlope on PIC16f87

I know this might seem like a question that is asked quite often, but I have spent alot of time here (searching this forum) trying to figure it out with no success.

Thanks
temtronic



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

View user's profile Send private message

PostPosted: Sat Feb 08, 2014 1:39 pm     Reply with quote

first question is ...

Do you have a MAX232 or equal between the PIC and PC?

I don't use the ICD-U64 so can't help you with the 'debugging' data. I use MPLAB and compiler in 'release' mode,burn the PIC, test in real world.

Using the internal 8MHz clock, you should get 9600 baud. Use the 'code' button and show us your code so we can see what you're doing.


hth
jay
zombiePIC



Joined: 08 Feb 2014
Posts: 23

View user's profile Send private message

PostPosted: Sat Feb 08, 2014 3:47 pm     Reply with quote

Hi,
Thanks for the reply
Just to make myself clear i was hoping to send data from one PIC to multiple PICs wirelessly. From searching the forums it looks doable.

But anyway i am starting from scratch (no wireless) and i am running into problems setting up the clock.

All i have between pic and PC is the ICD-u64.
I followed this guide when connecting to my devboard
http://www.ccsinfo.com/faq.php?page=connect_icd

I don't have a MAX232.
To the best of my knowledge i would not require a MAX232 chip as the ICD allows data to be routed through pin B3 via the icd-u64 and then on to the PC which is connected via usb.

here is a sample of code that was taken from the pic wizard.
Code:
#include <16F87.h>

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES NOBROWNOUT               //No brownout reset
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O

#use delay(internal=8MHz)

void main()
{

   while(TRUE)
   {
      //TODO: User Code
   }

}


I compile the code and press debug and this is what i get:
http://imgur.com/Cryne6n
notice the 0.03MHZ where i wan to run at 8mhz...
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Sat Feb 08, 2014 4:45 pm     Reply with quote

Quote:
I don't have a MAX232.
To the best of my knowledge i would not require a MAX232 chip as the ICD allows data to be routed through pin B3 via the icd-u64 and then on to the PC which is connected via usb.

Do a simple test with a voltmeter.
Most USB to RS232 devices I've met work on +/- 9V to 12V levels.
Check the TX from the PC and the TX from the ICD-U64.
You should then be able to work out whether you need a MAX232 or not.

Quote:
notice the 0.03MHZ where i wan to run at 8mhz...

The 0.03MHz looks close to the 32kHz default internal oscillator setting.
I believe the
Code:
#use delay(internal=8MHz)
should produce the correct frequency with recent compilers.

However you could try the setup_oscillator(xxx) option. See if that gets you nearer to where you want to be.

Mike

PS

Your 20MHz clock SHOULD be OK to 19,200 or even 38,400 baud.
The <1% error is not significant.
temtronic



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

View user's profile Send private message

PostPosted: Sat Feb 08, 2014 7:03 pm     Reply with quote

Hopefully someone who uses that ICD will reply as I have a HUGE problem trying to figure out HOW data on B3 gets routed to the ICD.
According to the schematic of your board (show 16F628) but 16F87/88 similar pinout, B3 goes to a header. The ICD gets connected only to the ICSP pins (same as PICkit3 that I use).
More 'troubles' as the UART uses pins RB5(tx) and RB2(rcv). So there is NO way I can see of getting 'serial' data from the PIC to the PC through the ICD unit.

I would add a 10pin header and cable to a MAX232 then to the PC.

hth
jay
zombiePIC



Joined: 08 Feb 2014
Posts: 23

View user's profile Send private message

PostPosted: Sun Feb 09, 2014 11:14 am     Reply with quote

Hi,

I dont need to use a MAX232 chip as I am using the ICD-U64, explained in better detail here:
https://pic-c.com/forum/viewtopic.php?p=168382
Quote:
The difference between debug monitoring and plain monitoring is....
Debug
With CCS IDE and debug monitoring using #USE RS232(DEBUGGER) any printf is directed through the default pinB3 to the monitor screen in the CCS IDE when debug is selected. The choice of the monitor pin is selectable.
Run time
Now suppose you don't want to debug but would still like to monitor. Also suppose you are powering the target board via the ICDU64. Your printf can still be directed through PIN_B3. The small advantage is you don't need to add a max232 level shifter and since you originally wired your target for programming and debugging via the ICD- U64 there is no extra wiring. The ICD-U64 delivers the printf data through the usb cable to a PC program without needing the IDE and its debugger. This avoids the very small overhead the debugger would need. Again its a small advantage that is a bit more useful with small code space PIC's and can help in some rare situations


I tried to send data from arduino to PIC (will be moving on to PIC to PIC, when i get the PIC sorted out)

here is the arduino code:
Code:
/*
Uses a FOR loop for data and prints a number in various formats.
*/
int x = 0;    // variable

void setup() {
  Serial.begin(9600);      // open the serial port at 9600 bps:   
}

void loop() { 
  // print labels
  Serial.print("NO FORMAT");       // prints a label
  Serial.print("\t");              // prints a tab

  Serial.print("DEC"); 
  Serial.print("\t");     

  Serial.print("HEX");
  Serial.print("\t"); 

  Serial.print("OCT");
  Serial.print("\t");

  Serial.print("BIN");
  Serial.print("\t");

  for(x=0; x< 64; x++){    // only part of the ASCII chart, change to suit

    // print it out in many formats:
    Serial.print(x);       // print as an ASCII-encoded decimal - same as "DEC"
    Serial.print("\t");    // prints a tab

    Serial.print(x, DEC);  // print as an ASCII-encoded decimal
    Serial.print("\t");    // prints a tab

    Serial.print(x, HEX);  // print as an ASCII-encoded hexadecimal
    Serial.print("\t");    // prints a tab

    Serial.print(x, OCT);  // print as an ASCII-encoded octal
    Serial.print("\t");    // prints a tab

    Serial.println(x, BIN);  // print as an ASCII-encoded binary
    //                             then adds the carriage return with "println"
    delay(500);            // delay 200 milliseconds
  }
  Serial.println("");      // prints another carriage return
}


Here is my PIC ccs c code
Code:
#include <16F87.H>
#device ICD=TRUE
#use delay (clock=20000000)
#fuses  HS, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP, NODEBUG
#use RS232(STREAM=Arduino,BAUD=9600, RCV=PIN_B2)
#use RS232(STREAM=debug,BAUD=9600, XMIT=PIN_B3,RCV=PIN_B3)


//======================================
main(void)
{

char ch;

while(1)
 {
   ch = getchar(Arduino);
  fputc( ch, debug);
 }

}


This is an image of my set up:
[IMG]http://i.imgur.com/9GyFdht.jpg?1[/IMG]
http://imgur.com/9GyFdht

This is what is being sent by the arduino:

Code:
O FORMAT       DEC     HEX     OCT     BIN     0       0       0       0       0
1       1       1       1       1
2       2       2       2       10
3       3       3       3       11
4       4       4       4       100
5       5       5       5       101
6       6       6       6       110
7       7       7       7       111
8       8       8       10      1000
9       9       9       11      1001
10      10      A       12      1010
11      11      B       13      1011
12      12      C       14      1100
13      13      D       15      1101
14      14      E       16      1110
15      15      F       17      1111
16      16      10      20      10000
17      17      11      21      10001
18      18      12      22      10010
19      19      13      23      10011
20      20      14      24      10100
21      21      15      25      10101
22      22      16      26      10110
23      23      17      27      10111
24      24      18      30      11000
25      25      19      31      11001
26      26      1A      32      11010
27      27      1B      33      11011
28      28      1C      34      11100
29      29      1D      35      11101
30      30      1E      36      11110
31      31      1F      37      11111
32      32      20      40      100000
33      33      21      41      100001
34      34      22      42      100010
35      35      23      43      100011
36      36      24      44      100100
37      37      25      45      100101
38      38      26      46      100110
39      39      27      47      100111
40      40      28      50      101000
41      41      29      51      101001
42      42      2A      52      101010
43      43      2B      53      101011
44      44      2C      54      101100
45      45      2D      55      101101
46      46      2E      56      101110
47      47      2F      57      101111
48      48      30      60      110000


This is what is recieved by the PIC....
Code:
ߌþƬkƍïÖ¬ïïÎßߍîƬkƍï߬ÿÞïÖ¬ÿïÎÏߌçÎÏߌîƬÏïþƬÏƍïÖ¬ÿï߬ÿïÎÏߌïÎÏߌþƬÏýîƬÏçÎï߬ÿïÖ¬ÿçÎÏάÿŒþÖŒîƬ֌þƬÏÿï߬çƌ߬ÿçÎÏ֍þÎÏߌîƬçƍƬkƍïÖ¬ÆÞŒÖÿþÎÞ­ÞîïïýïƬïcÖçƬŒïÆýïÆŒÿÿŒïþÆŒïþ­ïçÎçÎîÖÏ߬ïþÖŒÏÿý¬cÖŒÿÏƬïþÖŒÏÆý¬ßÖŒÿÏÿïÏÏŒîƬ¬ÿïÏ΍çÎßÖüîƬ¬ÆïÖߌïÎßÖÎþƬ¬ÿïÏßïçÎßߌþƬ¬ÆýïÖߍïÎßî΍Ƭ¬ïíïÏÎŒïÎßÖìîƬkƍï߬ïïÖ¬ÿçÎÏÖŒïÎÏÖŒîƬÖïþƬkçÞïÖ¬ïï߬ÿçÎÏߌçÎÏߌþƬߌîƬÏƍïßÏïïÖ¬cïÎÏÜÿçÎÏߍîά֌þƬÏƍƬÏÿï߬c÷ÎϬÿïÎÏÖŒþƬ֌îƬÏƍƬÏÿïÖ¬ÿÿÏÖ¬ÿïÎÏߌïÎÏߌþƬî΍Ƭ­ßÿïÎíçïŒßƌߌÿ¬ÆŒÖïïŒþÆŒïþŒcÆýŒÆÎÿïÿß­ïçÎÖŒÏïý¬ßÖŒÿ


I am at a huge loss......
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Sun Feb 09, 2014 11:47 am     Reply with quote

1) How do you KNOW what characters the PIC receives?
2) Can you convert the characters to HEX then compare with the expected characters?
3) Did the setup_oscillator help with clocks?
4) Are you in effect using a software UART on chip with a built in hardware one?
5) I'd be probing signals with a 'scope and some very short loop tests.

Mike
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Feb 09, 2014 12:38 pm     Reply with quote

I just did some experiments to determine the baud rate of the ICD
debugger monitor channel by comparing .LST files. It's actually 2400 baud.
(At least, for compiler vs. 5.016).
Change your #use rs232() line to this and see if it works better:
Code:
#use RS232(STREAM=DEBUGGER,BAUD=2400, XMIT=PIN_B3,RCV=PIN_B3)
zombiePIC



Joined: 08 Feb 2014
Posts: 23

View user's profile Send private message

PostPosted: Sun Feb 09, 2014 3:43 pm     Reply with quote

Hi thanks guys but i am still not 100%
I simplified my PIC and ARDUINO code
Quote:
I just did some experiments to determine the baud rate of the ICD
debugger monitor channel by comparing .LST files. It's actually 2400 baud.
(At least, for compiler vs. 5.016).
Change your #use rs232() line to this and see if it works better:
Code:
#use RS232(STREAM=DEBUGGER,BAUD=2400, XMIT=PIN_B3,RCV=PIN_B3)

My PIC code is updated to include your comment..
Code:
#include <16F87.H>
#device ICD=TRUE
#use delay (clock=20000000)
#fuses  HS, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP, NODEBUG
#use RS232(STREAM=Arduino,BAUD=9600, RCV=PIN_B2)
#use RS232(STREAM=DEBUGGER,BAUD=2400, XMIT=PIN_B3,RCV=PIN_B3)
//======================================
main(void)
{
char ch;

while(1)
 {
   ch = getchar(Arduino);
   fputc( ch, DEBUGGER);
   printf("\n\r");
 }
}



Arduino code:

Code:

void setup() {
  Serial.begin(9600);      // open the serial port at 9600 bps:   
}

void loop() {

  Serial.print("Hello");       // prints a Hello
 
    delay(1000);            // delay 1000 milliseconds
 
 
}


changing the Baud to 2400 improves things but all i am getting on the Debug monitor is
Code:

H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
H
H


Looks like i can only display the first character "H" of "Hello".
===============================================
Mike


Quote:
1) How do you KNOW what characters the PIC receives?
2) Can you convert the characters to HEX then compare with the expected characters?
3) Did the setup_oscillator help with clocks?
4) Are you in effect using a software UART on chip with a built in hardware one?
5) I'd be probing signals with a 'scope and some very short loop tests.

1. I am using Tera Term to monitor serial data sent from arduino to PIC
2. I will try...
3.I switched back to 20mhz...debugger says its at 19.88Mhz.... i should still be within the error?
4.I am recieving data on pin B2 which is hardware UART then the data is sent on through a software UART through Pin B3 a requirement by the icd-u64.
5. I've no scope at the moment but if I get my hands on I will loop "0x55" for sure.
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Sun Feb 09, 2014 4:10 pm     Reply with quote

Quote:
Quote:
1) How do you KNOW what characters the PIC receives?
2) Can you convert the characters to HEX then compare with the expected characters?
3) Did the setup_oscillator help with clocks?
4) Are you in effect using a software UART on chip with a built in hardware one?
5) I'd be probing signals with a 'scope and some very short loop tests.

1. I am using Tera Term to monitor serial data sent from arduino to PIC
2. I will try...
3.I switched back to 20mhz...debugger says its at 19.88Mhz.... i should still be within the error?
4.I am recieving data on pin B2 which is hardware UART then the data is sent on through a software UART through Pin B3 a requirement by the icd-u64.
5. I've no scope at the moment but if I get my hands on I will loop "0x55" for sure.

1) OK. The data from the arduino is confirmed as good. What I meant was, how did you know what the garbage recieved by the PIC was?
2) Comparing the HEX (or binary) codes for the sent and received data may give a clue as to what's going wrong.
3) The setup_oscillator controls the clock frequency when using the internal RC clock. Have you tried it?
4) So you're stuck with that.
5) Good, at least you know which tests to try for starters.

Mike.
zombiePIC



Joined: 08 Feb 2014
Posts: 23

View user's profile Send private message

PostPosted: Mon Feb 10, 2014 2:54 pm     Reply with quote

Hi mike,

I am starting to make good progress.... thats with the external 20mhz



Quote:
1) OK. The data from the arduino is confirmed as good. What I meant was, how did you know what the garbage received by the PIC was?
2) Comparing the HEX (or binary) codes for the sent and received data may give a clue as to what's going wrong.
3) The setup_oscillator controls the clock frequency when using the internal RC clock. Have you tried it?
4) So you're stuck with that.
5) Good, at least you know which tests to try for starters.

Mike.

1.I can see all the garbage in the debug monitor window in the CCS C compiler window.
2. YES done that and it works
3.I got everything working on the external clock, gonna try internal next.
4.Yes stuck with that, but not an issue I think.
5.Pick the 0x55 from reading the forums.

So my simple code so far to get data from arduino to PIC. The most important parts that i would like to highlight are the println("") line in the arduino(sends CR) which is needed for the PIC. Also i would highlight the 2400 baud setting for the ICD debugger.

The code so far:
Arduino


Code:
void setup() {
  Serial.begin(9600);      // open the serial port at 9600 bps:   
}

void loop() {

    Serial.print("Hello,world");// prints a Hello,world
Serial.println("");   prints CR
   
    delay(1000);            // delay 1000 milliseconds
 
 
}
 

PIC16F87
Code:

#include <16F87.H>
#device ICD=TRUE
#use delay (clock=20000000)
#fuses  HS, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP, NODEBUG
#use RS232(STREAM=Arduino,BAUD=9600, RCV=PIN_B2)
#use RS232(STREAM=DEBUGGER,BAUD=2400, XMIT=PIN_B3,RCV=PIN_B3)//2400 baud is the key for debugging
//======================================
main(void)
   { 

   char ch[20]; //More than enough room for all the characters

      while(1)
      {
       fgets(ch,Arduino);
      fputs(ch,DEBUGGER); 
      }

   }


Data received by PIC every 1 second....
Code:

Hello,world
Hello,world
Hello,world
Hello,world
Hello,world
...ect


The next step is to test the internal clock.....
Then I will try and figure out how to add a few serial interrupts/buffer incoming serial data... which I think the file EX_SISR.c will help.

Then hopefully a bit of manchester coding for wireless transmission....

I will have a million questions....

Thanks
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Feb 10, 2014 3:35 pm     Reply with quote

Quote:
#use RS232(STREAM=Arduino,BAUD=9600, RCV=PIN_B2)

This is a software UART. The hardware UART pins are B5 and B2.
When you only specify one of them in #use rs232(), the compiler will
make a software UART on that pin.

Quote:
while(1)
{
ch = getchar(Arduino);
fputc( ch, DEBUGGER);
printf("\n\r");
}

That's the reason why you could only receive the letter 'H' with this loop.
The software UART waits for the character, gets it, and then you spend
a lot of time transmitting characters. By the time you're done doing that,
you have missed the "ello" part.

Quote:
while(1)
{
fgets(ch,Arduino);
fputs(ch,DEBUGGER);
}

This one above works much better because fgets waits to get the whole string.

The best way would be to use ex_sisr.c and modify it to work with streams
Code:
#use RS232(STREAM=Arduino, BAUD=9600, UART1, ERRORS)
zombiePIC



Joined: 08 Feb 2014
Posts: 23

View user's profile Send private message

PostPosted: Tue Feb 11, 2014 10:32 am     Reply with quote

Thanks PCM
Quote:
#use RS232(STREAM=Arduino,BAUD=9600, RCV=PIN_B2)

This line of code works perfect....

I have modified the ex_sisr.c, only minimum changes were needed everything is working perfect.

I think the setting of 2400 baud for the ICD-u64 was where i got caught, and the hardware/software UART issue.
I am gonna see will things work off the internal clock now.

Thanks for all the help.
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