Вы находитесь на странице: 1из 17

Singular Engineer Programming PIC 18 using XC8 (MPLAB X) : IO ports http://singularengineer.

com/programming-pic-18-using-xc8-mplab-x-io-ports/

1 trong 17

Singular Engineer
In progress.
Home
About
Tutorials

Home > Electronics > Programming PIC 18 using XC8 (MPLAB X) : IO ports

Programming PIC 18 using XC8 (MPLAB X) : IO ports


April 14th, 2013 Singular Engineer Leave a comment Go to comments
When you start the project, the main.c (or newmain.c, depends on what you named your main c
file). You should be able to compile the code without any error or warning.
#include <stdio.h>
#include <stdlib.h>
/*
*
*/
int main(int argc, char** argv) {
return (EXIT_SUCCESS);
}

The code given above does nothing. Its just a way to make sure you programming environment is
set properly.
Follow the following steps
1) add config.h (remember the configuration bits?)
2) add xc.h the compiler header
3) change int main to void main (the return type int has a reason, Ill cover about it later)
Now your code should look like this and able to compile without a problem.
#include <xc.h>
#include "config.h"

10/29/2014 9:44 PM

Singular Engineer Programming PIC 18 using XC8 (MPLAB X) : IO ports http://singularengineer.com/programming-pic-18-using-xc8-mplab-x-io-ports/

2 trong 17

/*
*
*/
void main(int argc, char** argv) {
}

PIC18 IO Setup
I used this hardware from EZModule.com, which is a PIC breakout board.

In order to set the pin of PIC controller you need to know the registers associated with it. There
are 3 registers
1. TRIS To set a pin as Input or Output
2. LAT Write to this bit = write to a pin
3. PORT You can read/write to a pin
So the programing order will be, set the TRIS bits and when you read, read from PORT bits and
when you write, write to LAT bits.

10/29/2014 9:44 PM

Singular Engineer Programming PIC 18 using XC8 (MPLAB X) : IO ports http://singularengineer.com/programming-pic-18-using-xc8-mplab-x-io-ports/

3 trong 17

Set TRIS bit


1 = Input (1 looks like I input )
0 = Output ( 0 looks like o = output)

Read PORT bit


1 = pin is high
0 = pin is low

Set LAT bit


1 = make pin high
0 = make pin low

Ctrl + Space bar will activate the intelli-sense that looks like this

To set Pin RB0 as output pin do the following

10/29/2014 9:44 PM

Singular Engineer Programming PIC 18 using XC8 (MPLAB X) : IO ports http://singularengineer.com/programming-pic-18-using-xc8-mplab-x-io-ports/

4 trong 17

Lets try flip RB0 high and low


#include <xc.h>
#include "config.h"
void main(int argc, char** argv) {
TRISBbits.RB0 = 0;
while(1) //infinite loop
{
LATB0 = 1; //Set the PIN RB0 high
LATB0 = 0; //Set the PIN RB0 low
}
}

The program will run fast that you wont even see the difference. So we need to add some delay.
You got to set some delay. XC compiler gives you delay function, but you need to set a few
parameters (well actually its one line).
#include <xc.h>
#include "config.h"
#define _XTAL_FREQ 1000000 //This is the speed your controller is
running at

10/29/2014 9:44 PM

Singular Engineer Programming PIC 18 using XC8 (MPLAB X) : IO ports http://singularengineer.com/programming-pic-18-using-xc8-mplab-x-io-ports/

5 trong 17

int i = 0;
void Delay1Second(void);
void main(int argc, char** argv) {
TRISBbits.RB0 = 0;
while(1) //infinite loop
{
LATB0 = 1; //Set the PIN RB0 high
Delay1Second();
LATB0 = 0; //Set the PIN RB0 low
Delay1Second();
}
}
void Delay1Second()
{
for(i=0;i<100;i++)
__delay_ms(10);
}

Clock is not 8Mhz. Why? Well here is a a small screen shot of the datasheet why

