共2条
1/1 1 跳转至页

问
想在函数里操作P1口的任意一个位,就做了一个函数,可以选择操作指定的口线,发现函数的参数定义成bit时,寻址是在内部ram的20H,而不是所要的P1口90H。求教该怎么定义这个函数呢?多谢!
#include <reg52.h>
#include <intrins.h>
#define uchar unsigned char
void test(bit testport,uchar shijian);
void delay(uchar shijian);
main()
{
test(P1^0,10);
}
void test(bit testport,uchar shijian)
{
testport=1;
delay(shijian);
testport=0;
delay(shijian);
}
void delay(uchar shijian)
{
uchar i;
for(i=0;i<shijian;i++)
_nop_();
} 答 1: 改成宏吧#define test(TestPort,Period) {TestPort=1;delay(Period);TestPort=0;delay(Period);}
使用:
test(P1_0,30); 答 2: 是这样吗?编译不过啊代码改成了:
#include <reg52.h>
#include <absacc.h>
#include <intrins.h>
#define uchar unsigned char
#define test(TestPort,Period){ TestPort=1; delay(Period); TestPort=0; delay(Period);}
void delay(uchar shijian);
main()
{
test(P1^0,10);
}
void delay(uchar shijian)
{
uchar i;
for(i=0;i<shijian;i++)
_nop_();
}
TEST.C(11): error C141: syntax error near '='
TEST.C(11): error C141: syntax error near '='
怎么回事??? 答 3: 建议用at89x52.h头文件。如果你用reg52.h的话,就要自己定义IO,如sbit P1_0 = P1^0;
而不能直接在程序中使用P1^0. 答 4: 只是为什么宏定义非得写成一行呢?好像函数写成多行就不能编译?难道keil就是这么定义的?另外指针变量也需要把前面的*去掉吗?
我有一个函数是
void SendStr(bit TXD,unsigned char *str)
{
int i;
unsigned char j;
unsigned char ch;
for(i=0;i<MaxLenStr;i++){
ch=*(str+i);
if(ch=='\0') break;
else if(ch=='\n'){SendByte(TXD,10);SendByte(TXD2,13);}
else if(ch=='\t'){
for(j=0;j<TABNum;j++)
SendByte(TXD,' ');
}
else SendByte(TXD,ch);
}
}
改成宏定义后为
#define SendStr(TXD_num, str){int i; unsigned char j; unsigned char ch; for(i=0;i<MaxLenStr;i++){ ch=*(str+i); if(ch=='\0') break; else if(ch=='\n'){SendByte(TXD_num,10);SendByte(TXD_num,13);} else if(ch=='\t'){ for(j=0;j<TABNum;j++) SendByte(TXD_num,' '); } else SendByte(TXD_num,ch); }}
但是结果不是如我所想啊。
答 5: 写成多行时,要使用续行符\
#include <reg52.h>
#include <intrins.h>
#define uchar unsigned char
void test(bit testport,uchar shijian);
void delay(uchar shijian);
main()
{
test(P1^0,10);
}
void test(bit testport,uchar shijian)
{
testport=1;
delay(shijian);
testport=0;
delay(shijian);
}
void delay(uchar shijian)
{
uchar i;
for(i=0;i<shijian;i++)
_nop_();
} 答 1: 改成宏吧#define test(TestPort,Period) {TestPort=1;delay(Period);TestPort=0;delay(Period);}
使用:
test(P1_0,30); 答 2: 是这样吗?编译不过啊代码改成了:
#include <reg52.h>
#include <absacc.h>
#include <intrins.h>
#define uchar unsigned char
#define test(TestPort,Period){ TestPort=1; delay(Period); TestPort=0; delay(Period);}
void delay(uchar shijian);
main()
{
test(P1^0,10);
}
void delay(uchar shijian)
{
uchar i;
for(i=0;i<shijian;i++)
_nop_();
}
TEST.C(11): error C141: syntax error near '='
TEST.C(11): error C141: syntax error near '='
怎么回事??? 答 3: 建议用at89x52.h头文件。如果你用reg52.h的话,就要自己定义IO,如sbit P1_0 = P1^0;
而不能直接在程序中使用P1^0. 答 4: 只是为什么宏定义非得写成一行呢?好像函数写成多行就不能编译?难道keil就是这么定义的?另外指针变量也需要把前面的*去掉吗?
我有一个函数是
void SendStr(bit TXD,unsigned char *str)
{
int i;
unsigned char j;
unsigned char ch;
for(i=0;i<MaxLenStr;i++){
ch=*(str+i);
if(ch=='\0') break;
else if(ch=='\n'){SendByte(TXD,10);SendByte(TXD2,13);}
else if(ch=='\t'){
for(j=0;j<TABNum;j++)
SendByte(TXD,' ');
}
else SendByte(TXD,ch);
}
}
改成宏定义后为
#define SendStr(TXD_num, str){int i; unsigned char j; unsigned char ch; for(i=0;i<MaxLenStr;i++){ ch=*(str+i); if(ch=='\0') break; else if(ch=='\n'){SendByte(TXD_num,10);SendByte(TXD_num,13);} else if(ch=='\t'){ for(j=0;j<TABNum;j++) SendByte(TXD_num,' '); } else SendByte(TXD_num,ch); }}
但是结果不是如我所想啊。
答 5: 写成多行时,要使用续行符\
共2条
1/1 1 跳转至页
回复
有奖活动 | |
---|---|
【EEPW电子工程师创研计划】技术变现通道已开启~ | |
发原创文章 【每月瓜分千元赏金 凭实力攒钱买好礼~】 | |
【EEPW在线】E起听工程师的声音! | |
“我踩过的那些坑”主题活动——第001期 | |
高校联络员开始招募啦!有惊喜!! | |
【工程师专属福利】每天30秒,积分轻松拿!EEPW宠粉打卡计划启动! | |
送您一块开发板,2025年“我要开发板活动”又开始了! | |
打赏了!打赏了!打赏了! |
打赏帖 | |
---|---|
【我踩过的那些坑】结构堵孔导致的喇叭无声问题被打赏50分 | |
【我踩过的那些坑】分享一下调试一款AD芯片的遇到的“坑”被打赏50分 | |
电流检测模块MAX4080S被打赏10分 | |
【我踩过的那些坑】calloc和malloc错误使用导致跑飞问题排查被打赏50分 | |
多组DCTODC电源方案被打赏50分 | |
【我踩过的那些坑】STM32cubeMX软件的使用过程中的“坑”被打赏50分 | |
新手必看!C语言精华知识:表驱动法被打赏50分 | |
【我踩过的那些坑】杜绑线问题被打赏50分 | |
【我踩过的那些坑】STM32的硬件通讯调试过程的“坑”被打赏50分 | |
【我踩过的那些坑】晶振使用的问题被打赏100分 |