《土壤温湿度传感器c程序(4页).doc》由会员分享,可在线阅读,更多相关《土壤温湿度传感器c程序(4页).doc(4页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、-* 土壤温湿度传感器c程序-第 4 页* Created: 2015/8/5 9:18:56* Author: Administrator/* 常用头文件及宏定义 */#include #include /延时文件#include /睡眠文件#include /中断文件#include /eeprom#define BIT(x) (1(x)#define uchar unsigned char#define uint unsigned int/* 1602 */* 8位总线模式*/* 引脚说明 */* PC 0 RS PC 1 RW PC 2 E PC 3 背光 PB 0PB 7 数据 */
2、* 宏定义 */#define DDR_SHUJU DDRB#define PORT_SHUJU PORTB#define PIN_SHUJU PINB#define DDR_KZ DDRC#define PORT_KZ PORTC#define RS_H PORT_KZ|=BIT(0)#define RS_L PORT_KZ&=(BIT(0)#define RW_H PORT_KZ|=BIT(1)#define RW_L PORT_KZ&=(BIT(1)#define E_H PORT_KZ|=BIT(2)#define E_L PORT_KZ&=(BIT(2)#define BEIGUAB
3、G_H PORT_KZ|=BIT(3)#define BEIGUABG_L PORT_KZ&=(BIT(3)uchar lcd_read()/读忙函数uchar bl;DDR_SHUJU=0;/输入PORT_SHUJU=0xff;/上拉电阻RS_L;RW_H;E_H;_delay_ms(1);bl=PIN_SHUJU;E_L;RW_L;PORT_SHUJU=0;DDR_SHUJU=0xff;return bl;void lcd_write_char(uchar shuju)/写字符函数while(lcd_read()&BIT(7);RS_H;RW_L;PORT_SHUJU=shuju;E_H;
4、E_L;RS_L;void lcd_write_string(char *str)/写字符串函数while(*str!=0)lcd_write_char(*str);str+;void lcd_write_zhiling(uchar zhiling)while(lcd_read()&BIT(7);RS_L;RW_L;PORT_SHUJU=zhiling;E_H;E_L;void chushihua_1602()PORT_SHUJU=0;DDR_SHUJU=0xff;PORT_KZ=0;DDR_KZ=0xff;lcd_write_zhiling(0x38);/8位总线lcd_write_zhil
5、ing(0x08);lcd_write_zhiling(0x01);lcd_write_zhiling(0x06);lcd_write_zhiling(0x0c);/* 水泵初始化函数 */#define DDR_ZT DDRA#define PORT_ZT PORTA#define PIN_ZT PINA#define ZT_H PORT_ZT|=BIT(1)#define ZT_L PORT_ZT&=(BIT(1)void shuibengchushihua()DDR_ZT|=BIT(1);PORT_ZT|=BIT(1);/* 土壤湿度检测函数 */void shidujiande()sh
6、uibengchushihua();if (PIN_ZT&BIT(2)lcd_write_zhiling(0x80+6);lcd_write_char(1);ZT_H;elselcd_write_zhiling(0x80+6);lcd_write_char(0);ZT_L;/* ADC */#define ADC_ONADCSRA|=BIT(ADSC)/开始转换void chushihua_adc()int shuju_0;ADMUX=BIT(REFS0);/vcc参考电压,ADC0通道cli();/关中断ADCSRA=BIT(ADEN)|BIT(ADIE)|BIT(ADPS0);/2分频,中
7、断,ADCSRA|=BIT(ADSC);/开始转换while(!(ADCSRA&BIT(ADIF);shuju_0=ADCL;shuju_0+=ADCH8;ADCSRA|=BIT(ADIF);sei();/开中断ISR(ADC_vect)int shuju=0;shuju=ADCL;shuju+=ADCH8;shuju=shuju*(5.0/1024)*1000;lcd_write_zhiling(0x80+0x40+8);lcd_write_char(shuju/1000+0);lcd_write_char(.);lcd_write_char(shuju%1000/100+0);lcd_write_char(shuju%1000%100/10+0);lcd_write_char(shuju%10+0);_delay_ms(1000);/* 主函数 */int main(void)chushihua_1602();chushihua_adc();char *s_0=humi:,*s_1=dianya:;lcd_write_zhiling(0x80+0x40);lcd_write_string(s_1);lcd_write_zhiling(0x80);lcd_write_string(s_0);while(1)shidujiande();ADC_ON;_delay_ms(100);