系列文章【完善中】:
【POCKET 51】用 pocket 51学51单片机 之一:pocket 51诞生记
【POCKET 51】用 pocket 51学51单片机 之二:pocket 51的加工及焊接
【POCKET 51】用 pocket 51学51单片机 之三:pocket 51上电测试
【POCKET 51】用 pocket 51学51单片机 之四:pocket 51功能模块测试
【本设计已在立创开源平台开源】
1、LED灯测试
功能测试正常,代码见
【POCKET 51】用 pocket 51学51单片机 之三:pocket 51上电测试
中的内容。
2、数码管测试
V1.0版本数码管段码接反了,所以特殊生成了段码表进行测试
数码管测试代码:
#include "reg51.h"
#include "intrins.h"
sbit BEEP=P3^7;
sbit SEG1=P1^7;
sbit SEG2=P1^6;
unsigned char code SEG_code[]={
0x03, //"0"
0x9F, //"1"
0x25, //"2"
0x0D, //"3"
0x99, //"4"
0x49, //"5"
0x41, //"6"
0x1F, //"7"
0x01, //"8"
0x09, //"9"
0x11, //"A"
0xC1, //"B"
0x63, //"C"
0x85, //"D"
0x61, //"E"
0x71, //"F"
0x91, //"H"
0xE3, //"L"
0x13, //"n"
0x83, //"u"
0x31, //"P"
0xC5, //"o"
0xFD, //"-"
0xFF, //熄灭
0xFF //自定义
};
void delay()
{
int i, j;
for (i=0; i<10; i++)
for (j=0; j<500; j++);
}
void main()
{ int i;
BEEP=1;
while (1)
{
SEG1=0;
SEG2=1;
P0=SEG_code[3];
delay();
SEG1=1;
SEG2=0;
P0=SEG_code[9];
delay();
}
}
测试结果正常
3、蜂鸣器测试
蜂鸣器采用了无源蜂鸣器,驱动时,需要采用脉冲进行驱动。
测试代码:
#include "reg51.h"
sbit BEEP=P3^7;
void delay()
{
int i;
for (i=0; i<100; i++);
}
void main()
{ int i;
BEEP=1;
while (1)
{
BEEP=1;
delay();
BEEP=0;
delay();
}
}
调整delay时间,可以发出不同音高。
4、按键功能测试
按下按键在数码管上显示键值,程序代码:
#include "reg51.h"
#include "intrins.h"
sbit BEEP=P3^7;
sbit SEG1=P1^7;
sbit SEG2=P1^6;
sbit keya=P3^2;
sbit keyb=P3^3;
sbit keyc=P3^4;
sbit keyd=P3^5;
unsigned char code SEG_code[]={ 0x03,0x9f,0x25,0x0d,0x99,0x49,0x41,0x1f,0x01,0x09,0x11,0xc1,0x63,0x85,0x61,0x71};
void delay()
{
int i, j;
for (i=0; i<10; i++)
for (j=0; j<500; j++);
}
void main()
{ int i,key=0;
BEEP=1;
while (1)
{
if(keya==0) key=1;
if(keyb==0) key=2;
if(keyc==0) key=3;
if(keyd==0) key=4;
SEG1=0;
SEG2=1;
P0=SEG_code[key];
delay();
}
}
运行结果:
按下B键
按下D键
5、WS2812多彩LED灯功能测试
【待完善】
由于手头没有示波器无法查看波形,暂时先不进行测试
6、串口测试
测试串口主要是进行晶振部分是否工作正常。测试代码使用了官方的UART例程
/*------------------------------------------------------------------*/
/* --- STC MCU Limited ---------------------------------------------*/
/* --- STC12C5Axx Series MCU UART (8-bit/9-bit)Demo ----------------*/
/* --- Mobile: (86)13922805190 -------------------------------------*/
/* --- Fax: 86-0513-55012956,55012947,55012969 ---------------------*/
/* --- Tel: 86-0513-55012928,55012929,55012966----------------------*/
/* --- Web: www.STCMCU.com -----------------------------------------*/
/* --- Web: www.GXWMCU.com -----------------------------------------*/
/* If you want to use the program or the program referenced in the */
/* article, please specify in which data and procedures from STC */
/*------------------------------------------------------------------*/
#include "reg51.h"
#include "intrins.h"
typedef unsigned char BYTE;
typedef unsigned int WORD;
#define FOSC 11059200L //System frequency
#define BAUD 9600 //UART baudrate
/*Define UART parity mode*/
#define NONE_PARITY 0 //None parity
#define ODD_PARITY 1 //Odd parity
#define EVEN_PARITY 2 //Even parity
#define MARK_PARITY 3 //Mark parity
#define SPACE_PARITY 4 //Space parity
#define PARITYBIT EVEN_PARITY //Testing even parity
sbit bit9 = P2^2; //P2.2 show UART data bit9
bit busy;
void SendData(BYTE dat);
void SendString(char *s);
void main()
{
#if (PARITYBIT == NONE_PARITY)
SCON = 0x50; //8-bit variable UART
#elif (PARITYBIT == ODD_PARITY) || (PARITYBIT == EVEN_PARITY) || (PARITYBIT == MARK_PARITY)
SCON = 0xda; //9-bit variable UART, parity bit initial to 1
#elif (PARITYBIT == SPACE_PARITY)
SCON = 0xd2; //9-bit variable UART, parity bit initial to 0
#endif
TMOD = 0x20; //Set Timer1 as 8-bit auto reload mode
TH1 = TL1 = -(FOSC/12/32/BAUD); //Set auto-reload vaule
TR1 = 1; //Timer1 start run
ES = 1; //Enable UART interrupt
EA = 1; //Open master interrupt switch
SendString("STC12C5A60S2\r\nUart Test !\r\n");
while(1);
}
/*----------------------------
UART interrupt service routine
----------------------------*/
void Uart_Isr() interrupt 4 using 1
{
if (RI)
{
RI = 0; //Clear receive interrupt flag
P0 = SBUF; //P0 show UART data
bit9 = RB8; //P2.2 show parity bit
}
if (TI)
{
TI = 0; //Clear transmit interrupt flag
busy = 0; //Clear transmit busy flag
}
}
/*----------------------------
Send a byte data to UART
Input: dat (data to be sent)
Output:None
----------------------------*/
void SendData(BYTE dat)
{
while (busy); //Wait for the completion of the previous data is sent
ACC = dat; //Calculate the even parity bit P (PSW.0)
if (P) //Set the parity bit according to P
{
#if (PARITYBIT == ODD_PARITY)
TB8 = 0; //Set parity bit to 0
#elif (PARITYBIT == EVEN_PARITY)
TB8 = 1; //Set parity bit to 1
#endif
}
else
{
#if (PARITYBIT == ODD_PARITY)
TB8 = 1; //Set parity bit to 1
#elif (PARITYBIT == EVEN_PARITY)
TB8 = 0; //Set parity bit to 0
#endif
}
busy = 1;
SBUF = ACC; //Send data to UART buffer
}
/*----------------------------
Send a string to UART
Input: s (address of string)
Output:None
----------------------------*/
void SendString(char *s)
{
while (*s) //Check the end of the string
{
SendData(*s++); //Send current char and increment string ptr
}
}
烧录后工作正常,运行效果:
7、外接PAD焊盘功能测试
测试了P1.2端口(丝印误写为P2.2),采用了简单的闪灯程序,外部直接接一个LED灯到GND管脚。
测试程序如下:
#include "reg51.h"
sfr P4SW = 0xBB;
sbit P12=P1^2;
void delay()
{
int i, j;
for (i=0; i<1000; i++)
for (j=0; j<500; j++);
}
void main()
{
P4SW = 0x70;
while (1)
{
P12 = 1;
delay();
P12 = 0;
delay();
}
}
测试结果,闪灯正常: