dxtr123
Newbie level 3
Can anyone can help my why this code doesn't work. The timer interrupts doesn't count the rpm. The purpose of project is to find the rpm of Fan and display in 7segments. In specifications of Fan the rpm is 10000. I'm using atmega128 and the pin of IC is PIND4 and also to display in 7segments must be use this port. Here is the code I'm trying to do this project:
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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 #include <avr/io.h> #include <util/delay.h> #include <avr/interrupt.h> #define F_CPU 14745600 //index shows number from 0-9 that can be shown in 7segments char Numbers[] = {63, 6, 91, 79, 102, 109, 125, 7, 127, 111}; volatile uint16_t rpm = 0; //Revolution per minute volatile uint16_t rps=0; //Revolution per second volatile char DisplayBuffer[8]={0,0,0,0,0,0,0,0}; volatile uint16_t cnttime = 0; volatile uint16_t count=0; //Main revolution counter void display(void) { unsigned int i; for( i=0;i<8;i++) { PORTD = DisplayBuffer[i]; PORTB = i; if(PORTD==0) { PORTD = ~Numbers[0]; } _delay_ms(1); } } //prepare numbers for display void breakup(uint16_t num) { unsigned int i=7; int a=0; if(num==0) { unsigned int h; for(h=0;h<8;h++) { DisplayBuffer[i]=~Numbers[0]; } } else { while (num!=0) { a = num%10; DisplayBuffer[i]=~Numbers[a]; num=num/10; i--; } } } void initSegments(void) { DDRD = 0xFF; DDRB = 0x07; PORTD = 0xFF;// tung off all segments PORTB = 0xFF; unsigned int i; for( i=0;i<8;i++) { PORTD=~Numbers[0];//fill with 0 PORTB=i; } } void initCount(void) { DDRD = 0x00;//all ports input PORTD = 0xFF; } void InitTimer(void) { TCNT1=0; TCCR1A = 0x00; //Timer Clock = 1/256 of sys clock //Mode = CTC (Clear Timer On Compare) TCCR1B|=((1<<ICNC1)|(1<<ICES1)|(1<<WGM12)|(1<<CS12)); //prescaler 256 14745600/256=57600 OCR1A=57600; //1s TIMSK|=((1<<TICIE1)|(1<<OCIE1A)); } ISR(TIMER1_CAPT_vect)// { //CPU Jumps here automatically when pin detect a falling edge count++; } SIGNAL(SIG_OUTPUT_COMPARE1A) { rps = count; rpm = rps*60; cnttime++; //test to show every 5 sec if(cnttime==5) { initSegments(); unsigned int h; for(h=0;h<10000;h++) { breakup(rps); display(); } DDRD = 0x00; PORTD = 0xFF; PORTB = 0x00; cnttime=0; initCount(); } count=0; } int main() { initCount(); InitTimer(); //Enable interrupts globaly sei(); while(1) { } return 0; }
Last edited by a moderator: