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

Trouble Getting Started

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



Joined: 22 Oct 2006
Posts: 24

View user's profile Send private message

Trouble Getting Started
PostPosted: Sun Oct 22, 2006 1:33 pm     Reply with quote

Hi, I am completely new to CCS C, I've been using sourceboost, I just installed the demo version of the CCS PIC C compiler and I'm having trouble getting started. I have mplab and a pic start plus programmer, what settings do I need so that I can begin programming chips. I'll be using a 16F877a.

Any help will be greatly appreciated, I need this for a school project that I'm behind schedule on.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Oct 22, 2006 2:14 pm     Reply with quote

Quote:
I just installed the demo version of the CCS PIC C compiler.
I'll be using a 16F877a.


The CCS demo page doesn't say the 16F877A is supported by the demo:
http://www.ccsinfo.com/content.php?page=compdemo
It says:
Quote:

Only the Microchip 14 bit PIC16F877, PIC16C544 and 16 bit PIC18F458


The 'A' and the non-A versions of the 16F877 are different chips.
The 'A' version has comparators and it has a different programming
algorithm for the flash memory. The 'A' version is not just a simple
silicon "rev" of the non-A version (i.e., bug fixes). It's a different chip.
totalnoob



Joined: 22 Oct 2006
Posts: 24

View user's profile Send private message

PostPosted: Sun Oct 22, 2006 4:50 pm     Reply with quote

The demo came with a book I purchased, I didn't download it from CCS. I installed the demo and when I looked in the folder 'devices' the 16F877a.h file was there.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Oct 22, 2006 5:04 pm     Reply with quote

It's possible that you have an older version of the demo. I recall that
in earlier years, the demo did support the 16F877A.

I need to know if the demo that you installed is the Windows IDE
CCS compiler or if it's the command-line compiler. Normally, the
"demo" is the full IDE. It looks like this:
http://www.ccsinfo.com/images/content/C_aware_editor.gif

But your book might have only included the command-line compiler.
That version of the compiler uses MPLAB as the IDE.

Which version of the compiler do you have ? Does it look like the
one in the screen-shot posted above ?

Also, what's the name of the book ?
totalnoob



Joined: 22 Oct 2006
Posts: 24

View user's profile Send private message

PostPosted: Sun Oct 22, 2006 5:13 pm     Reply with quote

Yes it does look like the screen shot. The window is the same, with PCW C Compiler IDE. The book is called Embedded C Programming and the Microchip PIC by Barnett, Cox & O'Cull. The book came with a sample program to get two pics to communicate via I2C. I'm trying to recreate that program for a 16F877a instead of a 16F877. Here's the code for each pic.

Master:
#include <16F877.h>
#use delay(clock=10000000)
#fuses HS,WDT
//set up i2c peripheral and use hardware SSP
#use i2c(Master,sda=PIN_C4,scl=PIN_C3,RESTART_WDT,FORCE_HW)

#int_TIMER1
TIMER1_isr()
{ //communicate every 50 ms
i2c_start(); //set i2c to start condition
i2c_write(0xa0); //send address
i2c_write(input_b()); //send master data from port B bits
i2c_start(); //restart to change data direction
i2c_write(0xa1); //address with direction bit changed
output_d(~i2c_read(0)); //get slave data and display
i2c_stop(); //stop i2c activity
}

void main()
{
setup_counters(RTCC_INTERNAL,WDT_1152MS);
setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);
port_b_pullups(TRUE); //pull ups for input pins
enable_interrupts(INT_TIMER1);
enable_interrupts(global);
while(1)
{
//use WDT in case i2c 'hangs up' due to faulty slave
restart_wdt();
}
}

Slave:
#include <16F877.h>
#use delay(clock=10000000)
#fuses HS,NOWDT
#use i2c(SLAVE,sda=PIN_C4,scl=PIN_C3,address=0xa0)

enum {address,data};
int mode;

#int_SSP
SSP_isr()
{
int ch;
//check for valid char received. The interrupt occurs, but no
//valid character is received when setting up for the read
if (i2c_poll())
{
ch = i2c_read(); //get data received
//ch contains address on first transmission of the interchange
if (mode == address) mode = data; //so ignore and set for data byte
else //must be data byte
{
output_D(~ch); //output master data on LEDs
mode = address; //go back to waiting for address
}
}
//get to 'else' only if second command byte of intercahnge, so load
//i2c output register for read operation
else i2c_write(input_b()); //load for read
}


