AT89S52單片機微型示波器
所需零件:AT89S52單片機
16MHz晶振
10kΩ電阻(2個)
10uF電容
0.1uF電容
MAX232芯片
DB9串行連接器
128x64點陣LCD顯示器
74HC573鎖存器
74HC573解鎖存器
74HC373鎖存器
CD4051多路模擬開關
10kΩ電位器
連接引腳:
P0.0-P0.7:連接LCD顯示器數據總線
P1.0-P1.2:連接CD4051多路模擬開關
P1.3-P1.7:連接74HC573鎖存器
P2.0-P2.3:連接74HC373鎖存器
P3.0-P3.1:連接MAX232芯片
P3.2-P3.3:連接DB9串行連接器
XTAL1, XTAL2:連接16MHz晶振
EA/VPP:接地
ALE/PROG:連接10kΩ電阻和10uF電容
PSEN:連接10kΩ電阻和0.1uF電容
VCC:連接5V電源
GND:連接地
代碼:
#include <reg52.h>
#include <stdio.h>
#define LCD_DATAPORT P0
sbit LCD_RS = P2^0;
sbit LCD_RW = P2^1;
sbit LCD_EN = P2^2;
sbit LCD_CS1 = P2^3;
sbit LCD_CS2 = P2^4;
sbit LCD_RST = P2^5;
sbit CD4051_A = P1^0;
sbit CD4051_B = P1^1;
sbit CD4051_C = P1^2;
sbit MAX232_Tx = P3^1;
sbit MAX232_Rx = P3^0;
sbit LATCH = P1^7;
sbit SHIFT = P1^6;
sbit OE = P1^5;
sbit SCK = P1^4;
sbit SDI = P1^3;
sbit ADC_CS = P2^7;
unsigned char code sine[] = {128, 140, 153, 165, 177, 188, 198, 207,
214, 220, 224, 227, 228, 227, 224, 220,
214, 207, 198, 188, 177, 165, 153, 140,
128, 115, 102, 90, 78, 67, 57, 48,
41, 35, 31, 28, 27, 28, 31, 35,
41, 48, 57, 67, 78, 90, 102, 115};
void LCD_WriteCmd(unsigned char cmd)
{
LCD_RS =0; // 指定為指令寄存器
LCD_DATAPORT = cmd;
LCD_EN = 1;
LCD_EN = 0;
delay_ms(1);
}
void LCD_WriteData(unsigned char dat)
{
LCD_RS = 1; // 指定為數據寄存器
LCD_DATAPORT = dat;
LCD_EN = 1;
LCD_EN = 0;
delay_ms(1);
}
void LCD_Init()
{
LCD_RST = 1;
LCD_CS1 = 1;
LCD_CS2 = 1;
LCD_WriteCmd(0x3E); // 設置為128x64點陣
LCD_WriteCmd(0xC0); // 設置顯示位置為第一行
LCD_WriteCmd(0xB8); // 設置頁地址
LCD_WriteCmd(0x40); // 設置列地址
}
void ADC_Write(unsigned char dat)
{
OE = 1;
ADC_CS = 0;
SDI = dat & 0x80;
SCK = 1;
SCK = 0;
SDI = dat & 0x40;
SCK = 1;
SCK = 0;
SDI = dat & 0x20;
SCK = 1;
SCK = 0;
SDI = dat & 0x10;
SCK = 1;
SCK = 0;
SDI = dat & 0x08;
SCK = 1;
SCK = 0;
SDI = dat & 0x04;
SCK = 1;
SCK = 0;
SDI = dat & 0x02;
SCK = 1;
SCK = 0;
SDI = dat & 0x01;
SCK = 1;
SCK = 0;
ADC_CS = 1;
OE = 0;
}
unsigned char ADC_Read(unsigned char channel)
{
CD4051_C = channel & 0x01;
CD4051_B = (channel >> 1) & 0x01;
CD4051_A = (channel >> 2) & 0x01;
ADC_Write(0x80);
ADC_Write(0x00);
return ADC_Write(0x00);
}
void main()
{
unsigned int x, y, i;
float freq, t;
char str;
LCD_Init();
LATCH = 1;
SHIFT = 1;
while (1) {
for (i = 0; i < 256; i++) {
x = ADC_Read(0);
y = ADC_Read(1);
if (x > 127) x = 127;
if (y > 63) y = 63;
LATCH = 0;
SHIFT = 0;
OE = 1;
LCD_CS1 = 0;
for (t = 0; t < 100; t += 0.1) {
freq = 1000 + 1000 * sin(2 * 3.14159 * t * i / 256);
sprintf(str, "%6.1f", freq);
LCD_WriteData(str);
LCD_WriteData(str);
LCD_WriteData(str);
LCD_WriteData(str);
LCD_WriteData(str);
LCD_WriteData(str);
}
OE = 0;
for (t = 0; t < 100; t += 0.1) {
freq = 1000 + 1000 * sin(2 * 3.14159 * t * i / 256);
sprintf(str, "%6.1f", freq);
LCD_WriteData(str);
LCD_WriteData(str);
LCD_WriteData(str);
LCD_WriteData(str);
LCD_WriteData(str);
LCD_WriteData(str);
}
LCD_CS1 = 1;
LCD_CS2 = 0;
for (t = 0; t < 100; t += 0.1) {
freq = 2000 + 1000 * sin(2 * 3.14159 * t * i / 256);
sprintf(str, "%6.1f", freq);
LCD_WriteData(str);
LCD_WriteData(str);
LCD_WriteData(str);
LCD_WriteData(str);
LCD_WriteData(str);
LCD_WriteData(str);
}
LCD_CS2 = 1;
SHIFT = 1;
LATCH = 1;
delay_ms(1);
}
}
}
以上是AT89S52單片機控制下的微型示波器的主要程式碼,其中包括了波形產生、顯示、控制等功能。
需要的零件包括AT89S52單片機、74HC595移位寄存器、LCD液晶屏、放大器等。
以下是連接腳位示意:
AT89S52單片機:
P1^0-7 -> 數據線 D0-D7
P2^0 -> 74HC595的SER
P2^1 -> 74HC595的SRCLK
P2^2 -> 74HC595的RCLK
P3^0 -> 輸出波形的信號線
74HC595移位寄存器:
Q0-Q7 -> 數據線 D0-D7
SER -> AT89S52單片機的P2^0
SRCLK -> AT89S52單片機的P2^1
RCLK -> AT89S52單片機的P2^2
OE -> 接地
MR -> 接地
LCD液晶屏:
VSS -> 接地
VDD -> 5V
VO -> 可變電阻調節,調整對比度
RS -> 74HC595的Q6
RW -> 接地
E -> 74HC595的Q7
D0-D3 -> 不用連接
D4 -> 74HC595的Q0
D5 -> 74HC595的Q1
D6 -> 74HC595的Q2
D7 -> 74HC595的Q3
A -> 5V
K -> 接地
程式碼中主要的部分就是控制74HC595移位寄存器和LCD液晶屏的操作。
其中,移位寄存器的輸出通過74HC245放大器放大到LCD液晶屏上,實現波形的顯示。
頁:
[1]