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

Output_High doesn't work properly

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



Joined: 18 Jan 2010
Posts: 17

View user's profile Send private message

Output_High doesn't work properly
PostPosted: Mon Jan 18, 2010 8:42 pm     Reply with quote

I'm using a PIC16F873A and am trying to output high Pins C2 & C3 at the same time to control two 5V fans. The pins are connected to npn transistors with a 1k resistor to the base pin of the transistors. The problem I'm having is that I can get one or the other too go high but not at the same time. When i try to output high both pins, the first pin goes low when the second pin goes high.

Everything else works fine except the portion that controls fans
Here is my program :Compilier 4.093

Code:

//***********************************************************************************************
//
//  Displays Temperature and Humidity in Humidor Cabinet.
//  Turns on the humidifier if the RH is too low.
//  Keeps Track of how long the Humidifier has run for
//  Keeps Track of Min and Max values for Temperature & Humidity
//
//***********************************************************************************************

#include <16F873A.h>
#device adc=8

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES HS                       //High Speed Oscillator > 4mhz
#FUSES NOPUT                    //No Power Up Timer
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NODEBUG                  //No Debug mode for ICD
#FUSES NOBROWNOUT               //No brownout reset
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD                    //No EE protection
#FUSES WRT_50%                  //Lower half of Program Memory is Write Protected

#use delay(clock=20M, oscillator)

#include "C:\LCD420.C"
#include "C:\sht75.c"


//RTC variables and program variables
#define XTAL_FREQUENCY  20000000
#define TIMER1_FREQUENCY (XTAL_FREQUENCY / 4)      // 1 clock tick = 1 instr. cycle = crystal frequency / 4

int32 Ticker;

int8 Humid_Read=0;
int8 Seconds=0;
int8 Minutes=0;
int8 Hours=0;
int8 Humid_Cycle=0;
int1 Humid_State=0;

float restemp, truehumid, max_temp, min_temp, max_RH, min_RH;

////////////////////////////////////////////////////////////////////////////////
//    Initialize RTC
////////////////////////////////////////////////////////////////////////////////
void Initialize_RTC(void)
{
  Ticker = TIMER1_FREQUENCY;                  // initialize clock counter to number of clocks per second
  setup_timer_1( T1_INTERNAL | T1_DIV_BY_1 ); // initialize 16-bit Timer1 to interrupt
                                              // exactly every 65536 clock cycles
                                              // (about 76 times per second)
  enable_interrupts( INT_TIMER1 );            // Start RTC
}

#int_TIMER1
void TIMER1_isr()
{
  Ticker -= 65536;                        //   Decrement ticker by clocks per interrupt
  if ( Ticker < 65536 )                   //   If second has expired
  {
    Ticker += TIMER1_FREQUENCY;          //   Increment ticker by clocks per second
    Humid_Read++;

    if (Humid_State == 1)
    {
      Seconds++;
    }
  }

  if(Humid_Read == 61)
  {
    Humid_Read=0;
  }
 
  if(Seconds == 60)
  {
    Minutes++;
    Seconds=0;
  }

   if(Minutes == 60)
   {
     Hours++;
     Minutes=0;
   }

}

