本帖最后由 jxc827 于 2012-6-23 16:40 编辑
18b20程序参考本版例程,1602使用4位总线操作,只是在8位基础上稍微修改。
程序功能将18b20采集的温度显示到1602上。
具体接线如下:
RS ->GPB8;
R/W ->GPC6;
E ->GPC7;
DB4 ->GPA4;
DB5 ->GPA5;
DB6 ->GPA6;
DB7 ->GPA7;
main.c
- #include "includes.h"
- uint8_t table[]="Tempratrue:";
- uint16_t tmp = 0;
- uint8_t digit_hundred, digit_ten, digit_unit;
- int main (void)
- {
- UNLOCKREG();
- DrvSYS_SetOscCtrl(E_SYS_XTL12M, 1); //使用外部12M晶振
- delay_ms(100); //等待外部晶振就绪
- LOCKREG();
- LCD_GPIO_initial(); //1602 相关GPIO初始化
- LCD_initial(); //1602 模式初始化
- LCD_WriteString(2,0,table); //显示第一行文字
- while(1)
- {
- tmp = read_temp(); //读18B20温度
- digit_hundred = tmp/100;
- digit_ten = tmp%100/10;
- digit_unit = tmp%10;
- LCD_WriteChar(0,1,digit_hundred + 0x30); //显示十位
- LCD_WriteChar(1,1,digit_ten + 0x30); //显示个位
- LCD_WriteChar(2,1,'.'); //显示小数点
- LCD_WriteChar(3,1,digit_unit + 0x30); //显示小数点后一位
- LCD_WriteChar(4,1,0xdf); //显示摄氏度单位
- LCD_WriteChar(5,1,'C');
- delay_ms(10);
- }
- }
LCD1602.c
- /**************************************************
- ** 文件名称:lcd1602.c
- ** 文件说明:lcd1602液晶显示,4位数据总线
- ** 管脚分配:
- ** GPA4:DB4
- ** GPA5:DB5
- ** GPA6:DB6
- ** GPA7:DB7
- ** GPB8:RS
- ** GPC6:RW
- ** GPC7:EN
- **************************************************/
- #include "lcd1602.h"
- void LCD_GPIO_initial(void)
- {
- DrvGPIO_Open(LCD_DB4,E_IO_OUTPUT);
- DrvGPIO_Open(LCD_DB5,E_IO_OUTPUT);
- DrvGPIO_Open(LCD_DB6,E_IO_OUTPUT);
- DrvGPIO_Open(LCD_DB7,E_IO_OUTPUT);
- DrvGPIO_Open(LCD_RS,E_IO_OUTPUT);
- DrvGPIO_Open(LCD_RW,E_IO_OUTPUT);
- DrvGPIO_Open(LCD_EN,E_IO_OUTPUT);
- }
- void delay_us(uint32_t t) //延时
- {
- uint32_t i=0;
- while(t--)
- {
- for (i=0;i<1;i++);
- }
- }
- void delay_ms(uint32_t a)
- {
- uint32_t j=0;
- while(a--)
- {
- for (j=0;j<1000;j++);
- }
- }
- /******************************************************************
- ** 函数名称:LCD_command_write
- ** 函数说明:液晶写指令子程序
- ** 输入参数:com
- ** 输出参数:无
- ** 返回值: 无
- ** 其他说明:EN=1,RS=0,RW=0,分两次将DB0-DB7的指令代码写入指令寄存器中
- *******************************************************************/
- void LCD_command_write(uint8_t com)
- {
- CLR_EN;
- CLR_RS;
- CLR_RW;
- delay_us(5);
- SET_EN;
- delay_us(5);
- GPIOA -> DOUT = com & 0xf0; //写入高4位
- delay_us(5);
- CLR_EN;
- delay_us(5);
- SET_EN;
- GPIOA -> DOUT = (com << 4) & 0xf0; //写入低4位
- delay_us(5);
- CLR_EN;
- delay_us(5);
- }
- /************************************************************
- ** 函数名称:LCD_data_write
- ** 函数说明:液晶写数据子程序
- ** 输入参数:dat
- ** 输出参数:无
- ** 返回值: 无
- ** 其他说明:EN=0,RS=1,RW=0,将DB0-DB7数据写入数据寄存器中
- *************************************************************/
- void LCD_data_write(uint8_t dat)
- {
- CLR_EN;
- delay_us(5);
- SET_RS;
- CLR_RW;
- delay_us(5);
- SET_EN;
- GPIOA -> DOUT = dat & 0xf0; //写入高4位
- delay_us(5);
- CLR_EN;
- delay_us(5);
- SET_EN;
- GPIOA -> DOUT = (dat << 4)&0xf0; //写入低4位
- delay_us(5);
- CLR_EN;
- delay_us(5);
- }
- void LCD_initial(void) //液晶初始化
- {
- LCD_command_write(0x28); //4位数据总线,2行显示模式,5*7点阵+光标显示模式
- delay_us(5);
- LCD_command_write(0x0c); //开显示,显示光标,不闪烁
- LCD_command_write(0x01); //清显示
- }
- void LCD_WriteString(uint8_t x,uint8_t y,uint8_t *String)
- {
- LCD_SetXY(x,y);
- delay_ms(10); //加等待
- while(*String) //判断是否已经写完
- {
- LCD_data_write(*String++); // 写入当前指向的字符,并将指针指向下一个字符
- delay_ms(10);
- }
- }
- void LCD_SetXY(uint8_t x,uint8_t y)
- {
- if(y == 0)
- {
- LCD_command_write(0x80 + x); // 第一行
- }
- else
- {
- LCD_command_write(0xc0 + x); // 第二行
- }
- }
- void LCD_WriteChar(uint8_t x,uint8_t y,uint8_t data)
- {
- LCD_SetXY(x,y);
- delay_ms(10); //加等待
- LCD_data_write(data);
- }
18b20.c代码就不贴了,参考版上例程。
常温测试效果:
开发板放在电脑风扇出口:
完整的工程包:
祝大家端午节快乐!
完。
|