Timer1 do not work! Plz help!!

General discussion on mikroC.
Post Reply
Author
Message
dvu
Posts: 6
Joined: 09 Aug 2010 04:07

Timer1 do not work! Plz help!!

#1 Post by dvu » 10 Aug 2010 05:10

When I press a Button (which connects to PORTA.F2, PORTA.F3) then TIMER1 do not work. Plz help me to resolve that

Code: Select all


//Declare var

unsigned temp1, temp_res,temp_set;
unsigned short dvi, chuc;

unsigned t;
unsigned short hour, minute, sec;
const unsigned short  a[10] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};

void interrupt (void)  {

     t++ ;
     PIR1.TMR1IF = 0;            // clear TMR1IF
     TMR1H = 0x00;
     TMR1L = 0x00;
}

// Init
void khoi_tao ()  {
     //khoi tao PORTA
     TRISA  = 0xFF;              // PORTA is input
     ADCON1 = 0x8E;              // Configure analog inputs and Vref  AN0: Input analog
     PORTA = 0;                  //Fo/4; RA7-RA1 Digital input

     // Khoi tao PORTB
     PORTB = 0;
     TRISB = 0;

     //Khoi tao PORTD
     PORTD = 0;
     TRISD = 0x00;                //PORTD output

     // PORTC
     TRISC = 0x00;
     PORTC = 0x00;

     // Khoi tao TMR1

     T1CON = 1;                  // Timer1 settings
     PIR1.TMR1IF = 0;            // clear TMR1IF
     TMR1H = 0x00;               // TMR1 = 0;
     TMR1L = 0x00;
     PIE1.TMR1IE  = 1;           // enable Timer1 interrupt
     INTCON = 0xC0;              // Set GIE, PEIE

     // Varial
     t = 0;
     hour = 0;
     minute = 0;
     sec = 0;
     dvi = 0;
     chuc = 0;
     temp1 = 0;
     temp_res = 0;
}
// Read Temp 
void read_temp () {

    unsigned short i;
    unsigned temp_T;
    unsigned short  temp_2;
    unsigned short temp_new;
       temp_T = 0;
       for (i = 0; i<10; i++) {
           temp_res = Adc_Read(0);
           Delay_1us();
           temp_2 = (temp_res / 2)-1;
           temp_T = temp_T + temp_2;
       }
       temp_new = temp_T/10; 		//  
       temp1 = (temp1 + temp_new)/2; 	//Lay trung binh lan doc dau va lan sau

}

// Convert BCD to display on LED7
void convert_bcd (unsigned short x) {

     dvi = x % 10;
     chuc = x/10;
     delay_us (10);

}

// Display
void hien_thi() {
//temp:
        if (!(PORTA.F1)) convert_bcd (temp_set);
        else convert_bcd (temp1);

     PORTB = a[chuc];
     PORTD.F0 = 1;
     Delay_500us ();
     PORTD.F0 = 0;

     PORTB = a[dvi];
     PORTD.F1 = 1;
     Delay_500us ();
     PORTD.F1 = 0;
//       second
         convert_bcd (sec);
     PORTB = a[chuc];
     PORTD.F2 = 1;
     Delay_500us ();
     PORTD.F2 = 0;
     
     PORTB = a[dvi];
     PORTD.F3 = 1;
     Delay_500us ();
     PORTD.F3 = 0;
//      minute
        convert_bcd (minute);
     PORTB = a[chuc];
     PORTD.F4 = 1;
     Delay_500us ();
     PORTD.F4 = 0;
     

     PORTB = a[dvi];
     PORTD.F5 = 1;
     Delay_500us ();
     PORTD.F5 = 0;
//      Hour
        convert_bcd (hour);
     PORTB = a[chuc];
     PORTD.F6 = 1;
     Delay_500us ();
     PORTD.F6 = 0;

     PORTB = a[dvi];
     PORTD.F7 = 1;
     Delay_500us ();
     PORTD.F7 = 0;
}

// Set the temp

void set_temp () {
 /*     UP    */


       delay_ms (10);
     if (Button(&PORTA, 2, 100, 1)) {
        temp_set++;
        delay_ms (10);
        }
//down

     if (Button(&PORTA, 3, 100, 1)) {
        temp_set --;
        }
}




/*            PWM                  */ //dieu che xung ra
void dieu_che () {

//Level   1
     if (temp1 <=(temp_set)) {
         delay_ms (10);
         PORTC.F3 = 1;
         PORTC.F4 = 1; // Enable Motor1
         PORTC.F5 = 0; // DisEnable Motor2
        }
//Level   2
     if ((temp1 > temp_set)&& (temp1 <= (temp_set + 2))) {
         PORTC.F3 = 0; // Dung sinh nhiet
         PORTC.F4 = 1; //  Enable Motor1
         PORTC.F5 = 0; // DisEnable Motor2
        }
// Level  3
     if (temp1 > (temp_set + 2)) {
        // Qua nhiet, mo quat hut, dung sinh nhiet
         PORTC.F3 = 0; // Dung sinh nhiet
         PORTC.F4 = 1; // mo quat thoi
         PORTC.F5 = 1; // mo quat hut
     }

}

//         Caculate the time
void Cal_Time () {
     if (t == 38) {           // each interrupt TMR1 = 26ms
        sec++;
        t = 0;
        }

     if (sec == 60) {
        minute ++;
        sec = 0;
                      }
     if (minute == 60) {
        hour ++;
        minute = 0;
     }
     if (hour == 99) {
        hour = 99;
        minute = 99;
        sec = 99;
     }

}
//        



/////////////////////////////////////////////////////
void main() {

     khoi_tao ();
     delay_us (10);
     temp_set = 60;

     temp_res = Adc_Read(0);
     delay_us (5);
     temp1 = temp_res/2;
     temp1 = temp1 - 1;


while (1) {
     if (!(PORTA.F1)) set_temp ();
     else {
	     read_temp ();
	     hien_thi();
     }
    Cal_Time ();
    Delay_10us;
    hien_thi();
    Delay_10us;
    dieu_che ();
     }

}


User avatar
tihomir.losic
mikroElektronika team
Posts: 2138
Joined: 02 Dec 2009 14:16
Location: Serbia
Contact:

Re: Timer1 do not work! Plz help!!

#2 Post by tihomir.losic » 10 Aug 2010 08:44

Hello,

please, tell me, which PIC are you using?
I need that information in order to solve your issue.

Thanks.

Best regards,

Losic Tihomir
mikroElektronika [Support team]

dvu
Posts: 6
Joined: 09 Aug 2010 04:07

Re: Timer1 do not work! Plz help!!

#3 Post by dvu » 10 Aug 2010 09:51

I'm using PIC16F877a; crystal = 10MHz

I simulate in Proteus, Prog works OK!
if u need, i'll send 2u hardwave!

dvu
Posts: 6
Joined: 09 Aug 2010 04:07

Re: Timer1 do not work! Plz help!!

#4 Post by dvu » 10 Aug 2010 10:07

Hardware:
PIC16F877A
Crystal = 10MHz

LM35DZ -> AN0 ( PORTA.F0)
SWITCH -> AN1 (PORTA.F1)
UP Button -> AN2 (PORTA.F2)
DOWN Button -> AN3 (PORTA.F3)

8 LED7s -> PORTB
PORTD to controls LED7

dvu
Posts: 6
Joined: 09 Aug 2010 04:07

Re: Timer1 do not work! Plz help!!

#5 Post by dvu » 10 Aug 2010 15:19

Plz help me now! Tomorow i must complete this project!!!
I dont know why!!!!
Bored so much!

dvu
Posts: 6
Joined: 09 Aug 2010 04:07

Re: Timer1 do not work! Plz help!!

#6 Post by dvu » 12 Aug 2010 08:15

why No1 help me??? Please now!
Many Thanks!

braus
Posts: 167
Joined: 25 Jul 2007 22:55
Location: Mexico, city.

Re: Timer1 do not work! Plz help!!

