最近帮同学做同学做一个51 单片机的 仿真,好久没碰单片机和硬件了,以前也没怎么用过 proteus软件,花了接近一天时间弄出来了,代码和仿真的 电路图如下:
仿真图:
要求:右下角的电位器模拟电压传感器,电压值变化,由ADC采集转换成数字量,给单片机,单片机根据电压值的大小在两个8*8的点阵上显示三种不同的表情。
代码如下:
#include #include#define uchar unsigned char#define uint unsigned int//管脚声明//154引脚***it pinA = P3^0;***it pinB = P3^1;***it pinC = P3^2;***it pinD = P3^3;//AD相关的引脚定义***it wr = P3^5;***it rd = P3^6;***it cs = P3^7;//第一块595的控制引脚***it SHCP1=P2^0;***it DS1=P2^1;***it STCP1=P2^2;//第二块595的控制引脚***it SHCP2=P2^3;***it DS2=P2^4;***it STCP2=P2^5;//函数声明void delay(int i);void HC_595_SET1(uchar temp);void HC_595_SET2(uchar temp);void HC_595_Init();void HC154_Set(uchar i);uchar read_adc0804();uchar dis1[] = {0x00, 0x18, 0x24, 0x5A, 0x24, 0x24, 0x18, 0x00,0x00, 0x18, 0x24, 0x5A, 0x24, 0x24, 0x18, 0x00};uchar dis2[] = {0x00, 0x18, 0x24, 0x42, 0x81, 0x24, 0x18, 0x00,0x00, 0x18, 0x24, 0x42, 0x81, 0x24, 0x18, 0x00};uchar dis3[] = {0x18, 0x24, 0x42, 0x3C, 0x18, 0x18, 0x18, 0x18,0x18, 0x24, 0x42, 0x3C, 0x18, 0x18, 0x18, 0x18};void display1(){ int i=0;uchar temp=0;for(i=0;i<16;i++){temp = dis1;HC_595_SET1(temp);HC_595_SET2(temp);HC154_Set(i);delay(1);}}void display2(){ int i=0;uchar temp=0;for(i=0;i<16;i++){temp = dis2;HC_595_SET1(temp);HC_595_SET2(temp);HC154_Set(i);delay(1);}}void display3(){ int i=0;uchar temp=0;for(i=0;i<16;i++){temp = dis3;HC_595_SET1(temp);HC_595_SET2(temp);HC154_Set(i);delay(1);}}void main(){HC_595_Init();while(1){uchar adcdata = read_adc0804();if(adcdata < 85)display1();else if((adcdata>=85)&&(adcdata<170))display2();elsedisplay3();}}//延时函数void delay(int i){int j;while(i--)for(j=110;j>0;j--);}//第一块595设置函数void HC_595_SET1(uchar temp){uchar j=0;for(j=0;j<8;j++){temp = temp<<1;DS1 = CY;SHCP1 = 1;//上升沿发生移位_nop_();_nop_();SHCP1 = 0;}STCP1 = 0;//delay(1);_nop_();_nop_();STCP1 = 1;}//第二块595设置函数void HC_595_SET2(uchar temp){uchar j=0;for(j=0;j<8;j++){temp = temp<<1;DS2 = CY;SHCP2 = 1;//上升沿发生移位_nop_();_nop_();SHCP2 = 0;}STCP2 = 0;//下降沿输出数据//delay(1);_nop_();_nop_();STCP2 = 1;}//两块595芯片的初始化函数void HC_595_Init(){//595芯片引脚的初始化电平SHCP1 =0;STCP1 =1;SHCP2 =0;STCP2 =1;//......}//AD读取数据函数uchar read_adc0804(){uchar udata=0;cs =1;wr =1;cs =0;wr =0;_nop_();wr =1;P1 =0xff;rd=1;rd=0;_nop_();udata = P1;rd=1;cs=1;return udata;}//设置74HC154的函数void HC154_Set(uchar i){uchar a, b, c ,d;d = i/8;c = (i-8*d)/4;b = (i-8*d-4*c)/2;if((i%2)==1)a = 1;elsea=0;pinA=a;pinB=b;pinC=c;pinD=d;}仿真结果如下:
0
|
|
|
|