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 support@ccsinfo.com

Pins A3 and A4 wont turn on, only toggle
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
scaven92



Joined: 06 Oct 2006
Posts: 44

View user's profile Send private message

PostPosted: Fri Oct 09, 2009 5:58 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Oct 09, 2009 9:28 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Oct 12, 2009 10:33 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Oct 12, 2009 10:42 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Oct 12, 2009 12:27 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Oct 12, 2009 12:48 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Oct 12, 2009 1:50 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Oct 12, 2009 1:52 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Oct 12, 2009 1:55 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Oct 12, 2009 3:36 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Oct 12, 2009 3:54 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Oct 12, 2009 4:00 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Oct 12, 2009 4:25 pm     Reply with quote

Great! I'll have to see if my 30 days of compiler updates is still valid or order an update package.

Thanks!
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
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