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

2 LM35 Temperature sensors, 1 motor, 2 switches and 2 relays

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



Joined: 04 Feb 2007
Posts: 8

View user's profile Send private message

2 LM35 Temperature sensors, 1 motor, 2 switches and 2 relays
PostPosted: Wed Jan 11, 2012 7:39 pm     Reply with quote

Hi,

I have some problems with this ( http://imageshack.us/f/717/schematicsq.png/ )circuit and code. I'll be thankful if you help.

When I power the circuit:
1- Interrupts do not work (I think it is because int_rb does not work for RB1 pin but what about the int_ext?)
2- I do not see stable voltages on LM35 pins; sometimes they give 250mV and after 2,5V, 0V, 10mV etc. and sometimes they get stable(they are on pin headers on schematics).
3- Relays get on and off randomly.
4- When it gets in the while loop to read adc_channel(0), it does not go back to read adc_channel(1). I mean it does not get out of the while loop of channel(1) Here is the adc code for that loop:
Code:

while(TRUE)
   {
   set_adc_channel(1); //DIŞ SENSÖR OKUNUYOR |Outside LM35 Reading
   delay_us(20);
   bilgi_dis=read_adc(); //data_outside
      while (bilgi_dis>79)
      {
      output_high(pin_a7);
      menfez_kapat();
      }
   
      while(bilgi_dis<79) //data_outside
      {
      set_adc_channel(0); //İÇ SENSÖR OKUNUYOR |Inside LM35 Reading
      delay_us(20);
      bilgi_ic=read_adc(); //data_inside
         if (bilgi_ic>79)
         {
         fan1_calis();
         fan2_calis();
         output_high(pin_a6);
         menfez_ac();
         }
         
         else if(bilgi_ic<79 && bilgi_ic>62)
         {
         fan1_calis();
         fan2_kapat();
         menfez_ac();
         }
         
         else if(bilgi_ic<62)
         {
         fan1_kapat();
         fan2_kapat();
         menfez_ac();
         }
       }
   }

5- LEDs on RA6 and RA7 pins do not turn off when they do not match the criteria (I may not have wrote the code for that, I am not sure).
6- When I write set_tris_x(), #use fast_io(x) and define the input/output pins, one of the LM35s give voltages in reverse (the more hot it gets, the less voltage it gives).

Here is the full code:
Code:

#include "16F88.h"
#device ADC=10
#fuses INTRC_IO,NOWDT,NOPROTECT,NOBROWNOUT,NOLVP,NOWRT,NODEBUG,NOCPD
#use delay (clock=8000000)
/*#use fast_io(a)
#use fast_io(b)*/

#int_ext
void ust_bumper() //Motor stops if bumper_switch_1  go high
   {
      output_low(pin_a4);
      output_low(pin_a3);
      output_low(pin_a2);     
      output_b(0x00);
   }
   
#int_RB     
void alt_bumper() //Motor stops if bumper_switch_2 go high
   {
      if(input(pin_b1))
      {
      output_low(pin_a4);
      output_low(pin_a3);
      output_low(pin_a2);
      }
      output_b(0x00);
   }


unsigned int32 bilgi_ic, bilgi_dis;  //data_inside, data_outside

void menfez_ac()   //run motor forward
      {
      output_high(pin_b6);
      output_high(pin_b5);
      output_low(pin_b4);
      }
     
void menfez_kapat() //run motor backwards
      {
      output_high(pin_b6);
      output_low(pin_b5);
      output_high(pin_b4);
      }
     
     
void fan1_calis() //relay1_on
      {
      output_high(pin_b2);
      }

void fan2_calis() //relay2_on
      {
      output_high(pin_b3);
      }
     
void fan1_kapat() //relay1_off
      {
      output_low(pin_b2);
      }
     
void fan2_kapat()  //relay2_off       
      {
      output_low(pin_b3);
      }
     
void main()
{
setup_oscillator(OSC_8MHZ);
setup_spi(SPI_SS_DISABLED);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_CCP1(CCP_OFF);

/*set_tris_a(0b00000011);
set_tris_b(0b00000011);*/
setup_adc(adc_clock_div_32);
setup_adc_ports(sAN0|sAN1);

ext_int_edge(L_TO_H);
enable_interrupts(INT_RB);
enable_interrupts(INT_EXT);
enable_interrupts(GLOBAL);
setup_adc_ports(sAN0|sAN1|VSS_VDD);

output_high(pin_b7); //LED to see if PIC is running
delay_ms(500);
output_low(pin_b7);
delay_ms(500);
output_high(pin_b7);

while(TRUE)
   {
   set_adc_channel(1); //DIŞ SENSÖR OKUNUYOR |Outside LM35 Reading
   delay_us(20);
   bilgi_dis=read_adc(); //data_outside
      while (bilgi_dis>79)
      {
      output_high(pin_a7);
      menfez_kapat();
      }
   
      while(bilgi_dis<79) //data_outside
      {
      set_adc_channel(0); //İÇ SENSÖR OKUNUYOR |Inside LM35 Reading
      delay_us(20);
      bilgi_ic=read_adc(); //data_inside
         if (bilgi_ic>79)
         {
         fan1_calis();
         fan2_calis();
         output_high(pin_a6);
         menfez_ac();
         }
         
         else if(bilgi_ic<79 && bilgi_ic>62)
         {
         fan1_calis();
         fan2_kapat();
         menfez_ac();
         }
         
         else if(bilgi_ic<62)
         {
         fan1_kapat();
         fan2_kapat();
         menfez_ac();
         }
       }
   }
}


Thanks in advance, any help will be appreciated!


Last edited by cer on Thu Jan 12, 2012 4:21 pm; edited 6 times in total
cer



Joined: 04 Feb 2007
Posts: 8

View user's profile Send private message

PostPosted: Wed Jan 11, 2012 8:02 pm     Reply with quote

btw it does not execute this code as i understood but i might be wrong. I thought so because it does not power the LED by output_high(pin_a7);

Code:
set_adc_channel(1); //DIŞ SENSÖR OKUNUYOR |Outside LM35 Reading
   delay_us(20);
   bilgi_dis=read_adc(); //data_outside
      while (bilgi_dis>79)
      {
      output_high(pin_a7);
      menfez_kapat();
      }
cer



Joined: 04 Feb 2007
Posts: 8

View user's profile Send private message

PostPosted: Thu Jan 12, 2012 7:13 am     Reply with quote

It reads only channel(0) and does not react on what happened on channel(1). How can i break the channel(0) loop?
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Thu Jan 12, 2012 8:05 am     Reply with quote

i could be wrong , but i was under the impression that you have to READ port B to clear the interrupt not write to it.

care to post the SCHEMATIC of this device ??
that would help ME to figure out what could be wrong.


Last edited by asmboy on Thu Jan 12, 2012 8:18 am; edited 1 time in total
cer



Joined: 04 Feb 2007
Posts: 8

View user's profile Send private message

PostPosted: Thu Jan 12, 2012 8:10 am     Reply with quote

asmboy schematics link is under the post. How do i clear the interrupt without writing in it? Is the poblem here: output_b(0x00);?
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Thu Jan 12, 2012 8:18 am     Reply with quote

try
byte junk;
junk=input_b();

ALSO
i suggest that the LM35's SHOULD have a weak pull DOWN resistor to ground
say 10k ohms -with a .1UF metal film CAPACITOR in paraLLEL


Last edited by asmboy on Thu Jan 12, 2012 8:29 am; edited 1 time in total
cer



Joined: 04 Feb 2007
Posts: 8

View user's profile Send private message

PostPosted: Thu Jan 12, 2012 8:20 am     Reply with quote

asmboy wrote:
try
byte junk;
junk=input_b();

Should this be in the interrupt function or a global one?
temtronic



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

View user's profile Send private message

PostPosted: Thu Jan 12, 2012 8:26 am     Reply with quote

some comments..

INT_RB().. only gets triggered when there's a change on portb.7,.6,.5, or .4 ( the high nibble) and you MUST read the port to clear it.

You only read the inside LM35 IF the outside LM35 is < 79 bits.

You are using set_tris....().. Not necessary, the compiler will do it all for you.

Same comment about 'fast_io'....let the compiler do this, it'll keep the LM35s cool !!

LM35s. Need a stable power source. Put a 10mfd cap at their Vin. Also put a small cap( .01, .1) on the output to the ADC pin. If you read the datasheet you'll see they will oscillate unless properly connected.


Relays. Be sure to use diodes on the coils! What is between the PIC and the relays? Transistors, IC, ?

What is the power supply? It should be at least 2 AMPS depending on the relays you use. At least 2 X the maximum draw for ALL relays being on at the same time.

Be sure to use good bypass/filtering caps near the PIC. Noise from the relays could reset of corrupt the PICs pins.

Be sure to use say 500r to 1k resistors for the LEDs.
cer



Joined: 04 Feb 2007
Posts: 8

View user's profile Send private message

PostPosted: Thu Jan 12, 2012 8:41 am     Reply with quote

@temtronics

How can I read the port to clear it?

Yes, I wrote it intentionally, thanks anyway.

I removed set_tris and fast_io on behalf of your comment.

Schematics link is on the first post. I am using 1N4007. There are BC547 between PIC and relays as you can see on schematics.

OK, I'll add filtering caps.

I do not use resistors for the LEDs, it works anyway. What kind of a problem can occur if I do not use resistors for the LEDs?

Thank you very much for your comments and waiting for further comments if it is OK for you Smile
temtronic



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

View user's profile Send private message

PostPosted: Thu Jan 12, 2012 8:57 am     Reply with quote

You must use current limiting resistors otherwise you WILL stress the PIC and eventually destroy it. While it might seem to work, over time, it will do 'weird' stuff, not run right even though the code is correct.

to read the port, please press F11 while your project is open. this will bring up the 'onscreen' help files of the compiler( mine is always open) giving you access to almost everything you need to know about CCS C and how to use it.The FAQ section should have detials about reading ports and of course look at some of the examples in the 'examples' folder to see how CCS does some coding.

hope this helps
cer



Joined: 04 Feb 2007
Posts: 8

View user's profile Send private message

PostPosted: Thu Jan 12, 2012 11:10 am     Reply with quote

temtronic,

Great comments! Thank you very much, these will totally help!
cer



Joined: 04 Feb 2007
Posts: 8

View user's profile Send private message

PostPosted: Thu Jan 12, 2012 4:27 pm     Reply with quote

Hi,

I want it to read adc_channel(1) when the conditions occur but when it is in channel(0), it does not read channel(1) even if the conditions occur. Should i use a "break;" or something at the end of the channel(0) loop?

P.S: I intentionally wrote that adc_channel(0) will be read if bilgi_dis<79.
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