I finally give it some try, but the code does not looks like a picbasic? It seems like it is using a different syntax to call subroutines? (can't verify right now, do not have PBP on this PC)
Code:
' PICBASIC PRO code to read hours and minutes from DS3231 and send over serial port
' Define PIC16F877A configuration bits
#CONFIG
CONFIG FOSC = HS ; High-speed oscillator
CONFIG WDTE = OFF ; Watchdog Timer disabled
CONFIG PWRTE = ON ; Power-up Timer enabled
CONFIG BOREN = ON ; Brown-out Reset enabled
CONFIG LVP = OFF ; Low-Voltage Programming disabled
CONFIG CPD = OFF ; Data memory code protection disabled
CONFIG WRT = OFF ; Flash Program Memory Write protection off
CONFIG CP = OFF ; Flash Program Memory Code protection off
#ENDCONFIG
' Define constants
DS3231_SDA VAR PORTB.0 ; DS3231 Serial Data line
DS3231_SCL VAR PORTB.1 ; DS3231 Serial Clock line
SERIAL_TX VAR PORTD.1 ; Serial transmit pin
' Define variables
Hours VAR BYTE ; Variable to store hours
Minutes VAR BYTE ; Variable to store minutes
' Main program
MAIN:
TRISB.0 = 1 ; Set DS3231 SDA pin as input
TRISB.1 = 1 ; Set DS3231 SCL pin as input
TRISD.1 = 0 ; Set serial transmit pin as output
' Initialize I2C communication
I2CSetup DS3231_SDA, DS3231_SCL, 100000
' Main loop
DO
' Read hours from DS3231
I2CStart
I2CSend($D0) ; DS3231 I2C address for writing
I2CSend($00) ; Send the register address for hours
I2CStart
I2CSend($D1) ; DS3231 I2C address for reading
Hours = I2CRead(1) ; Read hours and send acknowledgment
I2CStop
' Read minutes from DS3231
I2CStart
I2CSend($D0) ; DS3231 I2C address for writing
I2CSend($01) ; Send the register address for minutes
I2CStart
I2CSend($D1) ; DS3231 I2C address for reading
Minutes = I2CRead(0) ; Read minutes and send acknowledgment
I2CStop
' Send hours and minutes over serial port
SEROUT SERIAL_TX, T9600_16, ["Time: ", DEC Hours, ":", DEC2 Minutes, 13, 10]
PAUSE 1000 ; Delay for some time before reading again
LOOP
' Subroutine to initialize I2C communication
I2CSetup:
I2CWrite SDApin, SCLpin, I2C_HIGH ; Set pins and speed
RETURN
Bookmarks