Okada
Banned
Hi
I am monitoring 5 different signals which are pulses. I am testing in Proteus. I am using PIC12F1840 with 8MHz Internal Oscillator.
I have referred device datasheet and configured all registers properly. I see 10ms Timer2 Interrupt working by toggling a pin but Leds doesn't turn ON. Gate control is not used. I am using Timer1 as 16 bit counter. I read pulses at T1CKI pin for 100ms and then calculate frequency (for 1 sec).
Here is my code.
I am monitoring 5 different signals which are pulses. I am testing in Proteus. I am using PIC12F1840 with 8MHz Internal Oscillator.
I have referred device datasheet and configured all registers properly. I see 10ms Timer2 Interrupt working by toggling a pin but Leds doesn't turn ON. Gate control is not used. I am using Timer1 as 16 bit counter. I read pulses at T1CKI pin for 100ms and then calculate frequency (for 1 sec).
Here is my code.
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 #define BRAKE_FREQUENCY_MIN 2110 #define BRAKE_FREQUENCY_MAX 2130 #define RH_FREQUENCY_MIN 1400 #define RH_FREQUENCY_MAX 1420 #define LH_FREQUENCY_MIN 990 #define LH_FREQUENCY_MAX 1010 #define BRAKE_RH_FREQUENCY_MIN 840 #define BRAKE_RH_FREQUENCY_MAX 860 #define BRAKE_LH_FREQUENCY_MIN 670 #define BRAKE_LH_FREQUENCY_MAX 690 #define OFF 0 #define ON 1 #define HUNDRED_MILLI_SECONDS_TIMER_TICKS_VALUE 10 sbit Red_led at LATA0_bit; sbit Green_led at LATA1_bit; sbit Blue_led at LATA2_bit; sbit Test_Signal at LATA4_bit; unsigned char my_flags = 0; unsigned timer_ticks = 0; unsigned int frequency = 0; sbit reading_complete_flag at my_flags.B0; //Timer2 //Prescaler 1:16; Postscaler 1:5; TMR2 Preload = 250; Actual Interrupt Time : 10.0025 ms //Place/Copy this part in declaration section void InitTimer2() { T2CON = 0x26; PR2 = 250; TMR2IE_bit = 1; } void Interrupt() { if(TMR1IF_bit) { ++frequency; //not required as frequency read is less than 3KHz TMR1IF_bit = 0; } if((TMR2IE_bit) && (TMR2IF_bit)) { //Enter your code here if(++timer_ticks == HUNDRED_MILLI_SECONDS_TIMER_TICKS_VALUE) { TMR1ON_bit = 0; TMR2IE_bit = 0; timer_ticks = 0; Test_Signal = ~Test_Signal; reading_complete_flag = 1; } TMR2IF_bit = 0; } } void Start_Reading_Input_Signal() { Test_Signal = 0; T1GCON = 0x00; InitTimer2(); TMR1H = 0; TMR1L = 0; TMR1IE_bit = 1; T1CON = 0x85; PEIE_bit = 1; GIE_bit = 1; } void main() { OSCCON = 0x73; OSCSTAT = 0x99; OSCTUNE = 0x00; asm clrwdt OPTION_REG = 0x8F; CM1CON0 = 0x00; ANSELA = 0x00; TRISA = 0x20; PORTA = 0x00; LATA = 0x00; Delay_ms(100); reading_complete_flag = 0; Start_Reading_Input_Signal(); while(1) { asm clrwdt if(reading_complete_flag) { //frequency = ((frequency << 16) + (TMR1H << 8) + TMR1L) * 10;// frequency = ((TMR1H << 8) + TMR1L) * 10; if((frequency > BRAKE_FREQUENCY_MIN) && (frequency < BRAKE_FREQUENCY_MAX)) { Red_Led = ON; Green_Led = OFF; Blue_Led = OFF; } else if((frequency > RH_FREQUENCY_MIN) && (frequency < RH_FREQUENCY_MAX)) { Red_Led = OFF; Green_Led = ON; Blue_Led = OFF; } else if((frequency > LH_FREQUENCY_MIN) && (frequency < LH_FREQUENCY_MAX)) { Red_Led = OFF; Green_Led = OFF; Blue_Led = ON; } else if((frequency > BRAKE_RH_FREQUENCY_MIN) && (frequency < BRAKE_RH_FREQUENCY_MAX)) { Red_Led = ON; Green_Led = ON; Blue_Led = OFF; } else if((frequency > BRAKE_LH_FREQUENCY_MIN) && (frequency < BRAKE_LH_FREQUENCY_MAX)) { Red_Led = ON; Green_Led = OFF; Blue_Led = ON; } frequency = 0; reading_complete_flag = 0; Start_Reading_Input_Signal(); } } }
Attachments
Last edited: