- 打卡等级:以坛为家II
- 打卡总天数:524
- 最近打卡:2025-05-04 02:13:29
论坛元老
- 积分
- 5133
|
发表于 2024-5-16 13:27:30
|
显示全部楼层
3个 595,1个驱动数码管段选(a-g,p),2个驱动数码管位选(com1-16) 用扫描方式驱动
- /****************************************************************
- * 文件名:code.c
- * 说 明:Chip=STC89C52; Crystal=12MHz;12T
- * 功 能:3*74xx595 显示16数码管
- * 修 订:FreeFish 2019
- * 版 本:V0.1
- ****************************************************************/
- #include<reg52.h>
- sbit Dat=P2^0;
- sbit RCK=P2^2;
- sbit SCK=P2^3;
- unsigned char index,k;
- unsigned int position;
- unsigned char code table[]={
- 0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};
- unsigned char Dat_Buf[3]; //要送出的3个字节数据 (段数据-位数据-位数据)
- unsigned char Dat_Disp[16]; //要显示的16个字符。
- /****************************************************************
- * 函数功能:595写入数据
- ****************************************************************/
- void write595(unsigned char Data)
- {
- unsigned char i;
- for (i=0;i<8;i++)
- {
- Dat = ((Data & 0x80)==0x80)?1:0;
- Data = Data << 1;
- SCK = 1;
- SCK = 0;
- }
- }
- void InitTimer0(void) //1ms
- {
- TMOD = 0x01;
- TH0 = 0x0FC;
- TL0 = 0x18;
- EA = 1;
- ET0 = 1;
- TR0 = 1;
- }
- /****************************************************************
- * 函数功能:主函数
- ****************************************************************/
- void main()
- {
- InitTimer0();
- Dat_Disp[0]=3;
- Dat_Disp[1]=1;
- Dat_Disp[2]=4;
- Dat_Disp[3]=1;
- Dat_Disp[4]=5;
- Dat_Disp[5]=9;
- Dat_Disp[6]=2;
- Dat_Disp[7]=6;
- Dat_Disp[8]=5;
- Dat_Disp[9]=3;
- Dat_Disp[10]=5;
- Dat_Disp[11]=8;
- Dat_Disp[12]=9;
- Dat_Disp[13]=7;
- Dat_Disp[14]=9;
- Dat_Disp[15]=3;
- while(1)
- {
- if(index==0)
- Dat_Buf[0]=table[Dat_Disp[index]]-0x80;//装载段数据
- else
- Dat_Buf[0]=table[Dat_Disp[index]];//装载段数据
- position=1<<index;
- Dat_Buf[1]=position/256; //位选16-9
- Dat_Buf[2]=position%256; //位选8-1
- for (k=0;k<3;k++)
- {
- write595(Dat_Buf[k]); //
- }
- RCK=1; //更新数据
- RCK=0; //准备下一次
- //Delay(10); //点亮一段时间
- }
- }
- void Timer0Interrupt(void) interrupt 1 //1ms
- {
- TH0 = 0x0FC;
- TL0 = 0x18;
- //add your code here!
- index++;
- index=index%16;
- }
|
|