📄 jjdogrc.c
字号:
#include <stdio.h>
#include "cygnal\c8051f330.h"
#define SYSCLCK 3062500
#define BAUDRATE 9600
sfr16 T3 = 0x94;
sfr16 T3RL = 0x92;
sbit inA = P1^0;
sbit inB = P1^1;
sbit inC = P1^2;
sbit OutA = P1^3;
sbit OutB = P1^4;
sbit OutC = P1^5;
sbit LED1 = P1^6;
sbit LED2 = P1^7;
unsigned char tempBuffer[20];
unsigned char xdata PrintBuffer[128];
unsigned char p_PB_end, p_PB_this;
unsigned int t3delay;
bit errflag;
void Init_Device(void)
{
int i = 0;
unsigned long sclk = SYSCLCK;
PCA0MD &= ~0x40;
PCA0MD = 0x00; //禁用看门狗
P0 = 0xF3; //将晶振引脚置低
P0MDIN = 0xF3;
P0SKIP = 0x0C;
XBR0 = 0x01;
XBR1 = 0x40;
/* OSCXCN = 0x67;
for (i = 0; i < 3000; i++); // Wait 1ms for initialization
while ((OSCXCN & 0x80) == 0);
CLKSEL = 0x01;
*/
switch(sclk)
{
case 24500000:
OSCICN = 0x83;
break;
case 12250000:
OSCICN = 0x82;
break;
case 6125000:
OSCICN = 0x81;
break;
case 3062500:
default:
OSCICN = 0x80;
break;
}
VDM0CN = 0x80;
for (i = 0; i < 350; i++); // Wait 100us for initialization
RSTSRC = 0x06; //内部电压监视,时钟丢失检测
//OSCICN = 0x00; //禁止内部时钟
EA = 1;
}
void WriteBuffer(unsigned char n, char *Buffer)
{ //Buffer总长为128
unsigned char p_PB_temp,i;
p_PB_temp = p_PB_end;
for(i=0;i<n;i++)
{
*(PrintBuffer + p_PB_temp) = *(Buffer + i);
p_PB_temp++;
if(p_PB_temp > 127)
p_PB_temp = 0;
}
if(p_PB_end == p_PB_this)
{
p_PB_end = p_PB_temp;
SBUF0 = *(PrintBuffer + p_PB_this);
}
else
p_PB_end = p_PB_temp;
}
void UART0_Init(unsigned int baud)
{
unsigned long sclk = SYSCLCK;
TMOD = 0x20;
switch(sclk)
{
case 3062500:
switch(baud) //sysclk/2/分频数/baud
{
case 4800: //系统时钟4分频,4785
CKCON = 0x01;
TH1 = -80;
break;
case 19200: //系统时钟4分频,19140
CKCON = 0x01;
TH1 = -20;
break;
case 9600: //系统时钟4分频,9570
default: //缺省为9600
CKCON = 0x01; //外部晶振8分频
TH1 = -40; //9600;
break;
}
break;
case 6125000:
switch(baud) //sysclk/2/分频数/baud
{
case 4800: //系统时钟4分频,4785
CKCON = 0x01;
TH1 = -160;
break;
case 19200: //系统时钟4分频,19140
CKCON = 0x01;
TH1 = -40;
break;
case 9600: //系统时钟4分频,9570
default: //缺省为9600
CKCON = 0x01; //外部晶振8分频
TH1 = -80; //9600;
break;
}
break;
case 12250000:
switch(baud) //sysclk/2/分频数/baud
{
case 4800: //系统时钟12分频,4770
CKCON = 0;
TH1 = -107;
break;
case 19200: //系统时钟4分频,19140
CKCON = 0x01;
TH1 = -80;
break;
case 9600: //系统时钟4分频,9570
default: //缺省为9600
CKCON = 0x01;
TH1 = -160; //9600;
break;
}
break;
case 24500000:
default:
switch(baud) //sysclk/2/分频数/baud
{
case 4800: //系统时钟12分频,4770
CKCON = 0;
TH1 = -214;
break;
case 19200: //系统时钟4分频,19140
CKCON = 0x01;
TH1 = -160;
break;
case 9600: //系统时钟12分频,9570
default: //缺省为9600
CKCON = 0;
TH1 = -107; //9540;
break;
}
break;
}
TL1 = TH1;
TR1 = 1;
SCON0 = 0x10;
ES0 = 1;
}
void Timer3_Init(unsigned char ms) //定时长ms
{
if(ms>250)
ms = 250;
T3RL = -SYSCLCK/12/1000*ms;//100HZ
T3 = T3RL;
TMR3CN = 0x04; //使用内部晶振的12分频
EIE1 |= 0x80; //定时器3中断允许
}
void Timer3_ISR(void) interrupt 14 //启动发送数据
{
TMR3CN &= 0x7F; //清中断标志
TMR3CN = 0x00; //禁止定时器
LED2 = ~LED2; //工作指示灯
P1 |= 0x3F;
/* InA = 1;
InB = 1;
InC = 1;
OutA = 1;
OutB = 1;
OutC = 1;
*/
if(t3delay)
t3delay--;
}
void UART0_ISR(void) interrupt 4
{
static unsigned char Buf[5];
static unsigned char c,in,out;
if(TI0)
{ TI0=0;
p_PB_this++;
if(p_PB_this > 127)
p_PB_this = 0;
if(p_PB_this != p_PB_end)
SBUF0 = *(PrintBuffer + p_PB_this);
}
if(RI0)
{ RI0 = 0;
c = SBUF0;
Buf[0] = Buf[1];
Buf[1] = Buf[2];
Buf[2] = Buf[3];
Buf[3] = Buf[4];
Buf[4] = c;
if(Buf[0] == 0xFF && Buf[3] == 0xFE && Buf[4] == 0xFD)
{
in = Buf[1] - 8;
in &= 0x07;
out = Buf[2] - 1;
out &= 0x07;
if(in>6 || out>6)
{
errflag = 1;
}
else
{
out <<= 3;
out |= in;
in = P1 & 0xC0;
out |= in;
P1 = out;
LED1 = ~LED1;
Timer3_Init(50);
}
}
// Timer3_Init(100);
}
}
void main(void)
{
unsigned int i;
t3delay= 0;
errflag =0;
p_PB_this =0;
p_PB_end =0;
Init_Device();
UART0_Init(BAUDRATE);
Timer3_Init(10);
while(1)
{
if(errflag)
{
errflag = 0;
i = sprintf(tempBuffer,"Err Input!");
WriteBuffer(i,tempBuffer);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -