基于430单片机的科学计算器C语言设计代码
时间: 2023-08-10 10:09:11 浏览: 102
好的,以下是一个基于430单片机的科学计算器C语言设计代码的示例:
```c
#include <msp430.h>
#include <stdio.h>
#include <math.h>
#define LCD_RS BIT0
#define LCD_RW BIT1
#define LCD_E BIT2
#define LCD_DB4 BIT4
#define LCD_DB5 BIT5
#define LCD_DB6 BIT6
#define LCD_DB7 BIT7
void LCD_init();
void LCD_command(char command);
void LCD_data(char data);
void LCD_string(char *string);
void LCD_clear();
void LCD_gotoxy(char x, char y);
void LCD_integer(int number);
void LCD_float(float number);
void delay_ms(unsigned int ms);
int main() {
char op;
float num1, num2, result;
WDTCTL = WDTPW + WDTHOLD;
P1DIR |= LCD_RS + LCD_RW + LCD_E + LCD_DB4 + LCD_DB5 + LCD_DB6 + LCD_DB7;
LCD_init();
LCD_string("Scientific Calc");
LCD_gotoxy(0, 1);
LCD_string("by Your Name");
delay_ms(2000);
LCD_clear();
while(1) {
LCD_string("Enter operator:");
LCD_gotoxy(0, 1);
LCD_string("+, -, *, /, sin, cos, tan, log, exp");
scanf("%c", &op);
if(op == '+' || op == '-' || op == '*' || op == '/') {
LCD_string("Enter first num:");
scanf("%f", &num1);
LCD_string("Enter second num:");
scanf("%f", &num2);
switch(op) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
result = num1 / num2;
break;
default:
LCD_string("Invalid operator");
delay_ms(2000);
LCD_clear();
break;
}
LCD_string("Result:");
LCD_float(result);
delay_ms(2000);
LCD_clear();
}
else if(op == 'sin' || op == 'cos' || op == 'tan' || op == 'log' || op == 'exp') {
LCD_string("Enter num:");
scanf("%f", &num1);
switch(op) {
case 'sin':
result = sin(num1);
break;
case 'cos':
result = cos(num1);
break;
case 'tan':
result = tan(num1);
break;
case 'log':
result = log(num1);
break;
case 'exp':
result = exp(num1);
break;
default:
LCD_string("Invalid operator");
delay_ms(2000);
LCD_clear();
break;
}
LCD_string("Result:");
LCD_float(result);
delay_ms(2000);
LCD_clear();
}
else {
LCD_string("Invalid operator");
delay_ms(2000);
LCD_clear();
}
}
return 0;
}
void LCD_init() {
P1OUT &= ~(LCD_RS + LCD_RW + LCD_E + LCD_DB4 + LCD_DB5 + LCD_DB6 + LCD_DB7);
P1OUT |= LCD_DB5;
delay_ms(15);
LCD_command(0x03);
delay_ms(5);
LCD_command(0x03);
delay_ms(1);
LCD_command(0x03);
delay_ms(1);
LCD_command(0x02);
LCD_command(0x28);
LCD_command(0x0C);
LCD_command(0x06);
LCD_command(0x01);
delay_ms(2);
}
void LCD_command(char command) {
P1OUT &= ~LCD_RS;
P1OUT &= ~LCD_RW;
P1OUT |= LCD_E;
P1OUT = (P1OUT & 0x0F) | ((command >> 4) & 0xF0);
P1OUT &= ~LCD_E;
delay_ms(1);
P1OUT |= LCD_E;
P1OUT = (P1OUT & 0x0F) | (command & 0xF0);
P1OUT &= ~LCD_E;
delay_ms(1);
}
void LCD_data(char data) {
P1OUT |= LCD_RS;
P1OUT &= ~LCD_RW;
P1OUT |= LCD_E;
P1OUT = (P1OUT & 0x0F) | ((data >> 4) & 0xF0);
P1OUT &= ~LCD_E;
delay_ms(1);
P1OUT |= LCD_E;
P1OUT = (P1OUT & 0x0F) | (data & 0xF0);
P1OUT &= ~LCD_E;
delay_ms(1);
}
void LCD_string(char *string) {
while(*string) {
LCD_data(*string++);
}
}
void LCD_clear() {
LCD_command(0x01);
delay_ms(2);
}
void LCD_gotoxy(char x, char y) {
char address;
if(y == 0) {
address = x;
}
else {
address = x + 0x40;
}
LCD_command(0x80 | address);
}
void LCD_integer(int number) {
char buffer[16];
sprintf(buffer, "%d", number);
LCD_string(buffer);
}
void LCD_float(float number) {
char buffer[16];
sprintf(buffer, "%.2f", number);
LCD_string(buffer);
}
void delay_ms(unsigned int ms) {
unsigned int i, j;
for(i = 0; i < ms; i++) {
for(j = 0; j < 1000; j++) {
__delay_cycles(1000);
}
}
}
```
这个示例代码使用了430单片机控制LCD显示屏,并支持基本的加减乘除、三角函数、对数函数、指数函数等功能。您可以根据自己的需求进行修改和扩展。需要注意的是,由于430单片机的处理能力有限,因此可能需要对代码进行优化,以提高计算器的响应速度和稳定性。
阅读全文