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

How to use Interrupt in PIC16F877A in C..??

Discussion in 'Microcontrollers' started by koolguy, Jan 14, 2013.

  1. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    You know my comments.... I wouldn't even try to to this without interrupts....
     
  2. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    How should i add interrupt in this...
    sorry to say i am still confuse with that tmr2 overflow i am not able to understand why at delay only it is going to ISr routine??
    it will be easier to learn isr with a very basic program...
     
  3. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    The reason the interrupt "interrupts" the program flow during the delay, is because the delay is the biggest part of the program flow.... The interrupt is fired when TMR2 overflows... TMR2 is running in the background and every machine cycle ( or pe-scaled cycles) the timer is increased..... when it gets to the value in PR2 it interrupts the main program, wherever it may be...
     
  4. dave

    Dave New Member

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


     
  5. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9

    Hi,

    my new code....working fine with row scanning and left right up down....
    need your help while scrolling left the default storage may be coming on display pls tell how to remove it!!
    and while using the bit shift>> and << the bit goes out from it i mean how to make it like circular array......

    Code (text):

    #include <htc.h>
    __CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS);
    #define _XTAL_FREQ 20000000
    #define DAD PORTB
    #define RST RC6
    #define CLK RC3
    #define DATA RC4
    extern const char Font2[];unsigned char leds[31];unsigned char pow[] = {128,64,32,16,8,4,2,1};
    display(char *str);int data( int p);int t;char b,e;void clear(void);
    main ()
        {
        TRISC=0B00000000;
        TRISB=0B00000000;
        while(1)
            {
            display("AKON>");
            for(char t=0;t<10;t++)
                {
                for(char y=0;y<10;y++)
                    {
                    RST=0;
                    RST=1;
                    char u=-1;
                    b=0b00000001;
                    for(int r=0;r<8;r++)
                        {
                        DAD=pow[r];
                        __delay_ms(2);
                        RST=0;
                        RST=1;
                        u++;
                        for(int a=0;a<32;a++)
                            {
                            DAD=0;
                            RST=1;
                            char t,n;
                            n=leds[a];
                            n>>=u;
                            t=(n & b);
                            if(t==0){DATA=0;}
                            if(t==1){DATA=1;}
                            CLK=1;
                            __delay_us(10);
                            CLK=0;
                            }
                        }
                    }
                for(char t=0;t<5;t++)
                    {
                    leds[t]>>=1;
                    __delay_ms(1);
                    }
                }
            clear();
            display("AKON>");
            for(char t=0;t<10;t++)
                {
                for(char y=0;y<10;y++)
                    {
                    RST=0;
                    RST=1;
                    char u=-1;
                    b=0b00000001;
                    for(int r=0;r<8;r++)
                        {
                        DAD=pow[r];
                        __delay_ms(2);
                        RST=0;
                        RST=1;
                        u++;
                        for(int a=0;a<32;a++)
                            {
                            DAD=0;
                            RST=1;
                            char t,n;
                            n=leds[a];
                            n>>=u;
                            t=(n & b);
                            if(t==0){DATA=0;}
                            if(t==1){DATA=1;}
                            CLK=1;
                            __delay_us(10);
                            CLK=0;
                            }
                        }
                    }
                for(char t=5;t<11;t++)
                    {
                    leds[t]>>=1;
                    __delay_ms(1);
                    }
                }
            clear();
            display("AKON>");

            for(char t=0;t<10;t++)
                {
                for(char y=0;y<10;y++)
                    {
                    RST=0;
                    RST=1;
                    char u=-1;
                    b=0b00000001;
                    for(int r=0;r<8;r++)
                        {
                        DAD=pow[r];
                        __delay_ms(2);
                        RST=0;
                        RST=1;
                        u++;
                        for(int a=0;a<32;a++)
                            {
                            DAD=0;
                            RST=1;
                            char t,n;
                            n=leds[a];
                            n>>=u;
                            t=(n & b);
                            if(t==0){DATA=0;}
                            if(t==1){DATA=1;}
                            CLK=1;
                            __delay_us(10);
                            CLK=0;
                            }
                        }
                    }
                for(char t=11;t<17;t++)
                    {
                    leds[t]>>=1;
                    __delay_ms(1);
                    }
                }
            clear();
            display("AKON>");
            for(char t=0;t<10;t++)
                {
                for(char y=0;y<10;y++)
                    {
                    RST=0;
                    RST=1;
                    char u=-1;
                    b=0b00000001;
                    for(int r=0;r<8;r++)
                        {
                        DAD=pow[r];
                        __delay_ms(2);
                        RST=0;
                        RST=1;
                        u++;
                        for(int a=0;a<32;a++)
                            {
                            DAD=0;
                            RST=1;
                            char t,n;
                            n=leds[a];
                            n>>=u;
                            t=(n & b);
                            if(t==0){DATA=0;}
                            if(t==1){DATA=1;}
                            CLK=1;
                            __delay_us(10);
                            CLK=0;
                            }
                        }
                    }
                for(char t=18;t<23;t++)
                    {
                    leds[t]>>=1;
                    __delay_ms(1);
                    }
                }
            clear();
            display("AKON>");
            for(char t=0;t<10;t++)
                {
                for(char y=0;y<10;y++)
                    {
                    RST=0;
                    RST=1;
                    char u=-1;
                    b=0b00000001;
                    for(int r=0;r<8;r++)
                        {
                        DAD=pow[r];
                        __delay_ms(2);
                        RST=0;
                        RST=1;
                        u++;
                        for(int a=0;a<32;a++)
                            {
                            DAD=0;
                            RST=1;
                            char t,n;
                            n=leds[a];
                            n>>=u;
                            t=(n & b);
                            if(t==0){DATA=0;}
                            if(t==1){DATA=1;}
                            CLK=1;
                            __delay_us(10);
                            CLK=0;
                            }
                        }
                    }
                for(char t=0;t<32;t++)
                    {
                    leds[t]>>=1;
                    }
                }
            clear();
            display("AKON>");
            for(char t=0;t<10;t++)
                {
                for(char y=0;y<10;y++)
                    {
                    RST=0;
                    RST=1;
                    char u=-1;
                    b=0b00000001;
                    for(int r=0;r<8;r++)
                        {
                        DAD=pow[r];
                        __delay_ms(2);
                        RST=0;
                        RST=1;
                        u++;
                        for(int a=0;a<32;a++)
                            {
                            DAD=0;
                            RST=1;
                            char t,n;
                            n=leds[a];
                            n>>=u;
                            t=(n & b);
                            if(t==0){DATA=0;}
                            if(t==1){DATA=1;}
                            CLK=1;
                            __delay_us(1);
                            CLK=0;
                            }
                        }
                    }
                for(char t=0;t<32;t++)
                    {
                    leds[t]<<=1;
                    }
                }

            display("AKON>");
            for(char e=0;e<32;e++)
                {
                for(char y=0;y<20;y++)
                    {
                    RST=0;
                    RST=1;
                    char u=-1;
                    b=0b00000001;
                    for(int r=0;r<8;r++)
                        {
                        DAD=pow[r];
                        __delay_ms(2);
                        RST=0;
                        RST=1;
                        u++;
                        for(int a=0;a<32;a++)
                            {
                            DAD=0;
                            RST=1;
                            char t,n;
                            n=leds[a];
                            n>>=u;
                            t=(n & b);
                            if(t==0){DATA=0;}
                            if(t==1){DATA=1;}
                            CLK=1;
                            __delay_us(1);
                            CLK=0;
                            }
                        }
                    }
                for(char t=0;t<32;t++)
                    {
                    leds[t]=leds[t+1];
                    }
                }
            clear();
            }
        }


    display(char *str)  
        {

        int addr;                                                      
        int z;
        while(*str!=0){
            addr = ((int)*str++ - 0x20);
            addr *= 5;
            int y=0;
            for(z=0;z<5;z++){
                leds[z] = Font2[addr+y];
                y++;
                }
            addr = ((int)*str++ - 0x20);
            addr *= 5;
            int y=0;
            for(z=6;z<11;z++){
                leds[z] = Font2[addr+y];
                y++;
                }
            addr = ((int)*str++ - 0x20);
            addr *= 5;
            int y=0;
            for(z=12;z<17;z++){
                leds[z] = Font2[addr+y];
                y++;
                }
            addr = ((int)*str++ - 0x20);
            addr *= 5;
            int y=0;
            for(z=18;z<23;z++){
                leds[z] = Font2[addr+y];
                y++;
                }
            addr = ((int)*str++ - 0x20);
            addr *= 5;
            int y=0;
            for(z=24;z<29;z++){
                leds[z] = Font2[addr+y];
                y++;
                }
            }
        }



    void clear(void){
        for(char y=0;y<31;y++){
        leds[y]=0x00;
        }
    }
     
    Last edited by a moderator: Mar 7, 2013
  6. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    Pleas use code tags and indents... It makes it easier for me to read it..... When I get home I'll look at it
     
  7. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    Hi,
    please give suggestion how to build buffer for string of character more than buffer size.....
     
  8. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    What is your buffer size now.... Are you on 16x32 led's now....
     
  9. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    No same same display 8x32 leds buffer...i want to add string more than buffer size....
     
  10. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    Why don't you build the circuit that I did for you... That way the string can be as long as you like...

    http://youtu.be/3GH8dPQzouk
     
  11. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    In column scanning i found one problem when column size increase the scroll flicker/vibrate due to scanning big size....but in my new code for rows scanning there is no problem......it moves soo smoothly and with effect.....i just need help with led buffer filling.
     
  12. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    No disrespect intended!!! Your code is...... Well... VERY difficult to follow... I re-read it several times to find out your logic..

    You need to build an engine..... The purpose of the engine is to do the mundane tasks.. All those nested "if" statements are extremely inefficient...

    You need to drive the LED update in a function which is called systematically..... Otherwise you will have real difficulty changing things.. Like for example, the string for display
     
  13. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    just neglect that if statement i was just creating reverse style at display for testing just DATA=t;

    As i said i am doing Row scanning so, i was using b=00000001; 'ANd' with the buffer rows with shifting after completing all column...
    after that the shifting of column or Rows is done for scrolling up/down/left....
    then clear is called b'cz the buffer are filled with default value and then again display is called to fill it...
     
  14. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    In C you should be able to create an unsigned long ( well 8 of them really ) this will be your group of LED buffers... Then "buffer <<=1;" will scroll the data right.

    There is a useful feature in C, which is really underused.. Its a union. This is a data structure in which all the elements are contained within the same data space.

    union.png


    This way when you shift the long, the bytes ( which become your buffers ) are automatically aligned.
     
  15. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    Can you explain me bit more what are you trying to say for??
    as led buffer is made of rows and colun so what will be the use of making long, then char not claer to me..?????
    and please help me with assm instruction the ROL and ROL how to do this in C.
     
  16. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    Right!!! You are not helping me..... I gave you everything you need to make a 8x24 LED scrolling machine using column scanning...

    You have decided that the original plan of row scanning is a better option.... 32 bits is an unsigned long .. You need to scroll the long left or right... AND you need to feed the data 1 byte at a time..

    This is why its easier to do this off screen... (and a lot easier to do columns) Scrolling the buffer is simple... "buffer<<=1;" scrolls the buffer 1 but you have 4 buffers in a line so using the full 32 bits will make it easier for you...
     
  17. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    Hi,

    i was testing unsigned long in place of unsigned char for leds buffer it is showing error why??
    invalid conversion between pointer, why??
     
  18. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    This is advanced stuff.... I really can't write another routine... You CAN'T use an unsigned long You would need to re-write the whole thing.... The suggestion of using a union is not going to help you until you can grasp the concept...
     
  19. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9

    Actually i am not familiar with your idea can u give me a bit more explanation???
     
  20. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    can you give me any hint??
     
  21. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    I'll try and do a dual matrix 8x16 using an int and two bytes in a single union... I'll do it without interrupts so you can follow..
     

Share This Page