Solved

simple program for pic18f1320 microcontroller

Posted on 2006-06-13
7
709 Views
Last Modified: 2008-01-09
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
Comment
Question by:jdav3579
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 

Author Comment

by:jdav3579
ID: 16897030
Forgot to add, I am using a 4Mhz crystal.
Cheers


0
 
LVL 37

Expert Comment

by:Harisha M G
ID: 16897054
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
 
LVL 17

Accepted Solution

by:
mokule earned 400 total points
ID: 16898091
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
Independent Software Vendors: 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!

 
LVL 17

Expert Comment

by:mokule
ID: 16898146
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
 

Author Comment

by:jdav3579
ID: 16906573
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
 
LVL 3

Expert Comment

by:srinims
ID: 16909098
0
 
LVL 17

Expert Comment

by:mokule
ID: 16910919
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

Industry Leaders: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question