Solved

simple program for pic18f1320 microcontroller

Posted on 2006-06-13
7
702 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
Comment Utility
Forgot to add, I am using a 4Mhz crystal.
Cheers


0
 
LVL 37

Expert Comment

by:Harisha M G
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 17

Expert Comment

by:mokule
Comment Utility
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
Comment Utility
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
Comment Utility
0
 
LVL 17

Expert Comment

by:mokule
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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 is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

762 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now