10/29/2014 9:44 PM

Singular Engineer Programming PIC 18 using XC8 (MPLAB X) : IO ports http://singularengineer.com/programming-pic-18-using-xc8-mplab-x-io-ports/

6 trong 17

Thats why developers are requested to read the datasheet. Here is an awesome pdf document
from microchip that explains about the oscillators http://ww1.microchip.com/downloads
/en/devicedoc/31002a.pdf
Now in order to increase the speed to 8MHz Ill adding this function (code snippet below)which
is self explanatory with the screenshot above
void SetupClock()
{
OSCCONbits.IRCF0 = 1;
OSCCONbits.IRCF1 = 1;
OSCCONbits.IRCF2 = 1;
}

Adding this to the code, this is how the whole program will look like. Remember, dont forget to
change the _XTAL_FREQ to 8000000 (6 zeros)

10/29/2014 9:44 PM

Singular Engineer Programming PIC 18 using XC8 (MPLAB X) : IO ports http://singularengineer.com/programming-pic-18-using-xc8-mplab-x-io-ports/

7 trong 17

#include <xc.h>
#include "config.h"
#define _XTAL_FREQ 8000000 //This is the speed the controller is
running at
int i = 0;
void Delay1Second(void);
void SetupClock(void);
void main(int argc, char** argv) {
SetupClock();
TRISBbits.RB0 = 0;
while(1) //infinite loop
{
LATB0 = 1; //Set the PIN RB0 high
Delay1Second();
LATB0 = 0; //Set the PIN RB0 low
Delay1Second();
}
}
void Delay1Second()
{
for(i=0;i<100;i++)
__delay_ms(10);
}
void SetupClock()
{
OSCCONbits.IRCF0 = 1;
OSCCONbits.IRCF1 = 1;
OSCCONbits.IRCF2 = 1;
}

Now our program does the same thing, but runs at 8MHz. Please take your time and read the
datasheet.

Reading from a PIN


Until now we were just writing to a pin. How about we read from a pin and write to another pin?
Like read from a switch and turn on and off a LED. This is how Im planning

10/29/2014 9:44 PM

Singular Engineer Programming PIC 18 using XC8 (MPLAB X) : IO ports http://singularengineer.com/programming-pic-18-using-xc8-mplab-x-io-ports/

8 trong 17

#include <xc.h>
#include "config.h"
#define _XTAL_FREQ 8000000 //The speed of clock after setup
int i = 0;
void Delay1Second(void);
void SetupClock(void);
void main(int argc, char** argv) {
SetupClock();
TRISBbits.RB0 = 0; //set RB0 as output
TRISBbits.RB1 = 1; //set RB1 as input
while(1) //infinite loop
{
if(PORTBbits.RB1)//if RB1 high, then make RB0 high
LATB0 = 1;
else
LATB0 = 0;
}
}
void Delay1Second()
{
for(i=0;i<100;i++)
__delay_ms(10);
}
void SetupClock()
{
OSCCONbits.IRCF0 = 1;
OSCCONbits.IRCF1 = 1;
OSCCONbits.IRCF2 = 1;
}

10/29/2014 9:44 PM

Singular Engineer Programming PIC 18 using XC8 (MPLAB X) : IO ports http://singularengineer.com/programming-pic-18-using-xc8-mplab-x-io-ports/

9 trong 17

Share this:

Categories: Electronics Tags: IO, PIC LAT, PIC18, Read from Pin, XC8 compiler
Comments (22) Trackbacks (0) Leave a comment Trackback
1.
tony
May 30th, 2014 at 05:59 | #1
Reply | Quote
Hello,
I started using Pic18f4550 with xc8 compiler. In the above program you first declared the
internal oscillator freq in conf file. when you declared the new freq #define
_XTAL_FREQ 1000000, should i remove the internal oscillator declaration from config
file. if no, which frequency does the compiler consider and why. I am new to uc
programming, excuse me for basic doubts
Thanks
2.
Supreeth
March 25th, 2014 at 02:17 | #2
Reply | Quote
Thank you for your reply. Why we cant use huge parameters in the delay? Something like
__delay_ms(1000);

