11.20号

Java
字符串的StringBuffer类型:处理频繁改变的字符串变量
如果频繁改变字符串变量,它会新开劈空间,再扔掉原本的空间,非常的浪费,和占用加载时间,但是StringBuffer不会,它会一直在一个空间里

//1.声明赋值的格式
StringBuffer con=new StringBuffer(“内容”);
试验:

public class day16_07 {
public static void main(String[]args){
StringBuffer con=new StringBuffer(“hello”); /写一个字符串Buff类,命名为con,值是hello
System.out.println(con); /打印输出
}

//2.添加字符串的方法:append() 不会开辟新的空间
用法:StringBuffer方法.append(“字符串”);
字符串会添加在后面
试验:

public class day16_07 {
public static void main(String[]args){
StringBuffer con=new StringBuffer(“hello”); /写一个字符串Buff类,命名为con,值是hello
con.append(“java”); /添加java
System.out.println(con); /打印输出
}

插入内容的方法:insert() 不会开辟新的空间
用法:StringBuffer方法.insert(插入的索引值,插入的内容);
试验:

public class day16_07 {
public static void main(String[]args){
StringBuffer con=new StringBuffer(“hellojava”);
con.insert(1,"…"); /在下标1,插入"…"
System.out.println(con); /打印输出
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是基于STM32 HAL库的DHT11驱动代码示例: dht11.h ```c #ifndef __DHT11_H #define __DHT11_H #include "stm32f1xx_hal.h" #define DHT11_PORT GPIOA #define DHT11_PIN GPIO_PIN_0 void delay_us(uint32_t us); void DHT11_Start(void); uint8_t DHT11_Check(void); uint8_t DHT11_Read_Bit(void); uint8_t DHT11_Read_Byte(void); uint8_t DHT11_Read_Data(uint8_t *humidity, uint8_t *temperature); #endif ``` dht11.c ```c #include "dht11.h" void delay_us(uint32_t us) { uint32_t count = us * (SystemCoreClock / 1000000) / 5; while(count--); } void DHT11_Start(void) { HAL_GPIO_WritePin(DHT11_PORT, DHT11_PIN, GPIO_PIN_RESET); delay_us(18000); HAL_GPIO_WritePin(DHT11_PORT, DHT11_PIN, GPIO_PIN_SET); delay_us(20); HAL_GPIO_WritePin(DHT11_PORT, DHT11_PIN, GPIO_PIN_RESET); } uint8_t DHT11_Check(void) { uint8_t response = 1; uint16_t count = 0; while(HAL_GPIO_ReadPin(DHT11_PORT, DHT11_PIN) == GPIO_PIN_SET) { count++; if(count > 10000) { response = 0; break; } } return response; } uint8_t DHT11_Read_Bit(void) { uint8_t response = 1; uint16_t count = 0; while(HAL_GPIO_ReadPin(DHT11_PORT, DHT11_PIN) == GPIO_PIN_RESET) { count++; if(count > 10000) { response = 0; break; } } delay_us(40); if(HAL_GPIO_ReadPin(DHT11_PORT, DHT11_PIN) == GPIO_PIN_SET) { response = 1; } else { response = 0; } count = 0; while(HAL_GPIO_ReadPin(DHT11_PORT, DHT11_PIN) == GPIO_PIN_SET) { count++; if(count > 10000) { response = 0; break; } } return response; } uint8_t DHT11_Read_Byte(void) { uint8_t i, temp = 0; for(i = 0; i < 8; i++) { temp |= (DHT11_Read_Bit() << (7 - i)); } return temp; } uint8_t DHT11_Read_Data(uint8_t *humidity, uint8_t *temperature) { uint8_t response = 0; uint8_t buffer[5]; DHT11_Start(); if(DHT11_Check() == 0) { response = 1; buffer[0] = DHT11_Read_Byte(); buffer[1] = DHT11_Read_Byte(); buffer[2] = DHT11_Read_Byte(); buffer[3] = DHT11_Read_Byte(); buffer[4] = DHT11_Read_Byte(); if(buffer[4] == (buffer[0] + buffer[1] + buffer[2] + buffer[3])) { *humidity = buffer[0]; *temperature = buffer[2]; } else { response = 2; } } return response; } ``` 使用方法: 1. 将以上代码保存为`dht11.h`和`dht11.c`文件,并将其添加到工程中。 2. 在需要使用DHT11传感器的文件中添加以下代码: ```c #include "dht11.h" uint8_t humidity, temperature; int main(void) { HAL_Init(); /* 初始化GPIO */ GPIO_InitTypeDef GPIO_InitStruct; __HAL_RCC_GPIOA_CLK_ENABLE(); GPIO_InitStruct.Pin = DHT11_PIN; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; HAL_GPIO_Init(DHT11_PORT, &GPIO_InitStruct); HAL_GPIO_WritePin(DHT11_PORT, DHT11_PIN, GPIO_PIN_RESET); /* 读取温湿度数据 */ uint8_t response = DHT11_Read_Data(&humidity, &temperature); if(response == 0) { /* 温湿度数据读取成功 */ printf("湿度:%d%%,温度:%d℃\n", humidity, temperature); } else if(response == 1) { /* DHT11无响应 */ printf("DHT11无响应\n"); } else if(response == 2) { /* 校验和错误 */ printf("校验和错误\n"); } while(1); } ``` 注意:需要根据实际的引脚和端口进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值