Chào cả nhà, dựa theo bài viết trên, e đã viết code cho con pic877a như sau:
Code:
#include <16F877A.H>
#include <def_877a.h>
#FUSES NOWDT, HS, PUT, NOPROTECT, NODEBUG, BROWNOUT, NOLVP, NOCPD, NOWRT
#use delay(clock=4000000)
#define PT_MCU_CLOCK getenv("CLOCK")
#define PT_SPEED (9600)
#define PT_MAX_FRAME_CHECK 2
#define PT_TX_PIN PIN_B0
#define PT_RX_PIN PIN_B1
#define PT_TIMER (((((PT_MCU_CLOCK/4)/8)/(PT_SPEED/4))))
#define PT_BIT_0 0b00010001
#define PT_BIT_1 0b01110111
#define PT_BIT_F 0b01110001
#define PT_SENDING 0
#define PT_IDLE 1
const char PT_BIT_TABLE[3]={PT_BIT_0,PT_BIT_1,PT_BIT_F};
signed long PT_TX_TIMER = -(PT_TIMER/1);
unsigned long TIME_0a_75_BIT = (float)(PT_TIMER)*(float)0.75;
unsigned long TIME_1a_25_BIT = (float)(PT_TIMER)*(float)1.25;
unsigned long TIME_2a_75_BIT = (float)(PT_TIMER)*(float)2.75;
unsigned long TIME_3a_25_BIT = (float)(PT_TIMER)*(float)3.25;
unsigned long TIME_30a_75_BIT = (float)(PT_TIMER)*(float)30.75;
unsigned long TIME_31a_25_BIT = (float)(PT_TIMER)*(float)31.25;
unsigned long data, addr;
unsigned int Pt2262BuffCurrent, Pt2262BuffCounter, Pt2262LoopCounter;
char PtBuff[16];
int1 PtState;
void Pt2262TxCheck()
{
unsigned short b;
if (Pt2262BuffCurrent!=Pt2262BuffCounter)
{
b = PtBuff[Pt2262BuffCurrent>>3]>>(Pt2262BuffCurrent&0x07);
output_bit(PT_TX_PIN,b);
Pt2262BuffCurrent++;
}
if (Pt2262BuffCurrent==Pt2262BuffCounter)
{
if (--Pt2262LoopCounter>0)
{
Pt2262BuffCurrent=0;
}
else
{
Pt2262BuffCurrent=0;
Pt2262LoopCounter = 4;
output_low(PT_TX_PIN);
clear_interrupt(INT_EXT);
enable_interrupts(INT_EXT);
PtState = PT_IDLE;
}
}
}
#int_timer1
void Timer1_ISR()
{
set_timer1(PT_TX_TIMER);
if(PtState==PT_SENDING)
{
Pt2262TxCheck();
}
else
{
PtState=PT_IDLE;
}
}
void main()
{
int i;
set_tris_b(0b00000010);
portB=0x00;
addr = 22222222;
data = 1111;
for (i=0;i<8;i++)
{
PtBuff[i] = PT_BIT_TABLE[(addr%10)%3];
addr = addr/10;
}
PtBuff[8] = ((data>>3)&0x01)?PT_BIT_1:PT_BIT_0;
PtBuff[9] = ((data>>2)&0x01)?PT_BIT_1:PT_BIT_0;
PtBuff[10] = ((data>>1)&0x01)?PT_BIT_1:PT_BIT_0;
PtBuff[11] = ((data>>0)&0x01)?PT_BIT_1:PT_BIT_0;
PtBuff[12] = 0b00000001;
PtBuff[13] = PtBuff[14] = PtBuff[15] = 0b00000000;
PtState = PT_IDLE;
enable_interrupts(global);
enable_interrupts(int_timer1);
setup_timer_1(T1_INTERNAL | T1_DIV_BY_1);
set_timer1(PT_TX_TIMER);
Pt2262BuffCurrent=0;
Pt2262BuffCounter = 16*8;
Pt2262LoopCounter = 4;
while(1)
{
PtState = PT_SENDING;
}
}
không biết sai sót chỗ nào mà mạch thu không nhận được tín hiệu, pic dùng thạch anh 4MHz, mạch thu dùng pt2272, Rosc = 820K. Ai giúp e với, thk.