Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[PIC] The blink does not work - the negation

Status
Not open for further replies.

K33rg4t3

Full Member level 3
Full Member level 3
Joined
Aug 2, 2015
Messages
165
Helped
7
Reputation
14
Reaction score
7
Trophy points
1,298
Activity points
2,607
Hey
What is exacly wrong with this code snipper?
Code:
void main() {

     ADCON1 |= 0x0F;                         // Configure all ports with analog function as digital
     CMCON  |= 7;                            // Disable comparators
     TRISB = 0;
     PORTB = 0;

     TRISA = 0;
     PORTA = 0;

     while(1)
     {
       Delay_ms(1000);
       LATB = !LATB;
       LATA = !LATA;
    }
     
}

you know, I can do this other way and it works, but Idk why the LED is not blinking if I use the "!" operator?
 

Performing a bitwise ! operation on a byte is an unusual thing to do (but legal)

Check MikroC help file - which states:

Logical vs. Bitwise
Be aware of the principle difference between how bitwise and logical operators work. For example:

0222222 & 0555555 /* equals 000000 */
0222222 && 0555555 /* equals 1 */

~ 0x1234 /* equals 0xEDCB */
! 0x1234 /* equals 0 */

Result will always be 1 or zero - so you MIGHT get away with LED toggling on bit0 of PORTA or PORTB - but only if all other LAT bits read as 0.
 
Last edited:
With this code only the LSB will alter.
That means only RB0, RA0 pins.
You could use
Code:
LATB=~LATB;
LATA=~LATA;
to toggle all port pins
 

ahh now I see...
!A will return always 0x0 OR 0x1, it will never return 0xff etc...
Somehow I forgot the basics.
 

You probably still wouldn't see it blink anyway, think of the program flow in the while loop:
while(1)
{
<wait for 1 second> // LED is still off from last pass of the loop
<turn LED on>
<turn LED off>
}

I may have the on/off reversed depening on how you have the LEDS wired. The principle still applies though, think how long the 'on' time is compared to the 'off' time!

Brian.
 

You probably still wouldn't see it blink anyway, think of the program flow in the while loop:
while(1)
{
<wait for 1 second> // LED is still off from last pass of the loop
<turn LED on>
<turn LED off>
}

I may have the on/off reversed depening on how you have the LEDS wired. The principle still applies though, think how long the 'on' time is compared to the 'off' time!

Brian.

you are wrong, I am toggling the ports in my code, please recheck first post
 

Since I can't see where LATA/B are pointing I suppose they are pointing to portA & B.

Are you sure that you can manipulate the outputs directly in one statement? You don't need to read the contents of the output port before you invert it?
I know some compilers warn about this. It may also be an idea to declare the LATA/B 'volatile' since they are connected to realtime hardware.
 

LATA and LATB are PIC mid-range core registers, not user variables.They are the output LATCH registers (correspomding to PORTA/B i/o registers). There is no error/warning in their usage.
 

He does not use this version of code:
...
while(1)
{
<wait for 1 second> // LED is still off from last pass of the loop
<turn LED on>
<turn LED off>
}
...

he uses this:
Code:
while(1)
{
<wait for 1 second> // LED is one time ON and next time OFF
toggle (reverse status) leds on porta
toggle (reverse status) leds on portb
}
and it's correct!
 
Then please tell me how long the LED is on for and how long it is off for.

Brian.
I have tested the OPs original code using MikroC pro compiler and EasyPIC7 dev board with PIC18F4550 fitted, 8MHz crystal for 48MHz clock.

The code flashes LEDs on RA0 and RB0 at 0.5 Hz. (1 second on, 1 second off).

The codes works just fine for flashing 2 LEDs. The issue was explained, understood and fully resolved by post #4 of this thread. Everything after post #4 seems to have added nothing but confusion :(

EDIT: ignore the above - xenos beat me to it
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top