我是新手,最近做个函数信号发生器,需要产生各种波形,还有个就是做个闹钟,
我用的是DS1302,TLV5618和数码管。
DS1302在PROTEUS上面仿真能运行,但是在实物上面就不能实现,数码管数字不变,不知道什么原因,希望高手帮忙看下,谢谢,
#include <AT89X51.H>
#include <intrins.h>
#include <math.h>
#define uchar unsigned char
#define uint unsigned int
#define Fosc 12000000
#define ALL 65535
sbit DIN=P3^7; //数据输入端
sbit SCLK=P3^6; //时钟信号
sbit CS=P3^5; //片选输入端,低电平有效
sbit DS1302_CLK=P3^0;
sbit DS1302_IO=P3^1;
sbit DS1302_RST=P3^2;
sbit RING = P3^7;
sbit ACC0=ACC^0;
sbit ACC7=ACC^7;
sbit qian=P2^4;
sbit bai=P2^5;
sbit shi=P2^6;
sbit ge=P2^7;
sfr smdis = 0x80;
sfr p2m1 = 0x95;
sfr p2m0 = 0x96;
sfr p1m1 = 0x91;
sfr p1m0 = 0x92;
unsigned char clk_time[8]={0x47,0x49,0x23,0x9,0x12,0x03,0x09,0x20};
code unsigned char seg[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
code unsigned char seg7code[14]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0X76};
code unsigned char seg2[]={0x00,0x01,0x02,0x03,0x04,0x04,0x06,0x07,0x08,0x09,0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x20,0x21,0x22,0x23};
unsigned char code tosin[256]={
0x800,0x830,0x860,0x890,0x8d0,0x900,0x930,0x960,0x990,0x9c0,0x9f0,0xa20,0xa50,0xa80,0xab0,0xae0,
0xb10,0xb40,0xb70,0xba0,0xbc0,0xbf0,0xc20,0xc50,0xc70,0xca0,0xcc0,0xcf0,0xd10,0xd40,0xd60,0xd80,
0xda0,0xdd0,0xdf0,0xe10,0xe30,0xe50,0xe70,0xe90,0xea0,0xec0,0xee0,0xef0,0xf10,0xf20,0xf40,0xf50,
0xf60,0xf70,0xf80,0xf90,0xfa0,0xfb0,0xfc0,0xfd0,0xfd0,0xfe0,0xff0,0xff1,0xff5,0xff8,0xfff,0xfff,
0xfff,0xfff,0xff8,0xff5,0xff1,0xff0,0xfe0,0xfd0,0xfd0,0xfc0,0xfb0,0xfa0,0xf90,0xf80,0xf70,0xf60,
0xf50,0xf40,0xf20,0xf10,0xef0,0xee0,0xec0,0xea0,0xe90,0xe70,0xe50,0xe30,0xe10,0xde0,0xdd0,0xda0,
0xd80,0xd60,0xd40,0xd10,0xcf0,0xcc0,0xca0,0xc70,0xc50,0xc20,0xbf0,0xbc0,0xba0,0xb70,0xb40,0xb10,
0xae0,0xab0,0xa80,0xa50,0xa20,0x9f0,0x9c0,0x990,0x960,0x930,0x900,0x8d0,0x890,0x860,0x830,0x800,
0x800,0x7c0,0x790,0x760,0x720,0x6f0,0x6c0,0x690,0x660,0x630,0x600,0x5d0,0x5a0,0x570,0x550,0x510,
0x4e0,0x4c0,0x480,0x450,0x430,0x400,0x3d0,0x3a0,0x380,0x350,0x330,0x300,0x2e0,0x2b0,0x290,0x270,
0x250,0x220,0x200,0x1e0,0x1c0,0x1a0,0x180,0x160,0x150,0x130,0x110,0x100,0x0e0,0x0d0,0x0b0,0x0a0,
0x090,0x080,0x070,0x060,0x050,0x040,0x030,0x020,0x020,0x010,0x000,0x000,0x000,0x000,0x000,0x000,
0x000,0x000,0x000,0x000,0x000,0x000,0x010,0x020,0x020,0x030,0x040,0x050,0x060,0x070,0x080,0x090,
0x0a0,0x0b0,0x0d0,0x0e0,0x100,0x110,0x130,0x150,0x160,0x180,0x1a0,0x1c0,0x1e0,0x200,0x220,0x250,
0x270,0x290,0x2b0,0x2e0,0x300,0x330,0x350,0x380,0x3a0,0x3d0,0x400,0x430,0x450,0x480,0x4c0,0x4e0,
0x510,0x550,0x570,0x5a0,0x5d0,0x600,0x630,0x660,0x690,0x6c0,0x6f0,0x720,0x760,0x790,0x7c0,0x800};
unsigned char code tosqu[256]={
0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,
0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,
0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,
0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,
0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,
0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,
0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,
0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,0xfff,
0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,
0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,
0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,
0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,
0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,
0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,
0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,
0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000};
unsigned char code totri[256]={
0x000,0x020,0x040,0x060,0x080,0x0a0,0x0c0,0x0e0,0x100,0x120,0x140,0x160,0x180,0x1a0,0x1c0,0x1e0,
0x200,0x220,0x240,0x260,0x280,0x2a0,0x2c0,0x2e0,0x300,0x320,0x340,0x360,0x380,0x3a0,0x3c0,0x3e0,
0x400,0x420,0x440,0x460,0x480,0x4a0,0x4c0,0x4e0,0x500,0x520,0x540,0x560,0x580,0x5a0,0x5c0,0x5e0,
0x600,0x620,0x640,0x660,0x680,0x6a0,0x6c0,0x6e0,0x700,0x720,0x740,0x760,0x780,0x7a0,0x7c0,0x7e0,
0x800,0x820,0x840,0x860,0x880,0x8a0,0x8c0,0x8e0,0x900,0x920,0x940,0x960,0x980,0x9a0,0x9c0,0x9e0,
0xa00,0xa20,0xa40,0xa60,0xa80,0xaa0,0xac0,0xae0,0xb00,0xb20,0xb40,0xb60,0xb80,0xba0,0xbc0,0xbe0,
0xc00,0xc20,0xc40,0xc60,0xc80,0xca0,0xcc0,0xce0,0xd00,0xd20,0xd40,0xd60,0xd80,0xda0,0xdc0,0xde0,
0xe00,0xe20,0xe40,0xe60,0xe80,0xea0,0xec0,0xee0,0xf00,0xf20,0xf40,0xf60,0xf80,0xfa0,0xfcf,0xfff,
0xfff,0xfcf,0xfa0,0xf80,0xf60,0xf40,0xf20,0xf00,0xee0,0xec0,0xea0,0xe80,0xe60,0xe40,0xe20,0xe00,
0xde0,0xdc0,0xda0,0xd80,0xd60,0xd40,0xd20,0xd00,0xce0,0xcc0,0xca0,0xc80,0xc60,0xc40,0xc20,0xc00,
0xbe0,0xbc0,0xba0,0xb80,0xb60,0xb40,0xb20,0xb00,0xae0,0xac0,0xaa0,0xa80,0xa60,0xa40,0xa20,0xa00,
0x9e0,0x9c0,0x9a0,0x980,0x960,0x940,0x920,0x900,0x8e0,0x8c0,0x8a0,0x880,0x860,0x840,0x820,0x800,
0x7e0,0x7c0,0x7a0,0x780,0x760,0x740,0x720,0x700,0x6e0,0x6c0,0x6a0,0x680,0x660,0x640,0x620,0x600,
0x5e0,0x5c0,0x5a0,0x580,0x560,0x540,0x520,0x500,0x4e0,0x4c0,0x4a0,0x480,0x460,0x440,0x420,0x400,
0x3e0,0x3c0,0x3a0,0x380,0x360,0x340,0x320,0x300,0x2e0,0x2c0,0x2a0,0x280,0x260,0x240,0x220,0x200,
0x1e0,0x1c0,0x1a0,0x180,0x160,0x140,0x120,0x100,0x0e0,0x0c0,0x0a0,0x080,0x060,0x040,0x020,0x000,
};
uint frequency,temp,key,down=0,form,bo=10,singlekey,flag,singlekeyf,dis=1,hour=0,mini=0;
static uint FREQUENCY=0,wei=0;
unsigned char fy=0,TIMER0_H,TIMER0_L,NUMBER;
void DA_conver(uint Dignum)
{
uint Dig=0;
uchar i=0;
SCLK=1;
CS=0; //片选有效
for(i=0;i<16;i++) //写入16为Bit的控制位和数据
{
Dig=Dignum&0x8000;
if(Dig)
{
DIN=1;
}
else
{
DIN=0;
}
SCLK=0;
_nop_();
Dignum<<=1;
SCLK=1;
_nop_();
}
SCLK=1;
CS=1; //片选无效
}
void Write_B(uint Data_B,bit Model)
{
uint Temp;
if(Model)
{
Temp=0x4000;
}
else
{
Temp=0x0000;
}
DA_conver(Temp|0x0000|(0x0fff&Data_B));
}
void calculater_f()
{
float temp2;
temp2=ALL-Fosc/12.0/256/dis;
TIMER0_H=(uint)temp2/256;
TIMER0_L=(uint)temp2%256;
}
void DS1302InputByte(unsigned char d)
{
unsigned char i,j;
EA=0;
for(i=8; i>0; i--)
{
DS1302_CLK = 0;
for(j=2;j>0;j--);
if(d&0x01)
DS1302_IO = 1;
else DS1302_IO = 0;
for(j=2;j>0;j--);
DS1302_CLK = 1;
d=d>>1;
}
EA=1;
}
unsigned char DS1302OutputByte(void)
{
unsigned char i,j,d=0;
EA=0;
for(i=0; i<8; i++)
{
DS1302_IO = 1;
DS1302_CLK = 1;
for(j=2;j>0;j--);
DS1302_CLK = 0;
if( DS1302_IO)
d=d|(0x01<<i) ;
}
return(d);
EA=1;
}
void Write1302(unsigned char ucAddr, unsigned char ucDa)
{
DS1302_RST = 0;
DS1302_CLK = 0;
DS1302_RST = 1;
DS1302InputByte(ucAddr);
DS1302InputByte(ucDa);
DS1302_CLK = 1;
DS1302_RST = 0;
}
unsigned char Read1302(unsigned char ucAddr)
{
unsigned char ucData;
DS1302_RST = 0;
DS1302_CLK = 0;
DS1302_RST = 1;
DS1302InputByte(ucAddr);
ucData = DS1302OutputByte();
DS1302_CLK = 1;
DS1302_RST = 0;
return(ucData);
}
void delay(unsigned int xms)
{
unsigned int i,j;
for(i=xms;i>0;i--)
for(j=110;j>0;j--);
}
void led_disp3(unsigned char *poi)
{
ge=0;
P0=seg[*poi % 16];
delay(2);
ge=1;
shi=0;
P0=seg[*poi /16];
delay(2);
shi=1;
poi++;
bai=0;
P0=seg[*poi % 16];
delay(2);
bai=1;
qian=0;
P0=seg[*poi / 16];
delay(2);
qian=1;
}
void ledsclk(uint singlekey)
{
P0=seg7code[singlekey];
switch(wei)
{
case 1:P2=0xef;break;
case 2:P2=0xdf;break;
case 3:P2=0xbf;break;
case 4:P2=0x7f;wei=0;break;
}
delay(2);
}
void SETCLK()
{
if(key<10 && down==1)
{
down=0;
FREQUENCY=FREQUENCY*10+key;
flag=key;
wei++;
}
dis=FREQUENCY;
}
void shixianfy()//?????
{
switch(fy)
{
case 1 : led_disp3(&clk_time[1]);
break;
case 0 : led_disp3(&clk_time[0]);
break;
}
}
void leds(uint singlekey)
{
P0=seg7code[singlekey];
P2=0x7f;
delay(3); P2=0xff;
}
此帖出自
小平头技术问答
{
P0=seg7code[singlekeyf];
P2=0xef;
delay(3); P2=0xff;
}
void led(uint frequency,uint form)
{
if(frequency<1000)
{
P0=seg7code[form];
P2=0xef;
delay(3);
P2=0xff;
P0=seg7code[frequency/100];
P2=0xdf;
delay(3);
P2=0xff;
P0=seg7code[frequency%100/10];
P2=0xbf;
delay(3); P2=0xff;
P0=seg7code[frequency%10];
P2=0x7f;
delay(3); P2=0xff;
}
else
{
P0=seg7code[form];
P2=0xef;
delay(3); P2=0xff;
P0=seg7code[1];
P2=0xdf;
delay(3); P2=0xff;
P0=seg7code[0];
P2=0xbf;
delay(3); P2=0xff;
P0=seg7code[13];
P2=0x7f;
delay(3); P2=0xff;
}
}
uint keyscan()
{
down=0;
P1=0xfe;
temp=P1;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(10);
temp=P1;
temp=temp&0xf0;
if(temp!=0xf0)
{
temp=P1;
switch(temp)
{
case 0xee:
key=3;down=1;
break;
case 0xde:
key=2;down=1;
break;
case 0xbe:
key=1;down=1;
break;
case 0x7e:
key=0;down=1;
break;
}
while(temp!=0xf0)
{
temp=P1;
temp=temp&0xf0;
}
}
}
P1=0xfd;
temp=P1;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(10);
temp=P1;
temp=temp&0xf0;
if(temp!=0xf0)
{
temp=P1;
switch(temp)
{
case 0xed:
key=7;down=1;
break;
case 0xdd:
key=6;down=1;
break;
case 0xbd:
key=5;down=1;
break;
case 0x7d:
key=4;down=1;
break;
}
down=1;
while(temp!=0xf0)
{
temp=P1;
temp=temp&0xf0;
}
}
}
P1=0xfb;
temp=P1;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(10);
temp=P1;
temp=temp&0xf0;
if(temp!=0xf0)
{
temp=P1;
switch(temp)
{
case 0xeb:
key=11;down=1;
break;
case 0xdb:
key=10;down=1;
break;
case 0xbb:
key=9;down=1;
break;
case 0x7b:
key=8;down=1;
break;
}
down=1;
while(temp!=0xf0)
{
temp=P1;
temp=temp&0xf0;
}
}
}
P1=0xf7;
temp=P1;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(10);
temp=P1;
temp=temp&0xf0;
if(temp!=0xf0)
{
temp=P1;
switch(temp)
{
case 0xe7:
key=15;down=1;
break;
case 0xd7:
key=14;down=1;
break;
case 0xb7:
key=13;down=1;
break;
case 0x77:
key=12;down=1;
break;
}
down=1;
while(temp!=0xf0)
{
temp=P1;
temp=temp&0xf0;
}
}
}
return key;
}
void FREQ()
{
if(key<10 && down==1)
{
down=0;
FREQUENCY=FREQUENCY*10+key;
flag=key;
}
dis=FREQUENCY;
}
void switcher()
{
// if(down==1)
{
if(key==10)
{
bo=10;
}
else if(key==11)
{
bo=11;
}
else if(key==12)
{
bo=12;
}
down=0;
}
}
void main()
{
EA =1; //开总中断
TMOD=0x01;
calculater_f();
TH0=TIMER0_H;
TL0=TIMER0_L;
ET0=1;
TR0=1;
EX1=1; //允许中断1
IT1=1; //电平触发方式
while(1)
{
keyscan();
if(key==15)
{
ET0=0;
TR0=0;
FREQUENCY=0;
key=0;
while(key!=15)
{
keyscan();
FREQ();
switcher();
leds(flag);
ledsf(bo);
}
key=0;
ET0=1;
TR0=1;
}
led(dis,bo);
calculater_f();
if(key==14)
{
dis=dis+1;
key=0;
}
if(key==13)
{
dis=dis-1;
key=0;
}
}
}
void CLKANDRING() interrupt 2
{
unsigned char temp1=0x80;
unsigned char i,p=0;
ET0=0;
TR0=0;
for(i=0;i<7;i++)
{
Write1302(temp1,clk_time);
temp1+=2;
}
RING=0;
while(1)
{
temp1=0x81;
for(i=0;i<7;i++)
{
clk_time=Read1302(temp1);
temp1+=2;
}
shixianfy();
keyscan();
if(key==15)
{
FREQUENCY=0;
key=0;
while(key!=15)
{
keyscan();
SETCLK();
ledsclk(flag);
}
key=0;
p=1;
}
hour=dis/100;
mini=dis%100;
if(clk_time[2]==seg2[hour] && clk_time[1]==seg2[mini] && clk_time[0]==0x00 && p)
{
RING=1;
delay(500);
RING=0;
}
}
}
void T0_time() interrupt 1
{
//TR0=0;
TH0=TIMER0_H;
TL0=TIMER0_L;
if(bo==10)
{
Write_B(tosin[NUMBER],1);
}
if(bo==11)
{
Write_B(tosqu[NUMBER],1);
}
if(bo==12)
{
Write_B(totri[NUMBER],1);
}
//led(dis,bo);
NUMBER++;
if(NUMBER==256)
{
NUMBER=0;
}
//TR0=1;
}
一周热门 更多>