|
|
View previous topic :: View next topic |
Author |
Message |
badbrad
Joined: 17 Apr 2005 Posts: 1
|
help with CO evironmental monitoring system code |
Posted: Sun Apr 17, 2005 11:26 pm |
|
|
Hi, for a senior design project I am creating a CO environmental monitoring system. I do have some code written but I am not sure if it is correct. I am using the PIC C MCU compiler. I am trying to program the PIC16F877 to display the min, max, and mean of the air sample every 10 seconds. I was wondering if anyone has sample code for a 9 pin serial port RS232. The main code I need is code for a 9 pin serial port. If anyone can help me, email me at [email protected]. Here is the code i have so far.
Thanks,
Brad Bockrath
#if defined(__PCM__)
#include <16F877a.h>
#device *=16
#fuses HS,NOWDT,NOPROTECT,NOLVP,XT,PUT,NOWRT
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_B1,rcv=PIN_B0)
void main() {
int i, value, min, max, mean;
printf("Sampling:");
setup_adc_ports( ALL_ANALOG );
setup_adc( ADC_CLOCK_INTERNAL );
set_adc_channel( 0 );
while (TRUE)
{
do { //Takes 3 samples from pin A0
long int mean;
min = 0; //and takes the minimum value
max = 0; // takes maximum value
mean = 0; //takes mean value
for(i = 0; i <= 3; ++i) {
delay_ms(1000); //values for that 1000ms or 10s period
value = read_adc();
if(value < min)
min = value;
if(value > max)
max = value;
if(value = (max + min)/2)
mean = mean + value;
}
mean = mean/3;
printf("\n\rMin:%d MAX:%d Mean:%d", min, max, (int)mean);
}
} |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Apr 17, 2005 11:51 pm |
|
|
What output you are currently getting
from the program. Tell us what numbers are displayed in your
terminal window. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Apr 18, 2005 12:18 pm |
|
|
I see that Darren has moved this thread from the Code Library to
here, where it belongs. I hope you notice that it was moved.
Anyway, to answer your questions:
Your #fuses statement is wrong. You've got both HS and XT in there.
You should only have one oscillator setting, and since you're running
at 20 MHz, you should be using HS. Remove the XT.
Code: | #fuses HS,NOWDT,NOPROTECT,NOLVP,XT,PUT,NOWRT |
Here, you have an outer while() loop, and then you have the beginning
of a do-while loop, but you're missing the while() on the end.
You need to study how to use loop control code in C.
Code: | while (TRUE)
{
do
{ |
Here, you think you're doing 3 interations of the loop, but in fact you're
doing 4. The for loop will continue until the middle condition becomes
false. So it starts at 0, then goes 1, 2, 3. It's not false until i reaches 4.
But 0,1,2,3 is four passes through the loop. You need to use "< 3"
instead of "<= 3" for your test.
Code: | for(i = 0; i <= 3; ++i) |
There might be other problems. |
|
|
|
|
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
|