Jump to content
ELFORUM - Forumul electronistilor

Folosirea RTCC hardware din PIC


Recommended Posts

Cum voiam sa incorporez un ceas intr-un proiect, am avut de ales intre partea clasica pe internet DS1307, 3231 (pe I2C) sau MCP795x0 (pe SPI), etc., sau sa lucrez cu RTCC-ul inclus in cateva familii PIC 16F191xx, 18FJ si 18FK.

 

Cel mai mic PIC (care va putea adresa si un LCD/GLCD/TFT - depinde de model) cu RTCC este 16F19155 (14 pini). Toate sub 2$ bucata la producator dar intre 2-3$ la vanzare (TME.eu).

 

NB: al doilea C din RTCC vine de la calendar.

 

Eu l-am incercat pe un 18F46J50 la care aveam deja un montaj facut. Am folosit oscilatorul intern RC cu PLL, astfel ca PIC-ul rula la 48MHz. Performantele au fost mediocre, dar intrucat pinii T1OSO si T1OSI (la 18FJ sau SOSCO/SOSCI la 18FK) erau ocupati n-am putut sa pun un quartz de 32,768kHz. Evident ca un quartz este obligatioriu si va face o mare diferenta in performante.

 

PIC-ul este conectat la un TFT, codul de mai jos nu face initializarea lui, etc. N-am stat sa curat mai mult codul prezentat. Nu sugerez TFT-ul caci refresh-ul se face aiurea (daca folosesti bibliotecile Mikroe) fata de un LCD sau GLCD.

 

Capturi de cod pentru Mikrobasic, dar cu putin efort pot pune si in Mikroc.

 

Variable folosite:

' Declarations section
dim year, day, month, wekday, hours, seconds, minutes            as word
    txt                                                          as string[4] 

Initializare RTCC:

' RTCC Init
sub procedure Init_RTCC()
  asm
    movlb 0x0F
    bcf INTCON, GIE
    movlw 0x55
    movwf EECON2
    movlw 0xAA
    movwf EECON2
    bsf RTCCFG,RTCWREN
  end asm

  PADCFG1.RTSECSEL1 = 1
  PADCFG1.RTSECSEL0 = 0

  RTCCFG     = 0x23 ' set pointer to Year
  RTCVALL    = 0x18 ' set Year to 2018
  RTCCFG     = 0x22 ' set pointer to Month & Day
  RTCVALL    = 0x20 ' set Day to the 20th
  RTCVALH    = 0x12 ' set Month to Jan (decrements pointer to Weekday & Hours)
  RTCVALL    = 0x13 ' set Hour to 13
  RTCVALH    = 0x06 ' 0=Sun, 6=Sat. set Weekday (decrements pointer to Minutes & Seconds)
  RTCVALL    = 0x00 ' set Seconds to 00
  RTCVALH    = 0x15 ' set Minutes to 00
  RTCCFG.RTCEN   = 1 ' RTCC enabled
  Delay_ms(800) '

  asm
    movlb 0x0F
    bcf INTCON, GIE
    movlw 0x55
    movwf EECON2
    movlw 0xAA
    movwf EECON2
    bcf RTCCFG,RTCWREN
  end asm

end sub

In procedura de initializare a PIC-ului (felul meu de a lucra) se va apela aceasta Init_RTCC().

 

In bucla principala se introduce:

' RTCC TFT update
   RTCCFG  = 3                                ' set pointer to Year
   year    = RTCVALL                          ' read Year
   RTCCFG  = 2                                ' set pointer to Month & Day
   day     = RTCVALL                          ' read Day
   month   = RTCVALH                          ' read Month & decrement pointer
   RTCCFG  = 1                                ' set pointer to Month & Day
   hours   = RTCVALL                          ' read Hours
   wekday  = RTCVALH                          ' read Weekday & decrement pointer
   RTCCFG  = 0                                ' set pointer to Month & Day
   seconds = RTCVALL                          ' read Seconds
   minutes = RTCVALH                          ' read Minutes (pointer already at 0)

   hours    = (((hours   and 0x30) >> 4) * 10) + (hours   and 0x0F)
   minutes  = (((minutes and 0x70) >> 4) * 10) + (minutes and 0x0F)
   seconds  = (((seconds and 0x70) >> 4) * 10) + (seconds and 0x0F)
   year     = (((year    and 0xF0) >> 4) * 10) + (year    and 0x0F)
   month    = (((month   and 0x10) >> 4) * 10) + (month   and 0x0F)
   day      = (((day     and 0x30) >> 4) * 10) + (day     and 0x0F)

' Time
   TFT_Set_Pen(CL_GRAY, 1)
   TFT_Set_Brush(1, 0, 1, LEFT_TO_RIGHT, CL_GRAY, CL_GRAY)
   TFT_Rectangle(230, 5, 310, 60)
   TFT_Set_Font(@TFT_defaultFont, CL_AQUA, FO_HORIZONTAL)
   ByteToStr(hours, txt)
   TFT_Write_Text(txt, 232, 13)
   TFT_Write_Text(":", 251, 13)
   ByteToStr(minutes, txt)
   TFT_Write_Text(txt, 256, 13)
   TFT_Write_Text(":", 275, 13)
   ByteToStr(seconds, txt)
   TFT_Write_Text(txt, 280, 13)
' Date
   TFT_Write_Text("20", 232, 30)
   ByteToStr(year, txt)
   TFT_Write_Text(txt, 245, 30)
   TFT_Write_Text("/", 264, 30)
   ByteToStr(month, txt)
   TFT_Write_Text(txt, 269, 30)
   TFT_Write_Text("/", 287, 30)
   ByteToStr(day, txt)
   TFT_Write_Text(txt, 293, 30)
Link to comment
  • Replies 0
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.Terms of Use si Guidelines