历史上的今天
今天是:2024年09月11日(星期三)
2020年09月11日 | mickoC 编写pic18f45k22读取i2c接口RTC pcf8583时间显示在数码管
2020-09-11 来源:51hei

单片机源程序如下:
/*
* Project name:
Seven Segment Display (The 'Hello World' example for the Seven Segment Display)
* Copyright:
(c) Mikroelektronika, 2011.
* Revision History:
20110929:
- initial release (FJ);
* Description:
This code demonstrates how to display number on one 7-segment display
(common cathode). Display is connected to PORTD (RD0..RD7, segment A to
RD0, segment B to RD1, etc); common cathode is connected to the pin RA0 on
PORTA. Number is incremented every 1s.
* NOTES:
- Turn on Seven Segment Display switches SW4.1, SW4.2, SW4.3 and SW4.4. (board specific)
*/
#include "Display_Utils.h"
//unsigned short portd_index;
char seconds, minutes, hours, day, month, year; // Global date/time variables
char i;
// Software I2C connections
/*sbit Soft_I2C_Scl at RC3_bit;
sbit Soft_I2C_Sda at RC4_bit;
sbit Soft_I2C_Scl_Direction at TRISC3_bit;
sbit Soft_I2C_Sda_Direction at TRISC4_bit;
*/
// End Software I2C connections
//--------------------- Reads time and date information from RTC (PCF8583)
void Read_Time() {
I2C1_Start(); // Issue start signal
I2C1_Wr(0xA0); // Address PCF8583, see PCF8583 datasheet
I2C1_Wr(2); // Start from address 2
I2C1_Repeated_Start(); // issue I2C signal repeated start
//I2C1_Start(); // Issue repeated start signal
I2C1_Wr(0xA1); // Address PCF8583 for reading R/W=1
seconds = I2C1_Rd(1); // Read seconds byte
minutes = I2C1_Rd(1); // Read minutes byte
hours = I2C1_Rd(1); // Read hours byte
day = I2C1_Rd(1); // Read year/day byte
month = I2C1_Rd(0); // Read weekday/month byte
I2C1_Stop(); // Issue stop signal
}
//-------------------- Formats date and time
void Transform_Time() {
seconds = ((seconds & 0xF0) >> 4)*10 + (seconds & 0x0F); // Transform seconds
minutes = ((minutes & 0xF0) >> 4)*10 + (minutes & 0x0F); // Transform months
hours = ((hours & 0xF0) >> 4)*10 + (hours & 0x0F); // Transform hours
year = (day & 0xC0) >> 6; // Transform year
day = ((day & 0x30) >> 4)*10 + (day & 0x0F); // Transform day
month = ((month & 0x10) >> 4)*10 + (month & 0x0F); // Transform month
}
/*
void interrupt() {
LATD = 0; // Turn off all 7seg displays
LATD = mask(hours/10u); // bring appropriate value to PORTD
LATA = 0b011111; // turn on appropriate 7seg. display
Delay_ms(2);
LATD=0;
LATD = mask(hours%10u);
LATA = 0b101111;
Delay_ms(2);
LATD=0;
LATD = mask(minutes/10u);
LATA = 0b110111;
Delay_ms(2);
LATD=0;
LATD = mask(minutes%10u);
LATA = 0b111011;
Delay_ms(2);
LATD=0;
LATD = mask(seconds/10u);
LATA = 0b111101;
Delay_ms(2);
LATD=0;
LATD = mask(seconds%10u);
LATA = 0b111110;
Delay_ms(2);
LATD=0;
TMR0L = 0; // reset TIMER0 value
TMR0IF_bit = 0; // Clear TMR0IF
} */
void main() {
ANSELA = 0; // Configure PORTA pins as digital
ANSELD = 0; // Configure PORTD pins as digital
TRISA = 0; // Configure PORTA as output
LATA = 0; // Clear PORTA
TRISD = 0; // Configure PORTD as output
LATD = 0; // Clear PORTD
//T0CON = 0xC4; // Set TMR0 in 8bit mode, assign prescaler to TMR0
//TMR0L = 0; // clear TMROL
//GIE_bit = 1;
//TMR0IE_bit = 1;
I2C1_Init(100000); // initialize I2C communication
//Soft_I2C_Init(); // Initialize Soft I2C communication
do {
Read_Time(); // Read time from RTC(PCF8583)
Transform_Time(); // Format date and time
for (i=0;i<32;i++)
{
LATD = 0; // Turn off all 7seg displays
LATD = mask(hours/10u); // bring appropriate value to PORTD
LATA = 0b011111; // turn on appropriate 7seg. display
Delay_ms(20);
LATD=0;
LATD = mask(hours%10u);
LATA = 0b101111;
Delay_ms(20);
LATD=0;
LATD = mask(minutes/10u);
LATA = 0b110111;
Delay_ms(20);
LATD=0;
LATD = mask(minutes%10u);
LATA = 0b111011;
Delay_ms(20);
LATD=0;
LATD = mask(seconds/10u);
LATA = 0b111101;
Delay_ms(20);
LATD=0;
LATD = mask(seconds%10u);
LATA = 0b111110;
Delay_ms(20);
LATD=0;
}
//Delay_ms(4000); // one second delay
} while(1); // endless loop
}
史海拾趣
|
//声程处理 void rangeprocess(long int nMyRange,long int nMyPDelay) { //float screenvalue; long int nTotal; long int nDiv; long int nAverage,nResidual,temp; int i,j; //nResidualAverage,i,j; void far *pN; ...… 查看全部问答> |
|
USB库头文件中usb_regs.h中/******************************************************************************** Macro Name : SetEPAddress.* Description :  ...… 查看全部问答> |
|
/* ADC1 configuration ------------------------------------------------------*/ ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; ADC_InitStructure.ADC_ScanConvMode = E ...… 查看全部问答> |
|
2012年06月04日 中安网 随着我国航空运输的不断推进和快速发展,对我国全省各地的机场建设起着强大的推动效应,同时,也对对机场所在的城市经济社会发挥了一定的提振拉动效应。当然,随着人们对于航空行业的接受度和空中交通流 ...… 查看全部问答> |
|
codesourcery sourcery会产生 blx 0x80144c8 非法指令 我现在用CodeSourcery+jlink+openocd开发stm32 平台:stm32f103zet6 flash:内置512K(大容量) sram:内置64K+外部512K 启动文件:startup_stm32f10x_hd.c 链接脚本:stm32f10x_flash_extsram.ld 程序一执行80行的除法运算就进入硬中断,这是 ...… 查看全部问答> |




