|
50黑幣
從商家那買來(lái)的RFID模塊,用52和他的歷程可以驅(qū)動(dòng)
想要移植到自己的STC8H1K28上,按理來(lái)說(shuō),改變串口的初始化就可以實(shí)現(xiàn)
但不知道為什么總是不行。懇請(qǐng)各位指點(diǎn)
①商家代碼C52單片機(jī)的#include "reg52.h"
sbit LED1 = P2^0;
sbit LED2 = P2^1;
typedef unsigned int u16; //對(duì)系統(tǒng)默認(rèn)數(shù)據(jù)類型進(jìn)行重定義
typedef unsigned char u8;
u8 rxdatabuf[32];
u8 rxdatacnt;
/*******************************************************************************
* 函 數(shù) 名 : uart_init
* 函數(shù)功能 : 串口通信中斷配置函數(shù),通過(guò)設(shè)置TH和TL即可確定定時(shí)時(shí)間
* 輸 入 : baud:波特率對(duì)應(yīng)的TH、TL裝載值
* 輸 出 : 無(wú)
*******************************************************************************/
void uart_init(u8 baud)
{
TMOD|=0X20; //設(shè)置計(jì)數(shù)器工作方式2
SCON=0X50; //設(shè)置為工作方式1
PCON=0X80; //波特率加倍
TH1=baud; //計(jì)數(shù)器初始值設(shè)置
TL1=baud;
ES=1; //打開接收中斷
EA=1; //打開總中斷
TR1=1; //打開計(jì)數(shù)器
}
//void sendchar(u8 ch)
//{
// ES=0;
// TI=0;
// SBUF = ch;
// while(!TI); //等待發(fā)送數(shù)據(jù)完成
// TI=0; //清除發(fā)送完成標(biāo)志位
// ES=1;
//}
/*******************************************************************************
* 函 數(shù) 名 : delay_10us
* 函數(shù)功能 : 延時(shí)函數(shù),ten_us=1時(shí),大約延時(shí)10us
* 輸 入 : ten_us
* 輸 出 : 無(wú)
*******************************************************************************/
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;
}
}
/*******************************************************************************
* 函 數(shù) 名 : main
* 函數(shù)功能 : 主函數(shù)
* 輸 入 : 無(wú)
* 輸 出 : 無(wú)
*******************************************************************************/
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) //判斷串口是否收到數(shù)據(jù)
{
LED2 = 0; //接收到數(shù)據(jù)閃LED2
delay_10us(2000);//等待串口接收完畢
LED2 = 1;
if(rxdatacnt >= 12) //判斷是否收到一幀數(shù)據(jù),自動(dòng)讀卡號(hào)讀卡器送的數(shù)據(jù)包長(zhǎng)度為12字節(jié)
{
//rxdatabuf數(shù)組中接收到的數(shù)據(jù)格式應(yīng)該如下
//rxdatabuf[0]包類型,0x04表示自動(dòng)讀卡返回的數(shù)據(jù)包
//rxdatabuf[1]包長(zhǎng)度,自動(dòng)讀卡號(hào)返回的數(shù)據(jù)包長(zhǎng)度為12字節(jié)
//rxdatabuf[2]命令,0x02表示自動(dòng)讀卡號(hào),0x03表示自動(dòng)讀數(shù)據(jù)塊,0x04表示自動(dòng)讀卡號(hào)+數(shù)據(jù)塊
//rxdatabuf[3]讀卡器地址,默認(rèn)0x20
//rxdatabuf[4]固定值0x00
//rxdatabuf[5],rxdatabuf[6]這兩個(gè)字節(jié)保存的是卡類型,比如04 00表示 M1 S50卡,02 00 表示M1 S70卡
//rxdatabuf[7],rxdatabuf[8],rxdatabuf[9],rxdatabuf[10]這個(gè)4個(gè)字節(jié)存儲(chǔ)的是卡號(hào)
//rxdatabuf[11]數(shù)據(jù)包校驗(yàn)值,計(jì)算方式參考手冊(cè)的校驗(yàn)和計(jì)算方方法或者參考本例子代碼CheckSum();
if((rxdatabuf[0] == 0x04)&&(rxdatabuf[1] == 12)&&(rxdatabuf[2] == 0x02)&&(rxdatabuf[3] == 0x20)&&(rxdatabuf[4] == 0x00))//判斷是否為讀卡器返回的數(shù)據(jù)包
{
if(CheckSum(rxdatabuf,12)) //判斷檢驗(yàn)是否正確,正確返回0x01,錯(cuò)誤返回0x00
{
//獲取卡號(hào)
for(i=0;i<4;i++)
{
cardid[ i] = rxdatabuf[7 + i]; //將rxdatabuf數(shù)組中的4字節(jié)卡號(hào)復(fù)制到數(shù)組cardid[]中
}
LED1 = 1; //熄滅LED
delay_10us(50000);
LED1 = 0; //點(diǎn)亮LED
}
}
}
clean_rxdatabuf();
}
}
}
void uart() interrupt 4 //串口通信中斷函數(shù)
{
RI = 0; //清除接收中斷標(biāo)志位
rxdatabuf[rxdatacnt] = SBUF; //存儲(chǔ)接收到的數(shù)據(jù)
rxdatacnt++;
if(rxdatacnt >= 32) //RxDataBuf數(shù)組最大存放32字節(jié)數(shù)據(jù),防止數(shù)組溢出
{
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; //對(duì)系統(tǒng)默認(rèn)數(shù)據(jù)類型進(jìn)行重定義
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數(shù)組最大存放32字節(jié)數(shù)據(jù),防止數(shù)組溢出
{
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) //判斷串口是否收到數(shù)據(jù)
{
LED1 = 1;
delay_10us(1000);
if (rxdatacnt >= 12) //判斷是否收到一幀數(shù)據(jù),自動(dòng)讀卡號(hào)讀卡器送的數(shù)據(jù)包長(zhǎng)度為12字節(jié)
{
LED2 = 0;
if((rxdatabuf[0] == 0x04)&&(rxdatabuf[1] ==0X0C)&&(rxdatabuf[2] == 0x02)&&(rxdatabuf[3] == 0x20)&&(rxdatabuf[4] == 0x00))//判斷是否為讀卡器返回的數(shù)據(jù)包
{
LED2 = 1;
if(CheckSum(rxdatabuf,12)) //判斷檢驗(yàn)是否正確,正確返回0x01,錯(cuò)誤返回0x00
{
//獲取卡號(hào)
for(i=0;i<4;i++)
{
cardid[ i] = rxdatabuf[7 + i]; //將rxdatabuf數(shù)組中的4字節(jié)卡號(hào)復(fù)制到數(shù)組cardid[]中
}
}
}
}
clean_rxdatabuf();
}
}
}
最后通過(guò)LED燈的狀態(tài)發(fā)現(xiàn)他卡在LED2=0;
不知道為什么,是數(shù)據(jù)不對(duì)還是?
我試過(guò)把接收到的數(shù)據(jù)傳到串口助手上看,數(shù)據(jù)時(shí)而是對(duì)的,時(shí)而都是000000
|
最佳答案
查看完整內(nèi)容
兩者速度不同,端口模式寄存器、定時(shí)器工作模式寄存器也有所不同,主要修改這三項(xiàng)即可。
|