Solved

simple program for pic18f1320 microcontroller

Posted on 2006-06-13
7
707 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

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…
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…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
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.

777 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