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,306
    Likes:
    914
    Location:
    Rochdale UK
    If you want a single statement ie..
    Code (text):

    if ( x== 0 )  PORTB=2;
     
    You can also write..
    Code (text):

    if ( x== 0 )
       PORTB=2;
     
    You can also write..
    Code (text):

    if ( x== 0 )
       {
       PORTB=2;
       }
     
    BUT!!! f there are more than one statements it MUST have the curly brackets
    Code (text):

    if ( x== 0 )
       {
       PORTB = 2;
       PORTC = 1;
       }
     
    So Naturally I use A) the first

    Code (text):

    if ( x== 0 ) PORTB=2;
     
    Or B) the last....

    When you debug the first bit of code... You need to separate the two lines so you can check the operation...
     
  2. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    I have doubt with this also..........
    {} // REMOVED the WDT function!!!!

    if(!T0IF) // Was this a timer overflow?
    bad_intr = 1; // NO! Shock horror!


    PORTB ^= 1;
    explain this also....not clear to me
     
  3. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,306
    Likes:
    914
    Location:
    Rochdale UK
    Okay... You are spending too much time on trivial stuff

    The original code was
    Code (text):
    for( ; ; )
       CLRWDT();
    You didn't understand... So I removed it and placed a __CONFIG(); at the top..
    Code (text):
     for( ; ; )
       {}
    You can either HAVE the empty curly brackets or not.... It's up to you...
    If NOT Timer0InterruptFlag...
    bad interrupt is then flagged..
    Port b is XORED with 1 C shortcut.. Same as PORTB = PORTB XOR 1 in basic... (Toggle)
     
  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

    OK, I am working scrolling LED display for 8x8 LED matrix just for now so, i have used 4017 it was working fine with big code without any keypad to input the data ...
    but after searching i found 74164 is used for serial to parallel converter for cascading large column so, i have problem to convert the data example 0x5f to serial feeding to 74164 i was using data array to solve this problem..i have to convert hex to binary vice versa.

    Code (text):
    int a[8]= {0,1,0,1,1,1,1,1};
    for(int i=0; i<8;i++){
    RST=0;
    RST=1;
    CLK=0;
    CLK=1;
    data=a[i];
    }
     

    please tell how to do this in fast and efficient way!!
     
  6. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,306
    Likes:
    914
    Location:
    Rochdale UK
    I have seen this on AAC... Can you post me a small schematic so I can see what you're doing... I'd like to have it on ISIS, so I can see whats occurring!!! Then I'll be able to test the code you post.

    I'll try and grab the code you posted on AAC as well.
     
  7. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    Here is schematic 4017 to 74164 the data a b are connected to gather to PORTD7 and clk and reset to PORTD6 PORTD5...like this.
     

    Attached Files:

    • 12.jpg
      12.jpg
      File size:
      1 MB
      Views:
      117
  8. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,306
    Likes:
    914
    Location:
    Rochdale UK
    OK... First off... Why do we need the external IC'c? The schematic shows a pic16f84a, If you use a 877a you can use port D for rows and port C for columns!!

    If you intended to expand the column drivers to incorporate MANY 8x8 matrices I'll use the 4017 and 74164 to drive a single matrix!!

    If you are driving "several" matrices, you may not be able to keep up the duty cycle needed for a ficker free display...


    Come to think of it... Why do you need the decade counter???

    In my "Nigel's tutorials in C" http://www.electro-tech-online.com/threads/directorial-antenna.467/ Tutorial 13

    In here you can find out how Nigel used TMR2 interrupt to run ASCII text on an 8x8 matrix.
     
    Last edited: Jan 19, 2013
  9. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    I have seen many application notes which say, when you need to connect large number of Led shift register are used...to shift the same data and scroll like that.
    one thing more i am having problem in initializing 2D array in MP lab showing error..

    int abc[2][7]={{0x0,0x1,x0x2,0x2,0x5,0x8,} {0xf,0xa,x0x2,0x2,0x5,0x8,}};

    please help me and provide a simple program containing 2D array.
     
  10. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,306
    Likes:
    914
    Location:
    Rochdale UK
    Works its stores the figures above in two sets of 7..
     
  11. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    I am not getting you?
     
  12. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,306
    Likes:
    914
    Location:
    Rochdale UK
    You asked how to initialise a 2d array!!!! I showed you why yours didn't work....
     
  13. koolguy

    koolguy Active Member

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

    I have solved that silly problem missing ,
    I was initializing font through it but only few were saved because of memory space was near to full 89.9%
    so, i watched program spaced and EEPROM was empty!! how to use it??
     
  14. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,306
    Likes:
    914
    Location:
    Rochdale UK
    There is only 255 eeprom locations.... You need to put the little 2d bitmaps in program memory

    Code (text):
    const rom char 2Darray[] = {,,,,,};
     
  15. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    can you explain me bit more how to add this in code to save array?
     
  16. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,306
    Likes:
    914
    Location:
    Rochdale UK
    Its all in the tutorial... Along with the C code you'll need the "font.c" file.... Look at it and see what I have done....
     
  17. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    seen your tut_6 all part but not clear how to use internal EEPROM of 877a and in user guide i found
    Code (text):
     eeprom_write(0x10, 0x1f);
    after this i build the project and view>EEPROM there is no values stored in it...,why?
     
  18. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,306
    Likes:
    914
    Location:
    Rochdale UK
    My tutorial 6 is for EXTERNAL eeprom not the small part in the pic I use a 24lc256 I2C eeprom...
     
  19. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    But if i want to use internal E2PROM then what command to use as i have seen user note of hi tech C but not working may be some fault......pls help with it..
    The file i have attached(font) is to be saved as .H file then called by its row col.
     

    Attached Files:

  20. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    here s the code i am using to text and scroll.
    int a[row][7]={};
    row=6 it work but when row>6 shows memory full on window of MP

    Code (text):

    #include <htc.h>
    __CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_XT);
    #define _XTAL_FREQ 4000000
    #define DAD PORTB
    #define RST RD7
    #define CLK RD4
    #define DATA RD5
    #define D 1
    int data(int a);

    int v;
     main (){
    TRISB=0B00000000;
    TRISD=0B00000000;
    int row;
    int  b[6][7]=  {
    {0x00, 0x0e, 0x01, 0x0d, 0x13, 0x13, 0x0d},//font
      {0x0e, 0x11, 0x13, 0x15, 0x19, 0x11, 0x0e,} ,  
       {0x04, 0x0c, 0x04, 0x04, 0x04, 0x04, 0x0e,} ,  
       {0x0e, 0x11, 0x01, 0x02, 0x04, 0x08, 0x1f,} ,
       {0x0e, 0x11, 0x01, 0x06, 0x01, 0x11, 0x0e,}  ,
       {0x02, 0x06, 0x0a, 0x12, 0x1f, 0x02, 0x02,},

    };
    while(1){
    DAD=0X00;
    int a;
    v=15;
    for(row=0;row<6;row++)// for getting new character fromnext row
    {
    for(int o =0;o<15;o++)// for scrolling/moving the font by changing the bit store in 164
    {
    v--;
    for (int k=0;k<50;k++)// just for holdong the character on led for mSec
    {
    a=b[row][0];
    data(a);
    DAD=0B10000000;
    __delay_ms(D);

    a=b[row][1];
    data(a);
    DAD=0B01000000;
    __delay_ms(D);

    a=b[row][2];
    data(a);
    DAD=0B00100000;
    __delay_ms(D);

    a=b[row][3];
    data(a);
    DAD=0B00010000;
    __delay_ms(D);

    a=b[row][4];
    data(a);
    DAD=0B00001000;
    __delay_ms(D);

    a=b[row][5];
    data(a);
    DAD=0B00000100;
    __delay_ms(D);

    a=b[row][6];
    data(a);
    DAD=0B00000010;
    __delay_ms(D);





    }
    }
        }
        }
    }


    int data(int a)// serial to // converter for 164
    {
    RST=0;
    RST=1;

    DAD=0X00;
      for (int k=0; k<v;k++) {  //send 8 bits
    CLK = 0;  // serial clock low
        if ((a & 0x01)==1) DATA = 1;

        else DATA =0;

        CLK =1;

        a >>=1;
    } }
     
  21. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,306
    Likes:
    914
    Location:
    Rochdale UK
    The rows are only 6 in length..... There isn't a row 7 .. If you have the font in external file you need to reference it first.

    Code (text):
    extern const unsigned char Font1[32][17];   // delclare as external (in another file)
    Near to the top BEFORE IT'S USED

    You need to change this too..
    Declaring an int will waste memory..
     

Share This Page