|
|
View previous topic :: View next topic |
Author |
Message |
fastlink30
Joined: 14 May 2007 Posts: 33
|
output_low() problem? |
Posted: Thu Jan 24, 2008 9:18 am |
|
|
ccs 4.057 on pic16f877 16mhz
have problem if i try this code, the pin ports B & D are all connected to led for test purpose.
when pic run only the last pin of each port (b0/d7) are ON, if i access directly the rigister all the things goes well, compiler bug??
Code: |
#include <16F877.h>
#device adc=8
#use delay(clock=16000000)
#fuses NOWDT, HS, NOPUT, NOPROTECT, BROWNOUT, NOLVP, NOCPD, NOWRT, NODEBUG
#use rs232(baud=38400,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
#use fast_io(A)
#use fast_io(B)
#use fast_io(C)
#use fast_io(D)
#use fast_io(E)
#byte PORTA_A=5
#byte PORTA_B=6
#byte PORTA_C=7
#byte PORTA_D=8
#byte PORTA_E=9
#zero_ram
void main()
{
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_spi(FALSE);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_psp(PSP_DISABLED);
set_tris_a(0b00000000);
set_tris_b(0b00000000);
set_tris_c(0b10000000);
set_tris_d(0b00000000);
set_tris_e(0b00000000);
port_b_pullups(false);
while (1) {
delay_ms(100);
output_low(PIN_D0);
output_low(PIN_D1);
output_low(PIN_D2);
output_low(PIN_D3);
output_low(PIN_D4);
output_low(PIN_D5);
output_low(PIN_D6);
output_low(PIN_D7);
//porta_b=0;
output_low(PIN_B0);
output_low(PIN_B1);
output_low(PIN_B2);
output_low(PIN_B3);
output_low(PIN_B4);
output_low(PIN_B5);
output_low(PIN_B6);
output_low(PIN_B7);
//printf(lcd_putc,"test");
delay_ms(100);
output_high(PIN_D0);
output_high(PIN_D1);
output_high(PIN_D2);
output_high(PIN_D3);
output_high(PIN_D4);
output_high(PIN_D5);
output_high(PIN_D6);
output_high(PIN_D7);
//porta_b=0xff;
output_high(PIN_B0);
output_high(PIN_B1);
output_high(PIN_B2);
output_high(PIN_B3);
output_high(PIN_B4);
output_high(PIN_B5);
output_high(PIN_B6);
output_high(PIN_B7);
//printf(lcd_putc,"test2");
}
}
|
|
|
|
Ttelmah Guest
|
|
Posted: Thu Jan 24, 2008 9:26 am |
|
|
Almost certainly, the PIC RMW problem. Do a search here on this.
This is a _hardware_limitation of the chip, when driving pins with a significant load.What resistors are you using on the LED's?.
Best Wishes |
|
|
fastlink30
Joined: 14 May 2007 Posts: 33
|
|
Posted: Thu Jan 24, 2008 9:38 am |
|
|
i use led only for test, originally they was connected with lcd and doing the same... (i also tryed with proteus and the result is the same) |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Thu Jan 24, 2008 9:59 am |
|
|
Ttelmah is talking about the Read Modify Write problem with PIC chips. A search should turn up lots of threads on it. The solution is not to make successive bit changes on the same port. You could:
1) ping-pong your bit setting
Code: |
output_low(PIN_D0);
output_low(PIN_B0);
output_low(PIN_D1);
output_low(PIN_B1);
output_low(PIN_D2);
output_low(PIN_B2);
output_low(PIN_D3);
output_low(PIN_B3);
output_low(PIN_D4);
Etc... |
2) put a short delay between bit sets
Code: | output_low(PIN_D0);
delay_cycles(1);
output_low(PIN_D1);
delay_cycles(1);
output_low(PIN_D2);
delay_cycles(1);
output_low(PIN_D3);
delay_cycles(1);
output_low(PIN_D4);
Etc... |
3) write the whole port at once using the output_X() command _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
fastlink30
Joined: 14 May 2007 Posts: 33
|
thanks |
Posted: Thu Jan 24, 2008 11:03 am |
|
|
thanks for the reply!
delay_cycles() not functioning, but output_x() is ok! |
|
|
Ttelmah Guest
|
|
Posted: Thu Jan 24, 2008 11:11 am |
|
|
If delay_cycles, does not fix it, then it really 'screams' that the pins are not actually pulling low, Stick a voltmeter on one of the lines, and see what voltage it actually gets down to. It _must_ get below 0.8v, to be 'seen' as a low, on the 'read' that occurs whenever you output a bit. I'd expect the delay, to fix the LCD (on this the load should be small), but on the LED's, I suspect you have not got enough resistance, and hence the lines are _never_ actually getting to the 'low' state.
Best Wishes |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Jan 24, 2008 11:14 am |
|
|
The delay_cycles() method would only help if the RMW problem was
caused by a capacitive load on the pin that caused a delay in the
changing of the pin state. If the problem is due to an LED diode
loading down the voltage on the pin, then you need to write the port
as a complete byte.
Do you have a resistor in series with each LED ? |
|
|
fastlink30
Joined: 14 May 2007 Posts: 33
|
|
Posted: Thu Jan 24, 2008 2:27 pm |
|
|
|
|
|
fastlink30
Joined: 14 May 2007 Posts: 33
|
|
Posted: Thu Jan 24, 2008 2:51 pm |
|
|
for my simplicity the signal for the lcd are on the same port, but in reality some are on port d & some on port b, this make me impossible use the output_x() function |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Jan 24, 2008 4:13 pm |
|
|
Don't connect LEDs directly to the LCD data bus and other signals.
If you must use LEDs, then put a buffer chip in between the LEDs
and the data bus. Isolate the LEDs from the data bus so they don't
put a load on it. |
|
|
Ttelmah Guest
|
|
Posted: Fri Jan 25, 2008 4:13 am |
|
|
To expand on what PCM programmer is saying, LED's, have a 'forward voltage' 'Vf' (depends on what colour they are), and at that voltage, behave almost like a zener diode. So (for instance), a green LED, will typically have a Vf of about 3.4v. If you apply less than this voltage to the LED, it won't light, but apply more, and the voltage across the LED, will hardly rise (depending on internal resistances), and the current will shoot up. The only reason your circuit is not going 'bang', is that the PIC itself, current limits it's output pins at about 25mA. So, when you pull the pin 'low' on the PIC, what happens, is that it drops to about 1.6v (5v - 3.4v). Whenever you change a _single_ output pin on a PIC, the port is 'read' (as an input), and then the bit you want to change, is altered, and the value of the whole port is written 'back'. In your case, none of the pins can _ever_ 'read' as low (since they won't go below 1.6v), so will always be read as a logic '1'.
For a green LED, you want current limiting resistors of typically perhaps 150R to 270R on each line.
I'll bet your LED's are very bright when on, and if you turn them all on, you risk damaging the PIC. As it stands, you have 16 LED's, each potentially being driven with 25mA, a total of 400mA 'worst case'. The PIC you are using, has an absolute 'maximum' current limit into the Vss pin, of 300mA. Bang.....
Your circuit _needs_ changing.
Best Wishes |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Fri Jan 25, 2008 8:33 am |
|
|
He has those resistor packs RP1 & RP2 in series with the LEDS. As long as they have reasonable resistor values they should work fine. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
mskala
Joined: 06 Mar 2007 Posts: 100 Location: Massachusetts, USA
|
|
Posted: Fri Jan 25, 2008 8:37 am |
|
|
I can see in his schematic he already has current limiting resistors. If the value chosen limits the current to a value safely below the max current capacity of the PIC pin, then what you're saying can't be the problem. |
|
|
Ttelmah Guest
|
|
Posted: Fri Jan 25, 2008 8:50 am |
|
|
Depends what the resistors are.
He was asked, but has given no values. My guess from what is happening, would be that the value is much too low.
Also, limiting to the pin current rating of the PIC, _is not_ safe. The PIC _won't_ pull it's pin down to a voltage it sees as a 'low', at the limit value, and you can still overload the whole chip. The manufacturers only specify the 'output low' voltage up to 7mA, beyond this, you are a 'test pilot', with regards to what voltage is really given...
His circuit _is_ displaying exactly this problem.
Best Wishes |
|
|
fastlink30
Joined: 14 May 2007 Posts: 33
|
|
Posted: Fri Jan 25, 2008 9:14 am |
|
|
the resistor value, in that case is not important, proteus (i think) adapt the value on the max current of the pin connected.
if i disconnect the led from the circuit, the problem remain the same. later i give time to solve this problem....
i start thinking now the problem is in my code...
Last edited by fastlink30 on Fri Jan 25, 2008 11:51 am; edited 1 time in total |
|
|
|
|
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
|