Dear futz,
thanks! your tips are really helping me making things simpler! Yet I have came into a result where I can make series of blinking LEDs (4leds) when S2 is pressed. I recovered from headache and am thinking carefully, so I ended up like:
Code:
#include <p18f4520.h>
#include <delays.h>
void main(void)
{
/*Settings*/
TRISD=0b00000000;
TRISA=0b11111111;
while(1)
{
if (PORTAbits.RA4 == 0)
{
PORTDbits.RD1 = 1;
Delay10KTCYx(30);
PORTDbits.RD1 = 0;
PORTDbits.RD3 = 1;
Delay10KTCYx(30);
PORTDbits.RD3 = 0;
PORTDbits.RD5 = 1;
Delay10KTCYx(30);
PORTDbits.RD5 = 0;
PORTDbits.RD4 = 1;
Delay10KTCYx(30);
PORTDbits.RD4 = 0;
}
}
}
Yes I am setting the PORT D for output as you may see above. The blinking time is controlled by Delays.h included in .h file. So I am excited, very excited. This is the first time I can program a push button to operate my LEDs.
Human, are never satisfied, me too.
I want to modify my program now. Before I read this forum I have finished the code above so the #pragma variables I haven't touched it yet. So, back to topic.
I want to make several modifications to the Push button S2 (RA4) on the demo board. I want to program so that when I pressed the button once, the blinky LEDs are looping forever until I pressed the button twice, it stops.
I am really noob to C programming but my spirit to learn is big. So if you guys would kindly help, I think you can lead my way here by helping me.
Another things which are questions:
1. Assigning I/O for TRIS command is kinda amusing for me. I spent like 6 hours looking for hints and tips in writing it onto the C Language. I ended up knowing that
TRISA is assigned for PORT A, making the value equal to 0b00000000 means you make this PORT A as an output. By making the value to 0b11111111 means you make this PORT B as an input. my question is really simple, but noob like me find it very difficult to understand.
"How do you assign PORT A in register RA7 for ex as input BUT the other at that PORT are output?"
2. I tried programming a LED with LAT and PORT command:
PORTAbits.RA1 = 1; /*Turn on LED at RA1*/
LATAbits.LATA1 = 1; /*Turn on LED at RA1*/
Both are equals, LED at RA1 turned on, but Where is the difference???
For your attention, I would like to say BIG THANKS! Cheers.
Best regards,
Kelvin