#7 Post by braus » 12 Aug 2010 22:40

Hello, some comments to respect your code...

Code: Select all


//Declare var

unsigned temp1, temp_res,temp_set;
unsigned short dvi, chuc;

unsigned t;
unsigned short hour, minute, sec;
const unsigned short  a[10] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};

void interrupt (void)  {

     t++ ;
     PIR1.TMR1IF = 0;            // clear TMR1IF
     TMR1H = 0x00;
     TMR1L = 0x00;      //You need to set INTCON.GIE bit in order to enable another interrupt
}

// Init
void khoi_tao ()  {
     //khoi tao PORTA
     TRISA  = 0xFF;              // PORTA is input
     ADCON1 = 0x8E;              // Configure analog inputs and Vref  AN0: Input analog
     PORTA = 0;                  //Fo/4; RA7-RA1 Digital input

     // Khoi tao PORTB
     PORTB = 0;
     TRISB = 0;

     //Khoi tao PORTD
     PORTD = 0;
     TRISD = 0x00;                //PORTD output

     // PORTC
     TRISC = 0x00;
     PORTC = 0x00;

     // Khoi tao TMR1

     T1CON = 1;                  // Try with 9 value
     PIR1.TMR1IF = 0;            // clear TMR1IF
     TMR1H = 0x00;               // TMR1 = 0;
     TMR1L = 0x00;
     PIE1.TMR1IE  = 1;           // enable Timer1 interrupt
     INTCON = 0xC0;              // Set GIE, PEIE

     // Varial
     t = 0;
     hour = 0;
     minute = 0;
     sec = 0;
     dvi = 0;
     chuc = 0;
     temp1 = 0;
     temp_res = 0;
}
// Read Temp 
void read_temp () {

    unsigned short i;
    unsigned temp_T;
    unsigned short  temp_2;
    unsigned short temp_new;
       temp_T = 0;
       for (i = 0; i<10; i++) {
           temp_res = Adc_Read(0);
           Delay_1us();
           temp_2 = (temp_res / 2)-1;
           temp_T = temp_T + temp_2;
       }
       temp_new = temp_T/10;       //  
       temp1 = (temp1 + temp_new)/2;    //Lay trung binh lan doc dau va lan sau

}

// Convert BCD to display on LED7
void convert_bcd (unsigned short x) {

     dvi = x % 10;
     chuc = x/10;
     delay_us (10);

}

// Display
void hien_thi() {
//temp:
        if (!(PORTA.F1)) convert_bcd (temp_set);
        else convert_bcd (temp1);

     PORTB = a[chuc];
     PORTD.F0 = 1;
     Delay_500us ();
     PORTD.F0 = 0;

     PORTB = a[dvi];
     PORTD.F1 = 1;
     Delay_500us ();
     PORTD.F1 = 0;
//       second
         convert_bcd (sec);
     PORTB = a[chuc];
     PORTD.F2 = 1;
     Delay_500us ();
     PORTD.F2 = 0;
     
     PORTB = a[dvi];
     PORTD.F3 = 1;
     Delay_500us ();
     PORTD.F3 = 0;
//      minute
        convert_bcd (minute);
     PORTB = a[chuc];
     PORTD.F4 = 1;
     Delay_500us ();
     PORTD.F4 = 0;
     

     PORTB = a[dvi];
     PORTD.F5 = 1;
     Delay_500us ();
     PORTD.F5 = 0;
//      Hour
        convert_bcd (hour);
     PORTB = a[chuc];
     PORTD.F6 = 1;
     Delay_500us ();
     PORTD.F6 = 0;

     PORTB = a[dvi];
     PORTD.F7 = 1;
     Delay_500us ();
     PORTD.F7 = 0;
}

// Set the temp

void set_temp () {
/*     UP    */


       delay_ms (10);
     if (Button(&PORTA, 2, 100, 1)) {   //I think you are missing some code here, try to read Button library 
        temp_set++;                            //explanation
        delay_ms (10);
        }
//down

     if (Button(&PORTA, 3, 100, 1)) {
        temp_set --;
        }
}




