51单片机结构简单,架构体系完备,学习资料丰富,应用领域广泛。通过对51单片机的学习,可帮助小白实现快速入门电子设计,理解威廉希尔官方网站 系统工作原理,为高级嵌入式芯片的学习及开发打下坚实基础。
本篇描述
1、51+Proteus8.10;
2、学习ADC0808/0809模拟量检测、数据转换、1602显示驱动;
仿真设计
采用Proteus作为仿真设计工具。Proteus是一款著名的EDA工具(仿真软件),从原理图布图、代码调试到单片机与外围威廉希尔官方网站 协同仿真,一键切换到PCB设计,真正实现了从概念到产品的完整设计。
软件设计
采用Keil作为软件设计工具。Keil提供了包括C编译器、宏汇编、链接器、库管理和一个功能强大的仿真调试器等在内的完整开发方案,通过一个集成开发环境(μVision)将这些部分组合在一起。
#include#include "SMC1602.h" #include "DATransform.h" #define TIME0H 0x3C #define TIME0L 0xB0 unsigned char uc_Clock=0; //¶¨Ê±Æ÷0ÖжϼÆÊý bit b_DATransform=0; void vShowVoltage(unsigned int uiNumber) { unsigned char ucaNumber[3],ucCount; if(uiNumber>999) uiNumber=999; ucaNumber[0]=uiNumber/100; //°Ñ¼ÆËãÊý×ÖµÄÿ¸öλ´æÈëÊý×é¡£ ucaNumber[1]=(uiNumber-100*(int)ucaNumber[0])/10; ucaNumber[2]=uiNumber-100*(int)ucaNumber[0]-10*ucaNumber[1]; for(ucCount=0;ucCount<3;ucCount++) { vShowOneChar(ucaNumber[ucCount]+48); //´ÓÊ×λµ½ÄλÖðÒ»Êä³ö¡£ if(ucCount==0) vShowOneChar('.'); } } void main() { TMOD=0x01; //¶¨Ê±Æ÷0£¬Ä£Ê½1¡£ TH0=TIME0H; TL0=TIME0L; TR0=1; //Æô¶¯¶¨Ê±Æ÷¡£ ET0=1; //¿ª¶¨Ê±Æ÷Öжϡ£ EA=1; //¿ª×ÜÖÐ¶Ï vdInitialize(); vWriteCMD(0x84); //дÈëÏÔʾÆðʼµØÖ·£¨µÚ¶þÐеÚÒ»¸öλÖã vShowChar("Voltage:"); vWriteCMD(0xC9); vShowChar("(V)"); while(1) { if(b_DATransform==1) { b_DATransform=0; vWriteCMD(0xC4); vShowVoltage(uiADTransform()); } } } void Time0() interrupt 1 { if(uc_Clock==0) { uc_Clock=5; b_DATransform=1; } else uc_Clock--; TH0=TIME0H; //»Ö¸´¶¨Ê±Æ÷0¡£ TL0=TIME0L; }
SMC1602.c
#include "SMC1602.h" void vWriteCMD(unsigned char ucCommand) { vDelay(); //ÏÈÑÓʱ¡£ LCDE=1; //È»ºó°Ñ£Ì£Ã£Ä¸ÄΪдÈëÃüÁî״̬¡£ LCDRS=0; LCDRW=0; LCDPORT=ucCommand; //ÔÙÊä³öÃüÁî¡£ LCDE=0; //×îºóÖ´ÐÐÃüÁî¡£ } void vWriteData(unsigned char ucData) { vDelay(); //ÏÈÑÓʱ¡£ LCDE=1; //È»ºó°Ñ£Ì£Ã£Ä¸ÄΪдÈëÊý¾Ý״̬¡£ LCDRS=1; LCDRW=0; LCDPORT=ucData; //ÔÙÊä³öÊý¾Ý¡£ LCDE=0; //×îºóÏÔʾÊý¾Ý¡£ } void vShowOneChar(unsigned char ucChar) { switch(ucChar) { case ' ': vWriteData(0x20);break; case '!': vWriteData(0x21);break; case '"': vWriteData(0x22);break; case '#': vWriteData(0x23);break; case '$': vWriteData(0x24);break; case '%': vWriteData(0x25);break; case '&': vWriteData(0x26);break; case '>': vWriteData(0x27);break; case '(': vWriteData(0x28);break; case ')': vWriteData(0x29);break; case '*': vWriteData(0x20);break; case '+': vWriteData(0x2A);break; case '-': vWriteData(0x2D);break; case '/': vWriteData(0x2F);break; case '=': vWriteData(0x3D);break; case '<': vWriteData(0x3E);break; case '?': vWriteData(0x3F);break; case '.': vWriteData(0x2E);break; case ':': vWriteData(0x3A);break; case '0': vWriteData(0x30);break; case '1': vWriteData(0x31);break; case '2': vWriteData(0x32);break; case '3': vWriteData(0x33);break; case '4': vWriteData(0x34);break; case '5': vWriteData(0x35);break; case '6': vWriteData(0x36);break; case '7': vWriteData(0x37);break; case '8': vWriteData(0x38);break; case '9': vWriteData(0x39);break; case 'A': vWriteData(0x41);break; case 'B': vWriteData(0x42);break; case 'C': vWriteData(0x43);break; case 'D': vWriteData(0x44);break; case 'E': vWriteData(0x45);break; case 'F': vWriteData(0x46);break; case 'G': vWriteData(0x47);break; case 'H': vWriteData(0x48);break; case 'I': vWriteData(0x49);break; case 'J': vWriteData(0x4A);break; case 'K': vWriteData(0x4B);break; case 'L': vWriteData(0x4C);break; case 'M': vWriteData(0x4D);break; case 'N': vWriteData(0x4E);break; case 'O': vWriteData(0x4F);break; case 'P': vWriteData(0x50);break; case 'Q': vWriteData(0x51);break; case 'R': vWriteData(0x52);break; case 'S': vWriteData(0x53);break; case 'T': vWriteData(0x54);break; case 'U': vWriteData(0x55);break; case 'V': vWriteData(0x56);break; case 'W': vWriteData(0x57);break; case 'X': vWriteData(0x58);break; case 'Y': vWriteData(0x59);break; case 'Z': vWriteData(0x5A);break; case 'a': vWriteData(0x61);break; case 'b': vWriteData(0x62);break; case 'c': vWriteData(0x63);break; case 'd': vWriteData(0x64);break; case 'e': vWriteData(0x65);break; case 'f': vWriteData(0x66);break; case 'g': vWriteData(0x67);break; case 'h': vWriteData(0x68);break; case 'i': vWriteData(0x69);break; case 'j': vWriteData(0x6A);break; case 'k': vWriteData(0x6B);break; case 'l': vWriteData(0x6C);break; case 'm': vWriteData(0x6D);break; case 'n': vWriteData(0x6E);break; case 'o': vWriteData(0x6F);break; case 'p': vWriteData(0x70);break; case 'q': vWriteData(0x71);break; case 'r': vWriteData(0x72);break; case 's': vWriteData(0x73);break; case 't': vWriteData(0x74);break; case 'u': vWriteData(0x75);break; case 'v': vWriteData(0x76);break; case 'w': vWriteData(0x77);break; case 'x': vWriteData(0x78);break; case 'y': vWriteData(0x79);break; case 'z': vWriteData(0x7A);break; default: break; } } void vShowChar(unsigned char ucaChar[]) { unsigned char ucCount; for(ucCount=0;;ucCount++) { vShowOneChar(ucaChar[ucCount]); if(ucaChar[ucCount+1]=='�') break; } } void vDelay() { unsigned int uiCount; for(uiCount=0;uiCount<250;uiCount++); }
SMC1602.h
#ifndef __SMC1602_H__ #define __SMC1602_H__ #include#define vdInitialize() vWriteCMD(0x01);vWriteCMD(0x38);vWriteCMD(0x0C); //³õʼ»¯ #define LCDRS P3_5 #define LCDRW P3_6 #define LCDE P3_7 #define LCDPORT P0 void vDelay(); //ÑÓʱº¯Êý¡£ void vWriteCMD(unsigned char ucCommand); //°Ñ1¸öÃüÁîдÈëLCD¡£ void vWriteData(unsigned char ucData); //°Ñ1¸öÊý¾ÝдÈëLCD¡£ void vShowOneChar(unsigned char ucChar); void vShowChar(unsigned char ucaChar[]); #endif
DATransform.c
#include#define START P3_4 //ATART£¬ALE½Ó¿Ú¡£0->1->0:Æô¶¯ADת»»¡£ #define EOC P3_3 //ת»»Íê±ÏÓÉ0±ä1. #define OUTPORT P2 unsigned int uiADTransform() { unsigned int uiResult; START=1; //Æô¶¯ADת»»¡£ START=0; while(EOC==0); //µÈ´ýת»»½áÊø¡£ uiResult=OUTPORT; //³öÈëת»»½á¹û¡£ uiResult=(100*uiResult)/51; //´¦ÀíÔËËã½á¹û¡£ return uiResult; }
DATransform.h
#ifndef __DATRANSFORM_H__ #define __DATRANSFORM_H__ unsigned int uiADTransform(); #endif
全部0条评论
快来发表一下你的评论吧 !