最近开端学习AVR单片机,我们之前买的51开发板是兼容AVR单片机的,所以就没有必要开发板了。所以从网上找了一个教程《十天学会AVR单片机》,也是郭天祥公司的人讲的,觉得还能够。由于没有买开发板,所以没有AVR_PQ1A.h这个头文件,这头文件界说了寄存器等,十分好用,所以有必要下下来解读。
/*文件:AVR_PQ1A.H功用:界说一些AVR_PQ1A开发板上应用程序用到的常量//#include//包括类型头文件#include //包括"位"操作头文件/数据类型宏界说/#define uchar unsigned char#define uint unsigned int/数码管和LED常量/#define LEDLK PA2 //LED锁存器操控端#define SEGLK PA3 //数码管段选锁存器操控端#define BITLK PA4 //数码管位选锁存器操控端#define TRUE 1#define FALSE 0#define ALLLED 0XFF/*函数称号: Delayus功 能: 延时指定微秒(8M晶振)参 数: US--延时的微秒数(大约,不是很精确,MS越大越精确)回来值 : 无//void Delayus(uint US) {uint i;US=US*5/4; //5/4是在8MHz晶振下,经过软件仿真重复试验得到的数值for( i=0;i 总线DAPORTB=0xFF; //封闭LEDPORTA|=BIT(LEDLK); //锁存数据,使LED平息Delayus(5); PORTA&=~BIT(LEDLK);PORTB=0x00; //输出段选PORTA|=BIT(SEGLK); Delayus(50); PORTA&=~BIT(SEGLK); PORTB=0xFF; //输出位选PORTA|=BIT(BITLK); Delayus(50); PORTA&=~BIT(BITLK); PORTD=0xFF;PORTA|=BIT(PA7);Delayus(50);PORTA&=~BIT(PA7);}/1602液晶常量及操作/#define LCDa_CTRL PORTD //1602操控端口界说#define LCDa_CTRL_DDR DDRD //操控端口方向寄存器界说#define LCDa_RS PD4 //界说三个操控引脚#define LCDa_RW PD5#define LCDa_E PD6#define LCDa_L1 0x80 //榜首行的地址:0x80+addr ,addr为列数#define LCDa_L2 0xC0 //第二行的地址:0x80+0x40+addr#define LCDa_CGRAM_ADDR 0x40 //CGRAM的开端地址#define LCDa_CGMAX 64 //CGRAM存储的最大字节数#define LCDa_SET_RS LCDa_CTRL|=BIT(LCDa_RS) //三个操控管脚的操控操作#define LCDa_SET_RW LCDa_CTRL|=BIT(LCDa_RW)#define LCDa_SET_E LCDa_CTRL|=BIT(LCDa_E)#define LCDa_CLR_RS LCDa_CTRL&=~BIT(LCDa_RS)#define LCDa_CLR_RW LCDa_CTRL&=~BIT(LCDa_RW)#define LCDa_CLR_E LCDa_CTRL&=~BIT(LCDa_E)#define LCDa_DO PORTB //输出数据总线端口界说#define LCDa_DI PINB //输入数据总线端口界说#define LCDa_DATA_DDR DDRB //数据总线方向寄存器界说#define LCDa_FUNCTION 0x38 // 液晶形式为8位,2行,5*8字符#define iDat 1 //数据标志#define iCmd 0 //指令标志#define LCDa_CLS 0x01 // 清屏#define LCDa_HOME 0x02 // 地址回来原点,不改动DDRAM内容#define LCDa_ENTRY 0x06 // 设定输入形式,光标加,屏幕不移动#define LCDa_C2L 0x10 // 光标左移#define LCDa_C2R 0x14 // 光标右移#define LCDa_D2L 0x18 // 屏幕左移#define LCDa_D2R 0x1C // 屏幕又移#define LCDa_ON 0x0C // 翻开显现#define LCDa_OFF 0x08 // 封闭显现#define LCDa_CURON 0x0E // 显现光标#define LCDa_CURFLA 0x0F // 翻开光标业绩/12864液晶常量及操作/#define LCDb_CTRL PORTD //12864操控端口界说#define LCDb_CTRL_DDR DDRD //操控端口方向寄存器界说#define LCDb_RS PD4 //界说四个操控引脚#define LCDb_RW PD5#define LCDb_E PD6#define LCDb_RST PD7#define LCDb_L1 0x80 //榜首行的地址#define LCDb_L2 0x90 //第二行的地址#define LCDb_L3 0x88 //第三行的地址#define LCDb_L4 0x98 //第四行的地址#define LCDb_CGRAM_ADDR 0x40 //CGRAM的开端地址#define LCDb_CGMAX 64 //CGRAM存储的最大字节数#define LCDb_SET_RS LCDb_CTRL|=BIT(LCDb_RS) //四个操控管脚的操控操作#define LCDb_SET_RW LCDb_CTRL|=BIT(LCDb_RW)#define LCDb_SET_E LCDb_CTRL|=BIT(LCDb_E)#define LCDb_SET_RST LCDb_CTRL|=BIT(LCDb_RST)#define LCDb_CLR_RS LCDb_CTRL&=~BIT(LCDb_RS)#define LCDb_CLR_RW LCDb_CTRL&=~BIT(LCDb_RW)#define LCDb_CLR_E LCDb_CTRL&=~BIT(LCDb_E)#define LCDb_CLR_RST LCDb_CTRL&=~BIT(LCDb_RST)#define LCDb_DO PORTB //输出数据总线端口界说#define LCDb_DI PINB //输入数据总线端口界说#define LCDb_DATA_DDR DDRB //数据总线方向寄存器界说#define LCDb_FUNCTION 0x38 // 液晶形式为8位#define LCDb_BASCMD 0x30 // 根本指令集#define LCDb_CLS 0x01 // 清屏#define LCDb_HOME 0x02 // 地址回来原点,不改动DDRAM内容#define LCDb_ENTRY 0x06 // 设定输入形式,光标加,屏幕不移动#define LCDb_C2L 0x10 // 光标左移#define LCDb_C2R 0x14 // 光标右移#define LCDb_D2L 0x18 // 屏幕左移#define LCDb_D2R 0x1C // 屏幕又移#define LCDb_ON 0x0C // 翻开显现#define LCDb_OFF 0x08 // 封闭显现#define LCDb_EXTCMD1 0x34 // 扩大指令集,封闭绘图显现#define LCDb_EXTCMD2 0x36 // 扩大指令集,翻开绘图显现#define LCDb_EXTCLS 0x01 // 清屏#define LCDb_REVL1 0x04 // 反显第1行#define LCDb_REVL2 0x05 // 反显第2行#define LCDb_REVL3 0x06 // 反显第3行#define LCDb_REVL4 0x07 // 反显第4行/按键常量/#define KEY_DDR DDRD //按键方向界说#define KEY_PORTO PORTD //按键断口输出界说#define KEY_PORTI PIND //按键断口输入界说#define OUT 0xFF //输出常量#define IN 0xF0 //输入常量/按键外部中止常量/#define GLOBAL 7 //大局中止位宏界说#define EXTINT1 7 //外部中止1位宏界说#define EXTINT0 6 //外部中止0位宏界说/串行DA常量/#define DA_DATA PB5 //串行数据线#define DA_CLK PB7 //串行时钟线#define DA_CS PA1 //片选线/DS18B20常量/#define DS18B20 PA5/TWI(IIC)常量/#define START 0x08 //START信号发送结束状况#define MT_SLA_ACK 0x18 //从器材地址发送,回来ACK#define MT_SLA_NOACK 0x20 //从器材地址发送,可是回来NOACK#define MT_DATA_ACK 0x28 //数据已发送,回来ACK#define MT_DATA_NOACK 0x30 //数据已发送,回来NOACK#define Start() (TWCR=(1< led(TCS<>1010 or DS=00 or RS=00)//RAM 指令#define C_RAMBASE 0xC0 //RAM0~RAM30<<1 地址需左移一位 /USB常量/#define CSUSB PA7#define D12_INT PD3#define WR_N PD4#define RD_N PD5#define C_D PD6#define D12_CS PD7#define USBCTL_DDR DDRD#define USBCTL_PORT PORTD#define USBDAT_DDR DDRB#define USBDAT_PORT PORTB#define USBDAT_PIN PINB#define CMD 0x01#define DAT 0x00#define UEN_GINT() SREG&=~BIT(7)#define EN_GINT() SREG|=BIT(7)#define MSB(x) (((x) >> 8) & 0xFF)#define MAX_ENDPOINTS (uchar)0x3#define NOLAZYCLOCK 0x02#define CLOCKRUNNING 0x04#define INTERRUPTMODE 0x08#define SOFTCONNECT 0x10#define ENDP_NONISO 0x00#define ENDP_ISOOUT 0x40#define ENDP_ISOIN 0x80#define ENDP_ISOIO 0xC0#define CLOCK_12M 0x03#define CLOCK_4M 0x0b#define SETTOONE 0x40#define SOFONLY 0x80#define NORMALPLUSSOF 0x20#define ENDP4INTENABLE 0x40#define ENDP5INTENABLE 0x80#define INT_ENDP0OUT 0x01#define INT_ENDP0IN 0x02#define INT_ENDP1OUT 0x04#define INT_ENDP1IN 0x08#define INT_ENDP2OUT 0x10#define INT_ENDP2IN 0x20#define INT_BUSRESET 0x40#define INT_SUSPENDCHANGE 0x80#define SETUPPACKET 0x20#define BUFFER0FULL 0x20#define BUFFER1FULL 0x40#define FULLEMPTY 0x01#define STALL 0x02#define USB_IDLE 0#define USB_TRANSMIT 1#define USB_RECEIVE 2#define MAX_CTLDAT_SIZE 8#define USB_ENDPOINT_DIRECTION_MASK 0x80#define USB_REQUEST_MASK (uchar)0x0F#define USB_REQUEST_TYPE_MASK (uchar)0x60#define USB_STANDARD_REQUEST (uchar)0x00#define DEVICE_ADDRESS_MASK 0x7F#define USB_FEATURE_ENDPOINT_STALL (uint)0x0000#define USB_FEATURE_REMOTE_WAKEUP (uint)0x0001#define EP0_PACKET_SIZE 16#define EP1_PACKET_SIZE 16#define EP2_PACKET_SIZE 64#define USB_RECIPIENT (uchar)0x1F#define USB_RECIPIENT_DEVICE (uchar)0x00#define USB_RECIPIENT_INTERFACE (uchar)0x01#define USB_RECIPIENT_ENDPOINT (uchar)0x02#define USB_DEVICE_DESCRIPTOR_TYPE 0x01#define USB_CONFIGURATION_DESCRIPTOR_TYPE 0x02#define USB_STRING_DESCRIPTOR_TYPE 0x03#define USB_INTERFACE_DESCRIPTOR_TYPE 0x04#define USB_ENDPOINT_DESCRIPTOR_TYPE 0x05#define USB_POWER_DESCRIPTOR_TYPE 0x06/* 设备描述符: 测验设备类型 */#define USB_CLASS_CODE_TEST_CLASS_DEVICE 0xdc/* D12 装备描述符长度 /#define CONFIG_DESCRIPTOR_LENGTH sizeof(USB_CONFIGURATION_DESCRIPTOR) \+ sizeof(USB_INTERFACE_DESCRIPTOR) \+ (NUM_ENDPOINTS * sizeof(USB_ENDPOINT_DESCRIPTOR))/* D12 接口描述符中: 除端点0的端点索引数目 /#define NUM_ENDPOINTS 4/* 设备描述符: 测验设备类型 */#define USB_CLASS_CODE_TEST_CLASS_DEVICE 0xdc/* 接口描述符: 子类代码 */#define USB_SUBCLASS_CODE_TEST_CLASS_D12 0xA0/* 接口描述符: 协议代码 */#define USB_PROTOCOL_CODE_TEST_CLASS_D12 0xB0/*USB 端点描述符结构体: 端点特点/#define USB_ENDPOINT_TYPE_MASK 0x03#define USB_ENDPOINT_TYPE_CONTROL 0x00#define USB_ENDPOINT_TYPE_ISOCHRONOUS 0x01#define USB_ENDPOINT_TYPE_BULK 0x02#define USB_ENDPOINT_TYPE_INTERRUPT 0x03/通用子函数/void Delayms(uint MS);/LED子函数/void One_led(uchar number,uchar light);void All_led(uchar light);void Flow_LED(uchar MS);void Flash_LED(uchar number,uchar MS);/数码管子函数/void One_smg_display(uchar data,uchar number);void All_smg_display(uchar *pdata);void Cycle_smg_display(uchar *pdata);/1602液晶管子函数/void LCD1602_portini(void);uchar LCD1602_readbyte(uchar DatCmd);void LCD1602_sendbyte(uchar DatCmd, uchar dByte);void LCD1602_sendstr(uchar *ptString);void LCD1602_clear(void);uchar LCD1602_readBF(void);void LCD1602_gotoXY(uchar Row, uchar Col);void LCD1602_initial(void);/USB函数/void D12_init(void);void OutB(uchar cmd,uchar dat);uchar InB(void);void Set_addren(uchar addr, uchar enable);void Set_EPen(uchar enable);void Set_mode(uchar config,uchar clkdiv);void Set_DMA(uchar bMode);uint Read_IntR(void);uchar Sel_EP(uchar endp);uchar Read_EPsta(uchar endp);uchar Read_LttransSta(uchar endp);void Set_EPsta(uchar endp,uchar stall);void Set_Sresume(void);uchar Read_EP(uchar endp,uchar len,uchar * buf);uchar Write_EP(uchar endp,uchar len,uchar * buf);void Ack_EP(uchar endp);void Clear_buf(uchar endp);void Stall_ep0(void);void S_trans(uchar * buf, uchar len);void Ep0_rxdone(void);void Ep0_txdone(void);void Ep1_txdone(void);void Ep1_rxdone(void);void Ep2_txdone(void);void Ep2_rxdone(void);void Reserved(void);void Get_status(void);void Clr_feature(void);void Set_feature(void);void Set_addr(void);void Get_descriptor(void);void Get_config(void);void Set_config(void);void Get_interface(void);void Set_interface(void);void USB_reconnect(void);void Control_handler(void);void Uart_sendB(uchar sendB);
留意以上是针对ATMEGA16单片机的,不然需求修正榜首句#include