View previous topic :: View next topic |
Author |
Message |
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! |
|
|
|