singularengineer
March 25th, 2014 at 05:32 | #3
Reply | Quote
Thats why using the delay for loop. _delay. is a macro. you can define your own if
you want more delay.
3.
Supreeth
March 24th, 2014 at 06:03 | #4
Reply | Quote

10/29/2014 9:44 PM

Singular Engineer Programming PIC 18 using XC8 (MPLAB X) : IO ports http://singularengineer.com/programming-pic-18-using-xc8-mplab-x-io-ports/

10 trong 17

Hi this tutorial really helped me. But I am having problem. when I am using LATB=0; it
is working fine but when I use LATBbits_t.LATB0=1; it is showing no identifier in
declaration error. . In the case of TRISB both TRISB0=1; and TRISBbits.TRISB0=1;
are compiling without errors.I am using MPLABX 2.05 and XC8 1.30.

singularengineer
March 24th, 2014 at 15:48 | #5
Reply | Quote
LATBbits_t is template. You cant use template like that. Use LATBbits.LATB0
(thats why _t means template to microchips naming convention). Please check
the declaration and source files which will answer a lot.
4.
Alessandro
February 14th, 2014 at 08:02 | #6
Reply | Quote
I would like to ask you another question but it isnt about IO but about MPLAB.
Sometimes it happens that while its building a project it shows something like this message
in output:
make[2]: waiting for unfinished job
and the building process stops here until I go to the Windows task manager, in process tab,
where I find 2 process named make.exe and I have to close them to go over with build. that
cause also a big slow down of whole programs. Have you got any idea on why that
happens? thank you

singularengineer
February 14th, 2014 at 09:25 | #7
Reply | Quote
Never came across a problem like that. Give it a try in Microchip forum and youll get
help.
5.
Alessandro
February 13th, 2014 at 19:30 | #8
Reply | Quote
Im sorry, I hadnt the datasheet with me. I checked it and I saw that also RA2 pin has that
thing. I wrote ANSEL=0b00000000 and it works! Im sorry for my ignorance,

10/29/2014 9:44 PM

Singular Engineer Programming PIC 18 using XC8 (MPLAB X) : IO ports http://singularengineer.com/programming-pic-18-using-xc8-mplab-x-io-ports/

11 trong 17

unfortunately I havent got so much time as I wish to dedicate to this great world, so I
couldnt read all parts of the datasheet. Thank you so much! All tutorial is great!
6.
Alessandro
February 13th, 2014 at 19:07 | #9
Reply | Quote
Thank you for reply, Ill try, but my problem isnt on the output, its on the input, I tryed it
on all A bank (like in the program i posted, where I was using RA2). for example if i
write
LATC0=1; the led turns on without problems and this is true for all 4 leds (on RC0-3 pins)
but if i write
if(PORTAbits.RA0==0)
LATC0=1;
else
LATC0=0;
the led doesnt change his state! And I tryed to use all pins of A bank as input and also to
connect the input directly to Vdd (or ground). Thats the problem, it seems that on input
the logical state doesnt change
7.
singularengineer
February 13th, 2014 at 13:36 | #10
Reply | Quote
@Alessandro
The pins you are trying to use as IO are used by ADC by default. If you go into the
datasheet of 18F14K22 and check page 88, there is a note (in a box)saying that after reset
the PORT C pins from RC0-RC3 and RC6 and RC7 will become analog input pins. Please
turn off ADC on the pins you want to use as Output and it will start working. In order to set
the pins as DIO, please read the chapter 8.4 and ADC section in the datasheet for
18F14k22 (the PIC you are using).
8.
Alessandro
February 13th, 2014 at 12:56 | #11
Reply | Quote
god why the include between are not shown? by the way they are xc.h and stdio.h
9.

