element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Embedded and Microcontrollers
  • Technologies
  • More
Embedded and Microcontrollers
Pic Microcontrollers Forum Upload code to PIC
  • Blog
  • Forum
  • Documents
  • Quiz
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Embedded and Microcontrollers to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Not Answered
  • Replies 4 replies
  • Subscribers 194 subscribers
  • Views 589 views
  • Users 0 members are here
  • compile
  • upload
  • pic
Related

Upload code to PIC

Former Member
Former Member over 13 years ago

Hello,

 

Newly I just trying to make something with PIC.

I have some newbie question, I hope I can get the solution from you all. image

 

Do anybody here know how to upload code (like in the Arduino) to PIC18F452?

 

I only have PIC2 Programmer and both MPLAB and PicKit2 software in PC, can I upload the code to the PIC18F452 only in breadboard?

Or do I need other tools?

 

Thank you.

  • Sign in to reply
  • Cancel
Parents
  • YT2095
    0 YT2095 over 13 years ago

    That`s all I use for my projects most of the time too image

    although I Do use GCBasic for almost all of my PIC programming.

    in MPlab, assuming you have a program to upload, you simply go into Configure and pick from the list what Chip you are using, then you choose what programmer you`re using in "select programmer".

    click on Quick Build, then click Program.

    a few seconds later if all goes well it`ll be programmed!

     

    this is an example I did here: http://www.element14.com/community/people/YT2095/blog/2012/04/13/surprisingly-simple-pic-development

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 13 years ago in reply to YT2095

    Thank you, now I can make a led blink at least image

     

    But I actually want to use a servo, so I uploaded the source code successfully but there is no response from the connecting servo.

    I connect to servo to RD0 and RD1

     

    Here is a bit of my code that I suspected contain the errors.

    void main(void)
    {
    //Set I/O ports
    TRISD = 0x00;
    //Clear Output Ports
    PORTD = 0x00;

     

    void InterruptHandlerHigh()      // Declaration of InterruptHandler
    {      //This gets run when either timer overflows from FFFF->0000
    if(INTCONbits.TMR0IF)      //check if TMR0 interrupt flag is set
    {
    WriteTimer0( 0x3CAF ); //Reset Timer0 for 20mS Delay
    WriteTimer1( 0xFC77 ); //Reset Timer1 for 1mS Delay
    count = 0;      //Reset Timer1 Interrupt Counter
    INTCONbits.TMR0IF = 0;     //Clear TMR0 Flag 
    }
    if(PIR1bits.TMR1IF == 1 && PIE1bits.TMR1IE == 1) 
    {      //If Timer1 interrupts, do your thing...
    count++;      //Increment Timer1 Interrupt Counter
    switch(count){      //Choose which servo to modify:
    case 1: PORTD = 0x01; 
    WriteTimer1( servo0 );     //Right Eyebrow
    break;
    case 2:      PORTD = 0x02;
    WriteTimer1( servo1 );     //Left Eyebrow
    break;
    case 3:      PORTD = 0x00;
    WriteTimer1( 0 );
    break;
    }

     

     

    And here is the whole code.

    #include <p18f452.h>
    #include <timers.h>
    #include <delays.h>
    
    
    //Location Definitions Used For Clarity
    #define right_level 0xF100
    #define right_down 0xF400
    #define right_up 0xEB00
    
    
    #define left_level 0xF000
    #define left_down 0xED00
    #define left_up 0xF600
    
    
    /*
    Locations:
    servo0 - Right Eyebrow:
    0xF100 - Straight
    0xF400 - Angle Down
    0xEB00 - Angle Up
    
    
    servo1 - Left Eyebrow:
    0xF000 - Straight
    0xED00 - Angle Down
    0xF600 - Angle Up
    */
    
    
    //The servos are all set to go to an initial 
    //state when the system is powered on
    
    
    int servo0 = right_level; // Right Eyebrow
    int servo1 = left_level; // Left Eyebrow
    
    
    int count = 0;      //Counts every Timer1 Interrupt
    
    
    //Declare all functions 
    void move(unsigned int,unsigned int);
    void slow_move(unsigned int);
    void delay(unsigned int);
    void InterruptHandlerHigh (void);
    
    
    void main(void)
    {
    //Set I/O ports
    TRISD = 0x00;
    //Clear Output Ports
    PORTD = 0x00;
    
    
    //Setup Interrupts
    RCON = 0b000000000; 
    INTCON = 0b10100000;
    
    
    //Setup Timers 0 & 1
    OpenTimer0( TIMER_INT_ON & T0_16BIT & T0_SOURCE_INT & T0_PS_1_2 );
    OpenTimer1( TIMER_INT_ON & T1_16BIT_RW & T1_SOURCE_INT & T1_PS_1_2 & T1_OSC1EN_OFF & T1_SYNC_EXT_OFF );
    //Initialize Timers 0 & 1
    WriteTimer0( 0x3CAF );
    WriteTimer1( 0xF63B );
    
    
    //Pre-Programmed Movements
    
    
    while(1){
    
    
    move(right_level,left_level);
    
    
    delay(2);     //Delay 3 Seconds
    
    
    //3 Sequences
    move(right_up,left_up);
    
    
    delay(2);     //Delay 3 Seconds
    
    
    move(right_up,left_level);
    
    
    delay(2);     //Delay 3 Seconds
    
    
    move(right_up,left_down);
    
    
    delay(2);     //Delay 3 Seconds
    
    
    //3 Sequences
    move(right_level,left_up);
    
    
    delay(2);     //Delay 3 Seconds
    
    
    move(right_level,left_level);
    
    
    delay(2);     //Delay 3 Seconds
    
    
    move(right_level,left_down);
    
    
    delay(2);     //Delay 3 Seconds
    
    
    //3 Sequences
    move(right_down,left_up);
    
    
    delay(2);     //Delay 3 Seconds
    
    
    move(right_down,left_level);
    
    
    delay(2);     //Delay 3 Seconds
    
    
    move(right_down,left_down);
    
    
    delay(2);     //Delay 3 Seconds
    
    
    //**********//
    
    
    slow_move(1);
    
    
    delay(2);     //Delay 3 Seconds
    
    
    slow_move(2);
    
    
    delay(2);     //Delay 3 Seconds
    
    
    slow_move(4);
    
    
    delay(2);     //Delay 3 Seconds
    
    
    slow_move(3);
    
    
    delay(25);
    
    
    }
    
    
    }
    
    
    //The move function sets all servo values
    void move(unsigned int one, unsigned int two)
    {
    if(one)
    servo0 = one;// Right Eyebrow
    if(two)
    servo1 = two;// Left Eyebrow
    }
    
    
    //Slow Move function moves the servo with
    //a more graduate and fluid movement
    void slow_move(unsigned int location){
    int i=0;
    //Top to Bottom
    if(location == 1){
    servo0 = right_up;
    servo1 = left_up;
    
    
    Delay1KTCYx(250);
    Delay1KTCYx(250);
    
    
    for(i=0;i<2304;i++){
    servo0 = servo0 + 1;
    servo1 = servo1 - 1;
    Delay1KTCYx(20);
    }
    }
    // Bottom To Top
    else if(location == 2){
    servo0 = right_down;
    servo1 = left_down;
    
    
    Delay1KTCYx(250);
    Delay1KTCYx(250);
    
    
    for(i=0;i<2304;i++){
    servo0 = servo0 - 1;
    servo1 = servo1 + 1;
    Delay1KTCYx(20);
    }
    }
    //Top To Middle
    else if(location == 3){
    servo0 = right_up;
    servo1 = left_up;
    
    
    Delay1KTCYx(250);
    Delay1KTCYx(250);
    
    
    for(i=0;i<1536;i++){
    servo0 = servo0 + 1;
    servo1 = servo1 - 1;
    Delay1KTCYx(20);
    }
    }
    //Middle To Bottom
    else if(location == 4){
    servo0 = right_level;
    servo1 = left_level;
    
    
    Delay1KTCYx(250);
    Delay1KTCYx(250);
    
    
    for(i=0;i<768;i++){
    servo0 = servo0 + 1;
    servo1 = servo1 - 1;
    Delay1KTCYx(20);
    }
    }
    }
    
    
    
    
    //INTERRUPT CONTROL
    #pragma code InterruptVectorHigh = 0x08      //interrupt pointer address (0x18 low priority)
    void InterruptVectorHigh (void)
    {
    _asm      //assembly code starts
    goto InterruptHandlerHigh     //interrupt control
    _endasm      //assembly code ends
    }
    #pragma code
    #pragma interrupt InterruptHandlerHigh      //end interrupt control
    
    
    void InterruptHandlerHigh()      // Declaration of InterruptHandler
    {      //This gets run when either timer overflows from FFFF->0000
    if(INTCONbits.TMR0IF)      //check if TMR0 interrupt flag is set
    {
    WriteTimer0( 0x3CAF ); //Reset Timer0 for 20mS Delay
    WriteTimer1( 0xFC77 ); //Reset Timer1 for 1mS Delay
    count = 0;      //Reset Timer1 Interrupt Counter
    INTCONbits.TMR0IF = 0;     //Clear TMR0 Flag 
    }
    if(PIR1bits.TMR1IF == 1 && PIE1bits.TMR1IE == 1) 
    {      //If Timer1 interrupts, do your thing...
    count++;      //Increment Timer1 Interrupt Counter
    switch(count){      //Choose which servo to modify:
    case 1: PORTD = 0x01; 
    WriteTimer1( servo0 );     //Right Eyebrow
    break;
    case 2:      PORTD = 0x02;
    WriteTimer1( servo1 );     //Left Eyebrow
    break;
    case 3:      PORTD = 0x00;
    WriteTimer1( 0 );
    break;
    }
    
    
    PIR1bits.TMR1IF = 0;      //Clear Timer1 flag
    PIE1bits.TMR1IE = 1;      //Make Sure Timer1 Interrupt Still Enabled
    }
    
    
    INTCONbits.GIE = 1;      //Re-enable all interrupts
    }
    
    
    //Delay In Seconds with 20 MHz Clock
    void delay(unsigned int delay_val){
    unsigned int i=0;
    for(i=0;i<delay_val;i++){
    Delay10KTCYx(250);
    Delay10KTCYx(250);
    }
    }

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • YT2095
    0 YT2095 over 13 years ago in reply to Former Member

    I don`t do C, so I can`t really help you with any of that code, but here`s a simple program in ASM, that will turn a single servo full direction each way in a loop, this will test that all your hardware is working perfectly at least, I`v set it for your chip and also 20MHz.

     

    ;Program compiled by Great Cow BASIC (0.9 7/2/2010)

     

    ;********************************************************************************

     

    ;Set up the assembler options (Chip type, clock source, other bits and pieces)

    LIST p=18F452, r=DEC

    #include <P18F452.inc>

    CONFIG LVP = OFF, WDT = OFF, OSC = HS

     

    ;********************************************************************************

     

    ;Set aside memory locations for variables

    DELAYTEMP    EQU    0

    DELAYTEMP2    EQU    1

    SysWaitTemp10US    EQU    5

    SysWaitTempMS    EQU    2

    SysWaitTempMS_H    EQU    3

    TEMP    EQU    4

     

    ;********************************************************************************

     

    ;Vectors

        ORG    0

        goto    BASPROGRAMSTART

        ORG    8

        retfie

     

    ;********************************************************************************

     

    ;Start of program memory page 0

        ORG    12

    BASPROGRAMSTART

    ;Call initialisation routines

        rcall    INITSYS

     

    ;Start of the main program

        bcf    TRISD,3,ACCESS

    SysDoLoop_S1

        movlw    64

        movwf    TEMP,BANKED

    SysForLoop1

        incf    TEMP,F,BANKED

        bsf    PORTD,3,ACCESS

        movff    TEMP,SysWaitTemp10US

        rcall    Delay_10US

        bcf    PORTD,3,ACCESS

        movlw    6

        movwf    SysWaitTempMS,ACCESS

        clrf    SysWaitTempMS_H,ACCESS

        rcall    Delay_MS

        movlw    225

        subwf    TEMP,W,BANKED

        btfss    STATUS, C,ACCESS

        bra    SysForLoop1

        movlw    226

        movwf    TEMP,BANKED

    SysForLoop2

        decf    TEMP,F,BANKED

        bsf    PORTD,3,ACCESS

        movff    TEMP,SysWaitTemp10US

        rcall    Delay_10US

        bcf    PORTD,3,ACCESS

        movlw    6

        movwf    SysWaitTempMS,ACCESS

        clrf    SysWaitTempMS_H,ACCESS

        rcall    Delay_MS

        movf    TEMP,W,BANKED

        sublw    65

        btfss    STATUS, C,ACCESS

        bra    SysForLoop2

        bra    SysDoLoop_S1

    SysDoLoop_E1

    BASPROGRAMEND

        sleep

        bra    $

     

    ;********************************************************************************

     

    Delay_10US

    D10US_START

        movlw    15

        movwf    DELAYTEMP,ACCESS

    DelayUS0

        decfsz    DELAYTEMP,F,ACCESS

        bra    DelayUS0

        nop

        decfsz    SysWaitTemp10US, F,ACCESS

        bra    D10US_START

        return

     

    ;********************************************************************************

     

    Delay_MS

        incf    SysWaitTempMS_H, F,ACCESS

    DMS_START

        movlw    227

        movwf    DELAYTEMP2,ACCESS

    DMS_OUTER

        movlw    6

        movwf    DELAYTEMP,ACCESS

    DMS_INNER

        decfsz    DELAYTEMP, F,ACCESS

        bra    DMS_INNER

        decfsz    DELAYTEMP2, F,ACCESS

        bra    DMS_OUTER

        decfsz    SysWaitTempMS, F,ACCESS

        bra    DMS_START

        decfsz    SysWaitTempMS_H, F,ACCESS

        bra    DMS_START

        return

     

    ;********************************************************************************

     

    INITSYS

        clrf    BSR,ACCESS

        clrf    TBLPTRU,ACCESS

        bcf    ADCON0,ADON,ACCESS

        bcf    ADCON1,ADFM,ACCESS

        bcf    ADCON1,PCFG3,ACCESS

        bsf    ADCON1,PCFG2,ACCESS

        bsf    ADCON1,PCFG1,ACCESS

        bcf    ADCON1,PCFG0,ACCESS

        clrf    PORTA,ACCESS

        clrf    PORTB,ACCESS

        clrf    PORTC,ACCESS

        clrf    PORTD,ACCESS

        clrf    PORTE,ACCESS

        return

     

     

    just load this into MPlab and hit Quick Build, it should compile into hex without a problem so that you burn it to you chip.

    and yes I know it looks HUGE! but it`s really quite small, here`s the source:

     

    ;Chip Settings

    #chip 18F452,20

     

    ;Defines (Constants)

    #define ServoPin PORTD.3

    #define ServoMin 65

    #define ServoMax 225

     

    Dir ServoPin Out

    Do Forever

        For Temp = ServoMin to ServoMax

            PulseOut ServoPin, Temp 10us

            Wait 6 ms

        Next

        For Temp = ServoMax to ServoMin

            PulseOut ServoPin, Temp 10us

            Wait 6 ms

        Next

    Loop

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • nermash
    0 nermash over 13 years ago in reply to Former Member

    Why did you connect servo to two outputs? Servo only needs one control signal input. If I would to use pic to control a servo, I would just use PWM modulator capability built into pic, and adjust the duty cycle depending on the desired position of the armature.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Reply
  • nermash
    0 nermash over 13 years ago in reply to Former Member

    Why did you connect servo to two outputs? Servo only needs one control signal input. If I would to use pic to control a servo, I would just use PWM modulator capability built into pic, and adjust the duty cycle depending on the desired position of the armature.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Children
No Data
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube