STC15W4K32S4单片机ADC读取 串口收发单片机解密
单片机解密单片机源程序如下:
- /*---------------------------------------------------------------------*/
- /* --- STC MCU Limited ------------------------------------------------*/
- /* --- STC15F4K60S4 系列 定时器1用作串口1的波特率发生器举例------------*/
- /* 如果要在程序中使用此代码,请在程序中注明使用了STC的资料及程序 */
- /* 如果要在文章中应用此代码,请在文章中注明使用了STC的资料及程序 */
- /*---------------------------------------------------------------------*/
- //本示例在Keil开发环境下请选择Intel的8058芯片型号进行编译
- //若无特别说明,工作频率一般为11.0592MHz
- #include "STC15Fxxxx.H"
- //#include "reg51.h"
- #include <stdio.h>
- typedef unsigned char BYTE;
- typedef unsigned int WORD;
- //typedef unsigned int u16; //对数据类型进行声明定义
- //typedef unsigned char u8;
- #define FOSC 11059200L //系统频率
- #define BAUD 115200 //串口波特率
- #define NONE_PARITY 0 //无校验
- #define ODD_PARITY 1 //奇校验
- #define EVEN_PARITY 2 //偶校验
- #define MARK_PARITY 3 //标记校验
- #define SPACE_PARITY 4 //空白校验
- #define PARITYBIT NONE_PARITY //定义校验位
- /* STC15Fxxxx.H中已定义
- //sfr P0M1 = 0x93;
- //sfr P0M0 = 0x94;
- //sfr P1M1 = 0x91;
- //sfr P1M0 = 0x92;
- //sfr P2M1 = 0x95;
- //sfr P2M0 = 0x96;
- //sfr P3M1 = 0xb1;
- //sfr P3M0 = 0xb2;
- //sfr P4M1 = 0xb3;
- //sfr P4M0 = 0xb4;
- //sfr P5M1 = 0xC9;
- //sfr P5M0 = 0xCA;
- //sfr P6M1 = 0xCB;
- //sfr P6M0 = 0xCC;
- //sfr P7M1 = 0xE1;
- //sfr P7M0 = 0xE2;
- //sfr AUXR = 0x8e; //辅助寄存器
- //sfr P_SW1 = 0xA2; //外设功能切换寄存器1
- */
- #define S1_S0 0x40 //P_SW1.6
- #define S1_S1 0x80 //P_SW1.7
- #define ADC_POWER 0x80 //ADC电源控制位
- #define ADC_FLAG 0x10 //ADC完成标志
- #define ADC_START 0x08 //ADC起始控制位
- #define ADC_SPEEDLL 0x00 //540个时钟
- #define ADC_SPEEDL 0x20 //360个时钟
- #define ADC_SPEEDH 0x40 //180个时钟
- #define ADC_SPEEDHH 0x60 //90个时钟
- //sbit P22 = P2^2;
- unsigned int ch;a,b;
- unsigned int dd=0;
- unsigned int cc=0;
- unsigned int cc0=0;
- unsigned int cc1=0;
- unsigned int cc00=0;
- unsigned int cc01=0;
- unsigned int cc10=0;
- unsigned int cc11=0;
- unsigned int time0count0 = 0 ;
- unsigned int time0count1 = 0 ;
- unsigned int time0count = 3000 ;
- bit busy;
- char receivechar = 0;
- int receive_count = 0;
- int received = 0;
- WORD receivestr[10];
- void InitUart();
- void InitADC();
- void Timer0Init();
- void SendData(BYTE dat);
- void SendString(char *s);
- void serial_put_uint(unsigned int number);
- void Delay(WORD n);
- //void ShowResult(BYTE ch);
- unsigned int GetADCResultint(BYTE ch);
- void main()
- {
- int initreceivestrcount=0;
- unsigned int tt0=0,tt1=0;
- // unsigned int d1,d2,d3,i;
- unsigned int i=0;
- if (i == 0 ) //初始化tt0 tt1 P0 -P7 数据
- {
- /*
- 例如将P1M0=0X60和P1M1=0X00译成bai二进制代码是
- P1M0=0110 0000,duP1M1=0000 0000.
- 按照I/O口工作模式设置zhi规定
- 都为0时为准双向口(dao传统8051单片机zhuanI/O口模式)
- 为0,1时为推挽(shu强上拉输出),
- 为1,0时为高阻(仅输入),
- 为1,1时为开漏(内部上拉断开,须外加上拉电阻)
- 你选择的是P1M0和P1M1所以针对的是P1口的操作,
- 将两个二进制代码一位位的比较可知:P1.0~P1.4及P1.7都为0,
- 故这些I/O口设置的是标准状态;P1.5和P1.6为高阻态
- (P1M0上的第六位和第七位都为1,P1M1上的第六位和第七位都是0。1,0时为高阻)
- */
- tt0=16640;
- tt1=tt0/256;
- P0M0 = 0x00;
- P0M1 = 0x00;
- P1M0 = 0x00;
- P1M1 = 0x03;
- P2M0 = 0x00;
- P2M1 = 0x00;
- P3M0 = 0x00;
- P3M1 = 0x00;
- P4M0 = 0x00;
- P4M1 = 0x00;
- P5M0 = 0x00;
- P5M1 = 0x00;
- P6M0 = 0x00;
- P6M1 = 0x00;
- P7M0 = 0x00;
- P7M1 = 0x00;
- }
- InitUart(); //初始化串口
- //printf ("20200102 第一次调试 a 1 取左字节 a %d A/256=A %d C51\n" , tt0,tt1 ); /* ?? "Hello C51 " */
- SendString("20200102 第二次调试 \r\n");
- initreceivestrcount=0;
- while (initreceivestrcount<10) //初始化 receivestr
- {
- receivestr[initreceivestrcount]=0;
- initreceivestrcount = initreceivestrcount + 1 ;
- }
- //printf ("Hello"); //printf 不能使用 编译通过 不能仿真运行
- InitADC(); //初始化ADC
- Timer0Init(); //定时器0初始化
- /* 除法测试
- //printf ("Hello ADC 第y次调试 a 1 加代码a %d A/256=A %d C51\n" , tt0,tt1 );
- // sprintf(str," receive %d \r\n",receivechar);
- // SendString(str);
- // sprintf(str," d1 %d a \r\n",tt0);
- // SendString(str);
- // sprintf(str," d2 %d a \r\n",tt1);
- // SendString(str);
- // sprintf(str," d3 %d a \r\n",d3);
- // SendString(str);
- */
- SendString("c STC15W4kS32\r\n Uart Test !\r\n");
- while(1)
- {
- if (received == 1) // 在串口接收预定义字符后回送 20210103 增加控制定时显示功能
- {
- /* 除法测试及sprintf
- // SendString("\r\n");
- // d1 = receivechar;
- // d2 = d1 ;
- // d3=d2*256;
- // d3=d1-d3;
- // sprintf(str," receive %d a\r\n",receivechar);
- // SendString(str);
- // sprintf(str," d1 %d b \r\n",d1);
- // SendString(str);
- // sprintf(str," d2 %d c \r\n",d2);
- // SendString(str);
- // sprintf(str," d3 %d d \r\n",d3);
- // SendString(str);
- // SendString(receivestr);receivechar
- */
- serial_put_uint(receivechar);
- SendString(" start ");
- //serial_put_uint(11);
- for (i=0;i<receive_count;i++)
- {
- SendData(receivestr[i]);
- }
- SendString(" COUNT= ");
- serial_put_uint(receive_count);
- receive_count = 0 ;
- if (receivestr[1] == 65 )
- {
- time0count = ( receivestr[2] - 48 ) * 1000 ;
- SendString(" \r\n 串口设定 time0 ");
- serial_put_uint(time0count/1000);
- SendString(" 秒定时 ");
- }
- SendString(" \r\n");
- received = 0 ;
- }
- if (time0count0 != time0count1) // 串口设定的秒定时
- {
- SendString(" time0 ");
- serial_put_uint(time0count/1000);
- SendString(" 秒定时 执行 ");
- serial_put_uint(time0count0);
- SendString(" \r\n ");
- time0count1 = time0count0;
- }
- b=b+1;
- dd=0;
- cc00 = GetADCResultint(dd); //读取ADC 0 P1.0 数据
- if (cc01 != cc00) // ADC 0 P1.0 数据变化发送串口
- {
- SendString(" ADC ");
- // serial_put_uint(b);
- SendString(" ch ");
- serial_put_uint(dd);
- SendString(" new ");
- serial_put_uint(cc00);
- SendString(" old ");
- serial_put_uint(cc01);
- SendString(" end \r\n");
- //printf (" ADC %d ch %d :0x %x 0d %d \n", b,dd,cc00,cc01 );
- cc01 = cc00;
- }
- dd=1;
- cc10 = GetADCResultint(dd); //读取ADC 1 P1.1 数据
- if (cc11 != cc10) // ADC 1 P1.1 数据变化发送串口
- {
- SendString(" ADC ");
- // serial_put_uint(b);
- SendString(" ch ");
- serial_put_uint(dd);
- SendString(" new ");
- serial_put_uint(cc10);
- SendString(" old ");
- serial_put_uint(cc11);
- SendString(" end \r\n");
- //printf (" ADC %d ch %d :0x %x 0d00 %d \n", b,dd,cc10,cc11 );
- cc11=cc10;
- }
- dd=0;
- if (b>10000) b=0;
- }

芯片解密