新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設計應用 > 于十天學會AVR單片機的教程頭文件

于十天學會AVR單片機的教程頭文件

作者: 時間:2016-11-19 來源:網(wǎng)絡 收藏
最近開始學習AVR單片機,由于之前買的51開發(fā)板是兼容AVR單片機的,所以就沒有必要開發(fā)板了。于是從網(wǎng)上找了一個教程《十天學會AVR單片機》,也是郭天祥公司的人講的,覺得還可以。因為沒有買開發(fā)板,所以沒有AVR_PQ1A.h這個頭文件,這頭文件定義了寄存器等,非常好用,所以有必要下下來解讀。
/*文件:AVR_PQ1A.H功能:定義一些AVR_PQ1A開發(fā)板上應用程序用到的常量//#include    //包含型號頭文件#include    //包含"位"操作頭文件/數(shù)據(jù)類型宏定義/#define uchar unsigned char#define uint unsigned int/數(shù)碼管和LED常量/#define LEDLK PA2 //LED鎖存器控制端#define SEGLK PA3 //數(shù)碼管段選鎖存器控制端#define BITLK PA4 //數(shù)碼管位選鎖存器控制端#define TRUE 1#define FALSE 0#define ALLLED 0XFF/*函數(shù)名稱: Delayus功    能: 延時指定微秒(8M晶振)參    數(shù): US--延時的微秒數(shù)(大約,不是很精確,MS越大越準確)返回值  : 無//void Delayus(uint US)    {uint i;US=US*5/4;      //5/4是在8MHz晶振下,通過軟件仿真反復實驗得到的數(shù)值for( i=0;i總線DAPORTB=0xFF; //關閉LEDPORTA|=BIT(LEDLK); //鎖存數(shù)據(jù),使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為列數(shù)#define LCDa_L2  0xC0 //第二行的地址:0x80+0x40+addr#define LCDa_CGRAM_ADDR 0x40 //CGRAM的開始地址#define LCDa_CGMAX  64 //CGRAM存儲的最大字節(jié)數(shù)#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 //輸出數(shù)據(jù)總線端口定義#define LCDa_DI  PINB //輸入數(shù)據(jù)總線端口定義#define LCDa_DATA_DDR DDRB //數(shù)據(jù)總線方向寄存器定義#define LCDa_FUNCTION 0x38    // 液晶模式為8位,2行,5*8字符#define iDat 1  //數(shù)據(jù)標志#define iCmd 0  //指令標志#define LCDa_CLS 0x01  // 清屏#define LCDa_HOME 0x02  // 地址返回原點,不改變DDRAM內(nèi)容#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存儲的最大字節(jié)數(shù)#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 //輸出數(shù)據(jù)總線端口定義#define LCDb_DI  PINB //輸入數(shù)據(jù)總線端口定義#define LCDb_DATA_DDR DDRB //數(shù)據(jù)總線方向寄存器定義#define LCDb_FUNCTION 0x38    // 液晶模式為8位#define LCDb_BASCMD 0x30  // 基本指令集#define LCDb_CLS 0x01  // 清屏#define LCDb_HOME 0x02  // 地址返回原點,不改變DDRAM內(nèi)容#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 //串行數(shù)據(jù)線#define DA_CLK  PB7 //串行時鐘線#define DA_CS   PA1 //片選線/DS18B20常量/#define DS18B20 PA5/TWI(IIC)常量/#define START 0x08  //START信號發(fā)送完畢狀態(tài)#define MT_SLA_ACK 0x18  //從器件地址發(fā)送,返回ACK#define MT_SLA_NOACK 0x20 //從器件地址發(fā)送,但是返回NOACK#define MT_DATA_ACK  0x28 //數(shù)據(jù)已發(fā)送,返回ACK#define MT_DATA_NOACK 0x30 //數(shù)據(jù)已發(fā)送,返回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的端點索引數(shù)目  /#define NUM_ENDPOINTS        4/* 設備描述符: 測試設備類型 */#define USB_CLASS_CODE_TEST_CLASS_DEVICE                    0xdc/* 接口描述符: 子類代碼 */#define USB_SUBCLASS_CODE_TEST_CLASS_D12                    0xA0/* 接口描述符: 協(xié)議代碼 */#define USB_PROTOCOL_CODE_TEST_CLASS_D12                    0xB0/*USB 端點描述符結(jié)構(gòu)體: 端點屬性/#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/通用子函數(shù)/void Delayms(uint MS);/LED子函數(shù)/void One_led(uchar number,uchar light);void All_led(uchar light);void Flow_LED(uchar MS);void Flash_LED(uchar number,uchar MS);/數(shù)碼管子函數(shù)/void One_smg_display(uchar data,uchar number);void All_smg_display(uchar *pdata);void Cycle_smg_display(uchar *pdata);/1602液晶管子函數(shù)/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函數(shù)/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);

本文引用地址:http://m.butianyuan.cn/article/201611/318415.htm

注意以上是針對ATMEGA16單片機的,否則需要修改第一句#include



關鍵詞: AVR單片機頭文

評論


技術專區(qū)

關閉