1.OLED
oled.c
#include "stm32f10x.h"
#include "oled.h"
#include "stdlib.h"
#include "oledfont.h"
#include "delay.h"
u8 OLED_GRAM[144][4];
//·´ÏÔº¯Êý
void OLED_ColorTurn(u8 i)
{
if(i==0)
{
OLED_WR_Byte(0xA6,OLED_CMD);//Õý³£ÏÔʾ
}
if(i==1)
{
OLED_WR_Byte(0xA7,OLED_CMD);//·´É«ÏÔʾ
}
}
//ÆÁÄ»Ðýת180¶È
void OLED_DisplayTurn(u8 i)
{
if(i==0)
{
OLED_WR_Byte(0xC8,OLED_CMD);//Õý³£ÏÔʾ
OLED_WR_Byte(0xA1,OLED_CMD);
}
if(i==1)
{
OLED_WR_Byte(0xC0,OLED_CMD);//·´×ªÏÔʾ
OLED_WR_Byte(0xA0,OLED_CMD);
}
}
//ÑÓʱ
void IIC_delay(void)
{
u8 t=3;
while(t--);
}
//ÆðʼÐźÅ
void I2C_Start(void)
{
OLED_SDA_Set();
OLED_SCL_Set();
IIC_delay();
OLED_SDA_Clr();
IIC_delay();
OLED_SCL_Clr();
IIC_delay();
}
//½áÊøÐźÅ
void I2C_Stop(void)
{
OLED_SDA_Clr();
OLED_SCL_Set();
IIC_delay();
OLED_SDA_Set();
}
//µÈ´ýÐźÅÏìÓ¦
void I2C_WaitAck(void) //²âÊý¾ÝÐźŵĵçƽ
{
OLED_SDA_Set();
IIC_delay();
OLED_SCL_Set();
IIC_delay();
OLED_SCL_Clr();
IIC_delay();
}
//дÈëÒ»¸ö×Ö½Ú
void Send_Byte(u8 dat)
{
u8 i;
for(i=0;i<8;i++)
{
if(dat&0x80)//½«datµÄ8λ´Ó×î¸ßλÒÀ´ÎдÈë
{
OLED_SDA_Set();
}
else
{
OLED_SDA_Clr();
}
IIC_delay();
OLED_SCL_Set();
IIC_delay();
OLED_SCL_Clr();//½«Ê±ÖÓÐźÅÉèÖÃΪµÍµçƽ
dat<<=1;
}
}
//·¢ËÍÒ»¸ö×Ö½Ú
//mode:Êý¾Ý/ÃüÁî±êÖ¾ 0,±íʾÃüÁî;1,±íʾÊý¾Ý;
void OLED_WR_Byte(u8 dat,u8 mode)
{
I2C_Start();
Send_Byte(0x78);
I2C_WaitAck();
if(mode){Send_Byte(0x40);}
else{Send_Byte(0x00);}
I2C_WaitAck();
Send_Byte(dat);
I2C_WaitAck();
I2C_Stop();
}
//¿ªÆôOLEDÏÔʾ
void OLED_DisPlay_On(void)
{
OLED_WR_Byte(0x8D,OLED_CMD);//µçºÉ±ÃʹÄÜ
OLED_WR_Byte(0x14,OLED_CMD);//¿ªÆôµçºÉ±Ã
OLED_WR_Byte(0xAF,OLED_CMD);//µãÁÁÆÁÄ»
}
//¹Ø±ÕOLEDÏÔʾ
void OLED_DisPlay_Off(void)
{
OLED_WR_Byte(0x8D,OLED_CMD);//µçºÉ±ÃʹÄÜ
OLED_WR_Byte(0x10,OLED_CMD);//¹Ø±ÕµçºÉ±Ã
OLED_WR_Byte(0xAE,OLED_CMD);//¹Ø±ÕÆÁÄ»
}
//¸üÐÂÏÔ´æµ½OLED
void OLED_Refresh(void)
{
u8 i,n;
for(i=0;i<4;i++)
{
OLED_WR_Byte(0xb0+i,OLED_CMD); //ÉèÖÃÐÐÆðʼµØÖ·
OLED_WR_Byte(0x00,OLED_CMD); //ÉèÖõÍÁÐÆðʼµØÖ·
OLED_WR_Byte(0x10,OLED_CMD); //ÉèÖøßÁÐÆðʼµØÖ·
I2C_Start();
Send_Byte(0x78);
I2C_WaitAck();
Send_Byte(0x40);
I2C_WaitAck();
for(n=0;n<128;n++)
{
Send_Byte(OLED_GRAM[n][i]);
I2C_WaitAck();
}
I2C_Stop();
}
}
//ÇåÆÁº¯Êý
void OLED_Clear(void)
{
u8 i,n;
for(i=0;i<4;i++)
{
for(n=0;n<128;n++)
{
OLED_GRAM[n][i]=0;//Çå³ýËùÓÐÊý¾Ý
}
}
OLED_Refresh();//¸üÐÂÏÔʾ
}
//»µã
//x:0~127
//y:0~63
//t:1 Ìî³ä 0,Çå¿Õ
void OLED_DrawPoint(u8 x,u8 y,u8 t)
{
u8 i,m,n;
i=y/8;
m=y%8;
n=1<<m;
if(t){OLED_GRAM[x][i]|=n;}
else
{
OLED_GRAM[x][i]=~OLED_GRAM[x][i];
OLED_GRAM[x][i]|=n;
OLED_GRAM[x][i]=~OLED_GRAM[x][i];
}
}
//»Ïß
//x1,y1:Æðµã×ø±ê
//x2,y2:½áÊø×ø±ê
void OLED_DrawLine(u8 x1,u8 y1,u8 x2,u8 y2,u8 mode)
{
u16 t;
int xerr=0,yerr=0,delta_x,delta_y,distance;
int incx,incy,uRow,uCol;
delta_x=x2-x1; //¼ÆËã×ø±êÔöÁ¿
delta_y=y2-y1;
uRow=x1;//»ÏßÆðµã×ø±ê
uCol=y1;
if(delta_x>0)incx=1; //ÉèÖõ¥²½·½Ïò
else if (delta_x==0)incx=0;//´¹Ö±Ïß
else {incx=-1;delta_x=-delta_x;}
if(delta_y>0)incy=1;
else if (delta_y==0)incy=0;//ˮƽÏß
else {incy=-1;delta_y=-delta_x;}
if(delta_x>delta_y)distance=delta_x; //Ñ¡È¡»ù±¾ÔöÁ¿×ø±êÖá
else distance=delta_y;
for(t=0;t<distance+1;t++)
{
OLED_DrawPoint(uRow,uCol,mode);//»µã
xerr+=delta_x;
yerr+=delta_y;
if(xerr>distance)
{
xerr-=distance;
uRow+=incx;
}
if(yerr>distance)
{
yerr-=distance;
uCol+=incy;
}
}
}
//x,y:Ô²ÐÄ×ø±ê
//r:Ô²µÄ°ë¾¶
void OLED_DrawCircle(u8 x,u8 y,u8 r)
{
int a, b,num;
a = 0;
b = r;
while(2 * b * b >= r * r)
{
OLED_DrawPoint(x + a, y - b,1);
OLED_DrawPoint(x - a, y - b,1);
OLED_DrawPoint(x - a, y + b,1);
OLED_DrawPoint(x + a, y + b,1);
OLED_DrawPoint(x + b, y + a,1);
OLED_DrawPoint(x + b, y - a,1);
OLED_DrawPoint(x - b, y - a,1);
OLED_DrawPoint(x - b, y + a,1);
a++;
num = (a * a + b * b) - r*r;//¼ÆË㻵ĵãÀëÔ²ÐĵľàÀë
if(num > 0)
{
b--;
a--;
}
}
}
//ÔÚÖ¸¶¨Î»ÖÃÏÔʾһ¸ö×Ö·û,°üÀ¨²¿·Ö×Ö·û
//x:0~127
//y:0~63
//size1:Ñ¡Ôñ×ÖÌå 6x8/6x12/8x16/12x24
//mode:0,·´É«ÏÔʾ;1,Õý³£ÏÔʾ
void OLED_ShowChar(u8 x,u8 y,u8 chr,u8 size1,u8 mode)
{
u8 i,m,temp,size2,chr1;
u8 x0=x,y0=y;
if(size1==8)size2=6;
else size2=(size1/8+((size1%8)?1:0))*(size1/2); //µÃµ½×ÖÌåÒ»¸ö×Ö·û¶ÔÓ¦µãÕó¼¯ËùÕ¼µÄ×Ö½ÚÊý
chr1=chr-' '; //¼ÆËãÆ«ÒƺóµÄÖµ
for(i=0;i<size2;i++)
{
if(size1==8)
{temp=asc2_0806[chr1][i];} //µ÷ÓÃ0806×ÖÌå
else if(size1==12)
{temp=asc2_1206[chr1][i];} //µ÷ÓÃ1206×ÖÌå
else if(size1==16)
{temp=asc2_1608[chr1][i];} //µ÷ÓÃ1608×ÖÌå
else if(size1==24)
{temp=asc2_2412[chr1][i];} //µ÷ÓÃ2412×ÖÌå
else return;
for(m=0;m<8;m++)
{
if(temp&0x01)OLED_DrawPoint(x,y,mode);
else OLED_DrawPoint(x,y,!mode);
temp>>=1;
y++;
}
x++;
if((size1!=8)&&((x-x0)==size1/2))
{x=x0;y0=y0+8;}
y=y0;
}
}
//ÏÔʾ×Ö·û´®
//x,y:Æðµã×ø±ê
//size1:×ÖÌå´óС
//*chr:×Ö·û´®ÆðʼµØÖ·
//mode:0,·´É«ÏÔʾ;1,Õý³£ÏÔʾ
void OLED_ShowString(u8 x,u8 y,u8 *chr,u8 size1,u8 mode)
{
while((*chr>=' ')&&(*chr<='~'))//ÅжÏÊDz»ÊÇ·Ç·¨×Ö·û!
{
OLED_ShowChar(x,y,*chr,size1,mode);
if(size1==8)x+=6;
else x+=size1/2;
chr++;
}
}
//m^n
u32 OLED_Pow(u8 m,u8 n)
{
u32 result=1;
while(n--)
{
result*=m;
}
return result;
}
//ÏÔʾÊý×Ö
//x,y :Æðµã×ø±ê
//len :Êý×ÖµÄλÊý
//size:×ÖÌå´óС
//mode:0,·´É«ÏÔʾ;1,Õý³£ÏÔʾ
void OLED_ShowNum(u8 x,u8 y,u32 num,u8 len,u8 size1,u8 mode)
{
u8 t,temp,m=0;
if(size1==8)m=2;
for(t=0;t<len;t++)
{
temp=(num/OLED_Pow(10,len-t-1))%10;
if(temp==0)
{
OLED_ShowChar(x+(size1/2+m)*t,y,'0',size1,mode);
}
else
{
OLED_ShowChar(x+(size1/2+m)*t,y,temp+'0',size1,mode);
}
}
}
//ÏÔʾºº×Ö
//x,y:Æðµã×ø±ê
//num:ºº×Ö¶ÔÓ¦µÄÐòºÅ
//mode:0,·´É«ÏÔʾ;1,Õý³£ÏÔʾ
void OLED_ShowChinese(u8 x,u8 y,u8 num,u8 size1,u8 mode)
{
u8 m,temp;
u8 x0=x,y0=y;
u16 i,size3=(size1/8+((size1%8)?1:0))*size1; //µÃµ½×ÖÌåÒ»¸ö×Ö·û¶ÔÓ¦µãÕó¼¯ËùÕ¼µÄ×Ö½ÚÊý
for(i=0;i<size3;i++)
{
if(size1==16)
{temp=Hzk1[num][i];}//µ÷ÓÃ16*16×ÖÌå
else if(size1==24)
{temp=Hzk2[num][i];}//µ÷ÓÃ24*24×ÖÌå
else if(size1==32)
{temp=Hzk3[num][i];}//µ÷ÓÃ32*32×ÖÌå
else if(size1==64)
{temp=Hzk4[num][i];}//µ÷ÓÃ64*64×ÖÌå
else return;
for(m=0;m<8;m++)
{
if(temp&0x01)OLED_DrawPoint(x,y,mode);
else OLED_DrawPoint(x,y,!mode);
temp>>=1;
y++;
}
x++;
if((x-x0)==size1)
{x=x0;y0=y0+8;}
y=y0;
}
}
//num ÏÔʾºº×ֵĸöÊý
//space ÿһ±éÏÔʾµÄ¼ä¸ô
//mode:0,·´É«ÏÔʾ;1,Õý³£ÏÔʾ
void OLED_ScrollDisplay(u8 num,u8 space,u8 mode)
{
u8 i,n,t=0,m=0,r;
while(1)
{
if(m==0)
{
OLED_ShowChinese(128,8,t,16,mode); //дÈëÒ»¸öºº×Ö±£´æÔÚOLED_GRAM[][]Êý×éÖÐ
t++;
}
if(t==num)
{
for(r=0;r<16*space;r++) //ÏÔʾ¼ä¸ô
{
for(i=1;i<144;i++)
{
for(n=0;n<4;n++)
{
OLED_GRAM[i-1][n]=OLED_GRAM[i][n];
}
}
OLED_Refresh();
}
t=0;
}
m++;
if(m==16){m=0;}
for(i=1;i<144;i++) //ʵÏÖ×óÒÆ
{
for(n=0;n<4;n++)
{
OLED_GRAM[i-1][n]=OLED_GRAM[i][n];
}
}
OLED_Refresh();
}
}
//x,y£ºÆðµã×ø±ê
//sizex,sizey,ͼƬ³¤¿í
//BMP[]£ºÒªÐ´ÈëµÄͼƬÊý×é
//mode:0,·´É«ÏÔʾ;1,Õý³£ÏÔʾ
void OLED_ShowPicture(u8 x,u8 y,u8 sizex,u8 sizey,u8 BMP[],u8 mode)
{
u16 j=0;
u8 i,n,temp,m;
u8 x0=x,y0=y;
sizey=sizey/8+((sizey%8)?1:0);
for(n=0;n<sizey;n++)
{
for(i=0;i<sizex;i++)
{
temp=BMP[j];
j++;
for(m=0;m<8;m++)
{
if(temp&0x01)OLED_DrawPoint(x,y,mode);
else OLED_DrawPoint(x,y,!mode);
temp>>=1;
y++;
}
x++;
if((x-x0)==sizex)
{
x=x0;
y0=y0+8;
}
y=y0;
}
}
}
//OLEDµÄ³õʼ»¯
void OLED_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOG|RCC_APB2Periph_GPIOD, ENABLE); //ʹÄÜA¶Ë¿ÚʱÖÓ
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD; //ÍÆÍìÊä³ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//ËÙ¶È50MHz
GPIO_Init(GPIOG, &GPIO_InitStructure); //³õʼ»¯GPIOG12
GPIO_SetBits(GPIOG,GPIO_Pin_12);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD; //ÍÆÍìÊä³ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//ËÙ¶È50MHz
GPIO_Init(GPIOD, &GPIO_InitStructure); //³õʼ»¯GPIOD1,5,15
GPIO_SetBits(GPIOD,GPIO_Pin_5);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //ÍÆÍìÊä³ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//ËÙ¶È50MHz
GPIO_Init(GPIOD, &GPIO_InitStructure); //³õʼ»¯GPIOD1,5,15
GPIO_SetBits(GPIOD,GPIO_Pin_4);
OLED_RES_Clr();
delay_ms(200);
OLED_RES_Set();
OLED_WR_Byte(0xAE,OLED_CMD); /*display off*/
OLED_WR_Byte(0x00,OLED_CMD); /*set lower column address*/
OLED_WR_Byte(0x10,OLED_CMD); /*set higher column address*/
OLED_WR_Byte(0x00,OLED_CMD); /*set display start line*/
OLED_WR_Byte(0xB0,OLED_CMD); /*set page address*/
OLED_WR_Byte(0x81,OLED_CMD); /*contract control*/
OLED_WR_Byte(0xff,OLED_CMD); /*128*/
OLED_WR_Byte(0xA1,OLED_CMD); /*set segment remap*/
OLED_WR_Byte(0xA6,OLED_CMD); /*normal / reverse*/
OLED_WR_Byte(0xA8,OLED_CMD); /*multiplex ratio*/
OLED_WR_Byte(0x1F,OLED_CMD); /*duty = 1/32*/
OLED_WR_Byte(0xC8,OLED_CMD); /*Com scan direction*/
OLED_WR_Byte(0xD3,OLED_CMD); /*set display offset*/
OLED_WR_Byte(0x00,OLED_CMD);
OLED_WR_Byte(0xD5,OLED_CMD); /*set osc division*/
OLED_WR_Byte(0x80,OLED_CMD);
OLED_WR_Byte(0xD9,OLED_CMD); /*set pre-charge period*/
OLED_WR_Byte(0x1f,OLED_CMD);
OLED_WR_Byte(0xDA,OLED_CMD); /*set COM pins*/
OLED_WR_Byte(0x00,OLED_CMD);
OLED_WR_Byte(0xdb,OLED_CMD); /*set vcomh*/
OLED_WR_Byte(0x40,OLED_CMD);
OLED_WR_Byte(0x8d,OLED_CMD); /*set charge pump enable*/
OLED_WR_Byte(0x14,OLED_CMD);
OLED_Clear();
OLED_WR_Byte(0xAF,OLED_CMD); /*display ON*/
}
oled.h
#ifndef __OLED_H
#define __OLED_H
#include "sys.h"
#include "stdlib.h"
//-----------------OLED¶Ë¿Ú¶¨Òå----------------
#define OLED_SCL_Clr() GPIO_ResetBits(GPIOG,GPIO_Pin_12)//SCL
#define OLED_SCL_Set() GPIO_SetBits(GPIOG,GPIO_Pin_12)
#define OLED_SDA_Clr() GPIO_ResetBits(GPIOD,GPIO_Pin_5)//DIN
#define OLED_SDA_Set() GPIO_SetBits(GPIOD,GPIO_Pin_5)
#define OLED_RES_Clr() GPIO_ResetBits(GPIOD,GPIO_Pin_4)//RES
#define OLED_RES_Set() GPIO_SetBits(GPIOD,GPIO_Pin_4)
#define OLED_CMD 0 //дÃüÁî
#define OLED_DATA 1 //дÊý¾Ý
void OLED_ClearPoint(u8 x,u8 y);
void OLED_ColorTurn(u8 i);
void OLED_DisplayTurn(u8 i);
void I2C_Start(void);
void I2C_Stop(void);
void I2C_WaitAck(void);
void Send_Byte(u8 dat);
void OLED_WR_Byte(u8 dat,u8 mode);
void OLED_DisPlay_On(void);
void OLED_DisPlay_Off(void);
void OLED_Refresh(void);
void OLED_Clear(void);
void OLED_DrawPoint(u8 x,u8 y,u8 t);
void OLED_DrawLine(u8 x1,u8 y1,u8 x2,u8 y2,u8 mode);
void OLED_DrawCircle(u8 x,u8 y,u8 r);
void OLED_ShowChar(u8 x,u8 y,u8 chr,u8 size1,u8 mode);
void OLED_ShowChar6x8(u8 x,u8 y,u8 chr,u8 mode);
void OLED_ShowString(u8 x,u8 y,u8 *chr,u8 size1,u8 mode);
void OLED_ShowNum(u8 x,u8 y,u32 num,u8 len,u8 size1,u8 mode);
void OLED_ShowChinese(u8 x,u8 y,u8 num,u8 size1,u8 mode);
void OLED_ScrollDisplay(u8 num,u8 space,u8 mode);
void OLED_ShowPicture(u8 x,u8 y,u8 sizex,u8 sizey,u8 BMP[],u8 mode);
void OLED_Init(void);
#endif
2.EXIT
exit.c
#include "exti.h"
#include "led.h"
#include "key.h"
#include "delay.h"
#include "usart.h"
#include "beep.h"
#include "dht11.h"
#include "MOTOR.h"
#include "oled.h"
void EXTIX_Init(void)
{
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
KEY_Init(); // °´¼ü¶Ë¿Ú³õʼ»¯
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); //ʹÄܸ´Óù¦ÄÜʱÖÓ
//GPIOE.2 ÖжÏÏßÒÔ¼°Öжϳõʼ»¯ÅäÖà ϽµÑØ´¥·¢
GPIO_EXTILineConfig(GPIO_PortSourceGPIOE,GPIO_PinSource2);
EXTI_InitStructure.EXTI_Line=EXTI_Line2; //KEY2
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure); //¸ù¾ÝEXTI_InitStructÖÐÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèEXTI¼Ä´æÆ÷
//GPIOE.3 ÖжÏÏßÒÔ¼°Öжϳõʼ»¯ÅäÖà ϽµÑØ´¥·¢ //KEY1
GPIO_EXTILineConfig(GPIO_PortSourceGPIOE,GPIO_PinSource3);
EXTI_InitStructure.EXTI_Line=EXTI_Line3;
EXTI_Init(&EXTI_InitStructure); //¸ù¾ÝEXTI_InitStructÖÐÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèEXTI¼Ä´æÆ÷
//GPIOE.4 ÖжÏÏßÒÔ¼°Öжϳõʼ»¯ÅäÖà ϽµÑØ´¥·¢ //KEY0
GPIO_EXTILineConfig(GPIO_PortSourceGPIOE,GPIO_PinSource4);
EXTI_InitStructure.EXTI_Line=EXTI_Line4;
EXTI_Init(&EXTI_InitStructure); //¸ù¾ÝEXTI_InitStructÖÐÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèEXTI¼Ä´æÆ÷
//GPIOA.0 ÖжÏÏßÒÔ¼°Öжϳõʼ»¯ÅäÖà ÉÏÉýÑØ´¥·¢ PA0 WK_UP
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource0);
EXTI_InitStructure.EXTI_Line=EXTI_Line0;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
EXTI_Init(&EXTI_InitStructure); //¸ù¾ÝEXTI_InitStructÖÐÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèEXTI¼Ä´æÆ÷
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn; //ʹÄÜ°´¼üWK_UPËùÔÚµÄÍⲿÖжÏͨµÀ
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x02; //ÇÀÕ¼ÓÅÏȼ¶2£¬
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x03; //×ÓÓÅÏȼ¶3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //ʹÄÜÍⲿÖжÏͨµÀ
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = EXTI2_IRQn; //ʹÄÜ°´¼üKEY2ËùÔÚµÄÍⲿÖжÏͨµÀ
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x02; //ÇÀÕ¼ÓÅÏȼ¶2£¬
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x02; //×ÓÓÅÏȼ¶2
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //ʹÄÜÍⲿÖжÏͨµÀ
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = EXTI3_IRQn; //ʹÄÜ°´¼üKEY1ËùÔÚµÄÍⲿÖжÏͨµÀ
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x02; //ÇÀÕ¼ÓÅÏȼ¶2
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x01; //×ÓÓÅÏȼ¶1
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //ʹÄÜÍⲿÖжÏͨµÀ
NVIC_Init(&NVIC_InitStructure); //¸ù¾ÝNVIC_InitStructÖÐÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèNVIC¼Ä´æÆ÷
NVIC_InitStructure.NVIC_IRQChannel = EXTI4_IRQn; //ʹÄÜ°´¼üKEY0ËùÔÚµÄÍⲿÖжÏͨµÀ
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x02; //ÇÀÕ¼ÓÅÏȼ¶2
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00; //×ÓÓÅÏȼ¶0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //ʹÄÜÍⲿÖжÏͨµÀ
NVIC_Init(&NVIC_InitStructure); //¸ù¾ÝNVIC_InitStructÖÐÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèNVIC¼Ä´æÆ÷
}
u8 T_max = 30;
u8 h_max=80;
u8 T_num[12],H_num[12],temp_num[12],humi_num[12];
u8 temp,humi;
//ÍⲿÖжÏ0·þÎñ³ÌÐò
void EXTI0_IRQHandler(void)
{
delay_ms(10);//Ïû¶¶
if(WK_UP==1) //WK_UP°´¼ü
{
// BEEP=!BEEP;
T_max=T_max+1;
}
EXTI_ClearITPendingBit(EXTI_Line0); //Çå³ýLINE0ÉϵÄÖжϱê־λ
}
//ÍⲿÖжÏ2·þÎñ³ÌÐò
void EXTI2_IRQHandler(void)
{
delay_ms(10);//Ïû¶¶
if(KEY2==0) //°´¼üKEY2
{
// LED0=!LED0;
T_max=T_max-1;
}
EXTI_ClearITPendingBit(EXTI_Line2); //Çå³ýLINE2ÉϵÄÖжϱê־λ
}
//ÍⲿÖжÏ3·þÎñ³ÌÐò
void EXTI3_IRQHandler(void)
{
delay_ms(10);//Ïû¶¶
if(KEY1==0) //°´¼üKEY1
{
// LED1=!LED1;
h_max=h_max+1;
}
EXTI_ClearITPendingBit(EXTI_Line3); //Çå³ýLINE3ÉϵÄÖжϱê־λ
}
void EXTI4_IRQHandler(void)
{
delay_ms(10);//Ïû¶¶
if(KEY0==0) //°´¼üKEY0
{
// LED0=!LED0;
// LED1=!LED1;
h_max=h_max-1;
}
EXTI_ClearITPendingBit(EXTI_Line4); //Çå³ýLINE4ÉϵÄÖжϱê־λ
}
void EXIT(void)
{
// u8 temp,humi;
if(temp>=T_max) //ζÈãÐÖµ
{
BEEP=1; //¿ª·äÃùÆ÷
Motor_ZHENG();
TIM_SetCompare2(TIM3,90);
}
else
{
BEEP=0;
Motor_OFF();
}
if(humi>=h_max)
{
LED1=0;
}
else
{
LED1=1;
}
}
void config(void)
{
sprintf((char*)T_num,"%d ",T_max);
sprintf((char*)H_num,"%d ",h_max);
OLED_ShowString(60,16,"T:",16,1);
OLED_ShowString(60,32,"H:",16,1);
OLED_ShowString(86,16,T_num,16,1);
OLED_ShowString(86,32,H_num,16,1);
}
void dht(void)
{
DHT11_Read_Data(&temp,&humi);
sprintf((char*)temp_num,"%d ",temp);
sprintf((char*)humi_num,"%d ",humi);
delay_ms(50);
OLED_Refresh();
OLED_ShowString(0,16,"t:",16,1);
OLED_ShowString(1,32,"h:",16,1);
OLED_ShowString(32,16,temp_num,16,1);
OLED_ShowString(32,32,humi_num,16,1);
}
exti.h
#ifndef __EXTI_H
#define __EXIT_H
#include "sys.h"
void EXTIX_Init(void);//ÍⲿÖжϳõʼ»¯
void EXIT(void);
#endif
3.LED
led.c
#include "led.h"
void LED_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOE, ENABLE); //ʹÄÜPB,PE¶Ë¿ÚʱÖÓ
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; //LED0-->PB.5 ¶Ë¿ÚÅäÖÃ
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //ÍÆÍìÊä³ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO¿ÚËÙ¶ÈΪ50MHz
GPIO_Init(GPIOB, &GPIO_InitStructure); //¸ù¾ÝÉ趨²ÎÊý³õʼ»¯GPIOB.5
GPIO_SetBits(GPIOB,GPIO_Pin_5); //PB.5 Êä³ö¸ß
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; //LED1-->PE.5 ¶Ë¿ÚÅäÖÃ, ÍÆÍìÊä³ö
GPIO_Init(GPIOE, &GPIO_InitStructure); //ÍÆÍìÊä³ö £¬IO¿ÚËÙ¶ÈΪ50MHz
GPIO_SetBits(GPIOE,GPIO_Pin_5); //PE.5 Êä³ö¸ß
}
led.h
#ifndef __LED_H
#define __LED_H
#include "sys.h"
#define LED0 PBout(5)// PB5
#define LED1 PEout(5)// PE5
void LED_Init(void);//³õʼ»¯
#endif