..\APP\DS18B20.C(88): error C267: 'ds18b20_write_byte': requires ANSI-style prototype
时间: 2024-03-23 22:38:40 浏览: 15
这个错误是由于函数ds18b20_write_byte的原型没有被正确声明所导致的。ANSI C标准要求函数的原型必须在函数调用之前被声明。所以,你需要在你的代码中添加一个函数原型声明,或者将函数定义放在函数调用之前。
例如,你可以在代码的开头添加以下原型声明:
```c
void ds18b20_write_byte(unsigned char byte);
```
这个声明告诉编译器ds18b20_write_byte函数接受一个unsigned char参数,并且没有返回值。这样,当编译器在代码中遇到函数调用时,就会知道该函数的参数类型和返回值类型,从而消除编译错误。
相关问题
用stm32f10x.h去写ds18b20.h和ds18b20.c的程序
好的,我可以给你提供一份基本的代码框架,你可以根据自己的需求进行修改和完善。这里假设你使用的是STM32F103C8T6开发板。
首先,需要在ds18b20.h文件中定义一些常量和函数原型:
```c
#ifndef __DS18B20_H__
#define __DS18B20_H__
#include "stm32f10x.h"
#define DS18B20_PORT GPIOA
#define DS18B20_PIN GPIO_Pin_0
void ds18b20_init(void);
void ds18b20_reset(void);
void ds18b20_write_bit(uint8_t bit);
uint8_t ds18b20_read_bit(void);
void ds18b20_write_byte(uint8_t byte);
uint8_t ds18b20_read_byte(void);
float ds18b20_get_temp(void);
#endif /* __DS18B20_H__ */
```
然后,在ds18b20.c文件中实现这些函数:
```c
#include "ds18b20.h"
void ds18b20_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = DS18B20_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(DS18B20_PORT, &GPIO_InitStructure);
}
void ds18b20_reset(void)
{
GPIO_ResetBits(DS18B20_PORT, DS18B20_PIN);
delay_us(500);
GPIO_SetBits(DS18B20_PORT, DS18B20_PIN);
delay_us(70);
while (GPIO_ReadInputDataBit(DS18B20_PORT, DS18B20_PIN));
delay_us(500);
}
void ds18b20_write_bit(uint8_t bit)
{
GPIO_ResetBits(DS18B20_PORT, DS18B20_PIN);
delay_us(5);
if (bit) {
GPIO_SetBits(DS18B20_PORT, DS18B20_PIN);
}
delay_us(60);
GPIO_SetBits(DS18B20_PORT, DS18B20_PIN);
}
uint8_t ds18b20_read_bit(void)
{
uint8_t bit = 0;
GPIO_ResetBits(DS18B20_PORT, DS18B20_PIN);
delay_us(2);
GPIO_SetBits(DS18B20_PORT, DS18B20_PIN);
delay_us(15);
bit = GPIO_ReadInputDataBit(DS18B20_PORT, DS18B20_PIN);
delay_us(45);
return bit;
}
void ds18b20_write_byte(uint8_t byte)
{
uint8_t i = 0;
for (i = 0; i < 8; i++) {
ds18b20_write_bit(byte & (1 << i));
}
}
uint8_t ds18b20_read_byte(void)
{
uint8_t byte = 0;
uint8_t i = 0;
for (i = 0; i < 8; i++) {
byte |= (ds18b20_read_bit() << i);
}
return byte;
}
float ds18b20_get_temp(void)
{
uint8_t temp_l = 0;
uint8_t temp_h = 0;
float temp = 0;
ds18b20_reset();
ds18b20_write_byte(0xcc);
ds18b20_write_byte(0x44);
delay_ms(750);
ds18b20_reset();
ds18b20_write_byte(0xcc);
ds18b20_write_byte(0xbe);
temp_l = ds18b20_read_byte();
temp_h = ds18b20_read_byte();
temp = (temp_h << 8) | temp_l;
temp /= 16;
return temp;
}
```
其中,delay_us和delay_ms函数需要自己实现或者引入外部库中的实现。
最后,在主函数中调用ds18b20_get_temp函数即可读取温度值。
stm32f10x.h编写ds18b20.c和ds18b20.h
以下是一个简单的DS18B20读取温度值的代码示例,其中使用了STM32F10x标准库函数。需要注意的是,这里使用了单总线模式,即将DS18B20的数据线与STM32的GPIO口相连,没有使用额外的硬件电路。
ds18b20.h文件:
```c
#ifndef __DS18B20_H
#define __DS18B20_H
#include "stm32f10x.h"
#define DS18B20_GPIO_PORT GPIOA
#define DS18B20_GPIO_PIN GPIO_Pin_0
float DS18B20_GetTemperature(void);
#endif /* __DS18B20_H */
```
ds18b20.c文件:
```c
#include "ds18b20.h"
/* DS18B20复位 */
static void DS18B20_Reset(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = DS18B20_GPIO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(DS18B20_GPIO_PORT, &GPIO_InitStructure);
GPIO_ResetBits(DS18B20_GPIO_PORT, DS18B20_GPIO_PIN);
delay_us(500);
GPIO_SetBits(DS18B20_GPIO_PORT, DS18B20_GPIO_PIN);
delay_us(80);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(DS18B20_GPIO_PORT, &GPIO_InitStructure);
}
/* 发送字节数据 */
static void DS18B20_WriteByte(uint8_t data)
{
uint8_t i = 0;
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = DS18B20_GPIO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(DS18B20_GPIO_PORT, &GPIO_InitStructure);
for (i = 0; i < 8; i++)
{
if ((data >> i) & 0x01)
{
GPIO_ResetBits(DS18B20_GPIO_PORT, DS18B20_GPIO_PIN);
delay_us(1);
GPIO_SetBits(DS18B20_GPIO_PORT, DS18B20_GPIO_PIN);
delay_us(80);
}
else
{
GPIO_ResetBits(DS18B20_GPIO_PORT, DS18B20_GPIO_PIN);
delay_us(80);
GPIO_SetBits(DS18B20_GPIO_PORT, DS18B20_GPIO_PIN);
delay_us(1);
}
}
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(DS18B20_GPIO_PORT, &GPIO_InitStructure);
}
/* 读取字节数据 */
static uint8_t DS18B20_ReadByte(void)
{
uint8_t i = 0, data = 0;
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = DS18B20_GPIO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(DS18B20_GPIO_PORT, &GPIO_InitStructure);
for (i = 0; i < 8; i++)
{
GPIO_ResetBits(DS18B20_GPIO_PORT, DS18B20_GPIO_PIN);
delay_us(4);
GPIO_SetBits(DS18B20_GPIO_PORT, DS18B20_GPIO_PIN);
delay_us(10);
if (GPIO_ReadInputDataBit(DS18B20_GPIO_PORT, DS18B20_GPIO_PIN))
{
data |= 0x01 << i;
}
delay_us(50);
}
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(DS18B20_GPIO_PORT, &GPIO_InitStructure);
return data;
}
/* 获取温度值 */
float DS18B20_GetTemperature(void)
{
uint8_t temp_l = 0, temp_h = 0;
float temperature = 0.0;
DS18B20_Reset();
DS18B20_WriteByte(0xCC);
DS18B20_WriteByte(0x44);
delay_ms(800);
DS18B20_Reset();
DS18B20_WriteByte(0xCC);
DS18B20_WriteByte(0xBE);
temp_l = DS18B20_ReadByte();
temp_h = DS18B20_ReadByte();
temperature = (float)((int16_t)(temp_h << 8 | temp_l)) / 16.0;
return temperature;
}
```
其中,delay_us和delay_ms函数为延时函数,需要自行实现。可以参考下面的示例:
```c
void delay_us(uint32_t time)
{
uint32_t i = 0;
for (i = 0; i < time * 8; i++);
}
void delay_ms(uint32_t time)
{
uint32_t i = 0;
for (i = 0; i < time * 8000; i++);
}
```
此外,需要在主函数中调用库函数GPIO_Init和RCC_APB2PeriphClockCmd等函数初始化STM32的GPIO口和时钟。具体使用方法可以参考STM32F10x官方文档和示例代码。