View previous topic :: View next topic |
Author |
Message |
scaven92
Joined: 06 Oct 2006 Posts: 44
|
|
Posted: Fri Oct 09, 2009 5:58 pm |
|
|
I have the following circuit, one for each of the following pins: A0, A1, A2, A3, A4, A5, C1 (7 total LEDs).
I also have pin C2 used for to turn on the 5V supply for all of the LED's.
Code: |
91ohm Led 2N7002
+5v ---/\/\/\/-------|>|-------- \__/----|> GND
|
| 10k
o -------/\/\/\/---|> GND
|
PIC PORT
|
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Oct 09, 2009 9:28 pm |
|
|
Quote: | void main()
{
int i, j = 0;
output_bit(LED_DRIVE, 1);
enable_led(1, 1, 0);
delay_ms(5000);
enable_led(0, 1, 0);
delay_ms(2000);
output_bit(LED_DRIVE, 1);
enable_led(1, 1, 0);
while(1);
} |
Add a delay to each enable_led() call, as shown above in bold.
Quote: |
void enable_led( int value, float time_ms, int number)
|
Also, the delay should not be a float. Change it to an 'int'. |
|
|
scaven92
Joined: 06 Oct 2006 Posts: 44
|
|
Posted: Mon Oct 12, 2009 10:33 am |
|
|
I tried the delay and it acts the same, the two leds connected to A3 and A4 blink for a very belief moment while all the other leds come on.
I am lost... |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Oct 12, 2009 10:42 am |
|
|
To prove if it's a Read-Modify-Write problem, try setting up a shadow
register for Port A. This would be just an 'int8' variable, called
PortA_Shadow or something like that. Then do all your bit operations
on that variable. After doing a bit operation on the shadow register,
then write the entire shadow register to Port A in one operation, by
using the output_a() function.
This means that the only way you will write to Port A will be with the
output_a() function. You will never use output_bit() or output_low()
or output_high() on individual pins. Port A will only be written to
with a full port write operation.
See if that fixes your problem. |
|
|
scaven92
Joined: 06 Oct 2006 Posts: 44
|
|
Posted: Mon Oct 12, 2009 12:27 pm |
|
|
Alright I will try that. If it is a read-modify-write problem, wouldn't a 500ms delay between port reads be enouph to allow that pin to settle to the correct value? The only load on that pin is the gate to the fet and the 10k pulldown, no substantial capacitive loads.
I'll let you know in a few.
Thanks for the help! |
|
|
scaven92
Joined: 06 Oct 2006 Posts: 44
|
|
Posted: Mon Oct 12, 2009 12:48 pm |
|
|
It looks like it is the read-modify-write problem you have previously mentioned. I was able to turn all the LED's on.
I have a question though, even if I put a 500ms delay in between successive bit manipulations on PORTA, I still run into the problem. I don't see how there is such a load on the pin to cause the state to change and the next read of the port to not be what I set it to 500ms prior. I only have a 10k pulldown and the port in feeding directly into the gate of the fet.
For the meantime I will use the shadow variable to keep track of every bit manipulations and write the PORT as a whole.
This is still leaving me confused as to why this is happening.
Thanks,
Chris |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Oct 12, 2009 1:50 pm |
|
|
To clarify your circuit diagram, which side of the FET is the Source and
which side is the Drain ? |
|
|
scaven92
Joined: 06 Oct 2006 Posts: 44
|
|
Posted: Mon Oct 12, 2009 1:52 pm |
|
|
I have changed my enable_led() function to accomodate this problem and everything is working smoothly.
Code: |
void enable_led( int value, int time_ms, int number )
{
int i, j = 0;
if( number != 0 )
{
if( value ) status |= (1 << (number-1)); //Set bit i of shadow reg
else status &= ~(1 << (number-1)); //Clear bit i of shadow reg
output_a(status);
}
else
{
for(i=0; i<7; i++)
{
if( value ) status |= (1 << i); //Set bit i of shadow reg
else status &= ~(1 << i); //Clear bit i of shadow reg
output_a(status);
for(j=0; j<time_ms; j++) delay_ms(1);
}
}
}
|
Code: |
void main()
{
output_bit(LED_DRIVE, ON);
while(1)
{
enable_led(ON, 500, ALL);
delay_ms(500);
enable_led(OFF, 500, ALL);
delay_ms(500);
}
}
|
|
|
|
scaven92
Joined: 06 Oct 2006 Posts: 44
|
|
Posted: Mon Oct 12, 2009 1:55 pm |
|
|
In my diagram the source of the fet is grounded and the drain connects to the cathode of the led. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Oct 12, 2009 3:36 pm |
|
|
The reason is because there is a bug in your version of the compiler
(vs. 4.093). In the start-up code, it's only clearing bits 0,1,2 of the
ANSELA register. It leaves bits 3, 4 and 5 in their power-up state,
which is all 1's. This means pins A3, A4 and A5 are still set as Analog
pins, which means they read back as 0's. That's what causing the RMW
problem.
To fix this, declare ANSELA above main() with a byte statement.
Then set it to 0 at the start of main() with a line of code. Example:
Code: |
#byte ANSELA = 0x185
//==========================
void main()
{
ANSELA = 0;
while(1);
}
|
If you do that, you probably don't need the RMW fix that you did to
your code. |
|
|
scaven92
Joined: 06 Oct 2006 Posts: 44
|
|
Posted: Mon Oct 12, 2009 3:54 pm |
|
|
Wow, that has to be it. I will correct my code to handle this problem. I appreciate your help, you have saved me alot of time and made me aware of this Read-Modify-Write problem.
Should I send CCS an email with a link to this forum for this problem for future update?
Thanks again! |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Oct 12, 2009 4:00 pm |
|
|
They fixed it in vs. 4.099, as shown below. Both ANSELA and ANSELB
are set to 0x00.
Code: |
.... void main()
.... {
0004: CLRF 04
0005: BCF 03.7
0006: MOVLW 1F
0007: ANDWF 03,F
0008: MOVLW 00
0009: BSF 03.5
000A: BSF 03.6
000B: MOVWF 06 // 0x186 = 0x00 ANSELB
000C: BCF 09.0 // Bug: 0x189 is not a register
000D: BCF 09.1
000E: BCF 09.2
000F: BCF 03.6
0010: BCF 1F.0 // ADCON1.0 = 0
0011: BCF 1F.1 // ADCON1.1 = 0 Vref = Vdd
0012: BSF 03.6
0013: MOVWF 05 // 0x185 ANSELA = 0x00
.... while(1);
0014: GOTO 014
|
|
|
|
scaven92
Joined: 06 Oct 2006 Posts: 44
|
|
Posted: Mon Oct 12, 2009 4:25 pm |
|
|
Great! I'll have to see if my 30 days of compiler updates is still valid or order an update package.
Thanks! |
|
|
|