- UID
- 373967
- 帖子
- 8867
- 主題
- 2609
- 精華
- 0
- 積分
- 1000
- 楓幣
- 15
- 威望
- 977
- 存款
- 34822
- 贊助金額
- 0
- 推廣
- 0
- GP
- 1205
- 閱讀權限
- 70
- 在線時間
- 455 小時
- 註冊時間
- 2023-1-12
- 最後登入
- 2024-11-22
|
以下是使用PIC16F72製作微型示波器示例:
#include <htc.h>
__CONFIG(HS & WDTDIS & PWRTEN & BORDIS & LVPDIS & DUNPROT & WRTEN & DEBUGDIS);
#define LCD_RS RA0
#define LCD_RW RA1
#define LCD_E RA2
#define LCD_D4 RA3
#define LCD_D5 RA4
#define LCD_D6 RA5
#define LCD_D7 RA6
typedef unsigned char u8;
typedef unsigned int u16;
void delay(u16 i)
{
while(i--);
}
void LCD_write_byte(u8 dat, u8 cmd)
{
LCD_RS = cmd;
LCD_RW = 0;
LCD_D7 = dat >> 7;
LCD_D6 = (dat >> 6) & 0x01;
LCD_D5 = (dat >> 5) & 0x01;
LCD_D4 = (dat >> 4) & 0x01;
LCD_E = 1;
delay(2);
LCD_E = 0;
LCD_D7 = (dat >> 3) & 0x01;
LCD_D6 = (dat >> 2) & 0x01;
LCD_D5 = (dat >> 1) & 0x01;
LCD_D4 = dat & 0x01;
LCD_E = 1;
delay(2);
LCD_E = 0;
}
void LCD_init()
{
LCD_RS = 0;
LCD_RW = 0;
LCD_E = 0;
LCD_write_byte(0x30, 1);
LCD_write_byte(0x30, 1);
LCD_write_byte(0x30, 1);
LCD_write_byte(0x20, 1);
LCD_write_byte(0x28, 1);
LCD_write_byte(0x0c, 1);
LCD_write_byte(0x06, 1);
LCD_write_byte(0x01, 1);
}
void LCD_clear()
{
LCD_write_byte(0x01, 1);
delay(2000);
}
void LCD_display(u8 *s, u8 x, u8 y)
{
u8 i;
if(y == 0)
{
x |= 0x80;
}
else if(y == 1)
{
x |= 0xc0;
}
LCD_write_byte(x, 1);
for(i = 0; s[i] != '\0'; i++)
{
LCD_write_byte(s[i], 0);
}
}
void main()
{
u8 buf[16];
u16 i, t;
TRISA = 0xff;
TRISB = 0x00;
TRISC = 0x00;
PORTB = 0x00;
PORTC = 0x00;
LCD_init();
LCD_display("OSC:", 0, 0);
while(1)
{
for(i = 0; i < 128; i++)
{
t = PORTA;
t = (t << 8) + PORTA;
sprintf(buf, "%04d", t);
LCD_display(buf, i % 16, i / 16 + 1);
delay(50);
}
}
}
在這個示例代碼中,使用了PORTA作為示波器的輸入口。
程序每50個時鐘週期讀取一次
輸入口的電平,並將讀取到的數值在LCD上進行顯示,實現了一個簡單的微型示波器。
需要的零件如下:
PIC16F72微控制器
16x2字符LCD顯示器
10K歐姆電位器
適當的電容和電阻
杜邦線等連接線
連接腳位如下:
PIC16F72 LCD顯示器
RA0 RS
RA1 RW
RA2 E
RA3 D4
RA4 D5
RA5 D6
RA6 D7
RB0 10K歐姆電位器
PORTA 輸入信號
其中,10K歐姆電位器用於調節LCD的對比度。 |
|