网购了一块TTP229的触摸模块。模块中实际使用的是8229BSF集成块。本人用STC89C52搭建了测试电路。用12864液晶显示。
显示01-16,表示16个按钮的对应值。
制作出来的实物图如下:
单片机源程序如下:
- /*********************************************************************************
- * 【作 者】:
- * 【版 本】:
- * 【函数功能】: 利用单线制DS18B20温度传感器, 并在液晶12864上显示
- * 【使用说明】: 触摸板P1的第一和第三跳线短接
- **********************************************************************************/
- #include <reg52.h>
- #include <string.h>
- #include <math.h>
- #include <stdio.h>
- #include <intrins.h>
- #define uchar unsigned char
- #define uint unsigned int
- // 液晶现实属区区
- uchar code pic1[];
- uchar code pic2[];
- uchar code pic3[];
- /* 液晶控制接口 -----------------------------------------------*/
- sbit RS = P0^7;
- sbit WRD = P0^6;
- sbit E = P0^5;
- sbit PSB = P0^4;
- sbit RES = P0^3;
- sbit sign = P1^0;
- sbit sign1 = P1^1;
- // TTP229接口
- sbit SDA=P1^2;
- sbit SCL=P1^3;
- // 温度传感器接口
- sbit DQ=P3^7; //数据传输线接单片机的相应的引脚
- /* 温度测量用变量定义 -----------------------------------------------*/
- unsigned char tempL=0; //设全局变量
- unsigned char tempH=0;
- unsigned int sdate;//测量到的温度的整数部分
- unsigned char xiaoshu1;//小数第一位
- unsigned char xiaoshu2;//小数第二位
- unsigned char xiaoshu;//两位小数
- bit fg=1; //温度正负标志
- // 液晶处理函数
- void TransferData(char data1,bit DI);
- void display(void);
- void display_grapic(void);
- void delayms(uint n);
- void DisplayLine(uchar line1,uchar line2);
- void DisplayGraphic(uchar *adder);
- void delay(uint m);
- void lcd_mesg(uchar *adder1);
- void delay1ms(unsigned int t);
- // 18B20温度传感器函数
- void ReadTemperature(void);
- void Init_DS18B20(void);
- unsigned char ReadOneChar();
- void WriteOneChar(unsigned char dat);
- void Delay10us(unsigned int t);
- void Initial_com(void);
- // 扫描TTP229电容触摸键盘模块
- unsigned int TTP229B(void);
- /********************************************************************
- * 名称 : Convert(uchar In_Dat)
- * 功能 : 因为电路设计时,P2.0--P2.7接法刚好了资料中的相反,所以设计该函数。
- * 输入 : 1602资料上的值
- * 输出 : 送到1602的值
- ***********************************************************************/
- unsigned char Convert(uchar In_Dat) {
- uchar i, Out_Dat;
- for(i=0; i<8; i++){
- Out_Dat= In_Dat;
- }
- return Out_Dat;
- }
- void initinal(void) { //LCD字库初始化程序
- delay(40); //大于40MS的延时程序
- PSB=1; //设置为8BIT并口工作模式
- delay(1); //延时
- RES=0; //复位
- delay(1); //延时
- RES=1; //复位置高
- delay(10);
- TransferData(0x30,0); //Extended Function Set :8BIT设置,RE=0: basic instruction set, G=0 :graphic display OFF
- delay(100); //大于100uS的延时程序
- TransferData(0x30,0); //Function Set
- delay(37); ////大于37uS的延时程序
- TransferData(0x08,0); //Display on Control
- delay(100); //大于100uS的延时程序
- TransferData(0x10,0); //Cursor Display Control光标设置
- delay(100); //大于100uS的延时程序
- TransferData(0x0C,0); //Display Control,D=1,显示开
- delay(100); //大于100uS的延时程序
- TransferData(0x01,0); //Display Clear
- delay(10); //大于10mS的延时程序
- TransferData(0x06,0); //Enry Mode Set,光标从右向左加1位移动
- delay(100); //大于100uS的延时程序
- }
- /********************************************************************
- * 名称 : initina2()
- * 功能 : 主函数
- * 输入 : 无
- * 输出 : 无
- ***********************************************************************/
- void initina2(void) //LCD显示图片(扩展)初始化程序
- {
- delay(40); //大于40MS的延时程序
- PSB=1; //设置为8BIT并口工作模式
- delay(1); //延时
- RES=0; //复位
- delay(1); //延时
- RES=1; //复位置高
- delay(10);
- TransferData(0x36,0); //Extended Function Set RE=1: extended instruction
- delay(100); //大于100uS的延时程序
- TransferData(0x36,0); //Extended Function Set:RE=1: extended instruction set
- delay(37); ////大于37uS的延时程序
- TransferData(0x3E,0); //EXFUNCTION(DL=8BITS,RE=1,G=1)
- delay(100); //大于100uS的延时程序
- TransferData(0x01,0); //CLEAR SCREEN
- delay(100); //大于100uS的延时程序
- }
- /********************************************************************
- * 名称 : Main()
- * 功能 : 主函数
- * 输入 : 无
- * 输出 : 无
- ***********************************************************************/
- void main(void) {
- uchar init18B20[]={
- "温度传感器18B20 "
- "初始化中...... "
- " "
- " "
- };
-
- uchar curtemp[]={
- "当前温度: "
- " "
- " "
- " "
- };
- uchar IC_DAT2[]={
- "白日依山尽, " //0x80
- "欲穷千里目, " //0x90
- "黄河入海流。 " //0x88
- "更上一层楼。 " //0x98
- };
- unsigned int key=0;
- // 初始化串口
- Initial_com();
- initinal(); //调用LCD字库初始化程序
- delay(100); //大于100uS的延时程序
- lcd_mesg(IC_DAT2); //显示中文汉字2
- delayms(80);
- sign = 0;
- // 显示温度传感器18B20初始化中
- initinal(); //调用LCD字库初始化程序
- delay(100); //大于100uS的延时程序
- lcd_mesg(init18B20); //显示中文汉字1
- delay(240);
- delay(240);
- Init_DS18B20(); //初始化
- sign = 1;
- delayms(80);
- SDA=1;
- SCL=1;
- while(1) {
- sign1 = 0;
- // 初始化温度传感器并取得当前温度
- ReadTemperature();
-
- sign1=1;
- SBUF=sdate;
- delayms(20);
- SBUF=xiaoshu;
- // 转换温度数据为字符串
- // 整数部分
- curtemp[10]=48+sdate/10;
- curtemp[11]=48+sdate%10;
- // 小数点
- curtemp[12]='.';
- // 小数部分
- //curtemp[13]=48+xiaoshu/10;
- curtemp[13]=48+xiaoshu1;
- curtemp[14]=48+xiaoshu2;
- delayms(20);
- // 读键盘,显示按钮标号
- key = TTP229B();
- curtemp[16]='K';
- curtemp[17]='e';
- curtemp[18]='y';
- curtemp[19]=':';
- curtemp[20]=key/10 + 48;
- curtemp[21]=key%10 + 48;
- lcd_mesg(curtemp); //显示按钮编号
- }
- }
- /********************************************************************
- * 名称 : lcd_mesg()
- * 功能 : 输出信息到LCD
- * 输入 : adder1 字符串信息
- * 输出 : 无
- ***********************************************************************/
- void lcd_mesg(unsigned char *adder1) {
- unsigned char i;
- TransferData(0x80,0); //Set Graphic Display RAM Address
- delay(100);
- for(i=0;i<32;i++) {
- TransferData(*adder1,1);
- adder1++;
- }
- TransferData(0x90,0); //Set Graphic Display RAM Address
- delay(100);
- for(i=32;i<64;i++) {
- TransferData(*adder1,1);
- adder1++;
- }
- }
- /********************************************************************
- * 名称 : TTP229B()
- * 功能 : 扫描TTP229电容键盘(16按键)函数
- * 触摸结果直接输出只有8个,平时输出低电平,触摸按键键后,对应输出脚为高电平
- * 同时触摸按键,只有第一个生效
- * 输入 : 无
- * 输出 : 按键编号
- ***********************************************************************/
- unsigned int TTP229B(void) {
- uchar key=0;
- unsigned char i;
- unsigned int temp=0x0000;
- SDA=0;
- delay(100);
- SDA=1;
- delay(20);
- for(i=0;i<16;i++) {
- SCL=0;
- _nop_();;
- SCL=1;
- _nop_();;
- if(SDA==0) {
- temp|=(1<<i);
- }
-
- }
- for (i=0;i<16;i++) {
- if ((temp & (1<<i)) > 0) {
- key=i+1;
- }
- }
- delayms(32);
-
- return key;
- }
- /********************************************************************
- * 名称 : TransferData()
- * 功能 : 主函数
- * 输入 : 无
- * 输出 : 无
- ***********************************************************************/
- void TransferData(char data1,bit DI) { //传送数据或者命令,当DI=0是,传送命令,当DI=1,传送数据.
- WRD=0;
- RS=DI;
- delay(1);
- P2=Convert(data1);
- E=1;
- delay(1);
- E=0;
- }
- /********************************************************************
- * 名称 : delayms()
- * 功能 : 主函数
- * 输入 : 无
- * 输出 : 无
- ***********************************************************************/
- void delayms(unsigned int n) { //延时10×n毫秒程序
- unsigned int i,j;
- for(i=0;i<n;i++)
- for(j=0;j<2000;j++);
- }
- ///********************************************************************
- //* 名称 : Main()
- //* 功能 : 主函数
- //* 输入 : 无
- //* 输出 : 无
- //***********************************************************************/
- //void delay(unsigned int m) { //延时程序
- // unsigned int i,j;
- // for(i=0;i<m;i++)
- // for(j=0;j<10;j++);
- //}
- /********************************************************************
- * 名称 : DisplayGraphic()
- * 功能 : 主函数
- * 输入 : 无
- * 输出 : 无
- ***********************************************************************/
- void DisplayGraphic(unsigned char *adder) {
- int i,j;
- //*******显示上半屏内容设置
- for(i=0;i<32;i++) { //
- TransferData((0x80 + i),0); //SET 垂直地址 VERTICAL ADD
- TransferData(0x80,0); //SET 水平地址 HORIZONTAL ADD
- for(j=0;j<16;j++)
- {
- TransferData(*adder,1);
- adder++;
- }
- }
- //*******显示下半屏内容设置
- for(i=0;i<32;i++) {
- TransferData((0x80 + i),0); //SET 垂直地址 VERTICAL ADD
- TransferData(0x88,0); //SET 水平地址 HORIZONTAL ADD
- for(j=0;j<16;j++) {
- TransferData(*adder,1);
- adder++;
- }
- }
- }
- unsigned char code pic2[]=
- {
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0F,0x78,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3C,0x1E,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xF0,0x07,0x80,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x03,0xC0,0x01,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x0F,0x00,0x00,0x78,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x3C,0x00,0x00,0x1E,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0xF0,0x00,0x00,0x07,0x80,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x03,0xC0,0x00,0x00,0x01,0xC0,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x0F,0x00,0x00,0x00,0x00,0x70,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x3C,0x00,0x00,0x00,0x00,0x1C,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,0x1E,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x03,0xC0,0x00,0x00,0x00,0x00,0x7E,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x0F,0x00,0x00,0x00,0x00,0x01,0xE6,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x3C,0x00,0x00,0x00,0x00,0x07,0x82,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,0x1E,0x02,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x01,0xE0,0x00,0x00,0x00,0x00,0x78,0x03,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x01,0xE0,0x00,0x00,0x00,0x01,0xE0,0x03,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x01,0x78,0x00,0x00,0x00,0x07,0x80,0x01,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x03,0x1E,0x00,0x00,0x00,0x1E,0x00,0x07,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x03,0x07,0x80,0x00,0x00,0x78,0x00,0x1F,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x02,0x01,0xE0,0x00,0x01,0xE0,0x00,0x3F,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x06,0x00,0x78,0x1F,0x07,0x80,0x01,0xF1,0x80,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x0E,0x00,0x1E,0x3F,0x9E,0x00,0x03,0xF9,0x80,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x0E,0x00,0x07,0xB3,0x78,0x00,0x0F,0xF9,0x80,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x0F,0x80,0x01,0xFF,0xE0,0x00,0x79,0x98,0x80,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x0F,0xE0,0x00,0x7F,0x80,0x00,0xE0,0xD8,0x80,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x0F,0xF8,0x00,0x0C,0x00,0x07,0xE0,0xF8,0x80,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x0E,0x7E,0x00,0x0C,0x00,0x1F,0xF0,0xF8,0x80,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x0E,0x1F,0x80,0x0C,0x00,0x7E,0x70,0xDC,0x80,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x0F,0x07,0xE0,0x0C,0x01,0xE3,0x70,0x5C,0xC0,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x0F,0x81,0xF8,0x0C,0x07,0x83,0x70,0x5F,0xC0,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x0D,0xE0,0x7E,0x0C,0x1F,0xC1,0xF0,0x5F,0xC0,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x0F,0x78,0x3F,0x8C,0x7F,0xC1,0xB8,0x47,0xC0,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x0F,0x1E,0x0F,0xED,0xEC,0xE1,0xB8,0xC7,0xC0,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x1F,0x07,0x83,0xFF,0xC6,0xE1,0xB9,0xC7,0xC0,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x1F,0x07,0xE0,0xFF,0xE7,0xE1,0xBF,0x07,0xC0,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x1F,0x07,0xF8,0x3F,0xE7,0xE1,0xBF,0x07,0xC0,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x1F,0x07,0x9E,0x0C,0xE6,0xE1,0x8F,0x07,0xC0,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x1F,0x07,0x87,0x8C,0xE6,0xE3,0x8F,0x83,0xC0,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x1F,0x0F,0x81,0xED,0xE6,0xE7,0x0F,0x83,0xC0,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x1F,0x07,0x00,0x7F,0xE6,0xFE,0x0F,0x83,0x40,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x1F,0x00,0x00,0x1E,0x76,0xFE,0x0F,0x83,0x40,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x1E,0x00,0x00,0x00,0x72,0x1E,0x0F,0x83,0x40,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x1E,0x00,0x00,0x00,0x7E,0x1A,0x0F,0x83,0x40,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x1E,0x00,0x00,0x00,0x7C,0x1A,0x0F,0x83,0xC0,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x0C,0x00,0x00,0x00,0x78,0x1A,0x0F,0x83,0xC0,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x78,0x1A,0x07,0x83,0xC0,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7C,0x1E,0x07,0x80,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7C,0x1F,0x07,0x80,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7C,0x1F,0x07,0x80,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7C,0x1F,0x07,0x80,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7C,0x1F,0x07,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7C,0x1F,0x00,0x00,0x00,0x00,0x08,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7C,0x1F,0x00,0x00,0x00,0x00,0x10,0x00,0x40,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3C,0x1F,0x00,0x00,0x00,0x00,0xFF,0x08,0x20,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3C,0x1F,0x00,0x00,0x00,0x00,0x28,0x05,0xFC,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x34,0x0C,0x00,0x00,0x00,0x00,0x48,0x00,0x40,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x34,0x00,0x00,0x00,0x00,0x00,0xFF,0x08,0x88,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x34,0x00,0x00,0x00,0x00,0x00,0x08,0x05,0xFC,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3C,0x00,0x00,0x00,0x00,0x00,0x4A,0x00,0xA8,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3C,0x00,0x00,0x00,0x00,0x00,0x89,0x04,0xA8,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x10,0x00,0x00,0x00,0x00,0x01,0x08,0x88,0xA8,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x38,0x10,0xAA,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x01,0x2E,
- };
- /*
- ********************************************************************************
- ** 函数名称 : delay(unsigned char i)
- ** 函数功能 : 延时函数 这个延时程序的具体延时时间是time=i*8+10,适用于小于2ms的延时
- ********************************************************************************
- */
- void delay(unsigned int i)
- {
-
- while(i--);
-
-
- }
- /*
- ********************************************************************************
- ** 函数名称 : delay1ms(unsigned char i)
- ** 函数功能 : 延时函数
- ********************************************************************************
- */
- void delay1ms(unsigned int t)
- {
- //for(i=124;i>0;i--); //延时124*8+10=1002us
- unsigned char a,b,c;
- for(t;t>0;t--)
- for(c=1;c>0;c--)
- for(b=142;b>0;b--)
- for(a=2;a>0;a--);
- }
- /*
- ********************************************************************************
- ** 函数名称 : Init_DS18B20(void)
- ** 函数功能 : 初始化
- ********************************************************************************
- */
- void Init_DS18B20(void)
- {
- unsigned char x=0;
- DQ=1; //DQ先置高
- Delay10us(60);//最小480,最大960
- DQ=0; //发送复位脉冲
- Delay10us(60);//最小480,最大960
-
- DQ=1; //拉高数据线
- Delay10us(6);//15-60us
-
- // x=DQ; //用X的值来判断初始化有没有成功,18B20存在的话X=0,否则X=1
- //delay(20);
- while(DQ==0);
- DQ=1;//让传感器释放总线,避免影响下一步
- }
- /*
- ********************************************************************************
- ** 函数名称 : ReadOneChar()
- ** 函数功能 : 读一个字节
- ********************************************************************************
- */
- unsigned char ReadOneChar() //主机数据线先从高拉至低电平1us以上,再使数据线升为高电平,从而产生读信号
- {
- unsigned char dat=0;
- unsigned char mask,fmask;
- for(mask=0x01;mask!=0;mask<<=1)//一个周期需要至少60us,但采样要在15us内完成
- {
- DQ=0;
- _nop_();
- DQ=1;
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- if(DQ==0)//读0
- {
- fmask=~mask;
- dat=dat&fmask;
- }
- else
- {
- dat|=mask;
- }
- Delay10us(5);
- DQ=1;
- }
- return dat;
- }
- /*
- ********************************************************************************
- ** 函数名称 : WriteOneChar(unsigned char dat)
- ** 函数功能 : 写一个字节
- //一个写周期为60-120us,主机在15--45us内对信号采样
- ********************************************************************************
- */
- void WriteOneChar(unsigned char dat)
- {
- unsigned char i=0; //数据线从高电平拉至低电平,产生写起始信号。15us之内将所需写的位送到数据线上,
- for(i=8;i>0;i--) //在15~60us之间对数据线进行采样,如果是高电平就写1,低写0发生。
- {
- DQ=0; //在开始另一个写周期前必须有1us以上的高电平恢复期。
- DQ=dat&0x01;
- Delay10us(6);//15-60us采样
- DQ=1;
- dat>>=1;
- }
- delay(4);
- }
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
12864_18B20_TTP229.zip
(71.34 KB, 下载次数: 115)
|