|
楼主 |
发表于 2014-8-11 09:26:37
|
显示全部楼层
本帖最后由 jufr12315 于 2014-8-11 09:27 编辑
上面是整个工程
下面的TM1628部分代码
- #include <main.H>
- #include <TM1628.H>
- sbit Pin_TM1628_STB1 = P1^2;
- sbit Pin_TM1628_CLK1 = P1^1;
- sbit Pin_TM1628_DIO1 = P1^0;
- sbit Pin_TM1628_STB2 = P2^7;
- sbit Pin_TM1628_CLK2 = P2^6;
- sbit Pin_TM1628_DIO2 = P2^5;
- uchar code ucTabBrightness[]={0x80,0x88,0x89,0x8a,0x8b,0x8c,0x8d,0x8e,0x8f};//显示亮度表
- unsigned char code ucTabCode[]=
- {
- 0x3F, // 0
- 0x06, // 1
- 0x5B, // 2
- 0x4F, // 3
- 0x66, // 4
- 0x6D, // 5
- 0x7D, // 6
- 0x07, // 7
- 0x7F, // 8
- 0x6F, // 9
- // 0xFF,//全亮 0x10
- 0x00,//全灭 0x11
- };
- //====================================================================
- //=== 功能变量定义 ===//
- uchar xdata dis_buf1[8]; //显示数据缓存区
- uchar xdata dis_buf11[8]; //显示数据缓存区
- uchar xdata dis_date1[8]; //段码数据缓存区
- uchar xdata dis_date11[8]; //段码数据缓存区
- uchar xdata dis_buf2[8]; //显示数据缓存区
- uchar xdata dis_buf22[8]; //显示数据缓存区
- uchar xdata dis_date2[8]; //段码数据缓存区
- uchar xdata dis_date22[8]; //段码数据缓存区
- uchar xdata key_tm[5]; //键值数据缓存区
- //====================================================================
- //=== TM16XX共阴码转共阳码函数 ===//
- //=== buf为准备转换的数据数组地址 ===//
- //=== date为转换后存储数组的数组地址 =//
- void A_To_K(unsigned char *buf,unsigned char *date)
- {
- uchar i,j,tmp,index;
- for(i=0;i<8;i++)
- {
- tmp=*(buf + i);
- for(j=0;j<8;j++)
- {
- if(i<8)
- index=j;
- else
- index=j+1;
- if(tmp&(1<<j))
- {
- *(date+index) |= (1<<i);
- }
- else
- {
- *(date+index) &=(~(1<<i));
- }
- }
- }
- }
- //====================================================================
- //=== TM1668驱动函数 ===//
- void WriteByte(uchar w_data,char flag) //写一字节数据
- {
- uchar i;
- if(1 == flag)
- {
- Pin_TM1628_STB1 = 0;
- for(i=8;i!=0;i--)
- {
- Pin_TM1628_CLK1 = 0;
- if(w_data&0x01)
- Pin_TM1628_DIO1 = 1;
- else
- Pin_TM1628_DIO1 = 0;
- Pin_TM1628_CLK1 = 1;
- w_data >>= 1;
- }
- }
- else
- {
- Pin_TM1628_STB2 = 0;
- for(i=8;i!=0;i--)
- {
- Pin_TM1628_CLK2 = 0;
- if(w_data&0x01)
- Pin_TM1628_DIO2 = 1;
- else
- Pin_TM1628_DIO2 = 0;
- Pin_TM1628_CLK2 = 1;
- w_data>>=1;
- }
- }
- }
- //== 数码管显示 flag :1->TM1628芯片1显示 2->TM1628芯片2显示
- //== Brighttness :亮度调节 级别 0-8
- void TM1628_Display(char flag,char Brightness) //写多字节数据
- {
- uchar i;
- if(1 == flag)
- {
- Pin_TM1628_CLK1 = 1;
- Pin_TM1628_DIO1 = 1;
- Pin_TM1628_STB1 = 1;
- WriteByte(0x03,1); //7位10段
- Pin_TM1628_STB1 = 1;
- WriteByte(0x44,1); //写数据
- Pin_TM1628_STB1 = 1;
- for(i=0;i<7;i++) //7个地址
- {
- WriteByte(0xc0+(i*2),1); //写地址
- WriteByte(dis_date1[i],1); //写显示数据到显存
- Pin_TM1628_STB1 = 1;
- }
- for(i=0;i<7;i++)
- {
- WriteByte(0xc0+(i*2)+1,1); //写地址
- WriteByte(dis_date11[i],1); //写显示数据到显存
- Pin_TM1628_STB1 = 1;
- }
- WriteByte(ucTabBrightness[Brightness],1); //开显示,亮度为10/16
- Pin_TM1628_STB1 = 1;
- }
- else
- {
- Pin_TM1628_CLK2 = 1;
- Pin_TM1628_DIO2 = 1;
- Pin_TM1628_STB2 = 1;
- WriteByte(0x03,2); //7位10段
- Pin_TM1628_STB2 = 1;
- WriteByte(0x44,2); //写数据
- Pin_TM1628_STB2 = 1;
- for(i=0;i<7;i++) //7个地址
- {
- WriteByte(0xc0+(i*2),2); //写地址
- WriteByte(dis_date2[i],2); //写显示数据到显存
- Pin_TM1628_STB2 = 1;
- }
- for(i=0;i<7;i++)
- {
- WriteByte(0xc0+(i*2)+1,2); //写地址
- WriteByte(dis_date22[i],2); //写显示数据到显存
- Pin_TM1628_STB2 = 1;
- }
- WriteByte(ucTabBrightness[Brightness],27); //开显示,亮度为10/16
- Pin_TM1628_STB2 = 1;
- }
- }
复制代码
- void main()
- {
- char i;
- uiCount = 0;
- uiTempCountStart = uiCount;
- uiTempCycles = 0;
- uiTempCountEnd = 500;
- Init();
- KeyInit();
- Write12887(0x0a,0x20); //开振荡器 第一次使用DS12C887和在仿真中 都必须打开
- Write12887(0x0B,0x06); //允许更新、设置格式为二进制,时钟为24小时制、开启闹钟
- Write12887(0x0B,0x86);
- Write12887(SEC_ADDR,0);
- Write12887(MIN_ADDR,30);
- Write12887(HOUR_ADDR,1);
- Write12887(WEEK_ADDR,5);
- Write12887(DAY_ADDR,8);
- Write12887(MON_ADDR,8);
- Write12887(YEAR_ADDR,14);
- Write12887(0x0B,0x06);
- Year = Read12887(YEAR_ADDR);
- Mon = Read12887(MON_ADDR);
- Day = Read12887(DAY_ADDR);
- Hour = Read12887(HOUR_ADDR);
- Min = Read12887(MIN_ADDR);
- Sec = Read12887(SEC_ADDR);
- Week = Read12887(WEEK_ADDR);
- DisCount();//转换存到数组
- for(i=0;i<8;i++)
- {
- dis_buf1[i]=ucTabCode[10];//清零
- dis_buf2[i]=ucTabCode[10];//清零
- dis_buf11[i]=ucTabCode[10];
- dis_buf22[i]=ucTabCode[10];
- }
- A_To_K(dis_buf1,dis_date1);//将共阴代码转换成共阳代码 芯片1的 SEG1-SEG8 8位数码管
- A_To_K(dis_buf11,dis_date11);// 芯片1的 SEG9-SEG10 2位数码管
- A_To_K(dis_buf2,dis_date2);//将共阴代码转换成共阳代码
- A_To_K(dis_buf22,dis_date22);
- TM1628_Display(1,1);//写显示数据到TM16**显存中
- TM1628_Display(2,1);
- [color=Magenta]//上面两个显示函数屏蔽,主循环时间读取是能正常工作的,因为时间读取中每次秒位变化,我就点亮led半秒,就是时钟的冒号闪烁
- //不屏蔽的话,led就不会闪烁,要么是不会进入扫描读取里面,要么就是时间没变化。但是这两个显示函数看不出影响到哪里,有时屏蔽一个显示函数led能闪烁 [/color]
- while(1)
- {
- // 时间读取
- if(uiCount >= uiReadCountStart)
- uiReadCycles = uiCount - uiReadCountStart;
- else
- uiReadCycles = 0xffff - uiReadCountStart + uiCount;
- if(uiReadCycles > 10) //10ms
- {
- Conversion();
- SecBuf = Sec;
- Sec = Read12887(SEC_ADDR);
- if(SecBuf != Sec)
- bSecFlag = 1;
- Year = Read12887(YEAR_ADDR);
- Mon = Read12887(MON_ADDR);
- Day = Read12887(DAY_ADDR);
- Hour = Read12887(HOUR_ADDR);
- Min = Read12887(MIN_ADDR);
- Sec = Read12887(SEC_ADDR);
- Week = Read12887(WEEK_ADDR);
- M_Year = Year;
- M_Mon = Mon;
- M_Day = Day;
- uiReadCountStart = uiCount;
- uiReadCycles = 0;
- }
- // 按键扫描
- if(uiCount >= uiKeyCountStart)
- uiKeyCycles = uiCount - uiKeyCountStart;
- else
- uiKeyCycles = 0xffff - uiKeyCountStart + uiCount;
- if(uiKeyCycles > 10) //20ms
- {
- Key(ReadKey());
- uiKeyCountStart = uiCount;
- uiKeyCycles = 0;
- }
- // 温度读取
- if(uiCount >= uiTempCountStart)
- uiTempCycles = uiCount - uiTempCountStart;
- else
- uiTempCycles = 0xffff - uiTempCountStart + uiCount;
- if(uiTempCycles > uiTempCountEnd) //20ms
- {
- if(bTempConverFlag)
- {
- bTempConverFlag = 0;
- ConversionTemp();
- uiTempCountStart = uiCount;
- uiTempCountEnd = 10;//10 * 2 =20ms
- uiTempCycles = 0;
- }
- else
- {
- bTempConverFlag = 1;//启动转换
- Temp = GetTemp();
- uiTempCountStart = uiCount;
- uiTempCountEnd = 500;//500 * 2 = 1000ms
- uiTempCycles = 0;
- }
- }
- DisCount();
- }
- }
- void T0_ISR(void) interrupt 1
- {
- TH0 = ucTh;
- TL0 = ucTl;
- uiCount++;
- if(bSecFlag)
- {
- Pin_Led = 0;
- if(++Count500Ms == 250)//亮500MS
- {
- Pin_Led = 1;
- Count500Ms = 0;
- bSecFlag = 0;
- }
- }
-
- }
复制代码 |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?注册
x
|