仿真原理圖如下(proteus仿真工程文件可到本帖附件中下載)
51hei.gif (146.83 KB, 下載次數(shù): 67)
下載附件
2021-12-25 03:40 上傳
51hei截圖20211224174348.png (73.8 KB, 下載次數(shù): 73)
下載附件
2021-12-24 17:45 上傳
51hei截圖20211224174411.png (100.09 KB, 下載次數(shù): 49)
下載附件
2021-12-24 17:45 上傳
單片機源程序如下:
- #include <reg51.h>
- #include<string.h>
- typedef unsigned char u8; // 重命名類型u8簡化代碼編寫
- typedef unsigned int u16;
- #define LCD1602_DATA_PORT P2 // LCD1602的8位數(shù)據(jù)端口
- sbit gLcd1602_E = P3^2; // LCD1602控制總線的使能信號
- sbit gLcd1602_RW = P3^1; // LCD1602控制總線的讀寫選擇信號
- sbit gLcd1602_RS = P3^0; // LCD1602控制總線的數(shù)據(jù)/命令選擇信號
- sbit Key1 = P3^5;
- sbit Key2 = P3^7;
- float Output_DA = 200;
- void delay10ms(void)
- {
- unsigned char a,b,c;
- for(c=1;c>0;c--)
- for(b=38;b>0;b--)
- for(a=130;a>0;a--);
- }
- void Lcd1602WaitNoBusy(void) //忙檢測函數(shù),判斷bit7是0,允許執(zhí)行;1禁止
- {
- unsigned char sta = 0; //
- LCD1602_DATA_PORT = 0xff;
- gLcd1602_RS = 0;
- gLcd1602_RW = 1;
- do
- {
- gLcd1602_E = 1;
- sta = LCD1602_DATA_PORT;
- gLcd1602_E = 0; //使能,用完就拉低,釋放總線
- }while(sta & 0x80);
- }
- void Lcd1602WriteCmd(unsigned char cmd)
- {
- Lcd1602WaitNoBusy(); // 先等待LCD1602處于不忙狀態(tài)
- gLcd1602_E = 0; // 禁止LCD
- gLcd1602_RS = 0; // 選擇發(fā)送命令模式
- gLcd1602_RW = 0; // 選擇寫入模式
- LCD1602_DATA_PORT = cmd; // 將1字節(jié)命令字放入8位并行數(shù)據(jù)端口
- gLcd1602_E = 1; // 使能LED
- gLcd1602_E = 0; // 禁止LCD
- }
- void Lcd1602WriteData(unsigned char dat)
- {
- Lcd1602WaitNoBusy(); // 先等待LCD1602處于不忙狀態(tài)
- gLcd1602_E = 0; // 禁止LCD
- gLcd1602_RS = 1; // 選擇發(fā)送數(shù)據(jù)模式
- gLcd1602_RW = 0; // 選擇寫入模式
- LCD1602_DATA_PORT = dat; // 將1字節(jié)命令字放入8位并行數(shù)據(jù)端口
- gLcd1602_E = 1; // 使能LED
- gLcd1602_E = 0; // 禁止LCD
- }
- void Lcd1602SetCursor(unsigned char x,unsigned char y) // 坐標顯示
- {
- unsigned char addr = 0;
- switch (y)
- {
- case 0: // 上面一行
- addr = 0x00 + x; break;
- case 1: // 下面一行
- addr = 0x40 + x; break;
- default:
- break;
- }
- Lcd1602WriteCmd(addr | 0x80);
- }
- void Lcd1602ShowStr(unsigned char x, unsigned char y, unsigned char *pStr) //顯示字符串
- {
- Lcd1602SetCursor(x, y); //當前字符的坐標
- while (*pStr != '\0')
- {
- Lcd1602WriteData(*pStr++);
- }
- }
- void Lcd1602Init(void)
- {
- Lcd1602WriteCmd(0x38); // 按照數(shù)據(jù)手冊的初始化時序,先發(fā)送38H
- delay10ms(); // 延時10ms
- Lcd1602WriteCmd(0x38); // 按照數(shù)據(jù)手冊的初始化時序,先發(fā)送38H
- delay10ms(); // 延時10ms
- Lcd1602WriteCmd(0x38); // 按照數(shù)據(jù)手冊的初始化時序,先發(fā)送38H
- delay10ms(); // 延時10ms
- Lcd1602WriteCmd(0x38); // 顯示模式設置
- Lcd1602WriteCmd(0x08); // 關閉顯示
- Lcd1602WriteCmd(0x01); // 清屏(同時清數(shù)據(jù)指針)
- Lcd1602WriteCmd(0x06); // 讀寫后指針自動加1,寫1個字符后整屏顯示不移動
- Lcd1602WriteCmd(0x0c); // 開顯示,不顯示光標
-
- Lcd1602ShowStr(0,0,"OutputVolt: V");
- }
- /*************** 顯示函數(shù) *************************************************/
- void Write_Volt(unsigned char hang,unsigned char add,unsigned int date) //用于顯示
- {
- if(hang==1)
- Lcd1602WriteCmd(0x80+add);
- else
- Lcd1602WriteCmd(0x80+0x40+add);
- Lcd1602WriteData(0x30+date%1000/100); //顯示百位
- Lcd1602WriteData(0x30+date%100/10); //顯示十位
- Lcd1602WriteData('.'); //顯示.
- Lcd1602WriteData(0x30+date%10); //顯示個位
- Lcd1602WriteData('V'); //顯示字母V
- }
- void Out_Volt(unsigned char Volt)
- {
- P0=Volt;
- }
- void main()
- {
- Lcd1602Init();
- while (1)
- {
- Out_Volt(Output_DA);
-
- if (Key1 == 0)
- {
- delay10ms();
- if (Key1 == 0)
- {
- if(Output_DA < 200) Output_DA += 2;
- }
- while(!Key1);
- }
- if (Key2 == 0)
- {
- delay10ms();
- if (Key2 == 0)
- {
- if(Output_DA >= 2) Output_DA -= 2;
- }
- while(!Key2);
- }
-
- Write_Volt(1,11,Output_DA/2.0);
- }
- }
復制代碼
Keil代碼與Proteus仿真下載:
電壓源DAC0832-1602-LM317.zip
(144.66 KB, 下載次數(shù): 104)
2021-12-24 17:45 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|