AVR单片机程序.docx
/* 文件名 : 闪烁灯.c * 杜邦线接法:用单条杜邦线把PD.0和J38的1端相连接。*/#include <avr/io.h>#define F_CPU 8000000 /这里的值是单片机工作的相关晶振频率#include <util/delay.h>#define LED1 PORTD|=0XFE /LED=1 LED不亮#define LED0 PORTD&=0XFE /LED=0 LED发光int main(void) DDRD = 0x01; /PD0定义为输出,PD的其他端口为输入。while(1)LED1; _delay_ms(500);LED0; _delay_ms(500);/* 文件名 : 闪烁灯2.c* 创建人 : 东流,2012年2月10日* 版本号 : 1.0* 杜邦线接法:用8针杜邦线把PD和J38的1-8连接(PD0对应J38的1端)。 用杜邦线把PB0对应J38的9端。 用杜邦线把PB1对应J38的10端。 用杜邦线把PB2对应J38的11端。 用杜邦线把PB3对应J38的12端。*/#include <avr/io.h>#define F_CPU 8000000 /这里的值是单片机工作的相关晶振频率#include <util/delay.h>int main(void) DDRD = 0xff;DDRB = 0x0f; while(1)/*北面的三个LED亮*/PORTD = 0xf8;PORTB = 0xff; _delay_ms(300);/*东面的三个LED亮*/PORTD = 0xc7;PORTB = 0xff; _delay_ms(300);/*南面的三个LED亮*/PORTD = 0x3f;PORTB = 0xfe;_delay_ms(300);/*西面的三个LED亮*/PORTD = 0xff;PORTB = 0xf1;_delay_ms(300);/*北面的两个LED亮,中间一个不亮*/PORTD = 0xfa;PORTB = 0xff;_delay_ms(300);/*东面的两个LED亮,中间一个不亮*/PORTD = 0xd7;PORTB = 0xff;_delay_ms(300);/*南面的两个LED亮,中间一个不亮*/PORTD = 0xbf;PORTB = 0xfe;_delay_ms(300);/*西面的两个LED亮,中间一个不亮*/PORTD = 0xff;PORTB = 0xf5;_delay_ms(300);/*12个LED全亮*/PORTD = 0x00;PORTB = 0xf0;_delay_ms(200);/*12个LED全灭*/PORTD = 0xff;PORTB = 0xff;_delay_ms(200);/*12个LED全亮*/PORTD = 0x00;PORTB = 0xf0;_delay_ms(200);/*12个LED全灭*/PORTD = 0xff;PORTB = 0xff;_delay_ms(200);_delay_ms(500); /延时0.5秒/* 文件名 : 闪烁灯2.c* 描述 : 在LED上根据要求,进行不同的显示。 * 杜邦线接法:用8针杜邦线把PD和J38的1-8连接(PD0对应J38的1端)。 用杜邦线把PB0对应J38的9端。 用杜邦线把PB1对应J38的10端。 用杜邦线把PB2对应J38的11端。 用杜邦线把PB3对应J38的12端。*/#include <avr/io.h>#define F_CPU 8000000 /这里的值是单片机工作的相关晶振频率#include <util/delay.h>#define uchar unsigned charint main(void)uchar i, temp;DDRD = 0xff;DDRB = 0x0f;PORTB = 0xff;PORTD = 0xff;while(1)temp = 0xf7;for(i=0; i<4; i+)PORTB = temp;/把值赋给P2口_delay_ms(100);/延时(10毫秒 * j)秒temp = temp >> 1;/值右移一位temp = temp | 0x08;PORTB = 0xff;temp = 0x7f;for(i=0; i<8; i+)PORTD = temp;/把值赋给P1口_delay_ms(100);/延时(10毫秒 * j)秒temp = temp >> 1;/值右移一位temp = temp | 0x80;PORTD=0xff;/* 文件名 : 闪烁灯2.c * 杜邦线接法:用8针杜邦线把PD和J38的1-8连接(PD0对应J38的1端)。用杜邦线把PB0对应J38的9端。用杜邦线把PB1对应J38的10端。用杜邦线把PB2对应J38的11端。用杜邦线把PB3对应J38的12端。*/#include <avr/io.h>#define F_CPU 8000000 /这里的值是单片机工作的相关晶振频率#include <util/delay.h>#define uchar unsigned char/定义北面的三个灯的两灭#define LED1L PORTD&=0XFE /LED1=0 LED1红灯发光#define LED2L PORTD&=0XFD /LED2=0 LED2黄灯发光#define LED3L PORTD&=0XFB /LED3=0 LED3绿灯发光#define LED1M PORTD|=0XFE /LED1=1 LED红灯不亮#define LED2M PORTD|=0XFD /LED2=1 LED黄灯不亮#define LED3M PORTD|=0XFB /LED3=1 LED绿灯不亮/定义东面的三个灯的两灭#define LED4L PORTD&=0XF7 /LED4=0 LED4红灯发光#define LED5L PORTD&=0XEF /LED5=0 LED5黄灯发光#define LED6L PORTD&=0XDF /LED6=0 LED6绿灯发光#define LED4M PORTD|=0XF7 /LED4=1 LED红灯不亮#define LED5M PORTD|=0XEF /LED5=1 LED黄灯不亮#define LED6M PORTD|=0XDF /LED6=1 LED绿灯不亮/定义南面的三个灯的两灭#define LED7L PORTD&=0XBF /LED7=0 LED7红灯发光#define LED8L PORTD&=0X7F /LED8=0 LED8黄灯发光#define LED9L PORTB&=0XFE /LED9=0 LED9绿灯发光#define LED7M PORTD|=0XBF /LED7=1 LED红灯不亮#define LED8M PORTD|=0X7F /LED8=1 LED黄灯不亮#define LED9M PORTB|=0XFE /LED9=1 LED绿灯不亮/定义西面的三个灯的两#define LED10L PORTB&=0XFD /LED10=0 LED10红灯发光#define LED11L PORTB&=0XFB /LED11=0 LED11黄灯发光#define LED12L PORTB&=0XF7 /LED12=0 LED12绿灯发光#define LED10M PORTB|=0XFD /LED10=1 LED红灯不亮#define LED11M PORTB|=0XFB /LED11=1 LED黄灯不亮#define LED12M PORTB|=0XF7 /LED12=1 LED绿灯不亮int main(void) unsigned char i;DDRD = 0xff;/PD口定义为输出DDRB = 0x0f;/PB口定义为输出PORTD = 0xff;/PD口赋值高电平PORTB = 0x0f;/PB口赋值高电平while(1) /*南北红灯亮、东西绿灯亮、持续时20秒*/LED1L; LED2M;LED3M;LED4M;LED5M;LED6L;LED7L;LED8M;LED9M;LED10M;LED11M;LED12L;/*这里,我们调用_delay_ms()函数的时候,参数不要过大,最好不要超过1000,怕引起溢出*/for(i=0;i<20;i+)_delay_ms(1000);/延时20个1秒,总共时间20秒/*南北红灯亮、东西黄灯亮、持续时间5秒*/LED1L;LED2M;LED3M;LED4M;LED5L;LED6M;LED7L;LED8M;LED9M;LED10M;LED11L;LED12M;for(i=0;i<5;i+)_delay_ms(1000);/*南北绿灯亮、东西红灯亮、持续时间20秒*/LED1M;LED2M;LED3L;LED4L;LED5M;LED6M;LED7M;LED8M;LED9L;LED10L;LED11M;LED12M;for(i=0;i<20;i+)_delay_ms(1000);/*南北黄灯亮、东西红灯亮、持续时间5秒*/LED1M;LED2L;LED3M;LED4L;LED5M;LED6M;LED10L;LED11M;LED12M;for(i=0;i<5;i+)_delay_ms(1000);/* 文件名 : 数码管显示.c * 杜邦线接法:用8针排线把PD口和J12的1-8连接(PD.0接J12的1端)。 用8针排线把PB口和J13的1-8连接(PB.7接J13的1端)。 */#include <avr/io.h>#define F_CPU 8000000 /这里的值是单片机工作的相关晶振频率#include <util/delay.h>#define uchar unsigned char#define uint unsigned int/数码管的段码编码uchar table16 = 0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71;/下一行的数组可以显示数值外,还可以显示数码管的点uchar table_d16 = 0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef,0xf7,0xfc,0xb9,0xde,0xf9,0xf1;int main(void) uchar i=0;DDRB = 0xff; DDRD = 0xff; PORTB = 0x00; /P2口控制的数码管的位选,当P2口的某一位为低电平时,点亮。顾客可以修改这里的值。 /比如改为0xfe,就是最后一位数码管亮。比如改为0x7f,就是最高一位数码管点亮。while(1)PORTD = tablei % 16; /在这里取 i 的个位数,不带点显示i+;_delay_ms(1000); /延时0.5秒后显示下一个数 /* 文件名 : 数码管显示.c * 杜邦线接法:用8针排线把PD口和J12的1-8连接(PD.0接J12的1端)。 用8针排线把PB口和J13的1-8连接(PB.7接J13的1端)。 */#include <avr/io.h>#define F_CPU 8000000 /这里的值是单片机工作的相关晶振频率#include <util/delay.h>#define uchar unsigned char#define uint unsigned int/数码管的段码编码uchar table16 = 0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71;/下一行的数组可以显示数值外,还可以显示数码管的点uchar table_d16 = 0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef,0xf7,0xfc,0xb9,0xde,0xf9,0xf1;int main(void) uchar i,j;DDRB = 0xff; DDRD = 0xff; while(1)for(j=0;j<100;j+)PORTD = table(i / 10) % 10; /在这里取 i 的个位数,不带点显示PORTB = 0xfd;_delay_ms(5); /延时0.5秒后显示下一个数PORTD = tablei % 10; /在这里取 i 的个位数,不带点显示PORTB = 0xfe;_delay_ms(5);i+;/* 文件名 : 数码管显示.c * 杜邦线接法:用8针排线把PD口和J12的1-8连接(PD.0接J12的1端)。 用8针排线把PB口和J13的1-8连接(PB.7接J13的1端)。 */#include <avr/io.h>#define F_CPU 8000000 /这里的值是单片机工作的相关晶振频率#include <util/delay.h>#define uchar unsigned char#define uint unsigned int/数码管的段码编码uchar table16 = 0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71;/下一行的数组可以显示数值外,还可以显示数码管的点uchar table_d16 = 0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef,0xf7,0xfc,0xb9,0xde,0xf9,0xf1;int main(void) uchar i=0;DDRB = 0xff; DDRD = 0xff; PORTB = 0xfe;PORTD = table6;while(1);/* 文件名 : 数码管显示.c * 杜邦线接法:用8针排线把PD口和J12的1-8连接(PD.0接J12的1端)。 用8针排线把PB口和J13的1-8连接(PB.7接J13的1端)。 */#include <avr/io.h>#define F_CPU 8000000 /这里的值是单片机工作的相关晶振频率#include <util/delay.h>#define uchar unsigned char#define uint unsigned int/数码管的段码编码uchar table16 = 0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71;/下一行的数组可以显示数值外,还可以显示数码管的点uchar table_d16 = 0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef,0xf7,0xfc,0xb9,0xde,0xf9,0xf1;int main(void) uchar i;DDRB = 0xff; DDRD = 0xff; while(1)PORTB = 0xfe;for(i=0;i<8;i+)PORTD = tablei; /在这里取 i 的个位数,不带点显示_delay_ms(3); /延时0.5秒后显示下一个数PORTB = (PORTB << 1)|0x01;i+;/* 文件名 : 数码管显示.c * 杜邦线接法:用8针排线把PD口和J12的1-8连接(PD.0接J12的1端)。 用8针排线把PB口和J13的1-8连接(PB.7接J13的1端)。 */#include <avr/io.h>#define F_CPU 8000000 /这里的值是单片机工作的相关晶振频率#include <util/delay.h>#define uchar unsigned char#define uint unsigned int/数码管的段码编码uchar table16 = 0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71;/下一行的数组可以显示数值外,还可以显示数码管的点uchar table_d16 = 0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef,0xf7,0xfc,0xb9,0xde,0xf9,0xf1;int main(void) uchar i,j,LED_temp;DDRB = 0xff; DDRD = 0xff; while(1)for(j=0;j<40;j+)PORTB = 0xfe;for(i=0;i<8;i+)PORTD = table(i + LED_temp) % 8; /数码管段值/点亮某一位数码管_delay_ms(2);PORTB = (PORTB << 1)|0X01;LED_temp +=1;/* 文件名 : 数码管显示.c * 杜邦线接法:用8针排线把J11口和J12的1-8连接(J11的1端接J12的8端)。用8针排线把J13口和J3的1-8连接(PB7接J13的1端)。 /注意这里是P2.7PD0接J10的1端;PD1接J10的2端;PD2接J10的3端;PD3接J10的4端。 */#include <avr/io.h>#define F_CPU 8000000 /这里的值是单片机工作的相关晶振频率#include <util/delay.h>#define uchar unsigned char#define uint unsigned int#define X164A0 PORTD&=0XFE #define X164A1 PORTD|=0XFE #define X164B0 PORTD&=0XFD #define X164B1 PORTD|=0XFD #define X164CLOCK0 PORTD&=0XFB #define X164CLOCK1 PORTD|=0XFB #define X164CLEAR0 PORTD&=0XF7 #define X164CLEAR1 PORTD|=0XF7/数码管的段码编码uchar table16 = 0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71;/下一行的数组可以显示数值外,还可以显示数码管的点uchar table_d16 = 0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef,0xf7,0xfc,0xb9,0xde,0xf9,0xf1;int main(void) uchar i,j,temp;DDRB = 0xff;DDRD = 0xff; while(1) for(j=0;j<16;j+) X164CLEAR0;X164A1;X164CLEAR1;temp=tablej;for(i=0; i<8; i+)if(temp>>i) & 0x01)X164B1;elseX164B0;X164CLOCK0;X164CLOCK1;_delay_ms(500);/* 文件名 : 数码管显示.c * 杜邦线接法:用8针排线把P0口和J12的1-8连接(P0.0接J12的1端)。用8针排线把P2口和J13的1-8连接(P2.7接J13的1端)。 /注意这里是P2.7* 杜邦线接法:用8针排线把J11口和J12的1-8连接(J11的1端接J12的8端)。用8针排线把J13口和J3的1-8连接(PB7接J13的1端)。 /注意这里是P2.7 PD0接J9的1端;PD1接J9的2端;PD2接J9的3端;PD3接J9的4端;PD4接J9的5端。 */#include <avr/io.h>#define F_CPU 8000000 /这里的值是单片机工作的相关晶振频率#include <util/delay.h>#define uchar unsigned char#define uint unsigned int#define SER0 PORTD&=0XFE #define SER1 PORTD|=0XFE #define SRCLK0 PORTD&=0XFD #define SRCLK1 PORTD|=0XFD #define SRCLR0 PORTD&=0XFB #define SRCLR1 PORTD|=0XFB #define RRCLK0 PORTD&=0XF7 #define RRCLK1 PORTD|=0XF7#define E0 PORTD&=0XEF #define E1 PORTD|=0XEFuchar table59510 = 0x81,0xcf,0x92,0x86,0xcc,0xa4,0xa0,0x8f,0x80,0x84;int main(void) uchar i,j,temp;DDRD = 0xff;E1;asm("nop"); E0;SRCLR1;while(1) for(j=0;j<10;j+)temp=table595j;for(i=0;i<8;i+) temp=temp << 1;if(temp & 0x80)SER1;elseSER0;SRCLK0;asm("nop"); SRCLK1;asm("nop"); RRCLK0;asm("nop"); RRCLK1;_delay_ms(500);/* 文件名 : 数码管显示.c * 杜邦线接法:用8针排线把PD口和J12的1-8连接(PD.0接J12的1端)。 用8针排线把PB口和J13的1-8连接(PB.7接J13的1端)。 PC0-PC4接到J8的1-4端。*/#include <avr/io.h>#define F_CPU 8000000 /这里的值是单片机工作的相关晶振频率#include <util/delay.h>#define uchar unsigned char#define uint unsigned int#define KEY1 (PINC&0X01)#define KEY2 (PINC&0X02)#define KEY3 (PINC&0X04)#define KEY4 (PINC&0X08)int main(void) DDRB = 0xff;DDRC = 0Xff;DDRD = 0xff;PORTC = 0xff;PORTB = 0xff;PORTD = 0xff;while(1)if(PINC != 0xff)delay_ms(20); /20毫秒软件防抖if(KEY1 = 0) /*西面的三个LED亮*/PORTD = 0xff;PORTB = 0xf1; else if(KEY2 = 0)/*南面的三个LED亮*/PORTD = 0x3f;PORTB = 0xfe;else if(KEY3 = 0)/*东面的三个LED亮*/PORTD = 0xc7;PORTB = 0xff; else if(KEY4 = 0)/*北面的三个LED亮*/PORTD = 0xf8;PORTB = 0xff; _delay_ms(500);/延时0.5秒再进行下次按键的检测/*蜂鸣器发音* 杜邦线接法:PB0接J19的5端。*/#include <avr/io.h>#define F_CPU 8000000 /这里的值是单片机工作的相关晶振频率#include <util/delay.h>#define uchar unsigned char#define uint unsigned int#define BELL0 PORTB&=0XFE #define BELL1 PORTB|=0XFE int main(void) uint i;DDRB = 0xff;while(1)for(i = 0; i < 500; i+)BELL0;_delay_ms(2); /延时10毫秒BELL1;_delay_ms(2); /延时10毫秒/*