[help] PIC interrupt of 1 sec

Discussion in 'General Electronics Chat' started by kirayamato_143, Nov 29, 2011.

  1. kirayamato_143

    Thread Starter Member

    Jan 23, 2011
    59
    0
    hi guys i'm trying to code in MikroC using C language, can somebody help me with my code or provide me a very simple code. I want that after every second the ':' will be on and off its not yet 1 second also after every second the buzzer will play and a number will decrement by 1 lets say 60 cause im making a clock in LCD.
    here is my code and stock in blingkin : cant follow up with the number. after one second the buzzer turns on i think but the ':' comes with cursor on in the first row of the LCD please help me i'm new with the interrupts, i assume that interrupts starts after you declare the value of TMR0, tnx

    // LCD module connections
    sbit LCD_RS at RA0_bit;
    sbit LCD_EN at RA1_bit;
    sbit LCD_D4 at RB0_bit;
    sbit LCD_D5 at RB1_bit;
    sbit LCD_D6 at RB2_bit;
    sbit LCD_D7 at RB3_bit;

    sbit LCD_RS_Direction at TRISA0_bit;
    sbit LCD_EN_Direction at TRISA1_bit;
    sbit LCD_D4_Direction at TRISB0_bit;
    sbit LCD_D5_Direction at TRISB1_bit;
    sbit LCD_D6_Direction at TRISB2_bit;
    sbit LCD_D7_Direction at TRISB3_bit;
    // End LCD module connections




    char Blink2[]=":";
    unsigned short i,j,Num,Blink;


    void Debounce(){
    delay_ms(500);}

    void Play(){
    Sound_Play(2500, 500);}

    void interrupt() {
    Num++;
    if(Num==18){
    j=1;
    Play();
    Num=0;
    }
    TMR0=39;
    INTCON.T0IF=0;
    }

    void main(){
    CMCON=0x07;
    TRISA=0b00100100;
    TRISB=0b00000000;
    Sound_Init(&PORTB,4);
    Num=0;
    i=0;
    j=0;
    OPTION_REG=0x07;
    TMR0=39;
    INTCON=0xA0;
    INTCON.T0IF=0;


    do{
    Lcd_Init();
    Lcd_Cmd(_LCD_CLEAR);
    Lcd_Cmd(_LCD_CURSOR_OFF);
    if(j==1){
    Lcd_Cmd(_LCD_CLEAR);
    Lcd_Cmd(_LCD_CURSOR_OFF);
    Lcd_Out(2,8,Blink2);}
    while(i<2){
    Debounce();
    i++;}
    }while(1);
    }
     
  2. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    What is your timer interrupt call frequency set up as?

    You may need to use a second interrupt that counts say, 74 milliseconds with a high pre or post scaler, and when the total = 1000mS add 1 to the Seconds period.

    You won't be able to do it in code when you have delay routines. You should never have delay routines or function calls in the interrupt, as you want to get out of the interrupt as quickly as possible.

    In the example above, you are calling play(), instead, you should set play=1, with play being a global bit variable, and in the main loop, a line such as if(play) play(); and the first line in the play function should set play to 0 again.

    That way your interrupt is jumped in, flags modified, seconds count incremented, and out so no interrupts are missed.

    Member Roman Black The_RB here has made a zero-error second counting routine you should take a look at as well.
     
Loading...