共2条
1/1 1 跳转至页
DX,IO 各位DX,能否贴几个IO口模拟串口的c语言程序啊
问
谢谢!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
答 1:
可以到我这里来看看http://www.glorywork.com
答 2:
在网上找了一个,就是不知道MODE_QUICK什么意思?因为不知道MODE_QUICK是什么意思?所以还是用不了
#include <reg51.h>
sbit BT_SND =P1^0;
sbit BT_REC =P1^1;
/**********************************************
IO 口模拟232通讯程序
使用两种方式的C程序 占用定时器0
**********************************************/
#define MODE_QUICK
#define F_TM F0
#define TIMER0_ENABLE TL0=TH0; TR0=1;
#define TIMER0_DISABLE TR0=0;
sbit ACC0= ACC^0;
sbit ACC1= ACC^1;
sbit ACC2= ACC^2;
sbit ACC3= ACC^3;
sbit ACC4= ACC^4;
sbit ACC5= ACC^5;
sbit ACC6= ACC^6;
sbit ACC7= ACC^7;
void IntTimer0() interrupt 1
{
F_TM=1;
}
//发送一个字符
void PSendChar(unsigned char inch)
{
#ifdef MODE_QUICK
ACC=inch;
F_TM=0;
BT_SND=0; //start bit
TIMER0_ENABLE; //启动
//先送出低位
while(!F_TM); BT_SND=ACC0; F_TM=0;
while(!F_TM); BT_SND=ACC1; F_TM=0;
while(!F_TM); BT_SND=ACC2; F_TM=0;
while(!F_TM); BT_SND=ACC3; F_TM=0;
while(!F_TM); BT_SND=ACC4; F_TM=0;
while(!F_TM); BT_SND=ACC5; F_TM=0;
while(!F_TM); BT_SND=ACC6; F_TM=0;
while(!F_TM); BT_SND=ACC7; F_TM=0;
while(!F_TM); BT_SND=1; F_TM=0;
while(!F_TM);
TIMER0_DISABLE; //停止timer
#else
unsigned char ii;
ii=0;
F_TM=0;
BT_SND=0; //start bit
TIMER0_ENABLE; //启动
while(!F_TM);
while(ii<8)
{
if(inch&1)
{ BT_SND=1; }
else
{ BT_SND=0; }
F_TM=0;
while(!F_TM);ii++;
inch>>=1;
}
BT_SND=1;
F_TM=0;
while(!F_TM);
#endif
TIMER0_DISABLE; //停止timer
}
//接收一个字符
unsigned char PGetChar()
{
#ifdef MODE_QUICK
TIMER0_ENABLE;
F_TM=0;
while(!F_TM); //等过起始位
ACC0=BT_REC;
TL0=TH0;
F_TM=0;
while(!F_TM);ACC1=BT_REC;F_TM=0;
while(!F_TM);ACC2=BT_REC;F_TM=0;
while(!F_TM);ACC3=BT_REC;F_TM=0;
while(!F_TM);ACC4=BT_REC;F_TM=0;
while(!F_TM);ACC5=BT_REC;F_TM=0;
while(!F_TM);ACC6=BT_REC;F_TM=0;
while(!F_TM);ACC7=BT_REC;F_TM=0;
while(!F_TM)
{
if(BT_REC) { break; }
}
TIMER0_DISABLE; //停止timer
return ACC;
#else
unsigned char rch,ii;
TIMER0_ENABLE;
F_TM=0;
ii=0;
rch=0;
while(!F_TM); //等过起始位
while(ii<8)
{
rch>>=1;
if(BT_REC)
{
rch|=0x80;
}
ii++;
F_TM=0;
while(!F_TM);
}
F_TM=0;
while(!F_TM)
{
if(BT_REC)
{ break; }
}
TIMER0_DISABLE; //停止timer
return rch;
#endif
}
//检查是不是有起始位
bit StartBitOn()
{return (BT_REC==0);}
void main()
{
unsigned char gch;
TMOD=0x22; /*定时器1为工作模式2(8位自动重装),0为模式2(8位自动重装) */
PCON=00;
TR0=0; //在发送或接收才开始使用
TF0=0;
TH0=(256-96); //9600bps 就是 1000000/9600=104.167微秒 执行的timer是
// 104.167*11.0592/12= 96
TL0=TH0;
ET0=1;
EA=1;
PSendChar(0x55);
PSendChar(0xaa);
PSendChar(0x00);
PSendChar(0xff);
while(1)
{
if(StartBitOn())
{
gch=PGetChar();
PSendChar(gch);
}
}
}
#include <reg51.h>
sbit BT_SND =P1^0;
sbit BT_REC =P1^1;
/**********************************************
IO 口模拟232通讯程序
使用两种方式的C程序 占用定时器0
**********************************************/
#define MODE_QUICK
#define F_TM F0
#define TIMER0_ENABLE TL0=TH0; TR0=1;
#define TIMER0_DISABLE TR0=0;
sbit ACC0= ACC^0;
sbit ACC1= ACC^1;
sbit ACC2= ACC^2;
sbit ACC3= ACC^3;
sbit ACC4= ACC^4;
sbit ACC5= ACC^5;
sbit ACC6= ACC^6;
sbit ACC7= ACC^7;
void IntTimer0() interrupt 1
{
F_TM=1;
}
//发送一个字符
void PSendChar(unsigned char inch)
{
#ifdef MODE_QUICK
ACC=inch;
F_TM=0;
BT_SND=0; //start bit
TIMER0_ENABLE; //启动
//先送出低位
while(!F_TM); BT_SND=ACC0; F_TM=0;
while(!F_TM); BT_SND=ACC1; F_TM=0;
while(!F_TM); BT_SND=ACC2; F_TM=0;
while(!F_TM); BT_SND=ACC3; F_TM=0;
while(!F_TM); BT_SND=ACC4; F_TM=0;
while(!F_TM); BT_SND=ACC5; F_TM=0;
while(!F_TM); BT_SND=ACC6; F_TM=0;
while(!F_TM); BT_SND=ACC7; F_TM=0;
while(!F_TM); BT_SND=1; F_TM=0;
while(!F_TM);
TIMER0_DISABLE; //停止timer
#else
unsigned char ii;
ii=0;
F_TM=0;
BT_SND=0; //start bit
TIMER0_ENABLE; //启动
while(!F_TM);
while(ii<8)
{
if(inch&1)
{ BT_SND=1; }
else
{ BT_SND=0; }
F_TM=0;
while(!F_TM);ii++;
inch>>=1;
}
BT_SND=1;
F_TM=0;
while(!F_TM);
#endif
TIMER0_DISABLE; //停止timer
}
//接收一个字符
unsigned char PGetChar()
{
#ifdef MODE_QUICK
TIMER0_ENABLE;
F_TM=0;
while(!F_TM); //等过起始位
ACC0=BT_REC;
TL0=TH0;
F_TM=0;
while(!F_TM);ACC1=BT_REC;F_TM=0;
while(!F_TM);ACC2=BT_REC;F_TM=0;
while(!F_TM);ACC3=BT_REC;F_TM=0;
while(!F_TM);ACC4=BT_REC;F_TM=0;
while(!F_TM);ACC5=BT_REC;F_TM=0;
while(!F_TM);ACC6=BT_REC;F_TM=0;
while(!F_TM);ACC7=BT_REC;F_TM=0;
while(!F_TM)
{
if(BT_REC) { break; }
}
TIMER0_DISABLE; //停止timer
return ACC;
#else
unsigned char rch,ii;
TIMER0_ENABLE;
F_TM=0;
ii=0;
rch=0;
while(!F_TM); //等过起始位
while(ii<8)
{
rch>>=1;
if(BT_REC)
{
rch|=0x80;
}
ii++;
F_TM=0;
while(!F_TM);
}
F_TM=0;
while(!F_TM)
{
if(BT_REC)
{ break; }
}
TIMER0_DISABLE; //停止timer
return rch;
#endif
}
//检查是不是有起始位
bit StartBitOn()
{return (BT_REC==0);}
void main()
{
unsigned char gch;
TMOD=0x22; /*定时器1为工作模式2(8位自动重装),0为模式2(8位自动重装) */
PCON=00;
TR0=0; //在发送或接收才开始使用
TF0=0;
TH0=(256-96); //9600bps 就是 1000000/9600=104.167微秒 执行的timer是
// 104.167*11.0592/12= 96
TL0=TH0;
ET0=1;
EA=1;
PSendChar(0x55);
PSendChar(0xaa);
PSendChar(0x00);
PSendChar(0xff);
while(1)
{
if(StartBitOn())
{
gch=PGetChar();
PSendChar(gch);
}
}
}
共2条
1/1 1 跳转至页
回复
有奖活动 | |
---|---|
【有奖活动】分享技术经验,兑换京东卡 | |
话不多说,快进群! | |
请大声喊出:我要开发板! | |
【有奖活动】EEPW网站征稿正在进行时,欢迎踊跃投稿啦 | |
奖!发布技术笔记,技术评测贴换取您心仪的礼品 | |
打赏了!打赏了!打赏了! |
打赏帖 | |
---|---|
【笔记】生成报错synthdesignERROR被打赏50分 | |
【STM32H7S78-DK评测】LTDC+DMA2D驱动RGBLCD屏幕被打赏50分 | |
【STM32H7S78-DK评测】Coremark基准测试被打赏50分 | |
【STM32H7S78-DK评测】浮点数计算性能测试被打赏50分 | |
【STM32H7S78-DK评测】Execute in place(XIP)模式学习笔记被打赏50分 | |
每周了解几个硬件知识+buckboost电路(五)被打赏10分 | |
【换取逻辑分析仪】RA8 PMU 模块功能寄存器功能说明被打赏20分 | |
野火启明6M5适配SPI被打赏20分 | |
NUCLEO-U083RC学习历程2-串口输出测试被打赏20分 | |
【笔记】STM32CUBEIDE的Noruletomaketarget编译问题被打赏50分 |