从商家那买来的RFID模块,用52和他的历程可以驱动
想要移植到自己的STC8H1K28上,按理来说,改变串口的初始化就可以实现
但不知道为什么总是不行。恳请各位指点
①商家代码C52单片机的#include "reg52.h"
sbit LED1 = P2^0;
sbit LED2 = P2^1;
typedef unsigned int u16; //对系统默认数据类型进行重定义
typedef unsigned char u8;
u8 rxdatabuf[32];
u8 rxdatacnt;
/*******************************************************************************
* 函 数 名 : uart_init
* 函数功能 : 串口通信中断配置函数,通过设置TH和TL即可确定定时时间
* 输 入 : baud:波特率对应的TH、TL装载值
* 输 出 : 无
*******************************************************************************/
void uart_init(u8 baud)
{
TMOD|=0X20; //设置计数器工作方式2
SCON=0X50; //设置为工作方式1
PCON=0X80; //波特率加倍
TH1=baud; //计数器初始值设置
TL1=baud;
ES=1; //打开接收中断
EA=1; //打开总中断
TR1=1; //打开计数器
}
//void sendchar(u8 ch)
//{
// ES=0;
// TI=0;
// SBUF = ch;
// while(!TI); //等待发送数据完成
// TI=0; //清除发送完成标志位
// ES=1;
//}
/*******************************************************************************
* 函 数 名 : delay_10us
* 函数功能 : 延时函数,ten_us=1时,大约延时10us
* 输 入 : ten_us
* 输 出 : 无
*******************************************************************************/
void delay_10us(u16 ten_us)
{
while(ten_us--);
}
unsigned char CheckSum(unsigned char *ptr,unsigned char len)
{
unsigned char i;
unsigned char checksum;
checksum = 0;
for(i=0;i<(len-1);i++)
{
checksum ^= ptr[ i];
}
if(ptr[len-1] == (~checksum))
return 0x01;
else
return 0x00;
}
void clean_rxdatabuf(void)
{
u8 i = 0;
rxdatacnt = 0;
for(i=0;i<32;i++)
{
rxdatabuf[ i] = 0;
}
}
/*******************************************************************************
* 函 数 名 : main
* 函数功能 : 主函数
* 输 入 : 无
* 输 出 : 无
*******************************************************************************/
void main()
{
u8 i;
u8 cardid[4];
rxdatacnt = 0;
uart_init(0xFA);//波特率为9600
LED1 = 1;
LED2 = 0;
delay_10us(5000);
LED1 = 0;
LED2 = 1;
while(1)
{
if(rxdatacnt > 0) //判断串口是否收到数据
{
LED2 = 0; //接收到数据闪LED2
delay_10us(2000);//等待串口接收完毕
LED2 = 1;
if(rxdatacnt >= 12) //判断是否收到一帧数据,自动读卡号读卡器送的数据包长度为12字节
{
//rxdatabuf数组中接收到的数据格式应该如下
//rxdatabuf[0]包类型,0x04表示自动读卡返回的数据包
//rxdatabuf[1]包长度,自动读卡号返回的数据包长度为12字节
//rxdatabuf[2]命令,0x02表示自动读卡号,0x03表示自动读数据块,0x04表示自动读卡号+数据块
//rxdatabuf[3]读卡器地址,默认0x20
//rxdatabuf[4]固定值0x00
//rxdatabuf[5],rxdatabuf[6]这两个字节保存的是卡类型,比如04 00表示 M1 S50卡,02 00 表示M1 S70卡
//rxdatabuf[7],rxdatabuf[8],rxdatabuf[9],rxdatabuf[10]这个4个字节存储的是卡号
//rxdatabuf[11]数据包校验值,计算方式参考手册的校验和计算方方法或者参考本例子代码CheckSum();
if((rxdatabuf[0] == 0x04)&&(rxdatabuf[1] == 12)&&(rxdatabuf[2] == 0x02)&&(rxdatabuf[3] == 0x20)&&(rxdatabuf[4] == 0x00))//判断是否为读卡器返回的数据包
{
if(CheckSum(rxdatabuf,12)) //判断检验是否正确,正确返回0x01,错误返回0x00
{
//获取卡号
for(i=0;i<4;i++)
{
cardid[ i] = rxdatabuf[7 + i]; //将rxdatabuf数组中的4字节卡号复制到数组cardid[]中
}
LED1 = 1; //熄灭LED
delay_10us(50000);
LED1 = 0; //点亮LED
}
}
}
clean_rxdatabuf();
}
}
}
void uart() interrupt 4 //串口通信中断函数
{
RI = 0; //清除接收中断标志位
rxdatabuf[rxdatacnt] = SBUF; //存储接收到的数据
rxdatacnt++;
if(rxdatacnt >= 32) //RxDataBuf数组最大存放32字节数据,防止数组溢出
{
rxdatacnt = 0;
}
}
②我移植的代码#include "STC8.h"
#include "intrins.h"
#define FOSC 11059200UL
#define BRT (256 - FOSC / 9600 / 32)
sbit LED1 = P2^0;
sbit LED2 = P2^1;
typedef unsigned int u16; //对系统默认数据类型进行重定义
typedef unsigned char u8;
u8 i;
u8 cardid[4];
bit busy;
u8 rxdatacnt;
char rptr;
char rxdatabuf[32];
void delay_10us(u16 ten_us)
{
while(ten_us--);
}
unsigned char CheckSum(unsigned char *ptr,unsigned char len)
{
unsigned char i;
unsigned char checksum;
checksum = 0;
for(i=0;i<(len-1);i++)
{
checksum ^= ptr[ i];
}
if(ptr[len-1] == (~checksum))
return 0x01;
else
return 0x00;
}
void clean_rxdatabuf(void)
{
u8 i = 0;
rxdatacnt = 0;
for(i=0;i<32;i++)
{
rxdatabuf[ i] = 0;
}
}
void UartIsr() interrupt 4
{
if (TI)
{
TI = 0;
busy = 0;
}
if (RI)
{
rxdatabuf[rxdatacnt++] =SBUF;
if( rxdatacnt >=13){
RI = 0;}
if(rxdatacnt >= 32) //RxDataBuf数组最大存放32字节数据,防止数组溢出
{
rxdatacnt = 0;
}
}
}
void UartInit()
{
SCON = 0x50;
TMOD = 0x20;
TL1 = BRT;
TH1 = BRT;
TR1 = 1;
AUXR = 0x40;
rxdatacnt = 0x00;
rptr = 0x00;
busy = 0;
}
void main()
{
P0M0 = 0x00;
P0M1 = 0x00;
P1M0 = 0x00;
P1M1 = 0x00;
P2M0 = 0x00;
P2M1 = 0x00;
P3M0 = 0x00;
P3M1 = 0x00;
P4M0 = 0x00;
P4M1 = 0x00;
P5M0 = 0x00;
P5M1 = 0x00;
UartInit();
ES = 1;
EA = 1;
LED1 = 0;
while (1)
{
if(rxdatacnt > 0) //判断串口是否收到数据
{
LED1 = 1;
delay_10us(1000);
if (rxdatacnt >= 12) //判断是否收到一帧数据,自动读卡号读卡器送的数据包长度为12字节
{
LED2 = 0;
if((rxdatabuf[0] == 0x04)&&(rxdatabuf[1] ==0X0C)&&(rxdatabuf[2] == 0x02)&&(rxdatabuf[3] == 0x20)&&(rxdatabuf[4] == 0x00))//判断是否为读卡器返回的数据包
{
LED2 = 1;
if(CheckSum(rxdatabuf,12)) //判断检验是否正确,正确返回0x01,错误返回0x00
{
//获取卡号
for(i=0;i<4;i++)
{
cardid[ i] = rxdatabuf[7 + i]; //将rxdatabuf数组中的4字节卡号复制到数组cardid[]中
}
}
}
}
clean_rxdatabuf();
}
}
}
最后通过LED灯的状态发现他卡在LED2=0;
不知道为什么,是数据不对还是?
我试过把接收到的数据传到串口助手上看,数据时而是对的,时而都是000000
|