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

📄 common.h

📁 Atmega168+HMC1052+OLED开发指南针
💻 H
字号:
#include <avr/io.h>
#include <avr/signal.h>
#include <avr/sleep.h>
#include <avr/pgmspace.h>
#include <avr/eeprom.h>
#include <avr/iom168.h>
#include <avr/wdt.h>
#include <util/delay.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <stdio.h>

#define uint unsigned int
#define uchar unsigned char

#define F_CPU 8000000

typedef struct
{ 
	uchar 	bit0 : 1,
			bit1 : 1,
			bit2 : 1,
			bit3 : 1,
			bit4 : 1,
			bit5 : 1,
			bit6 : 1,
			bit7 : 1;
} bit_field;

// Define macro to get the value of each bit

#define GET_BIT(port) (*(volatile bit_field *) (_SFR_ADDR(port)))

// Define functions for each bit of the I/O ports in the program

#define ACC_SLEEP	GET_BIT(PORTB).bit0
#define OLED_A0		GET_BIT(PORTB).bit1
#define OLED_RESET	GET_BIT(PORTB).bit2
#define OLED_CS		GET_BIT(PORTB).bit4
#define KeyValue	GET_BIT(PINB).bit7

#define VOLDCTR		GET_BIT(PORTC).bit0
#define VDDCTR		GET_BIT(PORTC).bit1
#define R_SET		GET_BIT(PORTC).bit2
//#define VINCTR		GET_BIT(PORTC).bit5

#define G1			GET_BIT(PORTD).bit4
#define G2_1		GET_BIT(PORTD).bit5
#define G2_2		GET_BIT(PORTD).bit6
#define G2_3		GET_BIT(PORTD).bit7

#define ACC_SLEEP_ON 	ACC_SLEEP=0
#define ACC_SLEEP_OFF 	ACC_SLEEP=1
#define MAG_POWER_ON 	VDDCTR=1
#define MAG_POWER_OFF 	VDDCTR=0
#define MAG_RESET		R_SET=0
#define MAG_SET			R_SET=1

//#define BATT_POWER_ON 	VINCTR=1
//#define BATT_POWER_OFF 	VINCTR=0
#define OLED_POWER_ON 	VOLDCTR=1
#define OLED_POWER_OFF 	VOLDCTR=0
#define SET_RESET		OLED_RESET=1
#define CLR_RESET		OLED_RESET=0
#define SET_CS			OLED_CS=1
#define	CLR_CS			OLED_CS=0
#define SET_A0			OLED_A0=1
#define CLR_A0			OLED_A0=0

static const prog_uchar ASCII8X16[64][16];
void oled_initial(void);
void oled_reset(void);
void MainOLED_WCom(uchar data);
void MainOLED_Sdata(uchar data);
void clear_screen(void);
void byte_disp_8x16(uchar pag, uchar row, const prog_uchar *data, uchar flag);
void print_asc8x16(uchar pag, uchar row, const prog_uchar *data, const prog_uchar *p, uchar flag);
void print_asc8x16_center(uchar pag, const prog_uchar *data, const prog_uchar *p, uchar flag);


uchar UnitONmark,FirstTimeON;

uint AdcConvert(void);
uint g_aAdValue[65];
//These variable are used dring calibrating HMC1052.
uchar HMC1052CalibrationisOK;
int ADC_Xaxis,ADC_Yaxis,Xaxis_MAX,Xaxis_MIN,Yaxis_MAX,Yaxis_MIN;
float Xsf,Ysf,Xoff,Yoff;
void Sample_XandYaxis(void);
void CalibrationforHMC1052(void);

float Xh,Yh;
int Angle;
void CalculateforAngle(void);
void GetCurrentPointer(void);

//These variable are used dring calibrating Accelerometer.
uint ACC_XaxisADC,ACC_YaxisADC,ACCXaxis_0g,ACCYaxis_0g;
uchar ACCCalibrationisOK;

//These variable are used to process button.
uchar KeyMark;
uint KeyCounter;

#define DDR_SPI 	DDRB
#define DD_MOSI		3
#define DD_SCK		5

void SPI_MasterInit(void);
void SPI_MasterTransmit(uchar cData);

⌨️ 快捷键说明

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