|
|
View previous topic :: View next topic |
Author |
Message |
sanddune008
Joined: 23 Oct 2008 Posts: 38
|
Is my I/O connected to LED sinking or sourcing current ? |
Posted: Wed Nov 05, 2008 6:15 am |
|
|
Hi All,
I am new to PIC....I have been trying out some simple programs.
1. I have connected switch to PIN BO/int.
2. Led is connect to Vcc thru a resistor and to PIN B2. PIN B2 in sink mode.
3. Connecting an external crystal of 4 MHz.
Following is my code for blinking LED when switch is pressed.....It doesn't work. I don't know what's wrong with the code.
Code: | #INT_EXT
void Ext_ISR (void)
{
Bell_Trigger_ISR();
}
void main(void)
{
disable_interrupts(GLOBAL); // all interrupts OFF
disable_interrupts( INT_EXT ); // mandatory if you want
ext_int_edge(0, H_TO_L);
enable_interrupts(INT_EXT);
enable_interrupts(GLOBAL); // mandatory to start it all
set_tris_A(0x0f);
set_tris_B(0x00);
while(1)
{
if(BellOn)
{
RED_LED_ON;//added for debuggin
delay_ms(50);//added for debuggin
RED_LED_OFF;//added for debugging
BellOn = FALSE;
enable_interrupts(INT_EXT);
}
}
void Bell_Trigger_ISR(void)
{
disable_interrupts( INT_EXT );
clear_interrupt(INT_EXT);
//switch is pressed
BellOn = TRUE;
} |
header:
Code: |
#fuses XT,NOWDT,NOPROTECT
#use delay(clock=4000000)
//#use rs232(baud=9600, xmit=PIN_A3, rcv=PIN_A2)
#define RED_LED_ON output_low(RED_LED)
#define RED_LED_OFF output_high(RED_LED) |
Thanks in advance
Last edited by sanddune008 on Wed Nov 05, 2008 6:34 am; edited 1 time in total |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Wed Nov 05, 2008 6:18 am |
|
|
set_tris_A(0x0f);
set_tris_B(0x00);
You are configuring B0 as output! |
|
|
sanddune008
Joined: 23 Oct 2008 Posts: 38
|
Is there a way to know whether my I/O connected to LED is si |
Posted: Wed Nov 05, 2008 6:28 am |
|
|
Hi,
I had mistyped the code.
I replace following line with this:
set_tris_B(0x01);
It doesn't work....
Is there a way to know whether my I/O connected to LED is sinking or sourcing current.
I have connected LED in sinking mode.......Is this causing the problem.
thanks |
|
|
asmallri
Joined: 12 Aug 2004 Posts: 1636 Location: Perth, Australia
|
Re: Is there a way to know whether my I/O connected to LED i |
Posted: Wed Nov 05, 2008 10:51 am |
|
|
sanddune008 wrote: | It doesn't work.... |
You have not provided enough information to tell help. Your code is not an operation code fragment. So how can we point to the problem?
How is RED_LED defined?
You do not need to clear the interrupt flag in the ISR, the CCS compiler will do that or you.
I am not sure you will see a 50ms flash. Try selling it to 200ms.
Prove you can flash the LED first without using interrupts. _________________ Regards, Andrew
http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!! |
|
|
dyeatman
Joined: 06 Sep 2003 Posts: 1939 Location: Norman, OK
|
|
Posted: Wed Nov 05, 2008 1:17 pm |
|
|
Get rid of the TRIS statements entirely and let the compiler control the the I/O. Your TRIS statement is STILL setting B2 as an output!
Unless your application is EXTREMELY time critical you should never need to use TRIS statements at all. |
|
|
sanddune008
Joined: 23 Oct 2008 Posts: 38
|
|
Posted: Thu Nov 06, 2008 7:45 am |
|
|
hi,
Ok let me explain in detail
---------------------------------------------------------------------------
following is the RED_LED_ON definition
#define RED_LED PIN_B2 // (output) Red LED (low true)
#define IR_LED PIN_B3 // (output) Infrared LED (low true)
// Macros to simplify I/O operations
#define RED_LED_ON output_low(RED_LED)
#define RED_LED_OFF output_high(RED_LED)
-------------------------------------------------------------------------------
I am using PIC16F84A, have connected switch to the BO PIN/EXT INT.
dyeatman wrote: | Get rid of the TRIS statements entirely and let the compiler control the the I/O. Your TRIS statement is STILL setting B2 as an output!
Unless your application is EXTREMELY time critical you should never need to use TRIS statements at all. |
Without TRIS statements how do i set the direction of the Pin?
if possible how?
Led connection
I have connected led to 5V through current limiting resistor to PIC PIN_B2.
is this ok?
I checked the pin PIN_B2 it seems to be always at 5V....even after triggering the switch. is my PIC dead?
I would like to read good documentation for enabling and disabling fuses and the selecting external oscillator.
Is my crystal configuration in the above code correct?
thanks |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Thu Nov 06, 2008 8:14 am |
|
|
Comment out the if (BellOn) statement
You need to add a delay after turning the LED off otherwise it will turn back on imediatelty. 50ms is also toooo fast, try 1 second (1000ms).
Code: |
// if(BellOn)
{
RED_LED_ON;//added for debuggin
delay_ms(1000);//added for debuggin
RED_LED_OFF;//added for debugging
delay_ms(1000);//added for debuggin
BellOn = FALSE;
enable_interrupts(INT_EXT);
}
|
Does the LED flash ? |
|
|
Ttelmah Guest
|
|
Posted: Thu Nov 06, 2008 8:23 am |
|
|
The compiler sets pin directions _for_ you, in normal use.
It has three 'modes' of operation, controlled by the keywords:
#use fast_io
#use standard_io
#use fixed_io
If you select the first, the compiler leaves it to _you_ to select directions.
If you select the last, the compiler expects to be 'told' what direction you want (in the statement), but then sets it to this for you.
The second, is the default. With this, if you 'input' a value from a pin, it switches the pin to input mode, and if you output a value, it switches the pin to output mode. This is how the compiler always wakes up, and unless you have need to control the TRIS yourself for something special, it works well.
So, your existing TRIS statements are redundant. The compiler will be changing the pins mode automatically anyway...
How big is the current limiting resistor?.
Your crystal setting is correct, _provided_ you have a 4MHz parallel cut crystal fitted between the two oscillator pins (15 & 16), and a capacitor to give the correct loading present from each pin to ground. Whole thing built close to the pins, with as little stray capacitance as possible.
Fuses, are only changeable when the chip is programmed. Documentation on what they 'do', is the Microchip data sheet, and what ones are available on your chip. There is a file called 'fuses.txt', with your compler, which gives the CCS 'names' for fuses and their functions.
The 16F84, _only_ has an 'external oscillator'. Unlike many more modern chips, that offer an internal oscillator as an option, the 84, can't do this.
Best Wishes |
|
|
sanddune008
Joined: 23 Oct 2008 Posts: 38
|
|
Posted: Thu Nov 06, 2008 9:29 am |
|
|
Ttelmah wrote: | The compiler sets pin directions _for_ you, in normal use.
It has three 'modes' of operation, controlled by the keywords:
#use fast_io
#use standard_io
#use fixed_io
If you select the first, the compiler leaves it to _you_ to select directions.
If you select the last, the compiler expects to be 'told' what direction you want (in the statement), but then sets it to this for you.
The second, is the default. With this, if you 'input' a value from a pin, it switches the pin to input mode, and if you output a value, it switches the pin to output mode. This is how the compiler always wakes up, and unless you have need to control the TRIS yourself for something special, it works well.
So, your existing TRIS statements are redundant. The compiler will be changing the pins mode automatically anyway...
How big is the current limiting resistor?.
Your crystal setting is correct, _provided_ you have a 4MHz parallel cut crystal fitted between the two oscillator pins (15 & 16), and a capacitor to give the correct loading present from each pin to ground. Whole thing built close to the pins, with as little stray capacitance as possible.
Fuses, are only changeable when the chip is programmed. Documentation on what they 'do', is the Microchip data sheet, and what ones are available on your chip. There is a file called 'fuses.txt', with your compler, which gives the CCS 'names' for fuses and their functions.
The 16F84, _only_ has an 'external oscillator'. Unlike many more modern chips, that offer an internal oscillator as an option, the 84, can't do this.
Best Wishes |
Thanks for the wonderful explanation....
I have connected a 330 ohm resistor....
observed behaviour.
if i comment out as below: led glows
Code: | while(1)
{
output_low(PIN_C2); //Switch on led
delay_ms(1000);
//output_high(PIN_C2); //Switch on led
//delay_ms(1000);
} |
but with this(code below) Led doen't glow at all
Code: | while(1)
{
output_low(PIN_C2); //Switch on led
delay_ms(1000);
output_high(PIN_C2); //Switch on led
delay_ms(1000);
} |
frustrated with this simple code..... |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Nov 06, 2008 6:47 pm |
|
|
Quote: | I am using PIC16F84A.
while(1)
{
output_low(PIN_C2); //Switch on led
delay_ms(1000);
output_high(PIN_C2); //Switch on led
delay_ms(1000);
} |
The 16F84A doesn't have a pin C2. It only has ports A and B.
Here is a test program for the 16F84A. It will blink an LED on pin B0.
Code: |
#include <16F84A.h>
#fuses XT, NOWDT, NOPROTECT, PUT
#use delay(clock=4000000)
//=======================================
void main(void)
{
while(1)
{
output_high(PIN_B0);
delay_ms(500);
output_low(PIN_B0);
delay_ms(500);
}
} |
|
|
|
sanddune008
Joined: 23 Oct 2008 Posts: 38
|
|
Posted: Fri Nov 07, 2008 3:56 am |
|
|
PCM programmer wrote: | Quote: | I am using PIC16F84A.
while(1)
{
output_low(PIN_C2); //Switch on led
delay_ms(1000);
output_high(PIN_C2); //Switch on led
delay_ms(1000);
} |
The 16F84A doesn't have a pin C2. It only has ports A and B.
Here is a test program for the 16F84A. It will blink an LED on pin B0.
Code: |
#include <16F84A.h>
#fuses XT, NOWDT, NOPROTECT, PUT
#use delay(clock=4000000)
//=======================================
void main(void)
{
while(1)
{
output_high(PIN_B0);
delay_ms(500);
output_low(PIN_B0);
delay_ms(500);
}
} |
|
Led glows with following line commented
while(1)
{
//output_high(PIN_B0);
//delay_ms(500);
output_low(PIN_B0);
delay_ms(500);
}
Led doesn't glow with following code
while(1)
{
output_high(PIN_B0);
delay_ms(500);
output_low(PIN_B0);
delay_ms(500);
}
With the above code the voltage at PIN_BO is always at 5v,
generally it toggle isn't?
what may be the reason?something wrong with the circuit?
or the micro controller is dead.
really frustrated with this simple code........
Thanks for all the help.......... |
|
|
asmallri
Joined: 12 Aug 2004 Posts: 1636 Location: Perth, Australia
|
|
Posted: Fri Nov 07, 2008 9:12 am |
|
|
sanddune008 wrote: |
2. Led is connect to Vcc thru a resistor and to PIN B2. PIN B2 in sink mode. |
PCM programmer wrote: |
Here is a test program for the 16F84A. It will blink an LED on pin B0.
Code: |
#include <16F84A.h>
#fuses XT, NOWDT, NOPROTECT, PUT
#use delay(clock=4000000)
//=======================================
void main(void)
{
while(1)
{
output_high(PIN_B0);
delay_ms(500);
output_low(PIN_B0);
delay_ms(500);
}
} |
|
Note that the test program listed assumed the LED is on B0. If your LED is on B2 you need to change the code to:
Code: |
#include <16F84A.h>
#fuses XT, NOWDT, NOPROTECT, PUT
#use delay(clock=4000000)
//=======================================
void main(void)
{
while(1)
{
output_high(PIN_B2);
delay_ms(500);
output_low(PIN_B2);
delay_ms(500);
}
} |
Personally do not let the compiler set the TRIS automatically. I always use FASTIO and manually set tris for my application. _________________ Regards, Andrew
http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!! |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Nov 07, 2008 10:34 am |
|
|
Right, but for newbies TRIS is confusing. He can't even make that
program work. I've never seen CCS screw up the automatic TRIS on
output_low() and output_high(). If they did, it would be extremely rare. |
|
|
dyeatman
Joined: 06 Sep 2003 Posts: 1939 Location: Norman, OK
|
|
Posted: Fri Nov 07, 2008 1:11 pm |
|
|
In well over 60 projects now I have never personally encountered a case where TRIS was set wrong by the compiler on either the 16 of 18 series. In fact, even though I use interrupts extensively and have had a few time critical applications, I have only one time ended up needing to set the TRIS manually so I guess it's, in some cases, a matter of preference.
However, as an instructor I agree with PCM for any "newbie" project they need to let the compiler do most of the work if possible just to keep things simple until they learn the more advanced concepts. |
|
|
|
|
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
|