编写DHT11和OLED驱动代码
时间: 2023-08-05 14:04:53 浏览: 77
以下是DHT11和OLED驱动代码的示例:
DHT11驱动代码:
```c
#include <msp430.h>
#define DHT11_PIN BIT4
void DHT11_Start(void)
{
P4DIR |= DHT11_PIN; //设置P4.4为输出
P4OUT &= ~DHT11_PIN; //使P4.4输出低电平
__delay_cycles(18000); //延时18ms
P4OUT |= DHT11_PIN; //使P4.4输出高电平
__delay_cycles(40); //延时40us
P4DIR &= ~DHT11_PIN; //设置P4.4为输入
}
int DHT11_Check_Response(void)
{
int response = 0;
__delay_cycles(40); //延时40us
if(!(P4IN & DHT11_PIN)) //判断是否为低电平响应
{
__delay_cycles(80); //延时80us
if(P4IN & DHT11_PIN) //判断是否为高电平响应
{
response = 1;
}
}
while(P4IN & DHT11_PIN); //等待低电平结束
return response;
}
void DHT11_Read_Data(int *data, int pin)
{
int i, j = 0;
for(i=0; i<5; i++)
{
data[i] = 0;
}
DHT11_Start(); //发送开始信号
if(DHT11_Check_Response()) //等待响应
{
for(i=0; i<40; i++)
{
while(!(P4IN & DHT11_PIN)); //等待高电平开始位
__delay_cycles(30); //延时30us
if(P4IN & DHT11_PIN) //判断是否为高电平数据位
{
j |= (1<<(7-(i%8))); //将数据存入j中
}
if(i%8 == 7)
{
data[i/8] = j;
j = 0;
}
}
}
}
```
OLED驱动代码:
```c
#include <msp430.h>
#define OLED_SCL BIT7
#define OLED_SDA BIT6
#define OLED_RST BIT5
#define OLED_DC BIT4
void OLED_Write_Command(unsigned char command)
{
UCB1IFG &= ~UCRXIFG; //清除接收标志位
UCB1TXBUF = command; //写入命令
while(!(UCB1IFG & UCTXIFG)); //等待发送完成
}
void OLED_Write_Data(unsigned char data)
{
UCB1IFG &= ~UCRXIFG; //清除接收标志位
UCB1TXBUF = data; //写入数据
while(!(UCB1IFG & UCTXIFG)); //等待发送完成
}
void OLED_Init(void)
{
P4DIR |= OLED_RST; //设置P4.5为输出
P4OUT &= ~OLED_RST; //使P4.5输出低电平
__delay_cycles(20000); //延时20ms
P4OUT |= OLED_RST; //使P4.5输出高电平
__delay_cycles(20000); //延时20ms
UCB1CTLW0 |= UCSWRST; //软件复位UCB1模块
UCB1CTLW0 |= UCMST + UCMODE_3 + UCSYNC; //主模式,3线SPI,同步通信
UCB1CTLW0 |= UCSSEL_2; //时钟源选择SMCLK
UCB1BRW = 0x02; //时钟分频为SMCLK/2
UCB1CTLW0 &= ~UCSWRST; //释放UCB1模块
P4DIR |= OLED_SCL + OLED_SDA + OLED_DC; //设置OLED引脚为输出
OLED_Write_Command(0xAE); //关闭OLED显示
OLED_Write_Command(0xD5); //设置时钟分频因子
OLED_Write_Command(0x80); //时钟分频因子为80h
OLED_Write_Command(0xA8); //设置显示偏移
OLED_Write_Command(0x3F); //偏移量为3Fh
OLED_Write_Command(0xD3); //设置显示开始行
OLED_Write_Command(0x00); //开始行为0
OLED_Write_Command(0x40); //设置显示位置
OLED_Write_Command(0x8D); //设置电荷泵
OLED_Write_Command(0x14); //开启电荷泵
OLED_Write_Command(0x20); //设置内存地址模式
OLED_Write_Command(0x02); //地址模式为页寻址模式
OLED_Write_Command(0xA1); //设置段重定向
OLED_Write_Command(0xC8); //设置COM扫描方向
OLED_Write_Command(0xDA); //设置COM硬件引脚配置
OLED_Write_Command(0x12); //设置COM引脚配置
OLED_Write_Command(0x81); //设置对比度控制
OLED_Write_Command(0xCF); //对比度为CFh
OLED_Write_Command(0xD9); //设置预充电周期
OLED_Write_Command(0xF1); //预充电周期为F1h
OLED_Write_Command(0xDB); //设置VCOMH
OLED_Write_Command(0x40); //VCOMH为40h
OLED_Write_Command(0xA4); //关闭全局显示开启
OLED_Write_Command(0xA6); //设置正常显示模式
OLED_Write_Command(0xAF); //开启OLED显示
}
void OLED_Set_Pos(unsigned char x, unsigned char y)
{
OLED_Write_Command(0xB0 + y); //设置页地址(0~7)
OLED_Write_Command(((x & 0xf0) >> 4) | 0x10); //设置高位列地址
OLED_Write_Command((x & 0x0f) | 0x01); //设置低位列地址
}
void OLED_ShowString(unsigned char x, unsigned char y, char *str)
{
unsigned char c = 0;
while(str[c] != '\0')
{
OLED_ShowChar(x, y, str[c]);
x += 8;
c++;
}
}
void OLED_ShowChar(unsigned char x, unsigned char y, unsigned char chr)
{
unsigned char c = 0, i = 0;
c = chr - 32;
if(x > 120)
{
x = 0;
y++;
}
OLED_Set_Pos(x, y);
for(i=0; i<8; i++)
{
OLED_Write_Data(OLED_ASCII[c][i]);
}
}
```
注:这里的OLED_ASCII数组为OLED显示的ASCII码对应点阵,需要根据具体OLED屏幕型号进行调整。
阅读全文