CrazyEngineers
  • Learning PIC Coding using C Language

    shahrul

    Member

    Updated: Oct 26, 2024
    Views: 958
    Here, I'm sharing PIC C Language for coding various type of hardware. I'm using MPLab Software with Hi-Tech Compiler.

    Firstly, I start with LED. You can connect LED to PIC by 2 ways.
    [​IMG]
    Let say you connect Active High LED on Port B0. Signal High will turn 'ON' LED and signal Low will turn 'OFF' LED.
    You can program like
    RB0=1;      //Turn ON LED
    RB0=0;      //Turn OFF LED
    or
    PORTB=0b00000001;      //Turn ON LED
    PORTB=0b00000000;      //Turn OFF LED
    To do LED Blinking
    for( ; ; ){  //loop forever
               PORTB=0b00000001;      //Turn ON LED
               for(i=0;i<=100;i++) __delay_ms(10);
               PORTB=0b00000000;      //Turn OFF LED
               for(i=0;i<=100;i++) __delay_ms(10);}
    
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • shahrul

    MemberJan 7, 2010

    How to connect seven segment display?
    Actually, seven segment display not much differ with LED. The seven segment display is only like 8 LED's. 2 way of connection.
    [​IMG]

    You do 'ON' and 'OFF' depending the position of the LED. Convert into this function.
    char convert(char dec)
    {char segment;
    switch(dec){
    case 0:segment=0b11111100;break;
    case 1:segment=0b01100000;break;
    case 2:segment=0b11011010;break;
    case 3:segment=0b11110010;break;
    case 4:segment=0b01100110;break;
    case 5:segment=0b10110110;break;
    case 6:segment=0b10111110;break;
    case 7:segment=0b11100000;break;
    case 8:segment=0b11111110;break;
    case 9:segment=0b11110110;break;}
    return segment;
    }
    Are you sure? This action cannot be undone.
    Cancel
  • Codes Guides

    MemberJan 17, 2010

    Good posts. In my experience, PICs do better sinking current than sourcing. I would recommend connecting the LEDs so that the PIC sinks the currents.

    - CG
    #-Link-Snipped-#
    Are you sure? This action cannot be undone.
    Cancel
  • shahrul

    MemberJan 18, 2010

    Codes Guides
    Good posts. In my experience, PICs do better sinking current than sourcing. I would recommend connecting the LEDs so that the PIC sinks the currents.
    Yes, that's good. Thank you for sharing experience. I know but I did't follow, I still do sourcing current.
    Are you sure? This action cannot be undone.
    Cancel
  • Codes Guides

    MemberJan 19, 2010

    Newer PICs may be better. Old ones used to drop a lot of voltage when sourcing current.
    Are you sure? This action cannot be undone.
    Cancel
  • shahrul

    MemberFeb 11, 2010

    How to read push button?
    You can connect push button in two ways.
    [​IMG]
    At example, I have 4 push button, then I convert the input into value. Any push button need to press and released.
    int read_button()
    {int i;
    if(PB1==0){
        while(PB1==0) continue;    //wait until PB1 release
        i=1;}
    else if(PB2==0){
        while(PB2==0) continue;    //wait until PB2 release
        i=2;}
    else if(PB3==0){
        while(PB3==0) continue;    //wait until PB3 release
        i=3;}
    else if(PB4==0){
        while(PB4==0) continue;    //wait until PB4 release
        i=4;}
    else i=0;
    return i;
    }
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register