Project mẫu AVR (phần 2) Viết bởi: le_hieu_29 - 19/03/2012 13.Pulse period by timer1 • Sơ đồ mạch điện: Sơ đồ mạch điện • Chương trình mẫu: Chip type : ATmega32 Program type : Application Clock frequency : 6.000000 MHz Memory model : Small External SRAM size : Data Stack size : 512 *****************************************************/ #include "mega32.h" // Standard Input/Output functions #include "stdio.h" unsigned char ov_counter; unsigned int starting_edge, ending_edge; unsigned long clocks; unsigned int period_out; bit done = 0; // Timer overflow interrupt service routine interrupt [TIM1_OVF] void timer1_ovf_isr(void) { // Place your code here ++ov_counter; } // Timer input capture interrupt service routine interrupt [TIM1_CAPT] void timer1_capt_isr(void) { // Place your code here ending_edge = (unsigned int)ICR1H*256 + (unsigned int)ICR1L; clocks = (unsigned long)ending_edge \ + ((unsigned long)ov_counter*65536) \ - (unsigned long) starting_edge; period_out = clocks/750; done = 1; ov_counter = 0; starting_edge = ending_edge; } // Declare your global variables here void main(void) { // Declare your local variables here // 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 initialization // Clock source: System Clock // Clock value: Timer Stopped // Mode: Normal top=FFh // OC0 output: Disconnected TCCR0=0x00; TCNT0=0x00; OCR0=0x00; // Timer/Counter initialization // Clock source: System Clock // Clock value: 750.000 kHz // Mode: Normal top=FFFFh // OC1A output: Discon // OC1B output: Discon // Noise Canceler: Off // Input Capture on Rising Edge // Timer Overflow Interrupt: On // Input Capture Interrupt: On // Compare A Match Interrupt: Off // Compare B Match Interrupt: Off TCCR1A=0x00; TCCR1B=0x42; TCNT1H=0x00; TCNT1L=0x00; ICR1H=0x00; ICR1L=0x00; OCR1AH=0x00; OCR1AL=0x00; OCR1BH=0x00; OCR1BL=0x00; // Timer/Counter initialization // Clock source: System Clock // Clock value: Timer 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=0x24; // USART initialization // Communication Parameters: Data, Stop, No Parity // USART Receiver: On // USART Transmitter: On // USART Mode: Asynchronous // USART Baud rate: 9600 UCSRA=0x00; UCSRB=0x18; UCSRC=0x86; UBRRH=0x00; UBRRL=0x26; // Analog Comparator initialization // Analog Comparator: Off // Analog Comparator Input Capture by Timer/Counter 1: Off ACSR=0x80; SFIOR=0x00; // Global enable interrupts #asm("sei") while (1) { if(done){ printf("Pulse period in ms = %u\n\r", period_out); done = 0; } }; } 14.ADC_Complete_Int • Sơ đồ mạch điện: Sơ đồ mạch điện • Chương trình mẫu: Chip type : ATmega32 Program type : Application Clock frequency : 7.372800 MHz Memory model : Small External SRAM size : Data Stack size : 512 *****************************************************/ #include "mega32.h" #include "delay.h" // Standard Input/Output functions #include"stdio.h" #define ADC_VREF_TYPE 0x00 // Declare your global variables here bit adc_done = 0; // Global ADC done flag bit // ADC interrupt service routine interrupt [ADC_INT] void adc_isr(void) { unsigned int adc_result; // Read the AD conversion result adc_result = ADCW; // Always clear ADC IF bit ADCSRA |= 0x10; // print this value printf("Done interrupt! ADC value = %d\n\r",adc_result); // Set done bit adc_done = 1; } // Trigger ADC conversion void adc_trigger_conv(unsigned char adc_input) { // Clear done bit adc_done = 0; // Select adc channel ADMUX=adc_input|ADC_VREF_TYPE; // Start the AD conversion ADCSRA|=0x40; // Don't wait for complete, exist now } void main(void) { // Declare your local variables here // 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 initialization // Clock source: System Clock // Clock value: Timer Stopped // Mode: Normal top=FFh // OC0 output: Disconnected TCCR0=0x00; TCNT0=0x00; OCR0=0x00; // Timer/Counter initialization // Clock source: System Clock // Clock value: Timer Stopped // Mode: Normal top=FFFFh // OC1A output: Discon // OC1B output: Discon // Noise Canceler: Off // Input Capture on Falling Edge // Timer Overflow Interrupt: Off // Input Capture Interrupt: Off // Compare A Match Interrupt: Off // Compare B Match Interrupt: Off TCCR1A=0x00; TCCR1B=0x00; TCNT1H=0x00; TCNT1L=0x00; ICR1H=0x00; ICR1L=0x00; OCR1AH=0x00; OCR1AL=0x00; OCR1BH=0x00; OCR1BL=0x00; // Timer/Counter initialization // Clock source: System Clock // Clock value: Timer 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=0x00; // USART initialization // Communication Parameters: Data, Stop, No Parity // USART Receiver: On // USART Transmitter: On // USART Mode: Asynchronous // USART Baud rate: 9600 UCSRA=0x00; UCSRB=0x18; UCSRC=0x86; UBRRH=0x00; UBRRL=0x2F; // Analog Comparator initialization // Analog Comparator: Off // Analog Comparator Input Capture by Timer/Counter 1: Off ACSR=0x80; SFIOR=0x00; // ADC initialization // ADC Clock frequency: 115.200 kHz // ADC Voltage Reference: AREF pin ADMUX=ADC_VREF_TYPE; ADCSRA=0x8E; // Global enable interrupts #asm("sei") // trigger for the first time adc_trigger_conv(0); while (1) { // Place your code here if (adc_done) adc_trigger_conv(0); delay_ms(1000); }; } 15.ADC_Differential • Sơ đồ mạch điện: Sơ đồ mạch điện • Chương trình mẫu: Chip type : ATmega32 Program type : Application Clock frequency : 7.372800 MHz Memory model : Small External SRAM size : Data Stack size : 512 *****************************************************/ #include "mega32.h" #include "delay.h" // Standard Input/Output functions #include "stdio.h" #define ADC_VREF_TYPE 0x00 // User's defines // Here, we choose ADC0 as Neg Input // ADC1 as Pos Input // Gain set to 10x // Look up on table in datasheet > MUX4:0 = 0b01001 #define ADC_DIFF_TYPE (0b01001) // Convert value from ADC data register with its sign (+/-) int convert_adc_value(unsigned int adc_10bit_input_value) { int result; result = (int)(adc_10bit_input_value & 0x1FF); result = (adc_10bit_input_value & (1