完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
#include
typedef unsigned char uchar; typedef unsigned int uint; uint sec = 0 ,sec1 = 0 ,sec2 = 0; const unsigned char table[10] = {0B00111111, 0B00110000, 0B01011011, 0B01111001, 0B01110100, 0B01101101, 0B01101111, 0B00111000, 0B01111111, 0B01111101}; void WriteByte(uint address); //写一个字节 uchar ReadByte(); //读一个字节 uchar ReadDS1302(uint address); //读取ds1302中的数据 void WriteCommand(uint address , uint data);//往ds1302中写数据 void InitDS1302(); //ds1302的初始化 void interrupt Display(); //中断并显示 void Init(); //端口初始化 void Spi_Init(); //spi的初始化 void main() { Init(); Spi_Init(); InitDS1302(); INTCON = 0b11100000; while(1) { sec = ReadDS1302(0x81); //读取秒钟的时间 sec1 = sec & 0X0f; sec2 = sec >> 4; } } void WriteByte(uint address) { SSPSTAT = 0b10000000; //cke = 0 RC0 = 1; //RC0被用来接CE线 SSPBUF = address; while(0 == SSPIF); SSPIF = 0; RC0 = 0; } uchar ReadByte() { TRISC4 = 1; SSPSTAT = 0b11000000; RC0 = 1; uchar data = 0; while(0 == BF); data = SSPBUF; RC0 = 0; TRISC4 = 0; return data; } uchar ReadDS1302(uint address) { uint tmp = 0 ,data; uint i; WriteByte(address); //写入addr data = ReadByte(); //读取 for(i = 0; i < 8; i++) //将数据顺序矫正 { tmp = data & 0X01; data >>= 1; tmp << =7; data = data | tmp; } return data; } void WriteCommand(uint address , uint data) { WriteByte(address); WriteByte(data); } void InitDS1302() //ds1302的数据顺序和spi的顺序是反的,所以设置的数据也反着来 { WriteCommand(0Xe1 , 0X00); //写保护寄存器关闭 WriteCommand(0X01 , 0X80); //秒为1,同时开启时钟; WriteCommand(0X41 , 0X2C); //分为1; WriteCommand(0X21 , 0X48); //时为1; WriteCommand(0X61 , 0X80); //日为1; WriteCommand(0X11 , 0X80); //月为1; WriteCommand(0X51 , 0X80); //星期1; WriteCommand(0X31 , 0X80); //年份1; WriteCommand(0X01 , 0X80); //秒为1,同时开启时钟; WriteCommand(0Xe1 , 0X01); //写保护寄存器开启; } void interrupt Display() { PORTB = 0Xfe; PORTA = table[sec2]; _delay(50); PORTA = 0X00; PORTB = 0Xfd; PORTA = table[sec1]; _delay(50); PORTA = 0X00; TMR0 = 0Xea; T0IF = 0; } void Init() { ANSEL = 0X00; ANSELH = 0X00; TRISA = 0X00; TRISB = 0X00; PORTB = 0Xff; INTCON = 0; OSCCON = 0b01010001; //2M时钟 OPtiON_REG= 0b01010010 ; //预分频8 } void Spi_Init() { TRISC = 0X00; SSPCON = 0b00100000; SSPSTAT = 0b11000000; ADCON1 = 0b00000110; } |
|
相关推荐
|
|
只有小组成员才能发言,加入小组>>
求解外围电路实现的是4脚给持续低电平复位并正常工作,高电平不工作的原因
2077 浏览 1 评论
3508 浏览 3 评论
PIC1946程序有一个变量在运行过程中恢复初始值其他变量保持不变
2334 浏览 2 评论
2761 浏览 0 评论
PIC16F1825的RC5引脚,在主程序中操作无效,在中断中可以改变是为什么?
4019 浏览 5 评论
968浏览 0评论
用XC8编译PIC18F25K80时提示下面Error,求怎么解决这个问题
6351浏览 0评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-30 12:58 , Processed in 0.445805 second(s), Total 47, Slave 36 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号