功能
msp430g2553,读取SHT31,读取gy-30,显示到lcd12864
硬件
/*
- 12864液晶串行显示测试程序
- P1.4模拟SID(接第5脚),
- P1.5模拟SCLK(接第6脚)
- 4脚(CS信号)接高电平选通, 接P2.0 !!!
- 15脚(PSB信号)接地选择串行方式
- 17脚(RST信号)根据注释提示悬空
- 以上四根线
- 另外还需要接四根线VCC GND BLA BLK
- 边上的是GND或者BLK 都接GND
- 第二个就是VCC或者BLA 都接5V
- MSP430是3.3的单片机 但是不会烧
- 并不是所有的12864都支持串口,程序不行就考虑是不是买到次品了
*/
//SHT31
//SCL–P1.2
//SDA–P1.1
//VCC–3.3V
//GND–GND
//ADDR–GND
//gy-30
//SCL–P2.1
//SDA–P2.2
//VCC–3.3V
//GND–GND
//ADDR–GND
部分代码
int lx_int;
float lx;int main(void)
{u8 BUFt[6] = { 0 };u16 temp = 0, humi = 0;unsigned char dis[10];WDTCTL = WDTPW + WDTHOLD; /* Stop WDT */if (CALBC1_1MHZ == 0xFF) // If calibration constant erased{while (1); // do not load, trap CPU!!}DCOCTL = 0; // Select lowest DCOx and MODx settingsBCSCTL1 = CALBC1_1MHZ; // Set rangeDCOCTL = CALDCO_1MHZ; // Set DCO step + modulation */lcdinit(); /* LCD初始化 */P2DIR = 0XFF;P2OUT = 0XFF;Lcd_WriteStr(0, 0, "温度");Lcd_WriteStr(0, 1, "湿度");Lcd_WriteStr(0, 2, "光强");IIC_Init();while (1){IIC_Start();IIC_Send_Byte(SHT30W); //发送设备地址+写信号while (IIC_Wait_Ack()); //从机应答IIC_Send_Byte(0x2C); //命令高8位,0x2220,周期测量while (IIC_Wait_Ack());IIC_Send_Byte(0x06); //命令高8位,0x2220,周期测量while (IIC_Wait_Ack());delay_ms(2);IIC_Start();IIC_Send_Byte(SHT30R); //发送设备地址+写信号while (IIC_Wait_Ack()); //从机应答BUFt[0] = IIC_Read_Byte(1); //温度高BUFt[1] = IIC_Read_Byte(1); //温度低BUFt[2] = IIC_Read_Byte(1); //温度crcBUFt[3] = IIC_Read_Byte(1); //湿度高BUFt[4] = IIC_Read_Byte(1); //湿度低BUFt[5] = IIC_Read_Byte(0); //湿度crcIIC_Stop();temp = (BUFt[0] << 8) | BUFt[1];humi = (BUFt[3] << 8) | BUFt[4];temp = (u16) (175 * (float) temp / 65535 - 45);humi = (u16) (100 * (float) humi / 65535);delay_ms(10);dis[0] = temp % 100 / 10 + '0';dis[1] = temp % 10 + '0';dis[2] = 'C';dis[3] = 0;Lcd_WriteStr(2, 0, dis);dis[0] = humi % 100 / 10 + '0';dis[1] = humi % 10 + '0';dis[2] = 'R';dis[3] = 0;Lcd_WriteStr(2, 1, dis);lx_int = (int) get_lightpower();dis[0] = lx_int / 10000 + '0';dis[1] = lx_int % 10000 / 1000 + '0';dis[2] = lx_int % 1000 / 100 + '0';dis[3] = lx_int % 100 / 10 + '0';dis[4] = lx_int % 10 + '0';dis[5] = 0;Lcd_WriteStr(2, 2, dis);delay_ms(200);}
}
全部代码
https://docs.qq.com/sheet/DUEdqZ2lmbmR6UVdU?tab=BB08J2