LM75温度传感器数据采集实验

 

 一、实验步骤

1、打开ICCAVR7 >> 打开工程>> 配置好端口 >> 将程序下载到ATmega162

2、打开sscom32.exe >> 配置选项如下

 

然后就会接收到数据。将数据复制出来粘贴到temp_data.txt中,运行printf.exe

就可以看到温度数据。

PS:可以通过串口猎手工具将温度数据通过画图展示出来。

工程源文件代码:

  1 /********************************************************************
  2  * 温度传感器LM75试验
  3  * 功能:     用串口打印当前温度的值,通过串口调试助手显示
  4  * 目标器件:ATmega162v                                                  
  5  * 晶振:     RC 8MHZ
  6  * 熔丝位设置:EE 99  FF
  7  * 编译环境:ICCAVR 7                                            
  8  * 版本:    V1.0 (2012/08/09)
  9  *******************************************************************/
 10 #define F_CPU 8000000
 11 #define UBRR  9600
 12 #define MYUBRR F_CPU / (16 *( UBRR + 1))
 13 
 14 #include <iom162v.h>
 15 #include <macros.h>
 16 
 17 typedef unsigned char  U8;       /* defined for unsigned 8-bits integer variable       无符号8位整型变量  */
 18 typedef signed   char  S8;       /* defined for signed 8-bits integer variable          有符号8位整型变量  */
 19 typedef unsigned int   U16;      /* defined for unsigned 16-bits integer variable       无符号16位整型变量 */
 20 typedef signed   int   S16;      /* defined for signed 16-bits integer variable       有符号16位整型变量 */
 21 typedef unsigned long  U32;      /* defined for unsigned 32-bits integer variable       无符号32位整型变量 */
 22 typedef signed   long  S32;      /* defined for signed 32-bits integer variable       有符号32位整型变量 */
 23 typedef float          F32;      /* single precision floating point variable (32bits) 单精度浮点数(32位长度) */
 24 typedef double         F64;      /* double precision floating point variable (64bits) 双精度浮点数(64位长度) */
 25 
 26 #define SETBIT(val, bitn)     (val |= (1<<(bitn))) 
 27 #define CLRBIT(val, bitn)     (val &=~(1<<(bitn))) 
 28 #define GETBIT(val, bitn)     (val &= (1<<(bitn))) 
 29 //PC0    模拟I2C数据传输位                       
 30 #define LM75_SDA 0                                               
 31 //PC1    模拟I2C时钟控制位
 32 #define LM75_SCL 1        
 33 #define LM75_OS  2
 34 //LM75A has a 7-bit slave address
 35 
 36 //----------------lm75地址、配置字-----------------//
 37 //1001    010 0 写 
 38 #define LM75_WR_ADDRESS   0x94              
 39 //1001    010 1 读
 40 #define LM75_RD_ADDRESS   0x95             
 41 //指针寄存器选择 TEMP 
 42 #define LM75_POINT         0x00                                        
 43 U8 temp[2];  
 44 
 45 void    delayus(U8 del)
 46 {
 47          while(del--);
 48 }
 49 /********************************************************************
 50  * 函数功能:初始化温度传感器LM75
 51  * 注意事项:
 52  * 提示说明:
 53  * 输    入:
 54  * 返    回:
 55  * *****************************************************************/
 56 void    init_lm75(void)
 57 {
 58          SETBIT(DDRC,LM75_SDA);      //设置IO 位输出
 59         SETBIT(DDRC,LM75_SCL);
 60 
 61         SETBIT(PORTC,LM75_SDA);      //输出  置1
 62         delayus(5);
 63         SETBIT(PORTC,LM75_SCL);
 64         delayus(5);
 65         
 66         CLRBIT(PORTC,LM75_SDA); //输出  清0
 67         delayus(5);
 68         CLRBIT(PORTC,LM75_SCL);
 69         delayus(5);
 70 }
 71 /********************************************************************
 72  * 函数功能:发送一字节数据给LM75函数
 73  * 注意事项:
 74  * 提示说明:
 75  * 输    入:
 76  * 返    回:
 77  * *****************************************************************/
 78 U8 sendbyte_lm75(U8 buf)
 79 {
 80          U8 i = 0;
 81         SETBIT(DDRC,LM75_SDA);         //设置IO为输出
 82         SETBIT(DDRC,LM75_SCL);                
 83         //将数据串行输出
 84         for(i = 8;i > 0;i--){
 85           CLRBIT(PORTC,LM75_SCL);
 86           delayus(5);
 87               if(buf & 0x80)         //bit7 == 1
 88                     SETBIT(PORTC,LM75_SDA);            
 89               else                     //bit7 == 0
 90                     CLRBIT(PORTC,LM75_SDA);
 91               buf <<= 1;
 92           SETBIT(PORTC,LM75_SCL);
 93           delayus(5);
 94         }
 95                                      
 96         CLRBIT(PORTC,LM75_SCL);
 97         delayus(20);                       //wait for ack
 98             SETBIT(PORTC,LM75_SDA);
 99             delayus(5);
100             CLRBIT(DDRC,LM75_SDA);    //set SDA  input    
101         SETBIT(PORTC,LM75_SCL);        
102         delayus(20);
103         
104         if(GETBIT(PINC,LM75_SDA)){        //fail
105             SETBIT(DDRC,LM75_SCL);
106             CLRBIT(PORTC,LM75_SCL);
107             return 0;
108         }else{                                   //成功返回 地址
109             SETBIT(DDRC,LM75_SCL);
110             CLRBIT(PORTC,LM75_SCL);
111             return LM75_RD_ADDRESS;
112         }
113 
114 }
115 /********************************************************************
116  * 函数功能:从LM75获取一字节数据函数
117  * 注意事项:
118  * 提示说明:
119  * 输    入:
120  * 返    回:
121  * *****************************************************************/
122 U8 getbyte_lm75(void)
123 {
124           U8 i = 0,buf = 0;
125          CLRBIT(DDRC,LM75_SDA);               //数据输入                     
126          SETBIT(DDRC,LM75_SCL);                    //时钟输出         
127          for(i = 0;i < 8;i++){
128                CLRBIT(PORTC,LM75_SCL);        /*置时钟线为低,准备接收数据位*/
129               delayus(20);
130               SETBIT(PORTC,LM75_SCL);        /*置时钟线为高使数据线上数据有效*/
131               delayus(10);
132               buf <<= 1;
133               if(GETBIT(PINC,LM75_SDA)){    /*读数据位,接收的数据位放入buf中*/
134                       buf += 1;
135                     delayus(10);
136               }
137               CLRBIT(PORTC,LM75_SCL);        
138          }
139          return buf;
140 }
141 
142 /********************************************************************
143  * 函数功能:
144  * 注意事项:
145  * 提示说明:
146  * 输    入:
147  * 返    回:
148  * *****************************************************************/
149 void stop_lm75(void)
150 {
151       SETBIT(DDRC,LM75_SDA);
152      SETBIT(DDRC,LM75_SCL);
153      
154      CLRBIT(PORTC,LM75_SDA);
155      delayus(20);
156      SETBIT(PORTC,LM75_SCL);
157      delayus(20);
158      SETBIT(PORTC,LM75_SDA);
159      delayus(20);
160 }
161 /********************************************************************
162  * 函数功能:主控器进行应答信号,(可以是应答或非应答信号)
163  * 注意事项:
164  * 提示说明:
165  * 输    入:
166  * 返    回:
167  * *****************************************************************/
168 void ack_lm75(U8 ack)
169 {
170       SETBIT(DDRC,LM75_SDA);
171      SETBIT(DDRC,LM75_SCL);
172 
173      if(ack)
174          CLRBIT(PORTC,LM75_SDA);
175      else
176          SETBIT(PORTC,LM75_SDA);
177 
178      delayus(3);
179      SETBIT(PORTC,LM75_SCL);
180      delayus(5);
181      CLRBIT(PORTC,LM75_SCL);
182      delayus(2);
183 }
184 /********************************************************************
185  * 函数功能:获取温度值
186  * 注意事项:
187  * 提示说明:
188  * 输    入:
189  * 返    回:
190  * *****************************************************************/
191 U8 temperature_lm75(void)
192 {
193       //U8 temp[2];
194      U8 i;
195      
196      init_lm75();
197      //I2CSendByte( 0x90 | ((DeviceAdr&0x07)<<1) | 0x01 ); // Send Device Adress 1010 A2/P2 A1/P1 A0/P0 R/W=1(Read)
198      if(sendbyte_lm75(LM75_RD_ADDRESS) == 0)              //发送LM75的地址:读写位 1001 A2 A1 A
199           return 0;                                             //发送地址出错,返回0
200          
201      temp[1] = getbyte_lm75();
202      ack_lm75(1);
203      temp[0] = getbyte_lm75();
204      ack_lm75(0);
205 
206      stop_lm75();
207      
208      if(temp[1] & 0x80)                  //temperature was minus负的
209          temp[1] = ~temp[1]+0x80;
210     
211      return 1;
212      //return 0;
213 }
214 /********************************************************************
215  * 函数功能:串口初始化
216  * 注意事项:
217  * 提示说明:
218  * 输    入:
219  * 返    回:
220  * *****************************************************************/
221 void USART_Init( unsigned int ubrr )
222 {
223      UCSR0B = 0x00;          //关闭USART1
224     UCSR0A = 0x00;          //不使用倍速发送
225     UCSR0C = (1<<URSEL0)|(1<<UCSZ00)|(1<<UCSZ01);//数据位为八位
226     UBRR0L = ubrr%256;
227     UBRR0H = ubrr/256;
228     UCSR0B = (1<<RXEN0)|(1<<TXEN0);    
229     //UCSR0B =(1<<RXEN0)|(1<<TXEN0)|(1<<RXCIE0);        //发送使能,接收使能,接收中断使能 
230 }
231 /********************************************************************
232  * 函数功能:打印一个字节
233  * 注意事项:
234  * 提示说明:
235  * 输    入:
236  * 返    回:
237  * *****************************************************************/
238 void putch(unsigned char data)
239 {
240     while (!( UCSR0A & (1<<UDRE0)));                       //表明发送器一准备就绪
241     UDR0 = data;
242 }
243 /********************************************************************
244  * 函数功能:延时ms函数
245  * 注意事项:
246  * 提示说明:
247  * 输    入:
248  * 返    回:
249  * *****************************************************************/
250 void delay_ms(long int s)
251 {
252     int t;
253     while(s--)
254     {
255         for(t=1000;t>0;t--)
256             ;
257     }
258 }
259 
260 /********************************************************************
261  * 函数功能:主函数
262  * 注意事项:
263  * 提示说明:
264  * 输    入:
265  * 返    回:
266  * *****************************************************************/
267 void main(void)
268 {    
269     CLKPR = (1<<CLKPCE);
270     CLKPR = 0x00;
271     USART_Init(MYUBRR);
272     
273     while(1){
274          //display(0);
275         temperature_lm75();
276         putch('A');
277         putch(temp[1]);
278         putch('.');
279         putch(temp[0]);    
280         putch('C');
281         delay_ms(1000);
282      }
283 }

printf.exe代码:

View Code
 1 #include<stdlib.h>
 2 #include<string.h>
 3 #include<stdio.h>
 4 #include<dos.h>
 5 
 6 int main(void){
 7  FILE *stream;
 8  char ch;
 9  char msg[2];
10  int data;
11  float temper;
12  int point,i;
13  int a,b;
14 
15  stream = fopen("temp_data.txt", "rb");
16  fseek(stream, 0, SEEK_SET);
17  do{
18  fgets(msg,3,stream);
19 
20  if(msg[0]>'9')
21     a=msg[0]-54;
22  else
23     a=msg[0]-48;
24 
25  if(msg[1]>'9')
26     b=msg[1]-54;
27  else
28     b=msg[1]-48;
29 
30  data=a*16+b;
31  temper=data/2;
32 
33  point=temper;
34  for(i=0;i<point;i+=2)
35     printf(".");
36     printf("%-3.1f  ",temper);
37     printf("\n");
38  ch=fgetc(stream);
39  }while(ch!=EOF);
40  fclose(stream);
41  ch=getchar();
42  if(ch==1)
43  return 0;
44  }

 

转载于:https://www.cnblogs.com/fusky/archive/2013/03/08/2950394.html

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值