1、protues单片机各子程序模块/需要找元器件的符号/* LCD1602相关程序*/#define uchar unsigned char#define uint unsigned int/LCD1602端口定义sbit RS=P20;sbit RW=P21;sbit E=P22;void delay(uchar x) /延时uchar y;while(x-)for(y=0;y110;y+);void LCD_write_com(uchar com)/写指令RS=0;E=0;RW=0;P0=com;delay(5);E=1;delay(5);E=0;void LCD_write_data(uc
2、har date)/写数据RS=1;E=0;RW=0;P0=date;delay(5);E=1;delay(5);E=0;void LCD_init()/数据初始化E=0;LCD_write_com(0x38);delay(5);LCD_write_com(0x0c);delay(5);LCD_write_com(0x06);delay(5);LCD_write_com(0x01);delay(5);/* DS18B20子程序*/#define uchar unsigned char#define uint unsigned intsbit DQ=P27; /定义uchar temp0,tem
3、p1,i;/温度转换的低位和高位void delay1(uchar n) /n为1时大概6.9us while(n-);uchar Init_DS18B20()/初始化uchar m;DQ=0;delay1(100);DQ=1;delay1(4);m=DQ;delay1(40);return m;uchar read_byte() /读字节uchar i,n;for(i=0;i=1; DQ=1; if(DQ) n|=0x80;delay1(10); return n;void write_byte(uchar k)/写字节uchar i;for(i=0;i=1; void read_temp(
4、) /读取温度Init_DS18B20();write_byte(0xcc);write_byte(0xbe);temp0=read_byte();temp1=read_byte(); Init_DS18B20();write_byte(0xcc);write_byte(0x44);delay1(10);/*/ 温度显示*void display()uchar aa,cc,k,i=0; k=temp_value1&0xf8; /与以后是否为负数 if(k=0xf8) /假如为负数 temp_value1=temp_value1; /高位取反 temp_value0=temp_value0+1;
5、 /低位取反加1 DSY_CODE27=-; /显示负号 if(temp_value0=0)temp_value1=temp_value1+1; / 低位为0时高位取反加1 else DSY_CODE27= ; cc=(temp_value04)|(temp_value1)4); /cc为整数 aa=(temp_value0&0x0f)*10/16; /aa为小数*10 disbuffer0=cc/10; disbuffer1=cc%10; disbuffer2=aa; DSY_CODE28=disbuffer0+0; DSY_CODE29=disbuffer1+0; DSY_CODE210=
6、.; DSY_CODE211=disbuffer2+0; DSY_CODE213=C; write_com(0x80); for(i=0;i16;i+) write_data(DSY_CODE1i); write_com(0x80+0x40); for(i=0;i16;i+) write_data(DSY_CODE2i); /* 多点测温程序*/#include#include #include #define uint unsigned int#define uchar unsigned charuchar SN88=0x28,0x30,0xc5,0xb8,0x00,0x00,0x00,0x
7、8e,0x28,0x31,0xc5,0xb8,0x00,0x00,0x00,0xb9,0x28,0x32,0xc5,0xb8,0x00,0x00,0x00,0xe0,0x28,0x33,0xc5,0xb8,0x00,0x00,0x00,0xd7,0x28,0x34,0xc5,0xb8,0x00,0x00,0x00,0x52,0x28,0x35,0xc5,0xb8,0x00,0x00,0x00,0x65,0x28,0x36,0xc5,0xb8,0x00,0x00,0x00,0x3c,0x28,0x37,0xc5,0xb8,0x00,0x00,0x00,0x0b ;uchar code DSY_C
8、ODE1=Current Temp : ;uchar DSY_CODE2=TEMP: ;sbit DQ=P10;uchar temp_value=0x00,0x00;uchar disbuffer=0,0,0;uchar num;void delay1(uchar n)/n=1时大概延时6.9us while(n-);uchar ow_DS18B20()/DS18B20复位 uchar m; DQ=0; delay1(100); DQ=1; delay1(4); m=DQ; delay1(40); return m;uchar read_byte()/读取字节uchar i;uchar n=0
9、;for(i=0;i=1; DQ=1; if(DQ) n|=0x80; delay1(10); return(n);void write_byte(uchar k)/写字节uchar i;for(i=0;i=1; void read_temp()/读温度uchar i,j;uchar aa,cc,k;write_byte(0xcc);write_byte(0x44);for(j=0;j8;j+) ow_DS18B20(); delay(1); write_byte(0x55); for(i=0;i4)|(temp_value1)4); /cc为整数 aa=(temp_value0&0x0f)*
10、10/16; /aa为小数*10 disbuffer0=cc/10; disbuffer1=cc%10; disbuffer2=aa; DSY_CODE28=disbuffer0+0; DSY_CODE29=disbuffer1+0; DSY_CODE210=.; DSY_CODE211=disbuffer2+0; DSY_CODE213=C; write_com(0x80); for(i=0;i16;i+) write_data(DSY_CODE1i); write_com(0x80+0x40); for(i=0;i16;i+) write_data(DSY_CODE2i); void ma
11、in()init();ow_DS18B20();read_temp();delay(40);while(1)read_temp();/* ADC0832 获取AD转换结果(模数转换电压)*/ADC0832端口定义sbit CS=P10;sbit CLK=P11;sbit DIO=P12;uchar get_adc()/获得的结果就是k=get_adc();unsigned char i,dat;CS=1; CLK=0;CS=0; DIO=1;CLK=1;CLK=0; DIO=1;CLK=1;CLK=0; DIO=0;CLK=1;CLK=0; DIO=1;CLK=1; for(i=0;i8;i+
12、) CLK=1; CLK=0; dat=1; dat|=(unsigned char)DIO; CS=1; return dat;/* DS1302数字时钟*/#define uint unsigned int#define uchar unsigned charuchar disbuffer7;sbit IO = P10;sbit SCLK = P11;sbit RST = P12;void write_byte(uchar x)/写入一字节uchar i;for(i=0;i=1; uchar read_byte()/读取一字节uchar i,b=0x00;for(i=0;i8;i+) b|
13、=_crol_(uchar)IO,i); SCLK=1; SCLK=0; return (b/16*10+b%16);uchar read_data(uchar addr)/在指定地址读数据uchar dat;RST=0;SCLK=0;RST=1;write_byte(addr);dat=read_byte();SCLK=1;RST=0;return dat;void get_time()/读取时间uchar i,addr=0x81;for(i=0;i7;i+) disbufferi=read_data(addr); /声明:disbuffer0是秒的值 addr+=2; / disbuffe
14、r1是分 / disbuffer06秒,分,时,日,星期,月,年* 时钟显示部分uchar i;uchar LCD_DSY1=DATE 00-00-00 ; /日期uchar LCD_DSY2=TIME 00:00:00 ; /时间void display()get_time();LCD_DSY212=disbuffer0%10+0;LCD_DSY211=disbuffer0/10+0; /秒低位,秒高位LCD_DSY29=disbuffer1%10+0;LCD_DSY28=disbuffer1/10+0; /分LCD_DSY26=disbuffer2%10+0;LCD_DSY25=disbu
15、ffer2/10+0; /时LCD_DSY112=disbuffer3%10+0;LCD_DSY111=disbuffer3/10+0;/日 / disbuffer5 是星期LCD_DSY19=disbuffer4%10+0;LCD_DSY18=disbuffer4/10+0; /月LCD_DSY16=disbuffer6%10+0;LCD_DSY15=disbuffer6/10+0; /年LCD_write_com(0x80);for(i=0;i16;i+)LCD_write_data(LCD_DSY1i);LCD_write_com(0x80+0x40);for(i=0;i40x0f; s
16、witch(b) /先去确定列 case 1: c=c+0;break; /第一行 case 2: c=c+4;break; case 4: c=c+8;break; case 8: c=c+12; /* LCD12864相关程序*/#define uchar unsigned char#define uint unsigned int#define disp_Off 0x3e /关显示#define disp_On 0x3f / 开显示#define Page_Add 0xb8 /页地址#define Col_Add 0x40 /列地址#define Start_Line 0xc0 /行地址
17、sbit RS= P20;sbit RW= P21;sbit E = P22;sbit CS1=P23;sbit CS2=P24;sbit RST=P25;uchar code wenzi32; /文字字模的定义uchar code shuzi16; /数字字摸的定义bit LCD_busy() / 检查忙位P0=0xff;RST=1;RW=1;RS=0;E=1;E=0;return (bit)(P0&0x80);void LCD_write_com(uchar com) /写命令到LCDLCD_busy();RW=0;RS=0;E=0;P0=com;E=1;E=0;void LCD_writ
18、e_data(uchar date)/ 写数据到LCDLCD_busy();RW=0;RS=1;P0=date;E=1;E=0;void LCD_Reset() / 初始化LCD屏 CS1=1;CS2=1;LCD_write_com(0x38);LCD_write_com(0x0f); LCD_write_com(0x01);LCD_write_com(0x06); LCD_write_com(Start_Line); void CLR_12864() /清屏uchar j,k;CS1=0;CS2=0;LCD_write_com(Page_Add+0);LCD_write_com(Col_Ad
19、d+0);for(k=0;k8;k+) LCD_write_com(Page_Add+k); for(j=0;j64;j+) LCD_write_com(Col_Add+j); LCD_write_data(0x00); void display_a_char(uchar p,uchar l,uchar n) /显示数字8*16的数字 /第P行第L位显示Muchar i,j;for(j=0;j2;j+) LCD_write_com(Page_Add+p+j); LCD_write_com(Col_Add+l); for(i=0;i8;i+) LCD_write_data(shuzin8*j+i
20、); /shuzi 为需要的数字 void display_a_word(uchar p,uchar l,uchar n) /显示数字16*16的汉字uchar i,j;for(j=0;j2;j+) LCD_write_com(Page_Add+p+j); LCD_write_com(Col_Add+l); for(i=0;i16;i+) LCD_write_data(wenzin16*j+i); /wenzi为需要的数字 void disp_img(uchar code *img) /显示12864图片 不需图片时可以不要uchar j,k;for(k=0;k8;k+) CS2=1; CS1=0; LCD_write_com(Page_Add+k); LCD_write_com(Col_Add+0); for(j=0;j64;j+) LCD_write_data(imgk*128+j); CS2=0; CS1=1; LCD_write_com(Page_Add+k); LCD_write_com(Col_Add+0); for(j=64;j128;j+) LCD_write_data(imgk*128+j
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1