10/29/2014 9:44 PM

Singular Engineer Programming PIC 18 using XC8 (MPLAB X) : IO ports http://singularengineer.com/programming-pic-18-using-xc8-mplab-x-io-ports/

12 trong 17

Alessandro
February 13th, 2014 at 12:54 | #12
Reply | Quote
sorry for the mistake, two #include directives are not shown in my message below but
theyre present in project. they are #include and #include
10.
Alessandro
February 13th, 2014 at 12:50 | #13
Reply | Quote
Hi and thank you for your great tutorials. Im having a problem on programming a
PIC18F14K22, I am using the PicKit3 with his own demoboard: I cant use the inputs in
any way. I try to explain it better: I tryed some simple programs with only outputs (RC0-4
are connected to 4 led) and they works good. But inputs dont work! I tryed to connect
directly with a cable the pin to Vdd but nothing changes, I tryed with the switch on the
demoboard (connected to RA2 pin of the mcu, checked with a tester directly on that pin,
when switch is not pressed, it get 4.97V and when pressed it get 0V so the logic is inverted)
but nothing has changed. I tryed also to change the input (like putting it on RB0 and so on)
Here is my code
#define _XTAL_FREQ 16000000
#include
#include myDelays.h
#include config.h
#include
#define TDELAYMS 50
void delay50ms(void) {
int i;
for(i=0;i!=5;i++) {
__delay_ms(10);
}
}
void main(void) {
OSCCONbits.IRCF=0b111;
TRISCbits.RC0=0;
TRISAbits.RA2=1;
LATC0=0;
while(1) //infinite loop
{
if(PORTAbits.RA2==0)
LATC0 = 1;
else
LATC0 = 0;

10/29/2014 9:44 PM

Singular Engineer Programming PIC 18 using XC8 (MPLAB X) : IO ports http://singularengineer.com/programming-pic-18-using-xc8-mplab-x-io-ports/

13 trong 17

}
}
It doesnt show any warning or problem, all build and programming went good, it seems
that the input doesnt reach the Vih voltage but thats not true because I checked it with the
tester and 4.97V reach that pin. It doesnt seems to be the MCU, because that thing
happened also with a PIC16F1829. For those tests Vdd is provided by an USB of my pc.
voltage is quitely constant (checked with the oscilloscope) and high enough (like I said
4.97V). I dont know where to bang my head anymore thank you for your help, best
regards
11.
Sebastian
October 18th, 2013 at 21:19 | #14
Reply | Quote
Great tutorial!!
I have a question though, Is it possible to refer to a single register bit by index? Por
instance, for the TRISBbits.RB0 something like TRISB(0)???
Thanks!

singularengineer
October 18th, 2013 at 22:26 | #15
Reply | Quote
The TRISx list is actually a structure. If you right click and go to declaration of
TRISx, you will be able to see the source and find a way to define the way you want.
TRISBbits.RBO already addresses one bit.
12.
Mehmet
August 15th, 2013 at 07:58 | #16
Reply | Quote
Thanks so much!!
Thanks to you, i can understand clearly how can Programming PIC 18 using XC8 !
I am from stanbul/TRKYE
so
Excuse my bad English!

10/29/2014 9:44 PM

Singular Engineer Programming PIC 18 using XC8 (MPLAB X) : IO ports http://singularengineer.com/programming-pic-18-using-xc8-mplab-x-io-ports/

14 trong 17

Regards
13.
SJK
June 17th, 2013 at 17:51 | #17
Reply | Quote
i use pic18f4550.i have read datasheet but it cant make me clear.if you give a outline with
screen short that must help me

singularengineer
June 17th, 2013 at 23:02 | #18
Reply | Quote
use this file : http://wp.me/a3oMLT-3G and change FOSC to #pragma config
FOSC=HS
14.
SJK
June 17th, 2013 at 02:50 | #19
Reply | Quote
can you give an outline of configuration of 20Mhz external crystal? I cant understand what
I do in CONFIG1L resister? I dont want to enable PLL.

