Resolvi finalmente começar a brincar com minha STM32F4discovery (7 meses na gaveta) e estou tendo um problema estranho quando tento apresentar em um display a leitura de Hora e Data. O que está estranho é que somente a hora e o ano não são apresentados conforme esperado. Estou configurando a Hora para 23h59m40s e o dia para hoje. Após passar os 20 segundos, o dia é incrementado normalmente, ou seja, aparentemente a configuração está correta, o que não estou conseguindo e exibir no display.
Reference Manual Páginas 800 e 801
Linguagem: C
Compilador: MikroC
- Código: Selecionar todos
long Hora_Reg;
long Data_Reg;
char s_temp[16];
int horas;
int minutos;
int segundos;
int dias;
int meses;
int anos;
int dia_semana_temp;
char diasemana;
// Pinos display TFT
unsigned int TFT_DataPort at GPIOD_ODR;
sbit TFT_WR at GPIOE_ODR.B9;
sbit TFT_RD at GPIOE_ODR.B10;
sbit TFT_CS at GPIOE_ODR.B12;
sbit TFT_RS at GPIOE_ODR.B8;
sbit TFT_RST at GPIOE_ODR.B13;
sbit TFT_BLED at GPIOE_ODR.B15;
//
static short RTC_Bcd2ToByte(short Value)
{
int tmp = 0;
tmp = ((short)(Value & (short)0xF0) >> (short)0x4) * 10;
return (tmp + (Value & (short)0x0F));
}
// *****************************************************************************
// * Read RTC *
// *****************************************************************************
void Le_RTC()
{
Hora_Reg = RTC_TR;
Data_Reg = RTC_DR;
horas = RTC_Bcd2ToByte((short)(RTC_TR & 0x3f0000) >> 16); // Converte Horas
minutos = RTC_Bcd2ToByte((short)((RTC_TR & 0x007f00) >> 8)); // Converte Minutos
segundos = RTC_Bcd2ToByte((short)(RTC_TR & 0x0000ff)); // Converte Segundos
sprintf(s_temp,"%02d:%02d:%02d",horas,minutos,segundos); // Formata Hora para exibição no display
TFT_Set_Font(&TFT_defaultFont, CL_BLACK, FO_HORIZONTAL);
TFT_Write_Text("Hour", 135, 65);
TFT_Write_Text(s_temp, 135, 85); // Escreve no display
anos = RTC_Bcd2ToByte((short)(RTC_DR & 0xff0000) >> 16); // Converte Anos
meses = RTC_Bcd2ToByte((short)((RTC_DR & 0x001f00) >> 8)); // Converte Meses
dias = RTC_Bcd2ToByte((short)(RTC_DR & 0x0000ff)); // Converte Dias
sprintf(s_temp,"%02d/%02d/%02d",dias,meses,anos); // Formata Data para exibição no display
TFT_Set_Font(&TFT_defaultFont, CL_BLACK, FO_HORIZONTAL); //
TFT_Write_Text("Date", 135, 115); //
TFT_Write_Text(s_temp, 135, 135); // Escreve no display
}
// *****************************************************************************
// * Config RTC *
// *****************************************************************************
void Config_RTC()
{
RCC_APB1ENRbits.PWREN = 1; // Power interface clock enable
PWR_CRbits.DBP = 1;
RCC_BDCRbits.RTCSEL0 = 1; // 01: LSE oscillator clock used as RTC
RCC_BDCRbits.RTCSEL1 = 0; // 01: LSE oscillator clock used as RTC
RCC_BDCRbits.RTCEN = 1; // RTC clock enable
RCC_BDCRbits.LSEON = 1; // External 32 kHz oscillator ON
// RTC-Register sincronizado?
RTC_ISRbits.RSF = 0;
delay_ms(1); //while (RTC_ISRbits.RSF == 0);
RTC_CR = RTC_CR | 0x20;
RTC_WPR = 0xCA;
RTC_WPR = 0x53;
RTC_ISRbits.INIT = 1;
delay_ms(1); //while(RTC_ISRbits.INITF != 1);
RTC_PRER = 255; // Configura Prescaler
RTC_PRER |= 127 << 16;
RTC_TR = 0x235940; // 23h59m40s
RTC_DR = 0xA52601; // 2015-Monday-June-01
RTC_CRbits.FMT = 0; // Formato 24h
RTC_ISRbits.INIT = 0;
RTC_WPR = 0xFF;
PWR_CRbits.DBP = 0;
}
void main() {
GPIO_Digital_Output(&GPIOE_ODR, _GPIO_PINMASK_15); // Configura PORTD.B13 como saída digital
TFT_Init_ILI9341_16bit(320, 240); // Configura Display
TFT_BLED = 1; // Liga Backlight
TFT_Fill_Screen(CL_WHITE);
Config_RTC(); // Configura RTC
while(1)
{
TFT_Fill_Screen(CL_WHITE);
Le_RTC(); // Lê valores do RTC e joga no display
delay_ms(1000);
}
}
Obrigado a todos.