چطور میشه Duty Cycleآ رو روی یکی از پینهای میکرو اندازه گرفت؟ هدف نهایی خوندن دما از سنسور SMT160 هست که خروجیش .D.C هست. میکرو مورد استفاده Atmega32 هست. تو این میکروها PWM Input نداریم؟! که بشه راحت اینکار رو انجام داد؟!
اطلاعیه
Collapse
No announcement yet.
اندازه گیری Duty Cycle
Collapse
X
-
پاسخ : اندازه گیری Duty Cycle
نوشته اصلی توسط .: Parham :.چطور میشه Duty Cycle رو روی یکی از پینهای میکرو اندازه گرفت؟ هدف نهایی خوندن دما از سنسور SMT160 هست که خروجیش .D.C هست. میکرو مورد استفاده Atmega32 هست. تو این میکروها PWM Input نداریم؟! که بشه راحت اینکار رو انجام داد؟!
http://www.eca.ir/forum2/index.php/topic,6671.msg26421.html#msg26421
Pwm input نداریم
موفق باشید
دیدگاه
-
پاسخ : اندازه گیری Duty Cycle
خوب. این کدش. از همون Input Capture Unit استفاده کردم. تو این کد یک LCD 2x20 در نظر گرفتم روی پورت C و خروجی سنسور هم به پین ICP یا همون PD6 وصله. میکروه هم که مگا۳۲ هست.
کد:#include <mega32.h> // Alphanumeric LCD Module functions #asm .equ __lcd_port=0x15 ;PORTC #endasm #include <lcd.h> #include <stdlib.h> #include <stdio.h> #include <delay.h> #include <math.h> volatile unsigned int start_time,stop_time,prev_period,prev_pw; // Timer 1 input capture interrupt service routine interrupt [TIM1_CAPT] void timer1_capt_isr(void) { // Place your code here unsigned int temp; if (TCCR1B==0x41){ temp = ICR1L+(ICR1H*256); TCCR1B = 0x01; prev_period = temp - start_time; prev_pw = stop_time - start_time; start_time = temp; } else { stop_time = ICR1L+(ICR1H*256); TCCR1B = 0x41; } } // Declare your global variables here void main(void) { // Declare your local variables here unsigned char lcdbuf[39]; // Input/Output Ports initialization // Port A initialization // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T PORTA=0x00; DDRA=0x00; // Port B initialization // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T PORTB=0x00; DDRB=0x00; // Port C initialization // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T PORTC=0x00; DDRC=0x00; // Port D initialization // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T PORTD=0x00; DDRD=0x00; // Timer/Counter 0 initialization // Clock source: System Clock // Clock value: Timer 0 Stopped // Mode: Normal top=FFh // OC0 output: Disconnected TCCR0=0x00; TCNT0=0x00; OCR0=0x00; // Timer/Counter 1 initialization // Clock source: System Clock // Clock value: 2000.000 kHz // Mode: Normal top=FFFFh // OC1A output: Discon. // OC1B output: Discon. // Noise Canceler: Off // Input Capture on Rising Edge // Timer 1 Overflow Interrupt: Off // Input Capture Interrupt: On // Compare A Match Interrupt: Off // Compare B Match Interrupt: Off TCCR1A=0x00; TCCR1B=0x41; TCNT1H=0x00; TCNT1L=0x00; ICR1H=0x00; ICR1L=0x00; OCR1AH=0x00; OCR1AL=0x00; OCR1BH=0x00; OCR1BL=0x00; // Timer/Counter 2 initialization // Clock source: System Clock // Clock value: Timer 2 Stopped // Mode: Normal top=FFh // OC2 output: Disconnected ASSR=0x00; TCCR2=0x00; TCNT2=0x00; OCR2=0x00; // External Interrupt(s) initialization // INT0: Off // INT1: Off // INT2: Off MCUCR=0x00; MCUCSR=0x00; // Timer(s)/Counter(s) Interrupt(s) initialization TIMSK=0x20; // Analog Comparator initialization // Analog Comparator: Off // Analog Comparator Input Capture by Timer/Counter 1: Off ACSR=0x80; SFIOR=0x00; // LCD module initialization lcd_init(20); // Global enable interrupts #asm("sei") while (1) { // Place your code here int t; lcd_clear(); t = (((float)prev_pw/(float)prev_period)-0.32)/0.00047; sprintf(lcdbuf,"Temp:%-i.%-u\xdfC",t/10,abs(t%10)); lcd_puts(lcdbuf); delay_ms(500); }; }
دیدگاه
-
پاسخ : اندازه گیری Duty Cycle
سلام
من یک دستور در بسکام می شناسم. شرح کاملش رو برات از روی کتاب کاهه می نویسم. ببین اگر کاری که میخوی رو انجام میده حتی اگه با بسکام کار نمی کنی فکر کنم بتونی اسمبلی کنی از اسمبلیش استفاده کنی. مهم اینه که بتونه کارت رو انجام بده:
دستورPULSEIN
توسط این دستور میتوان مدت زمان بین تغییر وضعیت پایه دلخواه رو از منطق 1 به 0 و یا بالعکس آشکار کرد.
PULSEIN var,PINX,PIN,STATE
این دستور از هیچ یک از تایمر ها استفاده نمی کند ولی یک کانتر 16 بیتی به کار گرفته می شود و هر 10 میکروثانیه یک واحد افزایش میابد که این مقدار بستگی به کریستال دارد.
امیدوارم مفید باشه.
عزت زیاد.اللهم صل علی فاطمه و ابیها، و بعلها و بنیها، و سر المستودع فیها،
بعدد ما احاط به علمک...
دیدگاه
-
پاسخ : اندازه گیری Duty Cycle
نوشته اصلی توسط پیام.کآقا پرهام دقت دما چه جوره؟؟؟
چقدر خطا داره؟تو چه دماهایی تستش کردی؟؟
دیدگاه
-
پاسخ : اندازه گیری Duty Cycle
نوشته اصلی توسط .: Parham :.خوب. این کدش. از همون Input Capture Unit استفاده کردم. تو این کد یک LCD 2x20 در نظر گرفتم روی پورت C و خروجی سنسور هم به پین ICP یا همون PD6 وصله. میکروه هم که مگا۳۲ هست.
کد:#include <mega32.h> // Alphanumeric LCD Module functions #asm .equ __lcd_port=0x15 ;PORTC #endasm #include <lcd.h> #include <stdlib.h> #include <stdio.h> #include <delay.h> #include <math.h> volatile unsigned int start_time,stop_time,prev_period,prev_pw; // Timer 1 input capture interrupt service routine interrupt [TIM1_CAPT] void timer1_capt_isr(void) { // Place your code here unsigned int temp; if (TCCR1B==0x41){ temp = ICR1L+(ICR1H*256); TCCR1B = 0x01; prev_period = temp - start_time; prev_pw = stop_time - start_time; start_time = temp; } else { stop_time = ICR1L+(ICR1H*256); TCCR1B = 0x41; } } // Declare your global variables here void main(void) { // Declare your local variables here unsigned char lcdbuf[39]; // Input/Output Ports initialization // Port A initialization // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T PORTA=0x00; DDRA=0x00; // Port B initialization // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T PORTB=0x00; DDRB=0x00; // Port C initialization // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T PORTC=0x00; DDRC=0x00; // Port D initialization // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T PORTD=0x00; DDRD=0x00; // Timer/Counter 0 initialization // Clock source: System Clock // Clock value: Timer 0 Stopped // Mode: Normal top=FFh // OC0 output: Disconnected TCCR0=0x00; TCNT0=0x00; OCR0=0x00; // Timer/Counter 1 initialization // Clock source: System Clock // Clock value: 2000.000 kHz // Mode: Normal top=FFFFh // OC1A output: Discon. // OC1B output: Discon. // Noise Canceler: Off // Input Capture on Rising Edge // Timer 1 Overflow Interrupt: Off // Input Capture Interrupt: On // Compare A Match Interrupt: Off // Compare B Match Interrupt: Off TCCR1A=0x00; TCCR1B=0x41; TCNT1H=0x00; TCNT1L=0x00; ICR1H=0x00; ICR1L=0x00; OCR1AH=0x00; OCR1AL=0x00; OCR1BH=0x00; OCR1BL=0x00; // Timer/Counter 2 initialization // Clock source: System Clock // Clock value: Timer 2 Stopped // Mode: Normal top=FFh // OC2 output: Disconnected ASSR=0x00; TCCR2=0x00; TCNT2=0x00; OCR2=0x00; // External Interrupt(s) initialization // INT0: Off // INT1: Off // INT2: Off MCUCR=0x00; MCUCSR=0x00; // Timer(s)/Counter(s) Interrupt(s) initialization TIMSK=0x20; // Analog Comparator initialization // Analog Comparator: Off // Analog Comparator Input Capture by Timer/Counter 1: Off ACSR=0x80; SFIOR=0x00; // LCD module initialization lcd_init(20); // Global enable interrupts #asm("sei") while (1) { // Place your code here int t; lcd_clear(); t = (((float)prev_pw/(float)prev_period)-0.32)/0.00047; sprintf(lcdbuf,"Temp:%-i.%-u\xdfC",t/10,abs(t%10)); lcd_puts(lcdbuf); delay_ms(500); }; }
آقا یک توضیح در مورد برنامه میدی . به کدوم پایه وصل کردی ؟؟
دیدگاه
دیدگاه