|
|
View previous topic :: View next topic |
Author |
Message |
nv Guest
|
new to ccs - need help with code |
Posted: Fri Oct 05, 2007 7:58 pm |
|
|
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
|
|
Posted: Sat Oct 06, 2007 9:51 pm |
|
|
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
|
|
Posted: Mon Oct 08, 2007 6:01 am |
|
|
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
|
|
Posted: Mon Oct 08, 2007 9:29 am |
|
|
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
|
Tried all the above suggestions |
Posted: Mon Oct 08, 2007 11:49 am |
|
|
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
|
Tried all the above suggestions |
Posted: Mon Oct 08, 2007 12:00 pm |
|
|
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
|
|
Posted: Mon Oct 08, 2007 3:35 pm |
|
|
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
|
|
Posted: Tue Oct 09, 2007 8:49 am |
|
|
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 |
|
|
|
|
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
|