Solved

simple program for pic18f1320 microcontroller

Posted on 2006-06-13
7
708 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
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
Flexible connectivity for any environment

The KE6900 series can extend and deploy computers with high definition displays across multiple stations in a variety of applications that suit any environment. Expand computer use to stations across multiple rooms with dynamic access.

 
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

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
memory leak detection 9 94
Where is my core dump file in Ubuntu? 12 454
mixing C++ and C code elegantly 10 158
Console based application in Linux 1 92
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

828 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