/******************************************************************
* 实验名称:Calculation 显示仿真 (MSP430F249)
*
* 实验说明:
* 通设置系统主时钟(MCLK=8Mhz ) 、辅助时钟(ACLK=8Mhz )、
* 子系统时钟(SMCLK=8Mhz )
* 编译软件:Embedded Workbench 6.4
*
*仿真软件:proteus7.8 sp 2
******************************************************************/
本实验采用的单片机是MSP430F249,用4x5的矩阵式非编码键盘作为计算器的输入按键,显示模块采用LM3229,该计算器的功能包括“+、-、*、/ 、^、 清除、返回上一次计算”等功能。
void main(void)
{
/*str为表达式数组*/
char str[MAX]="7+(8*9)+1";
char result[10];
unsigned char inputc='E', pos=0;
char next = 0;
init();
keyInit();
LCD_Init();
writeString(1,0,"hello!");
while(1)
{
//scan key value
inputc = scanKey();
//whether it is vaild
if(inputc != '_')
{
if(next)
{
clearCaculation();
next = 0;
}
if(inputc == '=')
{
str[pos]='\0';
caculation(str, result);
{
writeChar(6,0,'=');
writeString(6,1,result);
memset(result,0,10);
next = 1;
}
pos = 0;
continue;
}
if(inputc == 'c')
{
//clear caculation result.
clearCaculation();
pos=0;
continue;
}
if(inputc == 'b')
{
//back one step
pos--;
writeChar(5,pos,' ');
continue;
}
str[pos] = inputc;
writeChar(5,pos,inputc);
pos++;
}
}
}
实验结果如下所示:
需要下载代码的朋友可以关注微信公众号:时沿科技,或者下方二维码免费下载