Micro-C - 帮助监控8051上的开关 [英] Micro-C -- Help monitoring a switch on 8051

查看:46
本文介绍了Micro-C - 帮助监控8051上的开关的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我必须使用micro-C为8051微控制器编写一个程序来监控开关1并且如果切换到按下消息切换1

推送应显示在液晶显示屏上。另外微控制器

应该在LCD上显示施加到输入端的电压值

.


上述程序只应在用户输入后执行

1234使用连接到8051微处理器的键盘。


我想我可以找出键盘输入功能,它将等到用户输入的
1234 QUOT;在继续之前。这就是我到目前为止所用的那部分

...


我还没有使用微控制器对它进行测试但是这段代码编译了

使用Bipom Micro-Ide编译器。


#include< 8051io.h>

#include< 8051bit.h>

#include< 8051reg.h>


#define MAX_ROWS 4

#define MAX_COLS 4

静态字符KeyTable [] = {''1'',''2'',''3'',''A'',

''4'', ''''',''''',''B'',

''7'',''8'',''9'',''C'',

''*'','0'',''#'',''D''

};


static unsigned char RowTable [] = {0xFE,0xFD,0xFB,0xF7};


char ScanKeypad();


main ()

{

char key;


serinit(9600);

for(; ;)

{


key = ScanKeypad();


if(key ==" 1234")

{

printf(" \ nKey:''%c'' ",key);

//从这里继续执行其余的程序

}


printf(" \ n不正确代码已输入\ n;

}

}

char ScanKeypad()

{

char row;

char col;


col = 0;


for(row = 0;行< MAX_ROWS;行++)

{

P2 = RowTable [row];


if(!(P2& 0x80))

col = 4;


if(!(P2& 0x40))

col = 3;


if(!(P2& 0x20))

col = 2;


if(!(P2& 0x10))

col = 1;


if(col!= 0)

{

delay( 500);

返回KeyTable [col-1 + row * MAX_COLS];

}

}

返回0;

}


现在我不知道如何监控开关1 ...我试图制作

针对该特定要求的程序,这是我得到的:


#include< 8051io.h>

#include< 8051bit.h>

#include< 8051reg.h>

#define READ P0.1 / * READ = SET& WRITE = RESET * /

#define STROBE P0.2 / * DATA STROBE(ENABLE)* /

#define CTRL P0.0 / * CONTROL BIT(CONTROL = RESET & DATA = SET)* /


void InitLCD();

void WriteCtrl(unsigned char value);

void WriteData(unsigned char value);

void WriteCommon(unsigned char value);

void WriteLCD(unsigned char * message);

void CursorHome ();

void CursorSet(unsigned char pos);


main()

{

/ *将串口设置为9600 Baud * /

serinit(9600);


while(1)

{

//监控开关1 - 显示消息

// LCD如果按下开关

while(!(P3& ; 4))

{

InitLCD();

for(;;)

{

/ *向LCD写一条简单的消息* /

CursorHome();

WriteLCD(按下开关1);

}


}

}


//初始化和控制LCD的功能

void InitLCD()

{

/ *稍等一下上电* /

延迟(200);

/ *将LCD初始化为4位模式* /

WriteCtrl(3) ;

延迟(50);

WriteCtrl(3);

延迟(10);

WriteCtrl( 3);

延迟(10);

WriteCtrl(2);

延迟(10);

/ *功能设置* /

WriteCtrl(2);

延迟(10);

WriteCtrl(8);

延迟(10);

/ *显示OFF * /

WriteCtrl(0);

延迟(10);

WriteCtrl(8);

延迟(10);

/ *显示ON * /

WriteCtrl(0) ;

延迟(10);

WriteCtrl(0x0F);

延迟(10);

/ *进入模式* /

WriteCtrl(0);

延迟(10);

WriteCtrl(6);

延迟(10);

/ *清除屏幕* /

WriteCtrl(0);

延迟(10);

WriteCtrl(1);

延迟(100);

/ *光标回家* /

WriteCtrl(0);

延迟(10);

WriteCtrl(2);

延迟(100);

}

void CursorSet(unsigned char pos)

{

WriteCtrl(8 | ((pos>> 4)& 7));

delay(1);

WriteCtrl(pos& 0x0F);

延迟(1);

}

无效CursorHome()

{

/ *光标主页* /

WriteCtrl(0);

延迟(1);

WriteCtrl(2);

延迟(1 );

}

void WriteLCD(unsigned char * message)

{

unsigned char i;

for(i = 0; i< 20; i ++)

{

if(!message [i])

break;

WriteData(message [i]);

}

}

void WriteCtrl(unsigned char价值)

{

clrbit(CTRL);

WriteCommon(价值);

}

void WriteData(unsigned char value)

{

setbit(CTRL);

WriteCommon(value>> 4) ;

WriteCommon(值);

}

void WriteCommon(无符号字符值)

{

clrbit(READ);

value = value& 0x0F;

value = value<< 4;

P0 = P0& 0x0F;

P0 = P0 |价值;

setbit(STROBE);

延迟(1);

clrbit(STROBE);

setbit (阅读);

延迟(1);

}


我相信这将有助于监控交换机并显示
LCD上的
消息,但现在我真的不知道如何在

LCD中显示施加到ADC输入端的电压值。可以

有人请帮忙,并确认上面发布的程序

会考虑到规定的要求吗?


谢谢

解决方案

webzila< ae ******* @ yahoo.com>写道:

我必须使用micro-C编写一个8051微控制器的程序来监控开关1,如果开关按下了开关1的话。推"应显示在液晶显示屏上。此外,微控制器应在LCD中显示施加到ADC输入端的电压值。


这完全偏离主题。这个组是关于语言

C,而不是如何使用它来处理一些微控制器。但是

因为你的代码有很多C问题我会对此进行评论。

上述程序只应该用户输入后执行
1234使用连接到8051微处理器的键盘。
我想我可以弄清楚等待用户输入1234的键盘输入功能。在继续之前。这是我到目前为止所拥有的那个部分...
我还没有使用微控制器测试它,但是使用Bipom Micro-Ide编译器编译了这段代码。


希望这是一个符合标准的C编译器,否则你不会在这里获得帮助
。找到一个处理编译器问题的小组,如果你在那里遇到问题。并且,如果可能的话,让你的编译器发出尽可能多的警告,你可能会感到惊讶......

#include< 8051io .h>
#include< 8051bit.h>
#include< 8051reg.h>


非标准头文件。但是你错过了必要的那些

喜欢< stdio.h> ;.

#define MAX_ROWS 4
#define MAX_COLS 4
static char KeyTable [] = {''1'',''2'',''3'',''A'',
''4'',''''','''6' ',''''','''''''','''''''''''''''''''''''''''' ',''''',''D''
};
static unsigned char RowTable [] = {0xFE,0xFD,0xFB,0xF7};
char ScanKeypad();


制作


char ScanKeypad(无效);


因为该功能显然没有'不接受论点。

main()


int main(无效)

{
char key;
serinit(9600);
for(;;)
{
key = ScanKeypad();
if(key ==" 1234")


这是什么? ''key''是一个字符 - 但您尝试将其与文字字符串的

地址进行比较。你的编译器应该给你一个很大的

警告,当然,这不会起作用。


如果你想检查是否ScanKeypad()函数首先返回

字符''1',然后,在下次调用时返回''2'',然后返回''3'',最后返回

''4''然后你必须调用该函数四次并比较每次回合的

返回值。或者调用它四次,将返回的

值填充到一个char数组中,追加一个尾随的''\0''并使用strcmp()来表示

比较字符串至1234。提示:==运算符不能用

来比较字符串而只是字符串的地址。

{
printf(" \\\
Key:''%c) ''",key);
//从这里继续执行其余程序
}
printf(" \ nIncorrect Code Entered\\\
);
}


在这里缺少返回statememnt,main()应该返回一个int。

}


顺便说一句,缩进你的代码使得它更容易阅读。

char ScanKeypad()
{char row;
char col;
col = 0;
for(row = 0; row< MAX_ROWS; row ++)
{
P2 = RowTable [row];


''P2''从未定义过,所以已经应该让你的编译器发出错误消息了。

if(!(P2& 0x80))
col = 4;
if(!(P2& 0x40))
col = 3;
if(!(P2& 0x20))
col = 2;
if(!(P2& 0x10))
col = 1;
if(col!= 0)
{
延迟(500);


非标准功能...即使它符合名称提示,

即等待一段时间,为什么要使用它这里只是

从函数返回之前?

返回KeyTable [col-1 + row * MAX_COLS];
}
}
返回0;
}


让我们假设您将''P2'定义为char。然后这个函数将

总是返回0.所以我想''P2''的值必须以某种方式在某些用户输入(通过阅读键盘)中受到
的影响或者其他什么)

或者它完全没用 - 但你的代码没有任何东西可以实现这一点。


如果你正在改进这个功能,为什么不为你正在使用的魔术数字定义一些可读的

名称,比如


#define COLUMN_4_BIT 0x80

#define COLUMN_3_BIT 0x40

#define COLUMN_2_BIT 0x20

#define COLUMN_1_BIT 0x10


甚至


#define COLUMN_BIT(x)(0x10<<((x) - 1))


为了使用例如COLUMN_BIT(3)而不是0x40?下一个程序员

必须阅读你的代码将不胜感激。

现在我不知道如何监控交换机1 ...我试图制作
针对该特定要求的程序,这是我得到的:


你不会在comp.lang.c中发现这一点。所有系统或

硬件依赖通常都是非常不满意的。

#include< 8051io.h>
#include< 8051bit.h>
#include< 8051reg.h>


再次非标准头文件。

#define READ P0.1 / * READ = SET& WRITE = RESET * /


'P0.1'应该是什么?你尝试使用它作为setbit()和clrbit()函数的参数...但它既不是字符串也不是
某些值,所以你的编译器应该对此感到不安。

#define STROBE P0.2 / * DATA STROBE(ENABLE)* /
#define CTRL P0.0 / * CONTROL BIT(CONTROL = RESET& DATA = SET)* /


在这里相同。

void InitLCD();


void InitLCD(void);

void WriteCtrl(unsigned char value);
void WriteData(unsigned char value);
void WriteCommon(unsigned char value);
void WriteLCD(unsigned char * message);
void CursorHome();


void CursorHome(void);

void CursorSet(unsigned char pos);
main()


int main(无效)

{
/ *将串口设置为9600 Baud * /
serinit(9600);
while(1)
{监控开关1 - 显示消息
// LCD如果按下开关
while(!(P3& 4)) )


''P3''在范围内的任何地方都没有定义(因此它也没有价值
分配给它的
)。

{
InitLCD();
for(;;)
{
/ *向LCD写一条简单的信息* /
CursorHome ();
WriteLCD(按下开关1);
}
}
}


你错过了这里有一个return语句和一个结束''}'。什么'

无限循环内的不定式循环中的无限循环

有用吗?单个无穷大而不是ggod足够吗?

void WriteLCD(unsigned char * message)
{
unsigned char i;
for(i = 0; i< ; 20; i ++)
{
if(!message [i])
break;
WriteData(message [i]);
}
}


怎么样


void WriteLCD(unsigned char * message)

{

size_t i;

for(i = 0; * message&& i< 20; i ++)

WriteData(* message ++);

}


如果你给出20

a符号名称,你可能会帮自己一个忙,只是猜测我会推荐


#define DISPLAY_WIDTH 20

void WriteCommon(无符号字符值)
{
clrbit(READ);
value = value& 0x0F;
value = value<< 4;
P0 = P0& 0x0F;
P0 = P0 |值;


两者都没有定义''P0'',也不是它的价值(并且,通过代理,你的论证的价值是'/ $
''' value'')在此函数中的任何位置使用。那么什么'

所有这些应该是有益的?

setbit(STROBE);
延迟(1);
clrbit(STROBE) );
setbit(READ);
延迟(1);
}
我相信这可以用来监控开关并在液晶显示器上显示
信息但是现在我真的不知道如何在
LCD上显示施加到ADC输入端的电压值。可以请某人帮忙,并确认上面发布的程序
会考虑到规定的要求吗?




