【說明】
1. 本例程適合的CPU: STC89C52 11.0592晶振
2. 編譯環境: Keil_c51_v802
2. 串口輸出的波特率 = 9600bps 無校驗 無硬件流控(請取消串口工具上的RTS CTS功能)
3. 請在PC機上安裝串口軟件: SecureCRT(v6.1.0英文安裝版).rar
4. 如果你的板子經常出現SPI接口時序迷失,就請檢查P1口上的上拉電阻配置。這種情況多半是波形不好所致。
(1)如果你使用發光二極管串聯電阻代替上拉電阻,則極容易迷失接口。
(2)如果上拉電阻大于2K歐姆,波形上升沿比較緩,會影響時序。
(3) 建議: P1口增加1K-2K歐姆的上拉電阻(不要LED串聯)。
5. 在bsp.c 中有ms延遲函數,該函數是按照89C52 12T指令周期, 11.0592晶振調試通過的。
void bsp_DelayMS(uint16_t n) 延遲精度 99% (500ms 實測 498ms), 如果換其他CPU,請自行調整。
6. TM7705的驅動程序在 bsp_tm770.c 和.h文件。
其中 static void TM7705_WaitDRDY(void) 函數,已經做了接口時序迷失處理。當時序迷失時,會自動重新同步。
【接線方法】
TM7705模塊 STC89C52開發板
VCC ------ 5.0V (3.3V)
GND ------ GND
CS ------ P1.0
RST ------ P1.1
DIN ------ P1.2
SCK ------ P1.3
DOUT ------ P1.4
DRDY ------ P1.5
【運行結果】
*************************************************************
* 例程名稱 : STM8-101_TM7705雙路ADC模塊例程
* 例程版本 : 1.0
*************************************************************
接線方法:
AD7705模塊 51開發板(STC89C52, 11.0592M晶振)
VCC <------ 5.0V 5V供電
GND ------- GND 地
CS <------ P1.0 SPI片選
RST <------ P1.1 SPI復位
DIN <------ P1.2 SPI MOSI
SCK <------ P1.3 SPI時鐘
DOUT -------> P1.4 SPI MISO
DRDY <------> P1.5 滿信號,數據準備好
CH1= 10 ( 0mV) CH2= 10 ( 0mV)
電路原理圖如下:
無標題.png (114.9 KB, 下載次數: 81)
下載附件
51單片機AD采集開發板原理圖
2018-6-7 18:01 上傳
捕獲.PNG (23.19 KB, 下載次數: 75)
下載附件
2018-6-7 18:04 上傳
0.png (7.01 KB, 下載次數: 59)
下載附件
2018-6-8 02:32 上傳
AD7705_STC89C52(串口打印采集數據)
MAX7219驅動2個4位數碼管
MAX7920數碼管顯示AD7705數據
單片機源程序如下:
- //LCH2016-5-7實驗OK
- //MAX7920驅動2個4位數碼管顯示AD7705采集2電位器數據
- //CPU:STC89C52 晶振11.0592MHZ
- //AD7705的晶振用4MHZ
- //AD7705的基準電壓用2個10K電阻串聯接到DC5V+,分壓得到2.5V
- //2個4位的共陰極的數碼管:MT03461AR
- //2個10K的電位器
- #include "bsp.h"
- #define uchar unsigned char
- #define uint unsigned int
- //定義Max7219端口
- sbit Max7219_pinCLK = P2^2;
- sbit Max7219_pinCS = P2^1;
- sbit Max7219_pinDIN = P2^0;
- // 定義例程名和例程發布日期
- #define EXAMPLE_NAME "STC89C52_TM7705雙路ADC模塊例程"
- #define EXAMPLE_DATE "2016-5-6"
- #define DEMO_VER "1.0"
- unsigned char Xianshi[8];
- static void PrintfLogo(void);
- static void PrintfHardInfo(void);
- static void TM7705_Demo(void);
- void delay1(int count) // ms延時函數 (AT89C51 @ 11.0592MHz)
- {
- unsigned int i;
- unsigned char j;
- for(i=0;i<count;i++)
- {
- for(j=0;j<200;j++);
- for(j=0;j<102;j++);
- }
- }
- //--------------------------------------------
- //功能:向MAX7219(U3)寫入字節
- //入口參數:DATA
- //出口參數:無
- //說明:
- void Write_Max7219_byte(uchar DATA)
- {
- uchar i;
- Max7219_pinCS=0;
- for(i=8;i>=1;i--)
- {
- Max7219_pinCLK=0;
- Max7219_pinDIN=DATA&0x80;
- DATA=DATA<<1;
- Max7219_pinCLK=1;
- }
- }
- //-------------------------------------------
- //功能:向MAX7219寫入數據
- //入口參數:address、dat
- //出口參數:無
- //說明:
- void Write_Max7219(uchar address,uchar dat)
- {
- Max7219_pinCS=0;
- Write_Max7219_byte(address); //寫入地址,即數碼管編號
- Write_Max7219_byte(dat); //寫入數據,即數碼管顯示數字
- Max7219_pinCS=1;
- }
- void Init_MAX7219(void)
- {
- Write_Max7219(0x09, 0xff); //譯碼方式:BCD碼
- Write_Max7219(0x0a, 0x03); //亮度
- Write_Max7219(0x0b, 0x07); //掃描界限;4個數碼管顯示
- Write_Max7219(0x0c, 0x01); //掉電模式:0,普通模式:1
- Write_Max7219(0x0f, 0x01); //顯示測試:1;測試結束,正常顯示:0
- }
- void main(void)
- {
- bsp_Init(); // 初始化底層硬件。 該函數在 bsp.c文件
- PrintfLogo(); // 打印例程logo
- delay1(2000);
- PrintfHardInfo(); // 打印硬件接線信息
- delay1(2000);
- Init_MAX7219();
- delay1(2000);
- Write_Max7219(0x0f, 0x00); //顯示測試:1;測試結束,正常顯示:0
- Write_Max7219(1,8);
- Write_Max7219(2,7);
- Write_Max7219(3,6);
- Write_Max7219(4,5);
- Write_Max7219(5,4);
- Write_Max7219(6,3);
- Write_Max7219(7,2);
- Write_Max7219(8,1);
- TM7705_Demo();
- }
- // 功能說明: 定時讀取TM7705的ADC值,并打印到串口。請通過PC機串口工具查看結果。
- static void TM7705_Demo(void)
- {
- uint16_t adc1, adc2;
-
- bsp_InitTM7705(); // 初始化配置TM7705
- TM7705_CalibSelf(1); // 自校準。執行時間較長,約180ms
- adc1 = TM7705_ReadAdc(1);
-
- TM7705_CalibSelf(2); // 自校準。執行時間較長,約180ms
- adc2 = TM7705_ReadAdc(2);
-
- while (1)
- {
- bsp_Idle();
- #if 1
- // 雙通道切換采樣,執行一輪實際那約 160ms
- adc1 = TM7705_ReadAdc(1); // 執行時間 80ms
- adc2 = TM7705_ReadAdc(2); // 執行時間 80ms
- #else
- // 如果只采集1個通道,則刷新速率 50Hz (缺省設置的,最大可以設置為500Hz)
- adc1 = TM7705_ReadAdc(1); // 執行時間 20ms (50Hz速率刷新時)
- adc2 = 0;
- #endif
-
- //打印采集數據
- {
- int volt1, volt2;
-
- // 計算實際電壓值(近似估算的),如需準確,請進行校準
- volt1 = ((int32_t)adc1 * 5000) / 65535;
- volt2 = ((int32_t)adc2 * 5000) / 65535;
- Xianshi[0]=volt1/1000;
- Xianshi[1]=(volt1-Xianshi[0]*1000)/100;
- Xianshi[2]=(volt1-Xianshi[0]*1000-Xianshi[1]*100)/10;
- Xianshi[3]=volt1-Xianshi[0]*1000-Xianshi[1]*100-Xianshi[2]*10;
- Xianshi[4]=volt2/1000;
- Xianshi[5]=(volt2-Xianshi[4]*1000)/100;
- Xianshi[6]=(volt2-Xianshi[4]*1000-Xianshi[5]*100)/10;
- Xianshi[7]=volt2-Xianshi[4]*1000-Xianshi[5]*100-Xianshi[6]*10;
- Write_Max7219(1,Xianshi[7]);
- Write_Max7219(2,Xianshi[6]);
- Write_Max7219(3,Xianshi[5]);
- Write_Max7219(4,Xianshi[4]);
- Write_Max7219(5,Xianshi[3]);
- Write_Max7219(6,Xianshi[2]);
- Write_Max7219(7,Xianshi[1]);
- Write_Max7219(8,Xianshi[0]);
- // 打印ADC采樣結果
- printf("CH1=%5ld (%5dmV) CH2=%5ld (%5dmV)\r", (long int)adc1, volt1, (long int)adc2, volt2);
- delay1(1000);
- }
- }
- }
- /*
- *********************************************************************************************************
- * 函 數 名: PrintfLogo
- * 功能說明: 打印例程名稱和例程發布日期, 接上串口線后,打開PC機的超級終端軟件可以觀察結果
- * 形 參:無
- * 返 回 值: 無
- *********************************************************************************************************
- */
- static void PrintfLogo(void)
- {
- /* 友情提示:
- 使用STVD開發時,printf/sprintf 入口參數 %d 必須是 int 類型(16位),否則打印結果不正確。
- IAR無此問題。
- */
- printf("*************************************************************\n\r");
- printf("* 例程名稱 : %s\r\n", EXAMPLE_NAME); /* 打印例程名稱 */
- printf("* 例程版本 : %s\r\n", DEMO_VER); /* 打印例程版本 */
- printf("* 發布日期 : %s\r\n", EXAMPLE_DATE); /* 打印例程日期 */
- printf("* QQ : 1295744630 \r\n");
- printf("* Email : armfly@qq.com \r\n");
- printf("* Copyright www*armfly*com 安富萊電子\r\n");
- printf("*************************************************************\n\r");
- }
- /*
- *********************************************************************************************************
- * 函 數 名: PrintfHardInfo
- * 功能說明: 打印硬件接線信息
- * 形 參:無
- * 返 回 值: 無
- *********************************************************************************************************
- */
- static void PrintfHardInfo(void)
- {
- printf("接線方法: \r\n");
- printf("AD7705模塊 51開發板(STC89C52, 11.0592M晶振)\r\n");
- printf(" VCC <------ 5.0V 5V供電\r\n");
- printf(" GND ------- GND 地\r\n");
- printf(" CS <------ P1.0 SPI片選\r\n");
- printf(" RST <------ P1.1 SPI復位\r\n");
- printf(" DIN <------ P1.2 SPI MOSI\r\n");
- printf(" SCK <------ P1.3 SPI時鐘\r\n");
- printf(" DOUT -------> P1.4 SPI MISO\r\n");
- printf(" DRDY <------> P1.5 滿信號,數據準備好\r\n");
- }
- ……………………
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼
0.png (42.88 KB, 下載次數: 55)
下載附件
2018-6-8 02:35 上傳
所有資料51hei提供下載:
51單片機AD采集開發板配套資料170418.zip
(829.55 KB, 下載次數: 76)
2018-6-7 17:59 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|