⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hawaii_include.h

📁 CAN转232总线的单片机程序(AVR实现)
💻 H
📖 第 1 页 / 共 2 页
字号:
#define uchar unsigned char
#define uint unsigned int
#define ulon unsigned long

#define NOP	asm("NOP")  //空操作
#define WDR 	asm("WDR")  //
#define CLI 	asm("CLI")  //关闭全局中断
#define SEI 	asm("SEI")  //开启全局中断

#define BIT(X)	             (1<<(X))              //置位X
#define SETBIT(ADDRESS,X)    (ADDRESS |= (1<<X ))  //置位X
#define CLEARBIT(ADDRESS,X)  (ADDRESS &= ~(1<<X )) //清位X
#define TESTBIT(ADDRESS,X)   (ADDRESS &  (1<<X ))  //检测位X是否是1
#define AD_START             SETBIT(ADCSRA,ADSC)   //启动AD转换
#define AD_STOP              CLEARBIT(ADCSRA,ADSC) //停止AD转换


#define  ARR_SIZE(a)  (sizeof((a))/sizeof((a[0])))//返回数组元素的个数
#define  WORD_LO(x)  ((byte) ((word)(x) & 255))   //得到一个字的高位和低位字节
#define  WORD_HI(x)  ((byte) ((word)(x) >> 8))    //得到一个字的高位和低位字节
#define  B_PTR(var)  ( (byte *) (void *) &(var))  //得到一个变量的地址(word宽度)
#define  W_PTR(var)  ( (word *) (void *) &(var))  //得到一个变量的地址(word宽度)
#define  MAX( x,y )  ( ((x) > (y)) ? (x) : (y) )  //求最大值和最小值
#define  MIN( x,y )  ( ((x) < (y)) ? (x) : (y) )  //求最大值和最小值
#define  MEM_B( x )  ( *( (byte *) (x) ) )        //得到指定地址上的一个字节或字
#define  MEM_W( x )  ( *( (word *) (x) ) )        //得到指定地址上的一个字节或字


#define XTAL             8  //时钟频率8MHz
#define PORTCANADR    PORTA
#define DDRCANADR     DDRA
#define PINCANADR     PINA

#define PORTCANCS   PORTC
#define DDRCANCS    DDRC
#define PINCANCS    PINC
#define CS          0

#define PORTCANRST   PORTC
#define DDRCANRST    DDRC
#define PINCANRST    PINC
#define RST         1


#define PORTCANINT  PORTD
#define DDRCANINT   DDRD
#define PINCANINT   PIND
#define CAN_INT     0

#define PORTLED     PORTD
#define DDRLED      DDRD
#define PINLED      PIND
#define LED1         4
#define LED2         5
#define LED3         6
#define LED4         7

#define PORTCANWR   PORTG
#define DDRCANWR    DDRG
#define PINCANWR    PING
#define WR          0

#define PORTCANRD   PORTG
#define DDRCANRD    DDRG
#define PINCANRD    PING
#define RD          1

#define PORTCANALE   PORTG
#define DDRCANALE    DDRG
#define PINCANALE    PING
#define ALE          2

/*------函数定义------*/
extern void init_devices(void);
extern void port_init(void);
extern void timer0_init(void);
extern void timer1_init(void);
extern void timer2_init(void);
extern void adc_init(void);
extern void comparator_init(void);
extern void spi_init(void);
extern void twi_init(void);
extern void usart_init(void);
extern void int_init(void);

//SPI函数
extern void  spi_write(uchar sData);
extern uchar spi_read(void);
extern void  twi_start(void);
extern void  twi_stop(void);
extern void  twi_to_write(void);
extern void  twi_to_read(void);
extern void  twi_send(uchar data);
extern void  twi_receive(void);

//IIC函数
extern void  delay_iic(void);
extern void  iic_init(void);
extern uchar iic_write_byte(uchar write_byte);
extern uchar iic_write_nbyte(uchar iic_address,uchar iic_data);
extern uchar iic_read_byte(void);
extern uchar iic_read_nbyte(uchar iic_address,uchar iic_data);
extern uchar iic_write_command(uchar write_byte);
extern uchar iic_start(void);
extern uchar iic_stop(void);
extern void  led_init(void);
extern void  CtrlKeypad(uchar lu, uchar on);