////////////////////////////////////////////////////////////////////////////////
//
// Program for Displaying Temperature, Humidity
//
////////////////////////////////////////////////////////////////////////////////
void main()
{
  Initialize_RTC();
  enable_interrupts( GLOBAL );
  lcd_init();
  sht_init();

  min_temp=100;
  max_temp=0;
  min_RH=100;
  max_RH=0;

  sht_rd (restemp, truehumid);
 
  while(1)
  {
    if(Humid_Read == 60)                                                             
    {                                                                                 
      sht_rd (restemp, truehumid);  // Retrieves Temperature and RH every 60 secs             
    }                                                                                   

    lcd_gotoxy(1,1);
    printf(lcd_putc, "Temperature: %2.1f%cC ", restemp, 178);
    lcd_gotoxy(1,2);
    printf(lcd_putc, "R. Humidity: %2.1f %% ", truehumid);

//**********  Turn Humidfier on and off   **********

    if ((truehumid <= 70) && (Humid_State == 0))    //<=65     // Checks if RH is too low and if so turns on the humidifier
    {
      Output_High(PIN_A2);
      Humid_State = 1;
      Humid_Cycle++;
    }

    if ((truehumid >= 75) && (Humid_State == 1))  //>=67       // Checks if RH has reached optimum level and turns off the humidifier
    {
      Output_Low(PIN_A2);
      Humid_State = 0;
    }

//**********  Display Humidifier Information   **********

    lcd_gotoxy(1,3);
    printf(lcd_putc, "Cycle Cnt:%03d",Humid_Cycle);            // Displays How many times the Humidifier has turned on
    lcd_gotoxy(15,3);
    printf(lcd_putc,"%02U:%02U ",Hours,Minutes);               // Displays how long the Humidifier has ran for

//**********  Stores Max and Min Temperature and RH Values   **********

    if (restemp > max_temp)
    {
      max_temp = restemp;
    }

    if (truehumid > max_RH)
    {
      max_RH = truehumid;
    }
     
    if (restemp < min_temp)
    {
      min_temp = restemp;
    }

    if (truehumid < min_RH)
    {
      min_RH = truehumid;
    }

//**********  Turns on Fans to Circulate Air   **********

   Output_high(Pin_C2);       // Problem I'm having is that Pin C2 goes high and then after two seconds C2 goes low and Pin C3 goes high
   delay_ms(2000);             // If I take out this line Pin C2 never goes high????
   Output_high(PIN_C3);
   delay_ms(10000);
   Output_low(PIN_C2);
   Output_low(pin_C3);
   
//**********  Displays Max and Min Temperature and RH Values   **********
     
    lcd_gotoxy(1,4);
    printf(lcd_putc, "Max:%2.1f%c " , max_temp, 178);
    lcd_gotoxy(11,4);
    printf(lcd_putc, "Min:%2.1f%c ", min_temp, 178);

    delay_ms(5000);

    lcd_gotoxy(1,4);
    printf(lcd_putc, "Max:%2.1f%% ", max_RH);
    lcd_gotoxy(11,4);
    printf(lcd_putc, "Min:%2.1f%% ", min_RH);

    delay_ms(5000);

  }
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Jan 18, 2010 11:39 pm     Reply with quote

Quote:

I'm using a PIC16F873A and am trying to output high Pins C2 & C3 at the
same time to control two 5V fans. The pins are connected to npn
transistors with a 1k resistor to the base pin of the transistors.

It sounds like a classic Read-Modify-Write problem.

To trouble-shoot it, first strip the program down to a loop that only
contains code to turn the fans on/off, with delays in between statements.
Don't use any interrupts or talk to any external chips, etc. This will
isolate the problem so we're just testing the fan code and the fan circuits.
Ttelmah
Guest







PostPosted: Tue Jan 19, 2010 2:40 am     Reply with quote

You also might want to describe the circuit used to drive the fans.

I'd guess you have something like a power FET gate being driven directly from the processor pin, or possibly a transistor base with a potential divider. If the drive resistors are too low on the latter, the processor pin will never get to it's 'high' state, while with the former, it will eventually get there, but will take time....
A search in the forum, for 'read modify write', will find explanations of what is going on.

Best Wishes
danen



Joined: 18 Jan 2010
Posts: 17

View user's profile Send private message

PostPosted: Tue Jan 19, 2010 6:31 am     Reply with quote

Quote:

It sounds like a classic Read-Modify-Write problem.

To trouble-shoot it, first strip the program down to a loop that only
contains code to turn the fans on/off, with delays in between statements.
Don't use any interrupts or talk to any external chips, etc. This will
isolate the problem so we're just testing the fan code and the fan circuits.


Thank you for your reply
I've already done that and I still get the same result. I've even went as far as using another pic16f873a with two leds on a breadboard and no transistors and was getting the same thing.

As for the circuit, its a 1K resistor going from the pin to the base of a regular transistor (2N3904 fairchild). I have the fan hooked up to +5v and grounded to the collector pin and the emitter pin to ground. I'm guessing its missing some sort of statement somewhere. If I use pins C4 and C5 they both go high at the same time, however I have nothing connected to them at that point. I really don't feel like taking everything apart and resoldering.
Ttelmah
Guest







PostPosted: Tue Jan 19, 2010 9:39 am     Reply with quote

What trap protection have you got on the transistors?...
Remember that a fan, when switched off, _will_ develop voltage, both from the inductive spike as the driven coil goes off, and from it acting as a generator, with the inertia of the rotor. This voltage has to go somewhere. It sounds as if you have no protection, in which case the most common route, is back through the emitter/base junction, into the pin of the PIC. Often causes unexpected behaviour.

Best Wishes
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Tue Jan 19, 2010 6:16 pm     Reply with quote

Output_high() should work. Read up on the PIC classic read-modify-write problem.

You could also rewrite the code to OR the port with a mask to turn fans ON and AND the port with a mask to turn them OFF.
_________________
The search for better is endless. Instead simply find very good and get the job done.
danen



Joined: 18 Jan 2010
Posts: 17

View user's profile Send private message

PostPosted: Tue Jan 19, 2010 6:16 pm     Reply with quote

Quote:

What trap protection have you got on the transistors?...
Remember that a fan, when switched off, _will_ develop voltage, both from the inductive spike as the driven coil goes off, and from it acting as a generator, with the inertia of the rotor. This voltage has to go somewhere. It sounds as if you have no protection, in which case the most common route, is back through the emitter/base junction, into the pin of the PIC. Often causes unexpected behaviour.

Best Wishes


I don't have a diode connected however I also removed the fans and replaced them with an led on each pin and was getting the same result.
I'm thinking it is a Read Write Modify problem but have no idea how to make the pins all outputs and keep them that way. Would Timer1 in the RTC code affect the Port C in anyway? I'm not that knowledgeable in reading all the technical aspects of the datasheet.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Jan 19, 2010 6:35 pm     Reply with quote

Post the test program that you are using for the LED test.
Post a description of the external circuits on the LED pins.

If possible, post a schematic of the external circuits. Here is an
example of ASCII art:
http://www.ccsinfo.com/forum/viewtopic.php?t=40406&start=9
You have to compose the ASCII art in a text editor that uses a fixed font
and then copy and paste it into a forum post. You have to put it in a
code block. If you do this, then it will look good. If you try to enter
the ASCII art in the forum's text window, it will not look good.
danen



Joined: 18 Jan 2010
Posts: 17

View user's profile Send private message

PostPosted: Tue Jan 19, 2010 10:15 pm     Reply with quote

Code:

                                 +5V
                                  |   
                                  | 
                                 |>| Led with 220 ohm resistor
                1000 ohm       / 
Pic Port C2 --- /\/\/\/ ----- |  <==293904 Transistor
                               \
                                \
                                 GND

                       
                                 +5V
                                  |   
                                  | 
                                 |>| Led w/220 ohm resistor
                1000 ohm       / 
Pic Port C3 --- /\/\/\/ ----- |  <==293904 Transistor
                               \
                                \
                                 GND


The program I am using is the one posted above with the lcd, sensor and the fans removed with leds in the place of the fans. Sorry for the crappy schematic I hope you can understand it.

When I put a volt meter to the circuit, Pin C2 and C3 only puts out 1.5V, what would cause such a thing. I'm guessing thats why the pin doesn't stay high. Do I need a higher resistor or lower?

I made up a breadboard with just two leds on C2 and C3 with no tranistors and got it to work, so I'm assuming its no longer a programming issue rather something to do with the transistors
dyeatman



Joined: 06 Sep 2003
Posts: 1933
Location: Norman, OK

View user's profile Send private message

PostPosted: Tue Jan 19, 2010 10:29 pm     Reply with quote

It appears your problem is excessive current draw on the PIC port due to
insufficient gain. I use the ULN2803A in most of my designs where I drive
LEDs, relays and low current motors etc. It also has a built in snubber.
_________________
Google and Forum Search are some of your best tools!!!!
danen



Joined: 18 Jan 2010
Posts: 17

View user's profile Send private message

dyeatman I think you're right
PostPosted: Wed Jan 20, 2010 7:08 am     Reply with quote

Thanks for all your help everybody. The transistor is rated at 200 ma, I've got a fan hooked up to it that draws 240ma. That may be the culprit. So I took out the board and am going to use a relay to trigger the fans. I'll replace the transistors on the board with new ones and see what happens from there and use a diode as recommended. I will report back to see if that solves the problem.
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Wed Jan 20, 2010 7:19 am     Reply with quote

If those 1000 Ohm resistors are really there and are really 1000 Ohms, then the transistors, fans, or LEDs should make no difference to the PIC.

Something really weird is happening! It may seem silly, but when one of the output pins is high, measure the voltages at VDD and VCC of the PIC as well as each end of the 1000 Ohm resistor and see if something is not what you expect. If you have a scope do the same measurements with that to see if something is oscillating. Maybe you have a bad resistor or a short between traces of your board?
_________________
The search for better is endless. Instead simply find very good and get the job done.
danen



Joined: 18 Jan 2010
Posts: 17

View user's profile Send private message

PostPosted: Wed Jan 20, 2010 5:54 pm     Reply with quote

Quote:

If those 1000 Ohm resistors are really there and are really 1000 Ohms, then the transistors, fans, or LEDs should make no difference to the PIC.

Something really weird is happening! It may seem silly, but when one of the output pins is high, measure the voltages at VDD and VCC of the PIC as well as each end of the 1000 Ohm resistor and see if something is not what you expect. If you have a scope do the same measurements with that to see if something is oscillating. Maybe you have a bad resistor or a short between traces of your board?


I thought I had a 5v 2A regulated power supply but it was putting out 7 volts, so I have to wire in the 7805 voltage regulator. Second, between the pin C2 or C3 to ground was only putting out 1.5V. I don't have scope, so I don't have that option. The resistors do read 1K resistance with the voltage meter. I'll go over the board more carefully to ensure there isn't any wires shorting.

I think that because the fan is drawing more than 200ma it might be doing something to the transistor. I'm no electronics expert, I'm just glad
its not a programming issue.
danen



Joined: 18 Jan 2010
Posts: 17

View user's profile Send private message

Got it working
PostPosted: Wed Jan 27, 2010 6:18 pm     Reply with quote

I finally gave up and ripped out the transistors and replaced them, and switched to Port A.... it now works like a charm and is up and running. Thanks for all the help and input
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