• No results found

Real Time Clock

In document PIC for Beginners (Page 130-133)

midnight. Values that correspond to the day of week are user-defined but must be sequential (i.e., if 1 equals Sunday, then 2 equals Monday, and so on.) Illogical time and date entries result in undefined opera-tion. Bit 7 of Register 0 is the clock halt (CH) bit. When this bit is set to 1, the oscillator is disabled. When cleared to 0, the oscillator is enabled.

Note that the initial power-on state of all registers is not defined. Therefore, it is important to enable the oscillator (CH bit = 0) during initial configuration.

The DS1307 can be run in either hour or 24-hour mode. Bit 6 of the hours register is defined as the 12-hour or 24-12-hour mode-select bit. When high, the 12-12-hour mode is selected. In the 12-12-hour mode, bit 5 is the AM/PM bit with logic high being PM. In the 24-hour mode, bit 5 is the second 10-hour bit (20 to 23 hours).

The hours value must be re-entered whenever the 12/24-hour mode bit is changed. When reading or writing the time and date registers, secondary (user) buffers are used to prevent errors when the internal registers update. When reading the time and date registers, the user buffers are synchronized to the internal registers on any I2C START. The time information is read from these secondary registers while the clock continues to run. This eliminates the need to re-read the registers in case the internal registers update during a read.

The divider chain is reset whenever the seconds register is written. Write transfers occur on the I2C ac-knowledge from the DS1307. Once the divider chain is reset, to avoid rollover issues, the remaining time and date registers must be written within one second.

The DS1307 may operate in the following two modes:

1. Slave Receiver Mode (Write Mode): Serial data and clock are received through SDA and SCL. After each byte is received an acknowledge bit is transmitted. START and STOP conditions are recognized as the beginning and end of a serial transfer. Hardware performs address recognition after reception of the slave address and direction bit (see Figure 4). The slave address byte is the first byte received after the master generates the START condition. The slave address byte contains the 7-bit DS1307 address, which is 1101000, followed by the direction bit (R/W), which for a write is 0. After receiving and de-coding the slave address byte, the DS1307 outputs an acknowledge on SDA. After the DS1307 ac-knowledges the slave address + write bit, the master transmits a word address to the DS1307. This sets the register pointer on the DS1307, with the DS1307 acknowledging the transfer. The master can then transmit zero or more bytes of data with the DS1307 acknowledging each byte received. The register pointer automatically increments after each data byte are written. The master will generate a STOP con-dition to terminate the data write.

2. Slave Transmitter Mode (Read Mode): The first byte is received and handled as in the slave receiver mode. However, in this mode, the direction bit will indicate that the transfer direction is reversed. The DS1307 transmits serial data on SDA while the serial clock is input on SCL. START and STOP condi-tions are recognized as the beginning and end of a serial transfer. The slave address byte is the first byte received after the START condition is generated by the master. The slave address byte contains the 7-bit DS1307 address, which is 1101000, followed by the direction 7-bit (R/W), which is 1 for a read. After

receiving and decoding the slave address the DS1307 outputs an acknowledge on SDA. The DS1307 then begins to transmit data starting with the register address pointed to by the register pointer. If the register pointer is not written to before the initiation of a read mode the first address that is read is the last one stored in the register pointer. The register pointer automatically increments after each byte are read. The DS1307 must receive a Not Acknowledge to end a read.

Writing applications is fairly simple, however keep in mind that the data obtained from registers is in BCD format and not as binary number. Like seconds, say if its 49 seconds, bits 0 to 3 will contain 9 and bits 4,5,6 will contain 4 so you have to extract the numbers from the byte read.

Secondly and most importantly, when the chip is used for the very first time, always make sure that CH bit of register 0 is cleared, so that clock starts. After that always make sure whenever setting seconds, not to set this bit as logical 1 as this will halt the clock.

F

requency is defined as number occurrences of an event in a specified time. Usually expressed as events per second. A frequency counter is a

de-vice that continuously monitors the occurrence of some event. This event can be an electrical signal, or a real world event, like number of cars passing through a gate. In any case the frequency counter counts the num-ber of events in a given time scale and displays or records the data. Frequency counter consists of two parts, a digital part based upon microcontroller, which we will be working on in this project, and a suitable input part which captures the real world event, and converts it into appropriate mi-crocontroller readable pulse. For example if you want to measure the radio-frequency you need a suitable adapter, that will capture the radio signal, and convert it into digital signal.

Since frequency of any event is usually variable, it is mandatory to sample the events frequently and update the display continuously. From this perspective there are two types of frequency counters, one which take the sample when required and display the result, they do not take another sample unless told to do so. The other type will continuously take samples of the input line and update the display. The time duration after which samples will be taken dictate the resolution of counter. Real time counters, do it almost continuously.

A simple frequency counter measures frequency by counting the number of edges of an input signal over a defined period of time (T).

A more complex method is reciprocal counting (we shall talk about it later).

Frequency is defined as (Number of events) / (time in seconds) and measured in Hz.

To make calculations trivial using a 1 second gate time (T) gives a direct reading of frequency from the edge counter.

Making a frequency counter for frequencies up to 65.535kHz is easy as the counters in a PIC chip can count up to 65535 without overflowing. Up to 65.535kHz all you do is wait for 1 second while the count accumu-lates, read the value and display it. It will be the frequency in Hertz. Above 65.536kHz you have to monitor the overflow value while at the same time making an accurate delay time (T).

Note: Using a 1 second measurement period results in the frequency counter count value being a direct measurement of frequency requiring no further processing. It also means that the measurement is resolved to 1Hz. (Increasing T to 10s resolves to 0.1Hz while using T=0.1s gives a resolution of 10Hz).

Crystal oscillator

For the following projects the crystal oscillator is used as the time-base. In these projects measurement of T (set at one second) is made by executing a delay that takes a set number of machine cycles.

Project 1

In document PIC for Beginners (Page 130-133)