//内置EEPROM函数
extern uchar eeprom_read(uint addr);
extern void  eeprom_write(uint addr,uchar wData);
extern void  eeprom_reads(uint addr, uchar size);
extern void  eeprom_writes(uint addr, uchar size);

//选择AD通道函数,每次加1,在ADC0-ADC3之间循环,可以修改函数改正之
extern void  ad_select_next(void);

//延时函数
extern void delay_1us(void);
extern void delay_nus(uint nus);
extern void delay_1ms(void);
extern void delay_nms(uint nms);

//MAIN
uchar key_scan_first(void);
uchar key_scan(void);
void  send_buff(uchar send_num);

uchar  Config_SJA(void);                        //配置sja1000
void   CanRcv_Prg(void);		                //can总线数据接收后处理
void   CanSend_Prg(void);         				//can发送数据
void   CanErr_Prg(void);	                    //发现错误后处理
void   CanDtOver_Prg(void);                     //超载处理
void   CanWui_Prg(void);                    	//唤醒中断处理
void   D2D3Dispaly(void);                          //D2,D3闪 


/* Port F Input Pins Address - PINF */
#define    PINF7    7
#define    PINF6    6
#define    PINF5    5
#define    PINF4    4
#define    PINF3    3
#define    PINF2    2
#define    PINF1    1
#define    PINF0    0

/* Port E Input Pins Address - PINE */
#define    PINE7    7
#define    PINE6    6
#define    PINE5    5
#define    PINE4    4
#define    PINE3    3
#define    PINE2    2
#define    PINE1    1
#define    PINE0    0

/* Port E Data Direction Address - DDRE */
#define    DDE7     7
#define    DDE6     6
#define    DDE5     5
#define    DDE4     4
#define    DDE3     3
#define    DDE2     2
#define    DDE1     1
#define    DDE0     0
 
/* Port E Data Register - PORTE */
#define    PE7      7
#define    PE6      6
#define    PE5      5
#define    PE4      4
#define    PE3      3
#define    PE2      2
#define    PE1      1
#define    PE0      0
 
/* Port E Data Register - PORTE */
#define    PORTE7   7
#define    PORTE6   6
#define    PORTE5   5
#define    PORTE4   4
#define    PORTE3   3
#define    PORTE2   2
#define    PORTE1   1
#define    PORTE0   0
 
/* ADC Control and Status Register A - ADCSRA */
#define    ADEN     7
#define    ADSC     6
#define    ADATE    5
#define    ADIF     4
#define    ADIE     3
#define    ADPS2    2
#define    ADPS1    1
#define    ADPS0    0

/* ADC Multiplexer Selection Register - ADMUX */
#define    REFS1    7
#define    REFS0    6
#define    ADLAR    5
#define    MUX4     4
#define    MUX3     3
#define    MUX2     2
#define    MUX1     1
#define    MUX0     0

/* Analog Comparator Control and Status Register - ACSR */
#define    ACD      7
#define    ACBG     6
#define    ACO      5
#define    ACI      4
#define    ACIE     3
#define    ACIC     2
#define    ACIS1    1
#define    ACIS0    0

/* USART0 Control and Status Register B - UCSR0B */
#define    RXCIE0   7
#define    TXCIE0   6
#define    UDRIE0   5
#define    RXEN0    4
#define    TXEN0    3
#define    UCSZ02   2
#define    RXB80    1
#define    TXB80    0

/* USART0 Control and Status Register A - UCSR0A */
#define    RXC0     7
#define    TXC0     6
#define    UDRE0    5
#define    FE0      4
#define    DOR0     3
#define    UPE0     2
#define    U2X0     1
#define    MPCM0    0

/* SPI Control Register - SPCR */
#define    SPIE     7
#define    SPE      6
#define    DORD     5
#define    MSTR     4
#define    CPOL     3
#define    CPHA     2
#define    SPR1     1
#define    SPR0     0

