找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 1303|回復(fù): 2
打印 上一主題 下一主題
收起左側(cè)

avr單片機(jī)程序有沒有大神幫忙看一下哪里有錯(cuò)誤,并改一下

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
有沒有大神幫忙看一下哪里有錯(cuò)誤,并改一下
#include <avr/io.h>
#include <util/delay.h>
#define uchar unsigned char
#define uint unsigned int
#define BUS PORTC
// Low level port/pin definitions         低級端口/引腳定義
#define sbit(x,PORT) (PORT) |= (1<<x)
#define cbit(x,PORT) (PORT) &= ~(1<<x)
#define pin(x,PIN) (PIN) & (1<<x)
// Pins definition          引腳定義
#define s_digit1 sbit(5,PORTC)
#define c_digit1 cbit(5,PORTC)
#define s_digit2 sbit(4,PORTC)
#define c_digit2 cbit(4,PORTC)
#define out PORTC
#define DQ_IN DDRA&=~(1<<7)
#define DQ_OUT DDRA|=(1<<7)
#define S_DQ sbit(7,PORTA)
#define C_DQ cbit(7,PORTA)
#define DQ pin(7,PINA)
// Function Prototypes           函數(shù)原型
void init_ds18b20(void);
uchar readbyte(void);
void writecommand(uchar);
uchar readtemp(void);
uchar a, b, tt;
// Main program          主函數(shù)
int main(void)
{ uchar i=0, temp;
// Initialize Stack Pointer          初始化堆棧指針
SPL=0x54;
SPH=0x04;
// Configure port pins   配置端口引腳
DDRC = 0xff;
DDRA = 0xff;
while(1)
{ temp = readtemp();
for(i=0; i<10; i++) // 10 measures
{ // output the units        輸出的單位
out = (temp/10) & 0x0f;
s_digit1;
c_digit2;
_delay_ms(5);
// output the tens         輸出十
out = (temp%10) & 0x0f;
c_digit1;
s_digit2;
_delay_ms(5);
}
}
}
// Start transaction with 1-wire line.          用單線啟動(dòng)事務(wù)
void init_ds18b20(void)
{ DQ_OUT;
C_DQ ;
_delay_us(600);
S_DQ;
_delay_us(50);
DQ_IN;
while(DQ);
_delay_us(240);
DQ_OUT;
S_DQ;
_delay_us(300);
}
// Read a byte from the sensor        從傳感器讀取一個(gè)字節(jié)
uchar readbyte(void)
{ uchar i = 0,data = 0;
DQ_OUT;
for (i=0; i<8; i++)
{ C_DQ ;
data >>= 1;
_delay_us(3);
S_DQ;
DQ_IN;
_delay_us(12);
if(DQ)
data |= 0x80;
DQ_OUT;
S_DQ;
_delay_us(45);
_delay_us(5);
}
return(data);
}
// Write a command to the sensor  向傳感器寫入命令
void writecommand(uchar data)
{ uchar i;
for(i=0; i<8; i++)
{ C_DQ;
_delay_us(15);
if(data & 0x01)
S_DQ;
else
C_DQ;
_delay_us(45);
data >>= 1;
S_DQ;
_delay_us(2);
}
}
// Read value from the sensor        從傳感器讀取值
uchar readtemp(void)
{ uint t;
init_ds18b20();
// Convert          轉(zhuǎn)換
writecommand(0xCC);
writecommand(0x44);
init_ds18b20();
// Read Scratch memory area                讀暫存區(qū)
writecommand(0xCC);
writecommand(0xBE);
a = readbyte();
b = readbyte();
t = b;
t <<= 8;
t = t|a;
tt = t*0.0625;
return(tt);
}

-7b9a793026903896.png (115.82 KB, 下載次數(shù): 31)

-7b9a793026903896.png
分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:328014 發(fā)表于 2020-12-4 16:30 | 只看該作者
程序沒有錯(cuò)誤
回復(fù)

使用道具 舉報(bào)

板凳
ID:854138 發(fā)表于 2020-12-5 15:34 來自手機(jī) | 只看該作者
51hei團(tuán)團(tuán) 發(fā)表于 2020-12-4 16:30
程序沒有錯(cuò)誤

這個(gè)是基于數(shù)字溫度傳感器的數(shù)字溫度計(jì)
回復(fù)

使用道具 舉報(bào)

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表