Pic microcontroller control clock chip DS1302 program

#include <pic.h> // Includes internal resources of the microcontroller
__CONFIG(0x1832);
// Chip configuration word: watchdog off, power-on delay on, power-down detection off, low-voltage programming off, encryption, 4M crystal HS oscillation

#define i_o RB4 // Define data port for DS1302
#define sclk RB0 // Define clock port for DS1302
#define rst RB5 // Define reset port for DS1302

// unsigned char time_rx;
unsigned char time_rx @ 0x30; // Define receiving register
static volatile bit time_rx7 @(unsigned)&time_rx * 8 + 7; // Highest bit of receive register
// static volatile bit temp0 @(unsigned)&temp * 8 + 0;

void port_init(); // Declare pin initialization function
void ds1302_init(); // Declare DS1302 initialization function
void set_time(); // Declare set time function
void get_time(); // Declare read time function
void display(); // Declare display function
void time_write_1(unsigned char time_tx); // Declare byte write function
unsigned char time_read_1(); // Declare byte read function
void delay(); // Declare delay function

// Define the time to be set: seconds, minutes, hours, day, month, week, year, control bits
const char table[] = {0x00, 0x58, 0x12, 0x08, 0x03, 0x06, 0x06, 0x00};
// Define the storage table for reading time and date
char table1[7];
// Define display code for digits 0-9
const char table2[] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90};

//---------------------------------------------
// Main function
void main()
{
port_init(); // Call pin initialization function
ds1302_init(); // Call DS1302 initialization function
set_time(); // Call set time function
while(1)
{
get_time(); // Call get time function
display(); // Call display function
}
}

//---------------------------------------------
// DS1302 initialization function
void ds1302_init()
{
sclk = 0; // Pull down clock signal
rst = 0; // Reset DS1302
rst = 1; // Enable DS1302
time_write_1(0x8e); // Send control command
time_write_1(0); // Allow writing to DS1302
rst = 0; // Reset
}

//---------------------------------------------
// Set time function
void set_time()
{
int i; // Define loop variable
rst = 1; // Enable DS1302
time_write_1(0xbe); // Clock multi-byte write command
for(i=0; i<8; i++) // Write 8 bytes of data continuously
{
time_write_1(table[i]); // Call write one byte function
}
rst = 0; // Reset
}

//---------------------------------------------
// Read time function
void get_time()
{
int i; // Define loop variable
rst = 1; // Enable DS1302
time_write_1(0xbf); // Send multi-byte read command
for(i=0; i<7; i++) // Read 7 bytes of data continuously
{
table1[i] = time_read_1(); // Call read one byte function
}
rst = 0; // Reset DS1302
}

//--------------------------------------------
// Write one byte function
void time_write_1(unsigned char time_tx)
{
int j; // Define loop variable
for(j=0; j<8; j++) // Write 8 bits continuously
{
i_o = 0; // Set data to 0
sclk = 0; // Pull down clock signal
if(time_tx & 0x01) // Determine if the data bit is 0 or 1
{
i_o = 1; // Data bit to send is 1
}
time_tx = time_tx >> 1; // Shift data to the right by 1 bit
sclk = 1; // Pull up clock signal
}
sclk = 0; // After writing a byte, pull down clock signal
}

//---------------------------------------------
// Read one byte function
unsigned char time_read_1()
{
int j; // Define loop variable
TRISB4 = 1; // Set data port direction to input
for(j=0; j<8; j++) // Read 8 bits continuously
{
sclk = 0; // Pull down clock signal
time_rx = time_rx >> 1; // Shift receive register right by 1 bit
time_rx7 = i_o; // Put received data into highest bit of receive register
sclk = 1; // Pull up clock signal
}
TRISB4 = 0; // Restore data port direction to output
sclk = 0; // Pull down clock signal
return(time_rx); // Return read data
}

//--------------------------------------------
// Pin definition function
void port_init()
{
TRISA = 0x00; // Set A port as full output
TRISD = 0x00; // Set D port as full output
ADCON1 = 0x06; // Set A port as normal I/O
TRISB = 0x02; // Set RB1 as input, others as output
OPTION = 0x00; // Enable weak pull-up on B port
PORTA = 0xFF;
PORTD = 0xFF; // Turn off all displays initially
}

//-------------------------------------------
// Display function
void display()
{
int i; // Define table lookup variable
if(RB1 == 0) // Check if RB1 is pressed; if so, display date instead of time
{
table1[0] = table1[3];
table1[1] = table1[4];
table1[2] = table1[6];
}
i = table1[0] & 0x0f; // Get seconds digit
PORTD = table2[i]; // Send to D port for display
PORTA = 0x1f; // Light up the second digit
delay(); // Delay for brightness
i = table1[0] & 0xf0; // Get tens of seconds
i = i >> 4; // Shift right by 4 bits
PORTD = table2[i]; // Send to D port for display
PORTA = 0x2f; // Light up the tens of seconds
delay(); // Delay for brightness

i = table1[1] & 0x0f; // Get minute digit
PORTD = table2[i] & 0x7f; // Send to D port with decimal point
PORTA = 0x37; // Light up the minute digit
delay(); // Delay for brightness
i = table1[1] & 0xf0; // Get tens of minutes
i = i >> 4; // Shift right by 4 bits
PORTD = table2[i]; // Send to D port for display
PORTA = 0x3b; // Light up the tens of minutes
delay(); // Delay for brightness

i = table1[2] & 0x0f; // Get hour digit
PORTD = table2[i] & 0x7f; // Send to D port with decimal point
PORTA = 0x3d; // Light up the hour digit
delay(); // Delay for brightness
i = table1[2] & 0xf0; // Get tens of hours
i = i >> 4; // Shift right by 4 bits
PORTD = table2[i]; // Send to D port for display
PORTA = 0x3e; // Light up the tens of hours
delay(); // Delay for brightness
}

//------------------------------------------------ ------------------
// Delay program
void delay() // Delay function
{
int i; // Define shaping variable
for(i = 0x64; i--;); // Delay loop
}

// Set initial time and date: seconds (00), minutes (58), hour (12), day (08), month (03), year (06)
// The six-digit digital display shows the time and date. By default, it shows time. The decimal point helps distinguish between minutes and hours.

800V DC Power Supply

APM satisfies high voltage DC Power Supply application demands, allowing the SP series to cover a voltage range from 0V to 800V. It delivers advanced features. The compact 2U high 19inch wide adjustable dc power supply were designed to meet the needs of both system integrator and bench top users.

Some features of the switch mode power supply as below:


  • Ultrafast respond time and high efficiency
  • Accurate voltage and current measurement capability
  • Constant Power and wide range of voltage and current output
  • Equips with LIST waveform editing function
  • Compliant with SCPI communication protocol
  • Support RS232/RS485/LAN/USB (standard) ,GPIB (optional)
  • Built-in standard automobile electrical testing curves
  • Full protection: OVP/OCP/OPP/OTP/SCP
  • Voltage drop compensation by remote sense line.
  • Have obtained CE,UL,CSA,FCC.ROHS


800V DC Power Supply,Smps Power Supply,Variable Voltage Power Supply,DC Adapter Power Supply

APM Technologies Ltd , https://www.apmpowersupply.com