📄 interface8051.c
字号:
#include <reg52.h>
#include <stdio.h>
#include "common.h"
void SetBaud(void)
{
u16 BaudSpeed;
u16 Tick;
u8 TickH,TickL;
TickH=0;
TickL=0;
// T2CON=0x01; // Timer2, Timer Mode, Clear TF2
T2CON=0x00;
TH2=0; // Load Timer2 start value
TL2=0;
while(P30); // Wait for Start bit, falling edge
TR2=1; // Start Timer2 (Count-up)
while(!TF2) // While Timer2 not overflow (TF2=1)
{
while( !TF2 && !P30 ); // Wait for rising edge Timer2 overflow
TickH=TH2; // Capture Timer2
TickL=TL2;
while( !TF2 && P30 ); // Wait for falling edge or Timer2 overflow
}
TR2=0; // Stop Timer2
TF2=0; // Clear overflow flag
Tick=(TickH<<8)+TickL; // Tick = duration from start bit to stop bit
BaudSpeed=65536.084-Tick*0.0417372+0.5; // Round to nearest integer
SCON = 0x52; // SCON: Mode 1, 8-bit UART, enable rcvr
RCAP2H= BaudSpeed>>8;
RCAP2L= BaudSpeed&0x00FF;
T2CON = 0x3C; // Timer 2, Mode baud rate generator
}
#if 0
bit breakFromLoop=0;
u16 coinCount=0;
void Int_Timer2(void) interrupt 5 using 0
{
ET2=0;
EXF2=0;
breakFromLoop=1;
TMOD |= 0x01; // Timer 0, Mode 1, 16-bit
TH0=0; // Load Initial value
TL0=0;
TF0=0; // Clear Overflow Flag
TR0=1; // Start Timer0
while(!P11);
TR0=0; // Stop Timer0
if (TF0) {
coinCount++; // Overflow Occur
printf("Coin = %d\n", coinCount);
TF0=0;
}
ET2=1;
}
#endif
#if 0
volatile u8 xdata spisr _at_ 0x2080;
volatile u8 xdata spicr _at_ 0x2084;
volatile u8 xdata spitr _at_ 0x208a;
volatile u8 xdata spirr _at_ 0x208e;
u8 spiStatus=0;
u16 countInInt=0;
bit IntFlag=0;
#endif
volatile u8 xdata dataInReg _at_ 0xa084;
volatile u8 xdata addrZero _at_ 0x0051;
void main(void)
{
u16 i=0;
u8 xdata *x=0;
u16 j;
EA=1;
// EX0=1; //enable interrupt 0
// P33=1;
// P33=0; //Reset CPLD
// P33=1;
P10=1;
P10=1;
for(i=0;i<50;i++);
P10=0;
P10=0;
P10=1;
SetBaud();
// ET2=1; //Enable Timer2 interrupt
printf("RUN\n");
#if 0
P34=1; //CS of MMC much be high
while (spisr&0x20);
printf("Bus Busy\n");
spicr=0xdf;
spitr=0x55; //dummy data for gen 80 clk
spicr=0xff; //start transfer
while(countInInt<3) {
if (IntFlag) spitr=0x55;
IntFlag=0;
}
// while(countInInt<3) spitr=0xff;
spicr=0x00; //stop transfer
// while(1) {
if(spiStatus) printf("SPI Status =%bx\n",spiStatus);
if(countInInt) printf("Enter %u times\n",countInInt);
// }
#endif
// while(1) addrZero=0x00;
for(j=0;j<256;j++) {
printf("j=%d\r",j);
for(i=0x0000;i<0x8000;i++) {
dataInReg=0x51;
x[i]=j;
x[i+1]=j+1;
x[i+2]=j+2;
x[i+3]=j+3;
if(x[i]!=j) {
printf("Memory error at %x\n",i);
}
// getchar();
}
}
// ET2=1; //Enable Timer2 interrupt
}
#if 0
void Int_Ext_0(void) interrupt 0 using 1
{
EX0=0;
spiStatus=spisr;
countInInt++;
if(countInInt) printf("Enter %u times\n",countInInt);
IntFlag=1;
EX0=1;
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -