1. В сообществе нашего форума Вконтакте создан раздел по продаже электронных компонентов.
    Каждый может продать в нем свои залежавшиеся детали. Подробности здесь.

Туториал STM32F103C8T6 RTC (MikroBasic)

Тема в разделе "STM8/STM32", создана пользователем Дмитрий BASIC, 20 янв 2018.

  1. Дмитрий BASIC

    Дмитрий BASIC В доску свой

    Сообщения:
    185
    Симпатии:
    100
    Ох и намучался с запуском часиков но все же победил !
    А дело было в неправильно запаяном кварце перепаял и усе заработало :)



    Код:
    program RTC_internal
    include "ili_9341_lib"
    
    
    structure TimeStructure  'Структура для времени
    dim ss as byte ' seconds
    dim mn as byte ' minutes
    dim hh as byte ' hours
    dim md as byte ' day in month, from 1 to 31
    dim wd as byte ' day in week, monday=0, tuesday=1, .... sunday=6
    dim mo as byte ' month number, from 1 to 12 (and not from 0 to 11 as with unix C time !)
    dim yy as word ' year Y2K compliant, from 1892 to 2038
    end structure
    dim sec_str, 'строковые переменные для Времени
    min_str,
    hor_str,
    dat_str,
    day_str,
    mon_str as string[3]
    dim yer_str as string[5]
    
    dim time_2 as TimeStructure
    dim time_1 as longint
    dim time_0 as longint
    '==========================================================
    'Функция Включения LowSpeed Internal generator 40000 Hz
    sub function LSI_ON() as Boolean
    dim TimeOutCnt as Integer
    RCC_CSR.LSION = 1 '// try to switch on
    TimeOutCnt = 10   '// define maximum timeout 10 * 10 ms = 100 ms
    while TimeOutCnt > 0  '// while timeout is not over
    if RCC_CSR.LSIRDY = 1 then  '// check LSI state
    Break      '// break if ready
    else
    dec(TimeOutCnt)   ' // decrement timeout
    delay_ms(10)   '// wait more 10 ms
    end if
    wend
    Result = RCC_CSR.LSIRDY
    end sub
    'Функция Включения LowSpeed External generator 32768 Hz
    sub function LSE_ON() as Boolean
    dim TimeOutCnt as Integer
      RCC_BDCR.LSEON = 1 ' // try to switch on
      TimeOutCnt = 50    '// define maximum timeout 50 * 10 ms = 500 ms
      while TimeOutCnt > 0   '// while timeout is not over
       if RCC_BDCR.LSERDY = 1 then '// check LSI state
        Break ' // break if ready
        else
        dec(TimeOutCnt) ' // wait more 10 ms
        delay_ms(10)
        end if
    wend
    Result = RCC_BDCR.LSERDY
    end sub
    'Запуск от LSI генератора
    sub function RTC_Init_LSI() as boolean
    ' //Init power and clock source for RTC
      RCC_APB1ENR.PWREN = 1 '  // Power interface clock enable
      RCC_APB1ENR.BKPEN = 1 '  // Backup interface clock enable
      PWR_CR.DBP = 1 '         // enable access
      if RCC_BDCR.RTCEN = 0 then
      RCC_BDCR.RTCEN = 1 ';     // RTC clock enable
      RCC_BDCR.RTCSEL0 = 0 '   // 10: LSI oscillator clock used as RTC
      RCC_BDCR.RTCSEL1 = 1 '   // 10: LSI oscillator clock used as RTC
      if LSI_ON then
      while RTC_CRL.RTOFF = 0 ' // wait for prior write finished
      wend
        '// setup prescaler
        RTC_CRL.CNF = 1 '       // enter config mode
        RTC_PRLL = 40000 '      // RTC-clock-prescaler
        RTC_CRL.CNF = 0 '       // Configmode off (write data)
        while RTC_CRL.RTOFF = 0    '// wait for prior write finished
        wend
       PWR_CR.DBP = 0 '         // Disable access to RTC
       Result = True
      else
        Result = False
        end if
        else
        Result = False
        end if
    end sub
    'Запуск от LSE генератора
    sub function RTC_Init_LSE() as Boolean
    '//Init power and clock source for RTC
      RCC_APB1ENR.BKPEN = 1 ';  // Backup interface clock enable
      RCC_APB1ENR.PWREN = 1 ';  // Power interface clock enable
      PWR_CR.DBP = 1 ';         // enable access
      if RCC_BDCR.RTCEN = 0 then
      RCC_BDCR.RTCEN = 1 ';     // RTC clock enable
      RCC_BDCR.RTCSEL0 = 1 ';   // 01: LSE oscillator clock used as RTC
      RCC_BDCR.RTCSEL1 = 0 ';   // 01: LSE oscillator clock used as RTC
      if LSE_ON then
        while RTC_CRL.RTOFF = 0 ' // wait for prior write finished
        wend
        '// setup prescaler
        RTC_CRL.CNF = 1 '       // Config mode ON
        RTC_PRLL =  32768 '      // RTC-clock-prescaler
        RTC_CRL.CNF = 0 '       // Config mode OFF (write data)
        while RTC_CRL.RTOFF = 0  '// wait for prior write finished
        wend
       PWR_CR.DBP = 0  ' // Disable access to RTC
       Result = True
      else
      Result = False
      end if
      else
       Result = False
      end if
    end sub
    main:
    'Конфигурируем Gpio для дисплея на выход
    GPIO_Config(@GPIOA_BASE,
    _GPIO_PINMASK_1 or
    _GPIO_PINMASK_2 or
    _GPIO_PINMASK_3 or
    _GPIO_PINMASK_4,
    _GPIO_CFG_MODE_OUTPUT or
    _GPIO_CFG_SPEED_2MHZ or
    _GPIO_CFG_OTYPE_PP)
    'Конфигурируем SPI для дисплея [Miso (pin 9)=GpioA6];[Mosi (pin 6)=GpioA7];[Sck(pin 7)=GpioA5]
    SPI1_Init_Advanced(_SPI_FPCLK_DIV2,
    _SPI_MASTER OR
    _SPI_8_BIT OR
    _SPI_CLK_IDLE_LOW OR
    _SPI_FIRST_CLK_EDGE_TRANSITION OR
    _SPI_MSB_FIRST OR
    _SPI_SS_DISABLE OR
    _SPI_SSM_ENABLE OR
    _SPI_SSI_1,
    @_GPIO_MODULE_SPI1_PA567)
    '============================================
    GPIO_digital_output(@GPIOC_BASE, _GPIO_PINMASK_13)
    Tft_led=1
    '==============================================
    Tft_reset()
    Tft_init()
    RTC_Init_LSE()
    Tft_fill_screen(0,0,239,319,blue)
     'Установка начального времени
        time_2.ss = 0
        time_2.mn = 08
        time_2.hh = 23
        time_2.md = 20
        time_2.mo = 1
        time_2.yy = 2018
        time_0 = Time_dateToEpoch(@time_2)
    
    while true
    time_1 = RTC_CNTH<<16+RTC_CNTL
    time_1=time_1+time_0
    Time_epochToDate(time_1,@time_2)
    ByteToStr(time_2.ss,sec_str)
    ByteToStr(time_2.mn,min_str)
    ByteToStr(time_2.hh,hor_str)
    ByteToStr(time_2.md,dat_str)
    ByteToStr(time_2.wd,day_str)
    ByteToStr(time_2.mo,mon_str)
    wordToStr(time_2.yy,yer_str)
    
    Tft_print_6x8(sec_str,100,10,WHITE,BLue,2,2)
      Tft_print_6x8(min_str,60,10,WHITE,BLue,2,2)
       Tft_print_6x8(hor_str,20,10,WHITE,BLue,2,2)
        Tft_print_6x8(dat_str,20,30,red,BLACK,2,2)
          Tft_print_6x8(mon_str,60,30,YELLOW,BLACK,2,2)
           Tft_print_6x8(yer_str,100,30,GREEN,BLACK,2,2)
    
    delay_ms(950)
    
    
    wend
     
    : stm32f103c8t6, rtc, часы
  2. Дмитрий BASIC

    Дмитрий BASIC В доску свой

    Сообщения:
    185
    Симпатии:
    100

Поделиться этой страницей