• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 715
  • Last Modified:

simple program for pic18f1320 microcontroller

Hi
Could any one supply me with simple code for lighting an led on the pic18f1320. The buttons are all on port b and the LEDS are all on port a. I just want to press just one switch to light one led, just as a simple example.
I am using MPlab with the student trial version of the c compiler on.
From the codes I have seen, they have strange while loops in, so I would be gratefull for comments on how that works as well.
They look like:
While(1)
{
//do this
}

I dont understand while what is 1!!

cheers
John
0
jdav3579
Asked:
jdav3579
1 Solution
 
jdav3579Author Commented:
Forgot to add, I am using a 4Mhz crystal.
Cheers


0
 
Harisha M GCommented:
Hi, I am not an expert at microcontroller programming..

while() loop takes an argument, which evaluates into either true, or false. It need not necessarily be always a boolean expression or a relational expression.

In C, 0 is false, and anything else is true. In this case, 1 is not 0, and hence it is true.

while(1) means the loop would run forever.

This will make the code inside the while loop to run until a break is encountered, or until the power goes down.

---
Harish
0
 
mokuleCommented:
You must define configuration bits.
I don't know how it should be done in C.
I've always used asembler
    CONFIG      OSC=XT, IESO=OFF, WDT=OFF, MCLRE=ON, LVP=OFF, PBADEN=OFF, BOREN=ON, BORV = 0, CPB = ON

main()
{
// You must initialize some modules
TRISA = 0;       // port a is output (remember RA5 can't be an output)
TRISB = 0xff;    // port b is input
ADCON1 = 0x7f // configure ports as digital I/O not analog inputs
while(1)
  {
  PORTA = PORTB;   // read port b and output it on port a
  }
}

thats all
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
mokuleCommented:
I've forgot to say that depending on Your hardware configuration it may be desired to use internal pullup on port b
that is
// in initialization modules part
INTCON2 = 0x7f; // RBPU=0
0
 
jdav3579Author Commented:
Thankis to both of you for your comments very much appreciated.
Mokule - I can see that you code takes what ever switch has been pressed on port a and it will light the led on the same pin on port b. But how do I get it so that I specify what pin is read and what pin lights the led regardless of what is happening on the pin.
for example only if pin 3 on port a is pressed does pin 1 go high on  port b. so that it ignores what is happening on the other inputs, they may be going high or low. Also so it only affects that one led.

in Assembly I think it would be something like:
BTFSS porta,3 this would bit test pin 3 on port a.


and to light the led:

BSF porta, 1 this would set port a pin 1 high.

i have increased the points to reflect, the slight change in my question.
Cheers
John
0
 
srinimsCommented:
0
 
mokuleCommented:
You could do something like this

unsigned char mask;
unsigned char led;

// if for example You want to test RB2
mask = 0x04;
// and switch led on RA6
led = 0x40;
if(PORTB & mask)
  PORTA |= led;          // set RA6 high - other not changed
else
  PORTA &= ~led;       // set RA6 low - other not changed
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now