1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

New using PIC18F4520 with C Programming

Discussion in 'Microcontrollers' started by mrfunkyjay, Apr 22, 2008.

  1. arhi

    arhi Member

    Joined:
    Apr 17, 2008
    Messages:
    887
    Likes:
    12
    Location:
    Belgrade, .rs
    yup, I use C 99.9% of time but I was thinking more in course of
    Code (text):

    ...
    while(1){
    ...
      asm('rrf PORTD,1');
    ...
    }
     
    saves the ram for that extra variable you would use to rotate/shift and then assign to port..
     
  2. mrfunkyjay

    mrfunkyjay New Member

    Joined:
    Mar 26, 2008
    Messages:
    123
    Likes:
    0
    Location:
    Jakarta, Indonesia
    Thanks!! I know know how it works, wanna try some. Thanks!!
     
  3. mrfunkyjay

    mrfunkyjay New Member

    Joined:
    Mar 26, 2008
    Messages:
    123
    Likes:
    0
    Location:
    Jakarta, Indonesia
    The first program of mine!!! With helps from u guys! Thank u so much!!

    I need to change to PIC18F4580 because my PIC18F4520 is worn out. Hmm...

    Code (text):

    /*This program tells a series of LEDs in PORTD to flash (by pressing Push Button RA4) one by one in forward direction, from RD0 to RD7.*/
    /*When it reaches RD7, backward direction executed from RD7 to RD0 again. When PB RA4 is pressed again, the whole system stop. */

    #include <p18f4580.h>
    #include <delays.h>

    #pragma config OSC = HS
    #pragma config WDT = OFF
    #pragma config LVP = OFF

    int i, iKey, iPrevious;

    unsigned char cShift=1; /*Declaring shift bit interval*/
    unsigned char cPBState=1; /*State of the Push Button*/

    void vFlashing(void); /*Declaring flashing mode1*/

    /*Flashing Mode*/
    void vFlashing(void)

    {
        if (cShift==15) /*Count cShift to 15x*/
        {
            cShift=1; /*Restarting cShift value*/
        }

        if (cShift<8) /*When cShift smaller than 8*/
        {
            LATD=0b00000001<<cShift; /*Forward flash*/
            Delay10KTCYx(50); /*Delays*/
            cShift++; /*Increment cShift*/
        }
        else /*When cShift bigger than 8*/
        {
            LATD=0b10000000>>(cShift-7); /*Backward flash*/
            Delay10KTCYx(50); /*Delays*/
            cShift++; /*Increment cShift*/
        }
    }

    /*Main Function*/
    void main(void)

    {  
        /*Settings*/
        TRISD=0b00000000; /*Declaring all PORTD as an Output Port*/
        TRISA=0b00010000; /*Declaring RA4 as an Input Port*/
       
        /*Initializing*/
        iKey=0;
        cPBState=1;

        while(1) /*Loop Forever*/
        {
            iPrevious=iKey; /*Copy the last value*/
            iKey=PORTAbits.RA4; /*Assigning new value*/

            /*Push Button RA4 Function ON/OFF Button*/
            if(iKey==0 && iPrevious==1) /*Getting information*/
            {
                if(cPBState==0) /*Changing the PBState*/
                {
                    cPBState=1;
                }
                else
                {
                    cPBState=0;
                }
            }

                switch (cPBState) /*Switch function for cases*/
                {  
                    case 0:
                    vFlashing(); /*If case 0, do flashing1*/
                    break;
                   
                    case 1: /*If case 1, do nothing*/
                    break;
                   
                    default:
                    break;
                }
        }
    }
     
    Okay, I want to ask a question now:

    If I want this thing on a small pcb (not on a demo board), is it possible to make PCB layout according to the data sheet of this MC and make all the Vcc and GND and all of their routes, making PORTD as an output, say I connect them with connectors so I can drive several LEDs??

    Is that how these things work??? Is that how ppl out there makin MCU as "brain" of their PCB or system, I would say. ??
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. hong9088

    hong9088 New Member

    Joined:
    Nov 16, 2008
    Messages:
    1
    Likes:
    0

    Help is needed. Thanks :)

    Hey sorry i'm new here. Just to ask how do i make the LED blink(using C program statements) Light up a combination of LEDs on the target board(using C program statements) Light up a LED when a switch is pressed (using C program statements). I'm a student and i'm doing some research on it. But i can't get to find any help regarding the Question above. Can anyone help me out? Thanks lots:)
     
  6. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,162
    Likes:
    340
    Location:
    Brisbane Australia
    ONLINE
    To get you started, clicky.

    Mike.
     

Share This Page