=******************************************************MCU:Atmega8
本文引用地址:http://m.butianyuan.cn/article/201611/320582.htm內(nèi)部振蕩器:8M
環(huán)境:ICCAVR 7.0
程序:sdyzxue 薛海濤
2010/06/16
=*******************************************************
#include
#include
#define fosc 8000000
#define baud 9600
#define SEI() asm("sei")
#define CLR_DIR_1WIRE DDRC&=~BIT(1) //只要修改這里的參數(shù)就可以了!呵呵!
#define SET_DIR_1WIRE DDRC|=BIT(1) //里面什么都不用該!
#define CLR_OP_1WIRE PORTC&=~BIT(1)
#define SET_OP_1WIRE PORTC|=BIT(1)
#define CHECK_IP_1WIRE (PINC & 0x02) //檢測(cè)
unsigned char wmh,wmm,wml;
unsigned int temp,temp0,temp1,temp2;
unsigned char RX_data[10]={0} ; //串口接收的數(shù)據(jù)
unsigned char RX_counter=0 ; //串口接收到的字節(jié)數(shù)計(jì)數(shù)器
void delay_1us(void) //1us延時(shí)函數(shù)
{
asm("nop");
}
void delay_nus(unsigned int n) //N us延時(shí)函數(shù)
{
unsigned int i=0;
for (i=0;i delay_1us();
}
void delay_1ms(void) //1ms延時(shí)函數(shù)
{
unsigned int i;
for (i=0;i<1140;i++);
}
void delay_nms(unsigned int n) //N ms延時(shí)函數(shù)
{
unsigned int i=0;
for (i=0;i delay_1ms();
}
void putchar(unsigned char c)
{
while (!(UCSRA&(1< UDR=c;
}
//字符輸入函數(shù)
unsigned char getchar(void)
{
//等待接受字符
while(!(UCSRA&(1< return UDR;
}
//字符串輸出函數(shù)
int puts(char *s)
{
while (*s)
{
putchar(*s);
s++;
}
return 1;
}
//含回車換行的字符串輸出函數(shù)
void puts_hh(char *s)
{
while (*s)
{
putchar(*s);
s++;
}
// putchar(0x0a); //換行
putchar(0x0d); //回車
}
//UART初始化
void uart_init(void)
{
UCSRB=(1< //允許發(fā)送和接收 (RXCIE) 并響應(yīng)接收完成中斷
UBRRL=(fosc/16/baud+1)%256;
UBRRH=(fosc/16/baud+1)/256;
UCSRC=(1< //8位數(shù)據(jù)+1位停止位
}
#pragma interrupt_handler UART_rx: iv_USART_RX
void UART_rx(void) //串口接收中斷函數(shù)
{ RX_data[RX_counter] = UDR;
if (RX_data[RX_counter]==t)
{
RX_data[0]=RX_data[RX_counter];
RX_counter=0;
}
RX_counter++; //接收的字節(jié)數(shù)計(jì)數(shù)
}
void init_1820()
{
SET_DIR_1WIRE; //設(shè)置PC2 為輸出
SET_OP_1WIRE;
CLR_OP_1WIRE;
delay_nus(480); //480us以上
SET_OP_1WIRE;
CLR_DIR_1WIRE;
delay_nus(20); //15~60us
while(CHECK_IP_1WIRE);
SET_DIR_1WIRE;
SET_OP_1WIRE;
delay_nus(140); //60~240us
}
void write_1820(unsigned char x)
{
unsigned char m;
for(m=0;m<8;m++)
{
CLR_OP_1WIRE;
if(x&(1< //寫(xiě)數(shù)據(jù)了,先寫(xiě)低位的!
SET_OP_1WIRE;
else
{CLR_OP_1WIRE;}
delay_nus(40); //15~60us
SET_OP_1WIRE;
}
SET_OP_1WIRE;
}
unsigned char read_1820()
{
unsigned char temp,k,n;
temp=0;
for(n=0;n<8;n++)
{
CLR_OP_1WIRE;
SET_OP_1WIRE;
CLR_DIR_1WIRE;
k=(CHECK_IP_1WIRE); //讀數(shù)據(jù),從低位開(kāi)始
if(k)
temp|=(1<
else
temp&=~(1<
delay_nus(50); //60~120us
SET_DIR_1WIRE;
}
return (temp);
}
void gettemp() //讀取溫度值
{
unsigned char temh,teml;
init_1820(); //復(fù)位18b20
write_1820(0xcc); // 發(fā)出轉(zhuǎn)換命令
write_1820(0x44);
init_1820();
write_1820(0xcc); //發(fā)出讀命令
write_1820(0xbe);
teml=read_1820(); //讀數(shù)據(jù)
temh=read_1820();
temp0=teml&0x0F; //小數(shù)位
temp1=(temh<<4)|(teml>>4); //整數(shù)位
temp2=(temh>>3)&0x1F; //正負(fù)溫度
wmh=temp1/100; //一百以上輸出百位字
wmm=(temp1%100)/10; //出口參數(shù)!wmh是顯示的高位十位,wml使顯示的低位 個(gè)位
wml=(temp1%100)%10;
temp=temp0*625; //小數(shù)值
}
void main(void) //主函數(shù)
{
OSCCAL=0xb7;//系統(tǒng)時(shí)鐘校準(zhǔn),不同的芯片和不同的頻率,
DDRC=0XFF;
PORTC=0X00;
uart_init();
while(1)
{ gettemp();
puts_hh("=====Temp Test=====");
if(temp2)//負(fù)溫度
{
putchar(-);
}
if(!temp2)//正溫度
{
putchar(+);
}
putchar(wmh+0x30);//百
putchar(wmm+0x30);//十
putchar(wml+0x30);//個(gè)
putchar(.);
putchar(temp/1000+0x30);
putchar((temp%1000)/100+0x30);
putchar((temp%100)/10+0x30);
putchar(temp%10+0x30);
putchar(0x0d); //回車
delay_nms(10000);
}
}
評(píng)論