“与PC机串口通信”程序设计说明
- 明确程序目的:
实现单片机与PC进行通信,现在设置进行两位数码管(前面两位)来显示,只传递两位数 - 创建新项目
- 编写代码
3.1变量定义
#include<STC15F2K60S2.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define cstFocs 11059200L
#define cstBaud1 9600
#define cstKeyMaxNum 100
sbit sbtKey1 = P3 ^ 2 ;
sbit sbtKey2 = P3 ^ 3 ;
sbit sbtKey3 = P1 ^ 7 ;
sbit sbtLedSel = P2 ^ 3;
uchar ucT100usTimes;
uint uiKey1Cnt;
uint uiKey2Cnt;
uint uiKey3Cnt;
uint uiKeyAllCnt;
bit btT1msFlag;
bit btKey1Current;
bit btKey1Past;
bit btKey2Current;
bit btKey2Past;
bit btKey3Current;
bit btKey3Past;
bit btUart1SendBusy = 0 ;
uchar ucDateTmp;
uchar ucDateDigState;
uchar arrSegSelect[] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f, 0x77, 0x7c, 0x39, 0x5e, 0x79, 0x71};
3.2初始化函数
void Init()
{ P2M0 = 0x08;
P2M1 = 0x00;
P0M0 = 0xff;
P0M1 = 0x00;
TMOD = 0x01;
ET0 = 1;
TH0 = ( 65535 - 100 ) / 256;
TL0 = ( 65535 - 100 ) % 256;
TR0 = 1;
Uart1_Init();
ucDateTmp = 0x00;
sbtLedSel = 0;
btT1msFlag = 0;
btKey1Current = 1;
btKey1Past = 1;
btKey2Current = 1;
btKey2Past = 1;
btKey3Current = 1;
btKey3Past = 1;
uiKey1Cnt = 0x80 + cstKeyMaxNum / 3 * 2;
uiKey2Cnt = 0x80 + cstKeyMaxNum / 3 * 2;
uiKey3Cnt = 0x80 + cstKeyMaxNum / 3 * 2;
uiKeyAllCnt = cstKeyMaxNum;
}
void Uart1_Init( void )
{
AUXR = 0X80;
SCON |= 0X50;
TL1 = ( 65536 - ( cstFocs / 4 / cstBaud1 ) );
TH1 = ( 65536 - ( cstFocs / 4 / cstBaud1 ) ) >> 8;
AUXR |= 0X40;
RI = 0;
TI = 0;
TR1 = 1;
ES = 1;
EA = 1;
PS = 1 ;
}
3.3中断函数
void T0_Process() interrupt 1
{
TH0 = ( 65535 - 100 ) / 256;
TL0 = ( 65535 - 100 ) % 256;
ucT100usTimes++;
if( ucT100usTimes == 10 )
{
ucT100usTimes = 0;
btT1msFlag = 1;
}
ucDateDigState++;
if( ucDateDigState == 2 )
ucDateDigState = 0;
P0 = 0;
switch( ucDateDigState )
{
case 0:
P2 = 0x00; P0 = arrSegSelect[ucDateTmp / 16];
break;
case 1:
P2 = 0x01; P0 = arrSegSelect[ucDateTmp % 16]; break;
}
}
void SendData( unsigned char dat )
{
while( btUart1SendBusy );
btUart1SendBusy = 1;
SBUF = dat;
}
void Uart1_Process() interrupt 4 using 1
{
if( RI )
{
RI = 0; ucDateTmp = SBUF;
}
if( TI )
{
TI = 0; btUart1SendBusy = 0;
}
}
3.4主函数
void main()
{
Init();
while( 1 )
{
if( btT1msFlag )
{
btT1msFlag = 0;
if( sbtKey1 == 0 )
uiKey1Cnt--;
if( sbtKey2 == 0 )
uiKey2Cnt--;
if( sbtKey3 == 0 )
uiKey3Cnt--;
uiKeyAllCnt--;
if( uiKeyAllCnt == 0 )
{
if( uiKey1Cnt < 0x80 )
{
btKey1Current = 0;
if( btKey1Past == 1 )
{
btKey1Past = 0;
SendData( ucDateTmp ) ;
}
}
if( uiKey1Cnt >= 0x80 )
{
btKey1Current = 1;
if( btKey1Past == 0 )
btKey1Past = 1;
}
if( uiKey2Cnt < 0x80 )
{
btKey2Current = 0;
if( btKey2Past == 1 )
{
btKey2Past = 0;
ucDateTmp--;
}
}
if( uiKey2Cnt >= 0x80 )
{
btKey2Current = 1;
if( btKey2Past == 0 )
btKey2Past = 1;
}
if( uiKey3Cnt < 0x80 )
{
btKey3Current = 0;
if( btKey3Past == 1 )
{
btKey3Past = 0;
ucDateTmp++;
}
}
if( uiKey3Cnt >= 0x80 )
{
btKey3Current = 1;
if( btKey3Past == 0 )
btKey3Past = 1;
}
uiKey1Cnt = 0x80 + cstKeyMaxNum / 3 * 2;
uiKey2Cnt = 0x80 + cstKeyMaxNum / 3 * 2;
uiKey3Cnt = 0x80 + cstKeyMaxNum / 3 * 2;
uiKeyAllCnt = cstKeyMaxNum;
}
}
}
}
4.效果图片展示
|