Làm Quên Trình Dịch XC8

  • Upload
    danhis4

  • View
    220

  • Download
    0

Embed Size (px)

Citation preview

  • 7/21/2019 Lm Qun Trnh Dch XC8

    1/17

    Singular Engineer

    In progress.

    Home

    AboutTutorials

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

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

    April 14th, 2013 Singular EngineerLeave a commentGo to comments

    When you start the project, the main.c (or newmain.c, depends on what you named your main cfile). You should be able to compile the code without any error or warning.

    #i ncl ude #i ncl ude

    / * **/

    i nt mai n( i nt ar gc, char ** argv) {

    return (EXI T_SUCCESS) ;}

    The code given above does nothing. Its just a way to make sure you programming environment isset 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.

    #i ncl ude #i ncl ude "conf i g. h"

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

    ng 17 10/29/2014

  • 7/21/2019 Lm Qun Trnh Dch XC8

    2/17

    / * **/

    voi d mai n( i nt ar gc, 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. Thereare 3 registers

    TRIS To set a pin as Input or Output1.LAT Write to this bit = write to a pin2.PORT You can read/write to a pin3.

    So the programing order will be, set the TRIS bits and when you read, read from PORT bits andwhen you write, write to LAT bits.

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

    ng 17 10/29/2014

  • 7/21/2019 Lm Qun Trnh Dch XC8

    3/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

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

    ng 17 10/29/2014

  • 7/21/2019 Lm Qun Trnh Dch XC8

    4/17

    Lets try flip RB0 high and low

    #i ncl ude #i ncl ude "conf i g. h"

    voi d mai n( i nt ar gc, char ** argv) {

    TRI SBbi t s. RB0 = 0;

    whi l e( 1) / / i nf i ni t e l oop { LATB0 = 1; / / Set t he PI N RB0 hi gh LATB0 = 0; / / Set t he PI N RB0 l ow }

    }

    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 fewparameters (well actually its one line).

    #i ncl ude #i ncl ude "conf i g. h"

    #def i ne _XTAL_ FREQ 1000000 / / Thi s i s t he speed your cont r ol l er i sr unni ng at

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

    ng 17 10/29/2014

  • 7/21/2019 Lm Qun Trnh Dch XC8

    5/17

    i nt i = 0;voi d Del ay1Second( voi d) ;

    voi d mai n( i nt ar gc, char ** argv) {

    TRI SBbi t s. RB0 = 0;

    whi l e( 1) / / i nf i ni t e l oop {

    LATB0 = 1; / / Set t he PI N RB0 hi gh Del ay1Second( ) ; LATB0 = 0; / / Set t he PI N RB0 l ow Del ay1Second( ) ; }}

    voi d Del ay1Second( ){ f or ( i =0; i

  • 7/21/2019 Lm Qun Trnh Dch XC8

    6/17

    Thats why developers are requested to read the datasheet. Here is an awesome pdf documentfrom 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)whichis self explanatory with the screenshot above

    voi d SetupCl ock( ){

    OSCCONbi t s. I RCF0 = 1; OSCCONbi t s. I RCF1 = 1; OSCCONbi t s. I RCF2 = 1;}

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

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

    ng 17 10/29/2014

  • 7/21/2019 Lm Qun Trnh Dch XC8

    7/17

    #i ncl ude #i ncl ude "conf i g. h"

    #def i ne _XTAL_ FREQ 8000000 / / Thi s i s t he speed t he cont r ol l er i sr unni ng ati nt i = 0;voi d Del ay1Second( voi d) ;voi d Set upCl ock( voi d) ;

    voi d mai n( i nt ar gc, char ** argv) {

    Set upCl ock( ) ;

    TRI SBbi t s. RB0 = 0;

    whi l e( 1) / / i nf i ni t e l oop { LATB0 = 1; / / Set t he PI N RB0 hi gh Del ay1Second( ) ; LATB0 = 0; / / Set t he PI N RB0 l ow Del ay1Second( ) ; }}

    voi d Del ay1Second( ){ f or ( i =0; i

  • 7/21/2019 Lm Qun Trnh Dch XC8

    8/17

    #i ncl ude #i ncl ude "conf i g. h"

    #def i ne _XTAL_ FREQ 8000000 / / The speed of cl ock af t er set up

    i nt i = 0;voi d Del ay1Second( voi d) ;voi d Set upCl ock( voi d) ;

    voi d mai n( i nt ar gc, char ** argv) {

    Set upCl ock( ) ;

    TRI SBbi t s. RB0 = 0; / / set RB0 as out put

    TRI SBbi t s. RB1 = 1; / / set RB1 as i nput whi l e( 1) / / i nf i ni t e l oop { i f( PORTBbi t s. RB1) / / i f RB1 hi gh, t hen make RB0 hi gh LATB0 = 1; el se LATB0 = 0; }}

    voi d Del ay1Second( ){ f or ( i =0; i

  • 7/21/2019 Lm Qun Trnh Dch XC8

    9/17

    Share this:

    Categories: ElectronicsTags: IO, PIC LAT, PIC18, Read from Pin, XC8 compiler

    Comments (22)Trackbacks (0)Leave a commentTrackback

    tonyMay 30th, 2014 at 05:59 | #1Reply| Quote

    Hello,

    I started using Pic18f4550 with xc8 compiler. In the above program you first declared theinternal oscillator freq in conf file. when you declared the new freq #define

    _XTAL_FREQ 1000000, should i remove the internal oscillator declaration from configfile. if no, which frequency does the compiler consider and why. I am new to uc

    programming, excuse me for basic doubts

    Thanks

    1.

    Supreeth

    March 25th, 2014 at 02:17 | #2Reply| Quote

    Thank you for your reply. Why we cant use huge parameters in the delay? Something like__delay_ms(1000);

    singularengineerMarch 25th, 2014 at 05:32 | #3

    Reply| Quote

    Thats why using the delay for loop. _delay. is a macro. you can define your own ifyou want more delay.

    2.

    SupreethMarch 24th, 2014 at 06:03 | #4Reply| Quote

    3.

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

    ng 17 10/29/2014

  • 7/21/2019 Lm Qun Trnh Dch XC8

    10/17

    Hi this tutorial really helped me. But I am having problem. when I am using LATB=0; itis working fine but when I use LATBbits_t.LATB0=1; it is showing no identifier indeclaration 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.

    singularengineerMarch 24th, 2014 at 15:48 | #5Reply| Quote

    LATBbits_t is template. You cant use template like that. Use LATBbits.LATB0(thats why _t means template to microchips naming convention). Please checkthe declaration and source files which will answer a lot.

    AlessandroFebruary 14th, 2014 at 08:02 | #6Reply| 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 messagein output:make[2]: waiting for unfinished joband 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 thathappens? thank you

    singularengineerFebruary 14th, 2014 at 09:25 | #7Reply| Quote

    Never came across a problem like that. Give it a try in Microchip forum and youll get

    help.

    4.

    AlessandroFebruary 13th, 2014 at 19:30 | #8Reply| Quote

    Im sorry, I hadnt the datasheet with me. I checked it and I saw that also RA2 pin has thatthing. I wrote ANSEL=0b00000000 and it works! Im sorry for my ignorance,

    5.

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

    ng 17 10/29/2014

  • 7/21/2019 Lm Qun Trnh Dch XC8

    11/17

    unfortunately I havent got so much time as I wish to dedicate to this great world, so Icouldnt read all parts of the datasheet. Thank you so much! All tutorial is great!

    AlessandroFebruary 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 iton all A bank (like in the program i posted, where I was using RA2). for example if iwriteLATC0=1; the led turns on without problems and this is true for all 4 leds (on RC0-3 pins)

    but if i writeif(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 toconnect the input directly to Vdd (or ground). Thats the problem, it seems that on inputthe logical state doesnt change

    6.

    singularengineerFebruary 13th, 2014 at 13:36 | #10Reply| Quote

    @Alessandro

    The pins you are trying to use as IO are used by ADC by default. If you go into thedatasheet of 18F14K22 and check page 88, there is a note (in a box)saying that after resetthe PORT C pins from RC0-RC3 and RC6 and RC7 will become analog input pins. Pleaseturn off ADC on the pins you want to use as Output and it will start working. In order to setthe pins as DIO, please read the chapter 8.4 and ADC section in the datasheet for18F14k22 (the PIC you are using).

    7.

    AlessandroFebruary 13th, 2014 at 12:56 | #11Reply| Quote

    god why the include between are not shown? by the way they are xc.h and stdio.h

    8.

    9.

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

    ng 17 10/29/2014

  • 7/21/2019 Lm Qun Trnh Dch XC8

    12/17

    AlessandroFebruary 13th, 2014 at 12:54 | #12Reply| Quote

    sorry for the mistake, two #include directives are not shown in my message below buttheyre present in project. they are #include and #include

    AlessandroFebruary 13th, 2014 at 12:50 | #13Reply| Quote

    Hi and thank you for your great tutorials. Im having a problem on programming aPIC18F14K22, I am using the PicKit3 with his own demoboard: I cant use the inputs inany way. I try to explain it better: I tryed some simple programs with only outputs (RC0-4are 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 thedemoboard (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 50void 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;elseLATC0 = 0;

    10.

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

    ng 17 10/29/2014

  • 7/21/2019 Lm Qun Trnh Dch XC8

    13/17

    }}It doesnt show any warning or problem, all build and programming went good, it seemsthat the input doesnt reach the Vih voltage but thats not true because I checked it with thetester and 4.97V reach that pin. It doesnt seems to be the MCU, because that thinghappened 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, bestregards

    SebastianOctober 18th, 2013 at 21:19 | #14Reply| Quote

    Great tutorial!!

    I have a question though, Is it possible to refer to a single register bit by index? Porinstance, for the TRISBbits.RB0 something like TRISB(0)???

    Thanks!

    singularengineerOctober 18th, 2013 at 22:26 | #15

    Reply| Quote

    The TRISx list is actually a structure. If you right click and go to declaration ofTRISx, you will be able to see the source and find a way to define the way you want.TRISBbits.RBO already addresses one bit.

    11.

    MehmetAugust 15th, 2013 at 07:58 | #16Reply| Quote

    Thanks so much!!

    Thanks to you, i can understand clearly how can Programming PIC 18 using XC8 !

    I am from stanbul/TRKYEsoExcuse my bad English!

    12.

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

    ng 17 10/29/2014

  • 7/21/2019 Lm Qun Trnh Dch XC8

    14/17

    Regards

    SJKJune 17th, 2013 at 17:51 | #17Reply| Quote

    i use pic18f4550.i have read datasheet but it cant make me clear.if you give a outline withscreen short that must help me

    singularengineerJune 17th, 2013 at 23:02 | #18Reply| Quote

    use this file : http://wp.me/a3oMLT-3Gand change FOSC to #pragma configFOSC=HS

    13.

    SJKJune 17th, 2013 at 02:50 | #19Reply| Quote

    can you give an outline of configuration of 20Mhz external crystal? I cant understand whatI do in CONFIG1L resister? I dont want to enable PLL.

    singularengineerJune 17th, 2013 at 09:08 | #20Reply| Quote

    Here is a microchip device documentation on oscillators: http://ww1.microchip.com/downloads/en/devicedoc/31002a.pdfWhich 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 = HSAlso please read microchip forums where you have a vast resources.

    ~SE

    14.

    younesser15.

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

    ng 17 10/29/2014

  • 7/21/2019 Lm Qun Trnh Dch XC8

    15/17

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

    this instruction is not recognized by the compiler : #include config.h

    Thanks

    younesser

    singularengineerJune 10th, 2013 at 12:27 | #22Reply| Quote

    Younesser,You need to make your own config.h. Here is the config.h I used for mine which isspecific to PIC18F4550 and uses internal oscillator. Also you can refer to my previousposts on how to create your config file.

    file link (for config.h) : http://wp.me/a3oMLT-3Glink for making your own : http://singularengineer.com/programming-pic-18-using-xc8-mplab-x-configuration-bits-2/

    ~SE

    No trackbacks yet.1.

    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) : InterruptsProgramming PIC 18 using XC8(MPLAB X) : Setting Configuration BitsRSS

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

    ng 17 10/29/2014

  • 7/21/2019 Lm Qun Trnh Dch XC8

    16/17

    Twitter

    Search for:

    Recent Posts

    dsPIC/PIC24 Timers (using XC16 and MPLAB X)July 12, 2014dsPIC/PIC24 Interrupts (using XC16 and MPLAB X)June 29, 2014

    dsPIC/PIC24 Configuration Bits and Oscillator Settings(using XC16 and MPLAB X )February 5, 2014Why 16 bit controllers?November 18, 2013Programming PIC 18 using XC8 (MPLAB X) : ADC (using adc.h)May 26, 2013Programming PIC 18 using XC8 (MPLAB X) : USART (PIC18F Serial)May 15, 2013Programming PIC 18 using XC8 (MPLAB X) : PWM (using pwm.h)May 13, 2013Programming PIC 18 using XC8 (MPLAB X) : TimersMay 7, 2013Programming PIC 18 using XC8 (MPLAB X) : InterruptsApril 28, 2013Programming PIC 18 using XC8 (MPLAB X) : IO portsApril 14, 2013

    Recent Comments

    singularengineeron TutorialsChinmay on TutorialsChinmay on TutorialsDaniel on Programming PIC 18 using XC8 (MPLAB X) : Interruptsomran on Programming PIC 18 using XC8 (MPLAB X) : Setting Configuration Bits

    Archives

    July 2014June 2014February 2014

    November 2013May 2013April 2013October 2012September 2012

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

    ng 17 10/29/2014

  • 7/21/2019 Lm Qun Trnh Dch XC8

    17/17

    Categories

    Electronics

    Categories

    Electronics

    Blogroll

    Archives

    July 2014June 2014February 2014

    November 2013

    May 2013April 2013October 2012September 2012

    Meta

    Log in

    TopWordPressCopyright 2012-2014 Singular EngineerTheme byNeoEase. Valid XHTML 1.1and CSS 3.

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