Code Monkey home page Code Monkey logo

ds1302's Introduction

DS1302

makecode DS1302 RTC extension for micro:bit

Author: shaoziyang
Date: 2018.Mar

Add extension

open your microbit makecode project, in add extension, paste

https://github.com/makecode-extensions/DS1302

to search box then search.

Basic usage

let b = 0
let tmy: number[] = []
let a = 0
let tmx: number[] = []
let item: DS1302.DS1302RTC = null
let old = 0
let sec = 0
sec = 0
old = -1
item = DS1302.create(DigitalPin.P13, DigitalPin.P14, DigitalPin.P15)
tmx = [2, 3, 4, 4, 4, 3, 2, 1, 0, 0, 0, 1]
tmy = [0, 0, 1, 2, 3, 4, 4, 4, 3, 2, 1, 0]
basic.forever(() => {
    sec = item.getSecond()
    if (sec != old) {
        old = sec
        a = sec / 5
        b = sec % 5
        if (4 == b) {
            led.unplot(tmx[a], tmy[a])
        } else {
            led.plotBrightness(tmx[a], tmy[a], 20 + b * 20)
        }
        led.toggle(2, 2)
    }
    basic.pause(300)
}) 

API

  • function DateTime(year: number, month: number, day: number, weekday: number, hour: number, minute: number, second: number)
    set Date and Time.

  • function setSecond(dat: number)
    set second.

  • function getSecond(dat: number)
    get second.

  • function setMinute(dat: number)
    set minute.

  • function getMinute(dat: number)
    get minute.

  • function setHour(dat: number)
    set hour.

  • function getHour(dat: number)
    get hour.

  • function setWeekday(dat: number)
    set week day.

  • function getWeekday(dat: number)
    get week day.

  • function setDay(dat: number)
    set day.

  • function getDay(dat: number)
    get day.

  • function setMonth(dat: number)
    set month.

  • function getMonth(dat: number)
    get month.

  • function setYear(dat: number)
    set year.

  • function getYear(dat: number)
    get year.

  • writeRam(reg: number, dat: number)
    write data to ram
    reg: 0-30

  • readRam(reg: number)
    read data from ram
    reg: 0-30

Demo

License

MIT

Copyright (c) 2018, microbit/micropython Chinese community

Supported targets

  • for PXT/microbit

From microbit/micropython Chinese community

ds1302's People

Contributors

shaoziyang avatar

Stargazers

 avatar

Watchers

 avatar  avatar

ds1302's Issues

Bug reading BCD values when hardware not present or simulated

Hello @shaoziyang

Thank you for contributing this package.

Today, @edbye and I saw a program crash using this package. After much digging, we discovered that you use HexToDec to convert BCD numbers from the device to decimal.

Screen Shot 2019-11-20 at 16 48 07

For getWeekday, this causes an erroneous value to be returned to the user app, which can crash the user app.

Screen Shot 2019-11-20 at 16 48 24

Because inside the simulator, a digitalWritePin(1) means that the next digitalReadPin() will also return a 1, it means that inside the MakeCode simulator the routine returns FF.

This FF is then converted to Decimal like this:

(255>>4) * 10 + (255 % 16)
15*10 + 15
150 + 15
165

This returns day of week as 165, the user app then indexes into a 7 entry Array of String to turn it into the name of the day, element 165 does not exist, so it uses 'undefined', and a later displayString(undefined) causes the simulator to halt with a sad face.

@edbye also replicated this on the real hardware by holding the DIO pin high, and the user code suffers a similar fate.

Can we recommend that once you convert data, you range check it (and limit it perhaps to zero) in case of an out of range value, so that the user program does not have to do this?

The next operation of the user program is on button press to store a known value in the RTC chip registers - but this existing overflow behaviour creates a strange experience for users in the MakeCode simulator.

e.g. how about

getWeekday(): number {
            let d =HexToDec(this.getReg(DS1302_REG_WEEKDAY + 1))
            if (d < 1 || d > 7 ) return 1 
            return d
        }

Thanks!

VERSION TRACE

"name": "DS1302",
    "version": "1.1.0",
    "description": "makecode DS1302 RTC Package for micro:bit",

"installedVersion": "github:makecode-extensions/ds1302#fa167292547220ee2af362e90a2c86a63fc772db"

Chip valid ranges (from DS1302 datasheet)
https://datasheets.maximintegrated.com/en/ds/DS1302.pdf

Screen Shot 2019-11-20 at 16 59 53

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.