51单片机-红外计算器
1、硬件:普中-A2开发板 使用哪些硬件资源?
1.1、LCD1602液晶显示器 1.2、红外接收头 1.3、红外遥控器
2、软件介绍:
2.1、共分为8个.c文件。 2.1.1、main.c 主要负责初始化,调用函数进行计算、显示 2.1.2、calculator.c 主要负责计算和字符输入 2.1.3、delay.c 延时函数 2.1.4、lcd1602.c 液晶屏驱动函数 2.1.5、time.c 定时器初始化、中断服务函数 2.1.6、uart.c 串口相关函数 2.1.7、Infrared.c 红外接收函数 2.1.8、EXIT.c 外部中断0初始化及中断服务函数
2.2、Infrared.c和EXIT一起,用来读取红外遥控按键键值,红外接收头接在P3 ^ 2引脚上,因此一旦外部中断0有下降沿,就进入红外接收函数,读取键值。
上代码 1、main.c
#include <REGX52.H>
#include <intrins.h>
#include <stdio.h>
#include <string.h>
#include "time.h"
#include "lcd1602.h"
#include "delay.h"
#include "typedef.h"
#include "key.h"
#include <stdlib.h>
#include "uart.h"
#include "calculator.h"
#include "infrared.h"
uint16_t time0Cnt = 0;
int main(void)
{
LcdInit();
Timer0Init();
Timer1Init();
UART_Init(9600);
infrared_Init();
EA = 1;
CalculatorInit(&calculator_s);
setDisplayAddr(0, 0);
printf("calculator");
while(1)
{
if(infraredValue != 0)
{
switch(getNumber(&calculator_s))
{
case 0:
setDisplayAddr(0, 1);
printf("%s", calculator_s.inputCharBuf);
break;
case 1:
LcdWriteCom(0x01);
setDisplayAddr(0, 0);
printf("%s", calculator_s.inputCharBuf);
break;
}
infraredValue = 0;
}
}
}
2、calculator.h
#ifndef __CALCULATOR_H_
#define __CALCULATOR_H_
#include <REGX52.H>
typedef struct
{
unsigned char inputCharBuf[16];
unsigned int inputCharCnt;
unsigned int offset;
unsigned char errorFlag;
float result;
}Calculator_S;
extern Calculator_S calculator_s;
unsigned char keyDeal(unsigned char keyValue);
void CalculatorInit(Calculator_S *s);
unsigned char getNumber(Calculator_S *s);
float calculator(Calculator_S *s);
float calculator_core(void)reentrant;
float my_atof(const char* pbuf);
void my_cal_atof(float* result, const char *pstr, unsigned int* index);
#endif
3、calculator.c
#include "calculator.h"
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdio.h>
#include <math.h>
#include "infrared.h"
#define PRINTF 0
Calculator_S calculator_s;
void CalculatorInit(Calculator_S *s)
{
memset(s->inputCharBuf, 0x00, sizeof(s->inputCharBuf));
s->inputCharCnt = 0;
s->result = 0;
s->offset = 0;
s->errorFlag = 0;
}
unsigned char getNumber(Calculator_S *s)
{
unsigned char keyValue = 0xFF;
if(infraredValue != 0)
{
switch(infraredValue)
{
case 0x09:
s->inputCharBuf[s->inputCharCnt++] = ')';
break;
case 0x07:
s->inputCharBuf[s->inputCharCnt++] = '(';
break;
case 0x43:
if(s->inputCharCnt > 0)
{
s->inputCharCnt--;
s->inputCharBuf[s->inputCharCnt] = 0x00;
}
break;
case 0x0d:
s->inputCharCnt = 0;
memset(s->inputCharBuf, 0x00, sizeof(s->inputCharBuf));
break;
case 0x0c:
keyValue = '1';
break;
case 0x18:
keyValue = '2';
break;
case 0x5e:
keyValue = '3';
break;
case 0x40:
keyValue = '+';
break;
case 0x08:
keyValue = '4';
break;
case 0x1c:
keyValue = '5';
break;
case 0x5a:
keyValue = '6';
break;
case 0x19:
keyValue = '-';
break;
case 0x42:
keyValue = '7';
break;
case 0x52:
keyValue = '8';
break;
case 0x4a:
keyValue = '9';
break;
case 0x45:
keyValue = '*';
break;
case 0x44:
keyValue = '.';
break;
case 0x16:
keyValue = '0';
break;
case 0x15:
keyValue = '=';
break;
case 0x47:
keyValue = '/';
break;
default:
keyValue = 0xFF;
break;
}
if(keyValue != 0xFF)
{
if(keyValue == '=' || s->inputCharCnt >= 15)
{
s->result = calculator(s);
s->inputCharCnt = sprintf(s->inputCharBuf, "%.2f", calculator_s.result);
return 0;
}
else
{
s->inputCharBuf[s->inputCharCnt++] = keyValue;
return 1;
}
}
return 1;
}
return 2;
}
float calculator(Calculator_S *s)
{
s->offset = 0;
s->errorFlag = 0;
s->result = calculator_core();
memset(s->inputCharBuf, 0x00, sizeof(s->inputCharBuf));
s->inputCharCnt = 0;
return s->result;
}
float calculator_core(void)reentrant
{
char sign = 0;
float number[2] = {0, 0};
char numberCnt = 0;
sign = '+';
while(calculator_s.inputCharCnt > calculator_s.offset)
{
if(calculator_s.inputCharBuf[calculator_s.offset] == '(')
{
#if PRINTF == 1
printf("递归入口\r\n");
#endif
calculator_s.offset++;
calculator_s.result = calculator_core();
}
if(calculator_s.offset == 0)
{
my_cal_atof(&calculator_s.result, (const char*)calculator_s.inputCharBuf, &calculator_s.offset);
}
else if(isdigit(calculator_s.inputCharBuf[calculator_s.offset])
&& (!isdigit(calculator_s.inputCharBuf[calculator_s.offset - 1]) && calculator_s.inputCharBuf[calculator_s.offset - 1] != '.'))
{
my_cal_atof(&calculator_s.result, (const char*)calculator_s.inputCharBuf, &calculator_s.offset);
}
if(((calculator_s.inputCharBuf[calculator_s.offset] != ' ') && (!isdigit(calculator_s.inputCharBuf[calculator_s.offset])))
|| (calculator_s.offset == calculator_s.inputCharCnt - 1))
{
switch(sign)
{
case '+':
if(numberCnt == 2)
{
number[0] += number[1];
number[1] = calculator_s.result;
}
else
{
number[numberCnt++] = calculator_s.result;
}
break;
case '-':
if(numberCnt == 2)
{
number[0] += number[1];
number[1] = -calculator_s.result;
}
else
{
number[numberCnt++] = -calculator_s.result;
}
break;
case '*':
number[numberCnt - 1] *= calculator_s.result;
break;
case '/':
if(calculator_s.result != 0)
{
number[numberCnt - 1] /= calculator_s.result;
}
else
{
calculator_s.errorFlag = 1;
}
break;
case '%':
number[numberCnt - 1] = number[numberCnt - 1] - (int)(number[numberCnt - 1] / calculator_s.result) * calculator_s.result;
break;
default:
break;
}
#if PRINTF == 1
printf("\r\nsign:%c,%.2f, %.2f\r\n", sign, number[0], number[1]);
#endif
sign = calculator_s.inputCharBuf[calculator_s.offset];
}
if(calculator_s.inputCharBuf[calculator_s.offset] == ')')
{
#if PRINTF == 1
printf("递归出口\r\n");
#endif
calculator_s.offset++;
break;
}
calculator_s.offset++;
}
#if PRINTF == 1
printf("result: %.2f\r\n", number[0] + number[1]);
#endif
return (number[0] + number[1]);
}
void my_cal_atof(float* result, const char *pstr, unsigned int* index)
{
char sign = 1;
float point = 0.1;
*result = 0;
while (pstr[*index] < '0' || pstr[*index] > '9')
{
if (pstr[*index] == '-')
{
sign = 2;
}
else if(pstr[*index] == '+')
{
sign = 1;
}
(*index)++;
}
while (pstr[*index])
{
if (pstr[*index] == '.')
{
(*index)++;
while (pstr[*index] >= '0' && pstr[*index] <= '9')
{
*result += point * (pstr[*index] - '0');
point *= 0.1f;
(*index)++;
}
break;
}
else if (pstr[*index] >= '0' && pstr[*index] <= '9')
{
*result *= 10;
*result += pstr[*index] -'0';
}
else
{
break;
}
(*index)++;
}
(*index)--;
if(sign == 2)
{
*result = -*result;
}
return;
}
4、delay.h
#ifndef __DELAY_H_
#define __DELAY_H_
#include "typedef.h"
#define MAIN_FOSC_DELAY 11059200UL
void delay_ms(uint16_t ms);
void delay5us(void);
void delay_us(uint16_t us);
void delay_30us(void);
void delay_40us(void);
#endif
5、delay.c
#include "delay.h"
#include <intrins.h>
void delay5us(void)
{
#if MAIN_FOSC_DELAY == 11059200UL
_nop_();
#elif MAIN_FOSC_DELAY == 12000000UL
_nop_();
#elif MAIN_FOSC_DELAY == 22118400UL
_nop_();
_nop_();
_nop_();
#endif
}
void delay_us(uint16_t us)
{
uint16_t i = us;
i = (i > 4) ? (i - 4) : i;
while(i--)
{
#if MAIN_FOSC_DELAY == 11059200UL
_nop_();
#elif MAIN_FOSC_DELAY == 12000000UL
_nop_();
#elif MAIN_FOSC_DELAY == 22118400UL
_nop_();
_nop_();
_nop_();
#endif
}
}
void delay_ms(uint16_t ms)
{
uint16_t i, j;
#if MAIN_FOSC_DELAY == 11059200UL
for(i=ms; i>0; i--)
{
for(j=114; j>0; j--)
{
;
}
}
#elif MAIN_FOSC_DELAY == 12000000UL
for(i=0; i<ms; i++)
{
for(j=0; j<123; j++)
{
;
}
}
#endif
}
void delay_30us(void)
{
#if MAIN_FOSC_DELAY == 11059200
unsigned char i;
i = 11;
while(--i);
#elif MAIN_FOSC_DELAY == 12000000
unsigned char i;
_nop_();
i = 12;
while(--i);
#endif
}
void delay_40us(void)
{
#if MAIN_FOSC_DELAY == 11059200
unsigned char i;
_nop_();
i = 15;
while(--i);
#elif MAIN_FOSC_DELAY == 12000000
unsigned char i;
_nop_();
i = 17;
while(--i);
#endif
}
6、typedef.h
#ifndef __TYPEDEF_H_
#define __TYPEDEF_H_
#define __WEAK __attribute__((weak))
typedef unsigned long uint32_t;
typedef unsigned int uint16_t;
typedef unsigned char uint8_t;
#endif
7、lcd1602.h
#ifndef __LCD1602_H_
#define __LCD1602_H_
#define QUICK 1
#include <REGX52.H>
#include "typedef.h"
#define LCD1602_DATAPINS P0
sbit LCD1602_RS=P2^6;
sbit LCD1602_RW=P2^5;
sbit LCD1602_E=P2^7;
extern unsigned char code userCodeBuf[];
void Lcd1602_Delay1ms(uint16_t c);
void LcdWriteCom(uint8_t com);
void LcdWriteData(uint8_t dat) ;
void LcdInit();
void displayString(uint8_t* buf, uint16_t N, uint8_t Lin);
void setDisplayAddr(unsigned char x, unsigned char y);
#endif
8、lcd1602.c
#include "lcd1602.h"
#include "delay.h"
#include <stdio.h>
#ifdef UART
#include "uart.h"
#endif
unsigned char code table[]= {
0x00,0x00,0x00,0x1F,0x00,0x00,0x00,0x00,
0x00,0x00,0x0E,0x00,0x1F,0x00,0x00,0x00,
0x00,0x1F,0x00,0x0E,0x00,0x1F,0x00,0x00,
0x08,0x0f,0x12,0x0f,0x0a,0x1f,0x02,0x02,
0x0f,0x09,0x0f,0x09,0x0f,0x09,0x0b,0x11,
0x0F,0x09,0x09,0x0F,0x09,0x09,0x0F,0x00,
0x00,0x00,0x04,0x00,0x00,0x04,0x00,0x00,
0x18,0x18,0x07,0x08,0x08,0x08,0x07,0x00
};
code unsigned char userCodeBuf[] = {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07};
char putchar(char c)
{
if(c >= 0x20 && c <= 0x7E)
{
LcdWriteData(c);
}
#ifdef UART
sendByte(c);
#endif
return c;
}
void setDisplayAddr(unsigned char x, unsigned char y)
{
LcdWriteCom(0x80 + x + 0x40 * y);
}
void displayString(uint8_t* buf, uint16_t N, uint8_t Lin)
{
int i;
N = (N > 16) ? 16 : N;
if(Lin == 0)
{
LcdWriteCom(0x80);
}
else
{
LcdWriteCom(0x80 + 0x40);
}
for(i = 0; i < N; i++)
{
LcdWriteData(buf[i]);
}
}
void Busy(void)
{
LCD1602_DATAPINS = 0xff;
LCD1602_RS = 0;
LCD1602_RW = 1;
while(LCD1602_DATAPINS & 0x80)
{
LCD1602_E = 0;
LCD1602_E = 1;
}
LCD1602_E = 0;
}
void LcdWriteCom(uint8_t com)
{
Busy();
LCD1602_RS = 0;
LCD1602_RW = 0;
LCD1602_DATAPINS = com;
delay_ms(1);
LCD1602_E = 1;
#ifdef QUICK
delay_ms(1);
#else
delay_ms(5);
#endif
LCD1602_E = 0;
}
void LcdWriteData(uint8_t dat)
{
Busy();
LCD1602_RS = 1;
LCD1602_RW = 0;
LCD1602_DATAPINS = dat;
delay_ms(1);
LCD1602_E = 1;
#ifdef QUICK
delay_ms(1);
#else
delay_ms(5);
#endif
LCD1602_E = 0;
}
void LcdInit()
{
unsigned char i = 0;
delay5us();
delay_us(5);
delay_30us();
delay_40us();
LcdWriteCom(0x38);
LcdWriteCom(0x0c);
LcdWriteCom(0x06);
LcdWriteCom(0x01);
LcdWriteCom(0x80);
setDisplayAddr(0, 0);
displayString("lcd1602 Init", 12, 0);
LcdWriteCom(0x40);
for(i=0; i<64; i++)
{
LcdWriteData(table[i]);
}
displayString(" ", 12, 0);
}
9、time.h
#ifndef __TIME_H_
#define __TIME_H_
#include <REGX52.H>
#include "typedef.h"
#define T1MS_0 (65536-MAIN_FOSC_DELAY/12/1000)
#define T1MS_1 (65536-MAIN_FOSC_DELAY/12/1000)
void Timer0Init(void);
void Timer1Init(void);
#endif
10、time.c
#include "time.h"
#include "delay.h"
#ifdef ADC0809
#include "ADC0809.h"
#endif
#ifdef UART
#include "uart.h"
#endif
extern uint16_t time0Cnt;
void Timer0Init(void)
{
TMOD |= 0X01;
TL0 = T1MS_0;
TH0 = T1MS_0 >> 8;
ET0 = 1;
TR0 = 1;
}
void Timer1Init(void)
{
TMOD |= 0x10;
TL1 = T1MS_1;
TH1 = T1MS_1 >> 8;
TR1 = 0;
ET1 = 1;
}
void time0_isr() interrupt 1 using 0
{
TL0 = T1MS_0;
TH0 = T1MS_0 >> 8;
#ifdef ADC0809
CLK = !CLK;
#endif
#ifdef UART
if(timeout < RECEIVE_TIMEOUT)
{
if(++timeout == RECEIVE_TIMEOUT)
{
receiveFinshFlag = 1;
}
}
#endif
time0Cnt++;
}
void time1_isr() interrupt 3 using 1
{
TL1 = T1MS_1;
TH1 = T1MS_1 >> 8;
}
11、uart.h
#ifndef __UART_H_
#define __UART_H_
#include <REGX52.H>
#define RECEIVE_TIMEOUT 5
#define RECEIVE_LENGTH 5
extern volatile unsigned char UART_Buf[RECEIVE_LENGTH];
extern volatile unsigned char receiveCnt;
extern volatile unsigned int timeout;
extern volatile bit receiveFinshFlag;
void UART_Init(unsigned int baud);
void sendString(unsigned char* s, int length);
void sendByte(unsigned char b);
#endif
12、uart.c
#include "uart.h"
#include "delay.h"
volatile unsigned char UART_Buf[RECEIVE_LENGTH];
volatile unsigned char receiveCnt = 0;
volatile unsigned int timeout = 0xFF;
volatile bit receiveFinshFlag = 0;
void UART_isr(void) interrupt 4 using 1
{
UART_Buf[receiveCnt++] = SBUF;
timeout = 0;
receiveCnt = (receiveCnt >= RECEIVE_LENGTH) ? 0 : receiveCnt;
RI = 0;
}
void UART_Init(unsigned int baud)
{
TMOD &= 0x0F;
TMOD |= 0x20;
TH1 = -(MAIN_FOSC_DELAY/12/32/baud);
TL1 = -(MAIN_FOSC_DELAY/12/32/baud);
SCON = 0x50;
ET1 = 0;
TR1 = 1;
ES = 1;
sendString("UART is OK!\r\n", 13);
}
void sendByte(unsigned char b)
{
ES = 0;
SBUF = b;
while(!TI);
TI = 0;
ES = 1;
}
void sendString(unsigned char* s, int length)
{
int i;
for(i = 0; i < length; i++)
{
sendByte(s[i]);
}
}
13、Infrared.h
#ifndef __INFRARED_H_
#define __INFRARED_H_
#include <REGX52.H>
#include "typedef.h"
sbit infrared_in = P3 ^ 2;
extern unsigned char infraredValue;
void infrared_Init(void);
void readInfrared(unsigned char* dat);
#endif
14、Infrared.c
#include "infrared.h"
#include "delay.h"
#include "EXIT.h"
#include <intrins.h>
unsigned char infraredValue;
void infrared_Init(void)
{
EXIT0_Init();
EA = 1;
infrared_in = 1;
}
void readInfrared(unsigned char* dat)
{
unsigned char i,j;
unsigned char Time;
unsigned int errorCnt;
unsigned char readBuf[2];
Time = 0;
delay_ms(7);
if(!infrared_in)
{
errorCnt = 1000;
while((infrared_in == 0)&&( errorCnt > 0))
{
delay_us(1);
errorCnt--;
}
if(infrared_in == 1)
{
errorCnt = 500;
while((infrared_in == 1) && (errorCnt > 0))
{
delay_us(1);
errorCnt--;
}
for(i=0; i<4; i++)
{
for(j=0; j<8; j++)
{
errorCnt = 60;
while((infrared_in == 0)&&(errorCnt > 0))
{
delay_us(10);
errorCnt--;
}
errorCnt = 500;
while((infrared_in == 1) && (errorCnt > 0))
{
delay_us(10);
Time++;
errorCnt--;
if(Time > 30)
{
return;
}
}
readBuf[i%2] >>= 1;
if(Time >= 8)
{
readBuf[i%2] |= 0x80;
}
Time = 0;
}
}
}
if(readBuf[0] != ~readBuf[1])
{
*dat = 0;
}
else
{
*dat = readBuf[0];
}
}
}
15、EXIT.h
#ifndef __EXIT_H_
#define __EXIT_H_
#include "typedef.h"
void EXIT0_Init(void);
void EXIT1_Init(void);
#endif
16、EXIT.c
#include "EXIT.h"
#include "infrared.h"
#include <REGX52.H>
#ifdef EXIT_0
void EXIT0_Init(void)
{
IT0=1;
EX0 = 1;
}
void exint0() interrupt 0
{
#ifdef INFRARED
readInfrared(&infraredValue);
#endif
}
#endif
#ifdef EXIT_1
void EXIT1_Init(void)
{
IT1=1;
EX1 = 1;
}
void exint1() interrupt 2
{
}
#endif
|