singularengineer
June 17th, 2013 at 09:08 | #20
Reply | Quote
Here is a microchip device documentation on oscillators: http://ww1.microchip.com
/downloads/en/devicedoc/31002a.pdf
Which chip are you using? Are you sure you need an external oscillator? Any
justification for it?
To use external oscillator of 20MHz you got to set your FOSC config bit as
#pragma config FOSC = HS
Also please read microchip forums where you have a vast resources.
~SE
15.
younesser

10/29/2014 9:44 PM

Singular Engineer Programming PIC 18 using XC8 (MPLAB X) : IO ports http://singularengineer.com/programming-pic-18-using-xc8-mplab-x-io-ports/

June 10th, 2013 at 07:05 | #21


Reply | Quote
this instruction is not recognized by the compiler : #include config.h
Thanks
younesser

singularengineer
June 10th, 2013 at 12:27 | #22
Reply | Quote
Younesser,
You need to make your own config.h. Here is the config.h I used for mine which is
specific to PIC18F4550 and uses internal oscillator. Also you can refer to my previous
posts on how to create your config file.
file link (for config.h) : http://wp.me/a3oMLT-3G
link for making your own : http://singularengineer.com/programming-pic-18-usingxc8-mplab-x-configuration-bits-2/
~SE
1. No trackbacks yet.
Name (required)
E-Mail (will not be published) (required)
Website

Subscribe to comments feed

Notify me of follow-up comments by email.


Notify me of new posts by email.
Programming PIC 18 using XC8 (MPLAB X) : Interrupts Programming PIC 18 using XC8
(MPLAB X) : Setting Configuration Bits
RSS
15 trong 17

10/29/2014 9:44 PM

Singular Engineer Programming PIC 18 using XC8 (MPLAB X) : IO ports http://singularengineer.com/programming-pic-18-using-xc8-mplab-x-io-ports/

Twitter
Search for:

Recent Posts
dsPIC/PIC24 Timers (using XC16 and MPLAB X) July 12, 2014
dsPIC/PIC24 Interrupts (using XC16 and MPLAB X) June 29, 2014
dsPIC/PIC24 Configuration Bits and Oscillator Settings(using XC16 and MPLAB X )
February 5, 2014
Why 16 bit controllers? November 18, 2013
Programming PIC 18 using XC8 (MPLAB X) : ADC (using adc.h) May 26, 2013
Programming PIC 18 using XC8 (MPLAB X) : USART (PIC18F Serial) May 15, 2013
Programming PIC 18 using XC8 (MPLAB X) : PWM (using pwm.h) May 13, 2013
Programming PIC 18 using XC8 (MPLAB X) : Timers May 7, 2013
Programming PIC 18 using XC8 (MPLAB X) : Interrupts April 28, 2013
Programming PIC 18 using XC8 (MPLAB X) : IO ports April 14, 2013

Recent Comments
singularengineer on Tutorials
Chinmay on Tutorials
Chinmay on Tutorials
Daniel on Programming PIC 18 using XC8 (MPLAB X) : Interrupts
omran on Programming PIC 18 using XC8 (MPLAB X) : Setting Configuration Bits

Archives

16 trong 17

July 2014
June 2014
February 2014
November 2013
May 2013
April 2013
October 2012
September 2012

10/29/2014 9:44 PM

Singular Engineer Programming PIC 18 using XC8 (MPLAB X) : IO ports http://singularengineer.com/programming-pic-18-using-xc8-mplab-x-io-ports/

Categories
Electronics

Categories
Electronics

Blogroll
Archives
July 2014
June 2014
February 2014
November 2013
May 2013
April 2013
October 2012
September 2012

Meta
Log in
Top WordPress
Copyright 2012-2014 Singular Engineer
Theme by NeoEase. Valid XHTML 1.1 and CSS 3.

17 trong 17

10/29/2014 9:44 PM

Вам также может понравиться