Simple example of PIC16F84A reading clock chip DS1302

Publisher:芳华逝水Latest update time:2016-11-03 Source: eefocusKeywords:PIC16F84A Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
//Introduce files************************************************************

#include "delay.h"
#include "delay.c"
#include
#include

//Fuse configuration***********************************************************
__CONFIG(XT & WDTDIS & PWRTEN & PROTECT);

//Pin definition************************************************************
#define RS RB4 //Select register
#define EN RB5 //Signal enable

//Clock chip********************************************************
#define RST RA0 //Chip enable 
#define CLK RA1 //Chip clock
#define DAT RA2 //Chip data

//Display characters********************************************************
     unsigned char TopChar[] = {" "}; //Display buffer 
     unsigned char BotChar[] = {" "}; //

//********************************************************************
//Function name: portInit();
//Input parameters: None
//Output parameters: None
//Function description: Port settings
//Build date: 2008.12.12
//********************************************************************
void PortInit(void)
 {
     PORTA = 0x00; //Port settings 
     PORTB = 0x00; //
 
     TRISA = 0x00; //Direction settings   
     TRISB = 0x00; //

     OPTION = 0x00; //pull up to enable
 }

//*******************************************************************
//Function name: WriteData(data, rs);
//Input parameter: data to be written, 0 = instruction 1 = data 
//Output parameter: None
//Function description: Write 1602LCD
//Build date: 2008.12.12
//*******************************************************************
void WriteData(unsigned char data, unsigned char rs)
 {   
     RS = 0; //Assuming instruction

     if (rs & 0x01) RS = 1; //actual data

     PORTB = ((PORTB & 0xf0 ) | (data >> 4)); //Write high four bits    
     EN = 1; //Enable falling edge 
     EN = 0; //
     DelayUs(50); //
    
     PORTB = ((PORTB & 0xf0) | (data & 0x0f)); //Write low four bits
     EN = 1; //Enable falling edge 
     EN = 0; //                                            
     DelayUs(50); //
 }

//*******************************************************************
//Function name: AddrSite();
//Input parameter: coordinate parameter
//Output parameter: None
//Function description: Set the display address
//Construction date: 2008.12.12
//*******************************************************************
void AddrSite(unsigned char x, unsigned char y)
 {     
     if (y == 0) WriteData((0x80 | x), 0); //First row address
     
     else WriteData((0xc0 | x), 0); //Next row address 
 }

//*******************************************************************
//Function name: PrintChar(*s, len);
//Input parameters: buffer address, data length
//Output parameters: None
//Function description: string output display
//Construction date: 2008.12.12
//*******************************************************************
void PrintChar(unsigned char *s, unsigned char len)
 {   
     do //Loop sending                                      
     {
       WriteData(*s, 1); //Display character
       s++; //Next character
     }
     while (--len); //Display a line
 }

//*******************************************************************
//Function name: LcdInit();
//Input parameters: None
//Output parameters: None
//Function description: LCD initialization
//Construction date: 2008.12.12
//*******************************************************************
void LcdInit(void)
 {   
     unsigned char i = 3;

     RS = 0; //Select command
     PORTB = 0x03; //Interface settings

     do
     {
       EN = 1; // Enable falling edge 
       EN = 0; //
       DelayUs(50);      
     }
     while (--i); // Loop three times

     PORTB = 0x02; //Four-wire mode
     EN = 1; //Enable falling edge 
     EN = 0; //
     DelayUs(50);     

     WriteData(0x28, 0); //Interface settings
     WriteData(0x0c, 0); //Display open
     WriteData(0x01, 0); //Display clear
     WriteData(0x06, 0); //Address increment
 }

//*******************************************************************
//Function name: WriteByte(data);
//Input parameter: data to be written
//Output parameter: None
//Function description: Ds1302 write data
//Build date: 2008.12.12
//*******************************************************************
void WriteByte(unsigned char data)
 {
     unsigned char i;
     
     TRISA2 = 0; //Output direction
     i = 8; //Send eight bits    

     do
     {
       DAT = 0; //assumes it is false 
           
       if (data & 0x01) DAT = 1; //actually true
       
       CLK = 1; //clock rises
       data >>= 1; //shift right one bit
       CLK = 0; //clock clears
     }
     while (--i); //send in a loop  
 }

//*******************************************************************
//Function name: ReadByte();
//Input parameter: None
//Output parameter: Clock data
//Function description: Ds1302 read data
//Build date: 2008.12.12
//*******************************************************************
unsigned char ReadByte(void)
 {
     unsigned char i, data;
     
     TRISA2 = 1; //Input direction
     i = 8; //Collect eight bits

     do
     {
       data >>= 1; // data right shift

       if (DAT) data |= 0x80; //record one bit

       CLK = 1; //Clock set
       NOP();                   
       CLK = 0; //Clock falls
     }
     while (--i); //Loop receiving

     return data; //return data        
 }

//***********************************************************************
//Function name: WriteDs1302(addr, code);
//Input parameters: register address, instruction
//Output parameters: None
//Function description: Ds1302 write operation
//Construction date: 2008.12.12
//*******************************************************************
void WriteDs1302(unsigned char addr, unsigned char code)
 {
     RST = 0; //It can be cleared
     CLK = 0; //Clock clears
     RST = 1; //Use valid
      
     WriteByte(addr & 0xfe); //Write address
     WriteByte(code); //Write data
   
     CLK = 1; //Clock sets
     RST = 0; //It can be invalid
 } 
