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

new to ccs - need help with code

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







new to ccs - need help with code
PostPosted: Fri Oct 05, 2007 7:58 pm     Reply with quote

Hi,
I'm totally new to this ccs c compiler and i'm trying to write some code to get a feel for it. I have the PICDEM HPC Explorer board and am trying to light up some LEDs and output via RS232. My code compiles fine and I don't think I have missed anything, however it deosn't seem to work. Could someone please let me know what i'm doing wrong. Here's the code:

#include <18F8722.h>
#include <stdio.h>
#include <math.h>
#fuses HS,NOWDT
#use delay(clock = 10000000)
#use rs232(baud = 9600, xmit = PIN_C6, rcv = PIN_C7)
#use standard_io(D)


int find_volume(int s1, int s2, int s3);
void toggle_result();

int length = 10;
int width = 5;
int height = 2;


void main()
{
int volume;
volume = find_volume(length,width,height);

printf("Length = %d\n",length);
set_tris_d(0);
output_high (PIN_D0);
delay_ms (1000);
printf("Width = %d\n",width);
output_high( PIN_D1);
delay_ms (1000);
printf("Height = %d\n\n",height);
output_high (PIN_D2);
delay_ms (1000);
printf("Volume = %d\n",volume);
toggle_result();
setup_counters (RTCC_INTERNAL,RTCC_DIV_2);

}

int find_volume(int s1, int s2, int s3)
{
int result;
result = s1*s2*s3;
return(result);
}

void toggle_result()
{

while(true)
{
output_low (PIN_D0);
output_low (PIN_D1);
output_low (PIN_D2);
delay_ms(50);
output_high (PIN_D0);
output_high (PIN_D1);
output_high (PIN_D2);

}

}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Oct 06, 2007 9:51 pm     Reply with quote

Quote:
however it doesn't seem to work.

You should tell us exactly which parts of the program don't work,
and which parts do work.


Anyway at a minimum, you need to:

1. Add NOLVP to the #fuses statement.

2. Add a 2nd delay statement after the group of three output_high()
statements in your toggle loop. (In other words, at the end of the loop).
Also, 50 ms is a little short. It might be better to use 100 ms for both delays.
rina_1220



Joined: 02 Oct 2007
Posts: 20

View user's profile Send private message

PostPosted: Mon Oct 08, 2007 6:01 am     Reply with quote

well, I've never worked with PIC18F8722, but as i was observing your code, besides what was said about the NOLVP fuse, you should take a look at these parts:

Tell me what's working and what's not. Is your serial communication working? If so, forget my comment. But if it is not, then you should specify other configurations, such as parity and # of bits.
Code:

#use rs232(baud = 9600, xmit = PIN_C6, rcv = PIN_C7)


Code:
void main()
{
   ...
   toggle_result(); //when you call this function, as you should know, it will never end. So, the next line of your code will never be executed. I'm not sure what this function really does, but if you want it to be executed, you should change its place in the code
   setup_counters (RTCC_INTERNAL,RTCC_DIV_2);

}


void toggle_result()
{

   while(true)
   {
      ...
      delay_ms(50);  //well, when you put such a small delay, you wont see any difference with your eyes. Try a larger delay, for example, 500ms
      ...
   }
}
rnielsen



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

PostPosted: Mon Oct 08, 2007 9:29 am     Reply with quote

One, quick, item I noticed is this:

Quote:
int find_volume(int s1, int s2, int s3)
{
int result;
result = s1*s2*s3;
return(result);
}

Declaring a variable as an 'int' defaults to an 8-bit value. You are multiplying three 8-bit integers and storing them in another 8-bit integer. Unless s1, s2 & s3 are very values your result will be larger than 'result' can hold. Consider making 'result' an int16 or int32 depending on how big the result could be.

Ronald
cypher



Joined: 05 Oct 2007
Posts: 31

View user's profile Send private message

Tried all the above suggestions
PostPosted: Mon Oct 08, 2007 11:49 am     Reply with quote

Thank you all for your reply. I have made all the above changes and the code still doesn't work. I also have the configuration settings correct for the demo board as described here on page 9 of the pdf:

http://ww1.microchip.com/downloads/en/DeviceDoc/TrblshtngHPCexplorerdemboard.pdf

So nothing seems to be working. No LEDs light up or any sort of output on the RS232. I have tried everything. For the LEDs, I have defined all of PORT D as a output by using set_tris_d(0) and for the RS232 I have added the parity as N and 8 bits. but still no luck. What am I doing wrong? Is it the way I'm programming the PIC? I'm using the MPLAB ICD 2 debugger/programmer and when I program the PIC, the programmer blinks the busy light so i'm pretty sure its programmed succesfully and plus I have programming confirmation in the output window.

I'm going to try running some examples for the board and see if they work, any other suggestions will be highly appreciated.
cypher



Joined: 05 Oct 2007
Posts: 31

View user's profile Send private message

Tried all the above suggestions
PostPosted: Mon Oct 08, 2007 12:00 pm     Reply with quote

Thank you all for your reply. I have made all the above changes and the code still doesn't work. I also have the configuration settings correct for the demo board as described here on page 9 of the pdf:

http://ww1.microchip.com/downloads/en/DeviceDoc/TrblshtngHPCexplorerdemboard.pdf

So nothing seems to be working. No LEDs light up or any sort of output on the RS232. I have tried everything. For the LEDs, I have defined all of PORT D as a output by using set_tris_d(0) and for the RS232 I have added the parity as N and 8 bits. but still no luck. What am I doing wrong? Is it the way I'm programming the PIC? I'm using the MPLAB ICD 2 debugger/programmer and when I program the PIC, the programmer blinks the busy light so i'm pretty sure its programmed succesfully and plus I have programming confirmation in the output window.

I'm going to try running some examples for the board and see if they work, any other suggestions will be highly appreciated.
jecottrell



Joined: 16 Jan 2005
Posts: 559
Location: Tucson, AZ

View user's profile Send private message

PostPosted: Mon Oct 08, 2007 3:35 pm     Reply with quote

Write THE MOST BASIC PROGRAM to blink the LEDs as possible as a first test. I will give you a hint. This won't blink the LEDs:

Code:
 while(true)
{
  output_low (PIN_D0);
  output_low (PIN_D1);
  output_low (PIN_D2);
  delay_ms(50);
  output_high (PIN_D0);
  output_high (PIN_D1);
  output_high (PIN_D2);
}


Look at the loop and figure out what it is doing.

Add NOLVP to your fuses.

After you write your simplest program, post it here using the CODE feature and someone will help you.

Good luck,

John


BTW: (hint)

Quote:
delay_ms(50); //well, when you put such a small delay, you wont see any difference with your eyes. Try a larger delay, for example, 500ms


Won't fix it.
rnielsen



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

PostPosted: Tue Oct 09, 2007 8:49 am     Reply with quote

Quote:
while(true)
{
output_low (PIN_D0);
output_low (PIN_D1);
output_low (PIN_D2);
delay_ms(500);
output_high (PIN_D0);
output_high (PIN_D1);
output_high (PIN_D2);
}

Even using this will not allow you to see the LED change properly because the instant the outputs go high they will go low again. Try:

Code:
while(true)
{
  output_low (PIN_D0);
  output_low (PIN_D1);
  output_low (PIN_D2);
  delay_ms(500);
  output_high (PIN_D0);
  output_high (PIN_D1);
  output_high (PIN_D2);
  delay_ms(500);
}


This will make it easier to see. Your code just might be working and you can't see things changing because it's so fast.

Ronald
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