/* SPI Status Register - SPSR */
#define    SPIF     7
#define    WCOL     6
#define    SPI2X    0

/* Port D Input Pins Address - PIND */
#define    PIND7    7
#define    PIND6    6
#define    PIND5    5
#define    PIND4    4
#define    PIND3    3
#define    PIND2    2
#define    PIND1    1
#define    PIND0    0

/* Port D Data Direction Register - DDRD */
#define    DDD7     7
#define    DDD6     6
#define    DDD5     5
#define    DDD4     4
#define    DDD3     3
#define    DDD2     2
#define    DDD1     1
#define    DDD0     0
 
/* Port D Data Register - PORTD */
#define    PD7      7
#define    PD6      6
#define    PD5      5
#define    PD4      4
#define    PD3      3
#define    PD2      2
#define    PD1      1
#define    PD0      0
 
/* Port D Data Register - PORTD */
#define    PORTD7   7
#define    PORTD6   6
#define    PORTD5   5
#define    PORTD4   4
#define    PORTD3   3
#define    PORTD2   2
#define    PORTD1   1
#define    PORTD0   0
 
/* Port C Input Pins Address - PINC */
#define    PINC7    7
#define    PINC6    6
#define    PINC5    5
#define    PINC4    4
#define    PINC3    3
#define    PINC2    2
#define    PINC1    1
#define    PINC0    0

/* Port C Data Direction Register - DDRC */
#define    DDC7     7
#define    DDC6     6
#define    DDC5     5
#define    DDC4     4
#define    DDC3     3
#define    DDC2     2
#define    DDC1     1
#define    DDC0     0
 
/* Port C Data Register - PORTC */
#define    PC7      7
#define    PC6      6
#define    PC5      5
#define    PC4      4
#define    PC3      3
#define    PC2      2
#define    PC1      1
#define    PC0      0

/* Port C Data Register - PORTC */
#define    PORTC7   7
#define    PORTC6   6
#define    PORTC5   5
#define    PORTC4   4
#define    PORTC3   3
#define    PORTC2   2
#define    PORTC1   1
#define    PORTC0   0
 
/* Port B Input Pins Address - PINB */
#define    PINB7    7
#define    PINB6    6
#define    PINB5    5
#define    PINB4    4
#define    PINB3    3
#define    PINB2    2
#define    PINB1    1
#define    PINB0    0

/* Port B Data Direction Register - DDRB */
#define    DDB7     7
#define    DDB6     6
#define    DDB5     5
#define    DDB4     4
#define    DDB3     3
#define    DDB2     2
#define    DDB1     1
#define    DDB0     0
 
/* Port B Data Register - PORTB */
#define    PB7      7
#define    PB6      6
#define    PB5      5
#define    PB4      4
#define    PB3      3
#define    PB2      2
#define    PB1      1
#define    PB0      0
 
/* Port B Data Register - PORTB */
#define    PORTB7   7
#define    PORTB6   6
#define    PORTB5   5
#define    PORTB4   4
#define    PORTB3   3
#define    PORTB2   2
#define    PORTB1   1
#define    PORTB0   0
 
/* Port A Input Pins Address - PINA */
#define    PINA7    7
#define    PINA6    6
#define    PINA5    5
#define    PINA4    4
#define    PINA3    3
#define    PINA2    2
#define    PINA1    1
#define    PINA0    0

/* Port A Data Direction Register - DDRA */
#define    DDA7     7
#define    DDA6     6
#define    DDA5     5
#define    DDA4     4
#define    DDA3     3
#define    DDA2     2
#define    DDA1     1
#define    DDA0     0
 
/* Port A Data Register - PORTA */
#define    PA7      7
#define    PA6      6
#define    PA5      5
#define    PA4      4
#define    PA3      3
#define    PA2      2
#define    PA1      1
#define    PA0      0
 
/* Port A Data Register - PORTA */
#define    PORTA7   7
#define    PORTA6   6
#define    PORTA5   5
#define    PORTA4   4
#define    PORTA3   3
#define    PORTA2   2
#define    PORTA1   1
#define    PORTA0   0

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -