網(wǎng)站建設方案書是什么意思近期新聞事件
【IAR工程】STM8S基于ST標準庫讀取DS1302數(shù)據(jù)
- ?申明:本文章僅發(fā)表在CSDN網(wǎng)站,任何其他網(wǎng)站,未注明來源,見此內(nèi)容均為盜鏈和爬取,請多多尊重和支持原創(chuàng)!
- 🍁對于文中所提供的相關資源鏈接將作不定期更換。
- 🔖基于ST STM8S/A標準外設庫:STSW-STM8069,版本號:
2.3.1
- 📌STSW-STM8069官方資源下載地址:
https://www.st.com/zh/embedded-software/stsw-stm8069.html
- 🔧IAR編譯器版本:
IAR Assembler for STMicroelectronics STM8 3.11.1
- 📌STM8S207/208RBT6最小系統(tǒng)板:
https://oshwhub.com/perseverance51/stm8s207rbt6-kai-fa-ban
- 🎯本工程使用STM8S208RB+DS1302實物驗證沒有問題。
🎉基于標準庫工程,當然不局限與STM8其他型號的芯片的使用,只要是stm8芯片都可以使用該源文件進行驅動,方便適配移植,減少不必要的重復開發(fā)工作。
- 📜串口打印信息:
📑引腳定義
如果是其他型號可以根據(jù)自由更換其他引腳。注意修改相關定義。
TM8S單片機-->DS1302PC2 -->CLKPC3-->DATPC4 -->RST
📓DS1302驅動
- 🌿DS1302.h文件
#ifndef __DS1302_H
#define __DS1302_H/****************************驅動 RTC 芯片 DS1302******************************//* Includes ------------------------------------------------------------------*/#include "stm8s.h"/* Defines -------------------------------------------------------------------*/
//是否設置時間到DS1302中
#define RTC_RESET_TIME_EN 0u#define RTC_SCK_PORT (GPIO_TypeDef *)(GPIOC)
#define RTC_SCK_PIN (GPIO_PIN_2) // PC2
#define RTC_SCK_HIGH() GPIO_WriteHigh(RTC_SCK_PORT, RTC_SCK_PIN)
#define RTC_SCK_LOW() GPIO_WriteLow (RTC_SCK_PORT, RTC_SCK_PIN)#define RTC_IO_PORT (GPIO_TypeDef *)(GPIOC)
#define RTC_IO_PIN (GPIO_PIN_3) // PC3#define RTC_IO_IN() GPIO_Init(RTC_IO_PORT, RTC_IO_PIN, GPIO_MODE_IN_PU_NO_IT)
#define RTC_IO_STATUS() GPIO_ReadInputPin(RTC_IO_PORT, RTC_IO_PIN)#define RTC_IO_OUT() GPIO_Init(RTC_IO_PORT, RTC_IO_PIN, GPIO_MODE_OUT_PP_HIGH_SLOW)
#define RTC_IO_HIGH() GPIO_WriteHigh(RTC_IO_PORT, RTC_IO_PIN)
#define RTC_IO_LOW() GPIO_WriteLow (RTC_IO_PORT, RTC_IO_PIN)#define RTC_RST_PORT (GPIO_TypeDef *)(GPIOC)
#define RTC_RST_PIN (GPIO_PIN_4) // PC4
#define RTC_RST_HIGH() GPIO_WriteHigh(RTC_RST_PORT, RTC_RST_PIN)
#define RTC_RST_LOW() GPIO_WriteLow (RTC_RST_PORT, RTC_RST_PIN)/* Values --------------------------------------------------------------------*/typedef struct Time
{uint8_t year; // year 0-99uint8_t month; // month 01-12uint8_t day; // day 01-28,29,30,31uint8_t week; // week 01-07uint8_t hour; // hour 01-12 or 00-23uint8_t minute; // minute 00-59uint8_t second; // second 00-59
} TimeTypeDef;static TimeTypeDef TimeBuffer; // 數(shù)據(jù)緩沖區(qū)(8421-BCD碼)/* Functions -----------------------------------------------------------------*/void DS1302_Init ( void );static void DS1302_WriteByte ( uint8_t byte );
static uint8_t DS1302_ReadByte ( void );
static void DS1302_WriteData ( uint8_t addr, uint8_t data );
static uint8_t DS1302_ReadData ( uint8_t addr );TimeTypeDef DS1302_ReadTime ( void );
void DS1302_WriteTime ( TimeTypeDef *TimeDisplay );static uint8_t DectoBCD ( uint8_t num );
static uint8_t BCDtoDec ( uint8_t num );//static void DS1302_DLY_ms( uint16_t nCount );
static void DS1302_DLY_us( uint16_t nCount );#endif /* __DS1302_H */
- 🌿DS1302.c文件
#include "ds1302.h"/*************************************************************************初始化
--------------------------------------------------------------------------
無參數(shù)
--------------------------------------------------------------------------
無返回值
*************************************************************************/
void DS1302_Init ( void )
{GPIO_Init( RTC_SCK_PORT, RTC_SCK_PIN, GPIO_MODE_OUT_PP_HIGH_SLOW );GPIO_Init( RTC_RST_PORT, RTC_RST_PIN, GPIO_MODE_OUT_PP_HIGH_SLOW );GPIO_Init( RTC_IO_PORT, RTC_IO_PIN, GPIO_MODE_OUT_PP_HIGH_SLOW );RTC_SCK_LOW();RTC_IO_LOW();RTC_RST_LOW();
}/*************************************************************************寫一字節(jié)數(shù)據(jù)
--------------------------------------------------------------------------
byte:一字節(jié)數(shù)據(jù)
--------------------------------------------------------------------------
無返回值
*************************************************************************/
static void DS1302_WriteByte ( uint8_t byte )
{uint8_t i;BitStatus bit;RTC_IO_OUT(); // IO 配置為輸出模式for ( i = 0; i < 8; i++ ){RTC_SCK_LOW();bit = ( BitStatus )( byte & 0x01 );if ( bit != RESET )RTC_IO_HIGH();elseRTC_IO_LOW();RTC_SCK_HIGH();byte >>= 1;//DS1302_DLY_ms(1);}
}/*************************************************************************讀一字節(jié)數(shù)據(jù)
--------------------------------------------------------------------------
addr:地址
--------------------------------------------------------------------------
返回值:一字節(jié)數(shù)據(jù)
*************************************************************************/
static uint8_t DS1302_ReadByte ( void )
{uint8_t i;uint8_t data = 0;BitStatus bit;RTC_IO_IN(); // IO 配置為輸入模式for ( i = 0; i < 8; i++ ){data >>= 1;RTC_SCK_LOW();bit = RTC_IO_STATUS();if ( bit != RESET )data |= 0x80;elsedata &= 0x7F;RTC_SCK_HIGH();//DS1302_DLY_ms(1);}return data;
}/*************************************************************************往指定寄存器寫入一字節(jié)數(shù)據(jù)
--------------------------------------------------------------------------
addr:地址 data:一字節(jié)數(shù)據(jù)
--------------------------------------------------------------------------
無返回值
*************************************************************************/
static void DS1302_WriteData ( uint8_t addr, uint8_t data )
{// 數(shù)據(jù)傳輸開始RTC_RST_LOW();RTC_SCK_LOW();RTC_RST_HIGH();DS1302_WriteByte ( addr ); // 寫入的地址DS1302_WriteByte ( data ); // 寫入的數(shù)據(jù)// 數(shù)據(jù)傳輸結束RTC_RST_LOW();
}/*************************************************************************在指定寄存器讀出一字節(jié)數(shù)據(jù)
--------------------------------------------------------------------------
addr:地址
--------------------------------------------------------------------------
返回值:一字節(jié)數(shù)據(jù)
*************************************************************************/
static uint8_t DS1302_ReadData ( uint8_t addr )
{uint8_t data;// 數(shù)據(jù)傳輸開始RTC_RST_LOW();RTC_SCK_LOW();RTC_RST_HIGH();DS1302_WriteByte ( addr ); // 要讀的地址data = DS1302_ReadByte(); // 要讀的數(shù)據(jù)// 數(shù)據(jù)傳輸結束RTC_RST_LOW();return data;
}/*************************************************************************讀時間
--------------------------------------------------------------------------
無參數(shù)
--------------------------------------------------------------------------
返回值:時間數(shù)據(jù)
*************************************************************************/
TimeTypeDef DS1302_ReadTime ( void )
{TimeTypeDef TimeDisplay;// 讀出來的數(shù)據(jù)是 BCD 碼TimeBuffer.year = DS1302_ReadData ( 0x8D );TimeBuffer.month = DS1302_ReadData ( 0x89 );TimeBuffer.day = DS1302_ReadData ( 0x87 );TimeBuffer.week = DS1302_ReadData ( 0x8B );TimeBuffer.hour = DS1302_ReadData ( 0x85 );TimeBuffer.minute = DS1302_ReadData ( 0x83 );TimeBuffer.second = DS1302_ReadData ( 0x81 ); // bit7 定義為時鐘暫停標志(CH)// BCD 碼轉換為十進制TimeDisplay.year = BCDtoDec ( TimeBuffer.year );TimeDisplay.month = BCDtoDec ( TimeBuffer.month );TimeDisplay.day = BCDtoDec ( TimeBuffer.day );TimeDisplay.week = BCDtoDec ( TimeBuffer.week );TimeDisplay.hour = BCDtoDec ( TimeBuffer.hour );TimeDisplay.minute = BCDtoDec ( TimeBuffer.minute );TimeDisplay.second = BCDtoDec ( TimeBuffer.second );return TimeDisplay;
}/*************************************************************************修改時間
--------------------------------------------------------------------------
*TimeDisplay:要顯示的時間(十進制)
--------------------------------------------------------------------------
無返回值
*************************************************************************/
void DS1302_WriteTime ( TimeTypeDef *TimeDisplay )
{// 十進制轉換為 BCD 碼TimeBuffer.year = DectoBCD ( TimeDisplay->year );TimeBuffer.month = DectoBCD ( TimeDisplay->month );TimeBuffer.day = DectoBCD ( TimeDisplay->day );TimeBuffer.week = DectoBCD ( TimeDisplay->week );TimeBuffer.hour = DectoBCD ( TimeDisplay->hour );TimeBuffer.minute = DectoBCD ( TimeDisplay->minute );TimeBuffer.second = DectoBCD ( TimeDisplay->second );// 關閉寫保護(控制寄存器:8FH、8EH bit7:保護位)DS1302_WriteData ( 0x8E, 0x00 );// 寫入的數(shù)據(jù)是 BCD 碼DS1302_WriteData ( 0x8C, TimeBuffer.year );DS1302_WriteData ( 0x88, TimeBuffer.month );DS1302_WriteData ( 0x86, TimeBuffer.day );DS1302_WriteData ( 0x8A, TimeBuffer.week );DS1302_WriteData ( 0x84, TimeBuffer.hour );DS1302_WriteData ( 0x82, TimeBuffer.minute );DS1302_WriteData ( 0x80, TimeBuffer.second ); // bit7 定義為時鐘暫停標志(CH)// 開啟寫保護(控制寄存器:8FH、8EH bit7:保護位)DS1302_WriteData ( 0x8E, 0x80 );
}/*************************************************************************十進制轉BCD碼
--------------------------------------------------------------------------
num:十進制數(shù)
--------------------------------------------------------------------------
返回值:BCD碼
*************************************************************************/
static uint8_t DectoBCD ( uint8_t num )
{uint8_t result;uint8_t temp1, temp2;temp1 = ( num / 10 ) << 4; // 十位 / 10 * 16temp2 = num % 10; // 個位 % 10result = temp1 + temp2;return result;
}/*************************************************************************BCD碼轉十進制
--------------------------------------------------------------------------
num:BCD碼
--------------------------------------------------------------------------
返回值:十進制
*************************************************************************/
static uint8_t BCDtoDec ( uint8_t num )
{uint8_t result;uint8_t temp1, temp2;temp1 = ( num >> 4 ) * 10; // 十位 / 16 * 10temp2 = num & 0x0F; // 個位 % 16result = temp1 + temp2;return result;
}/*************************************************************************軟件延時(ms級別)
--------------------------------------------------------------------------
nCount:延時長度
--------------------------------------------------------------------------
無返回值
*************************************************************************/
//static void DS1302_DLY_ms( uint16_t nCount )
//{
// while( nCount-- )
// {
// DS1302_DLY_us( 1000 );
// }
//}/*************************************************************************軟件延時(us級別)
--------------------------------------------------------------------------
nCount:延時長度
--------------------------------------------------------------------------
無返回值
*************************************************************************/
static void DS1302_DLY_us( uint16_t nCount )
{nCount *= 2;while( --nCount );
}
📝main主程序代碼
/**************************************************************************************
實驗現(xiàn)象:打開串口調(diào)試助手,選擇CH340對應串口號,波特率設置9600, 串口助手上會顯示printf各種數(shù)據(jù)格式輸出信息。接線說明: 1,STM8S單片機-->LEDPC7-->LED1PC6-->LED2
---------------------------------------------------------TM8S單片機-->DS1302PC2 -->CLKPC3-->DATPC4 -->RST注意事項: 1、點擊“Download active application”按鈕,程序下載完成后,即可運行程序。2、串口1使用的是PA4和PA5引腳,所以這兩個IO口不要被占用
***************************************************************************************/#include "stm8s.h" /* 添加庫函數(shù)頭文件 */
#include "delay.h"
#include "led.h"
#include "usart.h"
#include "ds1302.h"
#include <stdio.h>//包含此頭文件調(diào)用printf函數(shù)串口才能有輸出/* 主函數(shù) */
int main( void )
{u8 i = 0;// 設置初始時間
// TimeTypeDef Set_Time = {23, 04, 4, 2, 23, 25, 10};
const char *WEEK[7] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };TimeTypeDef tm= {0};uint8_t TimeSecPre;disableInterrupts(); //關閉系統(tǒng)中斷//內(nèi)部時鐘為1分頻 = 16MhzCLK_SYSCLKConfig( CLK_PRESCALER_HSIDIV1 );LED_Init();USART1_Init( 9600 ); //初始化USART1 , 并設置波特率為9600DS1302_Init();//是否設置時間到DS1302中
#if RTC_RESET_TIME_EN > 0uDS1302_WriteTime( Set_Time );
#endifenableInterrupts(); //使能系統(tǒng)中斷while( 1 ){i++;if( i % 20 == 0 ){LED1_TOGGLE;LED2_TOGGLE;}tm = DS1302_ReadTime();if ( TimeSecPre != tm.second ){TimeSecPre = tm.second;printf( "20%02d年%02d月%02d日 星期:%s %02d:%02d:%02d\r\n", tm.year, tm.month, tm.day,WEEK[tm.week] , tm.hour, tm.minute, tm.second);}delay_ms( 10 );}
}
//是一個宏定義;在固件庫中,它的作用就是檢測傳遞給函數(shù)的參數(shù)是否是有效的參數(shù)
void assert_failed( u8* file, u32 line )
{while ( 1 ){}
}
📚程序源碼
- ?申明:本文章僅發(fā)表在CSDN網(wǎng)站,任何其他網(wǎng)站,未注明來源,見此內(nèi)容均為盜鏈和爬取,請多多尊重和支持原創(chuàng)!
- 🍁對于文中所提供的相關資源鏈接將作不定期更換。
鏈接: https://pan.baidu.com/s/18drnS5yPTSz79vxTBS7Brw
提取碼: thdy