/*            PWM                  */ //dieu che xung ra
void dieu_che () {

//Level   1
     if (temp1 <=(temp_set)) {
         delay_ms (10);
         PORTC.F3 = 1;
         PORTC.F4 = 1; // Enable Motor1
         PORTC.F5 = 0; // DisEnable Motor2
        }
//Level   2
     if ((temp1 > temp_set)&& (temp1 <= (temp_set + 2))) {
         PORTC.F3 = 0; // Dung sinh nhiet
         PORTC.F4 = 1; //  Enable Motor1
         PORTC.F5 = 0; // DisEnable Motor2
        }
// Level  3
     if (temp1 > (temp_set + 2)) {
        // Qua nhiet, mo quat hut, dung sinh nhiet
         PORTC.F3 = 0; // Dung sinh nhiet
         PORTC.F4 = 1; // mo quat thoi
         PORTC.F5 = 1; // mo quat hut
     }

}

//         Caculate the time
void Cal_Time () {
     if (t == 38) {           // each interrupt TMR1 = 26ms
        sec++;
        t = 0;
        }

     if (sec == 60) {
        minute ++;
        sec = 0;
                      }
     if (minute == 60) {
        hour ++;
        minute = 0;
     }
     if (hour == 99) {
        hour = 99;
        minute = 99;
        sec = 99;
     }

}
//        



/////////////////////////////////////////////////////
void main() {

     khoi_tao ();
     delay_us (10);
     temp_set = 60;

     temp_res = Adc_Read(0);
     delay_us (5);
     temp1 = temp_res/2;
     temp1 = temp1 - 1;


while (1) {
     if (!(PORTA.F1)) set_temp ();
     else {
        read_temp ();
        hien_thi();
     }
    Cal_Time ();
    Delay_10us;
    hien_thi();
    Delay_10us;
    dieu_che ();
     }

}


Best Regards
Omar Galicia
Mexico City

dvu
Posts: 6
Joined: 09 Aug 2010 04:07

Re: Timer1 do not work! Plz help!!

#8 Post by dvu » 18 Aug 2010 01:12

Thank for all! It's this!
But I think the problems in the buttons! May It makes noise????

Code: Select all

//Declare var

unsigned temp1, temp_res,temp_set;
unsigned short dvi, chuc;

unsigned t;
unsigned short hour, minute, sec;
const unsigned short  a[10] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};

void interrupt (void)  {

     t++ ;
     PIR1.TMR1IF = 0;            // clear TMR1IF
     TMR1H = 0x00;
     TMR1L = 0x00;      // I've set INTCON.GIE in khoi_tao ()
                               // [i]You need to set INTCON.GIE bit in order to enable another [/i]
interrupt
}

// Init
void khoi_tao ()  {
     //Initial PORTA
     TRISA  = 0xFF;              // PORTA is input
     ADCON1 = 0x8E;              // Configure analog inputs and Vref  AN0: Input analog
     PORTA = 0;                  //Fo/4; RA7-RA1 Digital input

     // Initial PORTB
     PORTB = 0;
     TRISB = 0;

     //Initial PORTD
     PORTD = 0;
     TRISD = 0x00;                //PORTD output

     // Initial
     TRISC = 0x00;
     PORTC = 0x00;

     // Initial TMR1

     T1CON = 1;                  // [i]Try with 9 value[/i]  // I've tried
     PIR1.TMR1IF = 0;            // clear TMR1IF
     TMR1H = 0x00;               // TMR1 = 0;
     TMR1L = 0x00;
     PIE1.TMR1IE  = 1;           // enable Timer1 interrupt
     INTCON = 0xC0;              // Set GIE, PEIE

     // Var
     t = 0;
     hour = 0;
     minute = 0;
     sec = 0;
     dvi = 0;
     chuc = 0;
     temp1 = 0;
     temp_res = 0;
}
// Read Temp
void read_temp () {

    unsigned short i;
    unsigned temp_T;
    unsigned short  temp_2;
    unsigned short temp_new;
       temp_T = 0;
       for (i = 0; i<10; i++) {
           temp_res = Adc_Read(0);
           Delay_1us();
           temp_2 = (temp_res / 2)-1;
           temp_T = temp_T + temp_2;
       }
       temp_new = temp_T/10;       // 
       temp1 = (temp1 + temp_new)/2;    //

}

