- UID
- 373967
- 帖子
- 9026
- 主題
- 2609
- 精華
- 0
- 積分
- 1009
- 楓幣
- 0
- 威望
- 986
- 存款
- 35994
- 贊助金額
- 0
- 推廣
- 0
- GP
- 1205
- 閱讀權限
- 70
- 在線時間
- 461 小時
- 註冊時間
- 2023-1-12
- 最後登入
- 2024-12-25
|
製作萬用測試表需要以下零件:
EM78P157SP 微控制器
示波螢幕模組
4位數7段顯示器模組
電容測試模組
電阻測試模組
電壓測試模組
電流測試模組
按鈕開關模組
杜邦線、電阻、電容等基本元件
EM78P157SP 微控制器腳位連結如下:
P0.0 - 電容測試模組輸入
P0.1 - 電阻測試模組輸入
P0.2 - 電壓測試模組輸入
P0.3 - 電流測試模組輸入
P0.4 - 示波螢幕SCL
P0.5 - 示波螢幕SDA
P1.0-P1.3 - 4位數7段顯示器模組控制腳
P2.0-P2.3 - 按鈕開關模組輸入
程式碼如下:
#include <reg_em78p157sp.h>
#define SCL P04 // 示波螢幕SCL
#define SDA P05 // 示波螢幕SDA
#define DISP_CTRL P1 // 4位數7段顯示器控制腳
#define BUTTON P2 // 按鈕開關模組輸入
void delay(int n) {
int i, j;
for (i = 0; i < n; i++)
for (j = 0; j < 100; j++);
}
void IIC_Start() {
SDA = 1;
SCL = 1;
delay(1);
SDA = 0;
delay(1);
SCL = 0;
}
void IIC_Stop() {
SDA = 0;
SCL = 1;
delay(1);
SDA = 1;
}
void IIC_WriteByte(unsigned char data) {
unsigned char i;
for (i = 0; i < 8; i++) {
SDA = (data & 0x80) >> 7;
data <<= 1;
SCL = 1;
delay(1);
SCL = 0;
}
SDA = 1;
SCL = 1;
delay(1);
SCL = 0;
}
void IIC_WriteCommand(unsigned char command) {
IIC_Start();
IIC_WriteByte(0x78);
IIC_WriteByte(0x00);
IIC_WriteByte(command);
IIC_Stop();
}
void IIC_WriteData(unsigned char data) {
IIC_Start();
IIC_WriteByte(0x78);
IIC_WriteByte(0x40);
IIC_WriteByte(dataIIC_Stop();
}
void Init_LCD() {
IIC_WriteCommand(0xAE); // turn off display
IIC_WriteCommand(0xD5); // set display clock divide ratio/oscillator frequency
IIC_WriteCommand(0x80);
IIC_WriteCommand(0xA8); // set multiplex ratio
IIC_WriteCommand(0x3F);
IIC_WriteCommand(0xD3); // set display offset
IIC_WriteCommand(0x00);
IIC_WriteCommand(0x40); // set display start line
IIC_WriteCommand(0x8D); // set charge pump
IIC_WriteCommand(0x14);
IIC_WriteCommand(0x20); // set memory addressing mode
IIC_WriteCommand(0x00);
IIC_WriteCommand(0xA1); // set segment remap
IIC_WriteCommand(0xC8); // set COM output scan direction
IIC_WriteCommand(0xDA); // set COM pins hardware configuration
IIC_WriteCommand(0x12);
IIC_WriteCommand(0x81); // set contrast control
IIC_WriteCommand(0xCF);
IIC_WriteCommand(0xD9); // set pre-charge period
IIC_WriteCommand(0xF1);
IIC_WriteCommand(0xDB); // set VCOMH deselect level
IIC_WriteCommand(0x40);
IIC_WriteCommand(0xA4); // set entire display on/off
IIC_WriteCommand(0xA6); // set normal/inverse display
IIC_WriteCommand(0xAF); // turn on display
}
void Disp_Digit(unsigned char digit) {
unsigned char seg_data = 0;
switch (digit) {
case 0: seg_data = 0xC0; break;
case 1: seg_data = 0xF9; break;
case 2: seg_data = 0xA4; break;
case 3: seg_data = 0xB0; break;
case 4: seg_data = 0x99; break;
case 5: seg_data = 0x92; break;
case 6: seg_data = 0x82; break;
case 7: seg_data = 0xF8; break;
case 8: seg_data = 0x80; break;
case 9: seg_data = 0x90; break;
default: break;
}
IIC_WriteData(seg_data);
}
void Disp_Num(unsigned int num) {
unsigned char i, digit;
for (i = 0; i < 4; i++) {
digit = num % 10;
num /= 10;
Disp_Digit(digit);
DISP_CTRL = ~(1 << i);
delay(10);
}
}
int Measure_Capacitance() {
// 電容測試程式碼
}
int Measure_Resistor() {
// 電阻測試程式碼
}
int Measure_Voltage() {
// 電壓測試程式碼
}
int Measure_Current() {
// 電流測試程式碼
}
void main() {
unsigned char btn_state = 0;
unsigned int value;
Init_LCD();
while (1) {
value = 0;
if (BUTTON & 0x01) { // 電容測試
value = Measure_Capacitance();
}
else if (BUTTON & 0x02) { // 電阻測試
value = Measure_Resistor();
}
else if (BUTTON & 0x04) { // 電壓測試
value = Measure_Voltage();
}
else if (BUTTON & 0x08) { // 電流測試
value = Measure_Current();
}
Disp_Num(value);
delay(500);
}
}
|
|