----------------------------------main.c----源文件--------------------
#include "reg51.h"
typedef bit BOOL;
typedef unsigned char BYTE;
typedef unsigned int WORD;
sbit RXB = P3^0; //串口模拟端口
sbit TXB = P3^1;
//#define BAUD 0xF400 // 1200bps @ 11 .0592MHz
//#define BAUD 0xFA00 // 2400bps @ 11 .0592MHz
//#define BAUD 0xFD00 // 4800bps @ 11 .0592MHz
#define BAUD 0xFE80 // 9600bps @ 11 .0592MHz
//#define BAUD 0xFF40 //19200bps @ 11 .0592MHz
//#define BAUD 0xFFA0 //38400bps @ 11 .0592MHz
//#define BAUD 0xEC00 // 1200bps @ 18.432MHz
//#define BAUD 0xF600 // 2400bps @ 18.432MHz
//#define BAUD 0xFB00 // 4800bps @ 18.432MHz
//#define BAUD 0xFD80 // 9600bps @ 18.432MHz
//#define BAUD 0xFEC0 //19200bps @ 18.432MHz
//#define BAUD 0xFF60 //38400bps @ 18.432MHz
//#define BAUD 0xE800 // 1200bps @ 22.11 84MHz
//#define BAUD 0xF400 // 2400bps @ 22.11 84MHz
//#define BAUD 0xFA00 // 4800bps @ 22.11 84MHz
//#define BAUD 0xFD00 // 9600bps @ 22.11 84MHz
//#define BAUD 0xFE80 //19200bps @ 22.11 84MHz
//#define BAUD 0xFF40 //38400bps @ 22.11 84MHz
//#define BAUD 0xFF80 //57600bps @ 22.11 84MHz
BYTE TBUF,RBUF;
BYTE TDAT,RDAT;
BYTE TCNT,RCNT;
BYTE TBIT,RBIT;
BOOL TING,RING;
BOOL TEND,REND;
BOOL CT;
BYTE t, r;
BYTE buf[16]="1234567890qwerty";
void UART_INIT()
{
TMOD = 0x00; //设置控制定时器1与控制定时器0都为定时器的16位自动重装模式
AUXR = 0x80; //AUXR只使用了第7与6位---0表示12分频为12T普通51的IO口--1表示不分频为1T
TL0 = BAUD;
TH0 = BAUD>>8; //设置定时器重装值--也就是设置波特率
TR0 = 1; //打开定时器0
ET0 = 1; //允许T0溢出中断
PT0 = 1; //最高优先级
EA = 1; //允许所有中断
TING = 0;
RING = 0;
TEND = 1;
REND = 0;
TCNT = 0;
RCNT = 0;
}
void main()
{
BYTE j=0;
WORD i=0;
UART_INIT(); //初始化
while (1)
{
TING = 1;
TBUF = buf[j++];
i=6000;
while(i--); //延时非常重要--不然会发送错误
if (j>16)
j=0;
}
}
//在定时器里是将一个数据按照二进制的8位依次一位一位发送到TXD数据线上的
//接受数据也是相同道理---根据数据线RXD的高低电平来保存数据
void tm0() interrupt 1 using 1
{
if (RING)
{
if (--RCNT == 0) //接收---先使用再减1
{
RCNT = 3;
if (--RBIT == 0) //一个数据接收完成---数据位数---这里9位--8个数据位---1个停止位
{
RBUF = RDAT; //保存这个数
RING = 0;
REND = 1;
}
else//没有完成9位数据的接收---一位一位依次保存--先低位
{
RDAT >>= 1;
if (RXB)
RDAT |= 0x80; //根据数据线的高低电平保存0或者1
}
}
}
else if (!RXB)
{
RING = 1;
RCNT = 4;
RBIT = 9;
}
if (--TCNT == 0) //发送
{
TCNT = 3;
if (TING)
{
if (TBIT == 0)//9位数据完成发送
{
TXB = 0; //释放发送线
TDAT = TBUF; //赋值
TBIT = 9; //再次给数据个数标志位置位8+1
TEND = 0;
}
else
{
TDAT >>= 1; //左移---一位一位发送
if (--TBIT == 0)
{
TXB = 1;
TING = 0;
TEND = 1;
}
else
{
TXB = CY; //CY表示程序状态字第7位
}
}
}
}
}
-----------------------reg51.h----头文件---------------
//内核特殊功能寄存器 // 复位值 描述
sfr ACC = 0xE0; //0000,0000 累加器Accumulator
sfr B = 0xF0; //0000,0000 B寄存器
sfr PSW = 0xD0; //0000,0000 程序状态字
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;
sfr SP = 0x81; //0000,0111 堆栈指针
sfr DPL = 0x82; //0000,0000 数据指针低字节
sfr DPH = 0x83; //0000,0000 数据指针高字节
//I/O 口特殊功能寄存器
sfr P0 = 0x80; //1111,1111 端口0
sbit P00 = P0^0;
sbit P01 = P0^1;
sbit P02 = P0^2;
sbit P03 = P0^3;
sbit P04 = P0^4;
sbit P05 = P0^5;
sbit P06 = P0^6;
sbit P07 = P0^7;
sfr P1 = 0x90; //1111,1111 端口1
sbit P10 = P1^0;
sbit P11 = P1^1;
sbit P12 = P1^2;
sbit P13 = P1^3;
sbit P14 = P1^4;
sbit P15 = P1^5;
sbit P16 = P1^6;
sbit P17 = P1^7;
sfr P2 = 0xA0; //1111,1111 端口2
sbit P20 = P2^0;
sbit P21 = P2^1;
sbit P22 = P2^2;
sbit P23 = P2^3;
sbit P24 = P2^4;
sbit P25 = P2^5;
sbit P26 = P2^6;
sbit P27 = P2^7;
sfr P3 = 0xB0; //1111,1111 端口3
sbit P30 = P3^0;
sbit P31 = P3^1;
sbit P32 = P3^2;
sbit P33 = P3^3;
sbit P34 = P3^4;
sbit P35 = P3^5;
sbit P36 = P3^6;
sbit P37 = P3^7;
sfr P4 = 0xC0; //1111,1111 端口4
sbit P40 = P4^0;
sbit P41 = P4^1;
sbit P42 = P4^2;
sbit P43 = P4^3;
sbit P44 = P4^4;
sbit P45 = P4^5;
sbit P46 = P4^6;
sbit P47 = P4^7;
sfr P5 = 0xC8; //xxxx,1111 端口5
sbit P50 = P5^0;
sbit P51 = P5^1;
sbit P52 = P5^2;
sbit P53 = P5^3;
sbit P54 = P5^4;
sbit P55 = P5^5;
sbit P56 = P5^6;
sbit P57 = P5^7;
sfr P6 = 0xE8; //0000,0000 端口6
sbit P60 = P6^0;
sbit P61 = P6^1;
sbit P62 = P6^2;
sbit P63 = P6^3;
sbit P64 = P6^4;
sbit P65 = P6^5;
sbit P66 = P6^6;
sbit P67 = P6^7;
sfr P7 = 0xF8; //0000,0000 端口7
sbit P70 = P7^0;
sbit P71 = P7^1;
sbit P72 = P7^2;
sbit P73 = P7^3;
sbit P74 = P7^4;
sbit P75 = P7^5;
sbit P76 = P7^6;
sbit P77 = P7^7;
sfr P0M0 = 0x94; //0000,0000 端口0模式寄存器0
sfr P0M1 = 0x93; //0000,0000 端口0模式寄存器1
sfr P1M0 = 0x92; //0000,0000 端口1模式寄存器0
sfr P1M1 = 0x91; //0000,0000 端口1模式寄存器1
sfr P2M0 = 0x96; //0000,0000 端口2模式寄存器0
sfr P2M1 = 0x95; //0000,0000 端口2模式寄存器1
sfr P3M0 = 0xB2; //0000,0000 端口3模式寄存器0
sfr P3M1 = 0xB1; //0000,0000 端口3模式寄存器1
sfr P4M0 = 0xB4; //0000,0000 端口4模式寄存器0
sfr P4M1 = 0xB3; //0000,0000 端口4模式寄存器1
sfr P5M0 = 0xCA; //0000,0000 端口5模式寄存器0
sfr P5M1 = 0xC9; //0000,0000 端口5模式寄存器1
sfr P6M0 = 0xCC; //0000,0000 端口6模式寄存器0
sfr P6M1 = 0xCB; //0000,0000 端口6模式寄存器1
sfr P7M0 = 0xE2; //0000,0000 端口7模式寄存器0
sfr P7M1 = 0xE1; //0000,0000 端口7模式寄存器1
//系统管理特殊功能寄存器
sfr PCON = 0x87; //0001,0000 电源控制寄存器
sfr AUXR = 0x8E; //0000,0000 辅助寄存器
sfr AUXR1 = 0xA2; //0000,0000 辅助寄存器1
sfr P_SW1 = 0xA2; //0000,0000 外设端口切换寄存器1
sfr CLK_DIV = 0x97; //xxxx,x000 时钟分频控制寄存器
sfr BUS_SPEED = 0xA1; //xx10,x011 总线速度控制寄存器
sfr P1ASF = 0x9D; //0000,0000 端口1模拟功能配置寄存器
sfr P_SW2 = 0xBA; //0000,0000 外设端口切换寄存器
sfr IRC_CLKO = 0xBB; //0000,0000 内部振荡器时钟输出控制寄存器
//中断特殊功能寄存器
sfr IE = 0xA8; //0000,0000 中断控制寄存器
sbit EA = IE^7;
sbit ELVD = IE^6;
sbit EADC = IE^5;
sbit ES = IE^4;
sbit ET1 = IE^3;
sbit EX1 = IE^2;
sbit ET0 = IE^1;
sbit EX0 = IE^0;
sfr IP = 0xB8; //0000,0000 中断优先级寄存器
sbit PPCA = IP^7;
sbit PLVD = IP^6;
sbit PADC = IP^5;
sbit PS = IP^4;
sbit PT1 = IP^3;
sbit PX1 = IP^2;
sbit PT0 = IP^1;
sbit PX0 = IP^0;
sfr IE2 = 0xAF; //0000,0000 中断控制寄存器2
sfr IP2 = 0xB5; //xxxx,xx00 中断优先级寄存器2
sfr INT_CLKO = 0x8F; //0000,0000 外部中断与时钟输出控制寄存器
//定时器特殊功能寄存器
sfr TCON = 0x88; //0000,0000 T0/T1控制寄存器
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;
sfr TMOD = 0x89; //0000,0000 T0/T1模式寄存器
sfr TL0 = 0x8A; //0000,0000 T0低字节
sfr TL1 = 0x8B; //0000,0000 T1低字节
sfr TH0 = 0x8C; //0000,0000 T0高字节
sfr TH1 = 0x8D; //0000,0000 T1高字节
sfr T4T3M = 0xD1; //0000,0000 T3/T4模式寄存器
sfr T3T4M = 0xD1; //0000,0000 T3/T4模式寄存器
sfr T4H = 0xD2; //0000,0000 T4高字节
sfr T4L = 0xD3; //0000,0000 T4低字节
sfr T3H = 0xD4; //0000,0000 T3高字节
sfr T3L = 0xD5; //0000,0000 T3低字节
sfr T2H = 0xD6; //0000,0000 T2高字节
sfr T2L = 0xD7; //0000,0000 T2低字节
sfr WKTCL = 0xAA; //0000,0000 掉电唤醒定时器低字节
sfr WKTCH = 0xAB; //0000,0000 掉电唤醒定时器高字节
sfr WDT_CONTR = 0xC1; //0000,0000 看门狗控制寄存器
//串行口特殊功能寄存器
sfr SCON = 0x98; //0000,0000 串口1控制寄存器
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;
sfr SBUF = 0x99; //xxxx,xxxx 串口1数据寄存器
sfr S2CON = 0x9A; //0000,0000 串口2控制寄存器
sfr S2BUF = 0x9B; //xxxx,xxxx 串口2数据寄存器
sfr S3CON = 0xAC; //0000,0000 串口3控制寄存器
sfr S3BUF = 0xAD; //xxxx,xxxx 串口3数据寄存器
sfr S4CON = 0x84; //0000,0000 串口4控制寄存器
sfr S4BUF = 0x85; //xxxx,xxxx 串口4数据寄存器
sfr SADDR = 0xA9; //0000,0000 从机地址寄存器
sfr SADEN = 0xB9; //0000,0000 从机地址屏蔽寄存器
//ADC 特殊功能寄存器
sfr ADC_CONTR = 0xBC; //0000,0000 A/D转换控制寄存器
sfr ADC_RES = 0xBD; //0000,0000 A/D转换结果高8位
sfr ADC_RESL = 0xBE; //0000,0000 A/D转换结果低2位
//SPI 特殊功能寄存器
sfr SPSTAT = 0xCD; //00xx,xxxx SPI状态寄存器
sfr SPCTL = 0xCE; //0000,0100 SPI控制寄存器
sfr SPDAT = 0xCF; //0000,0000 SPI数据寄存器
//IAP/ISP 特殊功能寄存器
sfr IAP_DATA = 0xC2; //0000,0000 EEPROM数据寄存器
sfr IAP_ADDRH = 0xC3; //0000,0000 EEPROM地址高字节
sfr IAP_ADDRL = 0xC4; //0000,0000 EEPROM地址第字节
sfr IAP_CMD = 0xC5; //xxxx,xx00 EEPROM命令寄存器
sfr IAP_TRIG = 0xC6; //0000,0000 EEPRPM命令触发寄存器
sfr IAP_CONTR = 0xC7; //0000,x000 EEPROM控制寄存器
//PCA/PWM 特殊功能寄存器
sfr CCON = 0xD8; //00xx,xx00 PCA控制寄存器
sbit CF = CCON^7;
sbit CR = CCON^6;
sbit CCF2 = CCON^2;
sbit CCF1 = CCON^1;
sbit CCF0 = CCON^0;
sfr CMOD = 0xD9; //0xxx,x000 PCA 工作模式寄存器
sfr CL = 0xE9; //0000,0000 PCA计数器低字节
sfr CH = 0xF9; //0000,0000 PCA计数器高字节
sfr CCAPM0 = 0xDA; //0000,0000 PCA模块0的PWM寄存器
sfr CCAPM1 = 0xDB; //0000,0000 PCA模块1的PWM寄存器
sfr CCAPM2 = 0xDC; //0000,0000 PCA模块2的PWM 寄存器
sfr CCAP0L = 0xEA; //0000,0000 PCA模块0的捕捉/比较寄存器低字节
sfr CCAP1L = 0xEB; //0000,0000 PCA模块1的捕捉/比较寄存器低字节
sfr CCAP2L = 0xEC; //0000,0000 PCA模块2的捕捉/比较寄存器低字节
sfr PCA_PWM0 = 0xF2; //xxxx,xx00 PCA模块0的PWM寄存器
sfr PCA_PWM1 = 0xF3; //xxxx,xx00 PCA模块1的PWM寄存器
sfr PCA_PWM2 = 0xF4; //xxxx,xx00 PCA模块1的PWM寄存器
sfr CCAP0H = 0xFA; //0000,0000 PCA模块0的捕捉/比较寄存器高字节
sfr CCAP1H = 0xFB; //0000,0000 PCA模块1的捕捉/比较寄存器高字节
sfr CCAP2H = 0xFC; //0000,0000 PCA模块2的捕捉/比较寄存器高字节
上一篇:STM32 关闭JTAG 使用相应GPIO口 简单记录
下一篇:STM32 ILI9325 驱动代码 学习总结
推荐阅读最新更新时间:2024-03-16 15:47