本帖最后由 wxdn 于 2019-5-22 04:46 编辑
调试了好久没有调出来,实在是没有办法,用STC8的内置IIC,一次通过了,后来才在STC15里面用端口模拟出来,分享给大家。
/**********************************************************************
主芯片:STC15W4K32S4 QFP-48 芯片供电:3.30V 时钟:内置11.0592M 工程建立日期:2019.05.22
**********************************************************************/
#include"RX8025.h"
#include "STC15F2K60S2.H"
#include"delays.h"
sbit scl = P2^4;//
sbit sda = P2^5;//
/* 秒 分钟 小时 日 月 星期 */
unsigned char TIM_Second,TIM_Minute,TIM_Hour,TIM_Date,TIM_Month,TIM_Week;
unsigned int TIM_Year; //年
void Delay_us(unsigned char i)
{
while(i--);
}
void start(void)
{
scl=1;
Delay_us(5);
sda=0;
Delay_us(5);
scl=0;
}
void stop(void)
{
sda=0;
Delay_us(5);
scl=1;
Delay_us(5);
sda=1;
Delay_us(5);
}
void iicinit(void)
{
scl=1;
Delay_us(5);
sda=1;
}
unsigned char iicwr_byte(unsigned char dat)//写数据
{
unsigned char i;
sda=1;
for(i=0;i<8;i++)
{
scl=0;
Delay_us(2);
if(dat&0x80)
{
sda=1;
}
else
{
sda=0;
}
dat<<=1;
scl=1;
Delay_us(5);
}
scl=0;
Delay_us(5);
sda=1;
scl=1;
Delay_us(5);
scl=0;
i=sda;
return i;
}
unsigned char iicre_byte(unsigned char x)//读数据 x:1为ACK,x:0为NACK
{
unsigned char i,dat;
sda=1;
for(i=0;i<8;i++)
{
scl=0;
Delay_us(2);
dat<<=1;
dat|=sda;
scl=1;
Delay_us(5);
}
scl=0;
if(x)
{
sda=0;
}
else
{
sda=1;
}
Delay_us(5);
scl=1;
Delay_us(5);
scl=0;
return dat;
}
void READ_TIM(void)
{
start();
iicwr_byte(0x64);
Delay_us(50);
iicwr_byte(0x00);
Delay_us(50);
start();
iicwr_byte(0x65);
Delay_us(50);
TIM_Second=iicre_byte(1);
Delay_us(50);
TIM_Minute=iicre_byte(1);
Delay_us(50);
TIM_Hour=iicre_byte(0);
Delay_us(50);
stop();
}
void READ_Date(void)
{
start();
iicwr_byte(0x64);
Delay_us(50);
iicwr_byte(0x03);
Delay_us(50);
start();
iicwr_byte(0x65);
Delay_us(50);
TIM_Week=iicre_byte(1);
Delay_us(50);
TIM_Date=iicre_byte(1);
Delay_us(50);
TIM_Month=iicre_byte(0);
Delay_us(50);
stop();
}
void SET_YEAR(unsigned char YE)
{
start();
iicwr_byte(0x64);
Delay_us(50);
iicwr_byte(0x06);
Delay_us(50);
iicwr_byte(YE);
Delay_us(50);
stop();
}
void SET_DAY(unsigned char M,unsigned char D)
{
start();
iicwr_byte(0x64);
Delay_us(50);
iicwr_byte(0x04);
Delay_us(50);
iicwr_byte(D);
Delay_us(50);
iicwr_byte(M);
Delay_us(50);
stop();
}
void SET_WEEK(unsigned char W)
{
start();
iicwr_byte(0x64);
Delay_us(50);
iicwr_byte(0x03);
Delay_us(50);
iicwr_byte(W);
Delay_us(50);
stop();
}
void SET_TIME(unsigned char H,unsigned char M,unsigned char S)
{
start();
iicwr_byte(0x64);
Delay_us(50);
iicwr_byte(0x00);
Delay_us(50);
iicwr_byte(S);
Delay_us(50);
iicwr_byte(M);
Delay_us(50);
iicwr_byte(H);
Delay_us(50);
stop();
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
softiic.h
- #ifndef _SOFT_IIC_H_
- #define _SOFT_IIC_H_
- #ifdef __cplusplus
- extern "C"
- {
- #endif
- #include <stdint.h>
- typedef struct
- {
- uint8_t dev_addr;
- //配置clk dat 端口操作接口
- void (*clk)(uint8_t chByte);
- void (*dat)(uint8_t chByte);
- uint8_t (*get_dat)(void);
- void (*delay)(void); //延时函数
- } softiic_t;
- extern void softiic_interface_register(softiic_t *iic,
- uint8_t dev_addr,
- void (*clk)(uint8_t chByte),
- void (*dat)(uint8_t chByte),
- uint8_t (*get_dat)(void),
- void (*delay)(void));
- extern void softiic_start(softiic_t *iic);
- extern void softiic_stop(softiic_t *iic);
- extern void softiic_tack(softiic_t *iic);
- extern void softiic_tnack(softiic_t *iic);
- extern int softiic_ckack(softiic_t *iic);
- extern void softiic_write_byte(softiic_t *iic, uint8_t chByte);
- extern void softiic_read_byte(softiic_t *iic, uint8_t *pchByte);
- extern void softiic_write_buff(softiic_t *iic, uint8_t addr, uint16_t len, uint8_t *buff);
- extern void softiic_read_buff(softiic_t *iic, uint8_t addr, uint16_t len, uint8_t *buff);
- extern void softiic_write_bits(softiic_t *iic, uint8_t addr, uint8_t mask, uint8_t data);
- extern void softiic_read_bits(softiic_t *iic, uint8_t addr, uint8_t mask, uint8_t *data);
- #ifdef __cplusplus
- }
- #endif
- #endif
复制代码softiic.c
- #include "softiic.h"
- void softiic_start(softiic_t *iic)
- {
- // iic->clk(0);
- // iic->delay();
- iic->dat(1);
- iic->delay();
- iic->clk(1);
- iic->delay();
- iic->dat(0);
- iic->delay();
- iic->clk(0);
- iic->delay();
- }
- void softiic_stop(softiic_t *iic)
- {
- iic->dat(0);
- iic->delay();
- iic->clk(1);
- iic->delay();
- iic->dat(1);
- iic->delay();
- // iic->dat(0);
- // iic->delay();
- iic->clk(0);
- iic->delay();
- }
- void softiic_tack(softiic_t *iic)
- {
- iic->clk(0);
- iic->delay();
- iic->dat(0);
- iic->delay();
- iic->clk(1);
- iic->delay();
- iic->clk(0);
- iic->delay();
- }
- void softiic_tnack(softiic_t *iic)
- {
- iic->clk(0);
- iic->delay();
- iic->dat(1);
- iic->delay();
- iic->clk(1);
- iic->delay();
- iic->clk(0);
- iic->delay();
- }
- // NOTE 注意检测延时
- int softiic_ckack(softiic_t *iic)
- {
- uint16_t timeout = 0;
- iic->dat(1);
- iic->delay();
- iic->clk(1);
- iic->delay();
- while (iic->get_dat()) {
- timeout++;
- if (timeout >= 10) {
- iic->clk(0);
- iic->delay();
- return 1;
- }
- }
- iic->clk(0);
- iic->delay();
- return 0;
- }
- void softiic_write_byte(softiic_t *iic, uint8_t chByte)
- {
- uint8_t i;
- for (i = 0; i < 8; i++) {
- if (chByte & 0x80) {
- iic->dat(1);
- } else {
- iic->dat(0);
- }
- iic->delay();
- iic->clk(1);
- iic->delay();
- iic->clk(0);
- iic->delay();
- chByte = chByte << 1;
- }
- }
- void softiic_read_byte(softiic_t *iic, uint8_t *pchByte)
- {
- uint8_t i;
- for (i = 0; i < 8; i++) {
- iic->dat(1);
- iic->delay();
- iic->clk(1);
- iic->delay();
- *pchByte = *pchByte << 1;
- if (iic->get_dat()) {
- *pchByte = *pchByte | 0x01;
- }
- iic->delay();
- iic->clk(0);
- iic->delay();
- }
- }
- void softiic_read_buff(softiic_t *iic, uint8_t addr, uint16_t len, uint8_t *buff)
- {
- uint8_t chByte;
- softiic_start(iic);
- softiic_write_byte(iic, iic->dev_addr);
- softiic_ckack(iic);
- softiic_write_byte(iic, addr);
- softiic_ckack(iic);
- softiic_start(iic);
- softiic_write_byte(iic, iic->dev_addr | 0x01);
- softiic_ckack(iic);
- while (len) {
- softiic_read_byte(iic, &chByte);
- *buff = chByte;
- if (len == 1) {
- softiic_tnack(iic);
- } else {
- softiic_tack(iic);
- }
- buff++;
- len--;
- }
- softiic_stop(iic);
- }
- void softiic_write_buff(softiic_t *iic, uint8_t addr, uint16_t len, uint8_t *buff)
- {
- softiic_start(iic);
- softiic_write_byte(iic, iic->dev_addr);
- softiic_ckack(iic);
- softiic_write_byte(iic, addr);
- softiic_ckack(iic);
- while (len) {
- softiic_write_byte(iic, *buff);
- softiic_ckack(iic);
- buff++;
- len--;
- }
- softiic_stop(iic);
- }
- void softiic_write_bits(softiic_t *iic, uint8_t addr, uint8_t mask, uint8_t data)
- {
- uint8_t chByte;
- softiic_read_buff(iic, addr, 1, &chByte);
- chByte &= ~mask;
- chByte |= data;
- softiic_write_buff(iic, addr, 1, &chByte);
- }
- void softiic_read_bits(softiic_t *iic, uint8_t addr, uint8_t mask, uint8_t *data)
- {
- uint8_t chByte;
- softiic_read_buff(iic, addr, 1, &chByte);
- chByte &= mask;
- *data = chByte;
- }
- /**
- * @brief IIC接口注册函数
- * @note
- * @param *iic:
- * @param (*clk:
- * @retval None
- */
- void softiic_interface_register(softiic_t *iic,
- uint8_t dev_addr,
- void (*clk)(uint8_t chByte),
- void (*dat)(uint8_t chByte),
- uint8_t (*get_dat)(void),
- void (*delay)(void))
- {
- iic->clk = clk;
- iic->dat = dat;
- iic->get_dat = get_dat;
- iic->delay = delay;
- iic->dev_addr = dev_addr;
- }
复制代码rtc_se8025.h
- #ifndef __RTC_SE8025_H__
- #define __RTC_SE8025_H__
- #include "app.h"
- #include "softiic.h"
- extern void rtc_se8025_init(void);
- extern void rtc_se8025_task(void);
- extern void rtc_se8025_set_time(uint8_t *buff);
- #endif
复制代码rtc_se8025.c
- #include "rtc_se8025.h"
- #define RTC_TIME(n) ((n) / 100)
- #if 1
- #define SE8025_DEBUG(...)
- #else
- #define SE8025_DEBUG(...)
- do {
- debug_printf("[RTC]");
- debug_printf(__VA_ARGS__);
- } while (0)
- #endif
- #define BCD2CONVER(__VALUE, __MAST) ((__VALUE & (__MAST)) >> 4) * 10 + ((__VALUE)&0X0F) // BCD转HEX
- #define SCL_H()
- do {
- HAL_GPIO_WritePin(RTC_CLK_PIN_GPIO_Port, RTC_CLK_PIN_Pin, GPIO_PIN_SET);
- } while (0)
- #define SCL_L()
- do {
- HAL_GPIO_WritePin(RTC_CLK_PIN_GPIO_Port, RTC_CLK_PIN_Pin, GPIO_PIN_RESET);
- } while (0)
- #define SDA_H()
- do {
- HAL_GPIO_WritePin(RTC_DAT_PIN_GPIO_Port, RTC_DAT_PIN_Pin, GPIO_PIN_SET);
- } while (0)
- #define SDA_L()
- do {
- HAL_GPIO_WritePin(RTC_DAT_PIN_GPIO_Port, RTC_DAT_PIN_Pin, GPIO_PIN_RESET);
- } while (0)
- #define SDA_IS_H() (HAL_GPIO_ReadPin(RTC_DAT_PIN_GPIO_Port, RTC_DAT_PIN_Pin) != GPIO_PIN_RESET)
- static void clk(uint8_t chByte)
- {
- if (chByte) {
- SCL_H();
- } else {
- SCL_L();
- }
- }
- static void dat(uint8_t chByte)
- {
- if (chByte) {
- SDA_H();
- } else {
- SDA_L();
- }
- }
- static uint8_t get_dat(void)
- {
- if (SDA_IS_H()) {
- return 1;
- } else {
- return 0;
- }
- }
- static void delay()
- {
- uint16_t i = 2;
- while (i--)
- ;
- }
- softiic_t se8025;
- void rtc_se8025_set_time(uint8_t *buff)
- {
- uint8_t array[7];
- array[6] = (buff[0] / 10 * 16) + (buff[0] % 10);
- array[5] = (buff[1] / 10 * 16) + (buff[1] % 10);
- array[4] = (buff[2] / 10 * 16) + (buff[2] % 10);
- array[2] = (buff[3] / 10 * 16) + (buff[3] % 10);
- array[1] = (buff[4] / 10 * 16) + (buff[4] % 10);
- array[0] = (buff[5] / 10 * 16) + (buff[5] % 10);
- array[3] = 0;
- softiic_write_buff(&se8025, 0, 7, array);
- }
- void rtc_se8025_read_time()
- {
- uint8_t buff[16];
- softiic_read_buff(&se8025, 0, 16, buff);
- run_param.dateTime[5] = BCD2CONVER(buff[0], 0X70); //秒
- run_param.dateTime[4] = BCD2CONVER(buff[1], 0X70); //分
- run_param.dateTime[3] = BCD2CONVER(buff[2], 0X30); //时
- // run_param.dateTime[6] = buff[3]; //星期
- run_param.dateTime[2] = BCD2CONVER(buff[4], 0X30); //日
- run_param.dateTime[1] = BCD2CONVER(buff[5], 0X10); //月
- run_param.dateTime[0] = BCD2CONVER(buff[6], 0XF0); //年
- }
- void rtc_se8025_init(void)
- {
- softiic_interface_register(&se8025, 0x64, clk, dat, get_dat, delay);
- rtc_se8025_read_time();
- debug_printf("[RTC]20%02d-%02d-%02d %02d:%02d:%02d
", run_param.dateTime[0], run_param.dateTime[1],
- run_param.dateTime[2], run_param.dateTime[3], run_param.dateTime[4], run_param.dateTime[5]);
- }
- void rtc_se8025_task(void)
- {
- static uint16_t s_hwTimeCnt = 0;
- rtc_se8025_read_time();
- if (++s_hwTimeCnt >= RTC_TIME(1000)) {
- s_hwTimeCnt = 0;
- if (run_param.timeSyncCntDown) {
- run_param.timeSyncCntDown--;
- }
- SE8025_DEBUG("20%02d-%02d-%02d %02d:%02d:%02d
", run_param.dateTime[0], run_param.dateTime[1],
- run_param.dateTime[2], run_param.dateTime[3], run_param.dateTime[4], run_param.dateTime[5]);
- LED_FB_TOGGLE(LED_DBG2_PIN_GPIO_Port, LED_DBG2_PIN_Pin);
- }
- }
复制代码一周热门 更多>