Click here to Skip to main content
16,005,491 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey guys...Just wondering if anybody can see why I can't getout of this loop? the idea is an upper nibble is placed on the upper 4 bits of an output port, and enable line pulsed. The lower nibble is then sent when the enable goes high again...

When I slow the process down and put in some LEDs the sequence works fine, but for some reason it loops for about 10 seconds. I don't have access to any other functions when this is happening, so there must be something stopping the switch statement from breaking. Here's the code...

C#
case 0x02:                                      //case 0x02 from firmware

            IDENT_PORT = 0X00;

            j = 0x16;                              //bit mask ie, & with 1111 0000 to clear bottom nibble
            upper_nibble = j & 0x0f0;              //   0001 0110
            PORTB = upper_nibble;                  //present upper nibble on port b

            RB1 = 1;                               //pulse enable line
            DelayMs(500);
            RB1 = 0;
            //DelayMs(500);
            PORTB = 0x00;

            k = (j << 4) | (j >> 4);               //swap upper and lower nibbles of 0x16
            lower_nibble = k & 0x0f0;              //again bit mask to leave the lower nibble (0110)
            PORTB = lower_nibble;                  //put on port

            RB1 = 1;
                DelayMs(500);                      //pulse enable line
            RB1 = 0;
                DelayMs(500);

            PORTB = 0x00;                          //clear port
            ReceivedDataBuffer[0] = 0x00;

    break;


I'd really appriciate the help, I think i'm too close to the project
Posted
Comments
kutalinelucas 27-Jul-11 13:19pm    
it's ok...its a problem with my application (not sure what yet though!)

That isn't the most obvious bit of code I've ever seen, and ignoring that it doesn't have a loop in it at any point an the comments are a bit rubbish...

Write a function which transmits a byte to the port - I assume that your port uses the top four bits as the data lines, and that data is valid out on the rising edge of you pulse line. The function should call a function to output a nibble, twice.

void OutByte(byte b)
   {
   OutNibble(b);             // High nibble
   OutNibble(b << 4);        // Low nibble
   }
void OutNibble(byte b);
   {
   PORTB = b & 0xf0;         // Data lines are top four bits of port
   RB1 = 1;                  // Strobe the data as available.
   DelayMs(500);             // Allow receiver to notice
   RB1 = 0;                  // Cancel strobe...
   DelayMs(500);             // ...and stabilize.
   }
You can fine tune your delays to allow for data/strobe stabilization when you put a scope on them.
 
Share this answer
 
Comments
Albert Holguin 27-Jul-11 21:35pm    
OP posted a follow-up as a solution.
yes I know my code is pretty sloppy...I haven't being doing it for very long so I kind of make it up with logic as Im not too sure on the language...but im getting there, slowly. I don't think I can use your code as most of the firmware is contained within a function, and I don't particully want to play around with it too much until I know what I'm doing...
I was wondering if I could ask you for some help tho, i'm playing with a pic USB interface tutorial, where an unsigned char is written to the firmware form a c++ application.

C++
void ProcessIO(void)
{
if((USBDeviceState < CONFIGURED_STATE)||(USBSuspendControl==1)) return;
       if(!HIDRxHandleBusy(USBOutHandle))                                    
           {
             switch(RecievedDataBuffer[0])
              {
                 case.............
              }
	USBOutHandle = HIDRxPacket(HID_EP,(BYTE*)&ReceivedDataBuffer,64);
    }
}


The problem is its full of switch statements and I would just like to assign j (in my original question) to the imported unsigned char. Although pretty crappy, my original code does the job, but when i try to replace the switch statement with

j = RecievedDataBuffer[0];
upper_nibble = j & 0x0f0 etc..(as in the original question).

The application crashes...Do you have any suggestion of how I could fix this?

Cheers

Martyn
 
Share this answer
 
Comments
Albert Holguin 27-Jul-11 21:34pm    
Don't post updates or follow-ups as solutions please. If you'd like to address a user who posted a solution, add a comment to their solution.
kutalinelucas 28-Jul-11 4:34am    
Sorry, I just thought it would look unclear if I placed code in the comment. I'll start a new thread now...thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900