www.mcudriver.cn 1.1 / 1) WinAVR20070525 2) Source Insight 3) ISP 4) PonyProg ISP 5) USB 6) 1.2. MCU ATMEGA16 1.3. AVR8 1.4 LED0~LED7 1 1.5 #include <avr/io.h> // Program 1.1 LED.C #define uchar unsigned char // uchar unsigned char #define uint unsigned int // uint unsigned int port_init IO IO void port_init(void) PORTA = 0xFF; DDRA = 0xFF; www.mcudriver.cn AVR 51 Freescale8 1 1-wire I2C SPI
www.mcudriver.cn PORTB = 0xFF; DDRB = 0xFF; PORTC = 0xFF; DDRC = 0xFF; PORTD = 0xFF; DDRD = 0xFF; delay t t 0~255 void delay(uchar t) uchar i; uint j; for(i=0;i<t;i++) for(j=0;j<60000;j++) // 5ms asm("nop"); main void main(void)// port_init(); while(1) delay(200); // 1s PORTC = 0xff; //8 LED delay(200); // 1s PORTC = 0x0; //8 LED 1.6 C HEX AVR www.mcudriver.cn AVR 51 Freescale8 2 1-wire I2C SPI
www.mcudriver.cn 2.1 / 1) WinAVR20070525 2) Source Insight 3) ISP 4) PonyProg ISP 5) USB 6) 2.2 MCU ATMEGA16 2 2.3 AVR8 2.4 KEY1~KEY4 LED 1) KEY1 LED0 7 LED 2) KEY2 LED1 7 LED 3) KEY3 LED2 7 LED 4) KEY4 LED3 7 LED 5) 8 LED 2.5 Program 2.1 POLL_BUZZER.C #include <avr/io.h> // #define uchar unsigned char // uchar unsigned char #define uint unsigned int // uint unsigned int #define LED0_On (PORTC&=~(1<<PC0)) // PC^0=0 LED0 #define LED1_On (PORTC&=~(1<<PC1)) // PC^1=0 LED1 #define LED2_On (PORTC&=~(1<<PC2)) // PC^2=0 LED2 www.mcudriver.cn AVR 51 Freescale8 3 1-wire I2C SPI
www.mcudriver.cn #define LED3_On (PORTC&=~(1<<PC3)) // PC^3=0 LED3 port_init IO IO void port_init(void) PORTA = 0xFF; DDRA = 0xFF; PORTB = 0xFF; DDRB = 0xFF; PORTC = 0xFF; DDRC = 0xFF; PORTD = 0xFF; DDRD = 0xFF; delay t t 0~255 void delay(uchar t) uchar i; uint j; for(i=0;i<t;i++) for(j=0;j<60000;j++) // 5ms asm("nop"); key_exe keynum void key_exe(uchar keynum) if(keynum ==0xE0) LED0_On; //key1 LED0 else if(keynum ==0xD0) LED1_On; //key2 LED1 else if(keynum ==0xB0) LED2_On; //key3 LED2 else if(keynum ==0x70) LED3_On; //key4 LED3 www.mcudriver.cn AVR 51 Freescale8 4 1-wire I2C SPI
www.mcudriver.cn else PORTC=0xff; // LED main void main(void)// uchar temp_pd,temp0,temp1; uchar n; // n port_init(); // IO DDRD=~((1<<PD7) (1<<PD6) (1<<PD5) (1<<PD4)); while(1) // temp_pd=pind; // IO temp0=temp_pd&0xf0;// if(temp0!=0xf0) // 0xf0 // delay(6);// 30ms temp_pd=pind; temp1=temp_pd&0xf0; // if(temp0==temp1) // key_exe(temp1); // key_exe 2.6 C HEX AVR 3.1 / 1) WinAVR20070525 2) Source Insight 3) ISP www.mcudriver.cn AVR 51 Freescale8 5 1-wire I2C SPI
www.mcudriver.cn 4) PonyProg ISP 5) USB 6) 3.2 MCU ATMEGA16 1 3.3 AVR8 3.4 3 1 3.5 #include <avr/io.h> // Program 3.1 BUZZER.C #define uchar unsigned char // uchar unsigned char #define uint unsigned int // uint unsigned int #define Buzzer_On (PORTD =(1<<PD7)) // PD^7=0 #define Buzzer_Off (PORTD&=~(1<<PD7)) // PD^7=1 port_init IO IO void port_init(void) PORTA = 0xFF; DDRA = 0xFF; PORTB = 0xFF; DDRB = 0xFF; PORTC = 0xFF; DDRC = 0xFF; PORTD = 0xFF; DDRD = 0xFF; www.mcudriver.cn AVR 51 Freescale8 6 1-wire I2C SPI
www.mcudriver.cn delay t t 0~255 void delay(uchar t) uchar i; uint j; for(i=0;i<t;i++) for(j=0;j<60000;j++) // 5ms asm("nop"); main void main(void)// uchar n; // n port_init(); Buzzer_Off;// for(n=0;n<3;n++) // 3 Buzzer_On; delay(200); // 1s Buzzer_Off; delay(200); // 1s while(1);// 3.6 C HEX AVR www.mcudriver.cn AVR 51 Freescale8 7 1-wire I2C SPI
www.mcudriver.cn 4.1 / 1) WinAVR20070525 2) Source Insight 3) ISP 4) PonyProg ISP 5) USB 6) 4.2 MCU ATMEGA16 1 2 4.3 AVR8 4.4 KEY1~KEY4 LED 1) KEY1 LED0 7 LED 2) KEY2 LED1 7 LED 3) KEY3 LED2 7 LED 4) KEY4 LED3 7 LED 5) 8 LED 4.5 Program 4.1 INT_BUZZER.C #include <avr/io.h> // #include <avr/signal.h> #include <avr/interrupt.h> #define uchar unsigned char // uchar unsigned char #define uint unsigned int // uint unsigned int www.mcudriver.cn AVR 51 Freescale8 8 1-wire I2C SPI
www.mcudriver.cn #define LED0_On (PORTC&=~(1<<PC0)) // PC^0=0 LED0 #define LED1_On (PORTC&=~(1<<PC1)) // PC^1=0 LED1 #define LED2_On (PORTC&=~(1<<PC2)) // PC^2=0 LED2 #define LED3_On (PORTC&=~(1<<PC3)) // PC^3=0 LED3 port_init IO IO void port_init(void) PORTA = 0xFF; DDRA = 0xFF; PORTB = 0xFF; DDRB = 0xFF; PORTC = 0xFF; DDRC = 0xFF; PORTD = 0xFF; DDRD = 0xFF; INT_Init void INT_Init() cli(); // MCUCR = 0x02; //INT0 GICR = 0x40; // 0 sei(); // delay t t 0~255 void delay(uchar t) uchar i; uint j; for(i=0;i<t;i++) for(j=0;j<60000;j++) // 5ms asm("nop"); www.mcudriver.cn AVR 51 Freescale8 9 1-wire I2C SPI
www.mcudriver.cn key_exe keynum void key_exe(uchar keynum) if(keynum ==0xE0) LED0_On; //key1 LED0 else if(keynum ==0xD0) LED1_On; //key2 LED1 else if(keynum ==0xB0) LED2_On; //key3 LED2 else if(keynum ==0x70) LED3_On; //key4 LED3 else PORTC=0xff; // LED SIGNAL(SIG_INTERRUPT0) INT0 SIGNAL(SIG_INTERRUPT0) uchar temp_pd,temp0,temp1; temp_pd=pind; // IO temp0=temp_pd&0xf0;// if(temp0!=0xf0) // 0xf0 // delay(6);// 30ms temp_pd=pind; temp1=temp_pd&0xf0; // if(temp0==temp1) // key_exe(temp1); // key_exe main void main(void)// www.mcudriver.cn AVR 51 Freescale8 10 1-wire I2C SPI
www.mcudriver.cn port_init(); // IO DDRD=~((1<<PD7) (1<<PD6) (1<<PD5) (1<<PD4)); INT_Init(); while(1) // INT0 4.6 C HEX AVR 5.1 / 1) WinAVR20070525 2) Source Insight 3) ISP 4) PonyProg ISP 5) USB 6) 5.2 MCU ATMEGA16 1 3 5.3 AVR8 5.4 LED 1 3 5 7 2 4 6 8 5. 5 Program 5.1 TIMER0.C www.mcudriver.cn AVR 51 Freescale8 11 1-wire I2C SPI
www.mcudriver.cn #include <avr/io.h> // #include <avr/signal.h> #include <avr/interrupt.h> #define uchar unsigned char // uchar unsigned char #define uint unsigned int // uint unsigned int #define LED0_On (PORTC&=~(1<<PC0)) // PC^0=0 LED0 #define LED1_On (PORTC&=~(1<<PC1)) // PC^1=0 LED1 #define LED2_On (PORTC&=~(1<<PC2)) // PC^2=0 LED2 #define LED3_On (PORTC&=~(1<<PC3)) // PC^3=0 LED3 port_init IO IO void port_init(void) PORTA = 0xFF; DDRA = 0xFF; PORTB = 0xFF; DDRB = 0xFF; PORTC = 0xFF; DDRC = 0xFF; PORTD = 0xFF; DDRD = 0xFF; delay t t 0~255 void delay(uchar t) uchar i; uint j; for(i=0;i<t;i++) for(j=0;j<60000;j++) // 5ms asm("nop"); SIGNAL(SIG_INTERRUPT0) INT0 www.mcudriver.cn AVR 51 Freescale8 12 1-wire I2C SPI
www.mcudriver.cn SIGNAL(SIG_INTERRUPT0) uchar temp_pd,temp0,temp1; temp_pd=pind; // IO temp0=temp_pd&0xf0;// if(temp0!=0xf0) // 0xf0 // delay(6);// 30ms temp_pd=pind; temp1=temp_pd&0xf0; // if(temp0==temp1) // key_exe(temp1); // key_exe main void main(void)// port_init(); // IO DDRD=~((1<<PD7) (1<<PD6) (1<<PD5) (1<<PD4)); while(1) // INT0 5.6 C HEX AVR www.mcudriver.cn AVR 51 Freescale8 13 1-wire I2C SPI
www.mcudriver.cn 6.1 / 1) WinAVR20070525 2) Source Insight 3) ISP 4) PonyProg ISP 5) USB 6) 7) RS232 6.2 MCU ATMEGA16 4 6.3 AVR8 6.4 PC This is a RS232 Comunication Program! PC PC 6.5 Program 6.1 RS232.C #include <avr/io.h> // #define uchar unsigned char // uchar unsigned char #define uint unsigned int // uint unsigned int #define fosc 12000000 // 12MHZ #define baud 9600 // RS232 port_init IO IO www.mcudriver.cn AVR 51 Freescale8 14 1-wire I2C SPI
www.mcudriver.cn void port_init(void) PORTA = 0xFF; DDRA = 0xFF; PORTB = 0xFF; DDRB = 0xFF; PORTC = 0xFF; DDRC = 0xFF; PORTD = 0xFF; DDRD = 0xFF; uart_init UART void uart_init(void) UCSRB=(1<<RXEN) (1<<TXEN);// UBRRL=(fosc/16/(baud+1))%256; UBRRH=(fosc/16/(baud+1))/256; // 9600 UCSRC=(1<<URSEL) (1<<UCSZ1) (1<<UCSZ0);//8 +1 STOP ( ) getchar uchar getchar(void) while(!(ucsra& (1<<RXC))); return UDR; putchar void putchar(uchar c) while (!(UCSRA&(1<<UDRE))); UDR=c; www.mcudriver.cn AVR 51 Freescale8 15 1-wire I2C SPI
www.mcudriver.cn puts int puts(char *s) while (*s) putchar(*s); s++; putchar(0x0a); putchar(0x0d); // return 1; main void main(void) uchar temp; port_init(); uart_init(); puts( This is a RS232 Comunication Program! );// while(1) // temp= getchar();// PC temp putchar(temp); // PC 6.6 C HEX AVR 6.7 comdebug.exe www.mcudriver.cn AVR 51 Freescale8 16 1-wire I2C SPI
www.mcudriver.cn PC USB This is a RS232 Comunication Program! PC S PC 7.1 / 1) WinAVR20070525 2) Source Insight 3) ISP 4) PonyProg ISP 5) USB 6) 7) A/D www.mcudriver.cn AVR 51 Freescale8 17 1-wire I2C SPI
www.mcudriver.cn 7.2 MCU ATMEGA16 4 5 7.3 AVR8 7.4 PC This is a A/D Conversion Program! ADC 0 PC 7.5 Program 7.1 ADC0.C #include <avr/io.h> // #define uchar unsigned char // uchar unsigned char #define uint unsigned int // uint unsigned int #define fosc 12000000 // 12MHZ #define baud 9600 // RS232 uint adc_rel; //AD adc_rel uchar adc_mux;//ad port_init IO IO void port_init(void) PORTA = 0xFF; DDRA = 0xFF; PORTB = 0xFF; DDRB = 0xFF; PORTC = 0xFF; DDRC = 0xFF; PORTD = 0xFF; DDRD = 0xFF; uart_init UART www.mcudriver.cn AVR 51 Freescale8 18 1-wire I2C SPI
www.mcudriver.cn void uart_init(void) UCSRB=(1<<RXEN) (1<<TXEN);// UBRRL=(fosc/16/(baud+1))%256; UBRRH=(fosc/16/(baud+1))/256; // 9600 UCSRC=(1<<URSEL) (1<<UCSZ1) (1<<UCSZ0);//8 +1 STOP ( ) getchar uchar getchar(void) while(!(ucsra& (1<<RXC))); return UDR; putchar void putchar(uchar c) while (!(UCSRA&(1<<UDRE))); UDR=c; puts int puts(char *s) while (*s) putchar(*s); s++; putchar(0x0a); putchar(0x0d); // www.mcudriver.cn AVR 51 Freescale8 19 1-wire I2C SPI
www.mcudriver.cn return 1; adc_init ADC void adc_init(void) adc_mux=0;// 0 DDRC=0x00; PORTC=0x00; // PC ADC ADCSRA = 0x00; // AVCC, 0 ADMUX =(1<<REFS0) (adc_mux&0x0f); ACSR =(1<<ACD); // // 64 ADCSRA=(1<<ADEN) (1<<ADSC) (1<<ADIE) (1<<ADPS2) (1<<ADPS1) ; SIGNAL ADC SIGNAL(SIG_ADC) adc_rel=adc&0x3ff;// AD // AVCC, 0 ADMUX =(1<<REFS0) (adc_mux&0x0f);// ADCSRA =(1<<ADSC);// AD ADCtoBCD ADC temp ADC void ADCtoBCD(uint temp) unsigned char result; unsigned char num_bit[3]; temp=(unsigned int)(((unsigned long)((unsigned long)temp*vref))/0x3ff); temp2=temp; /********** ***********/ for(i=0;i<3;i++) www.mcudriver.cn AVR 51 Freescale8 20 1-wire I2C SPI
www.mcudriver.cn num_bit[2-i]=temp%10; temp=temp/10; putchar(48+num_bit[0]); putchar('.'); putchar(48+num_bit[1]); putchar(48+num_bit[2]); putchar(' '); putchar(0x0d); putchar(0x0a);// delay n void delay(uchar n) uint i; for(i=0;i<n;i++) for(j=0;j<60000;j++) asm("nop");//5ms main void main(void) cli();//close interrupt port_init(); uart_init(); adc_init(); sei();//enable interrupt puts( This is a A/D Conversion Program! );// while(1) // ADCtoBCD(adc_rel);// PC delay(200);//delay 1s 7.6 C HEX www.mcudriver.cn AVR 51 Freescale8 21 1-wire I2C SPI
www.mcudriver.cn AVR 7.7 6.7 AD ADC 8.1 / 1) WinAVR20070525 2) Source Insight 3) ISP 4) PonyProg ISP 5) USB 6) 7) PC PS/2 8) PS/2 8.2 MCU ATMEGA16 1 2 4 8.3 AVR8 8.4 PC This is a PS/2 Comunication Program! PS/2 PC 8.5 www.mcudriver.cn AVR 51 Freescale8 22 1-wire I2C SPI
www.mcudriver.cn Program 8.1 PS2.C #include <avr/io.h> // #define uchar unsigned char // uchar unsigned char #define uint unsigned int // uint unsigned int #define fosc 12000000 // 12MHZ #define baud 9600 // RS232 static unsigned char IntNum=0 ; // static unsigned char KeyV=0; // static unsigned char DisNum=0; // static unsigned char Key_UP=0,;//Key_UP static unsigned char BF=0; // unsigned char UnShifted[59][2] = //PS/2 shift 0x1C, 'a', 0x32, 'b', 0x21, 'c', 0x23, 'd', 0x24, 'e', 0x2B, 'f', 0x34, 'g', 0x33, 'h', 0x43, 'i', 0x3B, 'j', 0x42, 'k', 0x4B, 'l', 0x3A, 'm', 0x31, 'n', 0x44, 'o', 0x4D, 'p', 0x15, 'q', 0x2D, 'r', 0x1B, 's', 0x2C, 't', 0x3C, 'u', 0x2A, 'v', 0x1D, 'w', 0x22, 'x', 0x35, 'y', 0x1A, 'z', 0x45, '0', 0x16, '1', 0x1E, '2', 0x26, '3', 0x25, '4', 0x2E, '5', www.mcudriver.cn AVR 51 Freescale8 23 1-wire I2C SPI
www.mcudriver.cn 0x36, '6', 0x3D, '7', 0x3E, '8', 0x46, '9', 0x0E, '`', 0x4E, '-', 0x55, '=', 0x5D, '\\', 0x29, ' ', 0x54, '[', 0x5B, ']', 0x4C, ';', 0x52, '\'', 0x41, ',', 0x49, '.', 0x4A, '/', 0x71, '.', 0x70, '0', 0x69, '1', 0x72, '2', 0x7A, '3', 0x6B, '4', 0x73, '5', 0x74, '6', 0x6C, '7', 0x75, '8', 0x7D, '9', ; port_init IO IO void port_init(void) PORTA = 0xFF; DDRA = 0xFF; PORTB = 0xFF; DDRB = 0xFF; PORTC = 0xFF; DDRC = 0xFF; PORTD = 0xFF; DDRD = 0xFF; www.mcudriver.cn AVR 51 Freescale8 24 1-wire I2C SPI
www.mcudriver.cn uart_init UART void uart_init(void) UCSRB=(1<<RXEN) (1<<TXEN);// UBRRL=(fosc/16/(baud+1))%256; UBRRH=(fosc/16/(baud+1))/256; // 9600 UCSRC=(1<<URSEL) (1<<UCSZ1) (1<<UCSZ0);//8 +1 STOP ( ) getchar uchar getchar(void) while(!(ucsra& (1<<RXC))); return UDR; putchar void putchar(uchar c) while (!(UCSRA&(1<<UDRE))); UDR=c; puts int puts(char *s) while (*s) putchar(*s); s++; www.mcudriver.cn AVR 51 Freescale8 25 1-wire I2C SPI
www.mcudriver.cn putchar(0x0a); putchar(0x0d); // return 1; void Decode(unsigned char ScanCode) uchar No; if (!Key_UP) // switch (ScanCode) case 0xF0 : // 0xF0 Key_UP 1 Key_UP = 1; break; case 0x12 : // SHIFT Shift = 1; break; case 0x59 : // SHIFT Shift = 1; break; default: if (DisNum > 15) // 15 putchar(0x0d); putchar(0x0a); DisNum = 0; for (No = 0;(UnShifted[No][0]!=ScanCode) &&( No<59); No++); // if (UnShifted[No][0] == ScanCode) putchar(unshifted[no][1]);// PC DisNum++; break; else Key_UP = 0; BF = 0; // SIGNAL(INTERRUPT1) if ((IntNum > 0) && (IntNum < 9)) KeyV = KeyV >> 1; // >> if (PIND&0x10) KeyV = KeyV 0x80; // 1 1 www.mcudriver.cn AVR 51 Freescale8 26 1-wire I2C SPI
www.mcudriver.cn IntNum++; while (!(PIND&0x08)); // PS/2CLK if (IntNum > 10) IntNum = 0; // 11 BF = 1; // cli(); // ( BF // Decode() Decode ) main void main(void) uchar temp; cli();// port_init(); uart_init(); MCUCR = 0x0; GICR = 0x80; // INT1 sei();// puts( This is a PS/2 Program! );// while(1) if(bf) Decode(KeyV); else sei(); // 8.6 C HEX AVR www.mcudriver.cn AVR 51 Freescale8 27 1-wire I2C SPI