//***********************************************************************
//Function name: ReadDs1302(addr);
//Input parameter: register address
//Output parameter: function data
//Function description: Ds1302 read operation
//Build date: 2008.12.12
//*******************************************************************
unsigned char ReadDs1302(unsigned addr)
 {   
     unsigned char code;

     RST = 0; //Clear
     CLK = 0; //Clear clock
     RST = 1; //Use valid
      
     WriteByte(addr | 0x01); //Write address
     code = ReadByte(); //Read data
   
     CLK = 1; //Set clock
     RST = 0; //Fail

     return code; //Return data
 }

//*******************************************************************
//Function name: TimeCahr();
//Input parameter: None
//Output parameter: None
//Function description: Clock conversion character
//Construction date: 2008.12.12
//*******************************************************************
void TimeCahr(void)
 {     
     unsigned char data;
     
     data = ReadDs1302(0x8c); //Year data
     TopChar[2] = 0x30 + (data >> 4); //Year tens digit
     TopChar[3] = 0x30 + (data & 0x0f); //Year ones digit

     data = ReadDs1302(0x88); //Month data
     TopChar[5] = 0x30 + (data >> 4); //Month tens digit
     TopChar[6] = 0x30 + (data & 0x0f); //Month units digit
 
     data = ReadDs1302(0x86); //Day data
     TopChar[8] = 0x30 + (data >> 4); //Day tens digit
     TopChar[9] = 0x30 + (data & 0x0f); //Day units digit
     
     data = ReadDs1302(0x84); //Clock data
     BotChar[0] = 0x30 + (data >> 4); //Hour tens digit  
     BotChar[1] = 0x30 + (data & 0x0f); //Hour units digit
       
     data = ReadDs1302(0x82); //Minutes data
     BotChar[3] = 0x30 + (data >> 4); //Minutes Tens digit  
     BotChar[4] = 0x30 + (data & 0x0f); //Minutes Units
       
     digit data = ReadDs1302(0x80); //Seconds data
     BotChar[6] = 0x30 + (data >> 4); //Seconds Tens digit  
     BotChar[7] = 0x30 + (data & 0x0f); //Seconds Units digit 
 }

//***********************************************************************
//Function name: Picture();
//Input parameters: None
//Output parameters: None
//Function description: Display picture
//Construction date: 2008.12.12
//*******************************************************************
void Picture(void)
 {
     TopChar[0] = '2'; //Default year
     TopChar[1] = '0';
     TopChar[4] = '/'; //Date label
     TopChar[7] = '/';
     BotChar[2] = ':'; //Clock label
     BotChar[5] = ':';
 }

//*******************************************************************
//Function name: Ds1302Init();
//Input parameter: None
//Output parameter: None
//Function description: Clock chip setting
//Build date: 2008.12.12
//*******************************************************************
void Ds1302Init(void)
 {
     unsigned char code;    
    
     code = ReadDs1302(0x80); //Read oscillator

     if (code & 0x80) //Whether to stop vibration
     {
       WriteDs1302(0x8e, 0x00); //Allow writing
       WriteDs1302(0x8c, 0x08); //Default date
       WriteDs1302(0x88, 0x01);
       WriteDs1302(0x86, 0x01);
       WriteDs1302(0x84, 0x00); //Default time    
       WriteDs1302(0x82, 0x00);                           
       WriteDs1302(0x80, 0x00);
       WriteDs1302(0x8e, 0x80); //Prohibit writing
     } 
     
     code = ReadDs1302(0x90); //Charging status
      
     if (code != 0xab) //Settings do not match 
     {
       WriteDs1302(0x8e, 0x00); //Allow
       writingWriteDs1302(0x90, 0xab); //Charging                                  
       settingWriteDs1302(0x8e, 0x80); //Prohibit writing
     }
 }

//***********************************************************************
//Function name: main();
//Input parameters: None
//Output parameters: None
//Function description: Main program
//Construction date: 2008.12.12
//*******************************************************************
void main(void)                                            
 {   
     unsigned char count, i;
 
     PortInit(); //Pin setting                   
     LcdInit(); //LCD setting
     Ds1302Init(); //Clock setting 
     TimeCahr(); //Time conversion
     Picture(); //Display label
 
     while (1)
     {
       AddrSite(0, 0); //Coordinate setting
       PrintChar(TopChar, 16); //Send character
       
       AddrSite(0, 1); //Coordinate setting
       PrintChar(BotChar, 16); //Send character

       DelayMs(100); //Update cycle
       TimeCahr(); //Update time
     }  
 }

Keywords:PIC16F84A Reference address:Simple example of PIC16F84A reading clock chip DS1302

Previous article:PIC key LCD+DS1302+AD program (C program)
Next article:PIC16F877A serial port transmission

Recommended ReadingLatest update time:2024-11-16 16:00

MCU·Real-time clock DS1302
1. Introduction to the real-time clock DS1302 The application of DS1302 in real-time time display. It can count the year, month, day, week, hour, minute, and second, and has many functions such as leap year compensation. Vcc2: power supply Vcc1: backup power supply, can continue to work after power failure G
[Microcontroller]
The Principle and Application of Real-time Clock Circuit DS1302
1 Introduction     There are many popular serial clock circuits, such as ds1302, ds1307, pcf8485, etc. These circuits have simple interfaces, low prices, and are easy to use, and are widely used. The real-time clock circuit ds1302 introduced in this article is a circuit with trickle current charging capability from Da
[Microcontroller]
The Principle and Application of Real-time Clock Circuit DS1302
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号