// Convert BCD to display on LED7
void convert_bcd (unsigned short x) {

     dvi = x % 10;
     chuc = x/10;
     delay_us (10);

}

// Display
void hien_thi() {
//temp:
        if (!(PORTA.F1)) convert_bcd (temp_set);
        else convert_bcd (temp1);

     PORTB = a[chuc];
     PORTD.F0 = 1;
     Delay_500us ();
     PORTD.F0 = 0;

     PORTB = a[dvi];
     PORTD.F1 = 1;
     Delay_500us ();
     PORTD.F1 = 0;
//       second
         convert_bcd (sec);
     PORTB = a[chuc];
     PORTD.F2 = 1;
     Delay_500us ();
     PORTD.F2 = 0;
     
     PORTB = a[dvi];
     PORTD.F3 = 1;
     Delay_500us ();
     PORTD.F3 = 0;
//      minute
        convert_bcd (minute);
     PORTB = a[chuc];
     PORTD.F4 = 1;
     Delay_500us ();
     PORTD.F4 = 0;
     

     PORTB = a[dvi];
     PORTD.F5 = 1;
     Delay_500us ();
     PORTD.F5 = 0;
//      Hour
        convert_bcd (hour);
     PORTB = a[chuc];
     PORTD.F6 = 1;
     Delay_500us ();
     PORTD.F6 = 0;

     PORTB = a[dvi];
     PORTD.F7 = 1;
     Delay_500us ();
     PORTD.F7 = 0;
}

// Set the temp

void set_temp () {
/*     UP    */


       delay_ms (10);
     if (Button(&PORTA, 2, 100, 1)) {   //[i]I think you are missing some code here, try to read Button library[/i] // I've tried the code in Library but no better! :( 
        temp_set++;                            //[i]explanation[/i] //increase temp_set by 1;
        delay_ms (10);
        }
//down

     if (Button(&PORTA, 3, 100, 1)) {
        temp_set --;
        }
}





void dieu_che () {

//Level   1
     if (temp1 <=(temp_set)) {
         delay_ms (10);
         PORTC.F3 = 1;
         PORTC.F4 = 1; // Enable Motor1
         PORTC.F5 = 0; // Disable Motor2
        }
//Level   2
     if ((temp1 > temp_set)&& (temp1 <= (temp_set + 2))) {
         PORTC.F3 = 0; // Stop Resistors 
         PORTC.F4 = 1; //  Enable Motor1
         PORTC.F5 = 0; // Disable Motor2
        }
// Level  3
     if (temp1 > (temp_set + 2)) {
        // Overheat
         PORTC.F3 = 0; // Stop Resistors
         PORTC.F4 = 1; // Enable Motor1
         PORTC.F5 = 1; // Disable Motor2
     }

}

//         Calculate the time
void Cal_Time () {
     if (t == 38) {           // each interrupt TMR1 = 26.412ms
        sec++;
        t = 0;
        }

     if (sec == 60) {
        minute ++;
        sec = 0;
                      }
     if (minute == 60) {
        hour ++;
        minute = 0;
     }
     if (hour == 99) {
        hour = 99;
        minute = 99;
        sec = 99;
     }

}
//       



/////////////////////////////////////////////////////
void main() {

     khoi_tao ();
     delay_us (10);
     temp_set = 60;

     temp_res = Adc_Read(0);
     delay_us (5);
     temp1 = temp_res/2;
     temp1 = temp1 - 1;


while (1) {
     if (!(PORTA.F1)) set_temp ();
     else {
        read_temp ();
        hien_thi();
     }
    Cal_Time ();
    Delay_10us;
    hien_thi();
    Delay_10us;
    dieu_che ();
     }

}



Post Reply

Return to “mikroC General”