Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

something not right??

Status
Not open for further replies.
While making coffee this Wednesday morning It dawned on me that I have made a mountain out of a mole hill.
Jonsea's #17 pointed this out. I only need to toggle the led when the swt = 0
On Pommie's post #18 looks interesting and will try out both routines.
Thanks to all
 
Could we / I get a tutorial on pointers here ! I always have to mess around (C) after compiler complains like.. ' using an int as a pointer without a cast' ... what ever that means, still seems to work, eventually error goes away usually don't know how i fixed it .. I understand the principal , ( perhaps) but where do I put the * ... perhaps a different thread Ian ?

I did do a tutorial on pointers... Seems to be lost... I'll find the old stuff and do another one..

Pointers are nothing more than indexed addressing... Using the FSR registers to access variables..

Unlike asm C likes to whinge.... Pointers can point to byte's int's and long's as well as struct's and array's..

Casting is a whole other topic... In asm they just assume that if you load a register with a high byte and another with a low byte then try to cram both into a single register it wont fit... If you know that if x is an int and var is a byte and you do this x= var <<8; and wonder where the bits have gone, then you need to know about casts...

For those wondering about the line of C code... << takes prescience in C, so all 8 bits are shifted out of var before x even gets involved.. The fix!!! x= (int) var <<8; Cast var to an int first...
 
I tried Jons code but edited it and found that making swt0=0 won't work unless I
DIM pressed AS BYTE
MY EDIT
IF swt0 = 0
THEN
TOGGLE (LED0)
DELAYMS(500)
END IF

Jons correct syntax and workable code
IF swt1 = pressed THEN
TOGGLE (LED1)
DELAYMS(500)
END IF

so much easier than my mountain of code.
After I get this done will try Pommies code.
 
I'm sorry I wasn't clear. I was trying to show you a method, not write your code for you.

Just as I didn't show the device type, clock frequency and necessary include files, I didn't show the needed variable dimensions and port pin assignments. I figured this would be clear, but I assumed a level of knowledge not present. Since MrDEB has been using microcontrollers about the same length of time I have, I thought these things would be self-evident.

It's perfectly valid to test if a port pin is equal to zero to determine if a switch is pressed, but I prefer to make my code more clear by using appropriately named variables for conditions.

Depending on how a switch is arranged, it can be either high or low when pressed. Depending on how an LED is wired, "on" can be either a high or low port pin.

If PortB.0 = 0 then
PortB.1 = 0
End If

Isn't nearly as clear as

If S1 = Pressed then
LED1 = LEDon
End If

That way, I don't have to remember which port pin is what or how things are wired.
 
your suggestion worked very well Thanks
I didn't want the entire code just a way to use the correct syntax to dimension the led(x)
I put all the leds on one port so led(0) is on port pin b.0 etc.
What I am wanting to do is
for x = 0 to 7
IF Swt(0) = pressed
THEN
TOGGLE (led(0)
DELAYMS(500)
END IF
next
tried led(7) as byte but it dosen't work
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top