********main.c********
#include "delay.h"
#include "sys.h"
#include "usart.h"
#include "led.h"
#include "tsensor.h"
#include "oled.h"
#include "myiic.h"
#include "BMP180.h"
#include "math.h"
#include "12864.h"
//ALIENTEK Mini STM32¿a·¢°å·¶ày′úÂë16
//Äú2¿Î¶è′«¸DÆ÷êμÑé
//¼¼êõÖ§3Ö£o
www.openedv.com
//1ãÖYêDDÇòíμç×ó¿Æ¼¼óDÏT1«Ë¾
int main(void)
{
//u16 adcx;
//float temp;
//float temperate;
extern long result_UT;
extern long result_UP;
float altitude;
delay_init(); //Ñóê±oˉêy3õê¼»ˉ
uart_init(9600); //′®¿ú3õê¼»ˉÎa9600
LED_Init(); //3õê¼»ˉóëLEDᬽóμÄó2¼t½ó¿ú
T_Adc_Init(); //ADC3õê¼»ˉ
OLED_Init(); //3õê¼»ˉOLED
IIC_Init(); //3õê¼»ˉ iic
Init_12864(); //3õê¼»ˉ12864
Init_BMP180(); //3õê¼»ˉbmp180
OLED_ShowString(20,0,"JinAn / P ,m ,`C",12); //45,0êÇ45áD0DD¿aê¼£¬12 ′ú±í×Öìå′óD¡£¬24,12,16 èyÖD′óD¡
OLED_ShowString(0,12,"Air Pressure:",12);
OLED_ShowString(0,32,"Altitude:",12);
OLED_ShowString(0,52,"Temperature:",12);
// D¡êyμãÇ°oó OLED_ShowString(77,12,"1010",12);
// OLED_ShowString(106,12,"96",12);
//OLED_ShowString(100,12,".",12);
//OLED_ShowString(100,32,".",12);
OLED_ShowString(100,52,".",12);
OLED_Refresh_Gram();//¸üDÂÏÔê¾μ½OLED
while(1)
{
//adcx=T_Get_Adc_Average(ADC_CH_TEMP,10);
//temp=(float)adcx*(3.3/4096);
//temperate=temp; //±£′æζè′«¸DÆ÷μÄμçÑ1Öμ
//adcx=temp; //
//temp-=(u8)temp; //¼õμôÕûêy2¿·Ö
//temperate=(1.43-temperate)/0.0043+25; //¼ÆËã3öμ±Ç°Î¶èÖμ
//OLED_ShowNum(77,52,(u8)temperate,4,12); ///////////////////////////////////////////////////////ζèÕûêy
//temperate-=(u8)temperate;
//OLED_ShowNum(106,52,temperate*100,2,12); //ζèD¡êy2¿·Ö
bmp180Convert();
OLED_ShowNum(77,12,result_UP,7,12); //ÏÔê¾ÆøÑ1.....................................
if(result_UP<80000)LED0=0; //ìí¼ó±¨¾ˉ ÆøÑ11yμíê±oò ¸ßÔ-μØÇøÆøÑ1μíóú600mmhg£¬¶ø1mmhg=133.3p¡£ËùòÔÎa80000p
altitude=44330.0*(1-pow((float)result_UP/101325,1/5.255)); //¼ÆËã¸ß¶è
OLED_ShowNum(77,32,altitude,4,12); //ÏÔ꾸߶è.......................................
OLED_ShowNum(77,52,(u8)(result_UT*0.1),4,12); //ÏÔê¾Î¶èÕûêy2¿·Ö òòÎa2aá¿ÖμàyèçÎa245ûóDD¡êyμã.....
if(result_UT>400)LED0=0; //¿éòÔ¼óò»¸öÅD¶Ï ±¨¾ˉ
result_UT=result_UT-(u8)(result_UT*0.1)*10; //¼ÆËãD¡êy2¿·Ö
OLED_ShowNum(103,52,result_UT,1,12); // ÏÔê¾D¡êy2¿·Ö....................................
LED1=!LED1; //3ìDòÔËDD ÂìμÆ
delay_ms(250);
OLED_Refresh_Gram();//¸üDÂÏÔê¾μ½OLED
}
}
***************BMP180.c*******
#include "BMP180.h"
#include "delay.h"
#include "myiic.h"
#include "sys.h"
#include "12864.h"
//***BMP085ê1óÃ
long result_UT=0;
long result_UP=0;
short ac1;
short ac2;
short ac3;
unsigned short ac4;
unsigned short ac5;
unsigned short ac6;
short b1;
short b2;
short mb;
short mc;
short md;
u8 ge,shi,bai,qian,wan,shiwan; //ÏÔê¾±äá¿
//*********************************************************
//¶á3öBMP085Äú2¿êy¾Y,á¬Døὸö
//*********************************************************
u16 Multiple_read(u8 ST_Address)
{
u8 msb, lsb;
u16 _data;
IIC_Start(); //Æeê¼DÅoÅ
IIC_Send_Byte(BMP180_SlaveAddress); //·¢Ëíé豸μØÖ·+D′DÅoÅ
while(IIC_Wait_Ack());
IIC_Send_Byte(ST_Address); //·¢Ëí′æ′¢μ¥ÔaμØÖ·
while(IIC_Wait_Ack());
IIC_Start(); //Æeê¼DÅoÅ
IIC_Send_Byte(BMP180_SlaveAddress+1); //·¢Ëíé豸μØÖ·+¶áDÅoÅ
while(IIC_Wait_Ack());
msb = IIC_Read_Byte(1); //BUF[0]′æ′¢
lsb = IIC_Read_Byte(0); //×îoóò»¸öêy¾YDèòa»ØNOACK
IIC_Stop(); //í£Ö1DÅoÅ
delay_ms(5);
_data = msb << 8;
_data |= lsb;
return _data;
}
//********************************************************************
u16 bmp180ReadTemp(void)
{
IIC_Start(); //Æeê¼DÅoÅ
IIC_Send_Byte(BMP180_SlaveAddress); //·¢Ëíé豸μØÖ·+D′DÅoÅ BMP180_SlaveAddress 0xee ÆøÑ1′«¸DÆ÷Æ÷¼tμØÖ·
while(IIC_Wait_Ack());
IIC_Send_Byte(0xF4); // write register address
while(IIC_Wait_Ack());
IIC_Send_Byte(0x2E); // write register data for temp
while(IIC_Wait_Ack());
IIC_Stop(); //·¢Ëíí£Ö1DÅoÅ
delay_ms(10); // max time is 4.5ms
return Multiple_read(0xF6);
}
//*************************************************************
u16 bmp180ReadPressure(void)
{
//u16 pressure = 0;
IIC_Start(); //Æeê¼DÅoÅ
IIC_Send_Byte(BMP180_SlaveAddress); //·¢Ëíé豸μØÖ·+D′DÅoÅ
while(IIC_Wait_Ack());
IIC_Send_Byte(0xF4); // write register address
while(IIC_Wait_Ack());
IIC_Send_Byte(0x34); // write register data for pressure
while(IIC_Wait_Ack());
IIC_Stop(); //·¢Ëíí£Ö1DÅoÅ
delay_ms(20); // max time is 4.5ms
//pressure = Multiple_read(0xF6);
//pressure &= 0x0FFFF;
return Multiple_read(0xF6);//pressure;
}
//**************************************************************
//3õê¼»ˉBMP085£¬¸ù¾YDèòaÇë2ο¼pdf½øDDDT¸Ä**************
void Init_BMP180()
{
IIC_Init();
Display_string(0,1,"14");
ac1 = Multiple_read(0xAA);
ac2 = Multiple_read(0xAC);
ac3 = Multiple_read(0xAE);
ac4 = Multiple_read(0xB0);
ac5 = Multiple_read(0xB2);
ac6 = Multiple_read(0xB4);
b1 = Multiple_read(0xB6);
b2 = Multiple_read(0xB8);
mb = Multiple_read(0xBA);
mc = Multiple_read(0xBC);
md = Multiple_read(0xBE);
Display_string(0,1,"111");
}
void conversion(long temp_data)
{
shiwan=temp_data/100000+0x30 ;
temp_data=temp_data%100000; //è¡óàÔËËã
wan=temp_data/10000+0x30 ;
temp_data=temp_data%10000; //è¡óàÔËËã
qian=temp_data/1000+0x30 ;
temp_data=temp_data%1000; //è¡óàÔËËã
bai=temp_data/100+0x30 ;
temp_data=temp_data%100; //è¡óàÔËËã
shi=temp_data/10+0x30 ;
temp_data=temp_data%10; //è¡óàÔËËã
ge=temp_data+0x30;
}
//***********************************************************************
void bmp180Convert()
{
unsigned int ut;
unsigned long up;
long x1, x2, b5, b6, x3, b3, p;
unsigned long b4, b7;
ut = bmp180ReadTemp(); // ¶áè¡Î¶è
up = bmp180ReadPressure(); // ¶áè¡Ñ1Ç¿ return pressure;
//*************
x1 = (((long)ut - (long)ac6)*(long)ac5) >> 15;
x2 = ((long) mc << 11) / (x1 + md);
b5 = x1 + x2;
result_UT = ((b5 + 8) >> 4);
write_12864com(0x89);
write_12864data('T'); //ζèÏÔê¾
write_12864data(':');
write_12864data(bai);
write_12864data(shi);
write_12864data('.');
write_12864data(ge);
write_12864data('C'); //ζèμ¥λ
write_12864data('a');
//*************
b6 = b5 - 4000;
// Calculate B3
x1 = (b2 * (b6 * b6)>>12)>>11;
x2 = (ac2 * b6)>>11;
x3 = x1 + x2;
b3 = (((((long)ac1)*4 + x3)<<OSS) + 2)>>2;
// Calculate B4
x1 = (ac3 * b6)>>13;
x2 = (b1 * ((b6 * b6)>>12))>>16;
x3 = ((x1 + x2) + 2)>>2;
b4 = (ac4 * (unsigned long)(x3 + 32768))>>15;
b7 = ((unsigned long)(up - b3) * (50000>>OSS));
if (b7 < 0x80000000)
p = (b7<<1)/b4;
else
p = (b7/b4)<<1;
x1 = (p>>8) * (p>>8);
x1 = (x1 * 3038)>>16;
x2 = (-7357 * p)>>16;
result_UP = p+((x1 + x2 + 3791)>>4);
}
*******************I2C.c************
#include "myiic.h"
#include "delay.h"
#include "led.h"
//////////////////////////////////////////////////////////////////////////////////
//±¾3ìDòÖ»1©Ñ§Ï°ê1óã¬Î′¾-×÷ÕßDí¿é£¬2»μÃóÃóúÆäËüèÎoÎóÃí¾
//Mini STM32¿a·¢°å
//IIC Çy¶ˉoˉêy
//ÕyμãÔ-×ó@ALIENTEK
//¼¼êõÂÛì3:
www.openedv.com
//DT¸ÄèÕÆú:2010/6/10
//°æ±¾£oV1.0
//°æè¨ËùóD£¬μá°æ±Ø¾¿¡£
//Copyright(C) ÕyμãÔ-×ó 2009-2019
//All rights reserved
//////////////////////////////////////////////////////////////////////////////////
//3õê¼»ˉIIC
void IIC_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
//RCC->APB2ENR|=1<<4;//Ïèê1ÄüíaéèIO PORTCê±Öó
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOC, ENABLE );
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12|GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP ; //íÆíìêä3ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
IIC_SCL=1;
IIC_SDA=1;
}
//2úéúIICÆeê¼DÅoÅ
void IIC_Start(void)
{
SDA_OUT(); //sdaÏßêä3ö
IIC_SDA=1;
IIC_SCL=1;
delay_us(4);
IIC_SDA=0;//START:when CLK is high,DATA change form high to low
delay_us(4);
IIC_SCL=0;//ÇˉסI2C×üÏߣ¬×¼±¸·¢Ëí»ò½óêÕêy¾Y
}
//2úéúIICí£Ö1DÅoÅ
void IIC_Stop(void)
{
SDA_OUT();//sdaÏßêä3ö
IIC_SCL=0;
IIC_SDA=0;//STOP:when CLK is high DATA change form low to high
delay_us(4);
IIC_SCL=1;
IIC_SDA=1;//·¢ËíI2C×üÏß½áêøDÅoÅ
delay_us(4);
}
//μè′yó|′eDÅoÅμ½à′
//·μ»ØÖμ£o1£¬½óêÕó|′e꧰ü
// 0£¬½óêÕó|′e3é1|
u8 IIC_Wait_Ack(void)
{
u8 ucErrTime=0;
SDA_IN(); //SDAéèÖÃÎaêäèë
IIC_SDA=1;delay_us(1);
IIC_SCL=1;delay_us(1);
while(READ_SDA)
{
ucErrTime++;
if(ucErrTime>250)
{
IIC_Stop();
return 1;
}
}
IIC_SCL=0;//ê±Öóêä3ö0
return 0;
}
//2úéúACKó|′e
void IIC_Ack(void)
{
IIC_SCL=0;
SDA_OUT();//éèÖÃÎaêä3ö
IIC_SDA=0;
delay_us(2);
IIC_SCL=1;
delay_us(2);
IIC_SCL=0;
}
//2»2úéúACKó|′e
void IIC_NAck(void)
{
IIC_SCL=0;
SDA_OUT();
IIC_SDA=1;
delay_us(2);
IIC_SCL=1;
delay_us(2);
IIC_SCL=0;
}
//IIC·¢Ëíò»¸ö×Ö½ú
//·μ»Ø′ó»úóDÎTó|′e
//1£¬óDó|′e
//0£¬ÎTó|′e
void IIC_Send_Byte(u8 txd)
{
u8 t;
SDA_OUT(); //éèÖÃÎaêä3ö¡¢
IIC_SCL=0;//à-μíê±Öó¿aê¼êy¾Y′«êä
for(t=0;t<8;t++)
{
IIC_SDA=(txd&0x80)>>7;
txd<<=1;
delay_us(2); //¶ÔTEA5767Õaèy¸öÑó걶¼êDZØDëμÄ
IIC_SCL=1;
delay_us(2);
IIC_SCL=0;
delay_us(2);
}
}
//¶á1¸ö×Ö½ú£¬ack=1ê±£¬·¢ËíACK£¬ack=0£¬·¢ËínACK
u8 IIC_Read_Byte(unsigned char ack)
{
unsigned char i,receive=0;
SDA_IN();//SDAéèÖÃÎaêäèë ///////////¶Ôóú°å×ó êäèë¡¢¡¢¡¢¡¢¡¢¡¢¡¢¡¢
for(i=0;i<8;i++ )
{
IIC_SCL=0;
delay_us(2);
IIC_SCL=1;
receive<<=1;
if(READ_SDA)receive++;
delay_us(1);
}
if (!ack)
IIC_NAck();//·¢ËínACK
else
IIC_Ack(); //·¢ËíACK
return receive;
}
/////////////////////////////////////////////////////////bmp180
/*u32 mybmp180(void)
{
IIC_Init();
IIC_Start();
u8 mid;
LED0=!LED0;
return mid;
}*/
一周热门 更多>