Jump to content
Sign in to follow this  
jamwaffles

Latc On Pic 18f4520 Wont Work Properly

Recommended Posts

i have recently built an led cube and the anodes of the leds are connected to latb and latc and layer slect on latd on a pic 18f4520. i can flash the intire port on and off like this:

 

latd = 0x01;

latc = 0xff;

delay_s(1);

latc = 0x00;

delay_s(1);

 

this enables layer 0 and flashes latc with an interval of 1 second. what i cannot do is make latc.1 and latc.4 operate independantly of each other. i sut the ports up with all outputs (trisc = 0x00;) and i am using timer0 for the interrupt on my cube. this is the config i have:

 

#pragma DATA _CONFIG1H, _OSC_HSPLL_1H

#pragma DATA _CONFIG4L, _LVP_OFF_4L

#pragma DATA _CONFIG2H, _WDT_OFF_2H

 

would i need more config to disable cool functions on the pins concerned to get them to work or should i go and point a stern finger and shout at my pic until it starts working? (or a more real solution)

 

thanks

 

james waples, 14

 

ps. i can email the source to anyone who wants/needs it. its not finished yet but pls use its noobness as inspiration for your cube circuit.

cube_test_1.txt

Share this post


Link to post
Share on other sites

Hmm. Just looking at your source now. Is the init function commented out in your final version? It probably shouldn't be - you're relying on the default IO configurations if you don't call that function. Also, the purpose of the interrupt shouldn't be to set variables and outputs but to flag to main that outputs can be set. And you almost certainly shouldn't have delay functions called within it, you should also check that the interrupt that's called was the one you're handling too, i.e. put something like

// Create global timer0 if variable
bool do_toif = false;

void interrupt(void) 
{
 if ( intcon.TMR0IF) ) { 
do_t0if = true;
clear_bit( intcon, TMR0IF );
 }
}

void main() {
 // ...
 while ( 1 ) {
// Test variable and execute code if it's true
if ( do_toif ) {
  do_toif = 0;
  // Your code here
}
 }
}

in the interrupt body.

 

Anyway, onto your question...

 

I don't have an 18F2540 so can't test the code, but in simulation it appears to work as I'd expect it to. Try this main and see what happens...

bool do_t0if = false;

/* interrupt service routine */
void interrupt(void) 
{
 if ( intcon.TOIF ) {
do_t0if = true;
clear_bit( intcon, TOIF );
 }	
}

// ...

void main() 
{
 init();
 trisc = 0x00;
 trisd = 0x00;

 latc = 0x00;

 while(1)
 {
delay_ms(100);
latc.1 ^= 1; // Toggle this port

delay_ms(100);
latc.4 ^= 1; // Toggle this port

if ( do_t0if ) {
  do_t0if = false;

  latd = 0x01;
  latb = cubeTmp[0];
  delay(LAYER_DELAY);
  latb = 0x00;
  latc = cubeTmp[1];
  delay(LAYER_DELAY);
  latc = 0x00;

  latd = 0x02;
  latb = cubeTmp[2];
  delay(LAYER_DELAY);
  latb = 0x00;
  latc = cubeTmp[3];
  delay(LAYER_DELAY);
  latc = 0x00;

  latd = 0x04;
  latb = cubeTmp[4];
  delay(LAYER_DELAY);
  latb = 0x00;
  latc = cubeTmp[5];
  delay(LAYER_DELAY);
  latc = 0x00;

  latd = 0x08;
  latb = cubeTmp[6];
  delay(LAYER_DELAY);
  latb = 0x00;
  latc = cubeTmp[7];
  delay(LAYER_DELAY);
  latc = 0x00;
}
 }
}

All I can say is that it works on simulation... don't know how it handles in a real component.

Edited by twomers

Share this post


Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...