Nguyên văn bởi developerv
Xem bài viết
quocviet253 code mình đây bạn.
Code:
/******************************************************* This program was created by the CodeWizardAVR V3.12 Advanced Automatic Program Generator © Copyright 1998-2014 Pavel Haiduc, HP InfoTech s.r.l. http://www.hpinfotech.com Project : Version : Date : 9/19/2016 Author : Company : Comments: Chip type : ATmega8 Program type : Application AVR Core Clock frequency: 16.000000 MHz Memory model : Small External RAM size : 0 Data Stack size : 256 *******************************************************/ #include <mega8.h> #include <delay.h> // Alphanumeric LCD functions #include <alcd.h> // Declare your global variables here bit dir_flag = 1 ; unsigned char bien_do = 50 ; //gia tri minimum là 0 và maximum là 100 unsigned int count_tb=0; unsigned int temp; unsigned int sin_table[] = {0,10,21,31,41,52,62,73,83,93,103,114,124,134,144,154,165,175,185,195,205,215,225,234,244,254,264,273,283,292,302,311,321,330,339,348,357,366,375,384,393,402,410,419,427,435,444,452,460,468,476,484,491,499,506,514,521,528,535,542,549,556,563,569,576,582,588,594,600,606,611,617,622,628,633,638,643,648,652,657,661,665,669,673,677,681,685,688,691,694,697,700,703,706,708,710,712,714,716,718,720,721,722,723,724,725,726,726,727,727,727,727,727,726,726,725,724,723,722,721,720,718,716,715,713,711,708,706,703,701,698,695,692,688,685,681,678,674,670,666,662,657,653,648,643,638,633,628,623,618,612,606,601,595,589,582,576,570,563,557,550,543,536,529,522,515,507,500,492,485,477,469,461,453,445,436,428,420,411,403,394,385,376,367,358,349,340,331,322,312,303,293,284,274,265,255,245,235,226,216,206,196,186,176,166,156,145,135,125,115,105,94,84,74,63,53,43,32,22,12}; //trinh con dieu chinh bien do sin theo % void dieu_chinh (unsigned int a){ temp = sin_table[count_tb]; temp = (temp*a)/100 ; } // Timer1 overflow interrupt service routine interrupt [TIM1_OVF] void timer1_ovf_isr(void) { // Place your code here dieu_chinh(bien_do); count_tb++ ; if (dir_flag == 1) { OCR1A = temp; //sin_table[count_tb]; // count_tb++ ; if (count_tb >= 220) { count_tb = 0 ; PORTB.3 = 0 ; delay_us(1) ; //deadtime khi dao chieu PORTB.0 = 1 ; dir_flag = ~dir_flag; }; } else { OCR1B = temp; //sin_table[count_tb]; // count_tb++ ; if (count_tb >= 220) { count_tb = 0 ; PORTB.0 = 0 ; delay_us(1) ; //deadtime khi dao chieu PORTB.3 = 1 ; dir_flag = ~dir_flag; }; } } #define FIRST_ADC_INPUT 0 #define LAST_ADC_INPUT 1 unsigned int adc_data[LAST_ADC_INPUT-FIRST_ADC_INPUT+1]; // Voltage Reference: AREF pin #define ADC_VREF_TYPE ((0<<REFS1) | (0<<REFS0) | (0<<ADLAR)) // ADC interrupt service routine // with auto input scanning interrupt [ADC_INT] void adc_isr(void) { static unsigned char input_index=0; // Read the AD conversion result adc_data[input_index]=ADCW; // Select next ADC input if (++input_index > (LAST_ADC_INPUT-FIRST_ADC_INPUT)) input_index=0; ADMUX=(FIRST_ADC_INPUT | ADC_VREF_TYPE)+input_index; // Delay needed for the stabilization of the ADC input voltage delay_us(10); // Start the AD conversion ADCSRA|=(1<<ADSC); } void hienthiso(unsigned int so, char x, char y) { unsigned char nghin,tram,chuc,dv; nghin=so/1000; tram=(so%1000)/100; chuc=(so%100)/10; dv=so%10; lcd_gotoxy(x,y); lcd_putchar(nghin+48); lcd_putchar(tram+48); lcd_putchar(chuc+48); lcd_putchar(dv+48); } void main(void) { // Declare your local variables here // Input/Output Ports initialization // Port B initialization // Function: Bit7=In Bit6=In Bit5=In Bit4=Out Bit3=Out Bit2=Out Bit1=Out Bit0=Out DDRB=(0<<DDB7) | (0<<DDB6) | (0<<DDB5) | (1<<DDB4) | (1<<DDB3) | (1<<DDB2) | (1<<DDB1) | (1<<DDB0); // State: Bit7=T Bit6=T Bit5=T Bit4=1 Bit3=1 Bit2=1 Bit1=1 Bit0=1 PORTB=(0<<PORTB7) | (0<<PORTB6) | (0<<PORTB5) | (1<<PORTB4) | (1<<PORTB3) | (1<<PORTB2) | (1<<PORTB1) | (1<<PORTB0); // Port C initialization // Function: Bit6=In Bit5=Out Bit4=In Bit3=In Bit2=In Bit1=In Bit0=In DDRC=(0<<DDC6) | (1<<DDC5) | (0<<DDC4) | (0<<DDC3) | (0<<DDC2) | (0<<DDC1) | (0<<DDC0); // State: Bit6=T Bit5=1 Bit4=P Bit3=P Bit2=P Bit1=T Bit0=T PORTC=(0<<PORTC6) | (1<<PORTC5) | (1<<PORTC4) | (1<<PORTC3) | (1<<PORTC2) | (0<<PORTC1) | (0<<PORTC0); // Port D initialization // Function: Bit7=Out Bit6=Out Bit5=Out Bit4=Out Bit3=Out Bit2=Out Bit1=Out Bit0=Out DDRD=(1<<DDD7) | (1<<DDD6) | (1<<DDD5) | (1<<DDD4) | (1<<DDD3) | (1<<DDD2) | (1<<DDD1) | (1<<DDD0); // State: Bit7=1 Bit6=1 Bit5=1 Bit4=1 Bit3=1 Bit2=1 Bit1=1 Bit0=1 PORTD=(1<<PORTD7) | (1<<PORTD6) | (1<<PORTD5) | (1<<PORTD4) | (1<<PORTD3) | (1<<PORTD2) | (1<<PORTD1) | (1<<PORTD0); // Timer/Counter 0 initialization // Clock source: System Clock // Clock value: Timer 0 Stopped TCCR0=(0<<CS02) | (0<<CS01) | (0<<CS00); TCNT0=0x00; // Timer/Counter 1 initialization // Clock source: System Clock // Clock value: 16000.000 kHz // Mode: Fast PWM top=ICR1 // OC1A output: Non-Inverted PWM // OC1B output: Non-Inverted PWM // Noise Canceler: Off // Input Capture on Falling Edge // Timer Period: 0.0455 ms // Output Pulse(s): // OC1A Period: 0.0455 ms Width: 0 us // OC1B Period: 0.0455 ms Width: 0 us // Timer1 Overflow Interrupt: On // Input Capture Interrupt: Off // Compare A Match Interrupt: Off // Compare B Match Interrupt: Off TCCR1A=(1<<COM1A1) | (0<<COM1A0) | (1<<COM1B1) | (0<<COM1B0) | (1<<WGM11) | (0<<WGM10); TCCR1B=(0<<ICNC1) | (0<<ICES1) | (1<<WGM13) | (1<<WGM12) | (0<<CS12) | (0<<CS11) | (1<<CS10); TCNT1H=0x00; TCNT1L=0x00; ICR1H=0x02; ICR1L=0xD7; OCR1AH=0x00; OCR1AL=0x00; OCR1BH=0x00; OCR1BL=0x00; // Timer/Counter 2 initialization // Clock source: System Clock // Clock value: Timer2 Stopped // Mode: Normal top=0xFF // OC2 output: Disconnected ASSR=0<<AS2; TCCR2=(0<<PWM2) | (0<<COM21) | (0<<COM20) | (0<<CTC2) | (0<<CS22) | (0<<CS21) | (0<<CS20); TCNT2=0x00; OCR2=0x00; // Timer(s)/Counter(s) Interrupt(s) initialization TIMSK=(0<<OCIE2) | (0<<TOIE2) | (0<<TICIE1) | (0<<OCIE1A) | (0<<OCIE1B) | (1<<TOIE1) | (0<<TOIE0); // External Interrupt(s) initialization // INT0: Off // INT1: Off MCUCR=(0<<ISC11) | (0<<ISC10) | (0<<ISC01) | (0<<ISC00); // USART initialization // USART disabled UCSRB=(0<<RXCIE) | (0<<TXCIE) | (0<<UDRIE) | (0<<RXEN) | (0<<TXEN) | (0<<UCSZ2) | (0<<RXB8) | (0<<TXB8); // Analog Comparator initialization // Analog Comparator: Off // The Analog Comparator's positive input is // connected to the AIN0 pin // The Analog Comparator's negative input is // connected to the AIN1 pin ACSR=(1<<ACD) | (0<<ACBG) | (0<<ACO) | (0<<ACI) | (0<<ACIE) | (0<<ACIC) | (0<<ACIS1) | (0<<ACIS0); // ADC initialization // ADC Clock frequency: 1000.000 kHz // ADC Voltage Reference: AREF pin ADMUX=FIRST_ADC_INPUT | ADC_VREF_TYPE; ADCSRA=(1<<ADEN) | (1<<ADSC) | (0<<ADFR) | (0<<ADIF) | (1<<ADIE) | (1<<ADPS2) | (0<<ADPS1) | (0<<ADPS0); SFIOR=(0<<ACME); // SPI initialization // SPI disabled SPCR=(0<<SPIE) | (0<<SPE) | (0<<DORD) | (0<<MSTR) | (0<<CPOL) | (0<<CPHA) | (0<<SPR1) | (0<<SPR0); // TWI initialization // TWI disabled TWCR=(0<<TWEA) | (0<<TWSTA) | (0<<TWSTO) | (0<<TWEN) | (0<<TWIE); // Alphanumeric LCD initialization // Connections are specified in the // Project|Configure|C Compiler|Libraries|Alphanumeric LCD menu: // RS - PORTD Bit 0 // RD - PORTD Bit 1 // EN - PORTD Bit 2 // D4 - PORTD Bit 4 // D5 - PORTD Bit 5 // D6 - PORTD Bit 6 // D7 - PORTD Bit 7 // Characters/line: 16 lcd_init(16); // Global enable interrupts #asm("sei") while (1) { // Place your code here hienthiso(adc_data[0],0,0); hienthiso(adc_data[1],10,0); OCR1A=adc_data[0]; OCR1B=adc_data[1]; } }
Comment