因为程序可能赢了'甚至编译他们是不太可能

做你想要的。但是,由于这里真的是错误的地方询问有关硬件特定编程问题的问题,你最好找到一个不同的

这个问题的地方。

问候,Jens

-

\ Jens Thoms Toerring ___ Je *********** @ physik.fu-berlin.de

\ __________________________ http://www.toerring.de


>那个'这里完全偏离主题。这个组是关于语言

C,而不是如何使用它来处理一些微控制器。但是,由于您的代码存在相当多的C问题,我将对此发表评论。


同意它是OT,但是给出了一些建议,

虽然对于标准实施是正确的,但不适用

在这种情况下可能会混淆OP。


是的:完全在这里加油 - 最好找一个比较熟悉C的人

微型嵌入式控制器和请求帮助。

#include< 8051io.h>
#include< 8051bit.h>
#include< 8051reg.h>
非标准头文件。但是你错过了必要的那些
喜欢< stdio.h>。


此环境中没有操作系统,因此没有

标准 I / O.


#define MAX_ROWS 4
#define MAX_COLS 4


main()


int main(void)
这里缺少返回statememnt,main()应该返回一个int。




没有操作系统,因此没有返回一个值。

如果主要返回(它不应该在普通的嵌入式系统中),

系统将挂起无限循环或重启取决于

