keil uvsion4文件目录:
12M晶振 89c52rc单片机
reg52.h
/*-------------------------------------------------------------------------- REG52.H Header file for generic 80C52 and 80C32 microcontroller. Copyright (c) 1988-2002 Keil Elektronik GmbH and Keil Software, Inc. All rights reserved. --------------------------------------------------------------------------*/ #ifndef __REG52_H__ #define __REG52_H__ /* BYTE Registers */ sfr P0 = 0x80; sfr P1 = 0x90; sfr P2 = 0xA0; sfr P3 = 0xB0; sfr PSW = 0xD0; sfr ACC = 0xE0; sfr B = 0xF0; sfr SP = 0x81; sfr DPL = 0x82; sfr DPH = 0x83; sfr PCON = 0x87; sfr TCON = 0x88; sfr TMOD = 0x89; sfr TL0 = 0x8A; sfr TL1 = 0x8B; sfr TH0 = 0x8C; sfr TH1 = 0x8D; sfr IE = 0xA8; sfr IP = 0xB8; sfr SCON = 0x98; sfr SBUF = 0x99; /* 8052 Extensions */ sfr T2CON = 0xC8; sfr RCAP2L = 0xCA; sfr RCAP2H = 0xCB; sfr TL2 = 0xCC; sfr TH2 = 0xCD; /* BIT Registers */ /* PSW */ sbit CY = PSW^7; sbit AC = PSW^6; sbit F0 = PSW^5; sbit RS1 = PSW^4; sbit RS0 = PSW^3; sbit OV = PSW^2; sbit P = PSW^0; //8052 only /* TCON */ sbit TF1 = TCON^7; sbit TR1 = TCON^6; sbit TF0 = TCON^5; sbit TR0 = TCON^4; sbit IE1 = TCON^3; sbit IT1 = TCON^2; sbit IE0 = TCON^1; sbit IT0 = TCON^0; /* IE */ sbit EA = IE^7; sbit ET2 = IE^5; //8052 only sbit ES = IE^4; sbit ET1 = IE^3; sbit EX1 = IE^2; sbit ET0 = IE^1; sbit EX0 = IE^0; /* IP */ sbit PT2 = IP^5; sbit PS = IP^4; sbit PT1 = IP^3; sbit PX1 = IP^2; sbit PT0 = IP^1; sbit PX0 = IP^0; /* P3 */ sbit RD = P3^7; sbit WR = P3^6; sbit T1 = P3^5; sbit T0 = P3^4; sbit INT1 = P3^3; sbit INT0 = P3^2; sbit TXD = P3^1; sbit RXD = P3^0; /* SCON */ sbit SM0 = SCON^7; sbit SM1 = SCON^6; sbit SM2 = SCON^5; sbit REN = SCON^4; sbit TB8 = SCON^3; sbit RB8 = SCON^2; sbit TI = SCON^1; sbit RI = SCON^0; /* P1 */ sbit T2EX = P1^1; // 8052 only sbit T2 = P1^0; // 8052 only /* T2CON */ sbit TF2 = T2CON^7; sbit EXF2 = T2CON^6; sbit RCLK = T2CON^5; sbit TCLK = T2CON^4; sbit EXEN2 = T2CON^3; sbit TR2 = T2CON^2; sbit C_T2 = T2CON^1; sbit CP_RL2 = T2CON^0; #endif
intrins.h
/*-------------------------------------------------------------------------- INTRINS.H Intrinsic functions for C51. Copyright (c) 1988-2004 Keil Elektronik GmbH and Keil Software, Inc. All rights reserved. --------------------------------------------------------------------------*/ #ifndef __INTRINS_H__ #define __INTRINS_H__ extern void _nop_ (void); extern bit _testbit_ (bit); extern unsigned char _cror_ (unsigned char, unsigned char); extern unsigned int _iror_ (unsigned int, unsigned char); extern unsigned long _lror_ (unsigned long, unsigned char); extern unsigned char _crol_ (unsigned char, unsigned char); extern unsigned int _irol_ (unsigned int, unsigned char); extern unsigned long _lrol_ (unsigned long, unsigned char); extern unsigned char _chkfloat_(float); extern void _push_ (unsigned char _sfr); extern void _pop_ (unsigned char _sfr); #endif
delay.h
#ifndef __DELAY_H__ #define __DELAY_H__ #include "mystyle.h" //微秒级延时 void DelayUs2x(UINT8 t); //毫秒级延时 void DelayMs(UINT8 t); #endif
mystyle.h
#ifndef __MYSTYLE_H__ #define __MYSTYLE_H__ typedef unsigned char UINT8; typedef unsigned int UINT16; #endif
stdio.h
/*-------------------------------------------------------------------------- STDIO.H Prototypes for standard I/O functions. Copyright (c) 1988-2002 Keil Elektronik GmbH and Keil Software, Inc. All rights reserved. --------------------------------------------------------------------------*/ #ifndef __STDIO_H__ #define __STDIO_H__ #ifndef EOF #define EOF -1 #endif #ifndef NULL #define NULL ((void *) 0) #endif #ifndef _SIZE_T #define _SIZE_T typedef unsigned int size_t; #endif #pragma SAVE #pragma REGPARMS extern char _getkey (void); extern char getchar (void); extern char ungetchar (char); extern char putchar (char); extern int printf (const char *, ...); extern int sprintf (char *, const char *, ...); extern int vprintf (const char *, char *); extern int vsprintf (char *, const char *, char *); extern char *gets (char *, int n); extern int scanf (const char *, ...); extern int sscanf (char *, const char *, ...); extern int puts (const char *); #pragma RESTORE #endif
lcd1602.h
#ifndef __LCD1602_H__ #define __LCD1602_H__ #include #include#include "mystyle.h" #include "delay.h" //LCD1602初始化 void LcdInit(void); //LCD1602判忙函数 //bit LcdCheckBusy(void); //LCD1602写命令 void LcdWriteCmd(UINT8 cmd); //LCD1602写数据 void LcdWriteByte(UINT8 Byte); //LCD1602清屏 void LcdClearScreen(void); //LCD1602写字符串 void LcdWriteString(UINT8 x,UINT8 y,UINT8 *s); #endif
delay.c
#include "delay.h" /* 函数功能:进行微秒级延时 参数:UINT8 t 延时约2*t+5 微秒 返回值:无 */ void DelayUs2x(UINT8 t) { while(--t); } /* 函数功能:进行毫秒级延时 参数:UINT8 t 延时约 t 毫秒 返回值:无 */ void DelayMs(UINT8 t) { while(t--) { DelayUs2x(245); DelayUs2x(245); } }
lcd1602.c
#include "lcd1602.h" //#define CHECK_BUSY #define RS_CLR LcdRS=0 #define RS_SET LcdRS=1 #define RW_CLR LcdRW=0 #define RW_SET LcdRW=1 #define EN_CLR LcdEN=0 #define EN_SET LcdEN=1 #define LCDPORT P0 sbit LcdRS = P2^4; sbit LcdRW = P2^5; sbit LcdEN = P2^6; /* 函数功能:LCD1602判忙 参数:无 返回值:1 空闲 ; 0 忙 */ /* bit LcdCheckBusy(void) { #ifdef CHECK_BUSY LCDPORT = 0XFF; RS_CLR; RW_SET; EN_CLR; _nop_(); EN_SET; return (bit) (LCDPORT&0x80); #else return 0; #endif } */ /* */ void LcdInit() { //设置显示模式 LcdWriteCmd(0x38); DelayMs(5); LcdWriteCmd(0x38); DelayMs(5); LcdWriteCmd(0x38); DelayMs(5); LcdWriteCmd(0x38); //显示关闭 LcdWriteCmd(0x80); //显示清屏 LcdWriteCmd(0x01); //设置 显示光标移动 LcdWriteCmd(0x06); DelayMs(5); //设置 显示开及光标 LcdWriteCmd(0x0c); } /* 函数功能:LCD1602写入命令 参数:UINT8 cmd 返回值:无 */ void LcdWriteCmd(UINT8 cmd) { //while(LcdCheckBusy()); DelayMs(5); RS_CLR; RW_CLR; EN_SET; LCDPORT=cmd; _nop_(); EN_CLR; } /* 函数功能:LCD1602写入一个字节数据 参数:UINT8 Byte 返回值:无 */ void LcdWriteByte(UINT8 Byte) { //while(LcdCheckBusy()); DelayMs(5); RS_SET; RW_CLR; EN_SET; LCDPORT=Byte; _nop_(); EN_CLR; } /* 函数功能:LCD1602清屏 参数:无 返回值:无 */ void LcdClearScreen(void) { LcdWriteCmd(0x01); DelayMs(5); } /* 函数功能:LCD1602写入字符串 参数: UINT8 x 写入第一个字节的X轴坐标 UINT8 y 写入第一个字节的Y轴坐标 UINT8 *s 写入字符串的首地址 返回值:无 */ void LcdWriteString(UINT8 x,UINT8 y,UINT8 *s) { // y=0 表示第一行 if(0==y) { LcdWriteCmd(0x80+x); } // y=1 表示第二行 else { LcdWriteCmd(0xc0+x); } while(*s) { LcdWriteByte(*s); s++; } }
ADC0809.c
#include"reg52.h" #include "intrins.h" #include #include #include#include sbit ST =P2^0; //转换启动信号 0:关闭转换 1:启动转换 sbit EOC=P2^1; //转换结束信号 0:转换还没有结束 1:转换已结束 sbit OE =P2^2; //输出允许信号 0:数据还不允许输出 1:数据已运行输出 sbit CLK=P2^3; //用于输出AD时钟信号 UINT16 all=0; //avr 用于保存最新一次的AD结果 float b=0.0; //AD和定时器初始化 void ADinit(); void Timerinit(); //AD转换 UINT8 readAD(); void main() { //硬延时 300ms //_nop_() 12个时钟周期(一个机器周期) 12M的晶振:1us UINT16 k=60000; char displaytemp[16];//定义显示区域临时存储数组 for(;k>0;k--) { _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); } //LCD1602初始化 LcdInit(); DelayMs(20); //延时有助于稳定 //AD初始化 ADinit(); //value[0]=(UINT16) 100*readAD(); //value[1]=(UINT16) 100*readAD(); //定时器初始化 Timerinit(); //清屏 LcdClearScreen(); LcdWriteString(1,0,"ruan jia you"); //LcdWriteString(1,1,"qiao li ting !"); while(1) { all=readAD(); b = all/51.0; DelayMs(200); DelayMs(200); sprintf(displaytemp,"Volt:%7.4f ",b); LcdWriteString(0,1,displaytemp);//显示第二行 DelayMs(200); DelayMs(200); } } /****************定时器初始化**********************/ void Timerinit() { ST=0; //关闭AD转换 OE=0; //关闭数据输出 } /*********定时器及AD初始化***********/ void ADinit() { EA = 1; //开总中断 TMOD = 0x22;//设定定时器T0、T1工作方式为16位自动重装 TH0=216; //利用T0中断产生CLK信号 TL0=216; ET0=1; //定时器0中断允许 TR0=1; //启动定时器T0 } /***********T0中断服务程序 用于产生时钟信号************/ void timer0(void) interrupt 1 { CLK=~CLK; } /***********AD转换函数**********/ UINT8 readAD() { UINT8 value=0; ST=0; DelayMs(1); ST=1; //启动AD转换 DelayMs(1); ST=0; while(0==EOC);//等待数据输出允许信号 OE=1; //设置允许数据输出 value=P1;//保存输出数据 //数据处理结束 OE=0; //关闭数据输出 return value; }