1. Trang chủ
  2. » Giáo Dục - Đào Tạo

bài tập lập trình c

19 195 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Bài 1a : Chớp tắt LED dùng for nhấp nháy lần #include #include #define data PORTB unsigned int i,; #define ADC_VREF_TYPE 0x00 unsigned int read_adc(unsigned char adc_input) { ADMUX=adc_input | (ADC_VREF_TYPE & 0xff); delay_us(10); ADCSRA|=0x40; while ((ADCSRA & 0x10)==0); ADCSRA|=0x10; return ADCW; } void tatled() { data=0x00; } void main(void) { PORTB=0x00; DDRB=0xff; PORTC=0x00; DDRC=0x00; PORTD=0x00; DDRD=0x00; TCCR0=0x00; TCNT0=0x00; TCCR1A=0x00; TCCR1B=0x00; TCNT1H=0x00; TCNT1L=0x00; ICR1H=0x00; ICR1L=0x00; OCR1AH=0x00; OCR1AL=0x00; OCR1BH=0x00; OCR1BL=0x00; ASSR=0x00; TCCR2=0x00; TCNT2=0x00; OCR2=0x00; MCUCR=0x00; TIMSK=0x00; UCSRB=0x00; ACSR=0x80; SFIOR=0x00; ADMUX=ADC_VREF_TYPE & 0xff; ADCSRA=0x83; SPCR=0x00; TWCR=0x00; while (1) { // Place your code here tatled(); for(i=0;i 255 #include #include #define data PORTB unsigned int i,; #define ADC_VREF_TYPE 0x00 unsigned int read_adc(unsigned char adc_input) { ADMUX=adc_input | (ADC_VREF_TYPE & 0xff); delay_us(10); ADCSRA|=0x40; while ((ADCSRA & 0x10)==0); ADCSRA|=0x10; return ADCW; } void tatled() { data=0x00; } void main(void) { PORTB=0x00; DDRB=0xff; PORTC=0x00; DDRC=0x00; PORTD=0x00; DDRD=0x00; TCCR0=0x00; TCNT0=0x00; TCCR1A=0x00; TCCR1B=0x00; TCNT1H=0x00; TCNT1L=0x00; ICR1H=0x00; ICR1L=0x00; OCR1AH=0x00; OCR1AL=0x00; OCR1BH=0x00; OCR1BL=0x00; ASSR=0x00; TCCR2=0x00; TCNT2=0x00; OCR2=0x00; MCUCR=0x00; TIMSK=0x00; UCSRB=0x00; ACSR=0x80; SFIOR=0x00; ADMUX=ADC_VREF_TYPE & 0xff; ADCSRA=0x83; SPCR=0x00; TWCR=0x00; while (1) { tatled(); for(i=0;i PORTX.7 lưu lại Nút nhấn bt2 tắt led từ PORTX.7 -> PORTX.0 #include #include #define data PORTB #define bt1 PIND.0 #define bt2 PIND.1 unsigned int i, k; #define ADC_VREF_TYPE 0x00 unsigned int read_adc(unsigned char adc_input) { ADMUX=adc_input | (ADC_VREF_TYPE & 0xff); delay_us(10); ADCSRA|=0x40; while ((ADCSRA & 0x10)==0); ADCSRA|=0x10; return ADCW; } void tatled() { data=0x00; } void main(void) { PORTB=0x00; DDRB=0xff; PORTC=0x0f; DDRC=0x0f; PORTD=0x0f; DDRD=0x0f; TCCR0=0x00; TCNT0=0x00; TCCR1A=0x00; TCCR1B=0x00; TCNT1H=0x00; TCNT1L=0x00; ICR1H=0x00; ICR1L=0x00; OCR1AH=0x00; OCR1AL=0x00; OCR1BH=0x00; OCR1BL=0x00; ASSR=0x00; TCCR2=0x00; TCNT2=0x00; OCR2=0x00; MCUCR=0x00; TIMSK=0x00; UCSRB=0x00; ACSR=0x80; SFIOR=0x00; ADMUX=ADC_VREF_TYPE & 0xff; ADCSRA=0x83; SPCR=0x00; TWCR=0x00; while (1) { for(k=0;k PORTX.0… không lưu led #include #include #define data PORTB #define bt1 PIND.0 #define bt2 PIND.1 #define ADC_VREF_TYPE 0x00 unsigned int read_adc(unsigned char adc_input) { ADMUX=adc_input | (ADC_VREF_TYPE & 0xff); delay_us(10); ADCSRA|=0x40; while ((ADCSRA & 0x10)==0); ADCSRA|=0x10; return ADCW; } void tatled() { data=0x00; } void main(void) { PORTB=0x00; DDRB=0xff; PORTC=0x00; DDRC=0x00; PORTD=0x0f; DDRD=0x0f; TCCR0=0x00; TCNT0=0x00; TCCR1A=0x00; TCCR1B=0x00; TCNT1H=0x00; TCNT1L=0x00; ICR1H=0x00; ICR1L=0x00; OCR1AH=0x00; OCR1AL=0x00; OCR1BH=0x00; OCR1BL=0x00; ASSR=0x00; TCCR2=0x00; TCNT2=0x00; OCR2=0x00; MCUCR=0x00; TIMSK=0x00; UCSRB=0x00; ACSR=0x80; SFIOR=0x00; ADMUX=ADC_VREF_TYPE & 0xff; ADCSRA=0x83; SPCR=0x00; TWCR=0x00; while (1) { tatled(); if(bt1==0) { data=0x01; delay_ms(100); data=0x02; delay_ms(100); data=0x04; delay_ms(100); data=0x08; delay_ms(100); data=0x10; delay_ms(100); data=0x20; delay_ms(100); data=0x40; delay_ms(100); data=0x80; delay_ms(100); } tatled(); if(bt2==0) { data=0x80; delay_ms(100); data=0x40; delay_ms(100); data=0x20; delay_ms(100); data=0x10; delay_ms(100); data=0x08; delay_ms(100); data=0x04; delay_ms(100); data=0x02; delay_ms(100); data=0x01; delay_ms(100); } } } Bài Dùng nút nhấn ĐK ngắt nút nhấn bt1 đếm lên từ số -> (led đoạn) nút nhấn bt2 đếm xuống từ số -> #include #include #define bt1 PIND.2 #define bt2 PIND.3 unsigned char dem; interrupt [EXT_INT0] void ext_int0_isr(void) { dem++; } interrupt [EXT_INT1] void ext_int1_isr(void) { dem ; } #define ADC_VREF_TYPE 0x00 unsigned int read_adc(unsigned char adc_input) { ADMUX=adc_input | (ADC_VREF_TYPE & 0xff); delay_us(10); ADCSRA|=0x40; while ((ADCSRA & 0x10)==0); ADCSRA|=0x10; return ADCW; } void main(void) { PORTB=0x00; DDRB=0xff; PORTC=0x00; DDRC=0x00; PORTD=0x0f; DDRD=0x00; TCCR0=0x00; TCNT0=0x00; TCCR1A=0x00; TCCR1B=0x00; TCNT1H=0x00; TCNT1L=0x00; ICR1H=0x00; ICR1L=0x00; OCR1AH=0x00; OCR1AL=0x00; OCR1BH=0x00; OCR1BL=0x00; ASSR=0x00; TCCR2=0x00; TCNT2=0x00; OCR2=0x00; GICR|=0xC0; MCUCR=0x0B; GIFR=0xC0; TIMSK=0x00; UCSRB=0x00; ACSR=0x80; SFIOR=0x00; ADMUX=ADC_VREF_TYPE & 0xff; ADCSRA=0x84; SPCR=0x00; TWCR=0x00; #asm("sei") while (1) { bt1=dem; if(dem>9) dem=0; PORTB=dem; bt2=dem; if(dem

Ngày đăng: 29/08/2017, 08:50

Xem thêm: bài tập lập trình c

TỪ KHÓA LIÊN QUAN

w