void main()
{
port_b_pullups(TRUE); //pullups on input bits
enable_interrupts(INT_SSP); //interrupt mode required
enable_interrupts(global);
mode = address; //set starting mode
while(1) //main does nothing
;
}


All I changed was the include file from 16F877 to 16F877a. Is there more that I need to change to implement this code on the 877a?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Oct 22, 2006 5:26 pm     Reply with quote

Quote:
All I changed was the include file from 16F877 to 16F877a.

Does the C program compile without errors when you do that ?

I recall that the various demos either worked with the 16F877
or the 16F877A, but not both.

The reason I'm making a big deal out of this, is because I want to
establish whether or not your version of the compiler will even work
with the 16F877A. There's no point in going further in this if you
can't generate code for the "A" chip.
totalnoob



Joined: 22 Oct 2006
Posts: 24

View user's profile Send private message

PostPosted: Sun Oct 22, 2006 6:12 pm     Reply with quote

Yes it compiled without error with the include 16F877A.h. When I created a new project for this code it asked me target device and I selected the PIC 16F877A. Then I copied the code from the book into the .c file and compiled it. The compiler didn't output a .asm file which mplab asks for, but I was able to take the .hex file from the compiler and program it to the PIC from mplab. However, the code doesn't work and I'm really not sure what I'm doing wrong because I'm very new to programming with PICs and I've never used I2C before.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Oct 22, 2006 6:26 pm     Reply with quote

You need to add NOLVP to the #fuses statement for both Master and slave.
Also, it wouldn't hurt if you added BROWNOUT and PUT. Example:

Code:
#fuses HS, NOWDT, BROWNOUT, PUT, NOLVP

The NOLVP fuse is necessary to prevent the PIC from locking up.

The BROWNOUT and PUT fuses will allow the PIC to start up
more reliably, when you first turn on the power.


With respect to the i2c Master and Slave, do you:

1. Have the two SDA pins connected together between the two PICs ?

2. Have the two SCL pins connected togeter between the two PICs ?

3. Have a pull-up resistor on the SDA signal and also on SCL ?
You can use 4.7K ohm resistors for this. A "pull-up" is a resistor
that connects between the signal and the Vdd voltage (+5v).

4. Do you have a ground wire connected between the two PIC boards ?
totalnoob



Joined: 22 Oct 2006
Posts: 24

View user's profile Send private message

PostPosted: Sun Oct 22, 2006 7:05 pm     Reply with quote

Ok, added all those fuses, the NOLVP, BROWNOUT, And PUT. Still doesn't work. The SDA pins are connected together, the SCL pins are connected together. I have 4.7K pull-up resistors on both lines. And both pics are connected to the same ground and source.


P.S.
One other thing I changed was the clock=20000000 instead of 10000000 because I'm using a 20 MHz crystal to clock my pics.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Oct 22, 2006 7:44 pm     Reply with quote

The comment for the Timer1 isr in your code says:
Code:
//communicate every 50 ms


Because your crystal is 20 MHz, to get a Timer1 interrupt every 50 ms
or so, you need to change your timer divisor to 4 (instead of 1). Example:
Code:

setup_timer_1(T1_INTERNAL | T1_DIV_BY_4);


Also, can you post your version of the compiler ?

You can find it at the top of the .LST file, which will be in your project
directory. The version will be a number such as 3.068, or 3.191, or
3.236. Something like that. If you post your version, I can check
if it will produce correct code for your program.
totalnoob



Joined: 22 Oct 2006
Posts: 24

View user's profile Send private message

PostPosted: Sun Oct 22, 2006 7:50 pm     Reply with quote

The version of my compiler is 3.170b.
totalnoob



Joined: 22 Oct 2006
Posts: 24

View user's profile Send private message

PostPosted: Sun Oct 22, 2006 8:04 pm     Reply with quote

Success!!!! Finally. I totally missed that timer change. Thanks a lot for your help. This is but a small step in my project, next I have to get this PIC to communicate with a motor controller via i2c to drive a robot. When that time comes I know who to ask for help. Very Happy
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