#include "myyyh.h"
#include "DS3231.h"
#define NUM_I2C_DATA 8
#define SLAVE_ADDRESS 0x68 //0111100 11010000 01101000
void delay(void)
{
uint32_t i;
for(i = 200;i>1;i–);
}
void InitConsole(void)
{
SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
GPIOPinConfigure(GPIO_PA0_U0RX);
GPIOPinConfigure(GPIO_PA1_U0TX);
GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
//UART0/???115200/????50MHZ
UARTStdioConfig(0,115200,50000000);
}
/*******ÏòµØַдÈë¶à×Ö½Ú*********/
void SendByte_More(uint8_t ucAddr, uint8_t *ucDa,uint32_t NUM)
{
unsigned int ulindex;
I2CMasterSlaveAddrSet(I2C0_BASE, SLAVE_ADDRESS, false);
//
I2CMasterDataPut(I2C0_BASE, ucAddr);
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_START);
//
while(I2CMasterBusy(I2C0_BASE));
//while(!(I2CSlaveStatus(I2C0_BASE) & I2C_SLAVE_ACT_RREQ));
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_CONT);
for(ulindex = 0; ulindex < NUM; ulindex++)
{
I2CMasterDataPut(I2C0_BASE, ucDa[ulindex]);
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_CONT);
while(I2CMasterBusy(I2C0_BASE));
//while(!(I2CSlaveStatus(I2C0_BASE) & I2C_SLAVE_ACT_RREQ));
}
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_FINISH);
}
/*******ÏòµØַдµ¥×Ö½Ú*********/
void SendByte_To_Adress(uint8_t ucAddr, uint8_t ucDa)
{
I2CMasterSlaveAddrSet(I2C0_BASE, SLAVE_ADDRESS, false);
I2CMasterDataPut(I2C0_BASE, ucAddr);
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_START);
//while(!(I2CSlaveStatus(I2C0_BASE) & I2C_SLAVE_ACT_RREQ));
while(I2CMasterBusy(I2C0_BASE));
//while(!(I2CSlaveStatus(I2C0_BASE) & I2C_SLAVE_ACT_RREQ));
I2CMasterDataPut(I2C0_BASE, ucDa);
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_CONT);
//I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_SEND);
//while(!(I2CSlaveStatus(I2C0_BASE) & I2C_SLAVE_ACT_RREQ));
while(I2CMasterBusy(I2C0_BASE));
//I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_FINISH);
}
/******¶ÁÒ»¸ö×Ö½Ú********/
uint8_t RcvByte(void)
{
uint8_t Value;
I2CMasterSlaveAddrSet(I2C0_BASE, SLAVE_ADDRESS, true);
I2CMasterControl(I2C0_BASE,I2C_MASTER_CMD_BURST_RECEIVE_START);
//while(!(I2CSlaveStatus(I2C0_BASE) & I2C_SLAVE_ACT_TREQ));
Value = I2CMasterDataGet(I2C0_BASE);
while(I2CMasterBusy(I2C0_BASE));
return Value;
}
/*********ÔÚÒ»¸öµØÖ·¶ÁÒ»¸ö×Ö½Ú***************/
uint8_t RecByte_From_Adress(uint8_t ucAddr)
{
uint8_t Value;
I2CMasterSlaveAddrSet(I2C0_BASE, SLAVE_ADDRESS, false);
I2CMasterDataPut(I2C0_BASE, ucAddr);
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_SEND);
//while(!(I2CSlaveStatus(I2C0_BASE) & I2C_SLAVE_ACT_RREQ));
while(I2CMasterBusy(I2C0_BASE));
//I2CMasterControl(I2C0_BASE,I2C_MASTER_CMD_BURST_SEND_FINISH);
I2CMasterSlaveAddrSet(I2C0_BASE, SLAVE_ADDRESS, true);
I2CMasterControl(I2C0_BASE,I2C_MASTER_CMD_SINGLE_RECEIVE);
//while(!(I2CSlaveStatus(I2C0_BASE) & I2C_SLAVE_ACT_TREQ));
while(I2CMasterBusy(I2C0_BASE));
Value = I2CMasterDataGet(I2C0_BASE);
//I2CMasterControl(I2C0_BASE,I2C_MASTER_CMD_BURST_RECEIVE_FINISH);
return Value;
}
void Write_DS13231Init(uint8_t *p)
{
SendByte_To_Adress(DS3231_SECOND,p[0]);
SendByte_To_Adress(DS3231_MINUTE,p[1]);
SendByte_To_Adress(DS3231_HOUR, p[2]);
SendByte_To_Adress(DS3231_WEEK, p[3]);
SendByte_To_Adress(DS3231_DAY, p[4]);
SendByte_To_Adress(DS3231_MONTH, p[5]);
SendByte_To_Adress(DS3231_YEAR, p[6]);
}
void Read_DS13231Init(uint8_t *pp)
{
pp[0] = RecByte_From_Adress(DS3231_SECOND);
pp[1] = RecByte_From_Adress(DS3231_MINUTE);
pp[2] = RecByte_From_Adress( DS3231_HOUR);
pp[3] = RecByte_From_Adress( DS3231_WEEK);
pp[4] = RecByte_From_Adress( DS3231_DAY);
pp[5] = RecByte_From_Adress( DS3231_MONTH);
pp[6] = RecByte_From_Adress( DS3231_YEAR);
}
void User_Inint(void)
{
SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ);
SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
GPIOPadConfigSet(GPIO_PORTB_BASE,GPIO_PIN_2,GPIO_STRENGTH_8MA,GPIO_PIN_TYPE_STD_WPU);
GPIOPadConfigSet(GPIO_PORTB_BASE,GPIO_PIN_3,GPIO_STRENGTH_8MA,GPIO_PIN_TYPE_OD);
GPIOPinConfigure(GPIO_PB2_I2C0SCL);
GPIOPinConfigure(GPIO_PB3_I2C0SDA);
GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_3);
GPIOPinTypeI2CSCL(GPIO_PORTB_BASE, GPIO_PIN_2);
//HWREG(I2C0_BASE + I2C_O_MCR) |= 0x01;
I2CMasterInitExpClk(I2C0_BASE, SysCtlClockGet(), true);
I2CMasterEnable(I2C0_BASE);
}
int main(void)
{
uint8_t ulDataTx[NUM_I2C_DATA]= {0x01,0x31,0x12,0x05,0x010,0x07,0x15};
uint8_t ulDataRx[NUM_I2C_DATA] = {0,0,0,0,0,0,0,0};
User_Inint();
InitConsole();
Write_DS13231Init(ulDataTx);
Read_DS13231Init(ulDataRx);
while(1);
}
#ifndef _MYYYH_H
#define _MYYYH_H
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <math.h>
#include "inc/tm4c123gh6pm.h"
#include "inc/hw_types.h"
#include "inc/hw_memmap.h"
#include "driverlib/sysctl.h"
#include "driverlib/pin_map.h"
#include "grlib/grlib.h"
#include "driverlib/gpio.h"
#include "driverlib/interrupt.h"
#include "driverlib/adc.h"
#include "inc/hw_gpio.h"
#include "driverlib/timer.h"
#include "inc/hw_ssi.h"
#include "driverlib/pin_map.h"
#include "driverlib/ssi.h"
#include "driverlib/sysctl.h"
#include "driverlib/rom.h"
#include "driverlib/pwm.h"
#include "driverlib/fpu.h"
#include "driverlib/qei.h"
#include "inc/hw_i2c.h"
#include "driverlib/i2c.h"
#include "utils/uartstdio.h"
#include "driverlib/uart.h"
#endif
#ifndef _DS3231_H
#define _DS3231_H
#define DS3231_WriteAddress 0xD0 //器件写地址
#define DS3231_ReadAddress 0xD1 //器件读地址
#define DS3231_SECOND 0x00 //秒
#define DS3231_MINUTE 0x01 //分
#define DS3231_HOUR 0x02 //时
#define DS3231_WEEK 0x03 //星期
#define DS3231_DAY 0x04 //日
#define DS3231_MONTH 0x05 //月
#define DS3231_YEAR 0x06 //年
//闹钟1#define DS3231_SALARM1ECOND 0x07 //秒
#define DS3231_ALARM1MINUTE 0x08 //分
#define DS3231_ALARM1HOUR 0x09 //时
#define DS3231_ALARM1WEEK 0x0A //星期/日
//闹钟2
#define DS3231_ALARM2MINUTE 0x0b //分
#define DS3231_ALARM2HOUR 0x0c //时
#define DS3231_ALARM2WEEK 0x0d //星期/日
#define DS3231_CONTROL 0x0e //控制寄存器
#define DS3231_STATUS 0x0f //状态寄存器
#define BSY 2 //忙
#define OSF 7 //振荡器停止标志位
#define DS3231_XTAL 0x10 //晶体老化寄存器
#define DS3231_TEMPERATUREH 0x11 //稳定寄存器高字节(8)
#define DS3231_TEMPERATUREL 0x12 //湿度寄存器低字节(高2位)#endif
可以用,分享一下,我也是在这里借鉴了点。
xyz549040622:
感谢楼主的分享,顺便提出一个问题,代码放在一起不好,建议按照模块分到几个.c文件中。
zhixiang Luo:
回复 Eason Wang:
I don't know should choose which forum published
zhixiang Luo:
回复 xyz549040622:
其实是不想分几次上传而已。