
-
UID:1933360
-
- 注册时间2015-11-02
- 最后登录2026-08-03
- 在线时间1320小时
-
-
访问TA的空间加好友用道具
|
前段时间在公司找出几块PCB板,做工还是很精致的,是原来一款产品上的红外原理触摸控制板,就是下图这块板子。现在这款产品早就不做了,就剩这几块板子,寻思着这板子有没有可以利用的剩余价值。
然后经过研究对比发现中间的48脚芯片其实就是一个STM32F103C8T6的单片机,而且上面有USB接口,USB的左边是SWD 下载接口,板子的反面有几个测试点是引出的GPIO 口。于是拿了一块板子经过一番改造变成下面这样子
连上DS3231模块和OLED 屏,用来练习写一个时钟程序。这两个模块都是I2C连接,挂在I2C总线上,两个GPIO 口就行了,I2C通信协议使用软件模拟。 程序主要参考抄写网上的,有自己的理解、整理,主要是整理成模块化,方便调试,以及以后的移植。以下贴出部分程序以及工程文件 i2c_soft.h - #ifndef __I2C_SOFT_H
- #define __I2C_SOFT_H
- #include "sys.h"
- #define SCL_PIN GPIO_Pin_10 //板上第11针
- #define SDA_PIN GPIO_Pin_12 //板上第17针
- #define SCL_PORT GPIOB
- #define SDA_PORT GPIOB
- #define SCL_RCC_CLOCK RCC_APB2Periph_GPIOB
- #define SDA_RCC_CLOCK RCC_APB2Periph_GPIOB
-
- #define SCL_H GPIOB->BSRR = GPIO_Pin_10
- #define SCL_L GPIOB->BRR = GPIO_Pin_10
-
- #define SDA_H GPIOB->BSRR = GPIO_Pin_12
- #define SDA_L GPIOB->BRR = GPIO_Pin_12
-
- #define SCL_read GPIOB->IDR & GPIO_Pin_10
- #define SDA_read GPIOB->IDR & GPIO_Pin_12
-
- //#define I2C_PageSize 8 //24C02每页8字节
- void I2C_GPIO_Config(void);
- void I2C_Start(void);
- void I2C_Stop(void);
- void I2C_Ack(void);
- void I2C_NoAck(void);
- u8 I2C_WaitAck(void);
- void I2C_SendByte(u8 SendByte);
- u8 I2C_ReceiveByte(void);
- //bool I2C_WriteByte(u8 SendByte, u16 WriteAddress, u8 DeviceAddress);
- //bool I2C_BufferWrite(u8* pBuffer, u8 length, u16 WriteAddress, u8 DeviceAddress);
- //void I2C_PageWrite(u8* pBuffer, u8 length, u16 WriteAddress, u8 DeviceAddress);
- //bool I2C_ReadByte(u8* pBuffer, u8 length, u16 ReadAddress, u8 DeviceAddress);
- //void I2C_Test(void);
-
- #endif
i2c_soft.c DS3231.h - #ifndef __DS3231_H__
- #define __DS3231_H__
- #include "sys.h"
- typedef struct
- {
- u8 hour;
- u8 min;
- u8 sec;
- u32 w_year;
- u8 w_month;
- u8 w_date;
- u8 week;
- u8 temper_H;
- u8 temper_L;
- }Calendar_OBJ;
- extern Calendar_OBJ calendar; //日历结构体
- extern u8 const mon_table[12]; //月份日期数据表
- void DS3231_Init(void);
- void Get_DS3231_Time(void);
- //u8 RTC_Get_Week(u16 year,u8 month,u8 day);
- void Set_DS3231_Time(u8 syear,u8 smon,u8 sday,u8 hour,u8 min,u8 sec,u8 week);//设置时间
- #endif
DS3231.c - #include "DS3231.h"
- #include "i2c_soft.h"
- #include "delay.h"
- Calendar_OBJ calendar;
- #define DS3231_WriteAddress 0xD0
- #define DS3231_ReadAddress 0xD1
- u8 BCD2HEX(u8 val)
- {
- u8 i;
- i= val&0x0f;
- val >>= 4;
- val &= 0x0f;
- val *= 10;
- i += val;
-
- return i;
- }
- u16 B_BCD(u8 val)
- {
- u8 i,j,k;
- i=val/10;
- j=val%10;
- k=j+(i<<4);
- return k;
- }
- void DS3231_WR_Byte(u8 addr,u8 bytedata)
- {
- I2C_Start();
- I2C_SendByte(DS3231_WriteAddress);
- I2C_WaitAck();
- I2C_SendByte(addr);
- I2C_WaitAck();
- I2C_SendByte(bytedata);
- I2C_WaitAck();
- I2C_Stop();
- }
- u8 DS3231_RD_Byte(u8 addr)
- {
- u8 Dat=0;
-
- I2C_Start();
- I2C_SendByte(DS3231_WriteAddress);
- I2C_WaitAck();
- I2C_SendByte(addr);
- I2C_WaitAck();
- I2C_Start();
- I2C_SendByte(DS3231_ReadAddress);
- I2C_WaitAck();
- Dat=I2C_ReceiveByte();
- I2C_Stop();
-
- return Dat;
- }
- void DS3231_Init(void)
- {
- I2C_GPIO_Config();
- DS3231_WR_Byte(0x0e,0);
- delay_ms(2);
- DS3231_WR_Byte(0x0f,0x0);
- delay_ms(2);
- }
- void Set_DS3231_Time(u8 yea,u8 mon,u8 da,u8 hou,u8 min,u8 sec,u8 week)
- {
- u8 temp=0;
-
- temp=B_BCD(yea);
- DS3231_WR_Byte(0x06,temp);
-
- temp=B_BCD(mon);
- DS3231_WR_Byte(0x05,temp);
-
- temp=B_BCD(da);
- DS3231_WR_Byte(0x04,temp);
-
- temp=B_BCD(hou);
- DS3231_WR_Byte(0x02,temp);
-
- temp=B_BCD(min);
- DS3231_WR_Byte(0x01,temp);
-
- temp=B_BCD(sec);
- DS3231_WR_Byte(0x00,temp);
-
- temp=B_BCD(week);
- DS3231_WR_Byte(0x03,temp);
- }
- void Get_DS3231_Time(void)
- {
- calendar.w_year=DS3231_RD_Byte(0x06);
- calendar.w_year=BCD2HEX(calendar.w_year);
- calendar.w_month=DS3231_RD_Byte(0x05);
- calendar.w_month=BCD2HEX(calendar.w_month);
- calendar.w_date=DS3231_RD_Byte(0x04);
- calendar.w_date=BCD2HEX(calendar.w_date);
-
- calendar.hour=DS3231_RD_Byte(0x02);
- calendar.hour&=0x3f;
- calendar.hour=BCD2HEX(calendar.hour);
- calendar.min=DS3231_RD_Byte(0x01);
- calendar.min=BCD2HEX(calendar.min);
- calendar.sec=DS3231_RD_Byte(0x00);
- calendar.sec=BCD2HEX(calendar.sec);
-
- calendar.week=DS3231_RD_Byte(0x03);
- calendar.week=BCD2HEX(calendar.week);
-
- DS3231_WR_Byte(0x0e,0x20);
- calendar.temper_H=DS3231_RD_Byte(0x11);
- calendar.temper_L=(DS3231_RD_Byte(0x12)>>6)*25;
- }
[ 此帖被shenlan121在2017-12-01 19:42重新编辑 ]
|