|
手上有塊ds1302時鐘模塊,也有些數碼管,于是試著做了個時鐘,當然因為是手工,所以沒有淘寶上買的好看,當然自己做的成本也高,所以無法相比,只是作為一種學習方式。做著送人了,所以照片只有一張,也沒有顯示。
20150913134609_62391.jpg (631.25 KB, 下載次數: 128)
下載附件
2016-3-30 17:44 上傳
這是我用手機拍的,只有一張,沒有多拍幾張,樣子太丑了,沒有淘寶上做的美觀,被同學批了一頓。
程序很簡單,不是很占用空間,io資源,所以只用了msp430g2231,我后來想用xin,xout做io口時,發現不行,所以就只用了8個io口。
數碼管直接顯示年月日小時,分鐘,秒,星期,每30秒刷新一次。用hc164做片選數碼管斷碼顯示芯片。
部分程序預覽:
- #include"io430.h"
- #define uchar unsigned char
- #define uint unsigned int
- #define ulong unsigned long
- #define CLK0 P1OUT&=~BIT6
- #define CLK1 P1OUT|=BIT6
- #define CPU_F ((double)1000000) //外部高頻晶振16MHZ
- //#define CPU_F ((double)32768) //外部低頻晶振32.768KHZ
- #define delay_us(x) __delay_cycles((long)(CPU_F*(double)x/1000000.0))
- #define delay_ms(x) __delay_cycles((long)(CPU_F*(double)x/1000.0))
- unsigned char const code_hex[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x39,0x79,0x0e,0x3e,0x00};//0,1,2,3,4,5,6,7,8,9,c,e,j,u, ,
- uchar t,t1,t2,t3;
- uchar dis_play[4];
- //數碼管顯示函數
- void extern_16m()
- {
- WDTCTL = WDTPW + WDTHOLD;
- if (CALBC1_1MHZ == 0xFF || CALDCO_1MHZ == 0xFF)
- {
- while(1); // If calibration constants erased, trap CPU!!
- }
- DCOCTL |= DCO0 + DCO1+DCO2; //SMCLK選擇LFXT1CLK
- // BCSCTL2 |= SELM_0;//MCLK采用1M的內部DCO
- // BCSCTL2 |= DIVS_0;//SMCLK采用內部的時鐘
- }
- void hc164_init()
- {
- P1DIR |=BIT6+BIT7+BIT0+BIT1+BIT2+BIT3; // P1.0 output
-
- }
- void sendbyte(uchar byte)
- {
- uchar c,num;
- num=byte;
- for(c=0;c<8;c++)
- {
- P1OUT&=~0x80;
- CLK0;
- P1OUT|=num&0x80; //(0x80即十進制的128, 二進制的10000000 按位發送
- CLK1;
- num<<=1;
- }
- }
- void send_char(uchar weizhi,uchar byte)
- {
-
- P1OUT |= 0x0f;
- uchar c,send_byte;
- send_byte=code_hex[byte];
- // if((weizhi==0)&&(xianshi_flag==0))
- // send_byte|=0x80;
- sendbyte(send_byte);
- c=weizhi&0x03;
- P1OUT&=~(1<<c);
- }
- void init_TA()
- {
-
- TACCR0 = 1000; //8ms中斷一次
- TACTL = TASSEL_2 + MC_1+ID_3; // SMCLK, upmode,8分頻,
- TACCTL0 = CCIE; // TACCR0 interrupt enabled
- }
- void shumaguan()
- {
- ++t;
- if(t>=4)
- t=0;
- send_char(t,dis_play[t]);
- }
- ////時鐘模塊
- #define DS1302_DIR P1DIR
- #define DS1302_IN P1IN
- #define DS1302_OUT P1OUT
- #define DS1302_RST BIT7
- #define DS1302_SCLK BIT4
- #define DS1302_SDI BIT5 //定義MSP320的端口
- #define DS1302_RST_LO DS1302_OUT &= ~DS1302_RST
- #define DS1302_RST_HI DS1302_OUT |= DS1302_RST
- #define DS1302_SCLK_LO DS1302_OUT &= ~DS1302_SCLK
- #define DS1302_SCLK_HI DS1302_OUT |= DS1302_SCLK
- #define DS1302_SDI_LO DS1302_OUT &= ~DS1302_SDI
- #define DS1302_SDI_HI DS1302_OUT |= DS1302_SDI
- void DS1302_Reset(void);
- void DS1302_WriteOneByte(unsigned char w_dat);
- void DS1302_WriteData(unsigned char addr,unsigned char w_dat);
- void DS1302_SettingData(void);
- void DS1302_GetData(unsigned char *str);
- unsigned char DS1302_ReadOneByte(void);
- unsigned char DS1302_ReadData(unsigned char addr);
- unsigned char Setting_Time[7]={ //bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0
- 0x15, //--------十位-------|-------個位--------|年份(當前07年)
- 0x04, // |-----個位-----|星期(當前周日)
- 0x09, // |十位|-------個位--------|月份(當前07月)
- 0x10, // |--十位---|-------個位--------|日期(當前01號)
- 0x13, //-12H| |--十位---|-------個位--------|小時(當前18點)
- 0x00, // |-----十位-----|-------個位--------|分鐘(當前20分)
- 0x21 // |-----十位-----|-------個位--------|秒鐘(當前30秒)
- };
- unsigned char ReadingData[7]; //讀出來的數據,同SettingData定義與格式
- /****************************
- void main(void) {
- WDTCTL = WDTPW + WDTHOLD;
- DS1302_Reset();
- //DS1302_SettingData();
- while(1){
- DS1302_GetData(ReadingData);
- _NOP();
- };
- }
- ****************************/
- //DS1302復位
- void DS1302_Reset(void) {
- DS1302_DIR |= DS1302_SCLK;
-
- DS1302_SCLK_LO;
- DS1302_RST_LO;
- // delay_us(10);
- DS1302_SCLK_HI;
- }
- //向DS1302寫入一個字節
- void DS1302_WriteOneByte(unsigned char w_dat) {
- unsigned char temp;
-
- DS1302_RST_HI;
- DS1302_DIR |= DS1302_SDI;
-
- for(temp=8;temp>0;temp--) {
- DS1302_SDI_LO;
- if(w_dat&BIT0) DS1302_SDI_HI;
- DS1302_SCLK_LO;
- // delay_us(10);
- DS1302_SCLK_HI;
- // delay_us(10);
- w_dat >>=1;
- }
- }
- //從DS1302中讀取一個字節
- unsigned char DS1302_ReadOneByte(void) {
- unsigned char temp,rdata;
- rdata = 0x00;
-
- DS1302_RST_HI;
- DS1302_DIR &= ~DS1302_SDI;
-
- for(temp=0;temp<8;temp++){rdata >>= 1;//將移位放到前面,否則讀不到最高一位
- DS1302_SCLK_HI;
- // delay_us(10);
- DS1302_SCLK_LO;
- // delay_us(10);
- if((DS1302_IN&DS1302_SDI)==DS1302_SDI)
- rdata |= BIT7;
- //rdata >>= 1;
- }
-
- return(rdata);
- }
- //向DS1302中寫入地址后寫入數據
- void DS1302_WriteData(unsigned char addr,unsigned char w_dat) {
- DS1302_RST_LO;
- DS1302_SCLK_LO;
- DS1302_RST_HI;
-
- DS1302_WriteOneByte(addr); //寫入地址
- DS1302_WriteOneByte(w_dat); //寫入數據
-
- DS1302_SCLK_HI;
- DS1302_RST_LO;
- }
- //向DS1302寫入地址后,從DS1302中讀取數據
- unsigned char DS1302_ReadData(unsigned char addr) {
- unsigned char r_dat;
- DS1302_RST_LO;
- DS1302_SCLK_LO;
- DS1302_RST_HI;
-
- DS1302_WriteOneByte(addr); //寫入地址
- r_dat = DS1302_ReadOneByte(); //讀出數據
-
- DS1302_SCLK_LO;
- DS1302_RST_LO;
-
- return(r_dat);
- }
- //按照SettingData的設置設置DS1302的時間
- void DS1302_SettingData(void) {
- unsigned char temp;
- unsigned char addr = 0x8C;
-
- DS1302_WriteData(0x8E,0x00); //寫入控制命令,禁用寫保護
- for(temp=0;temp<7;temp++) {
- DS1302_WriteData(addr,Setting_Time[temp]);
- addr -= 2;
- }
-
- DS1302_WriteData(0x8E,0x80); //寫入控制命令,啟用寫保護
- }
- //讀取DS1302時間到ReadingData中
- void DS1302_GetData(unsigned char *str) {
- unsigned char temp;
- unsigned char addr = 0x8D;
- for(temp=0;temp<7;temp++) {
- str[temp] = DS1302_ReadData(addr);//年
- addr -= 2;
- }
- }
- void shumaguan_buff(void)
- {
- if(++t3==30)
- t3=0;
- if(t3<5)t2=1;
- else if(t3<10)t2=2;
- else if(t3<20)t2=3;
- else t2=4;
- switch(t2)
- {
- case 1:dis_play[0]=2;dis_play[1]=0;dis_play[2]=ReadingData[0]/16;dis_play[3]=ReadingData[0]%16;break; //顯示年份
- case 2:dis_play[0]=ReadingData[2]/16;dis_play[1]=ReadingData[2]%16;dis_play[2]=ReadingData[3]/16;dis_play[3]=ReadingData[3]%16;break; //顯示月份日期
- case 3:dis_play[0]=ReadingData[4]/16;dis_play[1]=ReadingData[4]%16;dis_play[2]=ReadingData[5]/16;dis_play[3]=ReadingData[5]%16;break; //顯示時分
- case 4:dis_play[0]=ReadingData[1]%16;dis_play[1]=14;dis_play[2]=ReadingData[6]/16;dis_play[3]=ReadingData[6]%16;break; //顯示星期秒
- default:break;
- }
-
- }
-
- #pragma vector=TIMER0_A0_VECTOR
- __interrupt void TIMERA0_ISR() // the interrupt source is CC0
- {
- ++t1;
- if(t1>=125)
- t1=0;
- if(t1==0)
- {
- DS1302_GetData(ReadingData);
- shumaguan_buff();
- }
- else
- shumaguan();
- }
復制代碼
</c);
|
-
-
數碼管-時鐘.zip
2016-3-30 17:21 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
177.62 KB, 下載次數: 20, 下載積分: 黑幣 -5
|