如何配置。

P2 = RowTable [row];


''P2''从未被定义过,因此已经让你的编译器发出错误信息。




P2是特别的功能寄存器(SFR)用于访问8051s

内部并行I / O端口#2 - 在8051reg.h中定义


让我们假设您定义了'' P2''是个字符。然后这个函数将总是返回0.所以我猜'P2''的值必须以某种方式被一些用户输入(通过读键盘或其他任何东西)所影响
或它完全没用 - 但你的代码没有任何东西可以实现这一点。




实际上确实如此 - P2是一个硬件注册。

#define READ P0.1 / * READ = SET& WRITE = RESET * /


'P0.1'应该是什么?你试图将它作为setbit()和clrbit()函数的参数 - 但它既不是字符串也不是某些值,所以你的编译器应该对此感到不安。 / blockquote>


因为你问--setbit()和clrbit()是宏,而不是函数,它们生成一个内联汇编SETB或CLR指令 - P0.1是

该指令的操作数(表示端口0 - 位1)

while(!( P3& 4))


'''''''''''''''''''''''''''''''''''''''''''''''


8051并行I / O端口#3。

P0 = P0& 0x0F;
P0 = P0 |值;


在任何地方都没有定义''P0',也不是它的价值(并且,通过代理,你的论证的价值''值')在任何地方使用在这个功能。那么什么是应该有用的呢?




8051输出端口0.

问候,

Dave(Micro-C的作者)

-

Dunfield开发系统 http://www.dunfield.com

嵌入式系统的低成本软件开发工具

软件/固件开发服务传真:613-256-5821


Dave Dunfield< Da *********** @ use.techsupport。 link.on.my.websit e>写道:

这是完全偏离主题的。这个组是关于C语言的,而不是如何使用它来处理一些微控制器。但是,由于您的代码存在相当多的C问题,我将对此发表评论。

同意它是OT,但是给出的一些建议虽然对于标准实现是正确的,但在这种情况下并不适用
混淆OP。
是的:在这里完全加入 - 最好找到一个熟悉C /
微型嵌入式控制器的人并寻求帮助。

#include< 8051io.h>
#include< 8051bit.h>
#include< 8051reg.h>

非标准头文件。但是你错过了必要的那些
喜欢< stdio.h>。
在这种环境下没有操作系统,因此没有标准操作系统。 I / O。


好​​吧,我不知道他是独立实施的 - 可能

已经是一些DOS盒子,他试图访问一些内置硬件 -

ware。

P2 = RowTable [row];


''P2''从未定义过,所以已经应该让你的编译器发出错误信息。
P2是用于访问8051s的特殊功能寄存器(SFR)
内部并行I / O端口#2 - 在8051reg.h中定义
假设你定义了''P2''到是个角色。然后这个函数将总是返回0.所以我猜'P2''的值必须以某种方式被一些用户输入(通过读键盘或其他任何东西)所影响
或它完全没用 - 但你的代码没有任何东西可以实现这一点。



实际上确实如此 - P2是一个硬件寄存器。


所以这是一些深刻的,非标准的魔法 - 我在考虑是否

Px是宏,但是他们不会出现在左侧和

右侧的任务(并像P0.1一样使用)......

#define READ P0.1 / * READ = SET& WRITE = RESET * /


'P0.1'应该是什么?你试图将它作为setbit()和clrbit()函数的参数 - 但它既不是字符串也不是某些值,所以你的编译器应该对此感到不安。 / blockquote>



因为你问过--setbit()和clrbit()是宏,而不是函数,它们生成一个内联汇编SETB或CLR指令 - P0.1是该指令的操作数(意思是端口0 - 位1)




感谢您的澄清。你知道一个更合适的新闻 -

组可以询问吗?

问候,Jens

-

\ Jens Thoms Toerring ___ Je *********** @ physik.fu-berlin.de

\ __________________________ http: //www.toerring.de


Hello,

I have to write a program for an 8051 micro-controller using micro-C to
monitor Switch 1 and if the switch in pushed the message "switch 1
pushed" should be displayed in the LCD. Also the microcontroller
should display in the LCD the value of the voltage applied to the input
of the ADC.

The above procedure should only execute once the user has entered
"1234" using a keypad that is attached to the 8051 microprocessor.

I think I can figure out the keypad entry function that will wait until
the user has entered "1234" before proceeding. Here is what I have for
that part so far...

I havent tested it with a micro-controller yet but this code compiled
using the Bipom Micro-Ide compiler.

#include <8051io.h>
#include <8051bit.h>
#include <8051reg.h>

#define MAX_ROWS 4
#define MAX_COLS 4

static char KeyTable[] = { ''1'', ''2'', ''3'', ''A'',
''4'', ''5'', ''6'', ''B'',
''7'', ''8'', ''9'', ''C'',
''*'', ''0'', ''#'', ''D''
};

static unsigned char RowTable[] = { 0xFE, 0xFD, 0xFB, 0xF7 };

char ScanKeypad();

main()
{
char key;

serinit(9600);
for( ;; )
{

key = ScanKeypad();

if( key == "1234" )
{
printf( "\nKey: ''%c''", key );
//Proceed with the rest of the program from here
}

printf ("\nIncorrect Code Entered\n");
}
}
char ScanKeypad()
{
char row;
char col;

col = 0;

for( row=0; row<MAX_ROWS; row++ )
{
P2 = RowTable[row];

if( !(P2 & 0x80) )
col = 4;

if( !(P2 & 0x40) )
col = 3;

if( !(P2 & 0x20) )
col = 2;

if( !(P2 & 0x10) )
col = 1;

if( col != 0 )
{
delay(500);
return KeyTable[col-1 + row*MAX_COLS];
}
}
return 0;
}

Now I am not sure how to monitor the switch 1...I tried to make the
program for that specific requirement and here is what I got:

#include <8051io.h>
#include <8051bit.h>
#include <8051reg.h>
#define READ P0.1 /*READ=SET & WRITE=RESET*/
#define STROBE P0.2 /*DATA STROBE (ENABLE)*/
#define CTRL P0.0 /*CONTROL BIT (CONTROL=RESET & DATA=SET)*/

void InitLCD();
void WriteCtrl( unsigned char value );
void WriteData( unsigned char value );
void WriteCommon( unsigned char value );
void WriteLCD( unsigned char* message );
void CursorHome();
void CursorSet( unsigned char pos );

main()
{

/* Set the serial port to 9600 Baud */
serinit(9600);

while(1)
{
// Monitor Switch 1 - Display message on
// LCD if switch is pressed
while(!(P3 & 4))
{
InitLCD();
for(;;)
{
/* Write a simple message to the LCD */
CursorHome();
WriteLCD( "Switch 1 is pressed" );
}

}
}

//Functions to initialize and control the LCD
void InitLCD()
{
/* Wait a bit after power-up */
delay(200);
/* Initialize the LCD to 4-bit mode */
WriteCtrl(3);
delay(50);
WriteCtrl(3);
delay(10);
WriteCtrl(3);
delay(10);
WriteCtrl(2);
delay(10);
/* Function Set */
WriteCtrl(2);
delay(10);
WriteCtrl(8);
delay(10);
/* Display OFF */
WriteCtrl(0);
delay(10);
WriteCtrl(8);
delay(10);
/* Display ON */
WriteCtrl(0);
delay(10);
WriteCtrl(0x0F);
delay(10);
/* Entry mode */
WriteCtrl(0);
delay(10);
WriteCtrl(6);
delay(10);
/* Clear Screen */
WriteCtrl(0);
delay(10);
WriteCtrl(1);
delay(100);
/* Cursor home */
WriteCtrl(0);
delay(10);
WriteCtrl(2);
delay(100);
}
void CursorSet( unsigned char pos )
{
WriteCtrl(8 | ( (pos>>4) & 7 ) );
delay(1);
WriteCtrl(pos & 0x0F);
delay(1);
}
void CursorHome()
{
/* Cursor home */
WriteCtrl(0);
delay(1);
WriteCtrl(2);
delay(1);
}
void WriteLCD( unsigned char* message )
{
unsigned char i;
for( i=0; i<20; i++ )
{
if( !message[i] )
break;
WriteData(message[i]);
}
}
void WriteCtrl( unsigned char value )
{
clrbit(CTRL);
WriteCommon( value );
}
void WriteData( unsigned char value )
{
setbit(CTRL);
WriteCommon( value >> 4 );
WriteCommon( value );
}
void WriteCommon( unsigned char value )
{
clrbit(READ);
value = value & 0x0F;
value = value << 4;
P0 = P0 & 0x0F;
P0 = P0 | value;
setbit(STROBE);
delay(1);
clrbit(STROBE);
setbit(READ);
delay(1);
}

I believe that will work for monitoring the switch and displaying the
message on the LCD but now I really have no idea how to display in the
LCD the value of the voltage applied to the input of the ADC. Could
someone help with that please and verify that the programs posted above
would work considering the stated requirements?

Thank you

解决方案

webzila <ae*******@yahoo.com> wrote:

I have to write a program for an 8051 micro-controller using micro-C to
monitor Switch 1 and if the switch in pushed the message "switch 1
pushed" should be displayed in the LCD. Also the microcontroller
should display in the LCD the value of the voltage applied to the input
of the ADC.
That''s completely off-topic here. This group is about the language
C, not how to use it for dealing with some micro-controller. But
since there''s quite a number of C issues with your code I will
comment on this.
The above procedure should only execute once the user has entered
"1234" using a keypad that is attached to the 8051 microprocessor. I think I can figure out the keypad entry function that will wait until
the user has entered "1234" before proceeding. Here is what I have for
that part so far... I havent tested it with a micro-controller yet but this code compiled
using the Bipom Micro-Ide compiler.
Hopefully, this is a standard compliant C compiler, otherwise you won''t
get help here. Find a group that deals with issues of your compiler if
you get problems there. And, if that is possible, make your compiler emit
as many warnings as possible, you''re probably going to be surprised...
#include <8051io.h>
#include <8051bit.h>
#include <8051reg.h>
Non-standard header files. But then you''re missing essential ones
like <stdio.h>.
#define MAX_ROWS 4
#define MAX_COLS 4 static char KeyTable[] = { ''1'', ''2'', ''3'', ''A'',
''4'', ''5'', ''6'', ''B'',
''7'', ''8'', ''9'', ''C'',
''*'', ''0'', ''#'', ''D''
}; static unsigned char RowTable[] = { 0xFE, 0xFD, 0xFB, 0xF7 }; char ScanKeypad();
Make that

char ScanKeypad( void );

since the function obviously doesn''t accept arguments.
main()
int main( void )
{
char key; serinit(9600);
for( ;; )
{ key = ScanKeypad(); if( key == "1234" )
What''s that? ''key'' is a character - but you try to compare it to the
address of a literal string. Your compiler should give you a big fat
warning for that and, of course, that won''t work.

If you want to check if the ScanKeypad() function returned first the
character ''1'', then, on the next invocation, ''2'', then ''3'', and finally
''4'' then you have to call the function four times and compare the
return value each time round. Or call it four times, stuff the return
values into a char array, append a trailing ''\0'' and use strcmp() to
compare the string to "1234". Hint: the "==" operator can''t be used
to compare strings but just the addresses of strings.
{
printf( "\nKey: ''%c''", key );
//Proceed with the rest of the program from here
} printf ("\nIncorrect Code Entered\n");
}
Missing return statememnt here, main() is supposed to return an int.
}
BTW, indenting your code consistently makes it much easier to read.
char ScanKeypad()
{
char row;
char col; col = 0; for( row=0; row<MAX_ROWS; row++ )
{
P2 = RowTable[row];
''P2'' has never been defined, so that already should make your compiler
emit an error message.
if( !(P2 & 0x80) )
col = 4; if( !(P2 & 0x40) )
col = 3; if( !(P2 & 0x20) )
col = 2; if( !(P2 & 0x10) )
col = 1; if( col != 0 )
{
delay(500);
Non-standard function... And even if it does what the name hints at,
i.e. waiting for a certain time, why would you use that here just
before returning from the function?
return KeyTable[col-1 + row*MAX_COLS];
}
}
return 0;
}
Lets assume you defined ''P2'' to be a char. Then this function will
always return 0. So I guess the value of ''P2'' must somehow be in-
fluenced by some user input (via reading the keyboard or whatever)
or it''s completely useless - but your code doesn''t has anything
that would make that happen.

And if you''re at improving that function, why not define a few readable
names for the magic numbers you''re using, like

#define COLUMN_4_BIT 0x80
#define COLUMN_3_BIT 0x40
#define COLUMN_2_BIT 0x20
#define COLUMN_1_BIT 0x10

or even

#define COLUMN_BIT( x ) ( 0x10 << ( ( x ) - 1 ) )

in order to use e.g. COLUMN_BIT(3) instead of 0x40? The next programmer
having to read your code will be grateful.
Now I am not sure how to monitor the switch 1...I tried to make the
program for that specific requirement and here is what I got:
You won''t find out about that in comp.lang.c. Everything system or
hardware dependent is usually heavily frowned upon here.
#include <8051io.h>
#include <8051bit.h>
#include <8051reg.h>
Again non-standard header files.
#define READ P0.1 /*READ=SET & WRITE=RESET*/
What''s ''P0.1'' supposed to be? You try to use that as the argument of
the setbit() and clrbit() functions - but it''s neither a string nor
some value, so your compiler should get rather upset about that.
#define STROBE P0.2 /*DATA STROBE (ENABLE)*/
#define CTRL P0.0 /*CONTROL BIT (CONTROL=RESET & DATA=SET)*/
Same here.
void InitLCD();
void InitLCD( void );
void WriteCtrl( unsigned char value );
void WriteData( unsigned char value );
void WriteCommon( unsigned char value );
void WriteLCD( unsigned char* message );
void CursorHome();
void CursorHome( void );
void CursorSet( unsigned char pos ); main()
int main( void )
{ /* Set the serial port to 9600 Baud */
serinit(9600); while(1)
{
// Monitor Switch 1 - Display message on
// LCD if switch is pressed
while(!(P3 & 4))
''P3'' isn''t defined anywhere in scope (and so it also hasn''t a value
assigned to it).
{
InitLCD();
for(;;)
{
/* Write a simple message to the LCD */
CursorHome();
WriteLCD( "Switch 1 is pressed" );
}
}
}
You''re missing a return statement and a closing ''}'' here. And what''s
an infinite loop within an infinitive loop within an infinite loop
good for? Is a single infinity not ggod enough for you?
void WriteLCD( unsigned char* message )
{
unsigned char i;
for( i=0; i<20; i++ )
{
if( !message[i] )
break;
WriteData(message[i]);
}
}
What about

void WriteLCD( unsigned char* message )
{
size_t i;
for ( i = 0; *message && i < 20; i++ )
WriteData( *message++ );
}

And you probably would do yourself a favour if you would give the 20
a symbolic name, just guessing I would recommend

#define DISPLAY_WIDTH 20
void WriteCommon( unsigned char value )
{
clrbit(READ);
value = value & 0x0F;
value = value << 4;
P0 = P0 & 0x0F;
P0 = P0 | value;
Neither is ''P0'' defined anywhere, nor is it''s value (and, by proxy, the
value of your argument ''value'') use anywhere in this function. So what''s
all that supposed to be good for?
setbit(STROBE);
delay(1);
clrbit(STROBE);
setbit(READ);
delay(1);
} I believe that will work for monitoring the switch and displaying the
message on the LCD but now I really have no idea how to display in the
LCD the value of the voltage applied to the input of the ADC. Could
someone help with that please and verify that the programs posted above
would work considering the stated requirements?



Since the programs probably won''t even compile they are rather unlikely
to do what you want. But since here is really the wrong place to ask
about hardware specific programming issues you better find a different
place for this kind of questions.
Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de


>That''s completely off-topic here. This group is about the language

C, not how to use it for dealing with some micro-controller. But
since there''s quite a number of C issues with your code I will
comment on this.
Agreed that it is OT, however some of the advice that was given,
although correct for a standard implementation, does not apply
in this situation and may confuse the OP.

Yes: Totally OT here - best to find someone familier with C on
tiny embedded controllers and ask for help.

#include <8051io.h>
#include <8051bit.h>
#include <8051reg.h> Non-standard header files. But then you''re missing essential ones
like <stdio.h>.
There is no operating system in this environment, hence there is no
"standard" I/O.

#define MAX_ROWS 4
#define MAX_COLS 4
main()


int main( void )
Missing return statememnt here, main() is supposed to return an int.



There is no operating system, hence nothing to return a value to.
If main ever returns (which it shouldn''t in a normal embedded system),
the system will either hang in an infinite loop or restart depending on
how it is configured.

P2 = RowTable[row];


''P2'' has never been defined, so that already should make your compiler
emit an error message.



P2 is the Special Function Register (SFR) used to access the 8051s
internal parallel I/O port #2 - defined in 8051reg.h

Lets assume you defined ''P2'' to be a char. Then this function will
always return 0. So I guess the value of ''P2'' must somehow be in-
fluenced by some user input (via reading the keyboard or whatever)
or it''s completely useless - but your code doesn''t has anything
that would make that happen.



Actually it does - P2 is a hardware register.

#define READ P0.1 /*READ=SET & WRITE=RESET*/


What''s ''P0.1'' supposed to be? You try to use that as the argument of
the setbit() and clrbit() functions - but it''s neither a string nor
some value, so your compiler should get rather upset about that.



Since you asked - setbit() and clrbit() are macros, not functions, which
generate a single inline assembly SETB or CLR instruction - P0.1 is
the operand to that instruction (meaning port 0 - bit 1)

while(!(P3 & 4))


''P3'' isn''t defined anywhere in scope (and so it also hasn''t a value
assigned to it).



8051 parallel I/O port #3.

P0 = P0 & 0x0F;
P0 = P0 | value;


Neither is ''P0'' defined anywhere, nor is it''s value (and, by proxy, the
value of your argument ''value'') use anywhere in this function. So what''s
all that supposed to be good for?



8051 output port 0.
Regards,
Dave (Author of Micro-C)
--
Dunfield Development Systems http://www.dunfield.com
Low cost software development tools for embedded systems
Software/firmware development services Fax:613-256-5821


Dave Dunfield <Da***********@use.techsupport.link.on.my.websit e> wrote:

That''s completely off-topic here. This group is about the language
C, not how to use it for dealing with some micro-controller. But
since there''s quite a number of C issues with your code I will
comment on this.

Agreed that it is OT, however some of the advice that was given,
although correct for a standard implementation, does not apply
in this situation and may confuse the OP. Yes: Totally OT here - best to find someone familier with C on
tiny embedded controllers and ask for help.

#include <8051io.h>
#include <8051bit.h>
#include <8051reg.h>

Non-standard header files. But then you''re missing essential ones
like <stdio.h>. There is no operating system in this environment, hence there is no
"standard" I/O.
Ok, didn''t knew that he''s on a freestanding implementation - could
have been some DOS-box where he tries to access some built-in hard-
ware.
P2 = RowTable[row];


''P2'' has never been defined, so that already should make your compiler
emit an error message. P2 is the Special Function Register (SFR) used to access the 8051s
internal parallel I/O port #2 - defined in 8051reg.h Lets assume you defined ''P2'' to be a char. Then this function will
always return 0. So I guess the value of ''P2'' must somehow be in-
fluenced by some user input (via reading the keyboard or whatever)
or it''s completely useless - but your code doesn''t has anything
that would make that happen.


Actually it does - P2 is a hardware register.
So that''s some deep, non-standard magic - I was considering if the
Px would be macros, but then they wouldn''t appear on the left and
right hand side of an assignment (and being used like P0.1)...

#define READ P0.1 /*READ=SET & WRITE=RESET*/


What''s ''P0.1'' supposed to be? You try to use that as the argument of
the setbit() and clrbit() functions - but it''s neither a string nor
some value, so your compiler should get rather upset about that.


Since you asked - setbit() and clrbit() are macros, not functions, which
generate a single inline assembly SETB or CLR instruction - P0.1 is
the operand to that instruction (meaning port 0 - bit 1)



Thanks for the clarification. Do you know a more appropriate news-
group the OP could ask in?
Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de


这篇关于Micro-C - 帮助监控8051上的开关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