采用CVAVR自带的DS1307驱动怎么读出来的数一直不变,求大师
程序如下:#include <mega16.h>
// I2C Bus functions
#asm
.equ __i2c_port=0x1B ;PORTA
.equ __sda_bit=0
.equ __scl_bit=1
#endasm
#include <i2c.h>
// DS1307 Real Time Clock functions
#include <ds1307.h>
#include "delay.h"
int time;
long timej;
int dis_buff;
int timeA;
static int kr1=0,kr2=0,kr3=0,kr4=0,ZJ=0;
static long zhuCD=0,bCD,kaiCD,time3;
#define key_input PIND.0
#define key_input1 PIND.1
#define key_input2 PIND.2
#define key_input3 PIND.3
void Write_Max721901_byte(unsigned char DATA)
{
unsigned char i;
PORTB.1=0;
for(i=8;i>=1;i--)
{
PORTB.2=0;
PORTB.0=DATA&0x80;
DATA=DATA<<1;
PORTB.2=1;
}
}
void Write_Max721901(unsigned char address,unsigned char dat)
{
PORTB.1=0;
Write_Max721901_byte(address); //写入地址,即数码管编号
Write_Max721901_byte(dat); //写入数据,即数码管显示数字
PORTB.1=1;
}
void Init_MAX721901(void)
{
Write_Max721901(0x09, 0xff); //译码方式:BCD码
Write_Max721901(0x0a, 0x03); //亮度
Write_Max721901(0x0b, 0x07); //扫描界限;4个数码管显示
Write_Max721901(0x0c, 0x01); //掉电模式:0,普通模式:1
Write_Max721901(0x0f, 0x01); //显示测试:1;测试结束,正常显示:0
}
void Write_Max721902_byte(unsigned char DATA)
{
unsigned char i;
PORTB.4=0;
for(i=8;i>=1;i--)
{
PORTB.5=0;
PORTB.3=DATA&0x80;
DATA=DATA<<1;
PORTB.5=1;
}
}
void Write_Max721902(unsigned char address,unsigned char dat)
{
PORTB.4=0;
Write_Max721902_byte(address); //写入地址,即数码管编号
Write_Max721902_byte(dat); //写入数据,即数码管显示数字
PORTB.4=1;
}
void Init_MAX721902(void)
{
Write_Max721902(0x09, 0xff); //译码方式:BCD码
Write_Max721902(0x0a, 0x03); //亮度
Write_Max721902(0x0b, 0x07); //扫描界限;4个数码管显示
Write_Max721902(0x0c, 0x01); //掉电模式:0,普通模式:1
Write_Max721902(0x0f, 0x01); //显示测试:1;测试结束,正常显示:0
}
void read_key(void){
static unsigned char k1=0,k2=0,k3=0,k4=0;
unsigned char kp1,kp2,kp3,kp4;
kp1=key_input;
switch(k1){
case 0:
if(!kp1)k1=1;
break;
case 1:
if(!kp1){
kr1=1;
k1=2;
}
else k1=0;
break;
case 2:
if(kp1)k1=0;
break;
}
kp2=key_input1;
switch(k2){
case 0:
if(!kp2)k2=1;
break;
case 1:
if(!kp2){
kr2=1;
k2=2;}
else k2=0;
break;
case 2:
if(kp2)k2=0;
break;
}
kp3=key_input2;
switch(k3){
case 0:
if(!kp3)k3=1;
break;
case 1:
if(!kp3){
kr3=1;
k3=2;
}
else k3=0;
break;
case 2:
if(kp3)k3=0;
break;
}
kp4=key_input3;
switch(k4){
case 0:
if(!kp4)k4=1;
break;
case 1:
if(!kp4){
kr4=1;
k4=2;
}
else k4=0;
break;
case 2:
if(kp4)k4=0;
break;
}
}
void jianPanChuLi(void){
if(kr1==1){
if(++zhuCD>=4)zhuCD=0;
kr1=0;
}
if(kr2==1){
kr2=0;
if(zhuCD>=1){
if(++bCD>=10){
bCD=0;
}
}
}
if(kr3==1){
kr3=0;
if(zhuCD>=1){
if(--bCD<0){
bCD=9;
}
}
}
if(kr4==1){
kr4=0;
if(zhuCD==0){
if(++kaiCD>=2)kaiCD=0;
}
}
}
void display(void){
Write_Max721901(0x0f, 0x00);
Write_Max721901(1,0);
Write_Max721901(2,0);
Write_Max721901(3,0);
Write_Max721901(4,0);
Write_Max721901(5,dis_buff);
Write_Max721901(6,dis_buff);
Write_Max721901(7,dis_buff);
Write_Max721901(8,0);
Write_Max721902(0x0f, 0x00);
Write_Max721902(1,dis_buff);
Write_Max721902(2,dis_buff);
Write_Max721902(3,dis_buff);
Write_Max721902(4,0);
Write_Max721902(5,dis_buff);
Write_Max721902(6,dis_buff);
Write_Max721902(7,dis_buff);
Write_Max721902(8,0);
}
void display111(void){
dis_buff=time%10;
dis_buff=(time-time/100*100)/10;
dis_buff=time/100;
dis_buff=time3%10;
dis_buff=(time3-time3/100*100)/10;
dis_buff=time3/100;
dis_buff=time%10;
dis_buff=(time-time/100*100)/10;
dis_buff=time/100;
}
// Declare your global variables here
void qingKong(void){
rtc_init(0,0,0);
rtc_set_time(0,0,0);
rtc_set_date(0,0,0);
}
// Declare your global variables here
void main(void)
{
unsigned char h,m,s,n,y,d;
// Declare your local variables here
// Input/Output Ports initialization
// Port A initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTA=0x00;
DDRA=0x00;
// Port B initialization
// Func7=In Func6=In Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out
// State7=T State6=T State5=0 State4=0 State3=0 State2=0 State1=0 State0=0
PORTB=0x00;
DDRB=0x3F;
// Port C initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTC=0x00;
DDRC=0x00;
// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTD=0x00;
DDRD=0x00;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=0xFF
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer1 Stopped
// Mode: Normal top=0xFFFF
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer2 Stopped
// Mode: Normal top=0xFF
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
MCUCR=0x00;
MCUCSR=0x00;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;
// USART initialization
// USART disabled
UCSRB=0x00;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;
// ADC initialization
// ADC disabled
ADCSRA=0x00;
// SPI initialization
// SPI disabled
SPCR=0x00;
// TWI initialization
// TWI disabled
TWCR=0x00;
// I2C Bus initialization
i2c_init();
// DS1307 Real Time Clock initialization
// Square wave output on pin SQW/OUT: Off
// SQW/OUT pin state: 0
rtc_init(0,0,0);
time=0;time=0,time3=352;
Init_MAX721901();
Init_MAX721902();
delay_ms(5000);
read_key();
while (1)
{
read_key();
if(kr1||kr2||kr3||kr4){
if(zhuCD==1){
bCD=time3%10;
jianPanChuLi();
time3=bCD+(time3-time3/100*100)/10*10+time3/100*100;
}
if(zhuCD==2){
bCD=(time3-time3/100*100)/10;
jianPanChuLi();
time3=bCD*10+time3%10+time3/100*100;
}
if(zhuCD==3){
bCD=time3/100;
jianPanChuLi();
time3=bCD*100+time3%10+(time3-time3/100*100)/10*10;
}
jianPanChuLi();
}
if(zhuCD==0&&kaiCD==1){
rtc_get_time(&h,&m,&s);
rtc_get_date(&d,&y,&n);
timej=d*24;
timej=h;
timej=m;
timej=s;
timeA=(timej+timej)*3600+timej*60+timej;
}
if(zhuCD==0&&kaiCD==0){
qingKong();
ZJ=10;
}
if(zhuCD!=0){
ZJ=0;
qingKong();
}
time=timeA/time3;
time=timeA/time3+ZJ;
display111();
display();
delay_ms(10);
}
// Place your code here
}
最后显示time=H9.time=L9.求各位大神!!! 自己调成功了,我把unsigned char h,m,s,n,y,d;换成了静态函数就正常了 怎么有时不起振啊。。。
页:
[1]