123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744 |
- #include "HT16C22.h"
- #include "math.h"
- // #define cs _pa1 //ht1621
- // #define wr _pa2
- // #define dat _pa3
- #define BIAS 0x29 // 0x29//0x50// 1/3 bias 4 com-原28
- #define SYSEN 0x01 // 0X02//Turn on system oscillator振荡
- #define LCDON 0x03 // 0x06//Turn on LCD bias generator偏压发生器
- #define LCDOFF 0x02 // 0x04//Turn off LCD bias generator
- #define RC256 0x18
- // 增加变量定义,add by qys
- void IIC_delay(void)
- {
- u8 i = 400; // 这里可以优化速度 ,经测试最低到5还能写入
- while (i)
- {
- i--;
- }
- }
- void Delay_Ms()
- {
- u16 i, j;
- for (i = 0; i < 500; i++)
- for (j = 0; j < 700; j++)
- ;
- }
- void Delay_SS()
- {
- u16 i, j;
- for (i = 0; i < 2000; i++)
- for (j = 0; j < 1000; j++)
- ;
- }
- u8 OD_Value, OE_Value, OF_Value, IO_Value, II_Value, IE_Value, IS_Value;
- static uint16_t ram[4] = {0X00000000,0X00000000,0X00000000,0X00000000};
- uint16_t ram1 = 0X00000000;
- uint16_t ram2 = 0X00000000;
- uint16_t ram3 = 0X00000000;
- uint16_t ram4 = 0X00000000;
- /*uint8_t LcdDisplayData[4][22] = {{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
- {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
- {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
- {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}};
- uint8_t LcdDisplayData[4][22] = {{0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01},
- {0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01},
- {0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01},
- {0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01}
- };*/
- void DELAY(unsigned short i)
- {
- unsigned short j;
- for (j = 0; j < i; j++)
- Delay_Ms(10); // Delay i*10mS
- }
- void delay(uint8_t nun)
- {
- uint8_t i;
- for (i = 0; i < nun; i++)
- {
- __nop();
- }
- }
- void sendbit_high(uint8_t data, uint8_t cnt) // 传地址,高六位
- {
- uint8_t i;
- for (i = 0; i < cnt; i++)
- {
- CLR_LCD_WR();
- if ((data & 0x80) == 0)
- CLR_LCD_DATA();
- else
- SET_LCD_DATA();
- IIC_delay();
- //__nop();
- // delay(10); // 防止tCLK太短
- SET_LCD_WR();
- data <<= 1;
- }
- }
- void sendbit_low(uint8_t data, uint8_t cnt) // 传送数据,低四位
- {
- uint8_t i;
- for (i = 0; i < cnt; i++)
- {
- CLR_LCD_WR();
- if ((data & 0x01) == 0)
- CLR_LCD_DATA();
- else
- SET_LCD_DATA();
- IIC_delay();
- //__nop();
- // delay(10); // 防止tCLK太短
- SET_LCD_WR();
- data >>= 1;
- }
- }
- void sendcmd(uint8_t command) // 写命令
- {
- CLR_LCD_CS();
- sendbit_high(0x80, 3);
- sendbit_high(command, 8);
- sendbit_high(0x0, 1);
- SET_LCD_CS();
- }
- void write_1621(uint8_t addr, uint8_t data) // 写地址和数据
- {
- CLR_LCD_CS();
- sendbit_high(0xa0, 3);
- sendbit_high(addr << 2, 6);
- sendbit_low(data, 4);
- SET_LCD_CS();
- }
- void LCD_init(void)
- {
- char i;
- sendcmd(RC256);
- sendcmd(BIAS);
- sendcmd(SYSEN);
- sendcmd(LCDON);
- for (i = 0; i < 12; i++) // all the lights bright
- {
- write_1621(i, 0x00);
- // delay(5);
- }
- }
- /************************LCDkai****************************/
- //////////////////////////////////////////////////////////////////////////
- // 函数名 : IIC_Init
- // 功能 : 初始化i2c
- // 参数 : void
- // 作者 : wit_yuan
- // 时间 : 2014-11-07
- ////////////////////////////////////////////////////////////////////////////
- void IIC_Init(void)
- {
- /* enable the User key clock */
- rcu_periph_clock_enable(RCU_GPIOA);
- rcu_periph_clock_enable(RCU_GPIOB);
- rcu_periph_clock_enable(RCU_AF);
- gpio_pin_remap_config(GPIO_SWJ_NONJTRST_REMAP, ENABLE);
- gpio_pin_remap_config(GPIO_SWJ_SWDPENABLE_REMAP, ENABLE);
- gpio_init(GPIOB, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_4);
- gpio_init(GPIOA, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_0);
- gpio_bit_set(GPIOB, GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_4);
- SET_LCD_BL_LIGHT();
- // gpio_bit_set(GPIOC, GPIO_PIN_12);
- /*GPIO_InitTypeDef GPIO_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
- GPIO_StructInit(&GPIO_InitStructure);
- GPIO_InitStructure.GPIO_PIN = GPIO_PIN_6 | GPIO_PIN_7;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- GPIO_SetBits(GPIOB, GPIO_PIN_6|GPIO_PIN_7);*/
- }
- void LCDProcess()
- {
- ram[0]=ram1;
- ram[1]=ram2;
- ram[2]=ram3;
- ram[3]=ram4;
- char i, j;
- uint8_t data = 0;
- for (j = 0; j < 12; j++) // all the lights bright
- {
- for (i = 0; i < 4; i++)
- {
- if (((ram[i]>>j )&0x01)== 0x01)
- {
- if (i == 0)
- {
- data = data | 0x01;
- }
- else if (i == 1)
- {
- data = data | 0x02;
- }
- else if (i == 2)
- {
- data = data | 0x04;
- }
- else if (i == 3)
- {
- data = data | 0x08;
- }
- else
- {
- }
- }
- }
-
- write_1621(j, data);
- data = 0;
- // delay(5);
- }
- }
- /**
- * @brief ht16c22初始化
- * @param 无
- * @retval 无
- */
- void ht16c22_init(void)
- {
- int i = 0;
- IIC_Init();
- LCD_init();
- //Display_temp(123,1);
- //DH_ON;
- //Display_num(2, 2);
- //Display_num(3, 1);
- }
-
- /**
- * @brief 给ht16c22的addr 写dat
- * @param addr:地址,dat:数据
- * @retval 无
- */
- void ht16c22_display(uint8_t add,uint8_t ram_num,uint16_t ram_temp)
- {
- ram[0]=ram1;
- ram[1]=ram2;
- ram[2]=ram3;
- ram[3]=ram4;
- char i, j;
- uint8_t data = 0;
- for (i = 0; i < 4; i++)
- {
- if (((ram[i]>>add )&0x01)== 0x01)
- {
- if (i == 0)
- {
- data = data | 0x01;
- }
- else if (i == 1)
- {
- data = data | 0x02;
- }
- else if (i == 2)
- {
- data = data | 0x04;
- }
- else if (i == 3)
- {
- data = data | 0x08;
- }
- else
- {
- }
- }
- write_1621(add, data);
- // delay(5);
- }
- }
- /**
- * @brief 清空液晶屏显示的内容
- * @param 无
- * @retval 无
- */
- void ht16c22_clear(void)
- {
- char i;
- for (i = 0; i < 12; i++) // all the lights bright
- {
-
- write_1621(i, 0x00);
- // delay(5);
- }
- ram1 = 0X00000000;
- ram2 = 0X00000000;
- ram3 = 0X00000000;
- ram4 = 0X00000000;
- }
- /**
- * @brief 液晶屏第wei位数码管,显示Open_animation[num]数据
- * @param wei:液晶屏位 num:Open_animation数组的数据
- * @retval 无
- */
- char Open_animation[] = {0, 0, 0x04, 0x24, 0xA4, 0xA6, 0xA7, 0xB7, 0xF7};
- static void Display_animation(u8 wei, u8 num)
- {
- }
- /**
- * @brief 开机动画
- * @param 无
- * @retval 无
- */
- void Animation(void)
- {
- }
- /**
- * @brief 字母显示,第wei位,显示Letters[num]“ABCDEF”
- * @param wei:位号 num:字母
- * @retval 无
- */
- char Letters[] = {0x77, 0xE6, 0xF0, 0xA7, 0xF4, 0x74, 0xD6, 0x04, 0, 0xff}; // abcdefs- 空白 全填满
- void Dis_letter(u8 wei, u8 num)
- {
- }
- /**
- * @brief 液晶屏第wei位数码管,显示LCD_Table[num]数据,该数值存放“1.2.3.4..........”
- * @param wei:液晶屏位 num:LCD_Table数组的数据
- * @retval 无
- */
- char LCD_Table[] = {0xF3, 0x03, 0xB5, 0x97, 0x47, 0xD6, 0xF6, 0x13, 0xF7, 0xD7, 0xF0};
- void Display_num(u8 wei, u8 num)
- {
- if (wei == 1)
- {
- switch (num)
- {
- case 0:
- ram4 |= 0x0004;
- ram4 |= 0x0800;
- ram3 &= ~ 0x0004;
- ram3 |= 0x0800;
- ram2 |= 0x0004;
- ram2 |= 0x0800;
- ram1 |= 0x0800;
- break;
- case 1:
-
- ram4 &= ~ 0x0004;
- ram4 &= ~0x0800;
- ram3 &= ~ 0x0004;
- ram3 |= 0x0800;
- ram2 &= ~0x0004;
- ram2 |= 0x0800;
- ram1 &= ~ 0x0800;
- break;
- case 2:
- ram4 &= ~ 0x0004;
- ram4 |= 0x0800;
- ram3 |= 0x0004;
- ram3 |= 0x0800;
- ram2|= 0x0004;
- ram2 &= ~0x0800;
- ram1 |= 0x0800;
- break;
- case 3:
- ram4 &= ~0x0004;
- ram4 |= 0x0800;
- ram3 |= 0x0004;
- ram3 |= 0x0800;
- ram2 &= ~ 0x0004;
- ram2 |= 0x0800;
- ram1 |= 0x0800;
- break;
- case 4:
- ram4|=0x0004;
- ram4 &= ~ 0x0800;
- ram3 |= 0x0004;
- ram3 |= 0x0800;
- ram2 &= ~ 0x0004;
- ram2 |=0x0800;
- ram1 &= ~ 0x0800;
- break;
- case 5:
- ram4 |= 0x0004;
- ram4 |= 0x0800;
- ram3 |=0x0004;
- ram3 &= ~0x0800;
- ram2 &= ~0x0004;
- ram2 |=0x0800;
- ram1 |=0x0800;
- break;
- case 6:
- ram4 |= 0x0004;
- ram4 |= 0x0800;
- ram3 |=0x0004;
- ram3 &= ~ 0x0800;
- ram2 |= 0x0004;
- ram2 |= 0x0800;
- ram1 |= 0x0800;
- break;
- case 7:
- ram4 &= ~ 0x0004;
- ram4 |=0x0800;
- ram3 &= ~ 0x0004;
- ram3 |=0x0800;
- ram2 &= ~ 0x0004;
- ram2|=0x0800;
- ram1 &= ~ 0x0800;
- break;
- case 8:
- ram4 |= 0x0004;
- ram4 |= 0x0800;
- ram3 |= 0x0004;
- ram3 |= 0x0800;
- ram2 |= 0x0004;
- ram2 |= 0x0800;
- ram1 |= 0x0800;
- break;
- case 9:
- ram4 |= 0x0004;
- ram4 |= 0x0800;
- ram3 |= 0x0004;
- ram3 |= 0x0800;
- ram2 &= ~ 0x0004;
- ram2 |=0x0800;
- ram1 |= 0x0800;
- /* case 10:
- ram4 |= 0x0001;
- ram4 |= 0x0200;
- ram3 &= ~0x0001;
- ram3 |= 0x0200;
- ram2 &= ~0x0001;
- ram2 |= 0x0800;
- ram1 |= 0x0001;*/
- break;
- default:
- break;
- }
- }
- else if (wei == 2)
- {
-
- switch (num)
- {
- case 0:
- ram4 |= 0x0400;
- ram4 |= 0x0200;
- ram3 &= ~ 0x0400;
- ram3|= 0x0200;
- ram2 |= 0x0400;
- ram2 |= 0x0200;
- ram1 |= 0x0200;
- break;
- case 1:
- ram4 &= ~0x0400;
- ram4 &= ~0x0200;
- ram3 &=~0x0400;
- ram3|= 0x0200;
- ram2 &=~ 0x0400;
- ram2 |=0x0200;
- ram1 &= ~ 0x0200;
- break;
- case 2:
- ram4 &= ~ 0x0400;
- ram4|=0x0200;
- ram3 |= 0x0400;
- ram3 |= 0x0200;
- ram2 |=0x0400;
- ram2 &= ~ 0x0200;
- ram1 |= 0x0200;
- break;
- case 3:
- ram4 &= ~0x0400;
- ram4 |= 0x0200;
- ram3 |= 0x0400;
- ram3 |= 0x0200;
- ram2 &= ~ 0x0400;
- ram2 |= 0x0200;
- ram1 |= 0x0200;
- break;
- case 4:
- ram4|=0x0400;
- ram4 &= ~ 0x0200;
- ram3 |= 0x0400;
- ram3 |= 0x0200;
- ram2 &= ~ 0x0400;
- ram2 |=0x0200;
- ram1 &= ~ 0x0200;
- break;
- case 5:
- ram4 |= 0x0400;
- ram4 |= 0x0200;
- ram3 |=0x0400;
- ram3 &= ~0x0200;
- ram2 &= ~0x0400;
- ram2 |=0x0200;
- ram1 |=0x0200;
- break;
- case 6:
- ram4 |= 0x0400;
- ram4 |= 0x0200;
- ram3 |=0x0400;
- ram3 &= ~ 0x0200;
- ram2 |= 0x0400;
- ram2 |= 0x0200;
- ram1 |= 0x0200;
- break;
- case 7:
- ram4 &= ~ 0x0400;
- ram4 |=0x0200;
- ram3 &= ~ 0x0400;
- ram3 |=0x0200;
- ram2 &= ~ 0x0400;
- ram2|=0x0200;
- ram1 &= ~ 0x0200;
- break;
- case 8:
- ram4 |= 0x0400;
- ram4 |= 0x0200;
- ram3 |= 0x0400;
- ram3 |= 0x0200;
- ram2 |= 0x0400;
- ram2 |= 0x0200;
- ram1 |= 0x0200;
- break;
- case 9:
- ram4 |= 0x0400;
- ram4 |= 0x0200;
- ram3 |= 0x0400;
- ram3 |= 0x0200;
- ram2 &= ~ 0x0400;
- ram2 |=0x0200;
- ram1 |= 0x0200;
- break;
- default:
- break;
- }
- }
- else if (wei == 3)
- {
- switch (num)
- {
- case 0:
- ram4 |= 0x0080;
- ram4 |= 0x0040;
- ram3 &= ~ 0x0080;
- ram3|= 0x0040;
- ram2 |= 0x0080;
- ram2 |= 0x0040;
- ram1 |= 0x0040;
- break;
- case 1:
- ram4 &= ~0x0080;
- ram4 &= ~0x0040;
- ram3 &=~0x0080;
- ram3|= 0x0040;
- ram2 &=~ 0x0080;
- ram2 |=0x0040;
- ram1 &= ~ 0x0040;
- break;
- case 2:
- ram4 &= ~ 0x0080;
- ram4|=0x0040;
- ram3 |= 0x0080;
- ram3 |= 0x0040;
- ram2 |=0x0080;
- ram2 &= ~ 0x0040;
- ram1 |= 0x0040;
- break;
- case 3:
- ram4 &= ~0x0080;
- ram4 |= 0x0040;
- ram3 |= 0x0080;
- ram3 |= 0x0040;
- ram2 &= ~ 0x0080;
- ram2 |= 0x0040;
- ram1 |= 0x0040;
- break;
- case 4:
- ram4|=0x0080;
- ram4 &= ~ 0x0040;
- ram3 |= 0x0080;
- ram3 |= 0x0040;
- ram2 &= ~ 0x0080;
- ram2 |=0x0040;
- ram1 &= ~ 0x0040;
- break;
- case 5:
- ram4 |= 0x0080;
- ram4 |= 0x0040;
- ram3 |=0x0080;
- ram3 &= ~0x0040;
- ram2 &= ~0x0080;
- ram2 |=0x0040;
- ram1 |=0x0040;
- break;
- case 6:
- ram4 |= 0x0080;
- ram4 |= 0x0040;
- ram3 |=0x0080;
- ram3 &= ~ 0x0040;
- ram2 |= 0x0080;
- ram2 |= 0x0040;
- ram1 |= 0x0040;
- break;
- case 7:
- ram4 &= ~ 0x0080;
- ram4 |=0x0040;
- ram3 &= ~ 0x0080;
- ram3 |=0x0040;
- ram2 &= ~ 0x0080;
- ram2|=0x0040;
- ram1 &= ~ 0x0040;
- break;
- case 8:
- ram4 |= 0x0080;
- ram4 |= 0x0040;
- ram3 |= 0x0080;
- ram3 |= 0x0040;
- ram2 |= 0x0080;
- ram2 |= 0x0040;
- ram1 |= 0x0040;
- break;
- case 9:
- ram4 |= 0x0080;
- ram4 |= 0x0040;
- ram3 |= 0x0080;
- ram3 |= 0x0040;
- ram2 &= ~ 0x0080;
- ram2 |=0x0040;
- ram1 |= 0x0040;
- break;
- default:
- break;
- }
- }
- LCDProcess();
- }
- /**
- * @brief 多个位显示LCD_Table[num]数据,该数值存放“1.2.3.4..........”
- * @param num:LCD_Table数组的数据
- * @retval 无
- */
- // 做了最后位四舍五入 以及 百位为0 不显示的操作
- void Display_Multidigit(u16 num) // 显示多位数,不能超过四位
- {
- u8 ge,shi,bai;
- num = (u16)( ((float) num/10) +0.5);
- num = num *10;
- bai = num%1000/100;
- shi = num%100/10;
- ge = num%10;
- if(bai == 0)
- {
- //Dis_letter(1,8);
- }
- else
- {
- Display_num(1, bai);
- }
- Display_num(2, shi);
- Display_num(3, ge);
- }
- // 显示实时 正温度 不四舍五入
- void Display_R_Multidigit(int16_t num)
- {
- u8 ge,shi,bai;
- //求绝对值变为正的
- num = abs(num);
- bai = num%1000/100;
- shi = num%100/10;
- ge = num%10;
- if(bai == 0)
- {
- Dis_letter(1,8);
- }
- else
- {
- Display_num(1, bai);
- }
- Display_num(2, shi);
- Display_num(3, ge);
- }
- void Display_F_Multidigit(int16_t num) // 显示负温度 不四舍五入
- {
- }
- // 显示温度界面 摄氏度
- /*
- type 0 表示设置温度 四舍五入
- 1 表示实时温度 无四舍五入 有正 有负
- */
- void Display_temp(int16_t temp, uint8_t type) // 温度显示
- {
- if(type == 0)
- {
- Display_Multidigit((uint16_t)temp);
- DH_ON;
- }
- //实时温度
- else if(type == 1)
- {
- if(temp >= 0)
- {
- Display_R_Multidigit((uint16_t)temp);
- DH_ON;
- }
- else
- //小于0
- {
- Display_F_Multidigit(temp);
- //小数点关闭
- DH_OFF;
- }
- }
- //摄氏度 C
- // DU_ON;
- // Display_num(4, 10);
-
- }
- /*
- *没在用
- */
- // 显示温度界面 华氏度 //没在用
- // 输入为 *10 的温度
- void Display_H_temp(int16_t temp) // 温度显示
- {
-
- }
- /* @brief 湿度显示界面
- * @param hum:湿度
- * @retval 无
- */
- void Dis_hum(u8 hum)
- {
- }
|